From 2095354ff785324a6fd302929d1dc01313a346ad Mon Sep 17 00:00:00 2001 From: zoecarver Date: Thu, 11 Jun 2020 16:56:44 -0700 Subject: [PATCH 1/2] [cxx-interop] Add support for C++ shift operators. Support imported C++ `<<` and `>>` operators in Swift. --- lib/ClangImporter/ImportName.cpp | 2 ++ .../Cxx/operators/Inputs/non-member-inline.h | 8 ++++++++ .../non-member-inline-module-interface.swift | 2 ++ .../non-member-inline-typechecker.swift | 2 ++ .../Cxx/operators/non-member-inline.swift | 18 ++++++++++++++++++ 5 files changed, 32 insertions(+) diff --git a/lib/ClangImporter/ImportName.cpp b/lib/ClangImporter/ImportName.cpp index 8c3b90d485f09..9116f504689d2 100644 --- a/lib/ClangImporter/ImportName.cpp +++ b/lib/ClangImporter/ImportName.cpp @@ -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_LessLess: + case clang::OverloadedOperatorKind::OO_GreaterGreater: if (auto FD = dyn_cast(D)) { baseName = clang::getOperatorSpelling(op); isFunction = true; diff --git a/test/Interop/Cxx/operators/Inputs/non-member-inline.h b/test/Interop/Cxx/operators/Inputs/non-member-inline.h index fbe01efbe7146..34e8243c20e04 100644 --- a/test/Interop/Cxx/operators/Inputs/non-member-inline.h +++ b/test/Interop/Cxx/operators/Inputs/non-member-inline.h @@ -21,6 +21,14 @@ inline IntBox operator/(IntBox lhs, IntBox rhs) { return IntBox{.value = lhs.value / rhs.value}; } +inline IntBox operator<<(IntBox lhs, IntBox rhs) { + return IntBox{.value = lhs.value << rhs.value}; +} + +inline IntBox operator>>(IntBox lhs, IntBox rhs) { + return IntBox{.value = lhs.value >> rhs.value}; +} + // Make sure that we don't crash on templated operators template struct S {}; template S operator+(S lhs, S rhs); diff --git a/test/Interop/Cxx/operators/non-member-inline-module-interface.swift b/test/Interop/Cxx/operators/non-member-inline-module-interface.swift index 4c800e7dac306..eff9cb50ba2f3 100644 --- a/test/Interop/Cxx/operators/non-member-inline-module-interface.swift +++ b/test/Interop/Cxx/operators/non-member-inline-module-interface.swift @@ -4,3 +4,5 @@ // 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-NEXT: func << (lhs: IntBox, rhs: IntBox) -> IntBox +// CHECK-NEXT: func >> (lhs: IntBox, rhs: IntBox) -> IntBox diff --git a/test/Interop/Cxx/operators/non-member-inline-typechecker.swift b/test/Interop/Cxx/operators/non-member-inline-typechecker.swift index 45bb76c892488..f13d93455529d 100644 --- a/test/Interop/Cxx/operators/non-member-inline-typechecker.swift +++ b/test/Interop/Cxx/operators/non-member-inline-typechecker.swift @@ -9,3 +9,5 @@ let resultPlus = lhs + rhs let resultMinus = lhs - rhs let resultStar = lhs * rhs let resultSlash = lhs / rhs +let resultLessLess = lhs << rhs +let resultGreaterGreater = lhs >> rhs diff --git a/test/Interop/Cxx/operators/non-member-inline.swift b/test/Interop/Cxx/operators/non-member-inline.swift index f1ce1f052f232..472feeffdfac3 100644 --- a/test/Interop/Cxx/operators/non-member-inline.swift +++ b/test/Interop/Cxx/operators/non-member-inline.swift @@ -43,4 +43,22 @@ OperatorsTestSuite.test("slash") { expectEqual(1, result.value) } +OperatorsTestSuite.test("less less (<<)") { + let lhs = IntBox(value: 2) + let rhs = IntBox(value: 4) + + let result = lhs << rhs + + expectEqual(32, result.value) +} + +OperatorsTestSuite.test("greater greater (>>)") { + let lhs = IntBox(value: 512) + let rhs = IntBox(value: 8) + + let result = lhs >> rhs + + expectEqual(2, result.value) +} + runAllTests() From af276b703cce961c7743183a4819225a0786e9c8 Mon Sep 17 00:00:00 2001 From: Michael Forster Date: Fri, 12 Jun 2020 14:38:04 +0200 Subject: [PATCH 2/2] Update test names of existing operators ... to match the new ones. --- test/Interop/Cxx/operators/non-member-inline.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/Interop/Cxx/operators/non-member-inline.swift b/test/Interop/Cxx/operators/non-member-inline.swift index 472feeffdfac3..38a36ab4242b6 100644 --- a/test/Interop/Cxx/operators/non-member-inline.swift +++ b/test/Interop/Cxx/operators/non-member-inline.swift @@ -7,7 +7,7 @@ import StdlibUnittest var OperatorsTestSuite = TestSuite("Operators") -OperatorsTestSuite.test("plus") { +OperatorsTestSuite.test("plus (+)") { let lhs = IntBox(value: 42) let rhs = IntBox(value: 23) @@ -16,7 +16,7 @@ OperatorsTestSuite.test("plus") { expectEqual(65, result.value) } -OperatorsTestSuite.test("minus") { +OperatorsTestSuite.test("minus (-)") { let lhs = IntBox(value: 42) let rhs = IntBox(value: 23) @@ -25,7 +25,7 @@ OperatorsTestSuite.test("minus") { expectEqual(19, result.value) } -OperatorsTestSuite.test("star") { +OperatorsTestSuite.test("star (*)") { let lhs = IntBox(value: 42) let rhs = IntBox(value: 23) @@ -34,7 +34,7 @@ OperatorsTestSuite.test("star") { expectEqual(966, result.value) } -OperatorsTestSuite.test("slash") { +OperatorsTestSuite.test("slash (/)") { let lhs = IntBox(value: 42) let rhs = IntBox(value: 23)