#Bitshift operator expected int, found bool during including C header

1 messages · Page 1 of 1 (latest)

craggy tangle
#

Hello, I'm trying to @cInclude('fenster.h') but when I compile, the zig compiler throws an error on the translated source code for:

f->mod = (!!(m & ControlMask)) | !!(m & ShiftMask) << 1) | (!!(m & Mod1Mask) << 2) | (!!(m & Mod4Mask) << 3);

It doesn't seem to like the C trick of double negation to get numbers. As it gives "Bitshifting operator expected int, but found bool" for that line. In the translated zig, I can see a few casts, do my first thought was export the header to zig and add another cast, but it does #define os-specific headers that I don't really want to translate to cImports by hand

shrewd aurora
#

Does it error without using the macro, or only when you use the macro?

craggy tangle
shrewd aurora
#

I mean are you trying to call the function that has an error, or are you trying to call another function and it's erroring anyway

craggy tangle
shrewd aurora
craggy tangle
#

It's a pretty important function to the library though... Darn, alright

shrewd aurora
#

oh if you need it then don't do that

#

usually single-header libraries will require you to define a macro to include the implementation so you can build it separately than including it. but it doesn't look like fenster does that

craggy tangle
#

I did see some old zig builds that had that define at the top, but when I added it, the linker complained about un-found functions

#

here's the exact error

#
import.zig:2783:100: error: bit shifting operation expected integer type, found 'bool'
                        f.*.mod = ((@intFromBool(!!((m & (@as(c_int, 1) << @intCast(2))) != 0)) | (!!((m & (@as(c_int, 1) << @intCast(0))) != 0) << @intCast(1))) | (!!((m & (@as(c_int, 1) << @intCast(3))) != 0) << @intCast(2))) | (!!((m & (@as(c_int, 1) << @intCast(6))) != 0) << @intCast(3));
#
      f->mod = (!!(m & ControlMask)) | (!!(m & ShiftMask) << 1) |
               (!!(m & Mod1Mask) << 2) | (!!(m & Mod4Mask) << 3);

is the line in the headerfile that is the problematic one

shrewd aurora
#

I think in zig it might be best to build fenster.h with addCSourceFile() in build.zig, and then manually create bindings for it in zig

#

either that or edit fenster.h to put every function body behind an ifdef guard

craggy tangle
#

and I'm assuming that this is because the bishift outputs a number, the exclamation marks convert that to a bool, then it tries to do an integer compare and fails on types?

#

hmm, that's not right

#

It's translating

(!!(m & Mod1Mask) << 2)

to

(!!((m & (@as(c_int, 1) << @intCast(3))) != 0) << @intCast(2)))
#

I guess it doesn't really matter though, it's not working

shrewd aurora
#

what it's doing is true << 1 and it needs to see that it's going to be shifted and do like @as(c_int, @intFromBool(true)) << @intCast(1) or something

craggy tangle
#

alright... Thanks for the direction!

#

all this to construct an enum of bits

shrewd aurora
#

here's a smaller reproduction if you want to open an issue for zig

int shift(int a) {
    return !a << 1;
}