DeclBase.cpp revision 94a39005e3733f2e498f2642be95507dda184ca1
1//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the Decl and DeclContext classes. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/AST/DeclBase.h" 15#include "clang/AST/Decl.h" 16#include "clang/AST/DeclContextInternals.h" 17#include "clang/AST/DeclCXX.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/DeclTemplate.h" 20#include "clang/AST/ExternalASTSource.h" 21#include "clang/AST/ASTContext.h" 22#include "clang/AST/Type.h" 23#include "clang/AST/Stmt.h" 24#include "clang/AST/StmtCXX.h" 25#include "llvm/ADT/DenseMap.h" 26#include "llvm/Support/raw_ostream.h" 27#include <algorithm> 28#include <cstdio> 29#include <vector> 30using namespace clang; 31 32//===----------------------------------------------------------------------===// 33// Statistics 34//===----------------------------------------------------------------------===// 35 36#define DECL(Derived, Base) static int n##Derived##s = 0; 37#include "clang/AST/DeclNodes.def" 38 39static bool StatSwitch = false; 40 41const char *Decl::getDeclKindName() const { 42 switch (DeclKind) { 43 default: assert(0 && "Declaration not in DeclNodes.def!"); 44#define DECL(Derived, Base) case Derived: return #Derived; 45#include "clang/AST/DeclNodes.def" 46 } 47} 48 49const char *DeclContext::getDeclKindName() const { 50 switch (DeclKind) { 51 default: assert(0 && "Declaration context not in DeclNodes.def!"); 52#define DECL(Derived, Base) case Decl::Derived: return #Derived; 53#include "clang/AST/DeclNodes.def" 54 } 55} 56 57bool Decl::CollectingStats(bool Enable) { 58 if (Enable) StatSwitch = true; 59 return StatSwitch; 60} 61 62void Decl::PrintStats() { 63 fprintf(stderr, "*** Decl Stats:\n"); 64 65 int totalDecls = 0; 66#define DECL(Derived, Base) totalDecls += n##Derived##s; 67#include "clang/AST/DeclNodes.def" 68 fprintf(stderr, " %d decls total.\n", totalDecls); 69 70 int totalBytes = 0; 71#define DECL(Derived, Base) \ 72 if (n##Derived##s > 0) { \ 73 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \ 74 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \ 75 n##Derived##s, (int)sizeof(Derived##Decl), \ 76 (int)(n##Derived##s * sizeof(Derived##Decl))); \ 77 } 78#include "clang/AST/DeclNodes.def" 79 80 fprintf(stderr, "Total bytes = %d\n", totalBytes); 81} 82 83void Decl::addDeclKind(Kind k) { 84 switch (k) { 85 default: assert(0 && "Declaration not in DeclNodes.def!"); 86#define DECL(Derived, Base) case Derived: ++n##Derived##s; break; 87#include "clang/AST/DeclNodes.def" 88 } 89} 90 91bool Decl::isTemplateParameterPack() const { 92 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this)) 93 return TTP->isParameterPack(); 94 95 return false; 96} 97 98bool Decl::isFunctionOrFunctionTemplate() const { 99 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this)) 100 return UD->getTargetDecl()->isFunctionOrFunctionTemplate(); 101 102 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this); 103} 104 105//===----------------------------------------------------------------------===// 106// PrettyStackTraceDecl Implementation 107//===----------------------------------------------------------------------===// 108 109void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const { 110 SourceLocation TheLoc = Loc; 111 if (TheLoc.isInvalid() && TheDecl) 112 TheLoc = TheDecl->getLocation(); 113 114 if (TheLoc.isValid()) { 115 TheLoc.print(OS, SM); 116 OS << ": "; 117 } 118 119 OS << Message; 120 121 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) 122 OS << " '" << DN->getQualifiedNameAsString() << '\''; 123 OS << '\n'; 124} 125 126//===----------------------------------------------------------------------===// 127// Decl Implementation 128//===----------------------------------------------------------------------===// 129 130// Out-of-line virtual method providing a home for Decl. 131Decl::~Decl() { 132 assert(!HasAttrs && "attributes should have been freed by Destroy"); 133} 134 135void Decl::setDeclContext(DeclContext *DC) { 136 if (isOutOfSemaDC()) 137 delete getMultipleDC(); 138 139 DeclCtx = DC; 140} 141 142void Decl::setLexicalDeclContext(DeclContext *DC) { 143 if (DC == getLexicalDeclContext()) 144 return; 145 146 if (isInSemaDC()) { 147 MultipleDC *MDC = new (getASTContext()) MultipleDC(); 148 MDC->SemanticDC = getDeclContext(); 149 MDC->LexicalDC = DC; 150 DeclCtx = MDC; 151 } else { 152 getMultipleDC()->LexicalDC = DC; 153 } 154} 155 156bool Decl::isInAnonymousNamespace() const { 157 const DeclContext *DC = getDeclContext(); 158 do { 159 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC)) 160 if (ND->isAnonymousNamespace()) 161 return true; 162 } while ((DC = DC->getParent())); 163 164 return false; 165} 166 167TranslationUnitDecl *Decl::getTranslationUnitDecl() { 168 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this)) 169 return TUD; 170 171 DeclContext *DC = getDeclContext(); 172 assert(DC && "This decl is not contained in a translation unit!"); 173 174 while (!DC->isTranslationUnit()) { 175 DC = DC->getParent(); 176 assert(DC && "This decl is not contained in a translation unit!"); 177 } 178 179 return cast<TranslationUnitDecl>(DC); 180} 181 182ASTContext &Decl::getASTContext() const { 183 return getTranslationUnitDecl()->getASTContext(); 184} 185 186unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { 187 switch (DeclKind) { 188 case Function: 189 case CXXMethod: 190 case CXXConstructor: 191 case CXXDestructor: 192 case CXXConversion: 193 case OverloadedFunction: 194 case Typedef: 195 case EnumConstant: 196 case Var: 197 case ImplicitParam: 198 case ParmVar: 199 case NonTypeTemplateParm: 200 case ObjCMethod: 201 case ObjCContainer: 202 case ObjCCategory: 203 case ObjCInterface: 204 case ObjCProperty: 205 case ObjCCompatibleAlias: 206 return IDNS_Ordinary; 207 208 case UsingShadow: 209 return 0; // we'll actually overwrite this later 210 211 case UnresolvedUsingValue: 212 case UnresolvedUsingTypename: 213 return IDNS_Ordinary | IDNS_Using; 214 215 case Using: 216 return IDNS_Using; 217 218 case ObjCProtocol: 219 return IDNS_ObjCProtocol; 220 221 case ObjCImplementation: 222 return IDNS_ObjCImplementation; 223 224 case ObjCCategoryImpl: 225 return IDNS_ObjCCategoryImpl; 226 227 case Field: 228 case ObjCAtDefsField: 229 case ObjCIvar: 230 return IDNS_Member; 231 232 case Record: 233 case CXXRecord: 234 case Enum: 235 case TemplateTypeParm: 236 return IDNS_Tag; 237 238 case Namespace: 239 case Template: 240 case FunctionTemplate: 241 case ClassTemplate: 242 case TemplateTemplateParm: 243 case NamespaceAlias: 244 return IDNS_Tag | IDNS_Ordinary; 245 246 // Never have names. 247 case Friend: 248 case FriendTemplate: 249 case LinkageSpec: 250 case FileScopeAsm: 251 case StaticAssert: 252 case ObjCClass: 253 case ObjCPropertyImpl: 254 case ObjCForwardProtocol: 255 case Block: 256 case TranslationUnit: 257 258 // Aren't looked up? 259 case UsingDirective: 260 case ClassTemplateSpecialization: 261 case ClassTemplatePartialSpecialization: 262 return 0; 263 } 264 265 return 0; 266} 267 268void Decl::addAttr(Attr *NewAttr) { 269 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this); 270 271 NewAttr->setNext(ExistingAttr); 272 ExistingAttr = NewAttr; 273 274 HasAttrs = true; 275} 276 277void Decl::invalidateAttrs() { 278 if (!HasAttrs) return; 279 280 HasAttrs = false; 281 getASTContext().eraseDeclAttrs(this); 282} 283 284const Attr *Decl::getAttrsImpl() const { 285 assert(HasAttrs && "getAttrs() should verify this!"); 286 return getASTContext().getDeclAttrs(this); 287} 288 289void Decl::swapAttrs(Decl *RHS) { 290 bool HasLHSAttr = this->HasAttrs; 291 bool HasRHSAttr = RHS->HasAttrs; 292 293 // Usually, neither decl has attrs, nothing to do. 294 if (!HasLHSAttr && !HasRHSAttr) return; 295 296 // If 'this' has no attrs, swap the other way. 297 if (!HasLHSAttr) 298 return RHS->swapAttrs(this); 299 300 ASTContext &Context = getASTContext(); 301 302 // Handle the case when both decls have attrs. 303 if (HasRHSAttr) { 304 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS)); 305 return; 306 } 307 308 // Otherwise, LHS has an attr and RHS doesn't. 309 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this); 310 Context.eraseDeclAttrs(this); 311 this->HasAttrs = false; 312 RHS->HasAttrs = true; 313} 314 315 316void Decl::Destroy(ASTContext &C) { 317 // Free attributes for this decl. 318 if (HasAttrs) { 319 C.getDeclAttrs(this)->Destroy(C); 320 invalidateAttrs(); 321 HasAttrs = false; 322 } 323 324#if 0 325 // FIXME: Once ownership is fully understood, we can enable this code 326 if (DeclContext *DC = dyn_cast<DeclContext>(this)) 327 DC->decls_begin()->Destroy(C); 328 329 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0 330 // within the loop, only the Destroy method for the first Decl 331 // will deallocate all of the Decls in a chain. 332 333 Decl* N = getNextDeclInContext(); 334 335 while (N) { 336 Decl* Tmp = N->getNextDeclInContext(); 337 N->NextDeclInContext = 0; 338 N->Destroy(C); 339 N = Tmp; 340 } 341 342 if (isOutOfSemaDC()) 343 delete (C) getMultipleDC(); 344 345 this->~Decl(); 346 C.Deallocate((void *)this); 347#endif 348} 349 350Decl *Decl::castFromDeclContext (const DeclContext *D) { 351 Decl::Kind DK = D->getDeclKind(); 352 switch(DK) { 353#define DECL_CONTEXT(Name) \ 354 case Decl::Name: \ 355 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); 356#define DECL_CONTEXT_BASE(Name) 357#include "clang/AST/DeclNodes.def" 358 default: 359#define DECL_CONTEXT_BASE(Name) \ 360 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ 361 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); 362#include "clang/AST/DeclNodes.def" 363 assert(false && "a decl that inherits DeclContext isn't handled"); 364 return 0; 365 } 366} 367 368DeclContext *Decl::castToDeclContext(const Decl *D) { 369 Decl::Kind DK = D->getKind(); 370 switch(DK) { 371#define DECL_CONTEXT(Name) \ 372 case Decl::Name: \ 373 return static_cast<Name##Decl*>(const_cast<Decl*>(D)); 374#define DECL_CONTEXT_BASE(Name) 375#include "clang/AST/DeclNodes.def" 376 default: 377#define DECL_CONTEXT_BASE(Name) \ 378 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ 379 return static_cast<Name##Decl*>(const_cast<Decl*>(D)); 380#include "clang/AST/DeclNodes.def" 381 assert(false && "a decl that inherits DeclContext isn't handled"); 382 return 0; 383 } 384} 385 386CompoundStmt* Decl::getCompoundBody() const { 387 return dyn_cast_or_null<CompoundStmt>(getBody()); 388} 389 390SourceLocation Decl::getBodyRBrace() const { 391 Stmt *Body = getBody(); 392 if (!Body) 393 return SourceLocation(); 394 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body)) 395 return CS->getRBracLoc(); 396 assert(isa<CXXTryStmt>(Body) && 397 "Body can only be CompoundStmt or CXXTryStmt"); 398 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd(); 399} 400 401#ifndef NDEBUG 402void Decl::CheckAccessDeclContext() const { 403 // If the decl is the toplevel translation unit or if we're not in a 404 // record decl context, we don't need to check anything. 405 if (isa<TranslationUnitDecl>(this) || 406 !isa<CXXRecordDecl>(getDeclContext())) 407 return; 408 409 assert(Access != AS_none && 410 "Access specifier is AS_none inside a record decl"); 411} 412 413#endif 414 415//===----------------------------------------------------------------------===// 416// DeclContext Implementation 417//===----------------------------------------------------------------------===// 418 419bool DeclContext::classof(const Decl *D) { 420 switch (D->getKind()) { 421#define DECL_CONTEXT(Name) case Decl::Name: 422#define DECL_CONTEXT_BASE(Name) 423#include "clang/AST/DeclNodes.def" 424 return true; 425 default: 426#define DECL_CONTEXT_BASE(Name) \ 427 if (D->getKind() >= Decl::Name##First && \ 428 D->getKind() <= Decl::Name##Last) \ 429 return true; 430#include "clang/AST/DeclNodes.def" 431 return false; 432 } 433} 434 435DeclContext::~DeclContext() { 436 delete static_cast<StoredDeclsMap*>(LookupPtr); 437} 438 439void DeclContext::DestroyDecls(ASTContext &C) { 440 for (decl_iterator D = decls_begin(); D != decls_end(); ) 441 (*D++)->Destroy(C); 442} 443 444/// \brief Find the parent context of this context that will be 445/// used for unqualified name lookup. 446/// 447/// Generally, the parent lookup context is the semantic context. However, for 448/// a friend function the parent lookup context is the lexical context, which 449/// is the class in which the friend is declared. 450DeclContext *DeclContext::getLookupParent() { 451 // FIXME: Find a better way to identify friends 452 if (isa<FunctionDecl>(this)) 453 if (getParent()->getLookupContext()->isFileContext() && 454 getLexicalParent()->getLookupContext()->isRecord()) 455 return getLexicalParent(); 456 457 return getParent(); 458} 459 460bool DeclContext::isDependentContext() const { 461 if (isFileContext()) 462 return false; 463 464 if (isa<ClassTemplatePartialSpecializationDecl>(this)) 465 return true; 466 467 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) 468 if (Record->getDescribedClassTemplate()) 469 return true; 470 471 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) 472 if (Function->getDescribedFunctionTemplate()) 473 return true; 474 475 return getParent() && getParent()->isDependentContext(); 476} 477 478bool DeclContext::isTransparentContext() const { 479 if (DeclKind == Decl::Enum) 480 return true; // FIXME: Check for C++0x scoped enums 481 else if (DeclKind == Decl::LinkageSpec) 482 return true; 483 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast) 484 return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); 485 else if (DeclKind == Decl::Namespace) 486 return false; // FIXME: Check for C++0x inline namespaces 487 488 return false; 489} 490 491bool DeclContext::Encloses(DeclContext *DC) { 492 if (getPrimaryContext() != this) 493 return getPrimaryContext()->Encloses(DC); 494 495 for (; DC; DC = DC->getParent()) 496 if (DC->getPrimaryContext() == this) 497 return true; 498 return false; 499} 500 501DeclContext *DeclContext::getPrimaryContext() { 502 switch (DeclKind) { 503 case Decl::TranslationUnit: 504 case Decl::LinkageSpec: 505 case Decl::Block: 506 // There is only one DeclContext for these entities. 507 return this; 508 509 case Decl::Namespace: 510 // The original namespace is our primary context. 511 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); 512 513 case Decl::ObjCMethod: 514 return this; 515 516 case Decl::ObjCInterface: 517 case Decl::ObjCProtocol: 518 case Decl::ObjCCategory: 519 // FIXME: Can Objective-C interfaces be forward-declared? 520 return this; 521 522 case Decl::ObjCImplementation: 523 case Decl::ObjCCategoryImpl: 524 return this; 525 526 default: 527 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) { 528 // If this is a tag type that has a definition or is currently 529 // being defined, that definition is our primary context. 530 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>()) 531 if (TagT->isBeingDefined() || 532 (TagT->getDecl() && TagT->getDecl()->isDefinition())) 533 return TagT->getDecl(); 534 return this; 535 } 536 537 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && 538 "Unknown DeclContext kind"); 539 return this; 540 } 541} 542 543DeclContext *DeclContext::getNextContext() { 544 switch (DeclKind) { 545 case Decl::Namespace: 546 // Return the next namespace 547 return static_cast<NamespaceDecl*>(this)->getNextNamespace(); 548 549 default: 550 return 0; 551 } 552} 553 554/// \brief Load the declarations within this lexical storage from an 555/// external source. 556void 557DeclContext::LoadLexicalDeclsFromExternalStorage() const { 558 ExternalASTSource *Source = getParentASTContext().getExternalSource(); 559 assert(hasExternalLexicalStorage() && Source && "No external storage?"); 560 561 llvm::SmallVector<uint32_t, 64> Decls; 562 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this), 563 Decls)) 564 return; 565 566 // There is no longer any lexical storage in this context 567 ExternalLexicalStorage = false; 568 569 if (Decls.empty()) 570 return; 571 572 // Resolve all of the declaration IDs into declarations, building up 573 // a chain of declarations via the Decl::NextDeclInContext field. 574 Decl *FirstNewDecl = 0; 575 Decl *PrevDecl = 0; 576 for (unsigned I = 0, N = Decls.size(); I != N; ++I) { 577 Decl *D = Source->GetDecl(Decls[I]); 578 if (PrevDecl) 579 PrevDecl->NextDeclInContext = D; 580 else 581 FirstNewDecl = D; 582 583 PrevDecl = D; 584 } 585 586 // Splice the newly-read declarations into the beginning of the list 587 // of declarations. 588 PrevDecl->NextDeclInContext = FirstDecl; 589 FirstDecl = FirstNewDecl; 590 if (!LastDecl) 591 LastDecl = PrevDecl; 592} 593 594void 595DeclContext::LoadVisibleDeclsFromExternalStorage() const { 596 DeclContext *This = const_cast<DeclContext *>(this); 597 ExternalASTSource *Source = getParentASTContext().getExternalSource(); 598 assert(hasExternalVisibleStorage() && Source && "No external storage?"); 599 600 llvm::SmallVector<VisibleDeclaration, 64> Decls; 601 if (Source->ReadDeclsVisibleInContext(This, Decls)) 602 return; 603 604 // There is no longer any visible storage in this context 605 ExternalVisibleStorage = false; 606 607 // Load the declaration IDs for all of the names visible in this 608 // context. 609 assert(!LookupPtr && "Have a lookup map before de-serialization?"); 610 StoredDeclsMap *Map = new StoredDeclsMap; 611 LookupPtr = Map; 612 for (unsigned I = 0, N = Decls.size(); I != N; ++I) { 613 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations); 614 } 615} 616 617DeclContext::decl_iterator DeclContext::decls_begin() const { 618 if (hasExternalLexicalStorage()) 619 LoadLexicalDeclsFromExternalStorage(); 620 621 // FIXME: Check whether we need to load some declarations from 622 // external storage. 623 return decl_iterator(FirstDecl); 624} 625 626DeclContext::decl_iterator DeclContext::decls_end() const { 627 if (hasExternalLexicalStorage()) 628 LoadLexicalDeclsFromExternalStorage(); 629 630 return decl_iterator(); 631} 632 633bool DeclContext::decls_empty() const { 634 if (hasExternalLexicalStorage()) 635 LoadLexicalDeclsFromExternalStorage(); 636 637 return !FirstDecl; 638} 639 640void DeclContext::addHiddenDecl(Decl *D) { 641 assert(D->getLexicalDeclContext() == this && 642 "Decl inserted into wrong lexical context"); 643 assert(!D->getNextDeclInContext() && D != LastDecl && 644 "Decl already inserted into a DeclContext"); 645 646 if (FirstDecl) { 647 LastDecl->NextDeclInContext = D; 648 LastDecl = D; 649 } else { 650 FirstDecl = LastDecl = D; 651 } 652} 653 654void DeclContext::addDecl(Decl *D) { 655 addHiddenDecl(D); 656 657 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) 658 ND->getDeclContext()->makeDeclVisibleInContext(ND); 659} 660 661/// buildLookup - Build the lookup data structure with all of the 662/// declarations in DCtx (and any other contexts linked to it or 663/// transparent contexts nested within it). 664void DeclContext::buildLookup(DeclContext *DCtx) { 665 for (; DCtx; DCtx = DCtx->getNextContext()) { 666 for (decl_iterator D = DCtx->decls_begin(), 667 DEnd = DCtx->decls_end(); 668 D != DEnd; ++D) { 669 // Insert this declaration into the lookup structure, but only 670 // if it's semantically in its decl context. During non-lazy 671 // lookup building, this is implicitly enforced by addDecl. 672 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) 673 if (D->getDeclContext() == DCtx) 674 makeDeclVisibleInContextImpl(ND); 675 676 // Insert any forward-declared Objective-C interfaces into the lookup 677 // data structure. 678 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D)) 679 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end(); 680 I != IEnd; ++I) 681 makeDeclVisibleInContextImpl(I->getInterface()); 682 683 // If this declaration is itself a transparent declaration context, 684 // add its members (recursively). 685 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) 686 if (InnerCtx->isTransparentContext()) 687 buildLookup(InnerCtx->getPrimaryContext()); 688 } 689 } 690} 691 692DeclContext::lookup_result 693DeclContext::lookup(DeclarationName Name) { 694 DeclContext *PrimaryContext = getPrimaryContext(); 695 if (PrimaryContext != this) 696 return PrimaryContext->lookup(Name); 697 698 if (hasExternalVisibleStorage()) 699 LoadVisibleDeclsFromExternalStorage(); 700 701 /// If there is no lookup data structure, build one now by walking 702 /// all of the linked DeclContexts (in declaration order!) and 703 /// inserting their values. 704 if (!LookupPtr) { 705 buildLookup(this); 706 707 if (!LookupPtr) 708 return lookup_result(0, 0); 709 } 710 711 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr); 712 StoredDeclsMap::iterator Pos = Map->find(Name); 713 if (Pos == Map->end()) 714 return lookup_result(0, 0); 715 return Pos->second.getLookupResult(getParentASTContext()); 716} 717 718DeclContext::lookup_const_result 719DeclContext::lookup(DeclarationName Name) const { 720 return const_cast<DeclContext*>(this)->lookup(Name); 721} 722 723DeclContext *DeclContext::getLookupContext() { 724 DeclContext *Ctx = this; 725 // Skip through transparent contexts. 726 while (Ctx->isTransparentContext()) 727 Ctx = Ctx->getParent(); 728 return Ctx; 729} 730 731DeclContext *DeclContext::getEnclosingNamespaceContext() { 732 DeclContext *Ctx = this; 733 // Skip through non-namespace, non-translation-unit contexts. 734 while (!Ctx->isFileContext() || Ctx->isTransparentContext()) 735 Ctx = Ctx->getParent(); 736 return Ctx->getPrimaryContext(); 737} 738 739void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) { 740 // FIXME: This feels like a hack. Should DeclarationName support 741 // template-ids, or is there a better way to keep specializations 742 // from being visible? 743 if (isa<ClassTemplateSpecializationDecl>(D)) 744 return; 745 746 DeclContext *PrimaryContext = getPrimaryContext(); 747 if (PrimaryContext != this) { 748 PrimaryContext->makeDeclVisibleInContext(D, Recoverable); 749 return; 750 } 751 752 // If we already have a lookup data structure, perform the insertion 753 // into it. Otherwise, be lazy and don't build that structure until 754 // someone asks for it. 755 if (LookupPtr || !Recoverable) 756 makeDeclVisibleInContextImpl(D); 757 758 // If we are a transparent context, insert into our parent context, 759 // too. This operation is recursive. 760 if (isTransparentContext()) 761 getParent()->makeDeclVisibleInContext(D, Recoverable); 762} 763 764void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { 765 // Skip unnamed declarations. 766 if (!D->getDeclName()) 767 return; 768 769 // FIXME: This feels like a hack. Should DeclarationName support 770 // template-ids, or is there a better way to keep specializations 771 // from being visible? 772 if (isa<ClassTemplateSpecializationDecl>(D)) 773 return; 774 775 if (!LookupPtr) 776 LookupPtr = new StoredDeclsMap; 777 778 // Insert this declaration into the map. 779 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr); 780 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()]; 781 if (DeclNameEntries.isNull()) { 782 DeclNameEntries.setOnlyValue(D); 783 return; 784 } 785 786 // If it is possible that this is a redeclaration, check to see if there is 787 // already a decl for which declarationReplaces returns true. If there is 788 // one, just replace it and return. 789 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D)) 790 return; 791 792 // Put this declaration into the appropriate slot. 793 DeclNameEntries.AddSubsequentDecl(D); 794} 795 796/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within 797/// this context. 798DeclContext::udir_iterator_range 799DeclContext::getUsingDirectives() const { 800 lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); 801 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), 802 reinterpret_cast<udir_iterator>(Result.second)); 803} 804 805void StoredDeclsList::materializeDecls(ASTContext &Context) { 806 if (isNull()) 807 return; 808 809 switch ((DataKind)(Data & 0x03)) { 810 case DK_Decl: 811 case DK_Decl_Vector: 812 break; 813 814 case DK_DeclID: { 815 // Resolve this declaration ID to an actual declaration by 816 // querying the external AST source. 817 unsigned DeclID = Data >> 2; 818 819 ExternalASTSource *Source = Context.getExternalSource(); 820 assert(Source && "No external AST source available!"); 821 822 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID)); 823 break; 824 } 825 826 case DK_ID_Vector: { 827 // We have a vector of declaration IDs. Resolve all of them to 828 // actual declarations. 829 VectorTy &Vector = *getAsVector(); 830 ExternalASTSource *Source = Context.getExternalSource(); 831 assert(Source && "No external AST source available!"); 832 833 for (unsigned I = 0, N = Vector.size(); I != N; ++I) 834 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I])); 835 836 Data = (Data & ~0x03) | DK_Decl_Vector; 837 break; 838 } 839 } 840} 841