From 5cde386e5e0357c2d32ec8adb01079e8bc0d2755 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 11 Jan 2011 03:14:20 +0000 Subject: [PATCH] When mapping from a function parameter pack to the set of function parameters it expanded to, map exactly the number of function parameters that were expanded rather than just running to the end of the instantiated parameter list. This finishes the implementation of the last sentence of C++0x [temp.deduct.call]p1. llvm-svn: 123213 --- clang/include/clang/Sema/Sema.h | 9 ++++ .../lib/Sema/SemaTemplateInstantiateDecl.cpp | 7 +-- clang/lib/Sema/SemaTemplateVariadic.cpp | 44 +++++++++++++++++++ .../temp.deduct/temp.deduct.call/p1-0x.cpp | 3 -- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 368fe9dffaa9..d4ffd37bcae4 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3383,6 +3383,15 @@ public: bool &RetainExpansion, unsigned &NumExpansions); + /// \brief Determine the number of arguments in the given pack expansion + /// type. + /// + /// This routine already assumes that the pack expansion type can be + /// expanded and that the number of arguments in the expansion is + /// consistent across all of the unexpanded parameter packs in its pattern. + unsigned getNumArgumentsInExpansion(QualType T, + const MultiLevelTemplateArgumentList &TemplateArgs); + /// \brief Determine whether the given declarator contains any unexpanded /// parameter packs. /// diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 50b24dc41d32..a18bfbafc079 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -1894,9 +1894,10 @@ TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, // Parameter pack: make the instantiation an argument pack. SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack( OldParam); - // FIXME: Variadic templates. Figure out how many arguments are in the - // expansion of OldParam, so we don't gobble all of the arguments here. - while (NewIdx < NumNewParams) { + unsigned NumArgumentsInExpansion + = SemaRef.getNumArgumentsInExpansion(OldParam->getType(), + TemplateArgs); + while (NumArgumentsInExpansion--) { ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++); Params.push_back(NewParam); SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam, diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp index 09e610c675ca..63a8394d67a4 100644 --- a/clang/lib/Sema/SemaTemplateVariadic.cpp +++ b/clang/lib/Sema/SemaTemplateVariadic.cpp @@ -536,6 +536,50 @@ bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc, return false; } +unsigned Sema::getNumArgumentsInExpansion(QualType T, + const MultiLevelTemplateArgumentList &TemplateArgs) { + QualType Pattern = cast(T)->getPattern(); + llvm::SmallVector Unexpanded; + CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern); + + for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { + // Compute the depth and index for this parameter pack. + unsigned Depth; + unsigned Index; + + if (const TemplateTypeParmType *TTP + = Unexpanded[I].first.dyn_cast()) { + Depth = TTP->getDepth(); + Index = TTP->getIndex(); + } else { + NamedDecl *ND = Unexpanded[I].first.get(); + if (isa(ND)) { + // Function parameter pack. + typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; + + llvm::PointerUnion *Instantiation + = CurrentInstantiationScope->findInstantiationOf( + Unexpanded[I].first.get()); + if (Instantiation && Instantiation->is()) + return Instantiation->get()->size(); + + continue; + } + + llvm::tie(Depth, Index) = getDepthAndIndex(ND); + } + if (Depth >= TemplateArgs.getNumLevels() || + !TemplateArgs.hasTemplateArgument(Depth, Index)) + continue; + + // Determine the size of the argument pack. + return TemplateArgs(Depth, Index).pack_size(); + } + + llvm_unreachable("No unexpanded parameter packs in type expansion."); + return 0; +} + bool Sema::containsUnexpandedParameterPacks(Declarator &D) { const DeclSpec &DS = D.getDeclSpec(); switch (DS.getTypeSpecType()) { diff --git a/clang/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.call/p1-0x.cpp b/clang/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.call/p1-0x.cpp index 4b480ddd3f1e..8933b63ee6df 100644 --- a/clang/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.call/p1-0x.cpp +++ b/clang/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.call/p1-0x.cpp @@ -78,8 +78,6 @@ void test_pair_deduction(int *ip, float *fp, double *dp) { // For a function parameter pack that does not occur at the end of the // parameter-declaration-list, the type of the parameter pack is a // non-deduced context. -// FIXME: We're not in a position to handle this yet. -#if 0 template struct tuple { }; template @@ -88,4 +86,3 @@ void pack_not_at_end(tuple, Types... values, int); void test_pack_not_at_end(tuple t2) { pack_not_at_end(t2, 0, 0, 0); } -#endif