blob: e97de7381785bc17ace3092df6ae8ac0fe79defa [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
Chris Lattnera2f42b12008-04-11 07:06:57 +000011// scoped lookup, based on identifier.
12//
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#include <list>
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000018#include <vector>
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
26/// IdDeclInfoMap - Associates IdDeclInfos with Identifiers.
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 {
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000030 static const unsigned int VECTOR_SIZE = 512;
31 // Holds vectors of IdDeclInfos that serve as 'pools'.
32 // New vectors are added when the current one is full.
33 std::list< std::vector<IdDeclInfo> > IDIVecs;
Chris Lattnera2f42b12008-04-11 07:06:57 +000034 unsigned int CurIndex;
35
36public:
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000037 IdDeclInfoMap() : CurIndex(VECTOR_SIZE) {}
Chris Lattnera2f42b12008-04-11 07:06:57 +000038
39 /// Returns the IdDeclInfo associated to the IdentifierInfo.
40 /// It creates a new IdDeclInfo if one was not created before for this id.
41 IdDeclInfo &operator[](IdentifierInfo *II);
42};
43
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000044
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000045//===----------------------------------------------------------------------===//
46// LookupContext Implementation
47//===----------------------------------------------------------------------===//
48
49/// getContext - Returns translation unit context for non ScopedDecls and
50/// for EnumConstantDecls returns the parent context of their EnumDecl.
51DeclContext *IdentifierResolver::LookupContext::getContext(Decl *D) {
52 DeclContext *Ctx;
53
54 if (CXXFieldDecl *FD = dyn_cast<CXXFieldDecl>(D))
55 return FD->getParent();
56
57 if (EnumConstantDecl *EnumD = dyn_cast<EnumConstantDecl>(D)) {
58 Ctx = EnumD->getDeclContext()->getParent();
59 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D))
60 Ctx = SD->getDeclContext();
61 else
62 return TUCtx();
63
64 if (isa<TranslationUnitDecl>(Ctx))
65 return TUCtx();
66
67 return Ctx;
68}
69
70/// isEqOrContainedBy - Returns true of the given context is the same or a
71/// parent of this one.
72bool IdentifierResolver::LookupContext::isEqOrContainedBy(
73 const LookupContext &PC) const {
74 if (PC.isTU()) return true;
75
76 for (LookupContext Next = *this; !Next.isTU(); Next = Next.getParent())
77 if (Next.Ctx == PC.Ctx) return true;
78
79 return false;
80}
81
82
83//===----------------------------------------------------------------------===//
84// IdDeclInfo Implementation
85//===----------------------------------------------------------------------===//
86
87/// FindContext - Returns an iterator pointing just after the decl that is
88/// in the given context or in a parent of it. The search is in reverse
89/// order, from end to begin.
90IdentifierResolver::IdDeclInfo::DeclsTy::iterator
91IdentifierResolver::IdDeclInfo::FindContext(const LookupContext &Ctx,
92 const DeclsTy::iterator &Start) {
93 for (DeclsTy::iterator I = Start; I != Decls.begin(); --I) {
94 if (Ctx.isEqOrContainedBy(LookupContext(*(I-1))))
95 return I;
96 }
97
98 return Decls.begin();
99}
100
101/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
102/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
103/// be already added to the scope chain and must be in the same context as
104/// the decl that we want to add.
105void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
106 NamedDecl *Shadow) {
107 assert(LookupContext(D) == LookupContext(Shadow) &&
108 "Decl and Shadow not in same context!");
109
110 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
111 if (Shadow == *(I-1)) {
112 Decls.insert(I-1, D);
113 return;
114 }
115 }
116
117 assert(0 && "Shadow wasn't in scope chain!");
118}
119
120/// RemoveDecl - Remove the decl from the scope chain.
121/// The decl must already be part of the decl chain.
122void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
123 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
124 if (D == *(I-1)) {
125 Decls.erase(I-1);
126 return;
127 }
128 }
129
130 assert(0 && "Didn't find this decl on its identifier's chain!");
131}
132
133
134//===----------------------------------------------------------------------===//
135// IdentifierResolver Implementation
136//===----------------------------------------------------------------------===//
137
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000138IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
139 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
140}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000141IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000142 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000143}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000144
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000145/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
146/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
147/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000148bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
149 Scope *S) const {
150 if (Ctx->isFunctionOrMethod()) {
151 if (S->isDeclScope(D))
152 return true;
153 if (LangOpt.CPlusPlus) {
154 // C++ 3.3.2p4:
155 // Names declared in the for-init-statement, and in the condition of if,
156 // while, for, and switch statements are local to the if, while, for, or
157 // switch statement (including the controlled statement), and shall not be
158 // redeclared in a subsequent condition of that statement nor in the
159 // outermost block (or, for the if statement, any of the outermost blocks)
160 // of the controlled statement.
161 //
162 assert(S->getParent() && "No TUScope?");
163 if (S->getParent()->getFlags() & Scope::ControlScope)
164 return S->getParent()->isDeclScope(D);
165 }
166 return false;
167 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000168
169 return LookupContext(D) == LookupContext(Ctx);
170}
171
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000172/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000173void IdentifierResolver::AddDecl(NamedDecl *D) {
Chris Lattnera2f42b12008-04-11 07:06:57 +0000174 IdentifierInfo *II = D->getIdentifier();
175 void *Ptr = II->getFETokenInfo<void>();
176
177 if (!Ptr) {
178 II->setFETokenInfo(D);
179 return;
180 }
181
182 IdDeclInfo *IDI;
183
184 if (isDeclPtr(Ptr)) {
185 II->setFETokenInfo(NULL);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000186 IDI = &(*IdDeclInfos)[II];
187 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
188 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000189 } else
190 IDI = toIdDeclInfo(Ptr);
191
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000192 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000193}
194
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000195/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000196/// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000197/// encountered before the 'D' decl.
198void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
199 assert(D->getIdentifier() == Shadow->getIdentifier() && "Different ids!");
200 assert(LookupContext(D) == LookupContext(Shadow) && "Different context!");
201
Chris Lattnera2f42b12008-04-11 07:06:57 +0000202 IdentifierInfo *II = D->getIdentifier();
203 void *Ptr = II->getFETokenInfo<void>();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000204 assert(Ptr && "No decl from Ptr ?");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000205
206 IdDeclInfo *IDI;
207
208 if (isDeclPtr(Ptr)) {
209 II->setFETokenInfo(NULL);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000210 IDI = &(*IdDeclInfos)[II];
211 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
212 assert(PrevD == Shadow && "Invalid shadow decl ?");
213 IDI->AddDecl(D);
214 IDI->AddDecl(PrevD);
215 return;
216 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000217
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000218 IDI = toIdDeclInfo(Ptr);
219 IDI->AddShadowed(D, Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000220}
221
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000222/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000223/// The decl must already be part of the decl chain.
224void IdentifierResolver::RemoveDecl(NamedDecl *D) {
225 assert(D && "null param passed");
226 IdentifierInfo *II = D->getIdentifier();
227 void *Ptr = II->getFETokenInfo<void>();
228
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!");
233 II->setFETokenInfo(NULL);
234 return;
235 }
236
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000237 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000238}
239
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000240/// begin - Returns an iterator for decls of identifier 'II', starting at
241/// declaration context 'Ctx'. If 'LookInParentCtx' is true, it will walk the
242/// decls of parent declaration contexts too.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000243IdentifierResolver::iterator
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000244IdentifierResolver::begin(const IdentifierInfo *II, DeclContext *Ctx,
245 bool LookInParentCtx) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000246 assert(Ctx && "null param passed");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000247
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000248 void *Ptr = II->getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000249 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000250
251 LookupContext LC(Ctx);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000252
253 if (isDeclPtr(Ptr)) {
254 NamedDecl *D = static_cast<NamedDecl*>(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000255 LookupContext DC(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000256
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000257 if (( LookInParentCtx && LC.isEqOrContainedBy(DC)) ||
258 (!LookInParentCtx && LC == DC))
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000259 return iterator(D);
260 else
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000261 return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000262 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000263
Chris Lattnera2f42b12008-04-11 07:06:57 +0000264 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000265
266 IdDeclInfo::DeclsTy::iterator I;
267 if (LookInParentCtx)
268 I = IDI->FindContext(LC);
269 else {
270 for (I = IDI->decls_end(); I != IDI->decls_begin(); --I)
271 if (LookupContext(*(I-1)) == LC)
272 break;
273 }
274
275 if (I != IDI->decls_begin())
276 return iterator(I-1, LookInParentCtx);
277 else // No decls found.
278 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000279}
280
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000281/// PreIncIter - Do a preincrement when 'Ptr' is a BaseIter.
282void IdentifierResolver::iterator::PreIncIter() {
283 NamedDecl *D = **this;
284 LookupContext Ctx(D);
285 void *InfoPtr = D->getIdentifier()->getFETokenInfo<void>();
286 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
287 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000288
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000289 BaseIter I = getIterator();
290 if (LookInParentCtx())
291 I = Info->FindContext(Ctx, I);
292 else {
293 if (I != Info->decls_begin() && LookupContext(*(I-1)) != Ctx) {
294 // The next decl is in different declaration context.
295 // Skip remaining decls and set the iterator to the end.
296 I = Info->decls_begin();
297 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000298 }
299
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000300 if (I != Info->decls_begin())
301 *this = iterator(I-1, LookInParentCtx());
302 else // No more decls.
303 *this = end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000304}
305
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000306
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000307//===----------------------------------------------------------------------===//
308// IdDeclInfoMap Implementation
309//===----------------------------------------------------------------------===//
310
Chris Lattnera2f42b12008-04-11 07:06:57 +0000311/// Returns the IdDeclInfo associated to the IdentifierInfo.
312/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000313IdentifierResolver::IdDeclInfo &
314IdentifierResolver::IdDeclInfoMap::operator[](IdentifierInfo *II) {
Chris Lattnera2f42b12008-04-11 07:06:57 +0000315 assert (II && "null IdentifierInfo passed");
316 void *Ptr = II->getFETokenInfo<void>();
317
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000318 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000319
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000320 if (CurIndex == VECTOR_SIZE) {
321 // Add a IdDeclInfo vector 'pool'
Argyrios Kyrtzidis72e62b02008-04-12 12:38:58 +0000322 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000323 // Fill the vector
324 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000325 CurIndex = 0;
326 }
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000327 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Chris Lattnera2f42b12008-04-11 07:06:57 +0000328 II->setFETokenInfo(reinterpret_cast<void*>(
329 reinterpret_cast<uintptr_t>(IDI) | 0x1)
330 );
331 ++CurIndex;
332 return *IDI;
333}