@@ -16,6 +16,7 @@ import Foundation
16
16
import JavaTypes
17
17
import SwiftSyntax
18
18
19
+ /// Any imported (Swift) declaration
19
20
protocol ImportedDecl {
20
21
21
22
}
@@ -32,6 +33,7 @@ public struct ImportedNominalType: ImportedDecl {
32
33
33
34
public var initializers : [ ImportedFunc ] = [ ]
34
35
public var methods : [ ImportedFunc ] = [ ]
36
+ public var variables : [ ImportedVariable ] = [ ]
35
37
36
38
public init ( swiftTypeName: String , javaType: JavaType , swiftMangledName: String ? = nil , kind: NominalTypeKind ) {
37
39
self . swiftTypeName = swiftTypeName
@@ -195,7 +197,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
195
197
196
198
public var swiftMangledName : String = " "
197
199
198
- public var swiftDeclRaw : String ? = nil
200
+ public var syntax : String ? = nil
199
201
200
202
public var isInit : Bool = false
201
203
@@ -221,7 +223,160 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
221
223
222
224
Swift mangled name:
223
225
Imported from:
224
- \( swiftDeclRaw ?? " <no swift source> " )
226
+ \( syntax? . description ?? " <no swift source> " )
227
+ }
228
+ """
229
+ }
230
+ }
231
+
232
+ public enum VariableAccessorKind {
233
+ case get
234
+ case set
235
+
236
+ public var renderDescFieldName : String {
237
+ switch self {
238
+ case . get: " DESC_GET "
239
+ case . set: " DESC_SET "
240
+ }
241
+ }
242
+ }
243
+
244
+ public struct ImportedVariable : ImportedDecl , CustomStringConvertible {
245
+ /// If this function/method is member of a class/struct/protocol,
246
+ /// this will contain that declaration's imported name.
247
+ ///
248
+ /// This is necessary when rendering accessor Java code we need the type that "self" is expecting to have.
249
+ public var parentName : TranslatedType ?
250
+ public var hasParent : Bool { parentName != nil }
251
+
252
+ /// This is a full name such as "counter".
253
+ public var identifier : String
254
+
255
+ /// Which accessors are we able to expose.
256
+ ///
257
+ /// Usually this will be all the accessors the variable declares,
258
+ /// however if the getter is async or throwing we may not be able to import it
259
+ /// (yet), and therefore would skip it from the supported set.
260
+ public var supportedAccessorKinds : Set < VariableAccessorKind > = [ . get, . set]
261
+
262
+ /// This is the base identifier for the function, e.g., "init" for an
263
+ /// initializer or "f" for "f(a:b:)".
264
+ public var baseIdentifier : String {
265
+ guard let idx = identifier. firstIndex ( of: " ( " ) else {
266
+ return identifier
267
+ }
268
+ return String ( identifier [ ..< idx] )
269
+ }
270
+
271
+ /// A display name to use to refer to the Swift declaration with its
272
+ /// enclosing type, if there is one.
273
+ public var displayName : String {
274
+ if let parentName {
275
+ return " \( parentName. swiftTypeName) . \( identifier) "
276
+ }
277
+
278
+ return identifier
279
+ }
280
+
281
+ public var returnType : TranslatedType
282
+
283
+ /// Synthetic signature of an accessor function of the given kind of this property
284
+ public func accessorFunc( kind: VariableAccessorKind ) -> ImportedFunc ? {
285
+ guard self . supportedAccessorKinds. contains ( kind) else {
286
+ return nil
287
+ }
288
+
289
+ switch kind {
290
+ case . set:
291
+ let newValueParam : FunctionParameterSyntax = " _ newValue: \( self . returnType. cCompatibleSwiftType) "
292
+ var funcDecl = ImportedFunc (
293
+ parentName: self . parentName,
294
+ identifier: self . identifier,
295
+ returnType: TranslatedType . void,
296
+ parameters: [ . init( param: newValueParam, type: self . returnType) ] )
297
+ funcDecl. swiftMangledName = self . swiftMangledName + " s " // form mangled name of the getter by adding the suffix
298
+ return funcDecl
299
+
300
+ case . get:
301
+ var funcDecl = ImportedFunc (
302
+ parentName: self . parentName,
303
+ identifier: self . identifier,
304
+ returnType: self . returnType,
305
+ parameters: [ ] )
306
+ funcDecl. swiftMangledName = self . swiftMangledName + " g " // form mangled name of the getter by adding the suffix
307
+ return funcDecl
308
+ }
309
+ }
310
+
311
+ public func effectiveAccessorParameters( _ kind: VariableAccessorKind , selfVariant: SelfParameterVariant ? ) -> [ ImportedParam ] {
312
+ var params : [ ImportedParam ] = [ ]
313
+
314
+ if kind == . set {
315
+ let newValueParam : FunctionParameterSyntax = " _ newValue: \( raw: self . returnType. swiftTypeName) "
316
+ params. append (
317
+ ImportedParam (
318
+ param: newValueParam,
319
+ type: self . returnType)
320
+ )
321
+ }
322
+
323
+ if let parentName {
324
+ // Add `self: Self` for method calls on a member
325
+ //
326
+ // allocating initializer takes a Self.Type instead, but it's also a pointer
327
+ switch selfVariant {
328
+ case nil , . wrapper:
329
+ break
330
+
331
+ case . pointer:
332
+ let selfParam : FunctionParameterSyntax = " self$: $swift_pointer "
333
+ params. append (
334
+ ImportedParam (
335
+ param: selfParam,
336
+ type: parentName
337
+ )
338
+ )
339
+
340
+ case . memorySegment:
341
+ let selfParam : FunctionParameterSyntax = " self$: $java_lang_foreign_MemorySegment "
342
+ var parentForSelf = parentName
343
+ parentForSelf. javaType = . javaForeignMemorySegment
344
+ params. append (
345
+ ImportedParam (
346
+ param: selfParam,
347
+ type: parentForSelf
348
+ )
349
+ )
350
+ }
351
+ }
352
+
353
+ return params
354
+ }
355
+
356
+ public var swiftMangledName : String = " "
357
+
358
+ public var syntax : VariableDeclSyntax ? = nil
359
+
360
+ public init (
361
+ parentName: TranslatedType ? ,
362
+ identifier: String ,
363
+ returnType: TranslatedType
364
+ ) {
365
+ self . parentName = parentName
366
+ self . identifier = identifier
367
+ self . returnType = returnType
368
+ }
369
+
370
+ public var description : String {
371
+ """
372
+ ImportedFunc {
373
+ mangledName: \( swiftMangledName)
374
+ identifier: \( identifier)
375
+ returnType: \( returnType)
376
+
377
+ Swift mangled name:
378
+ Imported from:
379
+ \( syntax? . description ?? " <no swift source> " )
225
380
}
226
381
"""
227
382
}
0 commit comments