Skip to content

[cxx-interop] Add support for C++ increment decrement operations #41232

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

Closed
Closed
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 @@ -1802,6 +1802,8 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
case clang::OverloadedOperatorKind::OO_GreaterEqual:
case clang::OverloadedOperatorKind::OO_AmpAmp:
case clang::OverloadedOperatorKind::OO_PipePipe:
case clang::OverloadedOperatorKind::OO_PlusPlus:
case clang::OverloadedOperatorKind::OO_MinusMinus:
baseName = clang::getOperatorSpelling(op);
isFunction = true;
argumentNames.resize(
Expand Down
8 changes: 8 additions & 0 deletions test/Interop/Cxx/operators/Inputs/non-member-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ inline LoadableIntWrapper operator>>(LoadableIntWrapper lhs, LoadableIntWrapper
return LoadableIntWrapper{.value = lhs.value >> rhs.value};
}

inline LoadableIntWrapper operator++(LoadableIntWrapper lhs) {
return LoadableIntWrapper{.value = lhs.value++};
}

inline LoadableIntWrapper operator--(LoadableIntWrapper lhs) {
return LoadableIntWrapper{.value = lhs.value--};
}

inline bool operator<(LoadableIntWrapper lhs, LoadableIntWrapper rhs) { return lhs.value < rhs.value; }

inline bool operator>(LoadableIntWrapper lhs, LoadableIntWrapper rhs) { return lhs.value > rhs.value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// CHECK-NEXT: func | (lhs: LoadableIntWrapper, rhs: LoadableIntWrapper) -> LoadableIntWrapper
// CHECK-NEXT: func << (lhs: LoadableIntWrapper, rhs: LoadableIntWrapper) -> LoadableIntWrapper
// CHECK-NEXT: func >> (lhs: LoadableIntWrapper, rhs: LoadableIntWrapper) -> LoadableIntWrapper
// CHECK-NEXT: func ++ (lhs: LoadableIntWrapper) -> LoadableIntWrapper
// CHECK-NEXT: func -- (lhs: LoadableIntWrapper) -> LoadableIntWrapper
// CHECK-NEXT: func < (lhs: LoadableIntWrapper, rhs: LoadableIntWrapper) -> Bool
// CHECK-NEXT: func > (lhs: LoadableIntWrapper, rhs: LoadableIntWrapper) -> Bool
// CHECK-NEXT: func == (lhs: LoadableIntWrapper, rhs: LoadableIntWrapper) -> Bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ let resultEqualEqual = lhs == rhs
let resultExclaimEqual = lhs != rhs
let resultLessEqual = lhs <= rhs
let resultGreaterEqual = lhs >= rhs
let reusltPlusPlus = lhs ++ rhs
let reusltMinusMinus = lhs -- rhs

var lhsBool = LoadableBoolWrapper(value: true)
var rhsBool = LoadableBoolWrapper(value: false)
Expand Down
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 @@ -169,4 +169,22 @@ OperatorsTestSuite.test("pipe pipe (||)") {
expectEqual(true, result.value)
}

OperatorsTestSuite.test("plus plus (++)") {
let lhs = LoadableIntWrapper(value: 42)
let rhs = LoadableIntWrapper(value: 1)

let result = lhs ++ rhs

expectEqual(43, result.value)
}

OperatorsTestSuite.test("minus minus (--)") {
let lhs = LoadableIntWrapper(value: 42)
let rhs = LoadableIntWrapper(value: 1)

let result = lhs -- 1

expectEqual(41, result.value)
}

runAllTests()