@@ -310,12 +310,7 @@ extension Swift2JavaTranslator {
310
310
) {
311
311
printer. printSeparator ( decl. displayName)
312
312
313
- let descClassIdentifier = thunkNameRegistry. functionThunkName ( decl: decl)
314
- printer. printBraceBlock ( " private static class \( descClassIdentifier) " ) { printer in
315
- printFunctionDescriptorValue ( & printer, decl)
316
- printFunctionAddrValue ( & printer, decl)
317
- printFunctionHandleValue ( & printer)
318
- }
313
+ printJavaBindingDescriptorClass ( & printer, decl)
319
314
320
315
// Render the "make the downcall" functions.
321
316
printInitializerDowncallConstructor ( & printer, decl)
@@ -327,71 +322,59 @@ extension Swift2JavaTranslator {
327
322
) {
328
323
printer. printSeparator ( decl. displayName)
329
324
330
- let descClassIdentifier = thunkNameRegistry. functionThunkName ( decl: decl)
331
- printer. printBraceBlock ( " private static class \( descClassIdentifier) " ) { printer in
332
- printFunctionDescriptorValue ( & printer, decl)
333
- printFunctionAddrValue ( & printer, decl)
334
- printFunctionHandleValue ( & printer)
335
- }
325
+ printJavaBindingDescriptorClass ( & printer, decl)
336
326
337
327
// Render the "make the downcall" functions.
338
328
printFuncDowncallMethod ( & printer, decl)
339
329
}
340
330
341
- /// Print the 'FunctionDescriptor' of the Swift API.
342
- public func printFunctionDescriptorValue (
331
+ /// Printer FFM Java binding descriptors for the imported Swift API.
332
+ func printJavaBindingDescriptorClass (
343
333
_ printer: inout CodePrinter ,
344
334
_ decl: ImportedFunc
335
+ ) {
336
+ let thunkName = thunkNameRegistry. functionThunkName ( decl: decl)
337
+ let cFunc = decl. cFunctionDecl ( cName: thunkName)
338
+
339
+ printer. printBraceBlock ( " private static class \( cFunc. name) " ) { printer in
340
+ printFunctionDescriptorValue ( & printer, cFunc)
341
+ printer. print (
342
+ """
343
+ public static final MemorySegment ADDR =
344
+ \( self . swiftModuleName) .findOrThrow( " \( cFunc. name) " );
345
+ public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
346
+ """
347
+ )
348
+ }
349
+ }
350
+
351
+ /// Print the 'FunctionDescriptor' of the lowered cdecl thunk.
352
+ public func printFunctionDescriptorValue(
353
+ _ printer: inout CodePrinter ,
354
+ _ cFunc: CFunction
345
355
) {
346
356
printer. start ( " public static final FunctionDescriptor DESC = " )
347
357
348
- let loweredSignature = decl. loweredSignature
349
- let loweredParams = loweredSignature. allLoweredParameters
350
- let resultType = try ! CType ( cdeclType: loweredSignature. result. cdeclResultType)
351
- let isEmptyParam = loweredParams. isEmpty
352
- if resultType. isVoid {
358
+ let isEmptyParam = cFunc. parameters. isEmpty
359
+ if cFunc. resultType. isVoid {
353
360
printer. print ( " FunctionDescriptor.ofVoid( " , isEmptyParam ? . continue : . newLine)
354
361
printer. indent ( )
355
362
} else {
356
363
printer. print ( " FunctionDescriptor.of( " )
357
364
printer. indent ( )
358
365
printer. print ( " /* -> */" , . continue)
359
- printer. print ( resultType. foreignValueLayout, . parameterNewlineSeparator( isEmptyParam) )
366
+ printer. print ( cFunc . resultType. foreignValueLayout, . parameterNewlineSeparator( isEmptyParam) )
360
367
}
361
368
362
- for (param, isLast) in loweredParams. withIsLast {
363
- let paramType = try ! CType ( cdeclType: param. type)
364
- printer. print ( " /* \(param.parameterName ?? "_"): */" , . continue)
365
- printer. print ( paramType. foreignValueLayout, . parameterNewlineSeparator( isLast) )
369
+ for (param, isLast) in cFunc. parameters. withIsLast {
370
+ printer. print ( " /* \(param.name ?? "_"): */" , . continue)
371
+ printer. print ( param. type. foreignValueLayout, . parameterNewlineSeparator( isLast) )
366
372
}
367
373
368
374
printer. outdent ( )
369
375
printer. print ( " ); " )
370
376
}
371
377
372
- func printFunctionAddrValue(
373
- _ printer: inout CodePrinter ,
374
- _ decl: ImportedFunc
375
- ) {
376
- let thunkName = thunkNameRegistry. functionThunkName ( decl: decl)
377
- printer. print (
378
- """
379
- public static final MemorySegment ADDR =
380
- \( self . swiftModuleName) .findOrThrow( " \( thunkName) " );
381
- """
382
- )
383
- }
384
-
385
- func printFunctionHandleValue(
386
- _ printer: inout CodePrinter
387
- ) {
388
- printer. print (
389
- """
390
- public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
391
- """
392
- )
393
- }
394
-
395
378
public func printInitializerDowncallConstructor(
396
379
_ printer: inout CodePrinter ,
397
380
_ decl: ImportedFunc
@@ -562,10 +545,10 @@ extension Swift2JavaTranslator {
562
545
} else if decl. translatedSignature. result. javaResultType == . void {
563
546
printer. print ( " \( downCall) ; " )
564
547
} else {
565
- let placeholder = if !decl. translatedSignature. result. outParameters. isEmpty {
566
- " _result "
567
- } else {
548
+ let placeholder = if decl. translatedSignature. result. outParameters. isEmpty {
568
549
downCall
550
+ } else {
551
+ " _result "
569
552
}
570
553
let result = decl. translatedSignature. result. conversion. render ( & printer, placeholder)
571
554
0 commit comments