1 //===- IdentifierResolver.cpp - Lexical Scope Name lookup -----------------===//
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/AST/Decl.h"
17 #include "clang/AST/DeclBase.h"
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/Basic/IdentifierTable.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Lex/ExternalPreprocessorSource.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Sema/Scope.h"
24 #include "llvm/Support/ErrorHandling.h"
28 using namespace clang;
30 //===----------------------------------------------------------------------===//
31 // IdDeclInfoMap class
32 //===----------------------------------------------------------------------===//
34 /// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
35 /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
36 /// individual IdDeclInfo to heap.
37 class IdentifierResolver::IdDeclInfoMap {
38 static const unsigned int POOL_SIZE = 512;
40 /// We use our own linked-list implementation because it is sadly
41 /// impossible to add something to a pre-C++0x STL container without
42 /// a completely unnecessary copy.
43 struct IdDeclInfoPool {
45 IdDeclInfo Pool[POOL_SIZE];
47 IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
50 IdDeclInfoPool *CurPool = nullptr;
51 unsigned int CurIndex = POOL_SIZE;
54 IdDeclInfoMap() = default;
57 IdDeclInfoPool *Cur = CurPool;
58 while (IdDeclInfoPool *P = Cur) {
64 /// Returns the IdDeclInfo associated to the DeclarationName.
65 /// It creates a new IdDeclInfo if one was not created before for this id.
66 IdDeclInfo &operator[](DeclarationName Name);
69 //===----------------------------------------------------------------------===//
70 // IdDeclInfo Implementation
71 //===----------------------------------------------------------------------===//
73 /// RemoveDecl - Remove the decl from the scope chain.
74 /// The decl must already be part of the decl chain.
75 void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
76 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
83 llvm_unreachable("Didn't find this decl on its identifier's chain!");
86 //===----------------------------------------------------------------------===//
87 // IdentifierResolver Implementation
88 //===----------------------------------------------------------------------===//
90 IdentifierResolver::IdentifierResolver(Preprocessor &PP)
91 : LangOpt(PP.getLangOpts()), PP(PP), IdDeclInfos(new IdDeclInfoMap) {}
93 IdentifierResolver::~IdentifierResolver() {
97 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
98 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
99 /// true if 'D' belongs to the given declaration context.
100 bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
101 bool AllowInlineNamespace) const {
102 Ctx = Ctx->getRedeclContext();
104 if (Ctx->isFunctionOrMethod() || (S && S->isFunctionPrototypeScope())) {
105 // Ignore the scopes associated within transparent declaration contexts.
106 while (S->getEntity() && S->getEntity()->isTransparentContext())
109 if (S->isDeclScope(D))
111 if (LangOpt.CPlusPlus) {
113 // The name declared in a catch exception-declaration is local to the
114 // handler and shall not be redeclared in the outermost block of the
117 // Names declared in the for-init-statement, and in the condition of if,
118 // while, for, and switch statements are local to the if, while, for, or
119 // switch statement (including the controlled statement), and shall not be
120 // redeclared in a subsequent condition of that statement nor in the
121 // outermost block (or, for the if statement, any of the outermost blocks)
122 // of the controlled statement.
124 assert(S->getParent() && "No TUScope?");
125 if (S->getParent()->getFlags() & Scope::ControlScope) {
127 if (S->isDeclScope(D))
130 if (S->getFlags() & Scope::FnTryCatchScope)
131 return S->getParent()->isDeclScope(D);
136 // FIXME: If D is a local extern declaration, this check doesn't make sense;
137 // we should be checking its lexical context instead in that case, because
138 // that is its scope.
139 DeclContext *DCtx = D->getDeclContext()->getRedeclContext();
140 return AllowInlineNamespace ? Ctx->InEnclosingNamespaceSetOf(DCtx)
144 /// AddDecl - Link the decl to its shadowed decl chain.
145 void IdentifierResolver::AddDecl(NamedDecl *D) {
146 DeclarationName Name = D->getDeclName();
147 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
148 updatingIdentifier(*II);
150 void *Ptr = Name.getFETokenInfo<void>();
153 Name.setFETokenInfo(D);
159 if (isDeclPtr(Ptr)) {
160 Name.setFETokenInfo(nullptr);
161 IDI = &(*IdDeclInfos)[Name];
162 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
165 IDI = toIdDeclInfo(Ptr);
170 void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
171 DeclarationName Name = D->getDeclName();
172 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
173 updatingIdentifier(*II);
175 void *Ptr = Name.getFETokenInfo<void>();
182 if (isDeclPtr(Ptr)) {
183 // We only have a single declaration: insert before or after it,
185 if (Pos == iterator()) {
186 // Add the new declaration before the existing declaration.
187 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
192 // Add new declaration after the existing declaration.
199 // General case: insert the declaration at the appropriate point in the
200 // list, which already has at least two elements.
201 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
202 if (Pos.isIterator()) {
203 IDI->InsertDecl(Pos.getIterator() + 1, D);
205 IDI->InsertDecl(IDI->decls_begin(), D);
208 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
209 /// The decl must already be part of the decl chain.
210 void IdentifierResolver::RemoveDecl(NamedDecl *D) {
211 assert(D && "null param passed");
212 DeclarationName Name = D->getDeclName();
213 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
214 updatingIdentifier(*II);
216 void *Ptr = Name.getFETokenInfo<void>();
218 assert(Ptr && "Didn't find this decl on its identifier's chain!");
220 if (isDeclPtr(Ptr)) {
221 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
222 Name.setFETokenInfo(nullptr);
226 return toIdDeclInfo(Ptr)->RemoveDecl(D);
229 /// begin - Returns an iterator for decls with name 'Name'.
230 IdentifierResolver::iterator
231 IdentifierResolver::begin(DeclarationName Name) {
232 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
233 readingIdentifier(*II);
235 void *Ptr = Name.getFETokenInfo<void>();
236 if (!Ptr) return end();
239 return iterator(static_cast<NamedDecl*>(Ptr));
241 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
243 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
244 if (I != IDI->decls_begin())
245 return iterator(I-1);
260 /// Compare two declarations to see whether they are different or,
261 /// if they are the same, whether the new declaration should replace the
262 /// existing declaration.
263 static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) {
264 // If the declarations are identical, ignore the new one.
268 // If the declarations have different kinds, they're obviously different.
269 if (Existing->getKind() != New->getKind())
270 return DMK_Different;
272 // If the declarations are redeclarations of each other, keep the newest one.
273 if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) {
274 // If we're adding an imported declaration, don't replace another imported
276 if (Existing->isFromASTFile() && New->isFromASTFile())
277 return DMK_Different;
279 // If either of these is the most recent declaration, use it.
280 Decl *MostRecent = Existing->getMostRecentDecl();
281 if (Existing == MostRecent)
284 if (New == MostRecent)
287 // If the existing declaration is somewhere in the previous declaration
288 // chain of the new declaration, then prefer the new declaration.
289 for (auto RD : New->redecls()) {
293 if (RD->isCanonicalDecl())
300 return DMK_Different;
303 bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){
304 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
305 readingIdentifier(*II);
307 void *Ptr = Name.getFETokenInfo<void>();
310 Name.setFETokenInfo(D);
316 if (isDeclPtr(Ptr)) {
317 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
319 switch (compareDeclarations(PrevD, D)) {
327 Name.setFETokenInfo(D);
331 Name.setFETokenInfo(nullptr);
332 IDI = &(*IdDeclInfos)[Name];
334 // If the existing declaration is not visible in translation unit scope,
335 // then add the new top-level declaration first.
336 if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
346 IDI = toIdDeclInfo(Ptr);
348 // See whether this declaration is identical to any existing declarations.
349 // If not, find the right place to insert it.
350 for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(),
351 IEnd = IDI->decls_end();
354 switch (compareDeclarations(*I, D)) {
366 if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
367 // We've found a declaration that is not visible from the translation
368 // unit (it's in an inner scope). Insert our declaration here.
369 IDI->InsertDecl(I, D);
374 // Add the declaration to the end.
379 void IdentifierResolver::readingIdentifier(IdentifierInfo &II) {
380 if (II.isOutOfDate())
381 PP.getExternalSource()->updateOutOfDateIdentifier(II);
384 void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) {
385 if (II.isOutOfDate())
386 PP.getExternalSource()->updateOutOfDateIdentifier(II);
389 II.setFETokenInfoChangedSinceDeserialization();
392 //===----------------------------------------------------------------------===//
393 // IdDeclInfoMap Implementation
394 //===----------------------------------------------------------------------===//
396 /// Returns the IdDeclInfo associated to the DeclarationName.
397 /// It creates a new IdDeclInfo if one was not created before for this id.
398 IdentifierResolver::IdDeclInfo &
399 IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
400 void *Ptr = Name.getFETokenInfo<void>();
402 if (Ptr) return *toIdDeclInfo(Ptr);
404 if (CurIndex == POOL_SIZE) {
405 CurPool = new IdDeclInfoPool(CurPool);
408 IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
409 Name.setFETokenInfo(reinterpret_cast<void*>(
410 reinterpret_cast<uintptr_t>(IDI) | 0x1)
416 void IdentifierResolver::iterator::incrementSlowCase() {
417 NamedDecl *D = **this;
418 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
419 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
420 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
422 BaseIter I = getIterator();
423 if (I != Info->decls_begin())
424 *this = iterator(I-1);
425 else // No more decls.