blob: ceab859c90aa025ba1d46a49e75a1b293afdb9a4 [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//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000046// IdDeclInfo Implementation
47//===----------------------------------------------------------------------===//
48
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000049/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
50/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
51/// be already added to the scope chain and must be in the same context as
52/// the decl that we want to add.
53void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
54 NamedDecl *Shadow) {
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000055 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
56 if (Shadow == *(I-1)) {
57 Decls.insert(I-1, D);
58 return;
59 }
60 }
61
62 assert(0 && "Shadow wasn't in scope chain!");
63}
64
65/// RemoveDecl - Remove the decl from the scope chain.
66/// The decl must already be part of the decl chain.
67void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
68 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
69 if (D == *(I-1)) {
70 Decls.erase(I-1);
71 return;
72 }
73 }
74
75 assert(0 && "Didn't find this decl on its identifier's chain!");
76}
77
Douglas Gregor63935192009-03-02 00:19:53 +000078bool
79IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
80 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
81 if (Old == *(I-1)) {
82 *(I - 1) = New;
83 return true;
84 }
85 }
86
87 return false;
88}
89
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000090
91//===----------------------------------------------------------------------===//
92// IdentifierResolver Implementation
93//===----------------------------------------------------------------------===//
94
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +000095IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
96 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
97}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000098IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000099 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +0000100}
Chris Lattnera2f42b12008-04-11 07:06:57 +0000101
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000102/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
103/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
104/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000105bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregor44b43212008-12-11 16:49:14 +0000106 ASTContext &Context, Scope *S) const {
Douglas Gregorce356072009-01-06 23:51:29 +0000107 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +0000108
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000109 if (Ctx->isFunctionOrMethod()) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000110 // Ignore the scopes associated within transparent declaration contexts.
111 while (S->getEntity() &&
112 ((DeclContext *)S->getEntity())->isTransparentContext())
113 S = S->getParent();
114
Chris Lattnerb28317a2009-03-28 19:18:32 +0000115 if (S->isDeclScope(Action::DeclPtrTy::make(D)))
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000116 return true;
117 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000118 // C++ 3.3.2p3:
119 // The name declared in a catch exception-declaration is local to the
120 // handler and shall not be redeclared in the outermost block of the
121 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000122 // C++ 3.3.2p4:
123 // Names declared in the for-init-statement, and in the condition of if,
124 // while, for, and switch statements are local to the if, while, for, or
125 // switch statement (including the controlled statement), and shall not be
126 // redeclared in a subsequent condition of that statement nor in the
127 // outermost block (or, for the if statement, any of the outermost blocks)
128 // of the controlled statement.
129 //
130 assert(S->getParent() && "No TUScope?");
131 if (S->getParent()->getFlags() & Scope::ControlScope)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000132 return S->getParent()->isDeclScope(Action::DeclPtrTy::make(D));
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000133 }
134 return false;
135 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000136
Argyrios Kyrtzidis7fd46da2009-02-17 20:21:51 +0000137 return D->getDeclContext()->getLookupContext() == Ctx->getPrimaryContext();
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000138}
139
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000140/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000141void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000142 DeclarationName Name = D->getDeclName();
143 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000144
145 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000146 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000147 return;
148 }
149
150 IdDeclInfo *IDI;
151
152 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000153 Name.setFETokenInfo(NULL);
154 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000155 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
156 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000157 } else
158 IDI = toIdDeclInfo(Ptr);
159
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000160 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000161}
162
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000163/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000164/// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000165/// encountered before the 'D' decl.
166void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000167 assert(D->getDeclName() == Shadow->getDeclName() && "Different ids!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000168
Douglas Gregor2def4832008-11-17 20:34:05 +0000169 DeclarationName Name = D->getDeclName();
170 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000171 assert(Ptr && "No decl from Ptr ?");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000172
173 IdDeclInfo *IDI;
174
175 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000176 Name.setFETokenInfo(NULL);
177 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000178 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
179 assert(PrevD == Shadow && "Invalid shadow decl ?");
180 IDI->AddDecl(D);
181 IDI->AddDecl(PrevD);
182 return;
183 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000184
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000185 IDI = toIdDeclInfo(Ptr);
186 IDI->AddShadowed(D, Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000187}
188
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000189/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000190/// The decl must already be part of the decl chain.
191void IdentifierResolver::RemoveDecl(NamedDecl *D) {
192 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000193 DeclarationName Name = D->getDeclName();
194 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000195
196 assert(Ptr && "Didn't find this decl on its identifier's chain!");
197
198 if (isDeclPtr(Ptr)) {
199 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000200 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000201 return;
202 }
203
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000204 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000205}
206
Douglas Gregor63935192009-03-02 00:19:53 +0000207bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
208 assert(Old->getDeclName() == New->getDeclName() &&
209 "Cannot replace a decl with another decl of a different name");
210
211 DeclarationName Name = Old->getDeclName();
212 void *Ptr = Name.getFETokenInfo<void>();
213
214 if (!Ptr)
215 return false;
216
217 if (isDeclPtr(Ptr)) {
218 if (Ptr == Old) {
219 Name.setFETokenInfo(New);
220 return true;
221 }
222 return false;
223 }
224
225 return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
226}
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 Lattner0b2b6e12009-03-04 06:34:08 +0000234 if (isDeclPtr(Ptr))
235 return iterator(static_cast<NamedDecl*>(Ptr));
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000236
Chris Lattnera2f42b12008-04-11 07:06:57 +0000237 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000238
Douglas Gregor074149e2009-01-05 19:45:36 +0000239 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000240 if (I != IDI->decls_begin())
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000241 return iterator(I-1);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000242 // No decls found.
243 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000244}
245
Douglas Gregor668c1a42009-04-21 22:25:48 +0000246void IdentifierResolver::AddDeclToIdentifierChain(IdentifierInfo *II,
247 NamedDecl *D) {
248 void *Ptr = II->getFETokenInfo<void>();
249
250 if (!Ptr) {
251 II->setFETokenInfo(D);
252 return;
253 }
254
255 IdDeclInfo *IDI;
256
257 if (isDeclPtr(Ptr)) {
258 II->setFETokenInfo(NULL);
259 IDI = &(*IdDeclInfos)[II];
260 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
261 IDI->AddDecl(PrevD);
262 } else
263 IDI = toIdDeclInfo(Ptr);
264
265 IDI->AddDecl(D);
266}
267
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000268//===----------------------------------------------------------------------===//
269// IdDeclInfoMap Implementation
270//===----------------------------------------------------------------------===//
271
Douglas Gregor2def4832008-11-17 20:34:05 +0000272/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000273/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000274IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000275IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
276 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000277
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000278 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000279
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000280 if (CurIndex == VECTOR_SIZE) {
281 // Add a IdDeclInfo vector 'pool'
Argyrios Kyrtzidis72e62b02008-04-12 12:38:58 +0000282 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000283 // Fill the vector
284 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000285 CurIndex = 0;
286 }
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000287 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000288 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000289 reinterpret_cast<uintptr_t>(IDI) | 0x1)
290 );
291 ++CurIndex;
292 return *IDI;
293}