blob: ca2e7c8f3e6b85041706196ffa7f75ca68551d06 [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
15#include "clang/Index/Entity.h"
16#include "clang/Index/Program.h"
17#include "ProgramImpl.h"
18#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.
36class EntityGetter : public DeclVisitor<EntityGetter, Entity *> {
37 ProgramImpl &Prog;
38
39public:
40 EntityGetter(ProgramImpl &prog) : Prog(prog) { }
41
42 Entity *get(Entity *Parent, DeclarationName Name);
43
44 Entity *VisitNamedDecl(NamedDecl *D);
45 Entity *VisitVarDecl(VarDecl *D);
46 Entity *VisitFunctionDecl(FunctionDecl *D);
47};
48
49}
50}
51
52Entity *EntityGetter::get(Entity *Parent, DeclarationName Name) {
53 // FIXME: Only works for DeclarationNames that are identifiers.
54
55 if (!Name.isIdentifier())
56 return 0;
57
58 IdentifierInfo *II = Name.getAsIdentifierInfo();
Argyrios Kyrtzidisdc0b11e2009-07-17 01:19:03 +000059 if (!II)
60 return 0;
61
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +000062 ProgramImpl::IdEntryTy *Id =
63 &Prog.getIdents().GetOrCreateValue(II->getName(),
64 II->getName() + II->getLength());
65
66 llvm::FoldingSetNodeID ID;
67 Entity::Profile(ID, Parent, Id);
68
69 ProgramImpl::EntitySetTy &Entities = Prog.getEntities();
70 void *InsertPos = 0;
71 if (Entity *Ent = Entities.FindNodeOrInsertPos(ID, InsertPos))
72 return Ent;
73
74 void *Buf = Prog.Allocate(sizeof(Entity));
75 Entity *New = new (Buf) Entity(Parent, Id);
76 Entities.InsertNode(New, InsertPos);
77 return New;
78}
79
80Entity *EntityGetter::VisitNamedDecl(NamedDecl *D) {
81 // FIXME: Function declarations that are inside functions ?
82 if (!D->getDeclContext()->isFileContext())
83 return 0;
84
85 Entity *Parent = Visit(cast<Decl>(D->getDeclContext()));
86 return get(Parent, D->getDeclName());
87}
88
89Entity *EntityGetter::VisitVarDecl(VarDecl *D) {
90 // If it's static it cannot be referred to by another translation unit.
91 if (D->getStorageClass() == VarDecl::Static)
92 return 0;
93
94 return VisitNamedDecl(D);
95}
96
97Entity *EntityGetter::VisitFunctionDecl(FunctionDecl *D) {
98 // If it's static it cannot be refered to by another translation unit.
99 if (D->getStorageClass() == FunctionDecl::Static)
100 return 0;
101
102 return VisitNamedDecl(D);
103}
104
105//===----------------------------------------------------------------------===//
106// Entity Implementation
107//===----------------------------------------------------------------------===//
108
109/// \brief Find the Decl that can be referred to by this entity.
110Decl *Entity::getDecl(ASTContext &AST) {
111 DeclContext *DC =
112 Parent == 0 ? AST.getTranslationUnitDecl()
113 : cast<DeclContext>(Parent->getDecl(AST));
114 if (!DC)
115 return 0; // Couldn't get the parent context.
116
117 ProgramImpl::IdEntryTy *Entry = static_cast<ProgramImpl::IdEntryTy *>(Id);
118 IdentifierInfo &II = AST.Idents.get(Entry->getKeyData());
119
120 DeclContext::lookup_result Res = DC->lookup(DeclarationName(&II));
121 for (DeclContext::lookup_iterator I = Res.first, E = Res.second; I!=E; ++I) {
122 if (!isa<TagDecl>(*I))
123 return *I;
124 }
125
126 return 0; // Failed to find a decl using this Entity.
127}
128
Zhongxing Xu53363b22009-07-15 04:39:21 +0000129const char *Entity::getName(ASTContext &Ctx) {
130 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(getDecl(Ctx))) {
131 return ND->getNameAsCString();
132 }
133 return 0;
134}
135
Argyrios Kyrtzidis9eec4ed2009-07-05 22:22:19 +0000136/// \brief Get an Entity associated with the given Decl.
137/// \returns Null if an Entity cannot refer to this Decl.
138Entity *Entity::get(Decl *D, Program &Prog) {
139 assert(D && "Passed null Decl");
140 ProgramImpl &Impl = *static_cast<ProgramImpl*>(Prog.Impl);
141 return EntityGetter(Impl).Visit(D);
142}