clang API Documentation

ASTMatchersInternal.h
Go to the documentation of this file.
00001 //===--- ASTMatchersInternal.h - Structural query framework -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  Implements the base layer of the matcher framework.
00011 //
00012 //  Matchers are methods that return a Matcher<T> which provides a method
00013 //  Matches(...) which is a predicate on an AST node. The Matches method's
00014 //  parameters define the context of the match, which allows matchers to recurse
00015 //  or store the current node as bound to a specific string, so that it can be
00016 //  retrieved later.
00017 //
00018 //  In general, matchers have two parts:
00019 //  1. A function Matcher<T> MatcherName(<arguments>) which returns a Matcher<T>
00020 //     based on the arguments and optionally on template type deduction based
00021 //     on the arguments. Matcher<T>s form an implicit reverse hierarchy
00022 //     to clang's AST class hierarchy, meaning that you can use a Matcher<Base>
00023 //     everywhere a Matcher<Derived> is required.
00024 //  2. An implementation of a class derived from MatcherInterface<T>.
00025 //
00026 //  The matcher functions are defined in ASTMatchers.h. To make it possible
00027 //  to implement both the matcher function and the implementation of the matcher
00028 //  interface in one place, ASTMatcherMacros.h defines macros that allow
00029 //  implementing a matcher in a single place.
00030 //
00031 //  This file contains the base classes needed to construct the actual matchers.
00032 //
00033 //===----------------------------------------------------------------------===//
00034 
00035 #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERSINTERNAL_H
00036 #define LLVM_CLANG_ASTMATCHERS_ASTMATCHERSINTERNAL_H
00037 
00038 #include "clang/AST/ASTTypeTraits.h"
00039 #include "clang/AST/Decl.h"
00040 #include "clang/AST/DeclCXX.h"
00041 #include "clang/AST/ExprCXX.h"
00042 #include "clang/AST/Stmt.h"
00043 #include "clang/AST/StmtCXX.h"
00044 #include "clang/AST/Type.h"
00045 #include "llvm/ADT/Optional.h"
00046 #include "llvm/ADT/VariadicFunction.h"
00047 #include <map>
00048 #include <string>
00049 #include <vector>
00050 
00051 namespace clang {
00052 namespace ast_matchers {
00053 
00054 class BoundNodes;
00055 
00056 namespace internal {
00057 
00058 /// \brief Internal version of BoundNodes. Holds all the bound nodes.
00059 class BoundNodesMap {
00060 public:
00061   /// \brief Adds \c Node to the map with key \c ID.
00062   ///
00063   /// The node's base type should be in NodeBaseType or it will be unaccessible.
00064   void addNode(StringRef ID, const ast_type_traits::DynTypedNode& DynNode) {
00065     NodeMap[ID] = DynNode;
00066   }
00067 
00068   /// \brief Returns the AST node bound to \c ID.
00069   ///
00070   /// Returns NULL if there was no node bound to \c ID or if there is a node but
00071   /// it cannot be converted to the specified type.
00072   template <typename T>
00073   const T *getNodeAs(StringRef ID) const {
00074     IDToNodeMap::const_iterator It = NodeMap.find(ID);
00075     if (It == NodeMap.end()) {
00076       return nullptr;
00077     }
00078     return It->second.get<T>();
00079   }
00080 
00081   ast_type_traits::DynTypedNode getNode(StringRef ID) const {
00082     IDToNodeMap::const_iterator It = NodeMap.find(ID);
00083     if (It == NodeMap.end()) {
00084       return ast_type_traits::DynTypedNode();
00085     }
00086     return It->second;
00087   }
00088 
00089   /// \brief Imposes an order on BoundNodesMaps.
00090   bool operator<(const BoundNodesMap &Other) const {
00091     return NodeMap < Other.NodeMap;
00092   }
00093 
00094   /// \brief A map from IDs to the bound nodes.
00095   ///
00096   /// Note that we're using std::map here, as for memoization:
00097   /// - we need a comparison operator
00098   /// - we need an assignment operator
00099   typedef std::map<std::string, ast_type_traits::DynTypedNode> IDToNodeMap;
00100 
00101   const IDToNodeMap &getMap() const {
00102     return NodeMap;
00103   }
00104 
00105   /// \brief Returns \c true if this \c BoundNodesMap can be compared, i.e. all
00106   /// stored nodes have memoization data.
00107   bool isComparable() const {
00108     for (const auto &IDAndNode : NodeMap) {
00109       if (!IDAndNode.second.getMemoizationData())
00110         return false;
00111     }
00112     return true;
00113   }
00114 
00115 private:
00116   IDToNodeMap NodeMap;
00117 };
00118 
00119 /// \brief Creates BoundNodesTree objects.
00120 ///
00121 /// The tree builder is used during the matching process to insert the bound
00122 /// nodes from the Id matcher.
00123 class BoundNodesTreeBuilder {
00124 public:
00125   /// \brief A visitor interface to visit all BoundNodes results for a
00126   /// BoundNodesTree.
00127   class Visitor {
00128   public:
00129     virtual ~Visitor() {}
00130 
00131     /// \brief Called multiple times during a single call to VisitMatches(...).
00132     ///
00133     /// 'BoundNodesView' contains the bound nodes for a single match.
00134     virtual void visitMatch(const BoundNodes& BoundNodesView) = 0;
00135   };
00136 
00137   /// \brief Add a binding from an id to a node.
00138   void setBinding(const std::string &Id,
00139                   const ast_type_traits::DynTypedNode &DynNode) {
00140     if (Bindings.empty())
00141       Bindings.push_back(BoundNodesMap());
00142     for (BoundNodesMap &Binding : Bindings)
00143       Binding.addNode(Id, DynNode);
00144   }
00145 
00146   /// \brief Adds a branch in the tree.
00147   void addMatch(const BoundNodesTreeBuilder &Bindings);
00148 
00149   /// \brief Visits all matches that this BoundNodesTree represents.
00150   ///
00151   /// The ownership of 'ResultVisitor' remains at the caller.
00152   void visitMatches(Visitor* ResultVisitor);
00153 
00154   template <typename ExcludePredicate>
00155   bool removeBindings(const ExcludePredicate &Predicate) {
00156     Bindings.erase(std::remove_if(Bindings.begin(), Bindings.end(), Predicate),
00157                    Bindings.end());
00158     return !Bindings.empty();
00159   }
00160 
00161   /// \brief Imposes an order on BoundNodesTreeBuilders.
00162   bool operator<(const BoundNodesTreeBuilder &Other) const {
00163     return Bindings < Other.Bindings;
00164   }
00165 
00166   /// \brief Returns \c true if this \c BoundNodesTreeBuilder can be compared,
00167   /// i.e. all stored node maps have memoization data.
00168   bool isComparable() const {
00169     for (const BoundNodesMap &NodesMap : Bindings) {
00170       if (!NodesMap.isComparable())
00171         return false;
00172     }
00173     return true;
00174   }
00175 
00176 private:
00177   SmallVector<BoundNodesMap, 16> Bindings;
00178 };
00179 
00180 class ASTMatchFinder;
00181 
00182 /// \brief Generic interface for all matchers.
00183 ///
00184 /// Used by the implementation of Matcher<T> and DynTypedMatcher.
00185 /// In general, implement MatcherInterface<T> or SingleNodeMatcherInterface<T>
00186 /// instead.
00187 class DynMatcherInterface : public RefCountedBaseVPTR {
00188 public:
00189   /// \brief Returns true if \p DynNode can be matched.
00190   ///
00191   /// May bind \p DynNode to an ID via \p Builder, or recurse into
00192   /// the AST via \p Finder.
00193   virtual bool dynMatches(const ast_type_traits::DynTypedNode &DynNode,
00194                           ASTMatchFinder *Finder,
00195                           BoundNodesTreeBuilder *Builder) const = 0;
00196 };
00197 
00198 /// \brief Generic interface for matchers on an AST node of type T.
00199 ///
00200 /// Implement this if your matcher may need to inspect the children or
00201 /// descendants of the node or bind matched nodes to names. If you are
00202 /// writing a simple matcher that only inspects properties of the
00203 /// current node and doesn't care about its children or descendants,
00204 /// implement SingleNodeMatcherInterface instead.
00205 template <typename T>
00206 class MatcherInterface : public DynMatcherInterface {
00207 public:
00208   virtual ~MatcherInterface() {}
00209 
00210   /// \brief Returns true if 'Node' can be matched.
00211   ///
00212   /// May bind 'Node' to an ID via 'Builder', or recurse into
00213   /// the AST via 'Finder'.
00214   virtual bool matches(const T &Node,
00215                        ASTMatchFinder *Finder,
00216                        BoundNodesTreeBuilder *Builder) const = 0;
00217 
00218   bool dynMatches(const ast_type_traits::DynTypedNode &DynNode,
00219                   ASTMatchFinder *Finder,
00220                   BoundNodesTreeBuilder *Builder) const override {
00221     if (const T *Node = DynNode.get<T>()) {
00222       return matches(*Node, Finder, Builder);
00223     }
00224     return false;
00225   }
00226 };
00227 
00228 /// \brief Interface for matchers that only evaluate properties on a single
00229 /// node.
00230 template <typename T>
00231 class SingleNodeMatcherInterface : public MatcherInterface<T> {
00232 public:
00233   /// \brief Returns true if the matcher matches the provided node.
00234   ///
00235   /// A subclass must implement this instead of Matches().
00236   virtual bool matchesNode(const T &Node) const = 0;
00237 
00238 private:
00239   /// Implements MatcherInterface::Matches.
00240   bool matches(const T &Node,
00241                ASTMatchFinder * /* Finder */,
00242                BoundNodesTreeBuilder * /*  Builder */) const override {
00243     return matchesNode(Node);
00244   }
00245 };
00246 
00247 template <typename> class Matcher;
00248 
00249 /// \brief Matcher that works on a \c DynTypedNode.
00250 ///
00251 /// It is constructed from a \c Matcher<T> object and redirects most calls to
00252 /// underlying matcher.
00253 /// It checks whether the \c DynTypedNode is convertible into the type of the
00254 /// underlying matcher and then do the actual match on the actual node, or
00255 /// return false if it is not convertible.
00256 class DynTypedMatcher {
00257 public:
00258   /// \brief Takes ownership of the provided implementation pointer.
00259   template <typename T>
00260   DynTypedMatcher(MatcherInterface<T> *Implementation)
00261       : AllowBind(false),
00262         SupportedKind(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()),
00263         RestrictKind(SupportedKind), Implementation(Implementation) {}
00264 
00265   /// \brief Construct from a variadic function.
00266   typedef bool (*VariadicOperatorFunction)(
00267       const ast_type_traits::DynTypedNode DynNode, ASTMatchFinder *Finder,
00268       BoundNodesTreeBuilder *Builder, ArrayRef<DynTypedMatcher> InnerMatchers);
00269   static DynTypedMatcher
00270   constructVariadic(VariadicOperatorFunction Func,
00271                     std::vector<DynTypedMatcher> InnerMatchers);
00272 
00273   /// \brief Get a "true" matcher for \p NodeKind.
00274   ///
00275   /// It only checks that the node is of the right kind.
00276   static DynTypedMatcher trueMatcher(ast_type_traits::ASTNodeKind NodeKind);
00277 
00278   void setAllowBind(bool AB) { AllowBind = AB; }
00279 
00280   /// \brief Return a matcher that points to the same implementation, but
00281   ///   restricts the node types for \p Kind.
00282   DynTypedMatcher dynCastTo(const ast_type_traits::ASTNodeKind Kind) const;
00283 
00284   /// \brief Returns true if the matcher matches the given \c DynNode.
00285   bool matches(const ast_type_traits::DynTypedNode &DynNode,
00286                ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const;
00287 
00288   /// \brief Bind the specified \p ID to the matcher.
00289   /// \return A new matcher with the \p ID bound to it if this matcher supports
00290   ///   binding. Otherwise, returns an empty \c Optional<>.
00291   llvm::Optional<DynTypedMatcher> tryBind(StringRef ID) const;
00292 
00293   /// \brief Returns a unique \p ID for the matcher.
00294   ///
00295   /// Casting a Matcher<T> to Matcher<U> creates a matcher that has the
00296   /// same \c Implementation pointer, but different \c RestrictKind. We need to
00297   /// include both in the ID to make it unique.
00298   ///
00299   /// \c MatcherIDType supports operator< and provides strict weak ordering.
00300   typedef std::pair<ast_type_traits::ASTNodeKind, uint64_t> MatcherIDType;
00301   MatcherIDType getID() const {
00302     /// FIXME: Document the requirements this imposes on matcher
00303     /// implementations (no new() implementation_ during a Matches()).
00304     return std::make_pair(RestrictKind,
00305                           reinterpret_cast<uint64_t>(Implementation.get()));
00306   }
00307 
00308   /// \brief Returns the type this matcher works on.
00309   ///
00310   /// \c matches() will always return false unless the node passed is of this
00311   /// or a derived type.
00312   ast_type_traits::ASTNodeKind getSupportedKind() const {
00313     return SupportedKind;
00314   }
00315 
00316   /// \brief Returns \c true if the passed \c DynTypedMatcher can be converted
00317   ///   to a \c Matcher<T>.
00318   ///
00319   /// This method verifies that the underlying matcher in \c Other can process
00320   /// nodes of types T.
00321   template <typename T> bool canConvertTo() const {
00322     return canConvertTo(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
00323   }
00324   bool canConvertTo(ast_type_traits::ASTNodeKind To) const;
00325 
00326   /// \brief Construct a \c Matcher<T> interface around the dynamic matcher.
00327   ///
00328   /// This method asserts that \c canConvertTo() is \c true. Callers
00329   /// should call \c canConvertTo() first to make sure that \c this is
00330   /// compatible with T.
00331   template <typename T> Matcher<T> convertTo() const {
00332     assert(canConvertTo<T>());
00333     return unconditionalConvertTo<T>();
00334   }
00335 
00336   /// \brief Same as \c convertTo(), but does not check that the underlying
00337   ///   matcher can handle a value of T.
00338   ///
00339   /// If it is not compatible, then this matcher will never match anything.
00340   template <typename T> Matcher<T> unconditionalConvertTo() const;
00341 
00342 private:
00343  DynTypedMatcher(ast_type_traits::ASTNodeKind SupportedKind,
00344                  ast_type_traits::ASTNodeKind RestrictKind,
00345                  IntrusiveRefCntPtr<DynMatcherInterface> Implementation)
00346      : AllowBind(false),
00347        SupportedKind(SupportedKind),
00348        RestrictKind(RestrictKind),
00349        Implementation(std::move(Implementation)) {}
00350 
00351   bool AllowBind;
00352   ast_type_traits::ASTNodeKind SupportedKind;
00353   /// \brief A potentially stricter node kind.
00354   ///
00355   /// It allows to perform implicit and dynamic cast of matchers without
00356   /// needing to change \c Implementation.
00357   ast_type_traits::ASTNodeKind RestrictKind;
00358   IntrusiveRefCntPtr<DynMatcherInterface> Implementation;
00359 };
00360 
00361 /// \brief Wrapper of a MatcherInterface<T> *that allows copying.
00362 ///
00363 /// A Matcher<Base> can be used anywhere a Matcher<Derived> is
00364 /// required. This establishes an is-a relationship which is reverse
00365 /// to the AST hierarchy. In other words, Matcher<T> is contravariant
00366 /// with respect to T. The relationship is built via a type conversion
00367 /// operator rather than a type hierarchy to be able to templatize the
00368 /// type hierarchy instead of spelling it out.
00369 template <typename T>
00370 class Matcher {
00371 public:
00372   /// \brief Takes ownership of the provided implementation pointer.
00373   explicit Matcher(MatcherInterface<T> *Implementation)
00374       : Implementation(Implementation) {}
00375 
00376   /// \brief Implicitly converts \c Other to a Matcher<T>.
00377   ///
00378   /// Requires \c T to be derived from \c From.
00379   template <typename From>
00380   Matcher(const Matcher<From> &Other,
00381           typename std::enable_if<std::is_base_of<From, T>::value &&
00382                                   !std::is_same<From, T>::value>::type * = 0)
00383       : Implementation(restrictMatcher(Other.Implementation)) {
00384     assert(Implementation.getSupportedKind().isSame(
00385         ast_type_traits::ASTNodeKind::getFromNodeKind<T>()));
00386   }
00387 
00388   /// \brief Implicitly converts \c Matcher<Type> to \c Matcher<QualType>.
00389   ///
00390   /// The resulting matcher is not strict, i.e. ignores qualifiers.
00391   template <typename TypeT>
00392   Matcher(const Matcher<TypeT> &Other,
00393           typename std::enable_if<
00394             std::is_same<T, QualType>::value &&
00395             std::is_same<TypeT, Type>::value>::type* = 0)
00396       : Implementation(new TypeToQualType<TypeT>(Other)) {}
00397 
00398   /// \brief Convert \c this into a \c Matcher<T> by applying dyn_cast<> to the
00399   /// argument.
00400   /// \c To must be a base class of \c T.
00401   template <typename To>
00402   Matcher<To> dynCastTo() const {
00403     static_assert(std::is_base_of<To, T>::value, "Invalid dynCast call.");
00404     return Matcher<To>(Implementation);
00405   }
00406 
00407   /// \brief Forwards the call to the underlying MatcherInterface<T> pointer.
00408   bool matches(const T &Node,
00409                ASTMatchFinder *Finder,
00410                BoundNodesTreeBuilder *Builder) const {
00411     return Implementation.matches(ast_type_traits::DynTypedNode::create(Node),
00412                                   Finder, Builder);
00413   }
00414 
00415   /// \brief Returns an ID that uniquely identifies the matcher.
00416   DynTypedMatcher::MatcherIDType getID() const {
00417     return Implementation.getID();
00418   }
00419 
00420   /// \brief Extract the dynamic matcher.
00421   ///
00422   /// The returned matcher keeps the same restrictions as \c this and remembers
00423   /// that it is meant to support nodes of type \c T.
00424   operator DynTypedMatcher() const { return Implementation; }
00425 
00426   /// \brief Allows the conversion of a \c Matcher<Type> to a \c
00427   /// Matcher<QualType>.
00428   ///
00429   /// Depending on the constructor argument, the matcher is either strict, i.e.
00430   /// does only matches in the absence of qualifiers, or not, i.e. simply
00431   /// ignores any qualifiers.
00432   template <typename TypeT>
00433   class TypeToQualType : public MatcherInterface<QualType> {
00434    public:
00435     TypeToQualType(const Matcher<TypeT> &InnerMatcher)
00436         : InnerMatcher(InnerMatcher) {}
00437 
00438     bool matches(const QualType &Node, ASTMatchFinder *Finder,
00439                  BoundNodesTreeBuilder *Builder) const override {
00440       if (Node.isNull())
00441         return false;
00442       return InnerMatcher.matches(*Node, Finder, Builder);
00443     }
00444    private:
00445     const Matcher<TypeT> InnerMatcher;
00446   };
00447 
00448 private:
00449   // For Matcher<T> <=> Matcher<U> conversions.
00450   template <typename U> friend class Matcher;
00451   // For DynTypedMatcher::unconditionalConvertTo<T>.
00452   friend class DynTypedMatcher;
00453 
00454   static DynTypedMatcher restrictMatcher(const DynTypedMatcher &Other) {
00455     return Other.dynCastTo(ast_type_traits::ASTNodeKind::getFromNodeKind<T>());
00456   }
00457 
00458   explicit Matcher(const DynTypedMatcher &Implementation)
00459       : Implementation(restrictMatcher(Implementation)) {
00460     assert(this->Implementation.getSupportedKind()
00461                .isSame(ast_type_traits::ASTNodeKind::getFromNodeKind<T>()));
00462   }
00463 
00464   DynTypedMatcher Implementation;
00465 };  // class Matcher
00466 
00467 /// \brief A convenient helper for creating a Matcher<T> without specifying
00468 /// the template type argument.
00469 template <typename T>
00470 inline Matcher<T> makeMatcher(MatcherInterface<T> *Implementation) {
00471   return Matcher<T>(Implementation);
00472 }
00473 
00474 /// \brief Specialization of the conversion functions for QualType.
00475 ///
00476 /// This specialization provides the Matcher<Type>->Matcher<QualType>
00477 /// conversion that the static API does.
00478 template <>
00479 inline Matcher<QualType> DynTypedMatcher::convertTo<QualType>() const {
00480   assert(canConvertTo<QualType>());
00481   const ast_type_traits::ASTNodeKind SourceKind = getSupportedKind();
00482   if (SourceKind.isSame(
00483           ast_type_traits::ASTNodeKind::getFromNodeKind<Type>())) {
00484     // We support implicit conversion from Matcher<Type> to Matcher<QualType>
00485     return unconditionalConvertTo<Type>();
00486   }
00487   return unconditionalConvertTo<QualType>();
00488 }
00489 
00490 /// \brief Finds the first node in a range that matches the given matcher.
00491 template <typename MatcherT, typename IteratorT>
00492 bool matchesFirstInRange(const MatcherT &Matcher, IteratorT Start,
00493                          IteratorT End, ASTMatchFinder *Finder,
00494                          BoundNodesTreeBuilder *Builder) {
00495   for (IteratorT I = Start; I != End; ++I) {
00496     BoundNodesTreeBuilder Result(*Builder);
00497     if (Matcher.matches(*I, Finder, &Result)) {
00498       *Builder = std::move(Result);
00499       return true;
00500     }
00501   }
00502   return false;
00503 }
00504 
00505 /// \brief Finds the first node in a pointer range that matches the given
00506 /// matcher.
00507 template <typename MatcherT, typename IteratorT>
00508 bool matchesFirstInPointerRange(const MatcherT &Matcher, IteratorT Start,
00509                                 IteratorT End, ASTMatchFinder *Finder,
00510                                 BoundNodesTreeBuilder *Builder) {
00511   for (IteratorT I = Start; I != End; ++I) {
00512     BoundNodesTreeBuilder Result(*Builder);
00513     if (Matcher.matches(**I, Finder, &Result)) {
00514       *Builder = std::move(Result);
00515       return true;
00516     }
00517   }
00518   return false;
00519 }
00520 
00521 /// \brief Metafunction to determine if type T has a member called getDecl.
00522 template <typename T> struct has_getDecl {
00523   struct Default { int getDecl; };
00524   struct Derived : T, Default { };
00525 
00526   template<typename C, C> struct CheckT;
00527 
00528   // If T::getDecl exists, an ambiguity arises and CheckT will
00529   // not be instantiable. This makes f(...) the only available
00530   // overload.
00531   template<typename C>
00532   static char (&f(CheckT<int Default::*, &C::getDecl>*))[1];
00533   template<typename C> static char (&f(...))[2];
00534 
00535   static bool const value = sizeof(f<Derived>(nullptr)) == 2;
00536 };
00537 
00538 /// \brief Matches overloaded operators with a specific name.
00539 ///
00540 /// The type argument ArgT is not used by this matcher but is used by
00541 /// PolymorphicMatcherWithParam1 and should be StringRef.
00542 template <typename T, typename ArgT>
00543 class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface<T> {
00544   static_assert(std::is_same<T, CXXOperatorCallExpr>::value ||
00545                 std::is_base_of<FunctionDecl, T>::value,
00546                 "unsupported class for matcher");
00547   static_assert(std::is_same<ArgT, StringRef>::value,
00548                 "argument type must be StringRef");
00549 
00550 public:
00551   explicit HasOverloadedOperatorNameMatcher(const StringRef Name)
00552       : SingleNodeMatcherInterface<T>(), Name(Name) {}
00553 
00554   bool matchesNode(const T &Node) const override {
00555     return matchesSpecialized(Node);
00556   }
00557 
00558 private:
00559 
00560   /// \brief CXXOperatorCallExpr exist only for calls to overloaded operators
00561   /// so this function returns true if the call is to an operator of the given
00562   /// name.
00563   bool matchesSpecialized(const CXXOperatorCallExpr &Node) const {
00564     return getOperatorSpelling(Node.getOperator()) == Name;
00565   }
00566 
00567   /// \brief Returns true only if CXXMethodDecl represents an overloaded
00568   /// operator and has the given operator name.
00569   bool matchesSpecialized(const FunctionDecl &Node) const {
00570     return Node.isOverloadedOperator() &&
00571            getOperatorSpelling(Node.getOverloadedOperator()) == Name;
00572   }
00573 
00574   std::string Name;
00575 };
00576 
00577 /// \brief Matches named declarations with a specific name.
00578 ///
00579 /// See \c hasName() in ASTMatchers.h for details.
00580 class HasNameMatcher : public SingleNodeMatcherInterface<NamedDecl> {
00581  public:
00582   explicit HasNameMatcher(StringRef Name);
00583 
00584   bool matchesNode(const NamedDecl &Node) const override;
00585 
00586  private:
00587   /// \brief Unqualified match routine.
00588   ///
00589   /// It is much faster than the full match, but it only works for unqualified
00590   /// matches.
00591   bool matchesNodeUnqualified(const NamedDecl &Node) const;
00592 
00593   /// \brief Full match routine
00594   ///
00595   /// It generates the fully qualified name of the declaration (which is
00596   /// expensive) before trying to match.
00597   /// It is slower but simple and works on all cases.
00598   bool matchesNodeFull(const NamedDecl &Node) const;
00599 
00600   const bool UseUnqualifiedMatch;
00601   const std::string Name;
00602 };
00603 
00604 /// \brief Matches declarations for QualType and CallExpr.
00605 ///
00606 /// Type argument DeclMatcherT is required by PolymorphicMatcherWithParam1 but
00607 /// not actually used.
00608 template <typename T, typename DeclMatcherT>
00609 class HasDeclarationMatcher : public MatcherInterface<T> {
00610   static_assert(std::is_same<DeclMatcherT, Matcher<Decl>>::value,
00611                 "instantiated with wrong types");
00612 
00613 public:
00614   explicit HasDeclarationMatcher(const Matcher<Decl> &InnerMatcher)
00615       : InnerMatcher(InnerMatcher) {}
00616 
00617   bool matches(const T &Node, ASTMatchFinder *Finder,
00618                BoundNodesTreeBuilder *Builder) const override {
00619     return matchesSpecialized(Node, Finder, Builder);
00620   }
00621 
00622 private:
00623   /// \brief If getDecl exists as a member of U, returns whether the inner
00624   /// matcher matches Node.getDecl().
00625   template <typename U>
00626   bool matchesSpecialized(
00627       const U &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder,
00628       typename std::enable_if<has_getDecl<U>::value, int>::type = 0) const {
00629     return matchesDecl(Node.getDecl(), Finder, Builder);
00630   }
00631 
00632   /// \brief Extracts the CXXRecordDecl or EnumDecl of a QualType and returns
00633   /// whether the inner matcher matches on it.
00634   bool matchesSpecialized(const QualType &Node, ASTMatchFinder *Finder,
00635                           BoundNodesTreeBuilder *Builder) const {
00636     /// FIXME: Add other ways to convert...
00637     if (Node.isNull())
00638       return false;
00639     if (const EnumType *AsEnum = dyn_cast<EnumType>(Node.getTypePtr()))
00640       return matchesDecl(AsEnum->getDecl(), Finder, Builder);
00641     return matchesDecl(Node->getAsCXXRecordDecl(), Finder, Builder);
00642   }
00643 
00644   /// \brief Gets the TemplateDecl from a TemplateSpecializationType
00645   /// and returns whether the inner matches on it.
00646   bool matchesSpecialized(const TemplateSpecializationType &Node,
00647                           ASTMatchFinder *Finder,
00648                           BoundNodesTreeBuilder *Builder) const {
00649     return matchesDecl(Node.getTemplateName().getAsTemplateDecl(),
00650                        Finder, Builder);
00651   }
00652 
00653   /// \brief Extracts the Decl of the callee of a CallExpr and returns whether
00654   /// the inner matcher matches on it.
00655   bool matchesSpecialized(const CallExpr &Node, ASTMatchFinder *Finder,
00656                           BoundNodesTreeBuilder *Builder) const {
00657     return matchesDecl(Node.getCalleeDecl(), Finder, Builder);
00658   }
00659 
00660   /// \brief Extracts the Decl of the constructor call and returns whether the
00661   /// inner matcher matches on it.
00662   bool matchesSpecialized(const CXXConstructExpr &Node,
00663                           ASTMatchFinder *Finder,
00664                           BoundNodesTreeBuilder *Builder) const {
00665     return matchesDecl(Node.getConstructor(), Finder, Builder);
00666   }
00667 
00668   /// \brief Extracts the \c ValueDecl a \c MemberExpr refers to and returns
00669   /// whether the inner matcher matches on it.
00670   bool matchesSpecialized(const MemberExpr &Node,
00671                           ASTMatchFinder *Finder,
00672                           BoundNodesTreeBuilder *Builder) const {
00673     return matchesDecl(Node.getMemberDecl(), Finder, Builder);
00674   }
00675 
00676   /// \brief Returns whether the inner matcher \c Node. Returns false if \c Node
00677   /// is \c NULL.
00678   bool matchesDecl(const Decl *Node,
00679                    ASTMatchFinder *Finder,
00680                    BoundNodesTreeBuilder *Builder) const {
00681     return Node != nullptr && InnerMatcher.matches(*Node, Finder, Builder);
00682   }
00683 
00684   const Matcher<Decl> InnerMatcher;
00685 };
00686 
00687 /// \brief IsBaseType<T>::value is true if T is a "base" type in the AST
00688 /// node class hierarchies.
00689 template <typename T>
00690 struct IsBaseType {
00691   static const bool value =
00692       std::is_same<T, Decl>::value ||
00693       std::is_same<T, Stmt>::value ||
00694       std::is_same<T, QualType>::value ||
00695       std::is_same<T, Type>::value ||
00696       std::is_same<T, TypeLoc>::value ||
00697       std::is_same<T, NestedNameSpecifier>::value ||
00698       std::is_same<T, NestedNameSpecifierLoc>::value ||
00699       std::is_same<T, CXXCtorInitializer>::value;
00700 };
00701 template <typename T>
00702 const bool IsBaseType<T>::value;
00703 
00704 /// \brief Interface that allows matchers to traverse the AST.
00705 /// FIXME: Find a better name.
00706 ///
00707 /// This provides three entry methods for each base node type in the AST:
00708 /// - \c matchesChildOf:
00709 ///   Matches a matcher on every child node of the given node. Returns true
00710 ///   if at least one child node could be matched.
00711 /// - \c matchesDescendantOf:
00712 ///   Matches a matcher on all descendant nodes of the given node. Returns true
00713 ///   if at least one descendant matched.
00714 /// - \c matchesAncestorOf:
00715 ///   Matches a matcher on all ancestors of the given node. Returns true if
00716 ///   at least one ancestor matched.
00717 ///
00718 /// FIXME: Currently we only allow Stmt and Decl nodes to start a traversal.
00719 /// In the future, we wan to implement this for all nodes for which it makes
00720 /// sense. In the case of matchesAncestorOf, we'll want to implement it for
00721 /// all nodes, as all nodes have ancestors.
00722 class ASTMatchFinder {
00723 public:
00724   /// \brief Defines how we descend a level in the AST when we pass
00725   /// through expressions.
00726   enum TraversalKind {
00727     /// Will traverse any child nodes.
00728     TK_AsIs,
00729     /// Will not traverse implicit casts and parentheses.
00730     TK_IgnoreImplicitCastsAndParentheses
00731   };
00732 
00733   /// \brief Defines how bindings are processed on recursive matches.
00734   enum BindKind {
00735     /// Stop at the first match and only bind the first match.
00736     BK_First,
00737     /// Create results for all combinations of bindings that match.
00738     BK_All
00739   };
00740 
00741   /// \brief Defines which ancestors are considered for a match.
00742   enum AncestorMatchMode {
00743     /// All ancestors.
00744     AMM_All,
00745     /// Direct parent only.
00746     AMM_ParentOnly
00747   };
00748 
00749   virtual ~ASTMatchFinder() {}
00750 
00751   /// \brief Returns true if the given class is directly or indirectly derived
00752   /// from a base type matching \c base.
00753   ///
00754   /// A class is considered to be also derived from itself.
00755   virtual bool classIsDerivedFrom(const CXXRecordDecl *Declaration,
00756                                   const Matcher<NamedDecl> &Base,
00757                                   BoundNodesTreeBuilder *Builder) = 0;
00758 
00759   template <typename T>
00760   bool matchesChildOf(const T &Node,
00761                       const DynTypedMatcher &Matcher,
00762                       BoundNodesTreeBuilder *Builder,
00763                       TraversalKind Traverse,
00764                       BindKind Bind) {
00765     static_assert(std::is_base_of<Decl, T>::value ||
00766                   std::is_base_of<Stmt, T>::value ||
00767                   std::is_base_of<NestedNameSpecifier, T>::value ||
00768                   std::is_base_of<NestedNameSpecifierLoc, T>::value ||
00769                   std::is_base_of<TypeLoc, T>::value ||
00770                   std::is_base_of<QualType, T>::value,
00771                   "unsupported type for recursive matching");
00772    return matchesChildOf(ast_type_traits::DynTypedNode::create(Node),
00773                           Matcher, Builder, Traverse, Bind);
00774   }
00775 
00776   template <typename T>
00777   bool matchesDescendantOf(const T &Node,
00778                            const DynTypedMatcher &Matcher,
00779                            BoundNodesTreeBuilder *Builder,
00780                            BindKind Bind) {
00781     static_assert(std::is_base_of<Decl, T>::value ||
00782                   std::is_base_of<Stmt, T>::value ||
00783                   std::is_base_of<NestedNameSpecifier, T>::value ||
00784                   std::is_base_of<NestedNameSpecifierLoc, T>::value ||
00785                   std::is_base_of<TypeLoc, T>::value ||
00786                   std::is_base_of<QualType, T>::value,
00787                   "unsupported type for recursive matching");
00788     return matchesDescendantOf(ast_type_traits::DynTypedNode::create(Node),
00789                                Matcher, Builder, Bind);
00790   }
00791 
00792   // FIXME: Implement support for BindKind.
00793   template <typename T>
00794   bool matchesAncestorOf(const T &Node,
00795                          const DynTypedMatcher &Matcher,
00796                          BoundNodesTreeBuilder *Builder,
00797                          AncestorMatchMode MatchMode) {
00798     static_assert(std::is_base_of<Decl, T>::value ||
00799                   std::is_base_of<Stmt, T>::value,
00800                   "only Decl or Stmt allowed for recursive matching");
00801     return matchesAncestorOf(ast_type_traits::DynTypedNode::create(Node),
00802                              Matcher, Builder, MatchMode);
00803   }
00804 
00805   virtual ASTContext &getASTContext() const = 0;
00806 
00807 protected:
00808   virtual bool matchesChildOf(const ast_type_traits::DynTypedNode &Node,
00809                               const DynTypedMatcher &Matcher,
00810                               BoundNodesTreeBuilder *Builder,
00811                               TraversalKind Traverse,
00812                               BindKind Bind) = 0;
00813 
00814   virtual bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node,
00815                                    const DynTypedMatcher &Matcher,
00816                                    BoundNodesTreeBuilder *Builder,
00817                                    BindKind Bind) = 0;
00818 
00819   virtual bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node,
00820                                  const DynTypedMatcher &Matcher,
00821                                  BoundNodesTreeBuilder *Builder,
00822                                  AncestorMatchMode MatchMode) = 0;
00823 };
00824 
00825 /// \brief A type-list implementation.
00826 ///
00827 /// A list is declared as a tree of type list nodes, where the leafs are the
00828 /// types.
00829 /// However, it is used as a "linked list" of types, by using the ::head and
00830 /// ::tail typedefs.
00831 /// Each node supports up to 4 children (instead of just 2) to reduce the
00832 /// nesting required by large lists.
00833 template <typename T1 = void, typename T2 = void, typename T3 = void,
00834           typename T4 = void>
00835 struct TypeList {
00836   /// \brief Implementation detail. Combined with the specializations below,
00837   ///   this typedef allows for flattening of nested structures.
00838   typedef TypeList<T1, T2, T3, T4> self;
00839 
00840   /// \brief The first type on the list.
00841   typedef T1 head;
00842 
00843   /// \brief A sublist with the tail. ie everything but the head.
00844   ///
00845   /// This type is used to do recursion. TypeList<>/EmptyTypeList indicates the
00846   /// end of the list.
00847   typedef typename TypeList<T2, T3, T4>::self tail;
00848 };
00849 
00850 /// \brief Template specialization to allow nested lists.
00851 ///
00852 /// First element is a typelist. Pop its first element.
00853 template <typename Sub1, typename Sub2, typename Sub3, typename Sub4,
00854           typename T2, typename T3, typename T4>
00855 struct TypeList<TypeList<Sub1, Sub2, Sub3, Sub4>, T2, T3,
00856                 T4> : public TypeList<Sub1,
00857                                       typename TypeList<Sub2, Sub3, Sub4>::self,
00858                                       typename TypeList<T2, T3, T4>::self> {};
00859 
00860 /// \brief Template specialization to allow nested lists.
00861 ///
00862 /// First element is an empty typelist. Skip it.
00863 template <typename T2, typename T3, typename T4>
00864 struct TypeList<TypeList<>, T2, T3, T4> : public TypeList<T2, T3, T4> {
00865 };
00866 
00867 /// \brief The empty type list.
00868 typedef TypeList<> EmptyTypeList;
00869 
00870 /// \brief Helper meta-function to determine if some type \c T is present or
00871 ///   a parent type in the list.
00872 template <typename AnyTypeList, typename T>
00873 struct TypeListContainsSuperOf {
00874   static const bool value =
00875       std::is_base_of<typename AnyTypeList::head, T>::value ||
00876       TypeListContainsSuperOf<typename AnyTypeList::tail, T>::value;
00877 };
00878 template <typename T>
00879 struct TypeListContainsSuperOf<EmptyTypeList, T> {
00880   static const bool value = false;
00881 };
00882 
00883 /// \brief A "type list" that contains all types.
00884 ///
00885 /// Useful for matchers like \c anything and \c unless.
00886 typedef TypeList<
00887     TypeList<Decl, Stmt, NestedNameSpecifier, NestedNameSpecifierLoc>,
00888     TypeList<QualType, Type, TypeLoc, CXXCtorInitializer> > AllNodeBaseTypes;
00889 
00890 /// \brief Helper meta-function to extract the argument out of a function of
00891 ///   type void(Arg).
00892 ///
00893 /// See AST_POLYMORPHIC_SUPPORTED_TYPES_* for details.
00894 template <class T> struct ExtractFunctionArgMeta;
00895 template <class T> struct ExtractFunctionArgMeta<void(T)> {
00896   typedef T type;
00897 };
00898 
00899 /// \brief Default type lists for ArgumentAdaptingMatcher matchers.
00900 typedef AllNodeBaseTypes AdaptativeDefaultFromTypes;
00901 typedef TypeList<TypeList<Decl, Stmt, NestedNameSpecifier>,
00902                  TypeList<NestedNameSpecifierLoc, TypeLoc, QualType> >
00903 AdaptativeDefaultToTypes;
00904 
00905 /// \brief All types that are supported by HasDeclarationMatcher above.
00906 typedef TypeList<TypeList<CallExpr, CXXConstructExpr, DeclRefExpr, EnumType>,
00907                  TypeList<InjectedClassNameType, LabelStmt, MemberExpr>,
00908                  TypeList<QualType, RecordType, TagType>,
00909                  TypeList<TemplateSpecializationType, TemplateTypeParmType,
00910                           TypedefType, UnresolvedUsingType> >
00911 HasDeclarationSupportedTypes;
00912 
00913 /// \brief Converts a \c Matcher<T> to a matcher of desired type \c To by
00914 /// "adapting" a \c To into a \c T.
00915 ///
00916 /// The \c ArgumentAdapterT argument specifies how the adaptation is done.
00917 ///
00918 /// For example:
00919 ///   \c ArgumentAdaptingMatcher<HasMatcher, T>(InnerMatcher);
00920 /// Given that \c InnerMatcher is of type \c Matcher<T>, this returns a matcher
00921 /// that is convertible into any matcher of type \c To by constructing
00922 /// \c HasMatcher<To, T>(InnerMatcher).
00923 ///
00924 /// If a matcher does not need knowledge about the inner type, prefer to use
00925 /// PolymorphicMatcherWithParam1.
00926 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
00927           typename FromTypes = AdaptativeDefaultFromTypes,
00928           typename ToTypes = AdaptativeDefaultToTypes>
00929 struct ArgumentAdaptingMatcherFunc {
00930   template <typename T> class Adaptor {
00931   public:
00932     explicit Adaptor(const Matcher<T> &InnerMatcher)
00933         : InnerMatcher(InnerMatcher) {}
00934 
00935     typedef ToTypes ReturnTypes;
00936 
00937     template <typename To> operator Matcher<To>() const {
00938       return Matcher<To>(new ArgumentAdapterT<To, T>(InnerMatcher));
00939     }
00940 
00941   private:
00942     const Matcher<T> InnerMatcher;
00943   };
00944 
00945   template <typename T>
00946   static Adaptor<T> create(const Matcher<T> &InnerMatcher) {
00947     return Adaptor<T>(InnerMatcher);
00948   }
00949 
00950   template <typename T>
00951   Adaptor<T> operator()(const Matcher<T> &InnerMatcher) const {
00952     return create(InnerMatcher);
00953   }
00954 };
00955 
00956 /// \brief A PolymorphicMatcherWithParamN<MatcherT, P1, ..., PN> object can be
00957 /// created from N parameters p1, ..., pN (of type P1, ..., PN) and
00958 /// used as a Matcher<T> where a MatcherT<T, P1, ..., PN>(p1, ..., pN)
00959 /// can be constructed.
00960 ///
00961 /// For example:
00962 /// - PolymorphicMatcherWithParam0<IsDefinitionMatcher>()
00963 ///   creates an object that can be used as a Matcher<T> for any type T
00964 ///   where an IsDefinitionMatcher<T>() can be constructed.
00965 /// - PolymorphicMatcherWithParam1<ValueEqualsMatcher, int>(42)
00966 ///   creates an object that can be used as a Matcher<T> for any type T
00967 ///   where a ValueEqualsMatcher<T, int>(42) can be constructed.
00968 template <template <typename T> class MatcherT,
00969           typename ReturnTypesF = void(AllNodeBaseTypes)>
00970 class PolymorphicMatcherWithParam0 {
00971 public:
00972   typedef typename ExtractFunctionArgMeta<ReturnTypesF>::type ReturnTypes;
00973   template <typename T>
00974   operator Matcher<T>() const {
00975     static_assert(TypeListContainsSuperOf<ReturnTypes, T>::value,
00976                   "right polymorphic conversion");
00977     return Matcher<T>(new MatcherT<T>());
00978   }
00979 };
00980 
00981 template <template <typename T, typename P1> class MatcherT,
00982           typename P1,
00983           typename ReturnTypesF = void(AllNodeBaseTypes)>
00984 class PolymorphicMatcherWithParam1 {
00985 public:
00986   explicit PolymorphicMatcherWithParam1(const P1 &Param1)
00987       : Param1(Param1) {}
00988 
00989   typedef typename ExtractFunctionArgMeta<ReturnTypesF>::type ReturnTypes;
00990 
00991   template <typename T>
00992   operator Matcher<T>() const {
00993     static_assert(TypeListContainsSuperOf<ReturnTypes, T>::value,
00994                   "right polymorphic conversion");
00995     return Matcher<T>(new MatcherT<T, P1>(Param1));
00996   }
00997 
00998 private:
00999   const P1 Param1;
01000 };
01001 
01002 template <template <typename T, typename P1, typename P2> class MatcherT,
01003           typename P1, typename P2,
01004           typename ReturnTypesF = void(AllNodeBaseTypes)>
01005 class PolymorphicMatcherWithParam2 {
01006 public:
01007   PolymorphicMatcherWithParam2(const P1 &Param1, const P2 &Param2)
01008       : Param1(Param1), Param2(Param2) {}
01009 
01010   typedef typename ExtractFunctionArgMeta<ReturnTypesF>::type ReturnTypes;
01011 
01012   template <typename T>
01013   operator Matcher<T>() const {
01014     static_assert(TypeListContainsSuperOf<ReturnTypes, T>::value,
01015                   "right polymorphic conversion");
01016     return Matcher<T>(new MatcherT<T, P1, P2>(Param1, Param2));
01017   }
01018 
01019 private:
01020   const P1 Param1;
01021   const P2 Param2;
01022 };
01023 
01024 /// \brief Matches any instance of the given NodeType.
01025 ///
01026 /// This is useful when a matcher syntactically requires a child matcher,
01027 /// but the context doesn't care. See for example: anything().
01028 class TrueMatcher {
01029  public:
01030   typedef AllNodeBaseTypes ReturnTypes;
01031 
01032   template <typename T>
01033   operator Matcher<T>() const {
01034     return DynTypedMatcher::trueMatcher(
01035                ast_type_traits::ASTNodeKind::getFromNodeKind<T>())
01036         .template unconditionalConvertTo<T>();
01037   }
01038 };
01039 
01040 /// \brief A Matcher that allows binding the node it matches to an id.
01041 ///
01042 /// BindableMatcher provides a \a bind() method that allows binding the
01043 /// matched node to an id if the match was successful.
01044 template <typename T>
01045 class BindableMatcher : public Matcher<T> {
01046 public:
01047   explicit BindableMatcher(const Matcher<T> &M) : Matcher<T>(M) {}
01048   explicit BindableMatcher(MatcherInterface<T> *Implementation)
01049     : Matcher<T>(Implementation) {}
01050 
01051   /// \brief Returns a matcher that will bind the matched node on a match.
01052   ///
01053   /// The returned matcher is equivalent to this matcher, but will
01054   /// bind the matched node on a match.
01055   Matcher<T> bind(StringRef ID) const {
01056     return DynTypedMatcher(*this)
01057         .tryBind(ID)
01058         ->template unconditionalConvertTo<T>();
01059   }
01060 
01061   /// \brief Same as Matcher<T>'s conversion operator, but enables binding on
01062   /// the returned matcher.
01063   operator DynTypedMatcher() const {
01064     DynTypedMatcher Result = static_cast<const Matcher<T>&>(*this);
01065     Result.setAllowBind(true);
01066     return Result;
01067   }
01068 };
01069 
01070 /// \brief Matches nodes of type T that have child nodes of type ChildT for
01071 /// which a specified child matcher matches.
01072 ///
01073 /// ChildT must be an AST base type.
01074 template <typename T, typename ChildT>
01075 class HasMatcher : public MatcherInterface<T> {
01076   static_assert(IsBaseType<ChildT>::value,
01077                 "has only accepts base type matcher");
01078 
01079 public:
01080   explicit HasMatcher(const Matcher<ChildT> &ChildMatcher)
01081       : ChildMatcher(ChildMatcher) {}
01082 
01083   bool matches(const T &Node, ASTMatchFinder *Finder,
01084                BoundNodesTreeBuilder *Builder) const override {
01085     return Finder->matchesChildOf(
01086         Node, ChildMatcher, Builder,
01087         ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
01088         ASTMatchFinder::BK_First);
01089   }
01090 
01091  private:
01092   const Matcher<ChildT> ChildMatcher;
01093 };
01094 
01095 /// \brief Matches nodes of type T that have child nodes of type ChildT for
01096 /// which a specified child matcher matches. ChildT must be an AST base
01097 /// type.
01098 /// As opposed to the HasMatcher, the ForEachMatcher will produce a match
01099 /// for each child that matches.
01100 template <typename T, typename ChildT>
01101 class ForEachMatcher : public MatcherInterface<T> {
01102   static_assert(IsBaseType<ChildT>::value,
01103                 "for each only accepts base type matcher");
01104 
01105  public:
01106   explicit ForEachMatcher(const Matcher<ChildT> &ChildMatcher)
01107       : ChildMatcher(ChildMatcher) {}
01108 
01109   bool matches(const T& Node, ASTMatchFinder* Finder,
01110                BoundNodesTreeBuilder* Builder) const override {
01111     return Finder->matchesChildOf(
01112       Node, ChildMatcher, Builder,
01113       ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
01114       ASTMatchFinder::BK_All);
01115   }
01116 
01117 private:
01118   const Matcher<ChildT> ChildMatcher;
01119 };
01120 
01121 /// \brief VariadicOperatorMatcher related types.
01122 /// @{
01123 
01124 /// \brief "No argument" placeholder to use as template paratemers.
01125 struct VariadicOperatorNoArg {};
01126 
01127 /// \brief Polymorphic matcher object that uses a \c
01128 /// DynTypedMatcher::VariadicOperatorFunction operator.
01129 ///
01130 /// Input matchers can have any type (including other polymorphic matcher
01131 /// types), and the actual Matcher<T> is generated on demand with an implicit
01132 /// coversion operator.
01133 template <typename P1, typename P2 = VariadicOperatorNoArg,
01134           typename P3 = VariadicOperatorNoArg,
01135           typename P4 = VariadicOperatorNoArg,
01136           typename P5 = VariadicOperatorNoArg,
01137           typename P6 = VariadicOperatorNoArg,
01138           typename P7 = VariadicOperatorNoArg,
01139           typename P8 = VariadicOperatorNoArg,
01140           typename P9 = VariadicOperatorNoArg>
01141 class VariadicOperatorMatcher {
01142 public:
01143   VariadicOperatorMatcher(DynTypedMatcher::VariadicOperatorFunction Func,
01144                           const P1 &Param1,
01145                           const P2 &Param2 = VariadicOperatorNoArg(),
01146                           const P3 &Param3 = VariadicOperatorNoArg(),
01147                           const P4 &Param4 = VariadicOperatorNoArg(),
01148                           const P5 &Param5 = VariadicOperatorNoArg(),
01149                           const P6 &Param6 = VariadicOperatorNoArg(),
01150                           const P7 &Param7 = VariadicOperatorNoArg(),
01151                           const P8 &Param8 = VariadicOperatorNoArg(),
01152                           const P9 &Param9 = VariadicOperatorNoArg())
01153       : Func(Func), Param1(Param1), Param2(Param2), Param3(Param3),
01154         Param4(Param4), Param5(Param5), Param6(Param6), Param7(Param7),
01155         Param8(Param8), Param9(Param9) {}
01156 
01157   template <typename T> operator Matcher<T>() const {
01158     std::vector<DynTypedMatcher> Matchers;
01159     addMatcher<T>(Param1, Matchers);
01160     addMatcher<T>(Param2, Matchers);
01161     addMatcher<T>(Param3, Matchers);
01162     addMatcher<T>(Param4, Matchers);
01163     addMatcher<T>(Param5, Matchers);
01164     addMatcher<T>(Param6, Matchers);
01165     addMatcher<T>(Param7, Matchers);
01166     addMatcher<T>(Param8, Matchers);
01167     addMatcher<T>(Param9, Matchers);
01168     return DynTypedMatcher::constructVariadic(Func, std::move(Matchers))
01169         .template unconditionalConvertTo<T>();
01170   }
01171 
01172 private:
01173   template <typename T>
01174   static void addMatcher(const Matcher<T> &M,
01175                          std::vector<DynTypedMatcher> &Matchers) {
01176     Matchers.push_back(M);
01177   }
01178 
01179   /// \brief Overload to ignore \c VariadicOperatorNoArg arguments.
01180   template <typename T>
01181   static void addMatcher(VariadicOperatorNoArg,
01182                          std::vector<DynTypedMatcher> &Matchers) {}
01183 
01184   const DynTypedMatcher::VariadicOperatorFunction Func;
01185   const P1 Param1;
01186   const P2 Param2;
01187   const P3 Param3;
01188   const P4 Param4;
01189   const P5 Param5;
01190   const P6 Param6;
01191   const P7 Param7;
01192   const P8 Param8;
01193   const P9 Param9;
01194 };
01195 
01196 /// \brief Overloaded function object to generate VariadicOperatorMatcher
01197 ///   objects from arbitrary matchers.
01198 ///
01199 /// It supports 1-9 argument overloaded operator(). More can be added if needed.
01200 template <unsigned MinCount, unsigned MaxCount>
01201 struct VariadicOperatorMatcherFunc {
01202   DynTypedMatcher::VariadicOperatorFunction Func;
01203 
01204   template <unsigned Count, typename T>
01205   struct EnableIfValidArity
01206       : public std::enable_if<MinCount <= Count && Count <= MaxCount, T> {};
01207 
01208   template <typename M1>
01209   typename EnableIfValidArity<1, VariadicOperatorMatcher<M1> >::type
01210   operator()(const M1 &P1) const {
01211     return VariadicOperatorMatcher<M1>(Func, P1);
01212   }
01213   template <typename M1, typename M2>
01214   typename EnableIfValidArity<2, VariadicOperatorMatcher<M1, M2> >::type
01215   operator()(const M1 &P1, const M2 &P2) const {
01216     return VariadicOperatorMatcher<M1, M2>(Func, P1, P2);
01217   }
01218   template <typename M1, typename M2, typename M3>
01219   typename EnableIfValidArity<3, VariadicOperatorMatcher<M1, M2, M3> >::type
01220   operator()(const M1 &P1, const M2 &P2, const M3 &P3) const {
01221     return VariadicOperatorMatcher<M1, M2, M3>(Func, P1, P2, P3);
01222   }
01223   template <typename M1, typename M2, typename M3, typename M4>
01224   typename EnableIfValidArity<4, VariadicOperatorMatcher<M1, M2, M3, M4> >::type
01225   operator()(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4) const {
01226     return VariadicOperatorMatcher<M1, M2, M3, M4>(Func, P1, P2, P3, P4);
01227   }
01228   template <typename M1, typename M2, typename M3, typename M4, typename M5>
01229   typename EnableIfValidArity<
01230       5, VariadicOperatorMatcher<M1, M2, M3, M4, M5> >::type
01231   operator()(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4,
01232              const M5 &P5) const {
01233     return VariadicOperatorMatcher<M1, M2, M3, M4, M5>(Func, P1, P2, P3, P4,
01234                                                        P5);
01235   }
01236   template <typename M1, typename M2, typename M3, typename M4, typename M5,
01237             typename M6>
01238   typename EnableIfValidArity<
01239       6, VariadicOperatorMatcher<M1, M2, M3, M4, M5, M6> >::type
01240   operator()(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4,
01241              const M5 &P5, const M6 &P6) const {
01242     return VariadicOperatorMatcher<M1, M2, M3, M4, M5, M6>(
01243         Func, P1, P2, P3, P4, P5, P6);
01244   }
01245   template <typename M1, typename M2, typename M3, typename M4, typename M5,
01246             typename M6, typename M7>
01247   typename EnableIfValidArity<
01248       7, VariadicOperatorMatcher<M1, M2, M3, M4, M5, M6, M7> >::type
01249   operator()(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4,
01250              const M5 &P5, const M6 &P6, const M7 &P7) const {
01251     return VariadicOperatorMatcher<M1, M2, M3, M4, M5, M6, M7>(
01252         Func, P1, P2, P3, P4, P5, P6, P7);
01253   }
01254   template <typename M1, typename M2, typename M3, typename M4, typename M5,
01255             typename M6, typename M7, typename M8>
01256   typename EnableIfValidArity<
01257       8, VariadicOperatorMatcher<M1, M2, M3, M4, M5, M6, M7, M8> >::type
01258   operator()(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4,
01259              const M5 &P5, const M6 &P6, const M7 &P7, const M8 &P8) const {
01260     return VariadicOperatorMatcher<M1, M2, M3, M4, M5, M6, M7, M8>(
01261         Func, P1, P2, P3, P4, P5, P6, P7, P8);
01262   }
01263   template <typename M1, typename M2, typename M3, typename M4, typename M5,
01264             typename M6, typename M7, typename M8, typename M9>
01265   typename EnableIfValidArity<
01266       9, VariadicOperatorMatcher<M1, M2, M3, M4, M5, M6, M7, M8, M9> >::type
01267   operator()(const M1 &P1, const M2 &P2, const M3 &P3, const M4 &P4,
01268              const M5 &P5, const M6 &P6, const M7 &P7, const M8 &P8,
01269              const M9 &P9) const {
01270     return VariadicOperatorMatcher<M1, M2, M3, M4, M5, M6, M7, M8, M9>(
01271         Func, P1, P2, P3, P4, P5, P6, P7, P8, P9);
01272   }
01273 };
01274 
01275 /// @}
01276 
01277 /// \brief Matches nodes that do not match the provided matcher.
01278 ///
01279 /// Uses the variadic matcher interface, but fails if InnerMatchers.size()!=1.
01280 bool NotUnaryOperator(const ast_type_traits::DynTypedNode DynNode,
01281                       ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder,
01282                       ArrayRef<DynTypedMatcher> InnerMatchers);
01283 
01284 /// \brief Matches nodes for which all provided matchers match.
01285 bool AllOfVariadicOperator(const ast_type_traits::DynTypedNode DynNode,
01286                            ASTMatchFinder *Finder,
01287                            BoundNodesTreeBuilder *Builder,
01288                            ArrayRef<DynTypedMatcher> InnerMatchers);
01289 
01290 /// \brief Matches nodes for which at least one of the provided matchers
01291 /// matches, but doesn't stop at the first match.
01292 bool EachOfVariadicOperator(const ast_type_traits::DynTypedNode DynNode,
01293                             ASTMatchFinder *Finder,
01294                             BoundNodesTreeBuilder *Builder,
01295                             ArrayRef<DynTypedMatcher> InnerMatchers);
01296 
01297 /// \brief Matches nodes for which at least one of the provided matchers
01298 /// matches.
01299 bool AnyOfVariadicOperator(const ast_type_traits::DynTypedNode DynNode,
01300                            ASTMatchFinder *Finder,
01301                            BoundNodesTreeBuilder *Builder,
01302                            ArrayRef<DynTypedMatcher> InnerMatchers);
01303 
01304 template <typename T>
01305 inline Matcher<T> DynTypedMatcher::unconditionalConvertTo() const {
01306   return Matcher<T>(*this);
01307 }
01308 
01309 /// \brief Creates a Matcher<T> that matches if all inner matchers match.
01310 template<typename T>
01311 BindableMatcher<T> makeAllOfComposite(
01312     ArrayRef<const Matcher<T> *> InnerMatchers) {
01313   // For the size() == 0 case, we return a "true" matcher.
01314   if (InnerMatchers.size() == 0) {
01315     return BindableMatcher<T>(TrueMatcher());
01316   }
01317   // For the size() == 1 case, we simply return that one matcher.
01318   // No need to wrap it in a variadic operation.
01319   if (InnerMatchers.size() == 1) {
01320     return BindableMatcher<T>(*InnerMatchers[0]);
01321   }
01322 
01323   std::vector<DynTypedMatcher> DynMatchers;
01324   DynMatchers.reserve(InnerMatchers.size());
01325   for (const auto *InnerMatcher : InnerMatchers) {
01326     DynMatchers.push_back(*InnerMatcher);
01327   }
01328   return BindableMatcher<T>(DynTypedMatcher::constructVariadic(
01329                                 AllOfVariadicOperator, std::move(DynMatchers))
01330                                 .template unconditionalConvertTo<T>());
01331 }
01332 
01333 /// \brief Creates a Matcher<T> that matches if
01334 /// T is dyn_cast'able into InnerT and all inner matchers match.
01335 ///
01336 /// Returns BindableMatcher, as matchers that use dyn_cast have
01337 /// the same object both to match on and to run submatchers on,
01338 /// so there is no ambiguity with what gets bound.
01339 template<typename T, typename InnerT>
01340 BindableMatcher<T> makeDynCastAllOfComposite(
01341     ArrayRef<const Matcher<InnerT> *> InnerMatchers) {
01342   return BindableMatcher<T>(
01343       makeAllOfComposite(InnerMatchers).template dynCastTo<T>());
01344 }
01345 
01346 /// \brief Matches nodes of type T that have at least one descendant node of
01347 /// type DescendantT for which the given inner matcher matches.
01348 ///
01349 /// DescendantT must be an AST base type.
01350 template <typename T, typename DescendantT>
01351 class HasDescendantMatcher : public MatcherInterface<T> {
01352   static_assert(IsBaseType<DescendantT>::value,
01353                 "has descendant only accepts base type matcher");
01354 
01355 public:
01356   explicit HasDescendantMatcher(const Matcher<DescendantT> &DescendantMatcher)
01357       : DescendantMatcher(DescendantMatcher) {}
01358 
01359   bool matches(const T &Node, ASTMatchFinder *Finder,
01360                BoundNodesTreeBuilder *Builder) const override {
01361     return Finder->matchesDescendantOf(
01362         Node, DescendantMatcher, Builder, ASTMatchFinder::BK_First);
01363   }
01364 
01365  private:
01366   const Matcher<DescendantT> DescendantMatcher;
01367 };
01368 
01369 /// \brief Matches nodes of type \c T that have a parent node of type \c ParentT
01370 /// for which the given inner matcher matches.
01371 ///
01372 /// \c ParentT must be an AST base type.
01373 template <typename T, typename ParentT>
01374 class HasParentMatcher : public MatcherInterface<T> {
01375   static_assert(IsBaseType<ParentT>::value,
01376                 "has parent only accepts base type matcher");
01377 
01378 public:
01379   explicit HasParentMatcher(const Matcher<ParentT> &ParentMatcher)
01380       : ParentMatcher(ParentMatcher) {}
01381 
01382   bool matches(const T &Node, ASTMatchFinder *Finder,
01383                BoundNodesTreeBuilder *Builder) const override {
01384     return Finder->matchesAncestorOf(
01385         Node, ParentMatcher, Builder, ASTMatchFinder::AMM_ParentOnly);
01386   }
01387 
01388  private:
01389   const Matcher<ParentT> ParentMatcher;
01390 };
01391 
01392 /// \brief Matches nodes of type \c T that have at least one ancestor node of
01393 /// type \c AncestorT for which the given inner matcher matches.
01394 ///
01395 /// \c AncestorT must be an AST base type.
01396 template <typename T, typename AncestorT>
01397 class HasAncestorMatcher : public MatcherInterface<T> {
01398   static_assert(IsBaseType<AncestorT>::value,
01399                 "has ancestor only accepts base type matcher");
01400 
01401 public:
01402   explicit HasAncestorMatcher(const Matcher<AncestorT> &AncestorMatcher)
01403       : AncestorMatcher(AncestorMatcher) {}
01404 
01405   bool matches(const T &Node, ASTMatchFinder *Finder,
01406                BoundNodesTreeBuilder *Builder) const override {
01407     return Finder->matchesAncestorOf(
01408         Node, AncestorMatcher, Builder, ASTMatchFinder::AMM_All);
01409   }
01410 
01411  private:
01412   const Matcher<AncestorT> AncestorMatcher;
01413 };
01414 
01415 /// \brief Matches nodes of type T that have at least one descendant node of
01416 /// type DescendantT for which the given inner matcher matches.
01417 ///
01418 /// DescendantT must be an AST base type.
01419 /// As opposed to HasDescendantMatcher, ForEachDescendantMatcher will match
01420 /// for each descendant node that matches instead of only for the first.
01421 template <typename T, typename DescendantT>
01422 class ForEachDescendantMatcher : public MatcherInterface<T> {
01423   static_assert(IsBaseType<DescendantT>::value,
01424                 "for each descendant only accepts base type matcher");
01425 
01426  public:
01427   explicit ForEachDescendantMatcher(
01428       const Matcher<DescendantT>& DescendantMatcher)
01429       : DescendantMatcher(DescendantMatcher) {}
01430 
01431   bool matches(const T& Node, ASTMatchFinder* Finder,
01432                BoundNodesTreeBuilder* Builder) const override {
01433     return Finder->matchesDescendantOf(Node, DescendantMatcher, Builder,
01434                                        ASTMatchFinder::BK_All);
01435   }
01436 
01437 private:
01438   const Matcher<DescendantT> DescendantMatcher;
01439 };
01440 
01441 /// \brief Matches on nodes that have a getValue() method if getValue() equals
01442 /// the value the ValueEqualsMatcher was constructed with.
01443 template <typename T, typename ValueT>
01444 class ValueEqualsMatcher : public SingleNodeMatcherInterface<T> {
01445   static_assert(std::is_base_of<CharacterLiteral, T>::value ||
01446                 std::is_base_of<CXXBoolLiteralExpr, T>::value ||
01447                 std::is_base_of<FloatingLiteral, T>::value ||
01448                 std::is_base_of<IntegerLiteral, T>::value,
01449                 "the node must have a getValue method");
01450 
01451 public:
01452   explicit ValueEqualsMatcher(const ValueT &ExpectedValue)
01453       : ExpectedValue(ExpectedValue) {}
01454 
01455   bool matchesNode(const T &Node) const override {
01456     return Node.getValue() == ExpectedValue;
01457   }
01458 
01459 private:
01460   const ValueT ExpectedValue;
01461 };
01462 
01463 /// \brief A VariadicDynCastAllOfMatcher<SourceT, TargetT> object is a
01464 /// variadic functor that takes a number of Matcher<TargetT> and returns a
01465 /// Matcher<SourceT> that matches TargetT nodes that are matched by all of the
01466 /// given matchers, if SourceT can be dynamically casted into TargetT.
01467 ///
01468 /// For example:
01469 ///   const VariadicDynCastAllOfMatcher<
01470 ///       Decl, CXXRecordDecl> record;
01471 /// Creates a functor record(...) that creates a Matcher<Decl> given
01472 /// a variable number of arguments of type Matcher<CXXRecordDecl>.
01473 /// The returned matcher matches if the given Decl can by dynamically
01474 /// casted to CXXRecordDecl and all given matchers match.
01475 template <typename SourceT, typename TargetT>
01476 class VariadicDynCastAllOfMatcher
01477     : public llvm::VariadicFunction<
01478         BindableMatcher<SourceT>, Matcher<TargetT>,
01479         makeDynCastAllOfComposite<SourceT, TargetT> > {
01480 public:
01481   VariadicDynCastAllOfMatcher() {}
01482 };
01483 
01484 /// \brief A \c VariadicAllOfMatcher<T> object is a variadic functor that takes
01485 /// a number of \c Matcher<T> and returns a \c Matcher<T> that matches \c T
01486 /// nodes that are matched by all of the given matchers.
01487 ///
01488 /// For example:
01489 ///   const VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier;
01490 /// Creates a functor nestedNameSpecifier(...) that creates a
01491 /// \c Matcher<NestedNameSpecifier> given a variable number of arguments of type
01492 /// \c Matcher<NestedNameSpecifier>.
01493 /// The returned matcher matches if all given matchers match.
01494 template <typename T>
01495 class VariadicAllOfMatcher : public llvm::VariadicFunction<
01496                                BindableMatcher<T>, Matcher<T>,
01497                                makeAllOfComposite<T> > {
01498 public:
01499   VariadicAllOfMatcher() {}
01500 };
01501 
01502 /// \brief Matches nodes of type \c TLoc for which the inner
01503 /// \c Matcher<T> matches.
01504 template <typename TLoc, typename T>
01505 class LocMatcher : public MatcherInterface<TLoc> {
01506 public:
01507   explicit LocMatcher(const Matcher<T> &InnerMatcher)
01508     : InnerMatcher(InnerMatcher) {}
01509 
01510   bool matches(const TLoc &Node, ASTMatchFinder *Finder,
01511                BoundNodesTreeBuilder *Builder) const override {
01512     if (!Node)
01513       return false;
01514     return InnerMatcher.matches(*extract(Node), Finder, Builder);
01515   }
01516 
01517 private:
01518   const NestedNameSpecifier *extract(const NestedNameSpecifierLoc &Loc) const {
01519     return Loc.getNestedNameSpecifier();
01520   }
01521 
01522   const Matcher<T> InnerMatcher;
01523 };
01524 
01525 /// \brief Matches \c TypeLocs based on an inner matcher matching a certain
01526 /// \c QualType.
01527 ///
01528 /// Used to implement the \c loc() matcher.
01529 class TypeLocTypeMatcher : public MatcherInterface<TypeLoc> {
01530 public:
01531   explicit TypeLocTypeMatcher(const Matcher<QualType> &InnerMatcher)
01532       : InnerMatcher(InnerMatcher) {}
01533 
01534   bool matches(const TypeLoc &Node, ASTMatchFinder *Finder,
01535                BoundNodesTreeBuilder *Builder) const override {
01536     if (!Node)
01537       return false;
01538     return InnerMatcher.matches(Node.getType(), Finder, Builder);
01539   }
01540 
01541 private:
01542   const Matcher<QualType> InnerMatcher;
01543 };
01544 
01545 /// \brief Matches nodes of type \c T for which the inner matcher matches on a
01546 /// another node of type \c T that can be reached using a given traverse
01547 /// function.
01548 template <typename T>
01549 class TypeTraverseMatcher : public MatcherInterface<T> {
01550 public:
01551   explicit TypeTraverseMatcher(const Matcher<QualType> &InnerMatcher,
01552                                QualType (T::*TraverseFunction)() const)
01553       : InnerMatcher(InnerMatcher), TraverseFunction(TraverseFunction) {}
01554 
01555   bool matches(const T &Node, ASTMatchFinder *Finder,
01556                BoundNodesTreeBuilder *Builder) const override {
01557     QualType NextNode = (Node.*TraverseFunction)();
01558     if (NextNode.isNull())
01559       return false;
01560     return InnerMatcher.matches(NextNode, Finder, Builder);
01561   }
01562 
01563 private:
01564   const Matcher<QualType> InnerMatcher;
01565   QualType (T::*TraverseFunction)() const;
01566 };
01567 
01568 /// \brief Matches nodes of type \c T in a ..Loc hierarchy, for which the inner
01569 /// matcher matches on a another node of type \c T that can be reached using a
01570 /// given traverse function.
01571 template <typename T>
01572 class TypeLocTraverseMatcher : public MatcherInterface<T> {
01573 public:
01574   explicit TypeLocTraverseMatcher(const Matcher<TypeLoc> &InnerMatcher,
01575                                   TypeLoc (T::*TraverseFunction)() const)
01576       : InnerMatcher(InnerMatcher), TraverseFunction(TraverseFunction) {}
01577 
01578   bool matches(const T &Node, ASTMatchFinder *Finder,
01579                BoundNodesTreeBuilder *Builder) const override {
01580     TypeLoc NextNode = (Node.*TraverseFunction)();
01581     if (!NextNode)
01582       return false;
01583     return InnerMatcher.matches(NextNode, Finder, Builder);
01584   }
01585 
01586 private:
01587   const Matcher<TypeLoc> InnerMatcher;
01588   TypeLoc (T::*TraverseFunction)() const;
01589 };
01590 
01591 /// \brief Converts a \c Matcher<InnerT> to a \c Matcher<OuterT>, where
01592 /// \c OuterT is any type that is supported by \c Getter.
01593 ///
01594 /// \code Getter<OuterT>::value() \endcode returns a
01595 /// \code InnerTBase (OuterT::*)() \endcode, which is used to adapt a \c OuterT
01596 /// object into a \c InnerT
01597 template <typename InnerTBase,
01598           template <typename OuterT> class Getter,
01599           template <typename OuterT> class MatcherImpl,
01600           typename ReturnTypesF>
01601 class TypeTraversePolymorphicMatcher {
01602 private:
01603   typedef TypeTraversePolymorphicMatcher<InnerTBase, Getter, MatcherImpl,
01604                                          ReturnTypesF> Self;
01605   static Self create(ArrayRef<const Matcher<InnerTBase> *> InnerMatchers);
01606 
01607 public:
01608   typedef typename ExtractFunctionArgMeta<ReturnTypesF>::type ReturnTypes;
01609 
01610   explicit TypeTraversePolymorphicMatcher(
01611       ArrayRef<const Matcher<InnerTBase> *> InnerMatchers)
01612       : InnerMatcher(makeAllOfComposite(InnerMatchers)) {}
01613 
01614   template <typename OuterT> operator Matcher<OuterT>() const {
01615     return Matcher<OuterT>(
01616         new MatcherImpl<OuterT>(InnerMatcher, Getter<OuterT>::value()));
01617   }
01618 
01619   struct Func : public llvm::VariadicFunction<Self, Matcher<InnerTBase>,
01620                                               &Self::create> {
01621     Func() {}
01622   };
01623 
01624 private:
01625   const Matcher<InnerTBase> InnerMatcher;
01626 };
01627 
01628 // Define the create() method out of line to silence a GCC warning about
01629 // the struct "Func" having greater visibility than its base, which comes from
01630 // using the flag -fvisibility-inlines-hidden.
01631 template <typename InnerTBase, template <typename OuterT> class Getter,
01632           template <typename OuterT> class MatcherImpl, typename ReturnTypesF>
01633 TypeTraversePolymorphicMatcher<InnerTBase, Getter, MatcherImpl, ReturnTypesF>
01634 TypeTraversePolymorphicMatcher<
01635     InnerTBase, Getter, MatcherImpl,
01636     ReturnTypesF>::create(ArrayRef<const Matcher<InnerTBase> *> InnerMatchers) {
01637   return Self(InnerMatchers);
01638 }
01639 
01640 // FIXME: unify ClassTemplateSpecializationDecl and TemplateSpecializationType's
01641 // APIs for accessing the template argument list.
01642 inline ArrayRef<TemplateArgument>
01643 getTemplateSpecializationArgs(const ClassTemplateSpecializationDecl &D) {
01644   return D.getTemplateArgs().asArray();
01645 }
01646 
01647 inline ArrayRef<TemplateArgument>
01648 getTemplateSpecializationArgs(const TemplateSpecializationType &T) {
01649   return llvm::makeArrayRef(T.getArgs(), T.getNumArgs());
01650 }
01651 
01652 struct NotEqualsBoundNodePredicate {
01653   bool operator()(const internal::BoundNodesMap &Nodes) const {
01654     return Nodes.getNode(ID) != Node;
01655   }
01656   std::string ID;
01657   ast_type_traits::DynTypedNode Node;
01658 };
01659 
01660 } // end namespace internal
01661 } // end namespace ast_matchers
01662 } // end namespace clang
01663 
01664 #endif