Manuel Klimek | 4da2166 | 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 | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 21 | void BoundNodesMap::copyTo(BoundNodesTreeBuilder *Builder) const { |
| 22 | for (IDToNodeMap::const_iterator It = NodeMap.begin(); |
| 23 | It != NodeMap.end(); |
| 24 | ++It) { |
Manuel Klimek | 66341c5 | 2012-08-30 19:41:06 +0000 | [diff] [blame] | 25 | Builder->setBinding(It->first, It->second); |
Manuel Klimek | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | void BoundNodesMap::copyTo(BoundNodesMap *Other) const { |
| 30 | copy(NodeMap.begin(), NodeMap.end(), |
| 31 | inserter(Other->NodeMap, Other->NodeMap.begin())); |
| 32 | } |
| 33 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 34 | BoundNodesTree::BoundNodesTree() {} |
| 35 | |
| 36 | BoundNodesTree::BoundNodesTree( |
Manuel Klimek | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 37 | const BoundNodesMap& Bindings, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 38 | const std::vector<BoundNodesTree> RecursiveBindings) |
Manuel Klimek | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 39 | : Bindings(Bindings), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 40 | RecursiveBindings(RecursiveBindings) {} |
| 41 | |
| 42 | void BoundNodesTree::copyTo(BoundNodesTreeBuilder* Builder) const { |
Manuel Klimek | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 43 | Bindings.copyTo(Builder); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 44 | for (std::vector<BoundNodesTree>::const_iterator |
| 45 | I = RecursiveBindings.begin(), |
| 46 | E = RecursiveBindings.end(); |
| 47 | I != E; ++I) { |
| 48 | Builder->addMatch(*I); |
| 49 | } |
| 50 | } |
| 51 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 52 | void BoundNodesTree::visitMatches(Visitor* ResultVisitor) { |
Manuel Klimek | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 53 | BoundNodesMap AggregatedBindings; |
| 54 | visitMatchesRecursively(ResultVisitor, &AggregatedBindings); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void BoundNodesTree:: |
| 58 | visitMatchesRecursively(Visitor* ResultVisitor, |
Manuel Klimek | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 59 | BoundNodesMap* AggregatedBindings) { |
| 60 | Bindings.copyTo(AggregatedBindings); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 61 | if (RecursiveBindings.empty()) { |
Manuel Klimek | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 62 | ResultVisitor->visitMatch(BoundNodes(*AggregatedBindings)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 63 | } else { |
| 64 | for (unsigned I = 0; I < RecursiveBindings.size(); ++I) { |
| 65 | RecursiveBindings[I].visitMatchesRecursively(ResultVisitor, |
Manuel Klimek | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 66 | AggregatedBindings); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | BoundNodesTreeBuilder::BoundNodesTreeBuilder() {} |
| 72 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 73 | void BoundNodesTreeBuilder::addMatch(const BoundNodesTree& Bindings) { |
| 74 | RecursiveBindings.push_back(Bindings); |
| 75 | } |
| 76 | |
| 77 | BoundNodesTree BoundNodesTreeBuilder::build() const { |
Manuel Klimek | ec2a396 | 2012-08-28 23:26:39 +0000 | [diff] [blame] | 78 | return BoundNodesTree(Bindings, RecursiveBindings); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } // end namespace internal |
| 82 | } // end namespace ast_matchers |
| 83 | } // end namespace clang |