blob: 85b4f2e1961767d1e5f85f7e435f01f3213022b4 [file] [log] [blame]
Chris Lattnerc3a65402009-07-12 22:33:12 +00001//===--- Entity.cpp - Cross-translation-unit "token" for decls ------------===//
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Entity is a ASTContext-independent way to refer to declarations that are
11// visible across translation units.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000015#include "EntityImpl.h"
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000016#include "ProgramImpl.h"
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000017#include "clang/Index/Program.h"
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000018#include "clang/AST/Decl.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/DeclVisitor.h"
21using namespace clang;
22using namespace idx;
23
24// FIXME: Entity is really really basic currently, mostly written to work
25// on variables and functions. Should support types and other decls eventually..
26
27
28//===----------------------------------------------------------------------===//
29// EntityGetter
30//===----------------------------------------------------------------------===//
31
32namespace clang {
33namespace idx {
34
35/// \brief Gets the Entity associated with a Decl.
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000036class EntityGetter : public DeclVisitor<EntityGetter, Entity> {
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000037 ProgramImpl &Prog;
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000038
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000039public:
40 EntityGetter(ProgramImpl &prog) : Prog(prog) { }
41
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000042 Entity VisitNamedDecl(NamedDecl *D);
43 Entity VisitVarDecl(VarDecl *D);
44 Entity VisitFunctionDecl(FunctionDecl *D);
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000045};
46
47}
48}
49
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000050Entity EntityGetter::VisitNamedDecl(NamedDecl *D) {
51 Entity Parent;
52 if (!D->getDeclContext()->isTranslationUnit()) {
53 Parent = Visit(cast<Decl>(D->getDeclContext()));
54 // FIXME: Anonymous structs ?
55 if (Parent.isInvalid())
56 return Entity();
57 }
58 if (Parent.isValid() && Parent.isInternalToTU())
59 return Entity(D);
60
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000061 // FIXME: Only works for DeclarationNames that are identifiers.
62
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000063 DeclarationName Name = D->getDeclName();
64
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000065 if (!Name.isIdentifier())
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000066 return Entity();
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000067
68 IdentifierInfo *II = Name.getAsIdentifierInfo();
Argyrios Kyrtzidisdc0b11e2009-07-17 01:19:03 +000069 if (!II)
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000070 return Entity();
Argyrios Kyrtzidisdc0b11e2009-07-17 01:19:03 +000071
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000072 EntityImpl::IdEntryTy *Id =
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000073 &Prog.getIdents().GetOrCreateValue(II->getName(),
74 II->getName() + II->getLength());
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000075 unsigned IdNS = D->getIdentifierNamespace();
76
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000077 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000078 EntityImpl::Profile(ID, Parent, Id, IdNS);
79
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000080 ProgramImpl::EntitySetTy &Entities = Prog.getEntities();
81 void *InsertPos = 0;
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000082 if (EntityImpl *Ent = Entities.FindNodeOrInsertPos(ID, InsertPos))
83 return Entity(Ent);
84
85 void *Buf = Prog.Allocate(sizeof(EntityImpl));
86 EntityImpl *New = new (Buf) EntityImpl(Parent, Id, IdNS);
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000087 Entities.InsertNode(New, InsertPos);
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000088
89 return Entity(New);
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000090}
91
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000092Entity EntityGetter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000093 // If it's static it cannot be referred to by another translation unit.
94 if (D->getStorageClass() == VarDecl::Static)
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +000095 return Entity(D);
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000096
97 return VisitNamedDecl(D);
98}
99
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000100Entity EntityGetter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000101 // If it's static it cannot be refered to by another translation unit.
102 if (D->getStorageClass() == FunctionDecl::Static)
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000103 return Entity(D);
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000104
105 return VisitNamedDecl(D);
106}
107
108//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000109// EntityImpl Implementation
110//===----------------------------------------------------------------------===//
111
112Decl *EntityImpl::getDecl(ASTContext &AST) {
113 DeclContext *DC =
114 Parent.isInvalid() ? AST.getTranslationUnitDecl()
115 : cast<DeclContext>(Parent.getDecl(AST));
116 if (!DC)
117 return 0; // Couldn't get the parent context.
118
119 IdentifierInfo &II = AST.Idents.get(Id->getKeyData());
120
121 DeclContext::lookup_result Res = DC->lookup(DeclarationName(&II));
122 for (DeclContext::lookup_iterator I = Res.first, E = Res.second; I!=E; ++I) {
123 if ((*I)->getIdentifierNamespace() == IdNS)
124 return *I;
125 }
126
127 return 0; // Failed to find a decl using this Entity.
128}
129
130/// \brief Get an Entity associated with the given Decl.
131/// \returns Null if an Entity cannot refer to this Decl.
132Entity EntityImpl::get(Decl *D, ProgramImpl &Prog) {
133 assert(D && "Passed null Decl");
134 return EntityGetter(Prog).Visit(D);
135}
136
Argyrios Kyrtzidis4c7c5a12009-07-21 07:52:21 +0000137std::string EntityImpl::getPrintableName() {
138 return std::string(Id->getKeyData(), Id->getKeyData() + Id->getKeyLength());
139}
140
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000141//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000142// Entity Implementation
143//===----------------------------------------------------------------------===//
144
Argyrios Kyrtzidis1f717272009-07-21 02:10:32 +0000145Entity::Entity(Decl *D) : Val(D->getCanonicalDecl()) { }
146
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000147/// \brief Find the Decl that can be referred to by this entity.
Zhongxing Xu6d8f56f2009-07-24 03:38:27 +0000148Decl *Entity::getDecl(ASTContext &AST) const {
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000149 if (isInvalid())
150 return 0;
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000151
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000152 if (Decl *D = Val.dyn_cast<Decl *>())
153 // Check that the passed AST is actually the one that this Decl belongs to.
154 return (&D->getASTContext() == &AST) ? D : 0;
155
156 return Val.get<EntityImpl *>()->getDecl(AST);
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000157}
158
Zhongxing Xuf7856432009-07-23 08:32:25 +0000159std::string Entity::getPrintableName() const {
Argyrios Kyrtzidis4c7c5a12009-07-21 07:52:21 +0000160 if (isInvalid())
161 return "<< Invalid >>";
162
163 if (Decl *D = Val.dyn_cast<Decl *>()) {
164 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
165 return ND->getNameAsString();
166 else
167 return std::string();
Zhongxing Xu53363b22009-07-15 04:39:21 +0000168 }
Argyrios Kyrtzidis4c7c5a12009-07-21 07:52:21 +0000169
170 return Val.get<EntityImpl *>()->getPrintableName();
Zhongxing Xu53363b22009-07-15 04:39:21 +0000171}
172
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000173/// \brief Get an Entity associated with the given Decl.
174/// \returns Null if an Entity cannot refer to this Decl.
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000175Entity Entity::get(Decl *D, Program &Prog) {
176 if (D == 0)
177 return Entity();
178 ProgramImpl &ProgImpl = *static_cast<ProgramImpl*>(Prog.Impl);
179 return EntityImpl::get(D, ProgImpl);
180}
181
182unsigned
183llvm::DenseMapInfo<Entity>::getHashValue(Entity E) {
184 return DenseMapInfo<void*>::getHashValue(E.getAsOpaquePtr());
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000185}