[Issue 17636] Support pragma(mangle) on types

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Jul 11 12:08:53 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17636

--- Comment #1 from Jacob Carlborg <doob at me.com> ---
In D it's possible to use pragma(mangle) on a function to set the name that
will actually end up in the binary. This is useful if you need to link to a C
function that uses the same name as a keyword in D.

I think we need the same thing for types as well. This is important when
binding to C++ and Objective-C.

For C++ it's due the the types are mangled as part of function signatures.
Other than conflicting D keywords, it's also important for another reason. In
C, the name of structs live in their own namespace, making it possible to have
a struct and a function with the same name. Since you always need to use
"struct <name>" in C when referring to the type it works.

This caused a problem [1]. There's a function called "stat", which also accepts
an argument of the type "stat". In D, the struct is declared "stat_t" to avoid
the conflict with the function. When using C functions this works perfectly
fine since no mangling is used. But when using a C++ function taking an
argument of the type "stat_t" this will generate the wrong mangling since it's
expected to be "stat" on the C++ side.

For Objective-C a similar scenario can occur. Because in Objective-C protocols
(what we call interfaces in D) and classes live in different namespaces. In
Objective-C there's a root class called "Object" just as in D. But in
Objective-C this class also conforms to a protocol called "Object". For this to
work properly from the D side, one needs to be renamed in the D code and use
pragma(mangle) to set the actual name that would end up in the binary.

When it comes to C++ it might be better to have a pragma like pragma(name,
"Foo") that can be used on types that doesn't set the actual mangled name but
rather the identifier that should be used when the building the mangled name.
Since different compilers might used different mangled names.

[1] https://issues.dlang.org/show_bug.cgi?id=16650

--


More information about the Digitalmars-d-bugs mailing list