@@ -377,24 +377,36 @@ 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 ;
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 ;
383
392
NormalizedConstraint::OccurenceList Used =
384
393
Constraint.mappingOccurenceList ();
385
394
SubstitutedOuterMost =
386
395
llvm::to_vector_of<TemplateArgument>(MLTAL.getOutermost ());
387
- for (unsigned I = 0 , MappedIndex = 0 ; I < SubstArgs.size (); I++)
388
- if (I < Used.size () && Used[I]) {
396
+ for (unsigned I = 0 , MappedIndex = 0 ; I < Used.size (); I++) {
397
+ TemplateArgument Arg;
398
+ if (Used[I])
389
399
// SubstitutedOuterMost[I].dump();
390
400
// SubstArgs[MappedIndex].getArgument().dump();
391
- TemplateArgument Arg = S.Context .getCanonicalTemplateArgument (
392
- SubstArgs[MappedIndex++].getArgument ());
393
- if (I < SubstitutedOuterMost.size ())
394
- SubstitutedOuterMost[I] = Arg;
395
- else
396
- SubstitutedOuterMost.push_back (Arg);
397
- }
401
+ // Arg = S.Context.getCanonicalTemplateArgument(
402
+ // SubstArgs[MappedIndex++].getArgument());
403
+ Arg = S.Context .getCanonicalTemplateArgument (
404
+ CTAI.SugaredConverted [MappedIndex++]);
405
+ if (I < SubstitutedOuterMost.size ())
406
+ SubstitutedOuterMost[I] = Arg;
407
+ else
408
+ SubstitutedOuterMost.push_back (Arg);
409
+ }
398
410
MLTAL.replaceOutermostTemplateArguments (
399
411
const_cast <NamedDecl *>(Constraint.getConstraintDecl ()),
400
412
SubstitutedOuterMost);
@@ -681,7 +693,7 @@ static bool calculateConstraintSatisfaction(
681
693
Ok = calculateConstraintSatisfaction (S, Constraint.getRHS (), Template,
682
694
TemplateNameLoc, MLTAL, Satisfaction,
683
695
PackSubstitutionIndex);
684
- if (Ok && Satisfaction.IsSatisfied && !Satisfaction.ContainsErrors )
696
+ if (Ok && Satisfaction.IsSatisfied && !Satisfaction.ContainsErrors )
685
697
Satisfaction.Details .erase (Satisfaction.Details .begin () + EffectiveDetailEndIndex,
686
698
Satisfaction.Details .end ());
687
699
return Ok;
@@ -1574,26 +1586,37 @@ substituteParameterMappings(Sema &S, NormalizedConstraintWithParamMapping &N,
1574
1586
/* OnlyDeduced=*/ false ,
1575
1587
/* Depth=*/ 0 , OccurringIndices);
1576
1588
} else if (N.getKind () == NormalizedConstraint::ConstraintKind::ConceptId) {
1577
- auto Args = static_cast <ConceptIdConstraint &>(N)
1578
- .getConceptId ()
1579
- ->getTemplateArgsAsWritten ();
1589
+ auto * Args = static_cast <ConceptIdConstraint &>(N)
1590
+ .getConceptId ()
1591
+ ->getTemplateArgsAsWritten ();
1580
1592
if (Args)
1581
1593
S.MarkUsedTemplateParameters (Args->arguments (),
1582
1594
/* Depth=*/ 0 , OccurringIndices);
1583
1595
}
1584
1596
TemplateArgumentLoc *TempArgs =
1585
1597
new (S.Context ) TemplateArgumentLoc[OccurringIndices.count ()];
1598
+ llvm::SmallVector<NamedDecl *> UsedParams;
1586
1599
for (unsigned I = 0 , J = 0 , C = TemplateParams->size (); I != C; ++I) {
1587
1600
SourceLocation Loc = ArgsAsWritten->NumTemplateArgs > I
1588
1601
? ArgsAsWritten->arguments ()[I].getLocation ()
1589
1602
: SourceLocation ();
1590
- if (OccurringIndices[I])
1591
- new (&(TempArgs)[J++]) TemplateArgumentLoc (
1592
- 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
+ }
1593
1610
}
1611
+ auto *UsedList = TemplateParameterList::Create (
1612
+ S.Context , TemplateParams->getTemplateLoc (),
1613
+ TemplateParams->getLAngleLoc (), UsedParams,
1614
+ /* RAngleLoc=*/ SourceLocation (),
1615
+ /* RequiresClause=*/ nullptr );
1594
1616
N.updateParameterMapping (OccurringIndices,
1595
1617
MutableArrayRef<TemplateArgumentLoc>{
1596
- TempArgs, OccurringIndices.count ()});
1618
+ TempArgs, OccurringIndices.count ()},
1619
+ UsedList);
1597
1620
}
1598
1621
SourceLocation InstLocBegin =
1599
1622
ArgsAsWritten->arguments ().empty ()
@@ -1610,15 +1633,39 @@ substituteParameterMappings(Sema &S, NormalizedConstraintWithParamMapping &N,
1610
1633
{InstLocBegin, InstLocEnd});
1611
1634
if (Inst.isInvalid ())
1612
1635
return true ;
1613
- if (S.SubstTemplateArgumentsInParameterMapping (N.getParameterMapping (), MLTAL,
1614
- 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))
1615
1650
return true ;
1616
1651
TemplateArgumentLoc *TempArgs =
1617
- new (S.Context ) TemplateArgumentLoc[SubstArgs.size ()];
1618
- llvm::copy (SubstArgs.arguments (), TempArgs);
1619
- N.updateParameterMapping (
1620
- N.mappingOccurenceList (),
1621
- MutableArrayRef<TemplateArgumentLoc>(TempArgs, SubstArgs.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 ());
1622
1669
return false ;
1623
1670
}
1624
1671
@@ -1627,12 +1674,10 @@ substituteParameterMappings(Sema &S, ConceptIdConstraint &N,
1627
1674
const MultiLevelTemplateArgumentList &MLTAL,
1628
1675
const ASTTemplateArgumentListInfo *ArgsAsWritten) {
1629
1676
1630
- {
1631
- if (N.getConstraintDecl ()) {
1632
- substituteParameterMappings (
1633
- S, static_cast <NormalizedConstraintWithParamMapping &>(N), MLTAL,
1634
- ArgsAsWritten);
1635
- }
1677
+ if (N.getConstraintDecl ()) {
1678
+ substituteParameterMappings (
1679
+ S, static_cast <NormalizedConstraintWithParamMapping &>(N), MLTAL,
1680
+ ArgsAsWritten);
1636
1681
}
1637
1682
return substituteParameterMappings (S, N.getNormalizedConstraint (), MLTAL,
1638
1683
ArgsAsWritten);
0 commit comments