1 //===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Decl and DeclContext classes.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/DeclBase.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclContextInternals.h"
21 #include "clang/AST/DeclFriend.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/DeclOpenMP.h"
24 #include "clang/AST/DeclTemplate.h"
25 #include "clang/AST/DependentDiagnostic.h"
26 #include "clang/AST/ExternalASTSource.h"
27 #include "clang/AST/Stmt.h"
28 #include "clang/AST/StmtCXX.h"
29 #include "clang/AST/Type.h"
30 #include "clang/Basic/TargetInfo.h"
31 #include "llvm/ADT/DenseMap.h"
32 #include "llvm/Support/raw_ostream.h"
34 using namespace clang;
36 //===----------------------------------------------------------------------===//
38 //===----------------------------------------------------------------------===//
40 #define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
41 #define ABSTRACT_DECL(DECL)
42 #include "clang/AST/DeclNodes.inc"
44 void Decl::updateOutOfDate(IdentifierInfo &II) const {
45 getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
48 void *Decl::AllocateDeserializedDecl(const ASTContext &Context,
51 // Allocate an extra 8 bytes worth of storage, which ensures that the
52 // resulting pointer will still be 8-byte aligned.
53 void *Start = Context.Allocate(Size + 8);
54 void *Result = (char*)Start + 8;
56 unsigned *PrefixPtr = (unsigned *)Result - 2;
58 // Zero out the first 4 bytes; this is used to store the owning module ID.
61 // Store the global declaration ID in the second 4 bytes.
67 Module *Decl::getOwningModuleSlow() const {
68 assert(isFromASTFile() && "Not from AST file?");
69 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
72 const char *Decl::getDeclKindName() const {
74 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
75 #define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
76 #define ABSTRACT_DECL(DECL)
77 #include "clang/AST/DeclNodes.inc"
81 void Decl::setInvalidDecl(bool Invalid) {
82 InvalidDecl = Invalid;
83 if (Invalid && !isa<ParmVarDecl>(this)) {
84 // Defensive maneuver for ill-formed code: we're likely not to make it to
85 // a point where we set the access specifier, so default it to "public"
86 // to avoid triggering asserts elsewhere in the front end.
91 const char *DeclContext::getDeclKindName() const {
93 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
94 #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
95 #define ABSTRACT_DECL(DECL)
96 #include "clang/AST/DeclNodes.inc"
100 bool Decl::StatisticsEnabled = false;
101 void Decl::EnableStatistics() {
102 StatisticsEnabled = true;
105 void Decl::PrintStats() {
106 llvm::errs() << "\n*** Decl Stats:\n";
109 #define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
110 #define ABSTRACT_DECL(DECL)
111 #include "clang/AST/DeclNodes.inc"
112 llvm::errs() << " " << totalDecls << " decls total.\n";
115 #define DECL(DERIVED, BASE) \
116 if (n##DERIVED##s > 0) { \
117 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
118 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
119 << sizeof(DERIVED##Decl) << " each (" \
120 << n##DERIVED##s * sizeof(DERIVED##Decl) \
123 #define ABSTRACT_DECL(DECL)
124 #include "clang/AST/DeclNodes.inc"
126 llvm::errs() << "Total bytes = " << totalBytes << "\n";
129 void Decl::add(Kind k) {
131 #define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
132 #define ABSTRACT_DECL(DECL)
133 #include "clang/AST/DeclNodes.inc"
137 bool Decl::isTemplateParameterPack() const {
138 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
139 return TTP->isParameterPack();
140 if (const NonTypeTemplateParmDecl *NTTP
141 = dyn_cast<NonTypeTemplateParmDecl>(this))
142 return NTTP->isParameterPack();
143 if (const TemplateTemplateParmDecl *TTP
144 = dyn_cast<TemplateTemplateParmDecl>(this))
145 return TTP->isParameterPack();
149 bool Decl::isParameterPack() const {
150 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
151 return Parm->isParameterPack();
153 return isTemplateParameterPack();
156 bool Decl::isFunctionOrFunctionTemplate() const {
157 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
158 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
160 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
163 bool Decl::isTemplateDecl() const {
164 return isa<TemplateDecl>(this);
167 const DeclContext *Decl::getParentFunctionOrMethod() const {
168 for (const DeclContext *DC = getDeclContext();
169 DC && !DC->isTranslationUnit() && !DC->isNamespace();
170 DC = DC->getParent())
171 if (DC->isFunctionOrMethod())
178 //===----------------------------------------------------------------------===//
179 // PrettyStackTraceDecl Implementation
180 //===----------------------------------------------------------------------===//
182 void PrettyStackTraceDecl::print(raw_ostream &OS) const {
183 SourceLocation TheLoc = Loc;
184 if (TheLoc.isInvalid() && TheDecl)
185 TheLoc = TheDecl->getLocation();
187 if (TheLoc.isValid()) {
188 TheLoc.print(OS, SM);
194 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
196 DN->printQualifiedName(OS);
202 //===----------------------------------------------------------------------===//
203 // Decl Implementation
204 //===----------------------------------------------------------------------===//
206 // Out-of-line virtual method providing a home for Decl.
209 void Decl::setDeclContext(DeclContext *DC) {
213 void Decl::setLexicalDeclContext(DeclContext *DC) {
214 if (DC == getLexicalDeclContext())
218 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
220 getMultipleDC()->LexicalDC = DC;
224 void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
226 if (SemaDC == LexicalDC) {
229 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
230 MDC->SemanticDC = SemaDC;
231 MDC->LexicalDC = LexicalDC;
236 bool Decl::isInAnonymousNamespace() const {
237 const DeclContext *DC = getDeclContext();
239 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
240 if (ND->isAnonymousNamespace())
242 } while ((DC = DC->getParent()));
247 TranslationUnitDecl *Decl::getTranslationUnitDecl() {
248 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
251 DeclContext *DC = getDeclContext();
252 assert(DC && "This decl is not contained in a translation unit!");
254 while (!DC->isTranslationUnit()) {
255 DC = DC->getParent();
256 assert(DC && "This decl is not contained in a translation unit!");
259 return cast<TranslationUnitDecl>(DC);
262 ASTContext &Decl::getASTContext() const {
263 return getTranslationUnitDecl()->getASTContext();
266 ASTMutationListener *Decl::getASTMutationListener() const {
267 return getASTContext().getASTMutationListener();
270 unsigned Decl::getMaxAlignment() const {
275 const AttrVec &V = getAttrs();
276 ASTContext &Ctx = getASTContext();
277 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
279 Align = std::max(Align, I->getAlignment(Ctx));
283 bool Decl::isUsed(bool CheckUsedAttr) const {
287 // Check for used attribute.
288 if (CheckUsedAttr && hasAttr<UsedAttr>())
294 void Decl::markUsed(ASTContext &C) {
298 if (C.getASTMutationListener())
299 C.getASTMutationListener()->DeclarationMarkedUsed(this);
304 bool Decl::isReferenced() const {
308 // Check redeclarations.
309 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
316 /// \brief Determine the availability of the given declaration based on
317 /// the target platform.
319 /// When it returns an availability result other than \c AR_Available,
320 /// if the \p Message parameter is non-NULL, it will be set to a
321 /// string describing why the entity is unavailable.
323 /// FIXME: Make these strings localizable, since they end up in
325 static AvailabilityResult CheckAvailability(ASTContext &Context,
326 const AvailabilityAttr *A,
327 std::string *Message) {
328 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
329 StringRef PrettyPlatformName
330 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
331 if (PrettyPlatformName.empty())
332 PrettyPlatformName = TargetPlatform;
334 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
335 if (TargetMinVersion.empty())
338 // Match the platform name.
339 if (A->getPlatform()->getName() != TargetPlatform)
342 std::string HintMessage;
343 if (!A->getMessage().empty()) {
345 HintMessage += A->getMessage();
348 // Make sure that this declaration has not been marked 'unavailable'.
349 if (A->getUnavailable()) {
352 llvm::raw_string_ostream Out(*Message);
353 Out << "not available on " << PrettyPlatformName
357 return AR_Unavailable;
360 // Make sure that this declaration has already been introduced.
361 if (!A->getIntroduced().empty() &&
362 TargetMinVersion < A->getIntroduced()) {
365 llvm::raw_string_ostream Out(*Message);
366 Out << "introduced in " << PrettyPlatformName << ' '
367 << A->getIntroduced() << HintMessage;
370 return AR_NotYetIntroduced;
373 // Make sure that this declaration hasn't been obsoleted.
374 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
377 llvm::raw_string_ostream Out(*Message);
378 Out << "obsoleted in " << PrettyPlatformName << ' '
379 << A->getObsoleted() << HintMessage;
382 return AR_Unavailable;
385 // Make sure that this declaration hasn't been deprecated.
386 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
389 llvm::raw_string_ostream Out(*Message);
390 Out << "first deprecated in " << PrettyPlatformName << ' '
391 << A->getDeprecated() << HintMessage;
394 return AR_Deprecated;
400 AvailabilityResult Decl::getAvailability(std::string *Message) const {
401 AvailabilityResult Result = AR_Available;
402 std::string ResultMessage;
404 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
405 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
406 if (Result >= AR_Deprecated)
410 ResultMessage = Deprecated->getMessage();
412 Result = AR_Deprecated;
416 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
418 *Message = Unavailable->getMessage();
419 return AR_Unavailable;
422 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
423 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
426 if (AR == AR_Unavailable)
427 return AR_Unavailable;
432 ResultMessage.swap(*Message);
439 Message->swap(ResultMessage);
443 bool Decl::canBeWeakImported(bool &IsDefinition) const {
444 IsDefinition = false;
446 // Variables, if they aren't definitions.
447 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
448 if (Var->isThisDeclarationADefinition()) {
454 // Functions, if they aren't definitions.
455 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
462 // Objective-C classes, if this is the non-fragile runtime.
463 } else if (isa<ObjCInterfaceDecl>(this) &&
464 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
473 bool Decl::isWeakImported() const {
475 if (!canBeWeakImported(IsDefinition))
478 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
479 if (isa<WeakImportAttr>(*A))
482 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
483 if (CheckAvailability(getASTContext(), Availability, 0)
484 == AR_NotYetIntroduced)
492 unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
503 case NonTypeTemplateParm:
507 return IDNS_Ordinary;
511 return IDNS_Ordinary | IDNS_Member;
513 case ObjCCompatibleAlias:
515 return IDNS_Ordinary | IDNS_Type;
519 case TypeAliasTemplate:
520 case UnresolvedUsingTypename:
521 case TemplateTypeParm:
522 return IDNS_Ordinary | IDNS_Type;
525 return 0; // we'll actually overwrite this later
527 case UnresolvedUsingValue:
528 return IDNS_Ordinary | IDNS_Using;
534 return IDNS_ObjCProtocol;
537 case ObjCAtDefsField:
544 return IDNS_Tag | IDNS_Type;
548 return IDNS_Namespace;
550 case FunctionTemplate:
552 return IDNS_Ordinary;
555 case TemplateTemplateParm:
556 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
565 case ObjCPropertyImpl:
568 case TranslationUnit:
571 case ClassTemplateSpecialization:
572 case ClassTemplatePartialSpecialization:
573 case ClassScopeFunctionSpecialization:
574 case VarTemplateSpecialization:
575 case VarTemplatePartialSpecialization:
576 case ObjCImplementation:
578 case ObjCCategoryImpl:
580 case OMPThreadPrivate:
582 // Never looked up by name.
586 llvm_unreachable("Invalid DeclKind!");
589 void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
590 assert(!HasAttrs && "Decl already contains attrs.");
592 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
593 assert(AttrBlank.empty() && "HasAttrs was wrong?");
599 void Decl::dropAttrs() {
600 if (!HasAttrs) return;
603 getASTContext().eraseDeclAttrs(this);
606 const AttrVec &Decl::getAttrs() const {
607 assert(HasAttrs && "No attrs to get!");
608 return getASTContext().getDeclAttrs(this);
611 Decl *Decl::castFromDeclContext (const DeclContext *D) {
612 Decl::Kind DK = D->getDeclKind();
614 #define DECL(NAME, BASE)
615 #define DECL_CONTEXT(NAME) \
617 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
618 #define DECL_CONTEXT_BASE(NAME)
619 #include "clang/AST/DeclNodes.inc"
621 #define DECL(NAME, BASE)
622 #define DECL_CONTEXT_BASE(NAME) \
623 if (DK >= first##NAME && DK <= last##NAME) \
624 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
625 #include "clang/AST/DeclNodes.inc"
626 llvm_unreachable("a decl that inherits DeclContext isn't handled");
630 DeclContext *Decl::castToDeclContext(const Decl *D) {
631 Decl::Kind DK = D->getKind();
633 #define DECL(NAME, BASE)
634 #define DECL_CONTEXT(NAME) \
636 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
637 #define DECL_CONTEXT_BASE(NAME)
638 #include "clang/AST/DeclNodes.inc"
640 #define DECL(NAME, BASE)
641 #define DECL_CONTEXT_BASE(NAME) \
642 if (DK >= first##NAME && DK <= last##NAME) \
643 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
644 #include "clang/AST/DeclNodes.inc"
645 llvm_unreachable("a decl that inherits DeclContext isn't handled");
649 SourceLocation Decl::getBodyRBrace() const {
650 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
651 // FunctionDecl stores EndRangeLoc for this purpose.
652 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
653 const FunctionDecl *Definition;
654 if (FD->hasBody(Definition))
655 return Definition->getSourceRange().getEnd();
656 return SourceLocation();
659 if (Stmt *Body = getBody())
660 return Body->getSourceRange().getEnd();
662 return SourceLocation();
665 void Decl::CheckAccessDeclContext() const {
667 // Suppress this check if any of the following hold:
668 // 1. this is the translation unit (and thus has no parent)
669 // 2. this is a template parameter (and thus doesn't belong to its context)
670 // 3. this is a non-type template parameter
671 // 4. the context is not a record
673 // 6. it's a C++0x static_assert.
674 if (isa<TranslationUnitDecl>(this) ||
675 isa<TemplateTypeParmDecl>(this) ||
676 isa<NonTypeTemplateParmDecl>(this) ||
677 !isa<CXXRecordDecl>(getDeclContext()) ||
679 isa<StaticAssertDecl>(this) ||
680 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
681 // as DeclContext (?).
682 isa<ParmVarDecl>(this) ||
683 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
684 // AS_none as access specifier.
685 isa<CXXRecordDecl>(this) ||
686 isa<ClassScopeFunctionSpecializationDecl>(this))
689 assert(Access != AS_none &&
690 "Access specifier is AS_none inside a record decl");
694 static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
695 static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
697 /// Starting at a given context (a Decl or DeclContext), look for a
698 /// code context that is not a closure (a lambda, block, etc.).
699 template <class T> static Decl *getNonClosureContext(T *D) {
700 if (getKind(D) == Decl::CXXMethod) {
701 CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
702 if (MD->getOverloadedOperator() == OO_Call &&
703 MD->getParent()->isLambda())
704 return getNonClosureContext(MD->getParent()->getParent());
706 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
708 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
710 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
711 return getNonClosureContext(BD->getParent());
712 } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
713 return getNonClosureContext(CD->getParent());
719 Decl *Decl::getNonClosureContext() {
720 return ::getNonClosureContext(this);
723 Decl *DeclContext::getNonClosureAncestor() {
724 return ::getNonClosureContext(this);
727 //===----------------------------------------------------------------------===//
728 // DeclContext Implementation
729 //===----------------------------------------------------------------------===//
731 bool DeclContext::classof(const Decl *D) {
732 switch (D->getKind()) {
733 #define DECL(NAME, BASE)
734 #define DECL_CONTEXT(NAME) case Decl::NAME:
735 #define DECL_CONTEXT_BASE(NAME)
736 #include "clang/AST/DeclNodes.inc"
739 #define DECL(NAME, BASE)
740 #define DECL_CONTEXT_BASE(NAME) \
741 if (D->getKind() >= Decl::first##NAME && \
742 D->getKind() <= Decl::last##NAME) \
744 #include "clang/AST/DeclNodes.inc"
749 DeclContext::~DeclContext() { }
751 /// \brief Find the parent context of this context that will be
752 /// used for unqualified name lookup.
754 /// Generally, the parent lookup context is the semantic context. However, for
755 /// a friend function the parent lookup context is the lexical context, which
756 /// is the class in which the friend is declared.
757 DeclContext *DeclContext::getLookupParent() {
758 // FIXME: Find a better way to identify friends
759 if (isa<FunctionDecl>(this))
760 if (getParent()->getRedeclContext()->isFileContext() &&
761 getLexicalParent()->getRedeclContext()->isRecord())
762 return getLexicalParent();
767 bool DeclContext::isInlineNamespace() const {
768 return isNamespace() &&
769 cast<NamespaceDecl>(this)->isInline();
772 bool DeclContext::isDependentContext() const {
776 if (isa<ClassTemplatePartialSpecializationDecl>(this))
779 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
780 if (Record->getDescribedClassTemplate())
783 if (Record->isDependentLambda())
787 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
788 if (Function->getDescribedFunctionTemplate())
791 // Friend function declarations are dependent if their *lexical*
792 // context is dependent.
793 if (cast<Decl>(this)->getFriendObjectKind())
794 return getLexicalParent()->isDependentContext();
797 return getParent() && getParent()->isDependentContext();
800 bool DeclContext::isTransparentContext() const {
801 if (DeclKind == Decl::Enum)
802 return !cast<EnumDecl>(this)->isScoped();
803 else if (DeclKind == Decl::LinkageSpec)
809 static bool isLinkageSpecContext(const DeclContext *DC,
810 LinkageSpecDecl::LanguageIDs ID) {
811 while (DC->getDeclKind() != Decl::TranslationUnit) {
812 if (DC->getDeclKind() == Decl::LinkageSpec)
813 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
814 DC = DC->getParent();
819 bool DeclContext::isExternCContext() const {
820 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_c);
823 bool DeclContext::isExternCXXContext() const {
824 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_cxx);
827 bool DeclContext::Encloses(const DeclContext *DC) const {
828 if (getPrimaryContext() != this)
829 return getPrimaryContext()->Encloses(DC);
831 for (; DC; DC = DC->getParent())
832 if (DC->getPrimaryContext() == this)
837 DeclContext *DeclContext::getPrimaryContext() {
839 case Decl::TranslationUnit:
840 case Decl::LinkageSpec:
843 // There is only one DeclContext for these entities.
846 case Decl::Namespace:
847 // The original namespace is our primary context.
848 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
850 case Decl::ObjCMethod:
853 case Decl::ObjCInterface:
854 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
859 case Decl::ObjCProtocol:
860 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
865 case Decl::ObjCCategory:
868 case Decl::ObjCImplementation:
869 case Decl::ObjCCategoryImpl:
873 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
874 // If this is a tag type that has a definition or is currently
875 // being defined, that definition is our primary context.
876 TagDecl *Tag = cast<TagDecl>(this);
877 assert(isa<TagType>(Tag->TypeForDecl) ||
878 isa<InjectedClassNameType>(Tag->TypeForDecl));
880 if (TagDecl *Def = Tag->getDefinition())
883 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
884 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
885 if (TagTy->isBeingDefined())
886 // FIXME: is it necessarily being defined in the decl
887 // that owns the type?
888 return TagTy->getDecl();
894 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
895 "Unknown DeclContext kind");
901 DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
904 if (DeclKind != Decl::Namespace) {
905 Contexts.push_back(this);
909 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
910 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
911 N = N->getPreviousDecl())
912 Contexts.push_back(N);
914 std::reverse(Contexts.begin(), Contexts.end());
917 std::pair<Decl *, Decl *>
918 DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
919 bool FieldsAlreadyLoaded) {
920 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
921 Decl *FirstNewDecl = 0;
923 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
924 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
929 PrevDecl->NextInContextAndBits.setPointer(D);
936 return std::make_pair(FirstNewDecl, PrevDecl);
939 /// \brief We have just acquired external visible storage, and we already have
940 /// built a lookup map. For every name in the map, pull in the new names from
941 /// the external storage.
942 void DeclContext::reconcileExternalVisibleStorage() {
943 assert(NeedToReconcileExternalVisibleStorage && LookupPtr.getPointer());
944 NeedToReconcileExternalVisibleStorage = false;
946 StoredDeclsMap &Map = *LookupPtr.getPointer();
947 for (StoredDeclsMap::iterator I = Map.begin(); I != Map.end(); ++I)
948 I->second.setHasExternalDecls();
951 /// \brief Load the declarations within this lexical storage from an
954 DeclContext::LoadLexicalDeclsFromExternalStorage() const {
955 ExternalASTSource *Source = getParentASTContext().getExternalSource();
956 assert(hasExternalLexicalStorage() && Source && "No external storage?");
958 // Notify that we have a DeclContext that is initializing.
959 ExternalASTSource::Deserializing ADeclContext(Source);
961 // Load the external declarations, if any.
962 SmallVector<Decl*, 64> Decls;
963 ExternalLexicalStorage = false;
964 switch (Source->FindExternalLexicalDecls(this, Decls)) {
969 case ELR_AlreadyLoaded:
976 // We may have already loaded just the fields of this record, in which case
977 // we need to ignore them.
978 bool FieldsAlreadyLoaded = false;
979 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
980 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
982 // Splice the newly-read declarations into the beginning of the list
984 Decl *ExternalFirst, *ExternalLast;
985 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
986 FieldsAlreadyLoaded);
987 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
988 FirstDecl = ExternalFirst;
990 LastDecl = ExternalLast;
993 DeclContext::lookup_result
994 ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
995 DeclarationName Name) {
996 ASTContext &Context = DC->getParentASTContext();
998 if (!(Map = DC->LookupPtr.getPointer()))
999 Map = DC->CreateStoredDeclsMap(Context);
1001 (*Map)[Name].removeExternalDecls();
1003 return DeclContext::lookup_result();
1006 DeclContext::lookup_result
1007 ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
1008 DeclarationName Name,
1009 ArrayRef<NamedDecl*> Decls) {
1010 ASTContext &Context = DC->getParentASTContext();
1011 StoredDeclsMap *Map;
1012 if (!(Map = DC->LookupPtr.getPointer()))
1013 Map = DC->CreateStoredDeclsMap(Context);
1015 StoredDeclsList &List = (*Map)[Name];
1017 // Clear out any old external visible declarations, to avoid quadratic
1018 // performance in the redeclaration checks below.
1019 List.removeExternalDecls();
1021 if (!List.isNull()) {
1022 // We have both existing declarations and new declarations for this name.
1023 // Some of the declarations may simply replace existing ones. Handle those
1025 llvm::SmallVector<unsigned, 8> Skip;
1026 for (unsigned I = 0, N = Decls.size(); I != N; ++I)
1027 if (List.HandleRedeclaration(Decls[I]))
1029 Skip.push_back(Decls.size());
1031 // Add in any new declarations.
1032 unsigned SkipPos = 0;
1033 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1034 if (I == Skip[SkipPos])
1037 List.AddSubsequentDecl(Decls[I]);
1040 // Convert the array to a StoredDeclsList.
1041 for (ArrayRef<NamedDecl*>::iterator
1042 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1044 List.setOnlyValue(*I);
1046 List.AddSubsequentDecl(*I);
1050 return List.getLookupResult();
1053 DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
1054 return decl_iterator(FirstDecl);
1057 DeclContext::decl_iterator DeclContext::decls_begin() const {
1058 if (hasExternalLexicalStorage())
1059 LoadLexicalDeclsFromExternalStorage();
1061 return decl_iterator(FirstDecl);
1064 bool DeclContext::decls_empty() const {
1065 if (hasExternalLexicalStorage())
1066 LoadLexicalDeclsFromExternalStorage();
1071 bool DeclContext::containsDecl(Decl *D) const {
1072 return (D->getLexicalDeclContext() == this &&
1073 (D->NextInContextAndBits.getPointer() || D == LastDecl));
1076 void DeclContext::removeDecl(Decl *D) {
1077 assert(D->getLexicalDeclContext() == this &&
1078 "decl being removed from non-lexical context");
1079 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
1080 "decl is not in decls list");
1082 // Remove D from the decl chain. This is O(n) but hopefully rare.
1083 if (D == FirstDecl) {
1085 FirstDecl = LastDecl = 0;
1087 FirstDecl = D->NextInContextAndBits.getPointer();
1089 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
1090 assert(I && "decl not found in linked list");
1091 if (I->NextInContextAndBits.getPointer() == D) {
1092 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
1093 if (D == LastDecl) LastDecl = I;
1099 // Mark that D is no longer in the decl chain.
1100 D->NextInContextAndBits.setPointer(0);
1102 // Remove D from the lookup table if necessary.
1103 if (isa<NamedDecl>(D)) {
1104 NamedDecl *ND = cast<NamedDecl>(D);
1106 // Remove only decls that have a name
1107 if (!ND->getDeclName()) return;
1109 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
1112 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1113 assert(Pos != Map->end() && "no lookup entry for decl");
1114 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1115 Pos->second.remove(ND);
1119 void DeclContext::addHiddenDecl(Decl *D) {
1120 assert(D->getLexicalDeclContext() == this &&
1121 "Decl inserted into wrong lexical context");
1122 assert(!D->getNextDeclInContext() && D != LastDecl &&
1123 "Decl already inserted into a DeclContext");
1126 LastDecl->NextInContextAndBits.setPointer(D);
1129 FirstDecl = LastDecl = D;
1132 // Notify a C++ record declaration that we've added a member, so it can
1133 // update it's class-specific state.
1134 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1135 Record->addedMember(D);
1137 // If this is a newly-created (not de-serialized) import declaration, wire
1138 // it in to the list of local import declarations.
1139 if (!D->isFromASTFile()) {
1140 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1141 D->getASTContext().addedLocalImportDecl(Import);
1145 void DeclContext::addDecl(Decl *D) {
1148 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1149 ND->getDeclContext()->getPrimaryContext()->
1150 makeDeclVisibleInContextWithFlags(ND, false, true);
1153 void DeclContext::addDeclInternal(Decl *D) {
1156 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1157 ND->getDeclContext()->getPrimaryContext()->
1158 makeDeclVisibleInContextWithFlags(ND, true, true);
1161 /// shouldBeHidden - Determine whether a declaration which was declared
1162 /// within its semantic context should be invisible to qualified name lookup.
1163 static bool shouldBeHidden(NamedDecl *D) {
1164 // Skip unnamed declarations.
1165 if (!D->getDeclName())
1168 // Skip entities that can't be found by name lookup into a particular
1170 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1171 D->isTemplateParameter())
1174 // Skip template specializations.
1175 // FIXME: This feels like a hack. Should DeclarationName support
1176 // template-ids, or is there a better way to keep specializations
1177 // from being visible?
1178 if (isa<ClassTemplateSpecializationDecl>(D))
1180 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1181 if (FD->isFunctionTemplateSpecialization())
1187 /// buildLookup - Build the lookup data structure with all of the
1188 /// declarations in this DeclContext (and any other contexts linked
1189 /// to it or transparent contexts nested within it) and return it.
1190 StoredDeclsMap *DeclContext::buildLookup() {
1191 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1193 // FIXME: Should we keep going if hasExternalVisibleStorage?
1194 if (!LookupPtr.getInt())
1195 return LookupPtr.getPointer();
1197 SmallVector<DeclContext *, 2> Contexts;
1198 collectAllContexts(Contexts);
1199 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1200 buildLookupImpl<&DeclContext::decls_begin,
1201 &DeclContext::decls_end>(Contexts[I]);
1203 // We no longer have any lazy decls.
1204 LookupPtr.setInt(false);
1205 NeedToReconcileExternalVisibleStorage = false;
1206 return LookupPtr.getPointer();
1209 /// buildLookupImpl - Build part of the lookup data structure for the
1210 /// declarations contained within DCtx, which will either be this
1211 /// DeclContext, a DeclContext linked to it, or a transparent context
1212 /// nested within it.
1213 template<DeclContext::decl_iterator (DeclContext::*Begin)() const,
1214 DeclContext::decl_iterator (DeclContext::*End)() const>
1215 void DeclContext::buildLookupImpl(DeclContext *DCtx) {
1216 for (decl_iterator I = (DCtx->*Begin)(), E = (DCtx->*End)();
1220 // Insert this declaration into the lookup structure, but only if
1221 // it's semantically within its decl context. Any other decls which
1222 // should be found in this context are added eagerly.
1224 // If it's from an AST file, don't add it now. It'll get handled by
1225 // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1226 // in C++, we do not track external visible decls for the TU, so in
1227 // that case we need to collect them all here.
1228 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1229 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1230 (!ND->isFromASTFile() ||
1231 (isTranslationUnit() &&
1232 !getParentASTContext().getLangOpts().CPlusPlus)))
1233 makeDeclVisibleInContextImpl(ND, false);
1235 // If this declaration is itself a transparent declaration context
1236 // or inline namespace, add the members of this declaration of that
1237 // context (recursively).
1238 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1239 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1240 buildLookupImpl<Begin, End>(InnerCtx);
1244 DeclContext::lookup_result
1245 DeclContext::lookup(DeclarationName Name) {
1246 assert(DeclKind != Decl::LinkageSpec &&
1247 "Should not perform lookups into linkage specs!");
1249 DeclContext *PrimaryContext = getPrimaryContext();
1250 if (PrimaryContext != this)
1251 return PrimaryContext->lookup(Name);
1253 if (hasExternalVisibleStorage()) {
1254 StoredDeclsMap *Map = LookupPtr.getPointer();
1255 if (LookupPtr.getInt())
1256 Map = buildLookup();
1257 else if (NeedToReconcileExternalVisibleStorage)
1258 reconcileExternalVisibleStorage();
1261 Map = CreateStoredDeclsMap(getParentASTContext());
1263 // If we have a lookup result with no external decls, we are done.
1264 std::pair<StoredDeclsMap::iterator, bool> R =
1265 Map->insert(std::make_pair(Name, StoredDeclsList()));
1266 if (!R.second && !R.first->second.hasExternalDecls())
1267 return R.first->second.getLookupResult();
1269 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1270 if (Source->FindExternalVisibleDeclsByName(this, Name) || R.second) {
1271 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1272 StoredDeclsMap::iterator I = Map->find(Name);
1273 if (I != Map->end())
1274 return I->second.getLookupResult();
1278 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1281 StoredDeclsMap *Map = LookupPtr.getPointer();
1282 if (LookupPtr.getInt())
1283 Map = buildLookup();
1286 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1288 StoredDeclsMap::iterator I = Map->find(Name);
1289 if (I == Map->end())
1290 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1292 return I->second.getLookupResult();
1295 DeclContext::lookup_result
1296 DeclContext::noload_lookup(DeclarationName Name) {
1297 assert(DeclKind != Decl::LinkageSpec &&
1298 "Should not perform lookups into linkage specs!");
1299 if (!hasExternalVisibleStorage())
1300 return lookup(Name);
1302 DeclContext *PrimaryContext = getPrimaryContext();
1303 if (PrimaryContext != this)
1304 return PrimaryContext->noload_lookup(Name);
1306 StoredDeclsMap *Map = LookupPtr.getPointer();
1307 if (LookupPtr.getInt()) {
1308 // Carefully build the lookup map, without deserializing anything.
1309 SmallVector<DeclContext *, 2> Contexts;
1310 collectAllContexts(Contexts);
1311 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1312 buildLookupImpl<&DeclContext::noload_decls_begin,
1313 &DeclContext::noload_decls_end>(Contexts[I]);
1315 // We no longer have any lazy decls.
1316 LookupPtr.setInt(false);
1318 // There may now be names for which we have local decls but are
1319 // missing the external decls. FIXME: Just set the hasExternalDecls
1320 // flag on those names that have external decls.
1321 NeedToReconcileExternalVisibleStorage = true;
1323 Map = LookupPtr.getPointer();
1327 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1329 StoredDeclsMap::iterator I = Map->find(Name);
1330 return I != Map->end()
1331 ? I->second.getLookupResult()
1332 : lookup_result(lookup_iterator(0), lookup_iterator(0));
1335 void DeclContext::localUncachedLookup(DeclarationName Name,
1336 SmallVectorImpl<NamedDecl *> &Results) {
1339 // If there's no external storage, just perform a normal lookup and copy
1341 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
1342 lookup_result LookupResults = lookup(Name);
1343 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
1347 // If we have a lookup table, check there first. Maybe we'll get lucky.
1348 if (Name && !LookupPtr.getInt()) {
1349 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1350 StoredDeclsMap::iterator Pos = Map->find(Name);
1351 if (Pos != Map->end()) {
1352 Results.insert(Results.end(),
1353 Pos->second.getLookupResult().begin(),
1354 Pos->second.getLookupResult().end());
1360 // Slow case: grovel through the declarations in our chain looking for
1362 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1363 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1364 if (ND->getDeclName() == Name)
1365 Results.push_back(ND);
1369 DeclContext *DeclContext::getRedeclContext() {
1370 DeclContext *Ctx = this;
1371 // Skip through transparent contexts.
1372 while (Ctx->isTransparentContext())
1373 Ctx = Ctx->getParent();
1377 DeclContext *DeclContext::getEnclosingNamespaceContext() {
1378 DeclContext *Ctx = this;
1379 // Skip through non-namespace, non-translation-unit contexts.
1380 while (!Ctx->isFileContext())
1381 Ctx = Ctx->getParent();
1382 return Ctx->getPrimaryContext();
1385 bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1386 // For non-file contexts, this is equivalent to Equals.
1387 if (!isFileContext())
1388 return O->Equals(this);
1391 if (O->Equals(this))
1394 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1395 if (!NS || !NS->isInline())
1397 O = NS->getParent();
1403 void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1404 DeclContext *PrimaryDC = this->getPrimaryContext();
1405 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1406 // If the decl is being added outside of its semantic decl context, we
1407 // need to ensure that we eagerly build the lookup information for it.
1408 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
1411 void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1413 assert(this == getPrimaryContext() && "expected a primary DC");
1415 // Skip declarations within functions.
1416 if (isFunctionOrMethod())
1419 // Skip declarations which should be invisible to name lookup.
1420 if (shouldBeHidden(D))
1423 // If we already have a lookup data structure, perform the insertion into
1424 // it. If we might have externally-stored decls with this name, look them
1425 // up and perform the insertion. If this decl was declared outside its
1426 // semantic context, buildLookup won't add it, so add it now.
1428 // FIXME: As a performance hack, don't add such decls into the translation
1429 // unit unless we're in C++, since qualified lookup into the TU is never
1431 if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1432 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1433 (getParentASTContext().getLangOpts().CPlusPlus ||
1434 !isTranslationUnit()))) {
1435 // If we have lazily omitted any decls, they might have the same name as
1436 // the decl which we are adding, so build a full lookup table before adding
1439 makeDeclVisibleInContextImpl(D, Internal);
1441 LookupPtr.setInt(true);
1444 // If we are a transparent context or inline namespace, insert into our
1445 // parent context, too. This operation is recursive.
1446 if (isTransparentContext() || isInlineNamespace())
1447 getParent()->getPrimaryContext()->
1448 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1450 Decl *DCAsDecl = cast<Decl>(this);
1451 // Notify that a decl was made visible unless we are a Tag being defined.
1452 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1453 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1454 L->AddedVisibleDecl(this, D);
1457 void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1458 // Find or create the stored declaration map.
1459 StoredDeclsMap *Map = LookupPtr.getPointer();
1461 ASTContext *C = &getParentASTContext();
1462 Map = CreateStoredDeclsMap(*C);
1465 // If there is an external AST source, load any declarations it knows about
1466 // with this declaration's name.
1467 // If the lookup table contains an entry about this name it means that we
1468 // have already checked the external source.
1470 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1471 if (hasExternalVisibleStorage() &&
1472 Map->find(D->getDeclName()) == Map->end())
1473 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1475 // Insert this declaration into the map.
1476 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
1479 // If this is being added as part of loading an external declaration,
1480 // this may not be the only external declaration with this name.
1481 // In this case, we never try to replace an existing declaration; we'll
1482 // handle that when we finalize the list of declarations for this name.
1483 DeclNameEntries.setHasExternalDecls();
1484 DeclNameEntries.AddSubsequentDecl(D);
1488 else if (DeclNameEntries.isNull()) {
1489 DeclNameEntries.setOnlyValue(D);
1493 if (DeclNameEntries.HandleRedeclaration(D)) {
1494 // This declaration has replaced an existing one for which
1495 // declarationReplaces returns true.
1499 // Put this declaration into the appropriate slot.
1500 DeclNameEntries.AddSubsequentDecl(D);
1503 /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1505 DeclContext::udir_iterator_range
1506 DeclContext::getUsingDirectives() const {
1507 // FIXME: Use something more efficient than normal lookup for using
1508 // directives. In C++, using directives are looked up more than anything else.
1509 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
1510 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.begin()),
1511 reinterpret_cast<udir_iterator>(Result.end()));
1514 //===----------------------------------------------------------------------===//
1515 // Creation and Destruction of StoredDeclsMaps. //
1516 //===----------------------------------------------------------------------===//
1518 StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1519 assert(!LookupPtr.getPointer() && "context already has a decls map");
1520 assert(getPrimaryContext() == this &&
1521 "creating decls map on non-primary context");
1524 bool Dependent = isDependentContext();
1526 M = new DependentStoredDeclsMap();
1528 M = new StoredDeclsMap();
1529 M->Previous = C.LastSDM;
1530 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1531 LookupPtr.setPointer(M);
1535 void ASTContext::ReleaseDeclContextMaps() {
1536 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1537 // pointer because the subclass doesn't add anything that needs to
1539 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1542 void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1544 // Advance the iteration before we invalidate memory.
1545 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1548 delete static_cast<DependentStoredDeclsMap*>(Map);
1552 Map = Next.getPointer();
1553 Dependent = Next.getInt();
1557 DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1558 DeclContext *Parent,
1559 const PartialDiagnostic &PDiag) {
1560 assert(Parent->isDependentContext()
1561 && "cannot iterate dependent diagnostics of non-dependent context");
1562 Parent = Parent->getPrimaryContext();
1563 if (!Parent->LookupPtr.getPointer())
1564 Parent->CreateStoredDeclsMap(C);
1566 DependentStoredDeclsMap *Map
1567 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
1569 // Allocate the copy of the PartialDiagnostic via the ASTContext's
1570 // BumpPtrAllocator, rather than the ASTContext itself.
1571 PartialDiagnostic::Storage *DiagStorage = 0;
1572 if (PDiag.hasStorage())
1573 DiagStorage = new (C) PartialDiagnostic::Storage;
1575 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
1577 // TODO: Maybe we shouldn't reverse the order during insertion.
1578 DD->NextDiagnostic = Map->FirstDiagnostic;
1579 Map->FirstDiagnostic = DD;