From: Argyrios Kyrtzidis Date: Sun, 5 Jul 2009 22:22:06 +0000 (+0000) Subject: Introduce the DeclReferenceMap class inside the AST library. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2c2ba3e258961dd98cacffe3a2167bb6d958fd53;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74801 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DeclReferenceMap.h b/include/clang/AST/DeclReferenceMap.h new file mode 100644 index 0000000000..b2068fb275 --- /dev/null +++ b/include/clang/AST/DeclReferenceMap.h @@ -0,0 +1,82 @@ +//===--- 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 +// reference them. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_AST_DECLREFERENCEMAP_H +#define LLVM_CLANG_AST_DECLREFERENCEMAP_H + +#include "clang/AST/ASTNode.h" +#include + +namespace clang { + class ASTContext; + class NamedDecl; + +/// \brief Maps NamedDecls with the ASTNodes that reference them. +/// +/// References are mapped and retrieved using the primary decls +/// (see Decl::getPrimaryDecl()). +class DeclReferenceMap { +public: + explicit DeclReferenceMap(ASTContext &Ctx); + + typedef std::multimap MapTy; + + class astnode_iterator { + MapTy::iterator I; + + astnode_iterator(MapTy::iterator i) : I(i) { } + friend class DeclReferenceMap; + + public: + typedef ASTNode value_type; + typedef ASTNode& reference; + typedef ASTNode* pointer; + typedef MapTy::iterator::iterator_category iterator_category; + typedef MapTy::iterator::difference_type difference_type; + + astnode_iterator() { } + + reference operator*() const { return I->second; } + pointer operator->() const { return &I->second; } + + astnode_iterator& operator++() { + ++I; + return *this; + } + + astnode_iterator operator++(int) { + astnode_iterator tmp(*this); + ++(*this); + return tmp; + } + + friend bool operator==(astnode_iterator L, astnode_iterator R) { + return L.I == R.I; + } + friend bool operator!=(astnode_iterator L, astnode_iterator R) { + return L.I != R.I; + } + }; + + astnode_iterator refs_begin(NamedDecl *D) const; + astnode_iterator refs_end(NamedDecl *D) const; + bool refs_empty(NamedDecl *D) const; + +private: + mutable MapTy Map; +}; + +} // end clang namespace + +#endif diff --git a/lib/AST/CMakeLists.txt b/lib/AST/CMakeLists.txt index 8fb86c6fc2..899bc8f78e 100644 --- a/lib/AST/CMakeLists.txt +++ b/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/lib/AST/DeclReferenceMap.cpp b/lib/AST/DeclReferenceMap.cpp new file mode 100644 index 0000000000..3e0114a0cb --- /dev/null +++ b/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 { + 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 { + 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(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(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(D->getPrimaryDecl()); + return astnode_iterator(Map.lower_bound(Prim)); +} + +DeclReferenceMap::astnode_iterator +DeclReferenceMap::refs_end(NamedDecl *D) const { + NamedDecl *Prim = cast(D->getPrimaryDecl()); + return astnode_iterator(Map.upper_bound(Prim)); +} + +bool DeclReferenceMap::refs_empty(NamedDecl *D) const { + NamedDecl *Prim = cast(D->getPrimaryDecl()); + return refs_begin(Prim) == refs_end(Prim); +}