@@ -16,6 +16,7 @@ import Foundation
1616import JavaTypes
1717import SwiftSyntax
1818
19+ /// Any imported (Swift) declaration
1920protocol ImportedDecl {
2021
2122}
@@ -32,6 +33,7 @@ public struct ImportedNominalType: ImportedDecl {
3233
3334 public var initializers : [ ImportedFunc ] = [ ]
3435 public var methods : [ ImportedFunc ] = [ ]
36+ public var variables : [ ImportedVariable ] = [ ]
3537
3638 public init ( swiftTypeName: String , javaType: JavaType , swiftMangledName: String ? = nil , kind: NominalTypeKind ) {
3739 self . swiftTypeName = swiftTypeName
@@ -195,7 +197,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
195197
196198 public var swiftMangledName : String = " "
197199
198- public var swiftDeclRaw : String ? = nil
200+ public var syntax : String ? = nil
199201
200202 public var isInit : Bool = false
201203
@@ -221,7 +223,155 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
221223
222224 Swift mangled name:
223225 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+ public var supportedAccessorKinds : Set < VariableAccessorKind > = [ . get, . set]
256+
257+ /// This is the base identifier for the function, e.g., "init" for an
258+ /// initializer or "f" for "f(a:b:)".
259+ public var baseIdentifier : String {
260+ guard let idx = identifier. firstIndex ( of: " ( " ) else {
261+ return identifier
262+ }
263+ return String ( identifier [ ..< idx] )
264+ }
265+
266+ /// A display name to use to refer to the Swift declaration with its
267+ /// enclosing type, if there is one.
268+ public var displayName : String {
269+ if let parentName {
270+ return " \( parentName. swiftTypeName) . \( identifier) "
271+ }
272+
273+ return identifier
274+ }
275+
276+ public var returnType : TranslatedType
277+
278+ /// Synthetic signature of an accessor function of the given kind of this property
279+ public func accessorFunc( kind: VariableAccessorKind ) -> ImportedFunc ? {
280+ guard self . supportedAccessorKinds. contains ( kind) else {
281+ return nil
282+ }
283+
284+ switch kind {
285+ case . set:
286+ let newValueParam : FunctionParameterSyntax = " _ newValue: \( self . returnType. cCompatibleSwiftType) "
287+ var funcDecl = ImportedFunc (
288+ parentName: self . parentName,
289+ identifier: self . identifier,
290+ returnType: TranslatedType . void,
291+ parameters: [ . init( param: newValueParam, type: self . returnType) ] )
292+ funcDecl. swiftMangledName = self . swiftMangledName + " s " // form mangled name of the getter by adding the suffix
293+ return funcDecl
294+
295+ case . get:
296+ var funcDecl = ImportedFunc (
297+ parentName: self . parentName,
298+ identifier: self . identifier,
299+ returnType: self . returnType,
300+ parameters: [ ] )
301+ funcDecl. swiftMangledName = self . swiftMangledName + " g " // form mangled name of the getter by adding the suffix
302+ return funcDecl
303+ }
304+ }
305+
306+ public func effectiveAccessorParameters( _ kind: VariableAccessorKind , selfVariant: SelfParameterVariant ? ) -> [ ImportedParam ] {
307+ var params : [ ImportedParam ] = [ ]
308+
309+ if kind == . set {
310+ let newValueParam : FunctionParameterSyntax = " _ newValue: \( raw: self . returnType. swiftTypeName) "
311+ params. append (
312+ ImportedParam (
313+ param: newValueParam,
314+ type: self . returnType)
315+ )
316+ }
317+
318+ if let parentName {
319+ // Add `self: Self` for method calls on a member
320+ //
321+ // allocating initializer takes a Self.Type instead, but it's also a pointer
322+ switch selfVariant {
323+ case nil , . wrapper:
324+ break
325+
326+ case . pointer:
327+ let selfParam : FunctionParameterSyntax = " self$: $swift_pointer "
328+ params. append (
329+ ImportedParam (
330+ param: selfParam,
331+ type: parentName
332+ )
333+ )
334+
335+ case . memorySegment:
336+ let selfParam : FunctionParameterSyntax = " self$: $java_lang_foreign_MemorySegment "
337+ var parentForSelf = parentName
338+ parentForSelf. javaType = . javaForeignMemorySegment
339+ params. append (
340+ ImportedParam (
341+ param: selfParam,
342+ type: parentForSelf
343+ )
344+ )
345+ }
346+ }
347+
348+ return params
349+ }
350+
351+ public var swiftMangledName : String = " "
352+
353+ public var syntax : VariableDeclSyntax ? = nil
354+
355+ public init (
356+ parentName: TranslatedType ? ,
357+ identifier: String ,
358+ returnType: TranslatedType
359+ ) {
360+ self . parentName = parentName
361+ self . identifier = identifier
362+ self . returnType = returnType
363+ }
364+
365+ public var description : String {
366+ """
367+ ImportedFunc {
368+ mangledName: \( swiftMangledName)
369+ identifier: \( identifier)
370+ returnType: \( returnType)
371+
372+ Swift mangled name:
373+ Imported from:
374+ \( syntax? . description ?? " <no swift source> " )
225375 }
226376 """
227377 }
0 commit comments