blob: 027beede49124f3076af45c5115d03eea702c3d1 [file] [log] [blame]
Chris Lattnera2f42b12008-04-11 07:06:57 +00001//===- IdentifierResolver.cpp - Lexical Scope Name lookup -------*- C++ -*-===//
2//
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//
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000010// This file implements the IdentifierResolver class, which is used for lexical
Douglas Gregor2def4832008-11-17 20:34:05 +000011// scoped lookup, based on declaration names.
Chris Lattnera2f42b12008-04-11 07:06:57 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "IdentifierResolver.h"
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000016#include "clang/Basic/LangOptions.h"
Chris Lattnera2f42b12008-04-11 07:06:57 +000017#include <list>
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000018#include <vector>
Chris Lattnera2f42b12008-04-11 07:06:57 +000019
20using namespace clang;
21
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000022//===----------------------------------------------------------------------===//
23// IdDeclInfoMap class
24//===----------------------------------------------------------------------===//
Chris Lattnera2f42b12008-04-11 07:06:57 +000025
Douglas Gregor2def4832008-11-17 20:34:05 +000026/// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000027/// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
Chris Lattnera2f42b12008-04-11 07:06:57 +000028/// individual IdDeclInfo to heap.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000029class IdentifierResolver::IdDeclInfoMap {
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000030 static const unsigned int VECTOR_SIZE = 512;
31 // Holds vectors of IdDeclInfos that serve as 'pools'.
32 // New vectors are added when the current one is full.
33 std::list< std::vector<IdDeclInfo> > IDIVecs;
Chris Lattnera2f42b12008-04-11 07:06:57 +000034 unsigned int CurIndex;
35
36public:
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000037 IdDeclInfoMap() : CurIndex(VECTOR_SIZE) {}
Chris Lattnera2f42b12008-04-11 07:06:57 +000038
Douglas Gregor2def4832008-11-17 20:34:05 +000039 /// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +000040 /// It creates a new IdDeclInfo if one was not created before for this id.
Douglas Gregor2def4832008-11-17 20:34:05 +000041 IdDeclInfo &operator[](DeclarationName Name);
Chris Lattnera2f42b12008-04-11 07:06:57 +000042};
43
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000044
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000045//===----------------------------------------------------------------------===//
46// LookupContext Implementation
47//===----------------------------------------------------------------------===//
48
49/// getContext - Returns translation unit context for non ScopedDecls and
50/// for EnumConstantDecls returns the parent context of their EnumDecl.
51DeclContext *IdentifierResolver::LookupContext::getContext(Decl *D) {
52 DeclContext *Ctx;
53
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000054 if (EnumConstantDecl *EnumD = dyn_cast<EnumConstantDecl>(D)) {
55 Ctx = EnumD->getDeclContext()->getParent();
56 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D))
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000057 Ctx = SD->getDeclContext();
58 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D))
59 Ctx = Ovl->getDeclContext();
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000060 else
61 return TUCtx();
62
63 if (isa<TranslationUnitDecl>(Ctx))
64 return TUCtx();
65
66 return Ctx;
67}
68
69/// isEqOrContainedBy - Returns true of the given context is the same or a
70/// parent of this one.
71bool IdentifierResolver::LookupContext::isEqOrContainedBy(
72 const LookupContext &PC) const {
73 if (PC.isTU()) return true;
74
75 for (LookupContext Next = *this; !Next.isTU(); Next = Next.getParent())
76 if (Next.Ctx == PC.Ctx) return true;
77
78 return false;
79}
80
81
82//===----------------------------------------------------------------------===//
83// IdDeclInfo Implementation
84//===----------------------------------------------------------------------===//
85
86/// FindContext - Returns an iterator pointing just after the decl that is
87/// in the given context or in a parent of it. The search is in reverse
88/// order, from end to begin.
89IdentifierResolver::IdDeclInfo::DeclsTy::iterator
Zhongxing Xuebed7972008-12-05 01:57:57 +000090IdentifierResolver::IdDeclInfo::FindDeclVisibleInContext(
91 const LookupContext &Ctx,
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000092 const DeclsTy::iterator &Start) {
93 for (DeclsTy::iterator I = Start; I != Decls.begin(); --I) {
94 if (Ctx.isEqOrContainedBy(LookupContext(*(I-1))))
95 return I;
96 }
97
98 return Decls.begin();
99}
100
101/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
102/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
103/// be already added to the scope chain and must be in the same context as
104/// the decl that we want to add.
105void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
106 NamedDecl *Shadow) {
107 assert(LookupContext(D) == LookupContext(Shadow) &&
108 "Decl and Shadow not in same context!");
109
110 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
111 if (Shadow == *(I-1)) {
112 Decls.insert(I-1, D);
113 return;
114 }
115 }
116
117 assert(0 && "Shadow wasn't in scope chain!");
118}
119
120/// RemoveDecl - Remove the decl from the scope chain.
121/// The decl must already be part of the decl chain.
122void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
123 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
124 if (D == *(I-1)) {
125 Decls.erase(I-1);
126 return;
127 }
128 }
129
130 assert(0 && "Didn't find this decl on its identifier's chain!");
131}
132
133
134//===----------------------------------------------------------------------===//
135// IdentifierResolver Implementation
136//===----------------------------------------------------------------------===//
137
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000138IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
139 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
140}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000141IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000142 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000143}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000144
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000145/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
146/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
147/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000148bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregor44b43212008-12-11 16:49:14 +0000149 ASTContext &Context, Scope *S) const {
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000150 if (Ctx->isFunctionOrMethod()) {
151 if (S->isDeclScope(D))
152 return true;
153 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000154 // C++ 3.3.2p3:
155 // The name declared in a catch exception-declaration is local to the
156 // handler and shall not be redeclared in the outermost block of the
157 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000158 // C++ 3.3.2p4:
159 // Names declared in the for-init-statement, and in the condition of if,
160 // while, for, and switch statements are local to the if, while, for, or
161 // switch statement (including the controlled statement), and shall not be
162 // redeclared in a subsequent condition of that statement nor in the
163 // outermost block (or, for the if statement, any of the outermost blocks)
164 // of the controlled statement.
165 //
166 assert(S->getParent() && "No TUScope?");
167 if (S->getParent()->getFlags() & Scope::ControlScope)
168 return S->getParent()->isDeclScope(D);
169 }
170 return false;
171 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000172
Douglas Gregor44b43212008-12-11 16:49:14 +0000173 return LookupContext(D) == LookupContext(Ctx->getPrimaryContext(Context));
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000174}
175
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000176/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000177void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000178 DeclarationName Name = D->getDeclName();
179 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000180
181 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000182 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000183 return;
184 }
185
186 IdDeclInfo *IDI;
187
188 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000189 Name.setFETokenInfo(NULL);
190 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000191 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
192 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000193 } else
194 IDI = toIdDeclInfo(Ptr);
195
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000196 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000197}
198
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000199/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000200/// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000201/// encountered before the 'D' decl.
202void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000203 assert(D->getDeclName() == Shadow->getDeclName() && "Different ids!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000204 assert(LookupContext(D) == LookupContext(Shadow) && "Different context!");
205
Douglas Gregor2def4832008-11-17 20:34:05 +0000206 DeclarationName Name = D->getDeclName();
207 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000208 assert(Ptr && "No decl from Ptr ?");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000209
210 IdDeclInfo *IDI;
211
212 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000213 Name.setFETokenInfo(NULL);
214 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000215 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
216 assert(PrevD == Shadow && "Invalid shadow decl ?");
217 IDI->AddDecl(D);
218 IDI->AddDecl(PrevD);
219 return;
220 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000221
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000222 IDI = toIdDeclInfo(Ptr);
223 IDI->AddShadowed(D, Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000224}
225
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000226/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000227/// The decl must already be part of the decl chain.
228void IdentifierResolver::RemoveDecl(NamedDecl *D) {
229 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000230 DeclarationName Name = D->getDeclName();
231 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000232
233 assert(Ptr && "Didn't find this decl on its identifier's chain!");
234
235 if (isDeclPtr(Ptr)) {
236 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000237 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000238 return;
239 }
240
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000241 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000242}
243
Douglas Gregor2def4832008-11-17 20:34:05 +0000244/// begin - Returns an iterator for decls with name 'Name', starting at
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000245/// declaration context 'Ctx'. If 'LookInParentCtx' is true, it will walk the
246/// decls of parent declaration contexts too.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000247IdentifierResolver::iterator
Douglas Gregor2def4832008-11-17 20:34:05 +0000248IdentifierResolver::begin(DeclarationName Name, const DeclContext *Ctx,
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000249 bool LookInParentCtx) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000250 assert(Ctx && "null param passed");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000251
Douglas Gregor2def4832008-11-17 20:34:05 +0000252 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000253 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000254
255 LookupContext LC(Ctx);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000256
257 if (isDeclPtr(Ptr)) {
258 NamedDecl *D = static_cast<NamedDecl*>(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000259 LookupContext DC(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000260
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000261 if (( LookInParentCtx && LC.isEqOrContainedBy(DC)) ||
262 (!LookInParentCtx && LC == DC))
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000263 return iterator(D);
264 else
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000265 return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000266 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000267
Chris Lattnera2f42b12008-04-11 07:06:57 +0000268 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000269
270 IdDeclInfo::DeclsTy::iterator I;
271 if (LookInParentCtx)
Zhongxing Xuebed7972008-12-05 01:57:57 +0000272 I = IDI->FindDeclVisibleInContext(LC);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000273 else {
274 for (I = IDI->decls_end(); I != IDI->decls_begin(); --I)
275 if (LookupContext(*(I-1)) == LC)
276 break;
277 }
278
279 if (I != IDI->decls_begin())
280 return iterator(I-1, LookInParentCtx);
281 else // No decls found.
282 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000283}
284
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000285/// PreIncIter - Do a preincrement when 'Ptr' is a BaseIter.
286void IdentifierResolver::iterator::PreIncIter() {
287 NamedDecl *D = **this;
288 LookupContext Ctx(D);
Douglas Gregor2def4832008-11-17 20:34:05 +0000289 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000290 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
291 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000292
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000293 BaseIter I = getIterator();
294 if (LookInParentCtx())
Zhongxing Xuebed7972008-12-05 01:57:57 +0000295 I = Info->FindDeclVisibleInContext(Ctx, I);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000296 else {
297 if (I != Info->decls_begin() && LookupContext(*(I-1)) != Ctx) {
298 // The next decl is in different declaration context.
299 // Skip remaining decls and set the iterator to the end.
300 I = Info->decls_begin();
301 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000302 }
303
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000304 if (I != Info->decls_begin())
305 *this = iterator(I-1, LookInParentCtx());
306 else // No more decls.
307 *this = end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000308}
309
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000310
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000311//===----------------------------------------------------------------------===//
312// IdDeclInfoMap Implementation
313//===----------------------------------------------------------------------===//
314
Douglas Gregor2def4832008-11-17 20:34:05 +0000315/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000316/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000317IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000318IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
319 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000320
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000321 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000322
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000323 if (CurIndex == VECTOR_SIZE) {
324 // Add a IdDeclInfo vector 'pool'
Argyrios Kyrtzidis72e62b02008-04-12 12:38:58 +0000325 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000326 // Fill the vector
327 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000328 CurIndex = 0;
329 }
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000330 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000331 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000332 reinterpret_cast<uintptr_t>(IDI) | 0x1)
333 );
334 ++CurIndex;
335 return *IDI;
336}