Skip to content

[cxx-interop] add a SWIFT_MUTATING customization macro #66374

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
Jun 6, 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
8 changes: 8 additions & 0 deletions lib/ClangImporter/bridging
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@
#define SWIFT_COMPUTED_PROPERTY \
__attribute__((swift_attr("import_computed_property")))

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

#else // #if _CXX_INTEROP_HAS_ATTRIBUTE(swift_attr)

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

#endif // #if _CXX_INTEROP_HAS_ATTRIBUTE(swift_attr)

Expand Down
13 changes: 11 additions & 2 deletions test/Interop/Cxx/class/Inputs/mutable-members.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_MUTABLE_MEMBERS_H
#define TEST_INTEROP_CXX_CLASS_INPUTS_MUTABLE_MEMBERS_H

#ifdef USE_MUTATING
// Note: in actuality, this will be included
// as <swift/bridging>, but in this test we include
// it directly.
#include "bridging"
#else
#define SWIFT_MUTATING
#endif

struct HasPublicMutableMember {
mutable int a = 0;

int foo() const {
int foo() const SWIFT_MUTATING {
a++;
return a;
}
Expand All @@ -15,7 +24,7 @@ struct HasPrivateMutableMember {
mutable int a = 0;

public:
void bar() const { a++; }
void bar() const SWIFT_MUTATING { a++; }
};

#endif // TEST_INTEROP_CXX_CLASS_INPUTS_MUTABLE_MEMBERS_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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

// CHECK: struct HasPublicMutableMember {
// CHECK: mutating func foo() -> Int32
// CHECK: var a: Int32
// CHECK: }

// CHECK: struct HasPrivateMutableMember {
// CHECK: mutating func bar()
// CHECK: }