Skip to content

[Interop][SwiftToCxx] Add class type support for enum with associated value #62952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
37 changes: 25 additions & 12 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,24 @@ class DeclAndTypePrinter::Implementation
elementDecl->getParentEnum()->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineOS << ">::type";
outOfLineOS << "::returnNewValue([&](char * _Nonnull result) {\n";
outOfLineOS << " swift::"
<< cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter.printModuleNamespaceQualifiersIfNeeded(
objectTypeDecl->getModuleContext(),
elementDecl->getParentEnum()->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineOS << ">::type";
outOfLineOS
<< "::initializeWithTake(result, payloadFromDestruction);\n";
outOfLineOS << " });\n ";
if (isa<ClassDecl>(objectTypeDecl)) {
outOfLineOS << "::makeRetained(*reinterpret_cast<void "
"**>(payloadFromDestruction));\n ";
} else {
outOfLineOS
<< "::returnNewValue([&](char * _Nonnull result) {\n";
outOfLineOS << " swift::"
<< cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter.printModuleNamespaceQualifiersIfNeeded(
objectTypeDecl->getModuleContext(),
elementDecl->getParentEnum()->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineOS << ">::type";
outOfLineOS << "::initializeWithTake(result, "
"payloadFromDestruction);\n";
outOfLineOS << " });\n ";
}
}
},
ED, ED->getModuleContext(), outOfLineOS);
Expand Down Expand Up @@ -666,6 +672,13 @@ class DeclAndTypePrinter::Implementation
outOfLineOS
<< " memcpy(result._getOpaquePointer(), &val, "
"sizeof(val));\n";
} else if (isa<ClassDecl>(objectTypeDecl)) {
outOfLineOS
<< " auto op = swift::"
<< cxx_synthesis::getCxxImplNamespaceName()
<< "::_impl_RefCountedClass::copyOpaquePointer(val);\n";
outOfLineOS << " memcpy(result._getOpaquePointer(), "
"&op, sizeof(op));\n";
} else {
objectTypeDecl =
paramType->getNominalOrBoundGenericNominal();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend %S/enum-associated-value-class-type-cxx.swift -typecheck -module-name Enums -clang-header-expose-decls=all-public -emit-clang-header-path %t/enums.h

// RUN: %target-interop-build-clangxx -c %s -I %t -o %t/swift-enums-execution.o
// RUN: %target-interop-build-swift %S/enum-associated-value-class-type-cxx.swift -o %t/swift-enums-execution -Xlinker %t/swift-enums-execution.o -module-name Enums -Xfrontend -entry-point-function-name -Xfrontend swiftMain

// RUN: %target-codesign %t/swift-enums-execution
// RUN: %target-run %t/swift-enums-execution

// RUN: %empty-directory(%t-evo)

// RUN: %target-swift-frontend %S/enum-associated-value-class-type-cxx.swift -typecheck -module-name Enums -clang-header-expose-decls=all-public -enable-library-evolution -emit-clang-header-path %t-evo/enums.h

// RUN: %target-interop-build-clangxx -c %s -I %t-evo -o %t-evo/swift-enums-execution.o
// RUN: %target-interop-build-swift %S/enum-associated-value-class-type-cxx.swift -o %t-evo/swift-enums-execution -Xlinker %t-evo/swift-enums-execution.o -module-name Enums -enable-library-evolution -Xfrontend -entry-point-function-name -Xfrontend swiftMain

// RUN: %target-codesign %t-evo/swift-enums-execution
// RUN: %target-run %t-evo/swift-enums-execution

// REQUIRES: executable_test

#include <cassert>
#include "enums.h"

using namespace Enums;

extern "C" size_t swift_retainCount(void * _Nonnull obj);

size_t getRetainCount(const C & obj) {
void *p = swift::_impl::_impl_RefCountedClass::getOpaquePointer(obj);
return swift_retainCount(p);
}

int main() {
auto c = C::init(1234);
assert(c.getX() == 1234);
assert(getRetainCount(c) == 1);

{
auto e = E::c(c);
assert(e.isC());
assert(getRetainCount(c) == 2);

auto extracted = e.getC();
assert(getRetainCount(c) == 3);
assert(getRetainCount(extracted) == 3);
assert(extracted.getX() == 1234);

extracted.setX(5678);
assert(extracted.getX() == 5678);
assert(c.getX() == 5678);
}

assert(getRetainCount(c) == 1);
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name Enums -clang-header-expose-decls=all-public -emit-clang-header-path %t/enums.h
// RUN: %FileCheck %s < %t/enums.h

// RUN: %check-interop-cxx-header-in-clang(%t/enums.h -Wno-unused-private-field -Wno-unused-function)

public class C {
public var x: Int
public init(x: Int) { self.x = x }
}

public enum E {
case c(C)
case i(Int)
}

// CHECK: inline E E::_impl_c::operator()(const C& val) const {
// CHECK-NEXT: auto result = E::_make();
// CHECK-NEXT: auto op = swift::_impl::_impl_RefCountedClass::copyOpaquePointer(val);
// CHECK-NEXT: memcpy(result._getOpaquePointer(), &op, sizeof(op));
// CHECK-NEXT: result._destructiveInjectEnumTag(0);
// CHECK-NEXT: return result;
// CHECK-NEXT: }

// CHECK: inline C E::getC() const {
// CHECK-NEXT: if (!isC()) abort();
// CHECK-NEXT: alignas(E) unsigned char buffer[sizeof(E)];
// CHECK-NEXT: auto *thisCopy = new(buffer) E(*this);
// CHECK-NEXT: char * _Nonnull payloadFromDestruction = thisCopy->_destructiveProjectEnumData();
// CHECK-NEXT: return swift::_impl::implClassFor<C>::type::makeRetained(*reinterpret_cast<void **>(payloadFromDestruction));
// CHECK-NEXT: }