blob: 122d8041336f7c27dc32c37d7fb3ef1a62f5edd6 [file] [log] [blame]
Manuel Klimek24db0f02013-05-14 09:13:00 +00001//===--- 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 Klimek24db0f02013-05-14 09:13:00 +000023#include <string>
Manuel Klimek24db0f02013-05-14 09:13:00 +000024
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 Benzaquen0239b692013-08-13 14:54:51 +000029#include "llvm/ADT/STLExtras.h"
Manuel Klimek24db0f02013-05-14 09:13:00 +000030#include "llvm/Support/type_traits.h"
31
32namespace clang {
33namespace ast_matchers {
34namespace dynamic {
35
36namespace 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.
41template <class T> struct ArgTypeTraits;
42template <class T> struct ArgTypeTraits<const T &> : public ArgTypeTraits<T> {
43};
44
45template <> struct ArgTypeTraits<std::string> {
Samuel Benzaquen81ef9292013-06-20 14:28:32 +000046 static StringRef asString() { return "String"; }
Manuel Klimek24db0f02013-05-14 09:13:00 +000047 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 Benzaquenc6f2c9b2013-06-21 15:51:31 +000053template <>
54struct ArgTypeTraits<StringRef> : public ArgTypeTraits<std::string> {
55};
56
Manuel Klimek24db0f02013-05-14 09:13:00 +000057template <class T> struct ArgTypeTraits<ast_matchers::internal::Matcher<T> > {
Samuel Benzaquen81ef9292013-06-20 14:28:32 +000058 static std::string asString() {
59 return (Twine("Matcher<") +
60 ast_type_traits::ASTNodeKind::getFromNodeKind<T>().asStringRef() +
61 ">").str();
62 }
63 static bool is(const VariantValue &Value) {
Samuel Benzaquen998cda232013-08-30 15:09:52 +000064 return Value.isMatcher() && Value.getMatcher().hasTypedMatcher<T>();
Samuel Benzaquen81ef9292013-06-20 14:28:32 +000065 }
Manuel Klimek24db0f02013-05-14 09:13:00 +000066 static ast_matchers::internal::Matcher<T> get(const VariantValue &Value) {
Samuel Benzaquen998cda232013-08-30 15:09:52 +000067 return Value.getMatcher().getTypedMatcher<T>();
Manuel Klimek24db0f02013-05-14 09:13:00 +000068 }
Samuel Benzaquenc31b3522013-06-04 15:46:22 +000069};
Manuel Klimek24db0f02013-05-14 09:13:00 +000070
Samuel Benzaquenc31b3522013-06-04 15:46:22 +000071template <> struct ArgTypeTraits<unsigned> {
Samuel Benzaquen81ef9292013-06-20 14:28:32 +000072 static std::string asString() { return "Unsigned"; }
Samuel Benzaquenc31b3522013-06-04 15:46:22 +000073 static bool is(const VariantValue &Value) { return Value.isUnsigned(); }
74 static unsigned get(const VariantValue &Value) {
75 return Value.getUnsigned();
76 }
Manuel Klimek24db0f02013-05-14 09:13:00 +000077};
78
Peter Collingbournef43e6942013-11-23 01:34:36 +000079/// \brief Matcher descriptor interface.
Manuel Klimek24db0f02013-05-14 09:13:00 +000080///
Peter Collingbournef43e6942013-11-23 01:34:36 +000081/// Provides a \c create() method that constructs the matcher from the provided
Manuel Klimek24db0f02013-05-14 09:13:00 +000082/// arguments.
Peter Collingbournef43e6942013-11-23 01:34:36 +000083class MatcherDescriptor {
Manuel Klimek24db0f02013-05-14 09:13:00 +000084public:
Peter Collingbournef43e6942013-11-23 01:34:36 +000085 virtual ~MatcherDescriptor() {}
86 virtual VariantMatcher create(const SourceRange &NameRange,
87 ArrayRef<ParserValue> Args,
88 Diagnostics *Error) const = 0;
Manuel Klimek24db0f02013-05-14 09:13:00 +000089};
90
91/// \brief Simple callback implementation. Marshaller and function are provided.
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +000092///
93/// This class wraps a function of arbitrary signature and a marshaller
Peter Collingbournef43e6942013-11-23 01:34:36 +000094/// function into a MatcherDescriptor.
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +000095/// The marshaller is in charge of taking the VariantValue arguments, checking
96/// their types, unpacking them and calling the underlying function.
Peter Collingbournef43e6942013-11-23 01:34:36 +000097class FixedArgCountMatcherDescriptor : public MatcherDescriptor {
Manuel Klimek24db0f02013-05-14 09:13:00 +000098public:
Samuel Benzaquen998cda232013-08-30 15:09:52 +000099 typedef VariantMatcher (*MarshallerType)(void (*Func)(),
100 StringRef MatcherName,
101 const SourceRange &NameRange,
102 ArrayRef<ParserValue> Args,
103 Diagnostics *Error);
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000104
Dmitri Gribenkocb63baf2013-05-17 17:50:16 +0000105 /// \param Marshaller Function to unpack the arguments and call \c Func
106 /// \param Func Matcher construct function. This is the function that
107 /// compile-time matcher expressions would use to create the matcher.
Peter Collingbournef43e6942013-11-23 01:34:36 +0000108 FixedArgCountMatcherDescriptor(MarshallerType Marshaller, void (*Func)(),
109 StringRef MatcherName)
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000110 : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName) {}
Manuel Klimek24db0f02013-05-14 09:13:00 +0000111
Peter Collingbournef43e6942013-11-23 01:34:36 +0000112 VariantMatcher create(const SourceRange &NameRange,
113 ArrayRef<ParserValue> Args, Diagnostics *Error) const {
Manuel Klimek24db0f02013-05-14 09:13:00 +0000114 return Marshaller(Func, MatcherName, NameRange, Args, Error);
115 }
116
117private:
118 const MarshallerType Marshaller;
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000119 void (* const Func)();
Manuel Klimek24db0f02013-05-14 09:13:00 +0000120 const std::string MatcherName;
121};
122
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000123/// \brief Simple callback implementation. Free function is wrapped.
124///
125/// This class simply wraps a free function with the right signature to export
Peter Collingbournef43e6942013-11-23 01:34:36 +0000126/// it as a MatcherDescriptor.
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000127/// This allows us to have one implementation of the interface for as many free
128/// functions as we want, reducing the number of symbols and size of the
129/// object file.
Peter Collingbournef43e6942013-11-23 01:34:36 +0000130class FreeFuncMatcherDescriptor : public MatcherDescriptor {
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000131public:
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000132 typedef VariantMatcher (*RunFunc)(StringRef MatcherName,
133 const SourceRange &NameRange,
134 ArrayRef<ParserValue> Args,
135 Diagnostics *Error);
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000136
Peter Collingbournef43e6942013-11-23 01:34:36 +0000137 FreeFuncMatcherDescriptor(RunFunc Func, StringRef MatcherName)
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000138 : Func(Func), MatcherName(MatcherName.str()) {}
139
Peter Collingbournef43e6942013-11-23 01:34:36 +0000140 VariantMatcher create(const SourceRange &NameRange,
141 ArrayRef<ParserValue> Args, Diagnostics *Error) const {
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000142 return Func(MatcherName, NameRange, Args, Error);
143 }
144
145private:
146 const RunFunc Func;
147 const std::string MatcherName;
148};
Manuel Klimek24db0f02013-05-14 09:13:00 +0000149
150/// \brief Helper macros to check the arguments on all marshaller functions.
151#define CHECK_ARG_COUNT(count) \
152 if (Args.size() != count) { \
Samuel Benzaquena37bb8c2013-07-18 19:47:59 +0000153 Error->addError(NameRange, Error->ET_RegistryWrongArgCount) \
Manuel Klimek24db0f02013-05-14 09:13:00 +0000154 << count << Args.size(); \
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000155 return VariantMatcher(); \
Manuel Klimek24db0f02013-05-14 09:13:00 +0000156 }
157
158#define CHECK_ARG_TYPE(index, type) \
159 if (!ArgTypeTraits<type>::is(Args[index].Value)) { \
Samuel Benzaquena37bb8c2013-07-18 19:47:59 +0000160 Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \
Samuel Benzaquen81ef9292013-06-20 14:28:32 +0000161 << (index + 1) << ArgTypeTraits<type>::asString() \
162 << Args[index].Value.getTypeAsString(); \
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000163 return VariantMatcher(); \
Manuel Klimek24db0f02013-05-14 09:13:00 +0000164 }
165
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000166/// \brief Helper methods to extract and merge all possible typed matchers
167/// out of the polymorphic object.
168template <class PolyMatcher>
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000169static void mergePolyMatchers(const PolyMatcher &Poly,
Samuel Benzaquenf34ac3e2013-10-29 14:37:15 +0000170 std::vector<DynTypedMatcher> &Out,
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000171 ast_matchers::internal::EmptyTypeList) {}
172
173template <class PolyMatcher, class TypeList>
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000174static void mergePolyMatchers(const PolyMatcher &Poly,
Samuel Benzaquenf34ac3e2013-10-29 14:37:15 +0000175 std::vector<DynTypedMatcher> &Out, TypeList) {
176 Out.push_back(ast_matchers::internal::Matcher<typename TypeList::head>(Poly));
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000177 mergePolyMatchers(Poly, Out, typename TypeList::tail());
178}
179
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000180/// \brief Convert the return values of the functions into a VariantMatcher.
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000181///
182/// There are 2 cases right now: The return value is a Matcher<T> or is a
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000183/// polymorphic matcher. For the former, we just construct the VariantMatcher.
184/// For the latter, we instantiate all the possible Matcher<T> of the poly
185/// matcher.
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000186static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher) {
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000187 return VariantMatcher::SingleMatcher(Matcher);
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000188}
189
190template <typename T>
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000191static VariantMatcher outvalueToVariantMatcher(const T &PolyMatcher,
192 typename T::ReturnTypes * =
193 NULL) {
Samuel Benzaquenf34ac3e2013-10-29 14:37:15 +0000194 std::vector<DynTypedMatcher> Matchers;
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000195 mergePolyMatchers(PolyMatcher, Matchers, typename T::ReturnTypes());
196 VariantMatcher Out = VariantMatcher::PolymorphicMatcher(Matchers);
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000197 return Out;
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +0000198}
199
Manuel Klimek24db0f02013-05-14 09:13:00 +0000200/// \brief 0-arg marshaller function.
201template <typename ReturnType>
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000202static VariantMatcher matcherMarshall0(void (*Func)(), StringRef MatcherName,
203 const SourceRange &NameRange,
204 ArrayRef<ParserValue> Args,
205 Diagnostics *Error) {
206 typedef ReturnType (*FuncType)();
Manuel Klimek24db0f02013-05-14 09:13:00 +0000207 CHECK_ARG_COUNT(0);
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000208 return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)());
Manuel Klimek24db0f02013-05-14 09:13:00 +0000209}
210
211/// \brief 1-arg marshaller function.
Samuel Benzaquenc31b3522013-06-04 15:46:22 +0000212template <typename ReturnType, typename ArgType1>
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000213static VariantMatcher matcherMarshall1(void (*Func)(), StringRef MatcherName,
214 const SourceRange &NameRange,
215 ArrayRef<ParserValue> Args,
216 Diagnostics *Error) {
217 typedef ReturnType (*FuncType)(ArgType1);
Manuel Klimek24db0f02013-05-14 09:13:00 +0000218 CHECK_ARG_COUNT(1);
219 CHECK_ARG_TYPE(0, ArgType1);
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000220 return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)(
221 ArgTypeTraits<ArgType1>::get(Args[0].Value)));
Manuel Klimek24db0f02013-05-14 09:13:00 +0000222}
223
Samuel Benzaquenc31b3522013-06-04 15:46:22 +0000224/// \brief 2-arg marshaller function.
225template <typename ReturnType, typename ArgType1, typename ArgType2>
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000226static VariantMatcher matcherMarshall2(void (*Func)(), StringRef MatcherName,
227 const SourceRange &NameRange,
228 ArrayRef<ParserValue> Args,
229 Diagnostics *Error) {
230 typedef ReturnType (*FuncType)(ArgType1, ArgType2);
Samuel Benzaquenc31b3522013-06-04 15:46:22 +0000231 CHECK_ARG_COUNT(2);
232 CHECK_ARG_TYPE(0, ArgType1);
233 CHECK_ARG_TYPE(1, ArgType2);
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000234 return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)(
235 ArgTypeTraits<ArgType1>::get(Args[0].Value),
236 ArgTypeTraits<ArgType2>::get(Args[1].Value)));
Samuel Benzaquenc31b3522013-06-04 15:46:22 +0000237}
238
Manuel Klimek24db0f02013-05-14 09:13:00 +0000239#undef CHECK_ARG_COUNT
240#undef CHECK_ARG_TYPE
241
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000242/// \brief Variadic marshaller function.
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000243template <typename ResultT, typename ArgT,
244 ResultT (*Func)(ArrayRef<const ArgT *>)>
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000245VariantMatcher
Peter Collingbournef43e6942013-11-23 01:34:36 +0000246variadicMatcherDescriptor(StringRef MatcherName, const SourceRange &NameRange,
247 ArrayRef<ParserValue> Args, Diagnostics *Error) {
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000248 ArgT **InnerArgs = new ArgT *[Args.size()]();
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000249
250 bool HasError = false;
251 for (size_t i = 0, e = Args.size(); i != e; ++i) {
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000252 typedef ArgTypeTraits<ArgT> ArgTraits;
Samuel Benzaquen81ef9292013-06-20 14:28:32 +0000253 const ParserValue &Arg = Args[i];
254 const VariantValue &Value = Arg.Value;
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000255 if (!ArgTraits::is(Value)) {
Samuel Benzaquena37bb8c2013-07-18 19:47:59 +0000256 Error->addError(Arg.Range, Error->ET_RegistryWrongArgType)
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000257 << (i + 1) << ArgTraits::asString() << Value.getTypeAsString();
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000258 HasError = true;
259 break;
260 }
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000261 InnerArgs[i] = new ArgT(ArgTraits::get(Value));
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000262 }
263
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000264 VariantMatcher Out;
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000265 if (!HasError) {
Samuel Benzaquen0239b692013-08-13 14:54:51 +0000266 Out = outvalueToVariantMatcher(
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000267 Func(ArrayRef<const ArgT *>(InnerArgs, Args.size())));
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000268 }
269
270 for (size_t i = 0, e = Args.size(); i != e; ++i) {
271 delete InnerArgs[i];
272 }
273 delete[] InnerArgs;
274 return Out;
275}
276
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000277/// \brief Helper class used to collect all the possible overloads of an
278/// argument adaptative matcher function.
279template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
280 typename FromTypes, typename ToTypes>
281class AdaptativeOverloadCollector {
282public:
283 AdaptativeOverloadCollector(StringRef Name,
Peter Collingbournef43e6942013-11-23 01:34:36 +0000284 std::vector<MatcherDescriptor *> &Out)
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000285 : Name(Name), Out(Out) {
286 collect(FromTypes());
287 }
288
289private:
290 typedef ast_matchers::internal::ArgumentAdaptingMatcherFunc<
291 ArgumentAdapterT, FromTypes, ToTypes> AdaptativeFunc;
292
293 /// \brief End case for the recursion
294 static void collect(ast_matchers::internal::EmptyTypeList) {}
295
296 /// \brief Recursive case. Get the overload for the head of the list, and
297 /// recurse to the tail.
Peter Collingbournef43e6942013-11-23 01:34:36 +0000298 template <typename FromTypeList>
299 inline void collect(FromTypeList);
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000300
301 const StringRef Name;
Peter Collingbournef43e6942013-11-23 01:34:36 +0000302 std::vector<MatcherDescriptor *> &Out;
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000303};
304
Peter Collingbournef43e6942013-11-23 01:34:36 +0000305/// \brief MatcherDescriptor that wraps multiple "overloads" of the same
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000306/// matcher.
307///
308/// It will try every overload and generate appropriate errors for when none or
309/// more than one overloads match the arguments.
Peter Collingbournef43e6942013-11-23 01:34:36 +0000310class OverloadedMatcherDescriptor : public MatcherDescriptor {
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000311public:
Peter Collingbournef43e6942013-11-23 01:34:36 +0000312 OverloadedMatcherDescriptor(ArrayRef<MatcherDescriptor *> Callbacks)
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000313 : Overloads(Callbacks) {}
314
Peter Collingbournef43e6942013-11-23 01:34:36 +0000315 virtual ~OverloadedMatcherDescriptor() {
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000316 llvm::DeleteContainerPointers(Overloads);
317 }
318
Peter Collingbournef43e6942013-11-23 01:34:36 +0000319 virtual VariantMatcher create(const SourceRange &NameRange,
320 ArrayRef<ParserValue> Args,
321 Diagnostics *Error) const {
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000322 std::vector<VariantMatcher> Constructed;
323 Diagnostics::OverloadContext Ctx(Error);
324 for (size_t i = 0, e = Overloads.size(); i != e; ++i) {
Peter Collingbournef43e6942013-11-23 01:34:36 +0000325 VariantMatcher SubMatcher = Overloads[i]->create(NameRange, Args, Error);
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000326 if (!SubMatcher.isNull()) {
327 Constructed.push_back(SubMatcher);
328 }
329 }
330
331 if (Constructed.empty()) return VariantMatcher(); // No overload matched.
332 // We ignore the errors if any matcher succeeded.
333 Ctx.revertErrors();
334 if (Constructed.size() > 1) {
335 // More than one constructed. It is ambiguous.
336 Error->addError(NameRange, Error->ET_RegistryAmbiguousOverload);
337 return VariantMatcher();
338 }
339 return Constructed[0];
340 }
341
342private:
Peter Collingbournef43e6942013-11-23 01:34:36 +0000343 std::vector<MatcherDescriptor *> Overloads;
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000344};
345
Samuel Benzaquen4adca622013-08-28 18:42:04 +0000346/// \brief Variadic operator marshaller function.
Peter Collingbournef43e6942013-11-23 01:34:36 +0000347class VariadicOperatorMatcherDescriptor : public MatcherDescriptor {
Samuel Benzaquen4adca622013-08-28 18:42:04 +0000348public:
349 typedef ast_matchers::internal::VariadicOperatorFunction VarFunc;
Peter Collingbournef43e6942013-11-23 01:34:36 +0000350 VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount,
351 VarFunc Func, StringRef MatcherName)
Samuel Benzaquen4d058742013-11-22 14:41:48 +0000352 : MinCount(MinCount), MaxCount(MaxCount), Func(Func),
353 MatcherName(MatcherName) {}
Samuel Benzaquen4adca622013-08-28 18:42:04 +0000354
Peter Collingbournef43e6942013-11-23 01:34:36 +0000355 virtual VariantMatcher create(const SourceRange &NameRange,
356 ArrayRef<ParserValue> Args,
357 Diagnostics *Error) const {
Samuel Benzaquen4d058742013-11-22 14:41:48 +0000358 if (Args.size() < MinCount || MaxCount < Args.size()) {
359 const std::string MaxStr =
360 (MaxCount == UINT_MAX ? "" : Twine(MaxCount)).str();
361 Error->addError(NameRange, Error->ET_RegistryWrongArgCount)
362 << ("(" + Twine(MinCount) + ", " + MaxStr + ")") << Args.size();
363 return VariantMatcher();
364 }
365
Samuel Benzaquen4adca622013-08-28 18:42:04 +0000366 std::vector<VariantMatcher> InnerArgs;
367 for (size_t i = 0, e = Args.size(); i != e; ++i) {
368 const ParserValue &Arg = Args[i];
369 const VariantValue &Value = Arg.Value;
370 if (!Value.isMatcher()) {
371 Error->addError(Arg.Range, Error->ET_RegistryWrongArgType)
372 << (i + 1) << "Matcher<>" << Value.getTypeAsString();
373 return VariantMatcher();
374 }
375 InnerArgs.push_back(Value.getMatcher());
376 }
377 return VariantMatcher::VariadicOperatorMatcher(Func, InnerArgs);
378 }
379
380private:
Samuel Benzaquen4d058742013-11-22 14:41:48 +0000381 const unsigned MinCount;
382 const unsigned MaxCount;
Samuel Benzaquen4adca622013-08-28 18:42:04 +0000383 const VarFunc Func;
384 const StringRef MatcherName;
385};
386
387
Manuel Klimek24db0f02013-05-14 09:13:00 +0000388/// Helper functions to select the appropriate marshaller functions.
Samuel Benzaquenb5dd69f2013-06-11 18:51:07 +0000389/// They detect the number of arguments, arguments types and return type.
Manuel Klimek24db0f02013-05-14 09:13:00 +0000390
391/// \brief 0-arg overload
392template <typename ReturnType>
Peter Collingbournef43e6942013-11-23 01:34:36 +0000393MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(),
394 StringRef MatcherName) {
395 return new FixedArgCountMatcherDescriptor(matcherMarshall0<ReturnType>,
396 reinterpret_cast<void (*)()>(Func),
397 MatcherName);
Manuel Klimek24db0f02013-05-14 09:13:00 +0000398}
399
400/// \brief 1-arg overload
401template <typename ReturnType, typename ArgType1>
Peter Collingbournef43e6942013-11-23 01:34:36 +0000402MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1),
403 StringRef MatcherName) {
404 return new FixedArgCountMatcherDescriptor(
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000405 matcherMarshall1<ReturnType, ArgType1>,
406 reinterpret_cast<void (*)()>(Func), MatcherName);
Manuel Klimek24db0f02013-05-14 09:13:00 +0000407}
408
Samuel Benzaquenc31b3522013-06-04 15:46:22 +0000409/// \brief 2-arg overload
410template <typename ReturnType, typename ArgType1, typename ArgType2>
Peter Collingbournef43e6942013-11-23 01:34:36 +0000411MatcherDescriptor *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1,
412 ArgType2),
413 StringRef MatcherName) {
414 return new FixedArgCountMatcherDescriptor(
Samuel Benzaquen998cda232013-08-30 15:09:52 +0000415 matcherMarshall2<ReturnType, ArgType1, ArgType2>,
416 reinterpret_cast<void (*)()>(Func), MatcherName);
Samuel Benzaquenc31b3522013-06-04 15:46:22 +0000417}
418
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000419/// \brief Variadic overload.
420template <typename ResultT, typename ArgT,
421 ResultT (*Func)(ArrayRef<const ArgT *>)>
Peter Collingbournef43e6942013-11-23 01:34:36 +0000422MatcherDescriptor *
Samuel Benzaquen79656e12013-07-15 19:25:06 +0000423makeMatcherAutoMarshall(llvm::VariadicFunction<ResultT, ArgT, Func> VarFunc,
Manuel Klimek24db0f02013-05-14 09:13:00 +0000424 StringRef MatcherName) {
Peter Collingbournef43e6942013-11-23 01:34:36 +0000425 return new FreeFuncMatcherDescriptor(
426 &variadicMatcherDescriptor<ResultT, ArgT, Func>, MatcherName);
Manuel Klimek24db0f02013-05-14 09:13:00 +0000427}
428
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000429/// \brief Argument adaptative overload.
430template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
431 typename FromTypes, typename ToTypes>
Peter Collingbournef43e6942013-11-23 01:34:36 +0000432MatcherDescriptor *
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000433makeMatcherAutoMarshall(ast_matchers::internal::ArgumentAdaptingMatcherFunc<
434 ArgumentAdapterT, FromTypes, ToTypes>,
435 StringRef MatcherName) {
Peter Collingbournef43e6942013-11-23 01:34:36 +0000436 std::vector<MatcherDescriptor *> Overloads;
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000437 AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes, ToTypes>(MatcherName,
438 Overloads);
Peter Collingbournef43e6942013-11-23 01:34:36 +0000439 return new OverloadedMatcherDescriptor(Overloads);
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000440}
441
442template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
443 typename FromTypes, typename ToTypes>
444template <typename FromTypeList>
Peter Collingbournef43e6942013-11-23 01:34:36 +0000445inline void AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes,
446 ToTypes>::collect(FromTypeList) {
Samuel Benzaquenbd7d8872013-08-16 16:19:42 +0000447 Out.push_back(makeMatcherAutoMarshall(
448 &AdaptativeFunc::template create<typename FromTypeList::head>, Name));
449 collect(typename FromTypeList::tail());
450}
451
Samuel Benzaquen4adca622013-08-28 18:42:04 +0000452/// \brief Variadic operator overload.
Samuel Benzaquen4d058742013-11-22 14:41:48 +0000453template <unsigned MinCount, unsigned MaxCount>
Peter Collingbournef43e6942013-11-23 01:34:36 +0000454MatcherDescriptor *
Samuel Benzaquen4d058742013-11-22 14:41:48 +0000455makeMatcherAutoMarshall(ast_matchers::internal::VariadicOperatorMatcherFunc<
456 MinCount, MaxCount> Func,
457 StringRef MatcherName) {
Peter Collingbournef43e6942013-11-23 01:34:36 +0000458 return new VariadicOperatorMatcherDescriptor(MinCount, MaxCount, Func.Func,
459 MatcherName);
Samuel Benzaquen4adca622013-08-28 18:42:04 +0000460}
461
Manuel Klimek24db0f02013-05-14 09:13:00 +0000462} // namespace internal
463} // namespace dynamic
464} // namespace ast_matchers
465} // namespace clang
466
467#endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H