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
)
139 return S
->getParent()->isDeclScope(D
);
144 DeclContext
*DCtx
= D
->getDeclContext()->getRedeclContext();
145 return ExplicitInstantiationOrSpecialization
146 ? Ctx
->InEnclosingNamespaceSetOf(DCtx
)
150 /// AddDecl - Link the decl to its shadowed decl chain.
151 void IdentifierResolver::AddDecl(NamedDecl
*D
) {
152 DeclarationName Name
= D
->getDeclName();
153 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
154 updatingIdentifier(*II
);
156 void *Ptr
= Name
.getFETokenInfo
<void>();
159 Name
.setFETokenInfo(D
);
165 if (isDeclPtr(Ptr
)) {
166 Name
.setFETokenInfo(NULL
);
167 IDI
= &(*IdDeclInfos
)[Name
];
168 NamedDecl
*PrevD
= static_cast<NamedDecl
*>(Ptr
);
171 IDI
= toIdDeclInfo(Ptr
);
176 void IdentifierResolver::InsertDeclAfter(iterator Pos
, NamedDecl
*D
) {
177 DeclarationName Name
= D
->getDeclName();
178 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
179 updatingIdentifier(*II
);
181 void *Ptr
= Name
.getFETokenInfo
<void>();
188 if (isDeclPtr(Ptr
)) {
189 // We only have a single declaration: insert before or after it,
191 if (Pos
== iterator()) {
192 // Add the new declaration before the existing declaration.
193 NamedDecl
*PrevD
= static_cast<NamedDecl
*>(Ptr
);
198 // Add new declaration after the existing declaration.
205 // General case: insert the declaration at the appropriate point in the
206 // list, which already has at least two elements.
207 IdDeclInfo
*IDI
= toIdDeclInfo(Ptr
);
208 if (Pos
.isIterator()) {
209 IDI
->InsertDecl(Pos
.getIterator() + 1, D
);
211 IDI
->InsertDecl(IDI
->decls_begin(), D
);
214 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
215 /// The decl must already be part of the decl chain.
216 void IdentifierResolver::RemoveDecl(NamedDecl
*D
) {
217 assert(D
&& "null param passed");
218 DeclarationName Name
= D
->getDeclName();
219 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
220 updatingIdentifier(*II
);
222 void *Ptr
= Name
.getFETokenInfo
<void>();
224 assert(Ptr
&& "Didn't find this decl on its identifier's chain!");
226 if (isDeclPtr(Ptr
)) {
227 assert(D
== Ptr
&& "Didn't find this decl on its identifier's chain!");
228 Name
.setFETokenInfo(NULL
);
232 return toIdDeclInfo(Ptr
)->RemoveDecl(D
);
235 bool IdentifierResolver::ReplaceDecl(NamedDecl
*Old
, NamedDecl
*New
) {
236 assert(Old
->getDeclName() == New
->getDeclName() &&
237 "Cannot replace a decl with another decl of a different name");
239 DeclarationName Name
= Old
->getDeclName();
240 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
241 updatingIdentifier(*II
);
243 void *Ptr
= Name
.getFETokenInfo
<void>();
248 if (isDeclPtr(Ptr
)) {
250 Name
.setFETokenInfo(New
);
256 return toIdDeclInfo(Ptr
)->ReplaceDecl(Old
, New
);
259 /// begin - Returns an iterator for decls with name 'Name'.
260 IdentifierResolver::iterator
261 IdentifierResolver::begin(DeclarationName Name
) {
262 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
263 readingIdentifier(*II
);
265 void *Ptr
= Name
.getFETokenInfo
<void>();
266 if (!Ptr
) return end();
269 return iterator(static_cast<NamedDecl
*>(Ptr
));
271 IdDeclInfo
*IDI
= toIdDeclInfo(Ptr
);
273 IdDeclInfo::DeclsTy::iterator I
= IDI
->decls_end();
274 if (I
!= IDI
->decls_begin())
275 return iterator(I
-1);
288 /// \brief Compare two declarations to see whether they are different or,
289 /// if they are the same, whether the new declaration should replace the
290 /// existing declaration.
291 static DeclMatchKind
compareDeclarations(NamedDecl
*Existing
, NamedDecl
*New
) {
292 // If the declarations are identical, ignore the new one.
296 // If the declarations have different kinds, they're obviously different.
297 if (Existing
->getKind() != New
->getKind())
298 return DMK_Different
;
300 // If the declarations are redeclarations of each other, keep the newest one.
301 if (Existing
->getCanonicalDecl() == New
->getCanonicalDecl()) {
302 // If the existing declaration is somewhere in the previous declaration
303 // chain of the new declaration, then prefer the new declaration.
304 for (Decl::redecl_iterator RD
= New
->redecls_begin(),
305 RDEnd
= New
->redecls_end();
310 if (RD
->isCanonicalDecl())
317 return DMK_Different
;
320 bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl
*D
, DeclarationName Name
){
321 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
322 readingIdentifier(*II
);
324 void *Ptr
= Name
.getFETokenInfo
<void>();
327 Name
.setFETokenInfo(D
);
333 if (isDeclPtr(Ptr
)) {
334 NamedDecl
*PrevD
= static_cast<NamedDecl
*>(Ptr
);
336 switch (compareDeclarations(PrevD
, D
)) {
344 Name
.setFETokenInfo(D
);
348 Name
.setFETokenInfo(NULL
);
349 IDI
= &(*IdDeclInfos
)[Name
];
351 // If the existing declaration is not visible in translation unit scope,
352 // then add the new top-level declaration first.
353 if (!PrevD
->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
363 IDI
= toIdDeclInfo(Ptr
);
365 // See whether this declaration is identical to any existing declarations.
366 // If not, find the right place to insert it.
367 for (IdDeclInfo::DeclsTy::iterator I
= IDI
->decls_begin(),
368 IEnd
= IDI
->decls_end();
371 switch (compareDeclarations(*I
, D
)) {
383 if (!(*I
)->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
384 // We've found a declaration that is not visible from the translation
385 // unit (it's in an inner scope). Insert our declaration here.
386 IDI
->InsertDecl(I
, D
);
391 // Add the declaration to the end.
396 void IdentifierResolver::readingIdentifier(IdentifierInfo
&II
) {
397 if (II
.isOutOfDate())
398 PP
.getExternalSource()->updateOutOfDateIdentifier(II
);
401 void IdentifierResolver::updatingIdentifier(IdentifierInfo
&II
) {
402 if (II
.isOutOfDate())
403 PP
.getExternalSource()->updateOutOfDateIdentifier(II
);
406 II
.setChangedSinceDeserialization();
409 //===----------------------------------------------------------------------===//
410 // IdDeclInfoMap Implementation
411 //===----------------------------------------------------------------------===//
413 /// Returns the IdDeclInfo associated to the DeclarationName.
414 /// It creates a new IdDeclInfo if one was not created before for this id.
415 IdentifierResolver::IdDeclInfo
&
416 IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name
) {
417 void *Ptr
= Name
.getFETokenInfo
<void>();
419 if (Ptr
) return *toIdDeclInfo(Ptr
);
421 if (CurIndex
== POOL_SIZE
) {
422 CurPool
= new IdDeclInfoPool(CurPool
);
425 IdDeclInfo
*IDI
= &CurPool
->Pool
[CurIndex
];
426 Name
.setFETokenInfo(reinterpret_cast<void*>(
427 reinterpret_cast<uintptr_t>(IDI
) | 0x1)
433 void IdentifierResolver::iterator::incrementSlowCase() {
434 NamedDecl
*D
= **this;
435 void *InfoPtr
= D
->getDeclName().getFETokenInfo
<void>();
436 assert(!isDeclPtr(InfoPtr
) && "Decl with wrong id ?");
437 IdDeclInfo
*Info
= toIdDeclInfo(InfoPtr
);
439 BaseIter I
= getIterator();
440 if (I
!= Info
->decls_begin())
441 *this = iterator(I
-1);
442 else // No more decls.