Skip to content

Commit a395ac5

Browse files
authored
Merge pull request swiftlang#39428 from zoecarver/cxx/ref-as-inout
[cxx-interop] Import reference types as `inout` not `UnsafePointer`.
2 parents 5e72d69 + 7a42b71 commit a395ac5

File tree

7 files changed

+37
-60
lines changed

7 files changed

+37
-60
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
19151915
// Import the parameter type into Swift.
19161916
Type swiftParamTy;
19171917
bool isParamTypeImplicitlyUnwrapped = false;
1918+
bool isInOut = false;
19181919

19191920
auto referenceType = dyn_cast<clang::ReferenceType>(paramTy);
19201921
if (referenceType &&
@@ -1934,6 +1935,10 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
19341935
swiftParamTy =
19351936
findGenericTypeInGenericDecls(templateParamType, genericParams);
19361937
} else {
1938+
if (auto refType = dyn_cast<clang::ReferenceType>(paramTy)) {
1939+
paramTy = refType->getPointeeType();
1940+
isInOut = true;
1941+
}
19371942
auto importedType = importType(paramTy, importKind, allowNSUIntegerAsInt,
19381943
Bridgeability::Full, OptionalityOfParam);
19391944
if (!importedType)
@@ -1965,7 +1970,8 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
19651970
param, AccessLevel::Private, SourceLoc(), SourceLoc(), name,
19661971
importSourceLoc(param->getLocation()), bodyName,
19671972
ImportedHeaderUnit);
1968-
paramInfo->setSpecifier(ParamSpecifier::Default);
1973+
paramInfo->setSpecifier(isInOut ? ParamSpecifier::InOut
1974+
: ParamSpecifier::Default);
19691975
paramInfo->setInterfaceType(swiftParamTy);
19701976
recordImplicitUnwrapForDecl(paramInfo, isParamTypeImplicitlyUnwrapped);
19711977
recordUnsafeConcurrencyForDecl(

lib/SIL/IR/Bridging.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ TypeConverter::getBridgedParam(SILFunctionTypeRepresentation rep,
4242
AbstractionPattern pattern,
4343
AnyFunctionType::Param param,
4444
Bridgeability bridging) {
45-
assert(!param.getParameterFlags().isInOut() &&
46-
!param.getParameterFlags().isVariadic());
45+
assert(!param.getParameterFlags().isVariadic());
4746

4847
auto bridged = getLoweredBridgedType(pattern, param.getPlainType(), bridging,
4948
rep, TypeConverter::ForArgument);

test/Interop/Cxx/reference/reference-irgen.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,31 @@ public func getConstCxxRvalueRef() -> UnsafePointer<CInt> {
3232

3333
public func setCxxRef() {
3434
var val: CInt = 21
35-
withUnsafeMutablePointer(to: &val) {
36-
setStaticIntRef($0)
37-
}
35+
setStaticIntRef(&val)
3836
}
3937

4038
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main9setCxxRefyyF"()
4139
// CHECK: call void @{{_Z15setStaticIntRefRi|"\?setStaticIntRef@@YAXAEAH@Z"}}(i32* {{nonnull %val|%2}})
4240

4341
public func setCxxConstRef() {
4442
var val: CInt = 21
45-
withUnsafePointer(to: &val) {
46-
setConstStaticIntRef($0)
47-
}
43+
setConstStaticIntRef(&val)
4844
}
4945

5046
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main14setCxxConstRefyyF"()
5147
// CHECK: call void @{{_Z20setConstStaticIntRefRKi|"\?setConstStaticIntRef@@YAXAEBH@Z"}}(i32* {{nonnull %val|%2}})
5248

5349
public func setCxxRvalueRef() {
5450
var val: CInt = 21
55-
withUnsafeMutablePointer(to: &val) {
56-
setStaticIntRvalueRef($0)
57-
}
51+
setStaticIntRvalueRef(&val)
5852
}
5953

6054
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main15setCxxRvalueRefyyF"()
6155
// CHECK: call void @{{_Z21setStaticIntRvalueRefOi|"\?setStaticIntRvalueRef@@YAX\$\$QEAH@Z"}}(i32* {{nonnull %val|%2}})
6256

6357
public func setCxxConstRvalueRef() {
6458
var val: CInt = 21
65-
withUnsafePointer(to: &val) {
66-
setConstStaticIntRvalueRef($0)
67-
}
59+
setConstStaticIntRvalueRef(&val)
6860
}
6961

7062
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main20setCxxConstRvalueRefyyF"()

test/Interop/Cxx/reference/reference-module-interface.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
// CHECK: func getConstStaticIntRef() -> UnsafePointer<Int32>
77
// CHECK: func getConstStaticIntRvalueRef() -> UnsafePointer<Int32>
88
// CHECK: func setStaticInt(_: Int32)
9-
// CHECK: func setStaticIntRef(_: UnsafeMutablePointer<Int32>)
10-
// CHECK: func setStaticIntRvalueRef(_: UnsafeMutablePointer<Int32>)
11-
// CHECK: func setConstStaticIntRef(_: UnsafePointer<Int32>)
12-
// CHECK: func setConstStaticIntRvalueRef(_: UnsafePointer<Int32>)
9+
// CHECK: func setStaticIntRef(_: inout Int32)
10+
// CHECK: func setStaticIntRvalueRef(_: inout Int32)
11+
// CHECK: func setConstStaticIntRef(_: inout Int32)
12+
// CHECK: func setConstStaticIntRvalueRef(_: inout Int32)
1313
// CHECK: func getFuncRef() -> @convention(c) () -> Int32
1414
// CHECK: func getFuncRvalueRef() -> @convention(c) () -> Int32
1515

test/Interop/Cxx/reference/reference-silgen.swift

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,48 +36,36 @@ func getConstCxxRvalueRef() -> UnsafePointer<CInt> {
3636

3737
func setCxxRef() {
3838
var val: CInt = 21
39-
withUnsafeMutablePointer(to: &val) {
40-
setStaticIntRef($0)
41-
}
39+
setStaticIntRef(&val)
4240
}
4341

44-
// CHECK: // closure #1 in setCxxRef()
45-
// CHECK: sil private @$s4main9setCxxRefyyFySpys5Int32VGXEfU_ : $@convention(thin) (UnsafeMutablePointer<Int32>) -> ()
46-
// CHECK: [[REF:%.*]] = function_ref @{{_Z15setStaticIntRefRi|\?setStaticIntRef@@YAXAEAH@Z}} : $@convention(c) (UnsafeMutablePointer<Int32>) -> ()
47-
// CHECK: apply [[REF]](%0) : $@convention(c) (UnsafeMutablePointer<Int32>) -> ()
42+
// CHECK: sil hidden @$s4main9setCxxRefyyF : $@convention(thin) () -> ()
43+
// CHECK: [[REF:%.*]] = function_ref @{{_Z15setStaticIntRefRi|\?setStaticIntRef@@YAXAEAH@Z}} : $@convention(c) (@inout Int32) -> ()
44+
// CHECK: apply [[REF]](%{{[0-9]+}}) : $@convention(c) (@inout Int32) -> ()
4845

4946
func setCxxConstRef() {
5047
var val: CInt = 21
51-
withUnsafePointer(to: &val) {
52-
setConstStaticIntRef($0)
53-
}
48+
setConstStaticIntRef(&val)
5449
}
5550

56-
// CHECK: // closure #1 in setCxxConstRef()
57-
// CHECK: sil private @$s4main14setCxxConstRefyyFySPys5Int32VGXEfU_ : $@convention(thin) (UnsafePointer<Int32>) -> ()
58-
// CHECK: [[REF:%.*]] = function_ref @{{_Z20setConstStaticIntRefRKi|\?setConstStaticIntRef@@YAXAEBH@Z}} : $@convention(c) (UnsafePointer<Int32>) -> ()
59-
// CHECK: apply [[REF]](%0) : $@convention(c) (UnsafePointer<Int32>) -> ()
51+
// CHECK: sil hidden @$s4main14setCxxConstRefyyF : $@convention(thin) () -> ()
52+
// CHECK: [[REF:%.*]] = function_ref @{{_Z20setConstStaticIntRefRKi|\?setConstStaticIntRef@@YAXAEBH@Z}} : $@convention(c) (@inout Int32) -> ()
53+
// CHECK: apply [[REF]](%{{[0-9]+}}) : $@convention(c) (@inout Int32) -> ()
6054

6155
func setCxxRvalueRef() {
6256
var val: CInt = 21
63-
withUnsafeMutablePointer(to: &val) {
64-
setStaticIntRvalueRef($0)
65-
}
57+
setStaticIntRvalueRef(&val)
6658
}
6759

68-
// CHECK: // closure #1 in setCxxRvalueRef()
69-
// CHECK: sil private @$s4main15setCxxRvalueRefyyFySpys5Int32VGXEfU_ : $@convention(thin) (UnsafeMutablePointer<Int32>) -> ()
70-
// CHECK: [[REF:%.*]] = function_ref @{{_Z21setStaticIntRvalueRefOi|\?setStaticIntRvalueRef@@YAX\$\$QEAH@Z}} : $@convention(c) (UnsafeMutablePointer<Int32>) -> ()
71-
// CHECK: apply [[REF]](%0) : $@convention(c) (UnsafeMutablePointer<Int32>) -> ()
60+
// CHECK: sil hidden @$s4main15setCxxRvalueRefyyF : $@convention(thin) () -> ()
61+
// CHECK: [[REF:%.*]] = function_ref @{{_Z21setStaticIntRvalueRefOi|\?setStaticIntRvalueRef@@YAX\$\$QEAH@Z}} : $@convention(c) (@inout Int32) -> ()
62+
// CHECK: apply [[REF]](%{{[0-9]+}}) : $@convention(c) (@inout Int32) -> ()
7263

7364
func setCxxConstRvalueRef() {
7465
var val: CInt = 21
75-
withUnsafePointer(to: &val) {
76-
setConstStaticIntRvalueRef($0)
77-
}
66+
setConstStaticIntRvalueRef(&val)
7867
}
7968

80-
// CHECK: // closure #1 in setCxxConstRvalueRef()
81-
// CHECK: sil private @$s4main20setCxxConstRvalueRefyyFySPys5Int32VGXEfU_ : $@convention(thin) (UnsafePointer<Int32>) -> ()
82-
// CHECK: [[REF:%.*]] = function_ref @{{_Z26setConstStaticIntRvalueRefOKi|\?setConstStaticIntRvalueRef@@YAX\$\$QEBH@Z}} : $@convention(c) (UnsafePointer<Int32>) -> ()
83-
// CHECK: apply [[REF]](%0) : $@convention(c) (UnsafePointer<Int32>) -> ()
69+
// CHECK: sil hidden @$s4main20setCxxConstRvalueRefyyF : $@convention(thin) () -> ()
70+
// CHECK: [[REF:%.*]] = function_ref @{{_Z26setConstStaticIntRvalueRefOKi|\?setConstStaticIntRvalueRef@@YAX\$\$QEBH@Z}} : $@convention(c) (@inout Int32) -> ()
71+
// CHECK: apply [[REF]](%{{[0-9]+}}) : $@convention(c) (@inout Int32) -> ()

test/Interop/Cxx/reference/reference.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,28 @@ ReferenceTestSuite.test("write-rvalue-reference") {
4040
ReferenceTestSuite.test("pass-lvalue-reference") {
4141
expectNotEqual(21, getStaticInt())
4242
var val: CInt = 21
43-
withUnsafeMutablePointer(to: &val) {
44-
setStaticIntRef($0)
45-
}
43+
setStaticIntRef(&val)
4644
expectEqual(21, getStaticInt())
4745
}
4846

4947
ReferenceTestSuite.test("pass-const-lvalue-reference") {
5048
expectNotEqual(22, getStaticInt())
5149
var val: CInt = 22
52-
withUnsafePointer(to: &val) {
53-
setConstStaticIntRef($0)
54-
}
50+
setConstStaticIntRef(&val)
5551
expectEqual(22, getStaticInt())
5652
}
5753

5854
ReferenceTestSuite.test("pass-rvalue-reference") {
5955
expectNotEqual(52, getStaticInt())
6056
var val: CInt = 52
61-
withUnsafeMutablePointer(to: &val) {
62-
setStaticIntRvalueRef($0)
63-
}
57+
setStaticIntRvalueRef(&val)
6458
expectEqual(52, getStaticInt())
6559
}
6660

6761
ReferenceTestSuite.test("pass-const-rvalue-reference") {
6862
expectNotEqual(53, getStaticInt())
6963
var val: CInt = 53
70-
withUnsafePointer(to: &val) {
71-
setConstStaticIntRvalueRef($0)
72-
}
64+
setConstStaticIntRvalueRef(&val)
7365
expectEqual(53, getStaticInt())
7466
}
7567

test/Interop/Cxx/templates/defaulted-template-type-parameter-module-interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
// The following types aren't imported correctly, but that does not have to do
1919
// with the fact that the template type paramaters are defaulted.
2020
// CHECK: func defaultedTemplatePointerTypeParam<T>(_ t: OpaquePointer!)
21-
// CHECK: func defaultedTemplatePointerReferenceTypeParam<T>(_ t: UnsafeMutablePointer<OpaquePointer?>)
21+
// CHECK: func defaultedTemplatePointerReferenceTypeParam<T>(_ t: inout OpaquePointer!)
2222
// CHECK: func defaultedTemplatePointerPointerTypeParam<T>(_ t: UnsafeMutablePointer<OpaquePointer?>!)

0 commit comments

Comments
 (0)