Skip to content

Commit 1e52852

Browse files
zoecarverMForster
andauthored
[cxx-interop] Add support for C++ shift operators. (#32333)
* [cxx-interop] Add support for C++ shift operators. Support imported C++ `<<` and `>>` operators in Swift. * Update test names of existing operators ... to match the new ones. Co-authored-by: Michael Forster <[email protected]>
1 parent e1ceab0 commit 1e52852

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,8 @@ 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_LessLess:
1427+
case clang::OverloadedOperatorKind::OO_GreaterGreater:
14261428
case clang::OverloadedOperatorKind::OO_AmpAmp:
14271429
case clang::OverloadedOperatorKind::OO_PipePipe:
14281430
if (auto FD = dyn_cast<clang::FunctionDecl>(D)) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ 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+
2432
struct BoolBox {
2533
bool value;
2634
};

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

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

810
// CHECK: func && (lhs: BoolBox, rhs: BoolBox) -> BoolBox
911
// CHECK-NEXT: func || (lhs: BoolBox, rhs: BoolBox) -> BoolBox

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ let resultPlus = lhs + rhs
99
let resultMinus = lhs - rhs
1010
let resultStar = lhs * rhs
1111
let resultSlash = lhs / rhs
12+
let resultLessLess = lhs << rhs
13+
let resultGreaterGreater = lhs >> rhs
1214

1315
var lhsBool = BoolBox(value: true)
1416
var rhsBool = BoolBox(value: false)

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import StdlibUnittest
77

88
var OperatorsTestSuite = TestSuite("Operators")
99

10-
OperatorsTestSuite.test("plus") {
10+
OperatorsTestSuite.test("plus (+)") {
1111
let lhs = IntBox(value: 42)
1212
let rhs = IntBox(value: 23)
1313

@@ -16,7 +16,7 @@ OperatorsTestSuite.test("plus") {
1616
expectEqual(65, result.value)
1717
}
1818

19-
OperatorsTestSuite.test("minus") {
19+
OperatorsTestSuite.test("minus (-)") {
2020
let lhs = IntBox(value: 42)
2121
let rhs = IntBox(value: 23)
2222

@@ -25,7 +25,7 @@ OperatorsTestSuite.test("minus") {
2525
expectEqual(19, result.value)
2626
}
2727

28-
OperatorsTestSuite.test("star") {
28+
OperatorsTestSuite.test("star (*)") {
2929
let lhs = IntBox(value: 42)
3030
let rhs = IntBox(value: 23)
3131

@@ -34,7 +34,7 @@ OperatorsTestSuite.test("star") {
3434
expectEqual(966, result.value)
3535
}
3636

37-
OperatorsTestSuite.test("slash") {
37+
OperatorsTestSuite.test("slash (/)") {
3838
let lhs = IntBox(value: 42)
3939
let rhs = IntBox(value: 23)
4040

@@ -43,6 +43,24 @@ OperatorsTestSuite.test("slash") {
4343
expectEqual(1, result.value)
4444
}
4545

46+
OperatorsTestSuite.test("less less (<<)") {
47+
let lhs = IntBox(value: 2)
48+
let rhs = IntBox(value: 4)
49+
50+
let result = lhs << rhs
51+
52+
expectEqual(32, result.value)
53+
}
54+
55+
OperatorsTestSuite.test("greater greater (>>)") {
56+
let lhs = IntBox(value: 512)
57+
let rhs = IntBox(value: 8)
58+
59+
let result = lhs >> rhs
60+
61+
expectEqual(2, result.value)
62+
}
63+
4664
OperatorsTestSuite.test("amp amp (&&)") {
4765
let lhs = BoolBox(value: true)
4866
let rhs = BoolBox(value: false)

0 commit comments

Comments
 (0)