Skip to content

Commit

Permalink
[cxx-interop] add a SWIFT_MUTATING customization macro
Browse files Browse the repository at this point in the history
Fixes swiftlang#66322

(cherry picked from commit b25dec5)
  • Loading branch information
hyp committed Jun 6, 2023
1 parent 174b204 commit 0b52ca7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
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: }

0 comments on commit 0b52ca7

Please sign in to comment.