1 //===--- Marshallers.h - Generic matcher function marshallers -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Functions templates and classes to wrap matcher construct functions.
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.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H
21 #define LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H
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"
31 namespace ast_matchers {
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> {
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();
48 static ArgKind getKind() {
49 return ArgKind(ArgKind::AK_String);
54 struct ArgTypeTraits<StringRef> : public ArgTypeTraits<std::string> {
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>();
61 static ast_matchers::internal::Matcher<T> get(const VariantValue &Value) {
62 return Value.getMatcher().getTypedMatcher<T>();
64 static ArgKind getKind() {
65 return ArgKind(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
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();
74 static ArgKind getKind() {
75 return ArgKind(ArgKind::AK_Unsigned);
79 /// \brief Matcher descriptor interface.
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 {
85 virtual ~MatcherDescriptor() {}
86 virtual VariantMatcher create(const SourceRange &NameRange,
87 ArrayRef<ParserValue> Args,
88 Diagnostics *Error) const = 0;
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;
94 /// Returns the number of arguments accepted by the matcher if not variadic.
95 virtual unsigned getNumArgs() const = 0;
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;
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;
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; }
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;
134 /// \brief Simple callback implementation. Marshaller and function are provided.
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 {
142 typedef VariantMatcher (*MarshallerType)(void (*Func)(),
143 StringRef MatcherName,
144 const SourceRange &NameRange,
145 ArrayRef<ParserValue> Args,
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
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()) {}
162 VariantMatcher create(const SourceRange &NameRange,
163 ArrayRef<ParserValue> Args, Diagnostics *Error) const {
164 return Marshaller(Func, MatcherName, NameRange, Args, Error);
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]);
173 bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
174 ast_type_traits::ASTNodeKind *LeastDerivedKind) const {
175 return isRetKindConvertibleTo(RetKinds, Kind, Specificity,
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;
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) {}
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());
201 /// \brief Convert the return values of the functions into a VariantMatcher.
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
207 static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher) {
208 return VariantMatcher::SingleMatcher(Matcher);
211 template <typename T>
212 static VariantMatcher outvalueToVariantMatcher(const T &PolyMatcher,
213 typename T::ReturnTypes * =
215 std::vector<DynTypedMatcher> Matchers;
216 mergePolyMatchers(PolyMatcher, Matchers, typename T::ReturnTypes());
217 VariantMatcher Out = VariantMatcher::PolymorphicMatcher(std::move(Matchers));
221 template <typename T>
222 inline void buildReturnTypeVectorFromTypeList(
223 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
225 ast_type_traits::ASTNodeKind::getFromNodeKind<typename T::head>());
226 buildReturnTypeVectorFromTypeList<typename T::tail>(RetTypes);
231 buildReturnTypeVectorFromTypeList<ast_matchers::internal::EmptyTypeList>(
232 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {}
234 template <typename T>
235 struct BuildReturnTypeVector {
236 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
237 buildReturnTypeVectorFromTypeList<typename T::ReturnTypes>(RetTypes);
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>());
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>());
255 /// \brief Variadic marshaller function.
256 template <typename ResultT, typename ArgT,
257 ResultT (*Func)(ArrayRef<const ArgT *>)>
259 variadicMatcherDescriptor(StringRef MatcherName, const SourceRange &NameRange,
260 ArrayRef<ParserValue> Args, Diagnostics *Error) {
261 ArgT **InnerArgs = new ArgT *[Args.size()]();
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();
274 InnerArgs[i] = new ArgT(ArgTraits::get(Value));
279 Out = outvalueToVariantMatcher(
280 Func(ArrayRef<const ArgT *>(InnerArgs, Args.size())));
283 for (size_t i = 0, e = Args.size(); i != e; ++i) {
290 /// \brief Matcher descriptor for variadic functions.
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
297 class VariadicFuncMatcherDescriptor : public MatcherDescriptor {
299 typedef VariantMatcher (*RunFunc)(StringRef MatcherName,
300 const SourceRange &NameRange,
301 ArrayRef<ParserValue> Args,
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);
314 VariantMatcher create(const SourceRange &NameRange,
315 ArrayRef<ParserValue> Args, Diagnostics *Error) const {
316 return Func(MatcherName, NameRange, Args, Error);
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);
325 bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
326 ast_type_traits::ASTNodeKind *LeastDerivedKind) const {
327 return isRetKindConvertibleTo(RetKinds, Kind, Specificity,
333 const std::string MatcherName;
334 std::vector<ast_type_traits::ASTNodeKind> RetKinds;
335 const ArgKind ArgsKind;
338 /// \brief Return CK_Trivial when appropriate for VariadicDynCastAllOfMatchers.
339 class DynCastAllOfMatcherDescriptor : public VariadicFuncMatcherDescriptor {
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>()) {
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
356 if (VariadicFuncMatcherDescriptor::isConvertibleTo(Kind, Specificity,
358 if (Kind.isSame(DerivedKind) || !Kind.isBaseOf(DerivedKind)) {
369 const ast_type_traits::ASTNodeKind DerivedKind;
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(); \
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(); \
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)();
397 return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)());
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);
408 CHECK_ARG_TYPE(0, ArgType1);
409 return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)(
410 ArgTypeTraits<ArgType1>::get(Args[0].Value)));
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);
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)));
428 #undef CHECK_ARG_COUNT
429 #undef CHECK_ARG_TYPE
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 {
437 AdaptativeOverloadCollector(StringRef Name,
438 std::vector<MatcherDescriptor *> &Out)
439 : Name(Name), Out(Out) {
440 collect(FromTypes());
444 typedef ast_matchers::internal::ArgumentAdaptingMatcherFunc<
445 ArgumentAdapterT, FromTypes, ToTypes> AdaptativeFunc;
447 /// \brief End case for the recursion
448 static void collect(ast_matchers::internal::EmptyTypeList) {}
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);
455 const StringRef Name;
456 std::vector<MatcherDescriptor *> &Out;
459 /// \brief MatcherDescriptor that wraps multiple "overloads" of the same
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 {
466 OverloadedMatcherDescriptor(ArrayRef<MatcherDescriptor *> Callbacks)
467 : Overloads(Callbacks.begin(), Callbacks.end()) {}
469 virtual ~OverloadedMatcherDescriptor() {}
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);
483 if (Constructed.empty()) return VariantMatcher(); // No overload matched.
484 // We ignore the errors if any matcher succeeded.
486 if (Constructed.size() > 1) {
487 // More than one constructed. It is ambiguous.
488 Error->addError(NameRange, Error->ET_RegistryAmbiguousOverload);
489 return VariantMatcher();
491 return Constructed[0];
494 bool isVariadic() const {
495 bool Overload0Variadic = Overloads[0]->isVariadic();
497 for (const auto &O : Overloads) {
498 assert(Overload0Variadic == O->isVariadic());
501 return Overload0Variadic;
504 unsigned getNumArgs() const {
505 unsigned Overload0NumArgs = Overloads[0]->getNumArgs();
507 for (const auto &O : Overloads) {
508 assert(Overload0NumArgs == O->getNumArgs());
511 return Overload0NumArgs;
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);
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))
532 std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
535 /// \brief Variadic operator marshaller function.
536 class VariadicOperatorMatcherDescriptor : public MatcherDescriptor {
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) {}
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();
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();
564 InnerArgs.push_back(Value.getMatcher());
566 return VariantMatcher::VariadicOperatorMatcher(Func, std::move(InnerArgs));
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);
575 bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
576 ast_type_traits::ASTNodeKind *LeastDerivedKind) const {
579 if (LeastDerivedKind)
580 *LeastDerivedKind = Kind;
583 bool isPolymorphic() const override { return true; }
586 const unsigned MinCount;
587 const unsigned MaxCount;
589 const StringRef MatcherName;
592 /// Helper functions to select the appropriate marshaller functions.
593 /// They detect the number of arguments, arguments types and return type.
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);
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);
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);
631 /// \brief Variadic overload.
632 template <typename ResultT, typename ArgT,
633 ResultT (*Func)(ArrayRef<const ArgT *>)>
635 makeMatcherAutoMarshall(llvm::VariadicFunction<ResultT, ArgT, Func> VarFunc,
636 StringRef MatcherName) {
637 return new VariadicFuncMatcherDescriptor(VarFunc, MatcherName);
640 /// \brief Overload for VariadicDynCastAllOfMatchers.
642 /// Not strictly necessary, but DynCastAllOfMatcherDescriptor gives us better
643 /// completion results for that type of matcher.
644 template <typename BaseT, typename DerivedT>
646 makeMatcherAutoMarshall(ast_matchers::internal::VariadicDynCastAllOfMatcher<
647 BaseT, DerivedT> VarFunc,
648 StringRef MatcherName) {
649 return new DynCastAllOfMatcherDescriptor(VarFunc, MatcherName);
652 /// \brief Argument adaptative overload.
653 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
654 typename FromTypes, typename ToTypes>
656 makeMatcherAutoMarshall(ast_matchers::internal::ArgumentAdaptingMatcherFunc<
657 ArgumentAdapterT, FromTypes, ToTypes>,
658 StringRef MatcherName) {
659 std::vector<MatcherDescriptor *> Overloads;
660 AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes, ToTypes>(MatcherName,
662 return new OverloadedMatcherDescriptor(Overloads);
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());
675 /// \brief Variadic operator overload.
676 template <unsigned MinCount, unsigned MaxCount>
678 makeMatcherAutoMarshall(ast_matchers::internal::VariadicOperatorMatcherFunc<
679 MinCount, MaxCount> Func,
680 StringRef MatcherName) {
681 return new VariadicOperatorMatcherDescriptor(MinCount, MaxCount, Func.Func,
685 } // namespace internal
686 } // namespace dynamic
687 } // namespace ast_matchers
690 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H