blob: 9421cad2243a0f582062621ada4663835ce68a99 [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"
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000016#include "clang/Basic/LangOptions.h"
Chris Lattnera2f42b12008-04-11 07:06:57 +000017
18using namespace clang;
19
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000020//===----------------------------------------------------------------------===//
21// IdDeclInfoMap class
22//===----------------------------------------------------------------------===//
Chris Lattnera2f42b12008-04-11 07:06:57 +000023
Douglas Gregor2def4832008-11-17 20:34:05 +000024/// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000025/// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
Chris Lattnera2f42b12008-04-11 07:06:57 +000026/// individual IdDeclInfo to heap.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000027class IdentifierResolver::IdDeclInfoMap {
John McCalleeb1cb42010-02-15 19:38:00 +000028 static const unsigned int POOL_SIZE = 512;
29
30 /// We use our own linked-list implementation because it is sadly
31 /// impossible to add something to a pre-C++0x STL container without
32 /// a completely unnecessary copy.
33 struct IdDeclInfoPool {
34 IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
35
36 IdDeclInfoPool *Next;
37 IdDeclInfo Pool[POOL_SIZE];
38 };
39
40 IdDeclInfoPool *CurPool;
Chris Lattnera2f42b12008-04-11 07:06:57 +000041 unsigned int CurIndex;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Chris Lattnera2f42b12008-04-11 07:06:57 +000043public:
John McCalleeb1cb42010-02-15 19:38:00 +000044 IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE) {}
45
46 ~IdDeclInfoMap() {
47 IdDeclInfoPool *Cur = CurPool;
48 while (IdDeclInfoPool *P = Cur) {
49 Cur = Cur->Next;
50 delete P;
51 }
52 }
Chris Lattnera2f42b12008-04-11 07:06:57 +000053
Douglas Gregor2def4832008-11-17 20:34:05 +000054 /// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +000055 /// It creates a new IdDeclInfo if one was not created before for this id.
Douglas Gregor2def4832008-11-17 20:34:05 +000056 IdDeclInfo &operator[](DeclarationName Name);
Chris Lattnera2f42b12008-04-11 07:06:57 +000057};
58
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000059
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000060//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000061// IdDeclInfo Implementation
62//===----------------------------------------------------------------------===//
63
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000064/// RemoveDecl - Remove the decl from the scope chain.
65/// The decl must already be part of the decl chain.
66void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
67 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
68 if (D == *(I-1)) {
69 Decls.erase(I-1);
70 return;
71 }
72 }
73
74 assert(0 && "Didn't find this decl on its identifier's chain!");
75}
76
Mike Stump1eb44332009-09-09 15:08:12 +000077bool
Douglas Gregor63935192009-03-02 00:19:53 +000078IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
79 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
80 if (Old == *(I-1)) {
81 *(I - 1) = New;
82 return true;
83 }
84 }
85
86 return false;
87}
88
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000089
90//===----------------------------------------------------------------------===//
91// IdentifierResolver Implementation
92//===----------------------------------------------------------------------===//
93
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +000094IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
95 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
96}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000097IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000098 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000099}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000100
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000101/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
102/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
103/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000104bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregor44b43212008-12-11 16:49:14 +0000105 ASTContext &Context, Scope *S) const {
Douglas Gregorce356072009-01-06 23:51:29 +0000106 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +0000107
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000108 if (Ctx->isFunctionOrMethod()) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000109 // Ignore the scopes associated within transparent declaration contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000110 while (S->getEntity() &&
Douglas Gregor074149e2009-01-05 19:45:36 +0000111 ((DeclContext *)S->getEntity())->isTransparentContext())
112 S = S->getParent();
113
John McCalld226f652010-08-21 09:40:31 +0000114 if (S->isDeclScope(D))
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000115 return true;
116 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000117 // C++ 3.3.2p3:
118 // The name declared in a catch exception-declaration is local to the
119 // handler and shall not be redeclared in the outermost block of the
120 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000121 // C++ 3.3.2p4:
122 // Names declared in the for-init-statement, and in the condition of if,
123 // while, for, and switch statements are local to the if, while, for, or
124 // switch statement (including the controlled statement), and shall not be
125 // redeclared in a subsequent condition of that statement nor in the
126 // outermost block (or, for the if statement, any of the outermost blocks)
127 // of the controlled statement.
128 //
129 assert(S->getParent() && "No TUScope?");
130 if (S->getParent()->getFlags() & Scope::ControlScope)
John McCalld226f652010-08-21 09:40:31 +0000131 return S->getParent()->isDeclScope(D);
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000132 }
133 return false;
134 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000135
Douglas Gregor61481da2009-09-01 17:22:34 +0000136 return D->getDeclContext()->getLookupContext()->Equals(Ctx);
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000137}
138
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000139/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000140void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000141 DeclarationName Name = D->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000142 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000143 II->setIsFromAST(false);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000144
Douglas Gregor2def4832008-11-17 20:34:05 +0000145 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000146
147 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000148 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000149 return;
150 }
151
152 IdDeclInfo *IDI;
153
154 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000155 Name.setFETokenInfo(NULL);
156 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000157 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
158 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000159 } else
160 IDI = toIdDeclInfo(Ptr);
161
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000162 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000163}
164
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000165/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000166/// The decl must already be part of the decl chain.
167void IdentifierResolver::RemoveDecl(NamedDecl *D) {
168 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000169 DeclarationName Name = D->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000170 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000171 II->setIsFromAST(false);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000172
Douglas Gregor2def4832008-11-17 20:34:05 +0000173 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000174
175 assert(Ptr && "Didn't find this decl on its identifier's chain!");
176
177 if (isDeclPtr(Ptr)) {
178 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000179 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000180 return;
181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000183 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000184}
185
Douglas Gregor63935192009-03-02 00:19:53 +0000186bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
Mike Stump1eb44332009-09-09 15:08:12 +0000187 assert(Old->getDeclName() == New->getDeclName() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000188 "Cannot replace a decl with another decl of a different name");
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Douglas Gregor63935192009-03-02 00:19:53 +0000190 DeclarationName Name = Old->getDeclName();
Sebastian Redl1450ef92010-07-30 17:25:10 +0000191 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000192 II->setIsFromAST(false);
Sebastian Redl1450ef92010-07-30 17:25:10 +0000193
Douglas Gregor63935192009-03-02 00:19:53 +0000194 void *Ptr = Name.getFETokenInfo<void>();
195
196 if (!Ptr)
197 return false;
198
199 if (isDeclPtr(Ptr)) {
200 if (Ptr == Old) {
201 Name.setFETokenInfo(New);
202 return true;
203 }
204 return false;
205 }
206
Mike Stump1eb44332009-09-09 15:08:12 +0000207 return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
Douglas Gregor63935192009-03-02 00:19:53 +0000208}
209
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000210/// begin - Returns an iterator for decls with name 'Name'.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000211IdentifierResolver::iterator
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000212IdentifierResolver::begin(DeclarationName Name) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000213 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000214 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000215
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000216 if (isDeclPtr(Ptr))
217 return iterator(static_cast<NamedDecl*>(Ptr));
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000218
Chris Lattnera2f42b12008-04-11 07:06:57 +0000219 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000220
Douglas Gregor074149e2009-01-05 19:45:36 +0000221 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000222 if (I != IDI->decls_begin())
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000223 return iterator(I-1);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000224 // No decls found.
225 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000226}
227
Mike Stump1eb44332009-09-09 15:08:12 +0000228void IdentifierResolver::AddDeclToIdentifierChain(IdentifierInfo *II,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000229 NamedDecl *D) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000230 II->setIsFromAST(false);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000231 void *Ptr = II->getFETokenInfo<void>();
232
233 if (!Ptr) {
234 II->setFETokenInfo(D);
235 return;
236 }
237
238 IdDeclInfo *IDI;
239
240 if (isDeclPtr(Ptr)) {
241 II->setFETokenInfo(NULL);
242 IDI = &(*IdDeclInfos)[II];
243 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
244 IDI->AddDecl(PrevD);
245 } else
246 IDI = toIdDeclInfo(Ptr);
247
248 IDI->AddDecl(D);
249}
250
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000251//===----------------------------------------------------------------------===//
252// IdDeclInfoMap Implementation
253//===----------------------------------------------------------------------===//
254
Douglas Gregor2def4832008-11-17 20:34:05 +0000255/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000256/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000257IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000258IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
259 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000260
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000261 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000262
John McCalleeb1cb42010-02-15 19:38:00 +0000263 if (CurIndex == POOL_SIZE) {
264 CurPool = new IdDeclInfoPool(CurPool);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000265 CurIndex = 0;
266 }
John McCalleeb1cb42010-02-15 19:38:00 +0000267 IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000268 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000269 reinterpret_cast<uintptr_t>(IDI) | 0x1)
270 );
271 ++CurIndex;
272 return *IDI;
273}