DeclBase.cpp revision 198092
1178479Sjb//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===// 2178479Sjb// 3178479Sjb// The LLVM Compiler Infrastructure 4178479Sjb// 5178479Sjb// This file is distributed under the University of Illinois Open Source 6178479Sjb// License. See LICENSE.TXT for details. 7178479Sjb// 8178479Sjb//===----------------------------------------------------------------------===// 9178479Sjb// 10178479Sjb// This file implements the Decl and DeclContext classes. 11178479Sjb// 12178479Sjb//===----------------------------------------------------------------------===// 13178479Sjb 14178479Sjb#include "clang/AST/DeclBase.h" 15178479Sjb#include "clang/AST/Decl.h" 16178479Sjb#include "clang/AST/DeclContextInternals.h" 17178479Sjb#include "clang/AST/DeclCXX.h" 18178479Sjb#include "clang/AST/DeclObjC.h" 19178479Sjb#include "clang/AST/DeclTemplate.h" 20178479Sjb#include "clang/AST/ExternalASTSource.h" 21178479Sjb#include "clang/AST/ASTContext.h" 22178479Sjb#include "clang/AST/Type.h" 23178479Sjb#include "clang/AST/Stmt.h" 24178479Sjb#include "clang/AST/StmtCXX.h" 25178479Sjb#include "llvm/ADT/DenseMap.h" 26178479Sjb#include "llvm/Support/raw_ostream.h" 27178479Sjb#include <algorithm> 28178479Sjb#include <cstdio> 29178479Sjb#include <vector> 30178479Sjbusing namespace clang; 31178479Sjb 32178479Sjb//===----------------------------------------------------------------------===// 33178479Sjb// Statistics 34178479Sjb//===----------------------------------------------------------------------===// 35178479Sjb 36178479Sjb#define DECL(Derived, Base) static int n##Derived##s = 0; 37178479Sjb#include "clang/AST/DeclNodes.def" 38178479Sjb 39178479Sjbstatic bool StatSwitch = false; 40178479Sjb 41178479Sjbconst char *Decl::getDeclKindName() const { 42178479Sjb switch (DeclKind) { 43178479Sjb default: assert(0 && "Declaration not in DeclNodes.def!"); 44178479Sjb#define DECL(Derived, Base) case Derived: return #Derived; 45178479Sjb#include "clang/AST/DeclNodes.def" 46178479Sjb } 47178479Sjb} 48178479Sjb 49178479Sjbconst char *DeclContext::getDeclKindName() const { 50178479Sjb switch (DeclKind) { 51178479Sjb default: assert(0 && "Declaration context not in DeclNodes.def!"); 52178479Sjb#define DECL(Derived, Base) case Decl::Derived: return #Derived; 53178479Sjb#include "clang/AST/DeclNodes.def" 54178479Sjb } 55178479Sjb} 56178479Sjb 57178479Sjbbool Decl::CollectingStats(bool Enable) { 58178479Sjb if (Enable) 59178479Sjb StatSwitch = true; 60178479Sjb return StatSwitch; 61178479Sjb} 62178479Sjb 63178479Sjbvoid Decl::PrintStats() { 64178479Sjb fprintf(stderr, "*** Decl Stats:\n"); 65178479Sjb 66178479Sjb int totalDecls = 0; 67178479Sjb#define DECL(Derived, Base) totalDecls += n##Derived##s; 68178479Sjb#include "clang/AST/DeclNodes.def" 69178479Sjb fprintf(stderr, " %d decls total.\n", totalDecls); 70178479Sjb 71178479Sjb int totalBytes = 0; 72178479Sjb#define DECL(Derived, Base) \ 73178479Sjb if (n##Derived##s > 0) { \ 74178479Sjb totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \ 75178479Sjb fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \ 76178479Sjb n##Derived##s, (int)sizeof(Derived##Decl), \ 77178479Sjb (int)(n##Derived##s * sizeof(Derived##Decl))); \ 78178479Sjb } 79178479Sjb#include "clang/AST/DeclNodes.def" 80178479Sjb 81178479Sjb fprintf(stderr, "Total bytes = %d\n", totalBytes); 82178479Sjb} 83178479Sjb 84178479Sjbvoid Decl::addDeclKind(Kind k) { 85178479Sjb switch (k) { 86178479Sjb default: assert(0 && "Declaration not in DeclNodes.def!"); 87178479Sjb#define DECL(Derived, Base) case Derived: ++n##Derived##s; break; 88178479Sjb#include "clang/AST/DeclNodes.def" 89178479Sjb } 90178479Sjb} 91178479Sjb 92178479Sjbbool Decl::isTemplateParameterPack() const { 93178479Sjb if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this)) 94178479Sjb return TTP->isParameterPack(); 95178479Sjb 96178479Sjb return false; 97178479Sjb} 98178479Sjb 99178479Sjbbool Decl::isFunctionOrFunctionTemplate() const { 100178479Sjb if (const UsingDecl *UD = dyn_cast<UsingDecl>(this)) 101178479Sjb return UD->getTargetDecl()->isFunctionOrFunctionTemplate(); 102178479Sjb 103178479Sjb return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this); 104178479Sjb} 105178479Sjb 106178479Sjb//===----------------------------------------------------------------------===// 107178479Sjb// PrettyStackTraceDecl Implementation 108178479Sjb//===----------------------------------------------------------------------===// 109178479Sjb 110178479Sjbvoid PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const { 111178479Sjb SourceLocation TheLoc = Loc; 112178479Sjb if (TheLoc.isInvalid() && TheDecl) 113178479Sjb TheLoc = TheDecl->getLocation(); 114178479Sjb 115178479Sjb if (TheLoc.isValid()) { 116178479Sjb TheLoc.print(OS, SM); 117178479Sjb OS << ": "; 118178479Sjb } 119178479Sjb 120178479Sjb OS << Message; 121178479Sjb 122178479Sjb if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) 123178479Sjb OS << " '" << DN->getQualifiedNameAsString() << '\''; 124178479Sjb OS << '\n'; 125178479Sjb} 126178479Sjb 127178479Sjb//===----------------------------------------------------------------------===// 128178479Sjb// Decl Implementation 129178479Sjb//===----------------------------------------------------------------------===// 130178479Sjb 131178479Sjb// Out-of-line virtual method providing a home for Decl. 132178479SjbDecl::~Decl() { 133178479Sjb if (isOutOfSemaDC()) 134178479Sjb delete getMultipleDC(); 135178479Sjb 136178479Sjb assert(!HasAttrs && "attributes should have been freed by Destroy"); 137178479Sjb} 138178479Sjb 139178479Sjbvoid Decl::setDeclContext(DeclContext *DC) { 140178479Sjb if (isOutOfSemaDC()) 141178479Sjb delete getMultipleDC(); 142178479Sjb 143178479Sjb DeclCtx = DC; 144178479Sjb} 145178479Sjb 146178479Sjbvoid Decl::setLexicalDeclContext(DeclContext *DC) { 147178479Sjb if (DC == getLexicalDeclContext()) 148178479Sjb return; 149178479Sjb 150178479Sjb if (isInSemaDC()) { 151178550Sjb MultipleDC *MDC = new MultipleDC(); 152178479Sjb MDC->SemanticDC = getDeclContext(); 153178479Sjb MDC->LexicalDC = DC; 154178479Sjb DeclCtx = MDC; 155178479Sjb } else { 156178479Sjb getMultipleDC()->LexicalDC = DC; 157178479Sjb } 158178479Sjb} 159178479Sjb 160178479Sjbbool Decl::isInAnonymousNamespace() const { 161178479Sjb const DeclContext *DC = getDeclContext(); 162178479Sjb do { 163178479Sjb if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC)) 164178479Sjb if (ND->isAnonymousNamespace()) 165178479Sjb return true; 166178479Sjb } while ((DC = DC->getParent())); 167178479Sjb 168178479Sjb return false; 169178479Sjb} 170178479Sjb 171178479SjbTranslationUnitDecl *Decl::getTranslationUnitDecl() { 172178479Sjb if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this)) 173178479Sjb return TUD; 174178479Sjb 175178479Sjb DeclContext *DC = getDeclContext(); 176178479Sjb assert(DC && "This decl is not contained in a translation unit!"); 177178479Sjb 178178479Sjb while (!DC->isTranslationUnit()) { 179178479Sjb DC = DC->getParent(); 180178479Sjb assert(DC && "This decl is not contained in a translation unit!"); 181178479Sjb } 182178479Sjb 183178479Sjb return cast<TranslationUnitDecl>(DC); 184178479Sjb} 185178479Sjb 186178479SjbASTContext &Decl::getASTContext() const { 187178479Sjb return getTranslationUnitDecl()->getASTContext(); 188178479Sjb} 189178479Sjb 190178479Sjbunsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { 191178479Sjb switch (DeclKind) { 192178479Sjb default: 193178479Sjb if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast) 194178479Sjb return IDNS_Ordinary; 195178479Sjb assert(0 && "Unknown decl kind!"); 196178479Sjb case OverloadedFunction: 197178479Sjb case Typedef: 198178479Sjb case EnumConstant: 199178479Sjb case Var: 200178479Sjb case ImplicitParam: 201178479Sjb case ParmVar: 202178479Sjb case OriginalParmVar: 203178479Sjb case NonTypeTemplateParm: 204178479Sjb case Using: 205178479Sjb case UnresolvedUsing: 206178479Sjb case ObjCMethod: 207178479Sjb case ObjCContainer: 208178479Sjb case ObjCCategory: 209178479Sjb case ObjCInterface: 210178479Sjb case ObjCProperty: 211178479Sjb case ObjCCompatibleAlias: 212178479Sjb return IDNS_Ordinary; 213178479Sjb 214178479Sjb case ObjCProtocol: 215178479Sjb return IDNS_ObjCProtocol; 216178479Sjb 217178479Sjb case ObjCImplementation: 218178479Sjb return IDNS_ObjCImplementation; 219178479Sjb 220178479Sjb case ObjCCategoryImpl: 221178479Sjb return IDNS_ObjCCategoryImpl; 222178479Sjb 223178479Sjb case Field: 224178479Sjb case ObjCAtDefsField: 225178479Sjb case ObjCIvar: 226178479Sjb return IDNS_Member; 227178479Sjb 228178479Sjb case Record: 229178479Sjb case CXXRecord: 230178479Sjb case Enum: 231178479Sjb case TemplateTypeParm: 232178479Sjb return IDNS_Tag; 233178479Sjb 234178479Sjb case Namespace: 235178479Sjb case Template: 236178479Sjb case FunctionTemplate: 237178479Sjb case ClassTemplate: 238178479Sjb case TemplateTemplateParm: 239178479Sjb case NamespaceAlias: 240178479Sjb return IDNS_Tag | IDNS_Ordinary; 241178479Sjb 242178479Sjb // Never have names. 243178479Sjb case Friend: 244178479Sjb case FriendTemplate: 245178479Sjb case LinkageSpec: 246178479Sjb case FileScopeAsm: 247178479Sjb case StaticAssert: 248178479Sjb case ObjCClass: 249178479Sjb case ObjCPropertyImpl: 250178479Sjb case ObjCForwardProtocol: 251178479Sjb case Block: 252178479Sjb case TranslationUnit: 253178479Sjb 254178479Sjb // Aren't looked up? 255178479Sjb case UsingDirective: 256178479Sjb case ClassTemplateSpecialization: 257178479Sjb case ClassTemplatePartialSpecialization: 258178479Sjb return 0; 259178479Sjb } 260178479Sjb} 261178479Sjb 262178479Sjbvoid Decl::addAttr(Attr *NewAttr) { 263178479Sjb Attr *&ExistingAttr = getASTContext().getDeclAttrs(this); 264178479Sjb 265178479Sjb NewAttr->setNext(ExistingAttr); 266178479Sjb ExistingAttr = NewAttr; 267178479Sjb 268178479Sjb HasAttrs = true; 269178479Sjb} 270178479Sjb 271178479Sjbvoid Decl::invalidateAttrs() { 272178479Sjb if (!HasAttrs) return; 273178479Sjb 274178479Sjb HasAttrs = false; 275178479Sjb getASTContext().eraseDeclAttrs(this); 276178479Sjb} 277178479Sjb 278178479Sjbconst Attr *Decl::getAttrsImpl() const { 279178479Sjb assert(HasAttrs && "getAttrs() should verify this!"); 280178479Sjb return getASTContext().getDeclAttrs(this); 281178479Sjb} 282178479Sjb 283178479Sjbvoid Decl::swapAttrs(Decl *RHS) { 284178479Sjb bool HasLHSAttr = this->HasAttrs; 285178479Sjb bool HasRHSAttr = RHS->HasAttrs; 286178479Sjb 287178479Sjb // Usually, neither decl has attrs, nothing to do. 288178479Sjb if (!HasLHSAttr && !HasRHSAttr) return; 289178479Sjb 290178479Sjb // If 'this' has no attrs, swap the other way. 291178479Sjb if (!HasLHSAttr) 292178479Sjb return RHS->swapAttrs(this); 293178479Sjb 294178479Sjb ASTContext &Context = getASTContext(); 295178479Sjb 296178479Sjb // Handle the case when both decls have attrs. 297178479Sjb if (HasRHSAttr) { 298178479Sjb std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS)); 299178479Sjb return; 300178479Sjb } 301178479Sjb 302178479Sjb // Otherwise, LHS has an attr and RHS doesn't. 303178479Sjb Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this); 304178479Sjb Context.eraseDeclAttrs(this); 305178479Sjb this->HasAttrs = false; 306178479Sjb RHS->HasAttrs = true; 307178479Sjb} 308178479Sjb 309178479Sjb 310178479Sjbvoid Decl::Destroy(ASTContext &C) { 311178479Sjb // Free attributes for this decl. 312178479Sjb if (HasAttrs) { 313178479Sjb C.getDeclAttrs(this)->Destroy(C); 314178479Sjb invalidateAttrs(); 315178479Sjb HasAttrs = false; 316178479Sjb } 317178479Sjb 318178479Sjb#if 0 319178479Sjb // FIXME: Once ownership is fully understood, we can enable this code 320178479Sjb if (DeclContext *DC = dyn_cast<DeclContext>(this)) 321178479Sjb DC->decls_begin()->Destroy(C); 322178479Sjb 323178479Sjb // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0 324178479Sjb // within the loop, only the Destroy method for the first Decl 325178479Sjb // will deallocate all of the Decls in a chain. 326178479Sjb 327178479Sjb Decl* N = getNextDeclInContext(); 328178479Sjb 329178479Sjb while (N) { 330178479Sjb Decl* Tmp = N->getNextDeclInContext(); 331178479Sjb N->NextDeclInContext = 0; 332178479Sjb N->Destroy(C); 333178479Sjb N = Tmp; 334178479Sjb } 335178479Sjb 336178479Sjb this->~Decl(); 337178479Sjb C.Deallocate((void *)this); 338178479Sjb#endif 339178479Sjb} 340178479Sjb 341178479SjbDecl *Decl::castFromDeclContext (const DeclContext *D) { 342178479Sjb Decl::Kind DK = D->getDeclKind(); 343178479Sjb switch(DK) { 344178479Sjb#define DECL_CONTEXT(Name) \ 345178479Sjb case Decl::Name: \ 346178479Sjb return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); 347178479Sjb#define DECL_CONTEXT_BASE(Name) 348178479Sjb#include "clang/AST/DeclNodes.def" 349178479Sjb default: 350178479Sjb#define DECL_CONTEXT_BASE(Name) \ 351178479Sjb if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ 352178479Sjb return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); 353178479Sjb#include "clang/AST/DeclNodes.def" 354178479Sjb assert(false && "a decl that inherits DeclContext isn't handled"); 355178479Sjb return 0; 356178479Sjb } 357178479Sjb} 358178479Sjb 359178479SjbDeclContext *Decl::castToDeclContext(const Decl *D) { 360178479Sjb Decl::Kind DK = D->getKind(); 361178479Sjb switch(DK) { 362178479Sjb#define DECL_CONTEXT(Name) \ 363178479Sjb case Decl::Name: \ 364178479Sjb return static_cast<Name##Decl*>(const_cast<Decl*>(D)); 365178479Sjb#define DECL_CONTEXT_BASE(Name) 366178479Sjb#include "clang/AST/DeclNodes.def" 367178479Sjb default: 368178479Sjb#define DECL_CONTEXT_BASE(Name) \ 369178479Sjb if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ 370178479Sjb return static_cast<Name##Decl*>(const_cast<Decl*>(D)); 371178479Sjb#include "clang/AST/DeclNodes.def" 372178479Sjb assert(false && "a decl that inherits DeclContext isn't handled"); 373178479Sjb return 0; 374178479Sjb } 375178479Sjb} 376178479Sjb 377178479SjbCompoundStmt* Decl::getCompoundBody() const { 378178479Sjb return dyn_cast_or_null<CompoundStmt>(getBody()); 379178479Sjb} 380178479Sjb 381178479SjbSourceLocation Decl::getBodyRBrace() const { 382178479Sjb Stmt *Body = getBody(); 383178479Sjb if (!Body) 384178479Sjb return SourceLocation(); 385178479Sjb if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body)) 386178479Sjb return CS->getRBracLoc(); 387178479Sjb assert(isa<CXXTryStmt>(Body) && 388178479Sjb "Body can only be CompoundStmt or CXXTryStmt"); 389178479Sjb return cast<CXXTryStmt>(Body)->getSourceRange().getEnd(); 390178479Sjb} 391178479Sjb 392178479Sjb#ifndef NDEBUG 393178479Sjbvoid Decl::CheckAccessDeclContext() const { 394178479Sjb // If the decl is the toplevel translation unit or if we're not in a 395178479Sjb // record decl context, we don't need to check anything. 396178479Sjb if (isa<TranslationUnitDecl>(this) || 397178479Sjb !isa<CXXRecordDecl>(getDeclContext())) 398178479Sjb return; 399178479Sjb 400178479Sjb assert(Access != AS_none && 401178479Sjb "Access specifier is AS_none inside a record decl"); 402178479Sjb} 403178479Sjb 404178479Sjb#endif 405178479Sjb 406178479Sjb//===----------------------------------------------------------------------===// 407178479Sjb// DeclContext Implementation 408178479Sjb//===----------------------------------------------------------------------===// 409178479Sjb 410178479Sjbbool DeclContext::classof(const Decl *D) { 411178479Sjb switch (D->getKind()) { 412178479Sjb#define DECL_CONTEXT(Name) case Decl::Name: 413178479Sjb#define DECL_CONTEXT_BASE(Name) 414178479Sjb#include "clang/AST/DeclNodes.def" 415178479Sjb return true; 416178479Sjb default: 417178479Sjb#define DECL_CONTEXT_BASE(Name) \ 418178479Sjb if (D->getKind() >= Decl::Name##First && \ 419178479Sjb D->getKind() <= Decl::Name##Last) \ 420178479Sjb return true; 421178479Sjb#include "clang/AST/DeclNodes.def" 422178479Sjb return false; 423178479Sjb } 424178479Sjb} 425178479Sjb 426178479SjbDeclContext::~DeclContext() { 427178479Sjb delete static_cast<StoredDeclsMap*>(LookupPtr); 428178479Sjb} 429178479Sjb 430178479Sjbvoid DeclContext::DestroyDecls(ASTContext &C) { 431178479Sjb for (decl_iterator D = decls_begin(); D != decls_end(); ) 432178479Sjb (*D++)->Destroy(C); 433178479Sjb} 434178479Sjb 435178479Sjb/// \brief Find the parent context of this context that will be 436178479Sjb/// used for unqualified name lookup. 437178479Sjb/// 438178479Sjb/// Generally, the parent lookup context is the semantic context. However, for 439178479Sjb/// a friend function the parent lookup context is the lexical context, which 440178479Sjb/// is the class in which the friend is declared. 441178479SjbDeclContext *DeclContext::getLookupParent() { 442178479Sjb // FIXME: Find a better way to identify friends 443178479Sjb if (isa<FunctionDecl>(this)) 444178479Sjb if (getParent()->getLookupContext()->isFileContext() && 445178479Sjb getLexicalParent()->getLookupContext()->isRecord()) 446178479Sjb return getLexicalParent(); 447178479Sjb 448178479Sjb return getParent(); 449178479Sjb} 450178479Sjb 451178479Sjbbool DeclContext::isDependentContext() const { 452178479Sjb if (isFileContext()) 453178479Sjb return false; 454178479Sjb 455178479Sjb if (isa<ClassTemplatePartialSpecializationDecl>(this)) 456178479Sjb return true; 457178479Sjb 458178479Sjb if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) 459178479Sjb if (Record->getDescribedClassTemplate()) 460178479Sjb return true; 461178479Sjb 462178479Sjb if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) 463178479Sjb if (Function->getDescribedFunctionTemplate()) 464178479Sjb return true; 465178479Sjb 466178479Sjb return getParent() && getParent()->isDependentContext(); 467178479Sjb} 468178479Sjb 469178479Sjbbool DeclContext::isTransparentContext() const { 470178479Sjb if (DeclKind == Decl::Enum) 471178479Sjb return true; // FIXME: Check for C++0x scoped enums 472178479Sjb else if (DeclKind == Decl::LinkageSpec) 473178479Sjb return true; 474178479Sjb else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast) 475178479Sjb return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); 476178479Sjb else if (DeclKind == Decl::Namespace) 477178479Sjb return false; // FIXME: Check for C++0x inline namespaces 478178479Sjb 479178479Sjb return false; 480178479Sjb} 481178479Sjb 482178479Sjbbool DeclContext::Encloses(DeclContext *DC) { 483178479Sjb if (getPrimaryContext() != this) 484178479Sjb return getPrimaryContext()->Encloses(DC); 485178479Sjb 486178479Sjb for (; DC; DC = DC->getParent()) 487178479Sjb if (DC->getPrimaryContext() == this) 488178479Sjb return true; 489178479Sjb return false; 490178479Sjb} 491178479Sjb 492178479SjbDeclContext *DeclContext::getPrimaryContext() { 493178479Sjb switch (DeclKind) { 494178479Sjb case Decl::TranslationUnit: 495178479Sjb case Decl::LinkageSpec: 496178479Sjb case Decl::Block: 497178479Sjb // There is only one DeclContext for these entities. 498178479Sjb return this; 499178479Sjb 500178479Sjb case Decl::Namespace: 501178479Sjb // The original namespace is our primary context. 502178479Sjb return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); 503178479Sjb 504178479Sjb case Decl::ObjCMethod: 505178479Sjb return this; 506178479Sjb 507178479Sjb case Decl::ObjCInterface: 508178479Sjb case Decl::ObjCProtocol: 509178479Sjb case Decl::ObjCCategory: 510178479Sjb // FIXME: Can Objective-C interfaces be forward-declared? 511178479Sjb return this; 512178479Sjb 513178479Sjb case Decl::ObjCImplementation: 514178479Sjb case Decl::ObjCCategoryImpl: 515178479Sjb return this; 516178479Sjb 517178479Sjb default: 518178479Sjb if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) { 519178479Sjb // If this is a tag type that has a definition or is currently 520178479Sjb // being defined, that definition is our primary context. 521178479Sjb if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>()) 522178479Sjb if (TagT->isBeingDefined() || 523178479Sjb (TagT->getDecl() && TagT->getDecl()->isDefinition())) 524178550Sjb return TagT->getDecl(); 525178479Sjb return this; 526178550Sjb } 527178550Sjb 528178550Sjb assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && 529178479Sjb "Unknown DeclContext kind"); 530178479Sjb return this; 531178479Sjb } 532178479Sjb} 533178479Sjb 534178479SjbDeclContext *DeclContext::getNextContext() { 535178479Sjb switch (DeclKind) { 536178479Sjb case Decl::Namespace: 537178479Sjb // Return the next namespace 538178479Sjb return static_cast<NamespaceDecl*>(this)->getNextNamespace(); 539178479Sjb 540178479Sjb default: 541178479Sjb return 0; 542178479Sjb } 543178479Sjb} 544178479Sjb 545178479Sjb/// \brief Load the declarations within this lexical storage from an 546178479Sjb/// external source. 547178479Sjbvoid 548178479SjbDeclContext::LoadLexicalDeclsFromExternalStorage() const { 549178479Sjb ExternalASTSource *Source = getParentASTContext().getExternalSource(); 550178479Sjb assert(hasExternalLexicalStorage() && Source && "No external storage?"); 551178479Sjb 552178479Sjb llvm::SmallVector<uint32_t, 64> Decls; 553178479Sjb if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this), 554178479Sjb Decls)) 555178479Sjb return; 556178479Sjb 557178479Sjb // There is no longer any lexical storage in this context 558178479Sjb ExternalLexicalStorage = false; 559178479Sjb 560178479Sjb if (Decls.empty()) 561178479Sjb return; 562178479Sjb 563178479Sjb // Resolve all of the declaration IDs into declarations, building up 564178479Sjb // a chain of declarations via the Decl::NextDeclInContext field. 565178479Sjb Decl *FirstNewDecl = 0; 566178479Sjb Decl *PrevDecl = 0; 567178479Sjb for (unsigned I = 0, N = Decls.size(); I != N; ++I) { 568178479Sjb Decl *D = Source->GetDecl(Decls[I]); 569178479Sjb if (PrevDecl) 570178479Sjb PrevDecl->NextDeclInContext = D; 571178479Sjb else 572178479Sjb FirstNewDecl = D; 573178479Sjb 574178479Sjb PrevDecl = D; 575178550Sjb } 576178550Sjb 577178550Sjb // Splice the newly-read declarations into the beginning of the list 578178550Sjb // of declarations. 579178550Sjb PrevDecl->NextDeclInContext = FirstDecl; 580178479Sjb FirstDecl = FirstNewDecl; 581 if (!LastDecl) 582 LastDecl = PrevDecl; 583} 584 585void 586DeclContext::LoadVisibleDeclsFromExternalStorage() const { 587 DeclContext *This = const_cast<DeclContext *>(this); 588 ExternalASTSource *Source = getParentASTContext().getExternalSource(); 589 assert(hasExternalVisibleStorage() && Source && "No external storage?"); 590 591 llvm::SmallVector<VisibleDeclaration, 64> Decls; 592 if (Source->ReadDeclsVisibleInContext(This, Decls)) 593 return; 594 595 // There is no longer any visible storage in this context 596 ExternalVisibleStorage = false; 597 598 // Load the declaration IDs for all of the names visible in this 599 // context. 600 assert(!LookupPtr && "Have a lookup map before de-serialization?"); 601 StoredDeclsMap *Map = new StoredDeclsMap; 602 LookupPtr = Map; 603 for (unsigned I = 0, N = Decls.size(); I != N; ++I) { 604 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations); 605 } 606} 607 608DeclContext::decl_iterator DeclContext::decls_begin() const { 609 if (hasExternalLexicalStorage()) 610 LoadLexicalDeclsFromExternalStorage(); 611 612 // FIXME: Check whether we need to load some declarations from 613 // external storage. 614 return decl_iterator(FirstDecl); 615} 616 617DeclContext::decl_iterator DeclContext::decls_end() const { 618 if (hasExternalLexicalStorage()) 619 LoadLexicalDeclsFromExternalStorage(); 620 621 return decl_iterator(); 622} 623 624bool DeclContext::decls_empty() const { 625 if (hasExternalLexicalStorage()) 626 LoadLexicalDeclsFromExternalStorage(); 627 628 return !FirstDecl; 629} 630 631void DeclContext::addHiddenDecl(Decl *D) { 632 assert(D->getLexicalDeclContext() == this && 633 "Decl inserted into wrong lexical context"); 634 assert(!D->getNextDeclInContext() && D != LastDecl && 635 "Decl already inserted into a DeclContext"); 636 637 if (FirstDecl) { 638 LastDecl->NextDeclInContext = D; 639 LastDecl = D; 640 } else { 641 FirstDecl = LastDecl = D; 642 } 643} 644 645void DeclContext::addDecl(Decl *D) { 646 addHiddenDecl(D); 647 648 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) 649 ND->getDeclContext()->makeDeclVisibleInContext(ND); 650} 651 652/// buildLookup - Build the lookup data structure with all of the 653/// declarations in DCtx (and any other contexts linked to it or 654/// transparent contexts nested within it). 655void DeclContext::buildLookup(DeclContext *DCtx) { 656 for (; DCtx; DCtx = DCtx->getNextContext()) { 657 for (decl_iterator D = DCtx->decls_begin(), 658 DEnd = DCtx->decls_end(); 659 D != DEnd; ++D) { 660 // Insert this declaration into the lookup structure, but only 661 // if it's semantically in its decl context. During non-lazy 662 // lookup building, this is implicitly enforced by addDecl. 663 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) 664 if (D->getDeclContext() == DCtx) 665 makeDeclVisibleInContextImpl(ND); 666 667 // If this declaration is itself a transparent declaration context, 668 // add its members (recursively). 669 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) 670 if (InnerCtx->isTransparentContext()) 671 buildLookup(InnerCtx->getPrimaryContext()); 672 } 673 } 674} 675 676DeclContext::lookup_result 677DeclContext::lookup(DeclarationName Name) { 678 DeclContext *PrimaryContext = getPrimaryContext(); 679 if (PrimaryContext != this) 680 return PrimaryContext->lookup(Name); 681 682 if (hasExternalVisibleStorage()) 683 LoadVisibleDeclsFromExternalStorage(); 684 685 /// If there is no lookup data structure, build one now by walking 686 /// all of the linked DeclContexts (in declaration order!) and 687 /// inserting their values. 688 if (!LookupPtr) { 689 buildLookup(this); 690 691 if (!LookupPtr) 692 return lookup_result(0, 0); 693 } 694 695 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr); 696 StoredDeclsMap::iterator Pos = Map->find(Name); 697 if (Pos == Map->end()) 698 return lookup_result(0, 0); 699 return Pos->second.getLookupResult(getParentASTContext()); 700} 701 702DeclContext::lookup_const_result 703DeclContext::lookup(DeclarationName Name) const { 704 return const_cast<DeclContext*>(this)->lookup(Name); 705} 706 707DeclContext *DeclContext::getLookupContext() { 708 DeclContext *Ctx = this; 709 // Skip through transparent contexts. 710 while (Ctx->isTransparentContext()) 711 Ctx = Ctx->getParent(); 712 return Ctx; 713} 714 715DeclContext *DeclContext::getEnclosingNamespaceContext() { 716 DeclContext *Ctx = this; 717 // Skip through non-namespace, non-translation-unit contexts. 718 while (!Ctx->isFileContext() || Ctx->isTransparentContext()) 719 Ctx = Ctx->getParent(); 720 return Ctx->getPrimaryContext(); 721} 722 723void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) { 724 // FIXME: This feels like a hack. Should DeclarationName support 725 // template-ids, or is there a better way to keep specializations 726 // from being visible? 727 if (isa<ClassTemplateSpecializationDecl>(D)) 728 return; 729 730 DeclContext *PrimaryContext = getPrimaryContext(); 731 if (PrimaryContext != this) { 732 PrimaryContext->makeDeclVisibleInContext(D, Recoverable); 733 return; 734 } 735 736 // If we already have a lookup data structure, perform the insertion 737 // into it. Otherwise, be lazy and don't build that structure until 738 // someone asks for it. 739 if (LookupPtr || !Recoverable) 740 makeDeclVisibleInContextImpl(D); 741 742 // If we are a transparent context, insert into our parent context, 743 // too. This operation is recursive. 744 if (isTransparentContext()) 745 getParent()->makeDeclVisibleInContext(D, Recoverable); 746} 747 748void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { 749 // Skip unnamed declarations. 750 if (!D->getDeclName()) 751 return; 752 753 // FIXME: This feels like a hack. Should DeclarationName support 754 // template-ids, or is there a better way to keep specializations 755 // from being visible? 756 if (isa<ClassTemplateSpecializationDecl>(D)) 757 return; 758 759 if (!LookupPtr) 760 LookupPtr = new StoredDeclsMap; 761 762 // Insert this declaration into the map. 763 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr); 764 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()]; 765 if (DeclNameEntries.isNull()) { 766 DeclNameEntries.setOnlyValue(D); 767 return; 768 } 769 770 // If it is possible that this is a redeclaration, check to see if there is 771 // already a decl for which declarationReplaces returns true. If there is 772 // one, just replace it and return. 773 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D)) 774 return; 775 776 // Put this declaration into the appropriate slot. 777 DeclNameEntries.AddSubsequentDecl(D); 778} 779 780/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within 781/// this context. 782DeclContext::udir_iterator_range 783DeclContext::getUsingDirectives() const { 784 lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); 785 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), 786 reinterpret_cast<udir_iterator>(Result.second)); 787} 788 789void StoredDeclsList::materializeDecls(ASTContext &Context) { 790 if (isNull()) 791 return; 792 793 switch ((DataKind)(Data & 0x03)) { 794 case DK_Decl: 795 case DK_Decl_Vector: 796 break; 797 798 case DK_DeclID: { 799 // Resolve this declaration ID to an actual declaration by 800 // querying the external AST source. 801 unsigned DeclID = Data >> 2; 802 803 ExternalASTSource *Source = Context.getExternalSource(); 804 assert(Source && "No external AST source available!"); 805 806 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID)); 807 break; 808 } 809 810 case DK_ID_Vector: { 811 // We have a vector of declaration IDs. Resolve all of them to 812 // actual declarations. 813 VectorTy &Vector = *getAsVector(); 814 ExternalASTSource *Source = Context.getExternalSource(); 815 assert(Source && "No external AST source available!"); 816 817 for (unsigned I = 0, N = Vector.size(); I != N; ++I) 818 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I])); 819 820 Data = (Data & ~0x03) | DK_Decl_Vector; 821 break; 822 } 823 } 824} 825