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