Skip to content

Commit 0edddde

Browse files
committed
[interop][SwiftToCxx] Add inout support for UnsafePointer/UnsafeMutablePointer param types
1 parent 9a80d78 commit 0edddde

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class CFunctionSignatureTypePrinter
9999
languageMode(languageMode), modifiersDelegate(modifiersDelegate),
100100
moduleContext(moduleContext), typeUseKind(typeUseKind) {}
101101

102+
void printInoutTypeModifier() {
103+
os << (languageMode == swift::OutputLanguageMode::Cxx ? " &" : " *");
104+
}
105+
102106
bool printIfKnownSimpleType(const TypeDecl *typeDecl,
103107
Optional<OptionalTypeKind> optionalKind,
104108
bool isInOutParam) {
@@ -109,9 +113,8 @@ class CFunctionSignatureTypePrinter
109113
if (knownTypeInfo->canBeNullable) {
110114
printNullability(optionalKind);
111115
}
112-
if (isInOutParam) {
113-
os << (languageMode == swift::OutputLanguageMode::Cxx ? " &" : " *");
114-
}
116+
if (isInOutParam)
117+
printInoutTypeModifier();
115118
return true;
116119
}
117120

@@ -202,6 +205,8 @@ class CFunctionSignatureTypePrinter
202205
os << " const";
203206
os << " *";
204207
printNullability(optionalKind);
208+
if (isInOutParam)
209+
printInoutTypeModifier();
205210
return true;
206211
}
207212

test/Interop/SwiftToCxx/functions/swift-primitive-inout-functions-cxx-bridging.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// CHECK: SWIFT_EXTERN void $s9Functions11inOutTwoIntyySiz_SiztF(ptrdiff_t * x, ptrdiff_t * y) SWIFT_NOEXCEPT SWIFT_CALL; // inOutTwoInt(_:_:)
99
// CHECK: SWIFT_EXTERN void $s9Functions13inOutTwoParamyySbz_SdztF(bool * x, double * y) SWIFT_NOEXCEPT SWIFT_CALL; // inOutTwoParam(_:_:)
1010
// CHECK: SWIFT_EXTERN void $s9Functions24inoutTypeWithNullabilityyySVzF(void const * _Nonnull * x) SWIFT_NOEXCEPT SWIFT_CALL; // inoutTypeWithNullability(_:)
11+
// CHECK: SWIFT_EXTERN void $s9Functions25inoutUnsafeGenericPointeryySPys5Int32VGzF(int32_t const * _Nonnull * x) SWIFT_NOEXCEPT SWIFT_CALL; // inoutUnsafeGenericPointer(_:)
1112

1213
// CHECK: inline void inOutInt(swift::Int & x) noexcept {
1314
// CHECK-NEXT: return _impl::$s9Functions8inOutIntyySizF(&x);
@@ -25,6 +26,9 @@
2526
// CHECK-NEXT: return _impl::$s9Functions24inoutTypeWithNullabilityyySVzF(&x);
2627
// CHECK-NEXT: }
2728

29+
// CHECK: inline void inoutUnsafeGenericPointer(int32_t const * _Nonnull & x) noexcept {
30+
// CHECK-NEXT: return _impl::$s9Functions25inoutUnsafeGenericPointeryySPys5Int32VGzF(&x);
31+
// CHECK-NEXT: }
2832

2933
public func inOutInt(_ x: inout Int) { x = Int() }
3034

@@ -41,3 +45,7 @@ public func inOutTwoParam(_ x: inout Bool, _ y: inout Double) {
4145
public func inoutTypeWithNullability(_ x: inout UnsafeRawPointer) {
4246
x += 1
4347
}
48+
49+
public func inoutUnsafeGenericPointer(_ x: inout UnsafePointer<Int32>) {
50+
x += 1
51+
}

test/Interop/SwiftToCxx/functions/swift-primitive-inout-functions-execution.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,13 @@ int main() {
4747
inoutTypeWithNullability(p);
4848
assert(*static_cast<const char *>(p) == 'B');
4949
}
50+
51+
{
52+
int a[2] = {1,2};
53+
const int *p = a;
54+
assert(*p == 1);
55+
inoutUnsafeGenericPointer(p);
56+
assert(p == &a[1]);
57+
assert(*p == 2);
58+
}
5059
}

0 commit comments

Comments
 (0)