Skip to content

Commit 9d14286

Browse files
authored
Merge pull request #32335 from zoecarver/cxx/logical-and-or
[cxx-interop] Add support for C++ logical and/or operators.
2 parents 23c7fc5 + d4431c4 commit 9d14286

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
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_AmpAmp:
1427+
case clang::OverloadedOperatorKind::OO_PipePipe:
14261428
if (auto FD = dyn_cast<clang::FunctionDecl>(D)) {
14271429
baseName = clang::getOperatorSpelling(op);
14281430
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+
struct BoolBox {
25+
bool value;
26+
};
27+
28+
inline BoolBox operator&&(BoolBox lhs, BoolBox rhs) {
29+
return BoolBox{.value = lhs.value && rhs.value};
30+
}
31+
32+
inline BoolBox operator||(BoolBox lhs, BoolBox rhs) {
33+
return BoolBox{.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+
8+
// CHECK: func && (lhs: BoolBox, rhs: BoolBox) -> BoolBox
9+
// 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
@@ -9,3 +9,9 @@ let resultPlus = lhs + rhs
99
let resultMinus = lhs - rhs
1010
let resultStar = lhs * rhs
1111
let resultSlash = lhs / rhs
12+
13+
var lhsBool = BoolBox(value: true)
14+
var rhsBool = BoolBox(value: false)
15+
16+
let resultAmpAmp = lhsBool && rhsBool
17+
let resultPipePipe = lhsBool && rhsBool

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

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

46+
OperatorsTestSuite.test("amp amp (&&)") {
47+
let lhs = BoolBox(value: true)
48+
let rhs = BoolBox(value: false)
49+
50+
let result = lhs && rhs
51+
52+
expectEqual(false, result.value)
53+
}
54+
55+
OperatorsTestSuite.test("pipe pipe (||)") {
56+
let lhs = BoolBox(value: true)
57+
let rhs = BoolBox(value: false)
58+
59+
let result = lhs || rhs
60+
61+
expectEqual(true, result.value)
62+
}
63+
4664
runAllTests()

0 commit comments

Comments
 (0)