diff options
Diffstat (limited to 'contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp')
-rw-r--r-- | contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 213 |
1 files changed, 145 insertions, 68 deletions
diff --git a/contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 67343d11d333..d1ad304e62e4 100644 --- a/contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -86,15 +86,13 @@ static void instantiateDependentAlignedAttr( S, Sema::ExpressionEvaluationContext::ConstantEvaluated); ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs); if (!Result.isInvalid()) - S.AddAlignedAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), - Aligned->getSpellingListIndex(), IsPackExpansion); + S.AddAlignedAttr(New, *Aligned, Result.getAs<Expr>(), IsPackExpansion); } else { TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(), TemplateArgs, Aligned->getLocation(), DeclarationName()); if (Result) - S.AddAlignedAttr(Aligned->getLocation(), New, Result, - Aligned->getSpellingListIndex(), IsPackExpansion); + S.AddAlignedAttr(New, *Aligned, Result, IsPackExpansion); } } @@ -156,8 +154,7 @@ static void instantiateDependentAssumeAlignedAttr( OE = Result.getAs<Expr>(); } - S.AddAssumeAlignedAttr(Aligned->getLocation(), New, E, OE, - Aligned->getSpellingListIndex()); + S.AddAssumeAlignedAttr(New, *Aligned, E, OE); } static void instantiateDependentAlignValueAttr( @@ -168,8 +165,7 @@ static void instantiateDependentAlignValueAttr( S, Sema::ExpressionEvaluationContext::ConstantEvaluated); ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); if (!Result.isInvalid()) - S.AddAlignValueAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), - Aligned->getSpellingListIndex()); + S.AddAlignValueAttr(New, *Aligned, Result.getAs<Expr>()); } static void instantiateDependentAllocAlignAttr( @@ -179,8 +175,7 @@ static void instantiateDependentAllocAlignAttr( S.getASTContext(), llvm::APInt(64, Align->getParamIndex().getSourceIndex()), S.getASTContext().UnsignedLongLongTy, Align->getLocation()); - S.AddAllocAlignAttr(Align->getLocation(), New, Param, - Align->getSpellingListIndex()); + S.AddAllocAlignAttr(New, *Align, Param); } static Expr *instantiateDependentFunctionAttrCondition( @@ -221,9 +216,8 @@ static void instantiateDependentEnableIfAttr( S, TemplateArgs, EIA, EIA->getCond(), Tmpl, New); if (Cond) - New->addAttr(new (S.getASTContext()) EnableIfAttr( - EIA->getLocation(), S.getASTContext(), Cond, EIA->getMessage(), - EIA->getSpellingListIndex())); + New->addAttr(new (S.getASTContext()) EnableIfAttr(S.getASTContext(), *EIA, + Cond, EIA->getMessage())); } static void instantiateDependentDiagnoseIfAttr( @@ -234,9 +228,8 @@ static void instantiateDependentDiagnoseIfAttr( if (Cond) New->addAttr(new (S.getASTContext()) DiagnoseIfAttr( - DIA->getLocation(), S.getASTContext(), Cond, DIA->getMessage(), - DIA->getDiagnosticType(), DIA->getArgDependent(), New, - DIA->getSpellingListIndex())); + S.getASTContext(), *DIA, Cond, DIA->getMessage(), + DIA->getDiagnosticType(), DIA->getArgDependent(), New)); } // Constructs and adds to New a new instance of CUDALaunchBoundsAttr using @@ -261,16 +254,15 @@ static void instantiateDependentCUDALaunchBoundsAttr( MinBlocks = Result.getAs<Expr>(); } - S.AddLaunchBoundsAttr(Attr.getLocation(), New, MaxThreads, MinBlocks, - Attr.getSpellingListIndex()); + S.AddLaunchBoundsAttr(New, Attr, MaxThreads, MinBlocks); } static void instantiateDependentModeAttr(Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, const ModeAttr &Attr, Decl *New) { - S.AddModeAttr(Attr.getRange(), New, Attr.getMode(), - Attr.getSpellingListIndex(), /*InInstantiation=*/true); + S.AddModeAttr(New, Attr, Attr.getMode(), + /*InInstantiation=*/true); } /// Instantiation of 'declare simd' attribute and its arguments. @@ -356,6 +348,67 @@ static void instantiateOMPDeclareSimdDeclAttr( Attr.getRange()); } +/// Instantiation of 'declare variant' attribute and its arguments. +static void instantiateOMPDeclareVariantAttr( + Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, + const OMPDeclareVariantAttr &Attr, Decl *New) { + // Allow 'this' in clauses with varlists. + if (auto *FTD = dyn_cast<FunctionTemplateDecl>(New)) + New = FTD->getTemplatedDecl(); + auto *FD = cast<FunctionDecl>(New); + auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext()); + + auto &&SubstExpr = [FD, ThisContext, &S, &TemplateArgs](Expr *E) { + if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) + if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { + Sema::ContextRAII SavedContext(S, FD); + LocalInstantiationScope Local(S); + if (FD->getNumParams() > PVD->getFunctionScopeIndex()) + Local.InstantiatedLocal( + PVD, FD->getParamDecl(PVD->getFunctionScopeIndex())); + return S.SubstExpr(E, TemplateArgs); + } + Sema::CXXThisScopeRAII ThisScope(S, ThisContext, Qualifiers(), + FD->isCXXInstanceMember()); + return S.SubstExpr(E, TemplateArgs); + }; + + // Substitute a single OpenMP clause, which is a potentially-evaluated + // full-expression. + auto &&Subst = [&SubstExpr, &S](Expr *E) { + EnterExpressionEvaluationContext Evaluated( + S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); + ExprResult Res = SubstExpr(E); + if (Res.isInvalid()) + return Res; + return S.ActOnFinishFullExpr(Res.get(), false); + }; + + ExprResult VariantFuncRef; + if (Expr *E = Attr.getVariantFuncRef()) + VariantFuncRef = Subst(E); + + ExprResult Score; + if (Expr *E = Attr.getScore()) + Score = Subst(E); + + // Check function/variant ref. + Optional<std::pair<FunctionDecl *, Expr *>> DeclVarData = + S.checkOpenMPDeclareVariantFunction( + S.ConvertDeclToDeclGroup(New), VariantFuncRef.get(), Attr.getRange()); + if (!DeclVarData) + return; + // Instantiate the attribute. + Sema::OpenMPDeclareVariantCtsSelectorData Data( + Attr.getCtxSelectorSet(), Attr.getCtxSelector(), + llvm::makeMutableArrayRef(Attr.implVendors_begin(), + Attr.implVendors_size()), + Score); + S.ActOnOpenMPDeclareVariantDirective(DeclVarData.getValue().first, + DeclVarData.getValue().second, + Attr.getRange(), Data); +} + static void instantiateDependentAMDGPUFlatWorkGroupSizeAttr( Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, const AMDGPUFlatWorkGroupSizeAttr &Attr, Decl *New) { @@ -373,8 +426,7 @@ static void instantiateDependentAMDGPUFlatWorkGroupSizeAttr( return; Expr *MaxExpr = Result.getAs<Expr>(); - S.addAMDGPUFlatWorkGroupSizeAttr(Attr.getLocation(), New, MinExpr, MaxExpr, - Attr.getSpellingListIndex()); + S.addAMDGPUFlatWorkGroupSizeAttr(New, Attr, MinExpr, MaxExpr); } static ExplicitSpecifier @@ -420,8 +472,7 @@ static void instantiateDependentAMDGPUWavesPerEUAttr( MaxExpr = Result.getAs<Expr>(); } - S.addAMDGPUWavesPerEUAttr(Attr.getLocation(), New, MinExpr, MaxExpr, - Attr.getSpellingListIndex()); + S.addAMDGPUWavesPerEUAttr(New, Attr, MinExpr, MaxExpr); } void Sema::InstantiateAttrsForDecl( @@ -470,14 +521,12 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, continue; } - const AssumeAlignedAttr *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr); - if (AssumeAligned) { + if (const auto *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr)) { instantiateDependentAssumeAlignedAttr(*this, TemplateArgs, AssumeAligned, New); continue; } - const AlignValueAttr *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr); - if (AlignValue) { + if (const auto *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr)) { instantiateDependentAlignValueAttr(*this, TemplateArgs, AlignValue, New); continue; } @@ -500,14 +549,14 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, continue; } - if (const CUDALaunchBoundsAttr *CUDALaunchBounds = + if (const auto *CUDALaunchBounds = dyn_cast<CUDALaunchBoundsAttr>(TmplAttr)) { instantiateDependentCUDALaunchBoundsAttr(*this, TemplateArgs, *CUDALaunchBounds, New); continue; } - if (const ModeAttr *Mode = dyn_cast<ModeAttr>(TmplAttr)) { + if (const auto *Mode = dyn_cast<ModeAttr>(TmplAttr)) { instantiateDependentModeAttr(*this, TemplateArgs, *Mode, New); continue; } @@ -517,13 +566,18 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, continue; } - if (const AMDGPUFlatWorkGroupSizeAttr *AMDGPUFlatWorkGroupSize = + if (const auto *OMPAttr = dyn_cast<OMPDeclareVariantAttr>(TmplAttr)) { + instantiateOMPDeclareVariantAttr(*this, TemplateArgs, *OMPAttr, New); + continue; + } + + if (const auto *AMDGPUFlatWorkGroupSize = dyn_cast<AMDGPUFlatWorkGroupSizeAttr>(TmplAttr)) { instantiateDependentAMDGPUFlatWorkGroupSizeAttr( *this, TemplateArgs, *AMDGPUFlatWorkGroupSize, New); } - if (const AMDGPUWavesPerEUAttr *AMDGPUFlatWorkGroupSize = + if (const auto *AMDGPUFlatWorkGroupSize = dyn_cast<AMDGPUWavesPerEUAttr>(TmplAttr)) { instantiateDependentAMDGPUWavesPerEUAttr(*this, TemplateArgs, *AMDGPUFlatWorkGroupSize, New); @@ -537,21 +591,30 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, } } - if (auto ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) { - AddParameterABIAttr(ABIAttr->getRange(), New, ABIAttr->getABI(), - ABIAttr->getSpellingListIndex()); + if (const auto *ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) { + AddParameterABIAttr(New, *ABIAttr, ABIAttr->getABI()); continue; } if (isa<NSConsumedAttr>(TmplAttr) || isa<OSConsumedAttr>(TmplAttr) || isa<CFConsumedAttr>(TmplAttr)) { - AddXConsumedAttr(New, TmplAttr->getRange(), - TmplAttr->getSpellingListIndex(), - attrToRetainOwnershipKind(TmplAttr), + AddXConsumedAttr(New, *TmplAttr, attrToRetainOwnershipKind(TmplAttr), /*template instantiation=*/true); continue; } + if (auto *A = dyn_cast<PointerAttr>(TmplAttr)) { + if (!New->hasAttr<PointerAttr>()) + New->addAttr(A->clone(Context)); + continue; + } + + if (auto *A = dyn_cast<OwnerAttr>(TmplAttr)) { + if (!New->hasAttr<OwnerAttr>()) + New->addAttr(A->clone(Context)); + continue; + } + assert(!TmplAttr->isPackExpansion()); if (TmplAttr->isLateParsed() && LateAttrs) { // Late parsed attributes must be instantiated and attached after the @@ -711,6 +774,9 @@ Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); + if (D->getUnderlyingType()->getAs<DependentNameType>()) + SemaRef.inferGslPointerAttribute(Typedef); + Typedef->setAccess(D->getAccess()); return Typedef; @@ -2090,10 +2156,9 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl( Constructor->getConstexprKind()); Method->setRangeEnd(Constructor->getEndLoc()); } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { - Method = CXXDestructorDecl::Create(SemaRef.Context, Record, - StartLoc, NameInfo, T, TInfo, - Destructor->isInlineSpecified(), - false); + Method = CXXDestructorDecl::Create( + SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, + Destructor->isInlineSpecified(), false, Destructor->getConstexprKind()); Method->setRangeEnd(Destructor->getEndLoc()); } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { Method = CXXConversionDecl::Create( @@ -2339,6 +2404,7 @@ Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), D->getIndex(), D->getIdentifier(), D->wasDeclaredWithTypename(), D->isParameterPack()); Inst->setAccess(AS_public); + Inst->setImplicit(D->isImplicit()); if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { TypeSourceInfo *InstantiatedDefaultArg = @@ -2485,6 +2551,7 @@ Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( D->getPosition(), D->getIdentifier(), T, D->isParameterPack(), DI); Param->setAccess(AS_public); + Param->setImplicit(D->isImplicit()); if (Invalid) Param->setInvalidDecl(); @@ -2628,6 +2695,7 @@ TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( D->getDefaultArgument().getTemplateNameLoc())); } Param->setAccess(AS_public); + Param->setImplicit(D->isImplicit()); // Introduce this template parameter's instantiation into the instantiation // scope. @@ -3415,7 +3483,11 @@ Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, if (D->isInvalidDecl()) return nullptr; - return Instantiator.Visit(D); + Decl *SubstD; + runWithSufficientStackSpace(D->getLocation(), [&] { + SubstD = Instantiator.Visit(D); + }); + return SubstD; } /// Instantiates a nested template parameter list in the current @@ -3443,14 +3515,21 @@ TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { if (Invalid) return nullptr; - // Note: we substitute into associated constraints later - Expr *const UninstantiatedRequiresClause = L->getRequiresClause(); + // FIXME: Concepts: Substitution into requires clause should only happen when + // checking satisfaction. + Expr *InstRequiresClause = nullptr; + if (Expr *E = L->getRequiresClause()) { + ExprResult Res = SemaRef.SubstExpr(E, TemplateArgs); + if (Res.isInvalid() || !Res.isUsable()) { + return nullptr; + } + InstRequiresClause = Res.get(); + } TemplateParameterList *InstL = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), L->getLAngleLoc(), Params, - L->getRAngleLoc(), - UninstantiatedRequiresClause); + L->getRAngleLoc(), InstRequiresClause); return InstL; } @@ -5217,11 +5296,11 @@ DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, /// template struct X<int>; /// \endcode /// -/// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the -/// \p EnumConstantDecl for \p KnownValue (which refers to -/// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation -/// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs -/// this mapping from within the instantiation of <tt>X<int></tt>. +/// In the instantiation of X<int>::getKind(), we need to map the \p +/// EnumConstantDecl for \p KnownValue (which refers to +/// X<T>::<Kind>::KnownValue) to its instantiation (X<int>::<Kind>::KnownValue). +/// \p FindInstantiatedDecl performs this mapping from within the instantiation +/// of X<int>. NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, const MultiLevelTemplateArgumentList &TemplateArgs, bool FindingInstantiatedContext) { @@ -5312,20 +5391,6 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, return cast<LabelDecl>(Inst); } - // For variable template specializations, update those that are still - // type-dependent. - if (VarTemplateSpecializationDecl *VarSpec = - dyn_cast<VarTemplateSpecializationDecl>(D)) { - bool InstantiationDependent = false; - const TemplateArgumentListInfo &VarTemplateArgs = - VarSpec->getTemplateArgsInfo(); - if (TemplateSpecializationType::anyDependentTemplateArguments( - VarTemplateArgs, InstantiationDependent)) - D = cast<NamedDecl>( - SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs)); - return D; - } - if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { if (!Record->isDependentContext()) return D; @@ -5450,11 +5515,23 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, // find it. Does that ever matter? if (auto Name = D->getDeclName()) { DeclarationNameInfo NameInfo(Name, D->getLocation()); - Name = SubstDeclarationNameInfo(NameInfo, TemplateArgs).getName(); + DeclarationNameInfo NewNameInfo = + SubstDeclarationNameInfo(NameInfo, TemplateArgs); + Name = NewNameInfo.getName(); if (!Name) return nullptr; DeclContext::lookup_result Found = ParentDC->lookup(Name); - Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); + + if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) { + VarTemplateDecl *Templ = cast_or_null<VarTemplateDecl>( + findInstantiationOf(Context, VTSD->getSpecializedTemplate(), + Found.begin(), Found.end())); + if (!Templ) + return nullptr; + Result = getVarTemplateSpecialization( + Templ, &VTSD->getTemplateArgsInfo(), NewNameInfo, SourceLocation()); + } else + Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); } else { // Since we don't have a name for the entity we're looking for, // our only option is to walk through all of the declarations to |