Skip to content

🍒[cxx-interop][SwiftToCxx] Emit foreign reference types with * in the generated Clang header #68241

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
Sep 1, 2023
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
5 changes: 3 additions & 2 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2504,15 +2504,16 @@ class DeclAndTypePrinter::Implementation
void visitClassType(ClassType *CT,
llvm::Optional<OptionalTypeKind> optionalKind) {
const ClassDecl *CD = CT->getClassOrBoundGenericClass();
assert(CD->isObjC());
assert(CD->isObjC() || CD->isForeignReferenceType());
auto clangDecl = dyn_cast_or_null<clang::NamedDecl>(CD->getClangDecl());
if (clangDecl) {
// Hack for <os/object.h> types, which use classes in Swift but
// protocols in Objective-C, and a typedef to hide the difference.
StringRef osObjectName = maybeGetOSObjectBaseName(clangDecl);
if (!osObjectName.empty()) {
os << osObjectName << "_t";
} else if (isa<clang::ObjCInterfaceDecl>(clangDecl)) {
} else if (isa<clang::ObjCInterfaceDecl>(clangDecl) ||
CD->isForeignReferenceType()) {
os << clangDecl->getName() << " *";
} else {
maybePrintTagKeyword(CD);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class __attribute__((swift_attr("import_reference"),
swift_attr("retain:immortal"),
swift_attr("release:immortal"))) X {};
5 changes: 5 additions & 0 deletions test/Interop/CxxToSwiftToObjcxx/Inputs/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module MyCxxModule {
header "bridge-cxx-types-to-objcxx.h"
export *
requires cplusplus
}
15 changes: 15 additions & 0 deletions test/Interop/CxxToSwiftToObjcxx/bridge-cxx-types-to-objcxx.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -typecheck %s -module-name UseCxxTy -emit-clang-header-path %t/UseCxxTy.h -I %t -I %S/Inputs -enable-experimental-cxx-interop -clang-header-expose-decls=all-public -target arm64-apple-macosx13.3
// RUN: %FileCheck %s < %t/UseCxxTy.h

// REQUIRES: OS=macosx

import Foundation
import MyCxxModule

@objc class C: NSObject {
@objc var x: X? = nil
}

// CHECK: @property (nonatomic) X * _Nullable x;
1 change: 1 addition & 0 deletions test/Interop/CxxToSwiftToObjcxx/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.suffixes = ['.swift', '.cpp', '.mm']