authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-12-22 12:53:57+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-12-22 14:38:06+02:00
log9d31b65b34a222ff8b11d5bcaefcfc3c22ef4250
tree50769e52496541cec626911fb08a543bc3fbde81
parent40f607d195440fe62e819592deb6746cd2695753
signature Commit is signed but in an unrecognized format.

translate-c-2 various fixes

- make non-namespaced enums ints - fix .used compound assignments not being grouped - fix macro calls with casts producing invalid Zig

3 files changed, 187 insertions(+), 110 deletions(-)

src-self-hosted/c_tokenizer.zig+5
...@@ -30,6 +30,7 @@ pub const CToken = struct {...@@ -30,6 +30,7 @@ pub const CToken = struct {
30 Arrow,30 Arrow,
31 LBrace,31 LBrace,
32 RBrace,32 RBrace,
33 Pipe,
33 };34 };
3435
35 pub const NumLitSuffix = enum {36 pub const NumLitSuffix = enum {
...@@ -360,6 +361,10 @@ fn next(chars: [*:0]const u8, i: *usize) !CToken {...@@ -360,6 +361,10 @@ fn next(chars: [*:0]const u8, i: *usize) !CToken {
360 result.id = .RBrace;361 result.id = .RBrace;
361 state = .Done;362 state = .Done;
362 },363 },
364 '|' => {
365 result.id = .Pipe;
366 state = .Done;
367 },
363 else => return error.TokenizingFailed,368 else => return error.TokenizingFailed,
364 }369 }
365 },370 },
src-self-hosted/translate_c.zig+53-26
...@@ -781,7 +781,11 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No...@@ -781,7 +781,11 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
781 _ = try appendToken(c, .Comma, ",");781 _ = try appendToken(c, .Comma, ",");
782 // In C each enum value is in the global namespace. So we put them there too.782 // In C each enum value is in the global namespace. So we put them there too.
783 // At this point we can rely on the enum emitting successfully.783 // At this point we can rely on the enum emitting successfully.
784 try addEnumTopLevel(c, name, field_name, enum_val_name);784 const tld_node = try transCreateNodeVarDecl(c, true, true, enum_val_name);
785 tld_node.eq_token = try appendToken(c, .Equal, "=");
786 tld_node.init_node = try transCreateNodeAPInt(c, ZigClangEnumConstantDecl_getInitVal(enum_const));
787 tld_node.semicolon_token = try appendToken(c, .Semicolon, ";");
788 try addTopLevelDecl(c, field_name, &tld_node.base);
785 }789 }
786 container_node.rbrace_token = try appendToken(c, .RBrace, "}");790 container_node.rbrace_token = try appendToken(c, .RBrace, "}");
787791
...@@ -797,25 +801,6 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No...@@ -797,25 +801,6 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
797 return transCreateNodeIdentifier(c, name);801 return transCreateNodeIdentifier(c, name);
798}802}
799803
800fn addEnumTopLevel(c: *Context, enum_name: []const u8, field_name: []const u8, enum_val_name: []const u8) !void {
801 const node = try transCreateNodeVarDecl(c, true, true, enum_val_name);
802 node.eq_token = try appendToken(c, .Equal, "=");
803 const enum_ident = try transCreateNodeIdentifier(c, enum_name);
804 const period_tok = try appendToken(c, .Period, ".");
805 const field_ident = try transCreateNodeIdentifier(c, field_name);
806 node.semicolon_token = try appendToken(c, .Semicolon, ";");
807
808 const field_access_node = try c.a().create(ast.Node.InfixOp);
809 field_access_node.* = .{
810 .op_token = period_tok,
811 .lhs = enum_ident,
812 .op = .Period,
813 .rhs = field_ident,
814 };
815 node.init_node = &field_access_node.base;
816 try addTopLevelDecl(c, field_name, &node.base);
817}
818
819fn createAlias(c: *Context, alias: var) !void {804fn createAlias(c: *Context, alias: var) !void {
820 const node = try transCreateNodeVarDecl(c, true, true, alias.alias);805 const node = try transCreateNodeVarDecl(c, true, true, alias.alias);
821 node.eq_token = try appendToken(c, .Equal, "=");806 node.eq_token = try appendToken(c, .Equal, "=");
...@@ -1570,6 +1555,15 @@ fn transCCast(...@@ -1570,6 +1555,15 @@ fn transCCast(
1570 const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ZigClangQualType_getTypePtr(dst_type));1555 const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ZigClangQualType_getTypePtr(dst_type));
1571 return transCCast(rp, scope, loc, ZigClangElaboratedType_getNamedType(elaborated_ty), src_type, expr);1556 return transCCast(rp, scope, loc, ZigClangElaboratedType_getNamedType(elaborated_ty), src_type, expr);
1572 }1557 }
1558 if (ZigClangQualType_getTypeClass(dst_type) == .Enum)
1559 {
1560 const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@intToEnum");
1561 try builtin_node.params.push(try transQualType(rp, dst_type, loc));
1562 _ = try appendToken(rp.c, .Comma, ",");
1563 try builtin_node.params.push(expr);
1564 builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")");
1565 return &builtin_node.base;
1566 }
1573 if (ZigClangQualType_getTypeClass(src_type) == .Enum and1567 if (ZigClangQualType_getTypeClass(src_type) == .Enum and
1574 ZigClangQualType_getTypeClass(dst_type) != .Enum)1568 ZigClangQualType_getTypeClass(dst_type) != .Enum)
1575 {1569 {
...@@ -2326,7 +2320,13 @@ fn transCreatePreCrement(...@@ -2326,7 +2320,13 @@ fn transCreatePreCrement(
2326 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");2320 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
2327 // semicolon must immediately follow rbrace because it is the last token in a block2321 // semicolon must immediately follow rbrace because it is the last token in a block
2328 _ = try appendToken(rp.c, .Semicolon, ";");2322 _ = try appendToken(rp.c, .Semicolon, ";");
2329 return &block_scope.block_node.base;2323 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
2324 grouped_expr.* = .{
2325 .lparen = try appendToken(rp.c, .LParen, "("),
2326 .expr = &block_scope.block_node.base,
2327 .rparen = try appendToken(rp.c, .RParen, ")"),
2328 };
2329 return &grouped_expr.base;
2330}2330}
23312331
2332fn transCreatePostCrement(2332fn transCreatePostCrement(
...@@ -2392,7 +2392,13 @@ fn transCreatePostCrement(...@@ -2392,7 +2392,13 @@ fn transCreatePostCrement(
2392 try block_scope.block_node.statements.push(&break_node.base);2392 try block_scope.block_node.statements.push(&break_node.base);
2393 _ = try appendToken(rp.c, .Semicolon, ";");2393 _ = try appendToken(rp.c, .Semicolon, ";");
2394 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");2394 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
2395 return &block_scope.block_node.base;2395 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
2396 grouped_expr.* = .{
2397 .lparen = try appendToken(rp.c, .LParen, "("),
2398 .expr = &block_scope.block_node.base,
2399 .rparen = try appendToken(rp.c, .RParen, ")"),
2400 };
2401 return &grouped_expr.base;
2396}2402}
23972403
2398fn transCompoundAssignOperator(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCompoundAssignOperator, used: ResultUsed) TransError!*ast.Node {2404fn transCompoundAssignOperator(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCompoundAssignOperator, used: ResultUsed) TransError!*ast.Node {
...@@ -2508,7 +2514,13 @@ fn transCreateCompoundAssign(...@@ -2508,7 +2514,13 @@ fn transCreateCompoundAssign(
2508 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");2514 block_scope.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
2509 // semicolon must immediately follow rbrace because it is the last token in a block2515 // semicolon must immediately follow rbrace because it is the last token in a block
2510 _ = try appendToken(rp.c, .Semicolon, ";");2516 _ = try appendToken(rp.c, .Semicolon, ";");
2511 return &block_scope.block_node.base;2517 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
2518 grouped_expr.* = .{
2519 .lparen = try appendToken(rp.c, .LParen, "("),
2520 .expr = &block_scope.block_node.base,
2521 .rparen = try appendToken(rp.c, .RParen, ")"),
2522 };
2523 return &grouped_expr.base;
2512}2524}
25132525
2514fn transCPtrCast(2526fn transCPtrCast(
...@@ -4314,7 +4326,10 @@ fn parseCPrimaryExpr(rp: RestorePoint, it: *ctok.TokenList.Iterator, source_loc:...@@ -4314,7 +4326,10 @@ fn parseCPrimaryExpr(rp: RestorePoint, it: *ctok.TokenList.Iterator, source_loc:
43144326
4315 if (it.peek().?.id == .RParen) {4327 if (it.peek().?.id == .RParen) {
4316 _ = it.next();4328 _ = it.next();
4317 return inner_node;4329 if (it.peek().?.id != .LParen) {
4330 return inner_node;
4331 }
4332 _ = it.next();
4318 }4333 }
43194334
4320 // hack to get zig fmt to render a comma in builtin calls4335 // hack to get zig fmt to render a comma in builtin calls
...@@ -4458,7 +4473,7 @@ fn parseCSuffixOpExpr(rp: RestorePoint, it: *ctok.TokenList.Iterator, source_loc...@@ -4458,7 +4473,7 @@ fn parseCSuffixOpExpr(rp: RestorePoint, it: *ctok.TokenList.Iterator, source_loc
4458 },4473 },
4459 .Shl => {4474 .Shl => {
4460 const op_token = try appendToken(rp.c, .AngleBracketAngleBracketLeft, "<<");4475 const op_token = try appendToken(rp.c, .AngleBracketAngleBracketLeft, "<<");
4461 const rhs = try parseCPrimaryExpr(rp, it, source_loc, scope);4476 const rhs = try parseCExpr(rp, it, source_loc, scope);
4462 const bitshift_node = try rp.c.a().create(ast.Node.InfixOp);4477 const bitshift_node = try rp.c.a().create(ast.Node.InfixOp);
4463 bitshift_node.* = .{4478 bitshift_node.* = .{
4464 .op_token = op_token,4479 .op_token = op_token,
...@@ -4468,9 +4483,21 @@ fn parseCSuffixOpExpr(rp: RestorePoint, it: *ctok.TokenList.Iterator, source_loc...@@ -4468,9 +4483,21 @@ fn parseCSuffixOpExpr(rp: RestorePoint, it: *ctok.TokenList.Iterator, source_loc
4468 };4483 };
4469 node = &bitshift_node.base;4484 node = &bitshift_node.base;
4470 },4485 },
4486 .Pipe => {
4487 const op_token = try appendToken(rp.c, .Pipe, "|");
4488 const rhs = try parseCExpr(rp, it, source_loc, scope);
4489 const or_node = try rp.c.a().create(ast.Node.InfixOp);
4490 or_node.* = .{
4491 .op_token = op_token,
4492 .lhs = node,
4493 .op = .BitOr,
4494 .rhs = rhs,
4495 };
4496 node = &or_node.base;
4497 },
4471 .LBrace => {4498 .LBrace => {
4472 const arr_node = try transCreateNodeArrayAccess(rp.c, node);4499 const arr_node = try transCreateNodeArrayAccess(rp.c, node);
4473 arr_node.op.ArrayAccess = try parseCPrimaryExpr(rp, it, source_loc, scope);4500 arr_node.op.ArrayAccess = try parseCExpr(rp, it, source_loc, scope);
4474 arr_node.rtoken = try appendToken(rp.c, .RBrace, "]");4501 arr_node.rtoken = try appendToken(rp.c, .RBrace, "]");
4475 node = &arr_node.base;4502 node = &arr_node.base;
4476 if (it.next().?.id != .RBrace)4503 if (it.next().?.id != .RBrace)
test/translate_c.zig+129-84
...@@ -166,50 +166,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -166,50 +166,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
166 \\}166 \\}
167 });167 });
168168
169 cases.add_both("enums",
170 \\enum Foo {
171 \\ FooA,
172 \\ FooB,
173 \\ Foo1,
174 \\};
175 , &[_][]const u8{
176 \\pub const enum_Foo = extern enum {
177 \\ A,
178 \\ B,
179 \\ @"1",
180 \\};
181 ,
182 \\pub const FooA = enum_Foo.A;
183 ,
184 \\pub const FooB = enum_Foo.B;
185 ,
186 \\pub const Foo1 = enum_Foo.@"1";
187 ,
188 \\pub const Foo = enum_Foo;
189 });
190
191 cases.add_both("enums",
192 \\enum Foo {
193 \\ FooA = 2,
194 \\ FooB = 5,
195 \\ Foo1,
196 \\};
197 , &[_][]const u8{
198 \\pub const enum_Foo = extern enum {
199 \\ A = 2,
200 \\ B = 5,
201 \\ @"1" = 6,
202 \\};
203 ,
204 \\pub const FooA = enum_Foo.A;
205 ,
206 \\pub const FooB = enum_Foo.B;
207 ,
208 \\pub const Foo1 = enum_Foo.@"1";
209 ,
210 \\pub const Foo = enum_Foo;
211 });
212
213 cases.add_both("typedef of function in struct field",169 cases.add_both("typedef of function in struct field",
214 \\typedef void lws_callback_function(void);170 \\typedef void lws_callback_function(void);
215 \\struct Foo {171 \\struct Foo {
...@@ -921,27 +877,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -921,27 +877,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
921 \\ p,877 \\ p,
922 \\};878 \\};
923 , &[_][]const u8{879 , &[_][]const u8{
924 \\pub const a = enum_unnamed_1.a;880 \\pub const a = 0;
925 \\pub const b = enum_unnamed_1.b;881 \\pub const b = 1;
926 \\pub const c = enum_unnamed_1.c;882 \\pub const c = 2;
927 \\const enum_unnamed_1 = extern enum {883 \\const enum_unnamed_1 = extern enum {
928 \\ a,884 \\ a,
929 \\ b,885 \\ b,
930 \\ c,886 \\ c,
931 \\};887 \\};
932 \\pub const d = enum_unnamed_1;888 \\pub const d = enum_unnamed_1;
933 \\pub const e = enum_unnamed_2.e;889 \\pub const e = 0;
934 \\pub const f = enum_unnamed_2.f;890 \\pub const f = 4;
935 \\pub const g = enum_unnamed_2.g;891 \\pub const g = 5;
936 \\const enum_unnamed_2 = extern enum {892 \\const enum_unnamed_2 = extern enum {
937 \\ e = 0,893 \\ e = 0,
938 \\ f = 4,894 \\ f = 4,
939 \\ g = 5,895 \\ g = 5,
940 \\};896 \\};
941 \\pub export var h: enum_unnamed_2 = @as(enum_unnamed_2, e);897 \\pub export var h: enum_unnamed_2 = @intToEnum(enum_unnamed_2, e);
942 \\pub const i = enum_unnamed_3.i;898 \\pub const i = 0;
943 \\pub const j = enum_unnamed_3.j;899 \\pub const j = 1;
944 \\pub const k = enum_unnamed_3.k;900 \\pub const k = 2;
945 \\const enum_unnamed_3 = extern enum {901 \\const enum_unnamed_3 = extern enum {
946 \\ i,902 \\ i,
947 \\ j,903 \\ j,
...@@ -951,9 +907,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -951,9 +907,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
951 \\ l: enum_unnamed_3,907 \\ l: enum_unnamed_3,
952 \\ m: d,908 \\ m: d,
953 \\};909 \\};
954 \\pub const n = enum_i.n;910 \\pub const n = 0;
955 \\pub const o = enum_i.o;911 \\pub const o = 1;
956 \\pub const p = enum_i.p;912 \\pub const p = 2;
957 \\pub const enum_i = extern enum {913 \\pub const enum_i = extern enum {
958 \\ n,914 \\ n,
959 \\ o,915 \\ o,
...@@ -1393,8 +1349,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1393,8 +1349,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1393 \\ Two,1349 \\ Two,
1394 \\};1350 \\};
1395 , &[_][]const u8{1351 , &[_][]const u8{
1396 \\pub const One = enum_unnamed_1.One;1352 \\pub const One = 0;
1397 \\pub const Two = enum_unnamed_1.Two;1353 \\pub const Two = 1;
1398 \\const enum_unnamed_1 = extern enum {1354 \\const enum_unnamed_1 = extern enum {
1399 \\ One,1355 \\ One,
1400 \\ Two,1356 \\ Two,
...@@ -1496,9 +1452,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1496,9 +1452,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1496 \\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p);1452 \\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p);
1497 \\}1453 \\}
1498 , &[_][]const u8{1454 , &[_][]const u8{
1499 \\pub const FooA = enum_Foo.A;
1500 \\pub const FooB = enum_Foo.B;
1501 \\pub const FooC = enum_Foo.C;
1502 \\pub const enum_Foo = extern enum {1455 \\pub const enum_Foo = extern enum {
1503 \\ A,1456 \\ A,
1504 \\ B,1457 \\ B,
...@@ -1509,7 +1462,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1509,7 +1462,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1509 \\ var a = _arg_a;1462 \\ var a = _arg_a;
1510 \\ var b = _arg_b;1463 \\ var b = _arg_b;
1511 \\ var c = _arg_c;1464 \\ var c = _arg_c;
1512 \\ var d: enum_Foo = @as(enum_Foo, FooA);1465 \\ var d: enum_Foo = @intToEnum(enum_Foo, FooA);
1513 \\ var e: c_int = @boolToInt(((a != 0) and (b != 0)));1466 \\ var e: c_int = @boolToInt(((a != 0) and (b != 0)));
1514 \\ var f: c_int = @boolToInt(((b != 0) and (c != null)));1467 \\ var f: c_int = @boolToInt(((b != 0) and (c != null)));
1515 \\ var g: c_int = @boolToInt(((a != 0) and (c != null)));1468 \\ var g: c_int = @boolToInt(((a != 0) and (c != null)));
...@@ -1543,8 +1496,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1543,8 +1496,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1543 \\ x: c_int,1496 \\ x: c_int,
1544 \\ y: c_int,1497 \\ y: c_int,
1545 \\};1498 \\};
1546 \\pub const BarA = enum_Bar.A;1499 ,
1547 \\pub const BarB = enum_Bar.B;
1548 \\pub const enum_Bar = extern enum {1500 \\pub const enum_Bar = extern enum {
1549 \\ A,1501 \\ A,
1550 \\ B,1502 \\ B,
...@@ -1746,9 +1698,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1746,9 +1698,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1746 \\ return 4;1698 \\ return 4;
1747 \\}1699 \\}
1748 , &[_][]const u8{1700 , &[_][]const u8{
1749 \\pub const A = enum_SomeEnum.A;
1750 \\pub const B = enum_SomeEnum.B;
1751 \\pub const C = enum_SomeEnum.C;
1752 \\pub const enum_SomeEnum = extern enum {1701 \\pub const enum_SomeEnum = extern enum {
1753 \\ A,1702 \\ A,
1754 \\ B,1703 \\ B,
...@@ -1863,26 +1812,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1863,26 +1812,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1863 \\ i -= 1;1812 \\ i -= 1;
1864 \\ u +%= 1;1813 \\ u +%= 1;
1865 \\ u -%= 1;1814 \\ u -%= 1;
1866 \\ i = blk: {1815 \\ i = (blk: {
1867 \\ const _ref_1 = &i;1816 \\ const _ref_1 = &i;
1868 \\ _ref_1.* += 1;1817 \\ _ref_1.* += 1;
1869 \\ break :blk _ref_1.*;1818 \\ break :blk _ref_1.*;
1870 \\ };1819 \\ });
1871 \\ i = blk: {1820 \\ i = (blk: {
1872 \\ const _ref_2 = &i;1821 \\ const _ref_2 = &i;
1873 \\ _ref_2.* -= 1;1822 \\ _ref_2.* -= 1;
1874 \\ break :blk _ref_2.*;1823 \\ break :blk _ref_2.*;
1875 \\ };1824 \\ });
1876 \\ u = blk: {1825 \\ u = (blk: {
1877 \\ const _ref_3 = &u;1826 \\ const _ref_3 = &u;
1878 \\ _ref_3.* +%= 1;1827 \\ _ref_3.* +%= 1;
1879 \\ break :blk _ref_3.*;1828 \\ break :blk _ref_3.*;
1880 \\ };1829 \\ });
1881 \\ u = blk: {1830 \\ u = (blk: {
1882 \\ const _ref_4 = &u;1831 \\ const _ref_4 = &u;
1883 \\ _ref_4.* -%= 1;1832 \\ _ref_4.* -%= 1;
1884 \\ break :blk _ref_4.*;1833 \\ break :blk _ref_4.*;
1885 \\ };1834 \\ });
1886 \\}1835 \\}
1887 });1836 });
18881837
...@@ -2062,30 +2011,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2062,30 +2011,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2062 \\ i -= 1;2011 \\ i -= 1;
2063 \\ u +%= 1;2012 \\ u +%= 1;
2064 \\ u -%= 1;2013 \\ u -%= 1;
2065 \\ i = blk: {2014 \\ i = (blk: {
2066 \\ const _ref_1 = &i;2015 \\ const _ref_1 = &i;
2067 \\ const _tmp_2 = _ref_1.*;2016 \\ const _tmp_2 = _ref_1.*;
2068 \\ _ref_1.* += 1;2017 \\ _ref_1.* += 1;
2069 \\ break :blk _tmp_2;2018 \\ break :blk _tmp_2;
2070 \\ };2019 \\ });
2071 \\ i = blk: {2020 \\ i = (blk: {
2072 \\ const _ref_3 = &i;2021 \\ const _ref_3 = &i;
2073 \\ const _tmp_4 = _ref_3.*;2022 \\ const _tmp_4 = _ref_3.*;
2074 \\ _ref_3.* -= 1;2023 \\ _ref_3.* -= 1;
2075 \\ break :blk _tmp_4;2024 \\ break :blk _tmp_4;
2076 \\ };2025 \\ });
2077 \\ u = blk: {2026 \\ u = (blk: {
2078 \\ const _ref_5 = &u;2027 \\ const _ref_5 = &u;
2079 \\ const _tmp_6 = _ref_5.*;2028 \\ const _tmp_6 = _ref_5.*;
2080 \\ _ref_5.* +%= 1;2029 \\ _ref_5.* +%= 1;
2081 \\ break :blk _tmp_6;2030 \\ break :blk _tmp_6;
2082 \\ };2031 \\ });
2083 \\ u = blk: {2032 \\ u = (blk: {
2084 \\ const _ref_7 = &u;2033 \\ const _ref_7 = &u;
2085 \\ const _tmp_8 = _ref_7.*;2034 \\ const _tmp_8 = _ref_7.*;
2086 \\ _ref_7.* -%= 1;2035 \\ _ref_7.* -%= 1;
2087 \\ break :blk _tmp_8;2036 \\ break :blk _tmp_8;
2088 \\ };2037 \\ });
2089 \\}2038 \\}
2090 });2039 });
20912040
...@@ -2172,6 +2121,58 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2172,6 +2121,58 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2172 \\pub const FOO_CHAR = '\x3f';2121 \\pub const FOO_CHAR = '\x3f';
2173 });2122 });
21742123
2124 cases.add_2("enums",
2125 \\enum Foo {
2126 \\ FooA,
2127 \\ FooB,
2128 \\ Foo1,
2129 \\};
2130 , &[_][]const u8{
2131 \\pub const enum_Foo = extern enum {
2132 \\ A,
2133 \\ B,
2134 \\ @"1",
2135 \\};
2136 ,
2137 \\pub const FooA = 0;
2138 ,
2139 \\pub const FooB = 1;
2140 ,
2141 \\pub const Foo1 = 2;
2142 ,
2143 \\pub const Foo = enum_Foo;
2144 });
2145
2146 cases.add_2("enums",
2147 \\enum Foo {
2148 \\ FooA = 2,
2149 \\ FooB = 5,
2150 \\ Foo1,
2151 \\};
2152 , &[_][]const u8{
2153 \\pub const enum_Foo = extern enum {
2154 \\ A = 2,
2155 \\ B = 5,
2156 \\ @"1" = 6,
2157 \\};
2158 ,
2159 \\pub const FooA = 2;
2160 ,
2161 \\pub const FooB = 5;
2162 ,
2163 \\pub const Foo1 = 6;
2164 ,
2165 \\pub const Foo = enum_Foo;
2166 });
2167
2168 cases.add_2("macro cast",
2169 \\#define FOO(bar) baz((void *)(baz))
2170 , &[_][]const u8{
2171 \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast([*c]void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr([*c]void, baz) else @as([*c]void, baz))) {
2172 \\ return baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast([*c]void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr([*c]void, baz) else @as([*c]void, baz));
2173 \\}
2174 });
2175
2175 /////////////// Cases for only stage1 because stage2 behavior is better ////////////////2176 /////////////// Cases for only stage1 because stage2 behavior is better ////////////////
2176 cases.addC("Parameterless function prototypes",2177 cases.addC("Parameterless function prototypes",
2177 \\void foo() {}2178 \\void foo() {}
...@@ -3124,4 +3125,48 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3124,4 +3125,48 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3124 ,3125 ,
3125 \\pub const FOO_CHAR = 63;3126 \\pub const FOO_CHAR = 63;
3126 });3127 });
3128
3129 cases.add("enums",
3130 \\enum Foo {
3131 \\ FooA,
3132 \\ FooB,
3133 \\ Foo1,
3134 \\};
3135 , &[_][]const u8{
3136 \\pub const enum_Foo = extern enum {
3137 \\ A,
3138 \\ B,
3139 \\ @"1",
3140 \\};
3141 ,
3142 \\pub const FooA = enum_Foo.A;
3143 ,
3144 \\pub const FooB = enum_Foo.B;
3145 ,
3146 \\pub const Foo1 = enum_Foo.@"1";
3147 ,
3148 \\pub const Foo = enum_Foo;
3149 });
3150
3151 cases.add("enums",
3152 \\enum Foo {
3153 \\ FooA = 2,
3154 \\ FooB = 5,
3155 \\ Foo1,
3156 \\};
3157 , &[_][]const u8{
3158 \\pub const enum_Foo = extern enum {
3159 \\ A = 2,
3160 \\ B = 5,
3161 \\ @"1" = 6,
3162 \\};
3163 ,
3164 \\pub const FooA = enum_Foo.A;
3165 ,
3166 \\pub const FooB = enum_Foo.B;
3167 ,
3168 \\pub const Foo1 = enum_Foo.@"1";
3169 ,
3170 \\pub const Foo = enum_Foo;
3171 });
3127}3172}