@@ -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
@@ -1591,17 +1595,28 @@ substituteParameterMappings(Sema &S, NormalizedConstraintWithParamMapping &N,
1591
1595
}
1592
1596
TemplateArgumentLoc *TempArgs =
1593
1597
new (S.Context ) TemplateArgumentLoc[OccurringIndices.count ()];
1598
+ llvm::SmallVector<NamedDecl *> UsedParams;
1594
1599
for (unsigned I = 0 , J = 0 , C = TemplateParams->size (); I != C; ++I) {
1595
1600
SourceLocation Loc = ArgsAsWritten->NumTemplateArgs > I
1596
1601
? ArgsAsWritten->arguments ()[I].getLocation ()
1597
1602
: SourceLocation ();
1598
- if (OccurringIndices[I])
1599
- new (&(TempArgs)[J++]) TemplateArgumentLoc (
1600
- S.getIdentityTemplateArgumentLoc (TemplateParams->begin ()[I], Loc));
1603
+ if (OccurringIndices[I]) {
1604
+ NamedDecl *Param = TemplateParams->begin ()[I];
1605
+ new (&(TempArgs)[J])
1606
+ TemplateArgumentLoc (S.getIdentityTemplateArgumentLoc (Param, Loc));
1607
+ UsedParams.push_back (Param);
1608
+ J++;
1609
+ }
1601
1610
}
1611
+ auto *UsedList = TemplateParameterList::Create (
1612
+ S.Context , TemplateParams->getTemplateLoc (),
1613
+ TemplateParams->getLAngleLoc (), UsedParams,
1614
+ /* RAngleLoc=*/ SourceLocation (),
1615
+ /* RequiresClause=*/ nullptr );
1602
1616
N.updateParameterMapping (OccurringIndices,
1603
1617
MutableArrayRef<TemplateArgumentLoc>{
1604
- TempArgs, OccurringIndices.count ()});
1618
+ TempArgs, OccurringIndices.count ()},
1619
+ UsedList);
1605
1620
}
1606
1621
SourceLocation InstLocBegin =
1607
1622
ArgsAsWritten->arguments ().empty ()
@@ -1618,28 +1633,39 @@ substituteParameterMappings(Sema &S, NormalizedConstraintWithParamMapping &N,
1618
1633
{InstLocBegin, InstLocEnd});
1619
1634
if (Inst.isInvalid ())
1620
1635
return true ;
1621
- if (S.SubstTemplateArgumentsInParameterMapping (N.getParameterMapping (), MLTAL,
1622
- SubstArgs))
1636
+ // TransformTemplateArguments is unable to preserve the source location of a
1637
+ // pack. The SourceLocation is necessary for the instantiation location.
1638
+ // FIXME: The BaseLoc will be used as the location of the pack expansion,
1639
+ // which is wrong.
1640
+ if (S.SubstTemplateArgumentsInParameterMapping (
1641
+ N.getParameterMapping (), N.getBeginLoc (), MLTAL, SubstArgs))
1642
+ return true ;
1643
+ Sema::CheckTemplateArgumentInfo CTAI;
1644
+ auto *TD =
1645
+ const_cast <TemplateDecl *>(cast<TemplateDecl>(N.getConstraintDecl ()));
1646
+ if (S.CheckTemplateArgumentList (TD, N.getUsedTemplateParamList (),
1647
+ TD->getLocation (), SubstArgs,
1648
+ /* DefaultArguments=*/ {},
1649
+ /* PartialTemplateArgs=*/ false , CTAI))
1623
1650
return true ;
1624
- // Sema::CheckTemplateArgumentInfo CTAI;
1625
- // auto *TD =
1626
- // const_cast<TemplateDecl *>(cast<TemplateDecl>(N.getConstraintDecl()));
1627
- // if (S.CheckTemplateArgumentList(TD, TD->getLocation(), SubstArgs,
1628
- // /*DefaultArguments=*/{},
1629
- // /*PartialTemplateArgs=*/true, CTAI))
1630
- // return true;
1631
1651
TemplateArgumentLoc *TempArgs =
1632
- new (S.Context ) TemplateArgumentLoc[SubstArgs.size ()];
1633
- // for (unsigned I = 0; I < CTAI.SugaredConverted.size(); ++I)
1634
- // TempArgs[I] = S.getTrivialTemplateArgumentLoc(CTAI.SugaredConverted[I],
1635
- // QualType(), SourceLocation());
1636
- llvm::copy (SubstArgs.arguments (), TempArgs);
1637
- N.updateParameterMapping (
1638
- N.mappingOccurenceList (),
1639
- MutableArrayRef<TemplateArgumentLoc>(TempArgs, SubstArgs.size ()));
1640
- // N.updateParameterMapping(N.mappingOccurenceList(),
1641
- // MutableArrayRef<TemplateArgumentLoc>(
1642
- // TempArgs, CTAI.SugaredConverted.size()));
1652
+ new (S.Context ) TemplateArgumentLoc[CTAI.SugaredConverted .size ()];
1653
+ for (unsigned I = 0 ; I < CTAI.SugaredConverted .size (); ++I) {
1654
+ SourceLocation Loc;
1655
+ // If this is an empty pack, we have no corresponding SubstArgs.
1656
+ if (I < SubstArgs.size ())
1657
+ Loc = SubstArgs.arguments ()[I].getLocation ();
1658
+ TempArgs[I] = S.getTrivialTemplateArgumentLoc (CTAI.SugaredConverted [I],
1659
+ QualType (), Loc);
1660
+ }
1661
+ // llvm::copy(SubstArgs.arguments(), TempArgs);
1662
+ // N.updateParameterMapping(
1663
+ // N.mappingOccurenceList(),
1664
+ // MutableArrayRef<TemplateArgumentLoc>(TempArgs, SubstArgs.size()));
1665
+ N.updateParameterMapping (N.mappingOccurenceList (),
1666
+ MutableArrayRef<TemplateArgumentLoc>(
1667
+ TempArgs, CTAI.SugaredConverted .size ()),
1668
+ N.getUsedTemplateParamList ());
1643
1669
return false ;
1644
1670
}
1645
1671
0 commit comments