Skip to content

Commit 71d2c37

Browse files
zoecarverMForster
andauthored
[cxx-interop] Add support for percent, ampersand, and pipe operators. (#32332)
Adds support for three more basic infix operators: `%`, `&`, and `|`. Co-authored-by: Michael Forster <[email protected]>
1 parent be8ca6d commit 71d2c37

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,9 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
14231423
case clang::OverloadedOperatorKind::OO_Minus:
14241424
case clang::OverloadedOperatorKind::OO_Star:
14251425
case clang::OverloadedOperatorKind::OO_Slash:
1426+
case clang::OverloadedOperatorKind::OO_Percent:
1427+
case clang::OverloadedOperatorKind::OO_Amp:
1428+
case clang::OverloadedOperatorKind::OO_Pipe:
14261429
case clang::OverloadedOperatorKind::OO_LessLess:
14271430
case clang::OverloadedOperatorKind::OO_GreaterGreater:
14281431
case clang::OverloadedOperatorKind::OO_AmpAmp:

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ inline IntBox operator/(IntBox lhs, IntBox rhs) {
2121
return IntBox{.value = lhs.value / rhs.value};
2222
}
2323

24+
inline IntBox operator%(IntBox lhs, IntBox rhs) {
25+
return IntBox{.value = lhs.value % rhs.value};
26+
}
27+
28+
inline IntBox operator&(IntBox lhs, IntBox rhs) {
29+
return IntBox{.value = lhs.value & rhs.value};
30+
}
31+
32+
inline IntBox operator|(IntBox lhs, IntBox rhs) {
33+
return IntBox{.value = lhs.value | rhs.value};
34+
}
35+
2436
inline IntBox operator<<(IntBox lhs, IntBox rhs) {
2537
return IntBox{.value = lhs.value << rhs.value};
2638
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// CHECK-NEXT: func - (lhs: IntBox, rhs: IntBox) -> IntBox
55
// CHECK-NEXT: func * (lhs: IntBox, rhs: IntBox) -> IntBox
66
// CHECK-NEXT: func / (lhs: IntBox, rhs: IntBox) -> IntBox
7+
// CHECK-NEXT: func % (lhs: IntBox, rhs: IntBox) -> IntBox
8+
// CHECK-NEXT: func & (lhs: IntBox, rhs: IntBox) -> IntBox
9+
// CHECK-NEXT: func | (lhs: IntBox, rhs: IntBox) -> IntBox
710
// CHECK-NEXT: func << (lhs: IntBox, rhs: IntBox) -> IntBox
811
// CHECK-NEXT: func >> (lhs: IntBox, rhs: IntBox) -> IntBox
912

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ let resultPlus = lhs + rhs
99
let resultMinus = lhs - rhs
1010
let resultStar = lhs * rhs
1111
let resultSlash = lhs / rhs
12+
let resultPercent = lhs % rhs
13+
let resultAmp = lhs & rhs
14+
let resultPipe = lhs | rhs
1215
let resultLessLess = lhs << rhs
1316
let resultGreaterGreater = lhs >> rhs
1417

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ OperatorsTestSuite.test("slash (/)") {
4343
expectEqual(1, result.value)
4444
}
4545

46+
OperatorsTestSuite.test("percent") {
47+
let lhs = IntBox(value: 11)
48+
let rhs = IntBox(value: 2)
49+
50+
let result = lhs % rhs
51+
52+
expectEqual(1, result.value)
53+
}
54+
55+
OperatorsTestSuite.test("amp") {
56+
let lhs = IntBox(value: 6)
57+
let rhs = IntBox(value: 5)
58+
59+
let result = lhs & rhs
60+
61+
expectEqual(4, result.value)
62+
}
63+
64+
OperatorsTestSuite.test("pipe") {
65+
let lhs = IntBox(value: 6)
66+
let rhs = IntBox(value: 5)
67+
68+
let result = lhs | rhs
69+
70+
expectEqual(7, result.value)
71+
}
72+
4673
OperatorsTestSuite.test("less less (<<)") {
4774
let lhs = IntBox(value: 2)
4875
let rhs = IntBox(value: 4)

0 commit comments

Comments
 (0)