17330f729Sjoerg //===- DeclBase.cpp - Declaration AST Node Implementation -----------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file implements the Decl and DeclContext classes.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/AST/DeclBase.h"
147330f729Sjoerg #include "clang/AST/ASTContext.h"
157330f729Sjoerg #include "clang/AST/ASTLambda.h"
167330f729Sjoerg #include "clang/AST/ASTMutationListener.h"
177330f729Sjoerg #include "clang/AST/Attr.h"
187330f729Sjoerg #include "clang/AST/AttrIterator.h"
197330f729Sjoerg #include "clang/AST/Decl.h"
207330f729Sjoerg #include "clang/AST/DeclCXX.h"
217330f729Sjoerg #include "clang/AST/DeclContextInternals.h"
227330f729Sjoerg #include "clang/AST/DeclFriend.h"
237330f729Sjoerg #include "clang/AST/DeclObjC.h"
247330f729Sjoerg #include "clang/AST/DeclOpenMP.h"
257330f729Sjoerg #include "clang/AST/DeclTemplate.h"
267330f729Sjoerg #include "clang/AST/DependentDiagnostic.h"
277330f729Sjoerg #include "clang/AST/ExternalASTSource.h"
287330f729Sjoerg #include "clang/AST/Stmt.h"
297330f729Sjoerg #include "clang/AST/Type.h"
307330f729Sjoerg #include "clang/Basic/IdentifierTable.h"
317330f729Sjoerg #include "clang/Basic/LLVM.h"
327330f729Sjoerg #include "clang/Basic/LangOptions.h"
337330f729Sjoerg #include "clang/Basic/ObjCRuntime.h"
347330f729Sjoerg #include "clang/Basic/PartialDiagnostic.h"
357330f729Sjoerg #include "clang/Basic/SourceLocation.h"
367330f729Sjoerg #include "clang/Basic/TargetInfo.h"
377330f729Sjoerg #include "llvm/ADT/ArrayRef.h"
387330f729Sjoerg #include "llvm/ADT/PointerIntPair.h"
397330f729Sjoerg #include "llvm/ADT/SmallVector.h"
407330f729Sjoerg #include "llvm/ADT/StringRef.h"
417330f729Sjoerg #include "llvm/Support/Casting.h"
427330f729Sjoerg #include "llvm/Support/ErrorHandling.h"
437330f729Sjoerg #include "llvm/Support/MathExtras.h"
447330f729Sjoerg #include "llvm/Support/VersionTuple.h"
457330f729Sjoerg #include "llvm/Support/raw_ostream.h"
467330f729Sjoerg #include <algorithm>
477330f729Sjoerg #include <cassert>
487330f729Sjoerg #include <cstddef>
497330f729Sjoerg #include <string>
507330f729Sjoerg #include <tuple>
517330f729Sjoerg #include <utility>
527330f729Sjoerg
537330f729Sjoerg using namespace clang;
547330f729Sjoerg
557330f729Sjoerg //===----------------------------------------------------------------------===//
567330f729Sjoerg // Statistics
577330f729Sjoerg //===----------------------------------------------------------------------===//
587330f729Sjoerg
597330f729Sjoerg #define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
607330f729Sjoerg #define ABSTRACT_DECL(DECL)
617330f729Sjoerg #include "clang/AST/DeclNodes.inc"
627330f729Sjoerg
updateOutOfDate(IdentifierInfo & II) const637330f729Sjoerg void Decl::updateOutOfDate(IdentifierInfo &II) const {
647330f729Sjoerg getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
657330f729Sjoerg }
667330f729Sjoerg
677330f729Sjoerg #define DECL(DERIVED, BASE) \
687330f729Sjoerg static_assert(alignof(Decl) >= alignof(DERIVED##Decl), \
697330f729Sjoerg "Alignment sufficient after objects prepended to " #DERIVED);
707330f729Sjoerg #define ABSTRACT_DECL(DECL)
717330f729Sjoerg #include "clang/AST/DeclNodes.inc"
727330f729Sjoerg
operator new(std::size_t Size,const ASTContext & Context,unsigned ID,std::size_t Extra)737330f729Sjoerg void *Decl::operator new(std::size_t Size, const ASTContext &Context,
747330f729Sjoerg unsigned ID, std::size_t Extra) {
757330f729Sjoerg // Allocate an extra 8 bytes worth of storage, which ensures that the
767330f729Sjoerg // resulting pointer will still be 8-byte aligned.
777330f729Sjoerg static_assert(sizeof(unsigned) * 2 >= alignof(Decl),
787330f729Sjoerg "Decl won't be misaligned");
797330f729Sjoerg void *Start = Context.Allocate(Size + Extra + 8);
807330f729Sjoerg void *Result = (char*)Start + 8;
817330f729Sjoerg
827330f729Sjoerg unsigned *PrefixPtr = (unsigned *)Result - 2;
837330f729Sjoerg
847330f729Sjoerg // Zero out the first 4 bytes; this is used to store the owning module ID.
857330f729Sjoerg PrefixPtr[0] = 0;
867330f729Sjoerg
877330f729Sjoerg // Store the global declaration ID in the second 4 bytes.
887330f729Sjoerg PrefixPtr[1] = ID;
897330f729Sjoerg
907330f729Sjoerg return Result;
917330f729Sjoerg }
927330f729Sjoerg
operator new(std::size_t Size,const ASTContext & Ctx,DeclContext * Parent,std::size_t Extra)937330f729Sjoerg void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
947330f729Sjoerg DeclContext *Parent, std::size_t Extra) {
957330f729Sjoerg assert(!Parent || &Parent->getParentASTContext() == &Ctx);
967330f729Sjoerg // With local visibility enabled, we track the owning module even for local
977330f729Sjoerg // declarations. We create the TU decl early and may not yet know what the
987330f729Sjoerg // LangOpts are, so conservatively allocate the storage.
997330f729Sjoerg if (Ctx.getLangOpts().trackLocalOwningModule() || !Parent) {
1007330f729Sjoerg // Ensure required alignment of the resulting object by adding extra
1017330f729Sjoerg // padding at the start if required.
1027330f729Sjoerg size_t ExtraAlign =
1037330f729Sjoerg llvm::offsetToAlignment(sizeof(Module *), llvm::Align(alignof(Decl)));
1047330f729Sjoerg auto *Buffer = reinterpret_cast<char *>(
1057330f729Sjoerg ::operator new(ExtraAlign + sizeof(Module *) + Size + Extra, Ctx));
1067330f729Sjoerg Buffer += ExtraAlign;
1077330f729Sjoerg auto *ParentModule =
1087330f729Sjoerg Parent ? cast<Decl>(Parent)->getOwningModule() : nullptr;
1097330f729Sjoerg return new (Buffer) Module*(ParentModule) + 1;
1107330f729Sjoerg }
1117330f729Sjoerg return ::operator new(Size + Extra, Ctx);
1127330f729Sjoerg }
1137330f729Sjoerg
getOwningModuleSlow() const1147330f729Sjoerg Module *Decl::getOwningModuleSlow() const {
1157330f729Sjoerg assert(isFromASTFile() && "Not from AST file?");
1167330f729Sjoerg return getASTContext().getExternalSource()->getModule(getOwningModuleID());
1177330f729Sjoerg }
1187330f729Sjoerg
hasLocalOwningModuleStorage() const1197330f729Sjoerg bool Decl::hasLocalOwningModuleStorage() const {
1207330f729Sjoerg return getASTContext().getLangOpts().trackLocalOwningModule();
1217330f729Sjoerg }
1227330f729Sjoerg
getDeclKindName() const1237330f729Sjoerg const char *Decl::getDeclKindName() const {
1247330f729Sjoerg switch (DeclKind) {
1257330f729Sjoerg default: llvm_unreachable("Declaration not in DeclNodes.inc!");
1267330f729Sjoerg #define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
1277330f729Sjoerg #define ABSTRACT_DECL(DECL)
1287330f729Sjoerg #include "clang/AST/DeclNodes.inc"
1297330f729Sjoerg }
1307330f729Sjoerg }
1317330f729Sjoerg
setInvalidDecl(bool Invalid)1327330f729Sjoerg void Decl::setInvalidDecl(bool Invalid) {
1337330f729Sjoerg InvalidDecl = Invalid;
1347330f729Sjoerg assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
1357330f729Sjoerg if (!Invalid) {
1367330f729Sjoerg return;
1377330f729Sjoerg }
1387330f729Sjoerg
1397330f729Sjoerg if (!isa<ParmVarDecl>(this)) {
1407330f729Sjoerg // Defensive maneuver for ill-formed code: we're likely not to make it to
1417330f729Sjoerg // a point where we set the access specifier, so default it to "public"
1427330f729Sjoerg // to avoid triggering asserts elsewhere in the front end.
1437330f729Sjoerg setAccess(AS_public);
1447330f729Sjoerg }
1457330f729Sjoerg
1467330f729Sjoerg // Marking a DecompositionDecl as invalid implies all the child BindingDecl's
1477330f729Sjoerg // are invalid too.
1487330f729Sjoerg if (auto *DD = dyn_cast<DecompositionDecl>(this)) {
1497330f729Sjoerg for (auto *Binding : DD->bindings()) {
1507330f729Sjoerg Binding->setInvalidDecl();
1517330f729Sjoerg }
1527330f729Sjoerg }
1537330f729Sjoerg }
1547330f729Sjoerg
getDeclKindName() const1557330f729Sjoerg const char *DeclContext::getDeclKindName() const {
1567330f729Sjoerg switch (getDeclKind()) {
1577330f729Sjoerg #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
1587330f729Sjoerg #define ABSTRACT_DECL(DECL)
1597330f729Sjoerg #include "clang/AST/DeclNodes.inc"
1607330f729Sjoerg }
1617330f729Sjoerg llvm_unreachable("Declaration context not in DeclNodes.inc!");
1627330f729Sjoerg }
1637330f729Sjoerg
1647330f729Sjoerg bool Decl::StatisticsEnabled = false;
EnableStatistics()1657330f729Sjoerg void Decl::EnableStatistics() {
1667330f729Sjoerg StatisticsEnabled = true;
1677330f729Sjoerg }
1687330f729Sjoerg
PrintStats()1697330f729Sjoerg void Decl::PrintStats() {
1707330f729Sjoerg llvm::errs() << "\n*** Decl Stats:\n";
1717330f729Sjoerg
1727330f729Sjoerg int totalDecls = 0;
1737330f729Sjoerg #define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
1747330f729Sjoerg #define ABSTRACT_DECL(DECL)
1757330f729Sjoerg #include "clang/AST/DeclNodes.inc"
1767330f729Sjoerg llvm::errs() << " " << totalDecls << " decls total.\n";
1777330f729Sjoerg
1787330f729Sjoerg int totalBytes = 0;
1797330f729Sjoerg #define DECL(DERIVED, BASE) \
1807330f729Sjoerg if (n##DERIVED##s > 0) { \
1817330f729Sjoerg totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
1827330f729Sjoerg llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
1837330f729Sjoerg << sizeof(DERIVED##Decl) << " each (" \
1847330f729Sjoerg << n##DERIVED##s * sizeof(DERIVED##Decl) \
1857330f729Sjoerg << " bytes)\n"; \
1867330f729Sjoerg }
1877330f729Sjoerg #define ABSTRACT_DECL(DECL)
1887330f729Sjoerg #include "clang/AST/DeclNodes.inc"
1897330f729Sjoerg
1907330f729Sjoerg llvm::errs() << "Total bytes = " << totalBytes << "\n";
1917330f729Sjoerg }
1927330f729Sjoerg
add(Kind k)1937330f729Sjoerg void Decl::add(Kind k) {
1947330f729Sjoerg switch (k) {
1957330f729Sjoerg #define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
1967330f729Sjoerg #define ABSTRACT_DECL(DECL)
1977330f729Sjoerg #include "clang/AST/DeclNodes.inc"
1987330f729Sjoerg }
1997330f729Sjoerg }
2007330f729Sjoerg
isTemplateParameterPack() const2017330f729Sjoerg bool Decl::isTemplateParameterPack() const {
2027330f729Sjoerg if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(this))
2037330f729Sjoerg return TTP->isParameterPack();
2047330f729Sjoerg if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(this))
2057330f729Sjoerg return NTTP->isParameterPack();
2067330f729Sjoerg if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(this))
2077330f729Sjoerg return TTP->isParameterPack();
2087330f729Sjoerg return false;
2097330f729Sjoerg }
2107330f729Sjoerg
isParameterPack() const2117330f729Sjoerg bool Decl::isParameterPack() const {
2127330f729Sjoerg if (const auto *Var = dyn_cast<VarDecl>(this))
2137330f729Sjoerg return Var->isParameterPack();
2147330f729Sjoerg
2157330f729Sjoerg return isTemplateParameterPack();
2167330f729Sjoerg }
2177330f729Sjoerg
getAsFunction()2187330f729Sjoerg FunctionDecl *Decl::getAsFunction() {
2197330f729Sjoerg if (auto *FD = dyn_cast<FunctionDecl>(this))
2207330f729Sjoerg return FD;
2217330f729Sjoerg if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
2227330f729Sjoerg return FTD->getTemplatedDecl();
2237330f729Sjoerg return nullptr;
2247330f729Sjoerg }
2257330f729Sjoerg
isTemplateDecl() const2267330f729Sjoerg bool Decl::isTemplateDecl() const {
2277330f729Sjoerg return isa<TemplateDecl>(this);
2287330f729Sjoerg }
2297330f729Sjoerg
getDescribedTemplate() const2307330f729Sjoerg TemplateDecl *Decl::getDescribedTemplate() const {
2317330f729Sjoerg if (auto *FD = dyn_cast<FunctionDecl>(this))
2327330f729Sjoerg return FD->getDescribedFunctionTemplate();
233*e038c9c4Sjoerg if (auto *RD = dyn_cast<CXXRecordDecl>(this))
2347330f729Sjoerg return RD->getDescribedClassTemplate();
235*e038c9c4Sjoerg if (auto *VD = dyn_cast<VarDecl>(this))
2367330f729Sjoerg return VD->getDescribedVarTemplate();
237*e038c9c4Sjoerg if (auto *AD = dyn_cast<TypeAliasDecl>(this))
2387330f729Sjoerg return AD->getDescribedAliasTemplate();
2397330f729Sjoerg
2407330f729Sjoerg return nullptr;
2417330f729Sjoerg }
2427330f729Sjoerg
getDescribedTemplateParams() const243*e038c9c4Sjoerg const TemplateParameterList *Decl::getDescribedTemplateParams() const {
244*e038c9c4Sjoerg if (auto *TD = getDescribedTemplate())
245*e038c9c4Sjoerg return TD->getTemplateParameters();
246*e038c9c4Sjoerg if (auto *CTPSD = dyn_cast<ClassTemplatePartialSpecializationDecl>(this))
247*e038c9c4Sjoerg return CTPSD->getTemplateParameters();
248*e038c9c4Sjoerg if (auto *VTPSD = dyn_cast<VarTemplatePartialSpecializationDecl>(this))
249*e038c9c4Sjoerg return VTPSD->getTemplateParameters();
250*e038c9c4Sjoerg return nullptr;
251*e038c9c4Sjoerg }
252*e038c9c4Sjoerg
isTemplated() const2537330f729Sjoerg bool Decl::isTemplated() const {
254*e038c9c4Sjoerg // A declaration is templated if it is a template or a template pattern, or
2557330f729Sjoerg // is within (lexcially for a friend, semantically otherwise) a dependent
2567330f729Sjoerg // context.
2577330f729Sjoerg // FIXME: Should local extern declarations be treated like friends?
2587330f729Sjoerg if (auto *AsDC = dyn_cast<DeclContext>(this))
2597330f729Sjoerg return AsDC->isDependentContext();
2607330f729Sjoerg auto *DC = getFriendObjectKind() ? getLexicalDeclContext() : getDeclContext();
261*e038c9c4Sjoerg return DC->isDependentContext() || isTemplateDecl() ||
262*e038c9c4Sjoerg getDescribedTemplateParams();
263*e038c9c4Sjoerg }
264*e038c9c4Sjoerg
getTemplateDepth() const265*e038c9c4Sjoerg unsigned Decl::getTemplateDepth() const {
266*e038c9c4Sjoerg if (auto *DC = dyn_cast<DeclContext>(this))
267*e038c9c4Sjoerg if (DC->isFileContext())
268*e038c9c4Sjoerg return 0;
269*e038c9c4Sjoerg
270*e038c9c4Sjoerg if (auto *TPL = getDescribedTemplateParams())
271*e038c9c4Sjoerg return TPL->getDepth() + 1;
272*e038c9c4Sjoerg
273*e038c9c4Sjoerg // If this is a dependent lambda, there might be an enclosing variable
274*e038c9c4Sjoerg // template. In this case, the next step is not the parent DeclContext (or
275*e038c9c4Sjoerg // even a DeclContext at all).
276*e038c9c4Sjoerg auto *RD = dyn_cast<CXXRecordDecl>(this);
277*e038c9c4Sjoerg if (RD && RD->isDependentLambda())
278*e038c9c4Sjoerg if (Decl *Context = RD->getLambdaContextDecl())
279*e038c9c4Sjoerg return Context->getTemplateDepth();
280*e038c9c4Sjoerg
281*e038c9c4Sjoerg const DeclContext *DC =
282*e038c9c4Sjoerg getFriendObjectKind() ? getLexicalDeclContext() : getDeclContext();
283*e038c9c4Sjoerg return cast<Decl>(DC)->getTemplateDepth();
2847330f729Sjoerg }
2857330f729Sjoerg
getParentFunctionOrMethod() const2867330f729Sjoerg const DeclContext *Decl::getParentFunctionOrMethod() const {
2877330f729Sjoerg for (const DeclContext *DC = getDeclContext();
2887330f729Sjoerg DC && !DC->isTranslationUnit() && !DC->isNamespace();
2897330f729Sjoerg DC = DC->getParent())
2907330f729Sjoerg if (DC->isFunctionOrMethod())
2917330f729Sjoerg return DC;
2927330f729Sjoerg
2937330f729Sjoerg return nullptr;
2947330f729Sjoerg }
2957330f729Sjoerg
2967330f729Sjoerg //===----------------------------------------------------------------------===//
2977330f729Sjoerg // PrettyStackTraceDecl Implementation
2987330f729Sjoerg //===----------------------------------------------------------------------===//
2997330f729Sjoerg
print(raw_ostream & OS) const3007330f729Sjoerg void PrettyStackTraceDecl::print(raw_ostream &OS) const {
3017330f729Sjoerg SourceLocation TheLoc = Loc;
3027330f729Sjoerg if (TheLoc.isInvalid() && TheDecl)
3037330f729Sjoerg TheLoc = TheDecl->getLocation();
3047330f729Sjoerg
3057330f729Sjoerg if (TheLoc.isValid()) {
3067330f729Sjoerg TheLoc.print(OS, SM);
3077330f729Sjoerg OS << ": ";
3087330f729Sjoerg }
3097330f729Sjoerg
3107330f729Sjoerg OS << Message;
3117330f729Sjoerg
3127330f729Sjoerg if (const auto *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
3137330f729Sjoerg OS << " '";
3147330f729Sjoerg DN->printQualifiedName(OS);
3157330f729Sjoerg OS << '\'';
3167330f729Sjoerg }
3177330f729Sjoerg OS << '\n';
3187330f729Sjoerg }
3197330f729Sjoerg
3207330f729Sjoerg //===----------------------------------------------------------------------===//
3217330f729Sjoerg // Decl Implementation
3227330f729Sjoerg //===----------------------------------------------------------------------===//
3237330f729Sjoerg
3247330f729Sjoerg // Out-of-line virtual method providing a home for Decl.
3257330f729Sjoerg Decl::~Decl() = default;
3267330f729Sjoerg
setDeclContext(DeclContext * DC)3277330f729Sjoerg void Decl::setDeclContext(DeclContext *DC) {
3287330f729Sjoerg DeclCtx = DC;
3297330f729Sjoerg }
3307330f729Sjoerg
setLexicalDeclContext(DeclContext * DC)3317330f729Sjoerg void Decl::setLexicalDeclContext(DeclContext *DC) {
3327330f729Sjoerg if (DC == getLexicalDeclContext())
3337330f729Sjoerg return;
3347330f729Sjoerg
3357330f729Sjoerg if (isInSemaDC()) {
3367330f729Sjoerg setDeclContextsImpl(getDeclContext(), DC, getASTContext());
3377330f729Sjoerg } else {
3387330f729Sjoerg getMultipleDC()->LexicalDC = DC;
3397330f729Sjoerg }
3407330f729Sjoerg
3417330f729Sjoerg // FIXME: We shouldn't be changing the lexical context of declarations
3427330f729Sjoerg // imported from AST files.
3437330f729Sjoerg if (!isFromASTFile()) {
3447330f729Sjoerg setModuleOwnershipKind(getModuleOwnershipKindForChildOf(DC));
3457330f729Sjoerg if (hasOwningModule())
3467330f729Sjoerg setLocalOwningModule(cast<Decl>(DC)->getOwningModule());
3477330f729Sjoerg }
3487330f729Sjoerg
3497330f729Sjoerg assert(
3507330f729Sjoerg (getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported ||
3517330f729Sjoerg getOwningModule()) &&
3527330f729Sjoerg "hidden declaration has no owning module");
3537330f729Sjoerg }
3547330f729Sjoerg
setDeclContextsImpl(DeclContext * SemaDC,DeclContext * LexicalDC,ASTContext & Ctx)3557330f729Sjoerg void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
3567330f729Sjoerg ASTContext &Ctx) {
3577330f729Sjoerg if (SemaDC == LexicalDC) {
3587330f729Sjoerg DeclCtx = SemaDC;
3597330f729Sjoerg } else {
3607330f729Sjoerg auto *MDC = new (Ctx) Decl::MultipleDC();
3617330f729Sjoerg MDC->SemanticDC = SemaDC;
3627330f729Sjoerg MDC->LexicalDC = LexicalDC;
3637330f729Sjoerg DeclCtx = MDC;
3647330f729Sjoerg }
3657330f729Sjoerg }
3667330f729Sjoerg
isInLocalScopeForInstantiation() const367*e038c9c4Sjoerg bool Decl::isInLocalScopeForInstantiation() const {
3687330f729Sjoerg const DeclContext *LDC = getLexicalDeclContext();
369*e038c9c4Sjoerg if (!LDC->isDependentContext())
370*e038c9c4Sjoerg return false;
3717330f729Sjoerg while (true) {
3727330f729Sjoerg if (LDC->isFunctionOrMethod())
3737330f729Sjoerg return true;
3747330f729Sjoerg if (!isa<TagDecl>(LDC))
3757330f729Sjoerg return false;
376*e038c9c4Sjoerg if (const auto *CRD = dyn_cast<CXXRecordDecl>(LDC))
377*e038c9c4Sjoerg if (CRD->isLambda())
378*e038c9c4Sjoerg return true;
3797330f729Sjoerg LDC = LDC->getLexicalParent();
3807330f729Sjoerg }
3817330f729Sjoerg return false;
3827330f729Sjoerg }
3837330f729Sjoerg
isInAnonymousNamespace() const3847330f729Sjoerg bool Decl::isInAnonymousNamespace() const {
3857330f729Sjoerg for (const DeclContext *DC = getDeclContext(); DC; DC = DC->getParent()) {
3867330f729Sjoerg if (const auto *ND = dyn_cast<NamespaceDecl>(DC))
3877330f729Sjoerg if (ND->isAnonymousNamespace())
3887330f729Sjoerg return true;
3897330f729Sjoerg }
3907330f729Sjoerg
3917330f729Sjoerg return false;
3927330f729Sjoerg }
3937330f729Sjoerg
isInStdNamespace() const3947330f729Sjoerg bool Decl::isInStdNamespace() const {
3957330f729Sjoerg const DeclContext *DC = getDeclContext();
3967330f729Sjoerg return DC && DC->isStdNamespace();
3977330f729Sjoerg }
3987330f729Sjoerg
getTranslationUnitDecl()3997330f729Sjoerg TranslationUnitDecl *Decl::getTranslationUnitDecl() {
4007330f729Sjoerg if (auto *TUD = dyn_cast<TranslationUnitDecl>(this))
4017330f729Sjoerg return TUD;
4027330f729Sjoerg
4037330f729Sjoerg DeclContext *DC = getDeclContext();
4047330f729Sjoerg assert(DC && "This decl is not contained in a translation unit!");
4057330f729Sjoerg
4067330f729Sjoerg while (!DC->isTranslationUnit()) {
4077330f729Sjoerg DC = DC->getParent();
4087330f729Sjoerg assert(DC && "This decl is not contained in a translation unit!");
4097330f729Sjoerg }
4107330f729Sjoerg
4117330f729Sjoerg return cast<TranslationUnitDecl>(DC);
4127330f729Sjoerg }
4137330f729Sjoerg
getASTContext() const4147330f729Sjoerg ASTContext &Decl::getASTContext() const {
4157330f729Sjoerg return getTranslationUnitDecl()->getASTContext();
4167330f729Sjoerg }
4177330f729Sjoerg
418*e038c9c4Sjoerg /// Helper to get the language options from the ASTContext.
419*e038c9c4Sjoerg /// Defined out of line to avoid depending on ASTContext.h.
getLangOpts() const420*e038c9c4Sjoerg const LangOptions &Decl::getLangOpts() const {
421*e038c9c4Sjoerg return getASTContext().getLangOpts();
422*e038c9c4Sjoerg }
423*e038c9c4Sjoerg
getASTMutationListener() const4247330f729Sjoerg ASTMutationListener *Decl::getASTMutationListener() const {
4257330f729Sjoerg return getASTContext().getASTMutationListener();
4267330f729Sjoerg }
4277330f729Sjoerg
getMaxAlignment() const4287330f729Sjoerg unsigned Decl::getMaxAlignment() const {
4297330f729Sjoerg if (!hasAttrs())
4307330f729Sjoerg return 0;
4317330f729Sjoerg
4327330f729Sjoerg unsigned Align = 0;
4337330f729Sjoerg const AttrVec &V = getAttrs();
4347330f729Sjoerg ASTContext &Ctx = getASTContext();
4357330f729Sjoerg specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
436*e038c9c4Sjoerg for (; I != E; ++I) {
437*e038c9c4Sjoerg if (!I->isAlignmentErrorDependent())
4387330f729Sjoerg Align = std::max(Align, I->getAlignment(Ctx));
439*e038c9c4Sjoerg }
4407330f729Sjoerg return Align;
4417330f729Sjoerg }
4427330f729Sjoerg
isUsed(bool CheckUsedAttr) const4437330f729Sjoerg bool Decl::isUsed(bool CheckUsedAttr) const {
4447330f729Sjoerg const Decl *CanonD = getCanonicalDecl();
4457330f729Sjoerg if (CanonD->Used)
4467330f729Sjoerg return true;
4477330f729Sjoerg
4487330f729Sjoerg // Check for used attribute.
4497330f729Sjoerg // Ask the most recent decl, since attributes accumulate in the redecl chain.
4507330f729Sjoerg if (CheckUsedAttr && getMostRecentDecl()->hasAttr<UsedAttr>())
4517330f729Sjoerg return true;
4527330f729Sjoerg
4537330f729Sjoerg // The information may have not been deserialized yet. Force deserialization
4547330f729Sjoerg // to complete the needed information.
4557330f729Sjoerg return getMostRecentDecl()->getCanonicalDecl()->Used;
4567330f729Sjoerg }
4577330f729Sjoerg
markUsed(ASTContext & C)4587330f729Sjoerg void Decl::markUsed(ASTContext &C) {
4597330f729Sjoerg if (isUsed(false))
4607330f729Sjoerg return;
4617330f729Sjoerg
4627330f729Sjoerg if (C.getASTMutationListener())
4637330f729Sjoerg C.getASTMutationListener()->DeclarationMarkedUsed(this);
4647330f729Sjoerg
4657330f729Sjoerg setIsUsed();
4667330f729Sjoerg }
4677330f729Sjoerg
isReferenced() const4687330f729Sjoerg bool Decl::isReferenced() const {
4697330f729Sjoerg if (Referenced)
4707330f729Sjoerg return true;
4717330f729Sjoerg
4727330f729Sjoerg // Check redeclarations.
4737330f729Sjoerg for (const auto *I : redecls())
4747330f729Sjoerg if (I->Referenced)
4757330f729Sjoerg return true;
4767330f729Sjoerg
4777330f729Sjoerg return false;
4787330f729Sjoerg }
4797330f729Sjoerg
getExternalSourceSymbolAttr() const4807330f729Sjoerg ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const {
4817330f729Sjoerg const Decl *Definition = nullptr;
4827330f729Sjoerg if (auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
4837330f729Sjoerg Definition = ID->getDefinition();
4847330f729Sjoerg } else if (auto *PD = dyn_cast<ObjCProtocolDecl>(this)) {
4857330f729Sjoerg Definition = PD->getDefinition();
4867330f729Sjoerg } else if (auto *TD = dyn_cast<TagDecl>(this)) {
4877330f729Sjoerg Definition = TD->getDefinition();
4887330f729Sjoerg }
4897330f729Sjoerg if (!Definition)
4907330f729Sjoerg Definition = this;
4917330f729Sjoerg
4927330f729Sjoerg if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>())
4937330f729Sjoerg return attr;
4947330f729Sjoerg if (auto *dcd = dyn_cast<Decl>(getDeclContext())) {
4957330f729Sjoerg return dcd->getAttr<ExternalSourceSymbolAttr>();
4967330f729Sjoerg }
4977330f729Sjoerg
4987330f729Sjoerg return nullptr;
4997330f729Sjoerg }
5007330f729Sjoerg
hasDefiningAttr() const5017330f729Sjoerg bool Decl::hasDefiningAttr() const {
502*e038c9c4Sjoerg return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>() ||
503*e038c9c4Sjoerg hasAttr<LoaderUninitializedAttr>();
5047330f729Sjoerg }
5057330f729Sjoerg
getDefiningAttr() const5067330f729Sjoerg const Attr *Decl::getDefiningAttr() const {
5077330f729Sjoerg if (auto *AA = getAttr<AliasAttr>())
5087330f729Sjoerg return AA;
5097330f729Sjoerg if (auto *IFA = getAttr<IFuncAttr>())
5107330f729Sjoerg return IFA;
511*e038c9c4Sjoerg if (auto *NZA = getAttr<LoaderUninitializedAttr>())
512*e038c9c4Sjoerg return NZA;
5137330f729Sjoerg return nullptr;
5147330f729Sjoerg }
5157330f729Sjoerg
getRealizedPlatform(const AvailabilityAttr * A,const ASTContext & Context)5167330f729Sjoerg static StringRef getRealizedPlatform(const AvailabilityAttr *A,
5177330f729Sjoerg const ASTContext &Context) {
5187330f729Sjoerg // Check if this is an App Extension "platform", and if so chop off
5197330f729Sjoerg // the suffix for matching with the actual platform.
5207330f729Sjoerg StringRef RealizedPlatform = A->getPlatform()->getName();
5217330f729Sjoerg if (!Context.getLangOpts().AppExt)
5227330f729Sjoerg return RealizedPlatform;
5237330f729Sjoerg size_t suffix = RealizedPlatform.rfind("_app_extension");
5247330f729Sjoerg if (suffix != StringRef::npos)
5257330f729Sjoerg return RealizedPlatform.slice(0, suffix);
5267330f729Sjoerg return RealizedPlatform;
5277330f729Sjoerg }
5287330f729Sjoerg
5297330f729Sjoerg /// Determine the availability of the given declaration based on
5307330f729Sjoerg /// the target platform.
5317330f729Sjoerg ///
5327330f729Sjoerg /// When it returns an availability result other than \c AR_Available,
5337330f729Sjoerg /// if the \p Message parameter is non-NULL, it will be set to a
5347330f729Sjoerg /// string describing why the entity is unavailable.
5357330f729Sjoerg ///
5367330f729Sjoerg /// FIXME: Make these strings localizable, since they end up in
5377330f729Sjoerg /// diagnostics.
CheckAvailability(ASTContext & Context,const AvailabilityAttr * A,std::string * Message,VersionTuple EnclosingVersion)5387330f729Sjoerg static AvailabilityResult CheckAvailability(ASTContext &Context,
5397330f729Sjoerg const AvailabilityAttr *A,
5407330f729Sjoerg std::string *Message,
5417330f729Sjoerg VersionTuple EnclosingVersion) {
5427330f729Sjoerg if (EnclosingVersion.empty())
5437330f729Sjoerg EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion();
5447330f729Sjoerg
5457330f729Sjoerg if (EnclosingVersion.empty())
5467330f729Sjoerg return AR_Available;
5477330f729Sjoerg
5487330f729Sjoerg StringRef ActualPlatform = A->getPlatform()->getName();
5497330f729Sjoerg StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
5507330f729Sjoerg
5517330f729Sjoerg // Match the platform name.
5527330f729Sjoerg if (getRealizedPlatform(A, Context) != TargetPlatform)
5537330f729Sjoerg return AR_Available;
5547330f729Sjoerg
5557330f729Sjoerg StringRef PrettyPlatformName
5567330f729Sjoerg = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
5577330f729Sjoerg
5587330f729Sjoerg if (PrettyPlatformName.empty())
5597330f729Sjoerg PrettyPlatformName = ActualPlatform;
5607330f729Sjoerg
5617330f729Sjoerg std::string HintMessage;
5627330f729Sjoerg if (!A->getMessage().empty()) {
5637330f729Sjoerg HintMessage = " - ";
5647330f729Sjoerg HintMessage += A->getMessage();
5657330f729Sjoerg }
5667330f729Sjoerg
5677330f729Sjoerg // Make sure that this declaration has not been marked 'unavailable'.
5687330f729Sjoerg if (A->getUnavailable()) {
5697330f729Sjoerg if (Message) {
5707330f729Sjoerg Message->clear();
5717330f729Sjoerg llvm::raw_string_ostream Out(*Message);
5727330f729Sjoerg Out << "not available on " << PrettyPlatformName
5737330f729Sjoerg << HintMessage;
5747330f729Sjoerg }
5757330f729Sjoerg
5767330f729Sjoerg return AR_Unavailable;
5777330f729Sjoerg }
5787330f729Sjoerg
5797330f729Sjoerg // Make sure that this declaration has already been introduced.
5807330f729Sjoerg if (!A->getIntroduced().empty() &&
5817330f729Sjoerg EnclosingVersion < A->getIntroduced()) {
5827330f729Sjoerg if (Message) {
5837330f729Sjoerg Message->clear();
5847330f729Sjoerg llvm::raw_string_ostream Out(*Message);
5857330f729Sjoerg VersionTuple VTI(A->getIntroduced());
5867330f729Sjoerg Out << "introduced in " << PrettyPlatformName << ' '
5877330f729Sjoerg << VTI << HintMessage;
5887330f729Sjoerg }
5897330f729Sjoerg
5907330f729Sjoerg return A->getStrict() ? AR_Unavailable : AR_NotYetIntroduced;
5917330f729Sjoerg }
5927330f729Sjoerg
5937330f729Sjoerg // Make sure that this declaration hasn't been obsoleted.
5947330f729Sjoerg if (!A->getObsoleted().empty() && EnclosingVersion >= A->getObsoleted()) {
5957330f729Sjoerg if (Message) {
5967330f729Sjoerg Message->clear();
5977330f729Sjoerg llvm::raw_string_ostream Out(*Message);
5987330f729Sjoerg VersionTuple VTO(A->getObsoleted());
5997330f729Sjoerg Out << "obsoleted in " << PrettyPlatformName << ' '
6007330f729Sjoerg << VTO << HintMessage;
6017330f729Sjoerg }
6027330f729Sjoerg
6037330f729Sjoerg return AR_Unavailable;
6047330f729Sjoerg }
6057330f729Sjoerg
6067330f729Sjoerg // Make sure that this declaration hasn't been deprecated.
6077330f729Sjoerg if (!A->getDeprecated().empty() && EnclosingVersion >= A->getDeprecated()) {
6087330f729Sjoerg if (Message) {
6097330f729Sjoerg Message->clear();
6107330f729Sjoerg llvm::raw_string_ostream Out(*Message);
6117330f729Sjoerg VersionTuple VTD(A->getDeprecated());
6127330f729Sjoerg Out << "first deprecated in " << PrettyPlatformName << ' '
6137330f729Sjoerg << VTD << HintMessage;
6147330f729Sjoerg }
6157330f729Sjoerg
6167330f729Sjoerg return AR_Deprecated;
6177330f729Sjoerg }
6187330f729Sjoerg
6197330f729Sjoerg return AR_Available;
6207330f729Sjoerg }
6217330f729Sjoerg
getAvailability(std::string * Message,VersionTuple EnclosingVersion,StringRef * RealizedPlatform) const6227330f729Sjoerg AvailabilityResult Decl::getAvailability(std::string *Message,
6237330f729Sjoerg VersionTuple EnclosingVersion,
6247330f729Sjoerg StringRef *RealizedPlatform) const {
6257330f729Sjoerg if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
6267330f729Sjoerg return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion,
6277330f729Sjoerg RealizedPlatform);
6287330f729Sjoerg
6297330f729Sjoerg AvailabilityResult Result = AR_Available;
6307330f729Sjoerg std::string ResultMessage;
6317330f729Sjoerg
6327330f729Sjoerg for (const auto *A : attrs()) {
6337330f729Sjoerg if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
6347330f729Sjoerg if (Result >= AR_Deprecated)
6357330f729Sjoerg continue;
6367330f729Sjoerg
6377330f729Sjoerg if (Message)
638*e038c9c4Sjoerg ResultMessage = std::string(Deprecated->getMessage());
6397330f729Sjoerg
6407330f729Sjoerg Result = AR_Deprecated;
6417330f729Sjoerg continue;
6427330f729Sjoerg }
6437330f729Sjoerg
6447330f729Sjoerg if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
6457330f729Sjoerg if (Message)
646*e038c9c4Sjoerg *Message = std::string(Unavailable->getMessage());
6477330f729Sjoerg return AR_Unavailable;
6487330f729Sjoerg }
6497330f729Sjoerg
6507330f729Sjoerg if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
6517330f729Sjoerg AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
6527330f729Sjoerg Message, EnclosingVersion);
6537330f729Sjoerg
6547330f729Sjoerg if (AR == AR_Unavailable) {
6557330f729Sjoerg if (RealizedPlatform)
6567330f729Sjoerg *RealizedPlatform = Availability->getPlatform()->getName();
6577330f729Sjoerg return AR_Unavailable;
6587330f729Sjoerg }
6597330f729Sjoerg
6607330f729Sjoerg if (AR > Result) {
6617330f729Sjoerg Result = AR;
6627330f729Sjoerg if (Message)
6637330f729Sjoerg ResultMessage.swap(*Message);
6647330f729Sjoerg }
6657330f729Sjoerg continue;
6667330f729Sjoerg }
6677330f729Sjoerg }
6687330f729Sjoerg
6697330f729Sjoerg if (Message)
6707330f729Sjoerg Message->swap(ResultMessage);
6717330f729Sjoerg return Result;
6727330f729Sjoerg }
6737330f729Sjoerg
getVersionIntroduced() const6747330f729Sjoerg VersionTuple Decl::getVersionIntroduced() const {
6757330f729Sjoerg const ASTContext &Context = getASTContext();
6767330f729Sjoerg StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
6777330f729Sjoerg for (const auto *A : attrs()) {
6787330f729Sjoerg if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
6797330f729Sjoerg if (getRealizedPlatform(Availability, Context) != TargetPlatform)
6807330f729Sjoerg continue;
6817330f729Sjoerg if (!Availability->getIntroduced().empty())
6827330f729Sjoerg return Availability->getIntroduced();
6837330f729Sjoerg }
6847330f729Sjoerg }
6857330f729Sjoerg return {};
6867330f729Sjoerg }
6877330f729Sjoerg
canBeWeakImported(bool & IsDefinition) const6887330f729Sjoerg bool Decl::canBeWeakImported(bool &IsDefinition) const {
6897330f729Sjoerg IsDefinition = false;
6907330f729Sjoerg
6917330f729Sjoerg // Variables, if they aren't definitions.
6927330f729Sjoerg if (const auto *Var = dyn_cast<VarDecl>(this)) {
6937330f729Sjoerg if (Var->isThisDeclarationADefinition()) {
6947330f729Sjoerg IsDefinition = true;
6957330f729Sjoerg return false;
6967330f729Sjoerg }
6977330f729Sjoerg return true;
698*e038c9c4Sjoerg }
6997330f729Sjoerg // Functions, if they aren't definitions.
700*e038c9c4Sjoerg if (const auto *FD = dyn_cast<FunctionDecl>(this)) {
7017330f729Sjoerg if (FD->hasBody()) {
7027330f729Sjoerg IsDefinition = true;
7037330f729Sjoerg return false;
7047330f729Sjoerg }
7057330f729Sjoerg return true;
7067330f729Sjoerg
707*e038c9c4Sjoerg }
7087330f729Sjoerg // Objective-C classes, if this is the non-fragile runtime.
709*e038c9c4Sjoerg if (isa<ObjCInterfaceDecl>(this) &&
7107330f729Sjoerg getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
7117330f729Sjoerg return true;
7127330f729Sjoerg }
713*e038c9c4Sjoerg // Nothing else.
714*e038c9c4Sjoerg return false;
7157330f729Sjoerg }
7167330f729Sjoerg
isWeakImported() const7177330f729Sjoerg bool Decl::isWeakImported() const {
7187330f729Sjoerg bool IsDefinition;
7197330f729Sjoerg if (!canBeWeakImported(IsDefinition))
7207330f729Sjoerg return false;
7217330f729Sjoerg
722*e038c9c4Sjoerg for (const auto *A : getMostRecentDecl()->attrs()) {
7237330f729Sjoerg if (isa<WeakImportAttr>(A))
7247330f729Sjoerg return true;
7257330f729Sjoerg
7267330f729Sjoerg if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
7277330f729Sjoerg if (CheckAvailability(getASTContext(), Availability, nullptr,
7287330f729Sjoerg VersionTuple()) == AR_NotYetIntroduced)
7297330f729Sjoerg return true;
7307330f729Sjoerg }
7317330f729Sjoerg }
7327330f729Sjoerg
7337330f729Sjoerg return false;
7347330f729Sjoerg }
7357330f729Sjoerg
getIdentifierNamespaceForKind(Kind DeclKind)7367330f729Sjoerg unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
7377330f729Sjoerg switch (DeclKind) {
7387330f729Sjoerg case Function:
7397330f729Sjoerg case CXXDeductionGuide:
7407330f729Sjoerg case CXXMethod:
7417330f729Sjoerg case CXXConstructor:
7427330f729Sjoerg case ConstructorUsingShadow:
7437330f729Sjoerg case CXXDestructor:
7447330f729Sjoerg case CXXConversion:
7457330f729Sjoerg case EnumConstant:
7467330f729Sjoerg case Var:
7477330f729Sjoerg case ImplicitParam:
7487330f729Sjoerg case ParmVar:
7497330f729Sjoerg case ObjCMethod:
7507330f729Sjoerg case ObjCProperty:
7517330f729Sjoerg case MSProperty:
7527330f729Sjoerg return IDNS_Ordinary;
7537330f729Sjoerg case Label:
7547330f729Sjoerg return IDNS_Label;
7557330f729Sjoerg case IndirectField:
7567330f729Sjoerg return IDNS_Ordinary | IDNS_Member;
7577330f729Sjoerg
7587330f729Sjoerg case Binding:
7597330f729Sjoerg case NonTypeTemplateParm:
7607330f729Sjoerg case VarTemplate:
7617330f729Sjoerg case Concept:
7627330f729Sjoerg // These (C++-only) declarations are found by redeclaration lookup for
7637330f729Sjoerg // tag types, so we include them in the tag namespace.
7647330f729Sjoerg return IDNS_Ordinary | IDNS_Tag;
7657330f729Sjoerg
7667330f729Sjoerg case ObjCCompatibleAlias:
7677330f729Sjoerg case ObjCInterface:
7687330f729Sjoerg return IDNS_Ordinary | IDNS_Type;
7697330f729Sjoerg
7707330f729Sjoerg case Typedef:
7717330f729Sjoerg case TypeAlias:
7727330f729Sjoerg case TemplateTypeParm:
7737330f729Sjoerg case ObjCTypeParam:
7747330f729Sjoerg return IDNS_Ordinary | IDNS_Type;
7757330f729Sjoerg
7767330f729Sjoerg case UnresolvedUsingTypename:
7777330f729Sjoerg return IDNS_Ordinary | IDNS_Type | IDNS_Using;
7787330f729Sjoerg
7797330f729Sjoerg case UsingShadow:
7807330f729Sjoerg return 0; // we'll actually overwrite this later
7817330f729Sjoerg
7827330f729Sjoerg case UnresolvedUsingValue:
7837330f729Sjoerg return IDNS_Ordinary | IDNS_Using;
7847330f729Sjoerg
7857330f729Sjoerg case Using:
7867330f729Sjoerg case UsingPack:
7877330f729Sjoerg return IDNS_Using;
7887330f729Sjoerg
7897330f729Sjoerg case ObjCProtocol:
7907330f729Sjoerg return IDNS_ObjCProtocol;
7917330f729Sjoerg
7927330f729Sjoerg case Field:
7937330f729Sjoerg case ObjCAtDefsField:
7947330f729Sjoerg case ObjCIvar:
7957330f729Sjoerg return IDNS_Member;
7967330f729Sjoerg
7977330f729Sjoerg case Record:
7987330f729Sjoerg case CXXRecord:
7997330f729Sjoerg case Enum:
8007330f729Sjoerg return IDNS_Tag | IDNS_Type;
8017330f729Sjoerg
8027330f729Sjoerg case Namespace:
8037330f729Sjoerg case NamespaceAlias:
8047330f729Sjoerg return IDNS_Namespace;
8057330f729Sjoerg
8067330f729Sjoerg case FunctionTemplate:
8077330f729Sjoerg return IDNS_Ordinary;
8087330f729Sjoerg
8097330f729Sjoerg case ClassTemplate:
8107330f729Sjoerg case TemplateTemplateParm:
8117330f729Sjoerg case TypeAliasTemplate:
8127330f729Sjoerg return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
8137330f729Sjoerg
8147330f729Sjoerg case OMPDeclareReduction:
8157330f729Sjoerg return IDNS_OMPReduction;
8167330f729Sjoerg
8177330f729Sjoerg case OMPDeclareMapper:
8187330f729Sjoerg return IDNS_OMPMapper;
8197330f729Sjoerg
8207330f729Sjoerg // Never have names.
8217330f729Sjoerg case Friend:
8227330f729Sjoerg case FriendTemplate:
8237330f729Sjoerg case AccessSpec:
8247330f729Sjoerg case LinkageSpec:
8257330f729Sjoerg case Export:
8267330f729Sjoerg case FileScopeAsm:
8277330f729Sjoerg case StaticAssert:
8287330f729Sjoerg case ObjCPropertyImpl:
8297330f729Sjoerg case PragmaComment:
8307330f729Sjoerg case PragmaDetectMismatch:
8317330f729Sjoerg case Block:
8327330f729Sjoerg case Captured:
8337330f729Sjoerg case TranslationUnit:
8347330f729Sjoerg case ExternCContext:
8357330f729Sjoerg case Decomposition:
836*e038c9c4Sjoerg case MSGuid:
837*e038c9c4Sjoerg case TemplateParamObject:
8387330f729Sjoerg
8397330f729Sjoerg case UsingDirective:
8407330f729Sjoerg case BuiltinTemplate:
8417330f729Sjoerg case ClassTemplateSpecialization:
8427330f729Sjoerg case ClassTemplatePartialSpecialization:
8437330f729Sjoerg case ClassScopeFunctionSpecialization:
8447330f729Sjoerg case VarTemplateSpecialization:
8457330f729Sjoerg case VarTemplatePartialSpecialization:
8467330f729Sjoerg case ObjCImplementation:
8477330f729Sjoerg case ObjCCategory:
8487330f729Sjoerg case ObjCCategoryImpl:
8497330f729Sjoerg case Import:
8507330f729Sjoerg case OMPThreadPrivate:
8517330f729Sjoerg case OMPAllocate:
8527330f729Sjoerg case OMPRequires:
8537330f729Sjoerg case OMPCapturedExpr:
8547330f729Sjoerg case Empty:
855*e038c9c4Sjoerg case LifetimeExtendedTemporary:
856*e038c9c4Sjoerg case RequiresExprBody:
8577330f729Sjoerg // Never looked up by name.
8587330f729Sjoerg return 0;
8597330f729Sjoerg }
8607330f729Sjoerg
8617330f729Sjoerg llvm_unreachable("Invalid DeclKind!");
8627330f729Sjoerg }
8637330f729Sjoerg
setAttrsImpl(const AttrVec & attrs,ASTContext & Ctx)8647330f729Sjoerg void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
8657330f729Sjoerg assert(!HasAttrs && "Decl already contains attrs.");
8667330f729Sjoerg
8677330f729Sjoerg AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
8687330f729Sjoerg assert(AttrBlank.empty() && "HasAttrs was wrong?");
8697330f729Sjoerg
8707330f729Sjoerg AttrBlank = attrs;
8717330f729Sjoerg HasAttrs = true;
8727330f729Sjoerg }
8737330f729Sjoerg
dropAttrs()8747330f729Sjoerg void Decl::dropAttrs() {
8757330f729Sjoerg if (!HasAttrs) return;
8767330f729Sjoerg
8777330f729Sjoerg HasAttrs = false;
8787330f729Sjoerg getASTContext().eraseDeclAttrs(this);
8797330f729Sjoerg }
8807330f729Sjoerg
addAttr(Attr * A)8817330f729Sjoerg void Decl::addAttr(Attr *A) {
8827330f729Sjoerg if (!hasAttrs()) {
8837330f729Sjoerg setAttrs(AttrVec(1, A));
8847330f729Sjoerg return;
8857330f729Sjoerg }
8867330f729Sjoerg
8877330f729Sjoerg AttrVec &Attrs = getAttrs();
8887330f729Sjoerg if (!A->isInherited()) {
8897330f729Sjoerg Attrs.push_back(A);
8907330f729Sjoerg return;
8917330f729Sjoerg }
8927330f729Sjoerg
8937330f729Sjoerg // Attribute inheritance is processed after attribute parsing. To keep the
8947330f729Sjoerg // order as in the source code, add inherited attributes before non-inherited
8957330f729Sjoerg // ones.
8967330f729Sjoerg auto I = Attrs.begin(), E = Attrs.end();
8977330f729Sjoerg for (; I != E; ++I) {
8987330f729Sjoerg if (!(*I)->isInherited())
8997330f729Sjoerg break;
9007330f729Sjoerg }
9017330f729Sjoerg Attrs.insert(I, A);
9027330f729Sjoerg }
9037330f729Sjoerg
getAttrs() const9047330f729Sjoerg const AttrVec &Decl::getAttrs() const {
9057330f729Sjoerg assert(HasAttrs && "No attrs to get!");
9067330f729Sjoerg return getASTContext().getDeclAttrs(this);
9077330f729Sjoerg }
9087330f729Sjoerg
castFromDeclContext(const DeclContext * D)9097330f729Sjoerg Decl *Decl::castFromDeclContext (const DeclContext *D) {
9107330f729Sjoerg Decl::Kind DK = D->getDeclKind();
9117330f729Sjoerg switch(DK) {
9127330f729Sjoerg #define DECL(NAME, BASE)
9137330f729Sjoerg #define DECL_CONTEXT(NAME) \
9147330f729Sjoerg case Decl::NAME: \
9157330f729Sjoerg return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D));
9167330f729Sjoerg #define DECL_CONTEXT_BASE(NAME)
9177330f729Sjoerg #include "clang/AST/DeclNodes.inc"
9187330f729Sjoerg default:
9197330f729Sjoerg #define DECL(NAME, BASE)
9207330f729Sjoerg #define DECL_CONTEXT_BASE(NAME) \
9217330f729Sjoerg if (DK >= first##NAME && DK <= last##NAME) \
9227330f729Sjoerg return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D));
9237330f729Sjoerg #include "clang/AST/DeclNodes.inc"
9247330f729Sjoerg llvm_unreachable("a decl that inherits DeclContext isn't handled");
9257330f729Sjoerg }
9267330f729Sjoerg }
9277330f729Sjoerg
castToDeclContext(const Decl * D)9287330f729Sjoerg DeclContext *Decl::castToDeclContext(const Decl *D) {
9297330f729Sjoerg Decl::Kind DK = D->getKind();
9307330f729Sjoerg switch(DK) {
9317330f729Sjoerg #define DECL(NAME, BASE)
9327330f729Sjoerg #define DECL_CONTEXT(NAME) \
9337330f729Sjoerg case Decl::NAME: \
9347330f729Sjoerg return static_cast<NAME##Decl *>(const_cast<Decl *>(D));
9357330f729Sjoerg #define DECL_CONTEXT_BASE(NAME)
9367330f729Sjoerg #include "clang/AST/DeclNodes.inc"
9377330f729Sjoerg default:
9387330f729Sjoerg #define DECL(NAME, BASE)
9397330f729Sjoerg #define DECL_CONTEXT_BASE(NAME) \
9407330f729Sjoerg if (DK >= first##NAME && DK <= last##NAME) \
9417330f729Sjoerg return static_cast<NAME##Decl *>(const_cast<Decl *>(D));
9427330f729Sjoerg #include "clang/AST/DeclNodes.inc"
9437330f729Sjoerg llvm_unreachable("a decl that inherits DeclContext isn't handled");
9447330f729Sjoerg }
9457330f729Sjoerg }
9467330f729Sjoerg
getBodyRBrace() const9477330f729Sjoerg SourceLocation Decl::getBodyRBrace() const {
9487330f729Sjoerg // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
9497330f729Sjoerg // FunctionDecl stores EndRangeLoc for this purpose.
9507330f729Sjoerg if (const auto *FD = dyn_cast<FunctionDecl>(this)) {
9517330f729Sjoerg const FunctionDecl *Definition;
9527330f729Sjoerg if (FD->hasBody(Definition))
9537330f729Sjoerg return Definition->getSourceRange().getEnd();
9547330f729Sjoerg return {};
9557330f729Sjoerg }
9567330f729Sjoerg
9577330f729Sjoerg if (Stmt *Body = getBody())
9587330f729Sjoerg return Body->getSourceRange().getEnd();
9597330f729Sjoerg
9607330f729Sjoerg return {};
9617330f729Sjoerg }
9627330f729Sjoerg
AccessDeclContextSanity() const9637330f729Sjoerg bool Decl::AccessDeclContextSanity() const {
9647330f729Sjoerg #ifndef NDEBUG
9657330f729Sjoerg // Suppress this check if any of the following hold:
9667330f729Sjoerg // 1. this is the translation unit (and thus has no parent)
9677330f729Sjoerg // 2. this is a template parameter (and thus doesn't belong to its context)
9687330f729Sjoerg // 3. this is a non-type template parameter
9697330f729Sjoerg // 4. the context is not a record
9707330f729Sjoerg // 5. it's invalid
9717330f729Sjoerg // 6. it's a C++0x static_assert.
9727330f729Sjoerg // 7. it's a block literal declaration
973*e038c9c4Sjoerg // 8. it's a temporary with lifetime extended due to being default value.
974*e038c9c4Sjoerg if (isa<TranslationUnitDecl>(this) || isa<TemplateTypeParmDecl>(this) ||
975*e038c9c4Sjoerg isa<NonTypeTemplateParmDecl>(this) || !getDeclContext() ||
976*e038c9c4Sjoerg !isa<CXXRecordDecl>(getDeclContext()) || isInvalidDecl() ||
977*e038c9c4Sjoerg isa<StaticAssertDecl>(this) || isa<BlockDecl>(this) ||
9787330f729Sjoerg // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
9797330f729Sjoerg // as DeclContext (?).
9807330f729Sjoerg isa<ParmVarDecl>(this) ||
9817330f729Sjoerg // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
9827330f729Sjoerg // AS_none as access specifier.
9837330f729Sjoerg isa<CXXRecordDecl>(this) ||
984*e038c9c4Sjoerg isa<ClassScopeFunctionSpecializationDecl>(this) ||
985*e038c9c4Sjoerg isa<LifetimeExtendedTemporaryDecl>(this))
9867330f729Sjoerg return true;
9877330f729Sjoerg
9887330f729Sjoerg assert(Access != AS_none &&
9897330f729Sjoerg "Access specifier is AS_none inside a record decl");
9907330f729Sjoerg #endif
9917330f729Sjoerg return true;
9927330f729Sjoerg }
9937330f729Sjoerg
getKind(const Decl * D)9947330f729Sjoerg static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
getKind(const DeclContext * DC)9957330f729Sjoerg static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
9967330f729Sjoerg
getID() const9977330f729Sjoerg int64_t Decl::getID() const {
9987330f729Sjoerg return getASTContext().getAllocator().identifyKnownAlignedObject<Decl>(this);
9997330f729Sjoerg }
10007330f729Sjoerg
getFunctionType(bool BlocksToo) const10017330f729Sjoerg const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
10027330f729Sjoerg QualType Ty;
10037330f729Sjoerg if (const auto *D = dyn_cast<ValueDecl>(this))
10047330f729Sjoerg Ty = D->getType();
10057330f729Sjoerg else if (const auto *D = dyn_cast<TypedefNameDecl>(this))
10067330f729Sjoerg Ty = D->getUnderlyingType();
10077330f729Sjoerg else
10087330f729Sjoerg return nullptr;
10097330f729Sjoerg
10107330f729Sjoerg if (Ty->isFunctionPointerType())
10117330f729Sjoerg Ty = Ty->castAs<PointerType>()->getPointeeType();
10127330f729Sjoerg else if (Ty->isFunctionReferenceType())
10137330f729Sjoerg Ty = Ty->castAs<ReferenceType>()->getPointeeType();
10147330f729Sjoerg else if (BlocksToo && Ty->isBlockPointerType())
10157330f729Sjoerg Ty = Ty->castAs<BlockPointerType>()->getPointeeType();
10167330f729Sjoerg
10177330f729Sjoerg return Ty->getAs<FunctionType>();
10187330f729Sjoerg }
10197330f729Sjoerg
10207330f729Sjoerg /// Starting at a given context (a Decl or DeclContext), look for a
10217330f729Sjoerg /// code context that is not a closure (a lambda, block, etc.).
getNonClosureContext(T * D)10227330f729Sjoerg template <class T> static Decl *getNonClosureContext(T *D) {
10237330f729Sjoerg if (getKind(D) == Decl::CXXMethod) {
10247330f729Sjoerg auto *MD = cast<CXXMethodDecl>(D);
10257330f729Sjoerg if (MD->getOverloadedOperator() == OO_Call &&
10267330f729Sjoerg MD->getParent()->isLambda())
10277330f729Sjoerg return getNonClosureContext(MD->getParent()->getParent());
10287330f729Sjoerg return MD;
1029*e038c9c4Sjoerg }
1030*e038c9c4Sjoerg if (auto *FD = dyn_cast<FunctionDecl>(D))
10317330f729Sjoerg return FD;
1032*e038c9c4Sjoerg if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
10337330f729Sjoerg return MD;
1034*e038c9c4Sjoerg if (auto *BD = dyn_cast<BlockDecl>(D))
10357330f729Sjoerg return getNonClosureContext(BD->getParent());
1036*e038c9c4Sjoerg if (auto *CD = dyn_cast<CapturedDecl>(D))
10377330f729Sjoerg return getNonClosureContext(CD->getParent());
10387330f729Sjoerg return nullptr;
10397330f729Sjoerg }
10407330f729Sjoerg
getNonClosureContext()10417330f729Sjoerg Decl *Decl::getNonClosureContext() {
10427330f729Sjoerg return ::getNonClosureContext(this);
10437330f729Sjoerg }
10447330f729Sjoerg
getNonClosureAncestor()10457330f729Sjoerg Decl *DeclContext::getNonClosureAncestor() {
10467330f729Sjoerg return ::getNonClosureContext(this);
10477330f729Sjoerg }
10487330f729Sjoerg
10497330f729Sjoerg //===----------------------------------------------------------------------===//
10507330f729Sjoerg // DeclContext Implementation
10517330f729Sjoerg //===----------------------------------------------------------------------===//
10527330f729Sjoerg
DeclContext(Decl::Kind K)10537330f729Sjoerg DeclContext::DeclContext(Decl::Kind K) {
10547330f729Sjoerg DeclContextBits.DeclKind = K;
10557330f729Sjoerg setHasExternalLexicalStorage(false);
10567330f729Sjoerg setHasExternalVisibleStorage(false);
10577330f729Sjoerg setNeedToReconcileExternalVisibleStorage(false);
10587330f729Sjoerg setHasLazyLocalLexicalLookups(false);
10597330f729Sjoerg setHasLazyExternalLexicalLookups(false);
10607330f729Sjoerg setUseQualifiedLookup(false);
10617330f729Sjoerg }
10627330f729Sjoerg
classof(const Decl * D)10637330f729Sjoerg bool DeclContext::classof(const Decl *D) {
10647330f729Sjoerg switch (D->getKind()) {
10657330f729Sjoerg #define DECL(NAME, BASE)
10667330f729Sjoerg #define DECL_CONTEXT(NAME) case Decl::NAME:
10677330f729Sjoerg #define DECL_CONTEXT_BASE(NAME)
10687330f729Sjoerg #include "clang/AST/DeclNodes.inc"
10697330f729Sjoerg return true;
10707330f729Sjoerg default:
10717330f729Sjoerg #define DECL(NAME, BASE)
10727330f729Sjoerg #define DECL_CONTEXT_BASE(NAME) \
10737330f729Sjoerg if (D->getKind() >= Decl::first##NAME && \
10747330f729Sjoerg D->getKind() <= Decl::last##NAME) \
10757330f729Sjoerg return true;
10767330f729Sjoerg #include "clang/AST/DeclNodes.inc"
10777330f729Sjoerg return false;
10787330f729Sjoerg }
10797330f729Sjoerg }
10807330f729Sjoerg
10817330f729Sjoerg DeclContext::~DeclContext() = default;
10827330f729Sjoerg
10837330f729Sjoerg /// Find the parent context of this context that will be
10847330f729Sjoerg /// used for unqualified name lookup.
10857330f729Sjoerg ///
10867330f729Sjoerg /// Generally, the parent lookup context is the semantic context. However, for
10877330f729Sjoerg /// a friend function the parent lookup context is the lexical context, which
10887330f729Sjoerg /// is the class in which the friend is declared.
getLookupParent()10897330f729Sjoerg DeclContext *DeclContext::getLookupParent() {
10907330f729Sjoerg // FIXME: Find a better way to identify friends.
10917330f729Sjoerg if (isa<FunctionDecl>(this))
10927330f729Sjoerg if (getParent()->getRedeclContext()->isFileContext() &&
10937330f729Sjoerg getLexicalParent()->getRedeclContext()->isRecord())
10947330f729Sjoerg return getLexicalParent();
10957330f729Sjoerg
10967330f729Sjoerg // A lookup within the call operator of a lambda never looks in the lambda
10977330f729Sjoerg // class; instead, skip to the context in which that closure type is
10987330f729Sjoerg // declared.
10997330f729Sjoerg if (isLambdaCallOperator(this))
11007330f729Sjoerg return getParent()->getParent();
11017330f729Sjoerg
11027330f729Sjoerg return getParent();
11037330f729Sjoerg }
11047330f729Sjoerg
getInnermostBlockDecl() const11057330f729Sjoerg const BlockDecl *DeclContext::getInnermostBlockDecl() const {
11067330f729Sjoerg const DeclContext *Ctx = this;
11077330f729Sjoerg
11087330f729Sjoerg do {
11097330f729Sjoerg if (Ctx->isClosure())
11107330f729Sjoerg return cast<BlockDecl>(Ctx);
11117330f729Sjoerg Ctx = Ctx->getParent();
11127330f729Sjoerg } while (Ctx);
11137330f729Sjoerg
11147330f729Sjoerg return nullptr;
11157330f729Sjoerg }
11167330f729Sjoerg
isInlineNamespace() const11177330f729Sjoerg bool DeclContext::isInlineNamespace() const {
11187330f729Sjoerg return isNamespace() &&
11197330f729Sjoerg cast<NamespaceDecl>(this)->isInline();
11207330f729Sjoerg }
11217330f729Sjoerg
isStdNamespace() const11227330f729Sjoerg bool DeclContext::isStdNamespace() const {
11237330f729Sjoerg if (!isNamespace())
11247330f729Sjoerg return false;
11257330f729Sjoerg
11267330f729Sjoerg const auto *ND = cast<NamespaceDecl>(this);
11277330f729Sjoerg if (ND->isInline()) {
11287330f729Sjoerg return ND->getParent()->isStdNamespace();
11297330f729Sjoerg }
11307330f729Sjoerg
11317330f729Sjoerg if (!getParent()->getRedeclContext()->isTranslationUnit())
11327330f729Sjoerg return false;
11337330f729Sjoerg
11347330f729Sjoerg const IdentifierInfo *II = ND->getIdentifier();
11357330f729Sjoerg return II && II->isStr("std");
11367330f729Sjoerg }
11377330f729Sjoerg
isDependentContext() const11387330f729Sjoerg bool DeclContext::isDependentContext() const {
11397330f729Sjoerg if (isFileContext())
11407330f729Sjoerg return false;
11417330f729Sjoerg
11427330f729Sjoerg if (isa<ClassTemplatePartialSpecializationDecl>(this))
11437330f729Sjoerg return true;
11447330f729Sjoerg
11457330f729Sjoerg if (const auto *Record = dyn_cast<CXXRecordDecl>(this)) {
11467330f729Sjoerg if (Record->getDescribedClassTemplate())
11477330f729Sjoerg return true;
11487330f729Sjoerg
11497330f729Sjoerg if (Record->isDependentLambda())
11507330f729Sjoerg return true;
11517330f729Sjoerg }
11527330f729Sjoerg
11537330f729Sjoerg if (const auto *Function = dyn_cast<FunctionDecl>(this)) {
11547330f729Sjoerg if (Function->getDescribedFunctionTemplate())
11557330f729Sjoerg return true;
11567330f729Sjoerg
11577330f729Sjoerg // Friend function declarations are dependent if their *lexical*
11587330f729Sjoerg // context is dependent.
11597330f729Sjoerg if (cast<Decl>(this)->getFriendObjectKind())
11607330f729Sjoerg return getLexicalParent()->isDependentContext();
11617330f729Sjoerg }
11627330f729Sjoerg
11637330f729Sjoerg // FIXME: A variable template is a dependent context, but is not a
11647330f729Sjoerg // DeclContext. A context within it (such as a lambda-expression)
11657330f729Sjoerg // should be considered dependent.
11667330f729Sjoerg
11677330f729Sjoerg return getParent() && getParent()->isDependentContext();
11687330f729Sjoerg }
11697330f729Sjoerg
isTransparentContext() const11707330f729Sjoerg bool DeclContext::isTransparentContext() const {
11717330f729Sjoerg if (getDeclKind() == Decl::Enum)
11727330f729Sjoerg return !cast<EnumDecl>(this)->isScoped();
11737330f729Sjoerg
1174*e038c9c4Sjoerg return getDeclKind() == Decl::LinkageSpec || getDeclKind() == Decl::Export;
11757330f729Sjoerg }
11767330f729Sjoerg
isLinkageSpecContext(const DeclContext * DC,LinkageSpecDecl::LanguageIDs ID)11777330f729Sjoerg static bool isLinkageSpecContext(const DeclContext *DC,
11787330f729Sjoerg LinkageSpecDecl::LanguageIDs ID) {
11797330f729Sjoerg while (DC->getDeclKind() != Decl::TranslationUnit) {
11807330f729Sjoerg if (DC->getDeclKind() == Decl::LinkageSpec)
11817330f729Sjoerg return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
11827330f729Sjoerg DC = DC->getLexicalParent();
11837330f729Sjoerg }
11847330f729Sjoerg return false;
11857330f729Sjoerg }
11867330f729Sjoerg
isExternCContext() const11877330f729Sjoerg bool DeclContext::isExternCContext() const {
11887330f729Sjoerg return isLinkageSpecContext(this, LinkageSpecDecl::lang_c);
11897330f729Sjoerg }
11907330f729Sjoerg
getExternCContext() const11917330f729Sjoerg const LinkageSpecDecl *DeclContext::getExternCContext() const {
11927330f729Sjoerg const DeclContext *DC = this;
11937330f729Sjoerg while (DC->getDeclKind() != Decl::TranslationUnit) {
11947330f729Sjoerg if (DC->getDeclKind() == Decl::LinkageSpec &&
11957330f729Sjoerg cast<LinkageSpecDecl>(DC)->getLanguage() == LinkageSpecDecl::lang_c)
11967330f729Sjoerg return cast<LinkageSpecDecl>(DC);
11977330f729Sjoerg DC = DC->getLexicalParent();
11987330f729Sjoerg }
11997330f729Sjoerg return nullptr;
12007330f729Sjoerg }
12017330f729Sjoerg
isExternCXXContext() const12027330f729Sjoerg bool DeclContext::isExternCXXContext() const {
12037330f729Sjoerg return isLinkageSpecContext(this, LinkageSpecDecl::lang_cxx);
12047330f729Sjoerg }
12057330f729Sjoerg
Encloses(const DeclContext * DC) const12067330f729Sjoerg bool DeclContext::Encloses(const DeclContext *DC) const {
12077330f729Sjoerg if (getPrimaryContext() != this)
12087330f729Sjoerg return getPrimaryContext()->Encloses(DC);
12097330f729Sjoerg
12107330f729Sjoerg for (; DC; DC = DC->getParent())
12117330f729Sjoerg if (DC->getPrimaryContext() == this)
12127330f729Sjoerg return true;
12137330f729Sjoerg return false;
12147330f729Sjoerg }
12157330f729Sjoerg
getPrimaryContext()12167330f729Sjoerg DeclContext *DeclContext::getPrimaryContext() {
12177330f729Sjoerg switch (getDeclKind()) {
12187330f729Sjoerg case Decl::TranslationUnit:
12197330f729Sjoerg case Decl::ExternCContext:
12207330f729Sjoerg case Decl::LinkageSpec:
12217330f729Sjoerg case Decl::Export:
12227330f729Sjoerg case Decl::Block:
12237330f729Sjoerg case Decl::Captured:
12247330f729Sjoerg case Decl::OMPDeclareReduction:
12257330f729Sjoerg case Decl::OMPDeclareMapper:
1226*e038c9c4Sjoerg case Decl::RequiresExprBody:
12277330f729Sjoerg // There is only one DeclContext for these entities.
12287330f729Sjoerg return this;
12297330f729Sjoerg
12307330f729Sjoerg case Decl::Namespace:
12317330f729Sjoerg // The original namespace is our primary context.
12327330f729Sjoerg return static_cast<NamespaceDecl *>(this)->getOriginalNamespace();
12337330f729Sjoerg
12347330f729Sjoerg case Decl::ObjCMethod:
12357330f729Sjoerg return this;
12367330f729Sjoerg
12377330f729Sjoerg case Decl::ObjCInterface:
12387330f729Sjoerg if (auto *OID = dyn_cast<ObjCInterfaceDecl>(this))
12397330f729Sjoerg if (auto *Def = OID->getDefinition())
12407330f729Sjoerg return Def;
12417330f729Sjoerg return this;
12427330f729Sjoerg
12437330f729Sjoerg case Decl::ObjCProtocol:
12447330f729Sjoerg if (auto *OPD = dyn_cast<ObjCProtocolDecl>(this))
12457330f729Sjoerg if (auto *Def = OPD->getDefinition())
12467330f729Sjoerg return Def;
12477330f729Sjoerg return this;
12487330f729Sjoerg
12497330f729Sjoerg case Decl::ObjCCategory:
12507330f729Sjoerg return this;
12517330f729Sjoerg
12527330f729Sjoerg case Decl::ObjCImplementation:
12537330f729Sjoerg case Decl::ObjCCategoryImpl:
12547330f729Sjoerg return this;
12557330f729Sjoerg
12567330f729Sjoerg default:
12577330f729Sjoerg if (getDeclKind() >= Decl::firstTag && getDeclKind() <= Decl::lastTag) {
12587330f729Sjoerg // If this is a tag type that has a definition or is currently
12597330f729Sjoerg // being defined, that definition is our primary context.
12607330f729Sjoerg auto *Tag = cast<TagDecl>(this);
12617330f729Sjoerg
12627330f729Sjoerg if (TagDecl *Def = Tag->getDefinition())
12637330f729Sjoerg return Def;
12647330f729Sjoerg
12657330f729Sjoerg if (const auto *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
12667330f729Sjoerg // Note, TagType::getDecl returns the (partial) definition one exists.
12677330f729Sjoerg TagDecl *PossiblePartialDef = TagTy->getDecl();
12687330f729Sjoerg if (PossiblePartialDef->isBeingDefined())
12697330f729Sjoerg return PossiblePartialDef;
12707330f729Sjoerg } else {
12717330f729Sjoerg assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
12727330f729Sjoerg }
12737330f729Sjoerg
12747330f729Sjoerg return Tag;
12757330f729Sjoerg }
12767330f729Sjoerg
12777330f729Sjoerg assert(getDeclKind() >= Decl::firstFunction &&
12787330f729Sjoerg getDeclKind() <= Decl::lastFunction &&
12797330f729Sjoerg "Unknown DeclContext kind");
12807330f729Sjoerg return this;
12817330f729Sjoerg }
12827330f729Sjoerg }
12837330f729Sjoerg
12847330f729Sjoerg void
collectAllContexts(SmallVectorImpl<DeclContext * > & Contexts)12857330f729Sjoerg DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
12867330f729Sjoerg Contexts.clear();
12877330f729Sjoerg
12887330f729Sjoerg if (getDeclKind() != Decl::Namespace) {
12897330f729Sjoerg Contexts.push_back(this);
12907330f729Sjoerg return;
12917330f729Sjoerg }
12927330f729Sjoerg
12937330f729Sjoerg auto *Self = static_cast<NamespaceDecl *>(this);
12947330f729Sjoerg for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
12957330f729Sjoerg N = N->getPreviousDecl())
12967330f729Sjoerg Contexts.push_back(N);
12977330f729Sjoerg
12987330f729Sjoerg std::reverse(Contexts.begin(), Contexts.end());
12997330f729Sjoerg }
13007330f729Sjoerg
13017330f729Sjoerg std::pair<Decl *, Decl *>
BuildDeclChain(ArrayRef<Decl * > Decls,bool FieldsAlreadyLoaded)13027330f729Sjoerg DeclContext::BuildDeclChain(ArrayRef<Decl *> Decls,
13037330f729Sjoerg bool FieldsAlreadyLoaded) {
13047330f729Sjoerg // Build up a chain of declarations via the Decl::NextInContextAndBits field.
13057330f729Sjoerg Decl *FirstNewDecl = nullptr;
13067330f729Sjoerg Decl *PrevDecl = nullptr;
13077330f729Sjoerg for (auto *D : Decls) {
13087330f729Sjoerg if (FieldsAlreadyLoaded && isa<FieldDecl>(D))
13097330f729Sjoerg continue;
13107330f729Sjoerg
13117330f729Sjoerg if (PrevDecl)
13127330f729Sjoerg PrevDecl->NextInContextAndBits.setPointer(D);
13137330f729Sjoerg else
13147330f729Sjoerg FirstNewDecl = D;
13157330f729Sjoerg
13167330f729Sjoerg PrevDecl = D;
13177330f729Sjoerg }
13187330f729Sjoerg
13197330f729Sjoerg return std::make_pair(FirstNewDecl, PrevDecl);
13207330f729Sjoerg }
13217330f729Sjoerg
13227330f729Sjoerg /// We have just acquired external visible storage, and we already have
13237330f729Sjoerg /// built a lookup map. For every name in the map, pull in the new names from
13247330f729Sjoerg /// the external storage.
reconcileExternalVisibleStorage() const13257330f729Sjoerg void DeclContext::reconcileExternalVisibleStorage() const {
13267330f729Sjoerg assert(hasNeedToReconcileExternalVisibleStorage() && LookupPtr);
13277330f729Sjoerg setNeedToReconcileExternalVisibleStorage(false);
13287330f729Sjoerg
13297330f729Sjoerg for (auto &Lookup : *LookupPtr)
13307330f729Sjoerg Lookup.second.setHasExternalDecls();
13317330f729Sjoerg }
13327330f729Sjoerg
13337330f729Sjoerg /// Load the declarations within this lexical storage from an
13347330f729Sjoerg /// external source.
13357330f729Sjoerg /// \return \c true if any declarations were added.
13367330f729Sjoerg bool
LoadLexicalDeclsFromExternalStorage() const13377330f729Sjoerg DeclContext::LoadLexicalDeclsFromExternalStorage() const {
13387330f729Sjoerg ExternalASTSource *Source = getParentASTContext().getExternalSource();
13397330f729Sjoerg assert(hasExternalLexicalStorage() && Source && "No external storage?");
13407330f729Sjoerg
13417330f729Sjoerg // Notify that we have a DeclContext that is initializing.
13427330f729Sjoerg ExternalASTSource::Deserializing ADeclContext(Source);
13437330f729Sjoerg
13447330f729Sjoerg // Load the external declarations, if any.
13457330f729Sjoerg SmallVector<Decl*, 64> Decls;
13467330f729Sjoerg setHasExternalLexicalStorage(false);
13477330f729Sjoerg Source->FindExternalLexicalDecls(this, Decls);
13487330f729Sjoerg
13497330f729Sjoerg if (Decls.empty())
13507330f729Sjoerg return false;
13517330f729Sjoerg
13527330f729Sjoerg // We may have already loaded just the fields of this record, in which case
13537330f729Sjoerg // we need to ignore them.
13547330f729Sjoerg bool FieldsAlreadyLoaded = false;
13557330f729Sjoerg if (const auto *RD = dyn_cast<RecordDecl>(this))
13567330f729Sjoerg FieldsAlreadyLoaded = RD->hasLoadedFieldsFromExternalStorage();
13577330f729Sjoerg
13587330f729Sjoerg // Splice the newly-read declarations into the beginning of the list
13597330f729Sjoerg // of declarations.
13607330f729Sjoerg Decl *ExternalFirst, *ExternalLast;
13617330f729Sjoerg std::tie(ExternalFirst, ExternalLast) =
13627330f729Sjoerg BuildDeclChain(Decls, FieldsAlreadyLoaded);
13637330f729Sjoerg ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
13647330f729Sjoerg FirstDecl = ExternalFirst;
13657330f729Sjoerg if (!LastDecl)
13667330f729Sjoerg LastDecl = ExternalLast;
13677330f729Sjoerg return true;
13687330f729Sjoerg }
13697330f729Sjoerg
13707330f729Sjoerg DeclContext::lookup_result
SetNoExternalVisibleDeclsForName(const DeclContext * DC,DeclarationName Name)13717330f729Sjoerg ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
13727330f729Sjoerg DeclarationName Name) {
13737330f729Sjoerg ASTContext &Context = DC->getParentASTContext();
13747330f729Sjoerg StoredDeclsMap *Map;
13757330f729Sjoerg if (!(Map = DC->LookupPtr))
13767330f729Sjoerg Map = DC->CreateStoredDeclsMap(Context);
13777330f729Sjoerg if (DC->hasNeedToReconcileExternalVisibleStorage())
13787330f729Sjoerg DC->reconcileExternalVisibleStorage();
13797330f729Sjoerg
13807330f729Sjoerg (*Map)[Name].removeExternalDecls();
13817330f729Sjoerg
13827330f729Sjoerg return DeclContext::lookup_result();
13837330f729Sjoerg }
13847330f729Sjoerg
13857330f729Sjoerg DeclContext::lookup_result
SetExternalVisibleDeclsForName(const DeclContext * DC,DeclarationName Name,ArrayRef<NamedDecl * > Decls)13867330f729Sjoerg ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
13877330f729Sjoerg DeclarationName Name,
13887330f729Sjoerg ArrayRef<NamedDecl*> Decls) {
13897330f729Sjoerg ASTContext &Context = DC->getParentASTContext();
13907330f729Sjoerg StoredDeclsMap *Map;
13917330f729Sjoerg if (!(Map = DC->LookupPtr))
13927330f729Sjoerg Map = DC->CreateStoredDeclsMap(Context);
13937330f729Sjoerg if (DC->hasNeedToReconcileExternalVisibleStorage())
13947330f729Sjoerg DC->reconcileExternalVisibleStorage();
13957330f729Sjoerg
13967330f729Sjoerg StoredDeclsList &List = (*Map)[Name];
1397*e038c9c4Sjoerg List.replaceExternalDecls(Decls);
13987330f729Sjoerg return List.getLookupResult();
13997330f729Sjoerg }
14007330f729Sjoerg
decls_begin() const14017330f729Sjoerg DeclContext::decl_iterator DeclContext::decls_begin() const {
14027330f729Sjoerg if (hasExternalLexicalStorage())
14037330f729Sjoerg LoadLexicalDeclsFromExternalStorage();
14047330f729Sjoerg return decl_iterator(FirstDecl);
14057330f729Sjoerg }
14067330f729Sjoerg
decls_empty() const14077330f729Sjoerg bool DeclContext::decls_empty() const {
14087330f729Sjoerg if (hasExternalLexicalStorage())
14097330f729Sjoerg LoadLexicalDeclsFromExternalStorage();
14107330f729Sjoerg
14117330f729Sjoerg return !FirstDecl;
14127330f729Sjoerg }
14137330f729Sjoerg
containsDecl(Decl * D) const14147330f729Sjoerg bool DeclContext::containsDecl(Decl *D) const {
14157330f729Sjoerg return (D->getLexicalDeclContext() == this &&
14167330f729Sjoerg (D->NextInContextAndBits.getPointer() || D == LastDecl));
14177330f729Sjoerg }
14187330f729Sjoerg
containsDeclAndLoad(Decl * D) const14197330f729Sjoerg bool DeclContext::containsDeclAndLoad(Decl *D) const {
14207330f729Sjoerg if (hasExternalLexicalStorage())
14217330f729Sjoerg LoadLexicalDeclsFromExternalStorage();
14227330f729Sjoerg return containsDecl(D);
14237330f729Sjoerg }
14247330f729Sjoerg
14257330f729Sjoerg /// shouldBeHidden - Determine whether a declaration which was declared
14267330f729Sjoerg /// within its semantic context should be invisible to qualified name lookup.
shouldBeHidden(NamedDecl * D)14277330f729Sjoerg static bool shouldBeHidden(NamedDecl *D) {
14287330f729Sjoerg // Skip unnamed declarations.
14297330f729Sjoerg if (!D->getDeclName())
14307330f729Sjoerg return true;
14317330f729Sjoerg
14327330f729Sjoerg // Skip entities that can't be found by name lookup into a particular
14337330f729Sjoerg // context.
14347330f729Sjoerg if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
14357330f729Sjoerg D->isTemplateParameter())
14367330f729Sjoerg return true;
14377330f729Sjoerg
14387330f729Sjoerg // Skip friends and local extern declarations unless they're the first
14397330f729Sjoerg // declaration of the entity.
14407330f729Sjoerg if ((D->isLocalExternDecl() || D->getFriendObjectKind()) &&
14417330f729Sjoerg D != D->getCanonicalDecl())
14427330f729Sjoerg return true;
14437330f729Sjoerg
14447330f729Sjoerg // Skip template specializations.
14457330f729Sjoerg // FIXME: This feels like a hack. Should DeclarationName support
14467330f729Sjoerg // template-ids, or is there a better way to keep specializations
14477330f729Sjoerg // from being visible?
14487330f729Sjoerg if (isa<ClassTemplateSpecializationDecl>(D))
14497330f729Sjoerg return true;
14507330f729Sjoerg if (auto *FD = dyn_cast<FunctionDecl>(D))
14517330f729Sjoerg if (FD->isFunctionTemplateSpecialization())
14527330f729Sjoerg return true;
14537330f729Sjoerg
1454*e038c9c4Sjoerg // Hide destructors that are invalid. There should always be one destructor,
1455*e038c9c4Sjoerg // but if it is an invalid decl, another one is created. We need to hide the
1456*e038c9c4Sjoerg // invalid one from places that expect exactly one destructor, like the
1457*e038c9c4Sjoerg // serialization code.
1458*e038c9c4Sjoerg if (isa<CXXDestructorDecl>(D) && D->isInvalidDecl())
1459*e038c9c4Sjoerg return true;
1460*e038c9c4Sjoerg
14617330f729Sjoerg return false;
14627330f729Sjoerg }
14637330f729Sjoerg
removeDecl(Decl * D)14647330f729Sjoerg void DeclContext::removeDecl(Decl *D) {
14657330f729Sjoerg assert(D->getLexicalDeclContext() == this &&
14667330f729Sjoerg "decl being removed from non-lexical context");
14677330f729Sjoerg assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
14687330f729Sjoerg "decl is not in decls list");
14697330f729Sjoerg
14707330f729Sjoerg // Remove D from the decl chain. This is O(n) but hopefully rare.
14717330f729Sjoerg if (D == FirstDecl) {
14727330f729Sjoerg if (D == LastDecl)
14737330f729Sjoerg FirstDecl = LastDecl = nullptr;
14747330f729Sjoerg else
14757330f729Sjoerg FirstDecl = D->NextInContextAndBits.getPointer();
14767330f729Sjoerg } else {
14777330f729Sjoerg for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
14787330f729Sjoerg assert(I && "decl not found in linked list");
14797330f729Sjoerg if (I->NextInContextAndBits.getPointer() == D) {
14807330f729Sjoerg I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
14817330f729Sjoerg if (D == LastDecl) LastDecl = I;
14827330f729Sjoerg break;
14837330f729Sjoerg }
14847330f729Sjoerg }
14857330f729Sjoerg }
14867330f729Sjoerg
14877330f729Sjoerg // Mark that D is no longer in the decl chain.
14887330f729Sjoerg D->NextInContextAndBits.setPointer(nullptr);
14897330f729Sjoerg
14907330f729Sjoerg // Remove D from the lookup table if necessary.
14917330f729Sjoerg if (isa<NamedDecl>(D)) {
14927330f729Sjoerg auto *ND = cast<NamedDecl>(D);
14937330f729Sjoerg
14947330f729Sjoerg // Do not try to remove the declaration if that is invisible to qualified
14957330f729Sjoerg // lookup. E.g. template specializations are skipped.
14967330f729Sjoerg if (shouldBeHidden(ND))
14977330f729Sjoerg return;
14987330f729Sjoerg
14997330f729Sjoerg // Remove only decls that have a name
15007330f729Sjoerg if (!ND->getDeclName())
15017330f729Sjoerg return;
15027330f729Sjoerg
15037330f729Sjoerg auto *DC = D->getDeclContext();
15047330f729Sjoerg do {
15057330f729Sjoerg StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr;
15067330f729Sjoerg if (Map) {
15077330f729Sjoerg StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
15087330f729Sjoerg assert(Pos != Map->end() && "no lookup entry for decl");
15097330f729Sjoerg Pos->second.remove(ND);
15107330f729Sjoerg }
15117330f729Sjoerg } while (DC->isTransparentContext() && (DC = DC->getParent()));
15127330f729Sjoerg }
15137330f729Sjoerg }
15147330f729Sjoerg
addHiddenDecl(Decl * D)15157330f729Sjoerg void DeclContext::addHiddenDecl(Decl *D) {
15167330f729Sjoerg assert(D->getLexicalDeclContext() == this &&
15177330f729Sjoerg "Decl inserted into wrong lexical context");
15187330f729Sjoerg assert(!D->getNextDeclInContext() && D != LastDecl &&
15197330f729Sjoerg "Decl already inserted into a DeclContext");
15207330f729Sjoerg
15217330f729Sjoerg if (FirstDecl) {
15227330f729Sjoerg LastDecl->NextInContextAndBits.setPointer(D);
15237330f729Sjoerg LastDecl = D;
15247330f729Sjoerg } else {
15257330f729Sjoerg FirstDecl = LastDecl = D;
15267330f729Sjoerg }
15277330f729Sjoerg
15287330f729Sjoerg // Notify a C++ record declaration that we've added a member, so it can
15297330f729Sjoerg // update its class-specific state.
15307330f729Sjoerg if (auto *Record = dyn_cast<CXXRecordDecl>(this))
15317330f729Sjoerg Record->addedMember(D);
15327330f729Sjoerg
15337330f729Sjoerg // If this is a newly-created (not de-serialized) import declaration, wire
15347330f729Sjoerg // it in to the list of local import declarations.
15357330f729Sjoerg if (!D->isFromASTFile()) {
15367330f729Sjoerg if (auto *Import = dyn_cast<ImportDecl>(D))
15377330f729Sjoerg D->getASTContext().addedLocalImportDecl(Import);
15387330f729Sjoerg }
15397330f729Sjoerg }
15407330f729Sjoerg
addDecl(Decl * D)15417330f729Sjoerg void DeclContext::addDecl(Decl *D) {
15427330f729Sjoerg addHiddenDecl(D);
15437330f729Sjoerg
15447330f729Sjoerg if (auto *ND = dyn_cast<NamedDecl>(D))
15457330f729Sjoerg ND->getDeclContext()->getPrimaryContext()->
15467330f729Sjoerg makeDeclVisibleInContextWithFlags(ND, false, true);
15477330f729Sjoerg }
15487330f729Sjoerg
addDeclInternal(Decl * D)15497330f729Sjoerg void DeclContext::addDeclInternal(Decl *D) {
15507330f729Sjoerg addHiddenDecl(D);
15517330f729Sjoerg
15527330f729Sjoerg if (auto *ND = dyn_cast<NamedDecl>(D))
15537330f729Sjoerg ND->getDeclContext()->getPrimaryContext()->
15547330f729Sjoerg makeDeclVisibleInContextWithFlags(ND, true, true);
15557330f729Sjoerg }
15567330f729Sjoerg
15577330f729Sjoerg /// buildLookup - Build the lookup data structure with all of the
15587330f729Sjoerg /// declarations in this DeclContext (and any other contexts linked
15597330f729Sjoerg /// to it or transparent contexts nested within it) and return it.
15607330f729Sjoerg ///
15617330f729Sjoerg /// Note that the produced map may miss out declarations from an
15627330f729Sjoerg /// external source. If it does, those entries will be marked with
15637330f729Sjoerg /// the 'hasExternalDecls' flag.
buildLookup()15647330f729Sjoerg StoredDeclsMap *DeclContext::buildLookup() {
15657330f729Sjoerg assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
15667330f729Sjoerg
15677330f729Sjoerg if (!hasLazyLocalLexicalLookups() &&
15687330f729Sjoerg !hasLazyExternalLexicalLookups())
15697330f729Sjoerg return LookupPtr;
15707330f729Sjoerg
15717330f729Sjoerg SmallVector<DeclContext *, 2> Contexts;
15727330f729Sjoerg collectAllContexts(Contexts);
15737330f729Sjoerg
15747330f729Sjoerg if (hasLazyExternalLexicalLookups()) {
15757330f729Sjoerg setHasLazyExternalLexicalLookups(false);
15767330f729Sjoerg for (auto *DC : Contexts) {
15777330f729Sjoerg if (DC->hasExternalLexicalStorage()) {
15787330f729Sjoerg bool LoadedDecls = DC->LoadLexicalDeclsFromExternalStorage();
15797330f729Sjoerg setHasLazyLocalLexicalLookups(
15807330f729Sjoerg hasLazyLocalLexicalLookups() | LoadedDecls );
15817330f729Sjoerg }
15827330f729Sjoerg }
15837330f729Sjoerg
15847330f729Sjoerg if (!hasLazyLocalLexicalLookups())
15857330f729Sjoerg return LookupPtr;
15867330f729Sjoerg }
15877330f729Sjoerg
15887330f729Sjoerg for (auto *DC : Contexts)
15897330f729Sjoerg buildLookupImpl(DC, hasExternalVisibleStorage());
15907330f729Sjoerg
15917330f729Sjoerg // We no longer have any lazy decls.
15927330f729Sjoerg setHasLazyLocalLexicalLookups(false);
15937330f729Sjoerg return LookupPtr;
15947330f729Sjoerg }
15957330f729Sjoerg
15967330f729Sjoerg /// buildLookupImpl - Build part of the lookup data structure for the
15977330f729Sjoerg /// declarations contained within DCtx, which will either be this
15987330f729Sjoerg /// DeclContext, a DeclContext linked to it, or a transparent context
15997330f729Sjoerg /// nested within it.
buildLookupImpl(DeclContext * DCtx,bool Internal)16007330f729Sjoerg void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
16017330f729Sjoerg for (auto *D : DCtx->noload_decls()) {
16027330f729Sjoerg // Insert this declaration into the lookup structure, but only if
16037330f729Sjoerg // it's semantically within its decl context. Any other decls which
16047330f729Sjoerg // should be found in this context are added eagerly.
16057330f729Sjoerg //
16067330f729Sjoerg // If it's from an AST file, don't add it now. It'll get handled by
16077330f729Sjoerg // FindExternalVisibleDeclsByName if needed. Exception: if we're not
16087330f729Sjoerg // in C++, we do not track external visible decls for the TU, so in
16097330f729Sjoerg // that case we need to collect them all here.
16107330f729Sjoerg if (auto *ND = dyn_cast<NamedDecl>(D))
16117330f729Sjoerg if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
16127330f729Sjoerg (!ND->isFromASTFile() ||
16137330f729Sjoerg (isTranslationUnit() &&
16147330f729Sjoerg !getParentASTContext().getLangOpts().CPlusPlus)))
16157330f729Sjoerg makeDeclVisibleInContextImpl(ND, Internal);
16167330f729Sjoerg
16177330f729Sjoerg // If this declaration is itself a transparent declaration context
16187330f729Sjoerg // or inline namespace, add the members of this declaration of that
16197330f729Sjoerg // context (recursively).
16207330f729Sjoerg if (auto *InnerCtx = dyn_cast<DeclContext>(D))
16217330f729Sjoerg if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
16227330f729Sjoerg buildLookupImpl(InnerCtx, Internal);
16237330f729Sjoerg }
16247330f729Sjoerg }
16257330f729Sjoerg
16267330f729Sjoerg DeclContext::lookup_result
lookup(DeclarationName Name) const16277330f729Sjoerg DeclContext::lookup(DeclarationName Name) const {
16287330f729Sjoerg assert(getDeclKind() != Decl::LinkageSpec &&
16297330f729Sjoerg getDeclKind() != Decl::Export &&
16307330f729Sjoerg "should not perform lookups into transparent contexts");
16317330f729Sjoerg
16327330f729Sjoerg const DeclContext *PrimaryContext = getPrimaryContext();
16337330f729Sjoerg if (PrimaryContext != this)
16347330f729Sjoerg return PrimaryContext->lookup(Name);
16357330f729Sjoerg
16367330f729Sjoerg // If we have an external source, ensure that any later redeclarations of this
16377330f729Sjoerg // context have been loaded, since they may add names to the result of this
16387330f729Sjoerg // lookup (or add external visible storage).
16397330f729Sjoerg ExternalASTSource *Source = getParentASTContext().getExternalSource();
16407330f729Sjoerg if (Source)
16417330f729Sjoerg (void)cast<Decl>(this)->getMostRecentDecl();
16427330f729Sjoerg
16437330f729Sjoerg if (hasExternalVisibleStorage()) {
16447330f729Sjoerg assert(Source && "external visible storage but no external source?");
16457330f729Sjoerg
16467330f729Sjoerg if (hasNeedToReconcileExternalVisibleStorage())
16477330f729Sjoerg reconcileExternalVisibleStorage();
16487330f729Sjoerg
16497330f729Sjoerg StoredDeclsMap *Map = LookupPtr;
16507330f729Sjoerg
16517330f729Sjoerg if (hasLazyLocalLexicalLookups() ||
16527330f729Sjoerg hasLazyExternalLexicalLookups())
16537330f729Sjoerg // FIXME: Make buildLookup const?
16547330f729Sjoerg Map = const_cast<DeclContext*>(this)->buildLookup();
16557330f729Sjoerg
16567330f729Sjoerg if (!Map)
16577330f729Sjoerg Map = CreateStoredDeclsMap(getParentASTContext());
16587330f729Sjoerg
16597330f729Sjoerg // If we have a lookup result with no external decls, we are done.
16607330f729Sjoerg std::pair<StoredDeclsMap::iterator, bool> R =
16617330f729Sjoerg Map->insert(std::make_pair(Name, StoredDeclsList()));
16627330f729Sjoerg if (!R.second && !R.first->second.hasExternalDecls())
16637330f729Sjoerg return R.first->second.getLookupResult();
16647330f729Sjoerg
16657330f729Sjoerg if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
16667330f729Sjoerg if (StoredDeclsMap *Map = LookupPtr) {
16677330f729Sjoerg StoredDeclsMap::iterator I = Map->find(Name);
16687330f729Sjoerg if (I != Map->end())
16697330f729Sjoerg return I->second.getLookupResult();
16707330f729Sjoerg }
16717330f729Sjoerg }
16727330f729Sjoerg
16737330f729Sjoerg return {};
16747330f729Sjoerg }
16757330f729Sjoerg
16767330f729Sjoerg StoredDeclsMap *Map = LookupPtr;
16777330f729Sjoerg if (hasLazyLocalLexicalLookups() ||
16787330f729Sjoerg hasLazyExternalLexicalLookups())
16797330f729Sjoerg Map = const_cast<DeclContext*>(this)->buildLookup();
16807330f729Sjoerg
16817330f729Sjoerg if (!Map)
16827330f729Sjoerg return {};
16837330f729Sjoerg
16847330f729Sjoerg StoredDeclsMap::iterator I = Map->find(Name);
16857330f729Sjoerg if (I == Map->end())
16867330f729Sjoerg return {};
16877330f729Sjoerg
16887330f729Sjoerg return I->second.getLookupResult();
16897330f729Sjoerg }
16907330f729Sjoerg
16917330f729Sjoerg DeclContext::lookup_result
noload_lookup(DeclarationName Name)16927330f729Sjoerg DeclContext::noload_lookup(DeclarationName Name) {
16937330f729Sjoerg assert(getDeclKind() != Decl::LinkageSpec &&
16947330f729Sjoerg getDeclKind() != Decl::Export &&
16957330f729Sjoerg "should not perform lookups into transparent contexts");
16967330f729Sjoerg
16977330f729Sjoerg DeclContext *PrimaryContext = getPrimaryContext();
16987330f729Sjoerg if (PrimaryContext != this)
16997330f729Sjoerg return PrimaryContext->noload_lookup(Name);
17007330f729Sjoerg
17017330f729Sjoerg loadLazyLocalLexicalLookups();
17027330f729Sjoerg StoredDeclsMap *Map = LookupPtr;
17037330f729Sjoerg if (!Map)
17047330f729Sjoerg return {};
17057330f729Sjoerg
17067330f729Sjoerg StoredDeclsMap::iterator I = Map->find(Name);
17077330f729Sjoerg return I != Map->end() ? I->second.getLookupResult()
17087330f729Sjoerg : lookup_result();
17097330f729Sjoerg }
17107330f729Sjoerg
17117330f729Sjoerg // If we have any lazy lexical declarations not in our lookup map, add them
17127330f729Sjoerg // now. Don't import any external declarations, not even if we know we have
17137330f729Sjoerg // some missing from the external visible lookups.
loadLazyLocalLexicalLookups()17147330f729Sjoerg void DeclContext::loadLazyLocalLexicalLookups() {
17157330f729Sjoerg if (hasLazyLocalLexicalLookups()) {
17167330f729Sjoerg SmallVector<DeclContext *, 2> Contexts;
17177330f729Sjoerg collectAllContexts(Contexts);
17187330f729Sjoerg for (auto *Context : Contexts)
17197330f729Sjoerg buildLookupImpl(Context, hasExternalVisibleStorage());
17207330f729Sjoerg setHasLazyLocalLexicalLookups(false);
17217330f729Sjoerg }
17227330f729Sjoerg }
17237330f729Sjoerg
localUncachedLookup(DeclarationName Name,SmallVectorImpl<NamedDecl * > & Results)17247330f729Sjoerg void DeclContext::localUncachedLookup(DeclarationName Name,
17257330f729Sjoerg SmallVectorImpl<NamedDecl *> &Results) {
17267330f729Sjoerg Results.clear();
17277330f729Sjoerg
17287330f729Sjoerg // If there's no external storage, just perform a normal lookup and copy
17297330f729Sjoerg // the results.
17307330f729Sjoerg if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
17317330f729Sjoerg lookup_result LookupResults = lookup(Name);
17327330f729Sjoerg Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
17337330f729Sjoerg return;
17347330f729Sjoerg }
17357330f729Sjoerg
17367330f729Sjoerg // If we have a lookup table, check there first. Maybe we'll get lucky.
17377330f729Sjoerg // FIXME: Should we be checking these flags on the primary context?
17387330f729Sjoerg if (Name && !hasLazyLocalLexicalLookups() &&
17397330f729Sjoerg !hasLazyExternalLexicalLookups()) {
17407330f729Sjoerg if (StoredDeclsMap *Map = LookupPtr) {
17417330f729Sjoerg StoredDeclsMap::iterator Pos = Map->find(Name);
17427330f729Sjoerg if (Pos != Map->end()) {
17437330f729Sjoerg Results.insert(Results.end(),
17447330f729Sjoerg Pos->second.getLookupResult().begin(),
17457330f729Sjoerg Pos->second.getLookupResult().end());
17467330f729Sjoerg return;
17477330f729Sjoerg }
17487330f729Sjoerg }
17497330f729Sjoerg }
17507330f729Sjoerg
17517330f729Sjoerg // Slow case: grovel through the declarations in our chain looking for
17527330f729Sjoerg // matches.
17537330f729Sjoerg // FIXME: If we have lazy external declarations, this will not find them!
17547330f729Sjoerg // FIXME: Should we CollectAllContexts and walk them all here?
17557330f729Sjoerg for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
17567330f729Sjoerg if (auto *ND = dyn_cast<NamedDecl>(D))
17577330f729Sjoerg if (ND->getDeclName() == Name)
17587330f729Sjoerg Results.push_back(ND);
17597330f729Sjoerg }
17607330f729Sjoerg }
17617330f729Sjoerg
getRedeclContext()17627330f729Sjoerg DeclContext *DeclContext::getRedeclContext() {
17637330f729Sjoerg DeclContext *Ctx = this;
17647330f729Sjoerg
17657330f729Sjoerg // In C, a record type is the redeclaration context for its fields only. If
17667330f729Sjoerg // we arrive at a record context after skipping anything else, we should skip
17677330f729Sjoerg // the record as well. Currently, this means skipping enumerations because
17687330f729Sjoerg // they're the only transparent context that can exist within a struct or
17697330f729Sjoerg // union.
17707330f729Sjoerg bool SkipRecords = getDeclKind() == Decl::Kind::Enum &&
17717330f729Sjoerg !getParentASTContext().getLangOpts().CPlusPlus;
17727330f729Sjoerg
17737330f729Sjoerg // Skip through contexts to get to the redeclaration context. Transparent
17747330f729Sjoerg // contexts are always skipped.
17757330f729Sjoerg while ((SkipRecords && Ctx->isRecord()) || Ctx->isTransparentContext())
17767330f729Sjoerg Ctx = Ctx->getParent();
17777330f729Sjoerg return Ctx;
17787330f729Sjoerg }
17797330f729Sjoerg
getEnclosingNamespaceContext()17807330f729Sjoerg DeclContext *DeclContext::getEnclosingNamespaceContext() {
17817330f729Sjoerg DeclContext *Ctx = this;
17827330f729Sjoerg // Skip through non-namespace, non-translation-unit contexts.
17837330f729Sjoerg while (!Ctx->isFileContext())
17847330f729Sjoerg Ctx = Ctx->getParent();
17857330f729Sjoerg return Ctx->getPrimaryContext();
17867330f729Sjoerg }
17877330f729Sjoerg
getOuterLexicalRecordContext()17887330f729Sjoerg RecordDecl *DeclContext::getOuterLexicalRecordContext() {
17897330f729Sjoerg // Loop until we find a non-record context.
17907330f729Sjoerg RecordDecl *OutermostRD = nullptr;
17917330f729Sjoerg DeclContext *DC = this;
17927330f729Sjoerg while (DC->isRecord()) {
17937330f729Sjoerg OutermostRD = cast<RecordDecl>(DC);
17947330f729Sjoerg DC = DC->getLexicalParent();
17957330f729Sjoerg }
17967330f729Sjoerg return OutermostRD;
17977330f729Sjoerg }
17987330f729Sjoerg
InEnclosingNamespaceSetOf(const DeclContext * O) const17997330f729Sjoerg bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
18007330f729Sjoerg // For non-file contexts, this is equivalent to Equals.
18017330f729Sjoerg if (!isFileContext())
18027330f729Sjoerg return O->Equals(this);
18037330f729Sjoerg
18047330f729Sjoerg do {
18057330f729Sjoerg if (O->Equals(this))
18067330f729Sjoerg return true;
18077330f729Sjoerg
18087330f729Sjoerg const auto *NS = dyn_cast<NamespaceDecl>(O);
18097330f729Sjoerg if (!NS || !NS->isInline())
18107330f729Sjoerg break;
18117330f729Sjoerg O = NS->getParent();
18127330f729Sjoerg } while (O);
18137330f729Sjoerg
18147330f729Sjoerg return false;
18157330f729Sjoerg }
18167330f729Sjoerg
makeDeclVisibleInContext(NamedDecl * D)18177330f729Sjoerg void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
18187330f729Sjoerg DeclContext *PrimaryDC = this->getPrimaryContext();
18197330f729Sjoerg DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
18207330f729Sjoerg // If the decl is being added outside of its semantic decl context, we
18217330f729Sjoerg // need to ensure that we eagerly build the lookup information for it.
18227330f729Sjoerg PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
18237330f729Sjoerg }
18247330f729Sjoerg
makeDeclVisibleInContextWithFlags(NamedDecl * D,bool Internal,bool Recoverable)18257330f729Sjoerg void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
18267330f729Sjoerg bool Recoverable) {
18277330f729Sjoerg assert(this == getPrimaryContext() && "expected a primary DC");
18287330f729Sjoerg
18297330f729Sjoerg if (!isLookupContext()) {
18307330f729Sjoerg if (isTransparentContext())
18317330f729Sjoerg getParent()->getPrimaryContext()
18327330f729Sjoerg ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
18337330f729Sjoerg return;
18347330f729Sjoerg }
18357330f729Sjoerg
18367330f729Sjoerg // Skip declarations which should be invisible to name lookup.
18377330f729Sjoerg if (shouldBeHidden(D))
18387330f729Sjoerg return;
18397330f729Sjoerg
18407330f729Sjoerg // If we already have a lookup data structure, perform the insertion into
18417330f729Sjoerg // it. If we might have externally-stored decls with this name, look them
18427330f729Sjoerg // up and perform the insertion. If this decl was declared outside its
18437330f729Sjoerg // semantic context, buildLookup won't add it, so add it now.
18447330f729Sjoerg //
18457330f729Sjoerg // FIXME: As a performance hack, don't add such decls into the translation
18467330f729Sjoerg // unit unless we're in C++, since qualified lookup into the TU is never
18477330f729Sjoerg // performed.
18487330f729Sjoerg if (LookupPtr || hasExternalVisibleStorage() ||
18497330f729Sjoerg ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
18507330f729Sjoerg (getParentASTContext().getLangOpts().CPlusPlus ||
18517330f729Sjoerg !isTranslationUnit()))) {
18527330f729Sjoerg // If we have lazily omitted any decls, they might have the same name as
18537330f729Sjoerg // the decl which we are adding, so build a full lookup table before adding
18547330f729Sjoerg // this decl.
18557330f729Sjoerg buildLookup();
18567330f729Sjoerg makeDeclVisibleInContextImpl(D, Internal);
18577330f729Sjoerg } else {
18587330f729Sjoerg setHasLazyLocalLexicalLookups(true);
18597330f729Sjoerg }
18607330f729Sjoerg
18617330f729Sjoerg // If we are a transparent context or inline namespace, insert into our
18627330f729Sjoerg // parent context, too. This operation is recursive.
18637330f729Sjoerg if (isTransparentContext() || isInlineNamespace())
18647330f729Sjoerg getParent()->getPrimaryContext()->
18657330f729Sjoerg makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
18667330f729Sjoerg
18677330f729Sjoerg auto *DCAsDecl = cast<Decl>(this);
18687330f729Sjoerg // Notify that a decl was made visible unless we are a Tag being defined.
18697330f729Sjoerg if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
18707330f729Sjoerg if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
18717330f729Sjoerg L->AddedVisibleDecl(this, D);
18727330f729Sjoerg }
18737330f729Sjoerg
makeDeclVisibleInContextImpl(NamedDecl * D,bool Internal)18747330f729Sjoerg void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
18757330f729Sjoerg // Find or create the stored declaration map.
18767330f729Sjoerg StoredDeclsMap *Map = LookupPtr;
18777330f729Sjoerg if (!Map) {
18787330f729Sjoerg ASTContext *C = &getParentASTContext();
18797330f729Sjoerg Map = CreateStoredDeclsMap(*C);
18807330f729Sjoerg }
18817330f729Sjoerg
18827330f729Sjoerg // If there is an external AST source, load any declarations it knows about
18837330f729Sjoerg // with this declaration's name.
18847330f729Sjoerg // If the lookup table contains an entry about this name it means that we
18857330f729Sjoerg // have already checked the external source.
18867330f729Sjoerg if (!Internal)
18877330f729Sjoerg if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
18887330f729Sjoerg if (hasExternalVisibleStorage() &&
18897330f729Sjoerg Map->find(D->getDeclName()) == Map->end())
18907330f729Sjoerg Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
18917330f729Sjoerg
18927330f729Sjoerg // Insert this declaration into the map.
18937330f729Sjoerg StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
18947330f729Sjoerg
18957330f729Sjoerg if (Internal) {
18967330f729Sjoerg // If this is being added as part of loading an external declaration,
18977330f729Sjoerg // this may not be the only external declaration with this name.
18987330f729Sjoerg // In this case, we never try to replace an existing declaration; we'll
18997330f729Sjoerg // handle that when we finalize the list of declarations for this name.
19007330f729Sjoerg DeclNameEntries.setHasExternalDecls();
1901*e038c9c4Sjoerg DeclNameEntries.prependDeclNoReplace(D);
19027330f729Sjoerg return;
19037330f729Sjoerg }
19047330f729Sjoerg
1905*e038c9c4Sjoerg DeclNameEntries.addOrReplaceDecl(D);
19067330f729Sjoerg }
19077330f729Sjoerg
operator *() const19087330f729Sjoerg UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
19097330f729Sjoerg return cast<UsingDirectiveDecl>(*I);
19107330f729Sjoerg }
19117330f729Sjoerg
19127330f729Sjoerg /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
19137330f729Sjoerg /// this context.
using_directives() const19147330f729Sjoerg DeclContext::udir_range DeclContext::using_directives() const {
19157330f729Sjoerg // FIXME: Use something more efficient than normal lookup for using
19167330f729Sjoerg // directives. In C++, using directives are looked up more than anything else.
19177330f729Sjoerg lookup_result Result = lookup(UsingDirectiveDecl::getName());
19187330f729Sjoerg return udir_range(Result.begin(), Result.end());
19197330f729Sjoerg }
19207330f729Sjoerg
19217330f729Sjoerg //===----------------------------------------------------------------------===//
19227330f729Sjoerg // Creation and Destruction of StoredDeclsMaps. //
19237330f729Sjoerg //===----------------------------------------------------------------------===//
19247330f729Sjoerg
CreateStoredDeclsMap(ASTContext & C) const19257330f729Sjoerg StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
19267330f729Sjoerg assert(!LookupPtr && "context already has a decls map");
19277330f729Sjoerg assert(getPrimaryContext() == this &&
19287330f729Sjoerg "creating decls map on non-primary context");
19297330f729Sjoerg
19307330f729Sjoerg StoredDeclsMap *M;
19317330f729Sjoerg bool Dependent = isDependentContext();
19327330f729Sjoerg if (Dependent)
19337330f729Sjoerg M = new DependentStoredDeclsMap();
19347330f729Sjoerg else
19357330f729Sjoerg M = new StoredDeclsMap();
19367330f729Sjoerg M->Previous = C.LastSDM;
19377330f729Sjoerg C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
19387330f729Sjoerg LookupPtr = M;
19397330f729Sjoerg return M;
19407330f729Sjoerg }
19417330f729Sjoerg
ReleaseDeclContextMaps()19427330f729Sjoerg void ASTContext::ReleaseDeclContextMaps() {
19437330f729Sjoerg // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
19447330f729Sjoerg // pointer because the subclass doesn't add anything that needs to
19457330f729Sjoerg // be deleted.
19467330f729Sjoerg StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
19477330f729Sjoerg }
19487330f729Sjoerg
DestroyAll(StoredDeclsMap * Map,bool Dependent)19497330f729Sjoerg void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
19507330f729Sjoerg while (Map) {
19517330f729Sjoerg // Advance the iteration before we invalidate memory.
19527330f729Sjoerg llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
19537330f729Sjoerg
19547330f729Sjoerg if (Dependent)
19557330f729Sjoerg delete static_cast<DependentStoredDeclsMap*>(Map);
19567330f729Sjoerg else
19577330f729Sjoerg delete Map;
19587330f729Sjoerg
19597330f729Sjoerg Map = Next.getPointer();
19607330f729Sjoerg Dependent = Next.getInt();
19617330f729Sjoerg }
19627330f729Sjoerg }
19637330f729Sjoerg
Create(ASTContext & C,DeclContext * Parent,const PartialDiagnostic & PDiag)19647330f729Sjoerg DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
19657330f729Sjoerg DeclContext *Parent,
19667330f729Sjoerg const PartialDiagnostic &PDiag) {
19677330f729Sjoerg assert(Parent->isDependentContext()
19687330f729Sjoerg && "cannot iterate dependent diagnostics of non-dependent context");
19697330f729Sjoerg Parent = Parent->getPrimaryContext();
19707330f729Sjoerg if (!Parent->LookupPtr)
19717330f729Sjoerg Parent->CreateStoredDeclsMap(C);
19727330f729Sjoerg
19737330f729Sjoerg auto *Map = static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
19747330f729Sjoerg
19757330f729Sjoerg // Allocate the copy of the PartialDiagnostic via the ASTContext's
19767330f729Sjoerg // BumpPtrAllocator, rather than the ASTContext itself.
1977*e038c9c4Sjoerg DiagnosticStorage *DiagStorage = nullptr;
19787330f729Sjoerg if (PDiag.hasStorage())
1979*e038c9c4Sjoerg DiagStorage = new (C) DiagnosticStorage;
19807330f729Sjoerg
19817330f729Sjoerg auto *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
19827330f729Sjoerg
19837330f729Sjoerg // TODO: Maybe we shouldn't reverse the order during insertion.
19847330f729Sjoerg DD->NextDiagnostic = Map->FirstDiagnostic;
19857330f729Sjoerg Map->FirstDiagnostic = DD;
19867330f729Sjoerg
19877330f729Sjoerg return DD;
19887330f729Sjoerg }
1989