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