@@ -338,7 +338,7 @@ Response HandleFunctionTemplateDecl(Sema &SemaRef,
338338 const_cast <FunctionTemplateDecl *>(FTD),
339339 const_cast <FunctionTemplateDecl *>(FTD)->getInjectedTemplateArgs (
340340 SemaRef.Context ),
341- /* Final=*/ false );
341+ /* Final=*/ false , /* InjectedTemplateParams= */ true );
342342
343343 NestedNameSpecifier *NNS = FTD->getTemplatedDecl ()->getQualifier ();
344344
@@ -363,18 +363,20 @@ Response HandleFunctionTemplateDecl(Sema &SemaRef,
363363 // This meets the contract in
364364 // TreeTransform::TryExpandParameterPacks that the template arguments
365365 // for unexpanded parameters should be of a Pack kind.
366+ bool Injected = false ;
366367 if (TSTy->isCurrentInstantiation ()) {
367368 auto *RD = TSTy->getCanonicalTypeInternal ()->getAsCXXRecordDecl ();
368- if (ClassTemplateDecl *CTD = RD->getDescribedClassTemplate ())
369+ if (ClassTemplateDecl *CTD = RD->getDescribedClassTemplate ()) {
369370 Arguments = CTD->getInjectedTemplateArgs (SemaRef.Context );
370- else if (auto *Specialization =
371- dyn_cast<ClassTemplateSpecializationDecl>(RD))
371+ Injected = true ;
372+ } else if (auto *Specialization =
373+ dyn_cast<ClassTemplateSpecializationDecl>(RD))
372374 Arguments =
373375 Specialization->getTemplateInstantiationArgs ().asArray ();
374376 }
375377 Result.addOuterTemplateArguments (
376378 TSTy->getTemplateName ().getAsTemplateDecl (), Arguments,
377- /* Final=*/ false );
379+ /* Final=*/ false , /* InjectedTemplateParams= */ Injected );
378380 }
379381 }
380382
@@ -399,7 +401,7 @@ Response HandleRecordDecl(Sema &SemaRef, const CXXRecordDecl *Rec,
399401 Result.addOuterTemplateArguments (
400402 const_cast <CXXRecordDecl *>(Rec),
401403 ClassTemplate->getInjectedTemplateArgs (SemaRef.Context ),
402- /* Final=*/ false );
404+ /* Final=*/ false , /* InjectedTemplateParams= */ true );
403405 }
404406
405407 if (const MemberSpecializationInfo *MSInfo =
@@ -1476,8 +1478,8 @@ namespace {
14761478 }
14771479 }
14781480
1479- TemplateArgument
1480- getTemplateArgumentPackPatternForRewrite ( const TemplateArgument &TA) {
1481+ TemplateArgument getTemplateArgumentorUnsubstitutedExpansionPattern (
1482+ const TemplateArgument &TA) {
14811483 if (TA.getKind () != TemplateArgument::Pack)
14821484 return TA;
14831485 if (SemaRef.ArgPackSubstIndex )
@@ -1902,7 +1904,9 @@ Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
19021904
19031905 TemplateArgument Arg = TemplateArgs (TTP->getDepth (), TTP->getPosition ());
19041906
1905- if (TTP->isParameterPack ()) {
1907+ if (TemplateArgs.ArgumentsAreInjectedParameters (TTP->getDepth ())) {
1908+ Arg = getTemplateArgumentorUnsubstitutedExpansionPattern (Arg);
1909+ } else if (TTP->isParameterPack ()) {
19061910 assert (Arg.getKind () == TemplateArgument::Pack &&
19071911 " Missing argument pack" );
19081912 Arg = getPackSubstitutedTemplateArgument (getSema (), Arg);
@@ -2054,20 +2058,23 @@ TemplateName TemplateInstantiator::TransformTemplateName(
20542058 if (TemplateArgs.isRewrite ()) {
20552059 // We're rewriting the template parameter as a reference to another
20562060 // template parameter.
2057- Arg = getTemplateArgumentPackPatternForRewrite (Arg);
2061+ Arg = getTemplateArgumentorUnsubstitutedExpansionPattern (Arg);
20582062 assert (Arg.getKind () == TemplateArgument::Template &&
20592063 " unexpected nontype template argument kind in template rewrite" );
20602064 return Arg.getAsTemplate ();
20612065 }
20622066
2063- auto [AssociatedDecl, Final] =
2067+ auto [AssociatedDecl, Final, ArgumentsAreInjectedTemplateParams ] =
20642068 TemplateArgs.getAssociatedDecl (TTP->getDepth ());
20652069 UnsignedOrNone PackIndex = std::nullopt ;
20662070 if (TTP->isParameterPack ()) {
20672071 assert (Arg.getKind () == TemplateArgument::Pack &&
20682072 " Missing argument pack" );
20692073
2070- if (!getSema ().ArgPackSubstIndex ) {
2074+ if (ArgumentsAreInjectedTemplateParams)
2075+ Arg = getTemplateArgumentorUnsubstitutedExpansionPattern (Arg);
2076+
2077+ else if (!getSema ().ArgPackSubstIndex ) {
20712078 // We have the template argument pack to substitute, but we're not
20722079 // actually expanding the enclosing pack expansion yet. So, just
20732080 // keep the entire argument pack.
@@ -2131,15 +2138,15 @@ TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
21312138 if (TemplateArgs.isRewrite ()) {
21322139 // We're rewriting the template parameter as a reference to another
21332140 // template parameter.
2134- Arg = getTemplateArgumentPackPatternForRewrite (Arg);
2141+ Arg = getTemplateArgumentorUnsubstitutedExpansionPattern (Arg);
21352142 assert (Arg.getKind () == TemplateArgument::Expression &&
21362143 " unexpected nontype template argument kind in template rewrite" );
21372144 // FIXME: This can lead to the same subexpression appearing multiple times
21382145 // in a complete expression.
21392146 return Arg.getAsExpr ();
21402147 }
21412148
2142- auto [AssociatedDecl, Final] =
2149+ auto [AssociatedDecl, Final, ArgumentsAreInjectedTemplateParams ] =
21432150 TemplateArgs.getAssociatedDecl (NTTP->getDepth ());
21442151 UnsignedOrNone PackIndex = std::nullopt ;
21452152 if (NTTP->isParameterPack ()) {
@@ -2584,22 +2591,30 @@ TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
25842591 if (TemplateArgs.isRewrite ()) {
25852592 // We're rewriting the template parameter as a reference to another
25862593 // template parameter.
2587- Arg = getTemplateArgumentPackPatternForRewrite (Arg);
2594+ Arg = getTemplateArgumentorUnsubstitutedExpansionPattern (Arg);
25882595 assert (Arg.getKind () == TemplateArgument::Type &&
25892596 " unexpected nontype template argument kind in template rewrite" );
25902597 QualType NewT = Arg.getAsType ();
25912598 TLB.pushTrivial (SemaRef.Context , NewT, TL.getNameLoc ());
25922599 return NewT;
25932600 }
25942601
2595- auto [AssociatedDecl, Final] =
2602+ auto [AssociatedDecl, Final, ArgumentsAreInjectedTemplateParams ] =
25962603 TemplateArgs.getAssociatedDecl (T->getDepth ());
25972604 UnsignedOrNone PackIndex = std::nullopt ;
25982605 if (T->isParameterPack ()) {
25992606 assert (Arg.getKind () == TemplateArgument::Pack &&
26002607 " Missing argument pack" );
26012608
26022609 if (!getSema ().ArgPackSubstIndex ) {
2610+
2611+ if (ArgumentsAreInjectedTemplateParams) {
2612+ TemplateTypeParmTypeLoc NewTL =
2613+ TLB.push <TemplateTypeParmTypeLoc>(TL.getType ());
2614+ NewTL.setNameLoc (TL.getNameLoc ());
2615+ return TL.getType ();
2616+ }
2617+
26032618 // We have the template argument pack, but we're not expanding the
26042619 // enclosing pack expansion yet. Just save the template argument
26052620 // pack for later substitution.
0 commit comments