[cfe-commits] r74801 - in /cfe/trunk: include/clang/AST/DeclReferenceMap.h lib/AST/CMakeLists.txt lib/AST/DeclReferenceMap.cpp
Argiris Kirtzidis
akyrtzi at gmail.com
Sun Jul 5 15:22:06 PDT 2009
Author: akirtzidis
Date: Sun Jul 5 17:22:06 2009
New Revision: 74801
URL: http://llvm.org/viewvc/llvm-project?rev=74801&view=rev
Log:
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.
Added:
cfe/trunk/include/clang/AST/DeclReferenceMap.h
cfe/trunk/lib/AST/DeclReferenceMap.cpp
Modified:
cfe/trunk/lib/AST/CMakeLists.txt
Added: cfe/trunk/include/clang/AST/DeclReferenceMap.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclReferenceMap.h?rev=74801&view=auto
==============================================================================
--- cfe/trunk/include/clang/AST/DeclReferenceMap.h (added)
+++ cfe/trunk/include/clang/AST/DeclReferenceMap.h Sun Jul 5 17:22:06 2009
@@ -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 <map>
+
+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<NamedDecl*, ASTNode> 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
Modified: cfe/trunk/lib/AST/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/CMakeLists.txt?rev=74801&r1=74800&r2=74801&view=diff
==============================================================================
--- cfe/trunk/lib/AST/CMakeLists.txt (original)
+++ cfe/trunk/lib/AST/CMakeLists.txt Sun Jul 5 17:22:06 2009
@@ -13,6 +13,7 @@
DeclGroup.cpp
DeclObjC.cpp
DeclPrinter.cpp
+ DeclReferenceMap.cpp
DeclTemplate.cpp
ExprConstant.cpp
Expr.cpp
Added: cfe/trunk/lib/AST/DeclReferenceMap.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclReferenceMap.cpp?rev=74801&view=auto
==============================================================================
--- cfe/trunk/lib/AST/DeclReferenceMap.cpp (added)
+++ cfe/trunk/lib/AST/DeclReferenceMap.cpp Sun Jul 5 17:22:06 2009
@@ -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);
+}
More information about the cfe-commits
mailing list