Skip to content

Commit 5a20d72

Browse files
committed
[cxx-interop] Allow specific getters/setters to be imported as computed properties.
Adds `SWIFT_COMPUTED_PROPERTY`. Refs `-cxx-interop-getters-setters-as-properties`.
1 parent 5c5598e commit 5a20d72

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3441,10 +3441,19 @@ namespace {
34413441
});
34423442
}
34433443

3444+
static bool hasComputedPropertyAttr(const clang::Decl *decl) {
3445+
return decl->hasAttrs() && llvm::any_of(decl->getAttrs(), [](auto *attr) {
3446+
if (auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(attr))
3447+
return swiftAttr->getAttribute() == "import_computed_property";
3448+
return false;
3449+
});
3450+
}
3451+
34443452
Decl *VisitCXXMethodDecl(const clang::CXXMethodDecl *decl) {
34453453
auto method = VisitFunctionDecl(decl);
34463454

3447-
if (Impl.SwiftContext.LangOpts.CxxInteropGettersSettersAsProperties) {
3455+
if (Impl.SwiftContext.LangOpts.CxxInteropGettersSettersAsProperties ||
3456+
hasComputedPropertyAttr(decl)) {
34483457
CXXMethodBridging bridgingInfo(decl);
34493458
if (bridgingInfo.classify() == CXXMethodBridging::Kind::getter) {
34503459
auto name = bridgingInfo.getClangName().drop_front(3);

lib/ClangImporter/bridging

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,17 @@
111111
#define SWIFT_CONFORMS_TO_PROTOCOL(_moduleName_protocolName) \
112112
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(conforms_to:_moduleName_protocolName))))
113113

114+
/// Specifies that a specific C++ method should be imported as a computed
115+
/// property. If this macro is specified on a getter, a getter will be
116+
/// synthesized. If this macro is specified on a setter, both a getter and
117+
/// setter will be synthesized.
118+
///
119+
/// For example:
120+
/// ```
121+
/// int getX() SWIFT_COMPUTED_PROPERTY;
122+
/// ```
123+
/// Will be imported as `var x: CInt {...}`.
124+
#define SWIFT_COMPUTED_PROPERTY \
125+
__attribute__((swift_attr("import_computed_property")))
126+
114127
#endif // SWIFT_CLANGIMPORTER_SWIFT_INTEROP_SUPPORT_H
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: %target-swift-ide-test -print-module -module-to-print=Test -I %t/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
4+
5+
//--- Inputs/module.modulemap
6+
module Test {
7+
header "test.h"
8+
requires cplusplus
9+
}
10+
11+
//--- Inputs/test.h
12+
13+
#define SWIFT_COMPUTED_PROPERTY \
14+
__attribute__((swift_attr("import_computed_property")))
15+
16+
struct Record {
17+
int getX() SWIFT_COMPUTED_PROPERTY { return 42; }
18+
};
19+
20+
//--- test.swift
21+
22+
// CHECK: struct Record {
23+
// CHECK: init()
24+
// CHECK: var x: Int32 { mutating get }
25+
// CHECK: mutating func getX() -> Int32
26+
// CHECK: }

0 commit comments

Comments
 (0)