Skip to content
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

Unnamed struct members are skipped #233

Closed
athre0z opened this issue Aug 20, 2019 · 8 comments · Fixed by #278
Closed

Unnamed struct members are skipped #233

athre0z opened this issue Aug 20, 2019 · 8 comments · Fixed by #278
Labels
C11 record struct/union/enum issues

Comments

@athre0z
Copy link

athre0z commented Aug 20, 2019

Unnamed nested structures/unions are currently skipped.

Given

struct Meow {
    int blah;
    union {
        float either1;
        int either2;
    };
    float moo;
};

the lib generates the following Julia code:

struct Meow
    blah::Cint
    moo::Cfloat
end

The problem goes away when giving the member a name. Maybe generating a member called something along the lines of "anon_0" for these unnamed nested items would be best?

@Gnimuc
Copy link
Member

Gnimuc commented Aug 21, 2019

It's not easy to support this C11 feature using bare Julia struct since Julia's struct doesn't have this kinda feature. However, as Julia allows us to overload Base.getproperty, we could emulate this behavior. You could take this snippet as an example on how to emulate a C union in Julia.

In this way, non-canonical C structs/unions should be auto-generated as a struct with a single data byte field (e.g. struct Foo; data::NTuple{N,UInt8}); end) where the size N can be computed from Clang AST. The corresponding Base.getproperty function can also be generated basically in the same pattern. I guess __attribute__ identifiers(#224) can also be supported in a similar way.

To support this, wrap_c.jl needs to be refactored, but I currently don't have time to do this, so if your code only have a few C11 structures, the "best practice" for now is to overload Base.getproperty manually.

@Gnimuc Gnimuc added the record struct/union/enum issues label Aug 21, 2019
@athre0z
Copy link
Author

athre0z commented Aug 21, 2019

Thank you for the quick & detailed response! Overloading getproperty is what I currently do. Auto-generating such methods in the long term sounds great in general -- this way, regular unions would become a lot nicer as well!

Since just skipping the member results in misaligned fields afterwards, for now, maybe adding a warning when encountering such unnamed fields would be a good thing -- or just generating a name for the field? In my case it was rather obvious, but other people might be less fortunate, not realizing why their struct fields are misaligned after a skipped field.

@Gnimuc
Copy link
Member

Gnimuc commented Mar 3, 2021

Hi @athre0z, could you share a link to your wrapper repo? I'd like to give #278 a test. Any suggestions on how to overload Base.getproperty would be highly appreciated.

@athre0z
Copy link
Author

athre0z commented Mar 4, 2021

Hi @Gnimuc! Unfortunately I don't remember where exactly I used that and I'm not even sure if this ever ended up in a commit, sorry! I think what I did was simply performing an if on the symbol, then have an unsafe_load for each field with a hard-coded struct offset.

@cdsousa
Copy link

cdsousa commented Aug 23, 2022

Hi, I've just tried master branch with the code presented by the OP and it fails with ERROR: There is no definition for Meow's field: 's type: [`union Meow::(anonymous at ......
Is that expected?

@Gnimuc
Copy link
Member

Gnimuc commented Aug 23, 2022 via email

@cdsousa
Copy link

cdsousa commented Aug 24, 2022

I've tried it with Clang 0.16.0 and the current master branch, within Julia 1.8.

@Gnimuc
Copy link
Member

Gnimuc commented Aug 24, 2022

Could you share some info about the C header?

I just noticed you used the example in the OP.

Gnimuc added a commit that referenced this issue Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C11 record struct/union/enum issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants