digitalmars.D.learn - Name Mangling & its representation of D types
- NonNull (10/10) Aug 03 2021 I'd like to understand how any D type is represented as a string
- Mike Parker (24/34) Aug 03 2021 Name mangling applies to function parameters, as described here:
- Mike Parker (3/5) Aug 03 2021 A struct `S` with a member of type `S*` is still just a struct
- NonNull (3/8) Aug 03 2021 Aha, so it just uses the name S, not an algebraic representation
- Mathias LANG (17/26) Aug 03 2021 Yes, because D is a nominal type system, using the FQN of the
- NonNull (2/7) Aug 05 2021 All useful information, thank you.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/6) Aug 03 2021 Related article that mentions .mangleof, a property of all symbols:
- NonNull (2/9) Aug 03 2021 Thanks for that: will read.
I'd like to understand how any D type is represented as a string by the name mangling done by the compilers. Does this always have the desirable property that different types have different mangled names, so that a type is faithfully represented by its mangled string incorporated into a symbol name in an object file? What is that representation of a type as a string, and how does it work for recursive types like a struct containing a pointer to a struct of the same type? Please explain.
Aug 03 2021
On Tuesday, 3 August 2021 at 16:43:52 UTC, NonNull wrote:I'd like to understand how any D type is represented as a string by the name mangling done by the compilers. Does this always have the desirable property that different types have different mangled names, so that a type is faithfully represented by its mangled string incorporated into a symbol name in an object file? What is that representation of a type as a string, and how does it work for recursive types like a struct containing a pointer to a struct of the same type? Please explain.Name mangling applies to function parameters, as described here: https://forum.dlang.org/thread/akshntlfahjpknsxdrdv forum.dlang.org Type names aren't mangled. They have a Fully Qualified Name (FQN) which is constructed from package(s), module, parent. So for example: ```d module mypack.mymod; import std.stdio; struct S { struct Is {} } void main() { writeln(typeid(S)); writeln(typeid(S.Is)); } ``` This prints: ``` mypack.mymod.S mypack.mymod.S.Is ```
Aug 03 2021
On Tuesday, 3 August 2021 at 16:43:52 UTC, NonNull wrote:how does it work for recursive types like a struct containing a pointer to a struct of the same typeA struct `S` with a member of type `S*` is still just a struct `S`. The pointer doesn't change anything about the type.
Aug 03 2021
On Tuesday, 3 August 2021 at 17:01:38 UTC, Mike Parker wrote:On Tuesday, 3 August 2021 at 16:43:52 UTC, NonNull wrote:Aha, so it just uses the name S, not an algebraic representation of the structure of S.how does it work for recursive types like a struct containing a pointer to a struct of the same typeA struct `S` with a member of type `S*` is still just a struct `S`. The pointer doesn't change anything about the type.
Aug 03 2021
On Tuesday, 3 August 2021 at 20:29:10 UTC, NonNull wrote:On Tuesday, 3 August 2021 at 17:01:38 UTC, Mike Parker wrote:Yes, because D is a nominal type system, using the FQN of the symbol is enough. Mangled names start with `_D` and then contain the encoding of the symbol being mangled (can be a variable, a function, etc...). To avoid an explosion in the symbol length we have back reference (which the blog mentions IIRC). Not only are parameter types encoded, but so are function attributes (` safe` and co), with the notable exception of inferred `scope`, to allow people to link `-dip1000` code with non-DIP1000 code. `core.demangle` provides a programmatic way to demangle a symbol to its code representation. `ddemangle`, packed with DMD, allow to do it via the CLI (similar to `c++filt`). Newer binutils have support for D as well, so `nm --demangle=dlang` works (note that some older binutils support this, but don't support backref). Mangling is altered by `extern(LANG)` (where LANG is one of `C`, `C++`, `Objective-C`, `System`, `D`), or `pragma(mangle)`.On Tuesday, 3 August 2021 at 16:43:52 UTC, NonNull wrote:Aha, so it just uses the name S, not an algebraic representation of the structure of S.how does it work for recursive types like a struct containing a pointer to a struct of the same typeA struct `S` with a member of type `S*` is still just a struct `S`. The pointer doesn't change anything about the type.
Aug 03 2021
On Wednesday, 4 August 2021 at 02:01:54 UTC, Mathias LANG wrote:Yes, because D is a nominal type system, using the FQN of the symbol is enough. [...] Mangling is altered by `extern(LANG)` (where LANG is one of `C`, `C++`, `Objective-C`, `System`, `D`), or `pragma(mangle)`.All useful information, thank you.
Aug 05 2021
On 8/3/21 9:43 AM, NonNull wrote:I'd like to understand how any D type is represented as a string by the name mangling done by the compilers.Related article that mentions .mangleof, a property of all symbols: https://dlang.org/blog/2017/12/20/ds-newfangled-name-mangling/ Ali
Aug 03 2021
On Tuesday, 3 August 2021 at 17:14:42 UTC, Ali Çehreli wrote:On 8/3/21 9:43 AM, NonNull wrote:Thanks for that: will read.I'd like to understand how any D type is represented as a string by the name mangling done by the compilers.Related article that mentions .mangleof, a property of all symbols: https://dlang.org/blog/2017/12/20/ds-newfangled-name-mangling/ Ali
Aug 03 2021