-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathCDeclConversions.swift
More file actions
195 lines (178 loc) · 6.32 KB
/
CDeclConversions.swift
File metadata and controls
195 lines (178 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
extension ConversionStep {
/// Produce a conversion that takes in a value (or set of values) that
/// would be available in a @_cdecl function to represent the given Swift
/// type, and convert that to an instance of the Swift type.
init(cdeclToSwift swiftType: SwiftType) throws {
// If there is a 1:1 mapping between this Swift type and a C type, then
// there is no translation to do.
if let cType = try? CType(cdeclType: swiftType) {
_ = cType
self = .placeholder
return
}
switch swiftType {
case .function, .optional:
throw LoweringError.unhandledType(swiftType)
case .metatype(let instanceType):
self = .unsafeCastPointer(
.placeholder,
swiftType: instanceType
)
case .nominal(let nominal):
if let knownType = nominal.nominalTypeDecl.knownStandardLibraryType {
// Typed pointers
if let firstGenericArgument = nominal.genericArguments?.first {
switch knownType {
case .unsafePointer, .unsafeMutablePointer:
self = .typedPointer(
.explodedComponent(.placeholder, component: "pointer"),
swiftType: firstGenericArgument
)
return
case .unsafeBufferPointer, .unsafeMutableBufferPointer:
self = .initialize(
swiftType,
arguments: [
LabeledArgument(
label: "start",
argument: .typedPointer(
.explodedComponent(.placeholder, component: "pointer"),
swiftType: firstGenericArgument)
),
LabeledArgument(
label: "count",
argument: .explodedComponent(.placeholder, component: "count")
)
]
)
return
default:
break
}
}
}
// Arbitrary nominal types.
switch nominal.nominalTypeDecl.kind {
case .actor, .class:
// For actor and class, we pass around the pointer directly.
self = .unsafeCastPointer(.placeholder, swiftType: swiftType)
case .enum, .struct, .protocol:
// For enums, structs, and protocol types, we pass around the
// values indirectly.
self = .passIndirectly(
.pointee(.typedPointer(.placeholder, swiftType: swiftType))
)
}
case .tuple(let elements):
self = .tuplify(try elements.map { try ConversionStep(cdeclToSwift: $0) })
}
}
/// Produce a conversion that takes in a value that would be available in a
/// Swift function and convert that to the corresponding cdecl values.
///
/// This conversion goes in the opposite direction of init(cdeclToSwift:), and
/// is used for (e.g.) returning the Swift value from a cdecl function. When
/// there are multiple cdecl values that correspond to this one Swift value,
/// the result will be a tuple that can be assigned to a tuple of the cdecl
/// values, e.g., (<placeholder>.baseAddress, <placeholder>.count).
init(
swiftToCDecl swiftType: SwiftType,
stdlibTypes: SwiftStandardLibraryTypes
) throws {
// If there is a 1:1 mapping between this Swift type and a C type, then
// there is no translation to do.
if let cType = try? CType(cdeclType: swiftType) {
_ = cType
self = .placeholder
return
}
switch swiftType {
case .function, .optional:
throw LoweringError.unhandledType(swiftType)
case .metatype:
self = .unsafeCastPointer(
.placeholder,
swiftType: .nominal(
SwiftNominalType(
nominalTypeDecl: stdlibTypes[.unsafeRawPointer]
)
)
)
return
case .nominal(let nominal):
if let knownType = nominal.nominalTypeDecl.knownStandardLibraryType {
// Typed pointers
if nominal.genericArguments?.first != nil {
switch knownType {
case .unsafePointer, .unsafeMutablePointer:
let isMutable = knownType == .unsafeMutablePointer
self = ConversionStep(
initializeRawPointerFromTyped: .placeholder,
isMutable: isMutable,
isPartOfBufferPointer: false,
stdlibTypes: stdlibTypes
)
return
case .unsafeBufferPointer, .unsafeMutableBufferPointer:
let isMutable = knownType == .unsafeMutableBufferPointer
self = .tuplify(
[
ConversionStep(
initializeRawPointerFromTyped: .explodedComponent(
.placeholder,
component: "pointer"
),
isMutable: isMutable,
isPartOfBufferPointer: true,
stdlibTypes: stdlibTypes
),
.explodedComponent(.placeholder, component: "count")
]
)
return
default:
break
}
}
}
// Arbitrary nominal types.
switch nominal.nominalTypeDecl.kind {
case .actor, .class:
// For actor and class, we pass around the pointer directly. Case to
// the unsafe raw pointer type we use to represent it in C.
self = .unsafeCastPointer(
.placeholder,
swiftType: .nominal(
SwiftNominalType(
nominalTypeDecl: stdlibTypes[.unsafeRawPointer]
)
)
)
case .enum, .struct, .protocol:
// For enums, structs, and protocol types, we leave the value alone.
// The indirection will be handled by the caller.
self = .placeholder
}
case .tuple(let elements):
// Convert all of the elements.
self = .tuplify(
try elements.map { element in
try ConversionStep(swiftToCDecl: element, stdlibTypes: stdlibTypes)
}
)
}
}
}