[dllexport] Instantiate default ctor default args for explicit specializations (PR45811)

For dllexported default constructors with default arguments, we export
default constructor closures which pass in the default args. (See D8331
for a good explanation.)

For templates, that means those default args must be instantiated even
if the function isn't called. That is done by the
InstantiateDefaultCtorDefaultArgs() function, but it wasn't done for
explicit specializations, causing asserts (see bug).

Differential revision: https://reviews.llvm.org/D91089
This commit is contained in:
Hans Wennborg 2020-11-09 17:22:32 +01:00
parent e47805c995
commit a088766508
6 changed files with 49 additions and 23 deletions

View File

@ -9253,6 +9253,8 @@ public:
LateInstantiatedAttrVec *LateAttrs = nullptr,
LocalInstantiationScope *OuterMostScope = nullptr);
void InstantiateDefaultCtorDefaultArgs(CXXConstructorDecl *Ctor);
bool usesPartialOrExplicitSpecialization(
SourceLocation Loc, ClassTemplateSpecializationDecl *ClassTemplateSpec);

View File

@ -1244,12 +1244,14 @@ void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
// the typical calling convention and have a single 'this' pointer for an
// argument -or- they get a wrapper function which appropriately thunks to the
// real default constructor. This thunk is the default constructor closure.
if (D->hasAttr<DLLExportAttr>() && D->isDefaultConstructor())
if (D->hasAttr<DLLExportAttr>() && D->isDefaultConstructor() &&
D->isDefined()) {
if (!hasDefaultCXXMethodCC(getContext(), D) || D->getNumParams() != 0) {
llvm::Function *Fn = getAddrOfCXXCtorClosure(D, Ctor_DefaultClosure);
Fn->setLinkage(llvm::GlobalValue::WeakODRLinkage);
CGM.setGVProperties(Fn, D);
}
}
}
void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF,

View File

@ -13994,6 +13994,16 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
FD->setInvalidDecl();
}
if (auto *Ctor = dyn_cast<CXXConstructorDecl>(FD)) {
if (Ctor->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
Ctor->isDefaultConstructor() &&
Context.getTargetInfo().getCXXABI().isMicrosoft()) {
// If this is an MS ABI dllexport default constructor, instantiate any
// default arguments.
InstantiateDefaultCtorDefaultArgs(Ctor);
}
}
// See if this is a redefinition. If 'will have body' (or similar) is already
// set, then these checks were already performed when it was set.
if (!FD->willHaveBody() && !FD->isLateTemplateParsed() &&

View File

@ -715,6 +715,26 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
}
}
/// In the MS ABI, we need to instantiate default arguments of dllexported
/// default constructors along with the constructor definition. This allows IR
/// gen to emit a constructor closure which calls the default constructor with
/// its default arguments.
void Sema::InstantiateDefaultCtorDefaultArgs(CXXConstructorDecl *Ctor) {
assert(Context.getTargetInfo().getCXXABI().isMicrosoft() &&
Ctor->isDefaultConstructor());
unsigned NumParams = Ctor->getNumParams();
if (NumParams == 0)
return;
DLLExportAttr *Attr = Ctor->getAttr<DLLExportAttr>();
if (!Attr)
return;
for (unsigned I = 0; I != NumParams; ++I) {
(void)CheckCXXDefaultArgExpr(Attr->getLocation(), Ctor,
Ctor->getParamDecl(I));
DiscardCleanupsInEvaluationContext();
}
}
/// Get the previous declaration of a declaration for the purposes of template
/// instantiation. If this finds a previous declaration, then the previous
/// declaration of the instantiation of D should be an instantiation of the
@ -4608,27 +4628,6 @@ Sema::InstantiateFunctionDeclaration(FunctionTemplateDecl *FTD,
return cast_or_null<FunctionDecl>(SubstDecl(FD, FD->getParent(), MArgs));
}
/// In the MS ABI, we need to instantiate default arguments of dllexported
/// default constructors along with the constructor definition. This allows IR
/// gen to emit a constructor closure which calls the default constructor with
/// its default arguments.
static void InstantiateDefaultCtorDefaultArgs(Sema &S,
CXXConstructorDecl *Ctor) {
assert(S.Context.getTargetInfo().getCXXABI().isMicrosoft() &&
Ctor->isDefaultConstructor());
unsigned NumParams = Ctor->getNumParams();
if (NumParams == 0)
return;
DLLExportAttr *Attr = Ctor->getAttr<DLLExportAttr>();
if (!Attr)
return;
for (unsigned I = 0; I != NumParams; ++I) {
(void)S.CheckCXXDefaultArgExpr(Attr->getLocation(), Ctor,
Ctor->getParamDecl(I));
S.DiscardCleanupsInEvaluationContext();
}
}
/// Instantiate the definition of the given function from its
/// template.
///
@ -4847,7 +4846,7 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
// default arguments.
if (Context.getTargetInfo().getCXXABI().isMicrosoft() &&
Ctor->isDefaultConstructor()) {
InstantiateDefaultCtorDefaultArgs(*this, Ctor);
InstantiateDefaultCtorDefaultArgs(Ctor);
}
}

View File

@ -50,6 +50,14 @@ template struct __declspec(dllexport) TemplateWithClosure<int>;
// CHECK-LABEL: define weak_odr dso_local dllexport x86_thiscallcc void @"??_F?$TemplateWithClosure@H@@QAEXXZ"({{.*}}) {{#[0-9]+}} comdat
// CHECK: call {{.*}} @"??0?$TemplateWithClosure@H@@QAE@H@Z"({{.*}}, i32 4)
template <typename T> struct __declspec(dllexport) ExportedTemplateWithClosure {
ExportedTemplateWithClosure(int x = sizeof(T)) {}
};
template <> ExportedTemplateWithClosure<int>::ExportedTemplateWithClosure(int); // Don't try to emit the closure for a declaration.
template <> ExportedTemplateWithClosure<int>::ExportedTemplateWithClosure(int) {};
// CHECK-LABEL: define weak_odr dso_local dllexport x86_thiscallcc void @"??_F?$ExportedTemplateWithClosure@H@@QAEXXZ"({{.*}}) {{#[0-9]+}} comdat
// CHECK: call {{.*}} @"??0?$ExportedTemplateWithClosure@H@@QAE@H@Z"({{.*}}, i32 4)
struct __declspec(dllexport) NestedOuter {
DELETE_IMPLICIT_MEMBERS(NestedOuter);
NestedOuter(void *p = 0) {}

View File

@ -792,6 +792,11 @@ template <typename T> struct HasDefaults2 {
};
template struct HasDefaults2<void>; // expected-note {{in instantiation of member function 'HasDefaults2<void>::HasDefaults2' requested here}}
template <typename T> struct __declspec(dllexport) HasDefaults3 { // expected-note{{in instantiation of default function argument expression for 'HasDefaults3<void>' required here}}
HasDefaults3(int x = sizeof(T)) {} // expected-error {{invalid application of 'sizeof'}}
};
template <> HasDefaults3<void>::HasDefaults3(int) {};
#endif
//===----------------------------------------------------------------------===//