1 //===- IdentifierResolver.cpp - Lexical Scope Name lookup -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the IdentifierResolver class, which is used for lexical
11 // scoped lookup, based on declaration names.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Sema/IdentifierResolver.h"
16 #include "clang/Sema/Scope.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/Basic/LangOptions.h"
20 #include "clang/Lex/ExternalPreprocessorSource.h"
21 #include "clang/Lex/Preprocessor.h"
23 using namespace clang;
25 //===----------------------------------------------------------------------===//
26 // IdDeclInfoMap class
27 //===----------------------------------------------------------------------===//
29 /// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
30 /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
31 /// individual IdDeclInfo to heap.
32 class IdentifierResolver::IdDeclInfoMap {
33 static const unsigned int POOL_SIZE = 512;
35 /// We use our own linked-list implementation because it is sadly
36 /// impossible to add something to a pre-C++0x STL container without
37 /// a completely unnecessary copy.
38 struct IdDeclInfoPool {
39 IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
42 IdDeclInfo Pool[POOL_SIZE];
45 IdDeclInfoPool *CurPool;
46 unsigned int CurIndex;
49 IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE) {}
52 IdDeclInfoPool *Cur = CurPool;
53 while (IdDeclInfoPool *P = Cur) {
59 /// Returns the IdDeclInfo associated to the DeclarationName.
60 /// It creates a new IdDeclInfo if one was not created before for this id.
61 IdDeclInfo &operator[](DeclarationName Name);
65 //===----------------------------------------------------------------------===//
66 // IdDeclInfo Implementation
67 //===----------------------------------------------------------------------===//
69 /// RemoveDecl - Remove the decl from the scope chain.
70 /// The decl must already be part of the decl chain.
71 void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
72 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
79 llvm_unreachable("Didn't find this decl on its identifier's chain!");
83 IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
84 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
95 //===----------------------------------------------------------------------===//
96 // IdentifierResolver Implementation
97 //===----------------------------------------------------------------------===//
99 IdentifierResolver::IdentifierResolver(Preprocessor &PP)
100 : LangOpt(PP.getLangOpts()), PP(PP),
101 IdDeclInfos(new IdDeclInfoMap) {
104 IdentifierResolver::~IdentifierResolver() {
108 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
109 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
110 /// true if 'D' belongs to the given declaration context.
111 bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
112 ASTContext &Context, Scope *S,
113 bool ExplicitInstantiationOrSpecialization) const {
114 Ctx = Ctx->getRedeclContext();
116 if (Ctx->isFunctionOrMethod() || S->isFunctionPrototypeScope()) {
117 // Ignore the scopes associated within transparent declaration contexts.
118 while (S->getEntity() &&
119 ((DeclContext *)S->getEntity())->isTransparentContext())
122 if (S->isDeclScope(D))
124 if (LangOpt.CPlusPlus) {
126 // The name declared in a catch exception-declaration is local to the
127 // handler and shall not be redeclared in the outermost block of the
130 // Names declared in the for-init-statement, and in the condition of if,
131 // while, for, and switch statements are local to the if, while, for, or
132 // switch statement (including the controlled statement), and shall not be
133 // redeclared in a subsequent condition of that statement nor in the
134 // outermost block (or, for the if statement, any of the outermost blocks)
135 // of the controlled statement.
137 assert(S->getParent() && "No TUScope?");
138 if (S->getParent()->getFlags() & Scope::ControlScope) {
140 if (S->isDeclScope(D))
143 if (S->getFlags() & Scope::FnTryCatchScope)
144 return S->getParent()->isDeclScope(D);
149 DeclContext *DCtx = D->getDeclContext()->getRedeclContext();
150 return ExplicitInstantiationOrSpecialization
151 ? Ctx->InEnclosingNamespaceSetOf(DCtx)
155 /// AddDecl - Link the decl to its shadowed decl chain.
156 void IdentifierResolver::AddDecl(NamedDecl *D) {
157 DeclarationName Name = D->getDeclName();
158 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
159 updatingIdentifier(*II);
161 void *Ptr = Name.getFETokenInfo<void>();
164 Name.setFETokenInfo(D);
170 if (isDeclPtr(Ptr)) {
171 Name.setFETokenInfo(NULL);
172 IDI = &(*IdDeclInfos)[Name];
173 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
176 IDI = toIdDeclInfo(Ptr);
181 void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
182 DeclarationName Name = D->getDeclName();
183 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
184 updatingIdentifier(*II);
186 void *Ptr = Name.getFETokenInfo<void>();
193 if (isDeclPtr(Ptr)) {
194 // We only have a single declaration: insert before or after it,
196 if (Pos == iterator()) {
197 // Add the new declaration before the existing declaration.
198 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
203 // Add new declaration after the existing declaration.
210 // General case: insert the declaration at the appropriate point in the
211 // list, which already has at least two elements.
212 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
213 if (Pos.isIterator()) {
214 IDI->InsertDecl(Pos.getIterator() + 1, D);
216 IDI->InsertDecl(IDI->decls_begin(), D);
219 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
220 /// The decl must already be part of the decl chain.
221 void IdentifierResolver::RemoveDecl(NamedDecl *D) {
222 assert(D && "null param passed");
223 DeclarationName Name = D->getDeclName();
224 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
225 updatingIdentifier(*II);
227 void *Ptr = Name.getFETokenInfo<void>();
229 assert(Ptr && "Didn't find this decl on its identifier's chain!");
231 if (isDeclPtr(Ptr)) {
232 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
233 Name.setFETokenInfo(NULL);
237 return toIdDeclInfo(Ptr)->RemoveDecl(D);
240 bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
241 assert(Old->getDeclName() == New->getDeclName() &&
242 "Cannot replace a decl with another decl of a different name");
244 DeclarationName Name = Old->getDeclName();
245 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
246 updatingIdentifier(*II);
248 void *Ptr = Name.getFETokenInfo<void>();
253 if (isDeclPtr(Ptr)) {
255 Name.setFETokenInfo(New);
261 return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
264 /// begin - Returns an iterator for decls with name 'Name'.
265 IdentifierResolver::iterator
266 IdentifierResolver::begin(DeclarationName Name) {
267 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
268 readingIdentifier(*II);
270 void *Ptr = Name.getFETokenInfo<void>();
271 if (!Ptr) return end();
274 return iterator(static_cast<NamedDecl*>(Ptr));
276 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
278 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
279 if (I != IDI->decls_begin())
280 return iterator(I-1);
293 /// \brief Compare two declarations to see whether they are different or,
294 /// if they are the same, whether the new declaration should replace the
295 /// existing declaration.
296 static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) {
297 // If the declarations are identical, ignore the new one.
301 // If the declarations have different kinds, they're obviously different.
302 if (Existing->getKind() != New->getKind())
303 return DMK_Different;
305 // If the declarations are redeclarations of each other, keep the newest one.
306 if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) {
307 // If the existing declaration is somewhere in the previous declaration
308 // chain of the new declaration, then prefer the new declaration.
309 for (Decl::redecl_iterator RD = New->redecls_begin(),
310 RDEnd = New->redecls_end();
315 if (RD->isCanonicalDecl())
322 return DMK_Different;
325 bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){
326 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
327 readingIdentifier(*II);
329 void *Ptr = Name.getFETokenInfo<void>();
332 Name.setFETokenInfo(D);
338 if (isDeclPtr(Ptr)) {
339 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
341 switch (compareDeclarations(PrevD, D)) {
349 Name.setFETokenInfo(D);
353 Name.setFETokenInfo(NULL);
354 IDI = &(*IdDeclInfos)[Name];
356 // If the existing declaration is not visible in translation unit scope,
357 // then add the new top-level declaration first.
358 if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
368 IDI = toIdDeclInfo(Ptr);
370 // See whether this declaration is identical to any existing declarations.
371 // If not, find the right place to insert it.
372 for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(),
373 IEnd = IDI->decls_end();
376 switch (compareDeclarations(*I, D)) {
388 if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
389 // We've found a declaration that is not visible from the translation
390 // unit (it's in an inner scope). Insert our declaration here.
391 IDI->InsertDecl(I, D);
396 // Add the declaration to the end.
401 void IdentifierResolver::readingIdentifier(IdentifierInfo &II) {
402 if (II.isOutOfDate())
403 PP.getExternalSource()->updateOutOfDateIdentifier(II);
406 void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) {
407 if (II.isOutOfDate())
408 PP.getExternalSource()->updateOutOfDateIdentifier(II);
411 II.setChangedSinceDeserialization();
414 //===----------------------------------------------------------------------===//
415 // IdDeclInfoMap Implementation
416 //===----------------------------------------------------------------------===//
418 /// Returns the IdDeclInfo associated to the DeclarationName.
419 /// It creates a new IdDeclInfo if one was not created before for this id.
420 IdentifierResolver::IdDeclInfo &
421 IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
422 void *Ptr = Name.getFETokenInfo<void>();
424 if (Ptr) return *toIdDeclInfo(Ptr);
426 if (CurIndex == POOL_SIZE) {
427 CurPool = new IdDeclInfoPool(CurPool);
430 IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
431 Name.setFETokenInfo(reinterpret_cast<void*>(
432 reinterpret_cast<uintptr_t>(IDI) | 0x1)
438 void IdentifierResolver::iterator::incrementSlowCase() {
439 NamedDecl *D = **this;
440 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
441 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
442 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
444 BaseIter I = getIterator();
445 if (I != Info->decls_begin())
446 *this = iterator(I-1);
447 else // No more decls.