clang  3.9.0
Marshallers.h
Go to the documentation of this file.
1 //===--- Marshallers.h - Generic matcher function marshallers ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Functions templates and classes to wrap matcher construct functions.
12 ///
13 /// A collection of template function and classes that provide a generic
14 /// marshalling layer on top of matcher construct functions.
15 /// These are used by the registry to export all marshaller constructors with
16 /// the same generic interface.
17 ///
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H
21 #define LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H
22 
26 #include "clang/Basic/LLVM.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include <string>
29 
30 namespace clang {
31 namespace ast_matchers {
32 namespace dynamic {
33 namespace internal {
34 
35 /// \brief Helper template class to just from argument type to the right is/get
36 /// functions in VariantValue.
37 /// Used to verify and extract the matcher arguments below.
38 template <class T> struct ArgTypeTraits;
39 template <class T> struct ArgTypeTraits<const T &> : public ArgTypeTraits<T> {
40 };
41 
42 template <> struct ArgTypeTraits<std::string> {
43  static bool is(const VariantValue &Value) { return Value.isString(); }
44  static const std::string &get(const VariantValue &Value) {
45  return Value.getString();
46  }
47  static ArgKind getKind() {
49  }
50 };
51 
52 template <>
53 struct ArgTypeTraits<StringRef> : public ArgTypeTraits<std::string> {
54 };
55 
56 template <class T> struct ArgTypeTraits<ast_matchers::internal::Matcher<T> > {
57  static bool is(const VariantValue &Value) {
58  return Value.isMatcher() && Value.getMatcher().hasTypedMatcher<T>();
59  }
60  static ast_matchers::internal::Matcher<T> get(const VariantValue &Value) {
61  return Value.getMatcher().getTypedMatcher<T>();
62  }
63  static ArgKind getKind() {
64  return ArgKind(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
65  }
66 };
67 
68 template <> struct ArgTypeTraits<unsigned> {
69  static bool is(const VariantValue &Value) { return Value.isUnsigned(); }
70  static unsigned get(const VariantValue &Value) {
71  return Value.getUnsigned();
72  }
73  static ArgKind getKind() {
75  }
76 };
77 
78 template <> struct ArgTypeTraits<attr::Kind> {
79 private:
80  static attr::Kind getAttrKind(llvm::StringRef AttrKind) {
81  return llvm::StringSwitch<attr::Kind>(AttrKind)
82 #define ATTR(X) .Case("attr::" #X, attr:: X)
83 #include "clang/Basic/AttrList.inc"
84  .Default(attr::Kind(-1));
85  }
86 public:
87  static bool is(const VariantValue &Value) {
88  return Value.isString() &&
89  getAttrKind(Value.getString()) != attr::Kind(-1);
90  }
91  static attr::Kind get(const VariantValue &Value) {
92  return getAttrKind(Value.getString());
93  }
94  static ArgKind getKind() {
96  }
97 };
98 
99 template <> struct ArgTypeTraits<clang::CastKind> {
100 private:
101  static clang::CastKind getCastKind(llvm::StringRef AttrKind) {
102  return llvm::StringSwitch<clang::CastKind>(AttrKind)
103 #define CAST_OPERATION(Name) .Case( #Name, CK_##Name)
104 #include "clang/AST/OperationKinds.def"
105  .Default(CK_Invalid);
106  }
107 
108 public:
109  static bool is(const VariantValue &Value) {
110  return Value.isString() &&
111  getCastKind(Value.getString()) != CK_Invalid;
112  }
113  static clang::CastKind get(const VariantValue &Value) {
114  return getCastKind(Value.getString());
115  }
116  static ArgKind getKind() {
117  return ArgKind(ArgKind::AK_String);
118  }
119 };
120 
121 /// \brief Matcher descriptor interface.
122 ///
123 /// Provides a \c create() method that constructs the matcher from the provided
124 /// arguments, and various other methods for type introspection.
126 public:
127  virtual ~MatcherDescriptor() {}
128  virtual VariantMatcher create(SourceRange NameRange,
130  Diagnostics *Error) const = 0;
131 
132  /// Returns whether the matcher is variadic. Variadic matchers can take any
133  /// number of arguments, but they must be of the same type.
134  virtual bool isVariadic() const = 0;
135 
136  /// Returns the number of arguments accepted by the matcher if not variadic.
137  virtual unsigned getNumArgs() const = 0;
138 
139  /// Given that the matcher is being converted to type \p ThisKind, append the
140  /// set of argument types accepted for argument \p ArgNo to \p ArgKinds.
141  // FIXME: We should provide the ability to constrain the output of this
142  // function based on the types of other matcher arguments.
143  virtual void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
144  std::vector<ArgKind> &ArgKinds) const = 0;
145 
146  /// Returns whether this matcher is convertible to the given type. If it is
147  /// so convertible, store in *Specificity a value corresponding to the
148  /// "specificity" of the converted matcher to the given context, and in
149  /// *LeastDerivedKind the least derived matcher kind which would result in the
150  /// same matcher overload. Zero specificity indicates that this conversion
151  /// would produce a trivial matcher that will either always or never match.
152  /// Such matchers are excluded from code completion results.
153  virtual bool isConvertibleTo(
154  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity = nullptr,
155  ast_type_traits::ASTNodeKind *LeastDerivedKind = nullptr) const = 0;
156 
157  /// Returns whether the matcher will, given a matcher of any type T, yield a
158  /// matcher of type T.
159  virtual bool isPolymorphic() const { return false; }
160 };
161 
164  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
165  ast_type_traits::ASTNodeKind *LeastDerivedKind) {
166  for (const ast_type_traits::ASTNodeKind &NodeKind : RetKinds) {
167  if (ArgKind(NodeKind).isConvertibleTo(Kind, Specificity)) {
168  if (LeastDerivedKind)
169  *LeastDerivedKind = NodeKind;
170  return true;
171  }
172  }
173  return false;
174 }
175 
176 /// \brief Simple callback implementation. Marshaller and function are provided.
177 ///
178 /// This class wraps a function of arbitrary signature and a marshaller
179 /// function into a MatcherDescriptor.
180 /// The marshaller is in charge of taking the VariantValue arguments, checking
181 /// their types, unpacking them and calling the underlying function.
183 public:
184  typedef VariantMatcher (*MarshallerType)(void (*Func)(),
185  StringRef MatcherName,
186  SourceRange NameRange,
188  Diagnostics *Error);
189 
190  /// \param Marshaller Function to unpack the arguments and call \c Func
191  /// \param Func Matcher construct function. This is the function that
192  /// compile-time matcher expressions would use to create the matcher.
193  /// \param RetKinds The list of matcher types to which the matcher is
194  /// convertible.
195  /// \param ArgKinds The types of the arguments this matcher takes.
197  MarshallerType Marshaller, void (*Func)(), StringRef MatcherName,
199  ArrayRef<ArgKind> ArgKinds)
200  : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName),
201  RetKinds(RetKinds.begin(), RetKinds.end()),
202  ArgKinds(ArgKinds.begin(), ArgKinds.end()) {}
203 
206  Diagnostics *Error) const override {
207  return Marshaller(Func, MatcherName, NameRange, Args, Error);
208  }
209 
210  bool isVariadic() const override { return false; }
211  unsigned getNumArgs() const override { return ArgKinds.size(); }
212  void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
213  std::vector<ArgKind> &Kinds) const override {
214  Kinds.push_back(ArgKinds[ArgNo]);
215  }
217  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
218  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
219  return isRetKindConvertibleTo(RetKinds, Kind, Specificity,
220  LeastDerivedKind);
221  }
222 
223 private:
224  const MarshallerType Marshaller;
225  void (* const Func)();
226  const std::string MatcherName;
227  const std::vector<ast_type_traits::ASTNodeKind> RetKinds;
228  const std::vector<ArgKind> ArgKinds;
229 };
230 
231 /// \brief Helper methods to extract and merge all possible typed matchers
232 /// out of the polymorphic object.
233 template <class PolyMatcher>
234 static void mergePolyMatchers(const PolyMatcher &Poly,
235  std::vector<DynTypedMatcher> &Out,
236  ast_matchers::internal::EmptyTypeList) {}
237 
238 template <class PolyMatcher, class TypeList>
239 static void mergePolyMatchers(const PolyMatcher &Poly,
240  std::vector<DynTypedMatcher> &Out, TypeList) {
241  Out.push_back(ast_matchers::internal::Matcher<typename TypeList::head>(Poly));
242  mergePolyMatchers(Poly, Out, typename TypeList::tail());
243 }
244 
245 /// \brief Convert the return values of the functions into a VariantMatcher.
246 ///
247 /// There are 2 cases right now: The return value is a Matcher<T> or is a
248 /// polymorphic matcher. For the former, we just construct the VariantMatcher.
249 /// For the latter, we instantiate all the possible Matcher<T> of the poly
250 /// matcher.
251 static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher) {
252  return VariantMatcher::SingleMatcher(Matcher);
253 }
254 
255 template <typename T>
256 static VariantMatcher outvalueToVariantMatcher(const T &PolyMatcher,
257  typename T::ReturnTypes * =
258  nullptr) {
259  std::vector<DynTypedMatcher> Matchers;
260  mergePolyMatchers(PolyMatcher, Matchers, typename T::ReturnTypes());
261  VariantMatcher Out = VariantMatcher::PolymorphicMatcher(std::move(Matchers));
262  return Out;
263 }
264 
265 template <typename T>
267  std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
268  RetTypes.push_back(
269  ast_type_traits::ASTNodeKind::getFromNodeKind<typename T::head>());
270  buildReturnTypeVectorFromTypeList<typename T::tail>(RetTypes);
271 }
272 
273 template <>
274 inline void
275 buildReturnTypeVectorFromTypeList<ast_matchers::internal::EmptyTypeList>(
276  std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {}
277 
278 template <typename T>
280  static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
281  buildReturnTypeVectorFromTypeList<typename T::ReturnTypes>(RetTypes);
282  }
283 };
284 
285 template <typename T>
286 struct BuildReturnTypeVector<ast_matchers::internal::Matcher<T> > {
287  static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
288  RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
289  }
290 };
291 
292 template <typename T>
293 struct BuildReturnTypeVector<ast_matchers::internal::BindableMatcher<T> > {
294  static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
295  RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
296  }
297 };
298 
299 /// \brief Variadic marshaller function.
300 template <typename ResultT, typename ArgT,
301  ResultT (*Func)(ArrayRef<const ArgT *>)>
303 variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange,
304  ArrayRef<ParserValue> Args, Diagnostics *Error) {
305  ArgT **InnerArgs = new ArgT *[Args.size()]();
306 
307  bool HasError = false;
308  for (size_t i = 0, e = Args.size(); i != e; ++i) {
309  typedef ArgTypeTraits<ArgT> ArgTraits;
310  const ParserValue &Arg = Args[i];
311  const VariantValue &Value = Arg.Value;
312  if (!ArgTraits::is(Value)) {
313  Error->addError(Arg.Range, Error->ET_RegistryWrongArgType)
314  << (i + 1) << ArgTraits::getKind().asString() << Value.getTypeAsString();
315  HasError = true;
316  break;
317  }
318  InnerArgs[i] = new ArgT(ArgTraits::get(Value));
319  }
320 
321  VariantMatcher Out;
322  if (!HasError) {
323  Out = outvalueToVariantMatcher(Func(llvm::makeArrayRef(InnerArgs,
324  Args.size())));
325  }
326 
327  for (size_t i = 0, e = Args.size(); i != e; ++i) {
328  delete InnerArgs[i];
329  }
330  delete[] InnerArgs;
331  return Out;
332 }
333 
334 /// \brief Matcher descriptor for variadic functions.
335 ///
336 /// This class simply wraps a VariadicFunction with the right signature to export
337 /// it as a MatcherDescriptor.
338 /// This allows us to have one implementation of the interface for as many free
339 /// functions as we want, reducing the number of symbols and size of the
340 /// object file.
342 public:
343  typedef VariantMatcher (*RunFunc)(StringRef MatcherName,
344  SourceRange NameRange,
346  Diagnostics *Error);
347 
348  template <typename ResultT, typename ArgT,
349  ResultT (*F)(ArrayRef<const ArgT *>)>
351  ast_matchers::internal::VariadicFunction<ResultT, ArgT, F> Func,
352  StringRef MatcherName)
353  : Func(&variadicMatcherDescriptor<ResultT, ArgT, F>),
354  MatcherName(MatcherName.str()),
355  ArgsKind(ArgTypeTraits<ArgT>::getKind()) {
357  }
358 
361  Diagnostics *Error) const override {
362  return Func(MatcherName, NameRange, Args, Error);
363  }
364 
365  bool isVariadic() const override { return true; }
366  unsigned getNumArgs() const override { return 0; }
367  void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
368  std::vector<ArgKind> &Kinds) const override {
369  Kinds.push_back(ArgsKind);
370  }
372  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
373  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
374  return isRetKindConvertibleTo(RetKinds, Kind, Specificity,
375  LeastDerivedKind);
376  }
377 
378 private:
379  const RunFunc Func;
380  const std::string MatcherName;
381  std::vector<ast_type_traits::ASTNodeKind> RetKinds;
382  const ArgKind ArgsKind;
383 };
384 
385 /// \brief Return CK_Trivial when appropriate for VariadicDynCastAllOfMatchers.
387 public:
388  template <typename BaseT, typename DerivedT>
390  ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT> Func,
391  StringRef MatcherName)
392  : VariadicFuncMatcherDescriptor(Func, MatcherName),
393  DerivedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<DerivedT>()) {
394  }
395 
396  bool
398  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
399  // If Kind is not a base of DerivedKind, either DerivedKind is a base of
400  // Kind (in which case the match will always succeed) or Kind and
401  // DerivedKind are unrelated (in which case it will always fail), so set
402  // Specificity to 0.
404  LeastDerivedKind)) {
405  if (Kind.isSame(DerivedKind) || !Kind.isBaseOf(DerivedKind)) {
406  if (Specificity)
407  *Specificity = 0;
408  }
409  return true;
410  } else {
411  return false;
412  }
413  }
414 
415 private:
416  const ast_type_traits::ASTNodeKind DerivedKind;
417 };
418 
419 /// \brief Helper macros to check the arguments on all marshaller functions.
420 #define CHECK_ARG_COUNT(count) \
421  if (Args.size() != count) { \
422  Error->addError(NameRange, Error->ET_RegistryWrongArgCount) \
423  << count << Args.size(); \
424  return VariantMatcher(); \
425  }
426 
427 #define CHECK_ARG_TYPE(index, type) \
428  if (!ArgTypeTraits<type>::is(Args[index].Value)) { \
429  Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \
430  << (index + 1) << ArgTypeTraits<type>::getKind().asString() \
431  << Args[index].Value.getTypeAsString(); \
432  return VariantMatcher(); \
433  }
434 
435 /// \brief 0-arg marshaller function.
436 template <typename ReturnType>
437 static VariantMatcher matcherMarshall0(void (*Func)(), StringRef MatcherName,
438  SourceRange NameRange,
440  Diagnostics *Error) {
441  typedef ReturnType (*FuncType)();
442  CHECK_ARG_COUNT(0);
443  return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)());
444 }
445 
446 /// \brief 1-arg marshaller function.
447 template <typename ReturnType, typename ArgType1>
448 static VariantMatcher matcherMarshall1(void (*Func)(), StringRef MatcherName,
449  SourceRange NameRange,
451  Diagnostics *Error) {
452  typedef ReturnType (*FuncType)(ArgType1);
453  CHECK_ARG_COUNT(1);
454  CHECK_ARG_TYPE(0, ArgType1);
455  return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)(
457 }
458 
459 /// \brief 2-arg marshaller function.
460 template <typename ReturnType, typename ArgType1, typename ArgType2>
461 static VariantMatcher matcherMarshall2(void (*Func)(), StringRef MatcherName,
462  SourceRange NameRange,
464  Diagnostics *Error) {
465  typedef ReturnType (*FuncType)(ArgType1, ArgType2);
466  CHECK_ARG_COUNT(2);
467  CHECK_ARG_TYPE(0, ArgType1);
468  CHECK_ARG_TYPE(1, ArgType2);
469  return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)(
471  ArgTypeTraits<ArgType2>::get(Args[1].Value)));
472 }
473 
474 #undef CHECK_ARG_COUNT
475 #undef CHECK_ARG_TYPE
476 
477 /// \brief Helper class used to collect all the possible overloads of an
478 /// argument adaptative matcher function.
479 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
480  typename FromTypes, typename ToTypes>
482 public:
484  std::vector<MatcherDescriptor *> &Out)
485  : Name(Name), Out(Out) {
486  collect(FromTypes());
487  }
488 
489 private:
490  typedef ast_matchers::internal::ArgumentAdaptingMatcherFunc<
491  ArgumentAdapterT, FromTypes, ToTypes> AdaptativeFunc;
492 
493  /// \brief End case for the recursion
494  static void collect(ast_matchers::internal::EmptyTypeList) {}
495 
496  /// \brief Recursive case. Get the overload for the head of the list, and
497  /// recurse to the tail.
498  template <typename FromTypeList>
499  inline void collect(FromTypeList);
500 
501  StringRef Name;
502  std::vector<MatcherDescriptor *> &Out;
503 };
504 
505 /// \brief MatcherDescriptor that wraps multiple "overloads" of the same
506 /// matcher.
507 ///
508 /// It will try every overload and generate appropriate errors for when none or
509 /// more than one overloads match the arguments.
511 public:
513  : Overloads(Callbacks.begin(), Callbacks.end()) {}
514 
516 
519  Diagnostics *Error) const override {
520  std::vector<VariantMatcher> Constructed;
521  Diagnostics::OverloadContext Ctx(Error);
522  for (const auto &O : Overloads) {
523  VariantMatcher SubMatcher = O->create(NameRange, Args, Error);
524  if (!SubMatcher.isNull()) {
525  Constructed.push_back(SubMatcher);
526  }
527  }
528 
529  if (Constructed.empty()) return VariantMatcher(); // No overload matched.
530  // We ignore the errors if any matcher succeeded.
531  Ctx.revertErrors();
532  if (Constructed.size() > 1) {
533  // More than one constructed. It is ambiguous.
534  Error->addError(NameRange, Error->ET_RegistryAmbiguousOverload);
535  return VariantMatcher();
536  }
537  return Constructed[0];
538  }
539 
540  bool isVariadic() const override {
541  bool Overload0Variadic = Overloads[0]->isVariadic();
542 #ifndef NDEBUG
543  for (const auto &O : Overloads) {
544  assert(Overload0Variadic == O->isVariadic());
545  }
546 #endif
547  return Overload0Variadic;
548  }
549 
550  unsigned getNumArgs() const override {
551  unsigned Overload0NumArgs = Overloads[0]->getNumArgs();
552 #ifndef NDEBUG
553  for (const auto &O : Overloads) {
554  assert(Overload0NumArgs == O->getNumArgs());
555  }
556 #endif
557  return Overload0NumArgs;
558  }
559 
560  void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
561  std::vector<ArgKind> &Kinds) const override {
562  for (const auto &O : Overloads) {
563  if (O->isConvertibleTo(ThisKind))
564  O->getArgKinds(ThisKind, ArgNo, Kinds);
565  }
566  }
567 
569  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
570  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
571  for (const auto &O : Overloads) {
572  if (O->isConvertibleTo(Kind, Specificity, LeastDerivedKind))
573  return true;
574  }
575  return false;
576  }
577 
578 private:
579  std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
580 };
581 
582 /// \brief Variadic operator marshaller function.
584 public:
585  typedef DynTypedMatcher::VariadicOperator VarOp;
586  VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount,
587  VarOp Op, StringRef MatcherName)
588  : MinCount(MinCount), MaxCount(MaxCount), Op(Op),
589  MatcherName(MatcherName) {}
590 
593  Diagnostics *Error) const override {
594  if (Args.size() < MinCount || MaxCount < Args.size()) {
595  const std::string MaxStr =
596  (MaxCount == UINT_MAX ? "" : Twine(MaxCount)).str();
597  Error->addError(NameRange, Error->ET_RegistryWrongArgCount)
598  << ("(" + Twine(MinCount) + ", " + MaxStr + ")") << Args.size();
599  return VariantMatcher();
600  }
601 
602  std::vector<VariantMatcher> InnerArgs;
603  for (size_t i = 0, e = Args.size(); i != e; ++i) {
604  const ParserValue &Arg = Args[i];
605  const VariantValue &Value = Arg.Value;
606  if (!Value.isMatcher()) {
607  Error->addError(Arg.Range, Error->ET_RegistryWrongArgType)
608  << (i + 1) << "Matcher<>" << Value.getTypeAsString();
609  return VariantMatcher();
610  }
611  InnerArgs.push_back(Value.getMatcher());
612  }
613  return VariantMatcher::VariadicOperatorMatcher(Op, std::move(InnerArgs));
614  }
615 
616  bool isVariadic() const override { return true; }
617  unsigned getNumArgs() const override { return 0; }
618  void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
619  std::vector<ArgKind> &Kinds) const override {
620  Kinds.push_back(ThisKind);
621  }
622  bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
623  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
624  if (Specificity)
625  *Specificity = 1;
626  if (LeastDerivedKind)
627  *LeastDerivedKind = Kind;
628  return true;
629  }
630  bool isPolymorphic() const override { return true; }
631 
632 private:
633  const unsigned MinCount;
634  const unsigned MaxCount;
635  const VarOp Op;
636  const StringRef MatcherName;
637 };
638 
639 /// Helper functions to select the appropriate marshaller functions.
640 /// They detect the number of arguments, arguments types and return type.
641 
642 /// \brief 0-arg overload
643 template <typename ReturnType>
645  StringRef MatcherName) {
646  std::vector<ast_type_traits::ASTNodeKind> RetTypes;
649  matcherMarshall0<ReturnType>, reinterpret_cast<void (*)()>(Func),
650  MatcherName, RetTypes, None);
651 }
652 
653 /// \brief 1-arg overload
654 template <typename ReturnType, typename ArgType1>
655 MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1),
656  StringRef MatcherName) {
657  std::vector<ast_type_traits::ASTNodeKind> RetTypes;
661  matcherMarshall1<ReturnType, ArgType1>,
662  reinterpret_cast<void (*)()>(Func), MatcherName, RetTypes, AK);
663 }
664 
665 /// \brief 2-arg overload
666 template <typename ReturnType, typename ArgType1, typename ArgType2>
667 MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1, ArgType2),
668  StringRef MatcherName) {
669  std::vector<ast_type_traits::ASTNodeKind> RetTypes;
674  matcherMarshall2<ReturnType, ArgType1, ArgType2>,
675  reinterpret_cast<void (*)()>(Func), MatcherName, RetTypes, AKs);
676 }
677 
678 /// \brief Variadic overload.
679 template <typename ResultT, typename ArgT,
680  ResultT (*Func)(ArrayRef<const ArgT *>)>
682  ast_matchers::internal::VariadicFunction<ResultT, ArgT, Func> VarFunc,
683  StringRef MatcherName) {
684  return new VariadicFuncMatcherDescriptor(VarFunc, MatcherName);
685 }
686 
687 /// \brief Overload for VariadicDynCastAllOfMatchers.
688 ///
689 /// Not strictly necessary, but DynCastAllOfMatcherDescriptor gives us better
690 /// completion results for that type of matcher.
691 template <typename BaseT, typename DerivedT>
692 MatcherDescriptor *
693 makeMatcherAutoMarshall(ast_matchers::internal::VariadicDynCastAllOfMatcher<
694  BaseT, DerivedT> VarFunc,
695  StringRef MatcherName) {
696  return new DynCastAllOfMatcherDescriptor(VarFunc, MatcherName);
697 }
698 
699 /// \brief Argument adaptative overload.
700 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
701  typename FromTypes, typename ToTypes>
702 MatcherDescriptor *
703 makeMatcherAutoMarshall(ast_matchers::internal::ArgumentAdaptingMatcherFunc<
704  ArgumentAdapterT, FromTypes, ToTypes>,
705  StringRef MatcherName) {
706  std::vector<MatcherDescriptor *> Overloads;
708  Overloads);
709  return new OverloadedMatcherDescriptor(Overloads);
710 }
711 
712 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
713  typename FromTypes, typename ToTypes>
714 template <typename FromTypeList>
715 inline void AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes,
716  ToTypes>::collect(FromTypeList) {
717  Out.push_back(makeMatcherAutoMarshall(
718  &AdaptativeFunc::template create<typename FromTypeList::head>, Name));
719  collect(typename FromTypeList::tail());
720 }
721 
722 /// \brief Variadic operator overload.
723 template <unsigned MinCount, unsigned MaxCount>
724 MatcherDescriptor *
725 makeMatcherAutoMarshall(ast_matchers::internal::VariadicOperatorMatcherFunc<
726  MinCount, MaxCount> Func,
727  StringRef MatcherName) {
728  return new VariadicOperatorMatcherDescriptor(MinCount, MaxCount, Func.Op,
729  MatcherName);
730 }
731 
732 } // namespace internal
733 } // namespace dynamic
734 } // namespace ast_matchers
735 } // namespace clang
736 
737 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
Definition: Marshallers.h:618
const MatchFinder::MatchersByType * Matchers
bool isMatcher() const
Matcher value functions.
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Definition: Marshallers.h:591
static const CastKind CK_Invalid
std::string getTypeAsString() const
String representation of the type of the value.
AdaptativeOverloadCollector(StringRef Name, std::vector< MatcherDescriptor * > &Out)
Definition: Marshallers.h:483
VariantMatcher variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
Variadic marshaller function.
Definition: Marshallers.h:303
const DynTypedMatcher *const Matcher
bool isPolymorphic() const override
Returns whether the matcher will, given a matcher of any type T, yield a matcher of type T...
Definition: Marshallers.h:630
static VariantMatcher SingleMatcher(const DynTypedMatcher &Matcher)
Clones the provided matcher.
iterator begin() const
Definition: Type.h:4235
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:397
static VariantMatcher VariadicOperatorMatcher(DynTypedMatcher::VariadicOperator Op, std::vector< VariantMatcher > Args)
Creates a 'variadic' operator matcher.
bool isUnsigned() const
Unsigned value functions.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
Definition: Marshallers.h:211
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Definition: Marshallers.h:204
FixedArgCountMatcherDescriptor(MarshallerType Marshaller, void(*Func)(), StringRef MatcherName, ArrayRef< ast_type_traits::ASTNodeKind > RetKinds, ArrayRef< ArgKind > ArgKinds)
Definition: Marshallers.h:196
bool isBaseOf(ASTNodeKind Other, unsigned *Distance=nullptr) const
Returns true if this is a base kind of (or same as) Other.
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Definition: Marshallers.h:517
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
OverloadedMatcherDescriptor(ArrayRef< MatcherDescriptor * > Callbacks)
Definition: Marshallers.h:512
VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount, VarOp Op, StringRef MatcherName)
Definition: Marshallers.h:586
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
Definition: Marshallers.h:366
void revertErrors()
Revert all errors that happened within this context.
Definition: Diagnostics.cpp:56
bool isVariadic() const override
Returns whether the matcher is variadic.
Definition: Marshallers.h:540
iterator end() const
#define UINT_MAX
Definition: limits.h:72
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:371
bool isNull() const
Whether the matcher is null.
Definition: VariantValue.h:158
A VariantValue instance annotated with its parser context.
Definition: Diagnostics.h:43
CastKind
CastKind - The kind of operation required for a conversion.
bool isRetKindConvertibleTo(ArrayRef< ast_type_traits::ASTNodeKind > RetKinds, ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind)
Definition: Marshallers.h:162
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
Definition: Marshallers.h:617
static void build(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
Definition: Marshallers.h:287
static VariantMatcher matcherMarshall2(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
2-arg marshaller function.
Definition: Marshallers.h:461
virtual VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const =0
Diagnostics class to manage error messages.
VariadicFuncMatcherDescriptor(ast_matchers::internal::VariadicFunction< ResultT, ArgT, F > Func, StringRef MatcherName)
Definition: Marshallers.h:350
Helper class to manage error messages.
Definition: Diagnostics.h:51
#define ATTR(X)
ArgStream addError(SourceRange Range, ErrorType Error)
Add an error to the diagnostics.
Definition: Diagnostics.cpp:66
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:216
Kind
virtual bool isPolymorphic() const
Returns whether the matcher will, given a matcher of any type T, yield a matcher of type T...
Definition: Marshallers.h:159
bool isVariadic() const override
Returns whether the matcher is variadic.
Definition: Marshallers.h:616
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
Definition: Marshallers.h:212
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
Definition: Marshallers.h:560
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Definition: Marshallers.h:359
Return CK_Trivial when appropriate for VariadicDynCastAllOfMatchers.
Definition: Marshallers.h:386
MatcherDescriptor * makeMatcherAutoMarshall(ReturnType(*Func)(), StringRef MatcherName)
Helper functions to select the appropriate marshaller functions.
Definition: Marshallers.h:644
#define CHECK_ARG_COUNT(count)
Helper macros to check the arguments on all marshaller functions.
Definition: Marshallers.h:420
virtual bool isVariadic() const =0
Returns whether the matcher is variadic.
bool isVariadic() const override
Returns whether the matcher is variadic.
Definition: Marshallers.h:210
MatcherDescriptor that wraps multiple "overloads" of the same matcher.
Definition: Marshallers.h:510
virtual unsigned getNumArgs() const =0
Returns the number of arguments accepted by the matcher if not variadic.
VariantMatcher(* MarshallerType)(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
Definition: Marshallers.h:184
bool isSame(ASTNodeKind Other) const
Returns true if this and Other represent the same kind.
Definition: ASTTypeTraits.h:65
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:622
static VariantMatcher matcherMarshall0(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
0-arg marshaller function.
Definition: Marshallers.h:437
static VariantMatcher PolymorphicMatcher(std::vector< DynTypedMatcher > Matchers)
Clones the provided matchers.
static void mergePolyMatchers(const PolyMatcher &Poly, std::vector< DynTypedMatcher > &Out, ast_matchers::internal::EmptyTypeList)
Helper methods to extract and merge all possible typed matchers out of the polymorphic object...
Definition: Marshallers.h:234
Helper template class to just from argument type to the right is/get functions in VariantValue...
Definition: Marshallers.h:38
bool isString() const
String value functions.
Context for overloaded matcher construction.
Definition: Diagnostics.h:122
#define CHECK_ARG_TYPE(index, type)
Definition: Marshallers.h:427
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:12171
const std::string & getString() const
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
Definition: Marshallers.h:367
DynCastAllOfMatcherDescriptor(ast_matchers::internal::VariadicDynCastAllOfMatcher< BaseT, DerivedT > Func, StringRef MatcherName)
Definition: Marshallers.h:389
static void build(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
Definition: Marshallers.h:280
#define CAST_OPERATION(Name)
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:810
Polymorphic value type.
virtual void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &ArgKinds) const =0
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:568
static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher)
Convert the return values of the functions into a VariantMatcher.
Definition: Marshallers.h:251
Helper class used to collect all the possible overloads of an argument adaptative matcher function...
Definition: Marshallers.h:481
const VariantMatcher & getMatcher() const
virtual bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity=nullptr, ast_type_traits::ASTNodeKind *LeastDerivedKind=nullptr) const =0
Returns whether this matcher is convertible to the given type.
bool hasTypedMatcher() const
Determines if the contained matcher can be converted to Matcher<T>.
Definition: VariantValue.h:176
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
Definition: Marshallers.h:550
static VariantMatcher matcherMarshall1(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
1-arg marshaller function.
Definition: Marshallers.h:448
void buildReturnTypeVectorFromTypeList(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
Definition: Marshallers.h:266
bool isVariadic() const override
Returns whether the matcher is variadic.
Definition: Marshallers.h:365