clang API Documentation
00001 //===--- Marshallers.h - Generic matcher function marshallers -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 /// 00010 /// \file 00011 /// \brief Functions templates and classes to wrap matcher construct functions. 00012 /// 00013 /// A collection of template function and classes that provide a generic 00014 /// marshalling layer on top of matcher construct functions. 00015 /// These are used by the registry to export all marshaller constructors with 00016 /// the same generic interface. 00017 /// 00018 //===----------------------------------------------------------------------===// 00019 00020 #ifndef LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H 00021 #define LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H 00022 00023 #include "clang/ASTMatchers/ASTMatchers.h" 00024 #include "clang/ASTMatchers/Dynamic/Diagnostics.h" 00025 #include "clang/ASTMatchers/Dynamic/VariantValue.h" 00026 #include "clang/Basic/LLVM.h" 00027 #include "llvm/ADT/STLExtras.h" 00028 #include <string> 00029 00030 namespace clang { 00031 namespace ast_matchers { 00032 namespace dynamic { 00033 namespace internal { 00034 00035 00036 /// \brief Helper template class to just from argument type to the right is/get 00037 /// functions in VariantValue. 00038 /// Used to verify and extract the matcher arguments below. 00039 template <class T> struct ArgTypeTraits; 00040 template <class T> struct ArgTypeTraits<const T &> : public ArgTypeTraits<T> { 00041 }; 00042 00043 template <> struct ArgTypeTraits<std::string> { 00044 static bool is(const VariantValue &Value) { return Value.isString(); } 00045 static const std::string &get(const VariantValue &Value) { 00046 return Value.getString(); 00047 } 00048 static ArgKind getKind() { 00049 return ArgKind(ArgKind::AK_String); 00050 } 00051 }; 00052 00053 template <> 00054 struct ArgTypeTraits<StringRef> : public ArgTypeTraits<std::string> { 00055 }; 00056 00057 template <class T> struct ArgTypeTraits<ast_matchers::internal::Matcher<T> > { 00058 static bool is(const VariantValue &Value) { 00059 return Value.isMatcher() && Value.getMatcher().hasTypedMatcher<T>(); 00060 } 00061 static ast_matchers::internal::Matcher<T> get(const VariantValue &Value) { 00062 return Value.getMatcher().getTypedMatcher<T>(); 00063 } 00064 static ArgKind getKind() { 00065 return ArgKind(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()); 00066 } 00067 }; 00068 00069 template <> struct ArgTypeTraits<unsigned> { 00070 static bool is(const VariantValue &Value) { return Value.isUnsigned(); } 00071 static unsigned get(const VariantValue &Value) { 00072 return Value.getUnsigned(); 00073 } 00074 static ArgKind getKind() { 00075 return ArgKind(ArgKind::AK_Unsigned); 00076 } 00077 }; 00078 00079 template <> struct ArgTypeTraits<attr::Kind> { 00080 private: 00081 static attr::Kind getAttrKind(llvm::StringRef AttrKind) { 00082 return llvm::StringSwitch<attr::Kind>(AttrKind) 00083 #define ATTR(X) .Case("attr::" #X, attr:: X) 00084 #include "clang/Basic/AttrList.inc" 00085 .Default(attr::Kind(-1)); 00086 } 00087 public: 00088 static bool is(const VariantValue &Value) { 00089 return Value.isString() && 00090 getAttrKind(Value.getString()) != attr::Kind(-1); 00091 } 00092 static attr::Kind get(const VariantValue &Value) { 00093 return getAttrKind(Value.getString()); 00094 } 00095 static ArgKind getKind() { 00096 return ArgKind(ArgKind::AK_String); 00097 } 00098 }; 00099 00100 /// \brief Matcher descriptor interface. 00101 /// 00102 /// Provides a \c create() method that constructs the matcher from the provided 00103 /// arguments, and various other methods for type introspection. 00104 class MatcherDescriptor { 00105 public: 00106 virtual ~MatcherDescriptor() {} 00107 virtual VariantMatcher create(const SourceRange &NameRange, 00108 ArrayRef<ParserValue> Args, 00109 Diagnostics *Error) const = 0; 00110 00111 /// Returns whether the matcher is variadic. Variadic matchers can take any 00112 /// number of arguments, but they must be of the same type. 00113 virtual bool isVariadic() const = 0; 00114 00115 /// Returns the number of arguments accepted by the matcher if not variadic. 00116 virtual unsigned getNumArgs() const = 0; 00117 00118 /// Given that the matcher is being converted to type \p ThisKind, append the 00119 /// set of argument types accepted for argument \p ArgNo to \p ArgKinds. 00120 // FIXME: We should provide the ability to constrain the output of this 00121 // function based on the types of other matcher arguments. 00122 virtual void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, 00123 std::vector<ArgKind> &ArgKinds) const = 0; 00124 00125 /// Returns whether this matcher is convertible to the given type. If it is 00126 /// so convertible, store in *Specificity a value corresponding to the 00127 /// "specificity" of the converted matcher to the given context, and in 00128 /// *LeastDerivedKind the least derived matcher kind which would result in the 00129 /// same matcher overload. Zero specificity indicates that this conversion 00130 /// would produce a trivial matcher that will either always or never match. 00131 /// Such matchers are excluded from code completion results. 00132 virtual bool isConvertibleTo( 00133 ast_type_traits::ASTNodeKind Kind, unsigned *Specificity = nullptr, 00134 ast_type_traits::ASTNodeKind *LeastDerivedKind = nullptr) const = 0; 00135 00136 /// Returns whether the matcher will, given a matcher of any type T, yield a 00137 /// matcher of type T. 00138 virtual bool isPolymorphic() const { return false; } 00139 }; 00140 00141 inline bool isRetKindConvertibleTo( 00142 ArrayRef<ast_type_traits::ASTNodeKind> RetKinds, 00143 ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, 00144 ast_type_traits::ASTNodeKind *LeastDerivedKind) { 00145 for (const ast_type_traits::ASTNodeKind &NodeKind : RetKinds) { 00146 if (ArgKind(NodeKind).isConvertibleTo(Kind, Specificity)) { 00147 if (LeastDerivedKind) 00148 *LeastDerivedKind = NodeKind; 00149 return true; 00150 } 00151 } 00152 return false; 00153 } 00154 00155 /// \brief Simple callback implementation. Marshaller and function are provided. 00156 /// 00157 /// This class wraps a function of arbitrary signature and a marshaller 00158 /// function into a MatcherDescriptor. 00159 /// The marshaller is in charge of taking the VariantValue arguments, checking 00160 /// their types, unpacking them and calling the underlying function. 00161 class FixedArgCountMatcherDescriptor : public MatcherDescriptor { 00162 public: 00163 typedef VariantMatcher (*MarshallerType)(void (*Func)(), 00164 StringRef MatcherName, 00165 const SourceRange &NameRange, 00166 ArrayRef<ParserValue> Args, 00167 Diagnostics *Error); 00168 00169 /// \param Marshaller Function to unpack the arguments and call \c Func 00170 /// \param Func Matcher construct function. This is the function that 00171 /// compile-time matcher expressions would use to create the matcher. 00172 /// \param RetKinds The list of matcher types to which the matcher is 00173 /// convertible. 00174 /// \param ArgKinds The types of the arguments this matcher takes. 00175 FixedArgCountMatcherDescriptor( 00176 MarshallerType Marshaller, void (*Func)(), StringRef MatcherName, 00177 ArrayRef<ast_type_traits::ASTNodeKind> RetKinds, 00178 ArrayRef<ArgKind> ArgKinds) 00179 : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName), 00180 RetKinds(RetKinds.begin(), RetKinds.end()), 00181 ArgKinds(ArgKinds.begin(), ArgKinds.end()) {} 00182 00183 VariantMatcher create(const SourceRange &NameRange, 00184 ArrayRef<ParserValue> Args, Diagnostics *Error) const { 00185 return Marshaller(Func, MatcherName, NameRange, Args, Error); 00186 } 00187 00188 bool isVariadic() const { return false; } 00189 unsigned getNumArgs() const { return ArgKinds.size(); } 00190 void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, 00191 std::vector<ArgKind> &Kinds) const { 00192 Kinds.push_back(ArgKinds[ArgNo]); 00193 } 00194 bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, 00195 ast_type_traits::ASTNodeKind *LeastDerivedKind) const { 00196 return isRetKindConvertibleTo(RetKinds, Kind, Specificity, 00197 LeastDerivedKind); 00198 } 00199 00200 private: 00201 const MarshallerType Marshaller; 00202 void (* const Func)(); 00203 const std::string MatcherName; 00204 const std::vector<ast_type_traits::ASTNodeKind> RetKinds; 00205 const std::vector<ArgKind> ArgKinds; 00206 }; 00207 00208 /// \brief Helper methods to extract and merge all possible typed matchers 00209 /// out of the polymorphic object. 00210 template <class PolyMatcher> 00211 static void mergePolyMatchers(const PolyMatcher &Poly, 00212 std::vector<DynTypedMatcher> &Out, 00213 ast_matchers::internal::EmptyTypeList) {} 00214 00215 template <class PolyMatcher, class TypeList> 00216 static void mergePolyMatchers(const PolyMatcher &Poly, 00217 std::vector<DynTypedMatcher> &Out, TypeList) { 00218 Out.push_back(ast_matchers::internal::Matcher<typename TypeList::head>(Poly)); 00219 mergePolyMatchers(Poly, Out, typename TypeList::tail()); 00220 } 00221 00222 /// \brief Convert the return values of the functions into a VariantMatcher. 00223 /// 00224 /// There are 2 cases right now: The return value is a Matcher<T> or is a 00225 /// polymorphic matcher. For the former, we just construct the VariantMatcher. 00226 /// For the latter, we instantiate all the possible Matcher<T> of the poly 00227 /// matcher. 00228 static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher) { 00229 return VariantMatcher::SingleMatcher(Matcher); 00230 } 00231 00232 template <typename T> 00233 static VariantMatcher outvalueToVariantMatcher(const T &PolyMatcher, 00234 typename T::ReturnTypes * = 00235 NULL) { 00236 std::vector<DynTypedMatcher> Matchers; 00237 mergePolyMatchers(PolyMatcher, Matchers, typename T::ReturnTypes()); 00238 VariantMatcher Out = VariantMatcher::PolymorphicMatcher(std::move(Matchers)); 00239 return Out; 00240 } 00241 00242 template <typename T> 00243 inline void buildReturnTypeVectorFromTypeList( 00244 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) { 00245 RetTypes.push_back( 00246 ast_type_traits::ASTNodeKind::getFromNodeKind<typename T::head>()); 00247 buildReturnTypeVectorFromTypeList<typename T::tail>(RetTypes); 00248 } 00249 00250 template <> 00251 inline void 00252 buildReturnTypeVectorFromTypeList<ast_matchers::internal::EmptyTypeList>( 00253 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {} 00254 00255 template <typename T> 00256 struct BuildReturnTypeVector { 00257 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) { 00258 buildReturnTypeVectorFromTypeList<typename T::ReturnTypes>(RetTypes); 00259 } 00260 }; 00261 00262 template <typename T> 00263 struct BuildReturnTypeVector<ast_matchers::internal::Matcher<T> > { 00264 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) { 00265 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()); 00266 } 00267 }; 00268 00269 template <typename T> 00270 struct BuildReturnTypeVector<ast_matchers::internal::BindableMatcher<T> > { 00271 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) { 00272 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()); 00273 } 00274 }; 00275 00276 /// \brief Variadic marshaller function. 00277 template <typename ResultT, typename ArgT, 00278 ResultT (*Func)(ArrayRef<const ArgT *>)> 00279 VariantMatcher 00280 variadicMatcherDescriptor(StringRef MatcherName, const SourceRange &NameRange, 00281 ArrayRef<ParserValue> Args, Diagnostics *Error) { 00282 ArgT **InnerArgs = new ArgT *[Args.size()](); 00283 00284 bool HasError = false; 00285 for (size_t i = 0, e = Args.size(); i != e; ++i) { 00286 typedef ArgTypeTraits<ArgT> ArgTraits; 00287 const ParserValue &Arg = Args[i]; 00288 const VariantValue &Value = Arg.Value; 00289 if (!ArgTraits::is(Value)) { 00290 Error->addError(Arg.Range, Error->ET_RegistryWrongArgType) 00291 << (i + 1) << ArgTraits::getKind().asString() << Value.getTypeAsString(); 00292 HasError = true; 00293 break; 00294 } 00295 InnerArgs[i] = new ArgT(ArgTraits::get(Value)); 00296 } 00297 00298 VariantMatcher Out; 00299 if (!HasError) { 00300 Out = outvalueToVariantMatcher(Func(llvm::makeArrayRef(InnerArgs, 00301 Args.size()))); 00302 } 00303 00304 for (size_t i = 0, e = Args.size(); i != e; ++i) { 00305 delete InnerArgs[i]; 00306 } 00307 delete[] InnerArgs; 00308 return Out; 00309 } 00310 00311 /// \brief Matcher descriptor for variadic functions. 00312 /// 00313 /// This class simply wraps a VariadicFunction with the right signature to export 00314 /// it as a MatcherDescriptor. 00315 /// This allows us to have one implementation of the interface for as many free 00316 /// functions as we want, reducing the number of symbols and size of the 00317 /// object file. 00318 class VariadicFuncMatcherDescriptor : public MatcherDescriptor { 00319 public: 00320 typedef VariantMatcher (*RunFunc)(StringRef MatcherName, 00321 const SourceRange &NameRange, 00322 ArrayRef<ParserValue> Args, 00323 Diagnostics *Error); 00324 00325 template <typename ResultT, typename ArgT, 00326 ResultT (*F)(ArrayRef<const ArgT *>)> 00327 VariadicFuncMatcherDescriptor(llvm::VariadicFunction<ResultT, ArgT, F> Func, 00328 StringRef MatcherName) 00329 : Func(&variadicMatcherDescriptor<ResultT, ArgT, F>), 00330 MatcherName(MatcherName.str()), 00331 ArgsKind(ArgTypeTraits<ArgT>::getKind()) { 00332 BuildReturnTypeVector<ResultT>::build(RetKinds); 00333 } 00334 00335 VariantMatcher create(const SourceRange &NameRange, 00336 ArrayRef<ParserValue> Args, Diagnostics *Error) const { 00337 return Func(MatcherName, NameRange, Args, Error); 00338 } 00339 00340 bool isVariadic() const { return true; } 00341 unsigned getNumArgs() const { return 0; } 00342 void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, 00343 std::vector<ArgKind> &Kinds) const { 00344 Kinds.push_back(ArgsKind); 00345 } 00346 bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, 00347 ast_type_traits::ASTNodeKind *LeastDerivedKind) const { 00348 return isRetKindConvertibleTo(RetKinds, Kind, Specificity, 00349 LeastDerivedKind); 00350 } 00351 00352 private: 00353 const RunFunc Func; 00354 const std::string MatcherName; 00355 std::vector<ast_type_traits::ASTNodeKind> RetKinds; 00356 const ArgKind ArgsKind; 00357 }; 00358 00359 /// \brief Return CK_Trivial when appropriate for VariadicDynCastAllOfMatchers. 00360 class DynCastAllOfMatcherDescriptor : public VariadicFuncMatcherDescriptor { 00361 public: 00362 template <typename BaseT, typename DerivedT> 00363 DynCastAllOfMatcherDescriptor( 00364 ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT> Func, 00365 StringRef MatcherName) 00366 : VariadicFuncMatcherDescriptor(Func, MatcherName), 00367 DerivedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<DerivedT>()) { 00368 } 00369 00370 bool 00371 isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, 00372 ast_type_traits::ASTNodeKind *LeastDerivedKind) const override { 00373 // If Kind is not a base of DerivedKind, either DerivedKind is a base of 00374 // Kind (in which case the match will always succeed) or Kind and 00375 // DerivedKind are unrelated (in which case it will always fail), so set 00376 // Specificity to 0. 00377 if (VariadicFuncMatcherDescriptor::isConvertibleTo(Kind, Specificity, 00378 LeastDerivedKind)) { 00379 if (Kind.isSame(DerivedKind) || !Kind.isBaseOf(DerivedKind)) { 00380 if (Specificity) 00381 *Specificity = 0; 00382 } 00383 return true; 00384 } else { 00385 return false; 00386 } 00387 } 00388 00389 private: 00390 const ast_type_traits::ASTNodeKind DerivedKind; 00391 }; 00392 00393 /// \brief Helper macros to check the arguments on all marshaller functions. 00394 #define CHECK_ARG_COUNT(count) \ 00395 if (Args.size() != count) { \ 00396 Error->addError(NameRange, Error->ET_RegistryWrongArgCount) \ 00397 << count << Args.size(); \ 00398 return VariantMatcher(); \ 00399 } 00400 00401 #define CHECK_ARG_TYPE(index, type) \ 00402 if (!ArgTypeTraits<type>::is(Args[index].Value)) { \ 00403 Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \ 00404 << (index + 1) << ArgTypeTraits<type>::getKind().asString() \ 00405 << Args[index].Value.getTypeAsString(); \ 00406 return VariantMatcher(); \ 00407 } 00408 00409 00410 /// \brief 0-arg marshaller function. 00411 template <typename ReturnType> 00412 static VariantMatcher matcherMarshall0(void (*Func)(), StringRef MatcherName, 00413 const SourceRange &NameRange, 00414 ArrayRef<ParserValue> Args, 00415 Diagnostics *Error) { 00416 typedef ReturnType (*FuncType)(); 00417 CHECK_ARG_COUNT(0); 00418 return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)()); 00419 } 00420 00421 /// \brief 1-arg marshaller function. 00422 template <typename ReturnType, typename ArgType1> 00423 static VariantMatcher matcherMarshall1(void (*Func)(), StringRef MatcherName, 00424 const SourceRange &NameRange, 00425 ArrayRef<ParserValue> Args, 00426 Diagnostics *Error) { 00427 typedef ReturnType (*FuncType)(ArgType1); 00428 CHECK_ARG_COUNT(1); 00429 CHECK_ARG_TYPE(0, ArgType1); 00430 return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)( 00431 ArgTypeTraits<ArgType1>::get(Args[0].Value))); 00432 } 00433 00434 /// \brief 2-arg marshaller function. 00435 template <typename ReturnType, typename ArgType1, typename ArgType2> 00436 static VariantMatcher matcherMarshall2(void (*Func)(), StringRef MatcherName, 00437 const SourceRange &NameRange, 00438 ArrayRef<ParserValue> Args, 00439 Diagnostics *Error) { 00440 typedef ReturnType (*FuncType)(ArgType1, ArgType2); 00441 CHECK_ARG_COUNT(2); 00442 CHECK_ARG_TYPE(0, ArgType1); 00443 CHECK_ARG_TYPE(1, ArgType2); 00444 return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)( 00445 ArgTypeTraits<ArgType1>::get(Args[0].Value), 00446 ArgTypeTraits<ArgType2>::get(Args[1].Value))); 00447 } 00448 00449 #undef CHECK_ARG_COUNT 00450 #undef CHECK_ARG_TYPE 00451 00452 /// \brief Helper class used to collect all the possible overloads of an 00453 /// argument adaptative matcher function. 00454 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT, 00455 typename FromTypes, typename ToTypes> 00456 class AdaptativeOverloadCollector { 00457 public: 00458 AdaptativeOverloadCollector(StringRef Name, 00459 std::vector<MatcherDescriptor *> &Out) 00460 : Name(Name), Out(Out) { 00461 collect(FromTypes()); 00462 } 00463 00464 private: 00465 typedef ast_matchers::internal::ArgumentAdaptingMatcherFunc< 00466 ArgumentAdapterT, FromTypes, ToTypes> AdaptativeFunc; 00467 00468 /// \brief End case for the recursion 00469 static void collect(ast_matchers::internal::EmptyTypeList) {} 00470 00471 /// \brief Recursive case. Get the overload for the head of the list, and 00472 /// recurse to the tail. 00473 template <typename FromTypeList> 00474 inline void collect(FromTypeList); 00475 00476 StringRef Name; 00477 std::vector<MatcherDescriptor *> &Out; 00478 }; 00479 00480 /// \brief MatcherDescriptor that wraps multiple "overloads" of the same 00481 /// matcher. 00482 /// 00483 /// It will try every overload and generate appropriate errors for when none or 00484 /// more than one overloads match the arguments. 00485 class OverloadedMatcherDescriptor : public MatcherDescriptor { 00486 public: 00487 OverloadedMatcherDescriptor(ArrayRef<MatcherDescriptor *> Callbacks) 00488 : Overloads(Callbacks.begin(), Callbacks.end()) {} 00489 00490 virtual ~OverloadedMatcherDescriptor() {} 00491 00492 virtual VariantMatcher create(const SourceRange &NameRange, 00493 ArrayRef<ParserValue> Args, 00494 Diagnostics *Error) const { 00495 std::vector<VariantMatcher> Constructed; 00496 Diagnostics::OverloadContext Ctx(Error); 00497 for (const auto &O : Overloads) { 00498 VariantMatcher SubMatcher = O->create(NameRange, Args, Error); 00499 if (!SubMatcher.isNull()) { 00500 Constructed.push_back(SubMatcher); 00501 } 00502 } 00503 00504 if (Constructed.empty()) return VariantMatcher(); // No overload matched. 00505 // We ignore the errors if any matcher succeeded. 00506 Ctx.revertErrors(); 00507 if (Constructed.size() > 1) { 00508 // More than one constructed. It is ambiguous. 00509 Error->addError(NameRange, Error->ET_RegistryAmbiguousOverload); 00510 return VariantMatcher(); 00511 } 00512 return Constructed[0]; 00513 } 00514 00515 bool isVariadic() const { 00516 bool Overload0Variadic = Overloads[0]->isVariadic(); 00517 #ifndef NDEBUG 00518 for (const auto &O : Overloads) { 00519 assert(Overload0Variadic == O->isVariadic()); 00520 } 00521 #endif 00522 return Overload0Variadic; 00523 } 00524 00525 unsigned getNumArgs() const { 00526 unsigned Overload0NumArgs = Overloads[0]->getNumArgs(); 00527 #ifndef NDEBUG 00528 for (const auto &O : Overloads) { 00529 assert(Overload0NumArgs == O->getNumArgs()); 00530 } 00531 #endif 00532 return Overload0NumArgs; 00533 } 00534 00535 void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, 00536 std::vector<ArgKind> &Kinds) const { 00537 for (const auto &O : Overloads) { 00538 if (O->isConvertibleTo(ThisKind)) 00539 O->getArgKinds(ThisKind, ArgNo, Kinds); 00540 } 00541 } 00542 00543 bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, 00544 ast_type_traits::ASTNodeKind *LeastDerivedKind) const { 00545 for (const auto &O : Overloads) { 00546 if (O->isConvertibleTo(Kind, Specificity, LeastDerivedKind)) 00547 return true; 00548 } 00549 return false; 00550 } 00551 00552 private: 00553 std::vector<std::unique_ptr<MatcherDescriptor>> Overloads; 00554 }; 00555 00556 /// \brief Variadic operator marshaller function. 00557 class VariadicOperatorMatcherDescriptor : public MatcherDescriptor { 00558 public: 00559 typedef DynTypedMatcher::VariadicOperatorFunction VarFunc; 00560 VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount, 00561 VarFunc Func, StringRef MatcherName) 00562 : MinCount(MinCount), MaxCount(MaxCount), Func(Func), 00563 MatcherName(MatcherName) {} 00564 00565 virtual VariantMatcher create(const SourceRange &NameRange, 00566 ArrayRef<ParserValue> Args, 00567 Diagnostics *Error) const override { 00568 if (Args.size() < MinCount || MaxCount < Args.size()) { 00569 const std::string MaxStr = 00570 (MaxCount == UINT_MAX ? "" : Twine(MaxCount)).str(); 00571 Error->addError(NameRange, Error->ET_RegistryWrongArgCount) 00572 << ("(" + Twine(MinCount) + ", " + MaxStr + ")") << Args.size(); 00573 return VariantMatcher(); 00574 } 00575 00576 std::vector<VariantMatcher> InnerArgs; 00577 for (size_t i = 0, e = Args.size(); i != e; ++i) { 00578 const ParserValue &Arg = Args[i]; 00579 const VariantValue &Value = Arg.Value; 00580 if (!Value.isMatcher()) { 00581 Error->addError(Arg.Range, Error->ET_RegistryWrongArgType) 00582 << (i + 1) << "Matcher<>" << Value.getTypeAsString(); 00583 return VariantMatcher(); 00584 } 00585 InnerArgs.push_back(Value.getMatcher()); 00586 } 00587 return VariantMatcher::VariadicOperatorMatcher(Func, std::move(InnerArgs)); 00588 } 00589 00590 bool isVariadic() const override { return true; } 00591 unsigned getNumArgs() const override { return 0; } 00592 void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, 00593 std::vector<ArgKind> &Kinds) const override { 00594 Kinds.push_back(ThisKind); 00595 } 00596 bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, 00597 ast_type_traits::ASTNodeKind *LeastDerivedKind) const override { 00598 if (Specificity) 00599 *Specificity = 1; 00600 if (LeastDerivedKind) 00601 *LeastDerivedKind = Kind; 00602 return true; 00603 } 00604 bool isPolymorphic() const override { return true; } 00605 00606 private: 00607 const unsigned MinCount; 00608 const unsigned MaxCount; 00609 const VarFunc Func; 00610 const StringRef MatcherName; 00611 }; 00612 00613 /// Helper functions to select the appropriate marshaller functions. 00614 /// They detect the number of arguments, arguments types and return type. 00615 00616 /// \brief 0-arg overload 00617 template <typename ReturnType> 00618 MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(), 00619 StringRef MatcherName) { 00620 std::vector<ast_type_traits::ASTNodeKind> RetTypes; 00621 BuildReturnTypeVector<ReturnType>::build(RetTypes); 00622 return new FixedArgCountMatcherDescriptor( 00623 matcherMarshall0<ReturnType>, reinterpret_cast<void (*)()>(Func), 00624 MatcherName, RetTypes, None); 00625 } 00626 00627 /// \brief 1-arg overload 00628 template <typename ReturnType, typename ArgType1> 00629 MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1), 00630 StringRef MatcherName) { 00631 std::vector<ast_type_traits::ASTNodeKind> RetTypes; 00632 BuildReturnTypeVector<ReturnType>::build(RetTypes); 00633 ArgKind AK = ArgTypeTraits<ArgType1>::getKind(); 00634 return new FixedArgCountMatcherDescriptor( 00635 matcherMarshall1<ReturnType, ArgType1>, 00636 reinterpret_cast<void (*)()>(Func), MatcherName, RetTypes, AK); 00637 } 00638 00639 /// \brief 2-arg overload 00640 template <typename ReturnType, typename ArgType1, typename ArgType2> 00641 MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1, ArgType2), 00642 StringRef MatcherName) { 00643 std::vector<ast_type_traits::ASTNodeKind> RetTypes; 00644 BuildReturnTypeVector<ReturnType>::build(RetTypes); 00645 ArgKind AKs[] = { ArgTypeTraits<ArgType1>::getKind(), 00646 ArgTypeTraits<ArgType2>::getKind() }; 00647 return new FixedArgCountMatcherDescriptor( 00648 matcherMarshall2<ReturnType, ArgType1, ArgType2>, 00649 reinterpret_cast<void (*)()>(Func), MatcherName, RetTypes, AKs); 00650 } 00651 00652 /// \brief Variadic overload. 00653 template <typename ResultT, typename ArgT, 00654 ResultT (*Func)(ArrayRef<const ArgT *>)> 00655 MatcherDescriptor * 00656 makeMatcherAutoMarshall(llvm::VariadicFunction<ResultT, ArgT, Func> VarFunc, 00657 StringRef MatcherName) { 00658 return new VariadicFuncMatcherDescriptor(VarFunc, MatcherName); 00659 } 00660 00661 /// \brief Overload for VariadicDynCastAllOfMatchers. 00662 /// 00663 /// Not strictly necessary, but DynCastAllOfMatcherDescriptor gives us better 00664 /// completion results for that type of matcher. 00665 template <typename BaseT, typename DerivedT> 00666 MatcherDescriptor * 00667 makeMatcherAutoMarshall(ast_matchers::internal::VariadicDynCastAllOfMatcher< 00668 BaseT, DerivedT> VarFunc, 00669 StringRef MatcherName) { 00670 return new DynCastAllOfMatcherDescriptor(VarFunc, MatcherName); 00671 } 00672 00673 /// \brief Argument adaptative overload. 00674 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT, 00675 typename FromTypes, typename ToTypes> 00676 MatcherDescriptor * 00677 makeMatcherAutoMarshall(ast_matchers::internal::ArgumentAdaptingMatcherFunc< 00678 ArgumentAdapterT, FromTypes, ToTypes>, 00679 StringRef MatcherName) { 00680 std::vector<MatcherDescriptor *> Overloads; 00681 AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes, ToTypes>(MatcherName, 00682 Overloads); 00683 return new OverloadedMatcherDescriptor(Overloads); 00684 } 00685 00686 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT, 00687 typename FromTypes, typename ToTypes> 00688 template <typename FromTypeList> 00689 inline void AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes, 00690 ToTypes>::collect(FromTypeList) { 00691 Out.push_back(makeMatcherAutoMarshall( 00692 &AdaptativeFunc::template create<typename FromTypeList::head>, Name)); 00693 collect(typename FromTypeList::tail()); 00694 } 00695 00696 /// \brief Variadic operator overload. 00697 template <unsigned MinCount, unsigned MaxCount> 00698 MatcherDescriptor * 00699 makeMatcherAutoMarshall(ast_matchers::internal::VariadicOperatorMatcherFunc< 00700 MinCount, MaxCount> Func, 00701 StringRef MatcherName) { 00702 return new VariadicOperatorMatcherDescriptor(MinCount, MaxCount, Func.Func, 00703 MatcherName); 00704 } 00705 00706 } // namespace internal 00707 } // namespace dynamic 00708 } // namespace ast_matchers 00709 } // namespace clang 00710 00711 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H