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::operator new(std::size_t Size
, const ASTContext
&Context
,
49 unsigned ID
, std::size_t Extra
) {
50 // Allocate an extra 8 bytes worth of storage, which ensures that the
51 // resulting pointer will still be 8-byte aligned.
52 void *Start
= Context
.Allocate(Size
+ Extra
+ 8);
53 void *Result
= (char*)Start
+ 8;
55 unsigned *PrefixPtr
= (unsigned *)Result
- 2;
57 // Zero out the first 4 bytes; this is used to store the owning module ID.
60 // Store the global declaration ID in the second 4 bytes.
66 void *Decl::operator new(std::size_t Size
, const ASTContext
&Ctx
,
67 DeclContext
*Parent
, std::size_t Extra
) {
68 assert(!Parent
|| &Parent
->getParentASTContext() == &Ctx
);
69 return ::operator new(Size
+ Extra
, Ctx
);
72 Module
*Decl::getOwningModuleSlow() const {
73 assert(isFromASTFile() && "Not from AST file?");
74 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
77 const char *Decl::getDeclKindName() const {
79 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
80 #define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
81 #define ABSTRACT_DECL(DECL)
82 #include "clang/AST/DeclNodes.inc"
86 void Decl::setInvalidDecl(bool Invalid
) {
87 InvalidDecl
= Invalid
;
88 assert(!isa
<TagDecl
>(this) || !cast
<TagDecl
>(this)->isCompleteDefinition());
89 if (Invalid
&& !isa
<ParmVarDecl
>(this)) {
90 // Defensive maneuver for ill-formed code: we're likely not to make it to
91 // a point where we set the access specifier, so default it to "public"
92 // to avoid triggering asserts elsewhere in the front end.
97 const char *DeclContext::getDeclKindName() const {
99 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
100 #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
101 #define ABSTRACT_DECL(DECL)
102 #include "clang/AST/DeclNodes.inc"
106 bool Decl::StatisticsEnabled
= false;
107 void Decl::EnableStatistics() {
108 StatisticsEnabled
= true;
111 void Decl::PrintStats() {
112 llvm::errs() << "\n*** Decl Stats:\n";
115 #define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
116 #define ABSTRACT_DECL(DECL)
117 #include "clang/AST/DeclNodes.inc"
118 llvm::errs() << " " << totalDecls
<< " decls total.\n";
121 #define DECL(DERIVED, BASE) \
122 if (n##DERIVED##s > 0) { \
123 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
124 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
125 << sizeof(DERIVED##Decl) << " each (" \
126 << n##DERIVED##s * sizeof(DERIVED##Decl) \
129 #define ABSTRACT_DECL(DECL)
130 #include "clang/AST/DeclNodes.inc"
132 llvm::errs() << "Total bytes = " << totalBytes
<< "\n";
135 void Decl::add(Kind k
) {
137 #define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
138 #define ABSTRACT_DECL(DECL)
139 #include "clang/AST/DeclNodes.inc"
143 bool Decl::isTemplateParameterPack() const {
144 if (const TemplateTypeParmDecl
*TTP
= dyn_cast
<TemplateTypeParmDecl
>(this))
145 return TTP
->isParameterPack();
146 if (const NonTypeTemplateParmDecl
*NTTP
147 = dyn_cast
<NonTypeTemplateParmDecl
>(this))
148 return NTTP
->isParameterPack();
149 if (const TemplateTemplateParmDecl
*TTP
150 = dyn_cast
<TemplateTemplateParmDecl
>(this))
151 return TTP
->isParameterPack();
155 bool Decl::isParameterPack() const {
156 if (const ParmVarDecl
*Parm
= dyn_cast
<ParmVarDecl
>(this))
157 return Parm
->isParameterPack();
159 return isTemplateParameterPack();
162 FunctionDecl
*Decl::getAsFunction() {
163 if (FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(this))
165 if (const FunctionTemplateDecl
*FTD
= dyn_cast
<FunctionTemplateDecl
>(this))
166 return FTD
->getTemplatedDecl();
170 bool Decl::isTemplateDecl() const {
171 return isa
<TemplateDecl
>(this);
174 const DeclContext
*Decl::getParentFunctionOrMethod() const {
175 for (const DeclContext
*DC
= getDeclContext();
176 DC
&& !DC
->isTranslationUnit() && !DC
->isNamespace();
177 DC
= DC
->getParent())
178 if (DC
->isFunctionOrMethod())
185 //===----------------------------------------------------------------------===//
186 // PrettyStackTraceDecl Implementation
187 //===----------------------------------------------------------------------===//
189 void PrettyStackTraceDecl::print(raw_ostream
&OS
) const {
190 SourceLocation TheLoc
= Loc
;
191 if (TheLoc
.isInvalid() && TheDecl
)
192 TheLoc
= TheDecl
->getLocation();
194 if (TheLoc
.isValid()) {
195 TheLoc
.print(OS
, SM
);
201 if (const NamedDecl
*DN
= dyn_cast_or_null
<NamedDecl
>(TheDecl
)) {
203 DN
->printQualifiedName(OS
);
209 //===----------------------------------------------------------------------===//
210 // Decl Implementation
211 //===----------------------------------------------------------------------===//
213 // Out-of-line virtual method providing a home for Decl.
216 void Decl::setDeclContext(DeclContext
*DC
) {
220 void Decl::setLexicalDeclContext(DeclContext
*DC
) {
221 if (DC
== getLexicalDeclContext())
225 setDeclContextsImpl(getDeclContext(), DC
, getASTContext());
227 getMultipleDC()->LexicalDC
= DC
;
231 void Decl::setDeclContextsImpl(DeclContext
*SemaDC
, DeclContext
*LexicalDC
,
233 if (SemaDC
== LexicalDC
) {
236 Decl::MultipleDC
*MDC
= new (Ctx
) Decl::MultipleDC();
237 MDC
->SemanticDC
= SemaDC
;
238 MDC
->LexicalDC
= LexicalDC
;
243 bool Decl::isInAnonymousNamespace() const {
244 const DeclContext
*DC
= getDeclContext();
246 if (const NamespaceDecl
*ND
= dyn_cast
<NamespaceDecl
>(DC
))
247 if (ND
->isAnonymousNamespace())
249 } while ((DC
= DC
->getParent()));
254 bool Decl::isInStdNamespace() const {
255 return getDeclContext()->isStdNamespace();
258 TranslationUnitDecl
*Decl::getTranslationUnitDecl() {
259 if (TranslationUnitDecl
*TUD
= dyn_cast
<TranslationUnitDecl
>(this))
262 DeclContext
*DC
= getDeclContext();
263 assert(DC
&& "This decl is not contained in a translation unit!");
265 while (!DC
->isTranslationUnit()) {
266 DC
= DC
->getParent();
267 assert(DC
&& "This decl is not contained in a translation unit!");
270 return cast
<TranslationUnitDecl
>(DC
);
273 ASTContext
&Decl::getASTContext() const {
274 return getTranslationUnitDecl()->getASTContext();
277 ASTMutationListener
*Decl::getASTMutationListener() const {
278 return getASTContext().getASTMutationListener();
281 unsigned Decl::getMaxAlignment() const {
286 const AttrVec
&V
= getAttrs();
287 ASTContext
&Ctx
= getASTContext();
288 specific_attr_iterator
<AlignedAttr
> I(V
.begin()), E(V
.end());
290 Align
= std::max(Align
, I
->getAlignment(Ctx
));
294 bool Decl::isUsed(bool CheckUsedAttr
) const {
298 // Check for used attribute.
299 if (CheckUsedAttr
&& hasAttr
<UsedAttr
>())
305 void Decl::markUsed(ASTContext
&C
) {
309 if (C
.getASTMutationListener())
310 C
.getASTMutationListener()->DeclarationMarkedUsed(this);
315 bool Decl::isReferenced() const {
319 // Check redeclarations.
320 for (auto I
: redecls())
327 /// \brief Determine the availability of the given declaration based on
328 /// the target platform.
330 /// When it returns an availability result other than \c AR_Available,
331 /// if the \p Message parameter is non-NULL, it will be set to a
332 /// string describing why the entity is unavailable.
334 /// FIXME: Make these strings localizable, since they end up in
336 static AvailabilityResult
CheckAvailability(ASTContext
&Context
,
337 const AvailabilityAttr
*A
,
338 std::string
*Message
) {
339 StringRef TargetPlatform
= Context
.getTargetInfo().getPlatformName();
340 StringRef PrettyPlatformName
341 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform
);
342 if (PrettyPlatformName
.empty())
343 PrettyPlatformName
= TargetPlatform
;
345 VersionTuple TargetMinVersion
= Context
.getTargetInfo().getPlatformMinVersion();
346 if (TargetMinVersion
.empty())
349 // Match the platform name.
350 if (A
->getPlatform()->getName() != TargetPlatform
)
353 std::string HintMessage
;
354 if (!A
->getMessage().empty()) {
356 HintMessage
+= A
->getMessage();
359 // Make sure that this declaration has not been marked 'unavailable'.
360 if (A
->getUnavailable()) {
363 llvm::raw_string_ostream
Out(*Message
);
364 Out
<< "not available on " << PrettyPlatformName
368 return AR_Unavailable
;
371 // Make sure that this declaration has already been introduced.
372 if (!A
->getIntroduced().empty() &&
373 TargetMinVersion
< A
->getIntroduced()) {
376 llvm::raw_string_ostream
Out(*Message
);
377 VersionTuple
VTI(A
->getIntroduced());
378 VTI
.UseDotAsSeparator();
379 Out
<< "introduced in " << PrettyPlatformName
<< ' '
380 << VTI
<< HintMessage
;
383 return AR_NotYetIntroduced
;
386 // Make sure that this declaration hasn't been obsoleted.
387 if (!A
->getObsoleted().empty() && TargetMinVersion
>= A
->getObsoleted()) {
390 llvm::raw_string_ostream
Out(*Message
);
391 VersionTuple
VTO(A
->getObsoleted());
392 VTO
.UseDotAsSeparator();
393 Out
<< "obsoleted in " << PrettyPlatformName
<< ' '
394 << VTO
<< HintMessage
;
397 return AR_Unavailable
;
400 // Make sure that this declaration hasn't been deprecated.
401 if (!A
->getDeprecated().empty() && TargetMinVersion
>= A
->getDeprecated()) {
404 llvm::raw_string_ostream
Out(*Message
);
405 VersionTuple
VTD(A
->getDeprecated());
406 VTD
.UseDotAsSeparator();
407 Out
<< "first deprecated in " << PrettyPlatformName
<< ' '
408 << VTD
<< HintMessage
;
411 return AR_Deprecated
;
417 AvailabilityResult
Decl::getAvailability(std::string
*Message
) const {
418 AvailabilityResult Result
= AR_Available
;
419 std::string ResultMessage
;
421 for (const auto *A
: attrs()) {
422 if (const auto *Deprecated
= dyn_cast
<DeprecatedAttr
>(A
)) {
423 if (Result
>= AR_Deprecated
)
427 ResultMessage
= Deprecated
->getMessage();
429 Result
= AR_Deprecated
;
433 if (const auto *Unavailable
= dyn_cast
<UnavailableAttr
>(A
)) {
435 *Message
= Unavailable
->getMessage();
436 return AR_Unavailable
;
439 if (const auto *Availability
= dyn_cast
<AvailabilityAttr
>(A
)) {
440 AvailabilityResult AR
= CheckAvailability(getASTContext(), Availability
,
443 if (AR
== AR_Unavailable
)
444 return AR_Unavailable
;
449 ResultMessage
.swap(*Message
);
456 Message
->swap(ResultMessage
);
460 bool Decl::canBeWeakImported(bool &IsDefinition
) const {
461 IsDefinition
= false;
463 // Variables, if they aren't definitions.
464 if (const VarDecl
*Var
= dyn_cast
<VarDecl
>(this)) {
465 if (Var
->isThisDeclarationADefinition()) {
471 // Functions, if they aren't definitions.
472 } else if (const FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(this)) {
479 // Objective-C classes, if this is the non-fragile runtime.
480 } else if (isa
<ObjCInterfaceDecl
>(this) &&
481 getASTContext().getLangOpts().ObjCRuntime
.hasWeakClassImport()) {
490 bool Decl::isWeakImported() const {
492 if (!canBeWeakImported(IsDefinition
))
495 for (const auto *A
: attrs()) {
496 if (isa
<WeakImportAttr
>(A
))
499 if (const auto *Availability
= dyn_cast
<AvailabilityAttr
>(A
)) {
500 if (CheckAvailability(getASTContext(), Availability
,
501 nullptr) == AR_NotYetIntroduced
)
509 unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind
) {
520 case NonTypeTemplateParm
:
524 return IDNS_Ordinary
;
528 return IDNS_Ordinary
| IDNS_Member
;
530 case ObjCCompatibleAlias
:
532 return IDNS_Ordinary
| IDNS_Type
;
536 case TypeAliasTemplate
:
537 case UnresolvedUsingTypename
:
538 case TemplateTypeParm
:
539 return IDNS_Ordinary
| IDNS_Type
;
542 return 0; // we'll actually overwrite this later
544 case UnresolvedUsingValue
:
545 return IDNS_Ordinary
| IDNS_Using
;
551 return IDNS_ObjCProtocol
;
554 case ObjCAtDefsField
:
561 return IDNS_Tag
| IDNS_Type
;
565 return IDNS_Namespace
;
567 case FunctionTemplate
:
569 return IDNS_Ordinary
;
572 case TemplateTemplateParm
:
573 return IDNS_Ordinary
| IDNS_Tag
| IDNS_Type
;
582 case ObjCPropertyImpl
:
585 case TranslationUnit
:
588 case ClassTemplateSpecialization
:
589 case ClassTemplatePartialSpecialization
:
590 case ClassScopeFunctionSpecialization
:
591 case VarTemplateSpecialization
:
592 case VarTemplatePartialSpecialization
:
593 case ObjCImplementation
:
595 case ObjCCategoryImpl
:
597 case OMPThreadPrivate
:
599 // Never looked up by name.
603 llvm_unreachable("Invalid DeclKind!");
606 void Decl::setAttrsImpl(const AttrVec
&attrs
, ASTContext
&Ctx
) {
607 assert(!HasAttrs
&& "Decl already contains attrs.");
609 AttrVec
&AttrBlank
= Ctx
.getDeclAttrs(this);
610 assert(AttrBlank
.empty() && "HasAttrs was wrong?");
616 void Decl::dropAttrs() {
617 if (!HasAttrs
) return;
620 getASTContext().eraseDeclAttrs(this);
623 const AttrVec
&Decl::getAttrs() const {
624 assert(HasAttrs
&& "No attrs to get!");
625 return getASTContext().getDeclAttrs(this);
628 Decl
*Decl::castFromDeclContext (const DeclContext
*D
) {
629 Decl::Kind DK
= D
->getDeclKind();
631 #define DECL(NAME, BASE)
632 #define DECL_CONTEXT(NAME) \
634 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
635 #define DECL_CONTEXT_BASE(NAME)
636 #include "clang/AST/DeclNodes.inc"
638 #define DECL(NAME, BASE)
639 #define DECL_CONTEXT_BASE(NAME) \
640 if (DK >= first##NAME && DK <= last##NAME) \
641 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
642 #include "clang/AST/DeclNodes.inc"
643 llvm_unreachable("a decl that inherits DeclContext isn't handled");
647 DeclContext
*Decl::castToDeclContext(const Decl
*D
) {
648 Decl::Kind DK
= D
->getKind();
650 #define DECL(NAME, BASE)
651 #define DECL_CONTEXT(NAME) \
653 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
654 #define DECL_CONTEXT_BASE(NAME)
655 #include "clang/AST/DeclNodes.inc"
657 #define DECL(NAME, BASE)
658 #define DECL_CONTEXT_BASE(NAME) \
659 if (DK >= first##NAME && DK <= last##NAME) \
660 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
661 #include "clang/AST/DeclNodes.inc"
662 llvm_unreachable("a decl that inherits DeclContext isn't handled");
666 SourceLocation
Decl::getBodyRBrace() const {
667 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
668 // FunctionDecl stores EndRangeLoc for this purpose.
669 if (const FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(this)) {
670 const FunctionDecl
*Definition
;
671 if (FD
->hasBody(Definition
))
672 return Definition
->getSourceRange().getEnd();
673 return SourceLocation();
676 if (Stmt
*Body
= getBody())
677 return Body
->getSourceRange().getEnd();
679 return SourceLocation();
682 bool Decl::AccessDeclContextSanity() const {
684 // Suppress this check if any of the following hold:
685 // 1. this is the translation unit (and thus has no parent)
686 // 2. this is a template parameter (and thus doesn't belong to its context)
687 // 3. this is a non-type template parameter
688 // 4. the context is not a record
690 // 6. it's a C++0x static_assert.
691 if (isa
<TranslationUnitDecl
>(this) ||
692 isa
<TemplateTypeParmDecl
>(this) ||
693 isa
<NonTypeTemplateParmDecl
>(this) ||
694 !isa
<CXXRecordDecl
>(getDeclContext()) ||
696 isa
<StaticAssertDecl
>(this) ||
697 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
698 // as DeclContext (?).
699 isa
<ParmVarDecl
>(this) ||
700 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
701 // AS_none as access specifier.
702 isa
<CXXRecordDecl
>(this) ||
703 isa
<ClassScopeFunctionSpecializationDecl
>(this))
706 assert(Access
!= AS_none
&&
707 "Access specifier is AS_none inside a record decl");
712 static Decl::Kind
getKind(const Decl
*D
) { return D
->getKind(); }
713 static Decl::Kind
getKind(const DeclContext
*DC
) { return DC
->getDeclKind(); }
715 const FunctionType
*Decl::getFunctionType(bool BlocksToo
) const {
717 if (const ValueDecl
*D
= dyn_cast
<ValueDecl
>(this))
719 else if (const TypedefNameDecl
*D
= dyn_cast
<TypedefNameDecl
>(this))
720 Ty
= D
->getUnderlyingType();
724 if (Ty
->isFunctionPointerType())
725 Ty
= Ty
->getAs
<PointerType
>()->getPointeeType();
726 else if (BlocksToo
&& Ty
->isBlockPointerType())
727 Ty
= Ty
->getAs
<BlockPointerType
>()->getPointeeType();
729 return Ty
->getAs
<FunctionType
>();
733 /// Starting at a given context (a Decl or DeclContext), look for a
734 /// code context that is not a closure (a lambda, block, etc.).
735 template <class T
> static Decl
*getNonClosureContext(T
*D
) {
736 if (getKind(D
) == Decl::CXXMethod
) {
737 CXXMethodDecl
*MD
= cast
<CXXMethodDecl
>(D
);
738 if (MD
->getOverloadedOperator() == OO_Call
&&
739 MD
->getParent()->isLambda())
740 return getNonClosureContext(MD
->getParent()->getParent());
742 } else if (FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(D
)) {
744 } else if (ObjCMethodDecl
*MD
= dyn_cast
<ObjCMethodDecl
>(D
)) {
746 } else if (BlockDecl
*BD
= dyn_cast
<BlockDecl
>(D
)) {
747 return getNonClosureContext(BD
->getParent());
748 } else if (CapturedDecl
*CD
= dyn_cast
<CapturedDecl
>(D
)) {
749 return getNonClosureContext(CD
->getParent());
755 Decl
*Decl::getNonClosureContext() {
756 return ::getNonClosureContext(this);
759 Decl
*DeclContext::getNonClosureAncestor() {
760 return ::getNonClosureContext(this);
763 //===----------------------------------------------------------------------===//
764 // DeclContext Implementation
765 //===----------------------------------------------------------------------===//
767 bool DeclContext::classof(const Decl
*D
) {
768 switch (D
->getKind()) {
769 #define DECL(NAME, BASE)
770 #define DECL_CONTEXT(NAME) case Decl::NAME:
771 #define DECL_CONTEXT_BASE(NAME)
772 #include "clang/AST/DeclNodes.inc"
775 #define DECL(NAME, BASE)
776 #define DECL_CONTEXT_BASE(NAME) \
777 if (D->getKind() >= Decl::first##NAME && \
778 D->getKind() <= Decl::last##NAME) \
780 #include "clang/AST/DeclNodes.inc"
785 DeclContext::~DeclContext() { }
787 /// \brief Find the parent context of this context that will be
788 /// used for unqualified name lookup.
790 /// Generally, the parent lookup context is the semantic context. However, for
791 /// a friend function the parent lookup context is the lexical context, which
792 /// is the class in which the friend is declared.
793 DeclContext
*DeclContext::getLookupParent() {
794 // FIXME: Find a better way to identify friends
795 if (isa
<FunctionDecl
>(this))
796 if (getParent()->getRedeclContext()->isFileContext() &&
797 getLexicalParent()->getRedeclContext()->isRecord())
798 return getLexicalParent();
803 bool DeclContext::isInlineNamespace() const {
804 return isNamespace() &&
805 cast
<NamespaceDecl
>(this)->isInline();
808 bool DeclContext::isStdNamespace() const {
812 const NamespaceDecl
*ND
= cast
<NamespaceDecl
>(this);
813 if (ND
->isInline()) {
814 return ND
->getParent()->isStdNamespace();
817 if (!getParent()->getRedeclContext()->isTranslationUnit())
820 const IdentifierInfo
*II
= ND
->getIdentifier();
821 return II
&& II
->isStr("std");
824 bool DeclContext::isDependentContext() const {
828 if (isa
<ClassTemplatePartialSpecializationDecl
>(this))
831 if (const CXXRecordDecl
*Record
= dyn_cast
<CXXRecordDecl
>(this)) {
832 if (Record
->getDescribedClassTemplate())
835 if (Record
->isDependentLambda())
839 if (const FunctionDecl
*Function
= dyn_cast
<FunctionDecl
>(this)) {
840 if (Function
->getDescribedFunctionTemplate())
843 // Friend function declarations are dependent if their *lexical*
844 // context is dependent.
845 if (cast
<Decl
>(this)->getFriendObjectKind())
846 return getLexicalParent()->isDependentContext();
849 return getParent() && getParent()->isDependentContext();
852 bool DeclContext::isTransparentContext() const {
853 if (DeclKind
== Decl::Enum
)
854 return !cast
<EnumDecl
>(this)->isScoped();
855 else if (DeclKind
== Decl::LinkageSpec
)
861 static bool isLinkageSpecContext(const DeclContext
*DC
,
862 LinkageSpecDecl::LanguageIDs ID
) {
863 while (DC
->getDeclKind() != Decl::TranslationUnit
) {
864 if (DC
->getDeclKind() == Decl::LinkageSpec
)
865 return cast
<LinkageSpecDecl
>(DC
)->getLanguage() == ID
;
866 DC
= DC
->getLexicalParent();
871 bool DeclContext::isExternCContext() const {
872 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_c
);
875 bool DeclContext::isExternCXXContext() const {
876 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_cxx
);
879 bool DeclContext::Encloses(const DeclContext
*DC
) const {
880 if (getPrimaryContext() != this)
881 return getPrimaryContext()->Encloses(DC
);
883 for (; DC
; DC
= DC
->getParent())
884 if (DC
->getPrimaryContext() == this)
889 DeclContext
*DeclContext::getPrimaryContext() {
891 case Decl::TranslationUnit
:
892 case Decl::LinkageSpec
:
895 // There is only one DeclContext for these entities.
898 case Decl::Namespace
:
899 // The original namespace is our primary context.
900 return static_cast<NamespaceDecl
*>(this)->getOriginalNamespace();
902 case Decl::ObjCMethod
:
905 case Decl::ObjCInterface
:
906 if (ObjCInterfaceDecl
*Def
= cast
<ObjCInterfaceDecl
>(this)->getDefinition())
911 case Decl::ObjCProtocol
:
912 if (ObjCProtocolDecl
*Def
= cast
<ObjCProtocolDecl
>(this)->getDefinition())
917 case Decl::ObjCCategory
:
920 case Decl::ObjCImplementation
:
921 case Decl::ObjCCategoryImpl
:
925 if (DeclKind
>= Decl::firstTag
&& DeclKind
<= Decl::lastTag
) {
926 // If this is a tag type that has a definition or is currently
927 // being defined, that definition is our primary context.
928 TagDecl
*Tag
= cast
<TagDecl
>(this);
930 if (TagDecl
*Def
= Tag
->getDefinition())
933 if (const TagType
*TagTy
= dyn_cast
<TagType
>(Tag
->getTypeForDecl())) {
934 // Note, TagType::getDecl returns the (partial) definition one exists.
935 TagDecl
*PossiblePartialDef
= TagTy
->getDecl();
936 if (PossiblePartialDef
->isBeingDefined())
937 return PossiblePartialDef
;
939 assert(isa
<InjectedClassNameType
>(Tag
->getTypeForDecl()));
945 assert(DeclKind
>= Decl::firstFunction
&& DeclKind
<= Decl::lastFunction
&&
946 "Unknown DeclContext kind");
952 DeclContext::collectAllContexts(SmallVectorImpl
<DeclContext
*> &Contexts
){
955 if (DeclKind
!= Decl::Namespace
) {
956 Contexts
.push_back(this);
960 NamespaceDecl
*Self
= static_cast<NamespaceDecl
*>(this);
961 for (NamespaceDecl
*N
= Self
->getMostRecentDecl(); N
;
962 N
= N
->getPreviousDecl())
963 Contexts
.push_back(N
);
965 std::reverse(Contexts
.begin(), Contexts
.end());
968 std::pair
<Decl
*, Decl
*>
969 DeclContext::BuildDeclChain(ArrayRef
<Decl
*> Decls
,
970 bool FieldsAlreadyLoaded
) {
971 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
972 Decl
*FirstNewDecl
= nullptr;
973 Decl
*PrevDecl
= nullptr;
974 for (unsigned I
= 0, N
= Decls
.size(); I
!= N
; ++I
) {
975 if (FieldsAlreadyLoaded
&& isa
<FieldDecl
>(Decls
[I
]))
980 PrevDecl
->NextInContextAndBits
.setPointer(D
);
987 return std::make_pair(FirstNewDecl
, PrevDecl
);
990 /// \brief We have just acquired external visible storage, and we already have
991 /// built a lookup map. For every name in the map, pull in the new names from
992 /// the external storage.
993 void DeclContext::reconcileExternalVisibleStorage() const {
994 assert(NeedToReconcileExternalVisibleStorage
&& LookupPtr
.getPointer());
995 NeedToReconcileExternalVisibleStorage
= false;
997 for (auto &Lookup
: *LookupPtr
.getPointer())
998 Lookup
.second
.setHasExternalDecls();
1001 /// \brief Load the declarations within this lexical storage from an
1002 /// external source.
1004 DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1005 ExternalASTSource
*Source
= getParentASTContext().getExternalSource();
1006 assert(hasExternalLexicalStorage() && Source
&& "No external storage?");
1008 // Notify that we have a DeclContext that is initializing.
1009 ExternalASTSource::Deserializing
ADeclContext(Source
);
1011 // Load the external declarations, if any.
1012 SmallVector
<Decl
*, 64> Decls
;
1013 ExternalLexicalStorage
= false;
1014 switch (Source
->FindExternalLexicalDecls(this, Decls
)) {
1019 case ELR_AlreadyLoaded
:
1026 // We may have already loaded just the fields of this record, in which case
1027 // we need to ignore them.
1028 bool FieldsAlreadyLoaded
= false;
1029 if (const RecordDecl
*RD
= dyn_cast
<RecordDecl
>(this))
1030 FieldsAlreadyLoaded
= RD
->LoadedFieldsFromExternalStorage
;
1032 // Splice the newly-read declarations into the beginning of the list
1034 Decl
*ExternalFirst
, *ExternalLast
;
1035 std::tie(ExternalFirst
, ExternalLast
) =
1036 BuildDeclChain(Decls
, FieldsAlreadyLoaded
);
1037 ExternalLast
->NextInContextAndBits
.setPointer(FirstDecl
);
1038 FirstDecl
= ExternalFirst
;
1040 LastDecl
= ExternalLast
;
1043 DeclContext::lookup_result
1044 ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext
*DC
,
1045 DeclarationName Name
) {
1046 ASTContext
&Context
= DC
->getParentASTContext();
1047 StoredDeclsMap
*Map
;
1048 if (!(Map
= DC
->LookupPtr
.getPointer()))
1049 Map
= DC
->CreateStoredDeclsMap(Context
);
1050 if (DC
->NeedToReconcileExternalVisibleStorage
)
1051 DC
->reconcileExternalVisibleStorage();
1053 (*Map
)[Name
].removeExternalDecls();
1055 return DeclContext::lookup_result();
1058 DeclContext::lookup_result
1059 ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext
*DC
,
1060 DeclarationName Name
,
1061 ArrayRef
<NamedDecl
*> Decls
) {
1062 ASTContext
&Context
= DC
->getParentASTContext();
1063 StoredDeclsMap
*Map
;
1064 if (!(Map
= DC
->LookupPtr
.getPointer()))
1065 Map
= DC
->CreateStoredDeclsMap(Context
);
1066 if (DC
->NeedToReconcileExternalVisibleStorage
)
1067 DC
->reconcileExternalVisibleStorage();
1069 StoredDeclsList
&List
= (*Map
)[Name
];
1071 // Clear out any old external visible declarations, to avoid quadratic
1072 // performance in the redeclaration checks below.
1073 List
.removeExternalDecls();
1075 if (!List
.isNull()) {
1076 // We have both existing declarations and new declarations for this name.
1077 // Some of the declarations may simply replace existing ones. Handle those
1079 llvm::SmallVector
<unsigned, 8> Skip
;
1080 for (unsigned I
= 0, N
= Decls
.size(); I
!= N
; ++I
)
1081 if (List
.HandleRedeclaration(Decls
[I
]))
1083 Skip
.push_back(Decls
.size());
1085 // Add in any new declarations.
1086 unsigned SkipPos
= 0;
1087 for (unsigned I
= 0, N
= Decls
.size(); I
!= N
; ++I
) {
1088 if (I
== Skip
[SkipPos
])
1091 List
.AddSubsequentDecl(Decls
[I
]);
1094 // Convert the array to a StoredDeclsList.
1095 for (ArrayRef
<NamedDecl
*>::iterator
1096 I
= Decls
.begin(), E
= Decls
.end(); I
!= E
; ++I
) {
1098 List
.setOnlyValue(*I
);
1100 List
.AddSubsequentDecl(*I
);
1104 return List
.getLookupResult();
1107 DeclContext::decl_iterator
DeclContext::decls_begin() const {
1108 if (hasExternalLexicalStorage())
1109 LoadLexicalDeclsFromExternalStorage();
1110 return decl_iterator(FirstDecl
);
1113 bool DeclContext::decls_empty() const {
1114 if (hasExternalLexicalStorage())
1115 LoadLexicalDeclsFromExternalStorage();
1120 bool DeclContext::containsDecl(Decl
*D
) const {
1121 return (D
->getLexicalDeclContext() == this &&
1122 (D
->NextInContextAndBits
.getPointer() || D
== LastDecl
));
1125 void DeclContext::removeDecl(Decl
*D
) {
1126 assert(D
->getLexicalDeclContext() == this &&
1127 "decl being removed from non-lexical context");
1128 assert((D
->NextInContextAndBits
.getPointer() || D
== LastDecl
) &&
1129 "decl is not in decls list");
1131 // Remove D from the decl chain. This is O(n) but hopefully rare.
1132 if (D
== FirstDecl
) {
1134 FirstDecl
= LastDecl
= nullptr;
1136 FirstDecl
= D
->NextInContextAndBits
.getPointer();
1138 for (Decl
*I
= FirstDecl
; true; I
= I
->NextInContextAndBits
.getPointer()) {
1139 assert(I
&& "decl not found in linked list");
1140 if (I
->NextInContextAndBits
.getPointer() == D
) {
1141 I
->NextInContextAndBits
.setPointer(D
->NextInContextAndBits
.getPointer());
1142 if (D
== LastDecl
) LastDecl
= I
;
1148 // Mark that D is no longer in the decl chain.
1149 D
->NextInContextAndBits
.setPointer(nullptr);
1151 // Remove D from the lookup table if necessary.
1152 if (isa
<NamedDecl
>(D
)) {
1153 NamedDecl
*ND
= cast
<NamedDecl
>(D
);
1155 // Remove only decls that have a name
1156 if (!ND
->getDeclName()) return;
1158 StoredDeclsMap
*Map
= getPrimaryContext()->LookupPtr
.getPointer();
1161 StoredDeclsMap::iterator Pos
= Map
->find(ND
->getDeclName());
1162 assert(Pos
!= Map
->end() && "no lookup entry for decl");
1163 if (Pos
->second
.getAsVector() || Pos
->second
.getAsDecl() == ND
)
1164 Pos
->second
.remove(ND
);
1168 void DeclContext::addHiddenDecl(Decl
*D
) {
1169 assert(D
->getLexicalDeclContext() == this &&
1170 "Decl inserted into wrong lexical context");
1171 assert(!D
->getNextDeclInContext() && D
!= LastDecl
&&
1172 "Decl already inserted into a DeclContext");
1175 LastDecl
->NextInContextAndBits
.setPointer(D
);
1178 FirstDecl
= LastDecl
= D
;
1181 // Notify a C++ record declaration that we've added a member, so it can
1182 // update it's class-specific state.
1183 if (CXXRecordDecl
*Record
= dyn_cast
<CXXRecordDecl
>(this))
1184 Record
->addedMember(D
);
1186 // If this is a newly-created (not de-serialized) import declaration, wire
1187 // it in to the list of local import declarations.
1188 if (!D
->isFromASTFile()) {
1189 if (ImportDecl
*Import
= dyn_cast
<ImportDecl
>(D
))
1190 D
->getASTContext().addedLocalImportDecl(Import
);
1194 void DeclContext::addDecl(Decl
*D
) {
1197 if (NamedDecl
*ND
= dyn_cast
<NamedDecl
>(D
))
1198 ND
->getDeclContext()->getPrimaryContext()->
1199 makeDeclVisibleInContextWithFlags(ND
, false, true);
1202 void DeclContext::addDeclInternal(Decl
*D
) {
1205 if (NamedDecl
*ND
= dyn_cast
<NamedDecl
>(D
))
1206 ND
->getDeclContext()->getPrimaryContext()->
1207 makeDeclVisibleInContextWithFlags(ND
, true, true);
1210 /// shouldBeHidden - Determine whether a declaration which was declared
1211 /// within its semantic context should be invisible to qualified name lookup.
1212 static bool shouldBeHidden(NamedDecl
*D
) {
1213 // Skip unnamed declarations.
1214 if (!D
->getDeclName())
1217 // Skip entities that can't be found by name lookup into a particular
1219 if ((D
->getIdentifierNamespace() == 0 && !isa
<UsingDirectiveDecl
>(D
)) ||
1220 D
->isTemplateParameter())
1223 // Skip template specializations.
1224 // FIXME: This feels like a hack. Should DeclarationName support
1225 // template-ids, or is there a better way to keep specializations
1226 // from being visible?
1227 if (isa
<ClassTemplateSpecializationDecl
>(D
))
1229 if (FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(D
))
1230 if (FD
->isFunctionTemplateSpecialization())
1236 /// buildLookup - Build the lookup data structure with all of the
1237 /// declarations in this DeclContext (and any other contexts linked
1238 /// to it or transparent contexts nested within it) and return it.
1240 /// Note that the produced map may miss out declarations from an
1241 /// external source. If it does, those entries will be marked with
1242 /// the 'hasExternalDecls' flag.
1243 StoredDeclsMap
*DeclContext::buildLookup() {
1244 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1246 // FIXME: Should we keep going if hasExternalVisibleStorage?
1247 if (!LookupPtr
.getInt())
1248 return LookupPtr
.getPointer();
1250 SmallVector
<DeclContext
*, 2> Contexts
;
1251 collectAllContexts(Contexts
);
1252 for (unsigned I
= 0, N
= Contexts
.size(); I
!= N
; ++I
)
1253 buildLookupImpl
<&DeclContext::decls_begin
,
1254 &DeclContext::decls_end
>(Contexts
[I
]);
1256 // We no longer have any lazy decls.
1257 LookupPtr
.setInt(false);
1258 return LookupPtr
.getPointer();
1261 /// buildLookupImpl - Build part of the lookup data structure for the
1262 /// declarations contained within DCtx, which will either be this
1263 /// DeclContext, a DeclContext linked to it, or a transparent context
1264 /// nested within it.
1265 template<DeclContext::decl_iterator (DeclContext::*Begin
)() const,
1266 DeclContext::decl_iterator (DeclContext::*End
)() const>
1267 void DeclContext::buildLookupImpl(DeclContext
*DCtx
) {
1268 for (decl_iterator I
= (DCtx
->*Begin
)(), E
= (DCtx
->*End
)();
1272 // Insert this declaration into the lookup structure, but only if
1273 // it's semantically within its decl context. Any other decls which
1274 // should be found in this context are added eagerly.
1276 // If it's from an AST file, don't add it now. It'll get handled by
1277 // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1278 // in C++, we do not track external visible decls for the TU, so in
1279 // that case we need to collect them all here.
1280 if (NamedDecl
*ND
= dyn_cast
<NamedDecl
>(D
))
1281 if (ND
->getDeclContext() == DCtx
&& !shouldBeHidden(ND
) &&
1282 (!ND
->isFromASTFile() ||
1283 (isTranslationUnit() &&
1284 !getParentASTContext().getLangOpts().CPlusPlus
)))
1285 makeDeclVisibleInContextImpl(ND
, false);
1287 // If this declaration is itself a transparent declaration context
1288 // or inline namespace, add the members of this declaration of that
1289 // context (recursively).
1290 if (DeclContext
*InnerCtx
= dyn_cast
<DeclContext
>(D
))
1291 if (InnerCtx
->isTransparentContext() || InnerCtx
->isInlineNamespace())
1292 buildLookupImpl
<Begin
, End
>(InnerCtx
);
1296 DeclContext::lookup_result
1297 DeclContext::lookup(DeclarationName Name
) {
1298 assert(DeclKind
!= Decl::LinkageSpec
&&
1299 "Should not perform lookups into linkage specs!");
1301 DeclContext
*PrimaryContext
= getPrimaryContext();
1302 if (PrimaryContext
!= this)
1303 return PrimaryContext
->lookup(Name
);
1305 // If this is a namespace, ensure that any later redeclarations of it have
1306 // been loaded, since they may add names to the result of this lookup.
1307 if (auto *ND
= dyn_cast
<NamespaceDecl
>(this))
1308 (void)ND
->getMostRecentDecl();
1310 if (hasExternalVisibleStorage()) {
1311 if (NeedToReconcileExternalVisibleStorage
)
1312 reconcileExternalVisibleStorage();
1314 StoredDeclsMap
*Map
= LookupPtr
.getPointer();
1316 if (LookupPtr
.getInt())
1317 Map
= buildLookup();
1320 Map
= CreateStoredDeclsMap(getParentASTContext());
1322 // If we have a lookup result with no external decls, we are done.
1323 std::pair
<StoredDeclsMap::iterator
, bool> R
=
1324 Map
->insert(std::make_pair(Name
, StoredDeclsList()));
1325 if (!R
.second
&& !R
.first
->second
.hasExternalDecls())
1326 return R
.first
->second
.getLookupResult();
1328 ExternalASTSource
*Source
= getParentASTContext().getExternalSource();
1329 if (Source
->FindExternalVisibleDeclsByName(this, Name
) || !R
.second
) {
1330 if (StoredDeclsMap
*Map
= LookupPtr
.getPointer()) {
1331 StoredDeclsMap::iterator I
= Map
->find(Name
);
1332 if (I
!= Map
->end())
1333 return I
->second
.getLookupResult();
1337 return lookup_result(lookup_iterator(nullptr), lookup_iterator(nullptr));
1340 StoredDeclsMap
*Map
= LookupPtr
.getPointer();
1341 if (LookupPtr
.getInt())
1342 Map
= buildLookup();
1345 return lookup_result(lookup_iterator(nullptr), lookup_iterator(nullptr));
1347 StoredDeclsMap::iterator I
= Map
->find(Name
);
1348 if (I
== Map
->end())
1349 return lookup_result(lookup_iterator(nullptr), lookup_iterator(nullptr));
1351 return I
->second
.getLookupResult();
1354 DeclContext::lookup_result
1355 DeclContext::noload_lookup(DeclarationName Name
) {
1356 assert(DeclKind
!= Decl::LinkageSpec
&&
1357 "Should not perform lookups into linkage specs!");
1358 if (!hasExternalVisibleStorage())
1359 return lookup(Name
);
1361 DeclContext
*PrimaryContext
= getPrimaryContext();
1362 if (PrimaryContext
!= this)
1363 return PrimaryContext
->noload_lookup(Name
);
1365 StoredDeclsMap
*Map
= LookupPtr
.getPointer();
1366 if (LookupPtr
.getInt()) {
1367 // Carefully build the lookup map, without deserializing anything.
1368 SmallVector
<DeclContext
*, 2> Contexts
;
1369 collectAllContexts(Contexts
);
1370 for (unsigned I
= 0, N
= Contexts
.size(); I
!= N
; ++I
)
1371 buildLookupImpl
<&DeclContext::noload_decls_begin
,
1372 &DeclContext::noload_decls_end
>(Contexts
[I
]);
1374 // We no longer have any lazy decls.
1375 LookupPtr
.setInt(false);
1377 // There may now be names for which we have local decls but are
1378 // missing the external decls. FIXME: Just set the hasExternalDecls
1379 // flag on those names that have external decls.
1380 NeedToReconcileExternalVisibleStorage
= true;
1382 Map
= LookupPtr
.getPointer();
1386 return lookup_result(lookup_iterator(nullptr), lookup_iterator(nullptr));
1388 StoredDeclsMap::iterator I
= Map
->find(Name
);
1389 return I
!= Map
->end() ? I
->second
.getLookupResult()
1390 : lookup_result(lookup_iterator(nullptr),
1391 lookup_iterator(nullptr));
1394 void DeclContext::localUncachedLookup(DeclarationName Name
,
1395 SmallVectorImpl
<NamedDecl
*> &Results
) {
1398 // If there's no external storage, just perform a normal lookup and copy
1400 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name
) {
1401 lookup_result LookupResults
= lookup(Name
);
1402 Results
.insert(Results
.end(), LookupResults
.begin(), LookupResults
.end());
1406 // If we have a lookup table, check there first. Maybe we'll get lucky.
1407 if (Name
&& !LookupPtr
.getInt()) {
1408 if (StoredDeclsMap
*Map
= LookupPtr
.getPointer()) {
1409 StoredDeclsMap::iterator Pos
= Map
->find(Name
);
1410 if (Pos
!= Map
->end()) {
1411 Results
.insert(Results
.end(),
1412 Pos
->second
.getLookupResult().begin(),
1413 Pos
->second
.getLookupResult().end());
1419 // Slow case: grovel through the declarations in our chain looking for
1421 for (Decl
*D
= FirstDecl
; D
; D
= D
->getNextDeclInContext()) {
1422 if (NamedDecl
*ND
= dyn_cast
<NamedDecl
>(D
))
1423 if (ND
->getDeclName() == Name
)
1424 Results
.push_back(ND
);
1428 DeclContext
*DeclContext::getRedeclContext() {
1429 DeclContext
*Ctx
= this;
1430 // Skip through transparent contexts.
1431 while (Ctx
->isTransparentContext())
1432 Ctx
= Ctx
->getParent();
1436 DeclContext
*DeclContext::getEnclosingNamespaceContext() {
1437 DeclContext
*Ctx
= this;
1438 // Skip through non-namespace, non-translation-unit contexts.
1439 while (!Ctx
->isFileContext())
1440 Ctx
= Ctx
->getParent();
1441 return Ctx
->getPrimaryContext();
1444 RecordDecl
*DeclContext::getOuterLexicalRecordContext() {
1445 // Loop until we find a non-record context.
1446 RecordDecl
*OutermostRD
= nullptr;
1447 DeclContext
*DC
= this;
1448 while (DC
->isRecord()) {
1449 OutermostRD
= cast
<RecordDecl
>(DC
);
1450 DC
= DC
->getLexicalParent();
1455 bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext
*O
) const {
1456 // For non-file contexts, this is equivalent to Equals.
1457 if (!isFileContext())
1458 return O
->Equals(this);
1461 if (O
->Equals(this))
1464 const NamespaceDecl
*NS
= dyn_cast
<NamespaceDecl
>(O
);
1465 if (!NS
|| !NS
->isInline())
1467 O
= NS
->getParent();
1473 void DeclContext::makeDeclVisibleInContext(NamedDecl
*D
) {
1474 DeclContext
*PrimaryDC
= this->getPrimaryContext();
1475 DeclContext
*DeclDC
= D
->getDeclContext()->getPrimaryContext();
1476 // If the decl is being added outside of its semantic decl context, we
1477 // need to ensure that we eagerly build the lookup information for it.
1478 PrimaryDC
->makeDeclVisibleInContextWithFlags(D
, false, PrimaryDC
== DeclDC
);
1481 void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl
*D
, bool Internal
,
1483 assert(this == getPrimaryContext() && "expected a primary DC");
1485 // Skip declarations within functions.
1486 if (isFunctionOrMethod())
1489 // Skip declarations which should be invisible to name lookup.
1490 if (shouldBeHidden(D
))
1493 // If we already have a lookup data structure, perform the insertion into
1494 // it. If we might have externally-stored decls with this name, look them
1495 // up and perform the insertion. If this decl was declared outside its
1496 // semantic context, buildLookup won't add it, so add it now.
1498 // FIXME: As a performance hack, don't add such decls into the translation
1499 // unit unless we're in C++, since qualified lookup into the TU is never
1501 if (LookupPtr
.getPointer() || hasExternalVisibleStorage() ||
1502 ((!Recoverable
|| D
->getDeclContext() != D
->getLexicalDeclContext()) &&
1503 (getParentASTContext().getLangOpts().CPlusPlus
||
1504 !isTranslationUnit()))) {
1505 // If we have lazily omitted any decls, they might have the same name as
1506 // the decl which we are adding, so build a full lookup table before adding
1509 makeDeclVisibleInContextImpl(D
, Internal
);
1511 LookupPtr
.setInt(true);
1514 // If we are a transparent context or inline namespace, insert into our
1515 // parent context, too. This operation is recursive.
1516 if (isTransparentContext() || isInlineNamespace())
1517 getParent()->getPrimaryContext()->
1518 makeDeclVisibleInContextWithFlags(D
, Internal
, Recoverable
);
1520 Decl
*DCAsDecl
= cast
<Decl
>(this);
1521 // Notify that a decl was made visible unless we are a Tag being defined.
1522 if (!(isa
<TagDecl
>(DCAsDecl
) && cast
<TagDecl
>(DCAsDecl
)->isBeingDefined()))
1523 if (ASTMutationListener
*L
= DCAsDecl
->getASTMutationListener())
1524 L
->AddedVisibleDecl(this, D
);
1527 void DeclContext::makeDeclVisibleInContextImpl(NamedDecl
*D
, bool Internal
) {
1528 // Find or create the stored declaration map.
1529 StoredDeclsMap
*Map
= LookupPtr
.getPointer();
1531 ASTContext
*C
= &getParentASTContext();
1532 Map
= CreateStoredDeclsMap(*C
);
1535 // If there is an external AST source, load any declarations it knows about
1536 // with this declaration's name.
1537 // If the lookup table contains an entry about this name it means that we
1538 // have already checked the external source.
1540 if (ExternalASTSource
*Source
= getParentASTContext().getExternalSource())
1541 if (hasExternalVisibleStorage() &&
1542 Map
->find(D
->getDeclName()) == Map
->end())
1543 Source
->FindExternalVisibleDeclsByName(this, D
->getDeclName());
1545 // Insert this declaration into the map.
1546 StoredDeclsList
&DeclNameEntries
= (*Map
)[D
->getDeclName()];
1549 // If this is being added as part of loading an external declaration,
1550 // this may not be the only external declaration with this name.
1551 // In this case, we never try to replace an existing declaration; we'll
1552 // handle that when we finalize the list of declarations for this name.
1553 DeclNameEntries
.setHasExternalDecls();
1554 DeclNameEntries
.AddSubsequentDecl(D
);
1558 else if (DeclNameEntries
.isNull()) {
1559 DeclNameEntries
.setOnlyValue(D
);
1563 if (DeclNameEntries
.HandleRedeclaration(D
)) {
1564 // This declaration has replaced an existing one for which
1565 // declarationReplaces returns true.
1569 // Put this declaration into the appropriate slot.
1570 DeclNameEntries
.AddSubsequentDecl(D
);
1573 /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1575 DeclContext::udir_range
DeclContext::using_directives() const {
1576 // FIXME: Use something more efficient than normal lookup for using
1577 // directives. In C++, using directives are looked up more than anything else.
1578 lookup_const_result Result
= lookup(UsingDirectiveDecl::getName());
1580 reinterpret_cast<UsingDirectiveDecl
*const *>(Result
.begin()),
1581 reinterpret_cast<UsingDirectiveDecl
*const *>(Result
.end()));
1584 //===----------------------------------------------------------------------===//
1585 // Creation and Destruction of StoredDeclsMaps. //
1586 //===----------------------------------------------------------------------===//
1588 StoredDeclsMap
*DeclContext::CreateStoredDeclsMap(ASTContext
&C
) const {
1589 assert(!LookupPtr
.getPointer() && "context already has a decls map");
1590 assert(getPrimaryContext() == this &&
1591 "creating decls map on non-primary context");
1594 bool Dependent
= isDependentContext();
1596 M
= new DependentStoredDeclsMap();
1598 M
= new StoredDeclsMap();
1599 M
->Previous
= C
.LastSDM
;
1600 C
.LastSDM
= llvm::PointerIntPair
<StoredDeclsMap
*,1>(M
, Dependent
);
1601 LookupPtr
.setPointer(M
);
1605 void ASTContext::ReleaseDeclContextMaps() {
1606 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1607 // pointer because the subclass doesn't add anything that needs to
1609 StoredDeclsMap::DestroyAll(LastSDM
.getPointer(), LastSDM
.getInt());
1612 void StoredDeclsMap::DestroyAll(StoredDeclsMap
*Map
, bool Dependent
) {
1614 // Advance the iteration before we invalidate memory.
1615 llvm::PointerIntPair
<StoredDeclsMap
*,1> Next
= Map
->Previous
;
1618 delete static_cast<DependentStoredDeclsMap
*>(Map
);
1622 Map
= Next
.getPointer();
1623 Dependent
= Next
.getInt();
1627 DependentDiagnostic
*DependentDiagnostic::Create(ASTContext
&C
,
1628 DeclContext
*Parent
,
1629 const PartialDiagnostic
&PDiag
) {
1630 assert(Parent
->isDependentContext()
1631 && "cannot iterate dependent diagnostics of non-dependent context");
1632 Parent
= Parent
->getPrimaryContext();
1633 if (!Parent
->LookupPtr
.getPointer())
1634 Parent
->CreateStoredDeclsMap(C
);
1636 DependentStoredDeclsMap
*Map
1637 = static_cast<DependentStoredDeclsMap
*>(Parent
->LookupPtr
.getPointer());
1639 // Allocate the copy of the PartialDiagnostic via the ASTContext's
1640 // BumpPtrAllocator, rather than the ASTContext itself.
1641 PartialDiagnostic::Storage
*DiagStorage
= nullptr;
1642 if (PDiag
.hasStorage())
1643 DiagStorage
= new (C
) PartialDiagnostic::Storage
;
1645 DependentDiagnostic
*DD
= new (C
) DependentDiagnostic(PDiag
, DiagStorage
);
1647 // TODO: Maybe we shouldn't reverse the order during insertion.
1648 DD
->NextDiagnostic
= Map
->FirstDiagnostic
;
1649 Map
->FirstDiagnostic
= DD
;