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

Missing Declaration in Struct causes translate-c to Generate Invalid Zig #4738

Closed
Jared-Miller opened this issue Mar 14, 2020 · 1 comment · Fixed by #4973
Closed

Missing Declaration in Struct causes translate-c to Generate Invalid Zig #4738

Jared-Miller opened this issue Mar 14, 2020 · 1 comment · Fixed by #4973
Labels
translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@Jared-Miller
Copy link
Contributor

Some headers (Windows) use structs with missing declarations. When importing the Windows headers, you can #define NONAMELESSUNION to replace all the nameless unions/structs with dummy values. Discussion in #985, #1214, and #4081 might be relevant to how to implement this.

There are four different cases I tried in Zig:

  • No names defined (valid Zig code, but no elements)
  • One name defined (valid Zig code with the single named element)
  • Unnamed struct as a member (invalid Zig code)
  • Nested struct declaration without a name generates (valid Zig code, generated name)

Based on the fourth case, I believe that the previous three should all be generating unnamed elements instead of excluding them to be consistent. One of the referenced issues mentions that Zig can re-order the struct contents, so it may not be that simple to maintain compatibility.

No names defined generates valid Zig code with no elements:

typedef struct NONAMES
{
    int;
    long;
} NONAMES;
pub const struct_NONAMES = extern struct {};
pub const NONAMES = struct_NONAMES;

One name generates valid Zig code with the single named element:

typedef struct ONENAME
{
    int;
    long b;
} ONENAME;
pub const struct_ONENAME = extern struct {
    b: c_long,
};
pub const ONENAME = struct_ONENAME;

With an unnamed struct, invalid Zig code is generated.

typedef struct NAMED
{
    long name;
} NAMED;

typedef struct ONENAMEWITHSTRUCT
{
    NAMED;
    long b;
} ONENAMEWITHSTRUCT;
pub const struct_NAMED = extern struct {
    name: c_long,
};
pub const NAMED = struct_NAMED;
pub const struct_ONENAMEWITHSTRUCT = extern struct {
    : struct_NAMED,
    b: c_long,
};

A nested struct declaration without a name generates valid Zig code:

typedef struct ONENAMEWITHSTRUCT
{
    struct {
        long name;
    };
    long b;
} ONENAMEWITHSTRUCT;
const struct_unnamed_2 = extern struct {
    name: c_long,
};
pub const struct_ONENAMEWITHSTRUCT = extern struct {
    unnamed_1: struct_unnamed_2,
    b: c_long,
};
@Jared-Miller
Copy link
Contributor Author

Also related to #3453

@andrewrk andrewrk added this to the 0.7.0 milestone Mar 14, 2020
@andrewrk andrewrk added the translate-c C to Zig source translation feature (@cImport) label Mar 14, 2020
@Vexu Vexu modified the milestones: 0.7.0, 0.6.0 Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants