diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2009-07-05 22:22:06 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2009-07-05 22:22:06 +0000 |
commit | e568a9792f1179090196e9342466a636dd715609 (patch) | |
tree | 0946a5ee35af4187612ec7f7eefab7a89f88f79a /clang/lib | |
parent | 02dd4f9389836a2f554c6c92bc62b0122808bd6f (diff) | |
download | bcm5719-llvm-e568a9792f1179090196e9342466a636dd715609.tar.gz bcm5719-llvm-e568a9792f1179090196e9342466a636dd715609.zip |
Introduce the DeclReferenceMap class inside the AST library.
DeclReferenceMap (similar to ParentMap) is a helper class for mapping Decls to the AST nodes that reference them.
A client will initialize it by passing an ASTContext to its constructor and later use it to iterate over
the references of a Decl.
References are mapped and retrieved using the primary declaration (Decl::getPrimaryDecl()) of a particular Decl.
llvm-svn: 74801
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/AST/CMakeLists.txt | 1 | ||||
-rw-r--r-- | clang/lib/AST/DeclReferenceMap.cpp | 131 |
2 files changed, 132 insertions, 0 deletions
diff --git a/clang/lib/AST/CMakeLists.txt b/clang/lib/AST/CMakeLists.txt index 8fb86c6fc20..899bc8f78eb 100644 --- a/clang/lib/AST/CMakeLists.txt +++ b/clang/lib/AST/CMakeLists.txt @@ -13,6 +13,7 @@ add_clang_library(clangAST DeclGroup.cpp DeclObjC.cpp DeclPrinter.cpp + DeclReferenceMap.cpp DeclTemplate.cpp ExprConstant.cpp Expr.cpp diff --git a/clang/lib/AST/DeclReferenceMap.cpp b/clang/lib/AST/DeclReferenceMap.cpp new file mode 100644 index 00000000000..3e0114a0cbf --- /dev/null +++ b/clang/lib/AST/DeclReferenceMap.cpp @@ -0,0 +1,131 @@ +//===--- DeclReferenceMap.h - Map Decls to their references -----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// DeclReferenceMap creates a mapping from Decls to the ASTNodes that +// references them. +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/DeclReferenceMap.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/ASTNode.h" +#include "clang/AST/DeclVisitor.h" +#include "clang/AST/StmtVisitor.h" +#include "llvm/Support/Compiler.h" +using namespace clang; + +namespace { + +class VISIBILITY_HIDDEN StmtMapper : public StmtVisitor<StmtMapper> { + DeclReferenceMap::MapTy ⤅ + Decl *Parent; + +public: + StmtMapper(DeclReferenceMap::MapTy &map, Decl *parent) + : Map(map), Parent(parent) { } + + void VisitDeclStmt(DeclStmt *Node); + void VisitDeclRefExpr(DeclRefExpr *Node); + void VisitStmt(Stmt *Node); +}; + +class VISIBILITY_HIDDEN DeclMapper : public DeclVisitor<DeclMapper> { + DeclReferenceMap::MapTy ⤅ + +public: + DeclMapper(DeclReferenceMap::MapTy &map) + : Map(map) { } + + void VisitDeclContext(DeclContext *DC); + void VisitVarDecl(VarDecl *D); + void VisitFunctionDecl(FunctionDecl *D); + void VisitBlockDecl(BlockDecl *D); + void VisitDecl(Decl *D); +}; + +} // anonymous namespace + +//===----------------------------------------------------------------------===// +// StmtMapper Implementation +//===----------------------------------------------------------------------===// + +void StmtMapper::VisitDeclStmt(DeclStmt *Node) { + DeclMapper Mapper(Map); + for (DeclStmt::decl_iterator + I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I) + Mapper.Visit(*I); +} + +void StmtMapper::VisitDeclRefExpr(DeclRefExpr *Node) { + NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getPrimaryDecl()); + Map.insert(std::make_pair(PrimD, ASTNode(Parent, Node))); +} + +void StmtMapper::VisitStmt(Stmt *Node) { + for (Stmt::child_iterator + I = Node->child_begin(), E = Node->child_end(); I != E; ++I) + Visit(*I); +} + +//===----------------------------------------------------------------------===// +// DeclMapper Implementation +//===----------------------------------------------------------------------===// + +void DeclMapper::VisitDeclContext(DeclContext *DC) { + for (DeclContext::decl_iterator + I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) + Visit(*I); +} + +void DeclMapper::VisitFunctionDecl(FunctionDecl *D) { + if (!D->isThisDeclarationADefinition()) + return; + + StmtMapper(Map, D).Visit(D->getBody()); +} + +void DeclMapper::VisitBlockDecl(BlockDecl *D) { + StmtMapper(Map, D).Visit(D->getBody()); +} + +void DeclMapper::VisitVarDecl(VarDecl *D) { + if (Expr *Init = D->getInit()) + StmtMapper(Map, D).Visit(Init); +} + +void DeclMapper::VisitDecl(Decl *D) { + if (DeclContext *DC = dyn_cast<DeclContext>(D)) + VisitDeclContext(DC); +} + +//===----------------------------------------------------------------------===// +// DeclReferenceMap Implementation +//===----------------------------------------------------------------------===// + +DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) { + DeclMapper(Map).Visit(Ctx.getTranslationUnitDecl()); +} + +DeclReferenceMap::astnode_iterator +DeclReferenceMap::refs_begin(NamedDecl *D) const { + NamedDecl *Prim = cast<NamedDecl>(D->getPrimaryDecl()); + return astnode_iterator(Map.lower_bound(Prim)); +} + +DeclReferenceMap::astnode_iterator +DeclReferenceMap::refs_end(NamedDecl *D) const { + NamedDecl *Prim = cast<NamedDecl>(D->getPrimaryDecl()); + return astnode_iterator(Map.upper_bound(Prim)); +} + +bool DeclReferenceMap::refs_empty(NamedDecl *D) const { + NamedDecl *Prim = cast<NamedDecl>(D->getPrimaryDecl()); + return refs_begin(Prim) == refs_end(Prim); +} |