Skip to content

Commit 1adcf99

Browse files
authored
Merge pull request #66374 from hyp/eng/swift_mutating
[cxx-interop] add a SWIFT_MUTATING customization macro
2 parents 1425e32 + b25dec5 commit 1adcf99

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

lib/ClangImporter/bridging

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@
133133
#define SWIFT_COMPUTED_PROPERTY \
134134
__attribute__((swift_attr("import_computed_property")))
135135

136+
/// Specifies that a specific **constant** C++ member function should be imported as
137+
/// `mutating` Swift method. This annotation should be added to constant C++ member functions
138+
/// that mutate `mutable` fields in a C++ object, to let Swift know that this function is still mutating
139+
/// and thus that it should become a `mutating` method in Swift.
140+
#define SWIFT_MUTATING \
141+
__attribute__((swift_attr("mutating")))
142+
136143
#else // #if _CXX_INTEROP_HAS_ATTRIBUTE(swift_attr)
137144

138145
// Empty defines for compilers that don't support `attribute(swift_attr)`.
@@ -144,6 +151,7 @@
144151
#define SWIFT_NAME(_name)
145152
#define SWIFT_CONFORMS_TO_PROTOCOL(_moduleName_protocolName)
146153
#define SWIFT_COMPUTED_PROPERTY
154+
#define SWIFT_MUTATING
147155

148156
#endif // #if _CXX_INTEROP_HAS_ATTRIBUTE(swift_attr)
149157

test/Interop/Cxx/class/Inputs/mutable-members.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_MUTABLE_MEMBERS_H
22
#define TEST_INTEROP_CXX_CLASS_INPUTS_MUTABLE_MEMBERS_H
33

4+
#ifdef USE_MUTATING
5+
// Note: in actuality, this will be included
6+
// as <swift/bridging>, but in this test we include
7+
// it directly.
8+
#include "bridging"
9+
#else
10+
#define SWIFT_MUTATING
11+
#endif
12+
413
struct HasPublicMutableMember {
514
mutable int a = 0;
615

7-
int foo() const {
16+
int foo() const SWIFT_MUTATING {
817
a++;
918
return a;
1019
}
@@ -15,7 +24,7 @@ struct HasPrivateMutableMember {
1524
mutable int a = 0;
1625

1726
public:
18-
void bar() const { a++; }
27+
void bar() const SWIFT_MUTATING { a++; }
1928
};
2029

2130
#endif // TEST_INTEROP_CXX_CLASS_INPUTS_MUTABLE_MEMBERS_H
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=MutableMembers -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop -Xcc -DUSE_MUTATING -I %swift_src_root/lib/ClangImporter | %FileCheck %s
2+
3+
// CHECK: struct HasPublicMutableMember {
4+
// CHECK: mutating func foo() -> Int32
5+
// CHECK: var a: Int32
6+
// CHECK: }
7+
8+
// CHECK: struct HasPrivateMutableMember {
9+
// CHECK: mutating func bar()
10+
// CHECK: }

0 commit comments

Comments
 (0)