1414
1515import Foundation
1616import JavaTypes
17- import SwiftSyntax
1817import OrderedCollections
18+ import SwiftSyntax
1919
2020/// Any imported (Swift) declaration
2121protocol ImportedDecl {
@@ -54,7 +54,7 @@ public struct ImportedNominalType: ImportedDecl {
5454 /// The Java class name without the package.
5555 public var javaClassName : String {
5656 switch javaType {
57- case . class( package : _, name : let name) : name
57+ case . class( package : _, let name) : name
5858 default : javaType. description
5959 }
6060 }
@@ -125,12 +125,16 @@ public enum SelfParameterVariant {
125125}
126126
127127public 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+
128132 /// If this function/method is member of a class/struct/protocol,
129133 /// this will contain that declaration's imported name.
130134 ///
131135 /// 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 }
134138
135139 /// This is a full name such as init(cap:name:).
136140 public var identifier : String
@@ -147,8 +151,8 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
147151 /// A display name to use to refer to the Swift declaration with its
148152 /// enclosing type, if there is one.
149153 public var displayName : String {
150- if let parentName {
151- return " \( parentName . swiftTypeName) . \( identifier) "
154+ if let parent {
155+ return " \( parent . swiftTypeName) . \( identifier) "
152156 }
153157
154158 return identifier
@@ -158,7 +162,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
158162 public var parameters : [ ImportedParam ]
159163
160164 public func effectiveParameters( selfVariant: SelfParameterVariant ? ) -> [ ImportedParam ] {
161- if let parentName {
165+ if let parent {
162166 var params = parameters
163167
164168 // Add `self: Self` for method calls on a member
@@ -171,21 +175,15 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
171175 case . pointer:
172176 let selfParam : FunctionParameterSyntax = " self$: $swift_pointer "
173177 params. append (
174- ImportedParam (
175- param: selfParam,
176- type: parentName
177- )
178+ ImportedParam ( param: selfParam, type: parent)
178179 )
179180
180181 case . memorySegment:
181182 let selfParam : FunctionParameterSyntax = " self$: $java_lang_foreign_MemorySegment "
182- var parentForSelf = parentName
183+ var parentForSelf = parent
183184 parentForSelf. javaType = . javaForeignMemorySegment
184185 params. append (
185- ImportedParam (
186- param: selfParam,
187- type: parentForSelf
188- )
186+ ImportedParam ( param: selfParam, type: parentForSelf)
189187 )
190188 }
191189
@@ -197,7 +195,9 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
197195 }
198196 }
199197
200- public var swiftMangledName : String = " "
198+ public var accessorThunkName : String {
199+ SwiftKitPrinting . Names. functionThunk ( module: self . module, function: self )
200+ }
201201
202202 public var swiftDecl : any DeclSyntaxProtocol
203203
@@ -208,14 +208,16 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
208208 public var isInit : Bool = false
209209
210210 public init (
211+ module: String ,
211212 decl: any DeclSyntaxProtocol ,
212- parentName : TranslatedType ? ,
213+ parent : TranslatedType ? ,
213214 identifier: String ,
214215 returnType: TranslatedType ,
215216 parameters: [ ImportedParam ]
216217 ) {
217218 self . swiftDecl = decl
218- self . parentName = parentName
219+ self . module = module
220+ self . parent = parent
219221 self . identifier = identifier
220222 self . returnType = returnType
221223 self . parameters = parameters
@@ -224,7 +226,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
224226 public var description : String {
225227 """
226228 ImportedFunc {
227- mangledName : \( swiftMangledName )
229+ accessorThunkName : \( self . accessorThunkName )
228230 identifier: \( identifier)
229231 returnType: \( returnType)
230232 parameters: \( parameters)
@@ -243,6 +245,9 @@ public enum VariableAccessorKind {
243245}
244246
245247public struct ImportedVariable : ImportedDecl , CustomStringConvertible {
248+
249+ public var module : String
250+
246251 /// If this function/method is member of a class/struct/protocol,
247252 /// this will contain that declaration's imported name.
248253 ///
@@ -254,7 +259,7 @@ public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
254259 public var identifier : String
255260
256261 /// Which accessors are we able to expose.
257- ///
262+ ///
258263 /// Usually this will be all the accessors the variable declares,
259264 /// however if the getter is async or throwing we may not be able to import it
260265 /// (yet), and therefore would skip it from the supported set.
@@ -289,38 +294,44 @@ public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
289294
290295 switch kind {
291296 case . set:
292- let newValueParam : FunctionParameterSyntax = " _ newValue: \( self . returnType. cCompatibleSwiftType) "
297+ let newValueParam : FunctionParameterSyntax =
298+ " _ newValue: \( self . returnType. cCompatibleSwiftType) "
293299 var funcDecl = ImportedFunc (
300+ module: self . module,
294301 decl: self . syntax!,
295- parentName : self . parentName,
302+ parent : self . parentName,
296303 identifier: self . identifier,
297304 returnType: TranslatedType . void,
298305 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
300307 return funcDecl
301308
302309 case . get:
303310 var funcDecl = ImportedFunc (
311+ module: self . module,
304312 decl: self . syntax!,
305- parentName : self . parentName,
313+ parent : self . parentName,
306314 identifier: self . identifier,
307315 returnType: self . returnType,
308316 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
310318 return funcDecl
311319 }
312320 }
313321
314- public func effectiveAccessorParameters( _ kind: VariableAccessorKind , selfVariant: SelfParameterVariant ? ) -> [ ImportedParam ] {
322+ public func effectiveAccessorParameters(
323+ _ kind: VariableAccessorKind , selfVariant: SelfParameterVariant ?
324+ ) -> [ ImportedParam ] {
315325 var params : [ ImportedParam ] = [ ]
316326
317327 if kind == . set {
318- let newValueParam : FunctionParameterSyntax = " _ newValue: \( raw: self . returnType. swiftTypeName) "
328+ let newValueParam : FunctionParameterSyntax =
329+ " _ newValue: \( raw: self . returnType. swiftTypeName) "
319330 params. append (
320331 ImportedParam (
321332 param: newValueParam,
322333 type: self . returnType)
323- )
334+ )
324335 }
325336
326337 if let parentName {
@@ -361,10 +372,12 @@ public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
361372 public var syntax : VariableDeclSyntax ? = nil
362373
363374 public init (
375+ module: String ,
364376 parentName: TranslatedType ? ,
365377 identifier: String ,
366378 returnType: TranslatedType
367379 ) {
380+ self . module = module
368381 self . parentName = parentName
369382 self . identifier = identifier
370383 self . returnType = returnType
0 commit comments