blob: 781d89a0e99ad4e1ef1c63f85c0b30e79e3da59b [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
Douglas Gregor4afa39d2009-01-20 01:17:11 +000049/// getContext - Returns translation unit context for non Decls and
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000050/// for EnumConstantDecls returns the parent context of their EnumDecl.
51DeclContext *IdentifierResolver::LookupContext::getContext(Decl *D) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +000052 DeclContext *Ctx = D->getDeclContext();
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000053
Douglas Gregor72de6672009-01-08 20:45:30 +000054 if (!Ctx) // FIXME: HACK! We shouldn't end up with a NULL context here.
55 return TUCtx();
56
Douglas Gregorce356072009-01-06 23:51:29 +000057 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +000058
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000059 if (isa<TranslationUnitDecl>(Ctx))
60 return TUCtx();
61
62 return Ctx;
63}
64
65/// isEqOrContainedBy - Returns true of the given context is the same or a
66/// parent of this one.
67bool IdentifierResolver::LookupContext::isEqOrContainedBy(
68 const LookupContext &PC) const {
69 if (PC.isTU()) return true;
70
71 for (LookupContext Next = *this; !Next.isTU(); Next = Next.getParent())
72 if (Next.Ctx == PC.Ctx) return true;
73
74 return false;
75}
76
77
78//===----------------------------------------------------------------------===//
79// IdDeclInfo Implementation
80//===----------------------------------------------------------------------===//
81
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000082/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
83/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
84/// be already added to the scope chain and must be in the same context as
85/// the decl that we want to add.
86void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
87 NamedDecl *Shadow) {
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000088 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
89 if (Shadow == *(I-1)) {
90 Decls.insert(I-1, D);
91 return;
92 }
93 }
94
95 assert(0 && "Shadow wasn't in scope chain!");
96}
97
98/// RemoveDecl - Remove the decl from the scope chain.
99/// The decl must already be part of the decl chain.
100void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
101 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
102 if (D == *(I-1)) {
103 Decls.erase(I-1);
104 return;
105 }
106 }
107
108 assert(0 && "Didn't find this decl on its identifier's chain!");
109}
110
111
112//===----------------------------------------------------------------------===//
113// IdentifierResolver Implementation
114//===----------------------------------------------------------------------===//
115
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000116IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
117 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
118}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000119IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000120 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000121}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000122
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000123/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
124/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
125/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000126bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregor44b43212008-12-11 16:49:14 +0000127 ASTContext &Context, Scope *S) const {
Douglas Gregorce356072009-01-06 23:51:29 +0000128 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +0000129
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000130 if (Ctx->isFunctionOrMethod()) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000131 // Ignore the scopes associated within transparent declaration contexts.
132 while (S->getEntity() &&
133 ((DeclContext *)S->getEntity())->isTransparentContext())
134 S = S->getParent();
135
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000136 if (S->isDeclScope(D))
137 return true;
138 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000139 // C++ 3.3.2p3:
140 // The name declared in a catch exception-declaration is local to the
141 // handler and shall not be redeclared in the outermost block of the
142 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000143 // C++ 3.3.2p4:
144 // Names declared in the for-init-statement, and in the condition of if,
145 // while, for, and switch statements are local to the if, while, for, or
146 // switch statement (including the controlled statement), and shall not be
147 // redeclared in a subsequent condition of that statement nor in the
148 // outermost block (or, for the if statement, any of the outermost blocks)
149 // of the controlled statement.
150 //
151 assert(S->getParent() && "No TUScope?");
152 if (S->getParent()->getFlags() & Scope::ControlScope)
153 return S->getParent()->isDeclScope(D);
154 }
155 return false;
156 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000157
Steve Naroff0701bbb2009-01-08 17:28:14 +0000158 return LookupContext(D) == LookupContext(Ctx->getPrimaryContext());
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000159}
160
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000161/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000162void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000163 DeclarationName Name = D->getDeclName();
164 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000165
166 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000167 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000168 return;
169 }
170
171 IdDeclInfo *IDI;
172
173 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000174 Name.setFETokenInfo(NULL);
175 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000176 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
177 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000178 } else
179 IDI = toIdDeclInfo(Ptr);
180
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000181 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000182}
183
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000184/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000185/// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000186/// encountered before the 'D' decl.
187void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000188 assert(D->getDeclName() == Shadow->getDeclName() && "Different ids!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000189
Douglas Gregor2def4832008-11-17 20:34:05 +0000190 DeclarationName Name = D->getDeclName();
191 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000192 assert(Ptr && "No decl from Ptr ?");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000193
194 IdDeclInfo *IDI;
195
196 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000197 Name.setFETokenInfo(NULL);
198 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000199 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
200 assert(PrevD == Shadow && "Invalid shadow decl ?");
201 IDI->AddDecl(D);
202 IDI->AddDecl(PrevD);
203 return;
204 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000205
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000206 IDI = toIdDeclInfo(Ptr);
207 IDI->AddShadowed(D, Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000208}
209
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000210/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000211/// The decl must already be part of the decl chain.
212void IdentifierResolver::RemoveDecl(NamedDecl *D) {
213 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000214 DeclarationName Name = D->getDeclName();
215 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000216
217 assert(Ptr && "Didn't find this decl on its identifier's chain!");
218
219 if (isDeclPtr(Ptr)) {
220 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000221 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000222 return;
223 }
224
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000225 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000226}
227
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000228/// begin - Returns an iterator for decls with name 'Name'.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000229IdentifierResolver::iterator
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000230IdentifierResolver::begin(DeclarationName Name) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000231 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000232 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000233
Chris Lattnera2f42b12008-04-11 07:06:57 +0000234 if (isDeclPtr(Ptr)) {
235 NamedDecl *D = static_cast<NamedDecl*>(Ptr);
Douglas Gregor074149e2009-01-05 19:45:36 +0000236 return iterator(D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000237 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000238
Chris Lattnera2f42b12008-04-11 07:06:57 +0000239 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000240
Douglas Gregor074149e2009-01-05 19:45:36 +0000241 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000242 if (I != IDI->decls_begin())
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000243 return iterator(I-1);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000244 else // No decls found.
245 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000246}
247
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000248//===----------------------------------------------------------------------===//
249// IdDeclInfoMap Implementation
250//===----------------------------------------------------------------------===//
251
Douglas Gregor2def4832008-11-17 20:34:05 +0000252/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000253/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000254IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000255IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
256 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000257
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000258 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000259
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000260 if (CurIndex == VECTOR_SIZE) {
261 // Add a IdDeclInfo vector 'pool'
Argyrios Kyrtzidis72e62b02008-04-12 12:38:58 +0000262 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000263 // Fill the vector
264 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000265 CurIndex = 0;
266 }
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000267 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000268 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000269 reinterpret_cast<uintptr_t>(IDI) | 0x1)
270 );
271 ++CurIndex;
272 return *IDI;
273}