19 #ifndef LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H 20 #define LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H 30 #include "llvm/ADT/ArrayRef.h" 31 #include "llvm/ADT/None.h" 32 #include "llvm/ADT/STLExtras.h" 33 #include "llvm/ADT/StringRef.h" 34 #include "llvm/ADT/StringSwitch.h" 35 #include "llvm/ADT/Twine.h" 61 return Value.getString();
79 return Value.getMatcher().getTypedMatcher<T>();
83 return ArgKind(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
91 return Value.getBoolean();
103 return Value.getDouble();
115 return Value.getUnsigned();
126 return llvm::StringSwitch<Optional<attr::Kind>>(AttrKind)
128 #include "clang/Basic/AttrList.inc" 138 return *getAttrKind(
Value.getString());
149 return llvm::StringSwitch<Optional<CastKind>>(AttrKind)
151 #include "clang/AST/OperationKinds.def" 161 return *getCastKind(
Value.getString());
172 return llvm::StringSwitch<Optional<OpenMPClauseKind>>(ClauseKind)
174 .Case(
"OMPC_" #TextualSpelling, OMPC_##TextualSpelling)
175 #include "clang/Basic/OpenMPKinds.def" 185 return *getClauseKind(
Value.getString());
205 virtual bool isVariadic()
const = 0;
208 virtual unsigned getNumArgs()
const = 0;
215 std::vector<ArgKind> &ArgKinds)
const = 0;
224 virtual bool isConvertibleTo(
239 if (LeastDerivedKind)
256 StringRef MatcherName,
271 : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName),
272 RetKinds(RetKinds.begin(), RetKinds.end()),
273 ArgKinds(ArgKinds.begin(), ArgKinds.end()) {}
278 return Marshaller(Func, MatcherName, NameRange, Args, Error);
282 unsigned getNumArgs()
const override {
return ArgKinds.size(); }
285 std::vector<ArgKind> &Kinds)
const override {
286 Kinds.push_back(ArgKinds[ArgNo]);
298 void (*
const Func)();
299 const std::string MatcherName;
300 const std::vector<ast_type_traits::ASTNodeKind> RetKinds;
301 const std::vector<ArgKind> ArgKinds;
306 template <
class PolyMatcher>
308 std::vector<DynTypedMatcher> &Out,
309 ast_matchers::internal::EmptyTypeList) {}
311 template <
class PolyMatcher,
class TypeList>
313 std::vector<DynTypedMatcher> &Out, TypeList) {
314 Out.push_back(ast_matchers::internal::Matcher<typename TypeList::head>(Poly));
328 template <
typename T>
330 typename T::ReturnTypes * =
332 std::vector<DynTypedMatcher> Matchers;
338 template <
typename T>
340 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
342 ast_type_traits::ASTNodeKind::getFromNodeKind<typename T::head>());
343 buildReturnTypeVectorFromTypeList<typename T::tail>(RetTypes);
348 buildReturnTypeVectorFromTypeList<ast_matchers::internal::EmptyTypeList>(
349 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {}
351 template <
typename T>
353 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
354 buildReturnTypeVectorFromTypeList<typename T::ReturnTypes>(RetTypes);
358 template <
typename T>
360 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
361 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
365 template <
typename T>
367 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
368 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
373 template <
typename ResultT,
typename ArgT,
378 ArgT **InnerArgs =
new ArgT *[Args.size()]();
380 bool HasError =
false;
381 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
386 if (!ArgTraits::is(Value)) {
392 InnerArgs[i] =
new ArgT(ArgTraits::get(Value));
401 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
422 template <
typename ResultT,
typename ArgT,
425 ast_matchers::internal::VariadicFunction<ResultT, ArgT, F> Func,
426 StringRef MatcherName)
427 : Func(&variadicMatcherDescriptor<ResultT, ArgT, F>),
428 MatcherName(MatcherName.str()),
436 return Func(MatcherName, NameRange, Args, Error);
443 std::vector<ArgKind> &Kinds)
const override {
444 Kinds.push_back(ArgsKind);
456 const std::string MatcherName;
457 std::vector<ast_type_traits::ASTNodeKind> RetKinds;
464 template <
typename BaseT,
typename DerivedT>
466 ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT> Func,
467 StringRef MatcherName)
469 DerivedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<DerivedT>()) {
496 #define CHECK_ARG_COUNT(count) \ 497 if (Args.size() != count) { \ 498 Error->addError(NameRange, Error->ET_RegistryWrongArgCount) \ 499 << count << Args.size(); \ 500 return VariantMatcher(); \ 503 #define CHECK_ARG_TYPE(index, type) \ 504 if (!ArgTypeTraits<type>::is(Args[index].Value)) { \ 505 Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \ 506 << (index + 1) << ArgTypeTraits<type>::getKind().asString() \ 507 << Args[index].Value.getTypeAsString(); \ 508 return VariantMatcher(); \ 512 template <
typename ReturnType>
517 using FuncType = ReturnType (*)();
523 template <
typename ReturnType,
typename ArgType1>
528 using FuncType = ReturnType (*)(ArgType1);
536 template <
typename ReturnType,
typename ArgType1,
typename ArgType2>
541 using FuncType = ReturnType (*)(ArgType1, ArgType2);
550 #undef CHECK_ARG_COUNT 551 #undef CHECK_ARG_TYPE 555 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
556 typename FromTypes,
typename ToTypes>
560 StringRef Name, std::vector<std::unique_ptr<MatcherDescriptor>> &Out)
561 : Name(Name), Out(Out) {
562 collect(FromTypes());
566 using AdaptativeFunc = ast_matchers::internal::ArgumentAdaptingMatcherFunc<
567 ArgumentAdapterT, FromTypes, ToTypes>;
570 static void collect(ast_matchers::internal::EmptyTypeList) {}
574 template <
typename FromTypeList>
575 inline void collect(FromTypeList);
578 std::vector<std::unique_ptr<MatcherDescriptor>> &Out;
590 : Overloads(
std::make_move_iterator(Callbacks.begin()),
591 std::make_move_iterator(Callbacks.end())) {}
598 std::vector<VariantMatcher> Constructed;
600 for (
const auto &O : Overloads) {
602 if (!SubMatcher.
isNull()) {
603 Constructed.push_back(SubMatcher);
610 if (Constructed.size() > 1) {
615 return Constructed[0];
619 bool Overload0Variadic = Overloads[0]->isVariadic();
621 for (
const auto &O : Overloads) {
622 assert(Overload0Variadic == O->isVariadic());
625 return Overload0Variadic;
629 unsigned Overload0NumArgs = Overloads[0]->getNumArgs();
631 for (
const auto &O : Overloads) {
632 assert(Overload0NumArgs == O->getNumArgs());
635 return Overload0NumArgs;
639 std::vector<ArgKind> &Kinds)
const override {
640 for (
const auto &O : Overloads) {
641 if (O->isConvertibleTo(ThisKind))
642 O->getArgKinds(ThisKind, ArgNo, Kinds);
649 for (
const auto &O : Overloads) {
650 if (O->isConvertibleTo(Kind, Specificity, LeastDerivedKind))
657 std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
663 using VarOp = DynTypedMatcher::VariadicOperator;
666 VarOp Op, StringRef MatcherName)
667 : MinCount(MinCount), MaxCount(MaxCount), Op(Op),
668 MatcherName(MatcherName) {}
673 if (Args.size() < MinCount || MaxCount < Args.size()) {
674 const std::string MaxStr =
679 << (
"(" + Twine(MinCount) +
", " + MaxStr +
")") << Args.size();
683 std::vector<VariantMatcher> InnerArgs;
684 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
701 std::vector<ArgKind> &Kinds)
const override {
702 Kinds.push_back(ThisKind);
709 if (LeastDerivedKind)
710 *LeastDerivedKind =
Kind;
717 const unsigned MinCount;
718 const unsigned MaxCount;
720 const StringRef MatcherName;
727 template <
typename ReturnType>
728 std::unique_ptr<MatcherDescriptor>
730 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
732 return std::make_unique<FixedArgCountMatcherDescriptor>(
733 matcherMarshall0<ReturnType>,
reinterpret_cast<void (*)()
>(Func),
734 MatcherName, RetTypes, None);
738 template <
typename ReturnType,
typename ArgType1>
739 std::unique_ptr<MatcherDescriptor>
741 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
744 return std::make_unique<FixedArgCountMatcherDescriptor>(
745 matcherMarshall1<ReturnType, ArgType1>,
746 reinterpret_cast<void (*)()
>(Func), MatcherName, RetTypes, AK);
750 template <
typename ReturnType,
typename ArgType1,
typename ArgType2>
751 std::unique_ptr<MatcherDescriptor>
753 StringRef MatcherName) {
754 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
758 return std::make_unique<FixedArgCountMatcherDescriptor>(
759 matcherMarshall2<ReturnType, ArgType1, ArgType2>,
760 reinterpret_cast<void (*)()
>(Func), MatcherName, RetTypes, AKs);
764 template <
typename ResultT,
typename ArgT,
767 ast_matchers::internal::VariadicFunction<ResultT, ArgT, Func> VarFunc,
768 StringRef MatcherName) {
769 return std::make_unique<VariadicFuncMatcherDescriptor>(VarFunc, MatcherName);
776 template <
typename BaseT,
typename DerivedT>
778 ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT>
780 StringRef MatcherName) {
781 return std::make_unique<DynCastAllOfMatcherDescriptor>(VarFunc, MatcherName);
785 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
786 typename FromTypes,
typename ToTypes>
788 ast_matchers::internal::ArgumentAdaptingMatcherFunc<ArgumentAdapterT,
790 StringRef MatcherName) {
791 std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
794 return std::make_unique<OverloadedMatcherDescriptor>(Overloads);
797 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
798 typename FromTypes,
typename ToTypes>
799 template <
typename FromTypeList>
801 ToTypes>::collect(FromTypeList) {
803 &AdaptativeFunc::template create<typename FromTypeList::head>, Name));
804 collect(
typename FromTypeList::tail());
808 template <
unsigned MinCount,
unsigned MaxCount>
810 ast_matchers::internal::VariadicOperatorMatcherFunc<MinCount, MaxCount>
812 StringRef MatcherName) {
813 return std::make_unique<VariadicOperatorMatcherDescriptor>(
814 MinCount, MaxCount, Func.Op, MatcherName);
822 #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...
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
bool hasTypedMatcher() const
Determines if the contained matcher can be converted to Matcher<T>.
static bool is(const VariantValue &Value)
VariantMatcher variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
Variadic marshaller function.
#define OPENMP_CLAUSE(TextualSpelling, Class)
bool isSame(ASTNodeKind Other) const
Returns true if this and Other represent the same kind.
static bool is(const VariantValue &Value)
bool isPolymorphic() const override
Returns whether the matcher will, given a matcher of any type T, yield a matcher of type T...
VariantMatcher(*)(StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) RunFunc
static VariantMatcher SingleMatcher(const DynTypedMatcher &Matcher)
Clones the provided matcher.
bool isBoolean() const
Boolean value functions.
bool isUnsigned() const
Unsigned value functions.
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.
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)
Defines the clang::attr::Kind enum.
Defines some OpenMP-specific enums and functions.
__DEVICE__ int max(int __a, int __b)
virtual bool isPolymorphic() const
Returns whether the matcher will, given a matcher of any type T, yield a matcher of type T...
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...
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.
Matcher descriptor for variadic functions.
A VariantValue instance annotated with its parser context.
CastKind
CastKind - The kind of operation required for a conversion.
std::string getTypeAsString() const
String representation of the type of the value.
static bool is(const VariantValue &Value)
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.
OpenMPClauseKind
OpenMP clauses.
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.
static bool is(const VariantValue &Value)
Diagnostics class to manage error messages.
bool isDouble() const
Double value functions.
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.
NodeKind
A kind of a syntax node, used for implementing casts.
static void build(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
DynTypedMatcher::VariadicOperator VarOp
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.
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...
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Simple callback implementation.
const VariantMatcher & getMatcher() const
Return CK_Trivial when appropriate for VariadicDynCastAllOfMatchers.
#define CHECK_ARG_COUNT(count)
Helper macros to check the arguments on all marshaller functions.
const std::string & getString() const
bool isVariadic() const override
Returns whether the matcher is variadic.
bool isBaseOf(ASTNodeKind Other, unsigned *Distance=nullptr) const
Returns true if this is a base kind of (or same as) Other.
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.
Dataflow Directional Tag Classes.
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 isMatcher() const
Matcher value functions.
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
static VariantMatcher matcherMarshall0(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
0-arg marshaller function.
bool isNull() const
Whether the matcher is null.
static VariantMatcher PolymorphicMatcher(std::vector< DynTypedMatcher > Matchers)
Clones the provided matchers.
static bool is(const VariantValue &Value)
VariantMatcher(*)(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) MarshallerType
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...
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 ...
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)
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...
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
bool isString() const
String value functions.
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.