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