20 #ifndef LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H
21 #define LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H
27 #include "llvm/ADT/STLExtras.h"
31 namespace ast_matchers {
45 return Value.getString();
61 return Value.getMatcher().getTypedMatcher<T>();
64 return ArgKind(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
71 return Value.getBoolean();
81 return Value.getDouble();
91 return Value.getUnsigned();
100 static attr::Kind getAttrKind(llvm::StringRef AttrKind) {
101 return llvm::StringSwitch<attr::Kind>(AttrKind)
103 #include "clang/Basic/AttrList.inc"
112 return getAttrKind(
Value.getString());
122 return llvm::StringSwitch<clang::CastKind>(AttrKind)
124 #include "clang/AST/OperationKinds.def"
134 return getCastKind(
Value.getString());
164 std::vector<ArgKind> &ArgKinds)
const = 0;
187 if (
ArgKind(NodeKind).isConvertibleTo(Kind, Specificity)) {
188 if (LeastDerivedKind)
189 *LeastDerivedKind = NodeKind;
205 StringRef MatcherName,
220 : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName),
221 RetKinds(RetKinds.begin(), RetKinds.end()),
222 ArgKinds(ArgKinds.begin(), ArgKinds.end()) {}
227 return Marshaller(Func, MatcherName, NameRange, Args, Error);
231 unsigned getNumArgs()
const override {
return ArgKinds.size(); }
233 std::vector<ArgKind> &Kinds)
const override {
234 Kinds.push_back(ArgKinds[ArgNo]);
245 void (*
const Func)();
246 const std::string MatcherName;
247 const std::vector<ast_type_traits::ASTNodeKind> RetKinds;
248 const std::vector<ArgKind> ArgKinds;
253 template <
class PolyMatcher>
255 std::vector<DynTypedMatcher> &Out,
256 ast_matchers::internal::EmptyTypeList) {}
258 template <
class PolyMatcher,
class TypeList>
260 std::vector<DynTypedMatcher> &Out, TypeList) {
261 Out.push_back(ast_matchers::internal::Matcher<typename TypeList::head>(Poly));
275 template <
typename T>
277 typename T::ReturnTypes * =
279 std::vector<DynTypedMatcher>
Matchers;
285 template <
typename T>
287 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
289 ast_type_traits::ASTNodeKind::getFromNodeKind<typename T::head>());
290 buildReturnTypeVectorFromTypeList<typename T::tail>(RetTypes);
295 buildReturnTypeVectorFromTypeList<ast_matchers::internal::EmptyTypeList>(
296 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {}
298 template <
typename T>
300 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
301 buildReturnTypeVectorFromTypeList<typename T::ReturnTypes>(RetTypes);
305 template <
typename T>
307 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
308 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
312 template <
typename T>
314 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
315 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
320 template <
typename ResultT,
typename ArgT,
325 ArgT **InnerArgs =
new ArgT *[Args.size()]();
327 bool HasError =
false;
328 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
332 if (!ArgTraits::is(Value)) {
338 InnerArgs[i] =
new ArgT(ArgTraits::get(Value));
347 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
368 template <
typename ResultT,
typename ArgT,
371 ast_matchers::internal::VariadicFunction<ResultT, ArgT, F> Func,
372 StringRef MatcherName)
373 : Func(&variadicMatcherDescriptor<ResultT, ArgT, F>),
374 MatcherName(MatcherName.str()),
382 return Func(MatcherName, NameRange, Args, Error);
388 std::vector<ArgKind> &Kinds)
const override {
389 Kinds.push_back(ArgsKind);
400 const std::string MatcherName;
401 std::vector<ast_type_traits::ASTNodeKind> RetKinds;
408 template <
typename BaseT,
typename DerivedT>
410 ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT> Func,
411 StringRef MatcherName)
413 DerivedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<DerivedT>()) {
440 #define CHECK_ARG_COUNT(count) \
441 if (Args.size() != count) { \
442 Error->addError(NameRange, Error->ET_RegistryWrongArgCount) \
443 << count << Args.size(); \
444 return VariantMatcher(); \
447 #define CHECK_ARG_TYPE(index, type) \
448 if (!ArgTypeTraits<type>::is(Args[index].Value)) { \
449 Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \
450 << (index + 1) << ArgTypeTraits<type>::getKind().asString() \
451 << Args[index].Value.getTypeAsString(); \
452 return VariantMatcher(); \
456 template <
typename ReturnType>
461 typedef ReturnType (*FuncType)();
467 template <
typename ReturnType,
typename ArgType1>
472 typedef ReturnType (*FuncType)(ArgType1);
480 template <
typename ReturnType,
typename ArgType1,
typename ArgType2>
485 typedef ReturnType (*FuncType)(ArgType1, ArgType2);
494 #undef CHECK_ARG_COUNT
495 #undef CHECK_ARG_TYPE
499 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
500 typename FromTypes,
typename ToTypes>
504 StringRef Name, std::vector<std::unique_ptr<MatcherDescriptor>> &Out)
505 : Name(Name), Out(Out) {
506 collect(FromTypes());
510 typedef ast_matchers::internal::ArgumentAdaptingMatcherFunc<
511 ArgumentAdapterT, FromTypes, ToTypes> AdaptativeFunc;
514 static void collect(ast_matchers::internal::EmptyTypeList) {}
518 template <
typename FromTypeList>
519 inline void collect(FromTypeList);
522 std::vector<std::unique_ptr<MatcherDescriptor>> &Out;
534 : Overloads(std::make_move_iterator(Callbacks.begin()),
535 std::make_move_iterator(Callbacks.end())) {}
542 std::vector<VariantMatcher> Constructed;
544 for (
const auto &O : Overloads) {
546 if (!SubMatcher.
isNull()) {
547 Constructed.push_back(SubMatcher);
554 if (Constructed.size() > 1) {
559 return Constructed[0];
563 bool Overload0Variadic = Overloads[0]->isVariadic();
565 for (
const auto &O : Overloads) {
566 assert(Overload0Variadic == O->isVariadic());
569 return Overload0Variadic;
573 unsigned Overload0NumArgs = Overloads[0]->getNumArgs();
575 for (
const auto &O : Overloads) {
576 assert(Overload0NumArgs == O->getNumArgs());
579 return Overload0NumArgs;
583 std::vector<ArgKind> &Kinds)
const override {
584 for (
const auto &O : Overloads) {
585 if (O->isConvertibleTo(ThisKind))
586 O->getArgKinds(ThisKind, ArgNo, Kinds);
593 for (
const auto &O : Overloads) {
594 if (O->isConvertibleTo(Kind, Specificity, LeastDerivedKind))
601 std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
607 typedef DynTypedMatcher::VariadicOperator
VarOp;
609 VarOp Op, StringRef MatcherName)
610 : MinCount(MinCount), MaxCount(MaxCount), Op(Op),
611 MatcherName(MatcherName) {}
616 if (Args.size() < MinCount || MaxCount < Args.size()) {
617 const std::string MaxStr =
618 (MaxCount ==
UINT_MAX ?
"" : Twine(MaxCount)).str();
620 << (
"(" + Twine(MinCount) +
", " + MaxStr +
")") << Args.size();
624 std::vector<VariantMatcher> InnerArgs;
625 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
641 std::vector<ArgKind> &Kinds)
const override {
642 Kinds.push_back(ThisKind);
648 if (LeastDerivedKind)
649 *LeastDerivedKind =
Kind;
655 const unsigned MinCount;
656 const unsigned MaxCount;
658 const StringRef MatcherName;
665 template <
typename ReturnType>
666 std::unique_ptr<MatcherDescriptor>
668 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
670 return llvm::make_unique<FixedArgCountMatcherDescriptor>(
671 matcherMarshall0<ReturnType>,
reinterpret_cast<void (*)()
>(Func),
672 MatcherName, RetTypes,
None);
676 template <
typename ReturnType,
typename ArgType1>
677 std::unique_ptr<MatcherDescriptor>
679 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
682 return llvm::make_unique<FixedArgCountMatcherDescriptor>(
683 matcherMarshall1<ReturnType, ArgType1>,
684 reinterpret_cast<void (*)()
>(Func), MatcherName, RetTypes, AK);
688 template <
typename ReturnType,
typename ArgType1,
typename ArgType2>
689 std::unique_ptr<MatcherDescriptor>
691 StringRef MatcherName) {
692 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
696 return llvm::make_unique<FixedArgCountMatcherDescriptor>(
697 matcherMarshall2<ReturnType, ArgType1, ArgType2>,
698 reinterpret_cast<void (*)()
>(Func), MatcherName, RetTypes, AKs);
702 template <
typename ResultT,
typename ArgT,
705 ast_matchers::internal::VariadicFunction<ResultT, ArgT, Func> VarFunc,
706 StringRef MatcherName) {
707 return llvm::make_unique<VariadicFuncMatcherDescriptor>(VarFunc, MatcherName);
714 template <
typename BaseT,
typename DerivedT>
716 ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT>
718 StringRef MatcherName) {
719 return llvm::make_unique<DynCastAllOfMatcherDescriptor>(VarFunc, MatcherName);
723 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
724 typename FromTypes,
typename ToTypes>
726 ast_matchers::internal::ArgumentAdaptingMatcherFunc<ArgumentAdapterT,
728 StringRef MatcherName) {
729 std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
732 return llvm::make_unique<OverloadedMatcherDescriptor>(Overloads);
735 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
736 typename FromTypes,
typename ToTypes>
737 template <
typename FromTypeList>
738 inline void AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes,
739 ToTypes>::collect(FromTypeList) {
741 &AdaptativeFunc::template create<typename FromTypeList::head>,
Name));
742 collect(
typename FromTypeList::tail());
746 template <
unsigned MinCount,
unsigned MaxCount>
748 ast_matchers::internal::VariadicOperatorMatcherFunc<MinCount, MaxCount>
750 StringRef MatcherName) {
751 return llvm::make_unique<VariadicOperatorMatcherDescriptor>(
752 MinCount, MaxCount, Func.Op, MatcherName);
760 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H
Variadic operator marshaller function.
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
const MatchFinder::MatchersByType * Matchers
bool isMatcher() const
Matcher value functions.
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
static const CastKind CK_Invalid
std::string getTypeAsString() const
String representation of the type of the value.
VariantMatcher variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
Variadic marshaller function.
static bool is(const VariantValue &Value)
const DynTypedMatcher *const Matcher
bool isPolymorphic() const override
Returns whether the matcher will, given a matcher of any type T, yield a matcher of type T...
static VariantMatcher SingleMatcher(const DynTypedMatcher &Matcher)
Clones the provided matcher.
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
static VariantMatcher VariadicOperatorMatcher(DynTypedMatcher::VariadicOperator Op, std::vector< VariantMatcher > Args)
Creates a 'variadic' operator matcher.
bool isUnsigned() const
Unsigned value functions.
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
FixedArgCountMatcherDescriptor(MarshallerType Marshaller, void(*Func)(), StringRef MatcherName, ArrayRef< ast_type_traits::ASTNodeKind > RetKinds, ArrayRef< ArgKind > ArgKinds)
bool isBaseOf(ASTNodeKind Other, unsigned *Distance=nullptr) const
Returns true if this is a base kind of (or same as) Other.
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Matcher descriptor interface.
OverloadedMatcherDescriptor(MutableArrayRef< std::unique_ptr< MatcherDescriptor >> Callbacks)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
virtual ~MatcherDescriptor()
VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount, VarOp Op, StringRef MatcherName)
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
void revertErrors()
Revert all errors that happened within this context.
static bool is(const VariantValue &Value)
bool isVariadic() const override
Returns whether the matcher is variadic.
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
bool isNull() const
Whether the matcher is null.
Matcher descriptor for variadic functions.
A VariantValue instance annotated with its parser context.
CastKind
CastKind - The kind of operation required for a conversion.
static bool is(const VariantValue &Value)
bool isRetKindConvertibleTo(ArrayRef< ast_type_traits::ASTNodeKind > RetKinds, ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind)
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
~OverloadedMatcherDescriptor() override
DynTypedMatcher::VariadicOperator VarOp
static void build(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
static VariantMatcher matcherMarshall2(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
2-arg marshaller function.
virtual VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const =0
static bool is(const VariantValue &Value)
Diagnostics class to manage error messages.
VariadicFuncMatcherDescriptor(ast_matchers::internal::VariadicFunction< ResultT, ArgT, F > Func, StringRef MatcherName)
AdaptativeOverloadCollector(StringRef Name, std::vector< std::unique_ptr< MatcherDescriptor >> &Out)
Helper class to manage error messages.
static void build(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
ArgStream addError(SourceRange Range, ErrorType Error)
Add an error to the diagnostics.
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
virtual bool isPolymorphic() const
Returns whether the matcher will, given a matcher of any type T, yield a matcher of type T...
bool isVariadic() const override
Returns whether the matcher is variadic.
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
static bool is(const VariantValue &Value)
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Simple callback implementation.
Return CK_Trivial when appropriate for VariadicDynCastAllOfMatchers.
#define CHECK_ARG_COUNT(count)
Helper macros to check the arguments on all marshaller functions.
virtual bool isVariadic() const =0
Returns whether the matcher is variadic.
bool isVariadic() const override
Returns whether the matcher is variadic.
static bool is(const VariantValue &Value)
MatcherDescriptor that wraps multiple "overloads" of the same matcher.
std::unique_ptr< MatcherDescriptor > makeMatcherAutoMarshall(ReturnType(*Func)(), StringRef MatcherName)
Helper functions to select the appropriate marshaller functions.
virtual unsigned getNumArgs() const =0
Returns the number of arguments accepted by the matcher if not variadic.
VariantMatcher(* MarshallerType)(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
bool isSame(ASTNodeKind Other) const
Returns true if this and Other represent the same kind.
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
bool isBoolean() const
Boolean value functions.
static VariantMatcher matcherMarshall0(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
0-arg marshaller function.
static VariantMatcher PolymorphicMatcher(std::vector< DynTypedMatcher > Matchers)
Clones the provided matchers.
static bool is(const VariantValue &Value)
static void mergePolyMatchers(const PolyMatcher &Poly, std::vector< DynTypedMatcher > &Out, ast_matchers::internal::EmptyTypeList)
Helper methods to extract and merge all possible typed matchers out of the polymorphic object...
Helper template class to just from argument type to the right is/get functions in VariantValue...
bool isString() const
String value functions.
Context for overloaded matcher construction.
A variant matcher object.
#define CHECK_ARG_TYPE(index, type)
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
bool isDouble() const
Double value functions.
const std::string & getString() const
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
DynCastAllOfMatcherDescriptor(ast_matchers::internal::VariadicDynCastAllOfMatcher< BaseT, DerivedT > Func, StringRef MatcherName)
static void build(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
#define CAST_OPERATION(Name)
static Decl::Kind getKind(const Decl *D)
virtual void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &ArgKinds) const =0
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher)
Convert the return values of the functions into a VariantMatcher.
Helper class used to collect all the possible overloads of an argument adaptative matcher function...
const VariantMatcher & getMatcher() const
virtual bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity=nullptr, ast_type_traits::ASTNodeKind *LeastDerivedKind=nullptr) const =0
Returns whether this matcher is convertible to the given type.
bool hasTypedMatcher() const
Determines if the contained matcher can be converted to Matcher<T>.
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
static VariantMatcher matcherMarshall1(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
1-arg marshaller function.
void buildReturnTypeVectorFromTypeList(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
bool isVariadic() const override
Returns whether the matcher is variadic.