[clang] [Clang][CTAD][NFC] Unify transformTemplateParameter() (PR #100865)
Younan Zhang via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 28 03:23:17 PDT 2024
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/100865
>From 13724c6cd47c17b92b57f04436c544bd9d681070 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Sat, 27 Jul 2024 18:52:23 +0800
Subject: [PATCH 1/3] [Clang][CTAD][NFC] Unify transformTemplateParameter()
We ended up having two transformTemplateParameter() after CTAD for
type aliases was landed. This patch cleans them up and allows them
to share one implementation.
---
clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 120 ++++++++----------
1 file changed, 52 insertions(+), 68 deletions(-)
diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
index 0602d07c6b9b0..2e94e2d5660e5 100644
--- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
+++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
@@ -241,11 +241,10 @@ NamedDecl *buildDeductionGuide(
}
// Transform a given template type parameter `TTP`.
-TemplateTypeParmDecl *
-transformTemplateTypeParam(Sema &SemaRef, DeclContext *DC,
- TemplateTypeParmDecl *TTP,
- MultiLevelTemplateArgumentList &Args,
- unsigned NewDepth, unsigned NewIndex) {
+TemplateTypeParmDecl *transformTemplateTypeParam(
+ Sema &SemaRef, DeclContext *DC, TemplateTypeParmDecl *TTP,
+ MultiLevelTemplateArgumentList &Args, unsigned NewDepth, unsigned NewIndex,
+ bool EvaluateConstraint) {
// TemplateTypeParmDecl's index cannot be changed after creation, so
// substitute it directly.
auto *NewTTP = TemplateTypeParmDecl::Create(
@@ -257,7 +256,7 @@ transformTemplateTypeParam(Sema &SemaRef, DeclContext *DC,
: std::nullopt);
if (const auto *TC = TTP->getTypeConstraint())
SemaRef.SubstTypeConstraint(NewTTP, TC, Args,
- /*EvaluateConstraint=*/true);
+ /*EvaluateConstraint=*/EvaluateConstraint);
if (TTP->hasDefaultArgument()) {
TemplateArgumentLoc InstantiatedDefaultArg;
if (!SemaRef.SubstTemplateArgument(
@@ -284,6 +283,42 @@ transformTemplateParam(Sema &SemaRef, DeclContext *DC,
return NewParam;
}
+NamedDecl *transformTemplateParameter(Sema &SemaRef, DeclContext *DC,
+ NamedDecl *TemplateParam,
+ MultiLevelTemplateArgumentList &Args,
+ unsigned NewIndex, unsigned NewDepth,
+ bool EvaluateConstraint = true) {
+ if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
+ return transformTemplateTypeParam(
+ SemaRef, DC, TTP, Args, NewDepth, NewIndex,
+ /*EvaluateConstraint=*/EvaluateConstraint);
+ if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
+ return transformTemplateParam(SemaRef, DC, TTP, Args, NewIndex, NewDepth);
+ if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
+ return transformTemplateParam(SemaRef, DC, NTTP, Args, NewIndex, NewDepth);
+ llvm_unreachable("Unhandled template parameter types");
+}
+
+unsigned getTemplateParameterDepth(NamedDecl *TemplateParam) {
+ if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
+ return TTP->getDepth();
+ if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
+ return TTP->getDepth();
+ if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
+ return NTTP->getDepth();
+ llvm_unreachable("Unhandled template parameter types");
+}
+
+unsigned getTemplateParameterIndex(NamedDecl *TemplateParam) {
+ if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
+ return TTP->getIndex();
+ if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
+ return TTP->getIndex();
+ if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
+ return NTTP->getIndex();
+ llvm_unreachable("Unhandled template parameter types");
+}
+
/// Transform to convert portions of a constructor declaration into the
/// corresponding deduction guide, per C++1z [over.match.class.deduct]p1.
struct ConvertConstructorToDeductionGuideTransform {
@@ -358,21 +393,23 @@ struct ConvertConstructorToDeductionGuideTransform {
Args.addOuterRetainedLevel();
if (NestedPattern)
Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
- NamedDecl *NewParam = transformTemplateParameter(Param, Args);
+ NamedDecl *NewParam = transformTemplateParameter(
+ SemaRef, DC, Param, Args,
+ getTemplateParameterIndex(Param) + Depth1IndexAdjustment,
+ getTemplateParameterDepth(Param) - 1);
if (!NewParam)
return nullptr;
// Constraints require that we substitute depth-1 arguments
// to match depths when substituted for evaluation later
Depth1Args.push_back(SemaRef.Context.getInjectedTemplateArg(NewParam));
- if (NestedPattern) {
- TemplateDeclInstantiator Instantiator(SemaRef, DC,
- OuterInstantiationArgs);
- Instantiator.setEvaluateConstraints(false);
- SemaRef.runWithSufficientStackSpace(NewParam->getLocation(), [&] {
- NewParam = cast<NamedDecl>(Instantiator.Visit(NewParam));
- });
- }
+ if (NestedPattern)
+ NewParam = transformTemplateParameter(
+ SemaRef, DC, NewParam, OuterInstantiationArgs,
+ getTemplateParameterIndex(NewParam),
+ getTemplateParameterDepth(NewParam) -
+ OuterInstantiationArgs.getNumLevels(),
+ /*EvaluateConstraint=*/false);
assert(NewParam->getTemplateDepth() == 0 &&
"Unexpected template parameter depth");
@@ -479,25 +516,6 @@ struct ConvertConstructorToDeductionGuideTransform {
}
private:
- /// Transform a constructor template parameter into a deduction guide template
- /// parameter, rebuilding any internal references to earlier parameters and
- /// renumbering as we go.
- NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam,
- MultiLevelTemplateArgumentList &Args) {
- if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
- return transformTemplateTypeParam(
- SemaRef, DC, TTP, Args, TTP->getDepth() - 1,
- Depth1IndexAdjustment + TTP->getIndex());
- if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
- return transformTemplateParam(SemaRef, DC, TTP, Args,
- Depth1IndexAdjustment + TTP->getIndex(),
- TTP->getDepth() - 1);
- auto *NTTP = cast<NonTypeTemplateParmDecl>(TemplateParam);
- return transformTemplateParam(SemaRef, DC, NTTP, Args,
- Depth1IndexAdjustment + NTTP->getIndex(),
- NTTP->getDepth() - 1);
- }
-
QualType transformFunctionProtoType(
TypeLocBuilder &TLB, FunctionProtoTypeLoc TL,
SmallVectorImpl<ParmVarDecl *> &Params,
@@ -634,26 +652,6 @@ struct ConvertConstructorToDeductionGuideTransform {
}
};
-unsigned getTemplateParameterDepth(NamedDecl *TemplateParam) {
- if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
- return TTP->getDepth();
- if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
- return TTP->getDepth();
- if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
- return NTTP->getDepth();
- llvm_unreachable("Unhandled template parameter types");
-}
-
-unsigned getTemplateParameterIndex(NamedDecl *TemplateParam) {
- if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
- return TTP->getIndex();
- if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
- return TTP->getIndex();
- if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
- return NTTP->getIndex();
- llvm_unreachable("Unhandled template parameter types");
-}
-
// Find all template parameters that appear in the given DeducedArgs.
// Return the indices of the template parameters in the TemplateParams.
SmallVector<unsigned> TemplateParamsReferencedInTemplateArgumentList(
@@ -722,20 +720,6 @@ bool hasDeclaredDeductionGuides(DeclarationName Name, DeclContext *DC) {
return false;
}
-NamedDecl *transformTemplateParameter(Sema &SemaRef, DeclContext *DC,
- NamedDecl *TemplateParam,
- MultiLevelTemplateArgumentList &Args,
- unsigned NewIndex, unsigned NewDepth) {
- if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
- return transformTemplateTypeParam(SemaRef, DC, TTP, Args, NewDepth,
- NewIndex);
- if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
- return transformTemplateParam(SemaRef, DC, TTP, Args, NewIndex, NewDepth);
- if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
- return transformTemplateParam(SemaRef, DC, NTTP, Args, NewIndex, NewDepth);
- llvm_unreachable("Unhandled template parameter types");
-}
-
// Build the associated constraints for the alias deduction guides.
// C++ [over.match.class.deduct]p3.3:
// The associated constraints ([temp.constr.decl]) are the conjunction of the
>From 64da16d0fc5f142335c2f862882163e5567cff5c Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Sat, 27 Jul 2024 19:12:56 +0800
Subject: [PATCH 2/3] getNumLevels() -> getNumSubstitutedLevels()
---
clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
index 2e94e2d5660e5..79fc0485679ea 100644
--- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
+++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
@@ -408,7 +408,7 @@ struct ConvertConstructorToDeductionGuideTransform {
SemaRef, DC, NewParam, OuterInstantiationArgs,
getTemplateParameterIndex(NewParam),
getTemplateParameterDepth(NewParam) -
- OuterInstantiationArgs.getNumLevels(),
+ OuterInstantiationArgs.getNumSubstitutedLevels(),
/*EvaluateConstraint=*/false);
assert(NewParam->getTemplateDepth() == 0 &&
>From 0016929ee6b3493bfd21149f09e660d089b946a0 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Sun, 28 Jul 2024 18:21:45 +0800
Subject: [PATCH 3/3] Drop getTemplateParameter{Index,Depth}()
---
clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 59 +++++++------------
1 file changed, 20 insertions(+), 39 deletions(-)
diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
index 79fc0485679ea..545da21183c3c 100644
--- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
+++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
@@ -39,6 +39,7 @@
#include "clang/Sema/Overload.h"
#include "clang/Sema/Ownership.h"
#include "clang/Sema/Scope.h"
+#include "clang/Sema/SemaInternal.h"
#include "clang/Sema/Template.h"
#include "clang/Sema/TemplateDeduction.h"
#include "llvm/ADT/ArrayRef.h"
@@ -299,26 +300,6 @@ NamedDecl *transformTemplateParameter(Sema &SemaRef, DeclContext *DC,
llvm_unreachable("Unhandled template parameter types");
}
-unsigned getTemplateParameterDepth(NamedDecl *TemplateParam) {
- if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
- return TTP->getDepth();
- if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
- return TTP->getDepth();
- if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
- return NTTP->getDepth();
- llvm_unreachable("Unhandled template parameter types");
-}
-
-unsigned getTemplateParameterIndex(NamedDecl *TemplateParam) {
- if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
- return TTP->getIndex();
- if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
- return TTP->getIndex();
- if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
- return NTTP->getIndex();
- llvm_unreachable("Unhandled template parameter types");
-}
-
/// Transform to convert portions of a constructor declaration into the
/// corresponding deduction guide, per C++1z [over.match.class.deduct]p1.
struct ConvertConstructorToDeductionGuideTransform {
@@ -393,23 +374,22 @@ struct ConvertConstructorToDeductionGuideTransform {
Args.addOuterRetainedLevel();
if (NestedPattern)
Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
+ auto [Depth, Index] = getDepthAndIndex(Param);
NamedDecl *NewParam = transformTemplateParameter(
- SemaRef, DC, Param, Args,
- getTemplateParameterIndex(Param) + Depth1IndexAdjustment,
- getTemplateParameterDepth(Param) - 1);
+ SemaRef, DC, Param, Args, Index + Depth1IndexAdjustment, Depth - 1);
if (!NewParam)
return nullptr;
// Constraints require that we substitute depth-1 arguments
// to match depths when substituted for evaluation later
Depth1Args.push_back(SemaRef.Context.getInjectedTemplateArg(NewParam));
- if (NestedPattern)
+ if (NestedPattern) {
+ auto [Depth, Index] = getDepthAndIndex(NewParam);
NewParam = transformTemplateParameter(
- SemaRef, DC, NewParam, OuterInstantiationArgs,
- getTemplateParameterIndex(NewParam),
- getTemplateParameterDepth(NewParam) -
- OuterInstantiationArgs.getNumSubstitutedLevels(),
+ SemaRef, DC, NewParam, OuterInstantiationArgs, Index,
+ Depth - OuterInstantiationArgs.getNumSubstitutedLevels(),
/*EvaluateConstraint=*/false);
+ }
assert(NewParam->getTemplateDepth() == 0 &&
"Unexpected template parameter depth");
@@ -687,8 +667,10 @@ SmallVector<unsigned> TemplateParamsReferencedInTemplateArgumentList(
void MarkAppeared(NamedDecl *ND) {
if (llvm::isa<NonTypeTemplateParmDecl, TemplateTypeParmDecl,
- TemplateTemplateParmDecl>(ND))
- Mark(getTemplateParameterDepth(ND), getTemplateParameterIndex(ND));
+ TemplateTemplateParmDecl>(ND)) {
+ auto [Depth, Index] = getDepthAndIndex(ND);
+ Mark(Depth, Index);
+ }
}
void Mark(unsigned Depth, unsigned Index) {
if (Index < TemplateParamList->size() &&
@@ -775,7 +757,7 @@ buildAssociatedConstraints(Sema &SemaRef, FunctionTemplateDecl *F,
NamedDecl *NewParam = transformTemplateParameter(
SemaRef, AliasTemplate->getDeclContext(), TP, Args,
/*NewIndex=*/AdjustedAliasTemplateArgs.size(),
- getTemplateParameterDepth(TP) + AdjustDepth);
+ getDepthAndIndex(TP).first + AdjustDepth);
TemplateArgument NewTemplateArgument =
Context.getInjectedTemplateArg(NewParam);
@@ -798,10 +780,10 @@ buildAssociatedConstraints(Sema &SemaRef, FunctionTemplateDecl *F,
Args.setKind(TemplateSubstitutionKind::Rewrite);
Args.addOuterTemplateArguments(TemplateArgsForBuildingRC);
// Rebuild the template parameter with updated depth and index.
- NamedDecl *NewParam = transformTemplateParameter(
- SemaRef, F->getDeclContext(), TP, Args,
- /*NewIndex=*/FirstUndeducedParamIdx,
- getTemplateParameterDepth(TP) + AdjustDepth);
+ NamedDecl *NewParam =
+ transformTemplateParameter(SemaRef, F->getDeclContext(), TP, Args,
+ /*NewIndex=*/FirstUndeducedParamIdx,
+ getDepthAndIndex(TP).first + AdjustDepth);
FirstUndeducedParamIdx += 1;
assert(TemplateArgsForBuildingRC[Index].isNull());
TemplateArgsForBuildingRC[Index] =
@@ -903,7 +885,7 @@ Expr *buildIsDeducibleConstraint(Sema &SemaRef,
NamedDecl *NewParam = transformTemplateParameter(
SemaRef, AliasTemplate->getDeclContext(), TP, Args,
/*NewIndex=*/TransformedTemplateArgs.size(),
- getTemplateParameterDepth(TP) + AdjustDepth);
+ getDepthAndIndex(TP).first + AdjustDepth);
TemplateArgument NewTemplateArgument =
Context.getInjectedTemplateArg(NewParam);
@@ -1065,8 +1047,7 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
Args.addOuterTemplateArguments(TransformedDeducedAliasArgs);
NamedDecl *NewParam = transformTemplateParameter(
SemaRef, AliasTemplate->getDeclContext(), TP, Args,
- /*NewIndex=*/FPrimeTemplateParams.size(),
- getTemplateParameterDepth(TP));
+ /*NewIndex=*/FPrimeTemplateParams.size(), getDepthAndIndex(TP).first);
FPrimeTemplateParams.push_back(NewParam);
TemplateArgument NewTemplateArgument =
@@ -1085,7 +1066,7 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
Args.addOuterTemplateArguments(TemplateArgsForBuildingFPrime);
NamedDecl *NewParam = transformTemplateParameter(
SemaRef, F->getDeclContext(), TP, Args, FPrimeTemplateParams.size(),
- getTemplateParameterDepth(TP));
+ getDepthAndIndex(TP).first);
FPrimeTemplateParams.push_back(NewParam);
assert(TemplateArgsForBuildingFPrime[FTemplateParamIdx].isNull() &&
More information about the cfe-commits
mailing list