Skip to content

Commit 78a410c

Browse files
authored
[cxx-interop] Add support for operator! overloading (#41434)
This patch enables operator overloading for operator! when cxx interop is enabled.
1 parent b08993f commit 78a410c

File tree

6 files changed

+32
-0
lines changed

6 files changed

+32
-0
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
17971797
case clang::OverloadedOperatorKind::OO_Caret:
17981798
case clang::OverloadedOperatorKind::OO_Amp:
17991799
case clang::OverloadedOperatorKind::OO_Pipe:
1800+
case clang::OverloadedOperatorKind::OO_Exclaim:
18001801
case clang::OverloadedOperatorKind::OO_Less:
18011802
case clang::OverloadedOperatorKind::OO_Greater:
18021803
case clang::OverloadedOperatorKind::OO_LessLess:

test/Interop/Cxx/operators/Inputs/member-inline.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ struct LoadableIntWrapper {
2121
}
2222
};
2323

24+
struct LoadableBoolWrapper {
25+
bool value;
26+
LoadableBoolWrapper operator!() {
27+
return LoadableBoolWrapper{.value = !value};
28+
}
29+
};
30+
2431
struct AddressOnlyIntWrapper {
2532
int value;
2633

test/Interop/Cxx/operators/member-inline-module-interface.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
// CHECK: mutating func callAsFunction(_ x: Int32, _ y: Int32) -> Int32
88
// CHECK: }
99

10+
// CHECK: struct LoadableBoolWrapper {
11+
// CHECK: static func ! (lhs: inout LoadableBoolWrapper) -> LoadableBoolWrapper
12+
// CHECK: }
13+
1014
// CHECK: struct AddressOnlyIntWrapper {
1115
// CHECK: mutating func callAsFunction() -> Int32
1216
// CHECK: mutating func callAsFunction(_ x: Int32) -> Int32

test/Interop/Cxx/operators/member-inline-silgen.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ public func sub(_ lhs: inout LoadableIntWrapper, _ rhs: LoadableIntWrapper) -> L
1313

1414
// CHECK: sil [clang LoadableIntWrapper."-"] [[NAME]] : $@convention(c) (@inout LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
1515

16+
public func exclaim(_ wrapper: inout LoadableBoolWrapper) -> LoadableBoolWrapper { !wrapper }
17+
18+
// CHECK: bb0([[SELF:%.*]] : $*LoadableBoolWrapper):
19+
// CHECK: [[SELFACCESS:%.*]] = begin_access [modify] [static] [[SELF]] : $*LoadableBoolWrapper
20+
// CHECK: [[OP:%.*]] = function_ref [[NAME:@(_ZN19LoadableBoolWrapperntEv|\?\?7LoadableBoolWrapper@@QEAA\?AU0@XZ)]] : $@convention(c) (@inout LoadableBoolWrapper) -> LoadableBoolWrapper
21+
// CHECK: apply [[OP]]([[SELFACCESS]]) : $@convention(c) (@inout LoadableBoolWrapper) -> LoadableBoolWrapper
22+
// CHECK: end_access [[SELFACCESS]]
23+
24+
// CHECK: sil [clang LoadableBoolWrapper."!"] [[NAME]] : $@convention(c) (@inout LoadableBoolWrapper) -> LoadableBoolWrapper
25+
1626
public func call(_ wrapper: inout LoadableIntWrapper, _ arg: Int32) -> Int32 { wrapper(arg) }
1727

1828
// CHECK: bb0([[SELF:%.*]] : $*LoadableIntWrapper, [[RHS:%.*]] : $Int32):

test/Interop/Cxx/operators/member-inline-typechecker.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ let resultCall0 = lhs()
1010
let resultCall1 = lhs(1)
1111
let resultCall2 = lhs(1, 2)
1212

13+
var boolWrapper = LoadableBoolWrapper(value: true)
14+
let notBoolResult = !boolWrapper
15+
1316
var addressOnly = AddressOnlyIntWrapper(42)
1417

1518
let addressOnlyResultCall0 = addressOnly()

test/Interop/Cxx/operators/member-inline.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ OperatorsTestSuite.test("LoadableIntWrapper.call (inline)") {
3131
expectEqual(57, resultTwoArgs)
3232
}
3333

34+
OperatorsTestSuite.test("LoadableBoolWrapper.exclaim (inline)") {
35+
var wrapper = LoadableBoolWrapper(value: true)
36+
37+
let resultExclaim = !wrapper
38+
expectEqual(false, resultExclaim.value)
39+
}
40+
3441
OperatorsTestSuite.test("AddressOnlyIntWrapper.call (inline)") {
3542
var wrapper = AddressOnlyIntWrapper(42)
3643

0 commit comments

Comments
 (0)