@@ -377,16 +377,18 @@ SubstitutionInTemplateArguments(
377
377
? Constraint.getPackSubstitutionIndex ()
378
378
: PackSubstitutionIndex);
379
379
if (S.SubstTemplateArgumentsInParameterMapping (
380
- Constraint.getParameterMapping (), MLTAL, SubstArgs) ||
380
+ Constraint.getParameterMapping (), Constraint.getBeginLoc (), MLTAL,
381
+ SubstArgs) ||
381
382
Trap.hasErrorOccurred ())
382
383
return std::nullopt ;
383
- // Sema::CheckTemplateArgumentInfo CTAI;
384
- // auto *TD = const_cast<TemplateDecl *>(
385
- // cast<TemplateDecl>(Constraint.getConstraintDecl()));
386
- // if (S.CheckTemplateArgumentList(TD, TD->getLocation(), SubstArgs,
387
- // /*DefaultArguments=*/{},
388
- // /*PartialTemplateArgs=*/true, CTAI))
389
- // return std::nullopt;
384
+ Sema::CheckTemplateArgumentInfo CTAI;
385
+ auto *TD = const_cast <TemplateDecl *>(
386
+ cast<TemplateDecl>(Constraint.getConstraintDecl ()));
387
+ if (S.CheckTemplateArgumentList (TD, Constraint.getUsedTemplateParamList (),
388
+ TD->getLocation (), SubstArgs,
389
+ /* DefaultArguments=*/ {},
390
+ /* PartialTemplateArgs=*/ false , CTAI))
391
+ return std::nullopt ;
390
392
NormalizedConstraint::OccurenceList Used =
391
393
Constraint.mappingOccurenceList ();
392
394
SubstitutedOuterMost =
@@ -396,8 +398,10 @@ SubstitutionInTemplateArguments(
396
398
if (Used[I])
397
399
// SubstitutedOuterMost[I].dump();
398
400
// SubstArgs[MappedIndex].getArgument().dump();
401
+ // Arg = S.Context.getCanonicalTemplateArgument(
402
+ // SubstArgs[MappedIndex++].getArgument());
399
403
Arg = S.Context .getCanonicalTemplateArgument (
400
- SubstArgs [MappedIndex++]. getArgument () );
404
+ CTAI. SugaredConverted [MappedIndex++]);
401
405
if (I < SubstitutedOuterMost.size ())
402
406
SubstitutedOuterMost[I] = Arg;
403
407
else
@@ -1607,17 +1611,28 @@ substituteParameterMappings(Sema &S, NormalizedConstraintWithParamMapping &N,
1607
1611
}
1608
1612
TemplateArgumentLoc *TempArgs =
1609
1613
new (S.Context ) TemplateArgumentLoc[OccurringIndices.count ()];
1614
+ llvm::SmallVector<NamedDecl *> UsedParams;
1610
1615
for (unsigned I = 0 , J = 0 , C = TemplateParams->size (); I != C; ++I) {
1611
1616
SourceLocation Loc = ArgsAsWritten->NumTemplateArgs > I
1612
1617
? ArgsAsWritten->arguments ()[I].getLocation ()
1613
1618
: SourceLocation ();
1614
- if (OccurringIndices[I])
1615
- new (&(TempArgs)[J++]) TemplateArgumentLoc (
1616
- S.getIdentityTemplateArgumentLoc (TemplateParams->begin ()[I], Loc));
1619
+ if (OccurringIndices[I]) {
1620
+ NamedDecl *Param = TemplateParams->begin ()[I];
1621
+ new (&(TempArgs)[J])
1622
+ TemplateArgumentLoc (S.getIdentityTemplateArgumentLoc (Param, Loc));
1623
+ UsedParams.push_back (Param);
1624
+ J++;
1625
+ }
1617
1626
}
1627
+ auto *UsedList = TemplateParameterList::Create (
1628
+ S.Context , TemplateParams->getTemplateLoc (),
1629
+ TemplateParams->getLAngleLoc (), UsedParams,
1630
+ /* RAngleLoc=*/ SourceLocation (),
1631
+ /* RequiresClause=*/ nullptr );
1618
1632
N.updateParameterMapping (OccurringIndices,
1619
1633
MutableArrayRef<TemplateArgumentLoc>{
1620
- TempArgs, OccurringIndices.count ()});
1634
+ TempArgs, OccurringIndices.count ()},
1635
+ UsedList);
1621
1636
}
1622
1637
SourceLocation InstLocBegin =
1623
1638
ArgsAsWritten->arguments ().empty ()
@@ -1634,28 +1649,39 @@ substituteParameterMappings(Sema &S, NormalizedConstraintWithParamMapping &N,
1634
1649
{InstLocBegin, InstLocEnd});
1635
1650
if (Inst.isInvalid ())
1636
1651
return true ;
1637
- if (S.SubstTemplateArgumentsInParameterMapping (N.getParameterMapping (), MLTAL,
1638
- SubstArgs))
1652
+ // TransformTemplateArguments is unable to preserve the source location of a
1653
+ // pack. The SourceLocation is necessary for the instantiation location.
1654
+ // FIXME: The BaseLoc will be used as the location of the pack expansion,
1655
+ // which is wrong.
1656
+ if (S.SubstTemplateArgumentsInParameterMapping (
1657
+ N.getParameterMapping (), N.getBeginLoc (), MLTAL, SubstArgs))
1658
+ return true ;
1659
+ Sema::CheckTemplateArgumentInfo CTAI;
1660
+ auto *TD =
1661
+ const_cast <TemplateDecl *>(cast<TemplateDecl>(N.getConstraintDecl ()));
1662
+ if (S.CheckTemplateArgumentList (TD, N.getUsedTemplateParamList (),
1663
+ TD->getLocation (), SubstArgs,
1664
+ /* DefaultArguments=*/ {},
1665
+ /* PartialTemplateArgs=*/ false , CTAI))
1639
1666
return true ;
1640
- // Sema::CheckTemplateArgumentInfo CTAI;
1641
- // auto *TD =
1642
- // const_cast<TemplateDecl *>(cast<TemplateDecl>(N.getConstraintDecl()));
1643
- // if (S.CheckTemplateArgumentList(TD, TD->getLocation(), SubstArgs,
1644
- // /*DefaultArguments=*/{},
1645
- // /*PartialTemplateArgs=*/true, CTAI))
1646
- // return true;
1647
1667
TemplateArgumentLoc *TempArgs =
1648
- new (S.Context ) TemplateArgumentLoc[SubstArgs.size ()];
1649
- // for (unsigned I = 0; I < CTAI.SugaredConverted.size(); ++I)
1650
- // TempArgs[I] = S.getTrivialTemplateArgumentLoc(CTAI.SugaredConverted[I],
1651
- // QualType(), SourceLocation());
1652
- llvm::copy (SubstArgs.arguments (), TempArgs);
1653
- N.updateParameterMapping (
1654
- N.mappingOccurenceList (),
1655
- MutableArrayRef<TemplateArgumentLoc>(TempArgs, SubstArgs.size ()));
1656
- // N.updateParameterMapping(N.mappingOccurenceList(),
1657
- // MutableArrayRef<TemplateArgumentLoc>(
1658
- // TempArgs, CTAI.SugaredConverted.size()));
1668
+ new (S.Context ) TemplateArgumentLoc[CTAI.SugaredConverted .size ()];
1669
+ for (unsigned I = 0 ; I < CTAI.SugaredConverted .size (); ++I) {
1670
+ SourceLocation Loc;
1671
+ // If this is an empty pack, we have no corresponding SubstArgs.
1672
+ if (I < SubstArgs.size ())
1673
+ Loc = SubstArgs.arguments ()[I].getLocation ();
1674
+ TempArgs[I] = S.getTrivialTemplateArgumentLoc (CTAI.SugaredConverted [I],
1675
+ QualType (), Loc);
1676
+ }
1677
+ // llvm::copy(SubstArgs.arguments(), TempArgs);
1678
+ // N.updateParameterMapping(
1679
+ // N.mappingOccurenceList(),
1680
+ // MutableArrayRef<TemplateArgumentLoc>(TempArgs, SubstArgs.size()));
1681
+ N.updateParameterMapping (N.mappingOccurenceList (),
1682
+ MutableArrayRef<TemplateArgumentLoc>(
1683
+ TempArgs, CTAI.SugaredConverted .size ()),
1684
+ N.getUsedTemplateParamList ());
1659
1685
return false ;
1660
1686
}
1661
1687
0 commit comments