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
This commit is contained in:
Argyrios Kyrtzidis 2009-07-05 22:22:06 +00:00
parent 02dd4f9389
commit e568a9792f
3 changed files with 214 additions and 0 deletions

View File

@ -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

View File

@ -13,6 +13,7 @@ add_clang_library(clangAST
DeclGroup.cpp
DeclObjC.cpp
DeclPrinter.cpp
DeclReferenceMap.cpp
DeclTemplate.cpp
ExprConstant.cpp
Expr.cpp

View File

@ -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);
}