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