clang API Documentation
00001 //===- IdentifierResolver.cpp - Lexical Scope Name lookup -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the IdentifierResolver class, which is used for lexical 00011 // scoped lookup, based on declaration names. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "clang/Sema/IdentifierResolver.h" 00016 #include "clang/Sema/Scope.h" 00017 #include "clang/AST/Decl.h" 00018 #include "clang/AST/DeclObjC.h" 00019 #include "clang/Basic/LangOptions.h" 00020 #include "clang/Lex/ExternalPreprocessorSource.h" 00021 #include "clang/Lex/Preprocessor.h" 00022 00023 using namespace clang; 00024 00025 //===----------------------------------------------------------------------===// 00026 // IdDeclInfoMap class 00027 //===----------------------------------------------------------------------===// 00028 00029 /// IdDeclInfoMap - Associates IdDeclInfos with declaration names. 00030 /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each 00031 /// individual IdDeclInfo to heap. 00032 class IdentifierResolver::IdDeclInfoMap { 00033 static const unsigned int POOL_SIZE = 512; 00034 00035 /// We use our own linked-list implementation because it is sadly 00036 /// impossible to add something to a pre-C++0x STL container without 00037 /// a completely unnecessary copy. 00038 struct IdDeclInfoPool { 00039 IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {} 00040 00041 IdDeclInfoPool *Next; 00042 IdDeclInfo Pool[POOL_SIZE]; 00043 }; 00044 00045 IdDeclInfoPool *CurPool; 00046 unsigned int CurIndex; 00047 00048 public: 00049 IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE) {} 00050 00051 ~IdDeclInfoMap() { 00052 IdDeclInfoPool *Cur = CurPool; 00053 while (IdDeclInfoPool *P = Cur) { 00054 Cur = Cur->Next; 00055 delete P; 00056 } 00057 } 00058 00059 /// Returns the IdDeclInfo associated to the DeclarationName. 00060 /// It creates a new IdDeclInfo if one was not created before for this id. 00061 IdDeclInfo &operator[](DeclarationName Name); 00062 }; 00063 00064 00065 //===----------------------------------------------------------------------===// 00066 // IdDeclInfo Implementation 00067 //===----------------------------------------------------------------------===// 00068 00069 /// RemoveDecl - Remove the decl from the scope chain. 00070 /// The decl must already be part of the decl chain. 00071 void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) { 00072 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) { 00073 if (D == *(I-1)) { 00074 Decls.erase(I-1); 00075 return; 00076 } 00077 } 00078 00079 llvm_unreachable("Didn't find this decl on its identifier's chain!"); 00080 } 00081 00082 bool 00083 IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) { 00084 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) { 00085 if (Old == *(I-1)) { 00086 *(I - 1) = New; 00087 return true; 00088 } 00089 } 00090 00091 return false; 00092 } 00093 00094 00095 //===----------------------------------------------------------------------===// 00096 // IdentifierResolver Implementation 00097 //===----------------------------------------------------------------------===// 00098 00099 IdentifierResolver::IdentifierResolver(Preprocessor &PP) 00100 : LangOpt(PP.getLangOpts()), PP(PP), 00101 IdDeclInfos(new IdDeclInfoMap) { 00102 } 00103 00104 IdentifierResolver::~IdentifierResolver() { 00105 delete IdDeclInfos; 00106 } 00107 00108 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true 00109 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns 00110 /// true if 'D' belongs to the given declaration context. 00111 bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, 00112 ASTContext &Context, Scope *S, 00113 bool ExplicitInstantiationOrSpecialization) const { 00114 Ctx = Ctx->getRedeclContext(); 00115 00116 if (Ctx->isFunctionOrMethod() || S->isFunctionPrototypeScope()) { 00117 // Ignore the scopes associated within transparent declaration contexts. 00118 while (S->getEntity() && 00119 ((DeclContext *)S->getEntity())->isTransparentContext()) 00120 S = S->getParent(); 00121 00122 if (S->isDeclScope(D)) 00123 return true; 00124 if (LangOpt.CPlusPlus) { 00125 // C++ 3.3.2p3: 00126 // The name declared in a catch exception-declaration is local to the 00127 // handler and shall not be redeclared in the outermost block of the 00128 // handler. 00129 // C++ 3.3.2p4: 00130 // Names declared in the for-init-statement, and in the condition of if, 00131 // while, for, and switch statements are local to the if, while, for, or 00132 // switch statement (including the controlled statement), and shall not be 00133 // redeclared in a subsequent condition of that statement nor in the 00134 // outermost block (or, for the if statement, any of the outermost blocks) 00135 // of the controlled statement. 00136 // 00137 assert(S->getParent() && "No TUScope?"); 00138 if (S->getParent()->getFlags() & Scope::ControlScope) 00139 return S->getParent()->isDeclScope(D); 00140 } 00141 return false; 00142 } 00143 00144 DeclContext *DCtx = D->getDeclContext()->getRedeclContext(); 00145 return ExplicitInstantiationOrSpecialization 00146 ? Ctx->InEnclosingNamespaceSetOf(DCtx) 00147 : Ctx->Equals(DCtx); 00148 } 00149 00150 /// AddDecl - Link the decl to its shadowed decl chain. 00151 void IdentifierResolver::AddDecl(NamedDecl *D) { 00152 DeclarationName Name = D->getDeclName(); 00153 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) 00154 updatingIdentifier(*II); 00155 00156 void *Ptr = Name.getFETokenInfo<void>(); 00157 00158 if (!Ptr) { 00159 Name.setFETokenInfo(D); 00160 return; 00161 } 00162 00163 IdDeclInfo *IDI; 00164 00165 if (isDeclPtr(Ptr)) { 00166 Name.setFETokenInfo(NULL); 00167 IDI = &(*IdDeclInfos)[Name]; 00168 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr); 00169 IDI->AddDecl(PrevD); 00170 } else 00171 IDI = toIdDeclInfo(Ptr); 00172 00173 IDI->AddDecl(D); 00174 } 00175 00176 void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) { 00177 DeclarationName Name = D->getDeclName(); 00178 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) 00179 updatingIdentifier(*II); 00180 00181 void *Ptr = Name.getFETokenInfo<void>(); 00182 00183 if (!Ptr) { 00184 AddDecl(D); 00185 return; 00186 } 00187 00188 if (isDeclPtr(Ptr)) { 00189 // We only have a single declaration: insert before or after it, 00190 // as appropriate. 00191 if (Pos == iterator()) { 00192 // Add the new declaration before the existing declaration. 00193 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr); 00194 RemoveDecl(PrevD); 00195 AddDecl(D); 00196 AddDecl(PrevD); 00197 } else { 00198 // Add new declaration after the existing declaration. 00199 AddDecl(D); 00200 } 00201 00202 return; 00203 } 00204 00205 // General case: insert the declaration at the appropriate point in the 00206 // list, which already has at least two elements. 00207 IdDeclInfo *IDI = toIdDeclInfo(Ptr); 00208 if (Pos.isIterator()) { 00209 IDI->InsertDecl(Pos.getIterator() + 1, D); 00210 } else 00211 IDI->InsertDecl(IDI->decls_begin(), D); 00212 } 00213 00214 /// RemoveDecl - Unlink the decl from its shadowed decl chain. 00215 /// The decl must already be part of the decl chain. 00216 void IdentifierResolver::RemoveDecl(NamedDecl *D) { 00217 assert(D && "null param passed"); 00218 DeclarationName Name = D->getDeclName(); 00219 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) 00220 updatingIdentifier(*II); 00221 00222 void *Ptr = Name.getFETokenInfo<void>(); 00223 00224 assert(Ptr && "Didn't find this decl on its identifier's chain!"); 00225 00226 if (isDeclPtr(Ptr)) { 00227 assert(D == Ptr && "Didn't find this decl on its identifier's chain!"); 00228 Name.setFETokenInfo(NULL); 00229 return; 00230 } 00231 00232 return toIdDeclInfo(Ptr)->RemoveDecl(D); 00233 } 00234 00235 bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) { 00236 assert(Old->getDeclName() == New->getDeclName() && 00237 "Cannot replace a decl with another decl of a different name"); 00238 00239 DeclarationName Name = Old->getDeclName(); 00240 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) 00241 updatingIdentifier(*II); 00242 00243 void *Ptr = Name.getFETokenInfo<void>(); 00244 00245 if (!Ptr) 00246 return false; 00247 00248 if (isDeclPtr(Ptr)) { 00249 if (Ptr == Old) { 00250 Name.setFETokenInfo(New); 00251 return true; 00252 } 00253 return false; 00254 } 00255 00256 return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New); 00257 } 00258 00259 /// begin - Returns an iterator for decls with name 'Name'. 00260 IdentifierResolver::iterator 00261 IdentifierResolver::begin(DeclarationName Name) { 00262 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) 00263 readingIdentifier(*II); 00264 00265 void *Ptr = Name.getFETokenInfo<void>(); 00266 if (!Ptr) return end(); 00267 00268 if (isDeclPtr(Ptr)) 00269 return iterator(static_cast<NamedDecl*>(Ptr)); 00270 00271 IdDeclInfo *IDI = toIdDeclInfo(Ptr); 00272 00273 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end(); 00274 if (I != IDI->decls_begin()) 00275 return iterator(I-1); 00276 // No decls found. 00277 return end(); 00278 } 00279 00280 namespace { 00281 enum DeclMatchKind { 00282 DMK_Different, 00283 DMK_Replace, 00284 DMK_Ignore 00285 }; 00286 } 00287 00288 /// \brief Compare two declarations to see whether they are different or, 00289 /// if they are the same, whether the new declaration should replace the 00290 /// existing declaration. 00291 static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) { 00292 // If the declarations are identical, ignore the new one. 00293 if (Existing == New) 00294 return DMK_Ignore; 00295 00296 // If the declarations have different kinds, they're obviously different. 00297 if (Existing->getKind() != New->getKind()) 00298 return DMK_Different; 00299 00300 // If the declarations are redeclarations of each other, keep the newest one. 00301 if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) { 00302 // If the existing declaration is somewhere in the previous declaration 00303 // chain of the new declaration, then prefer the new declaration. 00304 for (Decl::redecl_iterator RD = New->redecls_begin(), 00305 RDEnd = New->redecls_end(); 00306 RD != RDEnd; ++RD) { 00307 if (RD == Existing) 00308 return DMK_Replace; 00309 00310 if (RD->isCanonicalDecl()) 00311 break; 00312 } 00313 00314 return DMK_Ignore; 00315 } 00316 00317 return DMK_Different; 00318 } 00319 00320 bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){ 00321 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) 00322 readingIdentifier(*II); 00323 00324 void *Ptr = Name.getFETokenInfo<void>(); 00325 00326 if (!Ptr) { 00327 Name.setFETokenInfo(D); 00328 return true; 00329 } 00330 00331 IdDeclInfo *IDI; 00332 00333 if (isDeclPtr(Ptr)) { 00334 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr); 00335 00336 switch (compareDeclarations(PrevD, D)) { 00337 case DMK_Different: 00338 break; 00339 00340 case DMK_Ignore: 00341 return false; 00342 00343 case DMK_Replace: 00344 Name.setFETokenInfo(D); 00345 return true; 00346 } 00347 00348 Name.setFETokenInfo(NULL); 00349 IDI = &(*IdDeclInfos)[Name]; 00350 00351 // If the existing declaration is not visible in translation unit scope, 00352 // then add the new top-level declaration first. 00353 if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { 00354 IDI->AddDecl(D); 00355 IDI->AddDecl(PrevD); 00356 } else { 00357 IDI->AddDecl(PrevD); 00358 IDI->AddDecl(D); 00359 } 00360 return true; 00361 } 00362 00363 IDI = toIdDeclInfo(Ptr); 00364 00365 // See whether this declaration is identical to any existing declarations. 00366 // If not, find the right place to insert it. 00367 for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(), 00368 IEnd = IDI->decls_end(); 00369 I != IEnd; ++I) { 00370 00371 switch (compareDeclarations(*I, D)) { 00372 case DMK_Different: 00373 break; 00374 00375 case DMK_Ignore: 00376 return false; 00377 00378 case DMK_Replace: 00379 *I = D; 00380 return true; 00381 } 00382 00383 if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) { 00384 // We've found a declaration that is not visible from the translation 00385 // unit (it's in an inner scope). Insert our declaration here. 00386 IDI->InsertDecl(I, D); 00387 return true; 00388 } 00389 } 00390 00391 // Add the declaration to the end. 00392 IDI->AddDecl(D); 00393 return true; 00394 } 00395 00396 void IdentifierResolver::readingIdentifier(IdentifierInfo &II) { 00397 if (II.isOutOfDate()) 00398 PP.getExternalSource()->updateOutOfDateIdentifier(II); 00399 } 00400 00401 void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) { 00402 if (II.isOutOfDate()) 00403 PP.getExternalSource()->updateOutOfDateIdentifier(II); 00404 00405 if (II.isFromAST()) 00406 II.setChangedSinceDeserialization(); 00407 } 00408 00409 //===----------------------------------------------------------------------===// 00410 // IdDeclInfoMap Implementation 00411 //===----------------------------------------------------------------------===// 00412 00413 /// Returns the IdDeclInfo associated to the DeclarationName. 00414 /// It creates a new IdDeclInfo if one was not created before for this id. 00415 IdentifierResolver::IdDeclInfo & 00416 IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) { 00417 void *Ptr = Name.getFETokenInfo<void>(); 00418 00419 if (Ptr) return *toIdDeclInfo(Ptr); 00420 00421 if (CurIndex == POOL_SIZE) { 00422 CurPool = new IdDeclInfoPool(CurPool); 00423 CurIndex = 0; 00424 } 00425 IdDeclInfo *IDI = &CurPool->Pool[CurIndex]; 00426 Name.setFETokenInfo(reinterpret_cast<void*>( 00427 reinterpret_cast<uintptr_t>(IDI) | 0x1) 00428 ); 00429 ++CurIndex; 00430 return *IDI; 00431 } 00432 00433 void IdentifierResolver::iterator::incrementSlowCase() { 00434 NamedDecl *D = **this; 00435 void *InfoPtr = D->getDeclName().getFETokenInfo<void>(); 00436 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?"); 00437 IdDeclInfo *Info = toIdDeclInfo(InfoPtr); 00438 00439 BaseIter I = getIterator(); 00440 if (I != Info->decls_begin()) 00441 *this = iterator(I-1); 00442 else // No more decls. 00443 *this = iterator(); 00444 }