@@ -310,12 +310,7 @@ extension Swift2JavaTranslator {
310310 ) {
311311 printer. printSeparator ( decl. displayName)
312312
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)
319314
320315 // Render the "make the downcall" functions.
321316 printInitializerDowncallConstructor ( & printer, decl)
@@ -327,71 +322,59 @@ extension Swift2JavaTranslator {
327322 ) {
328323 printer. printSeparator ( decl. displayName)
329324
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)
336326
337327 // Render the "make the downcall" functions.
338328 printFuncDowncallMethod ( & printer, decl)
339329 }
340330
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 (
343333 _ printer: inout CodePrinter ,
344334 _ 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
345355 ) {
346356 printer. start ( " public static final FunctionDescriptor DESC = " )
347357
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 {
353360 printer. print ( " FunctionDescriptor.ofVoid( " , isEmptyParam ? . continue : . newLine)
354361 printer. indent ( )
355362 } else {
356363 printer. print ( " FunctionDescriptor.of( " )
357364 printer. indent ( )
358365 printer. print ( " /* -> */" , . continue)
359- printer. print ( resultType. foreignValueLayout, . parameterNewlineSeparator( isEmptyParam) )
366+ printer. print ( cFunc . resultType. foreignValueLayout, . parameterNewlineSeparator( isEmptyParam) )
360367 }
361368
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) )
366372 }
367373
368374 printer. outdent ( )
369375 printer. print ( " ); " )
370376 }
371377
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-
395378 public func printInitializerDowncallConstructor(
396379 _ printer: inout CodePrinter ,
397380 _ decl: ImportedFunc
@@ -562,10 +545,10 @@ extension Swift2JavaTranslator {
562545 } else if decl. translatedSignature. result. javaResultType == . void {
563546 printer. print ( " \( downCall) ; " )
564547 } else {
565- let placeholder = if !decl. translatedSignature. result. outParameters. isEmpty {
566- " _result "
567- } else {
548+ let placeholder = if decl. translatedSignature. result. outParameters. isEmpty {
568549 downCall
550+ } else {
551+ " _result "
569552 }
570553 let result = decl. translatedSignature. result. conversion. render ( & printer, placeholder)
571554
0 commit comments