Skip to content

Commit 3fc034b

Browse files
authored
Merge pull request #75803 from swiftlang/gaborh/subscript-multi-param
[cxx-interop] Support operator[] with multiple parameters
2 parents a95b69c + 57e00e4 commit 3fc034b

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,10 @@ void DeclAndTypeClangFunctionPrinter::printCxxSubscriptAccessorMethod(
16591659
Type resultTy, bool isDefinition,
16601660
std::optional<IRABIDetailsProvider::MethodDispatchInfo> dispatchInfo) {
16611661
assert(accessor->isGetter());
1662+
// operator[] with multiple parameters only supported C++23 and up.
1663+
bool multiParam = accessor->getParameters()->size() > 1;
1664+
if (multiParam)
1665+
os << "#if __cplusplus >= 202302L\n";
16621666
FunctionSignatureModifiers modifiers;
16631667
if (isDefinition)
16641668
modifiers.qualifierContext = typeDeclContext;
@@ -1671,6 +1675,8 @@ void DeclAndTypeClangFunctionPrinter::printCxxSubscriptAccessorMethod(
16711675
declAndTypePrinter.printAvailability(os, accessor->getStorage());
16721676
if (!isDefinition) {
16731677
os << ";\n";
1678+
if (multiParam)
1679+
os << "#endif // #if __cplusplus >= 202302L\n";
16741680
return;
16751681
}
16761682
os << " {\n";
@@ -1680,6 +1686,8 @@ void DeclAndTypeClangFunctionPrinter::printCxxSubscriptAccessorMethod(
16801686
accessor->getModuleContext(), resultTy, accessor->getParameters(),
16811687
/*hasThrows=*/false, nullptr, /*isStatic=*/false, dispatchInfo);
16821688
os << " }\n";
1689+
if (multiParam)
1690+
os << "#endif // #if __cplusplus >= 202302L\n";
16831691
}
16841692

16851693
bool DeclAndTypeClangFunctionPrinter::hasKnownOptionalNullableCxxMapping(

test/Interop/SwiftToCxx/functions/swift-operators.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// RUN: %FileCheck %s < %t/operators.h
44

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

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

@@ -12,20 +13,34 @@
1213

1314
// CHECK: }
1415

15-
public struct IntBox { var x: CInt }
16+
public struct IntBox {
17+
var x: CInt
18+
19+
public subscript(x: CInt) -> CInt {
20+
return x
21+
}
22+
23+
public subscript(x: CInt, _: CInt) -> CInt {
24+
return x
25+
}
26+
}
27+
28+
// CHECK: #if __cplusplus >= 202302L
29+
// CHECK-NEXT: SWIFT_INLINE_THUNK int operator [](int x, int _2) const SWIFT_SYMBOL("s:9Operators6IntBoxVys5Int32VAE_AEtcig");
30+
// CHECK-NEXT: #endif // #if __cplusplus >= 202302L
1631

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

2136
// CHECK: SWIFT_INLINE_THUNK int operator-(const IntBox& lhs, const IntBox& rhs) noexcept SWIFT_SYMBOL("s:9Operators1soiys5Int32VAA6IntBoxV_AFtF") SWIFT_WARN_UNUSED_RESULT {
22-
// 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)));
23-
// CHECK: }
37+
// 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)));
38+
// CHECK-NEXT: }
2439

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

2944
// CHECK: SWIFT_INLINE_THUNK bool operator==(const IntBox& lhs, const IntBox& rhs) noexcept SWIFT_SYMBOL("s:9Operators2eeoiySbAA6IntBoxV_ADtF") SWIFT_WARN_UNUSED_RESULT {
30-
// 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)));
31-
// CHECK: }
45+
// 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)));
46+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)