Thread overview
[Issue 16650] Wrong mangling for extern(C++) with posix stat_t
Oct 30, 2016
Jacob Carlborg
Jul 08, 2017
Vladimir Panteleev
Jul 08, 2017
Vladimir Panteleev
Jul 08, 2017
Jacob Carlborg
Jul 09, 2017
Vladimir Panteleev
Jul 09, 2017
Jacob Carlborg
Jul 10, 2017
Vladimir Panteleev
October 30, 2016
https://issues.dlang.org/show_bug.cgi?id=16650

Jacob Carlborg <doob@me.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |doob@me.com

--- Comment #1 from Jacob Carlborg <doob@me.com> ---
I don't think it's the mangling that is wrong. Rather it's the declaration of the struct. Not sure why it's called "stat_t" in the D version when it's called "stat" in the C headers.

--
July 08, 2017
https://issues.dlang.org/show_bug.cgi?id=16650

Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
(In reply to Jacob Carlborg from comment #1)
> Not sure why it's called "stat_t" in the D version when it's called "stat" in the C headers.

It's because in C, "stat" is both the name of a struct and a function. This is not a problem for C because structs in C have their own namespace (unless you typedef them), but there is no equivalent in D.

There are some workarounds available:

- You can use extern(C) to link the D declaration with the C definition. As
argument types are not mangled with C linkage, type name mismatches will not
result in a link error.
- You can use pragma(mangle) on the D side to directly override a function's
mangled name;
- Finally, on the D side you could declare the function as:

struct stat;
void myFunc(stat*){}

then cast the stat_t* to stat* when invoking the function.

As this is not a bug in D and simple workarounds exist, I'll close this, but feel free to reopen if you can present actionable ideas for improving the situation.

--
July 08, 2017
https://issues.dlang.org/show_bug.cgi?id=16650

Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |C++
           Hardware|x86_64                      |All

--
July 08, 2017
https://issues.dlang.org/show_bug.cgi?id=16650

--- Comment #3 from Jacob Carlborg <doob@me.com> ---
Is it possible to use pragman(mangle) on a struct?

--
July 09, 2017
https://issues.dlang.org/show_bug.cgi?id=16650

--- Comment #4 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
It's easy enough to test!

$ cat test.d
pragma(mangle, "CeciNestPasUnS")
struct S { }

extern(C++) void fun(S* s);

pragma(msg, fun.mangleof);

$ dmd -o- test.d
_Z3funP1S

Looks like pragma(mangle) has no effect on structs so far.

--
July 09, 2017
https://issues.dlang.org/show_bug.cgi?id=16650

--- Comment #5 from Jacob Carlborg <doob@me.com> ---
(In reply to Vladimir Panteleev from comment #4)
> It's easy enough to test!
> 
> $ cat test.d
> pragma(mangle, "CeciNestPasUnS")
> struct S { }
> 
> extern(C++) void fun(S* s);
> 
> pragma(msg, fun.mangleof);
> 
> $ dmd -o- test.d
> _Z3funP1S
> 
> Looks like pragma(mangle) has no effect on structs so far.

I didn't really expect it to work. Perhaps a worthwhile feature?

--
July 10, 2017
https://issues.dlang.org/show_bug.cgi?id=16650

--- Comment #6 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
(In reply to Jacob Carlborg from comment #5)
> I didn't really expect it to work. Perhaps a worthwhile feature?

Perhaps! Feel free to file an enhancement request.

--