10b57cec5SDimitry Andric //===- Decl.cpp - Declaration AST Node Implementation ---------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the Decl subclasses. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "clang/AST/Decl.h" 140b57cec5SDimitry Andric #include "Linkage.h" 150b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 160b57cec5SDimitry Andric #include "clang/AST/ASTDiagnostic.h" 170b57cec5SDimitry Andric #include "clang/AST/ASTLambda.h" 180b57cec5SDimitry Andric #include "clang/AST/ASTMutationListener.h" 19480093f4SDimitry Andric #include "clang/AST/Attr.h" 200b57cec5SDimitry Andric #include "clang/AST/CanonicalType.h" 210b57cec5SDimitry Andric #include "clang/AST/DeclBase.h" 220b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h" 230b57cec5SDimitry Andric #include "clang/AST/DeclObjC.h" 240b57cec5SDimitry Andric #include "clang/AST/DeclOpenMP.h" 250b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h" 260b57cec5SDimitry Andric #include "clang/AST/DeclarationName.h" 270b57cec5SDimitry Andric #include "clang/AST/Expr.h" 280b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h" 290b57cec5SDimitry Andric #include "clang/AST/ExternalASTSource.h" 300b57cec5SDimitry Andric #include "clang/AST/ODRHash.h" 310b57cec5SDimitry Andric #include "clang/AST/PrettyDeclStackTrace.h" 320b57cec5SDimitry Andric #include "clang/AST/PrettyPrinter.h" 3381ad6265SDimitry Andric #include "clang/AST/Randstruct.h" 3481ad6265SDimitry Andric #include "clang/AST/RecordLayout.h" 350b57cec5SDimitry Andric #include "clang/AST/Redeclarable.h" 360b57cec5SDimitry Andric #include "clang/AST/Stmt.h" 370b57cec5SDimitry Andric #include "clang/AST/TemplateBase.h" 380b57cec5SDimitry Andric #include "clang/AST/Type.h" 390b57cec5SDimitry Andric #include "clang/AST/TypeLoc.h" 400b57cec5SDimitry Andric #include "clang/Basic/Builtins.h" 410b57cec5SDimitry Andric #include "clang/Basic/IdentifierTable.h" 420b57cec5SDimitry Andric #include "clang/Basic/LLVM.h" 430b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h" 440b57cec5SDimitry Andric #include "clang/Basic/Linkage.h" 450b57cec5SDimitry Andric #include "clang/Basic/Module.h" 46fe6060f1SDimitry Andric #include "clang/Basic/NoSanitizeList.h" 470b57cec5SDimitry Andric #include "clang/Basic/PartialDiagnostic.h" 480b57cec5SDimitry Andric #include "clang/Basic/Sanitizers.h" 490b57cec5SDimitry Andric #include "clang/Basic/SourceLocation.h" 500b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h" 510b57cec5SDimitry Andric #include "clang/Basic/Specifiers.h" 520b57cec5SDimitry Andric #include "clang/Basic/TargetCXXABI.h" 530b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h" 540b57cec5SDimitry Andric #include "clang/Basic/Visibility.h" 550b57cec5SDimitry Andric #include "llvm/ADT/APSInt.h" 560b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 570b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 580b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 590b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 60480093f4SDimitry Andric #include "llvm/ADT/StringSwitch.h" 610b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 620b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 630b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 6406c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h" 650b57cec5SDimitry Andric #include <algorithm> 660b57cec5SDimitry Andric #include <cassert> 670b57cec5SDimitry Andric #include <cstddef> 680b57cec5SDimitry Andric #include <cstring> 690b57cec5SDimitry Andric #include <memory> 70bdd1243dSDimitry Andric #include <optional> 710b57cec5SDimitry Andric #include <string> 720b57cec5SDimitry Andric #include <tuple> 730b57cec5SDimitry Andric #include <type_traits> 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric using namespace clang; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric Decl *clang::getPrimaryMergedDecl(Decl *D) { 780b57cec5SDimitry Andric return D->getASTContext().getPrimaryMergedDecl(D); 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const { 820b57cec5SDimitry Andric SourceLocation Loc = this->Loc; 830b57cec5SDimitry Andric if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); 840b57cec5SDimitry Andric if (Loc.isValid()) { 850b57cec5SDimitry Andric Loc.print(OS, Context.getSourceManager()); 860b57cec5SDimitry Andric OS << ": "; 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric OS << Message; 890b57cec5SDimitry Andric 905f757f3fSDimitry Andric if (auto *ND = dyn_cast_if_present<NamedDecl>(TheDecl)) { 910b57cec5SDimitry Andric OS << " '"; 920b57cec5SDimitry Andric ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true); 930b57cec5SDimitry Andric OS << "'"; 940b57cec5SDimitry Andric } 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric OS << '\n'; 970b57cec5SDimitry Andric } 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric // Defined here so that it can be inlined into its direct callers. 1000b57cec5SDimitry Andric bool Decl::isOutOfLine() const { 1010b57cec5SDimitry Andric return !getLexicalDeclContext()->Equals(getDeclContext()); 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx) 1050b57cec5SDimitry Andric : Decl(TranslationUnit, nullptr, SourceLocation()), 106fe6060f1SDimitry Andric DeclContext(TranslationUnit), redeclarable_base(ctx), Ctx(ctx) {} 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1090b57cec5SDimitry Andric // NamedDecl Implementation 1100b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric // Visibility rules aren't rigorously externally specified, but here 1130b57cec5SDimitry Andric // are the basic principles behind what we implement: 1140b57cec5SDimitry Andric // 1150b57cec5SDimitry Andric // 1. An explicit visibility attribute is generally a direct expression 1160b57cec5SDimitry Andric // of the user's intent and should be honored. Only the innermost 1170b57cec5SDimitry Andric // visibility attribute applies. If no visibility attribute applies, 1180b57cec5SDimitry Andric // global visibility settings are considered. 1190b57cec5SDimitry Andric // 1200b57cec5SDimitry Andric // 2. There is one caveat to the above: on or in a template pattern, 1210b57cec5SDimitry Andric // an explicit visibility attribute is just a default rule, and 1220b57cec5SDimitry Andric // visibility can be decreased by the visibility of template 1230b57cec5SDimitry Andric // arguments. But this, too, has an exception: an attribute on an 1240b57cec5SDimitry Andric // explicit specialization or instantiation causes all the visibility 1250b57cec5SDimitry Andric // restrictions of the template arguments to be ignored. 1260b57cec5SDimitry Andric // 1270b57cec5SDimitry Andric // 3. A variable that does not otherwise have explicit visibility can 1280b57cec5SDimitry Andric // be restricted by the visibility of its type. 1290b57cec5SDimitry Andric // 1300b57cec5SDimitry Andric // 4. A visibility restriction is explicit if it comes from an 1310b57cec5SDimitry Andric // attribute (or something like it), not a global visibility setting. 1320b57cec5SDimitry Andric // When emitting a reference to an external symbol, visibility 1330b57cec5SDimitry Andric // restrictions are ignored unless they are explicit. 1340b57cec5SDimitry Andric // 1350b57cec5SDimitry Andric // 5. When computing the visibility of a non-type, including a 1360b57cec5SDimitry Andric // non-type member of a class, only non-type visibility restrictions 1370b57cec5SDimitry Andric // are considered: the 'visibility' attribute, global value-visibility 1380b57cec5SDimitry Andric // settings, and a few special cases like __private_extern. 1390b57cec5SDimitry Andric // 1400b57cec5SDimitry Andric // 6. When computing the visibility of a type, including a type member 1410b57cec5SDimitry Andric // of a class, only type visibility restrictions are considered: 1420b57cec5SDimitry Andric // the 'type_visibility' attribute and global type-visibility settings. 1430b57cec5SDimitry Andric // However, a 'visibility' attribute counts as a 'type_visibility' 1440b57cec5SDimitry Andric // attribute on any declaration that only has the former. 1450b57cec5SDimitry Andric // 1460b57cec5SDimitry Andric // The visibility of a "secondary" entity, like a template argument, 1470b57cec5SDimitry Andric // is computed using the kind of that entity, not the kind of the 1480b57cec5SDimitry Andric // primary entity for which we are computing visibility. For example, 1490b57cec5SDimitry Andric // the visibility of a specialization of either of these templates: 1500b57cec5SDimitry Andric // template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X); 1510b57cec5SDimitry Andric // template <class T, bool (&compare)(T, X)> class matcher; 1520b57cec5SDimitry Andric // is restricted according to the type visibility of the argument 'T', 1530b57cec5SDimitry Andric // the type visibility of 'bool(&)(T,X)', and the value visibility of 1540b57cec5SDimitry Andric // the argument function 'compare'. That 'has_match' is a value 1550b57cec5SDimitry Andric // and 'matcher' is a type only matters when looking for attributes 1560b57cec5SDimitry Andric // and settings from the immediate context. 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric /// Does this computation kind permit us to consider additional 1590b57cec5SDimitry Andric /// visibility settings from attributes and the like? 1600b57cec5SDimitry Andric static bool hasExplicitVisibilityAlready(LVComputationKind computation) { 1610b57cec5SDimitry Andric return computation.IgnoreExplicitVisibility; 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric /// Given an LVComputationKind, return one of the same type/value sort 1650b57cec5SDimitry Andric /// that records that it already has explicit visibility. 1660b57cec5SDimitry Andric static LVComputationKind 1670b57cec5SDimitry Andric withExplicitVisibilityAlready(LVComputationKind Kind) { 1680b57cec5SDimitry Andric Kind.IgnoreExplicitVisibility = true; 1690b57cec5SDimitry Andric return Kind; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 172bdd1243dSDimitry Andric static std::optional<Visibility> getExplicitVisibility(const NamedDecl *D, 1730b57cec5SDimitry Andric LVComputationKind kind) { 1740b57cec5SDimitry Andric assert(!kind.IgnoreExplicitVisibility && 1750b57cec5SDimitry Andric "asking for explicit visibility when we shouldn't be"); 1760b57cec5SDimitry Andric return D->getExplicitVisibility(kind.getExplicitVisibilityKind()); 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric /// Is the given declaration a "type" or a "value" for the purposes of 1800b57cec5SDimitry Andric /// visibility computation? 1810b57cec5SDimitry Andric static bool usesTypeVisibility(const NamedDecl *D) { 1820b57cec5SDimitry Andric return isa<TypeDecl>(D) || 1830b57cec5SDimitry Andric isa<ClassTemplateDecl>(D) || 1840b57cec5SDimitry Andric isa<ObjCInterfaceDecl>(D); 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric /// Does the given declaration have member specialization information, 1880b57cec5SDimitry Andric /// and if so, is it an explicit specialization? 189bdd1243dSDimitry Andric template <class T> 190bdd1243dSDimitry Andric static std::enable_if_t<!std::is_base_of_v<RedeclarableTemplateDecl, T>, bool> 1910b57cec5SDimitry Andric isExplicitMemberSpecialization(const T *D) { 1920b57cec5SDimitry Andric if (const MemberSpecializationInfo *member = 1930b57cec5SDimitry Andric D->getMemberSpecializationInfo()) { 1940b57cec5SDimitry Andric return member->isExplicitSpecialization(); 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric return false; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric /// For templates, this question is easier: a member template can't be 2000b57cec5SDimitry Andric /// explicitly instantiated, so there's a single bit indicating whether 2010b57cec5SDimitry Andric /// or not this is an explicit member specialization. 2020b57cec5SDimitry Andric static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) { 2030b57cec5SDimitry Andric return D->isMemberSpecialization(); 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric /// Given a visibility attribute, return the explicit visibility 2070b57cec5SDimitry Andric /// associated with it. 2080b57cec5SDimitry Andric template <class T> 2090b57cec5SDimitry Andric static Visibility getVisibilityFromAttr(const T *attr) { 2100b57cec5SDimitry Andric switch (attr->getVisibility()) { 2110b57cec5SDimitry Andric case T::Default: 2120b57cec5SDimitry Andric return DefaultVisibility; 2130b57cec5SDimitry Andric case T::Hidden: 2140b57cec5SDimitry Andric return HiddenVisibility; 2150b57cec5SDimitry Andric case T::Protected: 2160b57cec5SDimitry Andric return ProtectedVisibility; 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric llvm_unreachable("bad visibility kind"); 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric /// Return the explicit visibility of the given declaration. 222bdd1243dSDimitry Andric static std::optional<Visibility> 223bdd1243dSDimitry Andric getVisibilityOf(const NamedDecl *D, NamedDecl::ExplicitVisibilityKind kind) { 2240b57cec5SDimitry Andric // If we're ultimately computing the visibility of a type, look for 2250b57cec5SDimitry Andric // a 'type_visibility' attribute before looking for 'visibility'. 2260b57cec5SDimitry Andric if (kind == NamedDecl::VisibilityForType) { 2270b57cec5SDimitry Andric if (const auto *A = D->getAttr<TypeVisibilityAttr>()) { 2280b57cec5SDimitry Andric return getVisibilityFromAttr(A); 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric } 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric // If this declaration has an explicit visibility attribute, use it. 2330b57cec5SDimitry Andric if (const auto *A = D->getAttr<VisibilityAttr>()) { 2340b57cec5SDimitry Andric return getVisibilityFromAttr(A); 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 237bdd1243dSDimitry Andric return std::nullopt; 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric LinkageInfo LinkageComputer::getLVForType(const Type &T, 2410b57cec5SDimitry Andric LVComputationKind computation) { 2420b57cec5SDimitry Andric if (computation.IgnoreAllVisibility) 2430b57cec5SDimitry Andric return LinkageInfo(T.getLinkage(), DefaultVisibility, true); 2440b57cec5SDimitry Andric return getTypeLinkageAndVisibility(&T); 2450b57cec5SDimitry Andric } 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric /// Get the most restrictive linkage for the types in the given 2480b57cec5SDimitry Andric /// template parameter list. For visibility purposes, template 2490b57cec5SDimitry Andric /// parameters are part of the signature of a template. 2500b57cec5SDimitry Andric LinkageInfo LinkageComputer::getLVForTemplateParameterList( 2510b57cec5SDimitry Andric const TemplateParameterList *Params, LVComputationKind computation) { 2520b57cec5SDimitry Andric LinkageInfo LV; 2530b57cec5SDimitry Andric for (const NamedDecl *P : *Params) { 2540b57cec5SDimitry Andric // Template type parameters are the most common and never 2550b57cec5SDimitry Andric // contribute to visibility, pack or not. 2560b57cec5SDimitry Andric if (isa<TemplateTypeParmDecl>(P)) 2570b57cec5SDimitry Andric continue; 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric // Non-type template parameters can be restricted by the value type, e.g. 2600b57cec5SDimitry Andric // template <enum X> class A { ... }; 2610b57cec5SDimitry Andric // We have to be careful here, though, because we can be dealing with 2620b57cec5SDimitry Andric // dependent types. 2630b57cec5SDimitry Andric if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { 2640b57cec5SDimitry Andric // Handle the non-pack case first. 2650b57cec5SDimitry Andric if (!NTTP->isExpandedParameterPack()) { 2660b57cec5SDimitry Andric if (!NTTP->getType()->isDependentType()) { 2670b57cec5SDimitry Andric LV.merge(getLVForType(*NTTP->getType(), computation)); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric continue; 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric // Look at all the types in an expanded pack. 2730b57cec5SDimitry Andric for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { 2740b57cec5SDimitry Andric QualType type = NTTP->getExpansionType(i); 2750b57cec5SDimitry Andric if (!type->isDependentType()) 2760b57cec5SDimitry Andric LV.merge(getTypeLinkageAndVisibility(type)); 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric continue; 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric // Template template parameters can be restricted by their 2820b57cec5SDimitry Andric // template parameters, recursively. 2830b57cec5SDimitry Andric const auto *TTP = cast<TemplateTemplateParmDecl>(P); 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric // Handle the non-pack case first. 2860b57cec5SDimitry Andric if (!TTP->isExpandedParameterPack()) { 2870b57cec5SDimitry Andric LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(), 2880b57cec5SDimitry Andric computation)); 2890b57cec5SDimitry Andric continue; 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // Look at all expansions in an expanded pack. 2930b57cec5SDimitry Andric for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); 2940b57cec5SDimitry Andric i != n; ++i) { 2950b57cec5SDimitry Andric LV.merge(getLVForTemplateParameterList( 2960b57cec5SDimitry Andric TTP->getExpansionTemplateParameters(i), computation)); 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric return LV; 3010b57cec5SDimitry Andric } 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric static const Decl *getOutermostFuncOrBlockContext(const Decl *D) { 3040b57cec5SDimitry Andric const Decl *Ret = nullptr; 3050b57cec5SDimitry Andric const DeclContext *DC = D->getDeclContext(); 3060b57cec5SDimitry Andric while (DC->getDeclKind() != Decl::TranslationUnit) { 3070b57cec5SDimitry Andric if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC)) 3080b57cec5SDimitry Andric Ret = cast<Decl>(DC); 3090b57cec5SDimitry Andric DC = DC->getParent(); 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric return Ret; 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric /// Get the most restrictive linkage for the types and 3150b57cec5SDimitry Andric /// declarations in the given template argument list. 3160b57cec5SDimitry Andric /// 3170b57cec5SDimitry Andric /// Note that we don't take an LVComputationKind because we always 3180b57cec5SDimitry Andric /// want to honor the visibility of template arguments in the same way. 3190b57cec5SDimitry Andric LinkageInfo 3200b57cec5SDimitry Andric LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args, 3210b57cec5SDimitry Andric LVComputationKind computation) { 3220b57cec5SDimitry Andric LinkageInfo LV; 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric for (const TemplateArgument &Arg : Args) { 3250b57cec5SDimitry Andric switch (Arg.getKind()) { 3260b57cec5SDimitry Andric case TemplateArgument::Null: 3270b57cec5SDimitry Andric case TemplateArgument::Integral: 3280b57cec5SDimitry Andric case TemplateArgument::Expression: 3290b57cec5SDimitry Andric continue; 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric case TemplateArgument::Type: 3320b57cec5SDimitry Andric LV.merge(getLVForType(*Arg.getAsType(), computation)); 3330b57cec5SDimitry Andric continue; 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric case TemplateArgument::Declaration: { 3360b57cec5SDimitry Andric const NamedDecl *ND = Arg.getAsDecl(); 3370b57cec5SDimitry Andric assert(!usesTypeVisibility(ND)); 3380b57cec5SDimitry Andric LV.merge(getLVForDecl(ND, computation)); 3390b57cec5SDimitry Andric continue; 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric case TemplateArgument::NullPtr: 3430b57cec5SDimitry Andric LV.merge(getTypeLinkageAndVisibility(Arg.getNullPtrType())); 3440b57cec5SDimitry Andric continue; 3450b57cec5SDimitry Andric 346*7a6dacacSDimitry Andric case TemplateArgument::StructuralValue: 347*7a6dacacSDimitry Andric LV.merge(getLVForValue(Arg.getAsStructuralValue(), computation)); 348*7a6dacacSDimitry Andric continue; 349*7a6dacacSDimitry Andric 3500b57cec5SDimitry Andric case TemplateArgument::Template: 3510b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 3520b57cec5SDimitry Andric if (TemplateDecl *Template = 3530b57cec5SDimitry Andric Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) 3540b57cec5SDimitry Andric LV.merge(getLVForDecl(Template, computation)); 3550b57cec5SDimitry Andric continue; 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric case TemplateArgument::Pack: 3580b57cec5SDimitry Andric LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation)); 3590b57cec5SDimitry Andric continue; 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric llvm_unreachable("bad template argument kind"); 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric return LV; 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric LinkageInfo 3680b57cec5SDimitry Andric LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, 3690b57cec5SDimitry Andric LVComputationKind computation) { 3700b57cec5SDimitry Andric return getLVForTemplateArgumentList(TArgs.asArray(), computation); 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn, 3740b57cec5SDimitry Andric const FunctionTemplateSpecializationInfo *specInfo) { 3750b57cec5SDimitry Andric // Include visibility from the template parameters and arguments 3760b57cec5SDimitry Andric // only if this is not an explicit instantiation or specialization 3770b57cec5SDimitry Andric // with direct explicit visibility. (Implicit instantiations won't 3780b57cec5SDimitry Andric // have a direct attribute.) 3790b57cec5SDimitry Andric if (!specInfo->isExplicitInstantiationOrSpecialization()) 3800b57cec5SDimitry Andric return true; 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric return !fn->hasAttr<VisibilityAttr>(); 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric /// Merge in template-related linkage and visibility for the given 3860b57cec5SDimitry Andric /// function template specialization. 3870b57cec5SDimitry Andric /// 3880b57cec5SDimitry Andric /// We don't need a computation kind here because we can assume 3890b57cec5SDimitry Andric /// LVForValue. 3900b57cec5SDimitry Andric /// 3910b57cec5SDimitry Andric /// \param[out] LV the computation to use for the parent 3920b57cec5SDimitry Andric void LinkageComputer::mergeTemplateLV( 3930b57cec5SDimitry Andric LinkageInfo &LV, const FunctionDecl *fn, 3940b57cec5SDimitry Andric const FunctionTemplateSpecializationInfo *specInfo, 3950b57cec5SDimitry Andric LVComputationKind computation) { 3960b57cec5SDimitry Andric bool considerVisibility = 3970b57cec5SDimitry Andric shouldConsiderTemplateVisibility(fn, specInfo); 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric FunctionTemplateDecl *temp = specInfo->getTemplate(); 40081ad6265SDimitry Andric // Merge information from the template declaration. 40181ad6265SDimitry Andric LinkageInfo tempLV = getLVForDecl(temp, computation); 40281ad6265SDimitry Andric // The linkage of the specialization should be consistent with the 40381ad6265SDimitry Andric // template declaration. 40481ad6265SDimitry Andric LV.setLinkage(tempLV.getLinkage()); 40581ad6265SDimitry Andric 40681ad6265SDimitry Andric // Merge information from the template parameters. 40781ad6265SDimitry Andric LinkageInfo paramsLV = 4080b57cec5SDimitry Andric getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 40981ad6265SDimitry Andric LV.mergeMaybeWithVisibility(paramsLV, considerVisibility); 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric // Merge information from the template arguments. 4120b57cec5SDimitry Andric const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; 4130b57cec5SDimitry Andric LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); 4140b57cec5SDimitry Andric LV.mergeMaybeWithVisibility(argsLV, considerVisibility); 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric /// Does the given declaration have a direct visibility attribute 4180b57cec5SDimitry Andric /// that would match the given rules? 4190b57cec5SDimitry Andric static bool hasDirectVisibilityAttribute(const NamedDecl *D, 4200b57cec5SDimitry Andric LVComputationKind computation) { 4210b57cec5SDimitry Andric if (computation.IgnoreAllVisibility) 4220b57cec5SDimitry Andric return false; 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric return (computation.isTypeVisibility() && D->hasAttr<TypeVisibilityAttr>()) || 4250b57cec5SDimitry Andric D->hasAttr<VisibilityAttr>(); 4260b57cec5SDimitry Andric } 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric /// Should we consider visibility associated with the template 4290b57cec5SDimitry Andric /// arguments and parameters of the given class template specialization? 4300b57cec5SDimitry Andric static bool shouldConsiderTemplateVisibility( 4310b57cec5SDimitry Andric const ClassTemplateSpecializationDecl *spec, 4320b57cec5SDimitry Andric LVComputationKind computation) { 4330b57cec5SDimitry Andric // Include visibility from the template parameters and arguments 4340b57cec5SDimitry Andric // only if this is not an explicit instantiation or specialization 4350b57cec5SDimitry Andric // with direct explicit visibility (and note that implicit 4360b57cec5SDimitry Andric // instantiations won't have a direct attribute). 4370b57cec5SDimitry Andric // 4380b57cec5SDimitry Andric // Furthermore, we want to ignore template parameters and arguments 4390b57cec5SDimitry Andric // for an explicit specialization when computing the visibility of a 4400b57cec5SDimitry Andric // member thereof with explicit visibility. 4410b57cec5SDimitry Andric // 4420b57cec5SDimitry Andric // This is a bit complex; let's unpack it. 4430b57cec5SDimitry Andric // 4440b57cec5SDimitry Andric // An explicit class specialization is an independent, top-level 4450b57cec5SDimitry Andric // declaration. As such, if it or any of its members has an 4460b57cec5SDimitry Andric // explicit visibility attribute, that must directly express the 4470b57cec5SDimitry Andric // user's intent, and we should honor it. The same logic applies to 4480b57cec5SDimitry Andric // an explicit instantiation of a member of such a thing. 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric // Fast path: if this is not an explicit instantiation or 4510b57cec5SDimitry Andric // specialization, we always want to consider template-related 4520b57cec5SDimitry Andric // visibility restrictions. 4530b57cec5SDimitry Andric if (!spec->isExplicitInstantiationOrSpecialization()) 4540b57cec5SDimitry Andric return true; 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric // This is the 'member thereof' check. 4570b57cec5SDimitry Andric if (spec->isExplicitSpecialization() && 4580b57cec5SDimitry Andric hasExplicitVisibilityAlready(computation)) 4590b57cec5SDimitry Andric return false; 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric return !hasDirectVisibilityAttribute(spec, computation); 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric /// Merge in template-related linkage and visibility for the given 4650b57cec5SDimitry Andric /// class template specialization. 4660b57cec5SDimitry Andric void LinkageComputer::mergeTemplateLV( 4670b57cec5SDimitry Andric LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec, 4680b57cec5SDimitry Andric LVComputationKind computation) { 4690b57cec5SDimitry Andric bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric // Merge information from the template parameters, but ignore 4720b57cec5SDimitry Andric // visibility if we're only considering template arguments. 4730b57cec5SDimitry Andric ClassTemplateDecl *temp = spec->getSpecializedTemplate(); 474972a253aSDimitry Andric // Merge information from the template declaration. 475972a253aSDimitry Andric LinkageInfo tempLV = getLVForDecl(temp, computation); 476972a253aSDimitry Andric // The linkage of the specialization should be consistent with the 477972a253aSDimitry Andric // template declaration. 478972a253aSDimitry Andric LV.setLinkage(tempLV.getLinkage()); 479972a253aSDimitry Andric 480972a253aSDimitry Andric LinkageInfo paramsLV = 4810b57cec5SDimitry Andric getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 482972a253aSDimitry Andric LV.mergeMaybeWithVisibility(paramsLV, 4830b57cec5SDimitry Andric considerVisibility && !hasExplicitVisibilityAlready(computation)); 4840b57cec5SDimitry Andric 4850b57cec5SDimitry Andric // Merge information from the template arguments. We ignore 4860b57cec5SDimitry Andric // template-argument visibility if we've got an explicit 4870b57cec5SDimitry Andric // instantiation with a visibility attribute. 4880b57cec5SDimitry Andric const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); 4890b57cec5SDimitry Andric LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); 4900b57cec5SDimitry Andric if (considerVisibility) 4910b57cec5SDimitry Andric LV.mergeVisibility(argsLV); 4920b57cec5SDimitry Andric LV.mergeExternalVisibility(argsLV); 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric /// Should we consider visibility associated with the template 4960b57cec5SDimitry Andric /// arguments and parameters of the given variable template 4970b57cec5SDimitry Andric /// specialization? As usual, follow class template specialization 4980b57cec5SDimitry Andric /// logic up to initialization. 4990b57cec5SDimitry Andric static bool shouldConsiderTemplateVisibility( 5000b57cec5SDimitry Andric const VarTemplateSpecializationDecl *spec, 5010b57cec5SDimitry Andric LVComputationKind computation) { 5020b57cec5SDimitry Andric // Include visibility from the template parameters and arguments 5030b57cec5SDimitry Andric // only if this is not an explicit instantiation or specialization 5040b57cec5SDimitry Andric // with direct explicit visibility (and note that implicit 5050b57cec5SDimitry Andric // instantiations won't have a direct attribute). 5060b57cec5SDimitry Andric if (!spec->isExplicitInstantiationOrSpecialization()) 5070b57cec5SDimitry Andric return true; 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric // An explicit variable specialization is an independent, top-level 5100b57cec5SDimitry Andric // declaration. As such, if it has an explicit visibility attribute, 5110b57cec5SDimitry Andric // that must directly express the user's intent, and we should honor 5120b57cec5SDimitry Andric // it. 5130b57cec5SDimitry Andric if (spec->isExplicitSpecialization() && 5140b57cec5SDimitry Andric hasExplicitVisibilityAlready(computation)) 5150b57cec5SDimitry Andric return false; 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric return !hasDirectVisibilityAttribute(spec, computation); 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric /// Merge in template-related linkage and visibility for the given 5210b57cec5SDimitry Andric /// variable template specialization. As usual, follow class template 5220b57cec5SDimitry Andric /// specialization logic up to initialization. 5230b57cec5SDimitry Andric void LinkageComputer::mergeTemplateLV(LinkageInfo &LV, 5240b57cec5SDimitry Andric const VarTemplateSpecializationDecl *spec, 5250b57cec5SDimitry Andric LVComputationKind computation) { 5260b57cec5SDimitry Andric bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric // Merge information from the template parameters, but ignore 5290b57cec5SDimitry Andric // visibility if we're only considering template arguments. 5300b57cec5SDimitry Andric VarTemplateDecl *temp = spec->getSpecializedTemplate(); 5310b57cec5SDimitry Andric LinkageInfo tempLV = 5320b57cec5SDimitry Andric getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 5330b57cec5SDimitry Andric LV.mergeMaybeWithVisibility(tempLV, 5340b57cec5SDimitry Andric considerVisibility && !hasExplicitVisibilityAlready(computation)); 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric // Merge information from the template arguments. We ignore 5370b57cec5SDimitry Andric // template-argument visibility if we've got an explicit 5380b57cec5SDimitry Andric // instantiation with a visibility attribute. 5390b57cec5SDimitry Andric const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); 5400b57cec5SDimitry Andric LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); 5410b57cec5SDimitry Andric if (considerVisibility) 5420b57cec5SDimitry Andric LV.mergeVisibility(argsLV); 5430b57cec5SDimitry Andric LV.mergeExternalVisibility(argsLV); 5440b57cec5SDimitry Andric } 5450b57cec5SDimitry Andric 5460b57cec5SDimitry Andric static bool useInlineVisibilityHidden(const NamedDecl *D) { 5470b57cec5SDimitry Andric // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. 5480b57cec5SDimitry Andric const LangOptions &Opts = D->getASTContext().getLangOpts(); 5490b57cec5SDimitry Andric if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) 5500b57cec5SDimitry Andric return false; 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric const auto *FD = dyn_cast<FunctionDecl>(D); 5530b57cec5SDimitry Andric if (!FD) 5540b57cec5SDimitry Andric return false; 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric TemplateSpecializationKind TSK = TSK_Undeclared; 5570b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *spec 5580b57cec5SDimitry Andric = FD->getTemplateSpecializationInfo()) { 5590b57cec5SDimitry Andric TSK = spec->getTemplateSpecializationKind(); 5600b57cec5SDimitry Andric } else if (MemberSpecializationInfo *MSI = 5610b57cec5SDimitry Andric FD->getMemberSpecializationInfo()) { 5620b57cec5SDimitry Andric TSK = MSI->getTemplateSpecializationKind(); 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric const FunctionDecl *Def = nullptr; 5660b57cec5SDimitry Andric // InlineVisibilityHidden only applies to definitions, and 5670b57cec5SDimitry Andric // isInlined() only gives meaningful answers on definitions 5680b57cec5SDimitry Andric // anyway. 5690b57cec5SDimitry Andric return TSK != TSK_ExplicitInstantiationDeclaration && 5700b57cec5SDimitry Andric TSK != TSK_ExplicitInstantiationDefinition && 5710b57cec5SDimitry Andric FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); 5720b57cec5SDimitry Andric } 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric template <typename T> static bool isFirstInExternCContext(T *D) { 5750b57cec5SDimitry Andric const T *First = D->getFirstDecl(); 5760b57cec5SDimitry Andric return First->isInExternCContext(); 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric static bool isSingleLineLanguageLinkage(const Decl &D) { 5800b57cec5SDimitry Andric if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext())) 5810b57cec5SDimitry Andric if (!SD->hasBraces()) 5820b57cec5SDimitry Andric return true; 5830b57cec5SDimitry Andric return false; 5840b57cec5SDimitry Andric } 5850b57cec5SDimitry Andric 58606c3fb27SDimitry Andric static bool isDeclaredInModuleInterfaceOrPartition(const NamedDecl *D) { 58706c3fb27SDimitry Andric if (auto *M = D->getOwningModule()) 58806c3fb27SDimitry Andric return M->isInterfaceOrPartition(); 58906c3fb27SDimitry Andric return false; 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andric static LinkageInfo getExternalLinkageFor(const NamedDecl *D) { 5930b57cec5SDimitry Andric return LinkageInfo::external(); 5940b57cec5SDimitry Andric } 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric static StorageClass getStorageClass(const Decl *D) { 5970b57cec5SDimitry Andric if (auto *TD = dyn_cast<TemplateDecl>(D)) 5980b57cec5SDimitry Andric D = TD->getTemplatedDecl(); 5990b57cec5SDimitry Andric if (D) { 6000b57cec5SDimitry Andric if (auto *VD = dyn_cast<VarDecl>(D)) 6010b57cec5SDimitry Andric return VD->getStorageClass(); 6020b57cec5SDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(D)) 6030b57cec5SDimitry Andric return FD->getStorageClass(); 6040b57cec5SDimitry Andric } 6050b57cec5SDimitry Andric return SC_None; 6060b57cec5SDimitry Andric } 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric LinkageInfo 6090b57cec5SDimitry Andric LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, 6100b57cec5SDimitry Andric LVComputationKind computation, 6110b57cec5SDimitry Andric bool IgnoreVarTypeLinkage) { 6120b57cec5SDimitry Andric assert(D->getDeclContext()->getRedeclContext()->isFileContext() && 6130b57cec5SDimitry Andric "Not a name having namespace scope"); 6140b57cec5SDimitry Andric ASTContext &Context = D->getASTContext(); 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric // C++ [basic.link]p3: 6170b57cec5SDimitry Andric // A name having namespace scope (3.3.6) has internal linkage if it 6180b57cec5SDimitry Andric // is the name of 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric if (getStorageClass(D->getCanonicalDecl()) == SC_Static) { 6210b57cec5SDimitry Andric // - a variable, variable template, function, or function template 6220b57cec5SDimitry Andric // that is explicitly declared static; or 6230b57cec5SDimitry Andric // (This bullet corresponds to C99 6.2.2p3.) 62406c3fb27SDimitry Andric return LinkageInfo::internal(); 6250b57cec5SDimitry Andric } 6260b57cec5SDimitry Andric 6270b57cec5SDimitry Andric if (const auto *Var = dyn_cast<VarDecl>(D)) { 6280b57cec5SDimitry Andric // - a non-template variable of non-volatile const-qualified type, unless 6290b57cec5SDimitry Andric // - it is explicitly declared extern, or 63006c3fb27SDimitry Andric // - it is declared in the purview of a module interface unit 63106c3fb27SDimitry Andric // (outside the private-module-fragment, if any) or module partition, or 63206c3fb27SDimitry Andric // - it is inline, or 6330b57cec5SDimitry Andric // - it was previously declared and the prior declaration did not have 6340b57cec5SDimitry Andric // internal linkage 6350b57cec5SDimitry Andric // (There is no equivalent in C99.) 63606c3fb27SDimitry Andric if (Context.getLangOpts().CPlusPlus && Var->getType().isConstQualified() && 63706c3fb27SDimitry Andric !Var->getType().isVolatileQualified() && !Var->isInline() && 63806c3fb27SDimitry Andric !isDeclaredInModuleInterfaceOrPartition(Var) && 6390b57cec5SDimitry Andric !isa<VarTemplateSpecializationDecl>(Var) && 6400b57cec5SDimitry Andric !Var->getDescribedVarTemplate()) { 6410b57cec5SDimitry Andric const VarDecl *PrevVar = Var->getPreviousDecl(); 6420b57cec5SDimitry Andric if (PrevVar) 6430b57cec5SDimitry Andric return getLVForDecl(PrevVar, computation); 6440b57cec5SDimitry Andric 6450b57cec5SDimitry Andric if (Var->getStorageClass() != SC_Extern && 6460b57cec5SDimitry Andric Var->getStorageClass() != SC_PrivateExtern && 6470b57cec5SDimitry Andric !isSingleLineLanguageLinkage(*Var)) 64806c3fb27SDimitry Andric return LinkageInfo::internal(); 6490b57cec5SDimitry Andric } 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar; 6520b57cec5SDimitry Andric PrevVar = PrevVar->getPreviousDecl()) { 6530b57cec5SDimitry Andric if (PrevVar->getStorageClass() == SC_PrivateExtern && 6540b57cec5SDimitry Andric Var->getStorageClass() == SC_None) 6550b57cec5SDimitry Andric return getDeclLinkageAndVisibility(PrevVar); 6560b57cec5SDimitry Andric // Explicitly declared static. 6570b57cec5SDimitry Andric if (PrevVar->getStorageClass() == SC_Static) 65806c3fb27SDimitry Andric return LinkageInfo::internal(); 6590b57cec5SDimitry Andric } 6600b57cec5SDimitry Andric } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) { 6610b57cec5SDimitry Andric // - a data member of an anonymous union. 6620b57cec5SDimitry Andric const VarDecl *VD = IFD->getVarDecl(); 6630b57cec5SDimitry Andric assert(VD && "Expected a VarDecl in this IndirectFieldDecl!"); 6640b57cec5SDimitry Andric return getLVForNamespaceScopeDecl(VD, computation, IgnoreVarTypeLinkage); 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!"); 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric // FIXME: This gives internal linkage to names that should have no linkage 6690b57cec5SDimitry Andric // (those not covered by [basic.link]p6). 6700b57cec5SDimitry Andric if (D->isInAnonymousNamespace()) { 6710b57cec5SDimitry Andric const auto *Var = dyn_cast<VarDecl>(D); 6720b57cec5SDimitry Andric const auto *Func = dyn_cast<FunctionDecl>(D); 6730b57cec5SDimitry Andric // FIXME: The check for extern "C" here is not justified by the standard 6740b57cec5SDimitry Andric // wording, but we retain it from the pre-DR1113 model to avoid breaking 6750b57cec5SDimitry Andric // code. 6760b57cec5SDimitry Andric // 6770b57cec5SDimitry Andric // C++11 [basic.link]p4: 6780b57cec5SDimitry Andric // An unnamed namespace or a namespace declared directly or indirectly 6790b57cec5SDimitry Andric // within an unnamed namespace has internal linkage. 6800b57cec5SDimitry Andric if ((!Var || !isFirstInExternCContext(Var)) && 6810b57cec5SDimitry Andric (!Func || !isFirstInExternCContext(Func))) 68206c3fb27SDimitry Andric return LinkageInfo::internal(); 6830b57cec5SDimitry Andric } 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric // Set up the defaults. 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric // C99 6.2.2p5: 6880b57cec5SDimitry Andric // If the declaration of an identifier for an object has file 6890b57cec5SDimitry Andric // scope and no storage-class specifier, its linkage is 6900b57cec5SDimitry Andric // external. 6910b57cec5SDimitry Andric LinkageInfo LV = getExternalLinkageFor(D); 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric if (!hasExplicitVisibilityAlready(computation)) { 694bdd1243dSDimitry Andric if (std::optional<Visibility> Vis = getExplicitVisibility(D, computation)) { 6950b57cec5SDimitry Andric LV.mergeVisibility(*Vis, true); 6960b57cec5SDimitry Andric } else { 6970b57cec5SDimitry Andric // If we're declared in a namespace with a visibility attribute, 6980b57cec5SDimitry Andric // use that namespace's visibility, and it still counts as explicit. 6990b57cec5SDimitry Andric for (const DeclContext *DC = D->getDeclContext(); 7000b57cec5SDimitry Andric !isa<TranslationUnitDecl>(DC); 7010b57cec5SDimitry Andric DC = DC->getParent()) { 7020b57cec5SDimitry Andric const auto *ND = dyn_cast<NamespaceDecl>(DC); 7030b57cec5SDimitry Andric if (!ND) continue; 704bdd1243dSDimitry Andric if (std::optional<Visibility> Vis = 705bdd1243dSDimitry Andric getExplicitVisibility(ND, computation)) { 7060b57cec5SDimitry Andric LV.mergeVisibility(*Vis, true); 7070b57cec5SDimitry Andric break; 7080b57cec5SDimitry Andric } 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric } 7110b57cec5SDimitry Andric 7120b57cec5SDimitry Andric // Add in global settings if the above didn't give us direct visibility. 7130b57cec5SDimitry Andric if (!LV.isVisibilityExplicit()) { 7140b57cec5SDimitry Andric // Use global type/value visibility as appropriate. 7150b57cec5SDimitry Andric Visibility globalVisibility = 7160b57cec5SDimitry Andric computation.isValueVisibility() 7170b57cec5SDimitry Andric ? Context.getLangOpts().getValueVisibilityMode() 7180b57cec5SDimitry Andric : Context.getLangOpts().getTypeVisibilityMode(); 7190b57cec5SDimitry Andric LV.mergeVisibility(globalVisibility, /*explicit*/ false); 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric // If we're paying attention to global visibility, apply 7220b57cec5SDimitry Andric // -finline-visibility-hidden if this is an inline method. 7230b57cec5SDimitry Andric if (useInlineVisibilityHidden(D)) 7240b57cec5SDimitry Andric LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); 7250b57cec5SDimitry Andric } 7260b57cec5SDimitry Andric } 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric // C++ [basic.link]p4: 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric // A name having namespace scope that has not been given internal linkage 7310b57cec5SDimitry Andric // above and that is the name of 7320b57cec5SDimitry Andric // [...bullets...] 7330b57cec5SDimitry Andric // has its linkage determined as follows: 7340b57cec5SDimitry Andric // - if the enclosing namespace has internal linkage, the name has 7350b57cec5SDimitry Andric // internal linkage; [handled above] 7360b57cec5SDimitry Andric // - otherwise, if the declaration of the name is attached to a named 7370b57cec5SDimitry Andric // module and is not exported, the name has module linkage; 7380b57cec5SDimitry Andric // - otherwise, the name has external linkage. 7390b57cec5SDimitry Andric // LV is currently set up to handle the last two bullets. 7400b57cec5SDimitry Andric // 7410b57cec5SDimitry Andric // The bullets are: 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric // - a variable; or 7440b57cec5SDimitry Andric if (const auto *Var = dyn_cast<VarDecl>(D)) { 7450b57cec5SDimitry Andric // GCC applies the following optimization to variables and static 7460b57cec5SDimitry Andric // data members, but not to functions: 7470b57cec5SDimitry Andric // 7480b57cec5SDimitry Andric // Modify the variable's LV by the LV of its type unless this is 7490b57cec5SDimitry Andric // C or extern "C". This follows from [basic.link]p9: 7500b57cec5SDimitry Andric // A type without linkage shall not be used as the type of a 7510b57cec5SDimitry Andric // variable or function with external linkage unless 7520b57cec5SDimitry Andric // - the entity has C language linkage, or 7530b57cec5SDimitry Andric // - the entity is declared within an unnamed namespace, or 7540b57cec5SDimitry Andric // - the entity is not used or is defined in the same 7550b57cec5SDimitry Andric // translation unit. 7560b57cec5SDimitry Andric // and [basic.link]p10: 7570b57cec5SDimitry Andric // ...the types specified by all declarations referring to a 7580b57cec5SDimitry Andric // given variable or function shall be identical... 7590b57cec5SDimitry Andric // C does not have an equivalent rule. 7600b57cec5SDimitry Andric // 7610b57cec5SDimitry Andric // Ignore this if we've got an explicit attribute; the user 7620b57cec5SDimitry Andric // probably knows what they're doing. 7630b57cec5SDimitry Andric // 7640b57cec5SDimitry Andric // Note that we don't want to make the variable non-external 7650b57cec5SDimitry Andric // because of this, but unique-external linkage suits us. 76604eeddc0SDimitry Andric 7670b57cec5SDimitry Andric if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var) && 7680b57cec5SDimitry Andric !IgnoreVarTypeLinkage) { 7690b57cec5SDimitry Andric LinkageInfo TypeLV = getLVForType(*Var->getType(), computation); 7700b57cec5SDimitry Andric if (!isExternallyVisible(TypeLV.getLinkage())) 7710b57cec5SDimitry Andric return LinkageInfo::uniqueExternal(); 7720b57cec5SDimitry Andric if (!LV.isVisibilityExplicit()) 7730b57cec5SDimitry Andric LV.mergeVisibility(TypeLV); 7740b57cec5SDimitry Andric } 7750b57cec5SDimitry Andric 7760b57cec5SDimitry Andric if (Var->getStorageClass() == SC_PrivateExtern) 7770b57cec5SDimitry Andric LV.mergeVisibility(HiddenVisibility, true); 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric // Note that Sema::MergeVarDecl already takes care of implementing 7800b57cec5SDimitry Andric // C99 6.2.2p4 and propagating the visibility attribute, so we don't have 7810b57cec5SDimitry Andric // to do it here. 7820b57cec5SDimitry Andric 7830b57cec5SDimitry Andric // As per function and class template specializations (below), 7840b57cec5SDimitry Andric // consider LV for the template and template arguments. We're at file 7850b57cec5SDimitry Andric // scope, so we do not need to worry about nested specializations. 7860b57cec5SDimitry Andric if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Var)) { 7870b57cec5SDimitry Andric mergeTemplateLV(LV, spec, computation); 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric // - a function; or 7910b57cec5SDimitry Andric } else if (const auto *Function = dyn_cast<FunctionDecl>(D)) { 7920b57cec5SDimitry Andric // In theory, we can modify the function's LV by the LV of its 7930b57cec5SDimitry Andric // type unless it has C linkage (see comment above about variables 7940b57cec5SDimitry Andric // for justification). In practice, GCC doesn't do this, so it's 7950b57cec5SDimitry Andric // just too painful to make work. 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric if (Function->getStorageClass() == SC_PrivateExtern) 7980b57cec5SDimitry Andric LV.mergeVisibility(HiddenVisibility, true); 7990b57cec5SDimitry Andric 800bdd1243dSDimitry Andric // OpenMP target declare device functions are not callable from the host so 801bdd1243dSDimitry Andric // they should not be exported from the device image. This applies to all 802bdd1243dSDimitry Andric // functions as the host-callable kernel functions are emitted at codegen. 80306c3fb27SDimitry Andric if (Context.getLangOpts().OpenMP && 80406c3fb27SDimitry Andric Context.getLangOpts().OpenMPIsTargetDevice && 805bdd1243dSDimitry Andric ((Context.getTargetInfo().getTriple().isAMDGPU() || 806bdd1243dSDimitry Andric Context.getTargetInfo().getTriple().isNVPTX()) || 807bdd1243dSDimitry Andric OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Function))) 808bdd1243dSDimitry Andric LV.mergeVisibility(HiddenVisibility, /*newExplicit=*/false); 809bdd1243dSDimitry Andric 8100b57cec5SDimitry Andric // Note that Sema::MergeCompatibleFunctionDecls already takes care of 8110b57cec5SDimitry Andric // merging storage classes and visibility attributes, so we don't have to 8120b57cec5SDimitry Andric // look at previous decls in here. 8130b57cec5SDimitry Andric 8140b57cec5SDimitry Andric // In C++, then if the type of the function uses a type with 8150b57cec5SDimitry Andric // unique-external linkage, it's not legally usable from outside 8160b57cec5SDimitry Andric // this translation unit. However, we should use the C linkage 8170b57cec5SDimitry Andric // rules instead for extern "C" declarations. 8180b57cec5SDimitry Andric if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Function)) { 8190b57cec5SDimitry Andric // Only look at the type-as-written. Otherwise, deducing the return type 8200b57cec5SDimitry Andric // of a function could change its linkage. 8210b57cec5SDimitry Andric QualType TypeAsWritten = Function->getType(); 8220b57cec5SDimitry Andric if (TypeSourceInfo *TSI = Function->getTypeSourceInfo()) 8230b57cec5SDimitry Andric TypeAsWritten = TSI->getType(); 8240b57cec5SDimitry Andric if (!isExternallyVisible(TypeAsWritten->getLinkage())) 8250b57cec5SDimitry Andric return LinkageInfo::uniqueExternal(); 8260b57cec5SDimitry Andric } 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric // Consider LV from the template and the template arguments. 8290b57cec5SDimitry Andric // We're at file scope, so we do not need to worry about nested 8300b57cec5SDimitry Andric // specializations. 8310b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *specInfo 8320b57cec5SDimitry Andric = Function->getTemplateSpecializationInfo()) { 8330b57cec5SDimitry Andric mergeTemplateLV(LV, Function, specInfo, computation); 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric // - a named class (Clause 9), or an unnamed class defined in a 8370b57cec5SDimitry Andric // typedef declaration in which the class has the typedef name 8380b57cec5SDimitry Andric // for linkage purposes (7.1.3); or 8390b57cec5SDimitry Andric // - a named enumeration (7.2), or an unnamed enumeration 8400b57cec5SDimitry Andric // defined in a typedef declaration in which the enumeration 8410b57cec5SDimitry Andric // has the typedef name for linkage purposes (7.1.3); or 8420b57cec5SDimitry Andric } else if (const auto *Tag = dyn_cast<TagDecl>(D)) { 8430b57cec5SDimitry Andric // Unnamed tags have no linkage. 8440b57cec5SDimitry Andric if (!Tag->hasNameForLinkage()) 8450b57cec5SDimitry Andric return LinkageInfo::none(); 8460b57cec5SDimitry Andric 8470b57cec5SDimitry Andric // If this is a class template specialization, consider the 8480b57cec5SDimitry Andric // linkage of the template and template arguments. We're at file 8490b57cec5SDimitry Andric // scope, so we do not need to worry about nested specializations. 8500b57cec5SDimitry Andric if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { 8510b57cec5SDimitry Andric mergeTemplateLV(LV, spec, computation); 8520b57cec5SDimitry Andric } 8530b57cec5SDimitry Andric 8540b57cec5SDimitry Andric // FIXME: This is not part of the C++ standard any more. 8550b57cec5SDimitry Andric // - an enumerator belonging to an enumeration with external linkage; or 8560b57cec5SDimitry Andric } else if (isa<EnumConstantDecl>(D)) { 8570b57cec5SDimitry Andric LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), 8580b57cec5SDimitry Andric computation); 8590b57cec5SDimitry Andric if (!isExternalFormalLinkage(EnumLV.getLinkage())) 8600b57cec5SDimitry Andric return LinkageInfo::none(); 8610b57cec5SDimitry Andric LV.merge(EnumLV); 8620b57cec5SDimitry Andric 8630b57cec5SDimitry Andric // - a template 8640b57cec5SDimitry Andric } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { 8650b57cec5SDimitry Andric bool considerVisibility = !hasExplicitVisibilityAlready(computation); 8660b57cec5SDimitry Andric LinkageInfo tempLV = 8670b57cec5SDimitry Andric getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 8680b57cec5SDimitry Andric LV.mergeMaybeWithVisibility(tempLV, considerVisibility); 8690b57cec5SDimitry Andric 8700b57cec5SDimitry Andric // An unnamed namespace or a namespace declared directly or indirectly 8710b57cec5SDimitry Andric // within an unnamed namespace has internal linkage. All other namespaces 8720b57cec5SDimitry Andric // have external linkage. 8730b57cec5SDimitry Andric // 8740b57cec5SDimitry Andric // We handled names in anonymous namespaces above. 8750b57cec5SDimitry Andric } else if (isa<NamespaceDecl>(D)) { 8760b57cec5SDimitry Andric return LV; 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric // By extension, we assign external linkage to Objective-C 8790b57cec5SDimitry Andric // interfaces. 8800b57cec5SDimitry Andric } else if (isa<ObjCInterfaceDecl>(D)) { 8810b57cec5SDimitry Andric // fallout 8820b57cec5SDimitry Andric 8830b57cec5SDimitry Andric } else if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { 8840b57cec5SDimitry Andric // A typedef declaration has linkage if it gives a type a name for 8850b57cec5SDimitry Andric // linkage purposes. 8860b57cec5SDimitry Andric if (!TD->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) 8870b57cec5SDimitry Andric return LinkageInfo::none(); 8880b57cec5SDimitry Andric 8895ffd83dbSDimitry Andric } else if (isa<MSGuidDecl>(D)) { 8905ffd83dbSDimitry Andric // A GUID behaves like an inline variable with external linkage. Fall 8915ffd83dbSDimitry Andric // through. 8925ffd83dbSDimitry Andric 8930b57cec5SDimitry Andric // Everything not covered here has no linkage. 8940b57cec5SDimitry Andric } else { 8950b57cec5SDimitry Andric return LinkageInfo::none(); 8960b57cec5SDimitry Andric } 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric // If we ended up with non-externally-visible linkage, visibility should 8990b57cec5SDimitry Andric // always be default. 9000b57cec5SDimitry Andric if (!isExternallyVisible(LV.getLinkage())) 9010b57cec5SDimitry Andric return LinkageInfo(LV.getLinkage(), DefaultVisibility, false); 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric return LV; 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric 9060b57cec5SDimitry Andric LinkageInfo 9070b57cec5SDimitry Andric LinkageComputer::getLVForClassMember(const NamedDecl *D, 9080b57cec5SDimitry Andric LVComputationKind computation, 9090b57cec5SDimitry Andric bool IgnoreVarTypeLinkage) { 9100b57cec5SDimitry Andric // Only certain class members have linkage. Note that fields don't 9110b57cec5SDimitry Andric // really have linkage, but it's convenient to say they do for the 9120b57cec5SDimitry Andric // purposes of calculating linkage of pointer-to-data-member 9130b57cec5SDimitry Andric // template arguments. 9140b57cec5SDimitry Andric // 9150b57cec5SDimitry Andric // Templates also don't officially have linkage, but since we ignore 9160b57cec5SDimitry Andric // the C++ standard and look at template arguments when determining 9170b57cec5SDimitry Andric // linkage and visibility of a template specialization, we might hit 9180b57cec5SDimitry Andric // a template template argument that way. If we do, we need to 9190b57cec5SDimitry Andric // consider its linkage. 9200b57cec5SDimitry Andric if (!(isa<CXXMethodDecl>(D) || 9210b57cec5SDimitry Andric isa<VarDecl>(D) || 9220b57cec5SDimitry Andric isa<FieldDecl>(D) || 9230b57cec5SDimitry Andric isa<IndirectFieldDecl>(D) || 9240b57cec5SDimitry Andric isa<TagDecl>(D) || 9250b57cec5SDimitry Andric isa<TemplateDecl>(D))) 9260b57cec5SDimitry Andric return LinkageInfo::none(); 9270b57cec5SDimitry Andric 9280b57cec5SDimitry Andric LinkageInfo LV; 9290b57cec5SDimitry Andric 9300b57cec5SDimitry Andric // If we have an explicit visibility attribute, merge that in. 9310b57cec5SDimitry Andric if (!hasExplicitVisibilityAlready(computation)) { 932bdd1243dSDimitry Andric if (std::optional<Visibility> Vis = getExplicitVisibility(D, computation)) 9330b57cec5SDimitry Andric LV.mergeVisibility(*Vis, true); 9340b57cec5SDimitry Andric // If we're paying attention to global visibility, apply 9350b57cec5SDimitry Andric // -finline-visibility-hidden if this is an inline method. 9360b57cec5SDimitry Andric // 9370b57cec5SDimitry Andric // Note that we do this before merging information about 9380b57cec5SDimitry Andric // the class visibility. 9390b57cec5SDimitry Andric if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D)) 9400b57cec5SDimitry Andric LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); 9410b57cec5SDimitry Andric } 9420b57cec5SDimitry Andric 9430b57cec5SDimitry Andric // If this class member has an explicit visibility attribute, the only 9440b57cec5SDimitry Andric // thing that can change its visibility is the template arguments, so 9450b57cec5SDimitry Andric // only look for them when processing the class. 9460b57cec5SDimitry Andric LVComputationKind classComputation = computation; 9470b57cec5SDimitry Andric if (LV.isVisibilityExplicit()) 9480b57cec5SDimitry Andric classComputation = withExplicitVisibilityAlready(computation); 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric LinkageInfo classLV = 9510b57cec5SDimitry Andric getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation); 9520b57cec5SDimitry Andric // The member has the same linkage as the class. If that's not externally 9530b57cec5SDimitry Andric // visible, we don't need to compute anything about the linkage. 9540b57cec5SDimitry Andric // FIXME: If we're only computing linkage, can we bail out here? 9550b57cec5SDimitry Andric if (!isExternallyVisible(classLV.getLinkage())) 9560b57cec5SDimitry Andric return classLV; 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric 9590b57cec5SDimitry Andric // Otherwise, don't merge in classLV yet, because in certain cases 9600b57cec5SDimitry Andric // we need to completely ignore the visibility from it. 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric // Specifically, if this decl exists and has an explicit attribute. 9630b57cec5SDimitry Andric const NamedDecl *explicitSpecSuppressor = nullptr; 9640b57cec5SDimitry Andric 9650b57cec5SDimitry Andric if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { 9660b57cec5SDimitry Andric // Only look at the type-as-written. Otherwise, deducing the return type 9670b57cec5SDimitry Andric // of a function could change its linkage. 9680b57cec5SDimitry Andric QualType TypeAsWritten = MD->getType(); 9690b57cec5SDimitry Andric if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) 9700b57cec5SDimitry Andric TypeAsWritten = TSI->getType(); 9710b57cec5SDimitry Andric if (!isExternallyVisible(TypeAsWritten->getLinkage())) 9720b57cec5SDimitry Andric return LinkageInfo::uniqueExternal(); 9730b57cec5SDimitry Andric 9740b57cec5SDimitry Andric // If this is a method template specialization, use the linkage for 9750b57cec5SDimitry Andric // the template parameters and arguments. 9760b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *spec 9770b57cec5SDimitry Andric = MD->getTemplateSpecializationInfo()) { 9780b57cec5SDimitry Andric mergeTemplateLV(LV, MD, spec, computation); 9790b57cec5SDimitry Andric if (spec->isExplicitSpecialization()) { 9800b57cec5SDimitry Andric explicitSpecSuppressor = MD; 9810b57cec5SDimitry Andric } else if (isExplicitMemberSpecialization(spec->getTemplate())) { 9820b57cec5SDimitry Andric explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl(); 9830b57cec5SDimitry Andric } 9840b57cec5SDimitry Andric } else if (isExplicitMemberSpecialization(MD)) { 9850b57cec5SDimitry Andric explicitSpecSuppressor = MD; 9860b57cec5SDimitry Andric } 9870b57cec5SDimitry Andric 988bdd1243dSDimitry Andric // OpenMP target declare device functions are not callable from the host so 989bdd1243dSDimitry Andric // they should not be exported from the device image. This applies to all 990bdd1243dSDimitry Andric // functions as the host-callable kernel functions are emitted at codegen. 991bdd1243dSDimitry Andric ASTContext &Context = D->getASTContext(); 99206c3fb27SDimitry Andric if (Context.getLangOpts().OpenMP && 99306c3fb27SDimitry Andric Context.getLangOpts().OpenMPIsTargetDevice && 994bdd1243dSDimitry Andric ((Context.getTargetInfo().getTriple().isAMDGPU() || 995bdd1243dSDimitry Andric Context.getTargetInfo().getTriple().isNVPTX()) || 996bdd1243dSDimitry Andric OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(MD))) 997bdd1243dSDimitry Andric LV.mergeVisibility(HiddenVisibility, /*newExplicit=*/false); 998bdd1243dSDimitry Andric 9990b57cec5SDimitry Andric } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { 10000b57cec5SDimitry Andric if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { 10010b57cec5SDimitry Andric mergeTemplateLV(LV, spec, computation); 10020b57cec5SDimitry Andric if (spec->isExplicitSpecialization()) { 10030b57cec5SDimitry Andric explicitSpecSuppressor = spec; 10040b57cec5SDimitry Andric } else { 10050b57cec5SDimitry Andric const ClassTemplateDecl *temp = spec->getSpecializedTemplate(); 10060b57cec5SDimitry Andric if (isExplicitMemberSpecialization(temp)) { 10070b57cec5SDimitry Andric explicitSpecSuppressor = temp->getTemplatedDecl(); 10080b57cec5SDimitry Andric } 10090b57cec5SDimitry Andric } 10100b57cec5SDimitry Andric } else if (isExplicitMemberSpecialization(RD)) { 10110b57cec5SDimitry Andric explicitSpecSuppressor = RD; 10120b57cec5SDimitry Andric } 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andric // Static data members. 10150b57cec5SDimitry Andric } else if (const auto *VD = dyn_cast<VarDecl>(D)) { 10160b57cec5SDimitry Andric if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(VD)) 10170b57cec5SDimitry Andric mergeTemplateLV(LV, spec, computation); 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric // Modify the variable's linkage by its type, but ignore the 10200b57cec5SDimitry Andric // type's visibility unless it's a definition. 10210b57cec5SDimitry Andric if (!IgnoreVarTypeLinkage) { 10220b57cec5SDimitry Andric LinkageInfo typeLV = getLVForType(*VD->getType(), computation); 10230b57cec5SDimitry Andric // FIXME: If the type's linkage is not externally visible, we can 10240b57cec5SDimitry Andric // give this static data member UniqueExternalLinkage. 10250b57cec5SDimitry Andric if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit()) 10260b57cec5SDimitry Andric LV.mergeVisibility(typeLV); 10270b57cec5SDimitry Andric LV.mergeExternalVisibility(typeLV); 10280b57cec5SDimitry Andric } 10290b57cec5SDimitry Andric 10300b57cec5SDimitry Andric if (isExplicitMemberSpecialization(VD)) { 10310b57cec5SDimitry Andric explicitSpecSuppressor = VD; 10320b57cec5SDimitry Andric } 10330b57cec5SDimitry Andric 10340b57cec5SDimitry Andric // Template members. 10350b57cec5SDimitry Andric } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { 10360b57cec5SDimitry Andric bool considerVisibility = 10370b57cec5SDimitry Andric (!LV.isVisibilityExplicit() && 10380b57cec5SDimitry Andric !classLV.isVisibilityExplicit() && 10390b57cec5SDimitry Andric !hasExplicitVisibilityAlready(computation)); 10400b57cec5SDimitry Andric LinkageInfo tempLV = 10410b57cec5SDimitry Andric getLVForTemplateParameterList(temp->getTemplateParameters(), computation); 10420b57cec5SDimitry Andric LV.mergeMaybeWithVisibility(tempLV, considerVisibility); 10430b57cec5SDimitry Andric 10440b57cec5SDimitry Andric if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) { 10450b57cec5SDimitry Andric if (isExplicitMemberSpecialization(redeclTemp)) { 10460b57cec5SDimitry Andric explicitSpecSuppressor = temp->getTemplatedDecl(); 10470b57cec5SDimitry Andric } 10480b57cec5SDimitry Andric } 10490b57cec5SDimitry Andric } 10500b57cec5SDimitry Andric 10510b57cec5SDimitry Andric // We should never be looking for an attribute directly on a template. 10520b57cec5SDimitry Andric assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor)); 10530b57cec5SDimitry Andric 10540b57cec5SDimitry Andric // If this member is an explicit member specialization, and it has 10550b57cec5SDimitry Andric // an explicit attribute, ignore visibility from the parent. 10560b57cec5SDimitry Andric bool considerClassVisibility = true; 10570b57cec5SDimitry Andric if (explicitSpecSuppressor && 10580b57cec5SDimitry Andric // optimization: hasDVA() is true only with explicit visibility. 10590b57cec5SDimitry Andric LV.isVisibilityExplicit() && 10600b57cec5SDimitry Andric classLV.getVisibility() != DefaultVisibility && 10610b57cec5SDimitry Andric hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) { 10620b57cec5SDimitry Andric considerClassVisibility = false; 10630b57cec5SDimitry Andric } 10640b57cec5SDimitry Andric 10650b57cec5SDimitry Andric // Finally, merge in information from the class. 10660b57cec5SDimitry Andric LV.mergeMaybeWithVisibility(classLV, considerClassVisibility); 10670b57cec5SDimitry Andric return LV; 10680b57cec5SDimitry Andric } 10690b57cec5SDimitry Andric 10700b57cec5SDimitry Andric void NamedDecl::anchor() {} 10710b57cec5SDimitry Andric 10720b57cec5SDimitry Andric bool NamedDecl::isLinkageValid() const { 10730b57cec5SDimitry Andric if (!hasCachedLinkage()) 10740b57cec5SDimitry Andric return true; 10750b57cec5SDimitry Andric 10760b57cec5SDimitry Andric Linkage L = LinkageComputer{} 10770b57cec5SDimitry Andric .computeLVForDecl(this, LVComputationKind::forLinkageOnly()) 10780b57cec5SDimitry Andric .getLinkage(); 10790b57cec5SDimitry Andric return L == getCachedLinkage(); 10800b57cec5SDimitry Andric } 10810b57cec5SDimitry Andric 10825f757f3fSDimitry Andric bool NamedDecl::isPlaceholderVar(const LangOptions &LangOpts) const { 10835f757f3fSDimitry Andric // [C++2c] [basic.scope.scope]/p5 10845f757f3fSDimitry Andric // A declaration is name-independent if its name is _ and it declares 10855f757f3fSDimitry Andric // - a variable with automatic storage duration, 10865f757f3fSDimitry Andric // - a structured binding not inhabiting a namespace scope, 10875f757f3fSDimitry Andric // - the variable introduced by an init-capture 10885f757f3fSDimitry Andric // - or a non-static data member. 10895f757f3fSDimitry Andric 10905f757f3fSDimitry Andric if (!LangOpts.CPlusPlus || !getIdentifier() || 10915f757f3fSDimitry Andric !getIdentifier()->isPlaceholder()) 10925f757f3fSDimitry Andric return false; 10935f757f3fSDimitry Andric if (isa<FieldDecl>(this)) 10945f757f3fSDimitry Andric return true; 1095cb14a3feSDimitry Andric if (const auto *IFD = dyn_cast<IndirectFieldDecl>(this)) { 10965f757f3fSDimitry Andric if (!getDeclContext()->isFunctionOrMethod() && 10975f757f3fSDimitry Andric !getDeclContext()->isRecord()) 10985f757f3fSDimitry Andric return false; 1099cb14a3feSDimitry Andric const VarDecl *VD = IFD->getVarDecl(); 11005f757f3fSDimitry Andric return !VD || VD->getStorageDuration() == SD_Automatic; 11015f757f3fSDimitry Andric } 11025f757f3fSDimitry Andric // and it declares a variable with automatic storage duration 11035f757f3fSDimitry Andric if (const auto *VD = dyn_cast<VarDecl>(this)) { 11045f757f3fSDimitry Andric if (isa<ParmVarDecl>(VD)) 11055f757f3fSDimitry Andric return false; 11065f757f3fSDimitry Andric if (VD->isInitCapture()) 11075f757f3fSDimitry Andric return true; 11085f757f3fSDimitry Andric return VD->getStorageDuration() == StorageDuration::SD_Automatic; 11095f757f3fSDimitry Andric } 11105f757f3fSDimitry Andric if (const auto *BD = dyn_cast<BindingDecl>(this); 11115f757f3fSDimitry Andric BD && getDeclContext()->isFunctionOrMethod()) { 1112cb14a3feSDimitry Andric const VarDecl *VD = BD->getHoldingVar(); 11135f757f3fSDimitry Andric return !VD || VD->getStorageDuration() == StorageDuration::SD_Automatic; 11145f757f3fSDimitry Andric } 11155f757f3fSDimitry Andric return false; 11165f757f3fSDimitry Andric } 11175f757f3fSDimitry Andric 1118fe6060f1SDimitry Andric ReservedIdentifierStatus 1119fe6060f1SDimitry Andric NamedDecl::isReserved(const LangOptions &LangOpts) const { 1120fe6060f1SDimitry Andric const IdentifierInfo *II = getIdentifier(); 1121fe6060f1SDimitry Andric 1122fe6060f1SDimitry Andric // This triggers at least for CXXLiteralIdentifiers, which we already checked 1123fe6060f1SDimitry Andric // at lexing time. 1124fe6060f1SDimitry Andric if (!II) 1125fe6060f1SDimitry Andric return ReservedIdentifierStatus::NotReserved; 1126fe6060f1SDimitry Andric 1127fe6060f1SDimitry Andric ReservedIdentifierStatus Status = II->isReserved(LangOpts); 1128349cc55cSDimitry Andric if (isReservedAtGlobalScope(Status) && !isReservedInAllContexts(Status)) { 1129349cc55cSDimitry Andric // This name is only reserved at global scope. Check if this declaration 1130349cc55cSDimitry Andric // conflicts with a global scope declaration. 1131fe6060f1SDimitry Andric if (isa<ParmVarDecl>(this) || isTemplateParameter()) 1132fe6060f1SDimitry Andric return ReservedIdentifierStatus::NotReserved; 1133349cc55cSDimitry Andric 1134349cc55cSDimitry Andric // C++ [dcl.link]/7: 1135349cc55cSDimitry Andric // Two declarations [conflict] if [...] one declares a function or 1136349cc55cSDimitry Andric // variable with C language linkage, and the other declares [...] a 1137349cc55cSDimitry Andric // variable that belongs to the global scope. 1138349cc55cSDimitry Andric // 1139349cc55cSDimitry Andric // Therefore names that are reserved at global scope are also reserved as 1140349cc55cSDimitry Andric // names of variables and functions with C language linkage. 1141fe6060f1SDimitry Andric const DeclContext *DC = getDeclContext()->getRedeclContext(); 1142349cc55cSDimitry Andric if (DC->isTranslationUnit()) 1143349cc55cSDimitry Andric return Status; 1144349cc55cSDimitry Andric if (auto *VD = dyn_cast<VarDecl>(this)) 1145349cc55cSDimitry Andric if (VD->isExternC()) 1146349cc55cSDimitry Andric return ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC; 1147349cc55cSDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(this)) 1148349cc55cSDimitry Andric if (FD->isExternC()) 1149349cc55cSDimitry Andric return ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC; 1150fe6060f1SDimitry Andric return ReservedIdentifierStatus::NotReserved; 1151fe6060f1SDimitry Andric } 1152fe6060f1SDimitry Andric 1153fe6060f1SDimitry Andric return Status; 1154fe6060f1SDimitry Andric } 1155fe6060f1SDimitry Andric 11560b57cec5SDimitry Andric ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const { 11570b57cec5SDimitry Andric StringRef name = getName(); 11580b57cec5SDimitry Andric if (name.empty()) return SFF_None; 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric if (name.front() == 'C') 11610b57cec5SDimitry Andric if (name == "CFStringCreateWithFormat" || 11620b57cec5SDimitry Andric name == "CFStringCreateWithFormatAndArguments" || 11630b57cec5SDimitry Andric name == "CFStringAppendFormat" || 11640b57cec5SDimitry Andric name == "CFStringAppendFormatAndArguments") 11650b57cec5SDimitry Andric return SFF_CFString; 11660b57cec5SDimitry Andric return SFF_None; 11670b57cec5SDimitry Andric } 11680b57cec5SDimitry Andric 11690b57cec5SDimitry Andric Linkage NamedDecl::getLinkageInternal() const { 11700b57cec5SDimitry Andric // We don't care about visibility here, so ask for the cheapest 11710b57cec5SDimitry Andric // possible visibility analysis. 11720b57cec5SDimitry Andric return LinkageComputer{} 11730b57cec5SDimitry Andric .getLVForDecl(this, LVComputationKind::forLinkageOnly()) 11740b57cec5SDimitry Andric .getLinkage(); 11750b57cec5SDimitry Andric } 11760b57cec5SDimitry Andric 11775f757f3fSDimitry Andric /// Determine whether D is attached to a named module. 11785f757f3fSDimitry Andric static bool isInNamedModule(const NamedDecl *D) { 11795f757f3fSDimitry Andric if (auto *M = D->getOwningModule()) 11805f757f3fSDimitry Andric return M->isNamedModule(); 11815f757f3fSDimitry Andric return false; 11825f757f3fSDimitry Andric } 11835f757f3fSDimitry Andric 11845f757f3fSDimitry Andric static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { 11855f757f3fSDimitry Andric // FIXME: Handle isModulePrivate. 11865f757f3fSDimitry Andric switch (D->getModuleOwnershipKind()) { 11875f757f3fSDimitry Andric case Decl::ModuleOwnershipKind::Unowned: 11885f757f3fSDimitry Andric case Decl::ModuleOwnershipKind::ReachableWhenImported: 11895f757f3fSDimitry Andric case Decl::ModuleOwnershipKind::ModulePrivate: 11905f757f3fSDimitry Andric return false; 11915f757f3fSDimitry Andric case Decl::ModuleOwnershipKind::Visible: 11925f757f3fSDimitry Andric case Decl::ModuleOwnershipKind::VisibleWhenImported: 11935f757f3fSDimitry Andric return isInNamedModule(D); 11945f757f3fSDimitry Andric } 11955f757f3fSDimitry Andric llvm_unreachable("unexpected module ownership kind"); 11965f757f3fSDimitry Andric } 11975f757f3fSDimitry Andric 119806c3fb27SDimitry Andric /// Get the linkage from a semantic point of view. Entities in 119906c3fb27SDimitry Andric /// anonymous namespaces are external (in c++98). 120006c3fb27SDimitry Andric Linkage NamedDecl::getFormalLinkage() const { 120106c3fb27SDimitry Andric Linkage InternalLinkage = getLinkageInternal(); 120206c3fb27SDimitry Andric 120306c3fb27SDimitry Andric // C++ [basic.link]p4.8: 120406c3fb27SDimitry Andric // - if the declaration of the name is attached to a named module and is not 120506c3fb27SDimitry Andric // exported 120606c3fb27SDimitry Andric // the name has module linkage; 120706c3fb27SDimitry Andric // 120806c3fb27SDimitry Andric // [basic.namespace.general]/p2 120906c3fb27SDimitry Andric // A namespace is never attached to a named module and never has a name with 121006c3fb27SDimitry Andric // module linkage. 12115f757f3fSDimitry Andric if (isInNamedModule(this) && InternalLinkage == Linkage::External && 121206c3fb27SDimitry Andric !isExportedFromModuleInterfaceUnit( 121306c3fb27SDimitry Andric cast<NamedDecl>(this->getCanonicalDecl())) && 121406c3fb27SDimitry Andric !isa<NamespaceDecl>(this)) 12155f757f3fSDimitry Andric InternalLinkage = Linkage::Module; 121606c3fb27SDimitry Andric 121706c3fb27SDimitry Andric return clang::getFormalLinkage(InternalLinkage); 121806c3fb27SDimitry Andric } 121906c3fb27SDimitry Andric 12200b57cec5SDimitry Andric LinkageInfo NamedDecl::getLinkageAndVisibility() const { 12210b57cec5SDimitry Andric return LinkageComputer{}.getDeclLinkageAndVisibility(this); 12220b57cec5SDimitry Andric } 12230b57cec5SDimitry Andric 1224bdd1243dSDimitry Andric static std::optional<Visibility> 12250b57cec5SDimitry Andric getExplicitVisibilityAux(const NamedDecl *ND, 12260b57cec5SDimitry Andric NamedDecl::ExplicitVisibilityKind kind, 12270b57cec5SDimitry Andric bool IsMostRecent) { 12280b57cec5SDimitry Andric assert(!IsMostRecent || ND == ND->getMostRecentDecl()); 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric // Check the declaration itself first. 1231bdd1243dSDimitry Andric if (std::optional<Visibility> V = getVisibilityOf(ND, kind)) 12320b57cec5SDimitry Andric return V; 12330b57cec5SDimitry Andric 12340b57cec5SDimitry Andric // If this is a member class of a specialization of a class template 12350b57cec5SDimitry Andric // and the corresponding decl has explicit visibility, use that. 12360b57cec5SDimitry Andric if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) { 12370b57cec5SDimitry Andric CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); 12380b57cec5SDimitry Andric if (InstantiatedFrom) 12390b57cec5SDimitry Andric return getVisibilityOf(InstantiatedFrom, kind); 12400b57cec5SDimitry Andric } 12410b57cec5SDimitry Andric 12420b57cec5SDimitry Andric // If there wasn't explicit visibility there, and this is a 12430b57cec5SDimitry Andric // specialization of a class template, check for visibility 12440b57cec5SDimitry Andric // on the pattern. 12450b57cec5SDimitry Andric if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(ND)) { 12460b57cec5SDimitry Andric // Walk all the template decl till this point to see if there are 12470b57cec5SDimitry Andric // explicit visibility attributes. 12480b57cec5SDimitry Andric const auto *TD = spec->getSpecializedTemplate()->getTemplatedDecl(); 12490b57cec5SDimitry Andric while (TD != nullptr) { 12500b57cec5SDimitry Andric auto Vis = getVisibilityOf(TD, kind); 1251bdd1243dSDimitry Andric if (Vis != std::nullopt) 12520b57cec5SDimitry Andric return Vis; 12530b57cec5SDimitry Andric TD = TD->getPreviousDecl(); 12540b57cec5SDimitry Andric } 1255bdd1243dSDimitry Andric return std::nullopt; 12560b57cec5SDimitry Andric } 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric // Use the most recent declaration. 12590b57cec5SDimitry Andric if (!IsMostRecent && !isa<NamespaceDecl>(ND)) { 12600b57cec5SDimitry Andric const NamedDecl *MostRecent = ND->getMostRecentDecl(); 12610b57cec5SDimitry Andric if (MostRecent != ND) 12620b57cec5SDimitry Andric return getExplicitVisibilityAux(MostRecent, kind, true); 12630b57cec5SDimitry Andric } 12640b57cec5SDimitry Andric 12650b57cec5SDimitry Andric if (const auto *Var = dyn_cast<VarDecl>(ND)) { 12660b57cec5SDimitry Andric if (Var->isStaticDataMember()) { 12670b57cec5SDimitry Andric VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); 12680b57cec5SDimitry Andric if (InstantiatedFrom) 12690b57cec5SDimitry Andric return getVisibilityOf(InstantiatedFrom, kind); 12700b57cec5SDimitry Andric } 12710b57cec5SDimitry Andric 12720b57cec5SDimitry Andric if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var)) 12730b57cec5SDimitry Andric return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(), 12740b57cec5SDimitry Andric kind); 12750b57cec5SDimitry Andric 1276bdd1243dSDimitry Andric return std::nullopt; 12770b57cec5SDimitry Andric } 12780b57cec5SDimitry Andric // Also handle function template specializations. 12790b57cec5SDimitry Andric if (const auto *fn = dyn_cast<FunctionDecl>(ND)) { 12800b57cec5SDimitry Andric // If the function is a specialization of a template with an 12810b57cec5SDimitry Andric // explicit visibility attribute, use that. 12820b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *templateInfo 12830b57cec5SDimitry Andric = fn->getTemplateSpecializationInfo()) 12840b57cec5SDimitry Andric return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(), 12850b57cec5SDimitry Andric kind); 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric // If the function is a member of a specialization of a class template 12880b57cec5SDimitry Andric // and the corresponding decl has explicit visibility, use that. 12890b57cec5SDimitry Andric FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); 12900b57cec5SDimitry Andric if (InstantiatedFrom) 12910b57cec5SDimitry Andric return getVisibilityOf(InstantiatedFrom, kind); 12920b57cec5SDimitry Andric 1293bdd1243dSDimitry Andric return std::nullopt; 12940b57cec5SDimitry Andric } 12950b57cec5SDimitry Andric 12960b57cec5SDimitry Andric // The visibility of a template is stored in the templated decl. 12970b57cec5SDimitry Andric if (const auto *TD = dyn_cast<TemplateDecl>(ND)) 12980b57cec5SDimitry Andric return getVisibilityOf(TD->getTemplatedDecl(), kind); 12990b57cec5SDimitry Andric 1300bdd1243dSDimitry Andric return std::nullopt; 13010b57cec5SDimitry Andric } 13020b57cec5SDimitry Andric 1303bdd1243dSDimitry Andric std::optional<Visibility> 13040b57cec5SDimitry Andric NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { 13050b57cec5SDimitry Andric return getExplicitVisibilityAux(this, kind, false); 13060b57cec5SDimitry Andric } 13070b57cec5SDimitry Andric 13080b57cec5SDimitry Andric LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC, 13090b57cec5SDimitry Andric Decl *ContextDecl, 13100b57cec5SDimitry Andric LVComputationKind computation) { 13110b57cec5SDimitry Andric // This lambda has its linkage/visibility determined by its owner. 13120b57cec5SDimitry Andric const NamedDecl *Owner; 13130b57cec5SDimitry Andric if (!ContextDecl) 13140b57cec5SDimitry Andric Owner = dyn_cast<NamedDecl>(DC); 13150b57cec5SDimitry Andric else if (isa<ParmVarDecl>(ContextDecl)) 13160b57cec5SDimitry Andric Owner = 13170b57cec5SDimitry Andric dyn_cast<NamedDecl>(ContextDecl->getDeclContext()->getRedeclContext()); 1318bdd1243dSDimitry Andric else if (isa<ImplicitConceptSpecializationDecl>(ContextDecl)) { 1319bdd1243dSDimitry Andric // Replace with the concept's owning decl, which is either a namespace or a 1320bdd1243dSDimitry Andric // TU, so this needs a dyn_cast. 1321bdd1243dSDimitry Andric Owner = dyn_cast<NamedDecl>(ContextDecl->getDeclContext()); 1322bdd1243dSDimitry Andric } else { 13230b57cec5SDimitry Andric Owner = cast<NamedDecl>(ContextDecl); 1324bdd1243dSDimitry Andric } 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric if (!Owner) 13270b57cec5SDimitry Andric return LinkageInfo::none(); 13280b57cec5SDimitry Andric 13290b57cec5SDimitry Andric // If the owner has a deduced type, we need to skip querying the linkage and 13300b57cec5SDimitry Andric // visibility of that type, because it might involve this closure type. The 13310b57cec5SDimitry Andric // only effect of this is that we might give a lambda VisibleNoLinkage rather 13320b57cec5SDimitry Andric // than NoLinkage when we don't strictly need to, which is benign. 13330b57cec5SDimitry Andric auto *VD = dyn_cast<VarDecl>(Owner); 13340b57cec5SDimitry Andric LinkageInfo OwnerLV = 13350b57cec5SDimitry Andric VD && VD->getType()->getContainedDeducedType() 13360b57cec5SDimitry Andric ? computeLVForDecl(Owner, computation, /*IgnoreVarTypeLinkage*/true) 13370b57cec5SDimitry Andric : getLVForDecl(Owner, computation); 13380b57cec5SDimitry Andric 13390b57cec5SDimitry Andric // A lambda never formally has linkage. But if the owner is externally 13400b57cec5SDimitry Andric // visible, then the lambda is too. We apply the same rules to blocks. 13410b57cec5SDimitry Andric if (!isExternallyVisible(OwnerLV.getLinkage())) 13420b57cec5SDimitry Andric return LinkageInfo::none(); 13435f757f3fSDimitry Andric return LinkageInfo(Linkage::VisibleNone, OwnerLV.getVisibility(), 13440b57cec5SDimitry Andric OwnerLV.isVisibilityExplicit()); 13450b57cec5SDimitry Andric } 13460b57cec5SDimitry Andric 13470b57cec5SDimitry Andric LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D, 13480b57cec5SDimitry Andric LVComputationKind computation) { 13490b57cec5SDimitry Andric if (const auto *Function = dyn_cast<FunctionDecl>(D)) { 13500b57cec5SDimitry Andric if (Function->isInAnonymousNamespace() && 13510b57cec5SDimitry Andric !isFirstInExternCContext(Function)) 135206c3fb27SDimitry Andric return LinkageInfo::internal(); 13530b57cec5SDimitry Andric 13540b57cec5SDimitry Andric // This is a "void f();" which got merged with a file static. 13550b57cec5SDimitry Andric if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) 135606c3fb27SDimitry Andric return LinkageInfo::internal(); 13570b57cec5SDimitry Andric 13580b57cec5SDimitry Andric LinkageInfo LV; 13590b57cec5SDimitry Andric if (!hasExplicitVisibilityAlready(computation)) { 1360bdd1243dSDimitry Andric if (std::optional<Visibility> Vis = 13610b57cec5SDimitry Andric getExplicitVisibility(Function, computation)) 13620b57cec5SDimitry Andric LV.mergeVisibility(*Vis, true); 13630b57cec5SDimitry Andric } 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric // Note that Sema::MergeCompatibleFunctionDecls already takes care of 13660b57cec5SDimitry Andric // merging storage classes and visibility attributes, so we don't have to 13670b57cec5SDimitry Andric // look at previous decls in here. 13680b57cec5SDimitry Andric 13690b57cec5SDimitry Andric return LV; 13700b57cec5SDimitry Andric } 13710b57cec5SDimitry Andric 13720b57cec5SDimitry Andric if (const auto *Var = dyn_cast<VarDecl>(D)) { 13730b57cec5SDimitry Andric if (Var->hasExternalStorage()) { 13740b57cec5SDimitry Andric if (Var->isInAnonymousNamespace() && !isFirstInExternCContext(Var)) 137506c3fb27SDimitry Andric return LinkageInfo::internal(); 13760b57cec5SDimitry Andric 13770b57cec5SDimitry Andric LinkageInfo LV; 13780b57cec5SDimitry Andric if (Var->getStorageClass() == SC_PrivateExtern) 13790b57cec5SDimitry Andric LV.mergeVisibility(HiddenVisibility, true); 13800b57cec5SDimitry Andric else if (!hasExplicitVisibilityAlready(computation)) { 1381bdd1243dSDimitry Andric if (std::optional<Visibility> Vis = 1382bdd1243dSDimitry Andric getExplicitVisibility(Var, computation)) 13830b57cec5SDimitry Andric LV.mergeVisibility(*Vis, true); 13840b57cec5SDimitry Andric } 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric if (const VarDecl *Prev = Var->getPreviousDecl()) { 13870b57cec5SDimitry Andric LinkageInfo PrevLV = getLVForDecl(Prev, computation); 13885f757f3fSDimitry Andric if (PrevLV.getLinkage() != Linkage::Invalid) 13890b57cec5SDimitry Andric LV.setLinkage(PrevLV.getLinkage()); 13900b57cec5SDimitry Andric LV.mergeVisibility(PrevLV); 13910b57cec5SDimitry Andric } 13920b57cec5SDimitry Andric 13930b57cec5SDimitry Andric return LV; 13940b57cec5SDimitry Andric } 13950b57cec5SDimitry Andric 13960b57cec5SDimitry Andric if (!Var->isStaticLocal()) 13970b57cec5SDimitry Andric return LinkageInfo::none(); 13980b57cec5SDimitry Andric } 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric ASTContext &Context = D->getASTContext(); 14010b57cec5SDimitry Andric if (!Context.getLangOpts().CPlusPlus) 14020b57cec5SDimitry Andric return LinkageInfo::none(); 14030b57cec5SDimitry Andric 14040b57cec5SDimitry Andric const Decl *OuterD = getOutermostFuncOrBlockContext(D); 14050b57cec5SDimitry Andric if (!OuterD || OuterD->isInvalidDecl()) 14060b57cec5SDimitry Andric return LinkageInfo::none(); 14070b57cec5SDimitry Andric 14080b57cec5SDimitry Andric LinkageInfo LV; 14090b57cec5SDimitry Andric if (const auto *BD = dyn_cast<BlockDecl>(OuterD)) { 14100b57cec5SDimitry Andric if (!BD->getBlockManglingNumber()) 14110b57cec5SDimitry Andric return LinkageInfo::none(); 14120b57cec5SDimitry Andric 14130b57cec5SDimitry Andric LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(), 14140b57cec5SDimitry Andric BD->getBlockManglingContextDecl(), computation); 14150b57cec5SDimitry Andric } else { 14160b57cec5SDimitry Andric const auto *FD = cast<FunctionDecl>(OuterD); 14170b57cec5SDimitry Andric if (!FD->isInlined() && 14180b57cec5SDimitry Andric !isTemplateInstantiation(FD->getTemplateSpecializationKind())) 14190b57cec5SDimitry Andric return LinkageInfo::none(); 14200b57cec5SDimitry Andric 14210b57cec5SDimitry Andric // If a function is hidden by -fvisibility-inlines-hidden option and 14220b57cec5SDimitry Andric // is not explicitly attributed as a hidden function, 14230b57cec5SDimitry Andric // we should not make static local variables in the function hidden. 14240b57cec5SDimitry Andric LV = getLVForDecl(FD, computation); 14250b57cec5SDimitry Andric if (isa<VarDecl>(D) && useInlineVisibilityHidden(FD) && 1426e8d8bef9SDimitry Andric !LV.isVisibilityExplicit() && 1427e8d8bef9SDimitry Andric !Context.getLangOpts().VisibilityInlinesHiddenStaticLocalVar) { 14280b57cec5SDimitry Andric assert(cast<VarDecl>(D)->isStaticLocal()); 14290b57cec5SDimitry Andric // If this was an implicitly hidden inline method, check again for 14300b57cec5SDimitry Andric // explicit visibility on the parent class, and use that for static locals 14310b57cec5SDimitry Andric // if present. 14320b57cec5SDimitry Andric if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) 14330b57cec5SDimitry Andric LV = getLVForDecl(MD->getParent(), computation); 14340b57cec5SDimitry Andric if (!LV.isVisibilityExplicit()) { 14350b57cec5SDimitry Andric Visibility globalVisibility = 14360b57cec5SDimitry Andric computation.isValueVisibility() 14370b57cec5SDimitry Andric ? Context.getLangOpts().getValueVisibilityMode() 14380b57cec5SDimitry Andric : Context.getLangOpts().getTypeVisibilityMode(); 14395f757f3fSDimitry Andric return LinkageInfo(Linkage::VisibleNone, globalVisibility, 14400b57cec5SDimitry Andric /*visibilityExplicit=*/false); 14410b57cec5SDimitry Andric } 14420b57cec5SDimitry Andric } 14430b57cec5SDimitry Andric } 14440b57cec5SDimitry Andric if (!isExternallyVisible(LV.getLinkage())) 14450b57cec5SDimitry Andric return LinkageInfo::none(); 14465f757f3fSDimitry Andric return LinkageInfo(Linkage::VisibleNone, LV.getVisibility(), 14470b57cec5SDimitry Andric LV.isVisibilityExplicit()); 14480b57cec5SDimitry Andric } 14490b57cec5SDimitry Andric 14500b57cec5SDimitry Andric LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D, 14510b57cec5SDimitry Andric LVComputationKind computation, 14520b57cec5SDimitry Andric bool IgnoreVarTypeLinkage) { 14530b57cec5SDimitry Andric // Internal_linkage attribute overrides other considerations. 14540b57cec5SDimitry Andric if (D->hasAttr<InternalLinkageAttr>()) 145506c3fb27SDimitry Andric return LinkageInfo::internal(); 14560b57cec5SDimitry Andric 14570b57cec5SDimitry Andric // Objective-C: treat all Objective-C declarations as having external 14580b57cec5SDimitry Andric // linkage. 14590b57cec5SDimitry Andric switch (D->getKind()) { 14600b57cec5SDimitry Andric default: 14610b57cec5SDimitry Andric break; 14620b57cec5SDimitry Andric 14630b57cec5SDimitry Andric // Per C++ [basic.link]p2, only the names of objects, references, 14640b57cec5SDimitry Andric // functions, types, templates, namespaces, and values ever have linkage. 14650b57cec5SDimitry Andric // 14660b57cec5SDimitry Andric // Note that the name of a typedef, namespace alias, using declaration, 14670b57cec5SDimitry Andric // and so on are not the name of the corresponding type, namespace, or 14680b57cec5SDimitry Andric // declaration, so they do *not* have linkage. 14690b57cec5SDimitry Andric case Decl::ImplicitParam: 14700b57cec5SDimitry Andric case Decl::Label: 14710b57cec5SDimitry Andric case Decl::NamespaceAlias: 14720b57cec5SDimitry Andric case Decl::ParmVar: 14730b57cec5SDimitry Andric case Decl::Using: 1474fe6060f1SDimitry Andric case Decl::UsingEnum: 14750b57cec5SDimitry Andric case Decl::UsingShadow: 14760b57cec5SDimitry Andric case Decl::UsingDirective: 14770b57cec5SDimitry Andric return LinkageInfo::none(); 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric case Decl::EnumConstant: 14800b57cec5SDimitry Andric // C++ [basic.link]p4: an enumerator has the linkage of its enumeration. 14810b57cec5SDimitry Andric if (D->getASTContext().getLangOpts().CPlusPlus) 14820b57cec5SDimitry Andric return getLVForDecl(cast<EnumDecl>(D->getDeclContext()), computation); 14830b57cec5SDimitry Andric return LinkageInfo::visible_none(); 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric case Decl::Typedef: 14860b57cec5SDimitry Andric case Decl::TypeAlias: 14870b57cec5SDimitry Andric // A typedef declaration has linkage if it gives a type a name for 14880b57cec5SDimitry Andric // linkage purposes. 14890b57cec5SDimitry Andric if (!cast<TypedefNameDecl>(D) 14900b57cec5SDimitry Andric ->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) 14910b57cec5SDimitry Andric return LinkageInfo::none(); 14920b57cec5SDimitry Andric break; 14930b57cec5SDimitry Andric 14940b57cec5SDimitry Andric case Decl::TemplateTemplateParm: // count these as external 14950b57cec5SDimitry Andric case Decl::NonTypeTemplateParm: 14960b57cec5SDimitry Andric case Decl::ObjCAtDefsField: 14970b57cec5SDimitry Andric case Decl::ObjCCategory: 14980b57cec5SDimitry Andric case Decl::ObjCCategoryImpl: 14990b57cec5SDimitry Andric case Decl::ObjCCompatibleAlias: 15000b57cec5SDimitry Andric case Decl::ObjCImplementation: 15010b57cec5SDimitry Andric case Decl::ObjCMethod: 15020b57cec5SDimitry Andric case Decl::ObjCProperty: 15030b57cec5SDimitry Andric case Decl::ObjCPropertyImpl: 15040b57cec5SDimitry Andric case Decl::ObjCProtocol: 15050b57cec5SDimitry Andric return getExternalLinkageFor(D); 15060b57cec5SDimitry Andric 15070b57cec5SDimitry Andric case Decl::CXXRecord: { 15080b57cec5SDimitry Andric const auto *Record = cast<CXXRecordDecl>(D); 15090b57cec5SDimitry Andric if (Record->isLambda()) { 1510a7dea167SDimitry Andric if (Record->hasKnownLambdaInternalLinkage() || 1511a7dea167SDimitry Andric !Record->getLambdaManglingNumber()) { 15120b57cec5SDimitry Andric // This lambda has no mangling number, so it's internal. 151306c3fb27SDimitry Andric return LinkageInfo::internal(); 15140b57cec5SDimitry Andric } 15150b57cec5SDimitry Andric 15160b57cec5SDimitry Andric return getLVForClosure( 15175ffd83dbSDimitry Andric Record->getDeclContext()->getRedeclContext(), 15185ffd83dbSDimitry Andric Record->getLambdaContextDecl(), computation); 15190b57cec5SDimitry Andric } 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andric break; 15220b57cec5SDimitry Andric } 1523e8d8bef9SDimitry Andric 1524e8d8bef9SDimitry Andric case Decl::TemplateParamObject: { 1525e8d8bef9SDimitry Andric // The template parameter object can be referenced from anywhere its type 1526e8d8bef9SDimitry Andric // and value can be referenced. 1527e8d8bef9SDimitry Andric auto *TPO = cast<TemplateParamObjectDecl>(D); 1528e8d8bef9SDimitry Andric LinkageInfo LV = getLVForType(*TPO->getType(), computation); 1529e8d8bef9SDimitry Andric LV.merge(getLVForValue(TPO->getValue(), computation)); 1530e8d8bef9SDimitry Andric return LV; 1531e8d8bef9SDimitry Andric } 15320b57cec5SDimitry Andric } 15330b57cec5SDimitry Andric 15340b57cec5SDimitry Andric // Handle linkage for namespace-scope names. 15350b57cec5SDimitry Andric if (D->getDeclContext()->getRedeclContext()->isFileContext()) 15360b57cec5SDimitry Andric return getLVForNamespaceScopeDecl(D, computation, IgnoreVarTypeLinkage); 15370b57cec5SDimitry Andric 15380b57cec5SDimitry Andric // C++ [basic.link]p5: 15390b57cec5SDimitry Andric // In addition, a member function, static data member, a named 15400b57cec5SDimitry Andric // class or enumeration of class scope, or an unnamed class or 15410b57cec5SDimitry Andric // enumeration defined in a class-scope typedef declaration such 15420b57cec5SDimitry Andric // that the class or enumeration has the typedef name for linkage 15430b57cec5SDimitry Andric // purposes (7.1.3), has external linkage if the name of the class 15440b57cec5SDimitry Andric // has external linkage. 15450b57cec5SDimitry Andric if (D->getDeclContext()->isRecord()) 15460b57cec5SDimitry Andric return getLVForClassMember(D, computation, IgnoreVarTypeLinkage); 15470b57cec5SDimitry Andric 15480b57cec5SDimitry Andric // C++ [basic.link]p6: 15490b57cec5SDimitry Andric // The name of a function declared in block scope and the name of 15500b57cec5SDimitry Andric // an object declared by a block scope extern declaration have 15510b57cec5SDimitry Andric // linkage. If there is a visible declaration of an entity with 15520b57cec5SDimitry Andric // linkage having the same name and type, ignoring entities 15530b57cec5SDimitry Andric // declared outside the innermost enclosing namespace scope, the 15540b57cec5SDimitry Andric // block scope declaration declares that same entity and receives 15550b57cec5SDimitry Andric // the linkage of the previous declaration. If there is more than 15560b57cec5SDimitry Andric // one such matching entity, the program is ill-formed. Otherwise, 15570b57cec5SDimitry Andric // if no matching entity is found, the block scope entity receives 15580b57cec5SDimitry Andric // external linkage. 15590b57cec5SDimitry Andric if (D->getDeclContext()->isFunctionOrMethod()) 15600b57cec5SDimitry Andric return getLVForLocalDecl(D, computation); 15610b57cec5SDimitry Andric 15620b57cec5SDimitry Andric // C++ [basic.link]p6: 15630b57cec5SDimitry Andric // Names not covered by these rules have no linkage. 15640b57cec5SDimitry Andric return LinkageInfo::none(); 15650b57cec5SDimitry Andric } 15660b57cec5SDimitry Andric 15670b57cec5SDimitry Andric /// getLVForDecl - Get the linkage and visibility for the given declaration. 15680b57cec5SDimitry Andric LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D, 15690b57cec5SDimitry Andric LVComputationKind computation) { 15700b57cec5SDimitry Andric // Internal_linkage attribute overrides other considerations. 15710b57cec5SDimitry Andric if (D->hasAttr<InternalLinkageAttr>()) 157206c3fb27SDimitry Andric return LinkageInfo::internal(); 15730b57cec5SDimitry Andric 15740b57cec5SDimitry Andric if (computation.IgnoreAllVisibility && D->hasCachedLinkage()) 15750b57cec5SDimitry Andric return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false); 15760b57cec5SDimitry Andric 1577bdd1243dSDimitry Andric if (std::optional<LinkageInfo> LI = lookup(D, computation)) 15780b57cec5SDimitry Andric return *LI; 15790b57cec5SDimitry Andric 15800b57cec5SDimitry Andric LinkageInfo LV = computeLVForDecl(D, computation); 15810b57cec5SDimitry Andric if (D->hasCachedLinkage()) 15820b57cec5SDimitry Andric assert(D->getCachedLinkage() == LV.getLinkage()); 15830b57cec5SDimitry Andric 15840b57cec5SDimitry Andric D->setCachedLinkage(LV.getLinkage()); 15850b57cec5SDimitry Andric cache(D, computation, LV); 15860b57cec5SDimitry Andric 15870b57cec5SDimitry Andric #ifndef NDEBUG 15880b57cec5SDimitry Andric // In C (because of gnu inline) and in c++ with microsoft extensions an 15890b57cec5SDimitry Andric // static can follow an extern, so we can have two decls with different 15900b57cec5SDimitry Andric // linkages. 15910b57cec5SDimitry Andric const LangOptions &Opts = D->getASTContext().getLangOpts(); 15920b57cec5SDimitry Andric if (!Opts.CPlusPlus || Opts.MicrosoftExt) 15930b57cec5SDimitry Andric return LV; 15940b57cec5SDimitry Andric 15950b57cec5SDimitry Andric // We have just computed the linkage for this decl. By induction we know 15960b57cec5SDimitry Andric // that all other computed linkages match, check that the one we just 15970b57cec5SDimitry Andric // computed also does. 15980b57cec5SDimitry Andric NamedDecl *Old = nullptr; 1599bdd1243dSDimitry Andric for (auto *I : D->redecls()) { 16000b57cec5SDimitry Andric auto *T = cast<NamedDecl>(I); 16010b57cec5SDimitry Andric if (T == D) 16020b57cec5SDimitry Andric continue; 16030b57cec5SDimitry Andric if (!T->isInvalidDecl() && T->hasCachedLinkage()) { 16040b57cec5SDimitry Andric Old = T; 16050b57cec5SDimitry Andric break; 16060b57cec5SDimitry Andric } 16070b57cec5SDimitry Andric } 16080b57cec5SDimitry Andric assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage()); 16090b57cec5SDimitry Andric #endif 16100b57cec5SDimitry Andric 16110b57cec5SDimitry Andric return LV; 16120b57cec5SDimitry Andric } 16130b57cec5SDimitry Andric 16140b57cec5SDimitry Andric LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) { 1615fe6060f1SDimitry Andric NamedDecl::ExplicitVisibilityKind EK = usesTypeVisibility(D) 16160b57cec5SDimitry Andric ? NamedDecl::VisibilityForType 1617fe6060f1SDimitry Andric : NamedDecl::VisibilityForValue; 1618fe6060f1SDimitry Andric LVComputationKind CK(EK); 1619fe6060f1SDimitry Andric return getLVForDecl(D, D->getASTContext().getLangOpts().IgnoreXCOFFVisibility 1620fe6060f1SDimitry Andric ? CK.forLinkageOnly() 1621fe6060f1SDimitry Andric : CK); 16220b57cec5SDimitry Andric } 16230b57cec5SDimitry Andric 16240b57cec5SDimitry Andric Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const { 162581ad6265SDimitry Andric if (isa<NamespaceDecl>(this)) 162681ad6265SDimitry Andric // Namespaces never have module linkage. It is the entities within them 162781ad6265SDimitry Andric // that [may] do. 162881ad6265SDimitry Andric return nullptr; 162981ad6265SDimitry Andric 16300b57cec5SDimitry Andric Module *M = getOwningModule(); 16310b57cec5SDimitry Andric if (!M) 16320b57cec5SDimitry Andric return nullptr; 16330b57cec5SDimitry Andric 16340b57cec5SDimitry Andric switch (M->Kind) { 16350b57cec5SDimitry Andric case Module::ModuleMapModule: 16360b57cec5SDimitry Andric // Module map modules have no special linkage semantics. 16370b57cec5SDimitry Andric return nullptr; 16380b57cec5SDimitry Andric 16390b57cec5SDimitry Andric case Module::ModuleInterfaceUnit: 164006c3fb27SDimitry Andric case Module::ModuleImplementationUnit: 164181ad6265SDimitry Andric case Module::ModulePartitionInterface: 164281ad6265SDimitry Andric case Module::ModulePartitionImplementation: 16430b57cec5SDimitry Andric return M; 16440b57cec5SDimitry Andric 164581ad6265SDimitry Andric case Module::ModuleHeaderUnit: 164606c3fb27SDimitry Andric case Module::ExplicitGlobalModuleFragment: 164706c3fb27SDimitry Andric case Module::ImplicitGlobalModuleFragment: { 16480b57cec5SDimitry Andric // External linkage declarations in the global module have no owning module 16490b57cec5SDimitry Andric // for linkage purposes. But internal linkage declarations in the global 16500b57cec5SDimitry Andric // module fragment of a particular module are owned by that module for 16510b57cec5SDimitry Andric // linkage purposes. 165281ad6265SDimitry Andric // FIXME: p1815 removes the need for this distinction -- there are no 165381ad6265SDimitry Andric // internal linkage declarations that need to be referred to from outside 165481ad6265SDimitry Andric // this TU. 16550b57cec5SDimitry Andric if (IgnoreLinkage) 16560b57cec5SDimitry Andric return nullptr; 16570b57cec5SDimitry Andric bool InternalLinkage; 16580b57cec5SDimitry Andric if (auto *ND = dyn_cast<NamedDecl>(this)) 16590b57cec5SDimitry Andric InternalLinkage = !ND->hasExternalFormalLinkage(); 166081ad6265SDimitry Andric else 166181ad6265SDimitry Andric InternalLinkage = isInAnonymousNamespace(); 166281ad6265SDimitry Andric return InternalLinkage ? M->Kind == Module::ModuleHeaderUnit ? M : M->Parent 166381ad6265SDimitry Andric : nullptr; 16640b57cec5SDimitry Andric } 16650b57cec5SDimitry Andric 16660b57cec5SDimitry Andric case Module::PrivateModuleFragment: 16670b57cec5SDimitry Andric // The private module fragment is part of its containing module for linkage 16680b57cec5SDimitry Andric // purposes. 16690b57cec5SDimitry Andric return M->Parent; 16700b57cec5SDimitry Andric } 16710b57cec5SDimitry Andric 16720b57cec5SDimitry Andric llvm_unreachable("unknown module kind"); 16730b57cec5SDimitry Andric } 16740b57cec5SDimitry Andric 167506c3fb27SDimitry Andric void NamedDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { 167606c3fb27SDimitry Andric Name.print(OS, Policy); 1677bdd1243dSDimitry Andric } 1678bdd1243dSDimitry Andric 1679bdd1243dSDimitry Andric void NamedDecl::printName(raw_ostream &OS) const { 1680bdd1243dSDimitry Andric printName(OS, getASTContext().getPrintingPolicy()); 16810b57cec5SDimitry Andric } 16820b57cec5SDimitry Andric 16830b57cec5SDimitry Andric std::string NamedDecl::getQualifiedNameAsString() const { 16840b57cec5SDimitry Andric std::string QualName; 16850b57cec5SDimitry Andric llvm::raw_string_ostream OS(QualName); 16860b57cec5SDimitry Andric printQualifiedName(OS, getASTContext().getPrintingPolicy()); 16870eae32dcSDimitry Andric return QualName; 16880b57cec5SDimitry Andric } 16890b57cec5SDimitry Andric 16900b57cec5SDimitry Andric void NamedDecl::printQualifiedName(raw_ostream &OS) const { 16910b57cec5SDimitry Andric printQualifiedName(OS, getASTContext().getPrintingPolicy()); 16920b57cec5SDimitry Andric } 16930b57cec5SDimitry Andric 16940b57cec5SDimitry Andric void NamedDecl::printQualifiedName(raw_ostream &OS, 16950b57cec5SDimitry Andric const PrintingPolicy &P) const { 1696a7dea167SDimitry Andric if (getDeclContext()->isFunctionOrMethod()) { 1697a7dea167SDimitry Andric // We do not print '(anonymous)' for function parameters without name. 1698bdd1243dSDimitry Andric printName(OS, P); 1699a7dea167SDimitry Andric return; 1700a7dea167SDimitry Andric } 1701a7dea167SDimitry Andric printNestedNameSpecifier(OS, P); 17025ffd83dbSDimitry Andric if (getDeclName()) 1703a7dea167SDimitry Andric OS << *this; 17045ffd83dbSDimitry Andric else { 17055ffd83dbSDimitry Andric // Give the printName override a chance to pick a different name before we 17065ffd83dbSDimitry Andric // fall back to "(anonymous)". 17075ffd83dbSDimitry Andric SmallString<64> NameBuffer; 17085ffd83dbSDimitry Andric llvm::raw_svector_ostream NameOS(NameBuffer); 1709bdd1243dSDimitry Andric printName(NameOS, P); 17105ffd83dbSDimitry Andric if (NameBuffer.empty()) 1711a7dea167SDimitry Andric OS << "(anonymous)"; 17125ffd83dbSDimitry Andric else 17135ffd83dbSDimitry Andric OS << NameBuffer; 17145ffd83dbSDimitry Andric } 1715a7dea167SDimitry Andric } 1716a7dea167SDimitry Andric 1717a7dea167SDimitry Andric void NamedDecl::printNestedNameSpecifier(raw_ostream &OS) const { 1718a7dea167SDimitry Andric printNestedNameSpecifier(OS, getASTContext().getPrintingPolicy()); 1719a7dea167SDimitry Andric } 1720a7dea167SDimitry Andric 1721a7dea167SDimitry Andric void NamedDecl::printNestedNameSpecifier(raw_ostream &OS, 1722a7dea167SDimitry Andric const PrintingPolicy &P) const { 17230b57cec5SDimitry Andric const DeclContext *Ctx = getDeclContext(); 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric // For ObjC methods and properties, look through categories and use the 17260b57cec5SDimitry Andric // interface as context. 17275ffd83dbSDimitry Andric if (auto *MD = dyn_cast<ObjCMethodDecl>(this)) { 17280b57cec5SDimitry Andric if (auto *ID = MD->getClassInterface()) 17290b57cec5SDimitry Andric Ctx = ID; 17305ffd83dbSDimitry Andric } else if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) { 17310b57cec5SDimitry Andric if (auto *MD = PD->getGetterMethodDecl()) 17320b57cec5SDimitry Andric if (auto *ID = MD->getClassInterface()) 17330b57cec5SDimitry Andric Ctx = ID; 17345ffd83dbSDimitry Andric } else if (auto *ID = dyn_cast<ObjCIvarDecl>(this)) { 17355ffd83dbSDimitry Andric if (auto *CI = ID->getContainingInterface()) 17365ffd83dbSDimitry Andric Ctx = CI; 17370b57cec5SDimitry Andric } 17380b57cec5SDimitry Andric 1739a7dea167SDimitry Andric if (Ctx->isFunctionOrMethod()) 17400b57cec5SDimitry Andric return; 17410b57cec5SDimitry Andric 17420b57cec5SDimitry Andric using ContextsTy = SmallVector<const DeclContext *, 8>; 17430b57cec5SDimitry Andric ContextsTy Contexts; 17440b57cec5SDimitry Andric 17450b57cec5SDimitry Andric // Collect named contexts. 1746e8d8bef9SDimitry Andric DeclarationName NameInScope = getDeclName(); 1747e8d8bef9SDimitry Andric for (; Ctx; Ctx = Ctx->getParent()) { 1748e8d8bef9SDimitry Andric // Suppress anonymous namespace if requested. 1749e8d8bef9SDimitry Andric if (P.SuppressUnwrittenScope && isa<NamespaceDecl>(Ctx) && 1750e8d8bef9SDimitry Andric cast<NamespaceDecl>(Ctx)->isAnonymousNamespace()) 1751e8d8bef9SDimitry Andric continue; 1752e8d8bef9SDimitry Andric 1753e8d8bef9SDimitry Andric // Suppress inline namespace if it doesn't make the result ambiguous. 1754e8d8bef9SDimitry Andric if (P.SuppressInlineNamespace && Ctx->isInlineNamespace() && NameInScope && 1755fe6060f1SDimitry Andric cast<NamespaceDecl>(Ctx)->isRedundantInlineQualifierFor(NameInScope)) 1756e8d8bef9SDimitry Andric continue; 1757e8d8bef9SDimitry Andric 1758e8d8bef9SDimitry Andric // Skip non-named contexts such as linkage specifications and ExportDecls. 1759e8d8bef9SDimitry Andric const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx); 1760e8d8bef9SDimitry Andric if (!ND) 1761e8d8bef9SDimitry Andric continue; 1762e8d8bef9SDimitry Andric 17630b57cec5SDimitry Andric Contexts.push_back(Ctx); 1764e8d8bef9SDimitry Andric NameInScope = ND->getDeclName(); 17650b57cec5SDimitry Andric } 17660b57cec5SDimitry Andric 1767349cc55cSDimitry Andric for (const DeclContext *DC : llvm::reverse(Contexts)) { 17680b57cec5SDimitry Andric if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 17690b57cec5SDimitry Andric OS << Spec->getName(); 17700b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 1771e8d8bef9SDimitry Andric printTemplateArgumentList( 1772e8d8bef9SDimitry Andric OS, TemplateArgs.asArray(), P, 1773e8d8bef9SDimitry Andric Spec->getSpecializedTemplate()->getTemplateParameters()); 17740b57cec5SDimitry Andric } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { 17750b57cec5SDimitry Andric if (ND->isAnonymousNamespace()) { 17760b57cec5SDimitry Andric OS << (P.MSVCFormatting ? "`anonymous namespace\'" 17770b57cec5SDimitry Andric : "(anonymous namespace)"); 17780b57cec5SDimitry Andric } 17790b57cec5SDimitry Andric else 17800b57cec5SDimitry Andric OS << *ND; 17810b57cec5SDimitry Andric } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) { 17820b57cec5SDimitry Andric if (!RD->getIdentifier()) 17830b57cec5SDimitry Andric OS << "(anonymous " << RD->getKindName() << ')'; 17840b57cec5SDimitry Andric else 17850b57cec5SDimitry Andric OS << *RD; 17860b57cec5SDimitry Andric } else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) { 17870b57cec5SDimitry Andric const FunctionProtoType *FT = nullptr; 17880b57cec5SDimitry Andric if (FD->hasWrittenPrototype()) 17890b57cec5SDimitry Andric FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); 17900b57cec5SDimitry Andric 17910b57cec5SDimitry Andric OS << *FD << '('; 17920b57cec5SDimitry Andric if (FT) { 17930b57cec5SDimitry Andric unsigned NumParams = FD->getNumParams(); 17940b57cec5SDimitry Andric for (unsigned i = 0; i < NumParams; ++i) { 17950b57cec5SDimitry Andric if (i) 17960b57cec5SDimitry Andric OS << ", "; 17970b57cec5SDimitry Andric OS << FD->getParamDecl(i)->getType().stream(P); 17980b57cec5SDimitry Andric } 17990b57cec5SDimitry Andric 18000b57cec5SDimitry Andric if (FT->isVariadic()) { 18010b57cec5SDimitry Andric if (NumParams > 0) 18020b57cec5SDimitry Andric OS << ", "; 18030b57cec5SDimitry Andric OS << "..."; 18040b57cec5SDimitry Andric } 18050b57cec5SDimitry Andric } 18060b57cec5SDimitry Andric OS << ')'; 18070b57cec5SDimitry Andric } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) { 18080b57cec5SDimitry Andric // C++ [dcl.enum]p10: Each enum-name and each unscoped 18090b57cec5SDimitry Andric // enumerator is declared in the scope that immediately contains 18100b57cec5SDimitry Andric // the enum-specifier. Each scoped enumerator is declared in the 18110b57cec5SDimitry Andric // scope of the enumeration. 18120b57cec5SDimitry Andric // For the case of unscoped enumerator, do not include in the qualified 18130b57cec5SDimitry Andric // name any information about its enum enclosing scope, as its visibility 18140b57cec5SDimitry Andric // is global. 18150b57cec5SDimitry Andric if (ED->isScoped()) 18160b57cec5SDimitry Andric OS << *ED; 18170b57cec5SDimitry Andric else 18180b57cec5SDimitry Andric continue; 18190b57cec5SDimitry Andric } else { 18200b57cec5SDimitry Andric OS << *cast<NamedDecl>(DC); 18210b57cec5SDimitry Andric } 18220b57cec5SDimitry Andric OS << "::"; 18230b57cec5SDimitry Andric } 18240b57cec5SDimitry Andric } 18250b57cec5SDimitry Andric 18260b57cec5SDimitry Andric void NamedDecl::getNameForDiagnostic(raw_ostream &OS, 18270b57cec5SDimitry Andric const PrintingPolicy &Policy, 18280b57cec5SDimitry Andric bool Qualified) const { 18290b57cec5SDimitry Andric if (Qualified) 18300b57cec5SDimitry Andric printQualifiedName(OS, Policy); 18310b57cec5SDimitry Andric else 1832bdd1243dSDimitry Andric printName(OS, Policy); 18330b57cec5SDimitry Andric } 18340b57cec5SDimitry Andric 18350b57cec5SDimitry Andric template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { 18360b57cec5SDimitry Andric return true; 18370b57cec5SDimitry Andric } 18380b57cec5SDimitry Andric static bool isRedeclarableImpl(...) { return false; } 18390b57cec5SDimitry Andric static bool isRedeclarable(Decl::Kind K) { 18400b57cec5SDimitry Andric switch (K) { 18410b57cec5SDimitry Andric #define DECL(Type, Base) \ 18420b57cec5SDimitry Andric case Decl::Type: \ 18430b57cec5SDimitry Andric return isRedeclarableImpl((Type##Decl *)nullptr); 18440b57cec5SDimitry Andric #define ABSTRACT_DECL(DECL) 18450b57cec5SDimitry Andric #include "clang/AST/DeclNodes.inc" 18460b57cec5SDimitry Andric } 18470b57cec5SDimitry Andric llvm_unreachable("unknown decl kind"); 18480b57cec5SDimitry Andric } 18490b57cec5SDimitry Andric 1850cb14a3feSDimitry Andric bool NamedDecl::declarationReplaces(const NamedDecl *OldD, 1851cb14a3feSDimitry Andric bool IsKnownNewer) const { 18520b57cec5SDimitry Andric assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); 18530b57cec5SDimitry Andric 18540b57cec5SDimitry Andric // Never replace one imported declaration with another; we need both results 18550b57cec5SDimitry Andric // when re-exporting. 18560b57cec5SDimitry Andric if (OldD->isFromASTFile() && isFromASTFile()) 18570b57cec5SDimitry Andric return false; 18580b57cec5SDimitry Andric 18590b57cec5SDimitry Andric // A kind mismatch implies that the declaration is not replaced. 18600b57cec5SDimitry Andric if (OldD->getKind() != getKind()) 18610b57cec5SDimitry Andric return false; 18620b57cec5SDimitry Andric 18630b57cec5SDimitry Andric // For method declarations, we never replace. (Why?) 18640b57cec5SDimitry Andric if (isa<ObjCMethodDecl>(this)) 18650b57cec5SDimitry Andric return false; 18660b57cec5SDimitry Andric 18670b57cec5SDimitry Andric // For parameters, pick the newer one. This is either an error or (in 18680b57cec5SDimitry Andric // Objective-C) permitted as an extension. 18690b57cec5SDimitry Andric if (isa<ParmVarDecl>(this)) 18700b57cec5SDimitry Andric return true; 18710b57cec5SDimitry Andric 18720b57cec5SDimitry Andric // Inline namespaces can give us two declarations with the same 18730b57cec5SDimitry Andric // name and kind in the same scope but different contexts; we should 18740b57cec5SDimitry Andric // keep both declarations in this case. 18750b57cec5SDimitry Andric if (!this->getDeclContext()->getRedeclContext()->Equals( 18760b57cec5SDimitry Andric OldD->getDeclContext()->getRedeclContext())) 18770b57cec5SDimitry Andric return false; 18780b57cec5SDimitry Andric 18790b57cec5SDimitry Andric // Using declarations can be replaced if they import the same name from the 18800b57cec5SDimitry Andric // same context. 1881cb14a3feSDimitry Andric if (const auto *UD = dyn_cast<UsingDecl>(this)) { 18820b57cec5SDimitry Andric ASTContext &Context = getASTContext(); 18830b57cec5SDimitry Andric return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) == 18840b57cec5SDimitry Andric Context.getCanonicalNestedNameSpecifier( 18850b57cec5SDimitry Andric cast<UsingDecl>(OldD)->getQualifier()); 18860b57cec5SDimitry Andric } 1887cb14a3feSDimitry Andric if (const auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) { 18880b57cec5SDimitry Andric ASTContext &Context = getASTContext(); 18890b57cec5SDimitry Andric return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) == 18900b57cec5SDimitry Andric Context.getCanonicalNestedNameSpecifier( 18910b57cec5SDimitry Andric cast<UnresolvedUsingValueDecl>(OldD)->getQualifier()); 18920b57cec5SDimitry Andric } 18930b57cec5SDimitry Andric 18940b57cec5SDimitry Andric if (isRedeclarable(getKind())) { 18950b57cec5SDimitry Andric if (getCanonicalDecl() != OldD->getCanonicalDecl()) 18960b57cec5SDimitry Andric return false; 18970b57cec5SDimitry Andric 18980b57cec5SDimitry Andric if (IsKnownNewer) 18990b57cec5SDimitry Andric return true; 19000b57cec5SDimitry Andric 19010b57cec5SDimitry Andric // Check whether this is actually newer than OldD. We want to keep the 19020b57cec5SDimitry Andric // newer declaration. This loop will usually only iterate once, because 19030b57cec5SDimitry Andric // OldD is usually the previous declaration. 1904cb14a3feSDimitry Andric for (const auto *D : redecls()) { 19050b57cec5SDimitry Andric if (D == OldD) 19060b57cec5SDimitry Andric break; 19070b57cec5SDimitry Andric 19080b57cec5SDimitry Andric // If we reach the canonical declaration, then OldD is not actually older 19090b57cec5SDimitry Andric // than this one. 19100b57cec5SDimitry Andric // 19110b57cec5SDimitry Andric // FIXME: In this case, we should not add this decl to the lookup table. 19120b57cec5SDimitry Andric if (D->isCanonicalDecl()) 19130b57cec5SDimitry Andric return false; 19140b57cec5SDimitry Andric } 19150b57cec5SDimitry Andric 19160b57cec5SDimitry Andric // It's a newer declaration of the same kind of declaration in the same 19170b57cec5SDimitry Andric // scope: we want this decl instead of the existing one. 19180b57cec5SDimitry Andric return true; 19190b57cec5SDimitry Andric } 19200b57cec5SDimitry Andric 19210b57cec5SDimitry Andric // In all other cases, we need to keep both declarations in case they have 19220b57cec5SDimitry Andric // different visibility. Any attempt to use the name will result in an 19230b57cec5SDimitry Andric // ambiguity if more than one is visible. 19240b57cec5SDimitry Andric return false; 19250b57cec5SDimitry Andric } 19260b57cec5SDimitry Andric 19270b57cec5SDimitry Andric bool NamedDecl::hasLinkage() const { 19285f757f3fSDimitry Andric switch (getFormalLinkage()) { 19295f757f3fSDimitry Andric case Linkage::Invalid: 19305f757f3fSDimitry Andric llvm_unreachable("Linkage hasn't been computed!"); 19315f757f3fSDimitry Andric case Linkage::None: 19325f757f3fSDimitry Andric return false; 19335f757f3fSDimitry Andric case Linkage::Internal: 19345f757f3fSDimitry Andric return true; 19355f757f3fSDimitry Andric case Linkage::UniqueExternal: 19365f757f3fSDimitry Andric case Linkage::VisibleNone: 19375f757f3fSDimitry Andric llvm_unreachable("Non-formal linkage is not allowed here!"); 19385f757f3fSDimitry Andric case Linkage::Module: 19395f757f3fSDimitry Andric case Linkage::External: 19405f757f3fSDimitry Andric return true; 19415f757f3fSDimitry Andric } 19425f757f3fSDimitry Andric llvm_unreachable("Unhandled Linkage enum"); 19430b57cec5SDimitry Andric } 19440b57cec5SDimitry Andric 19450b57cec5SDimitry Andric NamedDecl *NamedDecl::getUnderlyingDeclImpl() { 19460b57cec5SDimitry Andric NamedDecl *ND = this; 194781ad6265SDimitry Andric if (auto *UD = dyn_cast<UsingShadowDecl>(ND)) 19480b57cec5SDimitry Andric ND = UD->getTargetDecl(); 19490b57cec5SDimitry Andric 19500b57cec5SDimitry Andric if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) 19510b57cec5SDimitry Andric return AD->getClassInterface(); 19520b57cec5SDimitry Andric 19530b57cec5SDimitry Andric if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND)) 19540b57cec5SDimitry Andric return AD->getNamespace(); 19550b57cec5SDimitry Andric 19560b57cec5SDimitry Andric return ND; 19570b57cec5SDimitry Andric } 19580b57cec5SDimitry Andric 19590b57cec5SDimitry Andric bool NamedDecl::isCXXInstanceMember() const { 19600b57cec5SDimitry Andric if (!isCXXClassMember()) 19610b57cec5SDimitry Andric return false; 19620b57cec5SDimitry Andric 19630b57cec5SDimitry Andric const NamedDecl *D = this; 19640b57cec5SDimitry Andric if (isa<UsingShadowDecl>(D)) 19650b57cec5SDimitry Andric D = cast<UsingShadowDecl>(D)->getTargetDecl(); 19660b57cec5SDimitry Andric 19670b57cec5SDimitry Andric if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D)) 19680b57cec5SDimitry Andric return true; 19695f757f3fSDimitry Andric if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(D->getAsFunction())) 19700b57cec5SDimitry Andric return MD->isInstance(); 19710b57cec5SDimitry Andric return false; 19720b57cec5SDimitry Andric } 19730b57cec5SDimitry Andric 19740b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 19750b57cec5SDimitry Andric // DeclaratorDecl Implementation 19760b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 19770b57cec5SDimitry Andric 19780b57cec5SDimitry Andric template <typename DeclT> 19790b57cec5SDimitry Andric static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { 19800b57cec5SDimitry Andric if (decl->getNumTemplateParameterLists() > 0) 19810b57cec5SDimitry Andric return decl->getTemplateParameterList(0)->getTemplateLoc(); 19820b57cec5SDimitry Andric return decl->getInnerLocStart(); 19830b57cec5SDimitry Andric } 19840b57cec5SDimitry Andric 19850b57cec5SDimitry Andric SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { 19860b57cec5SDimitry Andric TypeSourceInfo *TSI = getTypeSourceInfo(); 19870b57cec5SDimitry Andric if (TSI) return TSI->getTypeLoc().getBeginLoc(); 19880b57cec5SDimitry Andric return SourceLocation(); 19890b57cec5SDimitry Andric } 19900b57cec5SDimitry Andric 1991480093f4SDimitry Andric SourceLocation DeclaratorDecl::getTypeSpecEndLoc() const { 1992480093f4SDimitry Andric TypeSourceInfo *TSI = getTypeSourceInfo(); 1993480093f4SDimitry Andric if (TSI) return TSI->getTypeLoc().getEndLoc(); 1994480093f4SDimitry Andric return SourceLocation(); 1995480093f4SDimitry Andric } 1996480093f4SDimitry Andric 19970b57cec5SDimitry Andric void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { 19980b57cec5SDimitry Andric if (QualifierLoc) { 19990b57cec5SDimitry Andric // Make sure the extended decl info is allocated. 20000b57cec5SDimitry Andric if (!hasExtInfo()) { 20010b57cec5SDimitry Andric // Save (non-extended) type source info pointer. 20020b57cec5SDimitry Andric auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 20030b57cec5SDimitry Andric // Allocate external info struct. 20040b57cec5SDimitry Andric DeclInfo = new (getASTContext()) ExtInfo; 20050b57cec5SDimitry Andric // Restore savedTInfo into (extended) decl info. 20060b57cec5SDimitry Andric getExtInfo()->TInfo = savedTInfo; 20070b57cec5SDimitry Andric } 20080b57cec5SDimitry Andric // Set qualifier info. 20090b57cec5SDimitry Andric getExtInfo()->QualifierLoc = QualifierLoc; 2010480093f4SDimitry Andric } else if (hasExtInfo()) { 20110b57cec5SDimitry Andric // Here Qualifier == 0, i.e., we are removing the qualifier (if any). 20120b57cec5SDimitry Andric getExtInfo()->QualifierLoc = QualifierLoc; 20130b57cec5SDimitry Andric } 20140b57cec5SDimitry Andric } 2015480093f4SDimitry Andric 2016480093f4SDimitry Andric void DeclaratorDecl::setTrailingRequiresClause(Expr *TrailingRequiresClause) { 2017480093f4SDimitry Andric assert(TrailingRequiresClause); 2018480093f4SDimitry Andric // Make sure the extended decl info is allocated. 2019480093f4SDimitry Andric if (!hasExtInfo()) { 2020480093f4SDimitry Andric // Save (non-extended) type source info pointer. 2021480093f4SDimitry Andric auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 2022480093f4SDimitry Andric // Allocate external info struct. 2023480093f4SDimitry Andric DeclInfo = new (getASTContext()) ExtInfo; 2024480093f4SDimitry Andric // Restore savedTInfo into (extended) decl info. 2025480093f4SDimitry Andric getExtInfo()->TInfo = savedTInfo; 2026480093f4SDimitry Andric } 2027480093f4SDimitry Andric // Set requires clause info. 2028480093f4SDimitry Andric getExtInfo()->TrailingRequiresClause = TrailingRequiresClause; 20290b57cec5SDimitry Andric } 20300b57cec5SDimitry Andric 20310b57cec5SDimitry Andric void DeclaratorDecl::setTemplateParameterListsInfo( 20320b57cec5SDimitry Andric ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { 20330b57cec5SDimitry Andric assert(!TPLists.empty()); 20340b57cec5SDimitry Andric // Make sure the extended decl info is allocated. 20350b57cec5SDimitry Andric if (!hasExtInfo()) { 20360b57cec5SDimitry Andric // Save (non-extended) type source info pointer. 20370b57cec5SDimitry Andric auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); 20380b57cec5SDimitry Andric // Allocate external info struct. 20390b57cec5SDimitry Andric DeclInfo = new (getASTContext()) ExtInfo; 20400b57cec5SDimitry Andric // Restore savedTInfo into (extended) decl info. 20410b57cec5SDimitry Andric getExtInfo()->TInfo = savedTInfo; 20420b57cec5SDimitry Andric } 20430b57cec5SDimitry Andric // Set the template parameter lists info. 20440b57cec5SDimitry Andric getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); 20450b57cec5SDimitry Andric } 20460b57cec5SDimitry Andric 20470b57cec5SDimitry Andric SourceLocation DeclaratorDecl::getOuterLocStart() const { 20480b57cec5SDimitry Andric return getTemplateOrInnerLocStart(this); 20490b57cec5SDimitry Andric } 20500b57cec5SDimitry Andric 20510b57cec5SDimitry Andric // Helper function: returns true if QT is or contains a type 20520b57cec5SDimitry Andric // having a postfix component. 20530b57cec5SDimitry Andric static bool typeIsPostfix(QualType QT) { 20540b57cec5SDimitry Andric while (true) { 20550b57cec5SDimitry Andric const Type* T = QT.getTypePtr(); 20560b57cec5SDimitry Andric switch (T->getTypeClass()) { 20570b57cec5SDimitry Andric default: 20580b57cec5SDimitry Andric return false; 20590b57cec5SDimitry Andric case Type::Pointer: 20600b57cec5SDimitry Andric QT = cast<PointerType>(T)->getPointeeType(); 20610b57cec5SDimitry Andric break; 20620b57cec5SDimitry Andric case Type::BlockPointer: 20630b57cec5SDimitry Andric QT = cast<BlockPointerType>(T)->getPointeeType(); 20640b57cec5SDimitry Andric break; 20650b57cec5SDimitry Andric case Type::MemberPointer: 20660b57cec5SDimitry Andric QT = cast<MemberPointerType>(T)->getPointeeType(); 20670b57cec5SDimitry Andric break; 20680b57cec5SDimitry Andric case Type::LValueReference: 20690b57cec5SDimitry Andric case Type::RValueReference: 20700b57cec5SDimitry Andric QT = cast<ReferenceType>(T)->getPointeeType(); 20710b57cec5SDimitry Andric break; 20720b57cec5SDimitry Andric case Type::PackExpansion: 20730b57cec5SDimitry Andric QT = cast<PackExpansionType>(T)->getPattern(); 20740b57cec5SDimitry Andric break; 20750b57cec5SDimitry Andric case Type::Paren: 20760b57cec5SDimitry Andric case Type::ConstantArray: 20770b57cec5SDimitry Andric case Type::DependentSizedArray: 20780b57cec5SDimitry Andric case Type::IncompleteArray: 20790b57cec5SDimitry Andric case Type::VariableArray: 20800b57cec5SDimitry Andric case Type::FunctionProto: 20810b57cec5SDimitry Andric case Type::FunctionNoProto: 20820b57cec5SDimitry Andric return true; 20830b57cec5SDimitry Andric } 20840b57cec5SDimitry Andric } 20850b57cec5SDimitry Andric } 20860b57cec5SDimitry Andric 20870b57cec5SDimitry Andric SourceRange DeclaratorDecl::getSourceRange() const { 20880b57cec5SDimitry Andric SourceLocation RangeEnd = getLocation(); 20890b57cec5SDimitry Andric if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { 20900b57cec5SDimitry Andric // If the declaration has no name or the type extends past the name take the 20910b57cec5SDimitry Andric // end location of the type. 20920b57cec5SDimitry Andric if (!getDeclName() || typeIsPostfix(TInfo->getType())) 20930b57cec5SDimitry Andric RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 20940b57cec5SDimitry Andric } 20950b57cec5SDimitry Andric return SourceRange(getOuterLocStart(), RangeEnd); 20960b57cec5SDimitry Andric } 20970b57cec5SDimitry Andric 20980b57cec5SDimitry Andric void QualifierInfo::setTemplateParameterListsInfo( 20990b57cec5SDimitry Andric ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { 21000b57cec5SDimitry Andric // Free previous template parameters (if any). 21010b57cec5SDimitry Andric if (NumTemplParamLists > 0) { 21020b57cec5SDimitry Andric Context.Deallocate(TemplParamLists); 21030b57cec5SDimitry Andric TemplParamLists = nullptr; 21040b57cec5SDimitry Andric NumTemplParamLists = 0; 21050b57cec5SDimitry Andric } 21060b57cec5SDimitry Andric // Set info on matched template parameter lists (if any). 21070b57cec5SDimitry Andric if (!TPLists.empty()) { 21080b57cec5SDimitry Andric TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()]; 21090b57cec5SDimitry Andric NumTemplParamLists = TPLists.size(); 21100b57cec5SDimitry Andric std::copy(TPLists.begin(), TPLists.end(), TemplParamLists); 21110b57cec5SDimitry Andric } 21120b57cec5SDimitry Andric } 21130b57cec5SDimitry Andric 21140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 21150b57cec5SDimitry Andric // VarDecl Implementation 21160b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 21170b57cec5SDimitry Andric 21180b57cec5SDimitry Andric const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { 21190b57cec5SDimitry Andric switch (SC) { 21200b57cec5SDimitry Andric case SC_None: break; 21210b57cec5SDimitry Andric case SC_Auto: return "auto"; 21220b57cec5SDimitry Andric case SC_Extern: return "extern"; 21230b57cec5SDimitry Andric case SC_PrivateExtern: return "__private_extern__"; 21240b57cec5SDimitry Andric case SC_Register: return "register"; 21250b57cec5SDimitry Andric case SC_Static: return "static"; 21260b57cec5SDimitry Andric } 21270b57cec5SDimitry Andric 21280b57cec5SDimitry Andric llvm_unreachable("Invalid storage class"); 21290b57cec5SDimitry Andric } 21300b57cec5SDimitry Andric 21310b57cec5SDimitry Andric VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, 21320b57cec5SDimitry Andric SourceLocation StartLoc, SourceLocation IdLoc, 213381ad6265SDimitry Andric const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, 21340b57cec5SDimitry Andric StorageClass SC) 21350b57cec5SDimitry Andric : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), 21360b57cec5SDimitry Andric redeclarable_base(C) { 21370b57cec5SDimitry Andric static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), 21380b57cec5SDimitry Andric "VarDeclBitfields too large!"); 21390b57cec5SDimitry Andric static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), 21400b57cec5SDimitry Andric "ParmVarDeclBitfields too large!"); 21410b57cec5SDimitry Andric static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned), 21420b57cec5SDimitry Andric "NonParmVarDeclBitfields too large!"); 21430b57cec5SDimitry Andric AllBits = 0; 21440b57cec5SDimitry Andric VarDeclBits.SClass = SC; 21450b57cec5SDimitry Andric // Everything else is implicitly initialized to false. 21460b57cec5SDimitry Andric } 21470b57cec5SDimitry Andric 214881ad6265SDimitry Andric VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation StartL, 214981ad6265SDimitry Andric SourceLocation IdL, const IdentifierInfo *Id, 215081ad6265SDimitry Andric QualType T, TypeSourceInfo *TInfo, StorageClass S) { 21510b57cec5SDimitry Andric return new (C, DC) VarDecl(Var, C, DC, StartL, IdL, Id, T, TInfo, S); 21520b57cec5SDimitry Andric } 21530b57cec5SDimitry Andric 21540b57cec5SDimitry Andric VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 21550b57cec5SDimitry Andric return new (C, ID) 21560b57cec5SDimitry Andric VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, 21570b57cec5SDimitry Andric QualType(), nullptr, SC_None); 21580b57cec5SDimitry Andric } 21590b57cec5SDimitry Andric 21600b57cec5SDimitry Andric void VarDecl::setStorageClass(StorageClass SC) { 21610b57cec5SDimitry Andric assert(isLegalForVariable(SC)); 21620b57cec5SDimitry Andric VarDeclBits.SClass = SC; 21630b57cec5SDimitry Andric } 21640b57cec5SDimitry Andric 21650b57cec5SDimitry Andric VarDecl::TLSKind VarDecl::getTLSKind() const { 21660b57cec5SDimitry Andric switch (VarDeclBits.TSCSpec) { 21670b57cec5SDimitry Andric case TSCS_unspecified: 21680b57cec5SDimitry Andric if (!hasAttr<ThreadAttr>() && 21690b57cec5SDimitry Andric !(getASTContext().getLangOpts().OpenMPUseTLS && 21700b57cec5SDimitry Andric getASTContext().getTargetInfo().isTLSSupported() && 21710b57cec5SDimitry Andric hasAttr<OMPThreadPrivateDeclAttr>())) 21720b57cec5SDimitry Andric return TLS_None; 21730b57cec5SDimitry Andric return ((getASTContext().getLangOpts().isCompatibleWithMSVC( 21740b57cec5SDimitry Andric LangOptions::MSVC2015)) || 21750b57cec5SDimitry Andric hasAttr<OMPThreadPrivateDeclAttr>()) 21760b57cec5SDimitry Andric ? TLS_Dynamic 21770b57cec5SDimitry Andric : TLS_Static; 21780b57cec5SDimitry Andric case TSCS___thread: // Fall through. 21790b57cec5SDimitry Andric case TSCS__Thread_local: 21800b57cec5SDimitry Andric return TLS_Static; 21810b57cec5SDimitry Andric case TSCS_thread_local: 21820b57cec5SDimitry Andric return TLS_Dynamic; 21830b57cec5SDimitry Andric } 21840b57cec5SDimitry Andric llvm_unreachable("Unknown thread storage class specifier!"); 21850b57cec5SDimitry Andric } 21860b57cec5SDimitry Andric 21870b57cec5SDimitry Andric SourceRange VarDecl::getSourceRange() const { 21880b57cec5SDimitry Andric if (const Expr *Init = getInit()) { 21890b57cec5SDimitry Andric SourceLocation InitEnd = Init->getEndLoc(); 21900b57cec5SDimitry Andric // If Init is implicit, ignore its source range and fallback on 21910b57cec5SDimitry Andric // DeclaratorDecl::getSourceRange() to handle postfix elements. 21920b57cec5SDimitry Andric if (InitEnd.isValid() && InitEnd != getLocation()) 21930b57cec5SDimitry Andric return SourceRange(getOuterLocStart(), InitEnd); 21940b57cec5SDimitry Andric } 21950b57cec5SDimitry Andric return DeclaratorDecl::getSourceRange(); 21960b57cec5SDimitry Andric } 21970b57cec5SDimitry Andric 21980b57cec5SDimitry Andric template<typename T> 21990b57cec5SDimitry Andric static LanguageLinkage getDeclLanguageLinkage(const T &D) { 22000b57cec5SDimitry Andric // C++ [dcl.link]p1: All function types, function names with external linkage, 22010b57cec5SDimitry Andric // and variable names with external linkage have a language linkage. 22020b57cec5SDimitry Andric if (!D.hasExternalFormalLinkage()) 22030b57cec5SDimitry Andric return NoLanguageLinkage; 22040b57cec5SDimitry Andric 22050b57cec5SDimitry Andric // Language linkage is a C++ concept, but saying that everything else in C has 22060b57cec5SDimitry Andric // C language linkage fits the implementation nicely. 2207cb14a3feSDimitry Andric if (!D.getASTContext().getLangOpts().CPlusPlus) 22080b57cec5SDimitry Andric return CLanguageLinkage; 22090b57cec5SDimitry Andric 22100b57cec5SDimitry Andric // C++ [dcl.link]p4: A C language linkage is ignored in determining the 22110b57cec5SDimitry Andric // language linkage of the names of class members and the function type of 22120b57cec5SDimitry Andric // class member functions. 22130b57cec5SDimitry Andric const DeclContext *DC = D.getDeclContext(); 22140b57cec5SDimitry Andric if (DC->isRecord()) 22150b57cec5SDimitry Andric return CXXLanguageLinkage; 22160b57cec5SDimitry Andric 22170b57cec5SDimitry Andric // If the first decl is in an extern "C" context, any other redeclaration 22180b57cec5SDimitry Andric // will have C language linkage. If the first one is not in an extern "C" 22190b57cec5SDimitry Andric // context, we would have reported an error for any other decl being in one. 22200b57cec5SDimitry Andric if (isFirstInExternCContext(&D)) 22210b57cec5SDimitry Andric return CLanguageLinkage; 22220b57cec5SDimitry Andric return CXXLanguageLinkage; 22230b57cec5SDimitry Andric } 22240b57cec5SDimitry Andric 22250b57cec5SDimitry Andric template<typename T> 22260b57cec5SDimitry Andric static bool isDeclExternC(const T &D) { 22270b57cec5SDimitry Andric // Since the context is ignored for class members, they can only have C++ 22280b57cec5SDimitry Andric // language linkage or no language linkage. 22290b57cec5SDimitry Andric const DeclContext *DC = D.getDeclContext(); 22300b57cec5SDimitry Andric if (DC->isRecord()) { 22310b57cec5SDimitry Andric assert(D.getASTContext().getLangOpts().CPlusPlus); 22320b57cec5SDimitry Andric return false; 22330b57cec5SDimitry Andric } 22340b57cec5SDimitry Andric 22350b57cec5SDimitry Andric return D.getLanguageLinkage() == CLanguageLinkage; 22360b57cec5SDimitry Andric } 22370b57cec5SDimitry Andric 22380b57cec5SDimitry Andric LanguageLinkage VarDecl::getLanguageLinkage() const { 22390b57cec5SDimitry Andric return getDeclLanguageLinkage(*this); 22400b57cec5SDimitry Andric } 22410b57cec5SDimitry Andric 22420b57cec5SDimitry Andric bool VarDecl::isExternC() const { 22430b57cec5SDimitry Andric return isDeclExternC(*this); 22440b57cec5SDimitry Andric } 22450b57cec5SDimitry Andric 22460b57cec5SDimitry Andric bool VarDecl::isInExternCContext() const { 22470b57cec5SDimitry Andric return getLexicalDeclContext()->isExternCContext(); 22480b57cec5SDimitry Andric } 22490b57cec5SDimitry Andric 22500b57cec5SDimitry Andric bool VarDecl::isInExternCXXContext() const { 22510b57cec5SDimitry Andric return getLexicalDeclContext()->isExternCXXContext(); 22520b57cec5SDimitry Andric } 22530b57cec5SDimitry Andric 22540b57cec5SDimitry Andric VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } 22550b57cec5SDimitry Andric 22560b57cec5SDimitry Andric VarDecl::DefinitionKind 22570b57cec5SDimitry Andric VarDecl::isThisDeclarationADefinition(ASTContext &C) const { 22580b57cec5SDimitry Andric if (isThisDeclarationADemotedDefinition()) 22590b57cec5SDimitry Andric return DeclarationOnly; 22600b57cec5SDimitry Andric 22610b57cec5SDimitry Andric // C++ [basic.def]p2: 22620b57cec5SDimitry Andric // A declaration is a definition unless [...] it contains the 'extern' 22630b57cec5SDimitry Andric // specifier or a linkage-specification and neither an initializer [...], 22640b57cec5SDimitry Andric // it declares a non-inline static data member in a class declaration [...], 22650b57cec5SDimitry Andric // it declares a static data member outside a class definition and the variable 22660b57cec5SDimitry Andric // was defined within the class with the constexpr specifier [...], 22670b57cec5SDimitry Andric // C++1y [temp.expl.spec]p15: 22680b57cec5SDimitry Andric // An explicit specialization of a static data member or an explicit 22690b57cec5SDimitry Andric // specialization of a static data member template is a definition if the 22700b57cec5SDimitry Andric // declaration includes an initializer; otherwise, it is a declaration. 22710b57cec5SDimitry Andric // 22720b57cec5SDimitry Andric // FIXME: How do you declare (but not define) a partial specialization of 22730b57cec5SDimitry Andric // a static data member template outside the containing class? 22740b57cec5SDimitry Andric if (isStaticDataMember()) { 22750b57cec5SDimitry Andric if (isOutOfLine() && 22760b57cec5SDimitry Andric !(getCanonicalDecl()->isInline() && 22770b57cec5SDimitry Andric getCanonicalDecl()->isConstexpr()) && 22780b57cec5SDimitry Andric (hasInit() || 22790b57cec5SDimitry Andric // If the first declaration is out-of-line, this may be an 22800b57cec5SDimitry Andric // instantiation of an out-of-line partial specialization of a variable 22810b57cec5SDimitry Andric // template for which we have not yet instantiated the initializer. 22820b57cec5SDimitry Andric (getFirstDecl()->isOutOfLine() 22830b57cec5SDimitry Andric ? getTemplateSpecializationKind() == TSK_Undeclared 22840b57cec5SDimitry Andric : getTemplateSpecializationKind() != 22850b57cec5SDimitry Andric TSK_ExplicitSpecialization) || 22860b57cec5SDimitry Andric isa<VarTemplatePartialSpecializationDecl>(this))) 22870b57cec5SDimitry Andric return Definition; 2288e8d8bef9SDimitry Andric if (!isOutOfLine() && isInline()) 22890b57cec5SDimitry Andric return Definition; 22900b57cec5SDimitry Andric return DeclarationOnly; 22910b57cec5SDimitry Andric } 22920b57cec5SDimitry Andric // C99 6.7p5: 22930b57cec5SDimitry Andric // A definition of an identifier is a declaration for that identifier that 22940b57cec5SDimitry Andric // [...] causes storage to be reserved for that object. 22950b57cec5SDimitry Andric // Note: that applies for all non-file-scope objects. 22960b57cec5SDimitry Andric // C99 6.9.2p1: 22970b57cec5SDimitry Andric // If the declaration of an identifier for an object has file scope and an 22980b57cec5SDimitry Andric // initializer, the declaration is an external definition for the identifier 22990b57cec5SDimitry Andric if (hasInit()) 23000b57cec5SDimitry Andric return Definition; 23010b57cec5SDimitry Andric 23020b57cec5SDimitry Andric if (hasDefiningAttr()) 23030b57cec5SDimitry Andric return Definition; 23040b57cec5SDimitry Andric 23050b57cec5SDimitry Andric if (const auto *SAA = getAttr<SelectAnyAttr>()) 23060b57cec5SDimitry Andric if (!SAA->isInherited()) 23070b57cec5SDimitry Andric return Definition; 23080b57cec5SDimitry Andric 23090b57cec5SDimitry Andric // A variable template specialization (other than a static data member 23100b57cec5SDimitry Andric // template or an explicit specialization) is a declaration until we 23110b57cec5SDimitry Andric // instantiate its initializer. 23120b57cec5SDimitry Andric if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(this)) { 23130b57cec5SDimitry Andric if (VTSD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && 23140b57cec5SDimitry Andric !isa<VarTemplatePartialSpecializationDecl>(VTSD) && 23150b57cec5SDimitry Andric !VTSD->IsCompleteDefinition) 23160b57cec5SDimitry Andric return DeclarationOnly; 23170b57cec5SDimitry Andric } 23180b57cec5SDimitry Andric 23190b57cec5SDimitry Andric if (hasExternalStorage()) 23200b57cec5SDimitry Andric return DeclarationOnly; 23210b57cec5SDimitry Andric 23220b57cec5SDimitry Andric // [dcl.link] p7: 23230b57cec5SDimitry Andric // A declaration directly contained in a linkage-specification is treated 23240b57cec5SDimitry Andric // as if it contains the extern specifier for the purpose of determining 23250b57cec5SDimitry Andric // the linkage of the declared name and whether it is a definition. 23260b57cec5SDimitry Andric if (isSingleLineLanguageLinkage(*this)) 23270b57cec5SDimitry Andric return DeclarationOnly; 23280b57cec5SDimitry Andric 23290b57cec5SDimitry Andric // C99 6.9.2p2: 23300b57cec5SDimitry Andric // A declaration of an object that has file scope without an initializer, 23310b57cec5SDimitry Andric // and without a storage class specifier or the scs 'static', constitutes 23320b57cec5SDimitry Andric // a tentative definition. 23330b57cec5SDimitry Andric // No such thing in C++. 23340b57cec5SDimitry Andric if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) 23350b57cec5SDimitry Andric return TentativeDefinition; 23360b57cec5SDimitry Andric 23370b57cec5SDimitry Andric // What's left is (in C, block-scope) declarations without initializers or 23380b57cec5SDimitry Andric // external storage. These are definitions. 23390b57cec5SDimitry Andric return Definition; 23400b57cec5SDimitry Andric } 23410b57cec5SDimitry Andric 23420b57cec5SDimitry Andric VarDecl *VarDecl::getActingDefinition() { 23430b57cec5SDimitry Andric DefinitionKind Kind = isThisDeclarationADefinition(); 23440b57cec5SDimitry Andric if (Kind != TentativeDefinition) 23450b57cec5SDimitry Andric return nullptr; 23460b57cec5SDimitry Andric 23470b57cec5SDimitry Andric VarDecl *LastTentative = nullptr; 2348349cc55cSDimitry Andric 2349349cc55cSDimitry Andric // Loop through the declaration chain, starting with the most recent. 2350349cc55cSDimitry Andric for (VarDecl *Decl = getMostRecentDecl(); Decl; 2351349cc55cSDimitry Andric Decl = Decl->getPreviousDecl()) { 2352349cc55cSDimitry Andric Kind = Decl->isThisDeclarationADefinition(); 23530b57cec5SDimitry Andric if (Kind == Definition) 23540b57cec5SDimitry Andric return nullptr; 2355349cc55cSDimitry Andric // Record the first (most recent) TentativeDefinition that is encountered. 2356349cc55cSDimitry Andric if (Kind == TentativeDefinition && !LastTentative) 2357349cc55cSDimitry Andric LastTentative = Decl; 23580b57cec5SDimitry Andric } 2359349cc55cSDimitry Andric 23600b57cec5SDimitry Andric return LastTentative; 23610b57cec5SDimitry Andric } 23620b57cec5SDimitry Andric 23630b57cec5SDimitry Andric VarDecl *VarDecl::getDefinition(ASTContext &C) { 23640b57cec5SDimitry Andric VarDecl *First = getFirstDecl(); 2365bdd1243dSDimitry Andric for (auto *I : First->redecls()) { 23660b57cec5SDimitry Andric if (I->isThisDeclarationADefinition(C) == Definition) 23670b57cec5SDimitry Andric return I; 23680b57cec5SDimitry Andric } 23690b57cec5SDimitry Andric return nullptr; 23700b57cec5SDimitry Andric } 23710b57cec5SDimitry Andric 23720b57cec5SDimitry Andric VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { 23730b57cec5SDimitry Andric DefinitionKind Kind = DeclarationOnly; 23740b57cec5SDimitry Andric 23750b57cec5SDimitry Andric const VarDecl *First = getFirstDecl(); 2376bdd1243dSDimitry Andric for (auto *I : First->redecls()) { 23770b57cec5SDimitry Andric Kind = std::max(Kind, I->isThisDeclarationADefinition(C)); 23780b57cec5SDimitry Andric if (Kind == Definition) 23790b57cec5SDimitry Andric break; 23800b57cec5SDimitry Andric } 23810b57cec5SDimitry Andric 23820b57cec5SDimitry Andric return Kind; 23830b57cec5SDimitry Andric } 23840b57cec5SDimitry Andric 23850b57cec5SDimitry Andric const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { 2386bdd1243dSDimitry Andric for (auto *I : redecls()) { 23870b57cec5SDimitry Andric if (auto Expr = I->getInit()) { 23880b57cec5SDimitry Andric D = I; 23890b57cec5SDimitry Andric return Expr; 23900b57cec5SDimitry Andric } 23910b57cec5SDimitry Andric } 23920b57cec5SDimitry Andric return nullptr; 23930b57cec5SDimitry Andric } 23940b57cec5SDimitry Andric 23950b57cec5SDimitry Andric bool VarDecl::hasInit() const { 23960b57cec5SDimitry Andric if (auto *P = dyn_cast<ParmVarDecl>(this)) 23970b57cec5SDimitry Andric if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg()) 23980b57cec5SDimitry Andric return false; 23990b57cec5SDimitry Andric 24000b57cec5SDimitry Andric return !Init.isNull(); 24010b57cec5SDimitry Andric } 24020b57cec5SDimitry Andric 24030b57cec5SDimitry Andric Expr *VarDecl::getInit() { 24040b57cec5SDimitry Andric if (!hasInit()) 24050b57cec5SDimitry Andric return nullptr; 24060b57cec5SDimitry Andric 24070b57cec5SDimitry Andric if (auto *S = Init.dyn_cast<Stmt *>()) 24080b57cec5SDimitry Andric return cast<Expr>(S); 24090b57cec5SDimitry Andric 241006c3fb27SDimitry Andric auto *Eval = getEvaluatedStmt(); 241106c3fb27SDimitry Andric return cast<Expr>(Eval->Value.isOffset() 241206c3fb27SDimitry Andric ? Eval->Value.get(getASTContext().getExternalSource()) 241306c3fb27SDimitry Andric : Eval->Value.get(nullptr)); 24140b57cec5SDimitry Andric } 24150b57cec5SDimitry Andric 24160b57cec5SDimitry Andric Stmt **VarDecl::getInitAddress() { 24170b57cec5SDimitry Andric if (auto *ES = Init.dyn_cast<EvaluatedStmt *>()) 241806c3fb27SDimitry Andric return ES->Value.getAddressOfPointer(getASTContext().getExternalSource()); 24190b57cec5SDimitry Andric 24200b57cec5SDimitry Andric return Init.getAddrOfPtr1(); 24210b57cec5SDimitry Andric } 24220b57cec5SDimitry Andric 2423a7dea167SDimitry Andric VarDecl *VarDecl::getInitializingDeclaration() { 2424a7dea167SDimitry Andric VarDecl *Def = nullptr; 2425bdd1243dSDimitry Andric for (auto *I : redecls()) { 2426a7dea167SDimitry Andric if (I->hasInit()) 2427a7dea167SDimitry Andric return I; 2428a7dea167SDimitry Andric 2429a7dea167SDimitry Andric if (I->isThisDeclarationADefinition()) { 2430a7dea167SDimitry Andric if (isStaticDataMember()) 2431a7dea167SDimitry Andric return I; 2432a7dea167SDimitry Andric Def = I; 2433a7dea167SDimitry Andric } 2434a7dea167SDimitry Andric } 2435a7dea167SDimitry Andric return Def; 2436a7dea167SDimitry Andric } 2437a7dea167SDimitry Andric 24380b57cec5SDimitry Andric bool VarDecl::isOutOfLine() const { 24390b57cec5SDimitry Andric if (Decl::isOutOfLine()) 24400b57cec5SDimitry Andric return true; 24410b57cec5SDimitry Andric 24420b57cec5SDimitry Andric if (!isStaticDataMember()) 24430b57cec5SDimitry Andric return false; 24440b57cec5SDimitry Andric 24450b57cec5SDimitry Andric // If this static data member was instantiated from a static data member of 24460b57cec5SDimitry Andric // a class template, check whether that static data member was defined 24470b57cec5SDimitry Andric // out-of-line. 24480b57cec5SDimitry Andric if (VarDecl *VD = getInstantiatedFromStaticDataMember()) 24490b57cec5SDimitry Andric return VD->isOutOfLine(); 24500b57cec5SDimitry Andric 24510b57cec5SDimitry Andric return false; 24520b57cec5SDimitry Andric } 24530b57cec5SDimitry Andric 24540b57cec5SDimitry Andric void VarDecl::setInit(Expr *I) { 24550b57cec5SDimitry Andric if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) { 24560b57cec5SDimitry Andric Eval->~EvaluatedStmt(); 24570b57cec5SDimitry Andric getASTContext().Deallocate(Eval); 24580b57cec5SDimitry Andric } 24590b57cec5SDimitry Andric 24600b57cec5SDimitry Andric Init = I; 24610b57cec5SDimitry Andric } 24620b57cec5SDimitry Andric 2463e8d8bef9SDimitry Andric bool VarDecl::mightBeUsableInConstantExpressions(const ASTContext &C) const { 24640b57cec5SDimitry Andric const LangOptions &Lang = C.getLangOpts(); 24650b57cec5SDimitry Andric 2466e8d8bef9SDimitry Andric // OpenCL permits const integral variables to be used in constant 2467e8d8bef9SDimitry Andric // expressions, like in C++98. 2468e8d8bef9SDimitry Andric if (!Lang.CPlusPlus && !Lang.OpenCL) 24690b57cec5SDimitry Andric return false; 24700b57cec5SDimitry Andric 24710b57cec5SDimitry Andric // Function parameters are never usable in constant expressions. 24720b57cec5SDimitry Andric if (isa<ParmVarDecl>(this)) 24730b57cec5SDimitry Andric return false; 24740b57cec5SDimitry Andric 2475e8d8bef9SDimitry Andric // The values of weak variables are never usable in constant expressions. 2476e8d8bef9SDimitry Andric if (isWeak()) 2477e8d8bef9SDimitry Andric return false; 2478e8d8bef9SDimitry Andric 24790b57cec5SDimitry Andric // In C++11, any variable of reference type can be used in a constant 24800b57cec5SDimitry Andric // expression if it is initialized by a constant expression. 24810b57cec5SDimitry Andric if (Lang.CPlusPlus11 && getType()->isReferenceType()) 24820b57cec5SDimitry Andric return true; 24830b57cec5SDimitry Andric 24840b57cec5SDimitry Andric // Only const objects can be used in constant expressions in C++. C++98 does 24850b57cec5SDimitry Andric // not require the variable to be non-volatile, but we consider this to be a 24860b57cec5SDimitry Andric // defect. 2487e8d8bef9SDimitry Andric if (!getType().isConstant(C) || getType().isVolatileQualified()) 24880b57cec5SDimitry Andric return false; 24890b57cec5SDimitry Andric 24900b57cec5SDimitry Andric // In C++, const, non-volatile variables of integral or enumeration types 24910b57cec5SDimitry Andric // can be used in constant expressions. 24920b57cec5SDimitry Andric if (getType()->isIntegralOrEnumerationType()) 24930b57cec5SDimitry Andric return true; 24940b57cec5SDimitry Andric 24950b57cec5SDimitry Andric // Additionally, in C++11, non-volatile constexpr variables can be used in 24960b57cec5SDimitry Andric // constant expressions. 24970b57cec5SDimitry Andric return Lang.CPlusPlus11 && isConstexpr(); 24980b57cec5SDimitry Andric } 24990b57cec5SDimitry Andric 2500e8d8bef9SDimitry Andric bool VarDecl::isUsableInConstantExpressions(const ASTContext &Context) const { 25010b57cec5SDimitry Andric // C++2a [expr.const]p3: 25020b57cec5SDimitry Andric // A variable is usable in constant expressions after its initializing 25030b57cec5SDimitry Andric // declaration is encountered... 25040b57cec5SDimitry Andric const VarDecl *DefVD = nullptr; 25050b57cec5SDimitry Andric const Expr *Init = getAnyInitializer(DefVD); 25060b57cec5SDimitry Andric if (!Init || Init->isValueDependent() || getType()->isDependentType()) 25070b57cec5SDimitry Andric return false; 25080b57cec5SDimitry Andric // ... if it is a constexpr variable, or it is of reference type or of 25090b57cec5SDimitry Andric // const-qualified integral or enumeration type, ... 25100b57cec5SDimitry Andric if (!DefVD->mightBeUsableInConstantExpressions(Context)) 25110b57cec5SDimitry Andric return false; 25120b57cec5SDimitry Andric // ... and its initializer is a constant initializer. 2513e8d8bef9SDimitry Andric if (Context.getLangOpts().CPlusPlus && !DefVD->hasConstantInitialization()) 2514e8d8bef9SDimitry Andric return false; 2515e8d8bef9SDimitry Andric // C++98 [expr.const]p1: 2516e8d8bef9SDimitry Andric // An integral constant-expression can involve only [...] const variables 2517e8d8bef9SDimitry Andric // or static data members of integral or enumeration types initialized with 2518e8d8bef9SDimitry Andric // [integer] constant expressions (dcl.init) 2519e8d8bef9SDimitry Andric if ((Context.getLangOpts().CPlusPlus || Context.getLangOpts().OpenCL) && 2520e8d8bef9SDimitry Andric !Context.getLangOpts().CPlusPlus11 && !DefVD->hasICEInitializer(Context)) 2521e8d8bef9SDimitry Andric return false; 2522e8d8bef9SDimitry Andric return true; 25230b57cec5SDimitry Andric } 25240b57cec5SDimitry Andric 25250b57cec5SDimitry Andric /// Convert the initializer for this declaration to the elaborated EvaluatedStmt 25260b57cec5SDimitry Andric /// form, which contains extra information on the evaluated value of the 25270b57cec5SDimitry Andric /// initializer. 25280b57cec5SDimitry Andric EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { 25290b57cec5SDimitry Andric auto *Eval = Init.dyn_cast<EvaluatedStmt *>(); 25300b57cec5SDimitry Andric if (!Eval) { 25310b57cec5SDimitry Andric // Note: EvaluatedStmt contains an APValue, which usually holds 25320b57cec5SDimitry Andric // resources not allocated from the ASTContext. We need to do some 25330b57cec5SDimitry Andric // work to avoid leaking those, but we do so in VarDecl::evaluateValue 25340b57cec5SDimitry Andric // where we can detect whether there's anything to clean up or not. 25350b57cec5SDimitry Andric Eval = new (getASTContext()) EvaluatedStmt; 25360b57cec5SDimitry Andric Eval->Value = Init.get<Stmt *>(); 25370b57cec5SDimitry Andric Init = Eval; 25380b57cec5SDimitry Andric } 25390b57cec5SDimitry Andric return Eval; 25400b57cec5SDimitry Andric } 25410b57cec5SDimitry Andric 2542e8d8bef9SDimitry Andric EvaluatedStmt *VarDecl::getEvaluatedStmt() const { 2543e8d8bef9SDimitry Andric return Init.dyn_cast<EvaluatedStmt *>(); 25440b57cec5SDimitry Andric } 25450b57cec5SDimitry Andric 2546e8d8bef9SDimitry Andric APValue *VarDecl::evaluateValue() const { 2547e8d8bef9SDimitry Andric SmallVector<PartialDiagnosticAt, 8> Notes; 254806c3fb27SDimitry Andric return evaluateValueImpl(Notes, hasConstantInitialization()); 2549e8d8bef9SDimitry Andric } 2550e8d8bef9SDimitry Andric 255106c3fb27SDimitry Andric APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes, 255206c3fb27SDimitry Andric bool IsConstantInitialization) const { 25530b57cec5SDimitry Andric EvaluatedStmt *Eval = ensureEvaluatedStmt(); 25540b57cec5SDimitry Andric 255506c3fb27SDimitry Andric const auto *Init = getInit(); 2556e8d8bef9SDimitry Andric assert(!Init->isValueDependent()); 2557e8d8bef9SDimitry Andric 25580b57cec5SDimitry Andric // We only produce notes indicating why an initializer is non-constant the 25590b57cec5SDimitry Andric // first time it is evaluated. FIXME: The notes won't always be emitted the 25600b57cec5SDimitry Andric // first time we try evaluation, so might not be produced at all. 25610b57cec5SDimitry Andric if (Eval->WasEvaluated) 25620b57cec5SDimitry Andric return Eval->Evaluated.isAbsent() ? nullptr : &Eval->Evaluated; 25630b57cec5SDimitry Andric 25640b57cec5SDimitry Andric if (Eval->IsEvaluating) { 25650b57cec5SDimitry Andric // FIXME: Produce a diagnostic for self-initialization. 25660b57cec5SDimitry Andric return nullptr; 25670b57cec5SDimitry Andric } 25680b57cec5SDimitry Andric 25690b57cec5SDimitry Andric Eval->IsEvaluating = true; 25700b57cec5SDimitry Andric 257106c3fb27SDimitry Andric ASTContext &Ctx = getASTContext(); 257206c3fb27SDimitry Andric bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, Ctx, this, Notes, 257306c3fb27SDimitry Andric IsConstantInitialization); 257406c3fb27SDimitry Andric 25755f757f3fSDimitry Andric // In C++, this isn't a constant initializer if we produced notes. In that 257606c3fb27SDimitry Andric // case, we can't keep the result, because it may only be correct under the 257706c3fb27SDimitry Andric // assumption that the initializer is a constant context. 25785f757f3fSDimitry Andric if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus && 257906c3fb27SDimitry Andric !Notes.empty()) 258006c3fb27SDimitry Andric Result = false; 25810b57cec5SDimitry Andric 25820b57cec5SDimitry Andric // Ensure the computed APValue is cleaned up later if evaluation succeeded, 25830b57cec5SDimitry Andric // or that it's empty (so that there's nothing to clean up) if evaluation 25840b57cec5SDimitry Andric // failed. 25850b57cec5SDimitry Andric if (!Result) 25860b57cec5SDimitry Andric Eval->Evaluated = APValue(); 25870b57cec5SDimitry Andric else if (Eval->Evaluated.needsCleanup()) 258806c3fb27SDimitry Andric Ctx.addDestruction(&Eval->Evaluated); 25890b57cec5SDimitry Andric 25900b57cec5SDimitry Andric Eval->IsEvaluating = false; 25910b57cec5SDimitry Andric Eval->WasEvaluated = true; 25920b57cec5SDimitry Andric 25930b57cec5SDimitry Andric return Result ? &Eval->Evaluated : nullptr; 25940b57cec5SDimitry Andric } 25950b57cec5SDimitry Andric 25960b57cec5SDimitry Andric APValue *VarDecl::getEvaluatedValue() const { 2597e8d8bef9SDimitry Andric if (EvaluatedStmt *Eval = getEvaluatedStmt()) 25980b57cec5SDimitry Andric if (Eval->WasEvaluated) 25990b57cec5SDimitry Andric return &Eval->Evaluated; 26000b57cec5SDimitry Andric 26010b57cec5SDimitry Andric return nullptr; 26020b57cec5SDimitry Andric } 26030b57cec5SDimitry Andric 2604e8d8bef9SDimitry Andric bool VarDecl::hasICEInitializer(const ASTContext &Context) const { 2605e8d8bef9SDimitry Andric const Expr *Init = getInit(); 2606e8d8bef9SDimitry Andric assert(Init && "no initializer"); 26070b57cec5SDimitry Andric 26080b57cec5SDimitry Andric EvaluatedStmt *Eval = ensureEvaluatedStmt(); 2609e8d8bef9SDimitry Andric if (!Eval->CheckedForICEInit) { 2610e8d8bef9SDimitry Andric Eval->CheckedForICEInit = true; 2611e8d8bef9SDimitry Andric Eval->HasICEInit = Init->isIntegerConstantExpr(Context); 2612e8d8bef9SDimitry Andric } 2613e8d8bef9SDimitry Andric return Eval->HasICEInit; 26140b57cec5SDimitry Andric } 26150b57cec5SDimitry Andric 2616e8d8bef9SDimitry Andric bool VarDecl::hasConstantInitialization() const { 2617e8d8bef9SDimitry Andric // In C, all globals (and only globals) have constant initialization. 2618e8d8bef9SDimitry Andric if (hasGlobalStorage() && !getASTContext().getLangOpts().CPlusPlus) 2619e8d8bef9SDimitry Andric return true; 26200b57cec5SDimitry Andric 2621e8d8bef9SDimitry Andric // In C++, it depends on whether the evaluation at the point of definition 2622e8d8bef9SDimitry Andric // was evaluatable as a constant initializer. 2623e8d8bef9SDimitry Andric if (EvaluatedStmt *Eval = getEvaluatedStmt()) 2624e8d8bef9SDimitry Andric return Eval->HasConstantInitialization; 2625e8d8bef9SDimitry Andric 26260b57cec5SDimitry Andric return false; 2627e8d8bef9SDimitry Andric } 26280b57cec5SDimitry Andric 2629e8d8bef9SDimitry Andric bool VarDecl::checkForConstantInitialization( 2630e8d8bef9SDimitry Andric SmallVectorImpl<PartialDiagnosticAt> &Notes) const { 2631e8d8bef9SDimitry Andric EvaluatedStmt *Eval = ensureEvaluatedStmt(); 2632e8d8bef9SDimitry Andric // If we ask for the value before we know whether we have a constant 2633e8d8bef9SDimitry Andric // initializer, we can compute the wrong value (for example, due to 2634e8d8bef9SDimitry Andric // std::is_constant_evaluated()). 2635e8d8bef9SDimitry Andric assert(!Eval->WasEvaluated && 2636e8d8bef9SDimitry Andric "already evaluated var value before checking for constant init"); 2637e8d8bef9SDimitry Andric assert(getASTContext().getLangOpts().CPlusPlus && "only meaningful in C++"); 2638e8d8bef9SDimitry Andric 263906c3fb27SDimitry Andric assert(!getInit()->isValueDependent()); 2640e8d8bef9SDimitry Andric 2641e8d8bef9SDimitry Andric // Evaluate the initializer to check whether it's a constant expression. 264206c3fb27SDimitry Andric Eval->HasConstantInitialization = 264306c3fb27SDimitry Andric evaluateValueImpl(Notes, true) && Notes.empty(); 264406c3fb27SDimitry Andric 264506c3fb27SDimitry Andric // If evaluation as a constant initializer failed, allow re-evaluation as a 264606c3fb27SDimitry Andric // non-constant initializer if we later find we want the value. 264706c3fb27SDimitry Andric if (!Eval->HasConstantInitialization) 264806c3fb27SDimitry Andric Eval->WasEvaluated = false; 264906c3fb27SDimitry Andric 2650e8d8bef9SDimitry Andric return Eval->HasConstantInitialization; 26510b57cec5SDimitry Andric } 26520b57cec5SDimitry Andric 26530b57cec5SDimitry Andric bool VarDecl::isParameterPack() const { 26540b57cec5SDimitry Andric return isa<PackExpansionType>(getType()); 26550b57cec5SDimitry Andric } 26560b57cec5SDimitry Andric 26570b57cec5SDimitry Andric template<typename DeclT> 26580b57cec5SDimitry Andric static DeclT *getDefinitionOrSelf(DeclT *D) { 26590b57cec5SDimitry Andric assert(D); 26600b57cec5SDimitry Andric if (auto *Def = D->getDefinition()) 26610b57cec5SDimitry Andric return Def; 26620b57cec5SDimitry Andric return D; 26630b57cec5SDimitry Andric } 26640b57cec5SDimitry Andric 26650b57cec5SDimitry Andric bool VarDecl::isEscapingByref() const { 26660b57cec5SDimitry Andric return hasAttr<BlocksAttr>() && NonParmVarDeclBits.EscapingByref; 26670b57cec5SDimitry Andric } 26680b57cec5SDimitry Andric 26690b57cec5SDimitry Andric bool VarDecl::isNonEscapingByref() const { 26700b57cec5SDimitry Andric return hasAttr<BlocksAttr>() && !NonParmVarDeclBits.EscapingByref; 26710b57cec5SDimitry Andric } 26720b57cec5SDimitry Andric 2673fe6060f1SDimitry Andric bool VarDecl::hasDependentAlignment() const { 2674fe6060f1SDimitry Andric QualType T = getType(); 2675bdd1243dSDimitry Andric return T->isDependentType() || T->isUndeducedType() || 2676fe6060f1SDimitry Andric llvm::any_of(specific_attrs<AlignedAttr>(), [](const AlignedAttr *AA) { 2677fe6060f1SDimitry Andric return AA->isAlignmentDependent(); 2678fe6060f1SDimitry Andric }); 2679fe6060f1SDimitry Andric } 2680fe6060f1SDimitry Andric 26810b57cec5SDimitry Andric VarDecl *VarDecl::getTemplateInstantiationPattern() const { 26820b57cec5SDimitry Andric const VarDecl *VD = this; 26830b57cec5SDimitry Andric 26840b57cec5SDimitry Andric // If this is an instantiated member, walk back to the template from which 26850b57cec5SDimitry Andric // it was instantiated. 26860b57cec5SDimitry Andric if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) { 26870b57cec5SDimitry Andric if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { 26880b57cec5SDimitry Andric VD = VD->getInstantiatedFromStaticDataMember(); 26890b57cec5SDimitry Andric while (auto *NewVD = VD->getInstantiatedFromStaticDataMember()) 26900b57cec5SDimitry Andric VD = NewVD; 26910b57cec5SDimitry Andric } 26920b57cec5SDimitry Andric } 26930b57cec5SDimitry Andric 26940b57cec5SDimitry Andric // If it's an instantiated variable template specialization, find the 26950b57cec5SDimitry Andric // template or partial specialization from which it was instantiated. 26960b57cec5SDimitry Andric if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(VD)) { 26970b57cec5SDimitry Andric if (isTemplateInstantiation(VDTemplSpec->getTemplateSpecializationKind())) { 26980b57cec5SDimitry Andric auto From = VDTemplSpec->getInstantiatedFrom(); 26990b57cec5SDimitry Andric if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) { 27000b57cec5SDimitry Andric while (!VTD->isMemberSpecialization()) { 27010b57cec5SDimitry Andric auto *NewVTD = VTD->getInstantiatedFromMemberTemplate(); 27020b57cec5SDimitry Andric if (!NewVTD) 27030b57cec5SDimitry Andric break; 27040b57cec5SDimitry Andric VTD = NewVTD; 27050b57cec5SDimitry Andric } 27060b57cec5SDimitry Andric return getDefinitionOrSelf(VTD->getTemplatedDecl()); 27070b57cec5SDimitry Andric } 27080b57cec5SDimitry Andric if (auto *VTPSD = 27090b57cec5SDimitry Andric From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { 27100b57cec5SDimitry Andric while (!VTPSD->isMemberSpecialization()) { 27110b57cec5SDimitry Andric auto *NewVTPSD = VTPSD->getInstantiatedFromMember(); 27120b57cec5SDimitry Andric if (!NewVTPSD) 27130b57cec5SDimitry Andric break; 27140b57cec5SDimitry Andric VTPSD = NewVTPSD; 27150b57cec5SDimitry Andric } 27160b57cec5SDimitry Andric return getDefinitionOrSelf<VarDecl>(VTPSD); 27170b57cec5SDimitry Andric } 27180b57cec5SDimitry Andric } 27190b57cec5SDimitry Andric } 27200b57cec5SDimitry Andric 27210b57cec5SDimitry Andric // If this is the pattern of a variable template, find where it was 27220b57cec5SDimitry Andric // instantiated from. FIXME: Is this necessary? 27230b57cec5SDimitry Andric if (VarTemplateDecl *VarTemplate = VD->getDescribedVarTemplate()) { 27240b57cec5SDimitry Andric while (!VarTemplate->isMemberSpecialization()) { 27250b57cec5SDimitry Andric auto *NewVT = VarTemplate->getInstantiatedFromMemberTemplate(); 27260b57cec5SDimitry Andric if (!NewVT) 27270b57cec5SDimitry Andric break; 27280b57cec5SDimitry Andric VarTemplate = NewVT; 27290b57cec5SDimitry Andric } 27300b57cec5SDimitry Andric 27310b57cec5SDimitry Andric return getDefinitionOrSelf(VarTemplate->getTemplatedDecl()); 27320b57cec5SDimitry Andric } 27330b57cec5SDimitry Andric 27340b57cec5SDimitry Andric if (VD == this) 27350b57cec5SDimitry Andric return nullptr; 27360b57cec5SDimitry Andric return getDefinitionOrSelf(const_cast<VarDecl*>(VD)); 27370b57cec5SDimitry Andric } 27380b57cec5SDimitry Andric 27390b57cec5SDimitry Andric VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { 27400b57cec5SDimitry Andric if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 27410b57cec5SDimitry Andric return cast<VarDecl>(MSI->getInstantiatedFrom()); 27420b57cec5SDimitry Andric 27430b57cec5SDimitry Andric return nullptr; 27440b57cec5SDimitry Andric } 27450b57cec5SDimitry Andric 27460b57cec5SDimitry Andric TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { 27470b57cec5SDimitry Andric if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) 27480b57cec5SDimitry Andric return Spec->getSpecializationKind(); 27490b57cec5SDimitry Andric 27500b57cec5SDimitry Andric if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 27510b57cec5SDimitry Andric return MSI->getTemplateSpecializationKind(); 27520b57cec5SDimitry Andric 27530b57cec5SDimitry Andric return TSK_Undeclared; 27540b57cec5SDimitry Andric } 27550b57cec5SDimitry Andric 27560b57cec5SDimitry Andric TemplateSpecializationKind 27570b57cec5SDimitry Andric VarDecl::getTemplateSpecializationKindForInstantiation() const { 27580b57cec5SDimitry Andric if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 27590b57cec5SDimitry Andric return MSI->getTemplateSpecializationKind(); 27600b57cec5SDimitry Andric 27610b57cec5SDimitry Andric if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) 27620b57cec5SDimitry Andric return Spec->getSpecializationKind(); 27630b57cec5SDimitry Andric 27640b57cec5SDimitry Andric return TSK_Undeclared; 27650b57cec5SDimitry Andric } 27660b57cec5SDimitry Andric 27670b57cec5SDimitry Andric SourceLocation VarDecl::getPointOfInstantiation() const { 27680b57cec5SDimitry Andric if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) 27690b57cec5SDimitry Andric return Spec->getPointOfInstantiation(); 27700b57cec5SDimitry Andric 27710b57cec5SDimitry Andric if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 27720b57cec5SDimitry Andric return MSI->getPointOfInstantiation(); 27730b57cec5SDimitry Andric 27740b57cec5SDimitry Andric return SourceLocation(); 27750b57cec5SDimitry Andric } 27760b57cec5SDimitry Andric 27770b57cec5SDimitry Andric VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { 27780b57cec5SDimitry Andric return getASTContext().getTemplateOrSpecializationInfo(this) 27790b57cec5SDimitry Andric .dyn_cast<VarTemplateDecl *>(); 27800b57cec5SDimitry Andric } 27810b57cec5SDimitry Andric 27820b57cec5SDimitry Andric void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { 27830b57cec5SDimitry Andric getASTContext().setTemplateOrSpecializationInfo(this, Template); 27840b57cec5SDimitry Andric } 27850b57cec5SDimitry Andric 27860b57cec5SDimitry Andric bool VarDecl::isKnownToBeDefined() const { 27870b57cec5SDimitry Andric const auto &LangOpts = getASTContext().getLangOpts(); 27880b57cec5SDimitry Andric // In CUDA mode without relocatable device code, variables of form 'extern 27890b57cec5SDimitry Andric // __shared__ Foo foo[]' are pointers to the base of the GPU core's shared 27900b57cec5SDimitry Andric // memory pool. These are never undefined variables, even if they appear 27910b57cec5SDimitry Andric // inside of an anon namespace or static function. 27920b57cec5SDimitry Andric // 27930b57cec5SDimitry Andric // With CUDA relocatable device code enabled, these variables don't get 27940b57cec5SDimitry Andric // special handling; they're treated like regular extern variables. 27950b57cec5SDimitry Andric if (LangOpts.CUDA && !LangOpts.GPURelocatableDeviceCode && 27960b57cec5SDimitry Andric hasExternalStorage() && hasAttr<CUDASharedAttr>() && 27970b57cec5SDimitry Andric isa<IncompleteArrayType>(getType())) 27980b57cec5SDimitry Andric return true; 27990b57cec5SDimitry Andric 28000b57cec5SDimitry Andric return hasDefinition(); 28010b57cec5SDimitry Andric } 28020b57cec5SDimitry Andric 28030b57cec5SDimitry Andric bool VarDecl::isNoDestroy(const ASTContext &Ctx) const { 28040b57cec5SDimitry Andric return hasGlobalStorage() && (hasAttr<NoDestroyAttr>() || 28050b57cec5SDimitry Andric (!Ctx.getLangOpts().RegisterStaticDestructors && 28060b57cec5SDimitry Andric !hasAttr<AlwaysDestroyAttr>())); 28070b57cec5SDimitry Andric } 28080b57cec5SDimitry Andric 2809a7dea167SDimitry Andric QualType::DestructionKind 2810a7dea167SDimitry Andric VarDecl::needsDestruction(const ASTContext &Ctx) const { 2811e8d8bef9SDimitry Andric if (EvaluatedStmt *Eval = getEvaluatedStmt()) 2812a7dea167SDimitry Andric if (Eval->HasConstantDestruction) 2813a7dea167SDimitry Andric return QualType::DK_none; 2814a7dea167SDimitry Andric 2815a7dea167SDimitry Andric if (isNoDestroy(Ctx)) 2816a7dea167SDimitry Andric return QualType::DK_none; 2817a7dea167SDimitry Andric 2818a7dea167SDimitry Andric return getType().isDestructedType(); 2819a7dea167SDimitry Andric } 2820a7dea167SDimitry Andric 282181ad6265SDimitry Andric bool VarDecl::hasFlexibleArrayInit(const ASTContext &Ctx) const { 282281ad6265SDimitry Andric assert(hasInit() && "Expect initializer to check for flexible array init"); 282381ad6265SDimitry Andric auto *Ty = getType()->getAs<RecordType>(); 282481ad6265SDimitry Andric if (!Ty || !Ty->getDecl()->hasFlexibleArrayMember()) 282581ad6265SDimitry Andric return false; 282681ad6265SDimitry Andric auto *List = dyn_cast<InitListExpr>(getInit()->IgnoreParens()); 282781ad6265SDimitry Andric if (!List) 282881ad6265SDimitry Andric return false; 282981ad6265SDimitry Andric const Expr *FlexibleInit = List->getInit(List->getNumInits() - 1); 283081ad6265SDimitry Andric auto InitTy = Ctx.getAsConstantArrayType(FlexibleInit->getType()); 283181ad6265SDimitry Andric if (!InitTy) 283281ad6265SDimitry Andric return false; 283381ad6265SDimitry Andric return InitTy->getSize() != 0; 283481ad6265SDimitry Andric } 283581ad6265SDimitry Andric 283681ad6265SDimitry Andric CharUnits VarDecl::getFlexibleArrayInitChars(const ASTContext &Ctx) const { 283781ad6265SDimitry Andric assert(hasInit() && "Expect initializer to check for flexible array init"); 283881ad6265SDimitry Andric auto *Ty = getType()->getAs<RecordType>(); 283981ad6265SDimitry Andric if (!Ty || !Ty->getDecl()->hasFlexibleArrayMember()) 284081ad6265SDimitry Andric return CharUnits::Zero(); 284181ad6265SDimitry Andric auto *List = dyn_cast<InitListExpr>(getInit()->IgnoreParens()); 28421db9f3b2SDimitry Andric if (!List || List->getNumInits() == 0) 284381ad6265SDimitry Andric return CharUnits::Zero(); 284481ad6265SDimitry Andric const Expr *FlexibleInit = List->getInit(List->getNumInits() - 1); 284581ad6265SDimitry Andric auto InitTy = Ctx.getAsConstantArrayType(FlexibleInit->getType()); 284681ad6265SDimitry Andric if (!InitTy) 284781ad6265SDimitry Andric return CharUnits::Zero(); 284881ad6265SDimitry Andric CharUnits FlexibleArraySize = Ctx.getTypeSizeInChars(InitTy); 284981ad6265SDimitry Andric const ASTRecordLayout &RL = Ctx.getASTRecordLayout(Ty->getDecl()); 285081ad6265SDimitry Andric CharUnits FlexibleArrayOffset = 285181ad6265SDimitry Andric Ctx.toCharUnitsFromBits(RL.getFieldOffset(RL.getFieldCount() - 1)); 285281ad6265SDimitry Andric if (FlexibleArrayOffset + FlexibleArraySize < RL.getSize()) 285381ad6265SDimitry Andric return CharUnits::Zero(); 285481ad6265SDimitry Andric return FlexibleArrayOffset + FlexibleArraySize - RL.getSize(); 285581ad6265SDimitry Andric } 285681ad6265SDimitry Andric 28570b57cec5SDimitry Andric MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { 28580b57cec5SDimitry Andric if (isStaticDataMember()) 28590b57cec5SDimitry Andric // FIXME: Remove ? 28600b57cec5SDimitry Andric // return getASTContext().getInstantiatedFromStaticDataMember(this); 28610b57cec5SDimitry Andric return getASTContext().getTemplateOrSpecializationInfo(this) 28620b57cec5SDimitry Andric .dyn_cast<MemberSpecializationInfo *>(); 28630b57cec5SDimitry Andric return nullptr; 28640b57cec5SDimitry Andric } 28650b57cec5SDimitry Andric 28660b57cec5SDimitry Andric void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 28670b57cec5SDimitry Andric SourceLocation PointOfInstantiation) { 28680b57cec5SDimitry Andric assert((isa<VarTemplateSpecializationDecl>(this) || 28690b57cec5SDimitry Andric getMemberSpecializationInfo()) && 28700b57cec5SDimitry Andric "not a variable or static data member template specialization"); 28710b57cec5SDimitry Andric 28720b57cec5SDimitry Andric if (VarTemplateSpecializationDecl *Spec = 28730b57cec5SDimitry Andric dyn_cast<VarTemplateSpecializationDecl>(this)) { 28740b57cec5SDimitry Andric Spec->setSpecializationKind(TSK); 28750b57cec5SDimitry Andric if (TSK != TSK_ExplicitSpecialization && 28760b57cec5SDimitry Andric PointOfInstantiation.isValid() && 28770b57cec5SDimitry Andric Spec->getPointOfInstantiation().isInvalid()) { 28780b57cec5SDimitry Andric Spec->setPointOfInstantiation(PointOfInstantiation); 28790b57cec5SDimitry Andric if (ASTMutationListener *L = getASTContext().getASTMutationListener()) 28800b57cec5SDimitry Andric L->InstantiationRequested(this); 28810b57cec5SDimitry Andric } 28820b57cec5SDimitry Andric } else if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { 28830b57cec5SDimitry Andric MSI->setTemplateSpecializationKind(TSK); 28840b57cec5SDimitry Andric if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && 28850b57cec5SDimitry Andric MSI->getPointOfInstantiation().isInvalid()) { 28860b57cec5SDimitry Andric MSI->setPointOfInstantiation(PointOfInstantiation); 28870b57cec5SDimitry Andric if (ASTMutationListener *L = getASTContext().getASTMutationListener()) 28880b57cec5SDimitry Andric L->InstantiationRequested(this); 28890b57cec5SDimitry Andric } 28900b57cec5SDimitry Andric } 28910b57cec5SDimitry Andric } 28920b57cec5SDimitry Andric 28930b57cec5SDimitry Andric void 28940b57cec5SDimitry Andric VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, 28950b57cec5SDimitry Andric TemplateSpecializationKind TSK) { 28960b57cec5SDimitry Andric assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && 28970b57cec5SDimitry Andric "Previous template or instantiation?"); 28980b57cec5SDimitry Andric getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK); 28990b57cec5SDimitry Andric } 29000b57cec5SDimitry Andric 29010b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 29020b57cec5SDimitry Andric // ParmVarDecl Implementation 29030b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 29040b57cec5SDimitry Andric 29050b57cec5SDimitry Andric ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, 29060b57cec5SDimitry Andric SourceLocation StartLoc, 29070b57cec5SDimitry Andric SourceLocation IdLoc, IdentifierInfo *Id, 29080b57cec5SDimitry Andric QualType T, TypeSourceInfo *TInfo, 29090b57cec5SDimitry Andric StorageClass S, Expr *DefArg) { 29100b57cec5SDimitry Andric return new (C, DC) ParmVarDecl(ParmVar, C, DC, StartLoc, IdLoc, Id, T, TInfo, 29110b57cec5SDimitry Andric S, DefArg); 29120b57cec5SDimitry Andric } 29130b57cec5SDimitry Andric 29140b57cec5SDimitry Andric QualType ParmVarDecl::getOriginalType() const { 29150b57cec5SDimitry Andric TypeSourceInfo *TSI = getTypeSourceInfo(); 29160b57cec5SDimitry Andric QualType T = TSI ? TSI->getType() : getType(); 29170b57cec5SDimitry Andric if (const auto *DT = dyn_cast<DecayedType>(T)) 29180b57cec5SDimitry Andric return DT->getOriginalType(); 29190b57cec5SDimitry Andric return T; 29200b57cec5SDimitry Andric } 29210b57cec5SDimitry Andric 29220b57cec5SDimitry Andric ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 29230b57cec5SDimitry Andric return new (C, ID) 29240b57cec5SDimitry Andric ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), 29250b57cec5SDimitry Andric nullptr, QualType(), nullptr, SC_None, nullptr); 29260b57cec5SDimitry Andric } 29270b57cec5SDimitry Andric 29280b57cec5SDimitry Andric SourceRange ParmVarDecl::getSourceRange() const { 29290b57cec5SDimitry Andric if (!hasInheritedDefaultArg()) { 29300b57cec5SDimitry Andric SourceRange ArgRange = getDefaultArgRange(); 29310b57cec5SDimitry Andric if (ArgRange.isValid()) 29320b57cec5SDimitry Andric return SourceRange(getOuterLocStart(), ArgRange.getEnd()); 29330b57cec5SDimitry Andric } 29340b57cec5SDimitry Andric 29350b57cec5SDimitry Andric // DeclaratorDecl considers the range of postfix types as overlapping with the 29360b57cec5SDimitry Andric // declaration name, but this is not the case with parameters in ObjC methods. 29370b57cec5SDimitry Andric if (isa<ObjCMethodDecl>(getDeclContext())) 29380b57cec5SDimitry Andric return SourceRange(DeclaratorDecl::getBeginLoc(), getLocation()); 29390b57cec5SDimitry Andric 29400b57cec5SDimitry Andric return DeclaratorDecl::getSourceRange(); 29410b57cec5SDimitry Andric } 29420b57cec5SDimitry Andric 2943e8d8bef9SDimitry Andric bool ParmVarDecl::isDestroyedInCallee() const { 2944349cc55cSDimitry Andric // ns_consumed only affects code generation in ARC 2945e8d8bef9SDimitry Andric if (hasAttr<NSConsumedAttr>()) 2946349cc55cSDimitry Andric return getASTContext().getLangOpts().ObjCAutoRefCount; 2947e8d8bef9SDimitry Andric 2948349cc55cSDimitry Andric // FIXME: isParamDestroyedInCallee() should probably imply 2949349cc55cSDimitry Andric // isDestructedType() 2950cb14a3feSDimitry Andric const auto *RT = getType()->getAs<RecordType>(); 2951349cc55cSDimitry Andric if (RT && RT->getDecl()->isParamDestroyedInCallee() && 2952349cc55cSDimitry Andric getType().isDestructedType()) 2953e8d8bef9SDimitry Andric return true; 2954e8d8bef9SDimitry Andric 2955e8d8bef9SDimitry Andric return false; 2956e8d8bef9SDimitry Andric } 2957e8d8bef9SDimitry Andric 29580b57cec5SDimitry Andric Expr *ParmVarDecl::getDefaultArg() { 29590b57cec5SDimitry Andric assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); 29600b57cec5SDimitry Andric assert(!hasUninstantiatedDefaultArg() && 29610b57cec5SDimitry Andric "Default argument is not yet instantiated!"); 29620b57cec5SDimitry Andric 29630b57cec5SDimitry Andric Expr *Arg = getInit(); 29645f757f3fSDimitry Andric if (auto *E = dyn_cast_if_present<FullExpr>(Arg)) 29650b57cec5SDimitry Andric return E->getSubExpr(); 29660b57cec5SDimitry Andric 29670b57cec5SDimitry Andric return Arg; 29680b57cec5SDimitry Andric } 29690b57cec5SDimitry Andric 29700b57cec5SDimitry Andric void ParmVarDecl::setDefaultArg(Expr *defarg) { 29710b57cec5SDimitry Andric ParmVarDeclBits.DefaultArgKind = DAK_Normal; 29720b57cec5SDimitry Andric Init = defarg; 29730b57cec5SDimitry Andric } 29740b57cec5SDimitry Andric 29750b57cec5SDimitry Andric SourceRange ParmVarDecl::getDefaultArgRange() const { 29760b57cec5SDimitry Andric switch (ParmVarDeclBits.DefaultArgKind) { 29770b57cec5SDimitry Andric case DAK_None: 29780b57cec5SDimitry Andric case DAK_Unparsed: 29790b57cec5SDimitry Andric // Nothing we can do here. 29800b57cec5SDimitry Andric return SourceRange(); 29810b57cec5SDimitry Andric 29820b57cec5SDimitry Andric case DAK_Uninstantiated: 29830b57cec5SDimitry Andric return getUninstantiatedDefaultArg()->getSourceRange(); 29840b57cec5SDimitry Andric 29850b57cec5SDimitry Andric case DAK_Normal: 29860b57cec5SDimitry Andric if (const Expr *E = getInit()) 29870b57cec5SDimitry Andric return E->getSourceRange(); 29880b57cec5SDimitry Andric 29890b57cec5SDimitry Andric // Missing an actual expression, may be invalid. 29900b57cec5SDimitry Andric return SourceRange(); 29910b57cec5SDimitry Andric } 29920b57cec5SDimitry Andric llvm_unreachable("Invalid default argument kind."); 29930b57cec5SDimitry Andric } 29940b57cec5SDimitry Andric 29950b57cec5SDimitry Andric void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) { 29960b57cec5SDimitry Andric ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated; 29970b57cec5SDimitry Andric Init = arg; 29980b57cec5SDimitry Andric } 29990b57cec5SDimitry Andric 30000b57cec5SDimitry Andric Expr *ParmVarDecl::getUninstantiatedDefaultArg() { 30010b57cec5SDimitry Andric assert(hasUninstantiatedDefaultArg() && 30020b57cec5SDimitry Andric "Wrong kind of initialization expression!"); 30035f757f3fSDimitry Andric return cast_if_present<Expr>(Init.get<Stmt *>()); 30040b57cec5SDimitry Andric } 30050b57cec5SDimitry Andric 30060b57cec5SDimitry Andric bool ParmVarDecl::hasDefaultArg() const { 30070b57cec5SDimitry Andric // FIXME: We should just return false for DAK_None here once callers are 30080b57cec5SDimitry Andric // prepared for the case that we encountered an invalid default argument and 30090b57cec5SDimitry Andric // were unable to even build an invalid expression. 30100b57cec5SDimitry Andric return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() || 30110b57cec5SDimitry Andric !Init.isNull(); 30120b57cec5SDimitry Andric } 30130b57cec5SDimitry Andric 30140b57cec5SDimitry Andric void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { 30150b57cec5SDimitry Andric getASTContext().setParameterIndex(this, parameterIndex); 30160b57cec5SDimitry Andric ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; 30170b57cec5SDimitry Andric } 30180b57cec5SDimitry Andric 30190b57cec5SDimitry Andric unsigned ParmVarDecl::getParameterIndexLarge() const { 30200b57cec5SDimitry Andric return getASTContext().getParameterIndex(this); 30210b57cec5SDimitry Andric } 30220b57cec5SDimitry Andric 30230b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 30240b57cec5SDimitry Andric // FunctionDecl Implementation 30250b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 30260b57cec5SDimitry Andric 30270b57cec5SDimitry Andric FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, 30280b57cec5SDimitry Andric SourceLocation StartLoc, 30290b57cec5SDimitry Andric const DeclarationNameInfo &NameInfo, QualType T, 30300b57cec5SDimitry Andric TypeSourceInfo *TInfo, StorageClass S, 3031349cc55cSDimitry Andric bool UsesFPIntrin, bool isInlineSpecified, 3032480093f4SDimitry Andric ConstexprSpecKind ConstexprKind, 3033480093f4SDimitry Andric Expr *TrailingRequiresClause) 30340b57cec5SDimitry Andric : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo, 30350b57cec5SDimitry Andric StartLoc), 3036480093f4SDimitry Andric DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0), 30370b57cec5SDimitry Andric EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) { 30380b57cec5SDimitry Andric assert(T.isNull() || T->isFunctionType()); 30390b57cec5SDimitry Andric FunctionDeclBits.SClass = S; 30400b57cec5SDimitry Andric FunctionDeclBits.IsInline = isInlineSpecified; 30410b57cec5SDimitry Andric FunctionDeclBits.IsInlineSpecified = isInlineSpecified; 30420b57cec5SDimitry Andric FunctionDeclBits.IsVirtualAsWritten = false; 3043*7a6dacacSDimitry Andric FunctionDeclBits.IsPureVirtual = false; 30440b57cec5SDimitry Andric FunctionDeclBits.HasInheritedPrototype = false; 30450b57cec5SDimitry Andric FunctionDeclBits.HasWrittenPrototype = true; 30460b57cec5SDimitry Andric FunctionDeclBits.IsDeleted = false; 30470b57cec5SDimitry Andric FunctionDeclBits.IsTrivial = false; 30480b57cec5SDimitry Andric FunctionDeclBits.IsTrivialForCall = false; 30490b57cec5SDimitry Andric FunctionDeclBits.IsDefaulted = false; 30500b57cec5SDimitry Andric FunctionDeclBits.IsExplicitlyDefaulted = false; 3051480093f4SDimitry Andric FunctionDeclBits.HasDefaultedFunctionInfo = false; 305281ad6265SDimitry Andric FunctionDeclBits.IsIneligibleOrNotSelected = false; 30530b57cec5SDimitry Andric FunctionDeclBits.HasImplicitReturnZero = false; 30540b57cec5SDimitry Andric FunctionDeclBits.IsLateTemplateParsed = false; 3055e8d8bef9SDimitry Andric FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(ConstexprKind); 305606c3fb27SDimitry Andric FunctionDeclBits.BodyContainsImmediateEscalatingExpression = false; 30570b57cec5SDimitry Andric FunctionDeclBits.InstantiationIsPending = false; 30580b57cec5SDimitry Andric FunctionDeclBits.UsesSEHTry = false; 3059349cc55cSDimitry Andric FunctionDeclBits.UsesFPIntrin = UsesFPIntrin; 30600b57cec5SDimitry Andric FunctionDeclBits.HasSkippedBody = false; 30610b57cec5SDimitry Andric FunctionDeclBits.WillHaveBody = false; 30620b57cec5SDimitry Andric FunctionDeclBits.IsMultiVersion = false; 306306c3fb27SDimitry Andric FunctionDeclBits.DeductionCandidateKind = 306406c3fb27SDimitry Andric static_cast<unsigned char>(DeductionCandidate::Normal); 30650b57cec5SDimitry Andric FunctionDeclBits.HasODRHash = false; 3066bdd1243dSDimitry Andric FunctionDeclBits.FriendConstraintRefersToEnclosingTemplate = false; 3067480093f4SDimitry Andric if (TrailingRequiresClause) 3068480093f4SDimitry Andric setTrailingRequiresClause(TrailingRequiresClause); 30690b57cec5SDimitry Andric } 30700b57cec5SDimitry Andric 30710b57cec5SDimitry Andric void FunctionDecl::getNameForDiagnostic( 30720b57cec5SDimitry Andric raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { 30730b57cec5SDimitry Andric NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); 30740b57cec5SDimitry Andric const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); 30750b57cec5SDimitry Andric if (TemplateArgs) 30760b57cec5SDimitry Andric printTemplateArgumentList(OS, TemplateArgs->asArray(), Policy); 30770b57cec5SDimitry Andric } 30780b57cec5SDimitry Andric 30790b57cec5SDimitry Andric bool FunctionDecl::isVariadic() const { 30800b57cec5SDimitry Andric if (const auto *FT = getType()->getAs<FunctionProtoType>()) 30810b57cec5SDimitry Andric return FT->isVariadic(); 30820b57cec5SDimitry Andric return false; 30830b57cec5SDimitry Andric } 30840b57cec5SDimitry Andric 3085480093f4SDimitry Andric FunctionDecl::DefaultedFunctionInfo * 3086480093f4SDimitry Andric FunctionDecl::DefaultedFunctionInfo::Create(ASTContext &Context, 3087480093f4SDimitry Andric ArrayRef<DeclAccessPair> Lookups) { 3088480093f4SDimitry Andric DefaultedFunctionInfo *Info = new (Context.Allocate( 3089480093f4SDimitry Andric totalSizeToAlloc<DeclAccessPair>(Lookups.size()), 3090480093f4SDimitry Andric std::max(alignof(DefaultedFunctionInfo), alignof(DeclAccessPair)))) 3091480093f4SDimitry Andric DefaultedFunctionInfo; 3092480093f4SDimitry Andric Info->NumLookups = Lookups.size(); 3093480093f4SDimitry Andric std::uninitialized_copy(Lookups.begin(), Lookups.end(), 3094480093f4SDimitry Andric Info->getTrailingObjects<DeclAccessPair>()); 3095480093f4SDimitry Andric return Info; 3096480093f4SDimitry Andric } 3097480093f4SDimitry Andric 3098480093f4SDimitry Andric void FunctionDecl::setDefaultedFunctionInfo(DefaultedFunctionInfo *Info) { 3099480093f4SDimitry Andric assert(!FunctionDeclBits.HasDefaultedFunctionInfo && "already have this"); 3100480093f4SDimitry Andric assert(!Body && "can't replace function body with defaulted function info"); 3101480093f4SDimitry Andric 3102480093f4SDimitry Andric FunctionDeclBits.HasDefaultedFunctionInfo = true; 3103480093f4SDimitry Andric DefaultedInfo = Info; 3104480093f4SDimitry Andric } 3105480093f4SDimitry Andric 3106480093f4SDimitry Andric FunctionDecl::DefaultedFunctionInfo * 3107480093f4SDimitry Andric FunctionDecl::getDefaultedFunctionInfo() const { 3108480093f4SDimitry Andric return FunctionDeclBits.HasDefaultedFunctionInfo ? DefaultedInfo : nullptr; 3109480093f4SDimitry Andric } 3110480093f4SDimitry Andric 31110b57cec5SDimitry Andric bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { 3112cb14a3feSDimitry Andric for (const auto *I : redecls()) { 31130b57cec5SDimitry Andric if (I->doesThisDeclarationHaveABody()) { 31140b57cec5SDimitry Andric Definition = I; 31150b57cec5SDimitry Andric return true; 31160b57cec5SDimitry Andric } 31170b57cec5SDimitry Andric } 31180b57cec5SDimitry Andric 31190b57cec5SDimitry Andric return false; 31200b57cec5SDimitry Andric } 31210b57cec5SDimitry Andric 3122480093f4SDimitry Andric bool FunctionDecl::hasTrivialBody() const { 3123cb14a3feSDimitry Andric const Stmt *S = getBody(); 31240b57cec5SDimitry Andric if (!S) { 31250b57cec5SDimitry Andric // Since we don't have a body for this function, we don't know if it's 31260b57cec5SDimitry Andric // trivial or not. 31270b57cec5SDimitry Andric return false; 31280b57cec5SDimitry Andric } 31290b57cec5SDimitry Andric 31300b57cec5SDimitry Andric if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) 31310b57cec5SDimitry Andric return true; 31320b57cec5SDimitry Andric return false; 31330b57cec5SDimitry Andric } 31340b57cec5SDimitry Andric 3135e8d8bef9SDimitry Andric bool FunctionDecl::isThisDeclarationInstantiatedFromAFriendDefinition() const { 3136e8d8bef9SDimitry Andric if (!getFriendObjectKind()) 3137e8d8bef9SDimitry Andric return false; 3138e8d8bef9SDimitry Andric 3139e8d8bef9SDimitry Andric // Check for a friend function instantiated from a friend function 3140e8d8bef9SDimitry Andric // definition in a templated class. 3141e8d8bef9SDimitry Andric if (const FunctionDecl *InstantiatedFrom = 3142e8d8bef9SDimitry Andric getInstantiatedFromMemberFunction()) 3143e8d8bef9SDimitry Andric return InstantiatedFrom->getFriendObjectKind() && 3144e8d8bef9SDimitry Andric InstantiatedFrom->isThisDeclarationADefinition(); 3145e8d8bef9SDimitry Andric 3146e8d8bef9SDimitry Andric // Check for a friend function template instantiated from a friend 3147e8d8bef9SDimitry Andric // function template definition in a templated class. 3148e8d8bef9SDimitry Andric if (const FunctionTemplateDecl *Template = getDescribedFunctionTemplate()) { 3149e8d8bef9SDimitry Andric if (const FunctionTemplateDecl *InstantiatedFrom = 3150e8d8bef9SDimitry Andric Template->getInstantiatedFromMemberTemplate()) 3151e8d8bef9SDimitry Andric return InstantiatedFrom->getFriendObjectKind() && 3152e8d8bef9SDimitry Andric InstantiatedFrom->isThisDeclarationADefinition(); 3153e8d8bef9SDimitry Andric } 3154e8d8bef9SDimitry Andric 3155e8d8bef9SDimitry Andric return false; 3156e8d8bef9SDimitry Andric } 3157e8d8bef9SDimitry Andric 3158e8d8bef9SDimitry Andric bool FunctionDecl::isDefined(const FunctionDecl *&Definition, 3159e8d8bef9SDimitry Andric bool CheckForPendingFriendDefinition) const { 3160e8d8bef9SDimitry Andric for (const FunctionDecl *FD : redecls()) { 3161e8d8bef9SDimitry Andric if (FD->isThisDeclarationADefinition()) { 3162e8d8bef9SDimitry Andric Definition = FD; 3163e8d8bef9SDimitry Andric return true; 3164e8d8bef9SDimitry Andric } 3165e8d8bef9SDimitry Andric 3166e8d8bef9SDimitry Andric // If this is a friend function defined in a class template, it does not 3167e8d8bef9SDimitry Andric // have a body until it is used, nevertheless it is a definition, see 3168e8d8bef9SDimitry Andric // [temp.inst]p2: 3169e8d8bef9SDimitry Andric // 3170e8d8bef9SDimitry Andric // ... for the purpose of determining whether an instantiated redeclaration 3171e8d8bef9SDimitry Andric // is valid according to [basic.def.odr] and [class.mem], a declaration that 3172e8d8bef9SDimitry Andric // corresponds to a definition in the template is considered to be a 3173e8d8bef9SDimitry Andric // definition. 3174e8d8bef9SDimitry Andric // 3175e8d8bef9SDimitry Andric // The following code must produce redefinition error: 3176e8d8bef9SDimitry Andric // 3177e8d8bef9SDimitry Andric // template<typename T> struct C20 { friend void func_20() {} }; 3178e8d8bef9SDimitry Andric // C20<int> c20i; 3179e8d8bef9SDimitry Andric // void func_20() {} 3180e8d8bef9SDimitry Andric // 3181e8d8bef9SDimitry Andric if (CheckForPendingFriendDefinition && 3182e8d8bef9SDimitry Andric FD->isThisDeclarationInstantiatedFromAFriendDefinition()) { 3183e8d8bef9SDimitry Andric Definition = FD; 31840b57cec5SDimitry Andric return true; 31850b57cec5SDimitry Andric } 31860b57cec5SDimitry Andric } 31870b57cec5SDimitry Andric 31880b57cec5SDimitry Andric return false; 31890b57cec5SDimitry Andric } 31900b57cec5SDimitry Andric 31910b57cec5SDimitry Andric Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { 31920b57cec5SDimitry Andric if (!hasBody(Definition)) 31930b57cec5SDimitry Andric return nullptr; 31940b57cec5SDimitry Andric 3195480093f4SDimitry Andric assert(!Definition->FunctionDeclBits.HasDefaultedFunctionInfo && 3196480093f4SDimitry Andric "definition should not have a body"); 31970b57cec5SDimitry Andric if (Definition->Body) 31980b57cec5SDimitry Andric return Definition->Body.get(getASTContext().getExternalSource()); 31990b57cec5SDimitry Andric 32000b57cec5SDimitry Andric return nullptr; 32010b57cec5SDimitry Andric } 32020b57cec5SDimitry Andric 32030b57cec5SDimitry Andric void FunctionDecl::setBody(Stmt *B) { 3204480093f4SDimitry Andric FunctionDeclBits.HasDefaultedFunctionInfo = false; 3205480093f4SDimitry Andric Body = LazyDeclStmtPtr(B); 32060b57cec5SDimitry Andric if (B) 32070b57cec5SDimitry Andric EndRangeLoc = B->getEndLoc(); 32080b57cec5SDimitry Andric } 32090b57cec5SDimitry Andric 3210*7a6dacacSDimitry Andric void FunctionDecl::setIsPureVirtual(bool P) { 3211*7a6dacacSDimitry Andric FunctionDeclBits.IsPureVirtual = P; 32120b57cec5SDimitry Andric if (P) 32130b57cec5SDimitry Andric if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) 32140b57cec5SDimitry Andric Parent->markedVirtualFunctionPure(); 32150b57cec5SDimitry Andric } 32160b57cec5SDimitry Andric 32170b57cec5SDimitry Andric template<std::size_t Len> 32180b57cec5SDimitry Andric static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { 3219cb14a3feSDimitry Andric const IdentifierInfo *II = ND->getIdentifier(); 32200b57cec5SDimitry Andric return II && II->isStr(Str); 32210b57cec5SDimitry Andric } 32220b57cec5SDimitry Andric 322306c3fb27SDimitry Andric bool FunctionDecl::isImmediateEscalating() const { 322406c3fb27SDimitry Andric // C++23 [expr.const]/p17 322506c3fb27SDimitry Andric // An immediate-escalating function is 322606c3fb27SDimitry Andric // - the call operator of a lambda that is not declared with the consteval 322706c3fb27SDimitry Andric // specifier, 322806c3fb27SDimitry Andric if (isLambdaCallOperator(this) && !isConsteval()) 322906c3fb27SDimitry Andric return true; 323006c3fb27SDimitry Andric // - a defaulted special member function that is not declared with the 323106c3fb27SDimitry Andric // consteval specifier, 323206c3fb27SDimitry Andric if (isDefaulted() && !isConsteval()) 323306c3fb27SDimitry Andric return true; 323406c3fb27SDimitry Andric // - a function that results from the instantiation of a templated entity 323506c3fb27SDimitry Andric // defined with the constexpr specifier. 323606c3fb27SDimitry Andric TemplatedKind TK = getTemplatedKind(); 323706c3fb27SDimitry Andric if (TK != TK_NonTemplate && TK != TK_DependentNonTemplate && 323806c3fb27SDimitry Andric isConstexprSpecified()) 323906c3fb27SDimitry Andric return true; 324006c3fb27SDimitry Andric return false; 324106c3fb27SDimitry Andric } 324206c3fb27SDimitry Andric 324306c3fb27SDimitry Andric bool FunctionDecl::isImmediateFunction() const { 324406c3fb27SDimitry Andric // C++23 [expr.const]/p18 324506c3fb27SDimitry Andric // An immediate function is a function or constructor that is 324606c3fb27SDimitry Andric // - declared with the consteval specifier 324706c3fb27SDimitry Andric if (isConsteval()) 324806c3fb27SDimitry Andric return true; 324906c3fb27SDimitry Andric // - an immediate-escalating function F whose function body contains an 325006c3fb27SDimitry Andric // immediate-escalating expression 325106c3fb27SDimitry Andric if (isImmediateEscalating() && BodyContainsImmediateEscalatingExpressions()) 325206c3fb27SDimitry Andric return true; 325306c3fb27SDimitry Andric 325406c3fb27SDimitry Andric if (const auto *MD = dyn_cast<CXXMethodDecl>(this); 325506c3fb27SDimitry Andric MD && MD->isLambdaStaticInvoker()) 325606c3fb27SDimitry Andric return MD->getParent()->getLambdaCallOperator()->isImmediateFunction(); 325706c3fb27SDimitry Andric 325806c3fb27SDimitry Andric return false; 325906c3fb27SDimitry Andric } 326006c3fb27SDimitry Andric 32610b57cec5SDimitry Andric bool FunctionDecl::isMain() const { 32620b57cec5SDimitry Andric const TranslationUnitDecl *tunit = 32630b57cec5SDimitry Andric dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); 32640b57cec5SDimitry Andric return tunit && 32650b57cec5SDimitry Andric !tunit->getASTContext().getLangOpts().Freestanding && 32660b57cec5SDimitry Andric isNamed(this, "main"); 32670b57cec5SDimitry Andric } 32680b57cec5SDimitry Andric 32690b57cec5SDimitry Andric bool FunctionDecl::isMSVCRTEntryPoint() const { 32700b57cec5SDimitry Andric const TranslationUnitDecl *TUnit = 32710b57cec5SDimitry Andric dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); 32720b57cec5SDimitry Andric if (!TUnit) 32730b57cec5SDimitry Andric return false; 32740b57cec5SDimitry Andric 32750b57cec5SDimitry Andric // Even though we aren't really targeting MSVCRT if we are freestanding, 32760b57cec5SDimitry Andric // semantic analysis for these functions remains the same. 32770b57cec5SDimitry Andric 32780b57cec5SDimitry Andric // MSVCRT entry points only exist on MSVCRT targets. 32790b57cec5SDimitry Andric if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT()) 32800b57cec5SDimitry Andric return false; 32810b57cec5SDimitry Andric 32820b57cec5SDimitry Andric // Nameless functions like constructors cannot be entry points. 32830b57cec5SDimitry Andric if (!getIdentifier()) 32840b57cec5SDimitry Andric return false; 32850b57cec5SDimitry Andric 32860b57cec5SDimitry Andric return llvm::StringSwitch<bool>(getName()) 32870b57cec5SDimitry Andric .Cases("main", // an ANSI console app 32880b57cec5SDimitry Andric "wmain", // a Unicode console App 32890b57cec5SDimitry Andric "WinMain", // an ANSI GUI app 32900b57cec5SDimitry Andric "wWinMain", // a Unicode GUI app 32910b57cec5SDimitry Andric "DllMain", // a DLL 32920b57cec5SDimitry Andric true) 32930b57cec5SDimitry Andric .Default(false); 32940b57cec5SDimitry Andric } 32950b57cec5SDimitry Andric 32960b57cec5SDimitry Andric bool FunctionDecl::isReservedGlobalPlacementOperator() const { 3297bdd1243dSDimitry Andric if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) 3298bdd1243dSDimitry Andric return false; 3299bdd1243dSDimitry Andric if (getDeclName().getCXXOverloadedOperator() != OO_New && 3300bdd1243dSDimitry Andric getDeclName().getCXXOverloadedOperator() != OO_Delete && 3301bdd1243dSDimitry Andric getDeclName().getCXXOverloadedOperator() != OO_Array_New && 3302bdd1243dSDimitry Andric getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) 3303bdd1243dSDimitry Andric return false; 33040b57cec5SDimitry Andric 33050b57cec5SDimitry Andric if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) 33060b57cec5SDimitry Andric return false; 33070b57cec5SDimitry Andric 33080b57cec5SDimitry Andric const auto *proto = getType()->castAs<FunctionProtoType>(); 33090b57cec5SDimitry Andric if (proto->getNumParams() != 2 || proto->isVariadic()) 33100b57cec5SDimitry Andric return false; 33110b57cec5SDimitry Andric 3312cb14a3feSDimitry Andric const ASTContext &Context = 33130b57cec5SDimitry Andric cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) 33140b57cec5SDimitry Andric ->getASTContext(); 33150b57cec5SDimitry Andric 33160b57cec5SDimitry Andric // The result type and first argument type are constant across all 33170b57cec5SDimitry Andric // these operators. The second argument must be exactly void*. 33180b57cec5SDimitry Andric return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); 33190b57cec5SDimitry Andric } 33200b57cec5SDimitry Andric 33215ffd83dbSDimitry Andric bool FunctionDecl::isReplaceableGlobalAllocationFunction( 3322bdd1243dSDimitry Andric std::optional<unsigned> *AlignmentParam, bool *IsNothrow) const { 33230b57cec5SDimitry Andric if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) 33240b57cec5SDimitry Andric return false; 33250b57cec5SDimitry Andric if (getDeclName().getCXXOverloadedOperator() != OO_New && 33260b57cec5SDimitry Andric getDeclName().getCXXOverloadedOperator() != OO_Delete && 33270b57cec5SDimitry Andric getDeclName().getCXXOverloadedOperator() != OO_Array_New && 33280b57cec5SDimitry Andric getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) 33290b57cec5SDimitry Andric return false; 33300b57cec5SDimitry Andric 33310b57cec5SDimitry Andric if (isa<CXXRecordDecl>(getDeclContext())) 33320b57cec5SDimitry Andric return false; 33330b57cec5SDimitry Andric 33340b57cec5SDimitry Andric // This can only fail for an invalid 'operator new' declaration. 33350b57cec5SDimitry Andric if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) 33360b57cec5SDimitry Andric return false; 33370b57cec5SDimitry Andric 33380b57cec5SDimitry Andric const auto *FPT = getType()->castAs<FunctionProtoType>(); 333906c3fb27SDimitry Andric if (FPT->getNumParams() == 0 || FPT->getNumParams() > 4 || FPT->isVariadic()) 33400b57cec5SDimitry Andric return false; 33410b57cec5SDimitry Andric 33420b57cec5SDimitry Andric // If this is a single-parameter function, it must be a replaceable global 33430b57cec5SDimitry Andric // allocation or deallocation function. 33440b57cec5SDimitry Andric if (FPT->getNumParams() == 1) 33450b57cec5SDimitry Andric return true; 33460b57cec5SDimitry Andric 33470b57cec5SDimitry Andric unsigned Params = 1; 33480b57cec5SDimitry Andric QualType Ty = FPT->getParamType(Params); 3349cb14a3feSDimitry Andric const ASTContext &Ctx = getASTContext(); 33500b57cec5SDimitry Andric 33510b57cec5SDimitry Andric auto Consume = [&] { 33520b57cec5SDimitry Andric ++Params; 33530b57cec5SDimitry Andric Ty = Params < FPT->getNumParams() ? FPT->getParamType(Params) : QualType(); 33540b57cec5SDimitry Andric }; 33550b57cec5SDimitry Andric 33560b57cec5SDimitry Andric // In C++14, the next parameter can be a 'std::size_t' for sized delete. 33570b57cec5SDimitry Andric bool IsSizedDelete = false; 33580b57cec5SDimitry Andric if (Ctx.getLangOpts().SizedDeallocation && 33590b57cec5SDimitry Andric (getDeclName().getCXXOverloadedOperator() == OO_Delete || 33600b57cec5SDimitry Andric getDeclName().getCXXOverloadedOperator() == OO_Array_Delete) && 33610b57cec5SDimitry Andric Ctx.hasSameType(Ty, Ctx.getSizeType())) { 33620b57cec5SDimitry Andric IsSizedDelete = true; 33630b57cec5SDimitry Andric Consume(); 33640b57cec5SDimitry Andric } 33650b57cec5SDimitry Andric 33660b57cec5SDimitry Andric // In C++17, the next parameter can be a 'std::align_val_t' for aligned 33670b57cec5SDimitry Andric // new/delete. 33680b57cec5SDimitry Andric if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) { 33690b57cec5SDimitry Andric Consume(); 33705ffd83dbSDimitry Andric if (AlignmentParam) 33715ffd83dbSDimitry Andric *AlignmentParam = Params; 33720b57cec5SDimitry Andric } 33730b57cec5SDimitry Andric 337406c3fb27SDimitry Andric // If this is not a sized delete, the next parameter can be a 337506c3fb27SDimitry Andric // 'const std::nothrow_t&'. 33760b57cec5SDimitry Andric if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) { 33770b57cec5SDimitry Andric Ty = Ty->getPointeeType(); 33780b57cec5SDimitry Andric if (Ty.getCVRQualifiers() != Qualifiers::Const) 33790b57cec5SDimitry Andric return false; 33805ffd83dbSDimitry Andric if (Ty->isNothrowT()) { 33815ffd83dbSDimitry Andric if (IsNothrow) 33825ffd83dbSDimitry Andric *IsNothrow = true; 33830b57cec5SDimitry Andric Consume(); 33840b57cec5SDimitry Andric } 33855ffd83dbSDimitry Andric } 33860b57cec5SDimitry Andric 338706c3fb27SDimitry Andric // Finally, recognize the not yet standard versions of new that take a 338806c3fb27SDimitry Andric // hot/cold allocation hint (__hot_cold_t). These are currently supported by 338906c3fb27SDimitry Andric // tcmalloc (see 339006c3fb27SDimitry Andric // https://github.com/google/tcmalloc/blob/220043886d4e2efff7a5702d5172cb8065253664/tcmalloc/malloc_extension.h#L53). 339106c3fb27SDimitry Andric if (!IsSizedDelete && !Ty.isNull() && Ty->isEnumeralType()) { 339206c3fb27SDimitry Andric QualType T = Ty; 339306c3fb27SDimitry Andric while (const auto *TD = T->getAs<TypedefType>()) 339406c3fb27SDimitry Andric T = TD->getDecl()->getUnderlyingType(); 3395cb14a3feSDimitry Andric const IdentifierInfo *II = 3396cb14a3feSDimitry Andric T->castAs<EnumType>()->getDecl()->getIdentifier(); 339706c3fb27SDimitry Andric if (II && II->isStr("__hot_cold_t")) 339806c3fb27SDimitry Andric Consume(); 339906c3fb27SDimitry Andric } 340006c3fb27SDimitry Andric 34010b57cec5SDimitry Andric return Params == FPT->getNumParams(); 34020b57cec5SDimitry Andric } 34030b57cec5SDimitry Andric 3404480093f4SDimitry Andric bool FunctionDecl::isInlineBuiltinDeclaration() const { 3405480093f4SDimitry Andric if (!getBuiltinID()) 3406480093f4SDimitry Andric return false; 3407480093f4SDimitry Andric 3408480093f4SDimitry Andric const FunctionDecl *Definition; 340906c3fb27SDimitry Andric if (!hasBody(Definition)) 341006c3fb27SDimitry Andric return false; 341106c3fb27SDimitry Andric 341206c3fb27SDimitry Andric if (!Definition->isInlineSpecified() || 341306c3fb27SDimitry Andric !Definition->hasAttr<AlwaysInlineAttr>()) 341406c3fb27SDimitry Andric return false; 341506c3fb27SDimitry Andric 341606c3fb27SDimitry Andric ASTContext &Context = getASTContext(); 341706c3fb27SDimitry Andric switch (Context.GetGVALinkageForFunction(Definition)) { 341806c3fb27SDimitry Andric case GVA_Internal: 341906c3fb27SDimitry Andric case GVA_DiscardableODR: 342006c3fb27SDimitry Andric case GVA_StrongODR: 342106c3fb27SDimitry Andric return false; 342206c3fb27SDimitry Andric case GVA_AvailableExternally: 342306c3fb27SDimitry Andric case GVA_StrongExternal: 342406c3fb27SDimitry Andric return true; 342506c3fb27SDimitry Andric } 342606c3fb27SDimitry Andric llvm_unreachable("Unknown GVALinkage"); 3427480093f4SDimitry Andric } 3428480093f4SDimitry Andric 34290b57cec5SDimitry Andric bool FunctionDecl::isDestroyingOperatorDelete() const { 34300b57cec5SDimitry Andric // C++ P0722: 34310b57cec5SDimitry Andric // Within a class C, a single object deallocation function with signature 34320b57cec5SDimitry Andric // (T, std::destroying_delete_t, <more params>) 34330b57cec5SDimitry Andric // is a destroying operator delete. 34340b57cec5SDimitry Andric if (!isa<CXXMethodDecl>(this) || getOverloadedOperator() != OO_Delete || 34350b57cec5SDimitry Andric getNumParams() < 2) 34360b57cec5SDimitry Andric return false; 34370b57cec5SDimitry Andric 34380b57cec5SDimitry Andric auto *RD = getParamDecl(1)->getType()->getAsCXXRecordDecl(); 34390b57cec5SDimitry Andric return RD && RD->isInStdNamespace() && RD->getIdentifier() && 34400b57cec5SDimitry Andric RD->getIdentifier()->isStr("destroying_delete_t"); 34410b57cec5SDimitry Andric } 34420b57cec5SDimitry Andric 34430b57cec5SDimitry Andric LanguageLinkage FunctionDecl::getLanguageLinkage() const { 34440b57cec5SDimitry Andric return getDeclLanguageLinkage(*this); 34450b57cec5SDimitry Andric } 34460b57cec5SDimitry Andric 34470b57cec5SDimitry Andric bool FunctionDecl::isExternC() const { 34480b57cec5SDimitry Andric return isDeclExternC(*this); 34490b57cec5SDimitry Andric } 34500b57cec5SDimitry Andric 34510b57cec5SDimitry Andric bool FunctionDecl::isInExternCContext() const { 34520b57cec5SDimitry Andric if (hasAttr<OpenCLKernelAttr>()) 34530b57cec5SDimitry Andric return true; 34540b57cec5SDimitry Andric return getLexicalDeclContext()->isExternCContext(); 34550b57cec5SDimitry Andric } 34560b57cec5SDimitry Andric 34570b57cec5SDimitry Andric bool FunctionDecl::isInExternCXXContext() const { 34580b57cec5SDimitry Andric return getLexicalDeclContext()->isExternCXXContext(); 34590b57cec5SDimitry Andric } 34600b57cec5SDimitry Andric 34610b57cec5SDimitry Andric bool FunctionDecl::isGlobal() const { 34620b57cec5SDimitry Andric if (const auto *Method = dyn_cast<CXXMethodDecl>(this)) 34630b57cec5SDimitry Andric return Method->isStatic(); 34640b57cec5SDimitry Andric 34650b57cec5SDimitry Andric if (getCanonicalDecl()->getStorageClass() == SC_Static) 34660b57cec5SDimitry Andric return false; 34670b57cec5SDimitry Andric 34680b57cec5SDimitry Andric for (const DeclContext *DC = getDeclContext(); 34690b57cec5SDimitry Andric DC->isNamespace(); 34700b57cec5SDimitry Andric DC = DC->getParent()) { 34710b57cec5SDimitry Andric if (const auto *Namespace = cast<NamespaceDecl>(DC)) { 34720b57cec5SDimitry Andric if (!Namespace->getDeclName()) 34730b57cec5SDimitry Andric return false; 34740b57cec5SDimitry Andric } 34750b57cec5SDimitry Andric } 34760b57cec5SDimitry Andric 34770b57cec5SDimitry Andric return true; 34780b57cec5SDimitry Andric } 34790b57cec5SDimitry Andric 34800b57cec5SDimitry Andric bool FunctionDecl::isNoReturn() const { 34810b57cec5SDimitry Andric if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || 34820b57cec5SDimitry Andric hasAttr<C11NoReturnAttr>()) 34830b57cec5SDimitry Andric return true; 34840b57cec5SDimitry Andric 34850b57cec5SDimitry Andric if (auto *FnTy = getType()->getAs<FunctionType>()) 34860b57cec5SDimitry Andric return FnTy->getNoReturnAttr(); 34870b57cec5SDimitry Andric 34880b57cec5SDimitry Andric return false; 34890b57cec5SDimitry Andric } 34900b57cec5SDimitry Andric 349106c3fb27SDimitry Andric bool FunctionDecl::isMemberLikeConstrainedFriend() const { 349206c3fb27SDimitry Andric // C++20 [temp.friend]p9: 349306c3fb27SDimitry Andric // A non-template friend declaration with a requires-clause [or] 349406c3fb27SDimitry Andric // a friend function template with a constraint that depends on a template 349506c3fb27SDimitry Andric // parameter from an enclosing template [...] does not declare the same 349606c3fb27SDimitry Andric // function or function template as a declaration in any other scope. 349706c3fb27SDimitry Andric 349806c3fb27SDimitry Andric // If this isn't a friend then it's not a member-like constrained friend. 349906c3fb27SDimitry Andric if (!getFriendObjectKind()) { 350006c3fb27SDimitry Andric return false; 350106c3fb27SDimitry Andric } 350206c3fb27SDimitry Andric 350306c3fb27SDimitry Andric if (!getDescribedFunctionTemplate()) { 350406c3fb27SDimitry Andric // If these friends don't have constraints, they aren't constrained, and 350506c3fb27SDimitry Andric // thus don't fall under temp.friend p9. Else the simple presence of a 350606c3fb27SDimitry Andric // constraint makes them unique. 350706c3fb27SDimitry Andric return getTrailingRequiresClause(); 350806c3fb27SDimitry Andric } 350906c3fb27SDimitry Andric 351006c3fb27SDimitry Andric return FriendConstraintRefersToEnclosingTemplate(); 351106c3fb27SDimitry Andric } 35120b57cec5SDimitry Andric 35130b57cec5SDimitry Andric MultiVersionKind FunctionDecl::getMultiVersionKind() const { 35140b57cec5SDimitry Andric if (hasAttr<TargetAttr>()) 35150b57cec5SDimitry Andric return MultiVersionKind::Target; 3516bdd1243dSDimitry Andric if (hasAttr<TargetVersionAttr>()) 3517bdd1243dSDimitry Andric return MultiVersionKind::TargetVersion; 35180b57cec5SDimitry Andric if (hasAttr<CPUDispatchAttr>()) 35190b57cec5SDimitry Andric return MultiVersionKind::CPUDispatch; 35200b57cec5SDimitry Andric if (hasAttr<CPUSpecificAttr>()) 35210b57cec5SDimitry Andric return MultiVersionKind::CPUSpecific; 35224824e7fdSDimitry Andric if (hasAttr<TargetClonesAttr>()) 35234824e7fdSDimitry Andric return MultiVersionKind::TargetClones; 35240b57cec5SDimitry Andric return MultiVersionKind::None; 35250b57cec5SDimitry Andric } 35260b57cec5SDimitry Andric 35270b57cec5SDimitry Andric bool FunctionDecl::isCPUDispatchMultiVersion() const { 35280b57cec5SDimitry Andric return isMultiVersion() && hasAttr<CPUDispatchAttr>(); 35290b57cec5SDimitry Andric } 35300b57cec5SDimitry Andric 35310b57cec5SDimitry Andric bool FunctionDecl::isCPUSpecificMultiVersion() const { 35320b57cec5SDimitry Andric return isMultiVersion() && hasAttr<CPUSpecificAttr>(); 35330b57cec5SDimitry Andric } 35340b57cec5SDimitry Andric 35350b57cec5SDimitry Andric bool FunctionDecl::isTargetMultiVersion() const { 3536bdd1243dSDimitry Andric return isMultiVersion() && 3537bdd1243dSDimitry Andric (hasAttr<TargetAttr>() || hasAttr<TargetVersionAttr>()); 35380b57cec5SDimitry Andric } 35390b57cec5SDimitry Andric 35404824e7fdSDimitry Andric bool FunctionDecl::isTargetClonesMultiVersion() const { 35414824e7fdSDimitry Andric return isMultiVersion() && hasAttr<TargetClonesAttr>(); 35424824e7fdSDimitry Andric } 35434824e7fdSDimitry Andric 35440b57cec5SDimitry Andric void 35450b57cec5SDimitry Andric FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { 35460b57cec5SDimitry Andric redeclarable_base::setPreviousDecl(PrevDecl); 35470b57cec5SDimitry Andric 35480b57cec5SDimitry Andric if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { 35490b57cec5SDimitry Andric FunctionTemplateDecl *PrevFunTmpl 35500b57cec5SDimitry Andric = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; 35510b57cec5SDimitry Andric assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); 35520b57cec5SDimitry Andric FunTmpl->setPreviousDecl(PrevFunTmpl); 35530b57cec5SDimitry Andric } 35540b57cec5SDimitry Andric 35550b57cec5SDimitry Andric if (PrevDecl && PrevDecl->isInlined()) 35560b57cec5SDimitry Andric setImplicitlyInline(true); 35570b57cec5SDimitry Andric } 35580b57cec5SDimitry Andric 35590b57cec5SDimitry Andric FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } 35600b57cec5SDimitry Andric 35610b57cec5SDimitry Andric /// Returns a value indicating whether this function corresponds to a builtin 35620b57cec5SDimitry Andric /// function. 35630b57cec5SDimitry Andric /// 35640b57cec5SDimitry Andric /// The function corresponds to a built-in function if it is declared at 35650b57cec5SDimitry Andric /// translation scope or within an extern "C" block and its name matches with 35660b57cec5SDimitry Andric /// the name of a builtin. The returned value will be 0 for functions that do 35670b57cec5SDimitry Andric /// not correspond to a builtin, a value of type \c Builtin::ID if in the 35680b57cec5SDimitry Andric /// target-independent range \c [1,Builtin::First), or a target-specific builtin 35690b57cec5SDimitry Andric /// value. 35700b57cec5SDimitry Andric /// 35710b57cec5SDimitry Andric /// \param ConsiderWrapperFunctions If true, we should consider wrapper 35720b57cec5SDimitry Andric /// functions as their wrapped builtins. This shouldn't be done in general, but 35730b57cec5SDimitry Andric /// it's useful in Sema to diagnose calls to wrappers based on their semantics. 35740b57cec5SDimitry Andric unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { 3575927c847dSDimitry Andric unsigned BuiltinID = 0; 3576480093f4SDimitry Andric 35775ffd83dbSDimitry Andric if (const auto *ABAA = getAttr<ArmBuiltinAliasAttr>()) { 35785ffd83dbSDimitry Andric BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); 3579fe6060f1SDimitry Andric } else if (const auto *BAA = getAttr<BuiltinAliasAttr>()) { 3580fe6060f1SDimitry Andric BuiltinID = BAA->getBuiltinName()->getBuiltinID(); 3581927c847dSDimitry Andric } else if (const auto *A = getAttr<BuiltinAttr>()) { 3582927c847dSDimitry Andric BuiltinID = A->getID(); 3583480093f4SDimitry Andric } 3584480093f4SDimitry Andric 35850b57cec5SDimitry Andric if (!BuiltinID) 35860b57cec5SDimitry Andric return 0; 35870b57cec5SDimitry Andric 35880b57cec5SDimitry Andric // If the function is marked "overloadable", it has a different mangled name 35890b57cec5SDimitry Andric // and is not the C library function. 3590480093f4SDimitry Andric if (!ConsiderWrapperFunctions && hasAttr<OverloadableAttr>() && 3591fe6060f1SDimitry Andric (!hasAttr<ArmBuiltinAliasAttr>() && !hasAttr<BuiltinAliasAttr>())) 35920b57cec5SDimitry Andric return 0; 35930b57cec5SDimitry Andric 3594cb14a3feSDimitry Andric const ASTContext &Context = getASTContext(); 35950b57cec5SDimitry Andric if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 35960b57cec5SDimitry Andric return BuiltinID; 35970b57cec5SDimitry Andric 35980b57cec5SDimitry Andric // This function has the name of a known C library 35990b57cec5SDimitry Andric // function. Determine whether it actually refers to the C library 36000b57cec5SDimitry Andric // function or whether it just has the same name. 36010b57cec5SDimitry Andric 36020b57cec5SDimitry Andric // If this is a static function, it's not a builtin. 36030b57cec5SDimitry Andric if (!ConsiderWrapperFunctions && getStorageClass() == SC_Static) 36040b57cec5SDimitry Andric return 0; 36050b57cec5SDimitry Andric 36060b57cec5SDimitry Andric // OpenCL v1.2 s6.9.f - The library functions defined in 36070b57cec5SDimitry Andric // the C99 standard headers are not available. 36080b57cec5SDimitry Andric if (Context.getLangOpts().OpenCL && 36090b57cec5SDimitry Andric Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 36100b57cec5SDimitry Andric return 0; 36110b57cec5SDimitry Andric 36120b57cec5SDimitry Andric // CUDA does not have device-side standard library. printf and malloc are the 36130b57cec5SDimitry Andric // only special cases that are supported by device-side runtime. 36140b57cec5SDimitry Andric if (Context.getLangOpts().CUDA && hasAttr<CUDADeviceAttr>() && 36150b57cec5SDimitry Andric !hasAttr<CUDAHostAttr>() && 36160b57cec5SDimitry Andric !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) 36170b57cec5SDimitry Andric return 0; 36180b57cec5SDimitry Andric 36195ffd83dbSDimitry Andric // As AMDGCN implementation of OpenMP does not have a device-side standard 36205ffd83dbSDimitry Andric // library, none of the predefined library functions except printf and malloc 36215ffd83dbSDimitry Andric // should be treated as a builtin i.e. 0 should be returned for them. 36225ffd83dbSDimitry Andric if (Context.getTargetInfo().getTriple().isAMDGCN() && 362306c3fb27SDimitry Andric Context.getLangOpts().OpenMPIsTargetDevice && 36245ffd83dbSDimitry Andric Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && 36255ffd83dbSDimitry Andric !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) 36265ffd83dbSDimitry Andric return 0; 36275ffd83dbSDimitry Andric 36280b57cec5SDimitry Andric return BuiltinID; 36290b57cec5SDimitry Andric } 36300b57cec5SDimitry Andric 36310b57cec5SDimitry Andric /// getNumParams - Return the number of parameters this function must have 36320b57cec5SDimitry Andric /// based on its FunctionType. This is the length of the ParamInfo array 36330b57cec5SDimitry Andric /// after it has been created. 36340b57cec5SDimitry Andric unsigned FunctionDecl::getNumParams() const { 36350b57cec5SDimitry Andric const auto *FPT = getType()->getAs<FunctionProtoType>(); 36360b57cec5SDimitry Andric return FPT ? FPT->getNumParams() : 0; 36370b57cec5SDimitry Andric } 36380b57cec5SDimitry Andric 36390b57cec5SDimitry Andric void FunctionDecl::setParams(ASTContext &C, 36400b57cec5SDimitry Andric ArrayRef<ParmVarDecl *> NewParamInfo) { 36410b57cec5SDimitry Andric assert(!ParamInfo && "Already has param info!"); 36420b57cec5SDimitry Andric assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); 36430b57cec5SDimitry Andric 36440b57cec5SDimitry Andric // Zero params -> null pointer. 36450b57cec5SDimitry Andric if (!NewParamInfo.empty()) { 36460b57cec5SDimitry Andric ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; 36470b57cec5SDimitry Andric std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); 36480b57cec5SDimitry Andric } 36490b57cec5SDimitry Andric } 36500b57cec5SDimitry Andric 36510b57cec5SDimitry Andric /// getMinRequiredArguments - Returns the minimum number of arguments 36520b57cec5SDimitry Andric /// needed to call this function. This may be fewer than the number of 36530b57cec5SDimitry Andric /// function parameters, if some of the parameters have default 36540b57cec5SDimitry Andric /// arguments (in C++) or are parameter packs (C++11). 36550b57cec5SDimitry Andric unsigned FunctionDecl::getMinRequiredArguments() const { 36560b57cec5SDimitry Andric if (!getASTContext().getLangOpts().CPlusPlus) 36570b57cec5SDimitry Andric return getNumParams(); 36580b57cec5SDimitry Andric 36595ffd83dbSDimitry Andric // Note that it is possible for a parameter with no default argument to 36605ffd83dbSDimitry Andric // follow a parameter with a default argument. 36610b57cec5SDimitry Andric unsigned NumRequiredArgs = 0; 36625ffd83dbSDimitry Andric unsigned MinParamsSoFar = 0; 36635ffd83dbSDimitry Andric for (auto *Param : parameters()) { 36645ffd83dbSDimitry Andric if (!Param->isParameterPack()) { 36655ffd83dbSDimitry Andric ++MinParamsSoFar; 36665ffd83dbSDimitry Andric if (!Param->hasDefaultArg()) 36675ffd83dbSDimitry Andric NumRequiredArgs = MinParamsSoFar; 36685ffd83dbSDimitry Andric } 36695ffd83dbSDimitry Andric } 36700b57cec5SDimitry Andric return NumRequiredArgs; 36710b57cec5SDimitry Andric } 36720b57cec5SDimitry Andric 36735f757f3fSDimitry Andric bool FunctionDecl::hasCXXExplicitFunctionObjectParameter() const { 36745f757f3fSDimitry Andric return getNumParams() != 0 && getParamDecl(0)->isExplicitObjectParameter(); 36755f757f3fSDimitry Andric } 36765f757f3fSDimitry Andric 36775f757f3fSDimitry Andric unsigned FunctionDecl::getNumNonObjectParams() const { 36785f757f3fSDimitry Andric return getNumParams() - 36795f757f3fSDimitry Andric static_cast<unsigned>(hasCXXExplicitFunctionObjectParameter()); 36805f757f3fSDimitry Andric } 36815f757f3fSDimitry Andric 36825f757f3fSDimitry Andric unsigned FunctionDecl::getMinRequiredExplicitArguments() const { 36835f757f3fSDimitry Andric return getMinRequiredArguments() - 36845f757f3fSDimitry Andric static_cast<unsigned>(hasCXXExplicitFunctionObjectParameter()); 36855f757f3fSDimitry Andric } 36865f757f3fSDimitry Andric 36875ffd83dbSDimitry Andric bool FunctionDecl::hasOneParamOrDefaultArgs() const { 36885ffd83dbSDimitry Andric return getNumParams() == 1 || 36895ffd83dbSDimitry Andric (getNumParams() > 1 && 3690bdd1243dSDimitry Andric llvm::all_of(llvm::drop_begin(parameters()), 36915ffd83dbSDimitry Andric [](ParmVarDecl *P) { return P->hasDefaultArg(); })); 36925ffd83dbSDimitry Andric } 36935ffd83dbSDimitry Andric 36940b57cec5SDimitry Andric /// The combination of the extern and inline keywords under MSVC forces 36950b57cec5SDimitry Andric /// the function to be required. 36960b57cec5SDimitry Andric /// 36970b57cec5SDimitry Andric /// Note: This function assumes that we will only get called when isInlined() 36980b57cec5SDimitry Andric /// would return true for this FunctionDecl. 36990b57cec5SDimitry Andric bool FunctionDecl::isMSExternInline() const { 37000b57cec5SDimitry Andric assert(isInlined() && "expected to get called on an inlined function!"); 37010b57cec5SDimitry Andric 37020b57cec5SDimitry Andric const ASTContext &Context = getASTContext(); 37030b57cec5SDimitry Andric if (!Context.getTargetInfo().getCXXABI().isMicrosoft() && 37040b57cec5SDimitry Andric !hasAttr<DLLExportAttr>()) 37050b57cec5SDimitry Andric return false; 37060b57cec5SDimitry Andric 37070b57cec5SDimitry Andric for (const FunctionDecl *FD = getMostRecentDecl(); FD; 37080b57cec5SDimitry Andric FD = FD->getPreviousDecl()) 37090b57cec5SDimitry Andric if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) 37100b57cec5SDimitry Andric return true; 37110b57cec5SDimitry Andric 37120b57cec5SDimitry Andric return false; 37130b57cec5SDimitry Andric } 37140b57cec5SDimitry Andric 37150b57cec5SDimitry Andric static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { 37160b57cec5SDimitry Andric if (Redecl->getStorageClass() != SC_Extern) 37170b57cec5SDimitry Andric return false; 37180b57cec5SDimitry Andric 37190b57cec5SDimitry Andric for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; 37200b57cec5SDimitry Andric FD = FD->getPreviousDecl()) 37210b57cec5SDimitry Andric if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) 37220b57cec5SDimitry Andric return false; 37230b57cec5SDimitry Andric 37240b57cec5SDimitry Andric return true; 37250b57cec5SDimitry Andric } 37260b57cec5SDimitry Andric 37270b57cec5SDimitry Andric static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { 37280b57cec5SDimitry Andric // Only consider file-scope declarations in this test. 37290b57cec5SDimitry Andric if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) 37300b57cec5SDimitry Andric return false; 37310b57cec5SDimitry Andric 37320b57cec5SDimitry Andric // Only consider explicit declarations; the presence of a builtin for a 37330b57cec5SDimitry Andric // libcall shouldn't affect whether a definition is externally visible. 37340b57cec5SDimitry Andric if (Redecl->isImplicit()) 37350b57cec5SDimitry Andric return false; 37360b57cec5SDimitry Andric 37370b57cec5SDimitry Andric if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) 37380b57cec5SDimitry Andric return true; // Not an inline definition 37390b57cec5SDimitry Andric 37400b57cec5SDimitry Andric return false; 37410b57cec5SDimitry Andric } 37420b57cec5SDimitry Andric 37430b57cec5SDimitry Andric /// For a function declaration in C or C++, determine whether this 37440b57cec5SDimitry Andric /// declaration causes the definition to be externally visible. 37450b57cec5SDimitry Andric /// 37460b57cec5SDimitry Andric /// For instance, this determines if adding the current declaration to the set 37470b57cec5SDimitry Andric /// of redeclarations of the given functions causes 37480b57cec5SDimitry Andric /// isInlineDefinitionExternallyVisible to change from false to true. 37490b57cec5SDimitry Andric bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { 37500b57cec5SDimitry Andric assert(!doesThisDeclarationHaveABody() && 37510b57cec5SDimitry Andric "Must have a declaration without a body."); 37520b57cec5SDimitry Andric 3753cb14a3feSDimitry Andric const ASTContext &Context = getASTContext(); 37540b57cec5SDimitry Andric 37550b57cec5SDimitry Andric if (Context.getLangOpts().MSVCCompat) { 37560b57cec5SDimitry Andric const FunctionDecl *Definition; 37570b57cec5SDimitry Andric if (hasBody(Definition) && Definition->isInlined() && 37580b57cec5SDimitry Andric redeclForcesDefMSVC(this)) 37590b57cec5SDimitry Andric return true; 37600b57cec5SDimitry Andric } 37610b57cec5SDimitry Andric 3762a7dea167SDimitry Andric if (Context.getLangOpts().CPlusPlus) 3763a7dea167SDimitry Andric return false; 3764a7dea167SDimitry Andric 37650b57cec5SDimitry Andric if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { 37660b57cec5SDimitry Andric // With GNU inlining, a declaration with 'inline' but not 'extern', forces 37670b57cec5SDimitry Andric // an externally visible definition. 37680b57cec5SDimitry Andric // 37690b57cec5SDimitry Andric // FIXME: What happens if gnu_inline gets added on after the first 37700b57cec5SDimitry Andric // declaration? 37710b57cec5SDimitry Andric if (!isInlineSpecified() || getStorageClass() == SC_Extern) 37720b57cec5SDimitry Andric return false; 37730b57cec5SDimitry Andric 37740b57cec5SDimitry Andric const FunctionDecl *Prev = this; 37750b57cec5SDimitry Andric bool FoundBody = false; 37760b57cec5SDimitry Andric while ((Prev = Prev->getPreviousDecl())) { 3777480093f4SDimitry Andric FoundBody |= Prev->doesThisDeclarationHaveABody(); 37780b57cec5SDimitry Andric 3779480093f4SDimitry Andric if (Prev->doesThisDeclarationHaveABody()) { 37800b57cec5SDimitry Andric // If it's not the case that both 'inline' and 'extern' are 37810b57cec5SDimitry Andric // specified on the definition, then it is always externally visible. 37820b57cec5SDimitry Andric if (!Prev->isInlineSpecified() || 37830b57cec5SDimitry Andric Prev->getStorageClass() != SC_Extern) 37840b57cec5SDimitry Andric return false; 37850b57cec5SDimitry Andric } else if (Prev->isInlineSpecified() && 37860b57cec5SDimitry Andric Prev->getStorageClass() != SC_Extern) { 37870b57cec5SDimitry Andric return false; 37880b57cec5SDimitry Andric } 37890b57cec5SDimitry Andric } 37900b57cec5SDimitry Andric return FoundBody; 37910b57cec5SDimitry Andric } 37920b57cec5SDimitry Andric 37930b57cec5SDimitry Andric // C99 6.7.4p6: 37940b57cec5SDimitry Andric // [...] If all of the file scope declarations for a function in a 37950b57cec5SDimitry Andric // translation unit include the inline function specifier without extern, 37960b57cec5SDimitry Andric // then the definition in that translation unit is an inline definition. 37970b57cec5SDimitry Andric if (isInlineSpecified() && getStorageClass() != SC_Extern) 37980b57cec5SDimitry Andric return false; 37990b57cec5SDimitry Andric const FunctionDecl *Prev = this; 38000b57cec5SDimitry Andric bool FoundBody = false; 38010b57cec5SDimitry Andric while ((Prev = Prev->getPreviousDecl())) { 3802480093f4SDimitry Andric FoundBody |= Prev->doesThisDeclarationHaveABody(); 38030b57cec5SDimitry Andric if (RedeclForcesDefC99(Prev)) 38040b57cec5SDimitry Andric return false; 38050b57cec5SDimitry Andric } 38060b57cec5SDimitry Andric return FoundBody; 38070b57cec5SDimitry Andric } 38080b57cec5SDimitry Andric 3809480093f4SDimitry Andric FunctionTypeLoc FunctionDecl::getFunctionTypeLoc() const { 38100b57cec5SDimitry Andric const TypeSourceInfo *TSI = getTypeSourceInfo(); 3811480093f4SDimitry Andric return TSI ? TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>() 3812480093f4SDimitry Andric : FunctionTypeLoc(); 3813480093f4SDimitry Andric } 3814480093f4SDimitry Andric 3815480093f4SDimitry Andric SourceRange FunctionDecl::getReturnTypeSourceRange() const { 3816480093f4SDimitry Andric FunctionTypeLoc FTL = getFunctionTypeLoc(); 38170b57cec5SDimitry Andric if (!FTL) 38180b57cec5SDimitry Andric return SourceRange(); 38190b57cec5SDimitry Andric 38200b57cec5SDimitry Andric // Skip self-referential return types. 38210b57cec5SDimitry Andric const SourceManager &SM = getASTContext().getSourceManager(); 38220b57cec5SDimitry Andric SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); 38230b57cec5SDimitry Andric SourceLocation Boundary = getNameInfo().getBeginLoc(); 38240b57cec5SDimitry Andric if (RTRange.isInvalid() || Boundary.isInvalid() || 38250b57cec5SDimitry Andric !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary)) 38260b57cec5SDimitry Andric return SourceRange(); 38270b57cec5SDimitry Andric 38280b57cec5SDimitry Andric return RTRange; 38290b57cec5SDimitry Andric } 38300b57cec5SDimitry Andric 3831480093f4SDimitry Andric SourceRange FunctionDecl::getParametersSourceRange() const { 3832480093f4SDimitry Andric unsigned NP = getNumParams(); 3833480093f4SDimitry Andric SourceLocation EllipsisLoc = getEllipsisLoc(); 3834480093f4SDimitry Andric 3835480093f4SDimitry Andric if (NP == 0 && EllipsisLoc.isInvalid()) 38360b57cec5SDimitry Andric return SourceRange(); 38370b57cec5SDimitry Andric 3838480093f4SDimitry Andric SourceLocation Begin = 3839480093f4SDimitry Andric NP > 0 ? ParamInfo[0]->getSourceRange().getBegin() : EllipsisLoc; 3840480093f4SDimitry Andric SourceLocation End = EllipsisLoc.isValid() 3841480093f4SDimitry Andric ? EllipsisLoc 3842480093f4SDimitry Andric : ParamInfo[NP - 1]->getSourceRange().getEnd(); 3843480093f4SDimitry Andric 3844480093f4SDimitry Andric return SourceRange(Begin, End); 3845480093f4SDimitry Andric } 3846480093f4SDimitry Andric 3847480093f4SDimitry Andric SourceRange FunctionDecl::getExceptionSpecSourceRange() const { 3848480093f4SDimitry Andric FunctionTypeLoc FTL = getFunctionTypeLoc(); 3849480093f4SDimitry Andric return FTL ? FTL.getExceptionSpecRange() : SourceRange(); 38500b57cec5SDimitry Andric } 38510b57cec5SDimitry Andric 38520b57cec5SDimitry Andric /// For an inline function definition in C, or for a gnu_inline function 38530b57cec5SDimitry Andric /// in C++, determine whether the definition will be externally visible. 38540b57cec5SDimitry Andric /// 38550b57cec5SDimitry Andric /// Inline function definitions are always available for inlining optimizations. 38560b57cec5SDimitry Andric /// However, depending on the language dialect, declaration specifiers, and 38570b57cec5SDimitry Andric /// attributes, the definition of an inline function may or may not be 38580b57cec5SDimitry Andric /// "externally" visible to other translation units in the program. 38590b57cec5SDimitry Andric /// 38600b57cec5SDimitry Andric /// In C99, inline definitions are not externally visible by default. However, 38610b57cec5SDimitry Andric /// if even one of the global-scope declarations is marked "extern inline", the 38620b57cec5SDimitry Andric /// inline definition becomes externally visible (C99 6.7.4p6). 38630b57cec5SDimitry Andric /// 38640b57cec5SDimitry Andric /// In GNU89 mode, or if the gnu_inline attribute is attached to the function 38650b57cec5SDimitry Andric /// definition, we use the GNU semantics for inline, which are nearly the 38660b57cec5SDimitry Andric /// opposite of C99 semantics. In particular, "inline" by itself will create 38670b57cec5SDimitry Andric /// an externally visible symbol, but "extern inline" will not create an 38680b57cec5SDimitry Andric /// externally visible symbol. 38690b57cec5SDimitry Andric bool FunctionDecl::isInlineDefinitionExternallyVisible() const { 38700b57cec5SDimitry Andric assert((doesThisDeclarationHaveABody() || willHaveBody() || 38710b57cec5SDimitry Andric hasAttr<AliasAttr>()) && 38720b57cec5SDimitry Andric "Must be a function definition"); 38730b57cec5SDimitry Andric assert(isInlined() && "Function must be inline"); 38740b57cec5SDimitry Andric ASTContext &Context = getASTContext(); 38750b57cec5SDimitry Andric 38760b57cec5SDimitry Andric if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { 38770b57cec5SDimitry Andric // Note: If you change the logic here, please change 38780b57cec5SDimitry Andric // doesDeclarationForceExternallyVisibleDefinition as well. 38790b57cec5SDimitry Andric // 38800b57cec5SDimitry Andric // If it's not the case that both 'inline' and 'extern' are 38810b57cec5SDimitry Andric // specified on the definition, then this inline definition is 38820b57cec5SDimitry Andric // externally visible. 3883a7dea167SDimitry Andric if (Context.getLangOpts().CPlusPlus) 3884a7dea167SDimitry Andric return false; 38850b57cec5SDimitry Andric if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) 38860b57cec5SDimitry Andric return true; 38870b57cec5SDimitry Andric 38880b57cec5SDimitry Andric // If any declaration is 'inline' but not 'extern', then this definition 38890b57cec5SDimitry Andric // is externally visible. 3890bdd1243dSDimitry Andric for (auto *Redecl : redecls()) { 38910b57cec5SDimitry Andric if (Redecl->isInlineSpecified() && 38920b57cec5SDimitry Andric Redecl->getStorageClass() != SC_Extern) 38930b57cec5SDimitry Andric return true; 38940b57cec5SDimitry Andric } 38950b57cec5SDimitry Andric 38960b57cec5SDimitry Andric return false; 38970b57cec5SDimitry Andric } 38980b57cec5SDimitry Andric 38990b57cec5SDimitry Andric // The rest of this function is C-only. 39000b57cec5SDimitry Andric assert(!Context.getLangOpts().CPlusPlus && 39010b57cec5SDimitry Andric "should not use C inline rules in C++"); 39020b57cec5SDimitry Andric 39030b57cec5SDimitry Andric // C99 6.7.4p6: 39040b57cec5SDimitry Andric // [...] If all of the file scope declarations for a function in a 39050b57cec5SDimitry Andric // translation unit include the inline function specifier without extern, 39060b57cec5SDimitry Andric // then the definition in that translation unit is an inline definition. 3907bdd1243dSDimitry Andric for (auto *Redecl : redecls()) { 39080b57cec5SDimitry Andric if (RedeclForcesDefC99(Redecl)) 39090b57cec5SDimitry Andric return true; 39100b57cec5SDimitry Andric } 39110b57cec5SDimitry Andric 39120b57cec5SDimitry Andric // C99 6.7.4p6: 39130b57cec5SDimitry Andric // An inline definition does not provide an external definition for the 39140b57cec5SDimitry Andric // function, and does not forbid an external definition in another 39150b57cec5SDimitry Andric // translation unit. 39160b57cec5SDimitry Andric return false; 39170b57cec5SDimitry Andric } 39180b57cec5SDimitry Andric 39190b57cec5SDimitry Andric /// getOverloadedOperator - Which C++ overloaded operator this 39200b57cec5SDimitry Andric /// function represents, if any. 39210b57cec5SDimitry Andric OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { 39220b57cec5SDimitry Andric if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) 39230b57cec5SDimitry Andric return getDeclName().getCXXOverloadedOperator(); 39240b57cec5SDimitry Andric return OO_None; 39250b57cec5SDimitry Andric } 39260b57cec5SDimitry Andric 39270b57cec5SDimitry Andric /// getLiteralIdentifier - The literal suffix identifier this function 39280b57cec5SDimitry Andric /// represents, if any. 39290b57cec5SDimitry Andric const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { 39300b57cec5SDimitry Andric if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) 39310b57cec5SDimitry Andric return getDeclName().getCXXLiteralIdentifier(); 39320b57cec5SDimitry Andric return nullptr; 39330b57cec5SDimitry Andric } 39340b57cec5SDimitry Andric 39350b57cec5SDimitry Andric FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { 39360b57cec5SDimitry Andric if (TemplateOrSpecialization.isNull()) 39370b57cec5SDimitry Andric return TK_NonTemplate; 3938fcaf7f86SDimitry Andric if (const auto *ND = TemplateOrSpecialization.dyn_cast<NamedDecl *>()) { 3939fcaf7f86SDimitry Andric if (isa<FunctionDecl>(ND)) 3940fcaf7f86SDimitry Andric return TK_DependentNonTemplate; 3941fcaf7f86SDimitry Andric assert(isa<FunctionTemplateDecl>(ND) && 3942fcaf7f86SDimitry Andric "No other valid types in NamedDecl"); 39430b57cec5SDimitry Andric return TK_FunctionTemplate; 3944fcaf7f86SDimitry Andric } 39450b57cec5SDimitry Andric if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) 39460b57cec5SDimitry Andric return TK_MemberSpecialization; 39470b57cec5SDimitry Andric if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) 39480b57cec5SDimitry Andric return TK_FunctionTemplateSpecialization; 39490b57cec5SDimitry Andric if (TemplateOrSpecialization.is 39500b57cec5SDimitry Andric <DependentFunctionTemplateSpecializationInfo*>()) 39510b57cec5SDimitry Andric return TK_DependentFunctionTemplateSpecialization; 39520b57cec5SDimitry Andric 39530b57cec5SDimitry Andric llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); 39540b57cec5SDimitry Andric } 39550b57cec5SDimitry Andric 39560b57cec5SDimitry Andric FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { 39570b57cec5SDimitry Andric if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) 39580b57cec5SDimitry Andric return cast<FunctionDecl>(Info->getInstantiatedFrom()); 39590b57cec5SDimitry Andric 39600b57cec5SDimitry Andric return nullptr; 39610b57cec5SDimitry Andric } 39620b57cec5SDimitry Andric 39630b57cec5SDimitry Andric MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { 39640b57cec5SDimitry Andric if (auto *MSI = 39650b57cec5SDimitry Andric TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) 39660b57cec5SDimitry Andric return MSI; 39670b57cec5SDimitry Andric if (auto *FTSI = TemplateOrSpecialization 39680b57cec5SDimitry Andric .dyn_cast<FunctionTemplateSpecializationInfo *>()) 39690b57cec5SDimitry Andric return FTSI->getMemberSpecializationInfo(); 39700b57cec5SDimitry Andric return nullptr; 39710b57cec5SDimitry Andric } 39720b57cec5SDimitry Andric 39730b57cec5SDimitry Andric void 39740b57cec5SDimitry Andric FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, 39750b57cec5SDimitry Andric FunctionDecl *FD, 39760b57cec5SDimitry Andric TemplateSpecializationKind TSK) { 39770b57cec5SDimitry Andric assert(TemplateOrSpecialization.isNull() && 39780b57cec5SDimitry Andric "Member function is already a specialization"); 39790b57cec5SDimitry Andric MemberSpecializationInfo *Info 39800b57cec5SDimitry Andric = new (C) MemberSpecializationInfo(FD, TSK); 39810b57cec5SDimitry Andric TemplateOrSpecialization = Info; 39820b57cec5SDimitry Andric } 39830b57cec5SDimitry Andric 39840b57cec5SDimitry Andric FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const { 39855f757f3fSDimitry Andric return dyn_cast_if_present<FunctionTemplateDecl>( 3986fcaf7f86SDimitry Andric TemplateOrSpecialization.dyn_cast<NamedDecl *>()); 39870b57cec5SDimitry Andric } 39880b57cec5SDimitry Andric 3989fcaf7f86SDimitry Andric void FunctionDecl::setDescribedFunctionTemplate( 3990fcaf7f86SDimitry Andric FunctionTemplateDecl *Template) { 39910b57cec5SDimitry Andric assert(TemplateOrSpecialization.isNull() && 39920b57cec5SDimitry Andric "Member function is already a specialization"); 39930b57cec5SDimitry Andric TemplateOrSpecialization = Template; 39940b57cec5SDimitry Andric } 39950b57cec5SDimitry Andric 39965f757f3fSDimitry Andric bool FunctionDecl::isFunctionTemplateSpecialization() const { 39975f757f3fSDimitry Andric return TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>() || 39985f757f3fSDimitry Andric TemplateOrSpecialization 39995f757f3fSDimitry Andric .is<DependentFunctionTemplateSpecializationInfo *>(); 40005f757f3fSDimitry Andric } 40015f757f3fSDimitry Andric 4002fcaf7f86SDimitry Andric void FunctionDecl::setInstantiatedFromDecl(FunctionDecl *FD) { 4003fcaf7f86SDimitry Andric assert(TemplateOrSpecialization.isNull() && 4004fcaf7f86SDimitry Andric "Function is already a specialization"); 4005fcaf7f86SDimitry Andric TemplateOrSpecialization = FD; 4006fcaf7f86SDimitry Andric } 4007fcaf7f86SDimitry Andric 4008fcaf7f86SDimitry Andric FunctionDecl *FunctionDecl::getInstantiatedFromDecl() const { 40095f757f3fSDimitry Andric return dyn_cast_if_present<FunctionDecl>( 4010fcaf7f86SDimitry Andric TemplateOrSpecialization.dyn_cast<NamedDecl *>()); 4011fcaf7f86SDimitry Andric } 4012fcaf7f86SDimitry Andric 40130b57cec5SDimitry Andric bool FunctionDecl::isImplicitlyInstantiable() const { 40140b57cec5SDimitry Andric // If the function is invalid, it can't be implicitly instantiated. 40150b57cec5SDimitry Andric if (isInvalidDecl()) 40160b57cec5SDimitry Andric return false; 40170b57cec5SDimitry Andric 40180b57cec5SDimitry Andric switch (getTemplateSpecializationKindForInstantiation()) { 40190b57cec5SDimitry Andric case TSK_Undeclared: 40200b57cec5SDimitry Andric case TSK_ExplicitInstantiationDefinition: 40210b57cec5SDimitry Andric case TSK_ExplicitSpecialization: 40220b57cec5SDimitry Andric return false; 40230b57cec5SDimitry Andric 40240b57cec5SDimitry Andric case TSK_ImplicitInstantiation: 40250b57cec5SDimitry Andric return true; 40260b57cec5SDimitry Andric 40270b57cec5SDimitry Andric case TSK_ExplicitInstantiationDeclaration: 40280b57cec5SDimitry Andric // Handled below. 40290b57cec5SDimitry Andric break; 40300b57cec5SDimitry Andric } 40310b57cec5SDimitry Andric 40320b57cec5SDimitry Andric // Find the actual template from which we will instantiate. 40330b57cec5SDimitry Andric const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); 40340b57cec5SDimitry Andric bool HasPattern = false; 40350b57cec5SDimitry Andric if (PatternDecl) 40360b57cec5SDimitry Andric HasPattern = PatternDecl->hasBody(PatternDecl); 40370b57cec5SDimitry Andric 40380b57cec5SDimitry Andric // C++0x [temp.explicit]p9: 40390b57cec5SDimitry Andric // Except for inline functions, other explicit instantiation declarations 40400b57cec5SDimitry Andric // have the effect of suppressing the implicit instantiation of the entity 40410b57cec5SDimitry Andric // to which they refer. 40420b57cec5SDimitry Andric if (!HasPattern || !PatternDecl) 40430b57cec5SDimitry Andric return true; 40440b57cec5SDimitry Andric 40450b57cec5SDimitry Andric return PatternDecl->isInlined(); 40460b57cec5SDimitry Andric } 40470b57cec5SDimitry Andric 40480b57cec5SDimitry Andric bool FunctionDecl::isTemplateInstantiation() const { 40490b57cec5SDimitry Andric // FIXME: Remove this, it's not clear what it means. (Which template 40500b57cec5SDimitry Andric // specialization kind?) 40510b57cec5SDimitry Andric return clang::isTemplateInstantiation(getTemplateSpecializationKind()); 40520b57cec5SDimitry Andric } 40530b57cec5SDimitry Andric 40545ffd83dbSDimitry Andric FunctionDecl * 40555ffd83dbSDimitry Andric FunctionDecl::getTemplateInstantiationPattern(bool ForDefinition) const { 40560b57cec5SDimitry Andric // If this is a generic lambda call operator specialization, its 40570b57cec5SDimitry Andric // instantiation pattern is always its primary template's pattern 40580b57cec5SDimitry Andric // even if its primary template was instantiated from another 40590b57cec5SDimitry Andric // member template (which happens with nested generic lambdas). 40600b57cec5SDimitry Andric // Since a lambda's call operator's body is transformed eagerly, 40610b57cec5SDimitry Andric // we don't have to go hunting for a prototype definition template 40620b57cec5SDimitry Andric // (i.e. instantiated-from-member-template) to use as an instantiation 40630b57cec5SDimitry Andric // pattern. 40640b57cec5SDimitry Andric 40650b57cec5SDimitry Andric if (isGenericLambdaCallOperatorSpecialization( 40660b57cec5SDimitry Andric dyn_cast<CXXMethodDecl>(this))) { 40670b57cec5SDimitry Andric assert(getPrimaryTemplate() && "not a generic lambda call operator?"); 40680b57cec5SDimitry Andric return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl()); 40690b57cec5SDimitry Andric } 40700b57cec5SDimitry Andric 4071e8d8bef9SDimitry Andric // Check for a declaration of this function that was instantiated from a 4072e8d8bef9SDimitry Andric // friend definition. 4073e8d8bef9SDimitry Andric const FunctionDecl *FD = nullptr; 4074e8d8bef9SDimitry Andric if (!isDefined(FD, /*CheckForPendingFriendDefinition=*/true)) 4075e8d8bef9SDimitry Andric FD = this; 4076e8d8bef9SDimitry Andric 4077e8d8bef9SDimitry Andric if (MemberSpecializationInfo *Info = FD->getMemberSpecializationInfo()) { 40785ffd83dbSDimitry Andric if (ForDefinition && 40795ffd83dbSDimitry Andric !clang::isTemplateInstantiation(Info->getTemplateSpecializationKind())) 40800b57cec5SDimitry Andric return nullptr; 40810b57cec5SDimitry Andric return getDefinitionOrSelf(cast<FunctionDecl>(Info->getInstantiatedFrom())); 40820b57cec5SDimitry Andric } 40830b57cec5SDimitry Andric 40845ffd83dbSDimitry Andric if (ForDefinition && 40855ffd83dbSDimitry Andric !clang::isTemplateInstantiation(getTemplateSpecializationKind())) 40860b57cec5SDimitry Andric return nullptr; 40870b57cec5SDimitry Andric 40880b57cec5SDimitry Andric if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { 40890b57cec5SDimitry Andric // If we hit a point where the user provided a specialization of this 40900b57cec5SDimitry Andric // template, we're done looking. 40915ffd83dbSDimitry Andric while (!ForDefinition || !Primary->isMemberSpecialization()) { 40920b57cec5SDimitry Andric auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate(); 40930b57cec5SDimitry Andric if (!NewPrimary) 40940b57cec5SDimitry Andric break; 40950b57cec5SDimitry Andric Primary = NewPrimary; 40960b57cec5SDimitry Andric } 40970b57cec5SDimitry Andric 40980b57cec5SDimitry Andric return getDefinitionOrSelf(Primary->getTemplatedDecl()); 40990b57cec5SDimitry Andric } 41000b57cec5SDimitry Andric 41010b57cec5SDimitry Andric return nullptr; 41020b57cec5SDimitry Andric } 41030b57cec5SDimitry Andric 41040b57cec5SDimitry Andric FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { 41050b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *Info 41060b57cec5SDimitry Andric = TemplateOrSpecialization 41070b57cec5SDimitry Andric .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 41080b57cec5SDimitry Andric return Info->getTemplate(); 41090b57cec5SDimitry Andric } 41100b57cec5SDimitry Andric return nullptr; 41110b57cec5SDimitry Andric } 41120b57cec5SDimitry Andric 41130b57cec5SDimitry Andric FunctionTemplateSpecializationInfo * 41140b57cec5SDimitry Andric FunctionDecl::getTemplateSpecializationInfo() const { 41150b57cec5SDimitry Andric return TemplateOrSpecialization 41160b57cec5SDimitry Andric .dyn_cast<FunctionTemplateSpecializationInfo *>(); 41170b57cec5SDimitry Andric } 41180b57cec5SDimitry Andric 41190b57cec5SDimitry Andric const TemplateArgumentList * 41200b57cec5SDimitry Andric FunctionDecl::getTemplateSpecializationArgs() const { 41210b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *Info 41220b57cec5SDimitry Andric = TemplateOrSpecialization 41230b57cec5SDimitry Andric .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 41240b57cec5SDimitry Andric return Info->TemplateArguments; 41250b57cec5SDimitry Andric } 41260b57cec5SDimitry Andric return nullptr; 41270b57cec5SDimitry Andric } 41280b57cec5SDimitry Andric 41290b57cec5SDimitry Andric const ASTTemplateArgumentListInfo * 41300b57cec5SDimitry Andric FunctionDecl::getTemplateSpecializationArgsAsWritten() const { 41310b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *Info 41320b57cec5SDimitry Andric = TemplateOrSpecialization 41330b57cec5SDimitry Andric .dyn_cast<FunctionTemplateSpecializationInfo*>()) { 41340b57cec5SDimitry Andric return Info->TemplateArgumentsAsWritten; 41350b57cec5SDimitry Andric } 41365f757f3fSDimitry Andric if (DependentFunctionTemplateSpecializationInfo *Info = 41375f757f3fSDimitry Andric TemplateOrSpecialization 41385f757f3fSDimitry Andric .dyn_cast<DependentFunctionTemplateSpecializationInfo *>()) { 41395f757f3fSDimitry Andric return Info->TemplateArgumentsAsWritten; 41405f757f3fSDimitry Andric } 41410b57cec5SDimitry Andric return nullptr; 41420b57cec5SDimitry Andric } 41430b57cec5SDimitry Andric 41440b57cec5SDimitry Andric void 41450b57cec5SDimitry Andric FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, 41460b57cec5SDimitry Andric FunctionTemplateDecl *Template, 41470b57cec5SDimitry Andric const TemplateArgumentList *TemplateArgs, 41480b57cec5SDimitry Andric void *InsertPos, 41490b57cec5SDimitry Andric TemplateSpecializationKind TSK, 41500b57cec5SDimitry Andric const TemplateArgumentListInfo *TemplateArgsAsWritten, 41510b57cec5SDimitry Andric SourceLocation PointOfInstantiation) { 41520b57cec5SDimitry Andric assert((TemplateOrSpecialization.isNull() || 41530b57cec5SDimitry Andric TemplateOrSpecialization.is<MemberSpecializationInfo *>()) && 41540b57cec5SDimitry Andric "Member function is already a specialization"); 41550b57cec5SDimitry Andric assert(TSK != TSK_Undeclared && 41560b57cec5SDimitry Andric "Must specify the type of function template specialization"); 41570b57cec5SDimitry Andric assert((TemplateOrSpecialization.isNull() || 41585f757f3fSDimitry Andric getFriendObjectKind() != FOK_None || 41590b57cec5SDimitry Andric TSK == TSK_ExplicitSpecialization) && 41600b57cec5SDimitry Andric "Member specialization must be an explicit specialization"); 41610b57cec5SDimitry Andric FunctionTemplateSpecializationInfo *Info = 41620b57cec5SDimitry Andric FunctionTemplateSpecializationInfo::Create( 41630b57cec5SDimitry Andric C, this, Template, TSK, TemplateArgs, TemplateArgsAsWritten, 41640b57cec5SDimitry Andric PointOfInstantiation, 41650b57cec5SDimitry Andric TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()); 41660b57cec5SDimitry Andric TemplateOrSpecialization = Info; 41670b57cec5SDimitry Andric Template->addSpecialization(Info, InsertPos); 41680b57cec5SDimitry Andric } 41690b57cec5SDimitry Andric 41705f757f3fSDimitry Andric void FunctionDecl::setDependentTemplateSpecialization( 41715f757f3fSDimitry Andric ASTContext &Context, const UnresolvedSetImpl &Templates, 41725f757f3fSDimitry Andric const TemplateArgumentListInfo *TemplateArgs) { 41730b57cec5SDimitry Andric assert(TemplateOrSpecialization.isNull()); 41740b57cec5SDimitry Andric DependentFunctionTemplateSpecializationInfo *Info = 41750b57cec5SDimitry Andric DependentFunctionTemplateSpecializationInfo::Create(Context, Templates, 41760b57cec5SDimitry Andric TemplateArgs); 41770b57cec5SDimitry Andric TemplateOrSpecialization = Info; 41780b57cec5SDimitry Andric } 41790b57cec5SDimitry Andric 41800b57cec5SDimitry Andric DependentFunctionTemplateSpecializationInfo * 41810b57cec5SDimitry Andric FunctionDecl::getDependentSpecializationInfo() const { 41820b57cec5SDimitry Andric return TemplateOrSpecialization 41830b57cec5SDimitry Andric .dyn_cast<DependentFunctionTemplateSpecializationInfo *>(); 41840b57cec5SDimitry Andric } 41850b57cec5SDimitry Andric 41860b57cec5SDimitry Andric DependentFunctionTemplateSpecializationInfo * 41870b57cec5SDimitry Andric DependentFunctionTemplateSpecializationInfo::Create( 41885f757f3fSDimitry Andric ASTContext &Context, const UnresolvedSetImpl &Candidates, 41895f757f3fSDimitry Andric const TemplateArgumentListInfo *TArgs) { 41905f757f3fSDimitry Andric const auto *TArgsWritten = 41915f757f3fSDimitry Andric TArgs ? ASTTemplateArgumentListInfo::Create(Context, *TArgs) : nullptr; 41925f757f3fSDimitry Andric return new (Context.Allocate( 41935f757f3fSDimitry Andric totalSizeToAlloc<FunctionTemplateDecl *>(Candidates.size()))) 41945f757f3fSDimitry Andric DependentFunctionTemplateSpecializationInfo(Candidates, TArgsWritten); 41950b57cec5SDimitry Andric } 41960b57cec5SDimitry Andric 41970b57cec5SDimitry Andric DependentFunctionTemplateSpecializationInfo:: 41985f757f3fSDimitry Andric DependentFunctionTemplateSpecializationInfo( 41995f757f3fSDimitry Andric const UnresolvedSetImpl &Candidates, 42005f757f3fSDimitry Andric const ASTTemplateArgumentListInfo *TemplateArgsWritten) 42015f757f3fSDimitry Andric : NumCandidates(Candidates.size()), 42025f757f3fSDimitry Andric TemplateArgumentsAsWritten(TemplateArgsWritten) { 42035f757f3fSDimitry Andric std::transform(Candidates.begin(), Candidates.end(), 42045f757f3fSDimitry Andric getTrailingObjects<FunctionTemplateDecl *>(), 42055f757f3fSDimitry Andric [](NamedDecl *ND) { 42065f757f3fSDimitry Andric return cast<FunctionTemplateDecl>(ND->getUnderlyingDecl()); 42075f757f3fSDimitry Andric }); 42080b57cec5SDimitry Andric } 42090b57cec5SDimitry Andric 42100b57cec5SDimitry Andric TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { 42110b57cec5SDimitry Andric // For a function template specialization, query the specialization 42120b57cec5SDimitry Andric // information object. 42130b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *FTSInfo = 42140b57cec5SDimitry Andric TemplateOrSpecialization 42150b57cec5SDimitry Andric .dyn_cast<FunctionTemplateSpecializationInfo *>()) 42160b57cec5SDimitry Andric return FTSInfo->getTemplateSpecializationKind(); 42170b57cec5SDimitry Andric 42180b57cec5SDimitry Andric if (MemberSpecializationInfo *MSInfo = 42190b57cec5SDimitry Andric TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) 42200b57cec5SDimitry Andric return MSInfo->getTemplateSpecializationKind(); 42210b57cec5SDimitry Andric 42225f757f3fSDimitry Andric // A dependent function template specialization is an explicit specialization, 42235f757f3fSDimitry Andric // except when it's a friend declaration. 42245f757f3fSDimitry Andric if (TemplateOrSpecialization 42255f757f3fSDimitry Andric .is<DependentFunctionTemplateSpecializationInfo *>() && 42265f757f3fSDimitry Andric getFriendObjectKind() == FOK_None) 42275f757f3fSDimitry Andric return TSK_ExplicitSpecialization; 42285f757f3fSDimitry Andric 42290b57cec5SDimitry Andric return TSK_Undeclared; 42300b57cec5SDimitry Andric } 42310b57cec5SDimitry Andric 42320b57cec5SDimitry Andric TemplateSpecializationKind 42330b57cec5SDimitry Andric FunctionDecl::getTemplateSpecializationKindForInstantiation() const { 42340b57cec5SDimitry Andric // This is the same as getTemplateSpecializationKind(), except that for a 42350b57cec5SDimitry Andric // function that is both a function template specialization and a member 42360b57cec5SDimitry Andric // specialization, we prefer the member specialization information. Eg: 42370b57cec5SDimitry Andric // 42380b57cec5SDimitry Andric // template<typename T> struct A { 42390b57cec5SDimitry Andric // template<typename U> void f() {} 42400b57cec5SDimitry Andric // template<> void f<int>() {} 42410b57cec5SDimitry Andric // }; 42420b57cec5SDimitry Andric // 42435f757f3fSDimitry Andric // Within the templated CXXRecordDecl, A<T>::f<int> is a dependent function 42445f757f3fSDimitry Andric // template specialization; both getTemplateSpecializationKind() and 42455f757f3fSDimitry Andric // getTemplateSpecializationKindForInstantiation() will return 42465f757f3fSDimitry Andric // TSK_ExplicitSpecialization. 42475f757f3fSDimitry Andric // 42480b57cec5SDimitry Andric // For A<int>::f<int>(): 42490b57cec5SDimitry Andric // * getTemplateSpecializationKind() will return TSK_ExplicitSpecialization 42500b57cec5SDimitry Andric // * getTemplateSpecializationKindForInstantiation() will return 42510b57cec5SDimitry Andric // TSK_ImplicitInstantiation 42520b57cec5SDimitry Andric // 42530b57cec5SDimitry Andric // This reflects the facts that A<int>::f<int> is an explicit specialization 42540b57cec5SDimitry Andric // of A<int>::f, and that A<int>::f<int> should be implicitly instantiated 42550b57cec5SDimitry Andric // from A::f<int> if a definition is needed. 42560b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *FTSInfo = 42570b57cec5SDimitry Andric TemplateOrSpecialization 42580b57cec5SDimitry Andric .dyn_cast<FunctionTemplateSpecializationInfo *>()) { 42590b57cec5SDimitry Andric if (auto *MSInfo = FTSInfo->getMemberSpecializationInfo()) 42600b57cec5SDimitry Andric return MSInfo->getTemplateSpecializationKind(); 42610b57cec5SDimitry Andric return FTSInfo->getTemplateSpecializationKind(); 42620b57cec5SDimitry Andric } 42630b57cec5SDimitry Andric 42640b57cec5SDimitry Andric if (MemberSpecializationInfo *MSInfo = 42650b57cec5SDimitry Andric TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) 42660b57cec5SDimitry Andric return MSInfo->getTemplateSpecializationKind(); 42670b57cec5SDimitry Andric 42685f757f3fSDimitry Andric if (TemplateOrSpecialization 42695f757f3fSDimitry Andric .is<DependentFunctionTemplateSpecializationInfo *>() && 42705f757f3fSDimitry Andric getFriendObjectKind() == FOK_None) 42715f757f3fSDimitry Andric return TSK_ExplicitSpecialization; 42725f757f3fSDimitry Andric 42730b57cec5SDimitry Andric return TSK_Undeclared; 42740b57cec5SDimitry Andric } 42750b57cec5SDimitry Andric 42760b57cec5SDimitry Andric void 42770b57cec5SDimitry Andric FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 42780b57cec5SDimitry Andric SourceLocation PointOfInstantiation) { 42790b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *FTSInfo 42800b57cec5SDimitry Andric = TemplateOrSpecialization.dyn_cast< 42810b57cec5SDimitry Andric FunctionTemplateSpecializationInfo*>()) { 42820b57cec5SDimitry Andric FTSInfo->setTemplateSpecializationKind(TSK); 42830b57cec5SDimitry Andric if (TSK != TSK_ExplicitSpecialization && 42840b57cec5SDimitry Andric PointOfInstantiation.isValid() && 42850b57cec5SDimitry Andric FTSInfo->getPointOfInstantiation().isInvalid()) { 42860b57cec5SDimitry Andric FTSInfo->setPointOfInstantiation(PointOfInstantiation); 42870b57cec5SDimitry Andric if (ASTMutationListener *L = getASTContext().getASTMutationListener()) 42880b57cec5SDimitry Andric L->InstantiationRequested(this); 42890b57cec5SDimitry Andric } 42900b57cec5SDimitry Andric } else if (MemberSpecializationInfo *MSInfo 42910b57cec5SDimitry Andric = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { 42920b57cec5SDimitry Andric MSInfo->setTemplateSpecializationKind(TSK); 42930b57cec5SDimitry Andric if (TSK != TSK_ExplicitSpecialization && 42940b57cec5SDimitry Andric PointOfInstantiation.isValid() && 42950b57cec5SDimitry Andric MSInfo->getPointOfInstantiation().isInvalid()) { 42960b57cec5SDimitry Andric MSInfo->setPointOfInstantiation(PointOfInstantiation); 42970b57cec5SDimitry Andric if (ASTMutationListener *L = getASTContext().getASTMutationListener()) 42980b57cec5SDimitry Andric L->InstantiationRequested(this); 42990b57cec5SDimitry Andric } 43000b57cec5SDimitry Andric } else 43010b57cec5SDimitry Andric llvm_unreachable("Function cannot have a template specialization kind"); 43020b57cec5SDimitry Andric } 43030b57cec5SDimitry Andric 43040b57cec5SDimitry Andric SourceLocation FunctionDecl::getPointOfInstantiation() const { 43050b57cec5SDimitry Andric if (FunctionTemplateSpecializationInfo *FTSInfo 43060b57cec5SDimitry Andric = TemplateOrSpecialization.dyn_cast< 43070b57cec5SDimitry Andric FunctionTemplateSpecializationInfo*>()) 43080b57cec5SDimitry Andric return FTSInfo->getPointOfInstantiation(); 4309e8d8bef9SDimitry Andric if (MemberSpecializationInfo *MSInfo = 4310e8d8bef9SDimitry Andric TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) 43110b57cec5SDimitry Andric return MSInfo->getPointOfInstantiation(); 43120b57cec5SDimitry Andric 43130b57cec5SDimitry Andric return SourceLocation(); 43140b57cec5SDimitry Andric } 43150b57cec5SDimitry Andric 43160b57cec5SDimitry Andric bool FunctionDecl::isOutOfLine() const { 43170b57cec5SDimitry Andric if (Decl::isOutOfLine()) 43180b57cec5SDimitry Andric return true; 43190b57cec5SDimitry Andric 43200b57cec5SDimitry Andric // If this function was instantiated from a member function of a 43210b57cec5SDimitry Andric // class template, check whether that member function was defined out-of-line. 43220b57cec5SDimitry Andric if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { 43230b57cec5SDimitry Andric const FunctionDecl *Definition; 43240b57cec5SDimitry Andric if (FD->hasBody(Definition)) 43250b57cec5SDimitry Andric return Definition->isOutOfLine(); 43260b57cec5SDimitry Andric } 43270b57cec5SDimitry Andric 43280b57cec5SDimitry Andric // If this function was instantiated from a function template, 43290b57cec5SDimitry Andric // check whether that function template was defined out-of-line. 43300b57cec5SDimitry Andric if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { 43310b57cec5SDimitry Andric const FunctionDecl *Definition; 43320b57cec5SDimitry Andric if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) 43330b57cec5SDimitry Andric return Definition->isOutOfLine(); 43340b57cec5SDimitry Andric } 43350b57cec5SDimitry Andric 43360b57cec5SDimitry Andric return false; 43370b57cec5SDimitry Andric } 43380b57cec5SDimitry Andric 43390b57cec5SDimitry Andric SourceRange FunctionDecl::getSourceRange() const { 43400b57cec5SDimitry Andric return SourceRange(getOuterLocStart(), EndRangeLoc); 43410b57cec5SDimitry Andric } 43420b57cec5SDimitry Andric 43430b57cec5SDimitry Andric unsigned FunctionDecl::getMemoryFunctionKind() const { 43440b57cec5SDimitry Andric IdentifierInfo *FnInfo = getIdentifier(); 43450b57cec5SDimitry Andric 43460b57cec5SDimitry Andric if (!FnInfo) 43470b57cec5SDimitry Andric return 0; 43480b57cec5SDimitry Andric 43490b57cec5SDimitry Andric // Builtin handling. 43500b57cec5SDimitry Andric switch (getBuiltinID()) { 43510b57cec5SDimitry Andric case Builtin::BI__builtin_memset: 43520b57cec5SDimitry Andric case Builtin::BI__builtin___memset_chk: 43530b57cec5SDimitry Andric case Builtin::BImemset: 43540b57cec5SDimitry Andric return Builtin::BImemset; 43550b57cec5SDimitry Andric 43560b57cec5SDimitry Andric case Builtin::BI__builtin_memcpy: 43570b57cec5SDimitry Andric case Builtin::BI__builtin___memcpy_chk: 43580b57cec5SDimitry Andric case Builtin::BImemcpy: 43590b57cec5SDimitry Andric return Builtin::BImemcpy; 43600b57cec5SDimitry Andric 4361480093f4SDimitry Andric case Builtin::BI__builtin_mempcpy: 4362480093f4SDimitry Andric case Builtin::BI__builtin___mempcpy_chk: 4363480093f4SDimitry Andric case Builtin::BImempcpy: 4364480093f4SDimitry Andric return Builtin::BImempcpy; 4365480093f4SDimitry Andric 43660b57cec5SDimitry Andric case Builtin::BI__builtin_memmove: 43670b57cec5SDimitry Andric case Builtin::BI__builtin___memmove_chk: 43680b57cec5SDimitry Andric case Builtin::BImemmove: 43690b57cec5SDimitry Andric return Builtin::BImemmove; 43700b57cec5SDimitry Andric 43710b57cec5SDimitry Andric case Builtin::BIstrlcpy: 43720b57cec5SDimitry Andric case Builtin::BI__builtin___strlcpy_chk: 43730b57cec5SDimitry Andric return Builtin::BIstrlcpy; 43740b57cec5SDimitry Andric 43750b57cec5SDimitry Andric case Builtin::BIstrlcat: 43760b57cec5SDimitry Andric case Builtin::BI__builtin___strlcat_chk: 43770b57cec5SDimitry Andric return Builtin::BIstrlcat; 43780b57cec5SDimitry Andric 43790b57cec5SDimitry Andric case Builtin::BI__builtin_memcmp: 43800b57cec5SDimitry Andric case Builtin::BImemcmp: 43810b57cec5SDimitry Andric return Builtin::BImemcmp; 43820b57cec5SDimitry Andric 43830b57cec5SDimitry Andric case Builtin::BI__builtin_bcmp: 43840b57cec5SDimitry Andric case Builtin::BIbcmp: 43850b57cec5SDimitry Andric return Builtin::BIbcmp; 43860b57cec5SDimitry Andric 43870b57cec5SDimitry Andric case Builtin::BI__builtin_strncpy: 43880b57cec5SDimitry Andric case Builtin::BI__builtin___strncpy_chk: 43890b57cec5SDimitry Andric case Builtin::BIstrncpy: 43900b57cec5SDimitry Andric return Builtin::BIstrncpy; 43910b57cec5SDimitry Andric 43920b57cec5SDimitry Andric case Builtin::BI__builtin_strncmp: 43930b57cec5SDimitry Andric case Builtin::BIstrncmp: 43940b57cec5SDimitry Andric return Builtin::BIstrncmp; 43950b57cec5SDimitry Andric 43960b57cec5SDimitry Andric case Builtin::BI__builtin_strncasecmp: 43970b57cec5SDimitry Andric case Builtin::BIstrncasecmp: 43980b57cec5SDimitry Andric return Builtin::BIstrncasecmp; 43990b57cec5SDimitry Andric 44000b57cec5SDimitry Andric case Builtin::BI__builtin_strncat: 44010b57cec5SDimitry Andric case Builtin::BI__builtin___strncat_chk: 44020b57cec5SDimitry Andric case Builtin::BIstrncat: 44030b57cec5SDimitry Andric return Builtin::BIstrncat; 44040b57cec5SDimitry Andric 44050b57cec5SDimitry Andric case Builtin::BI__builtin_strndup: 44060b57cec5SDimitry Andric case Builtin::BIstrndup: 44070b57cec5SDimitry Andric return Builtin::BIstrndup; 44080b57cec5SDimitry Andric 44090b57cec5SDimitry Andric case Builtin::BI__builtin_strlen: 44100b57cec5SDimitry Andric case Builtin::BIstrlen: 44110b57cec5SDimitry Andric return Builtin::BIstrlen; 44120b57cec5SDimitry Andric 44130b57cec5SDimitry Andric case Builtin::BI__builtin_bzero: 44140b57cec5SDimitry Andric case Builtin::BIbzero: 44150b57cec5SDimitry Andric return Builtin::BIbzero; 44160b57cec5SDimitry Andric 44175f757f3fSDimitry Andric case Builtin::BI__builtin_bcopy: 44185f757f3fSDimitry Andric case Builtin::BIbcopy: 44195f757f3fSDimitry Andric return Builtin::BIbcopy; 44205f757f3fSDimitry Andric 4421e8d8bef9SDimitry Andric case Builtin::BIfree: 4422e8d8bef9SDimitry Andric return Builtin::BIfree; 4423e8d8bef9SDimitry Andric 44240b57cec5SDimitry Andric default: 44250b57cec5SDimitry Andric if (isExternC()) { 44260b57cec5SDimitry Andric if (FnInfo->isStr("memset")) 44270b57cec5SDimitry Andric return Builtin::BImemset; 4428e8d8bef9SDimitry Andric if (FnInfo->isStr("memcpy")) 44290b57cec5SDimitry Andric return Builtin::BImemcpy; 4430e8d8bef9SDimitry Andric if (FnInfo->isStr("mempcpy")) 4431480093f4SDimitry Andric return Builtin::BImempcpy; 4432e8d8bef9SDimitry Andric if (FnInfo->isStr("memmove")) 44330b57cec5SDimitry Andric return Builtin::BImemmove; 4434e8d8bef9SDimitry Andric if (FnInfo->isStr("memcmp")) 44350b57cec5SDimitry Andric return Builtin::BImemcmp; 4436e8d8bef9SDimitry Andric if (FnInfo->isStr("bcmp")) 44370b57cec5SDimitry Andric return Builtin::BIbcmp; 4438e8d8bef9SDimitry Andric if (FnInfo->isStr("strncpy")) 44390b57cec5SDimitry Andric return Builtin::BIstrncpy; 4440e8d8bef9SDimitry Andric if (FnInfo->isStr("strncmp")) 44410b57cec5SDimitry Andric return Builtin::BIstrncmp; 4442e8d8bef9SDimitry Andric if (FnInfo->isStr("strncasecmp")) 44430b57cec5SDimitry Andric return Builtin::BIstrncasecmp; 4444e8d8bef9SDimitry Andric if (FnInfo->isStr("strncat")) 44450b57cec5SDimitry Andric return Builtin::BIstrncat; 4446e8d8bef9SDimitry Andric if (FnInfo->isStr("strndup")) 44470b57cec5SDimitry Andric return Builtin::BIstrndup; 4448e8d8bef9SDimitry Andric if (FnInfo->isStr("strlen")) 44490b57cec5SDimitry Andric return Builtin::BIstrlen; 4450e8d8bef9SDimitry Andric if (FnInfo->isStr("bzero")) 44510b57cec5SDimitry Andric return Builtin::BIbzero; 44525f757f3fSDimitry Andric if (FnInfo->isStr("bcopy")) 44535f757f3fSDimitry Andric return Builtin::BIbcopy; 4454e8d8bef9SDimitry Andric } else if (isInStdNamespace()) { 4455e8d8bef9SDimitry Andric if (FnInfo->isStr("free")) 4456e8d8bef9SDimitry Andric return Builtin::BIfree; 44570b57cec5SDimitry Andric } 44580b57cec5SDimitry Andric break; 44590b57cec5SDimitry Andric } 44600b57cec5SDimitry Andric return 0; 44610b57cec5SDimitry Andric } 44620b57cec5SDimitry Andric 44630b57cec5SDimitry Andric unsigned FunctionDecl::getODRHash() const { 44640b57cec5SDimitry Andric assert(hasODRHash()); 44650b57cec5SDimitry Andric return ODRHash; 44660b57cec5SDimitry Andric } 44670b57cec5SDimitry Andric 44680b57cec5SDimitry Andric unsigned FunctionDecl::getODRHash() { 44690b57cec5SDimitry Andric if (hasODRHash()) 44700b57cec5SDimitry Andric return ODRHash; 44710b57cec5SDimitry Andric 44720b57cec5SDimitry Andric if (auto *FT = getInstantiatedFromMemberFunction()) { 44730b57cec5SDimitry Andric setHasODRHash(true); 44740b57cec5SDimitry Andric ODRHash = FT->getODRHash(); 44750b57cec5SDimitry Andric return ODRHash; 44760b57cec5SDimitry Andric } 44770b57cec5SDimitry Andric 44780b57cec5SDimitry Andric class ODRHash Hash; 44790b57cec5SDimitry Andric Hash.AddFunctionDecl(this); 44800b57cec5SDimitry Andric setHasODRHash(true); 44810b57cec5SDimitry Andric ODRHash = Hash.CalculateHash(); 44820b57cec5SDimitry Andric return ODRHash; 44830b57cec5SDimitry Andric } 44840b57cec5SDimitry Andric 44850b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 44860b57cec5SDimitry Andric // FieldDecl Implementation 44870b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 44880b57cec5SDimitry Andric 44890b57cec5SDimitry Andric FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, 44900b57cec5SDimitry Andric SourceLocation StartLoc, SourceLocation IdLoc, 44910b57cec5SDimitry Andric IdentifierInfo *Id, QualType T, 44920b57cec5SDimitry Andric TypeSourceInfo *TInfo, Expr *BW, bool Mutable, 44930b57cec5SDimitry Andric InClassInitStyle InitStyle) { 44940b57cec5SDimitry Andric return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, 44950b57cec5SDimitry Andric BW, Mutable, InitStyle); 44960b57cec5SDimitry Andric } 44970b57cec5SDimitry Andric 44980b57cec5SDimitry Andric FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 44990b57cec5SDimitry Andric return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), 45000b57cec5SDimitry Andric SourceLocation(), nullptr, QualType(), nullptr, 45010b57cec5SDimitry Andric nullptr, false, ICIS_NoInit); 45020b57cec5SDimitry Andric } 45030b57cec5SDimitry Andric 45040b57cec5SDimitry Andric bool FieldDecl::isAnonymousStructOrUnion() const { 45050b57cec5SDimitry Andric if (!isImplicit() || getDeclName()) 45060b57cec5SDimitry Andric return false; 45070b57cec5SDimitry Andric 45080b57cec5SDimitry Andric if (const auto *Record = getType()->getAs<RecordType>()) 45090b57cec5SDimitry Andric return Record->getDecl()->isAnonymousStructOrUnion(); 45100b57cec5SDimitry Andric 45110b57cec5SDimitry Andric return false; 45120b57cec5SDimitry Andric } 45130b57cec5SDimitry Andric 451406c3fb27SDimitry Andric Expr *FieldDecl::getInClassInitializer() const { 451506c3fb27SDimitry Andric if (!hasInClassInitializer()) 451606c3fb27SDimitry Andric return nullptr; 451706c3fb27SDimitry Andric 451806c3fb27SDimitry Andric LazyDeclStmtPtr InitPtr = BitField ? InitAndBitWidth->Init : Init; 45195f757f3fSDimitry Andric return cast_if_present<Expr>( 452006c3fb27SDimitry Andric InitPtr.isOffset() ? InitPtr.get(getASTContext().getExternalSource()) 452106c3fb27SDimitry Andric : InitPtr.get(nullptr)); 452206c3fb27SDimitry Andric } 452306c3fb27SDimitry Andric 452406c3fb27SDimitry Andric void FieldDecl::setInClassInitializer(Expr *NewInit) { 452506c3fb27SDimitry Andric setLazyInClassInitializer(LazyDeclStmtPtr(NewInit)); 452606c3fb27SDimitry Andric } 452706c3fb27SDimitry Andric 452806c3fb27SDimitry Andric void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) { 452906c3fb27SDimitry Andric assert(hasInClassInitializer() && !getInClassInitializer()); 453006c3fb27SDimitry Andric if (BitField) 453106c3fb27SDimitry Andric InitAndBitWidth->Init = NewInit; 453206c3fb27SDimitry Andric else 453306c3fb27SDimitry Andric Init = NewInit; 453406c3fb27SDimitry Andric } 453506c3fb27SDimitry Andric 45360b57cec5SDimitry Andric unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { 45370b57cec5SDimitry Andric assert(isBitField() && "not a bitfield"); 45380b57cec5SDimitry Andric return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue(); 45390b57cec5SDimitry Andric } 45400b57cec5SDimitry Andric 45410b57cec5SDimitry Andric bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const { 45420b57cec5SDimitry Andric return isUnnamedBitfield() && !getBitWidth()->isValueDependent() && 45430b57cec5SDimitry Andric getBitWidthValue(Ctx) == 0; 45440b57cec5SDimitry Andric } 45450b57cec5SDimitry Andric 45460b57cec5SDimitry Andric bool FieldDecl::isZeroSize(const ASTContext &Ctx) const { 45470b57cec5SDimitry Andric if (isZeroLengthBitField(Ctx)) 45480b57cec5SDimitry Andric return true; 45490b57cec5SDimitry Andric 45500b57cec5SDimitry Andric // C++2a [intro.object]p7: 45510b57cec5SDimitry Andric // An object has nonzero size if it 45520b57cec5SDimitry Andric // -- is not a potentially-overlapping subobject, or 45530b57cec5SDimitry Andric if (!hasAttr<NoUniqueAddressAttr>()) 45540b57cec5SDimitry Andric return false; 45550b57cec5SDimitry Andric 45560b57cec5SDimitry Andric // -- is not of class type, or 45570b57cec5SDimitry Andric const auto *RT = getType()->getAs<RecordType>(); 45580b57cec5SDimitry Andric if (!RT) 45590b57cec5SDimitry Andric return false; 45600b57cec5SDimitry Andric const RecordDecl *RD = RT->getDecl()->getDefinition(); 45610b57cec5SDimitry Andric if (!RD) { 45620b57cec5SDimitry Andric assert(isInvalidDecl() && "valid field has incomplete type"); 45630b57cec5SDimitry Andric return false; 45640b57cec5SDimitry Andric } 45650b57cec5SDimitry Andric 45660b57cec5SDimitry Andric // -- [has] virtual member functions or virtual base classes, or 45670b57cec5SDimitry Andric // -- has subobjects of nonzero size or bit-fields of nonzero length 45680b57cec5SDimitry Andric const auto *CXXRD = cast<CXXRecordDecl>(RD); 45690b57cec5SDimitry Andric if (!CXXRD->isEmpty()) 45700b57cec5SDimitry Andric return false; 45710b57cec5SDimitry Andric 45720b57cec5SDimitry Andric // Otherwise, [...] the circumstances under which the object has zero size 45730b57cec5SDimitry Andric // are implementation-defined. 45745f757f3fSDimitry Andric if (!Ctx.getTargetInfo().getCXXABI().isMicrosoft()) 45750b57cec5SDimitry Andric return true; 45765f757f3fSDimitry Andric 45775f757f3fSDimitry Andric // MS ABI: has nonzero size if it is a class type with class type fields, 45785f757f3fSDimitry Andric // whether or not they have nonzero size 45795f757f3fSDimitry Andric return !llvm::any_of(CXXRD->fields(), [](const FieldDecl *Field) { 45805f757f3fSDimitry Andric return Field->getType()->getAs<RecordType>(); 45815f757f3fSDimitry Andric }); 45820b57cec5SDimitry Andric } 45830b57cec5SDimitry Andric 458406c3fb27SDimitry Andric bool FieldDecl::isPotentiallyOverlapping() const { 458506c3fb27SDimitry Andric return hasAttr<NoUniqueAddressAttr>() && getType()->getAsCXXRecordDecl(); 458606c3fb27SDimitry Andric } 458706c3fb27SDimitry Andric 45880b57cec5SDimitry Andric unsigned FieldDecl::getFieldIndex() const { 45890b57cec5SDimitry Andric const FieldDecl *Canonical = getCanonicalDecl(); 45900b57cec5SDimitry Andric if (Canonical != this) 45910b57cec5SDimitry Andric return Canonical->getFieldIndex(); 45920b57cec5SDimitry Andric 45930b57cec5SDimitry Andric if (CachedFieldIndex) return CachedFieldIndex - 1; 45940b57cec5SDimitry Andric 45950b57cec5SDimitry Andric unsigned Index = 0; 45960b57cec5SDimitry Andric const RecordDecl *RD = getParent()->getDefinition(); 45970b57cec5SDimitry Andric assert(RD && "requested index for field of struct with no definition"); 45980b57cec5SDimitry Andric 45990b57cec5SDimitry Andric for (auto *Field : RD->fields()) { 46000b57cec5SDimitry Andric Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; 460106c3fb27SDimitry Andric assert(Field->getCanonicalDecl()->CachedFieldIndex == Index + 1 && 460206c3fb27SDimitry Andric "overflow in field numbering"); 46030b57cec5SDimitry Andric ++Index; 46040b57cec5SDimitry Andric } 46050b57cec5SDimitry Andric 46060b57cec5SDimitry Andric assert(CachedFieldIndex && "failed to find field in parent"); 46070b57cec5SDimitry Andric return CachedFieldIndex - 1; 46080b57cec5SDimitry Andric } 46090b57cec5SDimitry Andric 46100b57cec5SDimitry Andric SourceRange FieldDecl::getSourceRange() const { 46110b57cec5SDimitry Andric const Expr *FinalExpr = getInClassInitializer(); 46120b57cec5SDimitry Andric if (!FinalExpr) 46130b57cec5SDimitry Andric FinalExpr = getBitWidth(); 46140b57cec5SDimitry Andric if (FinalExpr) 46150b57cec5SDimitry Andric return SourceRange(getInnerLocStart(), FinalExpr->getEndLoc()); 46160b57cec5SDimitry Andric return DeclaratorDecl::getSourceRange(); 46170b57cec5SDimitry Andric } 46180b57cec5SDimitry Andric 46190b57cec5SDimitry Andric void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { 46200b57cec5SDimitry Andric assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && 46210b57cec5SDimitry Andric "capturing type in non-lambda or captured record."); 462206c3fb27SDimitry Andric assert(StorageKind == ISK_NoInit && !BitField && 462306c3fb27SDimitry Andric "bit-field or field with default member initializer cannot capture " 462406c3fb27SDimitry Andric "VLA type"); 462506c3fb27SDimitry Andric StorageKind = ISK_CapturedVLAType; 462606c3fb27SDimitry Andric CapturedVLAType = VLAType; 46270b57cec5SDimitry Andric } 46280b57cec5SDimitry Andric 46295f757f3fSDimitry Andric void FieldDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { 46305f757f3fSDimitry Andric // Print unnamed members using name of their type. 46315f757f3fSDimitry Andric if (isAnonymousStructOrUnion()) { 46325f757f3fSDimitry Andric this->getType().print(OS, Policy); 46335f757f3fSDimitry Andric return; 46345f757f3fSDimitry Andric } 46355f757f3fSDimitry Andric // Otherwise, do the normal printing. 46365f757f3fSDimitry Andric DeclaratorDecl::printName(OS, Policy); 46375f757f3fSDimitry Andric } 46385f757f3fSDimitry Andric 46390b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 46400b57cec5SDimitry Andric // TagDecl Implementation 46410b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 46420b57cec5SDimitry Andric 46430b57cec5SDimitry Andric TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, 46440b57cec5SDimitry Andric SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, 46450b57cec5SDimitry Andric SourceLocation StartL) 46460b57cec5SDimitry Andric : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C), 46470b57cec5SDimitry Andric TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) { 46485f757f3fSDimitry Andric assert((DK != Enum || TK == TagTypeKind::Enum) && 46495f757f3fSDimitry Andric "EnumDecl not matched with TagTypeKind::Enum"); 46500b57cec5SDimitry Andric setPreviousDecl(PrevDecl); 46510b57cec5SDimitry Andric setTagKind(TK); 46520b57cec5SDimitry Andric setCompleteDefinition(false); 46530b57cec5SDimitry Andric setBeingDefined(false); 46540b57cec5SDimitry Andric setEmbeddedInDeclarator(false); 46550b57cec5SDimitry Andric setFreeStanding(false); 46560b57cec5SDimitry Andric setCompleteDefinitionRequired(false); 465781ad6265SDimitry Andric TagDeclBits.IsThisDeclarationADemotedDefinition = false; 46580b57cec5SDimitry Andric } 46590b57cec5SDimitry Andric 46600b57cec5SDimitry Andric SourceLocation TagDecl::getOuterLocStart() const { 46610b57cec5SDimitry Andric return getTemplateOrInnerLocStart(this); 46620b57cec5SDimitry Andric } 46630b57cec5SDimitry Andric 46640b57cec5SDimitry Andric SourceRange TagDecl::getSourceRange() const { 46650b57cec5SDimitry Andric SourceLocation RBraceLoc = BraceRange.getEnd(); 46660b57cec5SDimitry Andric SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); 46670b57cec5SDimitry Andric return SourceRange(getOuterLocStart(), E); 46680b57cec5SDimitry Andric } 46690b57cec5SDimitry Andric 46700b57cec5SDimitry Andric TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } 46710b57cec5SDimitry Andric 46720b57cec5SDimitry Andric void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { 46730b57cec5SDimitry Andric TypedefNameDeclOrQualifier = TDD; 46740b57cec5SDimitry Andric if (const Type *T = getTypeForDecl()) { 46750b57cec5SDimitry Andric (void)T; 46760b57cec5SDimitry Andric assert(T->isLinkageValid()); 46770b57cec5SDimitry Andric } 46780b57cec5SDimitry Andric assert(isLinkageValid()); 46790b57cec5SDimitry Andric } 46800b57cec5SDimitry Andric 46810b57cec5SDimitry Andric void TagDecl::startDefinition() { 46820b57cec5SDimitry Andric setBeingDefined(true); 46830b57cec5SDimitry Andric 46840b57cec5SDimitry Andric if (auto *D = dyn_cast<CXXRecordDecl>(this)) { 46850b57cec5SDimitry Andric struct CXXRecordDecl::DefinitionData *Data = 46860b57cec5SDimitry Andric new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); 4687bdd1243dSDimitry Andric for (auto *I : redecls()) 46880b57cec5SDimitry Andric cast<CXXRecordDecl>(I)->DefinitionData = Data; 46890b57cec5SDimitry Andric } 46900b57cec5SDimitry Andric } 46910b57cec5SDimitry Andric 46920b57cec5SDimitry Andric void TagDecl::completeDefinition() { 46930b57cec5SDimitry Andric assert((!isa<CXXRecordDecl>(this) || 46940b57cec5SDimitry Andric cast<CXXRecordDecl>(this)->hasDefinition()) && 46950b57cec5SDimitry Andric "definition completed but not started"); 46960b57cec5SDimitry Andric 46970b57cec5SDimitry Andric setCompleteDefinition(true); 46980b57cec5SDimitry Andric setBeingDefined(false); 46990b57cec5SDimitry Andric 47000b57cec5SDimitry Andric if (ASTMutationListener *L = getASTMutationListener()) 47010b57cec5SDimitry Andric L->CompletedTagDefinition(this); 47020b57cec5SDimitry Andric } 47030b57cec5SDimitry Andric 47040b57cec5SDimitry Andric TagDecl *TagDecl::getDefinition() const { 47050b57cec5SDimitry Andric if (isCompleteDefinition()) 47060b57cec5SDimitry Andric return const_cast<TagDecl *>(this); 47070b57cec5SDimitry Andric 47080b57cec5SDimitry Andric // If it's possible for us to have an out-of-date definition, check now. 47090b57cec5SDimitry Andric if (mayHaveOutOfDateDef()) { 47100b57cec5SDimitry Andric if (IdentifierInfo *II = getIdentifier()) { 47110b57cec5SDimitry Andric if (II->isOutOfDate()) { 47120b57cec5SDimitry Andric updateOutOfDate(*II); 47130b57cec5SDimitry Andric } 47140b57cec5SDimitry Andric } 47150b57cec5SDimitry Andric } 47160b57cec5SDimitry Andric 47170b57cec5SDimitry Andric if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this)) 47180b57cec5SDimitry Andric return CXXRD->getDefinition(); 47190b57cec5SDimitry Andric 4720bdd1243dSDimitry Andric for (auto *R : redecls()) 47210b57cec5SDimitry Andric if (R->isCompleteDefinition()) 47220b57cec5SDimitry Andric return R; 47230b57cec5SDimitry Andric 47240b57cec5SDimitry Andric return nullptr; 47250b57cec5SDimitry Andric } 47260b57cec5SDimitry Andric 47270b57cec5SDimitry Andric void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { 47280b57cec5SDimitry Andric if (QualifierLoc) { 47290b57cec5SDimitry Andric // Make sure the extended qualifier info is allocated. 47300b57cec5SDimitry Andric if (!hasExtInfo()) 47310b57cec5SDimitry Andric TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; 47320b57cec5SDimitry Andric // Set qualifier info. 47330b57cec5SDimitry Andric getExtInfo()->QualifierLoc = QualifierLoc; 47340b57cec5SDimitry Andric } else { 47350b57cec5SDimitry Andric // Here Qualifier == 0, i.e., we are removing the qualifier (if any). 47360b57cec5SDimitry Andric if (hasExtInfo()) { 47370b57cec5SDimitry Andric if (getExtInfo()->NumTemplParamLists == 0) { 47380b57cec5SDimitry Andric getASTContext().Deallocate(getExtInfo()); 47390b57cec5SDimitry Andric TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr; 47400b57cec5SDimitry Andric } 47410b57cec5SDimitry Andric else 47420b57cec5SDimitry Andric getExtInfo()->QualifierLoc = QualifierLoc; 47430b57cec5SDimitry Andric } 47440b57cec5SDimitry Andric } 47450b57cec5SDimitry Andric } 47460b57cec5SDimitry Andric 4747bdd1243dSDimitry Andric void TagDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { 4748bdd1243dSDimitry Andric DeclarationName Name = getDeclName(); 4749bdd1243dSDimitry Andric // If the name is supposed to have an identifier but does not have one, then 4750bdd1243dSDimitry Andric // the tag is anonymous and we should print it differently. 4751bdd1243dSDimitry Andric if (Name.isIdentifier() && !Name.getAsIdentifierInfo()) { 4752bdd1243dSDimitry Andric // If the caller wanted to print a qualified name, they've already printed 4753bdd1243dSDimitry Andric // the scope. And if the caller doesn't want that, the scope information 4754bdd1243dSDimitry Andric // is already printed as part of the type. 4755bdd1243dSDimitry Andric PrintingPolicy Copy(Policy); 4756bdd1243dSDimitry Andric Copy.SuppressScope = true; 4757bdd1243dSDimitry Andric getASTContext().getTagDeclType(this).print(OS, Copy); 4758bdd1243dSDimitry Andric return; 4759bdd1243dSDimitry Andric } 4760bdd1243dSDimitry Andric // Otherwise, do the normal printing. 4761bdd1243dSDimitry Andric Name.print(OS, Policy); 4762bdd1243dSDimitry Andric } 4763bdd1243dSDimitry Andric 47640b57cec5SDimitry Andric void TagDecl::setTemplateParameterListsInfo( 47650b57cec5SDimitry Andric ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { 47660b57cec5SDimitry Andric assert(!TPLists.empty()); 47670b57cec5SDimitry Andric // Make sure the extended decl info is allocated. 47680b57cec5SDimitry Andric if (!hasExtInfo()) 47690b57cec5SDimitry Andric // Allocate external info struct. 47700b57cec5SDimitry Andric TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; 47710b57cec5SDimitry Andric // Set the template parameter lists info. 47720b57cec5SDimitry Andric getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); 47730b57cec5SDimitry Andric } 47740b57cec5SDimitry Andric 47750b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 47760b57cec5SDimitry Andric // EnumDecl Implementation 47770b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 47780b57cec5SDimitry Andric 47790b57cec5SDimitry Andric EnumDecl::EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, 47800b57cec5SDimitry Andric SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, 47810b57cec5SDimitry Andric bool Scoped, bool ScopedUsingClassTag, bool Fixed) 47825f757f3fSDimitry Andric : TagDecl(Enum, TagTypeKind::Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) { 47830b57cec5SDimitry Andric assert(Scoped || !ScopedUsingClassTag); 47840b57cec5SDimitry Andric IntegerType = nullptr; 47850b57cec5SDimitry Andric setNumPositiveBits(0); 47860b57cec5SDimitry Andric setNumNegativeBits(0); 47870b57cec5SDimitry Andric setScoped(Scoped); 47880b57cec5SDimitry Andric setScopedUsingClassTag(ScopedUsingClassTag); 47890b57cec5SDimitry Andric setFixed(Fixed); 47900b57cec5SDimitry Andric setHasODRHash(false); 47910b57cec5SDimitry Andric ODRHash = 0; 47920b57cec5SDimitry Andric } 47930b57cec5SDimitry Andric 47940b57cec5SDimitry Andric void EnumDecl::anchor() {} 47950b57cec5SDimitry Andric 47960b57cec5SDimitry Andric EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, 47970b57cec5SDimitry Andric SourceLocation StartLoc, SourceLocation IdLoc, 47980b57cec5SDimitry Andric IdentifierInfo *Id, 47990b57cec5SDimitry Andric EnumDecl *PrevDecl, bool IsScoped, 48000b57cec5SDimitry Andric bool IsScopedUsingClassTag, bool IsFixed) { 48010b57cec5SDimitry Andric auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, 48020b57cec5SDimitry Andric IsScoped, IsScopedUsingClassTag, IsFixed); 48030b57cec5SDimitry Andric Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); 48040b57cec5SDimitry Andric C.getTypeDeclType(Enum, PrevDecl); 48050b57cec5SDimitry Andric return Enum; 48060b57cec5SDimitry Andric } 48070b57cec5SDimitry Andric 48080b57cec5SDimitry Andric EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 48090b57cec5SDimitry Andric EnumDecl *Enum = 48100b57cec5SDimitry Andric new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), 48110b57cec5SDimitry Andric nullptr, nullptr, false, false, false); 48120b57cec5SDimitry Andric Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); 48130b57cec5SDimitry Andric return Enum; 48140b57cec5SDimitry Andric } 48150b57cec5SDimitry Andric 48160b57cec5SDimitry Andric SourceRange EnumDecl::getIntegerTypeRange() const { 48170b57cec5SDimitry Andric if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) 48180b57cec5SDimitry Andric return TI->getTypeLoc().getSourceRange(); 48190b57cec5SDimitry Andric return SourceRange(); 48200b57cec5SDimitry Andric } 48210b57cec5SDimitry Andric 48220b57cec5SDimitry Andric void EnumDecl::completeDefinition(QualType NewType, 48230b57cec5SDimitry Andric QualType NewPromotionType, 48240b57cec5SDimitry Andric unsigned NumPositiveBits, 48250b57cec5SDimitry Andric unsigned NumNegativeBits) { 48260b57cec5SDimitry Andric assert(!isCompleteDefinition() && "Cannot redefine enums!"); 48270b57cec5SDimitry Andric if (!IntegerType) 48280b57cec5SDimitry Andric IntegerType = NewType.getTypePtr(); 48290b57cec5SDimitry Andric PromotionType = NewPromotionType; 48300b57cec5SDimitry Andric setNumPositiveBits(NumPositiveBits); 48310b57cec5SDimitry Andric setNumNegativeBits(NumNegativeBits); 48320b57cec5SDimitry Andric TagDecl::completeDefinition(); 48330b57cec5SDimitry Andric } 48340b57cec5SDimitry Andric 48350b57cec5SDimitry Andric bool EnumDecl::isClosed() const { 48360b57cec5SDimitry Andric if (const auto *A = getAttr<EnumExtensibilityAttr>()) 48370b57cec5SDimitry Andric return A->getExtensibility() == EnumExtensibilityAttr::Closed; 48380b57cec5SDimitry Andric return true; 48390b57cec5SDimitry Andric } 48400b57cec5SDimitry Andric 48410b57cec5SDimitry Andric bool EnumDecl::isClosedFlag() const { 48420b57cec5SDimitry Andric return isClosed() && hasAttr<FlagEnumAttr>(); 48430b57cec5SDimitry Andric } 48440b57cec5SDimitry Andric 48450b57cec5SDimitry Andric bool EnumDecl::isClosedNonFlag() const { 48460b57cec5SDimitry Andric return isClosed() && !hasAttr<FlagEnumAttr>(); 48470b57cec5SDimitry Andric } 48480b57cec5SDimitry Andric 48490b57cec5SDimitry Andric TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { 48500b57cec5SDimitry Andric if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) 48510b57cec5SDimitry Andric return MSI->getTemplateSpecializationKind(); 48520b57cec5SDimitry Andric 48530b57cec5SDimitry Andric return TSK_Undeclared; 48540b57cec5SDimitry Andric } 48550b57cec5SDimitry Andric 48560b57cec5SDimitry Andric void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, 48570b57cec5SDimitry Andric SourceLocation PointOfInstantiation) { 48580b57cec5SDimitry Andric MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); 48590b57cec5SDimitry Andric assert(MSI && "Not an instantiated member enumeration?"); 48600b57cec5SDimitry Andric MSI->setTemplateSpecializationKind(TSK); 48610b57cec5SDimitry Andric if (TSK != TSK_ExplicitSpecialization && 48620b57cec5SDimitry Andric PointOfInstantiation.isValid() && 48630b57cec5SDimitry Andric MSI->getPointOfInstantiation().isInvalid()) 48640b57cec5SDimitry Andric MSI->setPointOfInstantiation(PointOfInstantiation); 48650b57cec5SDimitry Andric } 48660b57cec5SDimitry Andric 48670b57cec5SDimitry Andric EnumDecl *EnumDecl::getTemplateInstantiationPattern() const { 48680b57cec5SDimitry Andric if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { 48690b57cec5SDimitry Andric if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { 48700b57cec5SDimitry Andric EnumDecl *ED = getInstantiatedFromMemberEnum(); 48710b57cec5SDimitry Andric while (auto *NewED = ED->getInstantiatedFromMemberEnum()) 48720b57cec5SDimitry Andric ED = NewED; 48730b57cec5SDimitry Andric return getDefinitionOrSelf(ED); 48740b57cec5SDimitry Andric } 48750b57cec5SDimitry Andric } 48760b57cec5SDimitry Andric 48770b57cec5SDimitry Andric assert(!isTemplateInstantiation(getTemplateSpecializationKind()) && 48780b57cec5SDimitry Andric "couldn't find pattern for enum instantiation"); 48790b57cec5SDimitry Andric return nullptr; 48800b57cec5SDimitry Andric } 48810b57cec5SDimitry Andric 48820b57cec5SDimitry Andric EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { 48830b57cec5SDimitry Andric if (SpecializationInfo) 48840b57cec5SDimitry Andric return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); 48850b57cec5SDimitry Andric 48860b57cec5SDimitry Andric return nullptr; 48870b57cec5SDimitry Andric } 48880b57cec5SDimitry Andric 48890b57cec5SDimitry Andric void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, 48900b57cec5SDimitry Andric TemplateSpecializationKind TSK) { 48910b57cec5SDimitry Andric assert(!SpecializationInfo && "Member enum is already a specialization"); 48920b57cec5SDimitry Andric SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); 48930b57cec5SDimitry Andric } 48940b57cec5SDimitry Andric 48950b57cec5SDimitry Andric unsigned EnumDecl::getODRHash() { 48960b57cec5SDimitry Andric if (hasODRHash()) 48970b57cec5SDimitry Andric return ODRHash; 48980b57cec5SDimitry Andric 48990b57cec5SDimitry Andric class ODRHash Hash; 49000b57cec5SDimitry Andric Hash.AddEnumDecl(this); 49010b57cec5SDimitry Andric setHasODRHash(true); 49020b57cec5SDimitry Andric ODRHash = Hash.CalculateHash(); 49030b57cec5SDimitry Andric return ODRHash; 49040b57cec5SDimitry Andric } 49050b57cec5SDimitry Andric 4906349cc55cSDimitry Andric SourceRange EnumDecl::getSourceRange() const { 4907349cc55cSDimitry Andric auto Res = TagDecl::getSourceRange(); 4908349cc55cSDimitry Andric // Set end-point to enum-base, e.g. enum foo : ^bar 4909349cc55cSDimitry Andric if (auto *TSI = getIntegerTypeSourceInfo()) { 4910349cc55cSDimitry Andric // TagDecl doesn't know about the enum base. 4911349cc55cSDimitry Andric if (!getBraceRange().getEnd().isValid()) 4912349cc55cSDimitry Andric Res.setEnd(TSI->getTypeLoc().getEndLoc()); 4913349cc55cSDimitry Andric } 4914349cc55cSDimitry Andric return Res; 4915349cc55cSDimitry Andric } 4916349cc55cSDimitry Andric 4917bdd1243dSDimitry Andric void EnumDecl::getValueRange(llvm::APInt &Max, llvm::APInt &Min) const { 4918bdd1243dSDimitry Andric unsigned Bitwidth = getASTContext().getIntWidth(getIntegerType()); 4919bdd1243dSDimitry Andric unsigned NumNegativeBits = getNumNegativeBits(); 4920bdd1243dSDimitry Andric unsigned NumPositiveBits = getNumPositiveBits(); 4921bdd1243dSDimitry Andric 4922bdd1243dSDimitry Andric if (NumNegativeBits) { 4923bdd1243dSDimitry Andric unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1); 4924bdd1243dSDimitry Andric Max = llvm::APInt(Bitwidth, 1) << (NumBits - 1); 4925bdd1243dSDimitry Andric Min = -Max; 4926bdd1243dSDimitry Andric } else { 4927bdd1243dSDimitry Andric Max = llvm::APInt(Bitwidth, 1) << NumPositiveBits; 4928bdd1243dSDimitry Andric Min = llvm::APInt::getZero(Bitwidth); 4929bdd1243dSDimitry Andric } 4930bdd1243dSDimitry Andric } 4931bdd1243dSDimitry Andric 49320b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 49330b57cec5SDimitry Andric // RecordDecl Implementation 49340b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 49350b57cec5SDimitry Andric 49360b57cec5SDimitry Andric RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, 49370b57cec5SDimitry Andric DeclContext *DC, SourceLocation StartLoc, 49380b57cec5SDimitry Andric SourceLocation IdLoc, IdentifierInfo *Id, 49390b57cec5SDimitry Andric RecordDecl *PrevDecl) 49400b57cec5SDimitry Andric : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { 49410b57cec5SDimitry Andric assert(classof(static_cast<Decl *>(this)) && "Invalid Kind!"); 49420b57cec5SDimitry Andric setHasFlexibleArrayMember(false); 49430b57cec5SDimitry Andric setAnonymousStructOrUnion(false); 49440b57cec5SDimitry Andric setHasObjectMember(false); 49450b57cec5SDimitry Andric setHasVolatileMember(false); 49460b57cec5SDimitry Andric setHasLoadedFieldsFromExternalStorage(false); 49470b57cec5SDimitry Andric setNonTrivialToPrimitiveDefaultInitialize(false); 49480b57cec5SDimitry Andric setNonTrivialToPrimitiveCopy(false); 49490b57cec5SDimitry Andric setNonTrivialToPrimitiveDestroy(false); 49500b57cec5SDimitry Andric setHasNonTrivialToPrimitiveDefaultInitializeCUnion(false); 49510b57cec5SDimitry Andric setHasNonTrivialToPrimitiveDestructCUnion(false); 49520b57cec5SDimitry Andric setHasNonTrivialToPrimitiveCopyCUnion(false); 49530b57cec5SDimitry Andric setParamDestroyedInCallee(false); 49545f757f3fSDimitry Andric setArgPassingRestrictions(RecordArgPassingKind::CanPassInRegs); 495581ad6265SDimitry Andric setIsRandomized(false); 4956bdd1243dSDimitry Andric setODRHash(0); 49570b57cec5SDimitry Andric } 49580b57cec5SDimitry Andric 49590b57cec5SDimitry Andric RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, 49600b57cec5SDimitry Andric SourceLocation StartLoc, SourceLocation IdLoc, 49610b57cec5SDimitry Andric IdentifierInfo *Id, RecordDecl* PrevDecl) { 49620b57cec5SDimitry Andric RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, 49630b57cec5SDimitry Andric StartLoc, IdLoc, Id, PrevDecl); 49640b57cec5SDimitry Andric R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); 49650b57cec5SDimitry Andric 49660b57cec5SDimitry Andric C.getTypeDeclType(R, PrevDecl); 49670b57cec5SDimitry Andric return R; 49680b57cec5SDimitry Andric } 49690b57cec5SDimitry Andric 49700b57cec5SDimitry Andric RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { 49715f757f3fSDimitry Andric RecordDecl *R = new (C, ID) 49725f757f3fSDimitry Andric RecordDecl(Record, TagTypeKind::Struct, C, nullptr, SourceLocation(), 49730b57cec5SDimitry Andric SourceLocation(), nullptr, nullptr); 49740b57cec5SDimitry Andric R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); 49750b57cec5SDimitry Andric return R; 49760b57cec5SDimitry Andric } 49770b57cec5SDimitry Andric 49780b57cec5SDimitry Andric bool RecordDecl::isInjectedClassName() const { 49790b57cec5SDimitry Andric return isImplicit() && getDeclName() && getDeclContext()->isRecord() && 49800b57cec5SDimitry Andric cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); 49810b57cec5SDimitry Andric } 49820b57cec5SDimitry Andric 49830b57cec5SDimitry Andric bool RecordDecl::isLambda() const { 49840b57cec5SDimitry Andric if (auto RD = dyn_cast<CXXRecordDecl>(this)) 49850b57cec5SDimitry Andric return RD->isLambda(); 49860b57cec5SDimitry Andric return false; 49870b57cec5SDimitry Andric } 49880b57cec5SDimitry Andric 49890b57cec5SDimitry Andric bool RecordDecl::isCapturedRecord() const { 49900b57cec5SDimitry Andric return hasAttr<CapturedRecordAttr>(); 49910b57cec5SDimitry Andric } 49920b57cec5SDimitry Andric 49930b57cec5SDimitry Andric void RecordDecl::setCapturedRecord() { 49940b57cec5SDimitry Andric addAttr(CapturedRecordAttr::CreateImplicit(getASTContext())); 49950b57cec5SDimitry Andric } 49960b57cec5SDimitry Andric 49975ffd83dbSDimitry Andric bool RecordDecl::isOrContainsUnion() const { 49985ffd83dbSDimitry Andric if (isUnion()) 49995ffd83dbSDimitry Andric return true; 50005ffd83dbSDimitry Andric 50015ffd83dbSDimitry Andric if (const RecordDecl *Def = getDefinition()) { 50025ffd83dbSDimitry Andric for (const FieldDecl *FD : Def->fields()) { 50035ffd83dbSDimitry Andric const RecordType *RT = FD->getType()->getAs<RecordType>(); 50045ffd83dbSDimitry Andric if (RT && RT->getDecl()->isOrContainsUnion()) 50055ffd83dbSDimitry Andric return true; 50065ffd83dbSDimitry Andric } 50075ffd83dbSDimitry Andric } 50085ffd83dbSDimitry Andric 50095ffd83dbSDimitry Andric return false; 50105ffd83dbSDimitry Andric } 50115ffd83dbSDimitry Andric 50120b57cec5SDimitry Andric RecordDecl::field_iterator RecordDecl::field_begin() const { 50130b57cec5SDimitry Andric if (hasExternalLexicalStorage() && !hasLoadedFieldsFromExternalStorage()) 50140b57cec5SDimitry Andric LoadFieldsFromExternalStorage(); 501506c3fb27SDimitry Andric // This is necessary for correctness for C++ with modules. 501606c3fb27SDimitry Andric // FIXME: Come up with a test case that breaks without definition. 501706c3fb27SDimitry Andric if (RecordDecl *D = getDefinition(); D && D != this) 501806c3fb27SDimitry Andric return D->field_begin(); 50190b57cec5SDimitry Andric return field_iterator(decl_iterator(FirstDecl)); 50200b57cec5SDimitry Andric } 50210b57cec5SDimitry Andric 50220b57cec5SDimitry Andric /// completeDefinition - Notes that the definition of this type is now 50230b57cec5SDimitry Andric /// complete. 50240b57cec5SDimitry Andric void RecordDecl::completeDefinition() { 50250b57cec5SDimitry Andric assert(!isCompleteDefinition() && "Cannot redefine record!"); 50260b57cec5SDimitry Andric TagDecl::completeDefinition(); 5027fe6060f1SDimitry Andric 5028fe6060f1SDimitry Andric ASTContext &Ctx = getASTContext(); 5029fe6060f1SDimitry Andric 5030fe6060f1SDimitry Andric // Layouts are dumped when computed, so if we are dumping for all complete 5031fe6060f1SDimitry Andric // types, we need to force usage to get types that wouldn't be used elsewhere. 5032fe6060f1SDimitry Andric if (Ctx.getLangOpts().DumpRecordLayoutsComplete) 5033fe6060f1SDimitry Andric (void)Ctx.getASTRecordLayout(this); 50340b57cec5SDimitry Andric } 50350b57cec5SDimitry Andric 50360b57cec5SDimitry Andric /// isMsStruct - Get whether or not this record uses ms_struct layout. 50370b57cec5SDimitry Andric /// This which can be turned on with an attribute, pragma, or the 50380b57cec5SDimitry Andric /// -mms-bitfields command-line option. 50390b57cec5SDimitry Andric bool RecordDecl::isMsStruct(const ASTContext &C) const { 50400b57cec5SDimitry Andric return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; 50410b57cec5SDimitry Andric } 50420b57cec5SDimitry Andric 504381ad6265SDimitry Andric void RecordDecl::reorderDecls(const SmallVectorImpl<Decl *> &Decls) { 504481ad6265SDimitry Andric std::tie(FirstDecl, LastDecl) = DeclContext::BuildDeclChain(Decls, false); 504581ad6265SDimitry Andric LastDecl->NextInContextAndBits.setPointer(nullptr); 504681ad6265SDimitry Andric setIsRandomized(true); 504781ad6265SDimitry Andric } 504881ad6265SDimitry Andric 50490b57cec5SDimitry Andric void RecordDecl::LoadFieldsFromExternalStorage() const { 50500b57cec5SDimitry Andric ExternalASTSource *Source = getASTContext().getExternalSource(); 50510b57cec5SDimitry Andric assert(hasExternalLexicalStorage() && Source && "No external storage?"); 50520b57cec5SDimitry Andric 50530b57cec5SDimitry Andric // Notify that we have a RecordDecl doing some initialization. 50540b57cec5SDimitry Andric ExternalASTSource::Deserializing TheFields(Source); 50550b57cec5SDimitry Andric 50560b57cec5SDimitry Andric SmallVector<Decl*, 64> Decls; 50570b57cec5SDimitry Andric setHasLoadedFieldsFromExternalStorage(true); 50580b57cec5SDimitry Andric Source->FindExternalLexicalDecls(this, [](Decl::Kind K) { 50590b57cec5SDimitry Andric return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); 50600b57cec5SDimitry Andric }, Decls); 50610b57cec5SDimitry Andric 50620b57cec5SDimitry Andric #ifndef NDEBUG 50630b57cec5SDimitry Andric // Check that all decls we got were FieldDecls. 50640b57cec5SDimitry Andric for (unsigned i=0, e=Decls.size(); i != e; ++i) 50650b57cec5SDimitry Andric assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); 50660b57cec5SDimitry Andric #endif 50670b57cec5SDimitry Andric 50680b57cec5SDimitry Andric if (Decls.empty()) 50690b57cec5SDimitry Andric return; 50700b57cec5SDimitry Andric 507106c3fb27SDimitry Andric auto [ExternalFirst, ExternalLast] = 507206c3fb27SDimitry Andric BuildDeclChain(Decls, 50730b57cec5SDimitry Andric /*FieldsAlreadyLoaded=*/false); 507406c3fb27SDimitry Andric ExternalLast->NextInContextAndBits.setPointer(FirstDecl); 507506c3fb27SDimitry Andric FirstDecl = ExternalFirst; 507606c3fb27SDimitry Andric if (!LastDecl) 507706c3fb27SDimitry Andric LastDecl = ExternalLast; 50780b57cec5SDimitry Andric } 50790b57cec5SDimitry Andric 50800b57cec5SDimitry Andric bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const { 50810b57cec5SDimitry Andric ASTContext &Context = getASTContext(); 50820b57cec5SDimitry Andric const SanitizerMask EnabledAsanMask = Context.getLangOpts().Sanitize.Mask & 50830b57cec5SDimitry Andric (SanitizerKind::Address | SanitizerKind::KernelAddress); 50840b57cec5SDimitry Andric if (!EnabledAsanMask || !Context.getLangOpts().SanitizeAddressFieldPadding) 50850b57cec5SDimitry Andric return false; 5086fe6060f1SDimitry Andric const auto &NoSanitizeList = Context.getNoSanitizeList(); 50870b57cec5SDimitry Andric const auto *CXXRD = dyn_cast<CXXRecordDecl>(this); 50880b57cec5SDimitry Andric // We may be able to relax some of these requirements. 50890b57cec5SDimitry Andric int ReasonToReject = -1; 50900b57cec5SDimitry Andric if (!CXXRD || CXXRD->isExternCContext()) 50910b57cec5SDimitry Andric ReasonToReject = 0; // is not C++. 50920b57cec5SDimitry Andric else if (CXXRD->hasAttr<PackedAttr>()) 50930b57cec5SDimitry Andric ReasonToReject = 1; // is packed. 50940b57cec5SDimitry Andric else if (CXXRD->isUnion()) 50950b57cec5SDimitry Andric ReasonToReject = 2; // is a union. 50960b57cec5SDimitry Andric else if (CXXRD->isTriviallyCopyable()) 50970b57cec5SDimitry Andric ReasonToReject = 3; // is trivially copyable. 50980b57cec5SDimitry Andric else if (CXXRD->hasTrivialDestructor()) 50990b57cec5SDimitry Andric ReasonToReject = 4; // has trivial destructor. 51000b57cec5SDimitry Andric else if (CXXRD->isStandardLayout()) 51010b57cec5SDimitry Andric ReasonToReject = 5; // is standard layout. 5102fe6060f1SDimitry Andric else if (NoSanitizeList.containsLocation(EnabledAsanMask, getLocation(), 51030b57cec5SDimitry Andric "field-padding")) 51045ffd83dbSDimitry Andric ReasonToReject = 6; // is in an excluded file. 5105fe6060f1SDimitry Andric else if (NoSanitizeList.containsType( 5106fe6060f1SDimitry Andric EnabledAsanMask, getQualifiedNameAsString(), "field-padding")) 51075ffd83dbSDimitry Andric ReasonToReject = 7; // The type is excluded. 51080b57cec5SDimitry Andric 51090b57cec5SDimitry Andric if (EmitRemark) { 51100b57cec5SDimitry Andric if (ReasonToReject >= 0) 51110b57cec5SDimitry Andric Context.getDiagnostics().Report( 51120b57cec5SDimitry Andric getLocation(), 51130b57cec5SDimitry Andric diag::remark_sanitize_address_insert_extra_padding_rejected) 51140b57cec5SDimitry Andric << getQualifiedNameAsString() << ReasonToReject; 51150b57cec5SDimitry Andric else 51160b57cec5SDimitry Andric Context.getDiagnostics().Report( 51170b57cec5SDimitry Andric getLocation(), 51180b57cec5SDimitry Andric diag::remark_sanitize_address_insert_extra_padding_accepted) 51190b57cec5SDimitry Andric << getQualifiedNameAsString(); 51200b57cec5SDimitry Andric } 51210b57cec5SDimitry Andric return ReasonToReject < 0; 51220b57cec5SDimitry Andric } 51230b57cec5SDimitry Andric 51240b57cec5SDimitry Andric const FieldDecl *RecordDecl::findFirstNamedDataMember() const { 51250b57cec5SDimitry Andric for (const auto *I : fields()) { 51260b57cec5SDimitry Andric if (I->getIdentifier()) 51270b57cec5SDimitry Andric return I; 51280b57cec5SDimitry Andric 51290b57cec5SDimitry Andric if (const auto *RT = I->getType()->getAs<RecordType>()) 51300b57cec5SDimitry Andric if (const FieldDecl *NamedDataMember = 51310b57cec5SDimitry Andric RT->getDecl()->findFirstNamedDataMember()) 51320b57cec5SDimitry Andric return NamedDataMember; 51330b57cec5SDimitry Andric } 51340b57cec5SDimitry Andric 51350b57cec5SDimitry Andric // We didn't find a named data member. 51360b57cec5SDimitry Andric return nullptr; 51370b57cec5SDimitry Andric } 51380b57cec5SDimitry Andric 5139bdd1243dSDimitry Andric unsigned RecordDecl::getODRHash() { 5140bdd1243dSDimitry Andric if (hasODRHash()) 5141bdd1243dSDimitry Andric return RecordDeclBits.ODRHash; 5142bdd1243dSDimitry Andric 5143bdd1243dSDimitry Andric // Only calculate hash on first call of getODRHash per record. 5144bdd1243dSDimitry Andric ODRHash Hash; 5145bdd1243dSDimitry Andric Hash.AddRecordDecl(this); 5146bdd1243dSDimitry Andric // For RecordDecl the ODRHash is stored in the remaining 26 5147bdd1243dSDimitry Andric // bit of RecordDeclBits, adjust the hash to accomodate. 5148bdd1243dSDimitry Andric setODRHash(Hash.CalculateHash() >> 6); 5149bdd1243dSDimitry Andric return RecordDeclBits.ODRHash; 5150bdd1243dSDimitry Andric } 5151bdd1243dSDimitry Andric 51520b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 51530b57cec5SDimitry Andric // BlockDecl Implementation 51540b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 51550b57cec5SDimitry Andric 51560b57cec5SDimitry Andric BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc) 51570b57cec5SDimitry Andric : Decl(Block, DC, CaretLoc), DeclContext(Block) { 51580b57cec5SDimitry Andric setIsVariadic(false); 51590b57cec5SDimitry Andric setCapturesCXXThis(false); 51600b57cec5SDimitry Andric setBlockMissingReturnType(true); 51610b57cec5SDimitry Andric setIsConversionFromLambda(false); 51620b57cec5SDimitry Andric setDoesNotEscape(false); 51630b57cec5SDimitry Andric setCanAvoidCopyToHeap(false); 51640b57cec5SDimitry Andric } 51650b57cec5SDimitry Andric 51660b57cec5SDimitry Andric void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { 51670b57cec5SDimitry Andric assert(!ParamInfo && "Already has param info!"); 51680b57cec5SDimitry Andric 51690b57cec5SDimitry Andric // Zero params -> null pointer. 51700b57cec5SDimitry Andric if (!NewParamInfo.empty()) { 51710b57cec5SDimitry Andric NumParams = NewParamInfo.size(); 51720b57cec5SDimitry Andric ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; 51730b57cec5SDimitry Andric std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); 51740b57cec5SDimitry Andric } 51750b57cec5SDimitry Andric } 51760b57cec5SDimitry Andric 51770b57cec5SDimitry Andric void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, 51780b57cec5SDimitry Andric bool CapturesCXXThis) { 51790b57cec5SDimitry Andric this->setCapturesCXXThis(CapturesCXXThis); 51800b57cec5SDimitry Andric this->NumCaptures = Captures.size(); 51810b57cec5SDimitry Andric 51820b57cec5SDimitry Andric if (Captures.empty()) { 51830b57cec5SDimitry Andric this->Captures = nullptr; 51840b57cec5SDimitry Andric return; 51850b57cec5SDimitry Andric } 51860b57cec5SDimitry Andric 51870b57cec5SDimitry Andric this->Captures = Captures.copy(Context).data(); 51880b57cec5SDimitry Andric } 51890b57cec5SDimitry Andric 51900b57cec5SDimitry Andric bool BlockDecl::capturesVariable(const VarDecl *variable) const { 51910b57cec5SDimitry Andric for (const auto &I : captures()) 51920b57cec5SDimitry Andric // Only auto vars can be captured, so no redeclaration worries. 51930b57cec5SDimitry Andric if (I.getVariable() == variable) 51940b57cec5SDimitry Andric return true; 51950b57cec5SDimitry Andric 51960b57cec5SDimitry Andric return false; 51970b57cec5SDimitry Andric } 51980b57cec5SDimitry Andric 51990b57cec5SDimitry Andric SourceRange BlockDecl::getSourceRange() const { 52000b57cec5SDimitry Andric return SourceRange(getLocation(), Body ? Body->getEndLoc() : getLocation()); 52010b57cec5SDimitry Andric } 52020b57cec5SDimitry Andric 52030b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 52040b57cec5SDimitry Andric // Other Decl Allocation/Deallocation Method Implementations 52050b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 52060b57cec5SDimitry Andric 52070b57cec5SDimitry Andric void TranslationUnitDecl::anchor() {} 52080b57cec5SDimitry Andric 52090b57cec5SDimitry Andric TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { 52100b57cec5SDimitry Andric return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); 52110b57cec5SDimitry Andric } 52120b57cec5SDimitry Andric 52130b57cec5SDimitry Andric void PragmaCommentDecl::anchor() {} 52140b57cec5SDimitry Andric 52150b57cec5SDimitry Andric PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C, 52160b57cec5SDimitry Andric TranslationUnitDecl *DC, 52170b57cec5SDimitry Andric SourceLocation CommentLoc, 52180b57cec5SDimitry Andric PragmaMSCommentKind CommentKind, 52190b57cec5SDimitry Andric StringRef Arg) { 52200b57cec5SDimitry Andric PragmaCommentDecl *PCD = 52210b57cec5SDimitry Andric new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1)) 52220b57cec5SDimitry Andric PragmaCommentDecl(DC, CommentLoc, CommentKind); 52230b57cec5SDimitry Andric memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size()); 52240b57cec5SDimitry Andric PCD->getTrailingObjects<char>()[Arg.size()] = '\0'; 52250b57cec5SDimitry Andric return PCD; 52260b57cec5SDimitry Andric } 52270b57cec5SDimitry Andric 52280b57cec5SDimitry Andric PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C, 52290b57cec5SDimitry Andric unsigned ID, 52300b57cec5SDimitry Andric unsigned ArgSize) { 52310b57cec5SDimitry Andric return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1)) 52320b57cec5SDimitry Andric PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown); 52330b57cec5SDimitry Andric } 52340b57cec5SDimitry Andric 52350b57cec5SDimitry Andric void PragmaDetectMismatchDecl::anchor() {} 52360b57cec5SDimitry Andric 52370b57cec5SDimitry Andric PragmaDetectMismatchDecl * 52380b57cec5SDimitry Andric PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC, 52390b57cec5SDimitry Andric SourceLocation Loc, StringRef Name, 52400b57cec5SDimitry Andric StringRef Value) { 52410b57cec5SDimitry Andric size_t ValueStart = Name.size() + 1; 52420b57cec5SDimitry Andric PragmaDetectMismatchDecl *PDMD = 52430b57cec5SDimitry Andric new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1)) 52440b57cec5SDimitry Andric PragmaDetectMismatchDecl(DC, Loc, ValueStart); 52450b57cec5SDimitry Andric memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size()); 52460b57cec5SDimitry Andric PDMD->getTrailingObjects<char>()[Name.size()] = '\0'; 52470b57cec5SDimitry Andric memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(), 52480b57cec5SDimitry Andric Value.size()); 52490b57cec5SDimitry Andric PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0'; 52500b57cec5SDimitry Andric return PDMD; 52510b57cec5SDimitry Andric } 52520b57cec5SDimitry Andric 52530b57cec5SDimitry Andric PragmaDetectMismatchDecl * 52540b57cec5SDimitry Andric PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID, 52550b57cec5SDimitry Andric unsigned NameValueSize) { 52560b57cec5SDimitry Andric return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1)) 52570b57cec5SDimitry Andric PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0); 52580b57cec5SDimitry Andric } 52590b57cec5SDimitry Andric 52600b57cec5SDimitry Andric void ExternCContextDecl::anchor() {} 52610b57cec5SDimitry Andric 52620b57cec5SDimitry Andric ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, 52630b57cec5SDimitry Andric TranslationUnitDecl *DC) { 52640b57cec5SDimitry Andric return new (C, DC) ExternCContextDecl(DC); 52650b57cec5SDimitry Andric } 52660b57cec5SDimitry Andric 52670b57cec5SDimitry Andric void LabelDecl::anchor() {} 52680b57cec5SDimitry Andric 52690b57cec5SDimitry Andric LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, 52700b57cec5SDimitry Andric SourceLocation IdentL, IdentifierInfo *II) { 52710b57cec5SDimitry Andric return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); 52720b57cec5SDimitry Andric } 52730b57cec5SDimitry Andric 52740b57cec5SDimitry Andric LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, 52750b57cec5SDimitry Andric SourceLocation IdentL, IdentifierInfo *II, 52760b57cec5SDimitry Andric SourceLocation GnuLabelL) { 52770b57cec5SDimitry Andric assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); 52780b57cec5SDimitry Andric return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); 52790b57cec5SDimitry Andric } 52800b57cec5SDimitry Andric 52810b57cec5SDimitry Andric LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 52820b57cec5SDimitry Andric return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, 52830b57cec5SDimitry Andric SourceLocation()); 52840b57cec5SDimitry Andric } 52850b57cec5SDimitry Andric 52860b57cec5SDimitry Andric void LabelDecl::setMSAsmLabel(StringRef Name) { 52870b57cec5SDimitry Andric char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; 52880b57cec5SDimitry Andric memcpy(Buffer, Name.data(), Name.size()); 52890b57cec5SDimitry Andric Buffer[Name.size()] = '\0'; 52900b57cec5SDimitry Andric MSAsmName = Buffer; 52910b57cec5SDimitry Andric } 52920b57cec5SDimitry Andric 52930b57cec5SDimitry Andric void ValueDecl::anchor() {} 52940b57cec5SDimitry Andric 52950b57cec5SDimitry Andric bool ValueDecl::isWeak() const { 5296e8d8bef9SDimitry Andric auto *MostRecent = getMostRecentDecl(); 5297e8d8bef9SDimitry Andric return MostRecent->hasAttr<WeakAttr>() || 5298e8d8bef9SDimitry Andric MostRecent->hasAttr<WeakRefAttr>() || isWeakImported(); 52990b57cec5SDimitry Andric } 53000b57cec5SDimitry Andric 5301bdd1243dSDimitry Andric bool ValueDecl::isInitCapture() const { 5302bdd1243dSDimitry Andric if (auto *Var = llvm::dyn_cast<VarDecl>(this)) 5303bdd1243dSDimitry Andric return Var->isInitCapture(); 5304bdd1243dSDimitry Andric return false; 5305bdd1243dSDimitry Andric } 5306bdd1243dSDimitry Andric 53070b57cec5SDimitry Andric void ImplicitParamDecl::anchor() {} 53080b57cec5SDimitry Andric 53090b57cec5SDimitry Andric ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, 53100b57cec5SDimitry Andric SourceLocation IdLoc, 53110b57cec5SDimitry Andric IdentifierInfo *Id, QualType Type, 53120b57cec5SDimitry Andric ImplicitParamKind ParamKind) { 53130b57cec5SDimitry Andric return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind); 53140b57cec5SDimitry Andric } 53150b57cec5SDimitry Andric 53160b57cec5SDimitry Andric ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type, 53170b57cec5SDimitry Andric ImplicitParamKind ParamKind) { 53180b57cec5SDimitry Andric return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind); 53190b57cec5SDimitry Andric } 53200b57cec5SDimitry Andric 53210b57cec5SDimitry Andric ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, 53220b57cec5SDimitry Andric unsigned ID) { 53230b57cec5SDimitry Andric return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other); 53240b57cec5SDimitry Andric } 53250b57cec5SDimitry Andric 5326349cc55cSDimitry Andric FunctionDecl * 5327349cc55cSDimitry Andric FunctionDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, 5328349cc55cSDimitry Andric const DeclarationNameInfo &NameInfo, QualType T, 5329349cc55cSDimitry Andric TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin, 5330349cc55cSDimitry Andric bool isInlineSpecified, bool hasWrittenPrototype, 5331480093f4SDimitry Andric ConstexprSpecKind ConstexprKind, 5332480093f4SDimitry Andric Expr *TrailingRequiresClause) { 5333349cc55cSDimitry Andric FunctionDecl *New = new (C, DC) FunctionDecl( 5334349cc55cSDimitry Andric Function, C, DC, StartLoc, NameInfo, T, TInfo, SC, UsesFPIntrin, 5335349cc55cSDimitry Andric isInlineSpecified, ConstexprKind, TrailingRequiresClause); 53360b57cec5SDimitry Andric New->setHasWrittenPrototype(hasWrittenPrototype); 53370b57cec5SDimitry Andric return New; 53380b57cec5SDimitry Andric } 53390b57cec5SDimitry Andric 53400b57cec5SDimitry Andric FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 5341e8d8bef9SDimitry Andric return new (C, ID) FunctionDecl( 5342e8d8bef9SDimitry Andric Function, C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), 5343349cc55cSDimitry Andric nullptr, SC_None, false, false, ConstexprSpecKind::Unspecified, nullptr); 53440b57cec5SDimitry Andric } 53450b57cec5SDimitry Andric 53460b57cec5SDimitry Andric BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { 53470b57cec5SDimitry Andric return new (C, DC) BlockDecl(DC, L); 53480b57cec5SDimitry Andric } 53490b57cec5SDimitry Andric 53500b57cec5SDimitry Andric BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 53510b57cec5SDimitry Andric return new (C, ID) BlockDecl(nullptr, SourceLocation()); 53520b57cec5SDimitry Andric } 53530b57cec5SDimitry Andric 53540b57cec5SDimitry Andric CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams) 53550b57cec5SDimitry Andric : Decl(Captured, DC, SourceLocation()), DeclContext(Captured), 53560b57cec5SDimitry Andric NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {} 53570b57cec5SDimitry Andric 53580b57cec5SDimitry Andric CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, 53590b57cec5SDimitry Andric unsigned NumParams) { 53600b57cec5SDimitry Andric return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) 53610b57cec5SDimitry Andric CapturedDecl(DC, NumParams); 53620b57cec5SDimitry Andric } 53630b57cec5SDimitry Andric 53640b57cec5SDimitry Andric CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID, 53650b57cec5SDimitry Andric unsigned NumParams) { 53660b57cec5SDimitry Andric return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) 53670b57cec5SDimitry Andric CapturedDecl(nullptr, NumParams); 53680b57cec5SDimitry Andric } 53690b57cec5SDimitry Andric 53700b57cec5SDimitry Andric Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); } 53710b57cec5SDimitry Andric void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); } 53720b57cec5SDimitry Andric 53730b57cec5SDimitry Andric bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); } 53740b57cec5SDimitry Andric void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); } 53750b57cec5SDimitry Andric 5376*7a6dacacSDimitry Andric EnumConstantDecl::EnumConstantDecl(const ASTContext &C, DeclContext *DC, 5377*7a6dacacSDimitry Andric SourceLocation L, IdentifierInfo *Id, 5378*7a6dacacSDimitry Andric QualType T, Expr *E, const llvm::APSInt &V) 5379*7a6dacacSDimitry Andric : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt *)E) { 5380*7a6dacacSDimitry Andric setInitVal(C, V); 5381*7a6dacacSDimitry Andric } 5382*7a6dacacSDimitry Andric 53830b57cec5SDimitry Andric EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, 53840b57cec5SDimitry Andric SourceLocation L, 53850b57cec5SDimitry Andric IdentifierInfo *Id, QualType T, 53860b57cec5SDimitry Andric Expr *E, const llvm::APSInt &V) { 5387*7a6dacacSDimitry Andric return new (C, CD) EnumConstantDecl(C, CD, L, Id, T, E, V); 53880b57cec5SDimitry Andric } 53890b57cec5SDimitry Andric 53900b57cec5SDimitry Andric EnumConstantDecl * 53910b57cec5SDimitry Andric EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 5392*7a6dacacSDimitry Andric return new (C, ID) EnumConstantDecl(C, nullptr, SourceLocation(), nullptr, 53930b57cec5SDimitry Andric QualType(), nullptr, llvm::APSInt()); 53940b57cec5SDimitry Andric } 53950b57cec5SDimitry Andric 53960b57cec5SDimitry Andric void IndirectFieldDecl::anchor() {} 53970b57cec5SDimitry Andric 53980b57cec5SDimitry Andric IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC, 53990b57cec5SDimitry Andric SourceLocation L, DeclarationName N, 54000b57cec5SDimitry Andric QualType T, 54010b57cec5SDimitry Andric MutableArrayRef<NamedDecl *> CH) 54020b57cec5SDimitry Andric : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()), 54030b57cec5SDimitry Andric ChainingSize(CH.size()) { 54040b57cec5SDimitry Andric // In C++, indirect field declarations conflict with tag declarations in the 54050b57cec5SDimitry Andric // same scope, so add them to IDNS_Tag so that tag redeclaration finds them. 54060b57cec5SDimitry Andric if (C.getLangOpts().CPlusPlus) 54070b57cec5SDimitry Andric IdentifierNamespace |= IDNS_Tag; 54080b57cec5SDimitry Andric } 54090b57cec5SDimitry Andric 54100b57cec5SDimitry Andric IndirectFieldDecl * 54110b57cec5SDimitry Andric IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 54120b57cec5SDimitry Andric IdentifierInfo *Id, QualType T, 54130b57cec5SDimitry Andric llvm::MutableArrayRef<NamedDecl *> CH) { 54140b57cec5SDimitry Andric return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH); 54150b57cec5SDimitry Andric } 54160b57cec5SDimitry Andric 54170b57cec5SDimitry Andric IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, 54180b57cec5SDimitry Andric unsigned ID) { 5419bdd1243dSDimitry Andric return new (C, ID) 5420bdd1243dSDimitry Andric IndirectFieldDecl(C, nullptr, SourceLocation(), DeclarationName(), 5421bdd1243dSDimitry Andric QualType(), std::nullopt); 54220b57cec5SDimitry Andric } 54230b57cec5SDimitry Andric 54240b57cec5SDimitry Andric SourceRange EnumConstantDecl::getSourceRange() const { 54250b57cec5SDimitry Andric SourceLocation End = getLocation(); 54260b57cec5SDimitry Andric if (Init) 54270b57cec5SDimitry Andric End = Init->getEndLoc(); 54280b57cec5SDimitry Andric return SourceRange(getLocation(), End); 54290b57cec5SDimitry Andric } 54300b57cec5SDimitry Andric 54310b57cec5SDimitry Andric void TypeDecl::anchor() {} 54320b57cec5SDimitry Andric 54330b57cec5SDimitry Andric TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, 54340b57cec5SDimitry Andric SourceLocation StartLoc, SourceLocation IdLoc, 54350b57cec5SDimitry Andric IdentifierInfo *Id, TypeSourceInfo *TInfo) { 54360b57cec5SDimitry Andric return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); 54370b57cec5SDimitry Andric } 54380b57cec5SDimitry Andric 54390b57cec5SDimitry Andric void TypedefNameDecl::anchor() {} 54400b57cec5SDimitry Andric 54410b57cec5SDimitry Andric TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const { 54420b57cec5SDimitry Andric if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) { 54430b57cec5SDimitry Andric auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl(); 54440b57cec5SDimitry Andric auto *ThisTypedef = this; 54450b57cec5SDimitry Andric if (AnyRedecl && OwningTypedef) { 54460b57cec5SDimitry Andric OwningTypedef = OwningTypedef->getCanonicalDecl(); 54470b57cec5SDimitry Andric ThisTypedef = ThisTypedef->getCanonicalDecl(); 54480b57cec5SDimitry Andric } 54490b57cec5SDimitry Andric if (OwningTypedef == ThisTypedef) 54500b57cec5SDimitry Andric return TT->getDecl(); 54510b57cec5SDimitry Andric } 54520b57cec5SDimitry Andric 54530b57cec5SDimitry Andric return nullptr; 54540b57cec5SDimitry Andric } 54550b57cec5SDimitry Andric 54560b57cec5SDimitry Andric bool TypedefNameDecl::isTransparentTagSlow() const { 54570b57cec5SDimitry Andric auto determineIsTransparent = [&]() { 54580b57cec5SDimitry Andric if (auto *TT = getUnderlyingType()->getAs<TagType>()) { 54590b57cec5SDimitry Andric if (auto *TD = TT->getDecl()) { 54600b57cec5SDimitry Andric if (TD->getName() != getName()) 54610b57cec5SDimitry Andric return false; 54620b57cec5SDimitry Andric SourceLocation TTLoc = getLocation(); 54630b57cec5SDimitry Andric SourceLocation TDLoc = TD->getLocation(); 54640b57cec5SDimitry Andric if (!TTLoc.isMacroID() || !TDLoc.isMacroID()) 54650b57cec5SDimitry Andric return false; 54660b57cec5SDimitry Andric SourceManager &SM = getASTContext().getSourceManager(); 54670b57cec5SDimitry Andric return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc); 54680b57cec5SDimitry Andric } 54690b57cec5SDimitry Andric } 54700b57cec5SDimitry Andric return false; 54710b57cec5SDimitry Andric }; 54720b57cec5SDimitry Andric 54730b57cec5SDimitry Andric bool isTransparent = determineIsTransparent(); 54740b57cec5SDimitry Andric MaybeModedTInfo.setInt((isTransparent << 1) | 1); 54750b57cec5SDimitry Andric return isTransparent; 54760b57cec5SDimitry Andric } 54770b57cec5SDimitry Andric 54780b57cec5SDimitry Andric TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 54790b57cec5SDimitry Andric return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), 54800b57cec5SDimitry Andric nullptr, nullptr); 54810b57cec5SDimitry Andric } 54820b57cec5SDimitry Andric 54830b57cec5SDimitry Andric TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, 54840b57cec5SDimitry Andric SourceLocation StartLoc, 54850b57cec5SDimitry Andric SourceLocation IdLoc, IdentifierInfo *Id, 54860b57cec5SDimitry Andric TypeSourceInfo *TInfo) { 54870b57cec5SDimitry Andric return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); 54880b57cec5SDimitry Andric } 54890b57cec5SDimitry Andric 54900b57cec5SDimitry Andric TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 54910b57cec5SDimitry Andric return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), 54920b57cec5SDimitry Andric SourceLocation(), nullptr, nullptr); 54930b57cec5SDimitry Andric } 54940b57cec5SDimitry Andric 54950b57cec5SDimitry Andric SourceRange TypedefDecl::getSourceRange() const { 54960b57cec5SDimitry Andric SourceLocation RangeEnd = getLocation(); 54970b57cec5SDimitry Andric if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { 54980b57cec5SDimitry Andric if (typeIsPostfix(TInfo->getType())) 54990b57cec5SDimitry Andric RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 55000b57cec5SDimitry Andric } 55010b57cec5SDimitry Andric return SourceRange(getBeginLoc(), RangeEnd); 55020b57cec5SDimitry Andric } 55030b57cec5SDimitry Andric 55040b57cec5SDimitry Andric SourceRange TypeAliasDecl::getSourceRange() const { 55050b57cec5SDimitry Andric SourceLocation RangeEnd = getBeginLoc(); 55060b57cec5SDimitry Andric if (TypeSourceInfo *TInfo = getTypeSourceInfo()) 55070b57cec5SDimitry Andric RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); 55080b57cec5SDimitry Andric return SourceRange(getBeginLoc(), RangeEnd); 55090b57cec5SDimitry Andric } 55100b57cec5SDimitry Andric 55110b57cec5SDimitry Andric void FileScopeAsmDecl::anchor() {} 55120b57cec5SDimitry Andric 55130b57cec5SDimitry Andric FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, 55140b57cec5SDimitry Andric StringLiteral *Str, 55150b57cec5SDimitry Andric SourceLocation AsmLoc, 55160b57cec5SDimitry Andric SourceLocation RParenLoc) { 55170b57cec5SDimitry Andric return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); 55180b57cec5SDimitry Andric } 55190b57cec5SDimitry Andric 55200b57cec5SDimitry Andric FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, 55210b57cec5SDimitry Andric unsigned ID) { 55220b57cec5SDimitry Andric return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), 55230b57cec5SDimitry Andric SourceLocation()); 55240b57cec5SDimitry Andric } 55250b57cec5SDimitry Andric 5526bdd1243dSDimitry Andric void TopLevelStmtDecl::anchor() {} 5527bdd1243dSDimitry Andric 5528bdd1243dSDimitry Andric TopLevelStmtDecl *TopLevelStmtDecl::Create(ASTContext &C, Stmt *Statement) { 5529bdd1243dSDimitry Andric assert(Statement); 5530bdd1243dSDimitry Andric assert(C.getLangOpts().IncrementalExtensions && 5531bdd1243dSDimitry Andric "Must be used only in incremental mode"); 5532bdd1243dSDimitry Andric 5533bdd1243dSDimitry Andric SourceLocation BeginLoc = Statement->getBeginLoc(); 5534bdd1243dSDimitry Andric DeclContext *DC = C.getTranslationUnitDecl(); 5535bdd1243dSDimitry Andric 5536bdd1243dSDimitry Andric return new (C, DC) TopLevelStmtDecl(DC, BeginLoc, Statement); 5537bdd1243dSDimitry Andric } 5538bdd1243dSDimitry Andric 5539bdd1243dSDimitry Andric TopLevelStmtDecl *TopLevelStmtDecl::CreateDeserialized(ASTContext &C, 5540bdd1243dSDimitry Andric unsigned ID) { 5541bdd1243dSDimitry Andric return new (C, ID) 5542bdd1243dSDimitry Andric TopLevelStmtDecl(/*DC=*/nullptr, SourceLocation(), /*S=*/nullptr); 5543bdd1243dSDimitry Andric } 5544bdd1243dSDimitry Andric 5545bdd1243dSDimitry Andric SourceRange TopLevelStmtDecl::getSourceRange() const { 5546bdd1243dSDimitry Andric return SourceRange(getLocation(), Statement->getEndLoc()); 5547bdd1243dSDimitry Andric } 5548bdd1243dSDimitry Andric 55490b57cec5SDimitry Andric void EmptyDecl::anchor() {} 55500b57cec5SDimitry Andric 55510b57cec5SDimitry Andric EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { 55520b57cec5SDimitry Andric return new (C, DC) EmptyDecl(DC, L); 55530b57cec5SDimitry Andric } 55540b57cec5SDimitry Andric 55550b57cec5SDimitry Andric EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 55560b57cec5SDimitry Andric return new (C, ID) EmptyDecl(nullptr, SourceLocation()); 55570b57cec5SDimitry Andric } 55580b57cec5SDimitry Andric 5559bdd1243dSDimitry Andric HLSLBufferDecl::HLSLBufferDecl(DeclContext *DC, bool CBuffer, 5560bdd1243dSDimitry Andric SourceLocation KwLoc, IdentifierInfo *ID, 5561bdd1243dSDimitry Andric SourceLocation IDLoc, SourceLocation LBrace) 5562bdd1243dSDimitry Andric : NamedDecl(Decl::Kind::HLSLBuffer, DC, IDLoc, DeclarationName(ID)), 5563bdd1243dSDimitry Andric DeclContext(Decl::Kind::HLSLBuffer), LBraceLoc(LBrace), KwLoc(KwLoc), 5564bdd1243dSDimitry Andric IsCBuffer(CBuffer) {} 5565bdd1243dSDimitry Andric 5566bdd1243dSDimitry Andric HLSLBufferDecl *HLSLBufferDecl::Create(ASTContext &C, 5567bdd1243dSDimitry Andric DeclContext *LexicalParent, bool CBuffer, 5568bdd1243dSDimitry Andric SourceLocation KwLoc, IdentifierInfo *ID, 5569bdd1243dSDimitry Andric SourceLocation IDLoc, 5570bdd1243dSDimitry Andric SourceLocation LBrace) { 5571bdd1243dSDimitry Andric // For hlsl like this 5572bdd1243dSDimitry Andric // cbuffer A { 5573bdd1243dSDimitry Andric // cbuffer B { 5574bdd1243dSDimitry Andric // } 5575bdd1243dSDimitry Andric // } 5576bdd1243dSDimitry Andric // compiler should treat it as 5577bdd1243dSDimitry Andric // cbuffer A { 5578bdd1243dSDimitry Andric // } 5579bdd1243dSDimitry Andric // cbuffer B { 5580bdd1243dSDimitry Andric // } 5581bdd1243dSDimitry Andric // FIXME: support nested buffers if required for back-compat. 5582bdd1243dSDimitry Andric DeclContext *DC = LexicalParent; 5583bdd1243dSDimitry Andric HLSLBufferDecl *Result = 5584bdd1243dSDimitry Andric new (C, DC) HLSLBufferDecl(DC, CBuffer, KwLoc, ID, IDLoc, LBrace); 5585bdd1243dSDimitry Andric return Result; 5586bdd1243dSDimitry Andric } 5587bdd1243dSDimitry Andric 5588bdd1243dSDimitry Andric HLSLBufferDecl *HLSLBufferDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 5589bdd1243dSDimitry Andric return new (C, ID) HLSLBufferDecl(nullptr, false, SourceLocation(), nullptr, 5590bdd1243dSDimitry Andric SourceLocation(), SourceLocation()); 5591bdd1243dSDimitry Andric } 5592bdd1243dSDimitry Andric 55930b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 55940b57cec5SDimitry Andric // ImportDecl Implementation 55950b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 55960b57cec5SDimitry Andric 55970b57cec5SDimitry Andric /// Retrieve the number of module identifiers needed to name the given 55980b57cec5SDimitry Andric /// module. 55990b57cec5SDimitry Andric static unsigned getNumModuleIdentifiers(Module *Mod) { 56000b57cec5SDimitry Andric unsigned Result = 1; 56010b57cec5SDimitry Andric while (Mod->Parent) { 56020b57cec5SDimitry Andric Mod = Mod->Parent; 56030b57cec5SDimitry Andric ++Result; 56040b57cec5SDimitry Andric } 56050b57cec5SDimitry Andric return Result; 56060b57cec5SDimitry Andric } 56070b57cec5SDimitry Andric 56080b57cec5SDimitry Andric ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, 56090b57cec5SDimitry Andric Module *Imported, 56100b57cec5SDimitry Andric ArrayRef<SourceLocation> IdentifierLocs) 56115ffd83dbSDimitry Andric : Decl(Import, DC, StartLoc), ImportedModule(Imported), 56125ffd83dbSDimitry Andric NextLocalImportAndComplete(nullptr, true) { 56130b57cec5SDimitry Andric assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); 56140b57cec5SDimitry Andric auto *StoredLocs = getTrailingObjects<SourceLocation>(); 56150b57cec5SDimitry Andric std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(), 56160b57cec5SDimitry Andric StoredLocs); 56170b57cec5SDimitry Andric } 56180b57cec5SDimitry Andric 56190b57cec5SDimitry Andric ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, 56200b57cec5SDimitry Andric Module *Imported, SourceLocation EndLoc) 56215ffd83dbSDimitry Andric : Decl(Import, DC, StartLoc), ImportedModule(Imported), 56225ffd83dbSDimitry Andric NextLocalImportAndComplete(nullptr, false) { 56230b57cec5SDimitry Andric *getTrailingObjects<SourceLocation>() = EndLoc; 56240b57cec5SDimitry Andric } 56250b57cec5SDimitry Andric 56260b57cec5SDimitry Andric ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, 56270b57cec5SDimitry Andric SourceLocation StartLoc, Module *Imported, 56280b57cec5SDimitry Andric ArrayRef<SourceLocation> IdentifierLocs) { 56290b57cec5SDimitry Andric return new (C, DC, 56300b57cec5SDimitry Andric additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size())) 56310b57cec5SDimitry Andric ImportDecl(DC, StartLoc, Imported, IdentifierLocs); 56320b57cec5SDimitry Andric } 56330b57cec5SDimitry Andric 56340b57cec5SDimitry Andric ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, 56350b57cec5SDimitry Andric SourceLocation StartLoc, 56360b57cec5SDimitry Andric Module *Imported, 56370b57cec5SDimitry Andric SourceLocation EndLoc) { 56380b57cec5SDimitry Andric ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1)) 56390b57cec5SDimitry Andric ImportDecl(DC, StartLoc, Imported, EndLoc); 56400b57cec5SDimitry Andric Import->setImplicit(); 56410b57cec5SDimitry Andric return Import; 56420b57cec5SDimitry Andric } 56430b57cec5SDimitry Andric 56440b57cec5SDimitry Andric ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, 56450b57cec5SDimitry Andric unsigned NumLocations) { 56460b57cec5SDimitry Andric return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations)) 56470b57cec5SDimitry Andric ImportDecl(EmptyShell()); 56480b57cec5SDimitry Andric } 56490b57cec5SDimitry Andric 56500b57cec5SDimitry Andric ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { 56515ffd83dbSDimitry Andric if (!isImportComplete()) 5652bdd1243dSDimitry Andric return std::nullopt; 56530b57cec5SDimitry Andric 56540b57cec5SDimitry Andric const auto *StoredLocs = getTrailingObjects<SourceLocation>(); 5655bdd1243dSDimitry Andric return llvm::ArrayRef(StoredLocs, 56560b57cec5SDimitry Andric getNumModuleIdentifiers(getImportedModule())); 56570b57cec5SDimitry Andric } 56580b57cec5SDimitry Andric 56590b57cec5SDimitry Andric SourceRange ImportDecl::getSourceRange() const { 56605ffd83dbSDimitry Andric if (!isImportComplete()) 56610b57cec5SDimitry Andric return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>()); 56620b57cec5SDimitry Andric 56630b57cec5SDimitry Andric return SourceRange(getLocation(), getIdentifierLocs().back()); 56640b57cec5SDimitry Andric } 56650b57cec5SDimitry Andric 56660b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 56670b57cec5SDimitry Andric // ExportDecl Implementation 56680b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 56690b57cec5SDimitry Andric 56700b57cec5SDimitry Andric void ExportDecl::anchor() {} 56710b57cec5SDimitry Andric 56720b57cec5SDimitry Andric ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC, 56730b57cec5SDimitry Andric SourceLocation ExportLoc) { 56740b57cec5SDimitry Andric return new (C, DC) ExportDecl(DC, ExportLoc); 56750b57cec5SDimitry Andric } 56760b57cec5SDimitry Andric 56770b57cec5SDimitry Andric ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 56780b57cec5SDimitry Andric return new (C, ID) ExportDecl(nullptr, SourceLocation()); 56790b57cec5SDimitry Andric } 5680