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