blob: feed3e4c80771a6d973173a35d95b2b796e38e8d [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
137//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000138// Entity Implementation
139//===----------------------------------------------------------------------===//
140
141/// \brief Find the Decl that can be referred to by this entity.
142Decl *Entity::getDecl(ASTContext &AST) {
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000143 if (isInvalid())
144 return 0;
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000145
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000146 if (Decl *D = Val.dyn_cast<Decl *>())
147 // Check that the passed AST is actually the one that this Decl belongs to.
148 return (&D->getASTContext() == &AST) ? D : 0;
149
150 return Val.get<EntityImpl *>()->getDecl(AST);
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000151}
152
Zhongxing Xu56aac3f2009-07-17 07:49:44 +0000153std::string Entity::getPrintableName(ASTContext &Ctx) {
Zhongxing Xu53363b22009-07-15 04:39:21 +0000154 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(getDecl(Ctx))) {
Zhongxing Xu56aac3f2009-07-17 07:49:44 +0000155 return ND->getNameAsString();
Zhongxing Xu53363b22009-07-15 04:39:21 +0000156 }
Zhongxing Xu56aac3f2009-07-17 07:49:44 +0000157 return std::string();
Zhongxing Xu53363b22009-07-15 04:39:21 +0000158}
159
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000160/// \brief Get an Entity associated with the given Decl.
161/// \returns Null if an Entity cannot refer to this Decl.
Argyrios Kyrtzidisf7cf15c2009-07-21 00:07:06 +0000162Entity Entity::get(Decl *D, Program &Prog) {
163 if (D == 0)
164 return Entity();
165 ProgramImpl &ProgImpl = *static_cast<ProgramImpl*>(Prog.Impl);
166 return EntityImpl::get(D, ProgImpl);
167}
168
169unsigned
170llvm::DenseMapInfo<Entity>::getHashValue(Entity E) {
171 return DenseMapInfo<void*>::getHashValue(E.getAsOpaquePtr());
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000172}