Gnimuc / CSyntax.jl

C-like syntax for Julia
MIT License
7 stars 3 forks source link

integrate `CUnion.jl` #1

Closed inkydragon closed 3 years ago

inkydragon commented 4 years ago
Gnimuc commented 4 years ago

FYI, CBinding.jl provides a similar macro that supports custom alignment.

I'm not a big fan of these two implementations because both CBinding.jl and CUnion.jl implement this feature with either cumbersome complicated macros or based on AST utility tools like MacroTools which will add tons of indirect deps to any c-wrapper-projects that need such a small feature. I'm wondering why it is so hard to interop with C unions in Julia.

Here is the old school method(IIRC, it hasn't been changed since Julia-v0.4) that Julia doc tells:

Packed structs and union declarations are not supported by Julia.

You can get an approximation of a union if you know, a priori, the field that will have the greatest size (potentially including padding). When translating your fields to Julia, declare the Julia field to be only of that type.

The only info we need to know is the greatest field size with correct padding. With this info, we can declare the field to be only of an NTuple type of that size and imitate the behavior of a C union by overloading Base.getproperty.

However, It looks like both CBinding.jl and CUnion.jl try to tackle the problem of finding the greatest field size or field padding from the Julia side. Wouldn't it be easier to directly query that info from a true C/C++ compiler like Clang? How about extracting the size, padding and offset info using Clang.jl and auto-generating "blob"-structs and corresponding methods for Base.getproperty?

inkydragon commented 4 years ago
Gnimuc commented 3 years ago

I'm closing this as Clang.jl has implemented the workaround proposed in the comments above and CUnion.jl seems to be dead now.