blob: 08c9bdeb3197dfe7a82d094c78580ff268cd332d [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
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000054 if (EnumConstantDecl *EnumD = dyn_cast<EnumConstantDecl>(D)) {
55 Ctx = EnumD->getDeclContext()->getParent();
56 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D))
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000057 Ctx = SD->getDeclContext();
58 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D))
59 Ctx = Ovl->getDeclContext();
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000060 else
61 return TUCtx();
62
Douglas Gregorce356072009-01-06 23:51:29 +000063 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +000064
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000065 if (isa<TranslationUnitDecl>(Ctx))
66 return TUCtx();
67
68 return Ctx;
69}
70
71/// isEqOrContainedBy - Returns true of the given context is the same or a
72/// parent of this one.
73bool IdentifierResolver::LookupContext::isEqOrContainedBy(
74 const LookupContext &PC) const {
75 if (PC.isTU()) return true;
76
77 for (LookupContext Next = *this; !Next.isTU(); Next = Next.getParent())
78 if (Next.Ctx == PC.Ctx) return true;
79
80 return false;
81}
82
83
84//===----------------------------------------------------------------------===//
85// IdDeclInfo Implementation
86//===----------------------------------------------------------------------===//
87
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000088/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
89/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
90/// be already added to the scope chain and must be in the same context as
91/// the decl that we want to add.
92void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
93 NamedDecl *Shadow) {
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000094 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
95 if (Shadow == *(I-1)) {
96 Decls.insert(I-1, D);
97 return;
98 }
99 }
100
101 assert(0 && "Shadow wasn't in scope chain!");
102}
103
104/// RemoveDecl - Remove the decl from the scope chain.
105/// The decl must already be part of the decl chain.
106void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
107 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
108 if (D == *(I-1)) {
109 Decls.erase(I-1);
110 return;
111 }
112 }
113
114 assert(0 && "Didn't find this decl on its identifier's chain!");
115}
116
117
118//===----------------------------------------------------------------------===//
119// IdentifierResolver Implementation
120//===----------------------------------------------------------------------===//
121
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000122IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
123 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
124}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000125IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000126 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000127}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000128
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000129/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
130/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
131/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000132bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregor44b43212008-12-11 16:49:14 +0000133 ASTContext &Context, Scope *S) const {
Douglas Gregorce356072009-01-06 23:51:29 +0000134 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +0000135
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000136 if (Ctx->isFunctionOrMethod()) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000137 // Ignore the scopes associated within transparent declaration contexts.
138 while (S->getEntity() &&
139 ((DeclContext *)S->getEntity())->isTransparentContext())
140 S = S->getParent();
141
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000142 if (S->isDeclScope(D))
143 return true;
144 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000145 // C++ 3.3.2p3:
146 // The name declared in a catch exception-declaration is local to the
147 // handler and shall not be redeclared in the outermost block of the
148 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000149 // C++ 3.3.2p4:
150 // Names declared in the for-init-statement, and in the condition of if,
151 // while, for, and switch statements are local to the if, while, for, or
152 // switch statement (including the controlled statement), and shall not be
153 // redeclared in a subsequent condition of that statement nor in the
154 // outermost block (or, for the if statement, any of the outermost blocks)
155 // of the controlled statement.
156 //
157 assert(S->getParent() && "No TUScope?");
158 if (S->getParent()->getFlags() & Scope::ControlScope)
159 return S->getParent()->isDeclScope(D);
160 }
161 return false;
162 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000163
Douglas Gregor44b43212008-12-11 16:49:14 +0000164 return LookupContext(D) == LookupContext(Ctx->getPrimaryContext(Context));
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000165}
166
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000167/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000168void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000169 DeclarationName Name = D->getDeclName();
170 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000171
172 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000173 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000174 return;
175 }
176
177 IdDeclInfo *IDI;
178
179 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000180 Name.setFETokenInfo(NULL);
181 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000182 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
183 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000184 } else
185 IDI = toIdDeclInfo(Ptr);
186
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000187 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000188}
189
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000190/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000191/// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000192/// encountered before the 'D' decl.
193void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000194 assert(D->getDeclName() == Shadow->getDeclName() && "Different ids!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000195
Douglas Gregor2def4832008-11-17 20:34:05 +0000196 DeclarationName Name = D->getDeclName();
197 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000198 assert(Ptr && "No decl from Ptr ?");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000199
200 IdDeclInfo *IDI;
201
202 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000203 Name.setFETokenInfo(NULL);
204 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000205 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
206 assert(PrevD == Shadow && "Invalid shadow decl ?");
207 IDI->AddDecl(D);
208 IDI->AddDecl(PrevD);
209 return;
210 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000211
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000212 IDI = toIdDeclInfo(Ptr);
213 IDI->AddShadowed(D, Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000214}
215
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000216/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000217/// The decl must already be part of the decl chain.
218void IdentifierResolver::RemoveDecl(NamedDecl *D) {
219 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000220 DeclarationName Name = D->getDeclName();
221 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000222
223 assert(Ptr && "Didn't find this decl on its identifier's chain!");
224
225 if (isDeclPtr(Ptr)) {
226 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000227 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000228 return;
229 }
230
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000231 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000232}
233
Douglas Gregor2def4832008-11-17 20:34:05 +0000234/// begin - Returns an iterator for decls with name 'Name', starting at
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000235/// declaration context 'Ctx'. If 'LookInParentCtx' is true, it will walk the
236/// decls of parent declaration contexts too.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000237IdentifierResolver::iterator
Douglas Gregor2def4832008-11-17 20:34:05 +0000238IdentifierResolver::begin(DeclarationName Name, const DeclContext *Ctx,
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000239 bool LookInParentCtx) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000240 assert(Ctx && "null param passed");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000241
Douglas Gregor2def4832008-11-17 20:34:05 +0000242 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000243 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000244
Chris Lattnera2f42b12008-04-11 07:06:57 +0000245 if (isDeclPtr(Ptr)) {
246 NamedDecl *D = static_cast<NamedDecl*>(Ptr);
Douglas Gregor074149e2009-01-05 19:45:36 +0000247 return iterator(D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000248 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000249
Chris Lattnera2f42b12008-04-11 07:06:57 +0000250 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000251
Douglas Gregor074149e2009-01-05 19:45:36 +0000252 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000253 if (I != IDI->decls_begin())
254 return iterator(I-1, LookInParentCtx);
255 else // No decls found.
256 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000257}
258
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000259/// PreIncIter - Do a preincrement when 'Ptr' is a BaseIter.
260void IdentifierResolver::iterator::PreIncIter() {
261 NamedDecl *D = **this;
Douglas Gregor2def4832008-11-17 20:34:05 +0000262 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000263 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
264 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000265
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000266 BaseIter I = getIterator();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000267 if (I != Info->decls_begin())
268 *this = iterator(I-1, LookInParentCtx());
269 else // No more decls.
270 *this = end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000271}
272
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000273
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000274//===----------------------------------------------------------------------===//
275// IdDeclInfoMap Implementation
276//===----------------------------------------------------------------------===//
277
Douglas Gregor2def4832008-11-17 20:34:05 +0000278/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000279/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000280IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000281IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
282 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000283
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000284 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000285
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000286 if (CurIndex == VECTOR_SIZE) {
287 // Add a IdDeclInfo vector 'pool'
Argyrios Kyrtzidis72e62b02008-04-12 12:38:58 +0000288 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000289 // Fill the vector
290 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000291 CurIndex = 0;
292 }
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000293 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000294 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000295 reinterpret_cast<uintptr_t>(IDI) | 0x1)
296 );
297 ++CurIndex;
298 return *IDI;
299}