@@ -5199,7 +5199,7 @@ convertTypeTemplateArgumentToTemplate(ASTContext &Context, TypeLoc TLoc) {
51995199}
52005200
52015201bool Sema::CheckTemplateArgument (
5202- NamedDecl *Param, TemplateArgumentLoc &Arg , NamedDecl *Template,
5202+ NamedDecl *Param, TemplateArgumentLoc &ArgLoc , NamedDecl *Template,
52035203 SourceLocation TemplateLoc, SourceLocation RAngleLoc,
52045204 unsigned ArgumentPackIndex,
52055205 SmallVectorImpl<TemplateArgument> &SugaredConverted,
@@ -5208,9 +5208,10 @@ bool Sema::CheckTemplateArgument(
52085208 bool PartialOrderingTTP, bool *MatchedPackOnParmToNonPackOnArg) {
52095209 // Check template type parameters.
52105210 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
5211- return CheckTemplateTypeArgument (TTP, Arg , SugaredConverted,
5211+ return CheckTemplateTypeArgument (TTP, ArgLoc , SugaredConverted,
52125212 CanonicalConverted);
52135213
5214+ const TemplateArgument &Arg = ArgLoc.getArgument ();
52145215 // Check non-type template parameters.
52155216 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
52165217 // Do substitution on the type of the non-type template parameter
@@ -5252,63 +5253,89 @@ bool Sema::CheckTemplateArgument(
52525253 return true ;
52535254 }
52545255
5255- switch (Arg.getArgument ().getKind ()) {
5256- case TemplateArgument::Null:
5257- llvm_unreachable (" Should never see a NULL template argument here" );
5258-
5259- case TemplateArgument::Expression: {
5260- Expr *E = Arg.getArgument ().getAsExpr ();
5256+ auto checkExpr = [&](Expr *E) -> Expr * {
52615257 TemplateArgument SugaredResult, CanonicalResult;
52625258 unsigned CurSFINAEErrors = NumSFINAEErrors;
52635259 ExprResult Res =
52645260 CheckTemplateArgument (NTTP, NTTPType, E, SugaredResult,
52655261 CanonicalResult, PartialOrderingTTP, CTAK);
5266- if (Res.isInvalid ())
5267- return true ;
52685262 // If the current template argument causes an error, give up now.
5269- if (CurSFINAEErrors < NumSFINAEErrors)
5270- return true ;
5263+ if (Res.isInvalid () || CurSFINAEErrors < NumSFINAEErrors)
5264+ return nullptr ;
5265+ SugaredConverted.push_back (SugaredResult);
5266+ CanonicalConverted.push_back (CanonicalResult);
5267+ return Res.get ();
5268+ };
5269+
5270+ switch (Arg.getKind ()) {
5271+ case TemplateArgument::Null:
5272+ llvm_unreachable (" Should never see a NULL template argument here" );
52715273
5274+ case TemplateArgument::Expression: {
5275+ Expr *E = Arg.getAsExpr ();
5276+ Expr *R = checkExpr (E);
5277+ if (!R)
5278+ return true ;
52725279 // If the resulting expression is new, then use it in place of the
52735280 // old expression in the template argument.
5274- if (Res. get () != E) {
5275- TemplateArgument TA (Res. get () );
5276- Arg = TemplateArgumentLoc (TA, Res. get () );
5281+ if (R != E) {
5282+ TemplateArgument TA (R );
5283+ ArgLoc = TemplateArgumentLoc (TA, R );
52775284 }
5278-
5279- SugaredConverted.push_back (SugaredResult);
5280- CanonicalConverted.push_back (CanonicalResult);
52815285 break ;
52825286 }
52835287
5284- case TemplateArgument::Declaration:
5285- case TemplateArgument::Integral:
5288+ // As for the converted NTTP kinds, they still might need another
5289+ // conversion, as the new corresponding parameter might be different.
5290+ // Ideally, we would always perform substitution starting with sugared types
5291+ // and never need these, as we would still have expressions. Since these are
5292+ // needed so rarely, it's probably a better tradeoff to just convert them
5293+ // back to expressions.
5294+ case TemplateArgument::Integral: {
5295+ IntegerLiteral ILE (Context, Arg.getAsIntegral (), Arg.getIntegralType (),
5296+ SourceLocation ());
5297+ if (!checkExpr (&ILE))
5298+ return true ;
5299+ break ;
5300+ }
5301+ case TemplateArgument::Declaration: {
5302+ DeclRefExpr DRE (Context, Arg.getAsDecl (),
5303+ /* RefersToEnclosingVariableOrCapture=*/ false ,
5304+ Arg.getParamTypeForDecl ().getNonReferenceType (),
5305+ VK_LValue, SourceLocation ());
5306+ if (!checkExpr (&DRE))
5307+ return true ;
5308+ break ;
5309+ }
5310+ case TemplateArgument::NullPtr: {
5311+ CXXNullPtrLiteralExpr NPLE (Arg.getNullPtrType (), SourceLocation ());
5312+ if (!checkExpr (&NPLE))
5313+ return true ;
5314+ break ;
5315+ }
52865316 case TemplateArgument::StructuralValue:
5287- case TemplateArgument::NullPtr:
5288- // We've already checked this template argument, so just copy
5289- // it to the list of converted arguments.
5290- SugaredConverted.push_back (Arg.getArgument ());
5291- CanonicalConverted.push_back (
5292- Context.getCanonicalTemplateArgument (Arg.getArgument ()));
5317+ // FIXME: Is it even possible to reach here?
5318+ // If this is actually used, this needs to convert the argument again.
5319+ SugaredConverted.push_back (Arg);
5320+ CanonicalConverted.push_back (Context.getCanonicalTemplateArgument (Arg));
52935321 break ;
52945322
52955323 case TemplateArgument::Template:
52965324 case TemplateArgument::TemplateExpansion:
52975325 // We were given a template template argument. It may not be ill-formed;
52985326 // see below.
5299- if (DependentTemplateName *DTN
5300- = Arg.getArgument ().getAsTemplateOrTemplatePattern ()
5301- .getAsDependentTemplateName ()) {
5327+ if (DependentTemplateName *DTN = Arg.getAsTemplateOrTemplatePattern ()
5328+ .getAsDependentTemplateName ()) {
53025329 // We have a template argument such as \c T::template X, which we
53035330 // parsed as a template template argument. However, since we now
53045331 // know that we need a non-type template argument, convert this
53055332 // template name into an expression.
53065333
53075334 DeclarationNameInfo NameInfo (DTN->getIdentifier (),
5308- Arg .getTemplateNameLoc ());
5335+ ArgLoc .getTemplateNameLoc ());
53095336
53105337 CXXScopeSpec SS;
5311- SS.Adopt (Arg .getTemplateQualifierLoc ());
5338+ SS.Adopt (ArgLoc .getTemplateQualifierLoc ());
53125339 // FIXME: the template-template arg was a DependentTemplateName,
53135340 // so it was provided with a template keyword. However, its source
53145341 // location is not stored in the template argument structure.
@@ -5319,8 +5346,8 @@ bool Sema::CheckTemplateArgument(
53195346
53205347 // If we parsed the template argument as a pack expansion, create a
53215348 // pack expansion expression.
5322- if (Arg.getArgument (). getKind () == TemplateArgument::TemplateExpansion){
5323- E = ActOnPackExpansion (E.get (), Arg .getTemplateEllipsisLoc ());
5349+ if (Arg.getKind () == TemplateArgument::TemplateExpansion) {
5350+ E = ActOnPackExpansion (E.get (), ArgLoc .getTemplateEllipsisLoc ());
53245351 if (E.isInvalid ())
53255352 return true ;
53265353 }
@@ -5340,8 +5367,8 @@ bool Sema::CheckTemplateArgument(
53405367 // We have a template argument that actually does refer to a class
53415368 // template, alias template, or template template parameter, and
53425369 // therefore cannot be a non-type template argument.
5343- Diag (Arg .getLocation (), diag::err_template_arg_must_be_expr)
5344- << Arg .getSourceRange ();
5370+ Diag (ArgLoc .getLocation (), diag::err_template_arg_must_be_expr)
5371+ << ArgLoc .getSourceRange ();
53455372 NoteTemplateParameterLocation (*Param);
53465373
53475374 return true ;
@@ -5357,8 +5384,8 @@ bool Sema::CheckTemplateArgument(
53575384 //
53585385 // We warn specifically about this case, since it can be rather
53595386 // confusing for users.
5360- QualType T = Arg.getArgument (). getAsType ();
5361- SourceRange SR = Arg .getSourceRange ();
5387+ QualType T = Arg.getAsType ();
5388+ SourceRange SR = ArgLoc .getSourceRange ();
53625389 if (T->isFunctionType ())
53635390 Diag (SR.getBegin (), diag::err_template_arg_nontype_ambig) << SR << T;
53645391 else
@@ -5409,34 +5436,33 @@ bool Sema::CheckTemplateArgument(
54095436 // When [the injected-class-name] is used [...] as a template-argument for
54105437 // a template template-parameter [...] it refers to the class template
54115438 // itself.
5412- if (Arg.getArgument (). getKind () == TemplateArgument::Type) {
5439+ if (Arg.getKind () == TemplateArgument::Type) {
54135440 TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate (
5414- Context, Arg .getTypeSourceInfo ()->getTypeLoc ());
5441+ Context, ArgLoc .getTypeSourceInfo ()->getTypeLoc ());
54155442 if (!ConvertedArg.getArgument ().isNull ())
5416- Arg = ConvertedArg;
5443+ ArgLoc = ConvertedArg;
54175444 }
54185445
5419- switch (Arg.getArgument (). getKind ()) {
5446+ switch (Arg.getKind ()) {
54205447 case TemplateArgument::Null:
54215448 llvm_unreachable (" Should never see a NULL template argument here" );
54225449
54235450 case TemplateArgument::Template:
54245451 case TemplateArgument::TemplateExpansion:
5425- if (CheckTemplateTemplateArgument (TempParm, Params, Arg , PartialOrdering,
5452+ if (CheckTemplateTemplateArgument (TempParm, Params, ArgLoc , PartialOrdering,
54265453 MatchedPackOnParmToNonPackOnArg))
54275454 return true ;
54285455
5429- SugaredConverted.push_back (Arg.getArgument ());
5430- CanonicalConverted.push_back (
5431- Context.getCanonicalTemplateArgument (Arg.getArgument ()));
5456+ SugaredConverted.push_back (Arg);
5457+ CanonicalConverted.push_back (Context.getCanonicalTemplateArgument (Arg));
54325458 break ;
54335459
54345460 case TemplateArgument::Expression:
54355461 case TemplateArgument::Type:
54365462 // We have a template template parameter but the template
54375463 // argument does not refer to a template.
5438- Diag (Arg .getLocation (), diag::err_template_arg_must_be_template)
5439- << getLangOpts ().CPlusPlus11 ;
5464+ Diag (ArgLoc .getLocation (), diag::err_template_arg_must_be_template)
5465+ << getLangOpts ().CPlusPlus11 ;
54405466 return true ;
54415467
54425468 case TemplateArgument::Declaration:
0 commit comments