Skip to content

Commit 16de2c2

Browse files
committed
[cxx-interop] Add support for percent, ampersand, and pipe operators.
Adds support for three more basic infix operators: `%`, `&`, and `|`.
1 parent 8f0569d commit 16de2c2

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
if (auto FD = dyn_cast<clang::FunctionDecl>(D)) {
14271430
baseName = clang::getOperatorSpelling(op);
14281431
isFunction = true;

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
// Make sure that we don't crash on templated operators
2537
template<typename T> struct S {};
2638
template<typename T> S<T> operator+(S<T> lhs, S<T> rhs);

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,3 +4,6 @@
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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ 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

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,31 @@ 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
runAllTests()

0 commit comments

Comments
 (0)