blob: 762885e42c388ecd41f8b6236bbd6cd4f3c18087 [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 Gregor72de6672009-01-08 20:45:30 +000063 if (!Ctx) // FIXME: HACK! We shouldn't end up with a NULL context here.
64 return TUCtx();
65
Douglas Gregorce356072009-01-06 23:51:29 +000066 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +000067
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000068 if (isa<TranslationUnitDecl>(Ctx))
69 return TUCtx();
70
71 return Ctx;
72}
73
74/// isEqOrContainedBy - Returns true of the given context is the same or a
75/// parent of this one.
76bool IdentifierResolver::LookupContext::isEqOrContainedBy(
77 const LookupContext &PC) const {
78 if (PC.isTU()) return true;
79
80 for (LookupContext Next = *this; !Next.isTU(); Next = Next.getParent())
81 if (Next.Ctx == PC.Ctx) return true;
82
83 return false;
84}
85
86
87//===----------------------------------------------------------------------===//
88// IdDeclInfo Implementation
89//===----------------------------------------------------------------------===//
90
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000091/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
92/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
93/// be already added to the scope chain and must be in the same context as
94/// the decl that we want to add.
95void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
96 NamedDecl *Shadow) {
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000097 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
98 if (Shadow == *(I-1)) {
99 Decls.insert(I-1, D);
100 return;
101 }
102 }
103
104 assert(0 && "Shadow wasn't in scope chain!");
105}
106
107/// RemoveDecl - Remove the decl from the scope chain.
108/// The decl must already be part of the decl chain.
109void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
110 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
111 if (D == *(I-1)) {
112 Decls.erase(I-1);
113 return;
114 }
115 }
116
117 assert(0 && "Didn't find this decl on its identifier's chain!");
118}
119
120
121//===----------------------------------------------------------------------===//
122// IdentifierResolver Implementation
123//===----------------------------------------------------------------------===//
124
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000125IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
126 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
127}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000128IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000129 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000130}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000131
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000132/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
133/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
134/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000135bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregor44b43212008-12-11 16:49:14 +0000136 ASTContext &Context, Scope *S) const {
Douglas Gregorce356072009-01-06 23:51:29 +0000137 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +0000138
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000139 if (Ctx->isFunctionOrMethod()) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000140 // Ignore the scopes associated within transparent declaration contexts.
141 while (S->getEntity() &&
142 ((DeclContext *)S->getEntity())->isTransparentContext())
143 S = S->getParent();
144
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000145 if (S->isDeclScope(D))
146 return true;
147 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000148 // C++ 3.3.2p3:
149 // The name declared in a catch exception-declaration is local to the
150 // handler and shall not be redeclared in the outermost block of the
151 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000152 // C++ 3.3.2p4:
153 // Names declared in the for-init-statement, and in the condition of if,
154 // while, for, and switch statements are local to the if, while, for, or
155 // switch statement (including the controlled statement), and shall not be
156 // redeclared in a subsequent condition of that statement nor in the
157 // outermost block (or, for the if statement, any of the outermost blocks)
158 // of the controlled statement.
159 //
160 assert(S->getParent() && "No TUScope?");
161 if (S->getParent()->getFlags() & Scope::ControlScope)
162 return S->getParent()->isDeclScope(D);
163 }
164 return false;
165 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000166
Steve Naroff0701bbb2009-01-08 17:28:14 +0000167 return LookupContext(D) == LookupContext(Ctx->getPrimaryContext());
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000168}
169
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000170/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000171void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000172 DeclarationName Name = D->getDeclName();
173 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000174
175 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000176 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000177 return;
178 }
179
180 IdDeclInfo *IDI;
181
182 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000183 Name.setFETokenInfo(NULL);
184 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000185 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
186 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000187 } else
188 IDI = toIdDeclInfo(Ptr);
189
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000190 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000191}
192
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000193/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000194/// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000195/// encountered before the 'D' decl.
196void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000197 assert(D->getDeclName() == Shadow->getDeclName() && "Different ids!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000198
Douglas Gregor2def4832008-11-17 20:34:05 +0000199 DeclarationName Name = D->getDeclName();
200 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000201 assert(Ptr && "No decl from Ptr ?");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000202
203 IdDeclInfo *IDI;
204
205 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000206 Name.setFETokenInfo(NULL);
207 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000208 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
209 assert(PrevD == Shadow && "Invalid shadow decl ?");
210 IDI->AddDecl(D);
211 IDI->AddDecl(PrevD);
212 return;
213 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000214
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000215 IDI = toIdDeclInfo(Ptr);
216 IDI->AddShadowed(D, Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000217}
218
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000219/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000220/// The decl must already be part of the decl chain.
221void IdentifierResolver::RemoveDecl(NamedDecl *D) {
222 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000223 DeclarationName Name = D->getDeclName();
224 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000225
226 assert(Ptr && "Didn't find this decl on its identifier's chain!");
227
228 if (isDeclPtr(Ptr)) {
229 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000230 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000231 return;
232 }
233
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000234 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000235}
236
Douglas Gregor2def4832008-11-17 20:34:05 +0000237/// begin - Returns an iterator for decls with name 'Name', starting at
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000238/// declaration context 'Ctx'. If 'LookInParentCtx' is true, it will walk the
239/// decls of parent declaration contexts too.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000240IdentifierResolver::iterator
Douglas Gregor2def4832008-11-17 20:34:05 +0000241IdentifierResolver::begin(DeclarationName Name, const DeclContext *Ctx,
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000242 bool LookInParentCtx) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000243 assert(Ctx && "null param passed");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000244
Douglas Gregor2def4832008-11-17 20:34:05 +0000245 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000246 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000247
Chris Lattnera2f42b12008-04-11 07:06:57 +0000248 if (isDeclPtr(Ptr)) {
249 NamedDecl *D = static_cast<NamedDecl*>(Ptr);
Douglas Gregor074149e2009-01-05 19:45:36 +0000250 return iterator(D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000251 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000252
Chris Lattnera2f42b12008-04-11 07:06:57 +0000253 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000254
Douglas Gregor074149e2009-01-05 19:45:36 +0000255 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000256 if (I != IDI->decls_begin())
257 return iterator(I-1, LookInParentCtx);
258 else // No decls found.
259 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000260}
261
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000262/// PreIncIter - Do a preincrement when 'Ptr' is a BaseIter.
263void IdentifierResolver::iterator::PreIncIter() {
264 NamedDecl *D = **this;
Douglas Gregor2def4832008-11-17 20:34:05 +0000265 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000266 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
267 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000268
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000269 BaseIter I = getIterator();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000270 if (I != Info->decls_begin())
271 *this = iterator(I-1, LookInParentCtx());
272 else // No more decls.
273 *this = end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000274}
275
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000276
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000277//===----------------------------------------------------------------------===//
278// IdDeclInfoMap Implementation
279//===----------------------------------------------------------------------===//
280
Douglas Gregor2def4832008-11-17 20:34:05 +0000281/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000282/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000283IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000284IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
285 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000286
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000287 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000288
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000289 if (CurIndex == VECTOR_SIZE) {
290 // Add a IdDeclInfo vector 'pool'
Argyrios Kyrtzidis72e62b02008-04-12 12:38:58 +0000291 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000292 // Fill the vector
293 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000294 CurIndex = 0;
295 }
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000296 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000297 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000298 reinterpret_cast<uintptr_t>(IDI) | 0x1)
299 );
300 ++CurIndex;
301 return *IDI;
302}