]> granicus.if.org Git - clang/commitdiff
Introduce the DeclReferenceMap class inside the AST library.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sun, 5 Jul 2009 22:22:06 +0000 (22:22 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sun, 5 Jul 2009 22:22:06 +0000 (22:22 +0000)
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

include/clang/AST/DeclReferenceMap.h [new file with mode: 0644]
lib/AST/CMakeLists.txt
lib/AST/DeclReferenceMap.cpp [new file with mode: 0644]

diff --git a/include/clang/AST/DeclReferenceMap.h b/include/clang/AST/DeclReferenceMap.h
new file mode 100644 (file)
index 0000000..b2068fb
--- /dev/null
@@ -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
index 8fb86c6fc20bc747817661952dc67a950ea3f9bf..899bc8f78ebbcb84eb5a9a8c42b6cdcfc97a1984 100644 (file)
@@ -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 (file)
index 0000000..3e0114a
--- /dev/null
@@ -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 &Map;
+  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 &Map;
+  
+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);  
+}