ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.31k stars 2.51k forks source link

unable to translate C expr: unexpected token .LBrace #13337

Open foodornt opened 1 year ago

foodornt commented 1 year ago

Zig Version

0.9.1

Steps to Reproduce and Observed Behavior

Try to compile my project https://github.com/foodornt/zigbug

(have libpipewire v 3 installed)

Expected Behavior

To translate C code just fine, though it's stuck at this line:

#define SPA_POD_BUILDER_INIT(buffer,size)  ((struct spa_pod_builder) { (buffer), (size), 0, {}, {} })

spa/pod/builder.h:72:9

uben0 commented 1 year ago

I have a similar bug trying to use GTK 4 (zig 0.10.1)

unable to translate C expr: unexpected token ')'

At /usr/include/glib-2.0/gobject/gtype.h line 2504

This is the macro failing :

#  define _G_TYPE_CIC(ip, gt, ct) \
    ((ct*) (void *) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
Dimethylebutane commented 1 year ago

I also have issue with glib-2.0/gobject/gtype.h, the same macro as @uben0 (not the same line but still _G_TYPE_CIC) (using zig 0.12.0-dev.415+5af5d87ad)

The error is

unable to translate C expr: unexpected token ')' pub const _G_TYPE_CIC = @compileError("unable to translate C expr: unexpected token ')'"); // /usr/include/glib-2.0/gobject/gtype.h:2647:11`

With translate-c, I see that of these 9 macros, 6 are translated to @compileError("unable to translate C expr: unexpected token ')'");

#  define _G_TYPE_CIC(ip, gt, ct)       ((ct*) (void *) ip) //->unexpected token ')'
#  define _G_TYPE_CCC(cp, gt, ct)       ((ct*) (void *) cp) //->unexpected token ')'
...
#  define _G_TYPE_CIC(ip, gt, ct) \
    ((ct*) (void *) g_type_check_instance_cast ((GTypeInstance*) ip, gt)) //->unexpected token ')'
#  define _G_TYPE_CCC(cp, gt, ct) \
    ((ct*) (void *) g_type_check_class_cast ((GTypeClass*) cp, gt)) //->unexpected token ')'
...
#define _G_TYPE_CHI(ip)         (g_type_check_instance ((GTypeInstance*) ip)) //OK
#define _G_TYPE_CHV(vl)         (g_type_check_value ((GValue*) vl)) //OK
#define _G_TYPE_IGC(ip, gt, ct)         ((ct*) (((GTypeInstance*) ip)->g_class)) //->unexpected token ')'
#define _G_TYPE_IGI(ip, gt, ct)         ((ct*) g_type_interface_peek (((GTypeInstance*) )->g_class, gt)) //->unexpected token ')'
#define _G_TYPE_CIFT(ip, ft)            (g_type_check_instance_is_fundamentally_a ((GTypeInstance*) ip, ft)) //OK

Also I'm new to Zig so I may be doing something wrong.

EDIT: I manually translated the macro

pub inline fn _G_TYPE_CIC(ip: anytype, gt: anytype, comptime ct: type) *ct {
    const p = @as(*GTypeInstance, @ptrCast(ip));
    return @as(*ct, @ptrCast(g_type_check_instance_cast(p, gt)));
}

And changed it inside the zig_cache, but Idk if it will be stable, nor if my translation is perfect x)

iluvcapra commented 5 months ago

I have this too, just writing a test app to see if GTK works.

SKBotNL commented 3 months ago

I can confirm that the _G_TYPE_CIC issue is still the case with Zig 0.14.0-dev.130+cb308ba3a and GTK+3

axlEscalada commented 1 month ago

Same problem here using gtk4 with Zig 0.14.0-dev.850+ddcb7b1c1