-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
importc
fails to generate stub type
#24604
Comments
but why |
because the type is being imported from C (and defined in a C library) |
Yet you want it to occur in the produced C file so as far as the compiler is concerned you export it... |
well, that doesn't seem logical - that would place the responsibility of defining the type on nim, which is not the intent. The type is defined in a C library - what nim is supposed to to is create an opaque struct to be used for pointers but whose definition remains unknown to the compilation unit - nothing else. In fact, this is also what happens, typedef struct MyType MyType;
struct MyType {
char dummy;
}; |
Hmm ok, I get what you're saying. |
It seems that lots of codebases relies on |
hm, that's an unfortunate mess - the manual seems pretty clear on the intent of nodecl / header here, in favor of the interpretation of this issue, so technically such code is buggy (as buggy as the compiler :) ).
|
Well the issue is subtle, "this type comes from C but also generate it in the C code" ... it's no wonder why we never encountered it before. |
I think it's mostly because most of the time, people tend to use In this particular case, there's actually no |
I think the solution here is: type MyType {.incompleteStruct.} = object
var v {.exportc.}: ptr MyType
It is incomplete so the codegen should produce the "forward only" struct tag thing and it needs to emit the type as it is not imported. Of course this doesn't work with today's compiler, but this can be done without breaking anybody's code (fingers crossed). |
This will still apply name mangling to the type - in theory it shouldn't matter since the name ultimately is arbitrary inside the compilation unit unless compilers do some fancy type-based aliasing analysis on this name in LTO mode, ie across CUs. I have no idea if this is a real issue or not, but it's something to consider at least - it would also maybe matter if you have something like: type MyType {.incompleteStruct.} = object
proc f(v: ptr MyType) {.importc, header: "myheader.h".} here, |
To influence the name mangling type MyType {.incompleteStruct, extern: "MyType".} = object
var v {.exportc.}: ptr MyType |
Description
My expectation, since there's no
nodecl
, would be a stub type generated (typedef struct MyType MyType;
or similar.Nim Version
devel
, 2.0Current Output
Expected Output
No response
Known Workarounds
No response
Additional Information
No response
The text was updated successfully, but these errors were encountered: