Best way to get various information from swift framework like function signature, mangled names etc.?

I hope this is the right place to ask this questions, I wasn't sure whether it belongs to "Source Tooling" or not.
I'm currently writing a library to bridge Java and Swift. For that I need various informations about the functions/types etc. in a swift library, to write java binding code.
These information include, but are not limited to:

  • Mangled name of functions and types
  • The function signature
  • the function declaration order (to calculate vtable offsets)

I have found multiple ways to parse some of the information, but they were all lacking something so far.

  1. Some librarys are shipped/build with a *.abi.json file. This file is ideal since it contains everything I need and is easy to parse. However, it doesn't seem to be always existent for swift librarys, so it is not feasable.
  2. Swiftlibrarys seem to get shipped with a *.swiftinterface file, which contains all declarations in correct order. This file can be parsed and traversed with something like SwiftSyntax, but it lacks mangled names. I also haven't found any easy to use a name mangler.
  3. Since the *.swiftinterface is pretty close to valid swift code, I tried to compile it (with a bit pre-processing) and produce a *.abi.json file for 1. While this works fine for smaller projects, converting the .swiftinterface file to valid swift code gets pretty hard.
  4. Using swift symbolgraph-extract on the *.swiftdoc file produces a .json with the function signature, mangled names etc., but the declaration order is messed up thus preventing the calculation of vtable offsets.

These were all the ideas I had and tried. Is there any good way to retrieve all the needed information from a swift library I oversaw?
The last option I see is combining 2. and 4., getting the declaration order from the *.swiftinterface file and getting the rest from the *.swiftdoc. However that is a bit complicated and ugly, so I wanted to ask here first whether someone knows a better way.

Thanks in advance!


I just tested it and you should be able to generate the abi.json file for a module by passing -emit-abi-descriptor-path to a -compile-module-from-interface job. Here's an example of generating a descriptor for Foundation.framework:

xcrun swift-frontend -compile-module-from-interface /Applications/ -module-name Foundation -sdk `xcrun -sdk macosx --show-sdk-path` -emit-abi-descriptor-path ./abi.json

I'm not necessarily endorsing the use of the ABI descriptor file for your purposes; I don't know enough about what you want to achieve and what information you need to achieve it. If it seems like that file contains what you need then this should be a way to get one, though.

you cannot get the function signature of subscripts or computed properties through the symbolgraph, this information is only present for func declarations.

Ohhh, thank you very much! I was not aware of the -compile-module-from-interface command.
The created ABI file parses like a charm, so I think this solution should work for now!

Yeah I'm also not sure, I'm currently in a very early stage of the project, so not sure whether I'll stick to the ABI descriptor for retrieving the information. Currently I just need it to be able to test my Java - Swift bridging project against a larger amount of swift librarys, for which it works fine.

Ahh okay, I didn't realised! Thats good to know, thank you!

1 Like