blob: 6f5ddca20b12409a1eb3b8dcdb65050db073fff3 [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/AST/Decl.h"
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000017#include "clang/Basic/LangOptions.h"
Douglas Gregoreee242f2011-10-27 09:33:13 +000018#include "clang/Lex/ExternalPreprocessorSource.h"
19#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/Sema/Scope.h"
Chris Lattnera2f42b12008-04-11 07:06:57 +000021
22using namespace clang;
23
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000024//===----------------------------------------------------------------------===//
25// IdDeclInfoMap class
26//===----------------------------------------------------------------------===//
Chris Lattnera2f42b12008-04-11 07:06:57 +000027
Douglas Gregor2def4832008-11-17 20:34:05 +000028/// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000029/// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
Chris Lattnera2f42b12008-04-11 07:06:57 +000030/// individual IdDeclInfo to heap.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000031class IdentifierResolver::IdDeclInfoMap {
John McCalleeb1cb42010-02-15 19:38:00 +000032 static const unsigned int POOL_SIZE = 512;
33
34 /// We use our own linked-list implementation because it is sadly
35 /// impossible to add something to a pre-C++0x STL container without
36 /// a completely unnecessary copy.
37 struct IdDeclInfoPool {
38 IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
39
40 IdDeclInfoPool *Next;
41 IdDeclInfo Pool[POOL_SIZE];
42 };
43
44 IdDeclInfoPool *CurPool;
Chris Lattnera2f42b12008-04-11 07:06:57 +000045 unsigned int CurIndex;
Mike Stump1eb44332009-09-09 15:08:12 +000046
Chris Lattnera2f42b12008-04-11 07:06:57 +000047public:
John McCalleeb1cb42010-02-15 19:38:00 +000048 IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE) {}
49
50 ~IdDeclInfoMap() {
51 IdDeclInfoPool *Cur = CurPool;
52 while (IdDeclInfoPool *P = Cur) {
53 Cur = Cur->Next;
54 delete P;
55 }
56 }
Chris Lattnera2f42b12008-04-11 07:06:57 +000057
Douglas Gregor2def4832008-11-17 20:34:05 +000058 /// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +000059 /// It creates a new IdDeclInfo if one was not created before for this id.
Douglas Gregor2def4832008-11-17 20:34:05 +000060 IdDeclInfo &operator[](DeclarationName Name);
Chris Lattnera2f42b12008-04-11 07:06:57 +000061};
62
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000063
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000064//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000065// IdDeclInfo Implementation
66//===----------------------------------------------------------------------===//
67
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000068/// RemoveDecl - Remove the decl from the scope chain.
69/// The decl must already be part of the decl chain.
70void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
71 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
72 if (D == *(I-1)) {
73 Decls.erase(I-1);
74 return;
75 }
76 }
77
David Blaikieb219cfc2011-09-23 05:06:16 +000078 llvm_unreachable("Didn't find this decl on its identifier's chain!");
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000079}
80
Mike Stump1eb44332009-09-09 15:08:12 +000081bool
Douglas Gregor63935192009-03-02 00:19:53 +000082IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
83 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
84 if (Old == *(I-1)) {
85 *(I - 1) = New;
86 return true;
87 }
88 }
89
90 return false;
91}
92
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000093
94//===----------------------------------------------------------------------===//
95// IdentifierResolver Implementation
96//===----------------------------------------------------------------------===//
97
Douglas Gregoreee242f2011-10-27 09:33:13 +000098IdentifierResolver::IdentifierResolver(Preprocessor &PP)
David Blaikie4e4d0842012-03-11 07:00:24 +000099 : LangOpt(PP.getLangOpts()), PP(PP),
Douglas Gregoreee242f2011-10-27 09:33:13 +0000100 IdDeclInfos(new IdDeclInfoMap) {
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000101}
Douglas Gregoreee242f2011-10-27 09:33:13 +0000102
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000103IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000104 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000105}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000106
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000107/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
108/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
109/// true if 'D' belongs to the given declaration context.
Nico Weber355a1662012-12-17 03:51:09 +0000110bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
Douglas Gregorcc209452011-03-07 16:54:27 +0000111 bool ExplicitInstantiationOrSpecialization) const {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000112 Ctx = Ctx->getRedeclContext();
Douglas Gregor074149e2009-01-05 19:45:36 +0000113
James Molloy16f1f712012-02-29 10:24:19 +0000114 if (Ctx->isFunctionOrMethod() || S->isFunctionPrototypeScope()) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000115 // Ignore the scopes associated within transparent declaration contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000116 while (S->getEntity() &&
Douglas Gregor074149e2009-01-05 19:45:36 +0000117 ((DeclContext *)S->getEntity())->isTransparentContext())
118 S = S->getParent();
119
John McCalld226f652010-08-21 09:40:31 +0000120 if (S->isDeclScope(D))
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000121 return true;
122 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000123 // C++ 3.3.2p3:
124 // The name declared in a catch exception-declaration is local to the
125 // handler and shall not be redeclared in the outermost block of the
126 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000127 // C++ 3.3.2p4:
128 // Names declared in the for-init-statement, and in the condition of if,
129 // while, for, and switch statements are local to the if, while, for, or
130 // switch statement (including the controlled statement), and shall not be
131 // redeclared in a subsequent condition of that statement nor in the
132 // outermost block (or, for the if statement, any of the outermost blocks)
133 // of the controlled statement.
134 //
135 assert(S->getParent() && "No TUScope?");
David Blaikiec4027c82012-11-10 01:04:23 +0000136 if (S->getParent()->getFlags() & Scope::ControlScope) {
David Blaikie3a9fefe2012-11-12 22:25:41 +0000137 S = S->getParent();
138 if (S->isDeclScope(D))
139 return true;
David Blaikiec4027c82012-11-10 01:04:23 +0000140 }
David Blaikie3a9fefe2012-11-12 22:25:41 +0000141 if (S->getFlags() & Scope::FnTryCatchScope)
142 return S->getParent()->isDeclScope(D);
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000143 }
144 return false;
145 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000146
Douglas Gregorcc209452011-03-07 16:54:27 +0000147 DeclContext *DCtx = D->getDeclContext()->getRedeclContext();
148 return ExplicitInstantiationOrSpecialization
149 ? Ctx->InEnclosingNamespaceSetOf(DCtx)
150 : Ctx->Equals(DCtx);
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000151}
152
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000153/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000154void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000155 DeclarationName Name = D->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000156 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregoreee242f2011-10-27 09:33:13 +0000157 updatingIdentifier(*II);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000158
Douglas Gregor2def4832008-11-17 20:34:05 +0000159 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000160
161 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000162 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000163 return;
164 }
165
166 IdDeclInfo *IDI;
167
168 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000169 Name.setFETokenInfo(NULL);
170 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000171 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
172 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000173 } else
174 IDI = toIdDeclInfo(Ptr);
175
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000176 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000177}
178
Douglas Gregor250e7a72011-03-16 16:39:03 +0000179void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
180 DeclarationName Name = D->getDeclName();
Douglas Gregoreee242f2011-10-27 09:33:13 +0000181 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
182 updatingIdentifier(*II);
183
Douglas Gregor250e7a72011-03-16 16:39:03 +0000184 void *Ptr = Name.getFETokenInfo<void>();
185
Douglas Gregorfa7b8ce2011-03-24 10:35:39 +0000186 if (!Ptr) {
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000187 AddDecl(D);
188 return;
189 }
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000190
Douglas Gregorfa7b8ce2011-03-24 10:35:39 +0000191 if (isDeclPtr(Ptr)) {
192 // We only have a single declaration: insert before or after it,
193 // as appropriate.
194 if (Pos == iterator()) {
195 // Add the new declaration before the existing declaration.
196 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
197 RemoveDecl(PrevD);
198 AddDecl(D);
199 AddDecl(PrevD);
200 } else {
201 // Add new declaration after the existing declaration.
202 AddDecl(D);
203 }
204
205 return;
206 }
207
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000208 // General case: insert the declaration at the appropriate point in the
209 // list, which already has at least two elements.
210 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Douglas Gregor250e7a72011-03-16 16:39:03 +0000211 if (Pos.isIterator()) {
212 IDI->InsertDecl(Pos.getIterator() + 1, D);
213 } else
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000214 IDI->InsertDecl(IDI->decls_begin(), D);
215}
216
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000217/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000218/// The decl must already be part of the decl chain.
219void IdentifierResolver::RemoveDecl(NamedDecl *D) {
220 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000221 DeclarationName Name = D->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000222 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregoreee242f2011-10-27 09:33:13 +0000223 updatingIdentifier(*II);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000224
Douglas Gregor2def4832008-11-17 20:34:05 +0000225 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000226
227 assert(Ptr && "Didn't find this decl on its identifier's chain!");
228
229 if (isDeclPtr(Ptr)) {
230 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000231 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000232 return;
233 }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000235 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000236}
237
Douglas Gregor63935192009-03-02 00:19:53 +0000238bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
Mike Stump1eb44332009-09-09 15:08:12 +0000239 assert(Old->getDeclName() == New->getDeclName() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000240 "Cannot replace a decl with another decl of a different name");
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Douglas Gregor63935192009-03-02 00:19:53 +0000242 DeclarationName Name = Old->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000243 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregoreee242f2011-10-27 09:33:13 +0000244 updatingIdentifier(*II);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000245
Douglas Gregor63935192009-03-02 00:19:53 +0000246 void *Ptr = Name.getFETokenInfo<void>();
247
248 if (!Ptr)
249 return false;
250
251 if (isDeclPtr(Ptr)) {
252 if (Ptr == Old) {
253 Name.setFETokenInfo(New);
254 return true;
255 }
256 return false;
257 }
258
Mike Stump1eb44332009-09-09 15:08:12 +0000259 return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
Douglas Gregor63935192009-03-02 00:19:53 +0000260}
261
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000262/// begin - Returns an iterator for decls with name 'Name'.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000263IdentifierResolver::iterator
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000264IdentifierResolver::begin(DeclarationName Name) {
Douglas Gregoreee242f2011-10-27 09:33:13 +0000265 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
266 readingIdentifier(*II);
267
Douglas Gregor2def4832008-11-17 20:34:05 +0000268 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000269 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000270
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000271 if (isDeclPtr(Ptr))
272 return iterator(static_cast<NamedDecl*>(Ptr));
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000273
Chris Lattnera2f42b12008-04-11 07:06:57 +0000274 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000275
Douglas Gregor074149e2009-01-05 19:45:36 +0000276 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000277 if (I != IDI->decls_begin())
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000278 return iterator(I-1);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000279 // No decls found.
280 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000281}
282
Douglas Gregoreee242f2011-10-27 09:33:13 +0000283namespace {
284 enum DeclMatchKind {
285 DMK_Different,
286 DMK_Replace,
287 DMK_Ignore
288 };
289}
Douglas Gregor668c1a42009-04-21 22:25:48 +0000290
Douglas Gregoreee242f2011-10-27 09:33:13 +0000291/// \brief Compare two declarations to see whether they are different or,
292/// if they are the same, whether the new declaration should replace the
293/// existing declaration.
294static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) {
295 // If the declarations are identical, ignore the new one.
296 if (Existing == New)
297 return DMK_Ignore;
298
299 // If the declarations have different kinds, they're obviously different.
300 if (Existing->getKind() != New->getKind())
301 return DMK_Different;
302
303 // If the declarations are redeclarations of each other, keep the newest one.
304 if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) {
305 // If the existing declaration is somewhere in the previous declaration
306 // chain of the new declaration, then prefer the new declaration.
307 for (Decl::redecl_iterator RD = New->redecls_begin(),
308 RDEnd = New->redecls_end();
309 RD != RDEnd; ++RD) {
David Blaikie581deb32012-06-06 20:45:41 +0000310 if (*RD == Existing)
Douglas Gregoreee242f2011-10-27 09:33:13 +0000311 return DMK_Replace;
312
313 if (RD->isCanonicalDecl())
314 break;
315 }
316
317 return DMK_Ignore;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000318 }
Douglas Gregoreee242f2011-10-27 09:33:13 +0000319
Douglas Gregoreee242f2011-10-27 09:33:13 +0000320 return DMK_Different;
321}
Douglas Gregor668c1a42009-04-21 22:25:48 +0000322
Douglas Gregoreee242f2011-10-27 09:33:13 +0000323bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){
324 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000325 readingIdentifier(*II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000326
327 void *Ptr = Name.getFETokenInfo<void>();
328
329 if (!Ptr) {
330 Name.setFETokenInfo(D);
331 return true;
332 }
333
Douglas Gregor668c1a42009-04-21 22:25:48 +0000334 IdDeclInfo *IDI;
Douglas Gregoreee242f2011-10-27 09:33:13 +0000335
Douglas Gregor668c1a42009-04-21 22:25:48 +0000336 if (isDeclPtr(Ptr)) {
Douglas Gregor668c1a42009-04-21 22:25:48 +0000337 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000338
339 switch (compareDeclarations(PrevD, D)) {
340 case DMK_Different:
341 break;
342
343 case DMK_Ignore:
344 return false;
345
346 case DMK_Replace:
347 Name.setFETokenInfo(D);
348 return true;
349 }
350
351 Name.setFETokenInfo(NULL);
352 IDI = &(*IdDeclInfos)[Name];
353
354 // If the existing declaration is not visible in translation unit scope,
355 // then add the new top-level declaration first.
356 if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
357 IDI->AddDecl(D);
358 IDI->AddDecl(PrevD);
359 } else {
360 IDI->AddDecl(PrevD);
361 IDI->AddDecl(D);
362 }
363 return true;
364 }
365
366 IDI = toIdDeclInfo(Ptr);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000367
Douglas Gregoreee242f2011-10-27 09:33:13 +0000368 // See whether this declaration is identical to any existing declarations.
369 // If not, find the right place to insert it.
370 for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(),
371 IEnd = IDI->decls_end();
372 I != IEnd; ++I) {
373
374 switch (compareDeclarations(*I, D)) {
375 case DMK_Different:
376 break;
377
378 case DMK_Ignore:
379 return false;
380
381 case DMK_Replace:
382 *I = D;
383 return true;
384 }
385
386 if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
387 // We've found a declaration that is not visible from the translation
388 // unit (it's in an inner scope). Insert our declaration here.
389 IDI->InsertDecl(I, D);
390 return true;
391 }
392 }
393
394 // Add the declaration to the end.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000395 IDI->AddDecl(D);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000396 return true;
397}
398
399void IdentifierResolver::readingIdentifier(IdentifierInfo &II) {
400 if (II.isOutOfDate())
401 PP.getExternalSource()->updateOutOfDateIdentifier(II);
402}
403
404void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) {
405 if (II.isOutOfDate())
406 PP.getExternalSource()->updateOutOfDateIdentifier(II);
407
408 if (II.isFromAST())
409 II.setChangedSinceDeserialization();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000410}
411
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000412//===----------------------------------------------------------------------===//
413// IdDeclInfoMap Implementation
414//===----------------------------------------------------------------------===//
415
Douglas Gregor2def4832008-11-17 20:34:05 +0000416/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000417/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000418IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000419IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
420 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000421
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000422 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000423
John McCalleeb1cb42010-02-15 19:38:00 +0000424 if (CurIndex == POOL_SIZE) {
425 CurPool = new IdDeclInfoPool(CurPool);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000426 CurIndex = 0;
427 }
John McCalleeb1cb42010-02-15 19:38:00 +0000428 IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000429 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000430 reinterpret_cast<uintptr_t>(IDI) | 0x1)
431 );
432 ++CurIndex;
433 return *IDI;
434}
John McCall7cd088e2010-08-24 07:21:54 +0000435
436void IdentifierResolver::iterator::incrementSlowCase() {
437 NamedDecl *D = **this;
438 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
439 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
440 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
441
442 BaseIter I = getIterator();
443 if (I != Info->decls_begin())
444 *this = iterator(I-1);
445 else // No more decls.
446 *this = iterator();
447}