From 1f6458cb1944b69f052db463e5f2c6304f16b256 Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Wed, 29 Sep 2021 13:24:28 +0200 Subject: [PATCH] [clang] NFC: remove duplicated code around type constraint and templ arg subst Signed-off-by: Matheus Izvekov Reviewed By: rsmith Differential Revision: https://reviews.llvm.org/D110726 --- clang/include/clang/AST/DeclTemplate.h | 4 ++ clang/include/clang/Sema/Sema.h | 6 +-- clang/lib/Sema/SemaTemplate.cpp | 18 +-------- clang/lib/Sema/SemaTemplateDeduction.cpp | 9 ++--- clang/lib/Sema/SemaTemplateInstantiate.cpp | 51 ++++++++++++------------- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 53 ++++++++------------------ 6 files changed, 53 insertions(+), 88 deletions(-) diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h index d59a054..2408f50 100644 --- a/clang/include/clang/AST/DeclTemplate.h +++ b/clang/include/clang/AST/DeclTemplate.h @@ -729,6 +729,10 @@ public: /// Returns the number of explicit template arguments that were given. unsigned getNumTemplateArgs() const { return NumArgs; } + llvm::ArrayRef arguments() const { + return llvm::makeArrayRef(getTemplateArgs(), getNumTemplateArgs()); + } + /// Returns the nth template argument. const TemplateArgumentLoc &getTemplateArg(unsigned I) const { assert(I < getNumTemplateArgs() && "template arg index out of range"); diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index bd5cf12..64863e3 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -9483,9 +9483,9 @@ public: SubstTemplateName(NestedNameSpecifierLoc QualifierLoc, TemplateName Name, SourceLocation Loc, const MultiLevelTemplateArgumentList &TemplateArgs); - bool Subst(const TemplateArgumentLoc *Args, unsigned NumArgs, - TemplateArgumentListInfo &Result, - const MultiLevelTemplateArgumentList &TemplateArgs); + + bool SubstTypeConstraint(TemplateTypeParmDecl *Inst, const TypeConstraint *TC, + const MultiLevelTemplateArgumentList &TemplateArgs); bool InstantiateDefaultArgument(SourceLocation CallLoc, FunctionDecl *FD, ParmVarDecl *Param); diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 00fe811..de05450 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -2267,22 +2267,8 @@ private: TTP->isParameterPack(), TTP->hasTypeConstraint(), TTP->isExpandedParameterPack() ? llvm::Optional(TTP->getNumExpansionParameters()) : None); - if (const auto *TC = TTP->getTypeConstraint()) { - TemplateArgumentListInfo TransformedArgs; - const auto *ArgsAsWritten = TC->getTemplateArgsAsWritten(); - if (!ArgsAsWritten || - SemaRef.Subst(ArgsAsWritten->getTemplateArgs(), - ArgsAsWritten->NumTemplateArgs, TransformedArgs, - Args)) - SemaRef.AttachTypeConstraint( - TC->getNestedNameSpecifierLoc(), TC->getConceptNameInfo(), - TC->getNamedConcept(), ArgsAsWritten ? &TransformedArgs : nullptr, - NewTTP, - NewTTP->isParameterPack() - ? cast(TC->getImmediatelyDeclaredConstraint()) - ->getEllipsisLoc() - : SourceLocation()); - } + if (const auto *TC = TTP->getTypeConstraint()) + SemaRef.SubstTypeConstraint(NewTTP, TC, Args); if (TTP->hasDefaultArgument()) { TypeSourceInfo *InstantiatedDefaultArg = SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp index 060f42b..424342d 100644 --- a/clang/lib/Sema/SemaTemplateDeduction.cpp +++ b/clang/lib/Sema/SemaTemplateDeduction.cpp @@ -2979,14 +2979,13 @@ FinishTemplateArgumentDeduction( auto *Template = Partial->getSpecializedTemplate(); const ASTTemplateArgumentListInfo *PartialTemplArgInfo = Partial->getTemplateArgsAsWritten(); - const TemplateArgumentLoc *PartialTemplateArgs = - PartialTemplArgInfo->getTemplateArgs(); TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, PartialTemplArgInfo->RAngleLoc); - if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, - InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { + if (S.SubstTemplateArguments( + PartialTemplArgInfo->arguments(), + MultiLevelTemplateArgumentList(*DeducedArgumentList), InstArgs)) { unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; if (ParamIdx >= Partial->getTemplateParameters()->size()) ParamIdx = Partial->getTemplateParameters()->size() - 1; @@ -2994,7 +2993,7 @@ FinishTemplateArgumentDeduction( Decl *Param = const_cast( Partial->getTemplateParameters()->getParam(ParamIdx)); Info.Param = makeTemplateParameter(Param); - Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); + Info.FirstArg = (*PartialTemplArgInfo)[ArgIdx].getArgument(); return Sema::TDK_SubstitutionFailure; } diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index 74889aa..3a09bad 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -2308,6 +2308,29 @@ namespace { } // namespace +bool Sema::SubstTypeConstraint( + TemplateTypeParmDecl *Inst, const TypeConstraint *TC, + const MultiLevelTemplateArgumentList &TemplateArgs) { + const ASTTemplateArgumentListInfo *TemplArgInfo = + TC->getTemplateArgsAsWritten(); + TemplateArgumentListInfo InstArgs; + + if (TemplArgInfo) { + InstArgs.setLAngleLoc(TemplArgInfo->LAngleLoc); + InstArgs.setRAngleLoc(TemplArgInfo->RAngleLoc); + if (SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs, + InstArgs)) + return true; + } + return AttachTypeConstraint( + TC->getNestedNameSpecifierLoc(), TC->getConceptNameInfo(), + TC->getNamedConcept(), &InstArgs, Inst, + Inst->isParameterPack() + ? cast(TC->getImmediatelyDeclaredConstraint()) + ->getEllipsisLoc() + : SourceLocation()); +} + ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, const MultiLevelTemplateArgumentList &TemplateArgs, int indexAdjustment, @@ -2372,24 +2395,7 @@ ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, if (Inst && !Inst->getTypeConstraint()) { // TODO: Concepts: do not instantiate the constraint (delayed constraint // substitution) - const ASTTemplateArgumentListInfo *TemplArgInfo - = TC->getTemplateArgsAsWritten(); - TemplateArgumentListInfo InstArgs; - - if (TemplArgInfo) { - InstArgs.setLAngleLoc(TemplArgInfo->LAngleLoc); - InstArgs.setRAngleLoc(TemplArgInfo->RAngleLoc); - if (Subst(TemplArgInfo->getTemplateArgs(), - TemplArgInfo->NumTemplateArgs, InstArgs, TemplateArgs)) - return nullptr; - } - if (AttachTypeConstraint( - TC->getNestedNameSpecifierLoc(), TC->getConceptNameInfo(), - TC->getNamedConcept(), TemplArgInfo ? &InstArgs : nullptr, Inst, - TTP->isParameterPack() - ? cast(TC->getImmediatelyDeclaredConstraint()) - ->getEllipsisLoc() - : SourceLocation())) + if (SubstTypeConstraint(Inst, TC, TemplateArgs)) return nullptr; } } @@ -3537,15 +3543,6 @@ Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc, return Instantiator.TransformTemplateName(SS, Name, Loc); } -bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs, - TemplateArgumentListInfo &Result, - const MultiLevelTemplateArgumentList &TemplateArgs) { - TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), - DeclarationName()); - - return Instantiator.TransformTemplateArguments(Args, NumArgs, Result); -} - static const Decl *getCanonicalParmVarDecl(const Decl *D) { // When storing ParmVarDecls in the local instantiation scope, we always // want to use the ParmVarDecl from the canonical function declaration, diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index e5ccfc2..c9d5b03 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2129,8 +2129,8 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl( // Instantiate the explicit template arguments. TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), Info->getRAngleLoc()); - if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), - ExplicitArgs, TemplateArgs)) + if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, + ExplicitArgs)) return nullptr; // Map the candidate templates to their instantiations. @@ -2157,8 +2157,8 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl( // Instantiate the explicit template arguments. TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), Info->getRAngleLoc()); - if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), - ExplicitArgs, TemplateArgs)) + if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, + ExplicitArgs)) return nullptr; if (SemaRef.CheckFunctionTemplateSpecialization(Function, @@ -2493,8 +2493,8 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl( // Instantiate the explicit template arguments. TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), Info->getRAngleLoc()); - if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), - ExplicitArgs, TemplateArgs)) + if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, + ExplicitArgs)) return nullptr; // Map the candidate templates to their instantiations. @@ -2520,8 +2520,8 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl( TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), Info->getRAngleLoc()); - if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), - ExplicitArgs, TemplateArgs)) + if (SemaRef.SubstTemplateArguments(Info->arguments(), TemplateArgs, + ExplicitArgs)) return nullptr; if (SemaRef.CheckFunctionTemplateSpecialization(Method, @@ -2689,25 +2689,7 @@ Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( // TODO: Concepts: do not instantiate the constraint (delayed constraint // substitution) - const ASTTemplateArgumentListInfo *TemplArgInfo - = TC->getTemplateArgsAsWritten(); - TemplateArgumentListInfo InstArgs; - - if (TemplArgInfo) { - InstArgs.setLAngleLoc(TemplArgInfo->LAngleLoc); - InstArgs.setRAngleLoc(TemplArgInfo->RAngleLoc); - if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), - TemplArgInfo->NumTemplateArgs, - InstArgs, TemplateArgs)) - return nullptr; - } - if (SemaRef.AttachTypeConstraint( - TC->getNestedNameSpecifierLoc(), TC->getConceptNameInfo(), - TC->getNamedConcept(), &InstArgs, Inst, - D->isParameterPack() - ? cast(TC->getImmediatelyDeclaredConstraint()) - ->getEllipsisLoc() - : SourceLocation())) + if (SemaRef.SubstTypeConstraint(Inst, TC, TemplateArgs)) return nullptr; } } @@ -3605,8 +3587,7 @@ TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( SmallVector ArgLocs; for (unsigned I = 0; I != Loc.getNumArgs(); ++I) ArgLocs.push_back(Loc.getArgLoc(I)); - if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(), - InstTemplateArgs, TemplateArgs)) + if (SemaRef.SubstTemplateArguments(ArgLocs, TemplateArgs, InstTemplateArgs)) return nullptr; // Check that the template argument list is well-formed for this @@ -3731,8 +3712,8 @@ Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc()); VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc()); - if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(), - TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs)) + if (SemaRef.SubstTemplateArguments(TemplateArgsInfo.arguments(), TemplateArgs, + VarTemplateArgsInfo)) return nullptr; // Check that the template argument list is well-formed for this template. @@ -3999,9 +3980,8 @@ TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( = PartialSpec->getTemplateArgsAsWritten(); TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, TemplArgInfo->RAngleLoc); - if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), - TemplArgInfo->NumTemplateArgs, - InstTemplateArgs, TemplateArgs)) + if (SemaRef.SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs, + InstTemplateArgs)) return nullptr; // Check that the template argument list is well-formed for this @@ -4127,9 +4107,8 @@ TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( = PartialSpec->getTemplateArgsAsWritten(); TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, TemplArgInfo->RAngleLoc); - if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), - TemplArgInfo->NumTemplateArgs, - InstTemplateArgs, TemplateArgs)) + if (SemaRef.SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs, + InstTemplateArgs)) return nullptr; // Check that the template argument list is well-formed for this -- 2.7.4