2009-07-13 06:33:12 +08:00
|
|
|
//===--- Entity.cpp - Cross-translation-unit "token" for decls ------------===//
|
2009-07-06 06:22:19 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Entity is a ASTContext-independent way to refer to declarations that are
|
|
|
|
// visible across translation units.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
#include "EntityImpl.h"
|
2009-07-06 06:22:19 +08:00
|
|
|
#include "ProgramImpl.h"
|
2009-07-21 08:07:06 +08:00
|
|
|
#include "clang/Index/Program.h"
|
2009-07-06 06:22:19 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace idx;
|
|
|
|
|
|
|
|
// FIXME: Entity is really really basic currently, mostly written to work
|
|
|
|
// on variables and functions. Should support types and other decls eventually..
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// EntityGetter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace idx {
|
|
|
|
|
|
|
|
/// \brief Gets the Entity associated with a Decl.
|
2009-07-21 08:07:06 +08:00
|
|
|
class EntityGetter : public DeclVisitor<EntityGetter, Entity> {
|
2009-07-06 06:22:19 +08:00
|
|
|
ProgramImpl &Prog;
|
2009-07-21 08:07:06 +08:00
|
|
|
|
2009-07-06 06:22:19 +08:00
|
|
|
public:
|
|
|
|
EntityGetter(ProgramImpl &prog) : Prog(prog) { }
|
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
Entity VisitNamedDecl(NamedDecl *D);
|
|
|
|
Entity VisitVarDecl(VarDecl *D);
|
|
|
|
Entity VisitFunctionDecl(FunctionDecl *D);
|
2009-07-06 06:22:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
Entity EntityGetter::VisitNamedDecl(NamedDecl *D) {
|
|
|
|
Entity Parent;
|
|
|
|
if (!D->getDeclContext()->isTranslationUnit()) {
|
|
|
|
Parent = Visit(cast<Decl>(D->getDeclContext()));
|
|
|
|
// FIXME: Anonymous structs ?
|
|
|
|
if (Parent.isInvalid())
|
|
|
|
return Entity();
|
|
|
|
}
|
|
|
|
if (Parent.isValid() && Parent.isInternalToTU())
|
|
|
|
return Entity(D);
|
|
|
|
|
2009-07-06 06:22:19 +08:00
|
|
|
// FIXME: Only works for DeclarationNames that are identifiers.
|
2009-07-30 07:39:52 +08:00
|
|
|
// Treats other DeclarationNames as internal Decls for now..
|
2009-07-06 06:22:19 +08:00
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
DeclarationName Name = D->getDeclName();
|
|
|
|
|
2009-07-06 06:22:19 +08:00
|
|
|
if (!Name.isIdentifier())
|
2009-07-30 07:39:52 +08:00
|
|
|
return Entity(D);
|
2009-07-06 06:22:19 +08:00
|
|
|
|
|
|
|
IdentifierInfo *II = Name.getAsIdentifierInfo();
|
2009-07-17 09:19:03 +08:00
|
|
|
if (!II)
|
2009-07-21 08:07:06 +08:00
|
|
|
return Entity();
|
2009-07-17 09:19:03 +08:00
|
|
|
|
2009-07-30 07:40:21 +08:00
|
|
|
IdentifierInfo *Id = &Prog.getIdents().get(II->getName(),
|
|
|
|
II->getName() + II->getLength());
|
2009-07-21 08:07:06 +08:00
|
|
|
unsigned IdNS = D->getIdentifierNamespace();
|
|
|
|
|
2009-07-06 06:22:19 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-07-21 08:07:06 +08:00
|
|
|
EntityImpl::Profile(ID, Parent, Id, IdNS);
|
|
|
|
|
2009-07-06 06:22:19 +08:00
|
|
|
ProgramImpl::EntitySetTy &Entities = Prog.getEntities();
|
|
|
|
void *InsertPos = 0;
|
2009-07-21 08:07:06 +08:00
|
|
|
if (EntityImpl *Ent = Entities.FindNodeOrInsertPos(ID, InsertPos))
|
|
|
|
return Entity(Ent);
|
2009-07-06 06:22:19 +08:00
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
void *Buf = Prog.Allocate(sizeof(EntityImpl));
|
|
|
|
EntityImpl *New = new (Buf) EntityImpl(Parent, Id, IdNS);
|
|
|
|
Entities.InsertNode(New, InsertPos);
|
|
|
|
|
|
|
|
return Entity(New);
|
2009-07-06 06:22:19 +08:00
|
|
|
}
|
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
Entity EntityGetter::VisitVarDecl(VarDecl *D) {
|
2009-07-06 06:22:19 +08:00
|
|
|
// If it's static it cannot be referred to by another translation unit.
|
|
|
|
if (D->getStorageClass() == VarDecl::Static)
|
2009-07-21 08:07:06 +08:00
|
|
|
return Entity(D);
|
2009-07-06 06:22:19 +08:00
|
|
|
|
|
|
|
return VisitNamedDecl(D);
|
|
|
|
}
|
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
Entity EntityGetter::VisitFunctionDecl(FunctionDecl *D) {
|
2009-07-06 06:22:19 +08:00
|
|
|
// If it's static it cannot be refered to by another translation unit.
|
|
|
|
if (D->getStorageClass() == FunctionDecl::Static)
|
2009-07-21 08:07:06 +08:00
|
|
|
return Entity(D);
|
2009-07-06 06:22:19 +08:00
|
|
|
|
|
|
|
return VisitNamedDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-07-21 08:07:06 +08:00
|
|
|
// EntityImpl Implementation
|
2009-07-06 06:22:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
Decl *EntityImpl::getDecl(ASTContext &AST) {
|
2009-07-06 06:22:19 +08:00
|
|
|
DeclContext *DC =
|
2009-07-21 08:07:06 +08:00
|
|
|
Parent.isInvalid() ? AST.getTranslationUnitDecl()
|
|
|
|
: cast<DeclContext>(Parent.getDecl(AST));
|
2009-07-06 06:22:19 +08:00
|
|
|
if (!DC)
|
|
|
|
return 0; // Couldn't get the parent context.
|
|
|
|
|
2009-07-30 07:40:21 +08:00
|
|
|
IdentifierInfo &II = AST.Idents.get(Id->getName(),
|
|
|
|
Id->getName() + Id->getLength());
|
2009-07-06 06:22:19 +08:00
|
|
|
|
|
|
|
DeclContext::lookup_result Res = DC->lookup(DeclarationName(&II));
|
|
|
|
for (DeclContext::lookup_iterator I = Res.first, E = Res.second; I!=E; ++I) {
|
2009-07-21 08:07:06 +08:00
|
|
|
if ((*I)->getIdentifierNamespace() == IdNS)
|
2009-07-06 06:22:19 +08:00
|
|
|
return *I;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // Failed to find a decl using this Entity.
|
|
|
|
}
|
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
/// \brief Get an Entity associated with the given Decl.
|
|
|
|
/// \returns Null if an Entity cannot refer to this Decl.
|
|
|
|
Entity EntityImpl::get(Decl *D, ProgramImpl &Prog) {
|
|
|
|
assert(D && "Passed null Decl");
|
|
|
|
return EntityGetter(Prog).Visit(D);
|
|
|
|
}
|
|
|
|
|
2009-07-21 15:52:21 +08:00
|
|
|
std::string EntityImpl::getPrintableName() {
|
2009-07-30 07:40:21 +08:00
|
|
|
return Id->getName();
|
2009-07-21 15:52:21 +08:00
|
|
|
}
|
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Entity Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-21 10:10:32 +08:00
|
|
|
Entity::Entity(Decl *D) : Val(D->getCanonicalDecl()) { }
|
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
/// \brief Find the Decl that can be referred to by this entity.
|
2009-07-24 11:38:27 +08:00
|
|
|
Decl *Entity::getDecl(ASTContext &AST) const {
|
2009-07-21 08:07:06 +08:00
|
|
|
if (isInvalid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (Decl *D = Val.dyn_cast<Decl *>())
|
|
|
|
// Check that the passed AST is actually the one that this Decl belongs to.
|
|
|
|
return (&D->getASTContext() == &AST) ? D : 0;
|
|
|
|
|
|
|
|
return Val.get<EntityImpl *>()->getDecl(AST);
|
|
|
|
}
|
|
|
|
|
2009-07-23 16:32:25 +08:00
|
|
|
std::string Entity::getPrintableName() const {
|
2009-07-21 15:52:21 +08:00
|
|
|
if (isInvalid())
|
|
|
|
return "<< Invalid >>";
|
|
|
|
|
|
|
|
if (Decl *D = Val.dyn_cast<Decl *>()) {
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
|
|
|
return ND->getNameAsString();
|
|
|
|
else
|
|
|
|
return std::string();
|
2009-07-15 12:39:21 +08:00
|
|
|
}
|
2009-07-21 15:52:21 +08:00
|
|
|
|
|
|
|
return Val.get<EntityImpl *>()->getPrintableName();
|
2009-07-15 12:39:21 +08:00
|
|
|
}
|
|
|
|
|
2009-07-06 06:22:19 +08:00
|
|
|
/// \brief Get an Entity associated with the given Decl.
|
|
|
|
/// \returns Null if an Entity cannot refer to this Decl.
|
2009-07-21 08:07:06 +08:00
|
|
|
Entity Entity::get(Decl *D, Program &Prog) {
|
|
|
|
if (D == 0)
|
|
|
|
return Entity();
|
|
|
|
ProgramImpl &ProgImpl = *static_cast<ProgramImpl*>(Prog.Impl);
|
|
|
|
return EntityImpl::get(D, ProgImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
llvm::DenseMapInfo<Entity>::getHashValue(Entity E) {
|
|
|
|
return DenseMapInfo<void*>::getHashValue(E.getAsOpaquePtr());
|
2009-07-06 06:22:19 +08:00
|
|
|
}
|