20 #ifndef LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H 21 #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());
183 virtual bool isVariadic()
const = 0;
186 virtual unsigned getNumArgs()
const = 0;
193 std::vector<ArgKind> &ArgKinds)
const = 0;
202 virtual bool isConvertibleTo(
216 if (
ArgKind(NodeKind).isConvertibleTo(Kind, Specificity)) {
217 if (LeastDerivedKind)
218 *LeastDerivedKind = NodeKind;
234 StringRef MatcherName,
249 : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName),
250 RetKinds(RetKinds.begin(), RetKinds.end()),
251 ArgKinds(ArgKinds.begin(), ArgKinds.end()) {}
256 return Marshaller(Func, MatcherName, NameRange, Args, Error);
260 unsigned getNumArgs()
const override {
return ArgKinds.size(); }
263 std::vector<ArgKind> &Kinds)
const override {
264 Kinds.push_back(ArgKinds[ArgNo]);
276 void (*
const Func)();
277 const std::string MatcherName;
278 const std::vector<ast_type_traits::ASTNodeKind> RetKinds;
279 const std::vector<ArgKind> ArgKinds;
284 template <
class PolyMatcher>
286 std::vector<DynTypedMatcher> &Out,
287 ast_matchers::internal::EmptyTypeList) {}
289 template <
class PolyMatcher,
class TypeList>
291 std::vector<DynTypedMatcher> &Out, TypeList) {
292 Out.push_back(ast_matchers::internal::Matcher<typename TypeList::head>(Poly));
306 template <
typename T>
308 typename T::ReturnTypes * =
310 std::vector<DynTypedMatcher> Matchers;
316 template <
typename T>
318 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
320 ast_type_traits::ASTNodeKind::getFromNodeKind<typename T::head>());
321 buildReturnTypeVectorFromTypeList<typename T::tail>(RetTypes);
326 buildReturnTypeVectorFromTypeList<ast_matchers::internal::EmptyTypeList>(
327 std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {}
329 template <
typename T>
331 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
332 buildReturnTypeVectorFromTypeList<typename T::ReturnTypes>(RetTypes);
336 template <
typename T>
338 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
339 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
343 template <
typename T>
345 static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
346 RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
351 template <
typename ResultT,
typename ArgT,
356 ArgT **InnerArgs =
new ArgT *[Args.size()]();
358 bool HasError =
false;
359 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
364 if (!ArgTraits::is(Value)) {
370 InnerArgs[i] =
new ArgT(ArgTraits::get(Value));
379 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
400 template <
typename ResultT,
typename ArgT,
403 ast_matchers::internal::VariadicFunction<ResultT, ArgT, F> Func,
404 StringRef MatcherName)
405 : Func(&variadicMatcherDescriptor<ResultT, ArgT, F>),
406 MatcherName(MatcherName.str()),
414 return Func(MatcherName, NameRange, Args, Error);
421 std::vector<ArgKind> &Kinds)
const override {
422 Kinds.push_back(ArgsKind);
434 const std::string MatcherName;
435 std::vector<ast_type_traits::ASTNodeKind> RetKinds;
442 template <
typename BaseT,
typename DerivedT>
444 ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT> Func,
445 StringRef MatcherName)
447 DerivedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<DerivedT>()) {
474 #define CHECK_ARG_COUNT(count) \ 475 if (Args.size() != count) { \ 476 Error->addError(NameRange, Error->ET_RegistryWrongArgCount) \ 477 << count << Args.size(); \ 478 return VariantMatcher(); \ 481 #define CHECK_ARG_TYPE(index, type) \ 482 if (!ArgTypeTraits<type>::is(Args[index].Value)) { \ 483 Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \ 484 << (index + 1) << ArgTypeTraits<type>::getKind().asString() \ 485 << Args[index].Value.getTypeAsString(); \ 486 return VariantMatcher(); \ 490 template <
typename ReturnType>
495 using FuncType = ReturnType (*)();
501 template <
typename ReturnType,
typename ArgType1>
506 using FuncType = ReturnType (*)(ArgType1);
514 template <
typename ReturnType,
typename ArgType1,
typename ArgType2>
519 using FuncType = ReturnType (*)(ArgType1, ArgType2);
528 #undef CHECK_ARG_COUNT 529 #undef CHECK_ARG_TYPE 533 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
534 typename FromTypes,
typename ToTypes>
538 StringRef Name, std::vector<std::unique_ptr<MatcherDescriptor>> &Out)
539 : Name(Name), Out(Out) {
540 collect(FromTypes());
544 using AdaptativeFunc = ast_matchers::internal::ArgumentAdaptingMatcherFunc<
545 ArgumentAdapterT, FromTypes, ToTypes>;
548 static void collect(ast_matchers::internal::EmptyTypeList) {}
552 template <
typename FromTypeList>
553 inline void collect(FromTypeList);
556 std::vector<std::unique_ptr<MatcherDescriptor>> &Out;
568 : Overloads(
std::make_move_iterator(Callbacks.begin()),
569 std::make_move_iterator(Callbacks.end())) {}
576 std::vector<VariantMatcher> Constructed;
578 for (
const auto &O : Overloads) {
580 if (!SubMatcher.
isNull()) {
581 Constructed.push_back(SubMatcher);
588 if (Constructed.size() > 1) {
593 return Constructed[0];
597 bool Overload0Variadic = Overloads[0]->isVariadic();
599 for (
const auto &O : Overloads) {
600 assert(Overload0Variadic == O->isVariadic());
603 return Overload0Variadic;
607 unsigned Overload0NumArgs = Overloads[0]->getNumArgs();
609 for (
const auto &O : Overloads) {
610 assert(Overload0NumArgs == O->getNumArgs());
613 return Overload0NumArgs;
617 std::vector<ArgKind> &Kinds)
const override {
618 for (
const auto &O : Overloads) {
619 if (O->isConvertibleTo(ThisKind))
620 O->getArgKinds(ThisKind, ArgNo, Kinds);
627 for (
const auto &O : Overloads) {
628 if (O->isConvertibleTo(Kind, Specificity, LeastDerivedKind))
635 std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
641 using VarOp = DynTypedMatcher::VariadicOperator;
644 VarOp Op, StringRef MatcherName)
645 : MinCount(MinCount), MaxCount(MaxCount), Op(Op),
646 MatcherName(MatcherName) {}
651 if (Args.size() < MinCount || MaxCount < Args.size()) {
652 const std::string MaxStr =
657 << (
"(" + Twine(MinCount) +
", " + MaxStr +
")") << Args.size();
661 std::vector<VariantMatcher> InnerArgs;
662 for (
size_t i = 0, e = Args.size(); i != e; ++i) {
679 std::vector<ArgKind> &Kinds)
const override {
680 Kinds.push_back(ThisKind);
687 if (LeastDerivedKind)
688 *LeastDerivedKind =
Kind;
695 const unsigned MinCount;
696 const unsigned MaxCount;
698 const StringRef MatcherName;
705 template <
typename ReturnType>
706 std::unique_ptr<MatcherDescriptor>
708 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
710 return llvm::make_unique<FixedArgCountMatcherDescriptor>(
711 matcherMarshall0<ReturnType>,
reinterpret_cast<void (*)()
>(Func),
712 MatcherName, RetTypes, None);
716 template <
typename ReturnType,
typename ArgType1>
717 std::unique_ptr<MatcherDescriptor>
719 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
722 return llvm::make_unique<FixedArgCountMatcherDescriptor>(
723 matcherMarshall1<ReturnType, ArgType1>,
724 reinterpret_cast<void (*)()
>(Func), MatcherName, RetTypes, AK);
728 template <
typename ReturnType,
typename ArgType1,
typename ArgType2>
729 std::unique_ptr<MatcherDescriptor>
731 StringRef MatcherName) {
732 std::vector<ast_type_traits::ASTNodeKind> RetTypes;
736 return llvm::make_unique<FixedArgCountMatcherDescriptor>(
737 matcherMarshall2<ReturnType, ArgType1, ArgType2>,
738 reinterpret_cast<void (*)()
>(Func), MatcherName, RetTypes, AKs);
742 template <
typename ResultT,
typename ArgT,
745 ast_matchers::internal::VariadicFunction<ResultT, ArgT, Func> VarFunc,
746 StringRef MatcherName) {
747 return llvm::make_unique<VariadicFuncMatcherDescriptor>(VarFunc, MatcherName);
754 template <
typename BaseT,
typename DerivedT>
756 ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT>
758 StringRef MatcherName) {
759 return llvm::make_unique<DynCastAllOfMatcherDescriptor>(VarFunc, MatcherName);
763 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
764 typename FromTypes,
typename ToTypes>
766 ast_matchers::internal::ArgumentAdaptingMatcherFunc<ArgumentAdapterT,
768 StringRef MatcherName) {
769 std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
772 return llvm::make_unique<OverloadedMatcherDescriptor>(Overloads);
775 template <
template <
typename ToArg,
typename FromArg>
class ArgumentAdapterT,
776 typename FromTypes,
typename ToTypes>
777 template <
typename FromTypeList>
779 ToTypes>::collect(FromTypeList) {
781 &AdaptativeFunc::template create<typename FromTypeList::head>, Name));
782 collect(
typename FromTypeList::tail());
786 template <
unsigned MinCount,
unsigned MaxCount>
788 ast_matchers::internal::VariadicOperatorMatcherFunc<MinCount, MaxCount>
790 StringRef MatcherName) {
791 return llvm::make_unique<VariadicOperatorMatcherDescriptor>(
792 MinCount, MaxCount, Func.Op, MatcherName);
800 #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>.
VariantMatcher variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
Variadic marshaller function.
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.
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.
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.
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)
__DEVICE__ int max(int __a, int __b)
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.