blob: e7ee8ee7ed378962082d47468f57bfd0e8840836 [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===--- 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
17namespace clang {
18namespace ast_matchers {
19namespace internal {
20
Manuel Klimekec2a3962012-08-28 23:26:39 +000021void BoundNodesMap::copyTo(BoundNodesTreeBuilder *Builder) const {
22 for (IDToNodeMap::const_iterator It = NodeMap.begin();
23 It != NodeMap.end();
24 ++It) {
Manuel Klimek66341c52012-08-30 19:41:06 +000025 Builder->setBinding(It->first, It->second);
Manuel Klimekec2a3962012-08-28 23:26:39 +000026 }
27}
28
29void BoundNodesMap::copyTo(BoundNodesMap *Other) const {
30 copy(NodeMap.begin(), NodeMap.end(),
31 inserter(Other->NodeMap, Other->NodeMap.begin()));
32}
33
Manuel Klimek4da21662012-07-06 05:48:52 +000034BoundNodesTree::BoundNodesTree() {}
35
36BoundNodesTree::BoundNodesTree(
Manuel Klimekec2a3962012-08-28 23:26:39 +000037 const BoundNodesMap& Bindings,
Manuel Klimek4da21662012-07-06 05:48:52 +000038 const std::vector<BoundNodesTree> RecursiveBindings)
Manuel Klimekec2a3962012-08-28 23:26:39 +000039 : Bindings(Bindings),
Manuel Klimek4da21662012-07-06 05:48:52 +000040 RecursiveBindings(RecursiveBindings) {}
41
42void BoundNodesTree::copyTo(BoundNodesTreeBuilder* Builder) const {
Manuel Klimekec2a3962012-08-28 23:26:39 +000043 Bindings.copyTo(Builder);
Manuel Klimek4da21662012-07-06 05:48:52 +000044 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 Klimek4da21662012-07-06 05:48:52 +000052void BoundNodesTree::visitMatches(Visitor* ResultVisitor) {
Manuel Klimekec2a3962012-08-28 23:26:39 +000053 BoundNodesMap AggregatedBindings;
54 visitMatchesRecursively(ResultVisitor, &AggregatedBindings);
Manuel Klimek4da21662012-07-06 05:48:52 +000055}
56
57void BoundNodesTree::
58visitMatchesRecursively(Visitor* ResultVisitor,
Manuel Klimekec2a3962012-08-28 23:26:39 +000059 BoundNodesMap* AggregatedBindings) {
60 Bindings.copyTo(AggregatedBindings);
Manuel Klimek4da21662012-07-06 05:48:52 +000061 if (RecursiveBindings.empty()) {
Manuel Klimekec2a3962012-08-28 23:26:39 +000062 ResultVisitor->visitMatch(BoundNodes(*AggregatedBindings));
Manuel Klimek4da21662012-07-06 05:48:52 +000063 } else {
64 for (unsigned I = 0; I < RecursiveBindings.size(); ++I) {
65 RecursiveBindings[I].visitMatchesRecursively(ResultVisitor,
Manuel Klimekec2a3962012-08-28 23:26:39 +000066 AggregatedBindings);
Manuel Klimek4da21662012-07-06 05:48:52 +000067 }
68 }
69}
70
71BoundNodesTreeBuilder::BoundNodesTreeBuilder() {}
72
Manuel Klimek4da21662012-07-06 05:48:52 +000073void BoundNodesTreeBuilder::addMatch(const BoundNodesTree& Bindings) {
74 RecursiveBindings.push_back(Bindings);
75}
76
77BoundNodesTree BoundNodesTreeBuilder::build() const {
Manuel Klimekec2a3962012-08-28 23:26:39 +000078 return BoundNodesTree(Bindings, RecursiveBindings);
Manuel Klimek4da21662012-07-06 05:48:52 +000079}
80
81} // end namespace internal
82} // end namespace ast_matchers
83} // end namespace clang