| ... | ... | @@ -3,6 +3,8 @@ const std = @import("std"); |
| 3 | 3 | const CrossTarget = std.zig.CrossTarget; |
| 4 | 4 | |
| 5 | 5 | pub fn addCases(cases: *tests.TranslateCContext) void { |
| 6 | const default_enum_type = if (std.Target.current.abi == .msvc) "c_int" else "c_uint"; |
| 7 | |
| 6 | 8 | cases.add("field access is grouped if necessary", |
| 7 | 9 | \\unsigned long foo(unsigned long x) { |
| 8 | 10 | \\ return ((union{unsigned long _x}){x})._x; |
| ... | ... | @@ -28,17 +30,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 28 | 30 | \\ int a, b; |
| 29 | 31 | \\} Bar; |
| 30 | 32 | , &[_][]const u8{ |
| 31 | | \\pub const Foo = extern enum(c_int) { |
| 32 | | \\ A, |
| 33 | | \\ B, |
| 34 | | \\ _, |
| 35 | | \\}; |
| 36 | | \\pub const FooA = @enumToInt(Foo.A); |
| 37 | | \\pub const FooB = @enumToInt(Foo.B); |
| 38 | | \\pub const Bar = extern struct { |
| 39 | | \\ a: c_int, |
| 40 | | \\ b: c_int, |
| 41 | | \\}; |
| 33 | \\pub const Foo = extern enum( |
| 34 | ++ default_enum_type ++ |
| 35 | \\) { |
| 36 | \\ A, |
| 37 | \\ B, |
| 38 | \\ _, |
| 39 | \\}; |
| 40 | \\pub const FooA = @enumToInt(Foo.A); |
| 41 | \\pub const FooB = @enumToInt(Foo.B); |
| 42 | \\pub const Bar = extern struct { |
| 43 | \\ a: c_int, |
| 44 | \\ b: c_int, |
| 45 | \\}; |
| 42 | 46 | }); |
| 43 | 47 | |
| 44 | 48 | cases.add("if as while stmt has semicolon", |
| ... | ... | @@ -118,29 +122,33 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 118 | 122 | \\} |
| 119 | 123 | , &[_][]const u8{ |
| 120 | 124 | \\pub export fn foo() void { |
| 121 | | \\ const enum_Foo = extern enum(c_int) { |
| 122 | | \\ A, |
| 123 | | \\ B, |
| 124 | | \\ C, |
| 125 | | \\ _, |
| 126 | | \\ }; |
| 127 | | \\ const A = @enumToInt(enum_Foo.A); |
| 128 | | \\ const B = @enumToInt(enum_Foo.B); |
| 129 | | \\ const C = @enumToInt(enum_Foo.C); |
| 130 | | \\ var a: enum_Foo = @import("std").meta.cast(enum_Foo, B); |
| 131 | | \\ { |
| 132 | | \\ const enum_Foo = extern enum(c_int) { |
| 133 | | \\ A, |
| 134 | | \\ B, |
| 135 | | \\ C, |
| 136 | | \\ _, |
| 137 | | \\ }; |
| 138 | | \\ const A_2 = @enumToInt(enum_Foo.A); |
| 139 | | \\ const B_3 = @enumToInt(enum_Foo.B); |
| 140 | | \\ const C_4 = @enumToInt(enum_Foo.C); |
| 141 | | \\ var a_5: enum_Foo = @import("std").meta.cast(enum_Foo, B_3); |
| 142 | | \\ } |
| 143 | | \\} |
| 125 | \\ const enum_Foo = extern enum( |
| 126 | ++ default_enum_type ++ |
| 127 | \\) { |
| 128 | \\ A, |
| 129 | \\ B, |
| 130 | \\ C, |
| 131 | \\ _, |
| 132 | \\ }; |
| 133 | \\ const A = @enumToInt(enum_Foo.A); |
| 134 | \\ const B = @enumToInt(enum_Foo.B); |
| 135 | \\ const C = @enumToInt(enum_Foo.C); |
| 136 | \\ var a: enum_Foo = @import("std").meta.cast(enum_Foo, B); |
| 137 | \\ { |
| 138 | \\ const enum_Foo = extern enum( |
| 139 | ++ default_enum_type ++ |
| 140 | \\) { |
| 141 | \\ A, |
| 142 | \\ B, |
| 143 | \\ C, |
| 144 | \\ _, |
| 145 | \\ }; |
| 146 | \\ const A_2 = @enumToInt(enum_Foo.A); |
| 147 | \\ const B_3 = @enumToInt(enum_Foo.B); |
| 148 | \\ const C_4 = @enumToInt(enum_Foo.C); |
| 149 | \\ var a_5: enum_Foo = @import("std").meta.cast(enum_Foo, B_3); |
| 150 | \\ } |
| 151 | \\} |
| 144 | 152 | }); |
| 145 | 153 | |
| 146 | 154 | cases.add("scoped record", |
| ... | ... | @@ -1702,47 +1710,55 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1702 | 1710 | \\ p, |
| 1703 | 1711 | \\}; |
| 1704 | 1712 | , &[_][]const u8{ |
| 1705 | | \\pub const d = extern enum(c_int) { |
| 1706 | | \\ a, |
| 1707 | | \\ b, |
| 1708 | | \\ c, |
| 1709 | | \\ _, |
| 1710 | | \\}; |
| 1711 | | \\pub const a = @enumToInt(d.a); |
| 1712 | | \\pub const b = @enumToInt(d.b); |
| 1713 | | \\pub const c = @enumToInt(d.c); |
| 1714 | | \\const enum_unnamed_1 = extern enum(c_int) { |
| 1715 | | \\ e = 0, |
| 1716 | | \\ f = 4, |
| 1717 | | \\ g = 5, |
| 1718 | | \\ _, |
| 1719 | | \\}; |
| 1720 | | \\pub const e = @enumToInt(enum_unnamed_1.e); |
| 1721 | | \\pub const f = @enumToInt(enum_unnamed_1.f); |
| 1722 | | \\pub const g = @enumToInt(enum_unnamed_1.g); |
| 1723 | | \\pub export var h: enum_unnamed_1 = @import("std").meta.cast(enum_unnamed_1, e); |
| 1724 | | \\const enum_unnamed_2 = extern enum(c_int) { |
| 1725 | | \\ i, |
| 1726 | | \\ j, |
| 1727 | | \\ k, |
| 1728 | | \\ _, |
| 1729 | | \\}; |
| 1730 | | \\pub const i = @enumToInt(enum_unnamed_2.i); |
| 1731 | | \\pub const j = @enumToInt(enum_unnamed_2.j); |
| 1732 | | \\pub const k = @enumToInt(enum_unnamed_2.k); |
| 1733 | | \\pub const struct_Baz = extern struct { |
| 1734 | | \\ l: enum_unnamed_2, |
| 1735 | | \\ m: d, |
| 1736 | | \\}; |
| 1737 | | \\pub const enum_i = extern enum(c_int) { |
| 1738 | | \\ n, |
| 1739 | | \\ o, |
| 1740 | | \\ p, |
| 1741 | | \\ _, |
| 1742 | | \\}; |
| 1743 | | \\pub const n = @enumToInt(enum_i.n); |
| 1744 | | \\pub const o = @enumToInt(enum_i.o); |
| 1745 | | \\pub const p = @enumToInt(enum_i.p); |
| 1713 | \\pub const d = extern enum( |
| 1714 | ++ default_enum_type ++ |
| 1715 | \\) { |
| 1716 | \\ a, |
| 1717 | \\ b, |
| 1718 | \\ c, |
| 1719 | \\ _, |
| 1720 | \\}; |
| 1721 | \\pub const a = @enumToInt(d.a); |
| 1722 | \\pub const b = @enumToInt(d.b); |
| 1723 | \\pub const c = @enumToInt(d.c); |
| 1724 | \\const enum_unnamed_1 = extern enum( |
| 1725 | ++ default_enum_type ++ |
| 1726 | \\) { |
| 1727 | \\ e = 0, |
| 1728 | \\ f = 4, |
| 1729 | \\ g = 5, |
| 1730 | \\ _, |
| 1731 | \\}; |
| 1732 | \\pub const e = @enumToInt(enum_unnamed_1.e); |
| 1733 | \\pub const f = @enumToInt(enum_unnamed_1.f); |
| 1734 | \\pub const g = @enumToInt(enum_unnamed_1.g); |
| 1735 | \\pub export var h: enum_unnamed_1 = @import("std").meta.cast(enum_unnamed_1, e); |
| 1736 | \\const enum_unnamed_2 = extern enum( |
| 1737 | ++ default_enum_type ++ |
| 1738 | \\) { |
| 1739 | \\ i, |
| 1740 | \\ j, |
| 1741 | \\ k, |
| 1742 | \\ _, |
| 1743 | \\}; |
| 1744 | \\pub const i = @enumToInt(enum_unnamed_2.i); |
| 1745 | \\pub const j = @enumToInt(enum_unnamed_2.j); |
| 1746 | \\pub const k = @enumToInt(enum_unnamed_2.k); |
| 1747 | \\pub const struct_Baz = extern struct { |
| 1748 | \\ l: enum_unnamed_2, |
| 1749 | \\ m: d, |
| 1750 | \\}; |
| 1751 | \\pub const enum_i = extern enum( |
| 1752 | ++ default_enum_type ++ |
| 1753 | \\) { |
| 1754 | \\ n, |
| 1755 | \\ o, |
| 1756 | \\ p, |
| 1757 | \\ _, |
| 1758 | \\}; |
| 1759 | \\pub const n = @enumToInt(enum_i.n); |
| 1760 | \\pub const o = @enumToInt(enum_i.o); |
| 1761 | \\pub const p = @enumToInt(enum_i.p); |
| 1746 | 1762 | , |
| 1747 | 1763 | \\pub const Baz = struct_Baz; |
| 1748 | 1764 | }); |
| ... | ... | @@ -2234,13 +2250,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2234 | 2250 | \\ Two, |
| 2235 | 2251 | \\}; |
| 2236 | 2252 | , &[_][]const u8{ |
| 2237 | | \\const enum_unnamed_1 = extern enum(c_int) { |
| 2238 | | \\ One, |
| 2239 | | \\ Two, |
| 2240 | | \\ _, |
| 2241 | | \\}; |
| 2242 | | \\pub const One = @enumToInt(enum_unnamed_1.One); |
| 2243 | | \\pub const Two = @enumToInt(enum_unnamed_1.Two); |
| 2253 | \\const enum_unnamed_1 = extern enum( |
| 2254 | ++ default_enum_type ++ |
| 2255 | \\) { |
| 2256 | \\ One, |
| 2257 | \\ Two, |
| 2258 | \\ _, |
| 2259 | \\}; |
| 2260 | \\pub const One = @enumToInt(enum_unnamed_1.One); |
| 2261 | \\pub const Two = @enumToInt(enum_unnamed_1.Two); |
| 2244 | 2262 | }); |
| 2245 | 2263 | |
| 2246 | 2264 | cases.add("c style cast", |
| ... | ... | @@ -2338,35 +2356,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2338 | 2356 | \\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p); |
| 2339 | 2357 | \\} |
| 2340 | 2358 | , &[_][]const u8{ |
| 2341 | | \\pub const enum_Foo = extern enum(c_int) { |
| 2342 | | \\ A, |
| 2343 | | \\ B, |
| 2344 | | \\ C, |
| 2345 | | \\ _, |
| 2346 | | \\}; |
| 2347 | | \\pub const FooA = @enumToInt(enum_Foo.A); |
| 2348 | | \\pub const FooB = @enumToInt(enum_Foo.B); |
| 2349 | | \\pub const FooC = @enumToInt(enum_Foo.C); |
| 2350 | | \\pub const SomeTypedef = c_int; |
| 2351 | | \\pub export fn and_or_non_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void) c_int { |
| 2352 | | \\ var a = arg_a; |
| 2353 | | \\ var b = arg_b; |
| 2354 | | \\ var c = arg_c; |
| 2355 | | \\ var d: enum_Foo = @import("std").meta.cast(enum_Foo, FooA); |
| 2356 | | \\ var e: c_int = @boolToInt((a != 0) and (b != 0)); |
| 2357 | | \\ var f: c_int = @boolToInt((b != 0) and (c != null)); |
| 2358 | | \\ var g: c_int = @boolToInt((a != 0) and (c != null)); |
| 2359 | | \\ var h: c_int = @boolToInt((a != 0) or (b != 0)); |
| 2360 | | \\ var i: c_int = @boolToInt((b != 0) or (c != null)); |
| 2361 | | \\ var j: c_int = @boolToInt((a != 0) or (c != null)); |
| 2362 | | \\ var k: c_int = @boolToInt((a != 0) or (@bitCast(c_int, @enumToInt(d)) != 0)); |
| 2363 | | \\ var l: c_int = @boolToInt((@bitCast(c_int, @enumToInt(d)) != 0) and (b != 0)); |
| 2364 | | \\ var m: c_int = @boolToInt((c != null) or (@bitCast(c_uint, @enumToInt(d)) != 0)); |
| 2365 | | \\ var td: SomeTypedef = 44; |
| 2366 | | \\ var o: c_int = @boolToInt((td != 0) or (b != 0)); |
| 2367 | | \\ var p: c_int = @boolToInt((c != null) and (td != 0)); |
| 2368 | | \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p; |
| 2369 | | \\} |
| 2359 | \\pub const enum_Foo = extern enum( |
| 2360 | ++ default_enum_type ++ |
| 2361 | \\) { |
| 2362 | \\ A, |
| 2363 | \\ B, |
| 2364 | \\ C, |
| 2365 | \\ _, |
| 2366 | \\}; |
| 2367 | \\pub const FooA = @enumToInt(enum_Foo.A); |
| 2368 | \\pub const FooB = @enumToInt(enum_Foo.B); |
| 2369 | \\pub const FooC = @enumToInt(enum_Foo.C); |
| 2370 | \\pub const SomeTypedef = c_int; |
| 2371 | \\pub export fn and_or_non_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void) c_int { |
| 2372 | \\ var a = arg_a; |
| 2373 | \\ var b = arg_b; |
| 2374 | \\ var c = arg_c; |
| 2375 | \\ var d: enum_Foo = @import("std").meta.cast(enum_Foo, FooA); |
| 2376 | \\ var e: c_int = @boolToInt((a != 0) and (b != 0)); |
| 2377 | \\ var f: c_int = @boolToInt((b != 0) and (c != null)); |
| 2378 | \\ var g: c_int = @boolToInt((a != 0) and (c != null)); |
| 2379 | \\ var h: c_int = @boolToInt((a != 0) or (b != 0)); |
| 2380 | \\ var i: c_int = @boolToInt((b != 0) or (c != null)); |
| 2381 | \\ var j: c_int = @boolToInt((a != 0) or (c != null)); |
| 2382 | \\ var k: c_int = @boolToInt((a != 0) or (@bitCast(c_int, @enumToInt(d)) != 0)); |
| 2383 | \\ var l: c_int = @boolToInt((@bitCast(c_int, @enumToInt(d)) != 0) and (b != 0)); |
| 2384 | \\ var m: c_int = @boolToInt((c != null) or (@bitCast(c_uint, @enumToInt(d)) != 0)); |
| 2385 | \\ var td: SomeTypedef = 44; |
| 2386 | \\ var o: c_int = @boolToInt((td != 0) or (b != 0)); |
| 2387 | \\ var p: c_int = @boolToInt((c != null) and (td != 0)); |
| 2388 | \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p; |
| 2389 | \\} |
| 2370 | 2390 | , |
| 2371 | 2391 | \\pub const Foo = enum_Foo; |
| 2372 | 2392 | }); |
| ... | ... | @@ -2387,14 +2407,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2387 | 2407 | \\ y: c_int, |
| 2388 | 2408 | \\}; |
| 2389 | 2409 | , |
| 2390 | | \\pub const enum_Bar = extern enum(c_int) { |
| 2391 | | \\ A, |
| 2392 | | \\ B, |
| 2393 | | \\ _, |
| 2394 | | \\}; |
| 2395 | | \\pub const BarA = @enumToInt(enum_Bar.A); |
| 2396 | | \\pub const BarB = @enumToInt(enum_Bar.B); |
| 2397 | | \\pub extern fn func(a: [*c]struct_Foo, b: [*c][*c]enum_Bar) void; |
| 2410 | \\pub const enum_Bar = extern enum( |
| 2411 | ++ default_enum_type ++ |
| 2412 | \\) { |
| 2413 | \\ A, |
| 2414 | \\ B, |
| 2415 | \\ _, |
| 2416 | \\}; |
| 2417 | \\pub const BarA = @enumToInt(enum_Bar.A); |
| 2418 | \\pub const BarB = @enumToInt(enum_Bar.B); |
| 2419 | \\pub extern fn func(a: [*c]struct_Foo, b: [*c][*c]enum_Bar) void; |
| 2398 | 2420 | , |
| 2399 | 2421 | \\pub const Foo = struct_Foo; |
| 2400 | 2422 | \\pub const Bar = enum_Bar; |
| ... | ... | @@ -2664,26 +2686,28 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2664 | 2686 | \\ return 4; |
| 2665 | 2687 | \\} |
| 2666 | 2688 | , &[_][]const u8{ |
| 2667 | | \\pub const enum_SomeEnum = extern enum(c_int) { |
| 2668 | | \\ A, |
| 2669 | | \\ B, |
| 2670 | | \\ C, |
| 2671 | | \\ _, |
| 2672 | | \\}; |
| 2673 | | \\pub const A = @enumToInt(enum_SomeEnum.A); |
| 2674 | | \\pub const B = @enumToInt(enum_SomeEnum.B); |
| 2675 | | \\pub const C = @enumToInt(enum_SomeEnum.C); |
| 2676 | | \\pub export fn if_none_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void, arg_d: enum_SomeEnum) c_int { |
| 2677 | | \\ var a = arg_a; |
| 2678 | | \\ var b = arg_b; |
| 2679 | | \\ var c = arg_c; |
| 2680 | | \\ var d = arg_d; |
| 2681 | | \\ if (a != 0) return 0; |
| 2682 | | \\ if (b != 0) return 1; |
| 2683 | | \\ if (c != null) return 2; |
| 2684 | | \\ if (@enumToInt(d) != 0) return 3; |
| 2685 | | \\ return 4; |
| 2686 | | \\} |
| 2689 | \\pub const enum_SomeEnum = extern enum( |
| 2690 | ++ default_enum_type ++ |
| 2691 | \\) { |
| 2692 | \\ A, |
| 2693 | \\ B, |
| 2694 | \\ C, |
| 2695 | \\ _, |
| 2696 | \\}; |
| 2697 | \\pub const A = @enumToInt(enum_SomeEnum.A); |
| 2698 | \\pub const B = @enumToInt(enum_SomeEnum.B); |
| 2699 | \\pub const C = @enumToInt(enum_SomeEnum.C); |
| 2700 | \\pub export fn if_none_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void, arg_d: enum_SomeEnum) c_int { |
| 2701 | \\ var a = arg_a; |
| 2702 | \\ var b = arg_b; |
| 2703 | \\ var c = arg_c; |
| 2704 | \\ var d = arg_d; |
| 2705 | \\ if (a != 0) return 0; |
| 2706 | \\ if (b != 0) return 1; |
| 2707 | \\ if (c != null) return 2; |
| 2708 | \\ if (@enumToInt(d) != 0) return 3; |
| 2709 | \\ return 4; |
| 2710 | \\} |
| 2687 | 2711 | }); |
| 2688 | 2712 | |
| 2689 | 2713 | cases.add("simple data types", |
| ... | ... | @@ -3130,15 +3154,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3130 | 3154 | \\ Foo1, |
| 3131 | 3155 | \\}; |
| 3132 | 3156 | , &[_][]const u8{ |
| 3133 | | \\pub const enum_Foo = extern enum(c_int) { |
| 3134 | | \\ A = 2, |
| 3135 | | \\ B = 5, |
| 3136 | | \\ @"1" = 6, |
| 3137 | | \\ _, |
| 3138 | | \\}; |
| 3139 | | \\pub const FooA = @enumToInt(enum_Foo.A); |
| 3140 | | \\pub const FooB = @enumToInt(enum_Foo.B); |
| 3141 | | \\pub const Foo1 = @enumToInt(enum_Foo.@"1"); |
| 3157 | \\pub const enum_Foo = extern enum( |
| 3158 | ++ default_enum_type ++ |
| 3159 | \\) { |
| 3160 | \\ A = 2, |
| 3161 | \\ B = 5, |
| 3162 | \\ @"1" = 6, |
| 3163 | \\ _, |
| 3164 | \\}; |
| 3165 | \\pub const FooA = @enumToInt(enum_Foo.A); |
| 3166 | \\pub const FooB = @enumToInt(enum_Foo.B); |
| 3167 | \\pub const Foo1 = @enumToInt(enum_Foo.@"1"); |
| 3142 | 3168 | , |
| 3143 | 3169 | \\pub const Foo = enum_Foo; |
| 3144 | 3170 | }); |