Skip to content

Commit 0468c8a

Browse files
committed
[interop][SwiftToCxx] add 'inout' support for passing generic struct
1 parent 9ac400f commit 0468c8a

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ class CFunctionSignatureTypePrinter
231231
if (typeUseKind == FunctionSignatureTypeUse::ParamType) {
232232
if (languageMode != OutputLanguageMode::Cxx && !genericArgs.empty()) {
233233
// FIXME: what about concrete generic type.
234-
// FIXME: inout.
235-
os << "const void * _Nonnull";
234+
if (!isInOutParam)
235+
os << "const ";
236+
os << "void * _Nonnull";
236237
return ClangRepresentation::representable;
237238
}
238239
if (languageMode != OutputLanguageMode::Cxx &&

test/Interop/SwiftToCxx/generics/generic-struct-execution.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ int main() {
2424
takeGenericPair(xprime);
2525
// CHECK-NEXT: GenericPair<Int32, Int32>(x: 11, y: 42)
2626
// CHECK-NEXT: GenericPair<Int32, Int32>(x: 11, y: -995)
27+
inoutGenericPair(x, 0xFF);
28+
takeGenericPair(x);
29+
// CHECK-NEXT: GenericPair<Int32, Int32>(x: 255, y: 42)
2730
return 0;
2831
}

test/Interop/SwiftToCxx/generics/generic-struct-in-cxx.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// RUN: %check-generic-interop-cxx-header-in-clang(%t/generics.h)
1414

1515
public struct GenericPair<T, T2> {
16-
let x: T
17-
let y: T2
16+
var x: T
17+
var y: T2
1818
}
1919

2020
public func makeGenericPair<T, T1>(_ x: T, _ y: T1) -> GenericPair<T, T1> {
@@ -29,6 +29,15 @@ public func passThroughGenericPair<T1, T>(_ x: GenericPair<T1, T>, _ y: T) -> G
2929
return GenericPair<T1, T>(x: x.x, y: y)
3030
}
3131

32+
public func inoutGenericPair<T1, T>(_ x: inout GenericPair<T1, T>, _ y: T1) {
33+
x.x = y
34+
}
35+
36+
// CHECK: SWIFT_EXTERN void $s8Generics16inoutGenericPairyyAA0cD0Vyxq_Gz_xtr0_lF(void * _Nonnull x, const void * _Nonnull y, void * _Nonnull , void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // inoutGenericPair(_:_:)
37+
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics15makeGenericPairyAA0cD0Vyxq_Gx_q_tr0_lF(SWIFT_INDIRECT_RESULT void * _Nonnull, const void * _Nonnull x, const void * _Nonnull y, void * _Nonnull , void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // makeGenericPair(_:_:)
38+
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics22passThroughGenericPairyAA0dE0Vyxq_GAE_q_tr0_lF(SWIFT_INDIRECT_RESULT void * _Nonnull, const void * _Nonnull x, const void * _Nonnull y, void * _Nonnull , void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // passThroughGenericPair(_:_:)
39+
// CHECK-NEXT: SWIFT_EXTERN void $s8Generics15takeGenericPairyyAA0cD0Vyxq_Gr0_lF(const void * _Nonnull x, void * _Nonnull , void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // takeGenericPair(_:)
40+
3241
// CHECK: template<class T_0_0, class T_0_1>
3342
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
3443
// CHECK-NEXT: class _impl_GenericPair;
@@ -65,7 +74,13 @@ public func passThroughGenericPair<T1, T>(_ x: GenericPair<T1, T>, _ y: T) -> G
6574
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
6675
// CHECK-NEXT: class GenericPair;
6776
// CHECK-EMPTY:
68-
// CHECK-NEXT: template<class T, class T1>
77+
// CHECK-NEXT: template<class T1, class T>
78+
// CHECK-NEXT: requires swift::isUsableInGenericContext<T1> && swift::isUsableInGenericContext<T>
79+
// CHECK-NEXT: inline void inoutGenericPair(GenericPair<T1, T>& x, const T1 & y) noexcept {
80+
// CHECK-NEXT: return _impl::$s8Generics16inoutGenericPairyyAA0cD0Vyxq_Gz_xtr0_lF(_impl::_impl_GenericPair<T1, T>::getOpaquePointer(x), swift::_impl::getOpaquePointer(y), swift::getTypeMetadata<T1>(), swift::getTypeMetadata<T>());
81+
// CHECK-NEXT: }
82+
83+
// CHECK: template<class T, class T1>
6984
// CHECK-NEXT: requires swift::isUsableInGenericContext<T> && swift::isUsableInGenericContext<T1>
7085
// CHECK-NEXT: inline GenericPair<T, T1> makeGenericPair(const T & x, const T1 & y) noexcept SWIFT_WARN_UNUSED_RESULT {
7186
// CHECK-NEXT: return _impl::_impl_GenericPair<T, T1>::returnNewValue([&](void * _Nonnull result) {

0 commit comments

Comments
 (0)