Open CrazyCasta opened 10 months ago
This is hard to fix. Maybe it's possible, but I don't know for sure. That's the reasoning behind why it's raising a NotImplementedError
. In two words: cffi generates C code when you call ffi.compile()
. This C code contains stuff like defining wrapper functions that can be called from Python, but also it defines code that put this kind of expression in some static array:
sizeof(struct a), offsetof(struct a, b), offsetof(struct a, d)
The problem is that it's harder to write these expressions if the type name is unknown, or if it just doesn't have any name at all. In particular, I'm unsure how to write offsetof()
in this case to get at the field offsets inside. It's probably possible on specific compilers like gcc, using the gcc extension typeof()
, but I'm looking for a solution that works for all C compilers.
Oh, according to https://stackoverflow.com/questions/10874211/finding-relative-offset-in-nested-structure then we should simply be able to write offsetof(struct a, d.subfield)
.
In summary, I think that it is possible to implement this. It's a matter of tweaking the build-time cffi.model.StructOrUnion
instance to store extra information: "this struct is anonymously defined as a field of that other struct". Then we'd use that when generating C code. It requires some careful handling of a lot of different cases and probably not all cases can be fixed. But for common cases it should work---unless I'm missing something else. Please tell me if you want to give it a go, or if you prefer to leave it to me (for some point in the future: I'm not doing much Python any more).
Note: you are correct that there is no way to use ...;
in an anonymous substructure. As far as I can tell, the only solution is to improve CFFI.
Let me take a look and see if I can do anything. Need to see if I can figure out the interface between the cffi.model.StructOrUnion
and C code generation.
Using cffi==1.16.0 the following can not be handled: