Skip to content

Commit c31b6cf

Browse files
committed
[interop][SwiftToCxx] generics: support inOut generic param
1 parent 63b652d commit c31b6cf

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ class CFunctionSignatureTypePrinter
223223
bool isInOutParam) {
224224
// FIXME: handle optionalKind.
225225
// FIXME: handle isInOutParam.
226-
os << "const ";
226+
if (!isInOutParam)
227+
os << "const ";
227228
if (languageMode == OutputLanguageMode::Cxx) {
228229
// Pass a reference to a template type.
229230
os << genericTpt->getName();
@@ -425,7 +426,10 @@ void DeclAndTypeClangFunctionPrinter::printCxxToCFunctionParameterUse(
425426
// FIXME: NEED to handle boxed resilient type.
426427
// os << "swift::" << cxx_synthesis::getCxxImplNamespaceName() <<
427428
// "::getOpaquePointer(";
428-
os << "reinterpret_cast<const void *>(&";
429+
os << "reinterpret_cast<";
430+
if (!isInOut)
431+
os << "const ";
432+
os << "void *>(&";
429433
namePrinter();
430434
os << ')';
431435
return;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,13 @@ int main() {
108108
// CHECK-NEXT: Bool value 1=false
109109
// CHECK-NEXT: other values=-78,45
110110
}
111+
112+
{
113+
int x = 42;
114+
int y = -13;
115+
genericSwap(x, y);
116+
assert(x == -13);
117+
assert(y == 42);
118+
}
111119
return 0;
112120
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ public func genericPrintFunctionMultiGeneric<T1, T2>(_ x: Int, _ t1: T1, _ t1p:
2121
print("other values=\(x),\(y)")
2222
}
2323

24+
public func genericSwap<T>(_ x: inout T, _ y: inout T) {
25+
let t = x
26+
x = y
27+
y = t
28+
}
29+
2430
// CHECK: SWIFT_EXTERN void $s9Functions20genericPrintFunctionyyxlF(const void * _Nonnull x, void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericPrintFunction(_:)
2531
// CHECK-NEXT: SWIFT_EXTERN void $s9Functions32genericPrintFunctionMultiGenericyySi_xxSiq_tr0_lF(ptrdiff_t x, const void * _Nonnull t1, const void * _Nonnull t1p, ptrdiff_t y, const void * _Nonnull t2, void * _Nonnull , void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericPrintFunctionMultiGeneric(_:_:_:_:_:)
2632
// CHECK-NEXT: SWIFT_EXTERN void $s9Functions26genericPrintFunctionTwoArgyyx_SitlF(const void * _Nonnull x, ptrdiff_t y, void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericPrintFunctionTwoArg(_:_:)
33+
// CHECK-NEXT: SWIFT_EXTERN void $s9Functions11genericSwapyyxz_xztlF(void * _Nonnull x, void * _Nonnull y, void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericSwap(_:_:)
2734

2835
// CHECK: template<class T>
2936
// CHECK-NEXT: requires swift::isUsableInGenericContext<T>
@@ -44,3 +51,9 @@ public func genericPrintFunctionMultiGeneric<T1, T2>(_ x: Int, _ t1: T1, _ t1p:
4451
// CHECK-NEXT: inline void genericPrintFunctionTwoArg(const T & x, swift::Int y) noexcept {
4552
// CHECK-NEXT: return _impl::$s9Functions26genericPrintFunctionTwoArgyyx_SitlF(reinterpret_cast<const void *>(&x), y, swift::getTypeMetadata<T>());
4653
// CHECK-NEXT: }
54+
55+
// CHECK: template<class T>
56+
// CHECK-NEXT: requires swift::isUsableInGenericContext<T>
57+
// CHECK-NEXT: inline void genericSwap(T & x, T & y) noexcept {
58+
// CHECK-NEXT: return _impl::$s9Functions11genericSwapyyxz_xztlF(reinterpret_cast<void *>(&x), reinterpret_cast<void *>(&y), swift::getTypeMetadata<T>());
59+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)