Thread overview
Matching an array-type of a C++ function signature in D, without using a D-array-type, because the compiler crashes otherwise
Mar 23, 2019
Simon
Mar 23, 2019
kinke
Mar 23, 2019
Simon
March 23, 2019
Hi,
I experienced some trouble with DMD today, while trying to declare an external C++ function in D, that gets linked from a C++ compiled object file.

The C++ function that I want to link against is declared as follows:
bool ColorEdit4(const char* label, float col[4], int flags = 0);

Yielding the following signature in the .obj (using a demangler):
BOOL __cdecl ImGui::ColorEdit4(char const * __ptr64,float * __ptr64 const,int)

The Function is declared in D as follows:
extern(C++, ImGui) bool ColorEdit4(const(char)* label, float[4] col, int flags = 0);

Which unfortunately crashes the compiler, which tells me I can't use a D array-type here, and should use a pointer. If you want to know the full story, you can look into the bug-report: https://issues.dlang.org/show_bug.cgi?id=19759

So as a workaround until this is fixed, I somehow need to end up with the same mangled function signature, while using a pointer type for the second argument.

I tried:
extern(C++, ImGui) bool ColorEdit4(const(char)* label, const float* col, int flags = 0);

which yields:
BOOL __cdecl ImGui::ColorEdit4(char const * __ptr64,float * __ptr64,int)

so the 2nd argument is of type "float * __ptr64", when it should be "float * __ptr64 const".

Trying "const float* col" or "const(float)* col" doesn't yield the correct result either ("float const * __ptr64 const" and "float const * __ptr64"), and I didn't find any other combinations I could try.

Is there any way to end up with the correct mangled function signature, using only pointer types? Another workaround would of course be to switch to C-Linkage, so the names don't get mangled at all, but the C++-object file has function overloads, so I would like to avoid that. If this isn't possible, I might just wait for the dmd update.
March 23, 2019
On Saturday, 23 March 2019 at 11:35:45 UTC, Simon wrote:
> Is there any way to end up with the correct mangled function signature, using only pointer types?

The problem is that the C++ compiler uses head-const for the array param (`float * const`), which cannot be represented in D.
What you can do is specify the mangle manually, e.g.:

pragma(mangle, "?ColorEdit4@ImGui@@YA_NPEBDQEAMH@Z")
extern(C++) bool ColorEdit4(const(char)* label, float* col, int flags = 0);
March 23, 2019
On Saturday, 23 March 2019 at 13:04:10 UTC, kinke wrote:
> On Saturday, 23 March 2019 at 11:35:45 UTC, Simon wrote:
>> Is there any way to end up with the correct mangled function signature, using only pointer types?
>
> The problem is that the C++ compiler uses head-const for the array param (`float * const`), which cannot be represented in D.
> What you can do is specify the mangle manually, e.g.:
>
> pragma(mangle, "?ColorEdit4@ImGui@@YA_NPEBDQEAMH@Z")
> extern(C++) bool ColorEdit4(const(char)* label, float* col, int flags = 0);

I didn't know specifying the mangle was possible, thanks!