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-30 07:41:46 +08:00
|
|
|
#include "clang/Index/GlobalSelector.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-30 07:41:46 +08:00
|
|
|
Program &Prog;
|
|
|
|
ProgramImpl &ProgImpl;
|
2009-07-21 08:07:06 +08:00
|
|
|
|
2009-07-06 06:22:19 +08:00
|
|
|
public:
|
2009-07-30 07:41:46 +08:00
|
|
|
EntityGetter(Program &prog, ProgramImpl &progImpl)
|
|
|
|
: Prog(prog), ProgImpl(progImpl) { }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
Entity VisitNamedDecl(NamedDecl *D);
|
|
|
|
Entity VisitVarDecl(VarDecl *D);
|
2009-09-09 23:08:12 +08:00
|
|
|
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-30 07:41:46 +08:00
|
|
|
// FIXME: Only works for DeclarationNames that are identifiers and selectors.
|
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-30 07:41:46 +08:00
|
|
|
DeclarationName LocalName = D->getDeclName();
|
|
|
|
if (!LocalName)
|
2009-07-30 07:39:52 +08:00
|
|
|
return Entity(D);
|
2009-07-06 06:22:19 +08:00
|
|
|
|
2009-07-30 07:41:46 +08:00
|
|
|
DeclarationName GlobName;
|
|
|
|
|
|
|
|
if (IdentifierInfo *II = LocalName.getAsIdentifierInfo()) {
|
|
|
|
IdentifierInfo *GlobII =
|
|
|
|
&ProgImpl.getIdents().get(II->getName(), II->getName() + II->getLength());
|
|
|
|
GlobName = DeclarationName(GlobII);
|
|
|
|
} else {
|
|
|
|
Selector LocalSel = LocalName.getObjCSelector();
|
|
|
|
|
|
|
|
// Treats other DeclarationNames as internal Decls for now..
|
|
|
|
if (LocalSel.isNull())
|
|
|
|
return Entity(D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-30 07:41:46 +08:00
|
|
|
Selector GlobSel =
|
|
|
|
(uintptr_t)GlobalSelector::get(LocalSel, Prog).getAsOpaquePtr();
|
|
|
|
GlobName = DeclarationName(GlobSel);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-30 07:41:46 +08:00
|
|
|
assert(GlobName);
|
2009-07-17 09:19:03 +08:00
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
unsigned IdNS = D->getIdentifierNamespace();
|
|
|
|
|
2009-07-30 07:41:46 +08:00
|
|
|
ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
|
|
|
|
bool isObjCInstanceMethod = MD && MD->isInstanceMethod();
|
|
|
|
|
2009-07-06 06:22:19 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-07-30 07:41:46 +08:00
|
|
|
EntityImpl::Profile(ID, Parent, GlobName, IdNS, isObjCInstanceMethod);
|
2009-07-21 08:07:06 +08:00
|
|
|
|
2009-07-30 07:41:46 +08:00
|
|
|
ProgramImpl::EntitySetTy &Entities = ProgImpl.getEntities();
|
2009-07-06 06:22:19 +08:00
|
|
|
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-30 07:41:46 +08:00
|
|
|
void *Buf = ProgImpl.Allocate(sizeof(EntityImpl));
|
|
|
|
EntityImpl *New =
|
|
|
|
new (Buf) EntityImpl(Parent, GlobName, IdNS, isObjCInstanceMethod);
|
2009-07-21 08:07:06 +08:00
|
|
|
Entities.InsertNode(New, InsertPos);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
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-09-09 23:08:12 +08:00
|
|
|
|
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-09-09 23:08:12 +08:00
|
|
|
|
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:41:46 +08:00
|
|
|
|
|
|
|
DeclarationName LocalName;
|
|
|
|
|
|
|
|
if (IdentifierInfo *GlobII = Name.getAsIdentifierInfo()) {
|
|
|
|
IdentifierInfo &II = AST.Idents.get(GlobII->getName(),
|
|
|
|
GlobII->getName() + GlobII->getLength());
|
|
|
|
LocalName = DeclarationName(&II);
|
|
|
|
} else {
|
|
|
|
Selector GlobSel = Name.getObjCSelector();
|
|
|
|
assert(!GlobSel.isNull() && "A not handled yet declaration name");
|
|
|
|
GlobalSelector GSel =
|
|
|
|
GlobalSelector::getFromOpaquePtr(GlobSel.getAsOpaquePtr());
|
|
|
|
LocalName = GSel.getSelector(AST);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(LocalName);
|
|
|
|
|
|
|
|
DeclContext::lookup_result Res = DC->lookup(LocalName);
|
2009-07-06 06:22:19 +08:00
|
|
|
for (DeclContext::lookup_iterator I = Res.first, E = Res.second; I!=E; ++I) {
|
2009-07-30 07:41:46 +08:00
|
|
|
Decl *D = *I;
|
|
|
|
if (D->getIdentifierNamespace() == IdNS) {
|
|
|
|
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
if (MD->isInstanceMethod() == IsObjCInstanceMethod)
|
|
|
|
return MD;
|
|
|
|
} else
|
|
|
|
return D;
|
|
|
|
}
|
2009-07-06 06:22:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2009-07-30 07:41:46 +08:00
|
|
|
Entity EntityImpl::get(Decl *D, Program &Prog, ProgramImpl &ProgImpl) {
|
2009-07-21 08:07:06 +08:00
|
|
|
assert(D && "Passed null Decl");
|
2009-07-30 07:41:46 +08:00
|
|
|
return EntityGetter(Prog, ProgImpl).Visit(D);
|
2009-07-21 08:07:06 +08:00
|
|
|
}
|
|
|
|
|
2009-07-21 15:52:21 +08:00
|
|
|
std::string EntityImpl::getPrintableName() {
|
2009-07-30 07:41:46 +08:00
|
|
|
return Name.getAsString();
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-21 08:07:06 +08:00
|
|
|
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 >>";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-21 15:52:21 +08:00
|
|
|
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);
|
2009-07-30 07:41:46 +08:00
|
|
|
return EntityImpl::get(D, Prog, ProgImpl);
|
2009-07-21 08:07:06 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned
|
2009-07-21 08:07:06 +08:00
|
|
|
llvm::DenseMapInfo<Entity>::getHashValue(Entity E) {
|
|
|
|
return DenseMapInfo<void*>::getHashValue(E.getAsOpaquePtr());
|
2009-07-06 06:22:19 +08:00
|
|
|
}
|