blob: 5fb18f6cef432dfadf4123fb33265e973d729309 [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#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
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 {
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
Douglas Gregor2def4832008-11-17 20:34:05 +000039 /// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +000040 /// It creates a new IdDeclInfo if one was not created before for this id.
Douglas Gregor2def4832008-11-17 20:34:05 +000041 IdDeclInfo &operator[](DeclarationName Name);
Chris Lattnera2f42b12008-04-11 07:06:57 +000042};
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))
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000060 Ctx = SD->getDeclContext();
61 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D))
62 Ctx = Ovl->getDeclContext();
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000063 else
64 return TUCtx();
65
66 if (isa<TranslationUnitDecl>(Ctx))
67 return TUCtx();
68
69 return Ctx;
70}
71
72/// isEqOrContainedBy - Returns true of the given context is the same or a
73/// parent of this one.
74bool IdentifierResolver::LookupContext::isEqOrContainedBy(
75 const LookupContext &PC) const {
76 if (PC.isTU()) return true;
77
78 for (LookupContext Next = *this; !Next.isTU(); Next = Next.getParent())
79 if (Next.Ctx == PC.Ctx) return true;
80
81 return false;
82}
83
84
85//===----------------------------------------------------------------------===//
86// IdDeclInfo Implementation
87//===----------------------------------------------------------------------===//
88
89/// FindContext - Returns an iterator pointing just after the decl that is
90/// in the given context or in a parent of it. The search is in reverse
91/// order, from end to begin.
92IdentifierResolver::IdDeclInfo::DeclsTy::iterator
Zhongxing Xuebed7972008-12-05 01:57:57 +000093IdentifierResolver::IdDeclInfo::FindDeclVisibleInContext(
94 const LookupContext &Ctx,
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000095 const DeclsTy::iterator &Start) {
96 for (DeclsTy::iterator I = Start; I != Decls.begin(); --I) {
97 if (Ctx.isEqOrContainedBy(LookupContext(*(I-1))))
98 return I;
99 }
100
101 return Decls.begin();
102}
103
104/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
105/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
106/// be already added to the scope chain and must be in the same context as
107/// the decl that we want to add.
108void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
109 NamedDecl *Shadow) {
110 assert(LookupContext(D) == LookupContext(Shadow) &&
111 "Decl and Shadow not in same context!");
112
113 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
114 if (Shadow == *(I-1)) {
115 Decls.insert(I-1, D);
116 return;
117 }
118 }
119
120 assert(0 && "Shadow wasn't in scope chain!");
121}
122
123/// RemoveDecl - Remove the decl from the scope chain.
124/// The decl must already be part of the decl chain.
125void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
126 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
127 if (D == *(I-1)) {
128 Decls.erase(I-1);
129 return;
130 }
131 }
132
133 assert(0 && "Didn't find this decl on its identifier's chain!");
134}
135
136
137//===----------------------------------------------------------------------===//
138// IdentifierResolver Implementation
139//===----------------------------------------------------------------------===//
140
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000141IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
142 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
143}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000144IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000145 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000146}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000147
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000148/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
149/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
150/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000151bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
152 Scope *S) const {
153 if (Ctx->isFunctionOrMethod()) {
154 if (S->isDeclScope(D))
155 return true;
156 if (LangOpt.CPlusPlus) {
157 // C++ 3.3.2p4:
158 // Names declared in the for-init-statement, and in the condition of if,
159 // while, for, and switch statements are local to the if, while, for, or
160 // switch statement (including the controlled statement), and shall not be
161 // redeclared in a subsequent condition of that statement nor in the
162 // outermost block (or, for the if statement, any of the outermost blocks)
163 // of the controlled statement.
164 //
165 assert(S->getParent() && "No TUScope?");
166 if (S->getParent()->getFlags() & Scope::ControlScope)
167 return S->getParent()->isDeclScope(D);
168 }
169 return false;
170 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000171
172 return LookupContext(D) == LookupContext(Ctx);
173}
174
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000175/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000176void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000177 DeclarationName Name = D->getDeclName();
178 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000179
180 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000181 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000182 return;
183 }
184
185 IdDeclInfo *IDI;
186
187 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000188 Name.setFETokenInfo(NULL);
189 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000190 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
191 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000192 } else
193 IDI = toIdDeclInfo(Ptr);
194
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000195 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000196}
197
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000198/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000199/// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000200/// encountered before the 'D' decl.
201void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000202 assert(D->getDeclName() == Shadow->getDeclName() && "Different ids!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000203 assert(LookupContext(D) == LookupContext(Shadow) && "Different context!");
204
Douglas Gregor2def4832008-11-17 20:34:05 +0000205 DeclarationName Name = D->getDeclName();
206 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000207 assert(Ptr && "No decl from Ptr ?");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000208
209 IdDeclInfo *IDI;
210
211 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000212 Name.setFETokenInfo(NULL);
213 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000214 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
215 assert(PrevD == Shadow && "Invalid shadow decl ?");
216 IDI->AddDecl(D);
217 IDI->AddDecl(PrevD);
218 return;
219 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000220
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000221 IDI = toIdDeclInfo(Ptr);
222 IDI->AddShadowed(D, Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000223}
224
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000225/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000226/// The decl must already be part of the decl chain.
227void IdentifierResolver::RemoveDecl(NamedDecl *D) {
228 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000229 DeclarationName Name = D->getDeclName();
230 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000231
232 assert(Ptr && "Didn't find this decl on its identifier's chain!");
233
234 if (isDeclPtr(Ptr)) {
235 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000236 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000237 return;
238 }
239
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000240 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000241}
242
Douglas Gregor2def4832008-11-17 20:34:05 +0000243/// begin - Returns an iterator for decls with name 'Name', starting at
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000244/// declaration context 'Ctx'. If 'LookInParentCtx' is true, it will walk the
245/// decls of parent declaration contexts too.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000246IdentifierResolver::iterator
Douglas Gregor2def4832008-11-17 20:34:05 +0000247IdentifierResolver::begin(DeclarationName Name, const DeclContext *Ctx,
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000248 bool LookInParentCtx) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000249 assert(Ctx && "null param passed");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000250
Douglas Gregor2def4832008-11-17 20:34:05 +0000251 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000252 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000253
254 LookupContext LC(Ctx);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000255
256 if (isDeclPtr(Ptr)) {
257 NamedDecl *D = static_cast<NamedDecl*>(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000258 LookupContext DC(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000259
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000260 if (( LookInParentCtx && LC.isEqOrContainedBy(DC)) ||
261 (!LookInParentCtx && LC == DC))
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000262 return iterator(D);
263 else
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000264 return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000265 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000266
Chris Lattnera2f42b12008-04-11 07:06:57 +0000267 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000268
269 IdDeclInfo::DeclsTy::iterator I;
270 if (LookInParentCtx)
Zhongxing Xuebed7972008-12-05 01:57:57 +0000271 I = IDI->FindDeclVisibleInContext(LC);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000272 else {
273 for (I = IDI->decls_end(); I != IDI->decls_begin(); --I)
274 if (LookupContext(*(I-1)) == LC)
275 break;
276 }
277
278 if (I != IDI->decls_begin())
279 return iterator(I-1, LookInParentCtx);
280 else // No decls found.
281 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000282}
283
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000284/// PreIncIter - Do a preincrement when 'Ptr' is a BaseIter.
285void IdentifierResolver::iterator::PreIncIter() {
286 NamedDecl *D = **this;
287 LookupContext Ctx(D);
Douglas Gregor2def4832008-11-17 20:34:05 +0000288 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000289 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
290 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000291
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000292 BaseIter I = getIterator();
293 if (LookInParentCtx())
Zhongxing Xuebed7972008-12-05 01:57:57 +0000294 I = Info->FindDeclVisibleInContext(Ctx, I);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000295 else {
296 if (I != Info->decls_begin() && LookupContext(*(I-1)) != Ctx) {
297 // The next decl is in different declaration context.
298 // Skip remaining decls and set the iterator to the end.
299 I = Info->decls_begin();
300 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000301 }
302
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000303 if (I != Info->decls_begin())
304 *this = iterator(I-1, LookInParentCtx());
305 else // No more decls.
306 *this = end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000307}
308
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000309
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000310//===----------------------------------------------------------------------===//
311// IdDeclInfoMap Implementation
312//===----------------------------------------------------------------------===//
313
Douglas Gregor2def4832008-11-17 20:34:05 +0000314/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000315/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000316IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000317IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
318 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000319
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000320 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000321
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000322 if (CurIndex == VECTOR_SIZE) {
323 // Add a IdDeclInfo vector 'pool'
Argyrios Kyrtzidis72e62b02008-04-12 12:38:58 +0000324 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000325 // Fill the vector
326 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000327 CurIndex = 0;
328 }
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000329 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000330 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000331 reinterpret_cast<uintptr_t>(IDI) | 0x1)
332 );
333 ++CurIndex;
334 return *IDI;
335}