Manuel Klimek | f7f295f | 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 | |
| 23 | #include <list> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 28 | #include "clang/ASTMatchers/Dynamic/Diagnostics.h" |
| 29 | #include "clang/ASTMatchers/Dynamic/VariantValue.h" |
| 30 | #include "clang/Basic/LLVM.h" |
| 31 | #include "llvm/Support/type_traits.h" |
| 32 | |
| 33 | namespace clang { |
| 34 | namespace ast_matchers { |
| 35 | namespace dynamic { |
| 36 | |
| 37 | namespace internal { |
| 38 | |
| 39 | /// \brief Helper template class to just from argument type to the right is/get |
| 40 | /// functions in VariantValue. |
| 41 | /// Used to verify and extract the matcher arguments below. |
| 42 | template <class T> struct ArgTypeTraits; |
| 43 | template <class T> struct ArgTypeTraits<const T &> : public ArgTypeTraits<T> { |
| 44 | }; |
| 45 | |
| 46 | template <> struct ArgTypeTraits<std::string> { |
| 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 | |
| 53 | template <class T> struct ArgTypeTraits<ast_matchers::internal::Matcher<T> > { |
| 54 | static bool is(const VariantValue &Value) { return Value.isMatcher(); } |
| 55 | static ast_matchers::internal::Matcher<T> get(const VariantValue &Value) { |
| 56 | return Value.getTypedMatcher<T>(); |
| 57 | } |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 58 | }; |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 59 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 60 | template <> struct ArgTypeTraits<unsigned> { |
| 61 | static bool is(const VariantValue &Value) { return Value.isUnsigned(); } |
| 62 | static unsigned get(const VariantValue &Value) { |
| 63 | return Value.getUnsigned(); |
| 64 | } |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /// \brief Generic MatcherCreate interface. |
| 68 | /// |
| 69 | /// Provides a \c run() method that constructs the matcher from the provided |
| 70 | /// arguments. |
| 71 | class MatcherCreateCallback { |
| 72 | public: |
| 73 | virtual ~MatcherCreateCallback() {} |
| 74 | virtual DynTypedMatcher *run(const SourceRange &NameRange, |
| 75 | ArrayRef<ParserValue> Args, |
| 76 | Diagnostics *Error) const = 0; |
| 77 | }; |
| 78 | |
| 79 | /// \brief Simple callback implementation. Marshaller and function are provided. |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 80 | template <typename MarshallerType, typename FuncType> |
| 81 | class FixedArgCountMatcherCreateCallback : public MatcherCreateCallback { |
| 82 | public: |
Dmitri Gribenko | e3c63fc | 2013-05-17 17:50:16 +0000 | [diff] [blame] | 83 | /// \param Marshaller Function to unpack the arguments and call \c Func |
| 84 | /// \param Func Matcher construct function. This is the function that |
| 85 | /// compile-time matcher expressions would use to create the matcher. |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 86 | FixedArgCountMatcherCreateCallback(MarshallerType Marshaller, FuncType Func, |
| 87 | StringRef MatcherName) |
| 88 | : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName.str()) {} |
| 89 | |
| 90 | DynTypedMatcher *run(const SourceRange &NameRange, |
| 91 | ArrayRef<ParserValue> Args, Diagnostics *Error) const { |
| 92 | return Marshaller(Func, MatcherName, NameRange, Args, Error); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | const MarshallerType Marshaller; |
| 97 | const FuncType Func; |
| 98 | const std::string MatcherName; |
| 99 | }; |
| 100 | |
| 101 | /// \brief Helper function to do template argument deduction. |
| 102 | template <typename MarshallerType, typename FuncType> |
| 103 | MatcherCreateCallback * |
| 104 | createMarshallerCallback(MarshallerType Marshaller, FuncType Func, |
| 105 | StringRef MatcherName) { |
| 106 | return new FixedArgCountMatcherCreateCallback<MarshallerType, FuncType>( |
| 107 | Marshaller, Func, MatcherName); |
| 108 | } |
| 109 | |
| 110 | /// \brief Helper macros to check the arguments on all marshaller functions. |
| 111 | #define CHECK_ARG_COUNT(count) \ |
| 112 | if (Args.size() != count) { \ |
| 113 | Error->pushErrorFrame(NameRange, Error->ET_RegistryWrongArgCount) \ |
| 114 | << count << Args.size(); \ |
| 115 | return NULL; \ |
| 116 | } |
| 117 | |
| 118 | #define CHECK_ARG_TYPE(index, type) \ |
| 119 | if (!ArgTypeTraits<type>::is(Args[index].Value)) { \ |
| 120 | Error->pushErrorFrame(Args[index].Range, Error->ET_RegistryWrongArgType) \ |
| 121 | << MatcherName << (index + 1); \ |
| 122 | return NULL; \ |
| 123 | } |
| 124 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 125 | /// \brief 0-arg marshaller function. |
| 126 | template <typename ReturnType> |
| 127 | DynTypedMatcher *matcherMarshall0(ReturnType (*Func)(), StringRef MatcherName, |
| 128 | const SourceRange &NameRange, |
| 129 | ArrayRef<ParserValue> Args, |
| 130 | Diagnostics *Error) { |
| 131 | CHECK_ARG_COUNT(0); |
| 132 | return Func().clone(); |
| 133 | } |
| 134 | |
| 135 | /// \brief 1-arg marshaller function. |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 136 | template <typename ReturnType, typename ArgType1> |
| 137 | DynTypedMatcher *matcherMarshall1(ReturnType (*Func)(ArgType1), |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 138 | StringRef MatcherName, |
| 139 | const SourceRange &NameRange, |
| 140 | ArrayRef<ParserValue> Args, |
| 141 | Diagnostics *Error) { |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 142 | CHECK_ARG_COUNT(1); |
| 143 | CHECK_ARG_TYPE(0, ArgType1); |
| 144 | return Func(ArgTypeTraits<ArgType1>::get(Args[0].Value)).clone(); |
| 145 | } |
| 146 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 147 | /// \brief 2-arg marshaller function. |
| 148 | template <typename ReturnType, typename ArgType1, typename ArgType2> |
| 149 | DynTypedMatcher *matcherMarshall2(ReturnType (*Func)(ArgType1, ArgType2), |
| 150 | StringRef MatcherName, |
| 151 | const SourceRange &NameRange, |
| 152 | ArrayRef<ParserValue> Args, |
| 153 | Diagnostics *Error) { |
| 154 | CHECK_ARG_COUNT(2); |
| 155 | CHECK_ARG_TYPE(0, ArgType1); |
| 156 | CHECK_ARG_TYPE(1, ArgType2); |
| 157 | return Func(ArgTypeTraits<ArgType1>::get(Args[0].Value), |
| 158 | ArgTypeTraits<ArgType2>::get(Args[1].Value)).clone(); |
| 159 | } |
| 160 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 161 | /// \brief Variadic marshaller function. |
| 162 | template <typename BaseType, typename DerivedType> |
| 163 | class VariadicMatcherCreateCallback : public MatcherCreateCallback { |
| 164 | public: |
| 165 | explicit VariadicMatcherCreateCallback(StringRef MatcherName) |
| 166 | : MatcherName(MatcherName.str()) {} |
| 167 | |
| 168 | typedef ast_matchers::internal::Matcher<DerivedType> DerivedMatcherType; |
| 169 | |
| 170 | DynTypedMatcher *run(const SourceRange &NameRange, ArrayRef<ParserValue> Args, |
| 171 | Diagnostics *Error) const { |
| 172 | std::list<DerivedMatcherType> References; |
| 173 | std::vector<const DerivedMatcherType *> InnerArgs(Args.size()); |
| 174 | for (size_t i = 0, e = Args.size(); i != e; ++i) { |
| 175 | CHECK_ARG_TYPE(i, DerivedMatcherType); |
| 176 | References.push_back( |
| 177 | ArgTypeTraits<DerivedMatcherType>::get(Args[i].Value)); |
| 178 | InnerArgs[i] = &References.back(); |
| 179 | } |
| 180 | return ast_matchers::internal::makeDynCastAllOfComposite<BaseType>( |
| 181 | ArrayRef<const DerivedMatcherType *>(InnerArgs)).clone(); |
| 182 | } |
| 183 | |
| 184 | private: |
| 185 | const std::string MatcherName; |
| 186 | }; |
| 187 | |
| 188 | #undef CHECK_ARG_COUNT |
| 189 | #undef CHECK_ARG_TYPE |
| 190 | |
| 191 | /// Helper functions to select the appropriate marshaller functions. |
| 192 | /// They detects the number of arguments, arguments types and return type. |
| 193 | |
| 194 | /// \brief 0-arg overload |
| 195 | template <typename ReturnType> |
| 196 | MatcherCreateCallback *makeMatcherAutoMarshall(ReturnType (*Func)(), |
| 197 | StringRef MatcherName) { |
| 198 | return createMarshallerCallback(matcherMarshall0<ReturnType>, Func, |
| 199 | MatcherName); |
| 200 | } |
| 201 | |
| 202 | /// \brief 1-arg overload |
| 203 | template <typename ReturnType, typename ArgType1> |
| 204 | MatcherCreateCallback *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1), |
| 205 | StringRef MatcherName) { |
| 206 | return createMarshallerCallback(matcherMarshall1<ReturnType, ArgType1>, Func, |
| 207 | MatcherName); |
| 208 | } |
| 209 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 210 | /// \brief 2-arg overload |
| 211 | template <typename ReturnType, typename ArgType1, typename ArgType2> |
| 212 | MatcherCreateCallback *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1, |
| 213 | ArgType2), |
| 214 | StringRef MatcherName) { |
| 215 | return createMarshallerCallback( |
| 216 | matcherMarshall2<ReturnType, ArgType1, ArgType2>, Func, MatcherName); |
| 217 | } |
| 218 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 219 | /// \brief Variadic overload. |
| 220 | template <typename MatcherType> |
| 221 | MatcherCreateCallback *makeMatcherAutoMarshall( |
| 222 | ast_matchers::internal::VariadicAllOfMatcher<MatcherType> Func, |
| 223 | StringRef MatcherName) { |
| 224 | return new VariadicMatcherCreateCallback<MatcherType, MatcherType>( |
| 225 | MatcherName); |
| 226 | } |
| 227 | |
| 228 | template <typename BaseType, typename MatcherType> |
| 229 | MatcherCreateCallback * |
| 230 | makeMatcherAutoMarshall(ast_matchers::internal::VariadicDynCastAllOfMatcher< |
| 231 | BaseType, MatcherType> Func, |
| 232 | StringRef MatcherName) { |
| 233 | return new VariadicMatcherCreateCallback<BaseType, MatcherType>(MatcherName); |
| 234 | } |
| 235 | |
| 236 | } // namespace internal |
| 237 | } // namespace dynamic |
| 238 | } // namespace ast_matchers |
| 239 | } // namespace clang |
| 240 | |
| 241 | #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H |