On Wednesday, 16 December 2020 at 04:17:13 UTC, Mike Parker wrote:
On Tuesday, 15 December 2020 at 22:04:12 UTC, Dave P. wrote:
[...]
Mangling does not play any role in passing and calling function
pointers between D and C. It only plays a role in linking and
loading. You can declare function pointers in D and C without
any name whatsoever.
What matters is that both sides agree on the number and type of
parameters accepted by the function that's pointed to, and that
they both agree on the calling convention. I don't believe
extern(C) has any effect on templated functions. However, the D
calling convention is defined to be identical to the C calling
convention on the host system for everything except Windows x86.
Oh interesting, so I only need extern(C) for declaring symbols
I’m linking to and
for symbols I want to export to C. I had sort of assumed that D
might have
different calling conventions for different things, but that
makes things a lot easier.
So theoretically, you should be able to pass a pointer to a
templated free function to C as long as the types in the
functions parameter list match those the C function expects. I
don't know if I'd do that myself, though.
[...]
There's no such thing as C name mangling. C functions are *not*
mangled (though some platforms do prepend an underscore to
symbol names). What extern(C) does is to turn off D name
mangling.
Yeah, I was playing a bit loose with the terminology and
referring to the
leading underscore.
So what you're asking for is a way to retain the D name
mangling on an extern C function. The way to do that is with
`pragma(mangle, "new_name")`. To match the original D function
mangling, declare the function first without extern(C) and
print `func.mangleof`. Use that as the parameter to
pragma(mangle).
I can't imagine any benefit you'd get from doing that, though.
Agreed!