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/Basic/LangOptions.h"
20 using namespace clang
;
22 //===----------------------------------------------------------------------===//
23 // IdDeclInfoMap class
24 //===----------------------------------------------------------------------===//
26 /// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
27 /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
28 /// individual IdDeclInfo to heap.
29 class IdentifierResolver::IdDeclInfoMap
{
30 static const unsigned int POOL_SIZE
= 512;
32 /// We use our own linked-list implementation because it is sadly
33 /// impossible to add something to a pre-C++0x STL container without
34 /// a completely unnecessary copy.
35 struct IdDeclInfoPool
{
36 IdDeclInfoPool(IdDeclInfoPool
*Next
) : Next(Next
) {}
39 IdDeclInfo Pool
[POOL_SIZE
];
42 IdDeclInfoPool
*CurPool
;
43 unsigned int CurIndex
;
46 IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE
) {}
49 IdDeclInfoPool
*Cur
= CurPool
;
50 while (IdDeclInfoPool
*P
= Cur
) {
56 /// Returns the IdDeclInfo associated to the DeclarationName.
57 /// It creates a new IdDeclInfo if one was not created before for this id.
58 IdDeclInfo
&operator[](DeclarationName Name
);
62 //===----------------------------------------------------------------------===//
63 // IdDeclInfo Implementation
64 //===----------------------------------------------------------------------===//
66 /// RemoveDecl - Remove the decl from the scope chain.
67 /// The decl must already be part of the decl chain.
68 void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl
*D
) {
69 for (DeclsTy::iterator I
= Decls
.end(); I
!= Decls
.begin(); --I
) {
76 assert(0 && "Didn't find this decl on its identifier's chain!");
80 IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl
*Old
, NamedDecl
*New
) {
81 for (DeclsTy::iterator I
= Decls
.end(); I
!= Decls
.begin(); --I
) {
92 //===----------------------------------------------------------------------===//
93 // IdentifierResolver Implementation
94 //===----------------------------------------------------------------------===//
96 IdentifierResolver::IdentifierResolver(const LangOptions
&langOpt
)
97 : LangOpt(langOpt
), IdDeclInfos(new IdDeclInfoMap
) {
99 IdentifierResolver::~IdentifierResolver() {
103 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
104 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
105 /// true if 'D' belongs to the given declaration context.
106 bool IdentifierResolver::isDeclInScope(Decl
*D
, DeclContext
*Ctx
,
107 ASTContext
&Context
, Scope
*S
) const {
108 Ctx
= Ctx
->getRedeclContext();
110 if (Ctx
->isFunctionOrMethod()) {
111 // Ignore the scopes associated within transparent declaration contexts.
112 while (S
->getEntity() &&
113 ((DeclContext
*)S
->getEntity())->isTransparentContext())
116 if (S
->isDeclScope(D
))
118 if (LangOpt
.CPlusPlus
) {
120 // The name declared in a catch exception-declaration is local to the
121 // handler and shall not be redeclared in the outermost block of the
124 // Names declared in the for-init-statement, and in the condition of if,
125 // while, for, and switch statements are local to the if, while, for, or
126 // switch statement (including the controlled statement), and shall not be
127 // redeclared in a subsequent condition of that statement nor in the
128 // outermost block (or, for the if statement, any of the outermost blocks)
129 // of the controlled statement.
131 assert(S
->getParent() && "No TUScope?");
132 if (S
->getParent()->getFlags() & Scope::ControlScope
)
133 return S
->getParent()->isDeclScope(D
);
138 return D
->getDeclContext()->getRedeclContext()->Equals(Ctx
);
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 II
->setIsFromAST(false);
147 void *Ptr
= Name
.getFETokenInfo
<void>();
150 Name
.setFETokenInfo(D
);
156 if (isDeclPtr(Ptr
)) {
157 Name
.setFETokenInfo(NULL
);
158 IDI
= &(*IdDeclInfos
)[Name
];
159 NamedDecl
*PrevD
= static_cast<NamedDecl
*>(Ptr
);
162 IDI
= toIdDeclInfo(Ptr
);
167 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
168 /// The decl must already be part of the decl chain.
169 void IdentifierResolver::RemoveDecl(NamedDecl
*D
) {
170 assert(D
&& "null param passed");
171 DeclarationName Name
= D
->getDeclName();
172 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
173 II
->setIsFromAST(false);
175 void *Ptr
= Name
.getFETokenInfo
<void>();
177 assert(Ptr
&& "Didn't find this decl on its identifier's chain!");
179 if (isDeclPtr(Ptr
)) {
180 assert(D
== Ptr
&& "Didn't find this decl on its identifier's chain!");
181 Name
.setFETokenInfo(NULL
);
185 return toIdDeclInfo(Ptr
)->RemoveDecl(D
);
188 bool IdentifierResolver::ReplaceDecl(NamedDecl
*Old
, NamedDecl
*New
) {
189 assert(Old
->getDeclName() == New
->getDeclName() &&
190 "Cannot replace a decl with another decl of a different name");
192 DeclarationName Name
= Old
->getDeclName();
193 if (IdentifierInfo
*II
= Name
.getAsIdentifierInfo())
194 II
->setIsFromAST(false);
196 void *Ptr
= Name
.getFETokenInfo
<void>();
201 if (isDeclPtr(Ptr
)) {
203 Name
.setFETokenInfo(New
);
209 return toIdDeclInfo(Ptr
)->ReplaceDecl(Old
, New
);
212 /// begin - Returns an iterator for decls with name 'Name'.
213 IdentifierResolver::iterator
214 IdentifierResolver::begin(DeclarationName Name
) {
215 void *Ptr
= Name
.getFETokenInfo
<void>();
216 if (!Ptr
) return end();
219 return iterator(static_cast<NamedDecl
*>(Ptr
));
221 IdDeclInfo
*IDI
= toIdDeclInfo(Ptr
);
223 IdDeclInfo::DeclsTy::iterator I
= IDI
->decls_end();
224 if (I
!= IDI
->decls_begin())
225 return iterator(I
-1);
230 void IdentifierResolver::AddDeclToIdentifierChain(IdentifierInfo
*II
,
232 II
->setIsFromAST(false);
233 void *Ptr
= II
->getFETokenInfo
<void>();
236 II
->setFETokenInfo(D
);
242 if (isDeclPtr(Ptr
)) {
243 II
->setFETokenInfo(NULL
);
244 IDI
= &(*IdDeclInfos
)[II
];
245 NamedDecl
*PrevD
= static_cast<NamedDecl
*>(Ptr
);
248 IDI
= toIdDeclInfo(Ptr
);
253 //===----------------------------------------------------------------------===//
254 // IdDeclInfoMap Implementation
255 //===----------------------------------------------------------------------===//
257 /// Returns the IdDeclInfo associated to the DeclarationName.
258 /// It creates a new IdDeclInfo if one was not created before for this id.
259 IdentifierResolver::IdDeclInfo
&
260 IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name
) {
261 void *Ptr
= Name
.getFETokenInfo
<void>();
263 if (Ptr
) return *toIdDeclInfo(Ptr
);
265 if (CurIndex
== POOL_SIZE
) {
266 CurPool
= new IdDeclInfoPool(CurPool
);
269 IdDeclInfo
*IDI
= &CurPool
->Pool
[CurIndex
];
270 Name
.setFETokenInfo(reinterpret_cast<void*>(
271 reinterpret_cast<uintptr_t>(IDI
) | 0x1)
277 void IdentifierResolver::iterator::incrementSlowCase() {
278 NamedDecl
*D
= **this;
279 void *InfoPtr
= D
->getDeclName().getFETokenInfo
<void>();
280 assert(!isDeclPtr(InfoPtr
) && "Decl with wrong id ?");
281 IdDeclInfo
*Info
= toIdDeclInfo(InfoPtr
);
283 BaseIter I
= getIterator();
284 if (I
!= Info
->decls_begin())
285 *this = iterator(I
-1);
286 else // No more decls.