Bug 16650 – Wrong mangling for extern(C++) with posix stat_t
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2016-10-30T05:11:00Z
Last change time
2017-07-10T21:15:41Z
Keywords
C++
Assigned to
nobody
Creator
Hexagonalstar64
Comments
Comment #0 by Hexagonalstar64 — 2016-10-30T05:11:47Z
---
import core.sys.posix.sys.stat;
extern(C++):
void myFunc(stat_t*){}
---
Doesn't match the c++ mangling
---
#include <sys/stat.h>
void myFunc(struct stat*){}
---
D mangles myFunc as "_Z6myFuncP6stat_t"
C++ mangles myFunc as "_Z6myFuncP4stat"
Comment #1 by doob — 2016-10-30T10:07:46Z
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.
Comment #2 by dlang-bugzilla — 2017-07-08T01:32:37Z
(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.
Comment #3 by doob — 2017-07-08T20:26:33Z
Is it possible to use pragman(mangle) on a struct?
Comment #4 by dlang-bugzilla — 2017-07-09T04:31:38Z
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.
Comment #5 by doob — 2017-07-09T09:07:31Z
(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?
Comment #6 by dlang-bugzilla — 2017-07-10T21:15:41Z
(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.