How to determine what module a symbol is declared in from its mangled name?

this is similar to Formula to extract mangled protocol name from mangled protocol extension member?

how do you determine the module a symbol is declared in based on its mangled name?

for example:

ss12IdentifiableP10ExtensionsE3bbbyyF

is part of the Extensions module, even though it is namespaced to Swift (ss).

the motivation is to filter out extraneous relationships from generated symbolgraphs. currently, symbolgraphs can declare symbol relationships that reference symbols that are not defined in the current symbolgraph, but it is not possible to determine if the symbol refers to a declaration in a different module (in which case, the relationship should be preserved), or if it refers to a hidden declaration in the current module (in which case, the relationship should be discarded).

i will note this is only necessary because SymbolGraphGen emits invalid relationships in the first place. if it did not emit references to hidden symbols, this would not be required.

I don’t have a right to say this because I’ve been away for years, but mangled names are just not what you want them to be. They’re either opaque unique identifiers or compressed encoding of a tree structure, and all of these parsing-by-regex approaches are targeting the latter form. Yes, you can do it, but it’s like performing arithmetic on the string representation of an integer. I really recommend either getting a proper demangler into the Swift project (it’s not hard to implement, just tedious, and if it’s part of the official project it’s much less likely to bitrot), or including more information explicitly in the doc JSON so that you stop having to reach for these things.

2 Likes

yes, i am fully aware of this. trust me, i do not want to be drilling into mangled names to query this information, it is simply not available anywhere else.

i posted this in the Development category because i figured mangled names are a compiler detail, and people here would be best equipped to answer these kinds of questions. but to clear up any misunderstandings, i do not work at Apple and i don’t have any authority to “get a proper demangler into the Swift project”.

although i certainly wish such a thing existed.

ditto. i do not control the “doc JSON” (symbolgraphs), that is generated by the toolchain through the swift symbolgraph-extract command. i am simply consuming the “doc JSON” as part of my own tooling.

1 Like

Fair enough, you are a frequent and valued contributor and can’t be faulted for seeking workarounds.

1 Like