dmd icon indicating copy to clipboard operation
dmd copied to clipboard

ImportC: typedef function pointer calling convention changes if C file compiled.

Open drpriver opened this issue 7 months ago • 1 comments

This was reduced from code that uses <windows.h>

// z.c
typedef void (__stdcall* proc)(void);
struct Foo {
    proc p;
};
// y.d
import z;
extern(C) void c();
extern(Windows) void w();
void main(){
    Foo f;
    pragma(msg, typeof(f.p).stringof);
    f.p = &c;
    f.p = &w;
}

A command line like dmd y.d prints extern (C) void function() and trying to assign &w fails.

A command line like dmd y.d z.c prints extern (Windows) void function() and trying to assign &c fails.

drpriver avatar Apr 19 '25 06:04 drpriver

I think I found the root cause, we're getting some unanticipated aliasing of types that the semantic routines aren't setup to handle due to the way C typedefs are getting resolved. Because of this, things can get run in a different order or just skipped.

I think I've got a fix cooking.

drpriver avatar Apr 20 '25 22:04 drpriver