Re: extern(C) and name mangling

2020-12-16 Thread Jacob Carlborg via Digitalmars-d-learn
On 2020-12-16 16:18, Dave P. wrote: Is this a bug in the spec or in the implementation? Yeah, that's a good question. That's always problematic with D. The spec is incomplete. How do we get this fixed? The simplest would be to change the spec. -- /Jacob Carlborg

Re: extern(C) and name mangling

2020-12-16 Thread Dave P. via Digitalmars-d-learn
On Wednesday, 16 December 2020 at 06:46:42 UTC, Jacob Carlborg wrote: On Wednesday, 16 December 2020 at 04:17:13 UTC, Mike Parker wrote: However, the D calling convention is defined to be identical to the C calling convention on the host system for everything except Windows x86. That's

Re: extern(C) and name mangling

2020-12-15 Thread Jacob Carlborg via Digitalmars-d-learn
On Wednesday, 16 December 2020 at 04:17:13 UTC, Mike Parker wrote: 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 fir

Re: extern(C) and name mangling

2020-12-15 Thread Jacob Carlborg via Digitalmars-d-learn
On Wednesday, 16 December 2020 at 04:17:13 UTC, Mike Parker wrote: However, the D calling convention is defined to be identical to the C calling convention on the host system for everything except Windows x86. Also keep in mind that D supports other types than C does, like D arrays and

Re: extern(C) and name mangling

2020-12-15 Thread Jacob Carlborg via Digitalmars-d-learn
On Wednesday, 16 December 2020 at 04:17:13 UTC, Mike Parker wrote: However, the D calling convention is defined to be identical to the C calling convention on the host system for everything except Windows x86. That's what's specified, but that's not how DMD actually behaves. DMD passes the

Re: extern(C) and name mangling

2020-12-15 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 16 December 2020 at 04:45:34 UTC, Dave P. wrote: 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

Re: extern(C) and name mangling

2020-12-15 Thread Dave P. via Digitalmars-d-learn
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.mang

Re: extern(C) and name mangling

2020-12-15 Thread Ali Çehreli via Digitalmars-d-learn
On 12/15/20 2:04 PM, Dave P. wrote: > I want to pass > some templated functions as function pointers to some C code As Mike Parker said, it works: // The same thing as a C function pointer: alias Func = long function(int); long bar(T)(int) { return 0; } Func f0 = &(bar!float); Func d1 =

Re: extern(C) and name mangling

2020-12-15 Thread Mike Parker via Digitalmars-d-learn
On Tuesday, 15 December 2020 at 22:04:12 UTC, Dave P. wrote: I can’t find this in the spec, but from experimentation it seems like extern(C) only affects name mangling of functions at the top level scope. Thus extern(C) function templates would be mangled differently, but still use the C

extern(C) and name mangling

2020-12-15 Thread Dave P. via Digitalmars-d-learn
I can’t find this in the spec, but from experimentation it seems like extern(C) only affects name mangling of functions at the top level scope. Thus extern(C) function templates would be mangled differently, but still use the C calling convention. Is this right? I want to pass some templated