diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -2150,7 +2150,8 @@ /// Transform a constructor declaration into a deduction guide. NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, CXXConstructorDecl *CD) { - SmallVector SubstArgs; + SmallVector OuterSubstArgs; + SmallVector InnerSubstArgs; LocalInstantiationScope Scope(SemaRef); @@ -2162,43 +2163,58 @@ // template followed by the template parameters (including default // template arguments) of the constructor, if any. TemplateParameterList *TemplateParams = Template->getTemplateParameters(); - if (FTD) { - TemplateParameterList *InnerParams = FTD->getTemplateParameters(); - SmallVector AllParams; - AllParams.reserve(TemplateParams->size() + InnerParams->size()); - AllParams.insert(AllParams.begin(), - TemplateParams->begin(), TemplateParams->end()); - SubstArgs.reserve(InnerParams->size()); + TemplateParameterList *InnerParams = + FTD ? FTD->getTemplateParameters() : nullptr; + SmallVector AllParams; + AllParams.reserve(TemplateParams->size() + + (InnerParams ? InnerParams->size() : 0)); + OuterSubstArgs.reserve(TemplateParams->size()); + for (NamedDecl *Param : *TemplateParams) { + MultiLevelTemplateArgumentList Args; + Args.setKind(TemplateSubstitutionKind::Rewrite); + Args.addOuterTemplateArguments(OuterSubstArgs); + NamedDecl *NewParam = transformTemplateParameter(Param, Args); + if (!NewParam) + return nullptr; + AllParams.push_back(NewParam); + OuterSubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( + SemaRef.Context.getInjectedTemplateArg(NewParam))); + } + if (InnerParams) { + InnerSubstArgs.reserve(InnerParams->size()); // Later template parameters could refer to earlier ones, so build up // a list of substituted template arguments as we go. for (NamedDecl *Param : *InnerParams) { MultiLevelTemplateArgumentList Args; Args.setKind(TemplateSubstitutionKind::Rewrite); - Args.addOuterTemplateArguments(SubstArgs); + Args.addOuterTemplateArguments(InnerSubstArgs); + Args.addOuterTemplateArguments(OuterSubstArgs); Args.addOuterRetainedLevel(); NamedDecl *NewParam = transformTemplateParameter(Param, Args); if (!NewParam) return nullptr; AllParams.push_back(NewParam); - SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( + InnerSubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( SemaRef.Context.getInjectedTemplateArg(NewParam))); } - TemplateParams = TemplateParameterList::Create( - SemaRef.Context, InnerParams->getTemplateLoc(), - InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), - /*FIXME: RequiresClause*/ nullptr); } + TemplateParameterList *InheritParameters = + InnerParams ? InnerParams : TemplateParams; + TemplateParams = TemplateParameterList::Create( + SemaRef.Context, InheritParameters->getTemplateLoc(), + InheritParameters->getLAngleLoc(), AllParams, + InheritParameters->getRAngleLoc(), + /*FIXME: RequiresClause*/ nullptr); // If we built a new template-parameter-list, track that we need to // substitute references to the old parameters into references to the // new ones. MultiLevelTemplateArgumentList Args; Args.setKind(TemplateSubstitutionKind::Rewrite); - if (FTD) { - Args.addOuterTemplateArguments(SubstArgs); - Args.addOuterRetainedLevel(); - } + Args.addOuterTemplateArguments(InnerSubstArgs); + Args.addOuterTemplateArguments(OuterSubstArgs); + Args.addOuterRetainedLevel(); FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() .getAsAdjusted(); diff --git a/clang/test/Modules/Inputs/ctad/a.h b/clang/test/Modules/Inputs/ctad/a.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/ctad/a.h @@ -0,0 +1,15 @@ +#pragma once + +template +struct A { + template + A(T, U) {} +}; + +template +struct B { + B(T) {} +}; + +A _a(2, 2); +B _b(2); diff --git a/clang/test/Modules/Inputs/ctad/b.h b/clang/test/Modules/Inputs/ctad/b.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/ctad/b.h @@ -0,0 +1,14 @@ +#pragma once + +template struct A { + template + A(T, U) {} +}; + +template +struct B { + B(T) {} +}; + +A _x(5, 1.2); +B _y(5); diff --git a/clang/test/Modules/Inputs/ctad/module.modulemap b/clang/test/Modules/Inputs/ctad/module.modulemap new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/ctad/module.modulemap @@ -0,0 +1,2 @@ +module a { header "a.h" export * } +module b { header "b.h" export * } diff --git a/clang/test/Modules/ctad.cpp b/clang/test/Modules/ctad.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Modules/ctad.cpp @@ -0,0 +1,14 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fmodules -fno-implicit-modules -x c++ -emit-module %S/Inputs/ctad/module.modulemap -fmodule-name=a -o %t/a.pcm -std=c++17 +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fmodules -fno-implicit-modules -x c++ -emit-module %S/Inputs/ctad/module.modulemap -fmodule-name=b -o %t/b.pcm -std=c++17 +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fmodules -fno-implicit-modules -x c++ -emit-llvm -I%S/Inputs/ctad -o - %s -fmodule-file=%t/a.pcm -fmodule-file=%t/b.pcm -std=c++17 | FileCheck %s + +// CHECK: @_a = global +// CHECK: @_b = global +// CHECK: @_x = global +// CHECK: @_y = global +// CHECK: call void @__cxx_global_var_init() +// CHECK: call void @__cxx_global_var_init.1() + +#include "a.h" +#include "b.h"