Skip to content

Commit 2e9061b

Browse files
committed
Don't call TransformFunctionTypeParams; expand the parameters ourselves
1 parent f0b1e7c commit 2e9061b

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,18 +1956,47 @@ bool TemplateInstantiator::maybeInstantiateFunctionParameterToScope(
19561956
ParmVarDecl *OldParm) {
19571957
if (SemaRef.CurrentInstantiationScope->findInstantiationUnsafe(OldParm))
19581958
return false;
1959-
// Make sure the instantiated parameters are owned by the function
1960-
// declaration.
1959+
// We're instantiating a function parameter whose associated function template
1960+
// has not been instantiated at this point for constraint evaluation, so make
1961+
// sure the instantiated parameters are owned by a function declaration such
1962+
// that they can be correctly 'captured' in tryCaptureVariable().
19611963
Sema::ContextRAII Context(SemaRef, OldParm->getDeclContext());
19621964

1963-
SmallVector<QualType> PTypes;
1964-
Sema::ExtParameterInfoBuilder TInfoBuilder;
1965+
if (!OldParm->isParameterPack())
1966+
return !TransformFunctionTypeParam(OldParm, /*indexAdjustment=*/0,
1967+
/*NumExpansions=*/std::nullopt,
1968+
/*ExpectParameterPack=*/false);
1969+
1970+
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1971+
1972+
// Find the parameter packs that could be expanded.
1973+
TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc();
1974+
PackExpansionTypeLoc ExpansionTL = TL.castAs<PackExpansionTypeLoc>();
1975+
TypeLoc Pattern = ExpansionTL.getPatternLoc();
1976+
SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1977+
assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
1978+
1979+
bool ShouldExpand = false;
1980+
bool RetainExpansion = false;
1981+
std::optional<unsigned> OrigNumExpansions =
1982+
ExpansionTL.getTypePtr()->getNumExpansions();
1983+
std::optional<unsigned> NumExpansions = OrigNumExpansions;
1984+
if (TryExpandParameterPacks(ExpansionTL.getEllipsisLoc(),
1985+
Pattern.getSourceRange(), Unexpanded,
1986+
ShouldExpand, RetainExpansion, NumExpansions))
1987+
return true;
19651988

1966-
return inherited::TransformFunctionTypeParams(
1967-
/*Loc=*/SourceLocation(), /*Params=*/OldParm, /*ParamTypes=*/nullptr,
1968-
/*ParamInfos=*/nullptr, /*PTypes=*/PTypes, /*PVars=*/nullptr,
1969-
TInfoBuilder, /*LastParamTransformed=*/nullptr,
1970-
/*IgnoreParameterIndex=*/true);
1989+
assert(ShouldExpand && !RetainExpansion &&
1990+
"Shouldn't preserve pack expansion when evaluating constraints");
1991+
ExpandingFunctionParameterPack(OldParm);
1992+
for (unsigned I = 0; I != *NumExpansions; ++I) {
1993+
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), I);
1994+
if (!TransformFunctionTypeParam(OldParm, /*indexAdjustment=*/0,
1995+
/*NumExpansions=*/OrigNumExpansions,
1996+
/*ExpectParameterPack=*/false))
1997+
return true;
1998+
}
1999+
return false;
19712000
}
19722001

19732002
Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {

clang/lib/Sema/TreeTransform.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,7 @@ class TreeTransform {
722722
const QualType *ParamTypes,
723723
const FunctionProtoType::ExtParameterInfo *ParamInfos,
724724
SmallVectorImpl<QualType> &PTypes, SmallVectorImpl<ParmVarDecl *> *PVars,
725-
Sema::ExtParameterInfoBuilder &PInfos, unsigned *LastParamTransformed,
726-
bool IgnoreParameterIndex = false);
725+
Sema::ExtParameterInfoBuilder &PInfos, unsigned *LastParamTransformed);
727726

728727
bool TransformFunctionTypeParams(
729728
SourceLocation Loc, ArrayRef<ParmVarDecl *> Params,
@@ -6090,16 +6089,16 @@ bool TreeTransform<Derived>::TransformFunctionTypeParams(
60906089
const FunctionProtoType::ExtParameterInfo *ParamInfos,
60916090
SmallVectorImpl<QualType> &OutParamTypes,
60926091
SmallVectorImpl<ParmVarDecl *> *PVars,
6093-
Sema::ExtParameterInfoBuilder &PInfos, unsigned *LastParamTransformed,
6094-
bool IgnoreParameterIndex) {
6092+
Sema::ExtParameterInfoBuilder &PInfos,
6093+
unsigned *LastParamTransformed) {
60956094
int indexAdjustment = 0;
60966095

60976096
unsigned NumParams = Params.size();
60986097
for (unsigned i = 0; i != NumParams; ++i) {
60996098
if (LastParamTransformed)
61006099
*LastParamTransformed = i;
61016100
if (ParmVarDecl *OldParm = Params[i]) {
6102-
assert(IgnoreParameterIndex || OldParm->getFunctionScopeIndex() == i);
6101+
assert(OldParm->getFunctionScopeIndex() == i);
61036102

61046103
std::optional<unsigned> NumExpansions;
61056104
ParmVarDecl *NewParm = nullptr;

0 commit comments

Comments
 (0)