Skip to content

[cxx-interop] Support operator[] with multiple parameters #75803

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
Aug 9, 2024
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
8 changes: 8 additions & 0 deletions lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,10 @@ void DeclAndTypeClangFunctionPrinter::printCxxSubscriptAccessorMethod(
Type resultTy, bool isDefinition,
std::optional<IRABIDetailsProvider::MethodDispatchInfo> dispatchInfo) {
assert(accessor->isGetter());
// operator[] with multiple parameters only supported C++23 and up.
bool multiParam = accessor->getParameters()->size() > 1;
if (multiParam)
os << "#if __cplusplus >= 202302L\n";
FunctionSignatureModifiers modifiers;
if (isDefinition)
modifiers.qualifierContext = typeDeclContext;
Expand All @@ -1671,6 +1675,8 @@ void DeclAndTypeClangFunctionPrinter::printCxxSubscriptAccessorMethod(
declAndTypePrinter.printAvailability(os, accessor->getStorage());
if (!isDefinition) {
os << ";\n";
if (multiParam)
os << "#endif // #if __cplusplus >= 202302L\n";
return;
}
os << " {\n";
Expand All @@ -1680,6 +1686,8 @@ void DeclAndTypeClangFunctionPrinter::printCxxSubscriptAccessorMethod(
accessor->getModuleContext(), resultTy, accessor->getParameters(),
/*hasThrows=*/false, nullptr, /*isStatic=*/false, dispatchInfo);
os << " }\n";
if (multiParam)
os << "#endif // #if __cplusplus >= 202302L\n";
}

bool DeclAndTypeClangFunctionPrinter::hasKnownOptionalNullableCxxMapping(
Expand Down
25 changes: 20 additions & 5 deletions test/Interop/SwiftToCxx/functions/swift-operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// RUN: %FileCheck %s < %t/operators.h

// RUN: %check-interop-cxx-header-in-clang(%t/operators.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY)
// RUN: %check-interop-cxx-header-in-clang(%t/operators.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -std=c++23)

// CHECK-LABEL: namespace Operators SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Operators") {

Expand All @@ -12,20 +13,34 @@

// CHECK: }

public struct IntBox { var x: CInt }
public struct IntBox {
var x: CInt

public subscript(x: CInt) -> CInt {
return x
}

public subscript(x: CInt, _: CInt) -> CInt {
return x
}
}

// CHECK: #if __cplusplus >= 202302L
// CHECK-NEXT: SWIFT_INLINE_THUNK int operator [](int x, int _2) const SWIFT_SYMBOL("s:9Operators6IntBoxVys5Int32VAE_AEtcig");
// CHECK-NEXT: #endif // #if __cplusplus >= 202302L

public func -(lhs: IntBox, rhs: IntBox) -> CInt {
return lhs.x - rhs.x
}

// CHECK: SWIFT_INLINE_THUNK int operator-(const IntBox& lhs, const IntBox& rhs) noexcept SWIFT_SYMBOL("s:9Operators1soiys5Int32VAA6IntBoxV_AFtF") SWIFT_WARN_UNUSED_RESULT {
// CHECK: return _impl::$s9Operators1soiys5Int32VAA6IntBoxV_AFtF(_impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(lhs)), _impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(rhs)));
// CHECK: }
// CHECK-NEXT: return _impl::$s9Operators1soiys5Int32VAA6IntBoxV_AFtF(_impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(lhs)), _impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(rhs)));
// CHECK-NEXT: }

public func ==(lhs: IntBox, rhs: IntBox) -> Bool {
return lhs.x == rhs.x
}

// CHECK: SWIFT_INLINE_THUNK bool operator==(const IntBox& lhs, const IntBox& rhs) noexcept SWIFT_SYMBOL("s:9Operators2eeoiySbAA6IntBoxV_ADtF") SWIFT_WARN_UNUSED_RESULT {
// CHECK: return _impl::$s9Operators2eeoiySbAA6IntBoxV_ADtF(_impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(lhs)), _impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(rhs)));
// CHECK: }
// CHECK-NEXT: return _impl::$s9Operators2eeoiySbAA6IntBoxV_ADtF(_impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(lhs)), _impl::swift_interop_passDirect_Operators_uint32_t_0_4(_impl::_impl_IntBox::getOpaquePointer(rhs)));
// CHECK-NEXT: }