Any libraries for working with Swift ABI manglings?

i’m starting to work with the Swift ABI manglings again, and one thing that would really help me is if there were a way to parse these strings and get structured information out of them. i’m also interested in using the mangled name grammar as a serialization format for things like generic constraints.

is this possible today?

I think the only supported way—aside from invoking swift-demangle -expand and parsing the text output—would be to compile and link directly to libDemangling from the compiler sources and traverse the node hierarchy that way. We have a project that does that, and suffice it to say, it's quite ugly going through the Swift <-> C <-> C++ funnel (we're not trying to use C++ interop yet).

With swift-syntax and the parser as precedent, I would love to see the next component of the compiler that gets Swiftified to be the mangler/demangler so that folks can just use it as a regular package dependency. :pray:

14 Likes

There's @Matt_Gallagher's CwlDemangle library, which we've been using in Reveal for years now for demangling type names for display.

None of the options available are ideal, and I too would love to see it exposed via some kind of officially supported API.

1 Like

that looks like exactly what i need. however, i noticed it has not been updated since Swift 5.3. has the Swift mangling ABI changed meaningfully since then? (e.g. will it work for async declarations?)

I'm only using it for types, so I'm not sure, but you're probably correct that it doesn't support newer mangled language features.

I haven't been proactive about updating CwlDemangle because it looked like Swift was going to implement it properly:

That proposal was returned for review but never returned :person_shrugging:

If it's missing something, I can probably update CwlDemangle. I don't use it much myself, anymore, so I'm not sure what the scale of missing features would be. Apple have pretty extensive test cases on their side and all I've done the last few times is convert their test cases to XCTests and work through the issues.

1 Like