Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2635,30 +2635,38 @@ namespace {
}
clang::CXXConstructorDecl *copyCtor = nullptr;
clang::CXXConstructorDecl *moveCtor = nullptr;
clang::CXXConstructorDecl *defaultCtor = nullptr;
if (decl->needsImplicitCopyConstructor()) {
copyCtor = clangSema.DeclareImplicitCopyConstructor(
const_cast<clang::CXXRecordDecl *>(decl));
} else if (decl->needsImplicitMoveConstructor()) {
}
if (decl->needsImplicitMoveConstructor()) {
moveCtor = clangSema.DeclareImplicitMoveConstructor(
const_cast<clang::CXXRecordDecl *>(decl));
} else {
// We may have a defaulted copy constructor that needs to be defined.
// Try to find it.
for (auto methods : decl->methods()) {
if (auto declCtor = dyn_cast<clang::CXXConstructorDecl>(methods)) {
if (declCtor->isDefaulted() &&
declCtor->getAccess() == clang::AS_public &&
!declCtor->isDeleted() &&
// Note: we use "doesThisDeclarationHaveABody" here because
// that's what "DefineImplicitCopyConstructor" checks.
!declCtor->doesThisDeclarationHaveABody()) {
if (declCtor->isCopyConstructor()) {
}
if (decl->needsImplicitDefaultConstructor()) {
defaultCtor = clangSema.DeclareImplicitDefaultConstructor(
const_cast<clang::CXXRecordDecl *>(decl));
}
// We may have a defaulted copy/move/default constructor that needs to
// be defined. Try to find it.
for (auto methods : decl->methods()) {
if (auto declCtor = dyn_cast<clang::CXXConstructorDecl>(methods)) {
if (declCtor->isDefaulted() &&
declCtor->getAccess() == clang::AS_public &&
!declCtor->isDeleted() &&
// Note: we use "doesThisDeclarationHaveABody" here because
// that's what "DefineImplicitCopyConstructor" checks.
!declCtor->doesThisDeclarationHaveABody()) {
if (declCtor->isCopyConstructor()) {
if (!copyCtor)
copyCtor = declCtor;
break;
} else if (declCtor->isMoveConstructor()) {
} else if (declCtor->isMoveConstructor()) {
if (!moveCtor)
moveCtor = declCtor;
break;
}
} else if (declCtor->isDefaultConstructor()) {
if (!defaultCtor)
defaultCtor = declCtor;
}
}
}
Expand All @@ -2671,6 +2679,10 @@ namespace {
clangSema.DefineImplicitMoveConstructor(clang::SourceLocation(),
moveCtor);
}
if (defaultCtor) {
clangSema.DefineImplicitDefaultConstructor(clang::SourceLocation(),
defaultCtor);
}

if (decl->needsImplicitDestructor()) {
auto dtor = clangSema.DeclareImplicitDestructor(
Expand Down
5 changes: 5 additions & 0 deletions test/Interop/Cxx/class/Inputs/constructors.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ struct ImplicitDefaultConstructor {
int x = 42;
};

struct DefaultedDefaultConstructor {
int x = 42;
DefaultedDefaultConstructor() = default;
};

struct MemberOfClassType {
ImplicitDefaultConstructor member;
};
Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/class/constructors-executable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ CxxConstructorTestSuite.test("ImplicitDefaultConstructor") {
expectEqual(42, instance.x)
}

CxxConstructorTestSuite.test("DefaultedDefaultConstructor") {
let instance = DefaultedDefaultConstructor()

expectEqual(42, instance.x)
}

CxxConstructorTestSuite.test("MemberOfClassType") {
let instance = MemberOfClassType()

Expand Down
5 changes: 5 additions & 0 deletions test/Interop/Cxx/class/constructors-module-interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
// CHECK-NEXT: init(x: Int32)
// CHECK-NEXT: var x: Int32
// CHECK-NEXT: }
// CHECK-NEXT: struct DefaultedDefaultConstructor {
// CHECK-NEXT: init()
// CHECK-NEXT: init(x: Int32)
// CHECK-NEXT: var x: Int32
// CHECK-NEXT: }
// CHECK-NEXT: struct MemberOfClassType {
// CHECK-NEXT: init()
// CHECK-NEXT: init(member: ImplicitDefaultConstructor)
Expand Down
2 changes: 0 additions & 2 deletions test/Interop/Cxx/stdlib/use-std-map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import Cxx

var StdMapTestSuite = TestSuite("StdMap")

#if !os(Linux) // https://github.com/apple/swift/issues/61412
StdMapTestSuite.test("init") {
let m = Map()
expectEqual(m.size(), 0)
expectTrue(m.empty())
}
#endif

StdMapTestSuite.test("Map.subscript") {
// This relies on the `std::map` conformance to `CxxDictionary` protocol.
Expand Down