Skip to content

workaround for #2043 #2068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/translate_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
100 changes: 50 additions & 50 deletions test/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
\\ };
\\ });
\\}
);

Expand Down Expand Up @@ -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.*;
\\ });
\\ }));
\\}
);

Expand All @@ -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.*;
\\ });
\\ }));
\\}
);

Expand Down Expand Up @@ -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;
\\ };
\\ });
\\}
);

Expand All @@ -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.*;
\\ };
\\ });
\\}
);

Expand Down