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

**URL:** <https://forums.swift.org/t/how-to-determine-what-module-a-symbol-is-declared-in-from-its-mangled-name/63892>\
**Category:** Compiler\
**Tags:** mangling, symbolgraphgen\
**Created:** [March 22, 2023, 8:15pm UTC](https://forums.swift.org/t/how-to-determine-what-module-a-symbol-is-declared-in-from-its-mangled-name/63892 "2023-03-22T20:15:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 22, 2023, 8:15pm UTC](https://forums.swift.org/t/how-to-determine-what-module-a-symbol-is-declared-in-from-its-mangled-name/63892/1 "2023-03-22T20:15:21Z")

</div>

this is similar to [Formula to extract mangled protocol name from mangled protocol extension member?](https://forums.swift.org/t/formula-to-extract-mangled-protocol-name-from-mangled-protocol-extension-member/63811)

how do you determine the module a symbol is _declared in_ based on its mangled name?

for example:

```swift
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.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [March 22, 2023, 9:17pm UTC](https://forums.swift.org/t/how-to-determine-what-module-a-symbol-is-declared-in-from-its-mangled-name/63892/2 "2023-03-22T21:17:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 22, 2023, 9:26pm UTC](https://forums.swift.org/t/how-to-determine-what-module-a-symbol-is-declared-in-from-its-mangled-name/63892/3 "2023-03-22T21:26:24Z")

</div>

> [@jrose](#):
>
> 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.

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.

> [@jrose](#):
>
> 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),

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.

> [@jrose](#):
>
> including more information explicitly in the doc JSON so that you stop having to reach for these things.

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.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [March 22, 2023, 9:29pm UTC](https://forums.swift.org/t/how-to-determine-what-module-a-symbol-is-declared-in-from-its-mangled-name/63892/4 "2023-03-22T21:29:41Z")

</div>

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