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/AST/Decl.h"
17 #include "clang/Basic/LangOptions.h"
18 #include "clang/Lex/ExternalPreprocessorSource.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Sema/Scope.h"
22 using namespace clang;
24 //===----------------------------------------------------------------------===//
25 // IdDeclInfoMap class
26 //===----------------------------------------------------------------------===//
28 /// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
29 /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
30 /// individual IdDeclInfo to heap.
31 class IdentifierResolver::IdDeclInfoMap {
32 static const unsigned int POOL_SIZE = 512;
34 /// We use our own linked-list implementation because it is sadly
35 /// impossible to add something to a pre-C++0x STL container without
36 /// a completely unnecessary copy.
37 struct IdDeclInfoPool {
38 IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
41 IdDeclInfo Pool[POOL_SIZE];
44 IdDeclInfoPool *CurPool;
45 unsigned int CurIndex;
48 IdDeclInfoMap() : CurPool(nullptr), CurIndex(POOL_SIZE) {}
51 IdDeclInfoPool *Cur = CurPool;
52 while (IdDeclInfoPool *P = Cur) {
58 /// Returns the IdDeclInfo associated to the DeclarationName.
59 /// It creates a new IdDeclInfo if one was not created before for this id.
60 IdDeclInfo &operator[](DeclarationName Name);
64 //===----------------------------------------------------------------------===//
65 // IdDeclInfo Implementation
66 //===----------------------------------------------------------------------===//
68 /// RemoveDecl - Remove the decl from the scope chain.
69 /// The decl must already be part of the decl chain.
70 void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
71 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
78 llvm_unreachable("Didn't find this decl on its identifier's chain!");
81 //===----------------------------------------------------------------------===//
82 // IdentifierResolver Implementation
83 //===----------------------------------------------------------------------===//
85 IdentifierResolver::IdentifierResolver(Preprocessor &PP)
86 : LangOpt(PP.getLangOpts()), PP(PP),
87 IdDeclInfos(new IdDeclInfoMap) {
90 IdentifierResolver::~IdentifierResolver() {
94 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
95 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
96 /// true if 'D' belongs to the given declaration context.
97 bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
98 bool AllowInlineNamespace) const {
99 Ctx = Ctx->getRedeclContext();
101 if (Ctx->isFunctionOrMethod() || (S && S->isFunctionPrototypeScope())) {
102 // Ignore the scopes associated within transparent declaration contexts.
103 while (S->getEntity() && S->getEntity()->isTransparentContext())
106 if (S->isDeclScope(D))
108 if (LangOpt.CPlusPlus) {
110 // The name declared in a catch exception-declaration is local to the
111 // handler and shall not be redeclared in the outermost block of the
114 // Names declared in the for-init-statement, and in the condition of if,
115 // while, for, and switch statements are local to the if, while, for, or
116 // switch statement (including the controlled statement), and shall not be
117 // redeclared in a subsequent condition of that statement nor in the
118 // outermost block (or, for the if statement, any of the outermost blocks)
119 // of the controlled statement.
121 assert(S->getParent() && "No TUScope?");
122 if (S->getParent()->getFlags() & Scope::ControlScope) {
124 if (S->isDeclScope(D))
127 if (S->getFlags() & Scope::FnTryCatchScope)
128 return S->getParent()->isDeclScope(D);
133 // FIXME: If D is a local extern declaration, this check doesn't make sense;
134 // we should be checking its lexical context instead in that case, because
135 // that is its scope.
136 DeclContext *DCtx = D->getDeclContext()->getRedeclContext();
137 return AllowInlineNamespace ? Ctx->InEnclosingNamespaceSetOf(DCtx)
141 /// AddDecl - Link the decl to its shadowed decl chain.
142 void IdentifierResolver::AddDecl(NamedDecl *D) {
143 DeclarationName Name = D->getDeclName();
144 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
145 updatingIdentifier(*II);
147 void *Ptr = Name.getFETokenInfo<void>();
150 Name.setFETokenInfo(D);
156 if (isDeclPtr(Ptr)) {
157 Name.setFETokenInfo(nullptr);
158 IDI = &(*IdDeclInfos)[Name];
159 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
162 IDI = toIdDeclInfo(Ptr);
167 void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
168 DeclarationName Name = D->getDeclName();
169 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
170 updatingIdentifier(*II);
172 void *Ptr = Name.getFETokenInfo<void>();
179 if (isDeclPtr(Ptr)) {
180 // We only have a single declaration: insert before or after it,
182 if (Pos == iterator()) {
183 // Add the new declaration before the existing declaration.
184 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
189 // Add new declaration after the existing declaration.
196 // General case: insert the declaration at the appropriate point in the
197 // list, which already has at least two elements.
198 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
199 if (Pos.isIterator()) {
200 IDI->InsertDecl(Pos.getIterator() + 1, D);
202 IDI->InsertDecl(IDI->decls_begin(), D);
205 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
206 /// The decl must already be part of the decl chain.
207 void IdentifierResolver::RemoveDecl(NamedDecl *D) {
208 assert(D && "null param passed");
209 DeclarationName Name = D->getDeclName();
210 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
211 updatingIdentifier(*II);
213 void *Ptr = Name.getFETokenInfo<void>();
215 assert(Ptr && "Didn't find this decl on its identifier's chain!");
217 if (isDeclPtr(Ptr)) {
218 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
219 Name.setFETokenInfo(nullptr);
223 return toIdDeclInfo(Ptr)->RemoveDecl(D);
226 /// begin - Returns an iterator for decls with name 'Name'.
227 IdentifierResolver::iterator
228 IdentifierResolver::begin(DeclarationName Name) {
229 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
230 readingIdentifier(*II);
232 void *Ptr = Name.getFETokenInfo<void>();
233 if (!Ptr) return end();
236 return iterator(static_cast<NamedDecl*>(Ptr));
238 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
240 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
241 if (I != IDI->decls_begin())
242 return iterator(I-1);
255 /// \brief Compare two declarations to see whether they are different or,
256 /// if they are the same, whether the new declaration should replace the
257 /// existing declaration.
258 static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) {
259 // If the declarations are identical, ignore the new one.
263 // If the declarations have different kinds, they're obviously different.
264 if (Existing->getKind() != New->getKind())
265 return DMK_Different;
267 // If the declarations are redeclarations of each other, keep the newest one.
268 if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) {
269 // If we're adding an imported declaration, don't replace another imported
271 if (Existing->isFromASTFile() && New->isFromASTFile())
272 return DMK_Different;
274 // If either of these is the most recent declaration, use it.
275 Decl *MostRecent = Existing->getMostRecentDecl();
276 if (Existing == MostRecent)
279 if (New == MostRecent)
282 // If the existing declaration is somewhere in the previous declaration
283 // chain of the new declaration, then prefer the new declaration.
284 for (auto RD : New->redecls()) {
288 if (RD->isCanonicalDecl())
295 return DMK_Different;
298 bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){
299 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
300 readingIdentifier(*II);
302 void *Ptr = Name.getFETokenInfo<void>();
305 Name.setFETokenInfo(D);
311 if (isDeclPtr(Ptr)) {
312 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
314 switch (compareDeclarations(PrevD, D)) {
322 Name.setFETokenInfo(D);
326 Name.setFETokenInfo(nullptr);
327 IDI = &(*IdDeclInfos)[Name];
329 // If the existing declaration is not visible in translation unit scope,
330 // then add the new top-level declaration first.
331 if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
341 IDI = toIdDeclInfo(Ptr);
343 // See whether this declaration is identical to any existing declarations.
344 // If not, find the right place to insert it.
345 for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(),
346 IEnd = IDI->decls_end();
349 switch (compareDeclarations(*I, D)) {
361 if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
362 // We've found a declaration that is not visible from the translation
363 // unit (it's in an inner scope). Insert our declaration here.
364 IDI->InsertDecl(I, D);
369 // Add the declaration to the end.
374 void IdentifierResolver::readingIdentifier(IdentifierInfo &II) {
375 if (II.isOutOfDate())
376 PP.getExternalSource()->updateOutOfDateIdentifier(II);
379 void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) {
380 if (II.isOutOfDate())
381 PP.getExternalSource()->updateOutOfDateIdentifier(II);
384 II.setFETokenInfoChangedSinceDeserialization();
387 //===----------------------------------------------------------------------===//
388 // IdDeclInfoMap Implementation
389 //===----------------------------------------------------------------------===//
391 /// Returns the IdDeclInfo associated to the DeclarationName.
392 /// It creates a new IdDeclInfo if one was not created before for this id.
393 IdentifierResolver::IdDeclInfo &
394 IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
395 void *Ptr = Name.getFETokenInfo<void>();
397 if (Ptr) return *toIdDeclInfo(Ptr);
399 if (CurIndex == POOL_SIZE) {
400 CurPool = new IdDeclInfoPool(CurPool);
403 IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
404 Name.setFETokenInfo(reinterpret_cast<void*>(
405 reinterpret_cast<uintptr_t>(IDI) | 0x1)
411 void IdentifierResolver::iterator::incrementSlowCase() {
412 NamedDecl *D = **this;
413 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
414 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
415 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
417 BaseIter I = getIterator();
418 if (I != Info->decls_begin())
419 *this = iterator(I-1);
420 else // No more decls.