Skip to content

[cxx-interop] Reenable exporting Foreign Reference Types to C++ #74654

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 1 commit into from
Jun 26, 2024
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
19 changes: 0 additions & 19 deletions lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,25 +289,6 @@ class CFunctionSignatureTypePrinter
os << " __strong";
printInoutTypeModifier();
}
if (isa<clang::CXXRecordDecl>(cd->getClangDecl())) {
if (std::find_if(
cd->getClangDecl()->getAttrs().begin(),
cd->getClangDecl()->getAttrs().end(), [](clang::Attr *attr) {
if (auto *sa = dyn_cast<clang::SwiftAttrAttr>(attr)) {
llvm::StringRef value = sa->getAttribute();
if ((value.starts_with("retain:") ||
value.starts_with("release:")) &&
!value.ends_with(":immortal"))
return true;
}
return false;
}) != cd->getClangDecl()->getAttrs().end()) {
// This is a shared FRT. Do not bridge it back to
// C++ as its ownership is not managed automatically
// in C++ yet.
return ClangRepresentation::unsupported;
}
}
// FIXME: Mark that this is only ObjC representable.
return ClangRepresentation::representable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ import CxxTest
public func consumeSharedFRT(_ x: consuming SharedFRT) {}
public func takeSharedFRT(_ x: SharedFRT) {}

// CHECK: Unavailable in C++: Swift global function 'consumeSharedFRT(_:)'.
// CHECK: SWIFT_EXTERN void $s8UseCxxTy16consumeSharedFRTyySo0eF0VnF(SharedFRT *_Nonnull x) SWIFT_NOEXCEPT SWIFT_CALL; // consumeSharedFRT(_:)

// CHECK: Unavailable in C++: Swift global function 'takeSharedFRT(_:)'.
// CHECK: SWIFT_EXTERN void $s8UseCxxTy13takeSharedFRTyySo0eF0VF(SharedFRT *_Nonnull x) SWIFT_NOEXCEPT SWIFT_CALL; // takeSharedFRT(_:)
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ class ImmortalFRT {
__attribute__((swift_attr("retain:immortal")))
__attribute__((swift_attr("release:immortal")));

class SharedFRT {
public:
SharedFRT() {}
SharedFRT(int x) : x(x) {}
int x;
} __attribute__((swift_attr("import_reference")))
__attribute__((swift_attr("retain:retainShared")))
__attribute__((swift_attr("release:releaseShared")));

inline void retainShared(SharedFRT *r) { puts("retainShared"); }
inline void releaseShared(SharedFRT *r) { puts("releaseShared"); }

//--- module.modulemap
module CxxTest {
header "header.h"
Expand All @@ -74,7 +86,21 @@ public struct TakesNonTrivial {
}

public func consumeImmortalFRT(_ x: consuming ImmortalFRT) {
print("frt x \(x.x)")
print("immortal frt x \(x.x)")
}

public
func consumeSharedFRT(_ x : consuming SharedFRT) {
print("consume shared frt x \(x.x)")
}

public
func takeSharedFRT(_ x : SharedFRT) { print("take shared frt x \(x.x)") }

public
func returnSharedFRT(_ x : SharedFRT) -> SharedFRT {
print("return shared frt x \(x.x)")
return x
}

//--- use-swift-cxx-types.cpp
Expand Down Expand Up @@ -119,7 +145,22 @@ int main() {
frt.x = 2;
UseCxx::consumeImmortalFRT(&frt);
}
// CHECK-NEXT: frt x 2
// CHECK-NEXT: immortal frt x 2
{
SharedFRT sfrt;
sfrt.x = 2;
UseCxx::takeSharedFRT(&sfrt);
// CHECK-NEXT: retainShared
// CHECK-NEXT: releaseShared
// CHECK-NEXT: take shared frt x 2
UseCxx::consumeSharedFRT(&sfrt);
// CHECK-NEXT: retainShared
// CHECK-NEXT: releaseShared
// CHECK-NEXT: consume shared frt x 2
SharedFRT *sfrtptr = UseCxx::returnSharedFRT(&sfrt);
// CHECK-NEXT: retainShared
// CHECK-NEXT: return shared frt x 2
}
puts("EndOfTest");
// CHECK-NEXT: EndOfTest
return 0;
Expand Down