blob: 95420a316ad07b0969e53555d9431c4e0c76d425 [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"
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000018#include "clang/Basic/LangOptions.h"
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 {
John McCalleeb1cb42010-02-15 19:38:00 +000030 static const unsigned int POOL_SIZE = 512;
31
32 /// We use our own linked-list implementation because it is sadly
33 /// impossible to add something to a pre-C++0x STL container without
34 /// a completely unnecessary copy.
35 struct IdDeclInfoPool {
36 IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
37
38 IdDeclInfoPool *Next;
39 IdDeclInfo Pool[POOL_SIZE];
40 };
41
42 IdDeclInfoPool *CurPool;
Chris Lattnera2f42b12008-04-11 07:06:57 +000043 unsigned int CurIndex;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Chris Lattnera2f42b12008-04-11 07:06:57 +000045public:
John McCalleeb1cb42010-02-15 19:38:00 +000046 IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE) {}
47
48 ~IdDeclInfoMap() {
49 IdDeclInfoPool *Cur = CurPool;
50 while (IdDeclInfoPool *P = Cur) {
51 Cur = Cur->Next;
52 delete P;
53 }
54 }
Chris Lattnera2f42b12008-04-11 07:06:57 +000055
Douglas Gregor2def4832008-11-17 20:34:05 +000056 /// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +000057 /// It creates a new IdDeclInfo if one was not created before for this id.
Douglas Gregor2def4832008-11-17 20:34:05 +000058 IdDeclInfo &operator[](DeclarationName Name);
Chris Lattnera2f42b12008-04-11 07:06:57 +000059};
60
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000061
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000062//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000063// IdDeclInfo Implementation
64//===----------------------------------------------------------------------===//
65
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000066/// RemoveDecl - Remove the decl from the scope chain.
67/// The decl must already be part of the decl chain.
68void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
69 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
70 if (D == *(I-1)) {
71 Decls.erase(I-1);
72 return;
73 }
74 }
75
76 assert(0 && "Didn't find this decl on its identifier's chain!");
77}
78
Mike Stump1eb44332009-09-09 15:08:12 +000079bool
Douglas Gregor63935192009-03-02 00:19:53 +000080IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
81 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
82 if (Old == *(I-1)) {
83 *(I - 1) = New;
84 return true;
85 }
86 }
87
88 return false;
89}
90
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000091
92//===----------------------------------------------------------------------===//
93// IdentifierResolver Implementation
94//===----------------------------------------------------------------------===//
95
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +000096IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
97 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
98}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000099IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000100 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000101}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000102
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000103/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
104/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
105/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000106bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregorcc209452011-03-07 16:54:27 +0000107 ASTContext &Context, Scope *S,
108 bool ExplicitInstantiationOrSpecialization) const {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000109 Ctx = Ctx->getRedeclContext();
Douglas Gregor074149e2009-01-05 19:45:36 +0000110
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000111 if (Ctx->isFunctionOrMethod()) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000112 // Ignore the scopes associated within transparent declaration contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000113 while (S->getEntity() &&
Douglas Gregor074149e2009-01-05 19:45:36 +0000114 ((DeclContext *)S->getEntity())->isTransparentContext())
115 S = S->getParent();
116
John McCalld226f652010-08-21 09:40:31 +0000117 if (S->isDeclScope(D))
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000118 return true;
119 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000120 // C++ 3.3.2p3:
121 // The name declared in a catch exception-declaration is local to the
122 // handler and shall not be redeclared in the outermost block of the
123 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000124 // C++ 3.3.2p4:
125 // Names declared in the for-init-statement, and in the condition of if,
126 // while, for, and switch statements are local to the if, while, for, or
127 // switch statement (including the controlled statement), and shall not be
128 // redeclared in a subsequent condition of that statement nor in the
129 // outermost block (or, for the if statement, any of the outermost blocks)
130 // of the controlled statement.
131 //
132 assert(S->getParent() && "No TUScope?");
133 if (S->getParent()->getFlags() & Scope::ControlScope)
John McCalld226f652010-08-21 09:40:31 +0000134 return S->getParent()->isDeclScope(D);
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000135 }
136 return false;
137 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000138
Douglas Gregorcc209452011-03-07 16:54:27 +0000139 DeclContext *DCtx = D->getDeclContext()->getRedeclContext();
140 return ExplicitInstantiationOrSpecialization
141 ? Ctx->InEnclosingNamespaceSetOf(DCtx)
142 : Ctx->Equals(DCtx);
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000143}
144
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000145/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000146void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000147 DeclarationName Name = D->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000148 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000149 II->setIsFromAST(false);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000150
Douglas Gregor2def4832008-11-17 20:34:05 +0000151 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000152
153 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000154 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000155 return;
156 }
157
158 IdDeclInfo *IDI;
159
160 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000161 Name.setFETokenInfo(NULL);
162 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000163 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
164 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000165 } else
166 IDI = toIdDeclInfo(Ptr);
167
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000168 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000169}
170
Douglas Gregor250e7a72011-03-16 16:39:03 +0000171void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
172 DeclarationName Name = D->getDeclName();
173 void *Ptr = Name.getFETokenInfo<void>();
174
Douglas Gregorfa7b8ce2011-03-24 10:35:39 +0000175 if (!Ptr) {
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000176 AddDecl(D);
177 return;
178 }
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000179
Douglas Gregorfa7b8ce2011-03-24 10:35:39 +0000180 if (isDeclPtr(Ptr)) {
181 // We only have a single declaration: insert before or after it,
182 // as appropriate.
183 if (Pos == iterator()) {
184 // Add the new declaration before the existing declaration.
185 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
186 RemoveDecl(PrevD);
187 AddDecl(D);
188 AddDecl(PrevD);
189 } else {
190 // Add new declaration after the existing declaration.
191 AddDecl(D);
192 }
193
194 return;
195 }
196
Douglas Gregor250e7a72011-03-16 16:39:03 +0000197 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
198 II->setIsFromAST(false);
199
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000200 // General case: insert the declaration at the appropriate point in the
201 // list, which already has at least two elements.
202 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Douglas Gregor250e7a72011-03-16 16:39:03 +0000203 if (Pos.isIterator()) {
204 IDI->InsertDecl(Pos.getIterator() + 1, D);
205 } else
Douglas Gregor7cbc5582011-03-14 21:19:51 +0000206 IDI->InsertDecl(IDI->decls_begin(), D);
207}
208
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000209/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000210/// The decl must already be part of the decl chain.
211void IdentifierResolver::RemoveDecl(NamedDecl *D) {
212 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000213 DeclarationName Name = D->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000214 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000215 II->setIsFromAST(false);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000216
Douglas Gregor2def4832008-11-17 20:34:05 +0000217 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000218
219 assert(Ptr && "Didn't find this decl on its identifier's chain!");
220
221 if (isDeclPtr(Ptr)) {
222 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000223 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000224 return;
225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000227 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000228}
229
Douglas Gregor63935192009-03-02 00:19:53 +0000230bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
Mike Stump1eb44332009-09-09 15:08:12 +0000231 assert(Old->getDeclName() == New->getDeclName() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000232 "Cannot replace a decl with another decl of a different name");
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Douglas Gregor63935192009-03-02 00:19:53 +0000234 DeclarationName Name = Old->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000235 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000236 II->setIsFromAST(false);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000237
Douglas Gregor63935192009-03-02 00:19:53 +0000238 void *Ptr = Name.getFETokenInfo<void>();
239
240 if (!Ptr)
241 return false;
242
243 if (isDeclPtr(Ptr)) {
244 if (Ptr == Old) {
245 Name.setFETokenInfo(New);
246 return true;
247 }
248 return false;
249 }
250
Mike Stump1eb44332009-09-09 15:08:12 +0000251 return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
Douglas Gregor63935192009-03-02 00:19:53 +0000252}
253
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000254/// begin - Returns an iterator for decls with name 'Name'.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000255IdentifierResolver::iterator
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000256IdentifierResolver::begin(DeclarationName Name) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000257 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000258 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000259
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000260 if (isDeclPtr(Ptr))
261 return iterator(static_cast<NamedDecl*>(Ptr));
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000262
Chris Lattnera2f42b12008-04-11 07:06:57 +0000263 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000264
Douglas Gregor074149e2009-01-05 19:45:36 +0000265 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000266 if (I != IDI->decls_begin())
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000267 return iterator(I-1);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000268 // No decls found.
269 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000270}
271
Mike Stump1eb44332009-09-09 15:08:12 +0000272void IdentifierResolver::AddDeclToIdentifierChain(IdentifierInfo *II,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000273 NamedDecl *D) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000274 II->setIsFromAST(false);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000275 void *Ptr = II->getFETokenInfo<void>();
276
277 if (!Ptr) {
278 II->setFETokenInfo(D);
279 return;
280 }
281
282 IdDeclInfo *IDI;
283
284 if (isDeclPtr(Ptr)) {
285 II->setFETokenInfo(NULL);
286 IDI = &(*IdDeclInfos)[II];
287 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
288 IDI->AddDecl(PrevD);
289 } else
290 IDI = toIdDeclInfo(Ptr);
291
292 IDI->AddDecl(D);
293}
294
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000295//===----------------------------------------------------------------------===//
296// IdDeclInfoMap Implementation
297//===----------------------------------------------------------------------===//
298
Douglas Gregor2def4832008-11-17 20:34:05 +0000299/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000300/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000301IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000302IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
303 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000304
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000305 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000306
John McCalleeb1cb42010-02-15 19:38:00 +0000307 if (CurIndex == POOL_SIZE) {
308 CurPool = new IdDeclInfoPool(CurPool);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000309 CurIndex = 0;
310 }
John McCalleeb1cb42010-02-15 19:38:00 +0000311 IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000312 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000313 reinterpret_cast<uintptr_t>(IDI) | 0x1)
314 );
315 ++CurIndex;
316 return *IDI;
317}
John McCall7cd088e2010-08-24 07:21:54 +0000318
319void IdentifierResolver::iterator::incrementSlowCase() {
320 NamedDecl *D = **this;
321 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
322 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
323 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
324
325 BaseIter I = getIterator();
326 if (I != Info->decls_begin())
327 *this = iterator(I-1);
328 else // No more decls.
329 *this = iterator();
330}