authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2019-03-15 11:17:21+01:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2019-03-15 11:17:21+01:00
log3ef9b899e4ea24af415381bf876c281d5ca9574c
tree33b7b41646eecf8de510295b220ccc39681adc94
parent761356209bd64ed655be96c0cf11869f2ff5f530

workaround for #2043


2 files changed, 71 insertions(+), 65 deletions(-)

src/translate_c.cpp+21-15
......@@ -446,6 +446,12 @@ static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, AstNode *r
446446 return fn_def;
447447}
448448
449static AstNode *trans_create_node_grouped_expr(Context *c, AstNode *child) {
450 AstNode *node = trans_create_node(c, NodeTypeGroupedExpr);
451 node->data.grouped_expr = child;
452 return node;
453}
454
449455static AstNode *get_global(Context *c, Buf *name) {
450456 {
451457 auto entry = c->global_table.maybe_get(name);
......@@ -1314,11 +1320,11 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco
13141320 } else {
13151321 // worst case
13161322 // c: lhs = rhs
1317 // zig: x: {
1323 // zig: (x: {
13181324 // zig: const _tmp = rhs;
13191325 // zig: lhs = _tmp;
13201326 // zig: break :x _tmp
1321 // zig: }
1327 // zig: })
13221328
13231329 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
13241330 Buf *label_name = buf_create_from_str("x");
......@@ -1343,7 +1349,7 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco
13431349 AstNode *tmp_symbol_node = trans_create_node_symbol(c, tmp_var_name);
13441350 child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, tmp_symbol_node));
13451351
1346 return child_scope->node;
1352 return trans_create_node_grouped_expr(c, child_scope->node);
13471353 }
13481354}
13491355
......@@ -1499,11 +1505,11 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
14991505 } else {
15001506 // need more complexity. worst case, this looks like this:
15011507 // c: lhs >>= rhs
1502 // zig: x: {
1508 // zig: (x: {
15031509 // zig: const _ref = &lhs;
15041510 // zig: *_ref = result_type(operation_type(*_ref) >> u5(rhs));
15051511 // zig: break :x *_ref
1506 // zig: }
1512 // zig: })
15071513 // where u5 is the appropriate type
15081514
15091515 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
......@@ -1556,7 +1562,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
15561562 trans_create_node_symbol(c, tmp_var_name))));
15571563 }
15581564
1559 return child_scope->node;
1565 return trans_create_node_grouped_expr(c, child_scope->node);
15601566 }
15611567}
15621568
......@@ -1574,11 +1580,11 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,
15741580 } else {
15751581 // need more complexity. worst case, this looks like this:
15761582 // c: lhs += rhs
1577 // zig: x: {
1583 // zig: (x: {
15781584 // zig: const _ref = &lhs;
15791585 // zig: *_ref = *_ref + rhs;
15801586 // zig: break :x *_ref
1581 // zig: }
1587 // zig: })
15821588
15831589 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
15841590 Buf *label_name = buf_create_from_str("x");
......@@ -1615,7 +1621,7 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,
16151621 trans_create_node_ptr_deref(c,
16161622 trans_create_node_symbol(c, tmp_var_name))));
16171623
1618 return child_scope->node;
1624 return trans_create_node_grouped_expr(c, child_scope->node);
16191625 }
16201626}
16211627
......@@ -1911,12 +1917,12 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr
19111917 }
19121918 // worst case
19131919 // c: expr++
1914 // zig: x: {
1920 // zig: (x: {
19151921 // zig: const _ref = &expr;
19161922 // zig: const _tmp = *_ref;
19171923 // zig: *_ref += 1;
19181924 // zig: break :x _tmp
1919 // zig: }
1925 // zig: })
19201926 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
19211927 Buf *label_name = buf_create_from_str("x");
19221928 child_scope->node->data.block.name = label_name;
......@@ -1948,7 +1954,7 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr
19481954 // break :x _tmp
19491955 child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, trans_create_node_symbol(c, tmp_var_name)));
19501956
1951 return child_scope->node;
1957 return trans_create_node_grouped_expr(c, child_scope->node);
19521958}
19531959
19541960static 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
19671973 }
19681974 // worst case
19691975 // c: ++expr
1970 // zig: x: {
1976 // zig: (x: {
19711977 // zig: const _ref = &expr;
19721978 // zig: *_ref += 1;
19731979 // zig: break :x *_ref
1974 // zig: }
1980 // zig: })
19751981 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
19761982 Buf *label_name = buf_create_from_str("x");
19771983 child_scope->node->data.block.name = label_name;
......@@ -1998,7 +2004,7 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra
19982004 trans_create_node_symbol(c, ref_var_name));
19992005 child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, deref_expr));
20002006
2001 return child_scope->node;
2007 return trans_create_node_grouped_expr(c, child_scope->node);
20022008}
20032009
20042010static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransScope *scope, const clang::UnaryOperator *stmt) {
test/translate_c.zig+50-50
......@@ -643,11 +643,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
643643 \\pub export fn max(a: c_int) void {
644644 \\ var b: c_int = undefined;
645645 \\ var c: c_int = undefined;
646 \\ c = x: {
646 \\ c = (x: {
647647 \\ const _tmp = a;
648648 \\ b = _tmp;
649649 \\ break :x _tmp;
650 \\ };
650 \\ });
651651 \\}
652652 );
653653
......@@ -820,46 +820,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
820820 ,
821821 \\pub export fn foo() void {
822822 \\ var a: c_int = 0;
823 \\ a += x: {
823 \\ a += (x: {
824824 \\ const _ref = &a;
825825 \\ _ref.* = (_ref.* + 1);
826826 \\ break :x _ref.*;
827 \\ };
828 \\ a -= x: {
827 \\ });
828 \\ a -= (x: {
829829 \\ const _ref = &a;
830830 \\ _ref.* = (_ref.* - 1);
831831 \\ break :x _ref.*;
832 \\ };
833 \\ a *= x: {
832 \\ });
833 \\ a *= (x: {
834834 \\ const _ref = &a;
835835 \\ _ref.* = (_ref.* * 1);
836836 \\ break :x _ref.*;
837 \\ };
838 \\ a &= x: {
837 \\ });
838 \\ a &= (x: {
839839 \\ const _ref = &a;
840840 \\ _ref.* = (_ref.* & 1);
841841 \\ break :x _ref.*;
842 \\ };
843 \\ a |= x: {
842 \\ });
843 \\ a |= (x: {
844844 \\ const _ref = &a;
845845 \\ _ref.* = (_ref.* | 1);
846846 \\ break :x _ref.*;
847 \\ };
848 \\ a ^= x: {
847 \\ });
848 \\ a ^= (x: {
849849 \\ const _ref = &a;
850850 \\ _ref.* = (_ref.* ^ 1);
851851 \\ break :x _ref.*;
852 \\ };
853 \\ a >>= @import("std").math.Log2Int(c_int)(x: {
852 \\ });
853 \\ a >>= @import("std").math.Log2Int(c_int)((x: {
854854 \\ const _ref = &a;
855855 \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_int)(1));
856856 \\ break :x _ref.*;
857 \\ });
858 \\ a <<= @import("std").math.Log2Int(c_int)(x: {
857 \\ }));
858 \\ a <<= @import("std").math.Log2Int(c_int)((x: {
859859 \\ const _ref = &a;
860860 \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_int)(1));
861861 \\ break :x _ref.*;
862 \\ });
862 \\ }));
863863 \\}
864864 );
865865
......@@ -878,46 +878,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
878878 ,
879879 \\pub export fn foo() void {
880880 \\ var a: c_uint = c_uint(0);
881 \\ a +%= x: {
881 \\ a +%= (x: {
882882 \\ const _ref = &a;
883883 \\ _ref.* = (_ref.* +% c_uint(1));
884884 \\ break :x _ref.*;
885 \\ };
886 \\ a -%= x: {
885 \\ });
886 \\ a -%= (x: {
887887 \\ const _ref = &a;
888888 \\ _ref.* = (_ref.* -% c_uint(1));
889889 \\ break :x _ref.*;
890 \\ };
891 \\ a *%= x: {
890 \\ });
891 \\ a *%= (x: {
892892 \\ const _ref = &a;
893893 \\ _ref.* = (_ref.* *% c_uint(1));
894894 \\ break :x _ref.*;
895 \\ };
896 \\ a &= x: {
895 \\ });
896 \\ a &= (x: {
897897 \\ const _ref = &a;
898898 \\ _ref.* = (_ref.* & c_uint(1));
899899 \\ break :x _ref.*;
900 \\ };
901 \\ a |= x: {
900 \\ });
901 \\ a |= (x: {
902902 \\ const _ref = &a;
903903 \\ _ref.* = (_ref.* | c_uint(1));
904904 \\ break :x _ref.*;
905 \\ };
906 \\ a ^= x: {
905 \\ });
906 \\ a ^= (x: {
907907 \\ const _ref = &a;
908908 \\ _ref.* = (_ref.* ^ c_uint(1));
909909 \\ break :x _ref.*;
910 \\ };
911 \\ a >>= @import("std").math.Log2Int(c_uint)(x: {
910 \\ });
911 \\ a >>= @import("std").math.Log2Int(c_uint)((x: {
912912 \\ const _ref = &a;
913913 \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_uint)(1));
914914 \\ break :x _ref.*;
915 \\ });
916 \\ a <<= @import("std").math.Log2Int(c_uint)(x: {
915 \\ }));
916 \\ a <<= @import("std").math.Log2Int(c_uint)((x: {
917917 \\ const _ref = &a;
918918 \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_uint)(1));
919919 \\ break :x _ref.*;
920 \\ });
920 \\ }));
921921 \\}
922922 );
923923
......@@ -953,30 +953,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
953953 \\ i -= 1;
954954 \\ u +%= 1;
955955 \\ u -%= 1;
956 \\ i = x: {
956 \\ i = (x: {
957957 \\ const _ref = &i;
958958 \\ const _tmp = _ref.*;
959959 \\ _ref.* += 1;
960960 \\ break :x _tmp;
961 \\ };
962 \\ i = x: {
961 \\ });
962 \\ i = (x: {
963963 \\ const _ref = &i;
964964 \\ const _tmp = _ref.*;
965965 \\ _ref.* -= 1;
966966 \\ break :x _tmp;
967 \\ };
968 \\ u = x: {
967 \\ });
968 \\ u = (x: {
969969 \\ const _ref = &u;
970970 \\ const _tmp = _ref.*;
971971 \\ _ref.* +%= 1;
972972 \\ break :x _tmp;
973 \\ };
974 \\ u = x: {
973 \\ });
974 \\ u = (x: {
975975 \\ const _ref = &u;
976976 \\ const _tmp = _ref.*;
977977 \\ _ref.* -%= 1;
978978 \\ break :x _tmp;
979 \\ };
979 \\ });
980980 \\}
981981 );
982982
......@@ -1001,26 +1001,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
10011001 \\ i -= 1;
10021002 \\ u +%= 1;
10031003 \\ u -%= 1;
1004 \\ i = x: {
1004 \\ i = (x: {
10051005 \\ const _ref = &i;
10061006 \\ _ref.* += 1;
10071007 \\ break :x _ref.*;
1008 \\ };
1009 \\ i = x: {
1008 \\ });
1009 \\ i = (x: {
10101010 \\ const _ref = &i;
10111011 \\ _ref.* -= 1;
10121012 \\ break :x _ref.*;
1013 \\ };
1014 \\ u = x: {
1013 \\ });
1014 \\ u = (x: {
10151015 \\ const _ref = &u;
10161016 \\ _ref.* +%= 1;
10171017 \\ break :x _ref.*;
1018 \\ };
1019 \\ u = x: {
1018 \\ });
1019 \\ u = (x: {
10201020 \\ const _ref = &u;
10211021 \\ _ref.* -%= 1;
10221022 \\ break :x _ref.*;
1023 \\ };
1023 \\ });
10241024 \\}
10251025 );
10261026