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