workaround for #2043

master
Jimmi Holst Christensen 2019-03-15 11:17:21 +01:00
parent 761356209b
commit 3ef9b899e4
2 changed files with 71 additions and 65 deletions

View File

@ -446,6 +446,12 @@ static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, AstNode *r
return fn_def; 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) { static AstNode *get_global(Context *c, Buf *name) {
{ {
auto entry = c->global_table.maybe_get(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 { } else {
// worst case // worst case
// c: lhs = rhs // c: lhs = rhs
// zig: x: { // zig: (x: {
// zig: const _tmp = rhs; // zig: const _tmp = rhs;
// zig: lhs = _tmp; // zig: lhs = _tmp;
// zig: break :x _tmp // zig: break :x _tmp
// zig: } // zig: })
TransScopeBlock *child_scope = trans_scope_block_create(c, scope); TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
Buf *label_name = buf_create_from_str("x"); 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); 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)); 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 { } else {
// need more complexity. worst case, this looks like this: // need more complexity. worst case, this looks like this:
// c: lhs >>= rhs // c: lhs >>= rhs
// zig: x: { // zig: (x: {
// zig: const _ref = &lhs; // zig: const _ref = &lhs;
// zig: *_ref = result_type(operation_type(*_ref) >> u5(rhs)); // zig: *_ref = result_type(operation_type(*_ref) >> u5(rhs));
// zig: break :x *_ref // zig: break :x *_ref
// zig: } // zig: })
// where u5 is the appropriate type // where u5 is the appropriate type
TransScopeBlock *child_scope = trans_scope_block_create(c, scope); 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)))); 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 { } else {
// need more complexity. worst case, this looks like this: // need more complexity. worst case, this looks like this:
// c: lhs += rhs // c: lhs += rhs
// zig: x: { // zig: (x: {
// zig: const _ref = &lhs; // zig: const _ref = &lhs;
// zig: *_ref = *_ref + rhs; // zig: *_ref = *_ref + rhs;
// zig: break :x *_ref // zig: break :x *_ref
// zig: } // zig: })
TransScopeBlock *child_scope = trans_scope_block_create(c, scope); TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
Buf *label_name = buf_create_from_str("x"); 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_ptr_deref(c,
trans_create_node_symbol(c, tmp_var_name)))); 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 // worst case
// c: expr++ // c: expr++
// zig: x: { // zig: (x: {
// zig: const _ref = &expr; // zig: const _ref = &expr;
// zig: const _tmp = *_ref; // zig: const _tmp = *_ref;
// zig: *_ref += 1; // zig: *_ref += 1;
// zig: break :x _tmp // zig: break :x _tmp
// zig: } // zig: })
TransScopeBlock *child_scope = trans_scope_block_create(c, scope); TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
Buf *label_name = buf_create_from_str("x"); Buf *label_name = buf_create_from_str("x");
child_scope->node->data.block.name = label_name; 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 // 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))); 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, 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 // worst case
// c: ++expr // c: ++expr
// zig: x: { // zig: (x: {
// zig: const _ref = &expr; // zig: const _ref = &expr;
// zig: *_ref += 1; // zig: *_ref += 1;
// zig: break :x *_ref // zig: break :x *_ref
// zig: } // zig: })
TransScopeBlock *child_scope = trans_scope_block_create(c, scope); TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
Buf *label_name = buf_create_from_str("x"); Buf *label_name = buf_create_from_str("x");
child_scope->node->data.block.name = label_name; 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)); trans_create_node_symbol(c, ref_var_name));
child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, deref_expr)); 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) { static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransScope *scope, const clang::UnaryOperator *stmt) {

View File

@ -643,11 +643,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub export fn max(a: c_int) void { \\pub export fn max(a: c_int) void {
\\ var b: c_int = undefined; \\ var b: c_int = undefined;
\\ var c: c_int = undefined; \\ var c: c_int = undefined;
\\ c = x: { \\ c = (x: {
\\ const _tmp = a; \\ const _tmp = a;
\\ b = _tmp; \\ b = _tmp;
\\ break :x _tmp; \\ break :x _tmp;
\\ }; \\ });
\\} \\}
); );
@ -820,46 +820,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
, ,
\\pub export fn foo() void { \\pub export fn foo() void {
\\ var a: c_int = 0; \\ var a: c_int = 0;
\\ a += x: { \\ a += (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* + 1); \\ _ref.* = (_ref.* + 1);
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a -= x: { \\ a -= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* - 1); \\ _ref.* = (_ref.* - 1);
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a *= x: { \\ a *= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* * 1); \\ _ref.* = (_ref.* * 1);
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a &= x: { \\ a &= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* & 1); \\ _ref.* = (_ref.* & 1);
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a |= x: { \\ a |= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* | 1); \\ _ref.* = (_ref.* | 1);
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a ^= x: { \\ a ^= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* ^ 1); \\ _ref.* = (_ref.* ^ 1);
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a >>= @import("std").math.Log2Int(c_int)(x: { \\ a >>= @import("std").math.Log2Int(c_int)((x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_int)(1)); \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_int)(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }); \\ }));
\\ a <<= @import("std").math.Log2Int(c_int)(x: { \\ a <<= @import("std").math.Log2Int(c_int)((x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_int)(1)); \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_int)(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }); \\ }));
\\} \\}
); );
@ -878,46 +878,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
, ,
\\pub export fn foo() void { \\pub export fn foo() void {
\\ var a: c_uint = c_uint(0); \\ var a: c_uint = c_uint(0);
\\ a +%= x: { \\ a +%= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* +% c_uint(1)); \\ _ref.* = (_ref.* +% c_uint(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a -%= x: { \\ a -%= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* -% c_uint(1)); \\ _ref.* = (_ref.* -% c_uint(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a *%= x: { \\ a *%= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* *% c_uint(1)); \\ _ref.* = (_ref.* *% c_uint(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a &= x: { \\ a &= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* & c_uint(1)); \\ _ref.* = (_ref.* & c_uint(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a |= x: { \\ a |= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* | c_uint(1)); \\ _ref.* = (_ref.* | c_uint(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a ^= x: { \\ a ^= (x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* ^ c_uint(1)); \\ _ref.* = (_ref.* ^ c_uint(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ a >>= @import("std").math.Log2Int(c_uint)(x: { \\ a >>= @import("std").math.Log2Int(c_uint)((x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_uint)(1)); \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_uint)(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }); \\ }));
\\ a <<= @import("std").math.Log2Int(c_uint)(x: { \\ a <<= @import("std").math.Log2Int(c_uint)((x: {
\\ const _ref = &a; \\ const _ref = &a;
\\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_uint)(1)); \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_uint)(1));
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }); \\ }));
\\} \\}
); );
@ -953,30 +953,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ i -= 1; \\ i -= 1;
\\ u +%= 1; \\ u +%= 1;
\\ u -%= 1; \\ u -%= 1;
\\ i = x: { \\ i = (x: {
\\ const _ref = &i; \\ const _ref = &i;
\\ const _tmp = _ref.*; \\ const _tmp = _ref.*;
\\ _ref.* += 1; \\ _ref.* += 1;
\\ break :x _tmp; \\ break :x _tmp;
\\ }; \\ });
\\ i = x: { \\ i = (x: {
\\ const _ref = &i; \\ const _ref = &i;
\\ const _tmp = _ref.*; \\ const _tmp = _ref.*;
\\ _ref.* -= 1; \\ _ref.* -= 1;
\\ break :x _tmp; \\ break :x _tmp;
\\ }; \\ });
\\ u = x: { \\ u = (x: {
\\ const _ref = &u; \\ const _ref = &u;
\\ const _tmp = _ref.*; \\ const _tmp = _ref.*;
\\ _ref.* +%= 1; \\ _ref.* +%= 1;
\\ break :x _tmp; \\ break :x _tmp;
\\ }; \\ });
\\ u = x: { \\ u = (x: {
\\ const _ref = &u; \\ const _ref = &u;
\\ const _tmp = _ref.*; \\ const _tmp = _ref.*;
\\ _ref.* -%= 1; \\ _ref.* -%= 1;
\\ break :x _tmp; \\ break :x _tmp;
\\ }; \\ });
\\} \\}
); );
@ -1001,26 +1001,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ i -= 1; \\ i -= 1;
\\ u +%= 1; \\ u +%= 1;
\\ u -%= 1; \\ u -%= 1;
\\ i = x: { \\ i = (x: {
\\ const _ref = &i; \\ const _ref = &i;
\\ _ref.* += 1; \\ _ref.* += 1;
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ i = x: { \\ i = (x: {
\\ const _ref = &i; \\ const _ref = &i;
\\ _ref.* -= 1; \\ _ref.* -= 1;
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ u = x: { \\ u = (x: {
\\ const _ref = &u; \\ const _ref = &u;
\\ _ref.* +%= 1; \\ _ref.* +%= 1;
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\ u = x: { \\ u = (x: {
\\ const _ref = &u; \\ const _ref = &u;
\\ _ref.* -%= 1; \\ _ref.* -%= 1;
\\ break :x _ref.*; \\ break :x _ref.*;
\\ }; \\ });
\\} \\}
); );