Thread overview
Name Mangling & its representation of D types
Aug 03, 2021
NonNull
Aug 03, 2021
Mike Parker
Aug 03, 2021
Mike Parker
Aug 03, 2021
NonNull
Aug 04, 2021
Mathias LANG
Aug 05, 2021
NonNull
Aug 03, 2021
Ali Çehreli
Aug 03, 2021
NonNull
August 03, 2021

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.

August 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:

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
August 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 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.

August 03, 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
August 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:

>

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.

August 03, 2021
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.

August 04, 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:

>

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).

August 05, 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.