blob: 3e0114a0cbf3aa41fa8dd7cec78401df496b203d [file] [log] [blame]
Argyrios Kyrtzidis2c2ba3e2009-07-05 22:22:06 +00001//===--- DeclReferenceMap.h - Map Decls to their references -----*- C++ -*-===//
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// DeclReferenceMap creates a mapping from Decls to the ASTNodes that
11// references them.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/DeclReferenceMap.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/Stmt.h"
18#include "clang/AST/ASTNode.h"
19#include "clang/AST/DeclVisitor.h"
20#include "clang/AST/StmtVisitor.h"
21#include "llvm/Support/Compiler.h"
22using namespace clang;
23
24namespace {
25
26class VISIBILITY_HIDDEN StmtMapper : public StmtVisitor<StmtMapper> {
27 DeclReferenceMap::MapTy &Map;
28 Decl *Parent;
29
30public:
31 StmtMapper(DeclReferenceMap::MapTy &map, Decl *parent)
32 : Map(map), Parent(parent) { }
33
34 void VisitDeclStmt(DeclStmt *Node);
35 void VisitDeclRefExpr(DeclRefExpr *Node);
36 void VisitStmt(Stmt *Node);
37};
38
39class VISIBILITY_HIDDEN DeclMapper : public DeclVisitor<DeclMapper> {
40 DeclReferenceMap::MapTy &Map;
41
42public:
43 DeclMapper(DeclReferenceMap::MapTy &map)
44 : Map(map) { }
45
46 void VisitDeclContext(DeclContext *DC);
47 void VisitVarDecl(VarDecl *D);
48 void VisitFunctionDecl(FunctionDecl *D);
49 void VisitBlockDecl(BlockDecl *D);
50 void VisitDecl(Decl *D);
51};
52
53} // anonymous namespace
54
55//===----------------------------------------------------------------------===//
56// StmtMapper Implementation
57//===----------------------------------------------------------------------===//
58
59void StmtMapper::VisitDeclStmt(DeclStmt *Node) {
60 DeclMapper Mapper(Map);
61 for (DeclStmt::decl_iterator
62 I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I)
63 Mapper.Visit(*I);
64}
65
66void StmtMapper::VisitDeclRefExpr(DeclRefExpr *Node) {
67 NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getPrimaryDecl());
68 Map.insert(std::make_pair(PrimD, ASTNode(Parent, Node)));
69}
70
71void StmtMapper::VisitStmt(Stmt *Node) {
72 for (Stmt::child_iterator
73 I = Node->child_begin(), E = Node->child_end(); I != E; ++I)
74 Visit(*I);
75}
76
77//===----------------------------------------------------------------------===//
78// DeclMapper Implementation
79//===----------------------------------------------------------------------===//
80
81void DeclMapper::VisitDeclContext(DeclContext *DC) {
82 for (DeclContext::decl_iterator
83 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
84 Visit(*I);
85}
86
87void DeclMapper::VisitFunctionDecl(FunctionDecl *D) {
88 if (!D->isThisDeclarationADefinition())
89 return;
90
91 StmtMapper(Map, D).Visit(D->getBody());
92}
93
94void DeclMapper::VisitBlockDecl(BlockDecl *D) {
95 StmtMapper(Map, D).Visit(D->getBody());
96}
97
98void DeclMapper::VisitVarDecl(VarDecl *D) {
99 if (Expr *Init = D->getInit())
100 StmtMapper(Map, D).Visit(Init);
101}
102
103void DeclMapper::VisitDecl(Decl *D) {
104 if (DeclContext *DC = dyn_cast<DeclContext>(D))
105 VisitDeclContext(DC);
106}
107
108//===----------------------------------------------------------------------===//
109// DeclReferenceMap Implementation
110//===----------------------------------------------------------------------===//
111
112DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) {
113 DeclMapper(Map).Visit(Ctx.getTranslationUnitDecl());
114}
115
116DeclReferenceMap::astnode_iterator
117DeclReferenceMap::refs_begin(NamedDecl *D) const {
118 NamedDecl *Prim = cast<NamedDecl>(D->getPrimaryDecl());
119 return astnode_iterator(Map.lower_bound(Prim));
120}
121
122DeclReferenceMap::astnode_iterator
123DeclReferenceMap::refs_end(NamedDecl *D) const {
124 NamedDecl *Prim = cast<NamedDecl>(D->getPrimaryDecl());
125 return astnode_iterator(Map.upper_bound(Prim));
126}
127
128bool DeclReferenceMap::refs_empty(NamedDecl *D) const {
129 NamedDecl *Prim = cast<NamedDecl>(D->getPrimaryDecl());
130 return refs_begin(Prim) == refs_end(Prim);
131}