Skip to content

[cxx-interop] Add support for C++ logical and/or operators. #32335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,8 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
case clang::OverloadedOperatorKind::OO_Minus:
case clang::OverloadedOperatorKind::OO_Star:
case clang::OverloadedOperatorKind::OO_Slash:
case clang::OverloadedOperatorKind::OO_AmpAmp:
case clang::OverloadedOperatorKind::OO_PipePipe:
if (auto FD = dyn_cast<clang::FunctionDecl>(D)) {
baseName = clang::getOperatorSpelling(op);
isFunction = true;
Expand Down
12 changes: 12 additions & 0 deletions test/Interop/Cxx/operators/Inputs/non-member-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ inline IntBox operator/(IntBox lhs, IntBox rhs) {
return IntBox{.value = lhs.value / rhs.value};
}

struct BoolBox {
bool value;
};

inline BoolBox operator&&(BoolBox lhs, BoolBox rhs) {
return BoolBox{.value = lhs.value && rhs.value};
}

inline BoolBox operator||(BoolBox lhs, BoolBox rhs) {
return BoolBox{.value = lhs.value || rhs.value};
}

// Make sure that we don't crash on templated operators
template<typename T> struct S {};
template<typename T> S<T> operator+(S<T> lhs, S<T> rhs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
// CHECK-NEXT: func - (lhs: IntBox, rhs: IntBox) -> IntBox
// CHECK-NEXT: func * (lhs: IntBox, rhs: IntBox) -> IntBox
// CHECK-NEXT: func / (lhs: IntBox, rhs: IntBox) -> IntBox

// CHECK: func && (lhs: BoolBox, rhs: BoolBox) -> BoolBox
// CHECK-NEXT: func || (lhs: BoolBox, rhs: BoolBox) -> BoolBox
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ let resultPlus = lhs + rhs
let resultMinus = lhs - rhs
let resultStar = lhs * rhs
let resultSlash = lhs / rhs

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

let resultAmpAmp = lhsBool && rhsBool
let resultPipePipe = lhsBool && rhsBool
18 changes: 18 additions & 0 deletions test/Interop/Cxx/operators/non-member-inline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,22 @@ OperatorsTestSuite.test("slash") {
expectEqual(1, result.value)
}

OperatorsTestSuite.test("amp amp (&&)") {
let lhs = BoolBox(value: true)
let rhs = BoolBox(value: false)

let result = lhs && rhs

expectEqual(false, result.value)
}

OperatorsTestSuite.test("pipe pipe (||)") {
let lhs = BoolBox(value: true)
let rhs = BoolBox(value: false)

let result = lhs || rhs

expectEqual(true, result.value)
}

runAllTests()