jacob-carlborg / dstep

A tool for converting C and Objective-C headers to D modules
205 stars 37 forks source link

const attribute is removed from parameter of a function pointer #32

Open joakim-noah opened 10 years ago

joakim-noah commented 10 years ago

I ran into this when translating the following:

typedef void (*AStorageManager_obbCallbackFunc)(const char* filename, const int32_t state, void* data);

DStep turned it into this, removing the const for the second parameter:

alias void function (const(char)*, int, void*) AStorageManager_obbCallbackFunc;

I haven't checked to see if dmd will actually complain about this, but it strikes me as a bug.

jacob-carlborg commented 10 years ago

DStep turned it into this, removing the const for the second parameter

This looks a bit odd. But according to libclang, which DStep uses under the hood, state is not const.

I haven't checked to see if dmd will actually complain about this

Why would DMD complain about this?

jacob-carlborg commented 10 years ago

Hmm, if I remove the typedef, i.e. during it into a variable declaration, libclang will see it as const.

jacob-carlborg commented 10 years ago

I've asked about this on the Clang mailing list.

joakim-noah commented 10 years ago

Why would DMD complain about this?

Because the callback signature doesn't match the C library? I've had that happen in other situations, not sure if it would here. If you mean that it's the linker that normally catches stuff like this, sure.

Hmm, if I remove the typedef, i.e. during it into a variable declaration, libclang will see it as const.

Yeah, if you turn it into an actual function declaration, it sees the parameter as const, just not when it's a function pointer.

jacob-carlborg commented 10 years ago

Because the callback signature doesn't match the C library? I've had that happen in other situations, not sure if it would here. If you mean that it's the linker that normally catches stuff like this, sure.

Right, when you use the function pointer. Yes, then DMD will complain.

Yeah, if you turn it into an actual function declaration, it sees the parameter as const, just not when it's a function pointer.

It's not the function pointer that is the problem, it's the typedef.

jacob-carlborg commented 10 years ago

I got a reply [1] from the mailing list and this is apparently how C behaves. I'm not sure that I can do anything about it.

[1] http://clang-developers.42468.n3.nabble.com/const-stripped-in-typedefs-for-parameters-of-primitive-type-td4041337.html

jacob-carlborg commented 10 years ago

Perhaps D should allow the callbacks anyway since const doesn't really matter for value types.

joakim-noah commented 10 years ago

I have no real inclination on what to do here, I wasn't even sure it was a bug. I just reported it as I was double-checking the translation and it seemed like it might be.

jacob-carlborg commented 10 years ago

It behaves as it should according to C. But that will cause problems on the D side. I really, really don't want to parse C code without the help of Clang.