File tree Expand file tree Collapse file tree 3 files changed +49
-1
lines changed
test/Interop/Cxx/ergonomics Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -3441,10 +3441,19 @@ namespace {
3441
3441
});
3442
3442
}
3443
3443
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
+
3444
3452
Decl *VisitCXXMethodDecl (const clang::CXXMethodDecl *decl) {
3445
3453
auto method = VisitFunctionDecl (decl);
3446
3454
3447
- if (Impl.SwiftContext .LangOpts .CxxInteropGettersSettersAsProperties ) {
3455
+ if (Impl.SwiftContext .LangOpts .CxxInteropGettersSettersAsProperties ||
3456
+ hasComputedPropertyAttr (decl)) {
3448
3457
CXXMethodBridging bridgingInfo (decl);
3449
3458
if (bridgingInfo.classify () == CXXMethodBridging::Kind::getter) {
3450
3459
auto name = bridgingInfo.getClangName ().drop_front (3 );
Original file line number Diff line number Diff line change 111
111
#define SWIFT_CONFORMS_TO_PROTOCOL (_moduleName_protocolName ) \
112
112
__attribute__ ((swift_attr(_CXX_INTEROP_STRINGIFY(conforms_to:_moduleName_protocolName))))
113
113
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
+
114
127
#endif // SWIFT_CLANGIMPORTER_SWIFT_INTEROP_SUPPORT_H
Original file line number Diff line number Diff line change
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: }
You can’t perform that action at this time.
0 commit comments