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

Enums aren't cast from ints in macros with translate-c #5656

Closed
shtanton opened this issue Jun 21, 2020 · 1 comment · Fixed by #9164
Closed

Enums aren't cast from ints in macros with translate-c #5656

shtanton opened this issue Jun 21, 2020 · 1 comment · Fixed by #9164
Labels
translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@shtanton
Copy link
Contributor

shtanton commented Jun 21, 2020

macro.h:

#define noargs someargs(Flag1)

typedef enum {
    Flag0,
    Flag1
} Flags;

int someargs(Flags flags);

macro.c:

typedef enum {
    Flag0,
    Flag1
} Flags;

int someargs(Flags flags) {
    if (flags == Flag1) {
        return 1;
    } else {
        return 0;
    }
}

test.zig:

const c = @cImport({
    @cInclude("macro.h");
});

pub fn main() !void {
    _ = c.noargs;
}

Then compile with zig build-exe test.zig --c-source macro.c -lc -I. and the error

zig-cache/o/INfBWWWMN1znJ7cpk1aHFpWrWqAEwqd-Uefh0pPlK7Sau8WYxYrtuxOF09n_V9xa/cimport.zig:225:35: error: expected type '.cimport:2:11.enum_unnamed_1', found 'c_int'

is produced.

As far as I can tell this is only a problem on the right hand side of c macros, in other places @intToEnum is inserted to make it work. I don't know why expressions in macros are handled differently but it's probably causing other bugs too.

@Vexu
Copy link
Member

Vexu commented Jun 27, 2020

Macros are translated separately from the rest of the code and have no type info available which is why the cast is not being inserted. Fixing this would require finding the types of the function and the argument (possibly through multiple macros) and casting based on that.

@Vexu Vexu added the translate-c C to Zig source translation feature (@cImport) label Jun 27, 2020
@Vexu Vexu added this to the 0.8.0 milestone Jun 27, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.10.0 May 19, 2021
@Vexu Vexu linked a pull request Jun 22, 2021 that will close this issue
@andrewrk andrewrk modified the milestones: 0.10.0, 0.9.0 Jun 29, 2021
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