clang  6.0.0svn
Marshallers.h
Go to the documentation of this file.
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_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H
21 #define LLVM_CLANG_LIB_ASTMATCHERS_DYNAMIC_MARSHALLERS_H
22 
26 #include "clang/Basic/LLVM.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include <string>
29 
30 namespace clang {
31 namespace ast_matchers {
32 namespace dynamic {
33 namespace internal {
34 
35 /// \brief Helper template class to just from argument type to the right is/get
36 /// functions in VariantValue.
37 /// Used to verify and extract the matcher arguments below.
38 template <class T> struct ArgTypeTraits;
39 template <class T> struct ArgTypeTraits<const T &> : public ArgTypeTraits<T> {
40 };
41 
42 template <> struct ArgTypeTraits<std::string> {
43  static bool is(const VariantValue &Value) { return Value.isString(); }
44  static const std::string &get(const VariantValue &Value) {
45  return Value.getString();
46  }
47  static ArgKind getKind() {
49  }
50 };
51 
52 template <>
53 struct ArgTypeTraits<StringRef> : public ArgTypeTraits<std::string> {
54 };
55 
56 template <class T> struct ArgTypeTraits<ast_matchers::internal::Matcher<T> > {
57  static bool is(const VariantValue &Value) {
58  return Value.isMatcher() && Value.getMatcher().hasTypedMatcher<T>();
59  }
60  static ast_matchers::internal::Matcher<T> get(const VariantValue &Value) {
61  return Value.getMatcher().getTypedMatcher<T>();
62  }
63  static ArgKind getKind() {
64  return ArgKind(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
65  }
66 };
67 
68 template <> struct ArgTypeTraits<bool> {
69  static bool is(const VariantValue &Value) { return Value.isBoolean(); }
70  static bool get(const VariantValue &Value) {
71  return Value.getBoolean();
72  }
73  static ArgKind getKind() {
75  }
76 };
77 
78 template <> struct ArgTypeTraits<double> {
79  static bool is(const VariantValue &Value) { return Value.isDouble(); }
80  static double get(const VariantValue &Value) {
81  return Value.getDouble();
82  }
83  static ArgKind getKind() {
85  }
86 };
87 
88 template <> struct ArgTypeTraits<unsigned> {
89  static bool is(const VariantValue &Value) { return Value.isUnsigned(); }
90  static unsigned get(const VariantValue &Value) {
91  return Value.getUnsigned();
92  }
93  static ArgKind getKind() {
95  }
96 };
97 
98 template <> struct ArgTypeTraits<attr::Kind> {
99 private:
100  static attr::Kind getAttrKind(llvm::StringRef AttrKind) {
101  return llvm::StringSwitch<attr::Kind>(AttrKind)
102 #define ATTR(X) .Case("attr::" #X, attr:: X)
103 #include "clang/Basic/AttrList.inc"
104  .Default(attr::Kind(-1));
105  }
106 public:
107  static bool is(const VariantValue &Value) {
108  return Value.isString() &&
109  getAttrKind(Value.getString()) != attr::Kind(-1);
110  }
111  static attr::Kind get(const VariantValue &Value) {
112  return getAttrKind(Value.getString());
113  }
114  static ArgKind getKind() {
115  return ArgKind(ArgKind::AK_String);
116  }
117 };
118 
119 template <> struct ArgTypeTraits<clang::CastKind> {
120 private:
121  static clang::CastKind getCastKind(llvm::StringRef AttrKind) {
122  return llvm::StringSwitch<clang::CastKind>(AttrKind)
123 #define CAST_OPERATION(Name) .Case( #Name, CK_##Name)
124 #include "clang/AST/OperationKinds.def"
125  .Default(CK_Invalid);
126  }
127 
128 public:
129  static bool is(const VariantValue &Value) {
130  return Value.isString() &&
131  getCastKind(Value.getString()) != CK_Invalid;
132  }
133  static clang::CastKind get(const VariantValue &Value) {
134  return getCastKind(Value.getString());
135  }
136  static ArgKind getKind() {
137  return ArgKind(ArgKind::AK_String);
138  }
139 };
140 
141 /// \brief Matcher descriptor interface.
142 ///
143 /// Provides a \c create() method that constructs the matcher from the provided
144 /// arguments, and various other methods for type introspection.
146 public:
147  virtual ~MatcherDescriptor() {}
148  virtual VariantMatcher create(SourceRange NameRange,
150  Diagnostics *Error) const = 0;
151 
152  /// Returns whether the matcher is variadic. Variadic matchers can take any
153  /// number of arguments, but they must be of the same type.
154  virtual bool isVariadic() const = 0;
155 
156  /// Returns the number of arguments accepted by the matcher if not variadic.
157  virtual unsigned getNumArgs() const = 0;
158 
159  /// Given that the matcher is being converted to type \p ThisKind, append the
160  /// set of argument types accepted for argument \p ArgNo to \p ArgKinds.
161  // FIXME: We should provide the ability to constrain the output of this
162  // function based on the types of other matcher arguments.
163  virtual void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
164  std::vector<ArgKind> &ArgKinds) const = 0;
165 
166  /// Returns whether this matcher is convertible to the given type. If it is
167  /// so convertible, store in *Specificity a value corresponding to the
168  /// "specificity" of the converted matcher to the given context, and in
169  /// *LeastDerivedKind the least derived matcher kind which would result in the
170  /// same matcher overload. Zero specificity indicates that this conversion
171  /// would produce a trivial matcher that will either always or never match.
172  /// Such matchers are excluded from code completion results.
173  virtual bool isConvertibleTo(
174  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity = nullptr,
175  ast_type_traits::ASTNodeKind *LeastDerivedKind = nullptr) const = 0;
176 
177  /// Returns whether the matcher will, given a matcher of any type T, yield a
178  /// matcher of type T.
179  virtual bool isPolymorphic() const { return false; }
180 };
181 
184  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
185  ast_type_traits::ASTNodeKind *LeastDerivedKind) {
186  for (const ast_type_traits::ASTNodeKind &NodeKind : RetKinds) {
187  if (ArgKind(NodeKind).isConvertibleTo(Kind, Specificity)) {
188  if (LeastDerivedKind)
189  *LeastDerivedKind = NodeKind;
190  return true;
191  }
192  }
193  return false;
194 }
195 
196 /// \brief Simple callback implementation. Marshaller and function are provided.
197 ///
198 /// This class wraps a function of arbitrary signature and a marshaller
199 /// function into a MatcherDescriptor.
200 /// The marshaller is in charge of taking the VariantValue arguments, checking
201 /// their types, unpacking them and calling the underlying function.
203 public:
204  typedef VariantMatcher (*MarshallerType)(void (*Func)(),
205  StringRef MatcherName,
206  SourceRange NameRange,
208  Diagnostics *Error);
209 
210  /// \param Marshaller Function to unpack the arguments and call \c Func
211  /// \param Func Matcher construct function. This is the function that
212  /// compile-time matcher expressions would use to create the matcher.
213  /// \param RetKinds The list of matcher types to which the matcher is
214  /// convertible.
215  /// \param ArgKinds The types of the arguments this matcher takes.
217  MarshallerType Marshaller, void (*Func)(), StringRef MatcherName,
219  ArrayRef<ArgKind> ArgKinds)
220  : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName),
221  RetKinds(RetKinds.begin(), RetKinds.end()),
222  ArgKinds(ArgKinds.begin(), ArgKinds.end()) {}
223 
226  Diagnostics *Error) const override {
227  return Marshaller(Func, MatcherName, NameRange, Args, Error);
228  }
229 
230  bool isVariadic() const override { return false; }
231  unsigned getNumArgs() const override { return ArgKinds.size(); }
232  void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
233  std::vector<ArgKind> &Kinds) const override {
234  Kinds.push_back(ArgKinds[ArgNo]);
235  }
237  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
238  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
239  return isRetKindConvertibleTo(RetKinds, Kind, Specificity,
240  LeastDerivedKind);
241  }
242 
243 private:
244  const MarshallerType Marshaller;
245  void (* const Func)();
246  const std::string MatcherName;
247  const std::vector<ast_type_traits::ASTNodeKind> RetKinds;
248  const std::vector<ArgKind> ArgKinds;
249 };
250 
251 /// \brief Helper methods to extract and merge all possible typed matchers
252 /// out of the polymorphic object.
253 template <class PolyMatcher>
254 static void mergePolyMatchers(const PolyMatcher &Poly,
255  std::vector<DynTypedMatcher> &Out,
256  ast_matchers::internal::EmptyTypeList) {}
257 
258 template <class PolyMatcher, class TypeList>
259 static void mergePolyMatchers(const PolyMatcher &Poly,
260  std::vector<DynTypedMatcher> &Out, TypeList) {
261  Out.push_back(ast_matchers::internal::Matcher<typename TypeList::head>(Poly));
262  mergePolyMatchers(Poly, Out, typename TypeList::tail());
263 }
264 
265 /// \brief Convert the return values of the functions into a VariantMatcher.
266 ///
267 /// There are 2 cases right now: The return value is a Matcher<T> or is a
268 /// polymorphic matcher. For the former, we just construct the VariantMatcher.
269 /// For the latter, we instantiate all the possible Matcher<T> of the poly
270 /// matcher.
271 static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher) {
272  return VariantMatcher::SingleMatcher(Matcher);
273 }
274 
275 template <typename T>
276 static VariantMatcher outvalueToVariantMatcher(const T &PolyMatcher,
277  typename T::ReturnTypes * =
278  nullptr) {
279  std::vector<DynTypedMatcher> Matchers;
280  mergePolyMatchers(PolyMatcher, Matchers, typename T::ReturnTypes());
281  VariantMatcher Out = VariantMatcher::PolymorphicMatcher(std::move(Matchers));
282  return Out;
283 }
284 
285 template <typename T>
287  std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
288  RetTypes.push_back(
289  ast_type_traits::ASTNodeKind::getFromNodeKind<typename T::head>());
290  buildReturnTypeVectorFromTypeList<typename T::tail>(RetTypes);
291 }
292 
293 template <>
294 inline void
295 buildReturnTypeVectorFromTypeList<ast_matchers::internal::EmptyTypeList>(
296  std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {}
297 
298 template <typename T>
300  static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
301  buildReturnTypeVectorFromTypeList<typename T::ReturnTypes>(RetTypes);
302  }
303 };
304 
305 template <typename T>
306 struct BuildReturnTypeVector<ast_matchers::internal::Matcher<T> > {
307  static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
308  RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
309  }
310 };
311 
312 template <typename T>
313 struct BuildReturnTypeVector<ast_matchers::internal::BindableMatcher<T> > {
314  static void build(std::vector<ast_type_traits::ASTNodeKind> &RetTypes) {
315  RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
316  }
317 };
318 
319 /// \brief Variadic marshaller function.
320 template <typename ResultT, typename ArgT,
321  ResultT (*Func)(ArrayRef<const ArgT *>)>
323 variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange,
324  ArrayRef<ParserValue> Args, Diagnostics *Error) {
325  ArgT **InnerArgs = new ArgT *[Args.size()]();
326 
327  bool HasError = false;
328  for (size_t i = 0, e = Args.size(); i != e; ++i) {
329  typedef ArgTypeTraits<ArgT> ArgTraits;
330  const ParserValue &Arg = Args[i];
331  const VariantValue &Value = Arg.Value;
332  if (!ArgTraits::is(Value)) {
333  Error->addError(Arg.Range, Error->ET_RegistryWrongArgType)
334  << (i + 1) << ArgTraits::getKind().asString() << Value.getTypeAsString();
335  HasError = true;
336  break;
337  }
338  InnerArgs[i] = new ArgT(ArgTraits::get(Value));
339  }
340 
341  VariantMatcher Out;
342  if (!HasError) {
343  Out = outvalueToVariantMatcher(Func(llvm::makeArrayRef(InnerArgs,
344  Args.size())));
345  }
346 
347  for (size_t i = 0, e = Args.size(); i != e; ++i) {
348  delete InnerArgs[i];
349  }
350  delete[] InnerArgs;
351  return Out;
352 }
353 
354 /// \brief Matcher descriptor for variadic functions.
355 ///
356 /// This class simply wraps a VariadicFunction with the right signature to export
357 /// it as a MatcherDescriptor.
358 /// This allows us to have one implementation of the interface for as many free
359 /// functions as we want, reducing the number of symbols and size of the
360 /// object file.
362 public:
363  typedef VariantMatcher (*RunFunc)(StringRef MatcherName,
364  SourceRange NameRange,
366  Diagnostics *Error);
367 
368  template <typename ResultT, typename ArgT,
369  ResultT (*F)(ArrayRef<const ArgT *>)>
371  ast_matchers::internal::VariadicFunction<ResultT, ArgT, F> Func,
372  StringRef MatcherName)
373  : Func(&variadicMatcherDescriptor<ResultT, ArgT, F>),
374  MatcherName(MatcherName.str()),
375  ArgsKind(ArgTypeTraits<ArgT>::getKind()) {
377  }
378 
381  Diagnostics *Error) const override {
382  return Func(MatcherName, NameRange, Args, Error);
383  }
384 
385  bool isVariadic() const override { return true; }
386  unsigned getNumArgs() const override { return 0; }
387  void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
388  std::vector<ArgKind> &Kinds) const override {
389  Kinds.push_back(ArgsKind);
390  }
392  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
393  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
394  return isRetKindConvertibleTo(RetKinds, Kind, Specificity,
395  LeastDerivedKind);
396  }
397 
398 private:
399  const RunFunc Func;
400  const std::string MatcherName;
401  std::vector<ast_type_traits::ASTNodeKind> RetKinds;
402  const ArgKind ArgsKind;
403 };
404 
405 /// \brief Return CK_Trivial when appropriate for VariadicDynCastAllOfMatchers.
407 public:
408  template <typename BaseT, typename DerivedT>
410  ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT> Func,
411  StringRef MatcherName)
412  : VariadicFuncMatcherDescriptor(Func, MatcherName),
413  DerivedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<DerivedT>()) {
414  }
415 
416  bool
418  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
419  // If Kind is not a base of DerivedKind, either DerivedKind is a base of
420  // Kind (in which case the match will always succeed) or Kind and
421  // DerivedKind are unrelated (in which case it will always fail), so set
422  // Specificity to 0.
424  LeastDerivedKind)) {
425  if (Kind.isSame(DerivedKind) || !Kind.isBaseOf(DerivedKind)) {
426  if (Specificity)
427  *Specificity = 0;
428  }
429  return true;
430  } else {
431  return false;
432  }
433  }
434 
435 private:
436  const ast_type_traits::ASTNodeKind DerivedKind;
437 };
438 
439 /// \brief Helper macros to check the arguments on all marshaller functions.
440 #define CHECK_ARG_COUNT(count) \
441  if (Args.size() != count) { \
442  Error->addError(NameRange, Error->ET_RegistryWrongArgCount) \
443  << count << Args.size(); \
444  return VariantMatcher(); \
445  }
446 
447 #define CHECK_ARG_TYPE(index, type) \
448  if (!ArgTypeTraits<type>::is(Args[index].Value)) { \
449  Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \
450  << (index + 1) << ArgTypeTraits<type>::getKind().asString() \
451  << Args[index].Value.getTypeAsString(); \
452  return VariantMatcher(); \
453  }
454 
455 /// \brief 0-arg marshaller function.
456 template <typename ReturnType>
457 static VariantMatcher matcherMarshall0(void (*Func)(), StringRef MatcherName,
458  SourceRange NameRange,
460  Diagnostics *Error) {
461  typedef ReturnType (*FuncType)();
462  CHECK_ARG_COUNT(0);
463  return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)());
464 }
465 
466 /// \brief 1-arg marshaller function.
467 template <typename ReturnType, typename ArgType1>
468 static VariantMatcher matcherMarshall1(void (*Func)(), StringRef MatcherName,
469  SourceRange NameRange,
471  Diagnostics *Error) {
472  typedef ReturnType (*FuncType)(ArgType1);
473  CHECK_ARG_COUNT(1);
474  CHECK_ARG_TYPE(0, ArgType1);
475  return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)(
477 }
478 
479 /// \brief 2-arg marshaller function.
480 template <typename ReturnType, typename ArgType1, typename ArgType2>
481 static VariantMatcher matcherMarshall2(void (*Func)(), StringRef MatcherName,
482  SourceRange NameRange,
484  Diagnostics *Error) {
485  typedef ReturnType (*FuncType)(ArgType1, ArgType2);
486  CHECK_ARG_COUNT(2);
487  CHECK_ARG_TYPE(0, ArgType1);
488  CHECK_ARG_TYPE(1, ArgType2);
489  return outvalueToVariantMatcher(reinterpret_cast<FuncType>(Func)(
491  ArgTypeTraits<ArgType2>::get(Args[1].Value)));
492 }
493 
494 #undef CHECK_ARG_COUNT
495 #undef CHECK_ARG_TYPE
496 
497 /// \brief Helper class used to collect all the possible overloads of an
498 /// argument adaptative matcher function.
499 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
500  typename FromTypes, typename ToTypes>
502 public:
504  StringRef Name, std::vector<std::unique_ptr<MatcherDescriptor>> &Out)
505  : Name(Name), Out(Out) {
506  collect(FromTypes());
507  }
508 
509 private:
510  typedef ast_matchers::internal::ArgumentAdaptingMatcherFunc<
511  ArgumentAdapterT, FromTypes, ToTypes> AdaptativeFunc;
512 
513  /// \brief End case for the recursion
514  static void collect(ast_matchers::internal::EmptyTypeList) {}
515 
516  /// \brief Recursive case. Get the overload for the head of the list, and
517  /// recurse to the tail.
518  template <typename FromTypeList>
519  inline void collect(FromTypeList);
520 
521  StringRef Name;
522  std::vector<std::unique_ptr<MatcherDescriptor>> &Out;
523 };
524 
525 /// \brief MatcherDescriptor that wraps multiple "overloads" of the same
526 /// matcher.
527 ///
528 /// It will try every overload and generate appropriate errors for when none or
529 /// more than one overloads match the arguments.
531 public:
533  MutableArrayRef<std::unique_ptr<MatcherDescriptor>> Callbacks)
534  : Overloads(std::make_move_iterator(Callbacks.begin()),
535  std::make_move_iterator(Callbacks.end())) {}
536 
538 
541  Diagnostics *Error) const override {
542  std::vector<VariantMatcher> Constructed;
543  Diagnostics::OverloadContext Ctx(Error);
544  for (const auto &O : Overloads) {
545  VariantMatcher SubMatcher = O->create(NameRange, Args, Error);
546  if (!SubMatcher.isNull()) {
547  Constructed.push_back(SubMatcher);
548  }
549  }
550 
551  if (Constructed.empty()) return VariantMatcher(); // No overload matched.
552  // We ignore the errors if any matcher succeeded.
553  Ctx.revertErrors();
554  if (Constructed.size() > 1) {
555  // More than one constructed. It is ambiguous.
556  Error->addError(NameRange, Error->ET_RegistryAmbiguousOverload);
557  return VariantMatcher();
558  }
559  return Constructed[0];
560  }
561 
562  bool isVariadic() const override {
563  bool Overload0Variadic = Overloads[0]->isVariadic();
564 #ifndef NDEBUG
565  for (const auto &O : Overloads) {
566  assert(Overload0Variadic == O->isVariadic());
567  }
568 #endif
569  return Overload0Variadic;
570  }
571 
572  unsigned getNumArgs() const override {
573  unsigned Overload0NumArgs = Overloads[0]->getNumArgs();
574 #ifndef NDEBUG
575  for (const auto &O : Overloads) {
576  assert(Overload0NumArgs == O->getNumArgs());
577  }
578 #endif
579  return Overload0NumArgs;
580  }
581 
582  void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
583  std::vector<ArgKind> &Kinds) const override {
584  for (const auto &O : Overloads) {
585  if (O->isConvertibleTo(ThisKind))
586  O->getArgKinds(ThisKind, ArgNo, Kinds);
587  }
588  }
589 
591  ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
592  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
593  for (const auto &O : Overloads) {
594  if (O->isConvertibleTo(Kind, Specificity, LeastDerivedKind))
595  return true;
596  }
597  return false;
598  }
599 
600 private:
601  std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
602 };
603 
604 /// \brief Variadic operator marshaller function.
606 public:
607  typedef DynTypedMatcher::VariadicOperator VarOp;
608  VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount,
609  VarOp Op, StringRef MatcherName)
610  : MinCount(MinCount), MaxCount(MaxCount), Op(Op),
611  MatcherName(MatcherName) {}
612 
615  Diagnostics *Error) const override {
616  if (Args.size() < MinCount || MaxCount < Args.size()) {
617  const std::string MaxStr =
618  (MaxCount == UINT_MAX ? "" : Twine(MaxCount)).str();
619  Error->addError(NameRange, Error->ET_RegistryWrongArgCount)
620  << ("(" + Twine(MinCount) + ", " + MaxStr + ")") << Args.size();
621  return VariantMatcher();
622  }
623 
624  std::vector<VariantMatcher> InnerArgs;
625  for (size_t i = 0, e = Args.size(); i != e; ++i) {
626  const ParserValue &Arg = Args[i];
627  const VariantValue &Value = Arg.Value;
628  if (!Value.isMatcher()) {
629  Error->addError(Arg.Range, Error->ET_RegistryWrongArgType)
630  << (i + 1) << "Matcher<>" << Value.getTypeAsString();
631  return VariantMatcher();
632  }
633  InnerArgs.push_back(Value.getMatcher());
634  }
635  return VariantMatcher::VariadicOperatorMatcher(Op, std::move(InnerArgs));
636  }
637 
638  bool isVariadic() const override { return true; }
639  unsigned getNumArgs() const override { return 0; }
640  void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo,
641  std::vector<ArgKind> &Kinds) const override {
642  Kinds.push_back(ThisKind);
643  }
644  bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity,
645  ast_type_traits::ASTNodeKind *LeastDerivedKind) const override {
646  if (Specificity)
647  *Specificity = 1;
648  if (LeastDerivedKind)
649  *LeastDerivedKind = Kind;
650  return true;
651  }
652  bool isPolymorphic() const override { return true; }
653 
654 private:
655  const unsigned MinCount;
656  const unsigned MaxCount;
657  const VarOp Op;
658  const StringRef MatcherName;
659 };
660 
661 /// Helper functions to select the appropriate marshaller functions.
662 /// They detect the number of arguments, arguments types and return type.
663 
664 /// \brief 0-arg overload
665 template <typename ReturnType>
666 std::unique_ptr<MatcherDescriptor>
667 makeMatcherAutoMarshall(ReturnType (*Func)(), StringRef MatcherName) {
668  std::vector<ast_type_traits::ASTNodeKind> RetTypes;
670  return llvm::make_unique<FixedArgCountMatcherDescriptor>(
671  matcherMarshall0<ReturnType>, reinterpret_cast<void (*)()>(Func),
672  MatcherName, RetTypes, None);
673 }
674 
675 /// \brief 1-arg overload
676 template <typename ReturnType, typename ArgType1>
677 std::unique_ptr<MatcherDescriptor>
678 makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1), StringRef MatcherName) {
679  std::vector<ast_type_traits::ASTNodeKind> RetTypes;
682  return llvm::make_unique<FixedArgCountMatcherDescriptor>(
683  matcherMarshall1<ReturnType, ArgType1>,
684  reinterpret_cast<void (*)()>(Func), MatcherName, RetTypes, AK);
685 }
686 
687 /// \brief 2-arg overload
688 template <typename ReturnType, typename ArgType1, typename ArgType2>
689 std::unique_ptr<MatcherDescriptor>
690 makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1, ArgType2),
691  StringRef MatcherName) {
692  std::vector<ast_type_traits::ASTNodeKind> RetTypes;
696  return llvm::make_unique<FixedArgCountMatcherDescriptor>(
697  matcherMarshall2<ReturnType, ArgType1, ArgType2>,
698  reinterpret_cast<void (*)()>(Func), MatcherName, RetTypes, AKs);
699 }
700 
701 /// \brief Variadic overload.
702 template <typename ResultT, typename ArgT,
703  ResultT (*Func)(ArrayRef<const ArgT *>)>
704 std::unique_ptr<MatcherDescriptor> makeMatcherAutoMarshall(
705  ast_matchers::internal::VariadicFunction<ResultT, ArgT, Func> VarFunc,
706  StringRef MatcherName) {
707  return llvm::make_unique<VariadicFuncMatcherDescriptor>(VarFunc, MatcherName);
708 }
709 
710 /// \brief Overload for VariadicDynCastAllOfMatchers.
711 ///
712 /// Not strictly necessary, but DynCastAllOfMatcherDescriptor gives us better
713 /// completion results for that type of matcher.
714 template <typename BaseT, typename DerivedT>
715 std::unique_ptr<MatcherDescriptor> makeMatcherAutoMarshall(
716  ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT>
717  VarFunc,
718  StringRef MatcherName) {
719  return llvm::make_unique<DynCastAllOfMatcherDescriptor>(VarFunc, MatcherName);
720 }
721 
722 /// \brief Argument adaptative overload.
723 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
724  typename FromTypes, typename ToTypes>
725 std::unique_ptr<MatcherDescriptor> makeMatcherAutoMarshall(
726  ast_matchers::internal::ArgumentAdaptingMatcherFunc<ArgumentAdapterT,
727  FromTypes, ToTypes>,
728  StringRef MatcherName) {
729  std::vector<std::unique_ptr<MatcherDescriptor>> Overloads;
731  Overloads);
732  return llvm::make_unique<OverloadedMatcherDescriptor>(Overloads);
733 }
734 
735 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
736  typename FromTypes, typename ToTypes>
737 template <typename FromTypeList>
738 inline void AdaptativeOverloadCollector<ArgumentAdapterT, FromTypes,
739  ToTypes>::collect(FromTypeList) {
740  Out.push_back(makeMatcherAutoMarshall(
741  &AdaptativeFunc::template create<typename FromTypeList::head>, Name));
742  collect(typename FromTypeList::tail());
743 }
744 
745 /// \brief Variadic operator overload.
746 template <unsigned MinCount, unsigned MaxCount>
747 std::unique_ptr<MatcherDescriptor> makeMatcherAutoMarshall(
748  ast_matchers::internal::VariadicOperatorMatcherFunc<MinCount, MaxCount>
749  Func,
750  StringRef MatcherName) {
751  return llvm::make_unique<VariadicOperatorMatcherDescriptor>(
752  MinCount, MaxCount, Func.Op, MatcherName);
753 }
754 
755 } // namespace internal
756 } // namespace dynamic
757 } // namespace ast_matchers
758 } // namespace clang
759 
760 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
Definition: Marshallers.h:640
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Definition: Marshallers.h:613
bool hasTypedMatcher() const
Determines if the contained matcher can be converted to Matcher<T>.
Definition: VariantValue.h:177
static const CastKind CK_Invalid
VariantMatcher variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
Variadic marshaller function.
Definition: Marshallers.h:323
bool isSame(ASTNodeKind Other) const
Returns true if this and Other represent the same kind.
Definition: ASTTypeTraits.h:65
bool isPolymorphic() const override
Returns whether the matcher will, given a matcher of any type T, yield a matcher of type T...
Definition: Marshallers.h:652
static VariantMatcher SingleMatcher(const DynTypedMatcher &Matcher)
Clones the provided matcher.
bool isBoolean() const
Boolean value functions.
bool isUnsigned() const
Unsigned value functions.
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:417
static VariantMatcher VariadicOperatorMatcher(DynTypedMatcher::VariadicOperator Op, std::vector< VariantMatcher > Args)
Creates a &#39;variadic&#39; operator matcher.
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
Definition: Marshallers.h:231
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Definition: Marshallers.h:224
FixedArgCountMatcherDescriptor(MarshallerType Marshaller, void(*Func)(), StringRef MatcherName, ArrayRef< ast_type_traits::ASTNodeKind > RetKinds, ArrayRef< ArgKind > ArgKinds)
Definition: Marshallers.h:216
Definition: Format.h:1821
virtual bool isPolymorphic() const
Returns whether the matcher will, given a matcher of any type T, yield a matcher of type T...
Definition: Marshallers.h:179
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Definition: Marshallers.h:539
OverloadedMatcherDescriptor(MutableArrayRef< std::unique_ptr< MatcherDescriptor >> Callbacks)
Definition: Marshallers.h:532
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount, VarOp Op, StringRef MatcherName)
Definition: Marshallers.h:608
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
Definition: Marshallers.h:386
void revertErrors()
Revert all errors that happened within this context.
Definition: Diagnostics.cpp:56
bool isVariadic() const override
Returns whether the matcher is variadic.
Definition: Marshallers.h:562
#define UINT_MAX
Definition: limits.h:72
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:391
A VariantValue instance annotated with its parser context.
Definition: Diagnostics.h:43
CastKind
CastKind - The kind of operation required for a conversion.
std::string getTypeAsString() const
String representation of the type of the value.
bool isRetKindConvertibleTo(ArrayRef< ast_type_traits::ASTNodeKind > RetKinds, ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind)
Definition: Marshallers.h:182
const FunctionProtoType * T
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
Definition: Marshallers.h:639
#define bool
Definition: stdbool.h:31
static void build(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
Definition: Marshallers.h:307
static VariantMatcher matcherMarshall2(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
2-arg marshaller function.
Definition: Marshallers.h:481
Diagnostics class to manage error messages.
bool isDouble() const
Double value functions.
VariadicFuncMatcherDescriptor(ast_matchers::internal::VariadicFunction< ResultT, ArgT, F > Func, StringRef MatcherName)
Definition: Marshallers.h:370
AdaptativeOverloadCollector(StringRef Name, std::vector< std::unique_ptr< MatcherDescriptor >> &Out)
Definition: Marshallers.h:503
Helper class to manage error messages.
Definition: Diagnostics.h:51
#define ATTR(X)
ArgStream addError(SourceRange Range, ErrorType Error)
Add an error to the diagnostics.
Definition: Diagnostics.cpp:66
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:236
Kind
bool isVariadic() const override
Returns whether the matcher is variadic.
Definition: Marshallers.h:638
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
Definition: Marshallers.h:232
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
Definition: Marshallers.h:582
VariantMatcher create(SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error) const override
Definition: Marshallers.h:379
const VariantMatcher & getMatcher() const
Return CK_Trivial when appropriate for VariadicDynCastAllOfMatchers.
Definition: Marshallers.h:406
#define CHECK_ARG_COUNT(count)
Helper macros to check the arguments on all marshaller functions.
Definition: Marshallers.h:440
const std::string & getString() const
bool isVariadic() const override
Returns whether the matcher is variadic.
Definition: Marshallers.h:230
bool isBaseOf(ASTNodeKind Other, unsigned *Distance=nullptr) const
Returns true if this is a base kind of (or same as) Other.
MatcherDescriptor that wraps multiple "overloads" of the same matcher.
Definition: Marshallers.h:530
std::unique_ptr< MatcherDescriptor > makeMatcherAutoMarshall(ReturnType(*Func)(), StringRef MatcherName)
Helper functions to select the appropriate marshaller functions.
Definition: Marshallers.h:667
Dataflow Directional Tag Classes.
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:644
bool isMatcher() const
Matcher value functions.
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
static VariantMatcher matcherMarshall0(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
0-arg marshaller function.
Definition: Marshallers.h:457
bool isNull() const
Whether the matcher is null.
Definition: VariantValue.h:159
static VariantMatcher PolymorphicMatcher(std::vector< DynTypedMatcher > Matchers)
Clones the provided matchers.
static void mergePolyMatchers(const PolyMatcher &Poly, std::vector< DynTypedMatcher > &Out, ast_matchers::internal::EmptyTypeList)
Helper methods to extract and merge all possible typed matchers out of the polymorphic object...
Definition: Marshallers.h:254
Helper template class to just from argument type to the right is/get functions in VariantValue...
Definition: Marshallers.h:38
Context for overloaded matcher construction.
Definition: Diagnostics.h:122
#define CHECK_ARG_TYPE(index, type)
Definition: Marshallers.h:447
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:13161
void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, std::vector< ArgKind > &Kinds) const override
Given that the matcher is being converted to type ThisKind, append the set of argument types accepted...
Definition: Marshallers.h:387
DynCastAllOfMatcherDescriptor(ast_matchers::internal::VariadicDynCastAllOfMatcher< BaseT, DerivedT > Func, StringRef MatcherName)
Definition: Marshallers.h:409
static void build(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
Definition: Marshallers.h:300
#define CAST_OPERATION(Name)
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:896
Polymorphic value type.
bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, ast_type_traits::ASTNodeKind *LeastDerivedKind) const override
Returns whether this matcher is convertible to the given type.
Definition: Marshallers.h:590
static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher)
Convert the return values of the functions into a VariantMatcher.
Definition: Marshallers.h:271
Helper class used to collect all the possible overloads of an argument adaptative matcher function...
Definition: Marshallers.h:501
unsigned getNumArgs() const override
Returns the number of arguments accepted by the matcher if not variadic.
Definition: Marshallers.h:572
bool isString() const
String value functions.
static VariantMatcher matcherMarshall1(void(*Func)(), StringRef MatcherName, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
1-arg marshaller function.
Definition: Marshallers.h:468
void buildReturnTypeVectorFromTypeList(std::vector< ast_type_traits::ASTNodeKind > &RetTypes)
Definition: Marshallers.h:286
bool isVariadic() const override
Returns whether the matcher is variadic.
Definition: Marshallers.h:385