Skip to content

Commit 6d4a5fb

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 18fd92e commit 6d4a5fb

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3422,10 +3422,19 @@ namespace {
34223422
});
34233423
}
34243424

3425+
static bool hasComputedPropertyAttr(const clang::Decl *decl) {
3426+
return decl->hasAttrs() && llvm::any_of(decl->getAttrs(), [](auto *attr) {
3427+
if (auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(attr))
3428+
return swiftAttr->getAttribute() == "import_computed_property";
3429+
return false;
3430+
});
3431+
}
3432+
34253433
Decl *VisitCXXMethodDecl(const clang::CXXMethodDecl *decl) {
34263434
auto method = VisitFunctionDecl(decl);
34273435

3428-
if (Impl.SwiftContext.LangOpts.CxxInteropGettersSettersAsProperties) {
3436+
if (Impl.SwiftContext.LangOpts.CxxInteropGettersSettersAsProperties ||
3437+
hasComputedPropertyAttr(decl)) {
34293438
CXXMethodBridging bridgingInfo(decl);
34303439
if (bridgingInfo.classify() == CXXMethodBridging::Kind::getter) {
34313440
auto name = bridgingInfo.getClangName().drop_front(3);

lib/ClangImporter/swift-interop-support.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@
3131
#define CONFORMS_TO(_name) \
3232
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(conforms_to:_name))))
3333

34+
#define SWIFT_COMPUTED_PROPERTY \
35+
__attribute__((swift_attr("import_computed_property")))
36+
37+
3438
#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)