1 //===- IdentifierResolver.cpp - Lexical Scope Name lookup -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the IdentifierResolver class, which is used for lexical
10 // scoped lookup, based on declaration names.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Sema/IdentifierResolver.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclBase.h"
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/Basic/IdentifierTable.h"
19 #include "clang/Basic/LangOptions.h"
20 #include "clang/Lex/ExternalPreprocessorSource.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "clang/Sema/Scope.h"
23 #include "llvm/Support/ErrorHandling.h"
27 using namespace clang
;
29 //===----------------------------------------------------------------------===//
30 // IdDeclInfoMap class
31 //===----------------------------------------------------------------------===//
33 /// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
34 /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
35 /// individual IdDeclInfo to heap.
36 class IdentifierResolver::IdDeclInfoMap
{
37 static const unsigned int POOL_SIZE
= 512;
39 /// We use our own linked-list implementation because it is sadly
40 /// impossible to add something to a pre-C++0x STL container without
41 /// a completely unnecessary copy.
42 struct IdDeclInfoPool
{
44 IdDeclInfo Pool
[POOL_SIZE
];
46 IdDeclInfoPool(IdDeclInfoPool
*Next
) : Next(Next
) {}
49 IdDeclInfoPool
*CurPool
= nullptr;
50 unsigned int CurIndex
= POOL_SIZE
;
53 IdDeclInfoMap() = default;
56 IdDeclInfoPool
*Cur
= CurPool
;
57 while (IdDeclInfoPool
*P
= Cur
) {
63 /// Returns the IdDeclInfo associated to the DeclarationName.
64 /// It creates a new IdDeclInfo if one was not created before for this id.
65 IdDeclInfo
&operator[](DeclarationName Name
);
68 //===----------------------------------------------------------------------===//
69 // IdDeclInfo Implementation
70 //===----------------------------------------------------------------------===//
72 /// RemoveDecl - Remove the decl from the scope chain.
73 /// The decl must already be part of the decl chain.
74 void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl
*D
) {
75 for (DeclsTy::iterator I
= Decls
.end(); I
!= Decls
.begin(); --I
) {
82 llvm_unreachable("Didn't find this decl on its identifier's chain!");
85 //===----------------------------------------------------------------------===//
86 // IdentifierResolver Implementation
87 //===----------------------------------------------------------------------===//
89 IdentifierResolver::IdentifierResolver(Preprocessor
&PP
)
90 : LangOpt(PP
.getLangOpts()), PP(PP
), IdDeclInfos(new IdDeclInfoMap
) {}
92 IdentifierResolver::~IdentifierResolver() {
96 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
97 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
98 /// true if 'D' belongs to the given declaration context.
99 bool IdentifierResolver::isDeclInScope(Decl
*D
, DeclContext
*Ctx
, Scope
*S
,
100 bool AllowInlineNamespace
) const {
101 Ctx
= Ctx
->getRedeclContext();
103 if (Ctx
->isFunctionOrMethod() || (S
&& S
->isFunctionPrototypeScope())) {
104 // Ignore the scopes associated within transparent declaration contexts.
105 while (S
->getEntity() && S
->getEntity()->isTransparentContext())
108 if (S
->isDeclScope(D
))
110 if (LangOpt
.CPlusPlus
) {
112 // The name declared in a catch exception-declaration is local to the
113 // handler and shall not be redeclared in the outermost block of the
116 // Names declared in the for-init-statement, and in the condition of if,
117 // while, for, and switch statements are local to the if, while, for, or
118 // switch statement (including the controlled statement), and shall not be
119 // redeclared in a subsequent condition of that statement nor in the
120 // outermost block (or, for the if statement, any of the outermost blocks)
121 // of the controlled statement.
123 assert(S
->getParent() && "No TUScope?");
124 // If the current decl is in a lambda, we shouldn't consider this is a
125 // redefinition as lambda has its own scope.
126 if (S
->getParent()->isControlScope() && !S
->isFunctionScope()) {
128 if (S
->isDeclScope(D
))
131 if (S
->isFnTryCatchScope())
132 return S
->getParent()->isDeclScope(D
);
137 // FIXME: If D is a local extern declaration, this check doesn't make sense;
138 // we should be checking its lexical context instead in that case, because
139 // that is its scope.
140 DeclContext
*DCtx
= D
->getDeclContext()->getRedeclContext();
141 return AllowInlineNamespace
? Ctx
->InEnclosingNamespaceSetOf(DCtx
)
145 /// AddDecl - Link the decl to its shadowed decl chain.
146 void IdentifierResolver::AddDecl(NamedDecl
*D
) {
147 DeclarationName Name
= D
->getDeclName();
148 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
149 updatingIdentifier(*II
);
151 void *Ptr
= Name
.getFETokenInfo();
154 Name
.setFETokenInfo(D
);
160 if (isDeclPtr(Ptr
)) {
161 Name
.setFETokenInfo(nullptr);
162 IDI
= &(*IdDeclInfos
)[Name
];
163 NamedDecl
*PrevD
= static_cast<NamedDecl
*>(Ptr
);
166 IDI
= toIdDeclInfo(Ptr
);
171 void IdentifierResolver::InsertDeclAfter(iterator Pos
, NamedDecl
*D
) {
172 DeclarationName Name
= D
->getDeclName();
173 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
174 updatingIdentifier(*II
);
176 void *Ptr
= Name
.getFETokenInfo();
183 if (isDeclPtr(Ptr
)) {
184 // We only have a single declaration: insert before or after it,
186 if (Pos
== iterator()) {
187 // Add the new declaration before the existing declaration.
188 NamedDecl
*PrevD
= static_cast<NamedDecl
*>(Ptr
);
193 // Add new declaration after the existing declaration.
200 // General case: insert the declaration at the appropriate point in the
201 // list, which already has at least two elements.
202 IdDeclInfo
*IDI
= toIdDeclInfo(Ptr
);
203 if (Pos
.isIterator()) {
204 IDI
->InsertDecl(Pos
.getIterator() + 1, D
);
206 IDI
->InsertDecl(IDI
->decls_begin(), D
);
209 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
210 /// The decl must already be part of the decl chain.
211 void IdentifierResolver::RemoveDecl(NamedDecl
*D
) {
212 assert(D
&& "null param passed");
213 DeclarationName Name
= D
->getDeclName();
214 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
215 updatingIdentifier(*II
);
217 void *Ptr
= Name
.getFETokenInfo();
219 assert(Ptr
&& "Didn't find this decl on its identifier's chain!");
221 if (isDeclPtr(Ptr
)) {
222 assert(D
== Ptr
&& "Didn't find this decl on its identifier's chain!");
223 Name
.setFETokenInfo(nullptr);
227 return toIdDeclInfo(Ptr
)->RemoveDecl(D
);
230 /// begin - Returns an iterator for decls with name 'Name'.
231 IdentifierResolver::iterator
232 IdentifierResolver::begin(DeclarationName Name
) {
233 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
234 readingIdentifier(*II
);
236 void *Ptr
= Name
.getFETokenInfo();
237 if (!Ptr
) return end();
240 return iterator(static_cast<NamedDecl
*>(Ptr
));
242 IdDeclInfo
*IDI
= toIdDeclInfo(Ptr
);
244 IdDeclInfo::DeclsTy::iterator I
= IDI
->decls_end();
245 if (I
!= IDI
->decls_begin())
246 return iterator(I
-1);
261 /// Compare two declarations to see whether they are different or,
262 /// if they are the same, whether the new declaration should replace the
263 /// existing declaration.
264 static DeclMatchKind
compareDeclarations(NamedDecl
*Existing
, NamedDecl
*New
) {
265 // If the declarations are identical, ignore the new one.
269 // If the declarations have different kinds, they're obviously different.
270 if (Existing
->getKind() != New
->getKind())
271 return DMK_Different
;
273 // If the declarations are redeclarations of each other, keep the newest one.
274 if (Existing
->getCanonicalDecl() == New
->getCanonicalDecl()) {
275 // If we're adding an imported declaration, don't replace another imported
277 if (Existing
->isFromASTFile() && New
->isFromASTFile())
278 return DMK_Different
;
280 // If either of these is the most recent declaration, use it.
281 Decl
*MostRecent
= Existing
->getMostRecentDecl();
282 if (Existing
== MostRecent
)
285 if (New
== MostRecent
)
288 // If the existing declaration is somewhere in the previous declaration
289 // chain of the new declaration, then prefer the new declaration.
290 for (auto *RD
: New
->redecls()) {
294 if (RD
->isCanonicalDecl())
301 return DMK_Different
;
304 bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl
*D
, DeclarationName Name
){
305 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
306 readingIdentifier(*II
);
308 void *Ptr
= Name
.getFETokenInfo();
311 Name
.setFETokenInfo(D
);
317 if (isDeclPtr(Ptr
)) {
318 NamedDecl
*PrevD
= static_cast<NamedDecl
*>(Ptr
);
320 switch (compareDeclarations(PrevD
, D
)) {
328 Name
.setFETokenInfo(D
);
332 Name
.setFETokenInfo(nullptr);
333 IDI
= &(*IdDeclInfos
)[Name
];
335 // If the existing declaration is not visible in translation unit scope,
336 // then add the new top-level declaration first.
337 if (!PrevD
->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
347 IDI
= toIdDeclInfo(Ptr
);
349 // See whether this declaration is identical to any existing declarations.
350 // If not, find the right place to insert it.
351 for (IdDeclInfo::DeclsTy::iterator I
= IDI
->decls_begin(),
352 IEnd
= IDI
->decls_end();
355 switch (compareDeclarations(*I
, D
)) {
367 if (!(*I
)->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
368 // We've found a declaration that is not visible from the translation
369 // unit (it's in an inner scope). Insert our declaration here.
370 IDI
->InsertDecl(I
, D
);
375 // Add the declaration to the end.
380 void IdentifierResolver::readingIdentifier(IdentifierInfo
&II
) {
381 if (II
.isOutOfDate())
382 PP
.getExternalSource()->updateOutOfDateIdentifier(II
);
385 void IdentifierResolver::updatingIdentifier(IdentifierInfo
&II
) {
386 if (II
.isOutOfDate())
387 PP
.getExternalSource()->updateOutOfDateIdentifier(II
);
390 II
.setFETokenInfoChangedSinceDeserialization();
393 //===----------------------------------------------------------------------===//
394 // IdDeclInfoMap Implementation
395 //===----------------------------------------------------------------------===//
397 /// Returns the IdDeclInfo associated to the DeclarationName.
398 /// It creates a new IdDeclInfo if one was not created before for this id.
399 IdentifierResolver::IdDeclInfo
&
400 IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name
) {
401 void *Ptr
= Name
.getFETokenInfo();
403 if (Ptr
) return *toIdDeclInfo(Ptr
);
405 if (CurIndex
== POOL_SIZE
) {
406 CurPool
= new IdDeclInfoPool(CurPool
);
409 IdDeclInfo
*IDI
= &CurPool
->Pool
[CurIndex
];
410 Name
.setFETokenInfo(reinterpret_cast<void*>(
411 reinterpret_cast<uintptr_t>(IDI
) | 0x1)
417 void IdentifierResolver::iterator::incrementSlowCase() {
418 NamedDecl
*D
= **this;
419 void *InfoPtr
= D
->getDeclName().getFETokenInfo();
420 assert(!isDeclPtr(InfoPtr
) && "Decl with wrong id ?");
421 IdDeclInfo
*Info
= toIdDeclInfo(InfoPtr
);
423 BaseIter I
= getIterator();
424 if (I
!= Info
->decls_begin())
425 *this = iterator(I
-1);
426 else // No more decls.