blob: 50413c3d95897582a12e9c123a449a52a30a41d3 [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
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/IdentifierResolver.h"
John McCall7cd088e2010-08-24 07:21:54 +000016#include "clang/Sema/Scope.h"
17#include "clang/AST/Decl.h"
Douglas Gregoreee242f2011-10-27 09:33:13 +000018#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000019#include "clang/Basic/LangOptions.h"
Douglas Gregoreee242f2011-10-27 09:33:13 +000020#include "clang/Lex/ExternalPreprocessorSource.h"
21#include "clang/Lex/Preprocessor.h"
Chris Lattnera2f42b12008-04-11 07:06:57 +000022
23using namespace clang;
24
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000025//===----------------------------------------------------------------------===//
26// IdDeclInfoMap class
27//===----------------------------------------------------------------------===//
Chris Lattnera2f42b12008-04-11 07:06:57 +000028
Douglas Gregor2def4832008-11-17 20:34:05 +000029/// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000030/// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
Chris Lattnera2f42b12008-04-11 07:06:57 +000031/// individual IdDeclInfo to heap.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000032class IdentifierResolver::IdDeclInfoMap {
John McCalleeb1cb42010-02-15 19:38:00 +000033 static const unsigned int POOL_SIZE = 512;
34
35 /// We use our own linked-list implementation because it is sadly
36 /// impossible to add something to a pre-C++0x STL container without
37 /// a completely unnecessary copy.
38 struct IdDeclInfoPool {
39 IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
40
41 IdDeclInfoPool *Next;
42 IdDeclInfo Pool[POOL_SIZE];
43 };
44
45 IdDeclInfoPool *CurPool;
Chris Lattnera2f42b12008-04-11 07:06:57 +000046 unsigned int CurIndex;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Chris Lattnera2f42b12008-04-11 07:06:57 +000048public:
John McCalleeb1cb42010-02-15 19:38:00 +000049 IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE) {}
50
51 ~IdDeclInfoMap() {
52 IdDeclInfoPool *Cur = CurPool;
53 while (IdDeclInfoPool *P = Cur) {
54 Cur = Cur->Next;
55 delete P;
56 }
57 }
Chris Lattnera2f42b12008-04-11 07:06:57 +000058
Douglas Gregor2def4832008-11-17 20:34:05 +000059 /// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +000060 /// It creates a new IdDeclInfo if one was not created before for this id.
Douglas Gregor2def4832008-11-17 20:34:05 +000061 IdDeclInfo &operator[](DeclarationName Name);
Chris Lattnera2f42b12008-04-11 07:06:57 +000062};
63
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000064
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000065//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000066// IdDeclInfo Implementation
67//===----------------------------------------------------------------------===//
68
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000069/// RemoveDecl - Remove the decl from the scope chain.
70/// The decl must already be part of the decl chain.
71void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
72 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
73 if (D == *(I-1)) {
74 Decls.erase(I-1);
75 return;
76 }
77 }
78
David Blaikieb219cfc2011-09-23 05:06:16 +000079 llvm_unreachable("Didn't find this decl on its identifier's chain!");
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000080}
81
Mike Stump1eb44332009-09-09 15:08:12 +000082bool
Douglas Gregor63935192009-03-02 00:19:53 +000083IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
84 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
85 if (Old == *(I-1)) {
86 *(I - 1) = New;
87 return true;
88 }
89 }
90
91 return false;
92}
93
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000094
95//===----------------------------------------------------------------------===//
96// IdentifierResolver Implementation
97//===----------------------------------------------------------------------===//
98
Douglas Gregoreee242f2011-10-27 09:33:13 +000099IdentifierResolver::IdentifierResolver(Preprocessor &PP)
David Blaikie4e4d0842012-03-11 07:00:24 +0000100 : LangOpt(PP.getLangOpts()), PP(PP),
Douglas Gregoreee242f2011-10-27 09:33:13 +0000101 IdDeclInfos(new IdDeclInfoMap) {
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000102}
Douglas Gregoreee242f2011-10-27 09:33:13 +0000103
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000104IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000105 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000106}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000107
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000108/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
109/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
110/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000111bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregorcc209452011-03-07 16:54:27 +0000112 ASTContext &Context, Scope *S,
113 bool ExplicitInstantiationOrSpecialization) const {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000114 Ctx = Ctx->getRedeclContext();
Douglas Gregor074149e2009-01-05 19:45:36 +0000115
James Molloy16f1f712012-02-29 10:24:19 +0000116 if (Ctx->isFunctionOrMethod() || S->isFunctionPrototypeScope()) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000117 // Ignore the scopes associated within transparent declaration contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000118 while (S->getEntity() &&
Douglas Gregor074149e2009-01-05 19:45:36 +0000119 ((DeclContext *)S->getEntity())->isTransparentContext())
120 S = S->getParent();
121
John McCalld226f652010-08-21 09:40:31 +0000122 if (S->isDeclScope(D))
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000123 return true;
124 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000125 // C++ 3.3.2p3:
126 // The name declared in a catch exception-declaration is local to the
127 // handler and shall not be redeclared in the outermost block of the
128 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000129 // C++ 3.3.2p4:
130 // Names declared in the for-init-statement, and in the condition of if,
131 // while, for, and switch statements are local to the if, while, for, or
132 // switch statement (including the controlled statement), and shall not be
133 // redeclared in a subsequent condition of that statement nor in the
134 // outermost block (or, for the if statement, any of the outermost blocks)
135 // of the controlled statement.
136 //
137 assert(S->getParent() && "No TUScope?");
David Blaikiec4027c82012-11-10 01:04:23 +0000138 if (S->getFlags() & Scope::FnTryScope)
John McCalld226f652010-08-21 09:40:31 +0000139 return S->getParent()->isDeclScope(D);
David Blaikiec4027c82012-11-10 01:04:23 +0000140 if (S->getParent()->getFlags() & Scope::ControlScope) {
141 if (S->getParent()->getFlags() & Scope::FnCatchScope)
142 S = S->getParent();
143 return S->getParent()->isDeclScope(D);
144 }
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000145 }
146 return false;
147 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000148
Douglas Gregorcc209452011-03-07 16:54:27 +0000149 DeclContext *DCtx = D->getDeclContext()->getRedeclContext();
150 return ExplicitInstantiationOrSpecialization
151 ? Ctx->InEnclosingNamespaceSetOf(DCtx)
152 : Ctx->Equals(DCtx);
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000153}
154
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000155/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000156void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000157 DeclarationName Name = D->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000158 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregoreee242f2011-10-27 09:33:13 +0000159 updatingIdentifier(*II);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000160
Douglas Gregor2def4832008-11-17 20:34:05 +0000161 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000162
163 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000164 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000165 return;
166 }
167
168 IdDeclInfo *IDI;
169
170 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000171 Name.setFETokenInfo(NULL);
172 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000173 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
174 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000175 } else
176 IDI = toIdDeclInfo(Ptr);
177
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000178 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000179}
180
Douglas Gregor250e7a72011-03-16 16:39:03 +0000181void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
182 DeclarationName Name = D->getDeclName();
Douglas Gregoreee242f2011-10-27 09:33:13 +0000183 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
184 updatingIdentifier(*II);
185
Douglas Gregor250e7a72011-03-16 16:39:03 +0000186 void *Ptr = Name.getFETokenInfo<void>();
187
Douglas Gregorfa7b8ce2011-03-24 10:35:39 +0000188 if (!Ptr) {
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000189 AddDecl(D);
190 return;
191 }
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000192
Douglas Gregorfa7b8ce2011-03-24 10:35:39 +0000193 if (isDeclPtr(Ptr)) {
194 // We only have a single declaration: insert before or after it,
195 // as appropriate.
196 if (Pos == iterator()) {
197 // Add the new declaration before the existing declaration.
198 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
199 RemoveDecl(PrevD);
200 AddDecl(D);
201 AddDecl(PrevD);
202 } else {
203 // Add new declaration after the existing declaration.
204 AddDecl(D);
205 }
206
207 return;
208 }
209
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000210 // General case: insert the declaration at the appropriate point in the
211 // list, which already has at least two elements.
212 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Douglas Gregor250e7a72011-03-16 16:39:03 +0000213 if (Pos.isIterator()) {
214 IDI->InsertDecl(Pos.getIterator() + 1, D);
215 } else
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000216 IDI->InsertDecl(IDI->decls_begin(), D);
217}
218
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000219/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000220/// The decl must already be part of the decl chain.
221void IdentifierResolver::RemoveDecl(NamedDecl *D) {
222 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000223 DeclarationName Name = D->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000224 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregoreee242f2011-10-27 09:33:13 +0000225 updatingIdentifier(*II);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000226
Douglas Gregor2def4832008-11-17 20:34:05 +0000227 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000228
229 assert(Ptr && "Didn't find this decl on its identifier's chain!");
230
231 if (isDeclPtr(Ptr)) {
232 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000233 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000234 return;
235 }
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000237 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000238}
239
Douglas Gregor63935192009-03-02 00:19:53 +0000240bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
Mike Stump1eb44332009-09-09 15:08:12 +0000241 assert(Old->getDeclName() == New->getDeclName() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000242 "Cannot replace a decl with another decl of a different name");
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Douglas Gregor63935192009-03-02 00:19:53 +0000244 DeclarationName Name = Old->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000245 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregoreee242f2011-10-27 09:33:13 +0000246 updatingIdentifier(*II);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000247
Douglas Gregor63935192009-03-02 00:19:53 +0000248 void *Ptr = Name.getFETokenInfo<void>();
249
250 if (!Ptr)
251 return false;
252
253 if (isDeclPtr(Ptr)) {
254 if (Ptr == Old) {
255 Name.setFETokenInfo(New);
256 return true;
257 }
258 return false;
259 }
260
Mike Stump1eb44332009-09-09 15:08:12 +0000261 return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
Douglas Gregor63935192009-03-02 00:19:53 +0000262}
263
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000264/// begin - Returns an iterator for decls with name 'Name'.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000265IdentifierResolver::iterator
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000266IdentifierResolver::begin(DeclarationName Name) {
Douglas Gregoreee242f2011-10-27 09:33:13 +0000267 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
268 readingIdentifier(*II);
269
Douglas Gregor2def4832008-11-17 20:34:05 +0000270 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000271 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000272
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000273 if (isDeclPtr(Ptr))
274 return iterator(static_cast<NamedDecl*>(Ptr));
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000275
Chris Lattnera2f42b12008-04-11 07:06:57 +0000276 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000277
Douglas Gregor074149e2009-01-05 19:45:36 +0000278 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000279 if (I != IDI->decls_begin())
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000280 return iterator(I-1);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000281 // No decls found.
282 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000283}
284
Douglas Gregoreee242f2011-10-27 09:33:13 +0000285namespace {
286 enum DeclMatchKind {
287 DMK_Different,
288 DMK_Replace,
289 DMK_Ignore
290 };
291}
Douglas Gregor668c1a42009-04-21 22:25:48 +0000292
Douglas Gregoreee242f2011-10-27 09:33:13 +0000293/// \brief Compare two declarations to see whether they are different or,
294/// if they are the same, whether the new declaration should replace the
295/// existing declaration.
296static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) {
297 // If the declarations are identical, ignore the new one.
298 if (Existing == New)
299 return DMK_Ignore;
300
301 // If the declarations have different kinds, they're obviously different.
302 if (Existing->getKind() != New->getKind())
303 return DMK_Different;
304
305 // If the declarations are redeclarations of each other, keep the newest one.
306 if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) {
307 // If the existing declaration is somewhere in the previous declaration
308 // chain of the new declaration, then prefer the new declaration.
309 for (Decl::redecl_iterator RD = New->redecls_begin(),
310 RDEnd = New->redecls_end();
311 RD != RDEnd; ++RD) {
David Blaikie581deb32012-06-06 20:45:41 +0000312 if (*RD == Existing)
Douglas Gregoreee242f2011-10-27 09:33:13 +0000313 return DMK_Replace;
314
315 if (RD->isCanonicalDecl())
316 break;
317 }
318
319 return DMK_Ignore;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000320 }
Douglas Gregoreee242f2011-10-27 09:33:13 +0000321
Douglas Gregoreee242f2011-10-27 09:33:13 +0000322 return DMK_Different;
323}
Douglas Gregor668c1a42009-04-21 22:25:48 +0000324
Douglas Gregoreee242f2011-10-27 09:33:13 +0000325bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){
326 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000327 readingIdentifier(*II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000328
329 void *Ptr = Name.getFETokenInfo<void>();
330
331 if (!Ptr) {
332 Name.setFETokenInfo(D);
333 return true;
334 }
335
Douglas Gregor668c1a42009-04-21 22:25:48 +0000336 IdDeclInfo *IDI;
Douglas Gregoreee242f2011-10-27 09:33:13 +0000337
Douglas Gregor668c1a42009-04-21 22:25:48 +0000338 if (isDeclPtr(Ptr)) {
Douglas Gregor668c1a42009-04-21 22:25:48 +0000339 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000340
341 switch (compareDeclarations(PrevD, D)) {
342 case DMK_Different:
343 break;
344
345 case DMK_Ignore:
346 return false;
347
348 case DMK_Replace:
349 Name.setFETokenInfo(D);
350 return true;
351 }
352
353 Name.setFETokenInfo(NULL);
354 IDI = &(*IdDeclInfos)[Name];
355
356 // If the existing declaration is not visible in translation unit scope,
357 // then add the new top-level declaration first.
358 if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
359 IDI->AddDecl(D);
360 IDI->AddDecl(PrevD);
361 } else {
362 IDI->AddDecl(PrevD);
363 IDI->AddDecl(D);
364 }
365 return true;
366 }
367
368 IDI = toIdDeclInfo(Ptr);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000369
Douglas Gregoreee242f2011-10-27 09:33:13 +0000370 // See whether this declaration is identical to any existing declarations.
371 // If not, find the right place to insert it.
372 for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(),
373 IEnd = IDI->decls_end();
374 I != IEnd; ++I) {
375
376 switch (compareDeclarations(*I, D)) {
377 case DMK_Different:
378 break;
379
380 case DMK_Ignore:
381 return false;
382
383 case DMK_Replace:
384 *I = D;
385 return true;
386 }
387
388 if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
389 // We've found a declaration that is not visible from the translation
390 // unit (it's in an inner scope). Insert our declaration here.
391 IDI->InsertDecl(I, D);
392 return true;
393 }
394 }
395
396 // Add the declaration to the end.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000397 IDI->AddDecl(D);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000398 return true;
399}
400
401void IdentifierResolver::readingIdentifier(IdentifierInfo &II) {
402 if (II.isOutOfDate())
403 PP.getExternalSource()->updateOutOfDateIdentifier(II);
404}
405
406void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) {
407 if (II.isOutOfDate())
408 PP.getExternalSource()->updateOutOfDateIdentifier(II);
409
410 if (II.isFromAST())
411 II.setChangedSinceDeserialization();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000412}
413
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000414//===----------------------------------------------------------------------===//
415// IdDeclInfoMap Implementation
416//===----------------------------------------------------------------------===//
417
Douglas Gregor2def4832008-11-17 20:34:05 +0000418/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000419/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000420IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000421IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
422 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000423
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000424 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000425
John McCalleeb1cb42010-02-15 19:38:00 +0000426 if (CurIndex == POOL_SIZE) {
427 CurPool = new IdDeclInfoPool(CurPool);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000428 CurIndex = 0;
429 }
John McCalleeb1cb42010-02-15 19:38:00 +0000430 IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000431 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000432 reinterpret_cast<uintptr_t>(IDI) | 0x1)
433 );
434 ++CurIndex;
435 return *IDI;
436}
John McCall7cd088e2010-08-24 07:21:54 +0000437
438void IdentifierResolver::iterator::incrementSlowCase() {
439 NamedDecl *D = **this;
440 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
441 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
442 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
443
444 BaseIter I = getIterator();
445 if (I != Info->decls_begin())
446 *this = iterator(I-1);
447 else // No more decls.
448 *this = iterator();
449}