diff --git a/src/translate_c.cpp b/src/translate_c.cpp index c56d31eb2..f19277982 100644 --- a/src/translate_c.cpp +++ b/src/translate_c.cpp @@ -446,6 +446,12 @@ static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, AstNode *r return fn_def; } +static AstNode *trans_create_node_grouped_expr(Context *c, AstNode *child) { + AstNode *node = trans_create_node(c, NodeTypeGroupedExpr); + node->data.grouped_expr = child; + return node; +} + static AstNode *get_global(Context *c, Buf *name) { { auto entry = c->global_table.maybe_get(name); @@ -1314,11 +1320,11 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco } else { // worst case // c: lhs = rhs - // zig: x: { + // zig: (x: { // zig: const _tmp = rhs; // zig: lhs = _tmp; // zig: break :x _tmp - // zig: } + // zig: }) TransScopeBlock *child_scope = trans_scope_block_create(c, scope); Buf *label_name = buf_create_from_str("x"); @@ -1343,7 +1349,7 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco AstNode *tmp_symbol_node = trans_create_node_symbol(c, tmp_var_name); child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, tmp_symbol_node)); - return child_scope->node; + return trans_create_node_grouped_expr(c, child_scope->node); } } @@ -1499,11 +1505,11 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result } else { // need more complexity. worst case, this looks like this: // c: lhs >>= rhs - // zig: x: { + // zig: (x: { // zig: const _ref = &lhs; // zig: *_ref = result_type(operation_type(*_ref) >> u5(rhs)); // zig: break :x *_ref - // zig: } + // zig: }) // where u5 is the appropriate type TransScopeBlock *child_scope = trans_scope_block_create(c, scope); @@ -1556,7 +1562,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result trans_create_node_symbol(c, tmp_var_name)))); } - return child_scope->node; + return trans_create_node_grouped_expr(c, child_scope->node); } } @@ -1574,11 +1580,11 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used, } else { // need more complexity. worst case, this looks like this: // c: lhs += rhs - // zig: x: { + // zig: (x: { // zig: const _ref = &lhs; // zig: *_ref = *_ref + rhs; // zig: break :x *_ref - // zig: } + // zig: }) TransScopeBlock *child_scope = trans_scope_block_create(c, scope); Buf *label_name = buf_create_from_str("x"); @@ -1615,7 +1621,7 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used, trans_create_node_ptr_deref(c, trans_create_node_symbol(c, tmp_var_name)))); - return child_scope->node; + return trans_create_node_grouped_expr(c, child_scope->node); } } @@ -1911,12 +1917,12 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr } // worst case // c: expr++ - // zig: x: { + // zig: (x: { // zig: const _ref = &expr; // zig: const _tmp = *_ref; // zig: *_ref += 1; // zig: break :x _tmp - // zig: } + // zig: }) TransScopeBlock *child_scope = trans_scope_block_create(c, scope); Buf *label_name = buf_create_from_str("x"); child_scope->node->data.block.name = label_name; @@ -1948,7 +1954,7 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr // break :x _tmp child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, trans_create_node_symbol(c, tmp_var_name))); - return child_scope->node; + return trans_create_node_grouped_expr(c, child_scope->node); } static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, TransScope *scope, @@ -1967,11 +1973,11 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra } // worst case // c: ++expr - // zig: x: { + // zig: (x: { // zig: const _ref = &expr; // zig: *_ref += 1; // zig: break :x *_ref - // zig: } + // zig: }) TransScopeBlock *child_scope = trans_scope_block_create(c, scope); Buf *label_name = buf_create_from_str("x"); child_scope->node->data.block.name = label_name; @@ -1998,7 +2004,7 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra trans_create_node_symbol(c, ref_var_name)); child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, deref_expr)); - return child_scope->node; + return trans_create_node_grouped_expr(c, child_scope->node); } static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransScope *scope, const clang::UnaryOperator *stmt) { diff --git a/test/translate_c.zig b/test/translate_c.zig index e83ca7478..5ed57de2f 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -643,11 +643,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub export fn max(a: c_int) void { \\ var b: c_int = undefined; \\ var c: c_int = undefined; - \\ c = x: { + \\ c = (x: { \\ const _tmp = a; \\ b = _tmp; \\ break :x _tmp; - \\ }; + \\ }); \\} ); @@ -820,46 +820,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , \\pub export fn foo() void { \\ var a: c_int = 0; - \\ a += x: { + \\ a += (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* + 1); \\ break :x _ref.*; - \\ }; - \\ a -= x: { + \\ }); + \\ a -= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* - 1); \\ break :x _ref.*; - \\ }; - \\ a *= x: { + \\ }); + \\ a *= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* * 1); \\ break :x _ref.*; - \\ }; - \\ a &= x: { + \\ }); + \\ a &= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* & 1); \\ break :x _ref.*; - \\ }; - \\ a |= x: { + \\ }); + \\ a |= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* | 1); \\ break :x _ref.*; - \\ }; - \\ a ^= x: { + \\ }); + \\ a ^= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* ^ 1); \\ break :x _ref.*; - \\ }; - \\ a >>= @import("std").math.Log2Int(c_int)(x: { + \\ }); + \\ a >>= @import("std").math.Log2Int(c_int)((x: { \\ const _ref = &a; \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_int)(1)); \\ break :x _ref.*; - \\ }); - \\ a <<= @import("std").math.Log2Int(c_int)(x: { + \\ })); + \\ a <<= @import("std").math.Log2Int(c_int)((x: { \\ const _ref = &a; \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_int)(1)); \\ break :x _ref.*; - \\ }); + \\ })); \\} ); @@ -878,46 +878,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , \\pub export fn foo() void { \\ var a: c_uint = c_uint(0); - \\ a +%= x: { + \\ a +%= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* +% c_uint(1)); \\ break :x _ref.*; - \\ }; - \\ a -%= x: { + \\ }); + \\ a -%= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* -% c_uint(1)); \\ break :x _ref.*; - \\ }; - \\ a *%= x: { + \\ }); + \\ a *%= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* *% c_uint(1)); \\ break :x _ref.*; - \\ }; - \\ a &= x: { + \\ }); + \\ a &= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* & c_uint(1)); \\ break :x _ref.*; - \\ }; - \\ a |= x: { + \\ }); + \\ a |= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* | c_uint(1)); \\ break :x _ref.*; - \\ }; - \\ a ^= x: { + \\ }); + \\ a ^= (x: { \\ const _ref = &a; \\ _ref.* = (_ref.* ^ c_uint(1)); \\ break :x _ref.*; - \\ }; - \\ a >>= @import("std").math.Log2Int(c_uint)(x: { + \\ }); + \\ a >>= @import("std").math.Log2Int(c_uint)((x: { \\ const _ref = &a; \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_uint)(1)); \\ break :x _ref.*; - \\ }); - \\ a <<= @import("std").math.Log2Int(c_uint)(x: { + \\ })); + \\ a <<= @import("std").math.Log2Int(c_uint)((x: { \\ const _ref = &a; \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_uint)(1)); \\ break :x _ref.*; - \\ }); + \\ })); \\} ); @@ -953,30 +953,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ i -= 1; \\ u +%= 1; \\ u -%= 1; - \\ i = x: { + \\ i = (x: { \\ const _ref = &i; \\ const _tmp = _ref.*; \\ _ref.* += 1; \\ break :x _tmp; - \\ }; - \\ i = x: { + \\ }); + \\ i = (x: { \\ const _ref = &i; \\ const _tmp = _ref.*; \\ _ref.* -= 1; \\ break :x _tmp; - \\ }; - \\ u = x: { + \\ }); + \\ u = (x: { \\ const _ref = &u; \\ const _tmp = _ref.*; \\ _ref.* +%= 1; \\ break :x _tmp; - \\ }; - \\ u = x: { + \\ }); + \\ u = (x: { \\ const _ref = &u; \\ const _tmp = _ref.*; \\ _ref.* -%= 1; \\ break :x _tmp; - \\ }; + \\ }); \\} ); @@ -1001,26 +1001,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ i -= 1; \\ u +%= 1; \\ u -%= 1; - \\ i = x: { + \\ i = (x: { \\ const _ref = &i; \\ _ref.* += 1; \\ break :x _ref.*; - \\ }; - \\ i = x: { + \\ }); + \\ i = (x: { \\ const _ref = &i; \\ _ref.* -= 1; \\ break :x _ref.*; - \\ }; - \\ u = x: { + \\ }); + \\ u = (x: { \\ const _ref = &u; \\ _ref.* +%= 1; \\ break :x _ref.*; - \\ }; - \\ u = x: { + \\ }); + \\ u = (x: { \\ const _ref = &u; \\ _ref.* -%= 1; \\ break :x _ref.*; - \\ }; + \\ }); \\} );