Skip to content

Commit 01b8081

Browse files
committed
[jextract] Improve CDecl lowering
* Newly introduce `lowerResult()` (splitted from `lowerParameter()` because result lowering can be differnt from parameter lowering. * Remove `ConversionStep.init(cdeclToSwift:)/.init(swiftToCdecl:)`. Instead embed the logic in `lowerParameter()`/`lowerResult()` so that we can implement type lowering and the actual conversion at the same place. * Support 'String', 'Void' and '() -> Void) types * Implement getter / setter lowering * Fix conversion for tuple returns (see: `lowerTupleReturns()` test) * Reference types are now returned indirectly, as the logic are being unifed. Thanks to this we don't need manual `retain()` before returning instances, which wasn't implemented correctly.
1 parent c7e5f12 commit 01b8081

11 files changed

+927
-676
lines changed

Sources/JExtractSwift/CDeclLowering/CDeclConversions.swift

Lines changed: 0 additions & 195 deletions
This file was deleted.

Sources/JExtractSwift/CDeclLowering/CRepresentation.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,22 @@ extension CType {
2424
init(cdeclType: SwiftType) throws {
2525
switch cdeclType {
2626
case .nominal(let nominalType):
27-
if let knownType = nominalType.nominalTypeDecl.knownStandardLibraryType,
28-
let primitiveCType = knownType.primitiveCType {
29-
self = primitiveCType
30-
return
27+
if let knownType = nominalType.nominalTypeDecl.knownStandardLibraryType {
28+
if let primitiveCType = knownType.primitiveCType {
29+
self = primitiveCType
30+
return
31+
}
32+
33+
switch knownType {
34+
case .unsafePointer where nominalType.genericArguments?.count == 1:
35+
self = .pointer(.qualified(const: true, volatile: false, type: try CType(cdeclType: nominalType.genericArguments![0])))
36+
return
37+
case .unsafeMutablePointer where nominalType.genericArguments?.count == 1:
38+
self = .pointer(try CType(cdeclType: nominalType.genericArguments![0]))
39+
return
40+
default:
41+
break
42+
}
3143
}
3244

3345
throw CDeclToCLoweringError.invalidNominalType(nominalType.nominalTypeDecl)
@@ -109,7 +121,8 @@ extension KnownStandardLibraryType {
109121
case .unsafeRawPointer: .pointer(
110122
.qualified(const: true, volatile: false, type: .void)
111123
)
112-
case .unsafePointer, .unsafeMutablePointer, .unsafeBufferPointer, .unsafeMutableBufferPointer:
124+
case .void: .void
125+
case .unsafePointer, .unsafeMutablePointer, .unsafeBufferPointer, .unsafeMutableBufferPointer, .string:
113126
nil
114127
}
115128
}

0 commit comments

Comments
 (0)