diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index e73dbf85febc..65708706d743 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -6362,9 +6362,8 @@ public: QualType InstantiatedParamType, Expr *Arg, TemplateArgument &Converted, CheckTemplateArgumentKind CTAK = CTAK_Specified); - bool CheckTemplateArgument(TemplateTemplateParmDecl *Param, - TemplateArgumentLoc &Arg, - unsigned ArgumentPackIndex); + bool CheckTemplateTemplateArgument(TemplateParameterList *Params, + TemplateArgumentLoc &Arg); ExprResult BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, @@ -7700,6 +7699,10 @@ public: StmtResult SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs); + TemplateParameterList * + SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, + const MultiLevelTemplateArgumentList &TemplateArgs); + Decl *SubstDecl(Decl *D, DeclContext *Owner, const MultiLevelTemplateArgumentList &TemplateArgs); diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 2e8a7061d718..12a74c1b64bc 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -4617,6 +4617,8 @@ bool Sema::CheckTemplateArgument(NamedDecl *Param, if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) NTTPType = NTTP->getExpansionType(ArgumentPackIndex); + // FIXME: Do we need to substitute into parameters here if they're + // instantiation-dependent but not dependent? if (NTTPType->isDependentType() && !isa(Template) && !Template->getDeclContext()->isDependentContext()) { @@ -4756,9 +4758,15 @@ bool Sema::CheckTemplateArgument(NamedDecl *Param, // Check template template parameters. TemplateTemplateParmDecl *TempParm = cast(Param); + TemplateParameterList *Params = TempParm->getTemplateParameters(); + if (TempParm->isExpandedParameterPack()) + Params = TempParm->getExpansionTemplateParameters(ArgumentPackIndex); + // Substitute into the template parameter list of the template // template parameter, since previously-supplied template arguments // may appear within the template template parameter. + // + // FIXME: Skip this if the parameters aren't instantiation-dependent. { // Set up a template instantiation context. LocalInstantiationScope Scope(*this); @@ -4769,10 +4777,9 @@ bool Sema::CheckTemplateArgument(NamedDecl *Param, return true; TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); - TempParm = cast_or_null( - SubstDecl(TempParm, CurContext, - MultiLevelTemplateArgumentList(TemplateArgs))); - if (!TempParm) + Params = SubstTemplateParams(Params, CurContext, + MultiLevelTemplateArgumentList(TemplateArgs)); + if (!Params) return true; } @@ -4793,7 +4800,7 @@ bool Sema::CheckTemplateArgument(NamedDecl *Param, case TemplateArgument::Template: case TemplateArgument::TemplateExpansion: - if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) + if (CheckTemplateTemplateArgument(Params, Arg)) return true; Converted.push_back(Arg.getArgument()); @@ -6536,9 +6543,8 @@ static void DiagnoseTemplateParameterListArityMismatch( /// /// This routine implements the semantics of C++ [temp.arg.template]. /// It returns true if an error occurred, and false otherwise. -bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, - TemplateArgumentLoc &Arg, - unsigned ArgumentPackIndex) { +bool Sema::CheckTemplateTemplateArgument(TemplateParameterList *Params, + TemplateArgumentLoc &Arg) { TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); TemplateDecl *Template = Name.getAsTemplateDecl(); if (!Template) { @@ -6573,10 +6579,6 @@ bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, << Template; } - TemplateParameterList *Params = Param->getTemplateParameters(); - if (Param->isExpandedParameterPack()) - Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); - // C++1z [temp.arg.template]p3: (DR 150) // A template-argument matches a template template-parameter P when P // is at least as specialized as the template-argument A. diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 800956dd535d..67204356080a 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -3118,6 +3118,13 @@ TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { return InstL; } +TemplateParameterList * +Sema::SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, + const MultiLevelTemplateArgumentList &TemplateArgs) { + TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); + return Instantiator.SubstTemplateParams(Params); +} + /// \brief Instantiate the declaration of a class template partial /// specialization. /// diff --git a/clang/test/CXX/drs/dr10xx.cpp b/clang/test/CXX/drs/dr10xx.cpp index e1db9ef54adf..7f5fd8c7ec27 100644 --- a/clang/test/CXX/drs/dr10xx.cpp +++ b/clang/test/CXX/drs/dr10xx.cpp @@ -31,9 +31,8 @@ namespace dr1004 { // dr1004: 5 // This example (from the standard) is actually ill-formed, because // name lookup of "T::template A" names the constructor. - // FIXME: Only issue one diagnostic for this case. - template class U = T::template A> struct Third { }; // expected-error 2{{is a constructor name}} - Third > t; // expected-note {{in instantiation of}} expected-note {{while substituting}} expected-note {{while checking}} + template class U = T::template A> struct Third { }; // expected-error {{is a constructor name}} + Third > t; // expected-note {{in instantiation of default argument}} } namespace dr1048 { // dr1048: 3.6 diff --git a/clang/test/SemaTemplate/temp-param-subst-linear.cpp b/clang/test/SemaTemplate/temp-param-subst-linear.cpp new file mode 100644 index 000000000000..fd93aa568553 --- /dev/null +++ b/clang/test/SemaTemplate/temp-param-subst-linear.cpp @@ -0,0 +1,56 @@ +// RUN: %clang_cc1 -std=c++17 %s -verify +// expected-no-diagnostics + +// This test attempts to ensure that the below template parameter pack +// splitting technique executes in linear time in the number of template +// parameters. The size of the list below is selected so as to execute +// relatively quickly on a "good" compiler and to time out otherwise. + +template struct TypeList; + +namespace detail { +template using Unsigned = unsigned; +template using ListOfNUnsignedsImpl = TypeList...>; +template using ListOfNUnsigneds = + __make_integer_seq; + +template struct TypeWrapper { + template using AsTemplate = T; +}; + +template struct Splitter { + template class ...L, + template class ...R> struct Split { + using Left = TypeList...>; + using Right = TypeList...>; + }; +}; +} + +template> +struct SplitAtIndex; + +template +struct SplitAtIndex, N, TypeList> : + detail::Splitter:: + template Split::template AsTemplate...> {}; + +template struct Rep : Rep::type, 1> {}; +template struct Rep, 1> { typedef TypeList type; }; + +using Ints = Rep, 14>::type; + +template extern int Size; +template constexpr int Size> = sizeof...(T); + +using Left = SplitAtIndex / 2>::Left; +using Right = SplitAtIndex / 2>::Right; +static_assert(Size == 8192); +static_assert(Size == 8192); + +template struct Concat; +template struct Concat, TypeList> { + using type = TypeList; +}; + +using Ints = Concat::type;