www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Name Mangling & its representation of D types

reply NonNull <non-null use.startmail.com> writes:
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
next sibling parent Mike Parker <aldacron gmail.com> writes:
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
prev sibling next sibling parent reply Mike Parker <aldacron gmail.com> writes:
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 type
A 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
parent reply NonNull <non-null use.startmail.com> writes:
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:
 how does it work for recursive types like a struct containing 
 a pointer to a struct of the same type
A struct `S` with a member of type `S*` is still just a struct `S`. The pointer doesn't change anything about the type.
Aha, so it just uses the name S, not an algebraic representation of the structure of S.
Aug 03 2021
parent reply Mathias LANG <geod24 gmail.com> writes:
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:
 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 type
A struct `S` with a member of type `S*` is still just a struct `S`. The pointer doesn't change anything about the type.
Aha, so it just uses the name S, not an algebraic representation of the structure of S.
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)`.
Aug 03 2021
parent NonNull <non-null use.startmail.com> writes:
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
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
parent NonNull <non-null use.startmail.com> writes:
On Tuesday, 3 August 2021 at 17:14:42 UTC, Ali Çehreli wrote:
 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
Thanks for that: will read.
Aug 03 2021