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.getUnsigned();
80 static attr::Kind getAttrKind(llvm::StringRef AttrKind) {
81 return llvm::StringSwitch<attr::Kind>(AttrKind)
83 #include "clang/Basic/AttrList.inc"
92 return getAttrKind(
Value.getString());
102 return llvm::StringSwitch<clang::CastKind>(AttrKind)
104 #include "clang/AST/OperationKinds.def"
114 return getCastKind(
Value.getString());
144 std::vector<ArgKind> &ArgKinds)
const = 0;
167 if (
ArgKind(NodeKind).isConvertibleTo(Kind, Specificity)) {
168 if (LeastDerivedKind)
169 *LeastDerivedKind = NodeKind;
185 StringRef MatcherName,
200 : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName),
201 RetKinds(RetKinds.
begin(), RetKinds.
end()),
202 ArgKinds(ArgKinds.
begin(), ArgKinds.
end()) {}
207 return Marshaller(Func, MatcherName, NameRange, Args, Error);
211 unsigned getNumArgs()
const override {
return ArgKinds.size(); }
213 std::vector<ArgKind> &Kinds)
const override {
214 Kinds.push_back(ArgKinds[ArgNo]);
225 void (*
const Func)();
226 const std::string MatcherName;
227 const std::vector<ast_type_traits::ASTNodeKind> RetKinds;
228 const std::vector<ArgKind> ArgKinds;
233 template <
class PolyMatcher>
235 std::vector<DynTypedMatcher> &Out,
236 ast_matchers::internal::EmptyTypeList) {}
238 template <
class PolyMatcher,
class TypeList>
240 std::vector<DynTypedMatcher> &Out, TypeList) {
241 Out.push_back(ast_matchers::internal::Matcher<typename TypeList::head>(Poly));
255 template <
typename T>
257 typename T::ReturnTypes * =
259 std::vector<DynTypedMatcher>
Matchers;
265 template <
typename T>
267 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
269 ast_type_traits::ASTNodeKind::getFromNodeKind<typename T::head>());
270 buildReturnTypeVectorFromTypeList<typename T::tail>(RetTypes);
275 buildReturnTypeVectorFromTypeList<ast_matchers::internal::EmptyTypeList>(
276 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {}
278 template <
typename T>
280 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
281 buildReturnTypeVectorFromTypeList<typename T::ReturnTypes>(RetTypes);
285 template <
typename T>
287 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
288 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
292 template <
typename T>
294 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
295 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
300 template <
typename ResultT,
typename ArgT,
305 ArgT **InnerArgs =
new ArgT *[Args.size()]();
307 bool HasError =
false;
308 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
312 if (!ArgTraits::is(Value)) {
318 InnerArgs[i] =
new ArgT(ArgTraits::get(Value));
327 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
348 template <
typename ResultT,
typename ArgT,
351 ast_matchers::internal::VariadicFunction<ResultT, ArgT, F> Func,
352 StringRef MatcherName)
353 : Func(&variadicMatcherDescriptor<ResultT, ArgT, F>),
354 MatcherName(MatcherName.str()),
362 return Func(MatcherName, NameRange, Args, Error);
368 std::vector<ArgKind> &Kinds)
const override {
369 Kinds.push_back(ArgsKind);
380 const std::string MatcherName;
381 std::vector<ast_type_traits::ASTNodeKind> RetKinds;
388 template <
typename BaseT,
typename DerivedT>
390 ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT> Func,
391 StringRef MatcherName)
393 DerivedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<DerivedT>()) {
420 #define CHECK_ARG_COUNT(count) \
421 if (Args.size() != count) { \
422 Error->addError(NameRange, Error->ET_RegistryWrongArgCount) \
423 << count << Args.size(); \
424 return VariantMatcher(); \
427 #define CHECK_ARG_TYPE(index, type) \
428 if (!ArgTypeTraits<type>::is(Args[index].Value)) { \
429 Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \
430 << (index + 1) << ArgTypeTraits<type>::getKind().asString() \
431 << Args[index].Value.getTypeAsString(); \
432 return VariantMatcher(); \
436 template <
typename ReturnType>
441 typedef ReturnType (*FuncType)();
447 template <
typename ReturnType,
typename ArgType1>
452 typedef ReturnType (*FuncType)(ArgType1);
460 template <
typename ReturnType,
typename ArgType1,
typename ArgType2>
465 typedef ReturnType (*FuncType)(ArgType1, ArgType2);
474 #undef CHECK_ARG_COUNT
475 #undef CHECK_ARG_TYPE
479 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
480 typename FromTypes,
typename ToTypes>
484 std::vector<MatcherDescriptor *> &Out)
485 : Name(Name), Out(Out) {
486 collect(FromTypes());
490 typedef ast_matchers::internal::ArgumentAdaptingMatcherFunc<
491 ArgumentAdapterT, FromTypes, ToTypes> AdaptativeFunc;
494 static void collect(ast_matchers::internal::EmptyTypeList) {}
498 template <
typename FromTypeList>
499 inline void collect(FromTypeList);
502 std::vector<MatcherDescriptor *> &Out;
513 : Overloads(Callbacks.
begin(), Callbacks.
end()) {}
520 std::vector<VariantMatcher> Constructed;
522 for (
const auto &O : Overloads) {
524 if (!SubMatcher.
isNull()) {
525 Constructed.push_back(SubMatcher);
532 if (Constructed.size() > 1) {
537 return Constructed[0];
541 bool Overload0Variadic = Overloads[0]->isVariadic();
543 for (
const auto &O : Overloads) {
544 assert(Overload0Variadic == O->isVariadic());
547 return Overload0Variadic;
551 unsigned Overload0NumArgs = Overloads[0]->getNumArgs();
553 for (
const auto &O : Overloads) {
554 assert(Overload0NumArgs == O->getNumArgs());
557 return Overload0NumArgs;
561 std::vector<ArgKind> &Kinds)
const override {
562 for (
const auto &O : Overloads) {
563 if (O->isConvertibleTo(ThisKind))
564 O->getArgKinds(ThisKind, ArgNo, Kinds);
571 for (
const auto &O : Overloads) {
572 if (O->isConvertibleTo(Kind, Specificity, LeastDerivedKind))
579 std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
585 typedef DynTypedMatcher::VariadicOperator
VarOp;
587 VarOp Op, StringRef MatcherName)
588 : MinCount(MinCount), MaxCount(MaxCount), Op(Op),
589 MatcherName(MatcherName) {}
594 if (Args.size() < MinCount || MaxCount < Args.size()) {
595 const std::string MaxStr =
596 (MaxCount ==
UINT_MAX ?
"" : Twine(MaxCount)).str();
598 << (
"(" + Twine(MinCount) +
", " + MaxStr +
")") << Args.size();
602 std::vector<VariantMatcher> InnerArgs;
603 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
619 std::vector<ArgKind> &Kinds)
const override {
620 Kinds.push_back(ThisKind);
626 if (LeastDerivedKind)
627 *LeastDerivedKind =
Kind;
633 const unsigned MinCount;
634 const unsigned MaxCount;
636 const StringRef MatcherName;
643 template <
typename ReturnType>
645 StringRef MatcherName) {
646 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
649 matcherMarshall0<ReturnType>,
reinterpret_cast<void (*)()
>(Func),
650 MatcherName, RetTypes,
None);
654 template <
typename ReturnType,
typename ArgType1>
656 StringRef MatcherName) {
657 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
661 matcherMarshall1<ReturnType, ArgType1>,
662 reinterpret_cast<void (*)()
>(Func), MatcherName, RetTypes, AK);
666 template <
typename ReturnType,
typename ArgType1,
typename ArgType2>
668 StringRef MatcherName) {
669 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
674 matcherMarshall2<ReturnType, ArgType1, ArgType2>,
675 reinterpret_cast<void (*)()
>(Func), MatcherName, RetTypes, AKs);
679 template <
typename ResultT,
typename ArgT,
682 ast_matchers::internal::VariadicFunction<ResultT, ArgT, Func> VarFunc,
683 StringRef MatcherName) {
691 template <
typename BaseT,
typename DerivedT>
694 BaseT, DerivedT> VarFunc,
695 StringRef MatcherName) {
700 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
701 typename FromTypes,
typename ToTypes>
704 ArgumentAdapterT, FromTypes, ToTypes>,
705 StringRef MatcherName) {
706 std::vector<MatcherDescriptor *> Overloads;
712 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
713 typename FromTypes,
typename ToTypes>
714 template <
typename FromTypeList>
715 inline void AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes,
716 ToTypes>::collect(FromTypeList) {
718 &AdaptativeFunc::template create<typename FromTypeList::head>,
Name));
719 collect(
typename FromTypeList::tail());
723 template <
unsigned MinCount,
unsigned MaxCount>
726 MinCount, MaxCount> Func,
727 StringRef MatcherName) {
737 #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.
AdaptativeOverloadCollector(StringRef Name, std::vector< MatcherDescriptor * > &Out)
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.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
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.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
virtual ~MatcherDescriptor()
OverloadedMatcherDescriptor(ArrayRef< MatcherDescriptor * > Callbacks)
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)
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.
MatcherDescriptor * makeMatcherAutoMarshall(ReturnType(*Func)(), StringRef MatcherName)
Helper functions to select the appropriate marshaller functions.
#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.
MatcherDescriptor that wraps multiple "overloads" of the same matcher.
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.
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 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 ...
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.