Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 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 | |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 23 | #include <string> |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 24 | |
| 25 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 26 | #include "clang/ASTMatchers/Dynamic/Diagnostics.h" |
| 27 | #include "clang/ASTMatchers/Dynamic/VariantValue.h" |
| 28 | #include "clang/Basic/LLVM.h" |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 30 | #include "llvm/Support/type_traits.h" |
| 31 | |
| 32 | namespace clang { |
| 33 | namespace ast_matchers { |
| 34 | namespace dynamic { |
| 35 | |
| 36 | namespace internal { |
| 37 | |
| 38 | /// \brief Helper template class to just from argument type to the right is/get |
| 39 | /// functions in VariantValue. |
| 40 | /// Used to verify and extract the matcher arguments below. |
| 41 | template <class T> struct ArgTypeTraits; |
| 42 | template <class T> struct ArgTypeTraits<const T &> : public ArgTypeTraits<T> { |
| 43 | }; |
| 44 | |
| 45 | template <> struct ArgTypeTraits<std::string> { |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 46 | static StringRef asString() { return "String"; } |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 47 | static bool is(const VariantValue &Value) { return Value.isString(); } |
| 48 | static const std::string &get(const VariantValue &Value) { |
| 49 | return Value.getString(); |
| 50 | } |
| 51 | }; |
| 52 | |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 53 | template <> |
| 54 | struct ArgTypeTraits<StringRef> : public ArgTypeTraits<std::string> { |
| 55 | }; |
| 56 | |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 57 | template <class T> struct ArgTypeTraits<ast_matchers::internal::Matcher<T> > { |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 58 | static std::string asString() { |
| 59 | return (Twine("Matcher<") + |
| 60 | ast_type_traits::ASTNodeKind::getFromNodeKind<T>().asStringRef() + |
| 61 | ">").str(); |
| 62 | } |
| 63 | static bool is(const VariantValue &Value) { |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 64 | return Value.isMatcher() && Value.getMatcher().hasTypedMatcher<T>(); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 65 | } |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 66 | static ast_matchers::internal::Matcher<T> get(const VariantValue &Value) { |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 67 | return Value.getMatcher().getTypedMatcher<T>(); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 68 | } |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 69 | }; |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 70 | |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 71 | template <> struct ArgTypeTraits<unsigned> { |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 72 | static std::string asString() { return "Unsigned"; } |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 73 | static bool is(const VariantValue &Value) { return Value.isUnsigned(); } |
| 74 | static unsigned get(const VariantValue &Value) { |
| 75 | return Value.getUnsigned(); |
| 76 | } |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 79 | /// \brief Matcher descriptor interface. |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 80 | /// |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 81 | /// Provides a \c create() method that constructs the matcher from the provided |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 82 | /// arguments. |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 83 | class MatcherDescriptor { |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 84 | public: |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 85 | virtual ~MatcherDescriptor() {} |
| 86 | virtual VariantMatcher create(const SourceRange &NameRange, |
| 87 | ArrayRef<ParserValue> Args, |
| 88 | Diagnostics *Error) const = 0; |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /// \brief Simple callback implementation. Marshaller and function are provided. |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 92 | /// |
| 93 | /// This class wraps a function of arbitrary signature and a marshaller |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 94 | /// function into a MatcherDescriptor. |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 95 | /// The marshaller is in charge of taking the VariantValue arguments, checking |
| 96 | /// their types, unpacking them and calling the underlying function. |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 97 | class FixedArgCountMatcherDescriptor : public MatcherDescriptor { |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 98 | public: |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 99 | typedef VariantMatcher (*MarshallerType)(void (*Func)(), |
| 100 | StringRef MatcherName, |
| 101 | const SourceRange &NameRange, |
| 102 | ArrayRef<ParserValue> Args, |
| 103 | Diagnostics *Error); |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 104 | |
Dmitri Gribenko | cb63baf | 2013-05-17 17:50:16 +0000 | [diff] [blame] | 105 | /// \param Marshaller Function to unpack the arguments and call \c Func |
| 106 | /// \param Func Matcher construct function. This is the function that |
| 107 | /// compile-time matcher expressions would use to create the matcher. |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 108 | FixedArgCountMatcherDescriptor(MarshallerType Marshaller, void (*Func)(), |
| 109 | StringRef MatcherName) |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 110 | : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName) {} |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 111 | |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 112 | VariantMatcher create(const SourceRange &NameRange, |
| 113 | ArrayRef<ParserValue> Args, Diagnostics *Error) const { |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 114 | return Marshaller(Func, MatcherName, NameRange, Args, Error); |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | const MarshallerType Marshaller; |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 119 | void (* const Func)(); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 120 | const std::string MatcherName; |
| 121 | }; |
| 122 | |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 123 | /// \brief Simple callback implementation. Free function is wrapped. |
| 124 | /// |
| 125 | /// This class simply wraps a free function with the right signature to export |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 126 | /// it as a MatcherDescriptor. |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 127 | /// This allows us to have one implementation of the interface for as many free |
| 128 | /// functions as we want, reducing the number of symbols and size of the |
| 129 | /// object file. |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 130 | class FreeFuncMatcherDescriptor : public MatcherDescriptor { |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 131 | public: |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 132 | typedef VariantMatcher (*RunFunc)(StringRef MatcherName, |
| 133 | const SourceRange &NameRange, |
| 134 | ArrayRef<ParserValue> Args, |
| 135 | Diagnostics *Error); |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 136 | |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 137 | FreeFuncMatcherDescriptor(RunFunc Func, StringRef MatcherName) |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 138 | : Func(Func), MatcherName(MatcherName.str()) {} |
| 139 | |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 140 | VariantMatcher create(const SourceRange &NameRange, |
| 141 | ArrayRef<ParserValue> Args, Diagnostics *Error) const { |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 142 | return Func(MatcherName, NameRange, Args, Error); |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | const RunFunc Func; |
| 147 | const std::string MatcherName; |
| 148 | }; |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 149 | |
| 150 | /// \brief Helper macros to check the arguments on all marshaller functions. |
| 151 | #define CHECK_ARG_COUNT(count) \ |
| 152 | if (Args.size() != count) { \ |
Samuel Benzaquen | a37bb8c | 2013-07-18 19:47:59 +0000 | [diff] [blame] | 153 | Error->addError(NameRange, Error->ET_RegistryWrongArgCount) \ |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 154 | << count << Args.size(); \ |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 155 | return VariantMatcher(); \ |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | #define CHECK_ARG_TYPE(index, type) \ |
| 159 | if (!ArgTypeTraits<type>::is(Args[index].Value)) { \ |
Samuel Benzaquen | a37bb8c | 2013-07-18 19:47:59 +0000 | [diff] [blame] | 160 | Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \ |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 161 | << (index + 1) << ArgTypeTraits<type>::asString() \ |
| 162 | << Args[index].Value.getTypeAsString(); \ |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 163 | return VariantMatcher(); \ |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 166 | /// \brief Helper methods to extract and merge all possible typed matchers |
| 167 | /// out of the polymorphic object. |
| 168 | template <class PolyMatcher> |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 169 | static void mergePolyMatchers(const PolyMatcher &Poly, |
Samuel Benzaquen | f34ac3e | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 170 | std::vector<DynTypedMatcher> &Out, |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 171 | ast_matchers::internal::EmptyTypeList) {} |
| 172 | |
| 173 | template <class PolyMatcher, class TypeList> |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 174 | static void mergePolyMatchers(const PolyMatcher &Poly, |
Samuel Benzaquen | f34ac3e | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 175 | std::vector<DynTypedMatcher> &Out, TypeList) { |
| 176 | Out.push_back(ast_matchers::internal::Matcher<typename TypeList::head>(Poly)); |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 177 | mergePolyMatchers(Poly, Out, typename TypeList::tail()); |
| 178 | } |
| 179 | |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 180 | /// \brief Convert the return values of the functions into a VariantMatcher. |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 181 | /// |
| 182 | /// There are 2 cases right now: The return value is a Matcher<T> or is a |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 183 | /// polymorphic matcher. For the former, we just construct the VariantMatcher. |
| 184 | /// For the latter, we instantiate all the possible Matcher<T> of the poly |
| 185 | /// matcher. |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 186 | static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher) { |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 187 | return VariantMatcher::SingleMatcher(Matcher); |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | template <typename T> |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 191 | static VariantMatcher outvalueToVariantMatcher(const T &PolyMatcher, |
| 192 | typename T::ReturnTypes * = |
| 193 | NULL) { |
Samuel Benzaquen | f34ac3e | 2013-10-29 14:37:15 +0000 | [diff] [blame] | 194 | std::vector<DynTypedMatcher> Matchers; |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 195 | mergePolyMatchers(PolyMatcher, Matchers, typename T::ReturnTypes()); |
| 196 | VariantMatcher Out = VariantMatcher::PolymorphicMatcher(Matchers); |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 197 | return Out; |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 200 | /// \brief 0-arg marshaller function. |
| 201 | template <typename ReturnType> |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 202 | static VariantMatcher matcherMarshall0(void (*Func)(), StringRef MatcherName, |
| 203 | const SourceRange &NameRange, |
| 204 | ArrayRef<ParserValue> Args, |
| 205 | Diagnostics *Error) { |
| 206 | typedef ReturnType (*FuncType)(); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 207 | CHECK_ARG_COUNT(0); |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 208 | return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /// \brief 1-arg marshaller function. |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 212 | template <typename ReturnType, typename ArgType1> |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 213 | static VariantMatcher matcherMarshall1(void (*Func)(), StringRef MatcherName, |
| 214 | const SourceRange &NameRange, |
| 215 | ArrayRef<ParserValue> Args, |
| 216 | Diagnostics *Error) { |
| 217 | typedef ReturnType (*FuncType)(ArgType1); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 218 | CHECK_ARG_COUNT(1); |
| 219 | CHECK_ARG_TYPE(0, ArgType1); |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 220 | return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)( |
| 221 | ArgTypeTraits<ArgType1>::get(Args[0].Value))); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 224 | /// \brief 2-arg marshaller function. |
| 225 | template <typename ReturnType, typename ArgType1, typename ArgType2> |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 226 | static VariantMatcher matcherMarshall2(void (*Func)(), StringRef MatcherName, |
| 227 | const SourceRange &NameRange, |
| 228 | ArrayRef<ParserValue> Args, |
| 229 | Diagnostics *Error) { |
| 230 | typedef ReturnType (*FuncType)(ArgType1, ArgType2); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 231 | CHECK_ARG_COUNT(2); |
| 232 | CHECK_ARG_TYPE(0, ArgType1); |
| 233 | CHECK_ARG_TYPE(1, ArgType2); |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 234 | return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)( |
| 235 | ArgTypeTraits<ArgType1>::get(Args[0].Value), |
| 236 | ArgTypeTraits<ArgType2>::get(Args[1].Value))); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 239 | #undef CHECK_ARG_COUNT |
| 240 | #undef CHECK_ARG_TYPE |
| 241 | |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 242 | /// \brief Variadic marshaller function. |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 243 | template <typename ResultT, typename ArgT, |
| 244 | ResultT (*Func)(ArrayRef<const ArgT *>)> |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 245 | VariantMatcher |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 246 | variadicMatcherDescriptor(StringRef MatcherName, const SourceRange &NameRange, |
| 247 | ArrayRef<ParserValue> Args, Diagnostics *Error) { |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 248 | ArgT **InnerArgs = new ArgT *[Args.size()](); |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 249 | |
| 250 | bool HasError = false; |
| 251 | for (size_t i = 0, e = Args.size(); i != e; ++i) { |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 252 | typedef ArgTypeTraits<ArgT> ArgTraits; |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 253 | const ParserValue &Arg = Args[i]; |
| 254 | const VariantValue &Value = Arg.Value; |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 255 | if (!ArgTraits::is(Value)) { |
Samuel Benzaquen | a37bb8c | 2013-07-18 19:47:59 +0000 | [diff] [blame] | 256 | Error->addError(Arg.Range, Error->ET_RegistryWrongArgType) |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 257 | << (i + 1) << ArgTraits::asString() << Value.getTypeAsString(); |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 258 | HasError = true; |
| 259 | break; |
| 260 | } |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 261 | InnerArgs[i] = new ArgT(ArgTraits::get(Value)); |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 264 | VariantMatcher Out; |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 265 | if (!HasError) { |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 266 | Out = outvalueToVariantMatcher( |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 267 | Func(ArrayRef<const ArgT *>(InnerArgs, Args.size()))); |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | for (size_t i = 0, e = Args.size(); i != e; ++i) { |
| 271 | delete InnerArgs[i]; |
| 272 | } |
| 273 | delete[] InnerArgs; |
| 274 | return Out; |
| 275 | } |
| 276 | |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 277 | /// \brief Helper class used to collect all the possible overloads of an |
| 278 | /// argument adaptative matcher function. |
| 279 | template <template <typename ToArg, typename FromArg> class ArgumentAdapterT, |
| 280 | typename FromTypes, typename ToTypes> |
| 281 | class AdaptativeOverloadCollector { |
| 282 | public: |
| 283 | AdaptativeOverloadCollector(StringRef Name, |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 284 | std::vector<MatcherDescriptor *> &Out) |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 285 | : Name(Name), Out(Out) { |
| 286 | collect(FromTypes()); |
| 287 | } |
| 288 | |
| 289 | private: |
| 290 | typedef ast_matchers::internal::ArgumentAdaptingMatcherFunc< |
| 291 | ArgumentAdapterT, FromTypes, ToTypes> AdaptativeFunc; |
| 292 | |
| 293 | /// \brief End case for the recursion |
| 294 | static void collect(ast_matchers::internal::EmptyTypeList) {} |
| 295 | |
| 296 | /// \brief Recursive case. Get the overload for the head of the list, and |
| 297 | /// recurse to the tail. |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 298 | template <typename FromTypeList> |
| 299 | inline void collect(FromTypeList); |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 300 | |
| 301 | const StringRef Name; |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 302 | std::vector<MatcherDescriptor *> &Out; |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 305 | /// \brief MatcherDescriptor that wraps multiple "overloads" of the same |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 306 | /// matcher. |
| 307 | /// |
| 308 | /// It will try every overload and generate appropriate errors for when none or |
| 309 | /// more than one overloads match the arguments. |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 310 | class OverloadedMatcherDescriptor : public MatcherDescriptor { |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 311 | public: |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 312 | OverloadedMatcherDescriptor(ArrayRef<MatcherDescriptor *> Callbacks) |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 313 | : Overloads(Callbacks) {} |
| 314 | |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 315 | virtual ~OverloadedMatcherDescriptor() { |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 316 | llvm::DeleteContainerPointers(Overloads); |
| 317 | } |
| 318 | |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 319 | virtual VariantMatcher create(const SourceRange &NameRange, |
| 320 | ArrayRef<ParserValue> Args, |
| 321 | Diagnostics *Error) const { |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 322 | std::vector<VariantMatcher> Constructed; |
| 323 | Diagnostics::OverloadContext Ctx(Error); |
| 324 | for (size_t i = 0, e = Overloads.size(); i != e; ++i) { |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 325 | VariantMatcher SubMatcher = Overloads[i]->create(NameRange, Args, Error); |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 326 | if (!SubMatcher.isNull()) { |
| 327 | Constructed.push_back(SubMatcher); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if (Constructed.empty()) return VariantMatcher(); // No overload matched. |
| 332 | // We ignore the errors if any matcher succeeded. |
| 333 | Ctx.revertErrors(); |
| 334 | if (Constructed.size() > 1) { |
| 335 | // More than one constructed. It is ambiguous. |
| 336 | Error->addError(NameRange, Error->ET_RegistryAmbiguousOverload); |
| 337 | return VariantMatcher(); |
| 338 | } |
| 339 | return Constructed[0]; |
| 340 | } |
| 341 | |
| 342 | private: |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 343 | std::vector<MatcherDescriptor *> Overloads; |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
Samuel Benzaquen | 4adca62 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 346 | /// \brief Variadic operator marshaller function. |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 347 | class VariadicOperatorMatcherDescriptor : public MatcherDescriptor { |
Samuel Benzaquen | 4adca62 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 348 | public: |
| 349 | typedef ast_matchers::internal::VariadicOperatorFunction VarFunc; |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 350 | VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount, |
| 351 | VarFunc Func, StringRef MatcherName) |
Samuel Benzaquen | 4d05874 | 2013-11-22 14:41:48 +0000 | [diff] [blame] | 352 | : MinCount(MinCount), MaxCount(MaxCount), Func(Func), |
| 353 | MatcherName(MatcherName) {} |
Samuel Benzaquen | 4adca62 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 354 | |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 355 | virtual VariantMatcher create(const SourceRange &NameRange, |
| 356 | ArrayRef<ParserValue> Args, |
| 357 | Diagnostics *Error) const { |
Samuel Benzaquen | 4d05874 | 2013-11-22 14:41:48 +0000 | [diff] [blame] | 358 | if (Args.size() < MinCount || MaxCount < Args.size()) { |
| 359 | const std::string MaxStr = |
| 360 | (MaxCount == UINT_MAX ? "" : Twine(MaxCount)).str(); |
| 361 | Error->addError(NameRange, Error->ET_RegistryWrongArgCount) |
| 362 | << ("(" + Twine(MinCount) + ", " + MaxStr + ")") << Args.size(); |
| 363 | return VariantMatcher(); |
| 364 | } |
| 365 | |
Samuel Benzaquen | 4adca62 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 366 | std::vector<VariantMatcher> InnerArgs; |
| 367 | for (size_t i = 0, e = Args.size(); i != e; ++i) { |
| 368 | const ParserValue &Arg = Args[i]; |
| 369 | const VariantValue &Value = Arg.Value; |
| 370 | if (!Value.isMatcher()) { |
| 371 | Error->addError(Arg.Range, Error->ET_RegistryWrongArgType) |
| 372 | << (i + 1) << "Matcher<>" << Value.getTypeAsString(); |
| 373 | return VariantMatcher(); |
| 374 | } |
| 375 | InnerArgs.push_back(Value.getMatcher()); |
| 376 | } |
| 377 | return VariantMatcher::VariadicOperatorMatcher(Func, InnerArgs); |
| 378 | } |
| 379 | |
| 380 | private: |
Samuel Benzaquen | 4d05874 | 2013-11-22 14:41:48 +0000 | [diff] [blame] | 381 | const unsigned MinCount; |
| 382 | const unsigned MaxCount; |
Samuel Benzaquen | 4adca62 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 383 | const VarFunc Func; |
| 384 | const StringRef MatcherName; |
| 385 | }; |
| 386 | |
| 387 | |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 388 | /// Helper functions to select the appropriate marshaller functions. |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 389 | /// They detect the number of arguments, arguments types and return type. |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 390 | |
| 391 | /// \brief 0-arg overload |
| 392 | template <typename ReturnType> |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 393 | MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(), |
| 394 | StringRef MatcherName) { |
| 395 | return new FixedArgCountMatcherDescriptor(matcherMarshall0<ReturnType>, |
| 396 | reinterpret_cast<void (*)()>(Func), |
| 397 | MatcherName); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /// \brief 1-arg overload |
| 401 | template <typename ReturnType, typename ArgType1> |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 402 | MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1), |
| 403 | StringRef MatcherName) { |
| 404 | return new FixedArgCountMatcherDescriptor( |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 405 | matcherMarshall1<ReturnType, ArgType1>, |
| 406 | reinterpret_cast<void (*)()>(Func), MatcherName); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 409 | /// \brief 2-arg overload |
| 410 | template <typename ReturnType, typename ArgType1, typename ArgType2> |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 411 | MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1, |
| 412 | ArgType2), |
| 413 | StringRef MatcherName) { |
| 414 | return new FixedArgCountMatcherDescriptor( |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 415 | matcherMarshall2<ReturnType, ArgType1, ArgType2>, |
| 416 | reinterpret_cast<void (*)()>(Func), MatcherName); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 419 | /// \brief Variadic overload. |
| 420 | template <typename ResultT, typename ArgT, |
| 421 | ResultT (*Func)(ArrayRef<const ArgT *>)> |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 422 | MatcherDescriptor * |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 423 | makeMatcherAutoMarshall(llvm::VariadicFunction<ResultT, ArgT, Func> VarFunc, |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 424 | StringRef MatcherName) { |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 425 | return new FreeFuncMatcherDescriptor( |
| 426 | &variadicMatcherDescriptor<ResultT, ArgT, Func>, MatcherName); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 429 | /// \brief Argument adaptative overload. |
| 430 | template <template <typename ToArg, typename FromArg> class ArgumentAdapterT, |
| 431 | typename FromTypes, typename ToTypes> |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 432 | MatcherDescriptor * |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 433 | makeMatcherAutoMarshall(ast_matchers::internal::ArgumentAdaptingMatcherFunc< |
| 434 | ArgumentAdapterT, FromTypes, ToTypes>, |
| 435 | StringRef MatcherName) { |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 436 | std::vector<MatcherDescriptor *> Overloads; |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 437 | AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes, ToTypes>(MatcherName, |
| 438 | Overloads); |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 439 | return new OverloadedMatcherDescriptor(Overloads); |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | template <template <typename ToArg, typename FromArg> class ArgumentAdapterT, |
| 443 | typename FromTypes, typename ToTypes> |
| 444 | template <typename FromTypeList> |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 445 | inline void AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes, |
| 446 | ToTypes>::collect(FromTypeList) { |
Samuel Benzaquen | bd7d887 | 2013-08-16 16:19:42 +0000 | [diff] [blame] | 447 | Out.push_back(makeMatcherAutoMarshall( |
| 448 | &AdaptativeFunc::template create<typename FromTypeList::head>, Name)); |
| 449 | collect(typename FromTypeList::tail()); |
| 450 | } |
| 451 | |
Samuel Benzaquen | 4adca62 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 452 | /// \brief Variadic operator overload. |
Samuel Benzaquen | 4d05874 | 2013-11-22 14:41:48 +0000 | [diff] [blame] | 453 | template <unsigned MinCount, unsigned MaxCount> |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 454 | MatcherDescriptor * |
Samuel Benzaquen | 4d05874 | 2013-11-22 14:41:48 +0000 | [diff] [blame] | 455 | makeMatcherAutoMarshall(ast_matchers::internal::VariadicOperatorMatcherFunc< |
| 456 | MinCount, MaxCount> Func, |
| 457 | StringRef MatcherName) { |
Peter Collingbourne | f43e694 | 2013-11-23 01:34:36 +0000 | [diff] [blame^] | 458 | return new VariadicOperatorMatcherDescriptor(MinCount, MaxCount, Func.Func, |
| 459 | MatcherName); |
Samuel Benzaquen | 4adca62 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 462 | } // namespace internal |
| 463 | } // namespace dynamic |
| 464 | } // namespace ast_matchers |
| 465 | } // namespace clang |
| 466 | |
| 467 | #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H |