extern(C) names are being mangled; that's crazy, Walter says I'm crazy

Mike Franklin slavo5150 at yahoo.com
Wed Jul 24 06:25:50 UTC 2019


On Wednesday, 24 July 2019 at 05:03:16 UTC, Manu wrote:
> https://issues.dlang.org/show_bug.cgi?id=20012
>
> I need to know who is the crazy one here?
> I can't believe the idea that extern(C) functions being mangled 
> to
> un-knowable generated names is what any sane user would ever 
> expect.
>
> Surely extern(C) means extern(C)?
>
> I'd like a poll here...

Just for comparison, here's what Rust does: 
https://doc.rust-lang.org/std/keyword.extern.html

I like the fact that `extern` and `mangle` mean different things, 
but I don't like the way D currently implements it. Here's how I 
would design it.

1. `pragma(mangle)` should come in more forms.
   a. `pragma(mangle, C)` will mangle according to C rules
   b. `pragma(mangle, C++)` will mangle according to C++ rules
   c. `pragma(mangle, D)` will mangle according to D rules
   d. `pragma(mangle, "foo")` will mangle explicitly

2. `extern` should default the mangling to one of the above
   a. `extern(C)` should mean `extern(C) pragma(mangle, C)`
   b. `extern(C++)` should mean `extern(C++) pragma(mangle, C++)`
   c. `extern(D)` should mean `extern(D) pragma(mangle, D)`

So, this code...
struct S
{
     extern(C) void foo() { }
}
... would actually translate into...
struct S
{
     extern(C) pragma(mangle, C) void foo() { }
}
... which, IIUC, would ultimately mean...
struct S
{
     extern(C) pragma(mangle, "foo") void foo() { }
}

This code...
mixin template M()
{
     extern(C) void fun() {}
}
mixin M!();
... would result in ...
{
     extern(C) pragma(mangle, C) void fun() {}
}
... which, in turn, would result in ...
{
     extern(C) pragma(mangle, "fun") void fun() {}
}

If the user instantiates the mixin multiple times, they will get 
duplicate symbol errors from the linker.  If they wanted to take 
control, they would have to opt into a different mangling scheme 
such as...

mixin template M()
{
     extern(C) pragma(mangle, D) void fun() {}
}

... which I, IIUC, would result in the current behavior.

If the compiler cannot deterministically compute the mangling 
scheme specified because there is no translation from the D code, 
the compiler would emit a meaningful error telling the user so, 
and they may be able to work around it with the explicit 
`pragma(mangle)` form.

IMO, that would be the most practical, covering the most common 
cases, and would also be the least surprising.

So, in general, I side with Manu here, but I expect a little more 
from the language.

Mike


More information about the Digitalmars-d mailing list