blob: b09526e097b603911607559c7cdca1e64a4627da [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
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
Chris Lattnerb28317a2009-03-28 19:18:32 +0000114 if (S->isDeclScope(Action::DeclPtrTy::make(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)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000131 return S->getParent()->isDeclScope(Action::DeclPtrTy::make(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();
142 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000143
144 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000145 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000146 return;
147 }
148
149 IdDeclInfo *IDI;
150
151 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000152 Name.setFETokenInfo(NULL);
153 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000154 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
155 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000156 } else
157 IDI = toIdDeclInfo(Ptr);
158
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000159 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000160}
161
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000162/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000163/// The decl must already be part of the decl chain.
164void IdentifierResolver::RemoveDecl(NamedDecl *D) {
165 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000166 DeclarationName Name = D->getDeclName();
167 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000168
169 assert(Ptr && "Didn't find this decl on its identifier's chain!");
170
171 if (isDeclPtr(Ptr)) {
172 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000173 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000174 return;
175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000177 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000178}
179
Douglas Gregor63935192009-03-02 00:19:53 +0000180bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
Mike Stump1eb44332009-09-09 15:08:12 +0000181 assert(Old->getDeclName() == New->getDeclName() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000182 "Cannot replace a decl with another decl of a different name");
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregor63935192009-03-02 00:19:53 +0000184 DeclarationName Name = Old->getDeclName();
185 void *Ptr = Name.getFETokenInfo<void>();
186
187 if (!Ptr)
188 return false;
189
190 if (isDeclPtr(Ptr)) {
191 if (Ptr == Old) {
192 Name.setFETokenInfo(New);
193 return true;
194 }
195 return false;
196 }
197
Mike Stump1eb44332009-09-09 15:08:12 +0000198 return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
Douglas Gregor63935192009-03-02 00:19:53 +0000199}
200
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000201/// begin - Returns an iterator for decls with name 'Name'.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000202IdentifierResolver::iterator
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000203IdentifierResolver::begin(DeclarationName Name) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000204 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000205 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000206
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000207 if (isDeclPtr(Ptr))
208 return iterator(static_cast<NamedDecl*>(Ptr));
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000209
Chris Lattnera2f42b12008-04-11 07:06:57 +0000210 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000211
Douglas Gregor074149e2009-01-05 19:45:36 +0000212 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000213 if (I != IDI->decls_begin())
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000214 return iterator(I-1);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000215 // No decls found.
216 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000217}
218
Mike Stump1eb44332009-09-09 15:08:12 +0000219void IdentifierResolver::AddDeclToIdentifierChain(IdentifierInfo *II,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000220 NamedDecl *D) {
221 void *Ptr = II->getFETokenInfo<void>();
222
223 if (!Ptr) {
224 II->setFETokenInfo(D);
225 return;
226 }
227
228 IdDeclInfo *IDI;
229
230 if (isDeclPtr(Ptr)) {
231 II->setFETokenInfo(NULL);
232 IDI = &(*IdDeclInfos)[II];
233 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
234 IDI->AddDecl(PrevD);
235 } else
236 IDI = toIdDeclInfo(Ptr);
237
238 IDI->AddDecl(D);
239}
240
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000241//===----------------------------------------------------------------------===//
242// IdDeclInfoMap Implementation
243//===----------------------------------------------------------------------===//
244
Douglas Gregor2def4832008-11-17 20:34:05 +0000245/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000246/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000247IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000248IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
249 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000250
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000251 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000252
John McCalleeb1cb42010-02-15 19:38:00 +0000253 if (CurIndex == POOL_SIZE) {
254 CurPool = new IdDeclInfoPool(CurPool);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000255 CurIndex = 0;
256 }
John McCalleeb1cb42010-02-15 19:38:00 +0000257 IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000258 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000259 reinterpret_cast<uintptr_t>(IDI) | 0x1)
260 );
261 ++CurIndex;
262 return *IDI;
263}