authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-08 11:26:53+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-08 11:26:53+02:00
log5aa993cd617e0ae3cabc9c626e45a748857e2f2a
tree23d4c5c3f95f0ff22e0be1e834fe647f396aeadd
parent9e60c89601ab205cd3e63215b318beb89ad1b471
signaturelock-open Commit is signed but in an unrecognized format.

translate-c fix nested loops without blocks.


2 files changed, 42 insertions(+), 37 deletions(-)

src-self-hosted/translate_c.zig+20-31
......@@ -2237,6 +2237,7 @@ fn transWhileLoop(
22372237 .id = .Loop,
22382238 };
22392239 while_node.body = try transStmt(rp, &loop_scope, ZigClangWhileStmt_getBody(stmt), .unused, .r_value);
2240 _ = try appendToken(rp.c, .Semicolon, ";");
22402241 return &while_node.base;
22412242}
22422243
......@@ -2346,8 +2347,10 @@ fn transForLoop(
23462347 try block_scope.?.block_node.statements.push(&while_node.base);
23472348 block_scope.?.block_node.rbrace = try appendToken(rp.c, .RBrace, "}");
23482349 return &block_scope.?.block_node.base;
2349 } else
2350 } else {
2351 _ = try appendToken(rp.c, .Semicolon, ";");
23502352 return &while_node.base;
2353 }
23512354}
23522355
23532356fn transSwitch(
......@@ -5431,6 +5434,8 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54315434 //else
54325435 // @as(dest, x)
54335436
5437 const lparen = try appendToken(c, .LParen, "(");
5438
54345439 const if_1 = try transCreateNodeIf(c);
54355440 const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
54365441 const type_of_1 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");
......@@ -5492,7 +5497,13 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54925497 as.rparen_token = try appendToken(c, .RParen, ")");
54935498 else_2.body = &as.base;
54945499
5495 return &if_1.base;
5500 const group_node = try c.a().create(ast.Node.GroupedExpression);
5501 group_node.* = .{
5502 .lparen = lparen,
5503 .expr = &if_1.base,
5504 .rparen = try appendToken(c, .RParen, ")"),
5505 };
5506 return &group_node.base;
54965507 },
54975508 else => {
54985509 const first_tok = it.list.at(0);
......@@ -5545,14 +5556,7 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
55455556 );
55465557 return error.ParseError;
55475558 }
5548 // deref is often used together with casts so we group the lhs expression
5549 const group = try c.a().create(ast.Node.GroupedExpression);
5550 group.* = .{
5551 .lparen = try appendToken(c, .LParen, "("),
5552 .expr = node,
5553 .rparen = try appendToken(c, .RParen, ")"),
5554 };
5555 const deref = try transCreateNodePtrDeref(c, &group.base);
5559 const deref = try transCreateNodePtrDeref(c, node);
55565560 node = try transCreateNodeFieldAccess(c, deref, source[name_tok.start..name_tok.end]);
55575561 continue;
55585562 },
......@@ -5596,7 +5600,7 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
55965600 },
55975601 .Ampersand => {
55985602 op_token = try appendToken(c, .Ampersand, "&");
5599 op_id .BitAnd;
5603 op_id= .BitAnd;
56005604 },
56015605 .Plus => {
56025606 op_token = try appendToken(c, .Plus, "+");
......@@ -5604,7 +5608,7 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
56045608 },
56055609 .Minus => {
56065610 op_token = try appendToken(c, .Minus, "-");
5607 op_id .Sub;
5611 op_id= .Sub;
56085612 },
56095613 .AmpersandAmpersand => {
56105614 op_token = try appendToken(c, .Keyword_and, "and");
......@@ -5676,19 +5680,17 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
56765680 },
56775681 .BangEqual => {
56785682 op_token = try appendToken(c, .BangEqual, "!=");
5679 op_id = .BangEqual;
5683 op_id = .BangEqual;
56805684 },
56815685 .EqualEqual => {
56825686 op_token = try appendToken(c, .EqualEqual, "==");
56835687 op_id = .EqualEqual;
56845688 },
56855689 .Slash => {
5686 // unsigned/float division uses the operator
56875690 op_id = .Div;
56885691 op_token = try appendToken(c, .Slash, "/");
56895692 },
56905693 .Percent => {
5691 // unsigned/float division uses the operator
56925694 op_id = .Mod;
56935695 op_token = try appendToken(c, .Percent, "%");
56945696 },
......@@ -5729,25 +5731,12 @@ fn parseCPrefixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
57295731 return &node.base;
57305732 },
57315733 .Asterisk => {
5732 // deref is often used together with casts so we group the lhs expression
5733 const group = try c.a().create(ast.Node.GroupedExpression);
5734 group.* = .{
5735 .lparen = try appendToken(c, .LParen, "("),
5736 .expr = try parseCPrefixOpExpr(c, it, source, source_loc, scope),
5737 .rparen = try appendToken(c, .RParen, ")"),
5738 };
5739 return try transCreateNodePtrDeref(c, &group.base);
5734 const node = try parseCPrefixOpExpr(c, it, source, source_loc, scope);
5735 return try transCreateNodePtrDeref(c, node);
57405736 },
57415737 .Ampersand => {
5742 // address of is often used together with casts so we group the rhs expression
57435738 const node = try transCreateNodePrefixOp(c, .AddressOf, .Ampersand, "&");
5744 const group = try c.a().create(ast.Node.GroupedExpression);
5745 group.* = .{
5746 .lparen = try appendToken(c, .LParen, "("),
5747 .expr = try parseCPrefixOpExpr(c, it, source, source_loc, scope),
5748 .rparen = try appendToken(c, .RParen, ")"),
5749 };
5750 node.rhs = &group.base;
5739 node.rhs = try parseCPrefixOpExpr(c, it, source, source_loc, scope);
57515740 return &node.base;
57525741 },
57535742 else => {
test/translate_c.zig+22-6
......@@ -3,6 +3,22 @@ const std = @import("std");
33const CrossTarget = std.zig.CrossTarget;
44
55pub fn addCases(cases: *tests.TranslateCContext) void {
6 cases.add("nested loops without blocks",
7 \\void foo() {
8 \\ while (0) while (0) {}
9 \\ for (;;) while (0);
10 \\ for (;;) do {} while (0);
11 \\}
12 , &[_][]const u8{
13 \\pub export fn foo() void {
14 \\ while (@as(c_int, 0) != 0) while (@as(c_int, 0) != 0) {};
15 \\ while (true) while (@as(c_int, 0) != 0) {};
16 \\ while (true) while (true) {
17 \\ if (!(@as(c_int, 0) != 0)) break;
18 \\ };
19 \\}
20 });
21
622 cases.add("macro comma operator",
723 \\#define foo (foo, bar)
824 \\#define bar(x) (&x, +3, 4 == 4, 5 * 6, baz(1, 2), 2 % 2, baz(1,2))
......@@ -14,7 +30,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1430 ,
1531 \\pub inline fn bar(x: var) @TypeOf(baz(1, 2)) {
1632 \\ return blk: {
17 \\ _ = &(x);
33 \\ _ = &x;
1834 \\ _ = 3;
1935 \\ _ = 4 == 4;
2036 \\ _ = 5 * 6;
......@@ -1404,7 +1420,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14041420 cases.add("macro pointer cast",
14051421 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
14061422 , &[_][]const u8{
1407 \\pub const NRF_GPIO = if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Pointer) @ptrCast([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Int) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE);
1423 \\pub const NRF_GPIO = (if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Pointer) @ptrCast([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Int) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE));
14081424 });
14091425
14101426 cases.add("basic macro function",
......@@ -1993,7 +2009,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19932009 ,
19942010 \\pub const DOT = a.b;
19952011 ,
1996 \\pub const ARROW = (a).*.b;
2012 \\pub const ARROW = a.*.b;
19972013 });
19982014
19992015 cases.add("array access",
......@@ -2588,11 +2604,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25882604 \\#define FOO(bar) baz((void *)(baz))
25892605 \\#define BAR (void*) a
25902606 , &[_][]const u8{
2591 \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz))) {
2592 \\ return baz(if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz));
2607 \\pub inline fn FOO(bar: var) @TypeOf(baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz)))) {
2608 \\ return baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz)));
25932609 \\}
25942610 ,
2595 \\pub const BAR = if (@typeInfo(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, a) else if (@typeInfo(@TypeOf(a)) == .Int) @intToPtr(*c_void, a) else @as(*c_void, a);
2611 \\pub const BAR = (if (@typeInfo(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, a) else if (@typeInfo(@TypeOf(a)) == .Int) @intToPtr(*c_void, a) else @as(*c_void, a));
25962612 });
25972613
25982614 cases.add("macro conditional operator",