From 9cc56bc373a32a2def1e752b33dd95b1200a994a Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 8 Mar 2018 01:07:33 +0000 Subject: [PATCH] When substituting previously-checked template arguments into a template template parameter that is an expanded parameter pack, only substitute into the current slice, not the entire pack. This reduces the checking of N template template arguments for an expanded parameter pack containing N parameters from quadratic time to linear time in the length of the pack. This is important because one (and possibly the only?) general technique for splitting a template parameter pack in linear time depends on doing this. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326973 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 9 ++- lib/Sema/SemaTemplate.cpp | 26 +++++---- lib/Sema/SemaTemplateInstantiateDecl.cpp | 7 +++ test/CXX/drs/dr10xx.cpp | 5 +- test/SemaTemplate/temp-param-subst-linear.cpp | 56 +++++++++++++++++++ 5 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 test/SemaTemplate/temp-param-subst-linear.cpp diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index e73dbf85fe..65708706d7 100644 --- a/include/clang/Sema/Sema.h +++ b/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/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 2e8a7061d7..12a74c1b64 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/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/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 800956dd53..6720435608 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/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/test/CXX/drs/dr10xx.cpp b/test/CXX/drs/dr10xx.cpp index e1db9ef54a..7f5fd8c7ec 100644 --- a/test/CXX/drs/dr10xx.cpp +++ b/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/test/SemaTemplate/temp-param-subst-linear.cpp b/test/SemaTemplate/temp-param-subst-linear.cpp new file mode 100644 index 0000000000..fd93aa5685 --- /dev/null +++ b/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; -- 2.40.0