14
14
15
15
import Foundation
16
16
import JavaTypes
17
- import SwiftSyntax
18
17
import OrderedCollections
18
+ import SwiftSyntax
19
19
20
20
/// Any imported (Swift) declaration
21
21
protocol ImportedDecl {
@@ -54,7 +54,7 @@ public struct ImportedNominalType: ImportedDecl {
54
54
/// The Java class name without the package.
55
55
public var javaClassName : String {
56
56
switch javaType {
57
- case . class( package : _, name : let name) : name
57
+ case . class( package : _, let name) : name
58
58
default : javaType. description
59
59
}
60
60
}
@@ -125,12 +125,16 @@ public enum SelfParameterVariant {
125
125
}
126
126
127
127
public struct ImportedFunc : ImportedDecl , CustomStringConvertible {
128
+
129
+ /// Swift module name (e.g. the target name where a type or function was declared)
130
+ public var module : String
131
+
128
132
/// If this function/method is member of a class/struct/protocol,
129
133
/// this will contain that declaration's imported name.
130
134
///
131
135
/// This is necessary when rendering accessor Java code we need the type that "self" is expecting to have.
132
- public var parentName : TranslatedType ?
133
- public var hasParent : Bool { parentName != nil }
136
+ public var parent : TranslatedType ?
137
+ public var hasParent : Bool { parent != nil }
134
138
135
139
/// This is a full name such as init(cap:name:).
136
140
public var identifier : String
@@ -147,8 +151,8 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
147
151
/// A display name to use to refer to the Swift declaration with its
148
152
/// enclosing type, if there is one.
149
153
public var displayName : String {
150
- if let parentName {
151
- return " \( parentName . swiftTypeName) . \( identifier) "
154
+ if let parent {
155
+ return " \( parent . swiftTypeName) . \( identifier) "
152
156
}
153
157
154
158
return identifier
@@ -158,7 +162,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
158
162
public var parameters : [ ImportedParam ]
159
163
160
164
public func effectiveParameters( selfVariant: SelfParameterVariant ? ) -> [ ImportedParam ] {
161
- if let parentName {
165
+ if let parent {
162
166
var params = parameters
163
167
164
168
// Add `self: Self` for method calls on a member
@@ -171,21 +175,15 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
171
175
case . pointer:
172
176
let selfParam : FunctionParameterSyntax = " self$: $swift_pointer "
173
177
params. append (
174
- ImportedParam (
175
- param: selfParam,
176
- type: parentName
177
- )
178
+ ImportedParam ( param: selfParam, type: parent)
178
179
)
179
180
180
181
case . memorySegment:
181
182
let selfParam : FunctionParameterSyntax = " self$: $java_lang_foreign_MemorySegment "
182
- var parentForSelf = parentName
183
+ var parentForSelf = parent
183
184
parentForSelf. javaType = . javaForeignMemorySegment
184
185
params. append (
185
- ImportedParam (
186
- param: selfParam,
187
- type: parentForSelf
188
- )
186
+ ImportedParam ( param: selfParam, type: parentForSelf)
189
187
)
190
188
}
191
189
@@ -197,7 +195,9 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
197
195
}
198
196
}
199
197
200
- public var swiftMangledName : String = " "
198
+ public var accessorThunkName : String {
199
+ SwiftKitPrinting . Names. functionThunk ( module: self . module, function: self )
200
+ }
201
201
202
202
public var swiftDecl : any DeclSyntaxProtocol
203
203
@@ -208,14 +208,16 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
208
208
public var isInit : Bool = false
209
209
210
210
public init (
211
+ module: String ,
211
212
decl: any DeclSyntaxProtocol ,
212
- parentName : TranslatedType ? ,
213
+ parent : TranslatedType ? ,
213
214
identifier: String ,
214
215
returnType: TranslatedType ,
215
216
parameters: [ ImportedParam ]
216
217
) {
217
218
self . swiftDecl = decl
218
- self . parentName = parentName
219
+ self . module = module
220
+ self . parent = parent
219
221
self . identifier = identifier
220
222
self . returnType = returnType
221
223
self . parameters = parameters
@@ -224,7 +226,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
224
226
public var description : String {
225
227
"""
226
228
ImportedFunc {
227
- mangledName : \( swiftMangledName )
229
+ accessorThunkName : \( self . accessorThunkName )
228
230
identifier: \( identifier)
229
231
returnType: \( returnType)
230
232
parameters: \( parameters)
@@ -243,6 +245,9 @@ public enum VariableAccessorKind {
243
245
}
244
246
245
247
public struct ImportedVariable : ImportedDecl , CustomStringConvertible {
248
+
249
+ public var module : String
250
+
246
251
/// If this function/method is member of a class/struct/protocol,
247
252
/// this will contain that declaration's imported name.
248
253
///
@@ -254,7 +259,7 @@ public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
254
259
public var identifier : String
255
260
256
261
/// Which accessors are we able to expose.
257
- ///
262
+ ///
258
263
/// Usually this will be all the accessors the variable declares,
259
264
/// however if the getter is async or throwing we may not be able to import it
260
265
/// (yet), and therefore would skip it from the supported set.
@@ -289,38 +294,44 @@ public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
289
294
290
295
switch kind {
291
296
case . set:
292
- let newValueParam : FunctionParameterSyntax = " _ newValue: \( self . returnType. cCompatibleSwiftType) "
297
+ let newValueParam : FunctionParameterSyntax =
298
+ " _ newValue: \( self . returnType. cCompatibleSwiftType) "
293
299
var funcDecl = ImportedFunc (
300
+ module: self . module,
294
301
decl: self . syntax!,
295
- parentName : self . parentName,
302
+ parent : self . parentName,
296
303
identifier: self . identifier,
297
304
returnType: TranslatedType . void,
298
305
parameters: [ . init( param: newValueParam, type: self . returnType) ] )
299
- funcDecl. swiftMangledName = self . swiftMangledName + " s " // form mangled name of the getter by adding the suffix
306
+ // FIXME: funcDecl.swiftMangledName = self.swiftMangledName + "s" // form mangled name of the getter by adding the suffix
300
307
return funcDecl
301
308
302
309
case . get:
303
310
var funcDecl = ImportedFunc (
311
+ module: self . module,
304
312
decl: self . syntax!,
305
- parentName : self . parentName,
313
+ parent : self . parentName,
306
314
identifier: self . identifier,
307
315
returnType: self . returnType,
308
316
parameters: [ ] )
309
- funcDecl. swiftMangledName = self . swiftMangledName + " g " // form mangled name of the getter by adding the suffix
317
+ // FIXME: funcDecl.swiftMangledName = self.swiftMangledName + "g" // form mangled name of the getter by adding the suffix
310
318
return funcDecl
311
319
}
312
320
}
313
321
314
- public func effectiveAccessorParameters( _ kind: VariableAccessorKind , selfVariant: SelfParameterVariant ? ) -> [ ImportedParam ] {
322
+ public func effectiveAccessorParameters(
323
+ _ kind: VariableAccessorKind , selfVariant: SelfParameterVariant ?
324
+ ) -> [ ImportedParam ] {
315
325
var params : [ ImportedParam ] = [ ]
316
326
317
327
if kind == . set {
318
- let newValueParam : FunctionParameterSyntax = " _ newValue: \( raw: self . returnType. swiftTypeName) "
328
+ let newValueParam : FunctionParameterSyntax =
329
+ " _ newValue: \( raw: self . returnType. swiftTypeName) "
319
330
params. append (
320
331
ImportedParam (
321
332
param: newValueParam,
322
333
type: self . returnType)
323
- )
334
+ )
324
335
}
325
336
326
337
if let parentName {
@@ -361,10 +372,12 @@ public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
361
372
public var syntax : VariableDeclSyntax ? = nil
362
373
363
374
public init (
375
+ module: String ,
364
376
parentName: TranslatedType ? ,
365
377
identifier: String ,
366
378
returnType: TranslatedType
367
379
) {
380
+ self . module = module
368
381
self . parentName = parentName
369
382
self . identifier = identifier
370
383
self . returnType = returnType
0 commit comments