blob: 18e78a0d1703be11f7b567ffa55a335aee52da03 [file] [log] [blame]
Chris Lattnerefa937a2008-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//
Argiris Kirtzidis6bfafc02008-04-12 01:50:47 +000010// This file implements the IdentifierResolver class, which is used for lexical
Douglas Gregorb0212bd2008-11-17 20:34:05 +000011// scoped lookup, based on declaration names.
Chris Lattnerefa937a2008-04-11 07:06:57 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "IdentifierResolver.h"
Argiris Kirtzidisaaa4dc62008-09-09 21:57:58 +000016#include "clang/Basic/LangOptions.h"
Chris Lattnerefa937a2008-04-11 07:06:57 +000017#include <list>
Argiris Kirtzidis6bfafc02008-04-12 01:50:47 +000018#include <vector>
Chris Lattnerefa937a2008-04-11 07:06:57 +000019
20using namespace clang;
21
Argiris Kirtzidis9c164b22008-09-09 19:28:27 +000022//===----------------------------------------------------------------------===//
23// IdDeclInfoMap class
24//===----------------------------------------------------------------------===//
Chris Lattnerefa937a2008-04-11 07:06:57 +000025
Douglas Gregorb0212bd2008-11-17 20:34:05 +000026/// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
Argiris Kirtzidis6bfafc02008-04-12 01:50:47 +000027/// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
Chris Lattnerefa937a2008-04-11 07:06:57 +000028/// individual IdDeclInfo to heap.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +000029class IdentifierResolver::IdDeclInfoMap {
Argiris Kirtzidis6bfafc02008-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 Lattnerefa937a2008-04-11 07:06:57 +000034 unsigned int CurIndex;
35
36public:
Argiris Kirtzidis6bfafc02008-04-12 01:50:47 +000037 IdDeclInfoMap() : CurIndex(VECTOR_SIZE) {}
Chris Lattnerefa937a2008-04-11 07:06:57 +000038
Douglas Gregorb0212bd2008-11-17 20:34:05 +000039 /// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnerefa937a2008-04-11 07:06:57 +000040 /// It creates a new IdDeclInfo if one was not created before for this id.
Douglas Gregorb0212bd2008-11-17 20:34:05 +000041 IdDeclInfo &operator[](DeclarationName Name);
Chris Lattnerefa937a2008-04-11 07:06:57 +000042};
43
Argiris Kirtzidise6369b02008-04-14 00:09:21 +000044
Argiris Kirtzidis9c164b22008-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
Argiris Kirtzidis9c164b22008-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 Gregord2baafd2008-10-21 16:13:35 +000057 Ctx = SD->getDeclContext();
58 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D))
59 Ctx = Ovl->getDeclContext();
Argiris Kirtzidis9c164b22008-09-09 19:28:27 +000060 else
61 return TUCtx();
62
Douglas Gregord8028382009-01-05 19:45:36 +000063 while (Ctx->isTransparentContext())
64 Ctx = Ctx->getParent();
65
Argiris Kirtzidis9c164b22008-09-09 19:28:27 +000066 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
Argiris Kirtzidis9c164b22008-09-09 19:28:27 +000089/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
90/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
91/// be already added to the scope chain and must be in the same context as
92/// the decl that we want to add.
93void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
94 NamedDecl *Shadow) {
Argiris Kirtzidis9c164b22008-09-09 19:28:27 +000095 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
96 if (Shadow == *(I-1)) {
97 Decls.insert(I-1, D);
98 return;
99 }
100 }
101
102 assert(0 && "Shadow wasn't in scope chain!");
103}
104
105/// RemoveDecl - Remove the decl from the scope chain.
106/// The decl must already be part of the decl chain.
107void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
108 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
109 if (D == *(I-1)) {
110 Decls.erase(I-1);
111 return;
112 }
113 }
114
115 assert(0 && "Didn't find this decl on its identifier's chain!");
116}
117
118
119//===----------------------------------------------------------------------===//
120// IdentifierResolver Implementation
121//===----------------------------------------------------------------------===//
122
Argiris Kirtzidis71579512008-09-09 21:32:02 +0000123IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
124 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
125}
Argiris Kirtzidise6369b02008-04-14 00:09:21 +0000126IdentifierResolver::~IdentifierResolver() {
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000127 delete IdDeclInfos;
Argiris Kirtzidise6369b02008-04-14 00:09:21 +0000128}
Chris Lattnerefa937a2008-04-11 07:06:57 +0000129
Argiris Kirtzidis9c164b22008-09-09 19:28:27 +0000130/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
131/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
132/// true if 'D' belongs to the given declaration context.
Argiris Kirtzidisaaa4dc62008-09-09 21:57:58 +0000133bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregor8acb7272008-12-11 16:49:14 +0000134 ASTContext &Context, Scope *S) const {
Douglas Gregord8028382009-01-05 19:45:36 +0000135 while (Ctx->isTransparentContext())
136 Ctx = Ctx->getParent();
137
Argiris Kirtzidisaaa4dc62008-09-09 21:57:58 +0000138 if (Ctx->isFunctionOrMethod()) {
Douglas Gregord8028382009-01-05 19:45:36 +0000139 // Ignore the scopes associated within transparent declaration contexts.
140 while (S->getEntity() &&
141 ((DeclContext *)S->getEntity())->isTransparentContext())
142 S = S->getParent();
143
Argiris Kirtzidisaaa4dc62008-09-09 21:57:58 +0000144 if (S->isDeclScope(D))
145 return true;
146 if (LangOpt.CPlusPlus) {
Sebastian Redlb12ac332008-12-21 16:41:36 +0000147 // C++ 3.3.2p3:
148 // The name declared in a catch exception-declaration is local to the
149 // handler and shall not be redeclared in the outermost block of the
150 // handler.
Argiris Kirtzidisaaa4dc62008-09-09 21:57:58 +0000151 // C++ 3.3.2p4:
152 // Names declared in the for-init-statement, and in the condition of if,
153 // while, for, and switch statements are local to the if, while, for, or
154 // switch statement (including the controlled statement), and shall not be
155 // redeclared in a subsequent condition of that statement nor in the
156 // outermost block (or, for the if statement, any of the outermost blocks)
157 // of the controlled statement.
158 //
159 assert(S->getParent() && "No TUScope?");
160 if (S->getParent()->getFlags() & Scope::ControlScope)
161 return S->getParent()->isDeclScope(D);
162 }
163 return false;
164 }
Argiris Kirtzidis9c164b22008-09-09 19:28:27 +0000165
Douglas Gregor8acb7272008-12-11 16:49:14 +0000166 return LookupContext(D) == LookupContext(Ctx->getPrimaryContext(Context));
Argiris Kirtzidis9c164b22008-09-09 19:28:27 +0000167}
168
Argiris Kirtzidis6bfafc02008-04-12 01:50:47 +0000169/// AddDecl - Link the decl to its shadowed decl chain.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000170void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000171 DeclarationName Name = D->getDeclName();
172 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnerefa937a2008-04-11 07:06:57 +0000173
174 if (!Ptr) {
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000175 Name.setFETokenInfo(D);
Chris Lattnerefa937a2008-04-11 07:06:57 +0000176 return;
177 }
178
179 IdDeclInfo *IDI;
180
181 if (isDeclPtr(Ptr)) {
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000182 Name.setFETokenInfo(NULL);
183 IDI = &(*IdDeclInfos)[Name];
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000184 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
185 IDI->AddDecl(PrevD);
Chris Lattnerefa937a2008-04-11 07:06:57 +0000186 } else
187 IDI = toIdDeclInfo(Ptr);
188
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000189 IDI->AddDecl(D);
Chris Lattnerefa937a2008-04-11 07:06:57 +0000190}
191
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000192/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argiris Kirtzidis207c3ce2008-05-15 17:26:35 +0000193/// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000194/// encountered before the 'D' decl.
195void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000196 assert(D->getDeclName() == Shadow->getDeclName() && "Different ids!");
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000197
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000198 DeclarationName Name = D->getDeclName();
199 void *Ptr = Name.getFETokenInfo<void>();
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000200 assert(Ptr && "No decl from Ptr ?");
Chris Lattnerefa937a2008-04-11 07:06:57 +0000201
202 IdDeclInfo *IDI;
203
204 if (isDeclPtr(Ptr)) {
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000205 Name.setFETokenInfo(NULL);
206 IDI = &(*IdDeclInfos)[Name];
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000207 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
208 assert(PrevD == Shadow && "Invalid shadow decl ?");
209 IDI->AddDecl(D);
210 IDI->AddDecl(PrevD);
211 return;
212 }
Chris Lattnerefa937a2008-04-11 07:06:57 +0000213
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000214 IDI = toIdDeclInfo(Ptr);
215 IDI->AddShadowed(D, Shadow);
Chris Lattnerefa937a2008-04-11 07:06:57 +0000216}
217
Argiris Kirtzidis6bfafc02008-04-12 01:50:47 +0000218/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnerefa937a2008-04-11 07:06:57 +0000219/// The decl must already be part of the decl chain.
220void IdentifierResolver::RemoveDecl(NamedDecl *D) {
221 assert(D && "null param passed");
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000222 DeclarationName Name = D->getDeclName();
223 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnerefa937a2008-04-11 07:06:57 +0000224
225 assert(Ptr && "Didn't find this decl on its identifier's chain!");
226
227 if (isDeclPtr(Ptr)) {
228 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000229 Name.setFETokenInfo(NULL);
Chris Lattnerefa937a2008-04-11 07:06:57 +0000230 return;
231 }
232
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000233 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnerefa937a2008-04-11 07:06:57 +0000234}
235
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000236/// begin - Returns an iterator for decls with name 'Name', starting at
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000237/// declaration context 'Ctx'. If 'LookInParentCtx' is true, it will walk the
238/// decls of parent declaration contexts too.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000239IdentifierResolver::iterator
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000240IdentifierResolver::begin(DeclarationName Name, const DeclContext *Ctx,
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000241 bool LookInParentCtx) {
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000242 assert(Ctx && "null param passed");
Chris Lattnerefa937a2008-04-11 07:06:57 +0000243
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000244 void *Ptr = Name.getFETokenInfo<void>();
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000245 if (!Ptr) return end();
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000246
Chris Lattnerefa937a2008-04-11 07:06:57 +0000247 if (isDeclPtr(Ptr)) {
248 NamedDecl *D = static_cast<NamedDecl*>(Ptr);
Douglas Gregord8028382009-01-05 19:45:36 +0000249 return iterator(D);
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000250 }
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000251
Chris Lattnerefa937a2008-04-11 07:06:57 +0000252 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000253
Douglas Gregord8028382009-01-05 19:45:36 +0000254 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000255 if (I != IDI->decls_begin())
256 return iterator(I-1, LookInParentCtx);
257 else // No decls found.
258 return end();
Chris Lattnerefa937a2008-04-11 07:06:57 +0000259}
260
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000261/// PreIncIter - Do a preincrement when 'Ptr' is a BaseIter.
262void IdentifierResolver::iterator::PreIncIter() {
263 NamedDecl *D = **this;
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000264 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000265 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
266 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
Chris Lattnerefa937a2008-04-11 07:06:57 +0000267
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000268 BaseIter I = getIterator();
Argiris Kirtzidis94805232008-07-17 17:49:50 +0000269 if (I != Info->decls_begin())
270 *this = iterator(I-1, LookInParentCtx());
271 else // No more decls.
272 *this = end();
Chris Lattnerefa937a2008-04-11 07:06:57 +0000273}
274
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000275
Argiris Kirtzidis9c164b22008-09-09 19:28:27 +0000276//===----------------------------------------------------------------------===//
277// IdDeclInfoMap Implementation
278//===----------------------------------------------------------------------===//
279
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000280/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnerefa937a2008-04-11 07:06:57 +0000281/// It creates a new IdDeclInfo if one was not created before for this id.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000282IdentifierResolver::IdDeclInfo &
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000283IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
284 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnerefa937a2008-04-11 07:06:57 +0000285
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000286 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnerefa937a2008-04-11 07:06:57 +0000287
Argiris Kirtzidis6bfafc02008-04-12 01:50:47 +0000288 if (CurIndex == VECTOR_SIZE) {
289 // Add a IdDeclInfo vector 'pool'
Argiris Kirtzidisc31c9052008-04-12 12:38:58 +0000290 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argiris Kirtzidis6bfafc02008-04-12 01:50:47 +0000291 // Fill the vector
292 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnerefa937a2008-04-11 07:06:57 +0000293 CurIndex = 0;
294 }
Argiris Kirtzidis6bfafc02008-04-12 01:50:47 +0000295 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Douglas Gregorb0212bd2008-11-17 20:34:05 +0000296 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnerefa937a2008-04-11 07:06:57 +0000297 reinterpret_cast<uintptr_t>(IDI) | 0x1)
298 );
299 ++CurIndex;
300 return *IDI;
301}