Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1 | //===--- ASTMatchersInternal.cpp - Structural query framework -------------===// |
| 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 | // Implements the base layer of the matcher framework. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 15 | #include "clang/ASTMatchers/ASTMatchersInternal.h" |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace ast_matchers { |
| 19 | namespace internal { |
| 20 | |
Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 21 | void BoundNodesTreeBuilder::visitMatches(Visitor *ResultVisitor) { |
| 22 | if (Bindings.empty()) |
| 23 | Bindings.push_back(BoundNodesMap()); |
| 24 | for (unsigned i = 0, e = Bindings.size(); i != e; ++i) { |
| 25 | ResultVisitor->visitMatch(BoundNodes(Bindings[i])); |
Manuel Klimek | 021d56f | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
Manuel Klimek | a0c025f | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 29 | void BoundNodesTreeBuilder::addMatch(const BoundNodesTreeBuilder &Other) { |
| 30 | for (unsigned i = 0, e = Other.Bindings.size(); i != e; ++i) { |
| 31 | Bindings.push_back(Other.Bindings[i]); |
Daniel Jasper | 94a5685 | 2012-11-16 18:39:22 +0000 | [diff] [blame] | 32 | } |
Manuel Klimek | 021d56f | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Samuel Benzaquen | 31edb51 | 2013-06-03 19:31:08 +0000 | [diff] [blame] | 35 | DynTypedMatcher::~DynTypedMatcher() {} |
| 36 | |
| 37 | DynTypedMatcher *DynTypedMatcher::tryBind(StringRef ID) const { return NULL; } |
| 38 | |
Samuel Benzaquen | 85ec25d | 2013-08-27 15:11:16 +0000 | [diff] [blame^] | 39 | bool AllOfVariadicOperator(const ast_type_traits::DynTypedNode DynNode, |
| 40 | ASTMatchFinder *Finder, |
| 41 | BoundNodesTreeBuilder *Builder, |
| 42 | ArrayRef<const DynTypedMatcher *> InnerMatchers) { |
| 43 | // allOf leads to one matcher for each alternative in the first |
| 44 | // matcher combined with each alternative in the second matcher. |
| 45 | // Thus, we can reuse the same Builder. |
| 46 | for (size_t i = 0, e = InnerMatchers.size(); i != e; ++i) { |
| 47 | if (!InnerMatchers[i]->matches(DynNode, Finder, Builder)) |
| 48 | return false; |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool EachOfVariadicOperator(const ast_type_traits::DynTypedNode DynNode, |
| 54 | ASTMatchFinder *Finder, |
| 55 | BoundNodesTreeBuilder *Builder, |
| 56 | ArrayRef<const DynTypedMatcher *> InnerMatchers) { |
| 57 | BoundNodesTreeBuilder Result; |
| 58 | bool Matched = false; |
| 59 | for (size_t i = 0, e = InnerMatchers.size(); i != e; ++i) { |
| 60 | BoundNodesTreeBuilder BuilderInner(*Builder); |
| 61 | if (InnerMatchers[i]->matches(DynNode, Finder, &BuilderInner)) { |
| 62 | Matched = true; |
| 63 | Result.addMatch(BuilderInner); |
| 64 | } |
| 65 | } |
| 66 | *Builder = Result; |
| 67 | return Matched; |
| 68 | } |
| 69 | |
| 70 | bool AnyOfVariadicOperator(const ast_type_traits::DynTypedNode DynNode, |
| 71 | ASTMatchFinder *Finder, |
| 72 | BoundNodesTreeBuilder *Builder, |
| 73 | ArrayRef<const DynTypedMatcher *> InnerMatchers) { |
| 74 | for (size_t i = 0, e = InnerMatchers.size(); i != e; ++i) { |
| 75 | BoundNodesTreeBuilder Result = *Builder; |
| 76 | if (InnerMatchers[i]->matches(DynNode, Finder, &Result)) { |
| 77 | *Builder = Result; |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
Manuel Klimek | 04616e4 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 84 | } // end namespace internal |
| 85 | } // end namespace ast_matchers |
| 86 | } // end namespace clang |