Skip to content

Commit d411f92

Browse files
committed
[cxx-interop] Add support for C++ comparison operators.
Adds support for C++ operators: `<`, `>`, `==`, `!=`, `<=`, and `>=`.
1 parent c17966e commit d411f92

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,12 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
14301430
case clang::OverloadedOperatorKind::OO_GreaterGreater:
14311431
case clang::OverloadedOperatorKind::OO_AmpAmp:
14321432
case clang::OverloadedOperatorKind::OO_PipePipe:
1433+
case clang::OverloadedOperatorKind::OO_Less:
1434+
case clang::OverloadedOperatorKind::OO_Greater:
1435+
case clang::OverloadedOperatorKind::OO_EqualEqual:
1436+
case clang::OverloadedOperatorKind::OO_ExclaimEqual:
1437+
case clang::OverloadedOperatorKind::OO_LessEqual:
1438+
case clang::OverloadedOperatorKind::OO_GreaterEqual:
14331439
if (auto FD = dyn_cast<clang::FunctionDecl>(D)) {
14341440
baseName = clang::getOperatorSpelling(op);
14351441
isFunction = true;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ inline IntBox operator>>(IntBox lhs, IntBox rhs) {
4141
return IntBox{.value = lhs.value >> rhs.value};
4242
}
4343

44+
inline bool operator<(IntBox lhs, IntBox rhs) {
45+
return lhs.value < rhs.value;
46+
}
47+
48+
inline bool operator>(IntBox lhs, IntBox rhs) {
49+
return lhs.value > rhs.value;
50+
}
51+
52+
inline bool operator==(IntBox lhs, IntBox rhs) {
53+
return lhs.value == rhs.value;
54+
}
55+
56+
inline bool operator!=(IntBox lhs, IntBox rhs) {
57+
return lhs.value != rhs.value;
58+
}
59+
60+
inline bool operator<=(IntBox lhs, IntBox rhs) {
61+
return lhs.value == rhs.value;
62+
}
63+
64+
inline bool operator>=(IntBox lhs, IntBox rhs) {
65+
return lhs.value != rhs.value;
66+
}
67+
4468
struct BoolBox {
4569
bool value;
4670
};

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
// CHECK-NEXT: func | (lhs: IntBox, rhs: IntBox) -> IntBox
1010
// CHECK-NEXT: func << (lhs: IntBox, rhs: IntBox) -> IntBox
1111
// CHECK-NEXT: func >> (lhs: IntBox, rhs: IntBox) -> IntBox
12+
// CHECK-NEXT: func < (lhs: IntBox, rhs: IntBox) -> Bool
13+
// CHECK-NEXT: func > (lhs: IntBox, rhs: IntBox) -> Bool
14+
// CHECK-NEXT: func == (lhs: IntBox, rhs: IntBox) -> Bool
15+
// CHECK-NEXT: func != (lhs: IntBox, rhs: IntBox) -> Bool
16+
// CHECK-NEXT: func <= (lhs: IntBox, rhs: IntBox) -> Bool
17+
// CHECK-NEXT: func >= (lhs: IntBox, rhs: IntBox) -> Bool
1218

1319
// CHECK: func && (lhs: BoolBox, rhs: BoolBox) -> BoolBox
1420
// CHECK-NEXT: func || (lhs: BoolBox, rhs: BoolBox) -> BoolBox

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ let resultAmp = lhs & rhs
1414
let resultPipe = lhs | rhs
1515
let resultLessLess = lhs << rhs
1616
let resultGreaterGreater = lhs >> rhs
17+
let resultLess = lhs < rhs
18+
let resultGreater = lhs > rhs
19+
let resultEqualEqual = lhs == rhs
20+
let resultExclaimEqual = lhs != rhs
21+
let resultLessEqual = lhs <= rhs
22+
let resultGreaterEqual = lhs >= rhs
1723

1824
var lhsBool = BoolBox(value: true)
1925
var rhsBool = BoolBox(value: false)

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,58 @@ OperatorsTestSuite.test("pipe pipe (||)") {
106106
expectEqual(true, result.value)
107107
}
108108

109+
OperatorsTestSuite.test("less (<)") {
110+
let lhs = IntBox(value: 5)
111+
let rhs = IntBox(value: 6)
112+
113+
let result = lhs < rhs
114+
115+
expectEqual(true, result)
116+
}
117+
118+
OperatorsTestSuite.test("greater (>)") {
119+
let lhs = IntBox(value: 5)
120+
let rhs = IntBox(value: 6)
121+
122+
let result = lhs > rhs
123+
124+
expectEqual(false, result)
125+
}
126+
127+
OperatorsTestSuite.test("equal equal (==)") {
128+
let lhs = IntBox(value: 5)
129+
let rhs = IntBox(value: 5)
130+
131+
let result = lhs == rhs
132+
133+
expectEqual(true, result)
134+
}
135+
136+
OperatorsTestSuite.test("exclaim equal (!=)") {
137+
let lhs = IntBox(value: 5)
138+
let rhs = IntBox(value: 5)
139+
140+
let result = lhs != rhs
141+
142+
expectEqual(false, result)
143+
}
144+
145+
OperatorsTestSuite.test("less equal (<=)") {
146+
let lhs = IntBox(value: 5)
147+
let rhs = IntBox(value: 5)
148+
149+
let result = lhs <= rhs
150+
151+
expectEqual(true, result)
152+
}
153+
154+
OperatorsTestSuite.test("greater equal (>=)") {
155+
let lhs = IntBox(value: 6)
156+
let rhs = IntBox(value: 5)
157+
158+
let result = lhs >= rhs
159+
160+
expectEqual(true, result)
161+
}
162+
109163
runAllTests()

0 commit comments

Comments
 (0)