Skip to content

[cxx-interop] Allow specific getters/setters to be imported as comput… #65010

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
Apr 7, 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
11 changes: 10 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3441,10 +3441,19 @@ namespace {
});
}

static bool hasComputedPropertyAttr(const clang::Decl *decl) {
return decl->hasAttrs() && llvm::any_of(decl->getAttrs(), [](auto *attr) {
if (auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(attr))
return swiftAttr->getAttribute() == "import_computed_property";
return false;
});
}

Decl *VisitCXXMethodDecl(const clang::CXXMethodDecl *decl) {
auto method = VisitFunctionDecl(decl);

if (Impl.SwiftContext.LangOpts.CxxInteropGettersSettersAsProperties) {
if (Impl.SwiftContext.LangOpts.CxxInteropGettersSettersAsProperties ||
hasComputedPropertyAttr(decl)) {
CXXMethodBridging bridgingInfo(decl);
if (bridgingInfo.classify() == CXXMethodBridging::Kind::getter) {
auto name = bridgingInfo.getClangName().drop_front(3);
Expand Down
13 changes: 13 additions & 0 deletions lib/ClangImporter/bridging
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,17 @@
#define SWIFT_CONFORMS_TO_PROTOCOL(_moduleName_protocolName) \
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(conforms_to:_moduleName_protocolName))))

/// Specifies that a specific C++ method should be imported as a computed
/// property. If this macro is specified on a getter, a getter will be
/// synthesized. If this macro is specified on a setter, both a getter and
/// setter will be synthesized.
///
/// For example:
/// ```
/// int getX() SWIFT_COMPUTED_PROPERTY;
/// ```
/// Will be imported as `var x: CInt {...}`.
#define SWIFT_COMPUTED_PROPERTY \
__attribute__((swift_attr("import_computed_property")))

#endif // SWIFT_CLANGIMPORTER_SWIFT_INTEROP_SUPPORT_H
26 changes: 26 additions & 0 deletions test/Interop/Cxx/ergonomics/explicit-computed-properties.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: %target-swift-ide-test -print-module -module-to-print=Test -I %t/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s

//--- Inputs/module.modulemap
module Test {
header "test.h"
requires cplusplus
}

//--- Inputs/test.h

#define SWIFT_COMPUTED_PROPERTY \
__attribute__((swift_attr("import_computed_property")))

struct Record {
int getX() SWIFT_COMPUTED_PROPERTY { return 42; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test this also applying to both getter and setter?

};

//--- test.swift

// CHECK: struct Record {
// CHECK: init()
// CHECK: var x: Int32 { mutating get }
// CHECK: mutating func getX() -> Int32
// CHECK: }