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 | |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 283 | /// Helper functions to select the appropriate marshaller functions. |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 284 | /// They detect the number of arguments, arguments types and return type. |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 285 | |
| 286 | /// \brief 0-arg overload |
| 287 | template <typename ReturnType> |
| 288 | MatcherCreateCallback *makeMatcherAutoMarshall(ReturnType (*Func)(), |
| 289 | StringRef MatcherName) { |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 290 | return new FixedArgCountMatcherCreateCallback<ReturnType (*)()>( |
| 291 | matcherMarshall0, Func, MatcherName); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /// \brief 1-arg overload |
| 295 | template <typename ReturnType, typename ArgType1> |
| 296 | MatcherCreateCallback *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1), |
| 297 | StringRef MatcherName) { |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 298 | return new FixedArgCountMatcherCreateCallback<ReturnType (*)(ArgType1)>( |
| 299 | matcherMarshall1, Func, MatcherName); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 302 | /// \brief 2-arg overload |
| 303 | template <typename ReturnType, typename ArgType1, typename ArgType2> |
| 304 | MatcherCreateCallback *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1, |
| 305 | ArgType2), |
| 306 | StringRef MatcherName) { |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 307 | return new FixedArgCountMatcherCreateCallback< |
| 308 | ReturnType (*)(ArgType1, ArgType2)>(matcherMarshall2, Func, MatcherName); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 311 | /// \brief Variadic overload. |
| 312 | template <typename ResultT, typename ArgT, |
| 313 | ResultT (*Func)(ArrayRef<const ArgT *>)> |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 314 | MatcherCreateCallback * |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 315 | makeMatcherAutoMarshall(llvm::VariadicFunction<ResultT, ArgT, Func> VarFunc, |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 316 | StringRef MatcherName) { |
Samuel Benzaquen | b5dd69f | 2013-06-11 18:51:07 +0000 | [diff] [blame] | 317 | return new FreeFuncMatcherCreateCallback( |
Samuel Benzaquen | 79656e1 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 318 | &variadicMatcherCreateCallback<ResultT, ArgT, Func>, MatcherName); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | } // namespace internal |
| 322 | } // namespace dynamic |
| 323 | } // namespace ast_matchers |
| 324 | } // namespace clang |
| 325 | |
| 326 | #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H |