authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-15 11:19:21-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-03-15 11:19:21-04:00
log4090fe81f600afa290de5bf06a287d5fab2ea9dc
tree5e1e65cd8d783e8edc0a94548c1b1b896a4db61f
parentb1c8c7973370caf638a4a278395616848c02b038
parent3ef9b899e4ea24af415381bf876c281d5ca9574c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2068 from ziglang/workaround-for-2043

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...@@ -446,6 +446,12 @@ static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, AstNode *r
446 return fn_def;446 return fn_def;
447}447}
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
449static AstNode *get_global(Context *c, Buf *name) {455static AstNode *get_global(Context *c, Buf *name) {
450 {456 {
451 auto entry = c->global_table.maybe_get(name);457 auto entry = c->global_table.maybe_get(name);
...@@ -1314,11 +1320,11 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco...@@ -1314,11 +1320,11 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco
1314 } else {1320 } else {
1315 // worst case1321 // worst case
1316 // c: lhs = rhs1322 // c: lhs = rhs
1317 // zig: x: {1323 // zig: (x: {
1318 // zig: const _tmp = rhs;1324 // zig: const _tmp = rhs;
1319 // zig: lhs = _tmp;1325 // zig: lhs = _tmp;
1320 // zig: break :x _tmp1326 // zig: break :x _tmp
1321 // zig: }1327 // zig: })
13221328
1323 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);1329 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
1324 Buf *label_name = buf_create_from_str("x");1330 Buf *label_name = buf_create_from_str("x");
...@@ -1343,7 +1349,7 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco...@@ -1343,7 +1349,7 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco
1343 AstNode *tmp_symbol_node = trans_create_node_symbol(c, tmp_var_name);1349 AstNode *tmp_symbol_node = trans_create_node_symbol(c, tmp_var_name);
1344 child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, tmp_symbol_node));1350 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);
1347 }1353 }
1348}1354}
13491355
...@@ -1499,11 +1505,11 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result...@@ -1499,11 +1505,11 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
1499 } else {1505 } else {
1500 // need more complexity. worst case, this looks like this:1506 // need more complexity. worst case, this looks like this:
1501 // c: lhs >>= rhs1507 // c: lhs >>= rhs
1502 // zig: x: {1508 // zig: (x: {
1503 // zig: const _ref = &lhs;1509 // zig: const _ref = &lhs;
1504 // zig: *_ref = result_type(operation_type(*_ref) >> u5(rhs));1510 // zig: *_ref = result_type(operation_type(*_ref) >> u5(rhs));
1505 // zig: break :x *_ref1511 // zig: break :x *_ref
1506 // zig: }1512 // zig: })
1507 // where u5 is the appropriate type1513 // where u5 is the appropriate type
15081514
1509 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);1515 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
...@@ -1556,7 +1562,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result...@@ -1556,7 +1562,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
1556 trans_create_node_symbol(c, tmp_var_name))));1562 trans_create_node_symbol(c, tmp_var_name))));
1557 }1563 }
15581564
1559 return child_scope->node;1565 return trans_create_node_grouped_expr(c, child_scope->node);
1560 }1566 }
1561}1567}
15621568
...@@ -1574,11 +1580,11 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,...@@ -1574,11 +1580,11 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,
1574 } else {1580 } else {
1575 // need more complexity. worst case, this looks like this:1581 // need more complexity. worst case, this looks like this:
1576 // c: lhs += rhs1582 // c: lhs += rhs
1577 // zig: x: {1583 // zig: (x: {
1578 // zig: const _ref = &lhs;1584 // zig: const _ref = &lhs;
1579 // zig: *_ref = *_ref + rhs;1585 // zig: *_ref = *_ref + rhs;
1580 // zig: break :x *_ref1586 // zig: break :x *_ref
1581 // zig: }1587 // zig: })
15821588
1583 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);1589 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
1584 Buf *label_name = buf_create_from_str("x");1590 Buf *label_name = buf_create_from_str("x");
...@@ -1615,7 +1621,7 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,...@@ -1615,7 +1621,7 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,
1615 trans_create_node_ptr_deref(c,1621 trans_create_node_ptr_deref(c,
1616 trans_create_node_symbol(c, tmp_var_name))));1622 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);
1619 }1625 }
1620}1626}
16211627
...@@ -1911,12 +1917,12 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr...@@ -1911,12 +1917,12 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr
1911 }1917 }
1912 // worst case1918 // worst case
1913 // c: expr++1919 // c: expr++
1914 // zig: x: {1920 // zig: (x: {
1915 // zig: const _ref = &expr;1921 // zig: const _ref = &expr;
1916 // zig: const _tmp = *_ref;1922 // zig: const _tmp = *_ref;
1917 // zig: *_ref += 1;1923 // zig: *_ref += 1;
1918 // zig: break :x _tmp1924 // zig: break :x _tmp
1919 // zig: }1925 // zig: })
1920 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);1926 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
1921 Buf *label_name = buf_create_from_str("x");1927 Buf *label_name = buf_create_from_str("x");
1922 child_scope->node->data.block.name = label_name;1928 child_scope->node->data.block.name = label_name;
...@@ -1948,7 +1954,7 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr...@@ -1948,7 +1954,7 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr
1948 // break :x _tmp1954 // break :x _tmp
1949 child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, trans_create_node_symbol(c, tmp_var_name)));1955 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);
1952}1958}
19531959
1954static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, TransScope *scope,1960static 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...@@ -1967,11 +1973,11 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra
1967 }1973 }
1968 // worst case1974 // worst case
1969 // c: ++expr1975 // c: ++expr
1970 // zig: x: {1976 // zig: (x: {
1971 // zig: const _ref = &expr;1977 // zig: const _ref = &expr;
1972 // zig: *_ref += 1;1978 // zig: *_ref += 1;
1973 // zig: break :x *_ref1979 // zig: break :x *_ref
1974 // zig: }1980 // zig: })
1975 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);1981 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
1976 Buf *label_name = buf_create_from_str("x");1982 Buf *label_name = buf_create_from_str("x");
1977 child_scope->node->data.block.name = label_name;1983 child_scope->node->data.block.name = label_name;
...@@ -1998,7 +2004,7 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra...@@ -1998,7 +2004,7 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra
1998 trans_create_node_symbol(c, ref_var_name));2004 trans_create_node_symbol(c, ref_var_name));
1999 child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, deref_expr));2005 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);
2002}2008}
20032009
2004static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransScope *scope, const clang::UnaryOperator *stmt) {2010static 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 {...@@ -643,11 +643,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
643 \\pub export fn max(a: c_int) void {643 \\pub export fn max(a: c_int) void {
644 \\ var b: c_int = undefined;644 \\ var b: c_int = undefined;
645 \\ var c: c_int = undefined;645 \\ var c: c_int = undefined;
646 \\ c = x: {646 \\ c = (x: {
647 \\ const _tmp = a;647 \\ const _tmp = a;
648 \\ b = _tmp;648 \\ b = _tmp;
649 \\ break :x _tmp;649 \\ break :x _tmp;
650 \\ };650 \\ });
651 \\}651 \\}
652 );652 );
653653
...@@ -820,46 +820,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -820,46 +820,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
820 ,820 ,
821 \\pub export fn foo() void {821 \\pub export fn foo() void {
822 \\ var a: c_int = 0;822 \\ var a: c_int = 0;
823 \\ a += x: {823 \\ a += (x: {
824 \\ const _ref = &a;824 \\ const _ref = &a;
825 \\ _ref.* = (_ref.* + 1);825 \\ _ref.* = (_ref.* + 1);
826 \\ break :x _ref.*;826 \\ break :x _ref.*;
827 \\ };827 \\ });
828 \\ a -= x: {828 \\ a -= (x: {
829 \\ const _ref = &a;829 \\ const _ref = &a;
830 \\ _ref.* = (_ref.* - 1);830 \\ _ref.* = (_ref.* - 1);
831 \\ break :x _ref.*;831 \\ break :x _ref.*;
832 \\ };832 \\ });
833 \\ a *= x: {833 \\ a *= (x: {
834 \\ const _ref = &a;834 \\ const _ref = &a;
835 \\ _ref.* = (_ref.* * 1);835 \\ _ref.* = (_ref.* * 1);
836 \\ break :x _ref.*;836 \\ break :x _ref.*;
837 \\ };837 \\ });
838 \\ a &= x: {838 \\ a &= (x: {
839 \\ const _ref = &a;839 \\ const _ref = &a;
840 \\ _ref.* = (_ref.* & 1);840 \\ _ref.* = (_ref.* & 1);
841 \\ break :x _ref.*;841 \\ break :x _ref.*;
842 \\ };842 \\ });
843 \\ a |= x: {843 \\ a |= (x: {
844 \\ const _ref = &a;844 \\ const _ref = &a;
845 \\ _ref.* = (_ref.* | 1);845 \\ _ref.* = (_ref.* | 1);
846 \\ break :x _ref.*;846 \\ break :x _ref.*;
847 \\ };847 \\ });
848 \\ a ^= x: {848 \\ a ^= (x: {
849 \\ const _ref = &a;849 \\ const _ref = &a;
850 \\ _ref.* = (_ref.* ^ 1);850 \\ _ref.* = (_ref.* ^ 1);
851 \\ break :x _ref.*;851 \\ break :x _ref.*;
852 \\ };852 \\ });
853 \\ a >>= @import("std").math.Log2Int(c_int)(x: {853 \\ a >>= @import("std").math.Log2Int(c_int)((x: {
854 \\ const _ref = &a;854 \\ const _ref = &a;
855 \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_int)(1));855 \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_int)(1));
856 \\ break :x _ref.*;856 \\ break :x _ref.*;
857 \\ });857 \\ }));
858 \\ a <<= @import("std").math.Log2Int(c_int)(x: {858 \\ a <<= @import("std").math.Log2Int(c_int)((x: {
859 \\ const _ref = &a;859 \\ const _ref = &a;
860 \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_int)(1));860 \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_int)(1));
861 \\ break :x _ref.*;861 \\ break :x _ref.*;
862 \\ });862 \\ }));
863 \\}863 \\}
864 );864 );
865865
...@@ -878,46 +878,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -878,46 +878,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
878 ,878 ,
879 \\pub export fn foo() void {879 \\pub export fn foo() void {
880 \\ var a: c_uint = c_uint(0);880 \\ var a: c_uint = c_uint(0);
881 \\ a +%= x: {881 \\ a +%= (x: {
882 \\ const _ref = &a;882 \\ const _ref = &a;
883 \\ _ref.* = (_ref.* +% c_uint(1));883 \\ _ref.* = (_ref.* +% c_uint(1));
884 \\ break :x _ref.*;884 \\ break :x _ref.*;
885 \\ };885 \\ });
886 \\ a -%= x: {886 \\ a -%= (x: {
887 \\ const _ref = &a;887 \\ const _ref = &a;
888 \\ _ref.* = (_ref.* -% c_uint(1));888 \\ _ref.* = (_ref.* -% c_uint(1));
889 \\ break :x _ref.*;889 \\ break :x _ref.*;
890 \\ };890 \\ });
891 \\ a *%= x: {891 \\ a *%= (x: {
892 \\ const _ref = &a;892 \\ const _ref = &a;
893 \\ _ref.* = (_ref.* *% c_uint(1));893 \\ _ref.* = (_ref.* *% c_uint(1));
894 \\ break :x _ref.*;894 \\ break :x _ref.*;
895 \\ };895 \\ });
896 \\ a &= x: {896 \\ a &= (x: {
897 \\ const _ref = &a;897 \\ const _ref = &a;
898 \\ _ref.* = (_ref.* & c_uint(1));898 \\ _ref.* = (_ref.* & c_uint(1));
899 \\ break :x _ref.*;899 \\ break :x _ref.*;
900 \\ };900 \\ });
901 \\ a |= x: {901 \\ a |= (x: {
902 \\ const _ref = &a;902 \\ const _ref = &a;
903 \\ _ref.* = (_ref.* | c_uint(1));903 \\ _ref.* = (_ref.* | c_uint(1));
904 \\ break :x _ref.*;904 \\ break :x _ref.*;
905 \\ };905 \\ });
906 \\ a ^= x: {906 \\ a ^= (x: {
907 \\ const _ref = &a;907 \\ const _ref = &a;
908 \\ _ref.* = (_ref.* ^ c_uint(1));908 \\ _ref.* = (_ref.* ^ c_uint(1));
909 \\ break :x _ref.*;909 \\ break :x _ref.*;
910 \\ };910 \\ });
911 \\ a >>= @import("std").math.Log2Int(c_uint)(x: {911 \\ a >>= @import("std").math.Log2Int(c_uint)((x: {
912 \\ const _ref = &a;912 \\ const _ref = &a;
913 \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_uint)(1));913 \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_uint)(1));
914 \\ break :x _ref.*;914 \\ break :x _ref.*;
915 \\ });915 \\ }));
916 \\ a <<= @import("std").math.Log2Int(c_uint)(x: {916 \\ a <<= @import("std").math.Log2Int(c_uint)((x: {
917 \\ const _ref = &a;917 \\ const _ref = &a;
918 \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_uint)(1));918 \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_uint)(1));
919 \\ break :x _ref.*;919 \\ break :x _ref.*;
920 \\ });920 \\ }));
921 \\}921 \\}
922 );922 );
923923
...@@ -953,30 +953,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -953,30 +953,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
953 \\ i -= 1;953 \\ i -= 1;
954 \\ u +%= 1;954 \\ u +%= 1;
955 \\ u -%= 1;955 \\ u -%= 1;
956 \\ i = x: {956 \\ i = (x: {
957 \\ const _ref = &i;957 \\ const _ref = &i;
958 \\ const _tmp = _ref.*;958 \\ const _tmp = _ref.*;
959 \\ _ref.* += 1;959 \\ _ref.* += 1;
960 \\ break :x _tmp;960 \\ break :x _tmp;
961 \\ };961 \\ });
962 \\ i = x: {962 \\ i = (x: {
963 \\ const _ref = &i;963 \\ const _ref = &i;
964 \\ const _tmp = _ref.*;964 \\ const _tmp = _ref.*;
965 \\ _ref.* -= 1;965 \\ _ref.* -= 1;
966 \\ break :x _tmp;966 \\ break :x _tmp;
967 \\ };967 \\ });
968 \\ u = x: {968 \\ u = (x: {
969 \\ const _ref = &u;969 \\ const _ref = &u;
970 \\ const _tmp = _ref.*;970 \\ const _tmp = _ref.*;
971 \\ _ref.* +%= 1;971 \\ _ref.* +%= 1;
972 \\ break :x _tmp;972 \\ break :x _tmp;
973 \\ };973 \\ });
974 \\ u = x: {974 \\ u = (x: {
975 \\ const _ref = &u;975 \\ const _ref = &u;
976 \\ const _tmp = _ref.*;976 \\ const _tmp = _ref.*;
977 \\ _ref.* -%= 1;977 \\ _ref.* -%= 1;
978 \\ break :x _tmp;978 \\ break :x _tmp;
979 \\ };979 \\ });
980 \\}980 \\}
981 );981 );
982982
...@@ -1001,26 +1001,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1001,26 +1001,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1001 \\ i -= 1;1001 \\ i -= 1;
1002 \\ u +%= 1;1002 \\ u +%= 1;
1003 \\ u -%= 1;1003 \\ u -%= 1;
1004 \\ i = x: {1004 \\ i = (x: {
1005 \\ const _ref = &i;1005 \\ const _ref = &i;
1006 \\ _ref.* += 1;1006 \\ _ref.* += 1;
1007 \\ break :x _ref.*;1007 \\ break :x _ref.*;
1008 \\ };1008 \\ });
1009 \\ i = x: {1009 \\ i = (x: {
1010 \\ const _ref = &i;1010 \\ const _ref = &i;
1011 \\ _ref.* -= 1;1011 \\ _ref.* -= 1;
1012 \\ break :x _ref.*;1012 \\ break :x _ref.*;
1013 \\ };1013 \\ });
1014 \\ u = x: {1014 \\ u = (x: {
1015 \\ const _ref = &u;1015 \\ const _ref = &u;
1016 \\ _ref.* +%= 1;1016 \\ _ref.* +%= 1;
1017 \\ break :x _ref.*;1017 \\ break :x _ref.*;
1018 \\ };1018 \\ });
1019 \\ u = x: {1019 \\ u = (x: {
1020 \\ const _ref = &u;1020 \\ const _ref = &u;
1021 \\ _ref.* -%= 1;1021 \\ _ref.* -%= 1;
1022 \\ break :x _ref.*;1022 \\ break :x _ref.*;
1023 \\ };1023 \\ });
1024 \\}1024 \\}
1025 );1025 );
10261026