]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / ASTMatchers / ASTMatchersInternal.cpp
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
21 void BoundNodesMap::copyTo(BoundNodesTreeBuilder *Builder) const {
22   for (IDToNodeMap::const_iterator It = NodeMap.begin();
23        It != NodeMap.end();
24        ++It) {
25     Builder->setBinding(It->first, It->second);
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
34 BoundNodesTree::BoundNodesTree() {}
35
36 BoundNodesTree::BoundNodesTree(
37   const BoundNodesMap& Bindings,
38   const std::vector<BoundNodesTree> RecursiveBindings)
39   : Bindings(Bindings),
40     RecursiveBindings(RecursiveBindings) {}
41
42 void BoundNodesTree::copyTo(BoundNodesTreeBuilder* Builder) const {
43   Bindings.copyTo(Builder);
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
52 void BoundNodesTree::visitMatches(Visitor* ResultVisitor) {
53   BoundNodesMap AggregatedBindings;
54   visitMatchesRecursively(ResultVisitor, AggregatedBindings);
55 }
56
57 void BoundNodesTree::
58 visitMatchesRecursively(Visitor* ResultVisitor,
59                         const BoundNodesMap& AggregatedBindings) {
60   BoundNodesMap CombinedBindings(AggregatedBindings);
61   Bindings.copyTo(&CombinedBindings);
62   if (RecursiveBindings.empty()) {
63     ResultVisitor->visitMatch(BoundNodes(CombinedBindings));
64   } else {
65     for (unsigned I = 0; I < RecursiveBindings.size(); ++I) {
66       RecursiveBindings[I].visitMatchesRecursively(ResultVisitor,
67                                                    CombinedBindings);
68     }
69   }
70 }
71
72 BoundNodesTreeBuilder::BoundNodesTreeBuilder() {}
73
74 void BoundNodesTreeBuilder::addMatch(const BoundNodesTree& Bindings) {
75   RecursiveBindings.push_back(Bindings);
76 }
77
78 BoundNodesTree BoundNodesTreeBuilder::build() const {
79   return BoundNodesTree(Bindings, RecursiveBindings);
80 }
81
82 } // end namespace internal
83 } // end namespace ast_matchers
84 } // end namespace clang