authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-16 15:45:33+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-16 16:40:43+02:00
log74e9d4ca820f9940b47ae658c41e7edb315681c8
treefc36ac23530b57b4a43eeaef636d67296acd8e86
parent78fba4e0213dc9bcc2274ef6c31023256a235301
signature Commit is signed but in an unrecognized format.

translate-c: get all translate-c tests passing


3 files changed, 281 insertions(+), 233 deletions(-)

src/translate_c.zig+10-4
...@@ -622,7 +622,7 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co...@@ -622,7 +622,7 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co
622 return; // Avoid processing this decl twice622 return; // Avoid processing this decl twice
623623
624 const is_pub = mangled_name == null;624 const is_pub = mangled_name == null;
625 const is_thread_local = var_decl.getTLSKind() != .None;625 const is_threadlocal = var_decl.getTLSKind() != .None;
626 const scope = &c.global_scope.base;626 const scope = &c.global_scope.base;
627627
628 // TODO https://github.com/ziglang/zig/issues/3756628 // TODO https://github.com/ziglang/zig/issues/3756
...@@ -706,6 +706,7 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co...@@ -706,6 +706,7 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co
706 .is_const = is_const,706 .is_const = is_const,
707 .is_extern = is_extern,707 .is_extern = is_extern,
708 .is_export = is_export,708 .is_export = is_export,
709 .is_threadlocal = is_threadlocal,
709 .linksection_string = linksection_string,710 .linksection_string = linksection_string,
710 .alignment = alignment,711 .alignment = alignment,
711 .name = checked_name,712 .name = checked_name,
...@@ -1307,6 +1308,7 @@ fn transDeclStmtOne(...@@ -1307,6 +1308,7 @@ fn transDeclStmtOne(
1307 .is_const = is_const,1308 .is_const = is_const,
1308 .is_extern = false,1309 .is_extern = false,
1309 .is_export = false,1310 .is_export = false,
1311 .is_threadlocal = false,
1310 .linksection_string = null,1312 .linksection_string = null,
1311 .alignment = null,1313 .alignment = null,
1312 .name = mangled_name,1314 .name = mangled_name,
...@@ -2886,11 +2888,11 @@ fn transCreateCompoundAssign(...@@ -2886,11 +2888,11 @@ fn transCreateCompoundAssign(
2886 if ((is_mod or is_div) and is_signed) {2888 if ((is_mod or is_div) and is_signed) {
2887 const rhs_node = try transExpr(c, &block_scope.base, rhs, .used);2889 const rhs_node = try transExpr(c, &block_scope.base, rhs, .used);
2888 const builtin = if (is_mod)2890 const builtin = if (is_mod)
2889 try Tag.rem.create(c.arena, .{ .lhs = lhs_node, .rhs = rhs_node })2891 try Tag.rem.create(c.arena, .{ .lhs = ref_node, .rhs = rhs_node })
2890 else2892 else
2891 try Tag.div_trunc.create(c.arena, .{ .lhs = lhs_node, .rhs = rhs_node });2893 try Tag.div_trunc.create(c.arena, .{ .lhs = ref_node, .rhs = rhs_node });
28922894
2893 const assign = try transCreateNodeInfixOp(c, &block_scope.base, .assign, lhs_node, builtin, .used);2895 const assign = try transCreateNodeInfixOp(c, &block_scope.base, .assign, ref_node, builtin, .used);
2894 try block_scope.statements.append(assign);2896 try block_scope.statements.append(assign);
2895 } else {2897 } else {
2896 var rhs_node = try transExpr(c, &block_scope.base, rhs, .used);2898 var rhs_node = try transExpr(c, &block_scope.base, rhs, .used);
...@@ -4794,6 +4796,10 @@ fn parseCPostfixExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {...@@ -4794,6 +4796,10 @@ fn parseCPostfixExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {
4794 .LBracket => {4796 .LBracket => {
4795 const index = try macroBoolToInt(c, try parseCExpr(c, m, scope));4797 const index = try macroBoolToInt(c, try parseCExpr(c, m, scope));
4796 node = try Tag.array_access.create(c.arena, .{ .lhs = node, .rhs = index });4798 node = try Tag.array_access.create(c.arena, .{ .lhs = node, .rhs = index });
4799 if (m.next().? != .RBracket) {
4800 try m.fail(c, "unable to translate C expr: expected ']'", .{});
4801 return error.ParseError;
4802 }
4797 },4803 },
4798 .LParen => {4804 .LParen => {
4799 var args = std.ArrayList(Node).init(c.gpa);4805 var args = std.ArrayList(Node).init(c.gpa);
src/translate_c/ast.zig+62-39
...@@ -458,6 +458,7 @@ pub const Payload = struct {...@@ -458,6 +458,7 @@ pub const Payload = struct {
458 is_const: bool,458 is_const: bool,
459 is_extern: bool,459 is_extern: bool,
460 is_export: bool,460 is_export: bool,
461 is_threadlocal: bool,
461 alignment: ?c_uint,462 alignment: ?c_uint,
462 linksection_string: ?[]const u8,463 linksection_string: ?[]const u8,
463 name: []const u8,464 name: []const u8,
...@@ -1164,42 +1165,42 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1164,42 +1165,42 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1164 },1165 },
1165 });1166 });
1166 },1167 },
1167 .add => return renderBinOp(c, node, .add, .plus, "+"),1168 .add => return renderBinOpGrouped(c, node, .add, .plus, "+"),
1168 .add_assign => return renderBinOp(c, node, .assign_add, .plus_equal, "+="),1169 .add_assign => return renderBinOp(c, node, .assign_add, .plus_equal, "+="),
1169 .add_wrap => return renderBinOp(c, node, .add_wrap, .plus_percent, "+%"),1170 .add_wrap => return renderBinOpGrouped(c, node, .add_wrap, .plus_percent, "+%"),
1170 .add_wrap_assign => return renderBinOp(c, node, .assign_add_wrap, .plus_percent_equal, "+%="),1171 .add_wrap_assign => return renderBinOp(c, node, .assign_add_wrap, .plus_percent_equal, "+%="),
1171 .sub => return renderBinOp(c, node, .sub, .minus, "-"),1172 .sub => return renderBinOpGrouped(c, node, .sub, .minus, "-"),
1172 .sub_assign => return renderBinOp(c, node, .assign_sub, .minus_equal, "-="),1173 .sub_assign => return renderBinOp(c, node, .assign_sub, .minus_equal, "-="),
1173 .sub_wrap => return renderBinOp(c, node, .sub_wrap, .minus_percent, "-%"),1174 .sub_wrap => return renderBinOpGrouped(c, node, .sub_wrap, .minus_percent, "-%"),
1174 .sub_wrap_assign => return renderBinOp(c, node, .assign_sub_wrap, .minus_percent_equal, "-%="),1175 .sub_wrap_assign => return renderBinOp(c, node, .assign_sub_wrap, .minus_percent_equal, "-%="),
1175 .mul => return renderBinOp(c, node, .mul, .asterisk, "*"),1176 .mul => return renderBinOpGrouped(c, node, .mul, .asterisk, "*"),
1176 .mul_assign => return renderBinOp(c, node, .assign_mul, .asterisk_equal, "*="),1177 .mul_assign => return renderBinOp(c, node, .assign_mul, .asterisk_equal, "*="),
1177 .mul_wrap => return renderBinOp(c, node, .mul_wrap, .asterisk_percent, "*="),1178 .mul_wrap => return renderBinOpGrouped(c, node, .mul_wrap, .asterisk_percent, "*%"),
1178 .mul_wrap_assign => return renderBinOp(c, node, .assign_mul_wrap, .asterisk_percent_equal, "*%="),1179 .mul_wrap_assign => return renderBinOp(c, node, .assign_mul_wrap, .asterisk_percent_equal, "*%="),
1179 .div => return renderBinOp(c, node, .div, .slash, "/"),1180 .div => return renderBinOpGrouped(c, node, .div, .slash, "/"),
1180 .div_assign => return renderBinOp(c, node, .assign_div, .slash_equal, "/="),1181 .div_assign => return renderBinOp(c, node, .assign_div, .slash_equal, "/="),
1181 .shl => return renderBinOp(c, node, .bit_shift_left, .angle_bracket_angle_bracket_left, "<<"),1182 .shl => return renderBinOpGrouped(c, node, .bit_shift_left, .angle_bracket_angle_bracket_left, "<<"),
1182 .shl_assign => return renderBinOp(c, node, .assign_bit_shift_left, .angle_bracket_angle_bracket_left_equal, "<<="),1183 .shl_assign => return renderBinOp(c, node, .assign_bit_shift_left, .angle_bracket_angle_bracket_left_equal, "<<="),
1183 .shr => return renderBinOp(c, node, .bit_shift_right, .angle_bracket_angle_bracket_right, ">>"),1184 .shr => return renderBinOpGrouped(c, node, .bit_shift_right, .angle_bracket_angle_bracket_right, ">>"),
1184 .shr_assign => return renderBinOp(c, node, .assign_bit_shift_right, .angle_bracket_angle_bracket_right_equal, ">>="),1185 .shr_assign => return renderBinOp(c, node, .assign_bit_shift_right, .angle_bracket_angle_bracket_right_equal, ">>="),
1185 .mod => return renderBinOp(c, node, .mod, .percent, "%"),1186 .mod => return renderBinOpGrouped(c, node, .mod, .percent, "%"),
1186 .mod_assign => return renderBinOp(c, node, .assign_mod, .percent_equal, "%="),1187 .mod_assign => return renderBinOp(c, node, .assign_mod, .percent_equal, "%="),
1187 .@"and" => return renderBinOp(c, node, .bool_and, .keyword_and, "and"),1188 .@"and" => return renderBinOpGrouped(c, node, .bool_and, .keyword_and, "and"),
1188 .@"or" => return renderBinOp(c, node, .bool_or, .keyword_or, "or"),1189 .@"or" => return renderBinOpGrouped(c, node, .bool_or, .keyword_or, "or"),
1189 .less_than => return renderBinOp(c, node, .less_than, .angle_bracket_left, "<"),1190 .less_than => return renderBinOpGrouped(c, node, .less_than, .angle_bracket_left, "<"),
1190 .less_than_equal => return renderBinOp(c, node, .less_or_equal, .angle_bracket_left_equal, "<="),1191 .less_than_equal => return renderBinOpGrouped(c, node, .less_or_equal, .angle_bracket_left_equal, "<="),
1191 .greater_than => return renderBinOp(c, node, .greater_than, .angle_bracket_right, ">="),1192 .greater_than => return renderBinOpGrouped(c, node, .greater_than, .angle_bracket_right, ">="),
1192 .greater_than_equal => return renderBinOp(c, node, .greater_or_equal, .angle_bracket_right_equal, ">="),1193 .greater_than_equal => return renderBinOpGrouped(c, node, .greater_or_equal, .angle_bracket_right_equal, ">="),
1193 .equal => return renderBinOp(c, node, .equal_equal, .equal_equal, "=="),1194 .equal => return renderBinOpGrouped(c, node, .equal_equal, .equal_equal, "=="),
1194 .not_equal => return renderBinOp(c, node, .bang_equal, .bang_equal, "!="),1195 .not_equal => return renderBinOpGrouped(c, node, .bang_equal, .bang_equal, "!="),
1195 .bit_and => return renderBinOp(c, node, .bit_and, .ampersand, "&"),1196 .bit_and => return renderBinOpGrouped(c, node, .bit_and, .ampersand, "&"),
1196 .bit_and_assign => return renderBinOp(c, node, .assign_bit_and, .ampersand_equal, "&="),1197 .bit_and_assign => return renderBinOp(c, node, .assign_bit_and, .ampersand_equal, "&="),
1197 .bit_or => return renderBinOp(c, node, .bit_or, .pipe, "|"),1198 .bit_or => return renderBinOpGrouped(c, node, .bit_or, .pipe, "|"),
1198 .bit_or_assign => return renderBinOp(c, node, .assign_bit_or, .pipe_equal, "|="),1199 .bit_or_assign => return renderBinOp(c, node, .assign_bit_or, .pipe_equal, "|="),
1199 .bit_xor => return renderBinOp(c, node, .bit_xor, .caret, "^"),1200 .bit_xor => return renderBinOpGrouped(c, node, .bit_xor, .caret, "^"),
1200 .bit_xor_assign => return renderBinOp(c, node, .assign_bit_xor, .caret_equal, "^="),1201 .bit_xor_assign => return renderBinOp(c, node, .assign_bit_xor, .caret_equal, "^="),
1201 .array_cat => return renderBinOp(c, node, .array_cat, .plus_plus, "++"),1202 .array_cat => return renderBinOp(c, node, .array_cat, .plus_plus, "++"),
1202 .ellipsis3 => return renderBinOp(c, node, .switch_range, .ellipsis3, "..."),1203 .ellipsis3 => return renderBinOpGrouped(c, node, .switch_range, .ellipsis3, "..."),
1203 .assign => return renderBinOp(c, node, .assign, .equal, "="),1204 .assign => return renderBinOp(c, node, .assign, .equal, "="),
1204 .empty_block => {1205 .empty_block => {
1205 const l_brace = try c.addToken(.l_brace, "{");1206 const l_brace = try c.addToken(.l_brace, "{");
...@@ -1222,7 +1223,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1222,7 +1223,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
12221223
1223 _ = try c.addToken(.r_brace, "}");1224 _ = try c.addToken(.r_brace, "}");
1224 return c.addNode(.{1225 return c.addNode(.{
1225 .tag = .block_two,1226 .tag = .block_two_semicolon,
1226 .main_token = l_brace,1227 .main_token = l_brace,
1227 .data = .{1228 .data = .{
1228 .lhs = stmt,1229 .lhs = stmt,
...@@ -1410,13 +1411,13 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1410,13 +1411,13 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1410 var cases = try c.gpa.alloc(NodeIndex, payload.cases.len);1411 var cases = try c.gpa.alloc(NodeIndex, payload.cases.len);
1411 defer c.gpa.free(cases);1412 defer c.gpa.free(cases);
1412 for (payload.cases) |case, i| {1413 for (payload.cases) |case, i| {
1413 if (i != 0) _ = try c.addToken(.comma, ",");
1414 cases[i] = try renderNode(c, case);1414 cases[i] = try renderNode(c, case);
1415 _ = try c.addToken(.comma, ",");
1415 }1416 }
1416 const span = try c.listToSpan(cases);1417 const span = try c.listToSpan(cases);
1417 _ = try c.addToken(.r_brace, "}");1418 _ = try c.addToken(.r_brace, "}");
1418 return c.addNode(.{1419 return c.addNode(.{
1419 .tag = .@"switch",1420 .tag = .switch_comma,
1420 .main_token = switch_tok,1421 .main_token = switch_tok,
1421 .data = .{1422 .data = .{
1422 .lhs = cond,1423 .lhs = cond,
...@@ -1623,9 +1624,10 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1623,9 +1624,10 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1623 const payload = node.castTag(.tuple).?.data;1624 const payload = node.castTag(.tuple).?.data;
1624 _ = try c.addToken(.period, ".");1625 _ = try c.addToken(.period, ".");
1625 const l_brace = try c.addToken(.l_brace, "{");1626 const l_brace = try c.addToken(.l_brace, "{");
1626 var inits = try c.gpa.alloc(NodeIndex, std.math.max(payload.len, 1));1627 var inits = try c.gpa.alloc(NodeIndex, std.math.max(payload.len, 2));
1627 defer c.gpa.free(inits);1628 defer c.gpa.free(inits);
1628 inits[0] = 0;1629 inits[0] = 0;
1630 inits[1] = 0;
1629 for (payload) |init, i| {1631 for (payload) |init, i| {
1630 if (i != 0) _ = try c.addToken(.comma, ",");1632 if (i != 0) _ = try c.addToken(.comma, ",");
1631 inits[i] = try renderNode(c, init);1633 inits[i] = try renderNode(c, init);
...@@ -1661,17 +1663,17 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1661,17 +1663,17 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1661 defer c.gpa.free(inits);1663 defer c.gpa.free(inits);
1662 inits[0] = 0;1664 inits[0] = 0;
1663 for (payload.inits) |init, i| {1665 for (payload.inits) |init, i| {
1664 if (i != 0) _ = try c.addToken(.comma, ",");
1665 _ = try c.addToken(.period, ".");1666 _ = try c.addToken(.period, ".");
1666 _ = try c.addIdentifier(init.name);1667 _ = try c.addIdentifier(init.name);
1667 _ = try c.addToken(.equal, "=");1668 _ = try c.addToken(.equal, "=");
1668 inits[i] = try renderNode(c, init.value);1669 inits[i] = try renderNode(c, init.value);
1670 _ = try c.addToken(.comma, ",");
1669 }1671 }
1670 _ = try c.addToken(.r_brace, "}");1672 _ = try c.addToken(.r_brace, "}");
16711673
1672 if (payload.inits.len < 2) {1674 if (payload.inits.len < 2) {
1673 return c.addNode(.{1675 return c.addNode(.{
1674 .tag = .struct_init_one,1676 .tag = .struct_init_one_comma,
1675 .main_token = l_brace,1677 .main_token = l_brace,
1676 .data = .{1678 .data = .{
1677 .lhs = lhs,1679 .lhs = lhs,
...@@ -1681,7 +1683,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1681,7 +1683,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1681 } else {1683 } else {
1682 const span = try c.listToSpan(inits);1684 const span = try c.listToSpan(inits);
1683 return c.addNode(.{1685 return c.addNode(.{
1684 .tag = .struct_init,1686 .tag = .struct_init_comma,
1685 .main_token = l_brace,1687 .main_token = l_brace,
1686 .data = .{1688 .data = .{
1687 .lhs = lhs,1689 .lhs = lhs,
...@@ -1791,13 +1793,13 @@ fn renderArrayInit(c: *Context, lhs: NodeIndex, inits: []const Node) !NodeIndex...@@ -1791,13 +1793,13 @@ fn renderArrayInit(c: *Context, lhs: NodeIndex, inits: []const Node) !NodeIndex
1791 defer c.gpa.free(rendered);1793 defer c.gpa.free(rendered);
1792 rendered[0] = 0;1794 rendered[0] = 0;
1793 for (inits) |init, i| {1795 for (inits) |init, i| {
1794 if (i != 0) _ = try c.addToken(.comma, ",");
1795 rendered[i] = try renderNode(c, init);1796 rendered[i] = try renderNode(c, init);
1797 _ = try c.addToken(.comma, ",");
1796 }1798 }
1797 _ = try c.addToken(.r_brace, "}");1799 _ = try c.addToken(.r_brace, "}");
1798 if (inits.len < 2) {1800 if (inits.len < 2) {
1799 return c.addNode(.{1801 return c.addNode(.{
1800 .tag = .array_init_one,1802 .tag = .array_init_one_comma,
1801 .main_token = l_brace,1803 .main_token = l_brace,
1802 .data = .{1804 .data = .{
1803 .lhs = lhs,1805 .lhs = lhs,
...@@ -1807,7 +1809,7 @@ fn renderArrayInit(c: *Context, lhs: NodeIndex, inits: []const Node) !NodeIndex...@@ -1807,7 +1809,7 @@ fn renderArrayInit(c: *Context, lhs: NodeIndex, inits: []const Node) !NodeIndex
1807 } else {1809 } else {
1808 const span = try c.listToSpan(rendered);1810 const span = try c.listToSpan(rendered);
1809 return c.addNode(.{1811 return c.addNode(.{
1810 .tag = .array_init,1812 .tag = .array_init_comma,
1811 .main_token = l_brace,1813 .main_token = l_brace,
1812 .data = .{1814 .data = .{
1813 .lhs = lhs,1815 .lhs = lhs,
...@@ -1842,25 +1844,32 @@ fn renderArrayType(c: *Context, len: usize, elem_type: Node) !NodeIndex {...@@ -1842,25 +1844,32 @@ fn renderArrayType(c: *Context, len: usize, elem_type: Node) !NodeIndex {
1842fn addSemicolonIfNeeded(c: *Context, node: Node) !void {1844fn addSemicolonIfNeeded(c: *Context, node: Node) !void {
1843 switch (node.tag()) {1845 switch (node.tag()) {
1844 .warning => unreachable,1846 .warning => unreachable,
1845 .var_decl, .var_simple, .arg_redecl, .alias, .enum_redecl, .block, .empty_block, .@"switch" => {},1847 .var_decl, .var_simple, .arg_redecl, .alias, .enum_redecl, .block, .empty_block, .block_single, .@"switch" => {},
1846 .while_true => {1848 .while_true => {
1847 const payload = node.castTag(.while_true).?.data;1849 const payload = node.castTag(.while_true).?.data;
1848 return addSemicolonIfNeeded(c, payload);1850 return addSemicolonIfNotBlock(c, payload);
1849 },1851 },
1850 .@"while" => {1852 .@"while" => {
1851 const payload = node.castTag(.@"while").?.data;1853 const payload = node.castTag(.@"while").?.data;
1852 return addSemicolonIfNeeded(c, payload.body);1854 return addSemicolonIfNotBlock(c, payload.body);
1853 },1855 },
1854 .@"if" => {1856 .@"if" => {
1855 const payload = node.castTag(.@"if").?.data;1857 const payload = node.castTag(.@"if").?.data;
1856 if (payload.@"else") |some|1858 if (payload.@"else") |some|
1857 return addSemicolonIfNeeded(c, some);1859 return addSemicolonIfNotBlock(c, some);
1858 return addSemicolonIfNeeded(c, payload.then);1860 return addSemicolonIfNotBlock(c, payload.then);
1859 },1861 },
1860 else => _ = try c.addToken(.semicolon, ";"),1862 else => _ = try c.addToken(.semicolon, ";"),
1861 }1863 }
1862}1864}
18631865
1866fn addSemicolonIfNotBlock(c: *Context, node: Node) !void {
1867 switch (node.tag()) {
1868 .block, .empty_block, .block_single, => {},
1869 else => _ = try c.addToken(.semicolon, ";"),
1870 }
1871}
1872
1864fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {1873fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
1865 switch (node.tag()) {1874 switch (node.tag()) {
1866 .null_literal,1875 .null_literal,
...@@ -1918,6 +1927,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {...@@ -1918,6 +1927,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
1918 .func,1927 .func,
1919 .call,1928 .call,
1920 .array_type,1929 .array_type,
1930 .bool_to_int,
1921 => {1931 => {
1922 // no grouping needed1932 // no grouping needed
1923 return renderNode(c, node);1933 return renderNode(c, node);
...@@ -1926,7 +1936,6 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {...@@ -1926,7 +1936,6 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
1926 .opaque_literal,1936 .opaque_literal,
1927 .empty_array,1937 .empty_array,
1928 .block_single,1938 .block_single,
1929 .bool_to_int,
1930 .add,1939 .add,
1931 .add_wrap,1940 .add_wrap,
1932 .sub,1941 .sub,
...@@ -2022,7 +2031,7 @@ fn renderPrefixOp(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: T...@@ -2022,7 +2031,7 @@ fn renderPrefixOp(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: T
2022 });2031 });
2023}2032}
20242033
2025fn renderBinOp(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: TokenTag, bytes: []const u8) !NodeIndex {2034fn renderBinOpGrouped(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: TokenTag, bytes: []const u8) !NodeIndex {
2026 const payload = @fieldParentPtr(Payload.BinOp, "base", node.ptr_otherwise).data;2035 const payload = @fieldParentPtr(Payload.BinOp, "base", node.ptr_otherwise).data;
2027 const lhs = try renderNodeGrouped(c, payload.lhs);2036 const lhs = try renderNodeGrouped(c, payload.lhs);
2028 return c.addNode(.{2037 return c.addNode(.{
...@@ -2035,6 +2044,19 @@ fn renderBinOp(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: Toke...@@ -2035,6 +2044,19 @@ fn renderBinOp(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: Toke
2035 });2044 });
2036}2045}
20372046
2047fn renderBinOp(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: TokenTag, bytes: []const u8) !NodeIndex {
2048 const payload = @fieldParentPtr(Payload.BinOp, "base", node.ptr_otherwise).data;
2049 const lhs = try renderNode(c, payload.lhs);
2050 return c.addNode(.{
2051 .tag = tag,
2052 .main_token = try c.addToken(tok_tag, bytes),
2053 .data = .{
2054 .lhs = lhs,
2055 .rhs = try renderNode(c, payload.rhs),
2056 },
2057 });
2058}
2059
2038fn renderStdImport(c: *Context, first: []const u8, second: []const u8) !NodeIndex {2060fn renderStdImport(c: *Context, first: []const u8, second: []const u8) !NodeIndex {
2039 const import_tok = try c.addToken(.builtin, "@import");2061 const import_tok = try c.addToken(.builtin, "@import");
2040 _ = try c.addToken(.l_paren, "(");2062 _ = try c.addToken(.l_paren, "(");
...@@ -2143,6 +2165,7 @@ fn renderVar(c: *Context, node: Node) !NodeIndex {...@@ -2143,6 +2165,7 @@ fn renderVar(c: *Context, node: Node) !NodeIndex {
2143 if (payload.is_pub) _ = try c.addToken(.keyword_pub, "pub");2165 if (payload.is_pub) _ = try c.addToken(.keyword_pub, "pub");
2144 if (payload.is_extern) _ = try c.addToken(.keyword_extern, "extern");2166 if (payload.is_extern) _ = try c.addToken(.keyword_extern, "extern");
2145 if (payload.is_export) _ = try c.addToken(.keyword_export, "export");2167 if (payload.is_export) _ = try c.addToken(.keyword_export, "export");
2168 if (payload.is_threadlocal) _ = try c.addToken(.keyword_threadlocal, "threadlocal");
2146 const mut_tok = if (payload.is_const)2169 const mut_tok = if (payload.is_const)
2147 try c.addToken(.keyword_const, "const")2170 try c.addToken(.keyword_const, "const")
2148 else2171 else
test/translate_c.zig+209-190
...@@ -3,6 +3,14 @@ const std = @import("std");...@@ -3,6 +3,14 @@ const std = @import("std");
3const CrossTarget = std.zig.CrossTarget;3const CrossTarget = std.zig.CrossTarget;
44
5pub fn addCases(cases: *tests.TranslateCContext) void {5pub fn addCases(cases: *tests.TranslateCContext) void {
6 cases.add("use cast param as macro fn return type",
7 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((u32)(x) + SYS_BASE_CACHED)
8 , &[_][]const u8{
9 \\pub fn MEM_PHYSICAL_TO_K0(x: anytype) callconv(.Inline) ?*c_void {
10 \\ return @import("std").meta.cast(?*c_void, @import("std").meta.cast(u32, x) + SYS_BASE_CACHED);
11 \\}
12 });
13
6 cases.add("variadic function demoted to prototype",14 cases.add("variadic function demoted to prototype",
7 \\int foo(int bar, ...) {15 \\int foo(int bar, ...) {
8 \\ return 1;16 \\ return 1;
...@@ -21,11 +29,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -21,11 +29,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21 \\ Foo *bar;29 \\ Foo *bar;
22 \\} Bar;30 \\} Bar;
23 , &[_][]const u8{31 , &[_][]const u8{
24 \\const struct_unnamed_1 = //32 \\source.h:1:9: warning: struct demoted to opaque type - unable to translate type of field foo
25 ,33 \\const struct_unnamed_1 = opaque {};
26 \\warning: unsupported type: 'Atomic'
27 \\ opaque {}; //
28 ,
29 \\pub const Foo = struct_unnamed_1;34 \\pub const Foo = struct_unnamed_1;
30 \\const struct_unnamed_2 = extern struct {35 \\const struct_unnamed_2 = extern struct {
31 \\ bar: ?*Foo,36 \\ bar: ?*Foo,
...@@ -43,8 +48,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -43,8 +48,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
43 ,48 ,
44 \\pub const VALUE = ((((1 + (2 * 3)) + (4 * 5)) + 6) << 7) | @boolToInt(8 == 9);49 \\pub const VALUE = ((((1 + (2 * 3)) + (4 * 5)) + 6) << 7) | @boolToInt(8 == 9);
45 ,50 ,
46 \\pub fn _AL_READ3BYTES(p: anytype) callconv(.Inline) @TypeOf(((@import("std").meta.cast([*c]u8, p)).* | (((@import("std").meta.cast([*c]u8, p)) + 1).* << 8)) | (((@import("std").meta.cast([*c]u8, p)) + 2).* << 16)) {51 \\pub fn _AL_READ3BYTES(p: anytype) callconv(.Inline) @TypeOf((@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + 1).* << 8)) | ((@import("std").meta.cast([*c]u8, p) + 2).* << 16)) {
47 \\ return ((@import("std").meta.cast([*c]u8, p)).* | (((@import("std").meta.cast([*c]u8, p)) + 1).* << 8)) | (((@import("std").meta.cast([*c]u8, p)) + 2).* << 16);52 \\ return (@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + 1).* << 8)) | ((@import("std").meta.cast([*c]u8, p) + 2).* << 16);
48 \\}53 \\}
49 });54 });
5055
...@@ -107,7 +112,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -107,7 +112,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
107 \\ int i1;112 \\ int i1;
108 \\} boom_t;113 \\} boom_t;
109 \\#define FOO ((boom_t){1})114 \\#define FOO ((boom_t){1})
110 , &[_][]const u8{ // TODO properly translate this115 , &[_][]const u8{
111 \\pub const struct_Color = extern struct {116 \\pub const struct_Color = extern struct {
112 \\ r: u8,117 \\ r: u8,
113 \\ g: u8,118 \\ g: u8,
...@@ -127,7 +132,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -127,7 +132,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
127 \\};132 \\};
128 \\pub const boom_t = struct_boom_t;133 \\pub const boom_t = struct_boom_t;
129 ,134 ,
130 \\pub const FOO = @import("std").mem.zeroInit(boom_t, .{ 1 });135 \\pub const FOO = @import("std").mem.zeroInit(boom_t, .{1});
131 });136 });
132137
133 cases.add("complex switch",138 cases.add("complex switch",
...@@ -142,14 +147,34 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -142,14 +147,34 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
142 \\ }147 \\ }
143 \\}148 \\}
144 , &[_][]const u8{ // TODO properly translate this149 , &[_][]const u8{ // TODO properly translate this
145 \\pub const main = @compileError("unable to translate function");150 \\pub export fn main() c_int {
151 \\ var i: c_int = 2;
152 \\ @"switch": {
153 \\ case_1: {
154 \\ case: {
155 \\ switch (i) {
156 \\ @as(c_int, 0) => break :case,
157 \\ @as(c_int, 2) => break :case_1,
158 \\ else => break :@"switch",
159 \\ }
160 \\ }
161 \\ }
162 \\ {
163 \\ {
164 \\ i += @as(c_int, 2);
165 \\ }
166 \\ i += @as(c_int, 1);
167 \\ }
168 \\ }
169 \\ return 0;
170 \\}
146 });171 });
147172
148 cases.add("correct semicolon after infixop",173 cases.add("correct semicolon after infixop",
149 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)174 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
150 , &[_][]const u8{175 , &[_][]const u8{
151 \\pub fn __ferror_unlocked_body(_fp: anytype) callconv(.Inline) @TypeOf(((_fp.*._flags) & _IO_ERR_SEEN) != 0) {176 \\pub fn __ferror_unlocked_body(_fp: anytype) callconv(.Inline) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != 0) {
152 \\ return ((_fp.*._flags) & _IO_ERR_SEEN) != 0;177 \\ return (_fp.*._flags & _IO_ERR_SEEN) != 0;
153 \\}178 \\}
154 });179 });
155180
...@@ -194,7 +219,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -194,7 +219,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
194 \\ while (false) while (false) {};219 \\ while (false) while (false) {};
195 \\ while (true) while (false) {};220 \\ while (true) while (false) {};
196 \\ while (true) while (true) {221 \\ while (true) while (true) {
197 \\ if (!false) break;222 \\ break;
198 \\ };223 \\ };
199 \\}224 \\}
200 });225 });
...@@ -245,15 +270,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -245,15 +270,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
245 \\ volatile _Atomic int abufused[12];270 \\ volatile _Atomic int abufused[12];
246 \\};271 \\};
247 , &[_][]const u8{272 , &[_][]const u8{
248 \\pub const struct_arcan_shmif_page = //273 \\source.h:4:8: warning: struct demoted to opaque type - unable to translate type of field abufused
249 ,274 \\pub const struct_arcan_shmif_page = opaque {};
250 \\warning: unsupported type: 'Atomic'
251 \\ opaque {}; //
252 ,
253 \\ warning: struct demoted to opaque type - unable to translate type of field abufused
254 , // TODO should be `addr: *struct_arcan_shmif_page`
255 \\pub const struct_arcan_shmif_cont = extern struct {275 \\pub const struct_arcan_shmif_cont = extern struct {
256 \\ addr: [*c]struct_arcan_shmif_page,276 \\ addr: ?*struct_arcan_shmif_page,
257 \\};277 \\};
258 });278 });
259279
...@@ -514,8 +534,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -514,8 +534,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
514 \\ var a: c_int = undefined;534 \\ var a: c_int = undefined;
515 \\ _ = @as(c_int, 1);535 \\ _ = @as(c_int, 1);
516 \\ _ = "hey";536 \\ _ = "hey";
517 \\ _ = (@as(c_int, 1) + @as(c_int, 1));537 \\ _ = @as(c_int, 1) + @as(c_int, 1);
518 \\ _ = (@as(c_int, 1) - @as(c_int, 1));538 \\ _ = @as(c_int, 1) - @as(c_int, 1);
519 \\ a = 1;539 \\ a = 1;
520 \\}540 \\}
521 });541 });
...@@ -634,9 +654,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -634,9 +654,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
634 \\ var a: c_int = undefined;654 \\ var a: c_int = undefined;
635 \\ var b: c_int = undefined;655 \\ var b: c_int = undefined;
636 \\ var c: c_int = undefined;656 \\ var c: c_int = undefined;
637 \\ c = (a + b);657 \\ c = a + b;
638 \\ c = (a - b);658 \\ c = a - b;
639 \\ c = (a * b);659 \\ c = a * b;
640 \\ c = @divTrunc(a, b);660 \\ c = @divTrunc(a, b);
641 \\ c = @rem(a, b);661 \\ c = @rem(a, b);
642 \\ return 0;662 \\ return 0;
...@@ -645,11 +665,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -645,11 +665,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
645 \\ var a: c_uint = undefined;665 \\ var a: c_uint = undefined;
646 \\ var b: c_uint = undefined;666 \\ var b: c_uint = undefined;
647 \\ var c: c_uint = undefined;667 \\ var c: c_uint = undefined;
648 \\ c = (a +% b);668 \\ c = a +% b;
649 \\ c = (a -% b);669 \\ c = a -% b;
650 \\ c = (a *% b);670 \\ c = a *% b;
651 \\ c = (a / b);671 \\ c = a / b;
652 \\ c = (a % b);672 \\ c = a % b;
653 \\ return 0;673 \\ return 0;
654 \\}674 \\}
655 });675 });
...@@ -1639,7 +1659,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1639,7 +1659,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1639 cases.add("macro pointer cast",1659 cases.add("macro pointer cast",
1640 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)1660 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
1641 , &[_][]const u8{1661 , &[_][]const u8{
1642 \\pub const NRF_GPIO = (@import("std").meta.cast([*c]NRF_GPIO_Type, NRF_GPIO_BASE));1662 \\pub const NRF_GPIO = @import("std").meta.cast([*c]NRF_GPIO_Type, NRF_GPIO_BASE);
1643 });1663 });
16441664
1645 cases.add("basic macro function",1665 cases.add("basic macro function",
...@@ -1723,17 +1743,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1723,17 +1743,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1723 \\}1743 \\}
1724 , &[_][]const u8{1744 , &[_][]const u8{
1725 \\pub export fn foo() c_int {1745 \\pub export fn foo() c_int {
1726 \\ _ = (blk: {1746 \\ _ = blk: {
1727 \\ _ = @as(c_int, 2);1747 \\ _ = @as(c_int, 2);
1728 \\ break :blk @as(c_int, 4);1748 \\ break :blk @as(c_int, 4);
1729 \\ });1749 \\ };
1730 \\ return (blk: {1750 \\ return blk: {
1731 \\ _ = (blk_1: {1751 \\ _ = blk_1: {
1732 \\ _ = @as(c_int, 2);1752 \\ _ = @as(c_int, 2);
1733 \\ break :blk_1 @as(c_int, 4);1753 \\ break :blk_1 @as(c_int, 4);
1734 \\ });1754 \\ };
1735 \\ break :blk @as(c_int, 6);1755 \\ break :blk @as(c_int, 6);
1736 \\ });1756 \\ };
1737 \\}1757 \\}
1738 });1758 });
17391759
...@@ -1780,20 +1800,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1780,20 +1800,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1780 \\ while (true) {1800 \\ while (true) {
1781 \\ var a_1: c_int = 4;1801 \\ var a_1: c_int = 4;
1782 \\ a_1 = 9;1802 \\ a_1 = 9;
1783 \\ return (blk: {1803 \\ return blk: {
1784 \\ _ = @as(c_int, 6);1804 \\ _ = @as(c_int, 6);
1785 \\ break :blk a_1;1805 \\ break :blk a_1;
1786 \\ });1806 \\ };
1787 \\ }1807 \\ }
1788 \\ while (true) {1808 \\ while (true) {
1789 \\ var a_1: c_int = 2;1809 \\ var a_1: c_int = 2;
1790 \\ a_1 = 12;1810 \\ a_1 = 12;
1791 \\ if (!true) break;
1792 \\ }
1793 \\ while (true) {
1794 \\ a = 7;
1795 \\ if (!true) break;
1796 \\ }1811 \\ }
1812 \\ while (true) a = 7;
1797 \\ return 0;1813 \\ return 0;
1798 \\}1814 \\}
1799 });1815 });
...@@ -1813,13 +1829,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1813,13 +1829,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1813 \\ var b: c_int = 4;1829 \\ var b: c_int = 4;
1814 \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {1830 \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {
1815 \\ var a: c_int = 2;1831 \\ var a: c_int = 2;
1816 \\ _ = (blk: {1832 \\ _ = blk: {
1817 \\ _ = (blk_1: {1833 \\ _ = blk_1: {
1818 \\ a = 6;1834 \\ a = 6;
1819 \\ break :blk_1 @as(c_int, 5);1835 \\ break :blk_1 @as(c_int, 5);
1820 \\ });1836 \\ };
1821 \\ break :blk @as(c_int, 7);1837 \\ break :blk @as(c_int, 7);
1822 \\ });1838 \\ };
1823 \\ }1839 \\ }
1824 \\ }1840 \\ }
1825 \\ var i: u8 = @bitCast(u8, @truncate(i8, @as(c_int, 2)));1841 \\ var i: u8 = @bitCast(u8, @truncate(i8, @as(c_int, 2)));
...@@ -1854,7 +1870,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1854,7 +1870,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1854 \\}1870 \\}
1855 , &[_][]const u8{1871 , &[_][]const u8{
1856 \\pub export fn bar() c_int {1872 \\pub export fn bar() c_int {
1857 \\ if ((if (true) @as(c_int, 5) else (if (true) @as(c_int, 4) else @as(c_int, 6))) != 0) _ = @as(c_int, 2);1873 \\ if ((if (true) @as(c_int, 5) else if (true) @as(c_int, 4) else @as(c_int, 6)) != 0) _ = @as(c_int, 2);
1858 \\ return if (true) @as(c_int, 5) else if (true) @as(c_int, 4) else @as(c_int, 6);1874 \\ return if (true) @as(c_int, 5) else if (true) @as(c_int, 4) else @as(c_int, 6);
1859 \\}1875 \\}
1860 });1876 });
...@@ -1894,7 +1910,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1894,7 +1910,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1894 \\ }1910 \\ }
1895 \\ res = 2;1911 \\ res = 2;
1896 \\ }1912 \\ }
1897 \\ res = (@as(c_int, 3) * i);1913 \\ res = @as(c_int, 3) * i;
1898 \\ break :@"switch";1914 \\ break :@"switch";
1899 \\ }1915 \\ }
1900 \\ res = 5;1916 \\ res = 5;
...@@ -2043,12 +2059,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2043,12 +2059,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2043 \\pub export fn foo() void {2059 \\pub export fn foo() void {
2044 \\ var a: c_int = 2;2060 \\ var a: c_int = 2;
2045 \\ while (true) {2061 \\ while (true) {
2046 \\ a = (a - @as(c_int, 1));2062 \\ a = a - @as(c_int, 1);
2047 \\ if (!(a != 0)) break;2063 \\ if (!(a != 0)) break;
2048 \\ }2064 \\ }
2049 \\ var b: c_int = 2;2065 \\ var b: c_int = 2;
2050 \\ while (true) {2066 \\ while (true) {
2051 \\ b = (b - @as(c_int, 1));2067 \\ b = b - @as(c_int, 1);
2052 \\ if (!(b != 0)) break;2068 \\ if (!(b != 0)) break;
2053 \\ }2069 \\ }
2054 \\}2070 \\}
...@@ -2078,6 +2094,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2078,6 +2094,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2078 \\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p);2094 \\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p);
2079 \\}2095 \\}
2080 , &[_][]const u8{2096 , &[_][]const u8{
2097 \\pub const FooA = @enumToInt(enum_Foo.A);
2098 \\pub const FooB = @enumToInt(enum_Foo.B);
2099 \\pub const FooC = @enumToInt(enum_Foo.C);
2081 \\pub const enum_Foo = extern enum(c_int) {2100 \\pub const enum_Foo = extern enum(c_int) {
2082 \\ A,2101 \\ A,
2083 \\ B,2102 \\ B,
...@@ -2090,19 +2109,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2090,19 +2109,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2090 \\ var b = arg_b;2109 \\ var b = arg_b;
2091 \\ var c = arg_c;2110 \\ var c = arg_c;
2092 \\ var d: enum_Foo = @intToEnum(enum_Foo, FooA);2111 \\ var d: enum_Foo = @intToEnum(enum_Foo, FooA);
2093 \\ var e: c_int = @boolToInt(((a != 0) and (b != 0)));2112 \\ var e: c_int = @boolToInt((a != 0) and (b != 0));
2094 \\ var f: c_int = @boolToInt(((b != 0) and (c != null)));2113 \\ var f: c_int = @boolToInt((b != 0) and (c != null));
2095 \\ var g: c_int = @boolToInt(((a != 0) and (c != null)));2114 \\ var g: c_int = @boolToInt((a != 0) and (c != null));
2096 \\ var h: c_int = @boolToInt(((a != 0) or (b != 0)));2115 \\ var h: c_int = @boolToInt((a != 0) or (b != 0));
2097 \\ var i: c_int = @boolToInt(((b != 0) or (c != null)));2116 \\ var i: c_int = @boolToInt((b != 0) or (c != null));
2098 \\ var j: c_int = @boolToInt(((a != 0) or (c != null)));2117 \\ var j: c_int = @boolToInt((a != 0) or (c != null));
2099 \\ var k: c_int = @boolToInt(((a != 0) or (@bitCast(c_int, @enumToInt(d)) != 0)));2118 \\ var k: c_int = @boolToInt((a != 0) or (@bitCast(c_int, @enumToInt(d)) != 0));
2100 \\ var l: c_int = @boolToInt(((@bitCast(c_int, @enumToInt(d)) != 0) and (b != 0)));2119 \\ var l: c_int = @boolToInt((@bitCast(c_int, @enumToInt(d)) != 0) and (b != 0));
2101 \\ var m: c_int = @boolToInt(((c != null) or (@bitCast(c_uint, @enumToInt(d)) != 0)));2120 \\ var m: c_int = @boolToInt((c != null) or (@bitCast(c_uint, @enumToInt(d)) != 0));
2102 \\ var td: SomeTypedef = 44;2121 \\ var td: SomeTypedef = 44;
2103 \\ var o: c_int = @boolToInt(((td != 0) or (b != 0)));2122 \\ var o: c_int = @boolToInt((td != 0) or (b != 0));
2104 \\ var p: c_int = @boolToInt(((c != null) and (td != 0)));2123 \\ var p: c_int = @boolToInt((c != null) and (td != 0));
2105 \\ return ((((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p);2124 \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p;
2106 \\}2125 \\}
2107 ,2126 ,
2108 \\pub const Foo = enum_Foo;2127 \\pub const Foo = enum_Foo;
...@@ -2143,7 +2162,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2143,7 +2162,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2143 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {2162 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
2144 \\ var a = arg_a;2163 \\ var a = arg_a;
2145 \\ var b = arg_b;2164 \\ var b = arg_b;
2146 \\ return ((a & b) ^ (a | b));2165 \\ return (a & b) ^ (a | b);
2147 \\}2166 \\}
2148 });2167 });
21492168
...@@ -2162,13 +2181,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2162,13 +2181,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2162 \\pub export fn test_comparisons(arg_a: c_int, arg_b: c_int) c_int {2181 \\pub export fn test_comparisons(arg_a: c_int, arg_b: c_int) c_int {
2163 \\ var a = arg_a;2182 \\ var a = arg_a;
2164 \\ var b = arg_b;2183 \\ var b = arg_b;
2165 \\ var c: c_int = @boolToInt((a < b));2184 \\ var c: c_int = @boolToInt(a < b);
2166 \\ var d: c_int = @boolToInt((a > b));2185 \\ var d: c_int = @boolToInt(a > b);
2167 \\ var e: c_int = @boolToInt((a <= b));2186 \\ var e: c_int = @boolToInt(a <= b);
2168 \\ var f: c_int = @boolToInt((a >= b));2187 \\ var f: c_int = @boolToInt(a >= b);
2169 \\ var g: c_int = @boolToInt((c < d));2188 \\ var g: c_int = @boolToInt(c < d);
2170 \\ var h: c_int = @boolToInt((e < f));2189 \\ var h: c_int = @boolToInt(e < f);
2171 \\ var i: c_int = @boolToInt((g < h));2190 \\ var i: c_int = @boolToInt(g < h);
2172 \\ return i;2191 \\ return i;
2173 \\}2192 \\}
2174 });2193 });
...@@ -2215,11 +2234,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2215,11 +2234,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2215 \\}2234 \\}
2216 , &[_][]const u8{2235 , &[_][]const u8{
2217 \\pub export fn foo() c_int {2236 \\pub export fn foo() c_int {
2218 \\ return (blk: {2237 \\ return blk: {
2219 \\ var a: c_int = 1;2238 \\ var a: c_int = 1;
2220 \\ _ = a;2239 \\ _ = a;
2221 \\ break :blk a;2240 \\ break :blk a;
2222 \\ });2241 \\ };
2223 \\}2242 \\}
2224 });2243 });
22252244
...@@ -2371,9 +2390,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2371,9 +2390,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2371 \\ var a: c_int = 2;2390 \\ var a: c_int = 2;
2372 \\ }2391 \\ }
2373 \\ if ((blk: {2392 \\ if ((blk: {
2374 \\ _ = @as(c_int, 2);2393 \\ _ = @as(c_int, 2);
2375 \\ break :blk @as(c_int, 5);2394 \\ break :blk @as(c_int, 5);
2376 \\ }) != 0) {2395 \\ }) != 0) {
2377 \\ var a: c_int = 2;2396 \\ var a: c_int = 2;
2378 \\ }2397 \\ }
2379 \\}2398 \\}
...@@ -2484,10 +2503,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2484,10 +2503,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2484 \\ var f: ?fn () callconv(.C) void = foo;2503 \\ var f: ?fn () callconv(.C) void = foo;
2485 \\ var b: ?fn () callconv(.C) c_int = baz;2504 \\ var b: ?fn () callconv(.C) c_int = baz;
2486 \\ f.?();2505 \\ f.?();
2487 \\ (f).?();2506 \\ f.?();
2488 \\ foo();2507 \\ foo();
2489 \\ _ = b.?();2508 \\ _ = b.?();
2490 \\ _ = (b).?();2509 \\ _ = b.?();
2491 \\ _ = baz();2510 \\ _ = baz();
2492 \\}2511 \\}
2493 });2512 });
...@@ -2513,26 +2532,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2513,26 +2532,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2513 \\ i -= 1;2532 \\ i -= 1;
2514 \\ u +%= 1;2533 \\ u +%= 1;
2515 \\ u -%= 1;2534 \\ u -%= 1;
2516 \\ i = (blk: {2535 \\ i = blk: {
2517 \\ const ref = &i;2536 \\ const ref = &i;
2518 \\ ref.* += 1;2537 \\ ref.* += 1;
2519 \\ break :blk ref.*;2538 \\ break :blk ref.*;
2520 \\ });2539 \\ };
2521 \\ i = (blk: {2540 \\ i = blk: {
2522 \\ const ref = &i;2541 \\ const ref = &i;
2523 \\ ref.* -= 1;2542 \\ ref.* -= 1;
2524 \\ break :blk ref.*;2543 \\ break :blk ref.*;
2525 \\ });2544 \\ };
2526 \\ u = (blk: {2545 \\ u = blk: {
2527 \\ const ref = &u;2546 \\ const ref = &u;
2528 \\ ref.* +%= 1;2547 \\ ref.* +%= 1;
2529 \\ break :blk ref.*;2548 \\ break :blk ref.*;
2530 \\ });2549 \\ };
2531 \\ u = (blk: {2550 \\ u = blk: {
2532 \\ const ref = &u;2551 \\ const ref = &u;
2533 \\ ref.* -%= 1;2552 \\ ref.* -%= 1;
2534 \\ break :blk ref.*;2553 \\ break :blk ref.*;
2535 \\ });2554 \\ };
2536 \\}2555 \\}
2537 });2556 });
25382557
...@@ -2596,66 +2615,66 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2596,66 +2615,66 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2596 \\pub export fn foo() void {2615 \\pub export fn foo() void {
2597 \\ var a: c_int = 0;2616 \\ var a: c_int = 0;
2598 \\ var b: c_uint = @bitCast(c_uint, @as(c_int, 0));2617 \\ var b: c_uint = @bitCast(c_uint, @as(c_int, 0));
2599 \\ a += (blk: {2618 \\ a += blk: {
2600 \\ const ref = &a;2619 \\ const ref = &a;
2601 \\ ref.* = ref.* + @as(c_int, 1);2620 \\ ref.* += @as(c_int, 1);
2602 \\ break :blk ref.*;2621 \\ break :blk ref.*;
2603 \\ });2622 \\ };
2604 \\ a -= (blk: {2623 \\ a -= blk: {
2605 \\ const ref = &a;2624 \\ const ref = &a;
2606 \\ ref.* = ref.* - @as(c_int, 1);2625 \\ ref.* -= @as(c_int, 1);
2607 \\ break :blk ref.*;2626 \\ break :blk ref.*;
2608 \\ });2627 \\ };
2609 \\ a *= (blk: {2628 \\ a *= blk: {
2610 \\ const ref = &a;2629 \\ const ref = &a;
2611 \\ ref.* = ref.* * @as(c_int, 1);2630 \\ ref.* *= @as(c_int, 1);
2612 \\ break :blk ref.*;2631 \\ break :blk ref.*;
2613 \\ });2632 \\ };
2614 \\ a &= (blk: {2633 \\ a &= blk: {
2615 \\ const ref = &a;2634 \\ const ref = &a;
2616 \\ ref.* = ref.* & @as(c_int, 1);2635 \\ ref.* &= @as(c_int, 1);
2617 \\ break :blk ref.*;2636 \\ break :blk ref.*;
2618 \\ });2637 \\ };
2619 \\ a |= (blk: {2638 \\ a |= blk: {
2620 \\ const ref = &a;2639 \\ const ref = &a;
2621 \\ ref.* = ref.* | @as(c_int, 1);2640 \\ ref.* |= @as(c_int, 1);
2622 \\ break :blk ref.*;2641 \\ break :blk ref.*;
2623 \\ });2642 \\ };
2624 \\ a ^= (blk: {2643 \\ a ^= blk: {
2625 \\ const ref = &a;2644 \\ const ref = &a;
2626 \\ ref.* = ref.* ^ @as(c_int, 1);2645 \\ ref.* ^= @as(c_int, 1);
2627 \\ break :blk ref.*;2646 \\ break :blk ref.*;
2628 \\ });2647 \\ };
2629 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), (blk: {2648 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), blk: {
2630 \\ const ref = &a;2649 \\ const ref = &a;
2631 \\ ref.* = ref.* >> @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));2650 \\ ref.* >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2632 \\ break :blk ref.*;2651 \\ break :blk ref.*;
2633 \\ }));2652 \\ });
2634 \\ a <<= @intCast(@import("std").math.Log2Int(c_int), (blk: {2653 \\ a <<= @intCast(@import("std").math.Log2Int(c_int), blk: {
2635 \\ const ref = &a;2654 \\ const ref = &a;
2636 \\ ref.* = ref.* << @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));2655 \\ ref.* <<= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2637 \\ break :blk ref.*;2656 \\ break :blk ref.*;
2638 \\ }));2657 \\ });
2639 \\ a = @divTrunc(a, (blk: {2658 \\ a = @divTrunc(a, blk: {
2640 \\ const ref = &a;2659 \\ const ref = &a;
2641 \\ ref.* = @divTrunc(ref.*, @as(c_int, 1));2660 \\ ref.* = @divTrunc(ref.*, @as(c_int, 1));
2642 \\ break :blk ref.*;2661 \\ break :blk ref.*;
2643 \\ }));2662 \\ });
2644 \\ a = @rem(a, (blk: {2663 \\ a = @rem(a, blk: {
2645 \\ const ref = &a;2664 \\ const ref = &a;
2646 \\ ref.* = @rem(ref.*, @as(c_int, 1));2665 \\ ref.* = @rem(ref.*, @as(c_int, 1));
2647 \\ break :blk ref.*;2666 \\ break :blk ref.*;
2648 \\ }));2667 \\ });
2649 \\ b /= (blk: {2668 \\ b /= blk: {
2650 \\ const ref = &b;2669 \\ const ref = &b;
2651 \\ ref.* = ref.* / @bitCast(c_uint, @as(c_int, 1));2670 \\ ref.* /= @bitCast(c_uint, @as(c_int, 1));
2652 \\ break :blk ref.*;2671 \\ break :blk ref.*;
2653 \\ });2672 \\ };
2654 \\ b %= (blk: {2673 \\ b %= blk: {
2655 \\ const ref = &b;2674 \\ const ref = &b;
2656 \\ ref.* = ref.* % @bitCast(c_uint, @as(c_int, 1));2675 \\ ref.* %= @bitCast(c_uint, @as(c_int, 1));
2657 \\ break :blk ref.*;2676 \\ break :blk ref.*;
2658 \\ });2677 \\ };
2659 \\}2678 \\}
2660 });2679 });
26612680
...@@ -2674,46 +2693,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2674,46 +2693,46 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2674 , &[_][]const u8{2693 , &[_][]const u8{
2675 \\pub export fn foo() void {2694 \\pub export fn foo() void {
2676 \\ var a: c_uint = @bitCast(c_uint, @as(c_int, 0));2695 \\ var a: c_uint = @bitCast(c_uint, @as(c_int, 0));
2677 \\ a +%= (blk: {2696 \\ a +%= blk: {
2678 \\ const ref = &a;2697 \\ const ref = &a;
2679 \\ ref.* = ref.* +% @bitCast(c_uint, @as(c_int, 1));2698 \\ ref.* +%= @bitCast(c_uint, @as(c_int, 1));
2680 \\ break :blk ref.*;2699 \\ break :blk ref.*;
2681 \\ });2700 \\ };
2682 \\ a -%= (blk: {2701 \\ a -%= blk: {
2683 \\ const ref = &a;2702 \\ const ref = &a;
2684 \\ ref.* = ref.* -% @bitCast(c_uint, @as(c_int, 1));2703 \\ ref.* -%= @bitCast(c_uint, @as(c_int, 1));
2685 \\ break :blk ref.*;2704 \\ break :blk ref.*;
2686 \\ });2705 \\ };
2687 \\ a *%= (blk: {2706 \\ a *%= blk: {
2688 \\ const ref = &a;2707 \\ const ref = &a;
2689 \\ ref.* = ref.* *% @bitCast(c_uint, @as(c_int, 1));2708 \\ ref.* *%= @bitCast(c_uint, @as(c_int, 1));
2690 \\ break :blk ref.*;2709 \\ break :blk ref.*;
2691 \\ });2710 \\ };
2692 \\ a &= (blk: {2711 \\ a &= blk: {
2693 \\ const ref = &a;2712 \\ const ref = &a;
2694 \\ ref.* = ref.* & @bitCast(c_uint, @as(c_int, 1));2713 \\ ref.* &= @bitCast(c_uint, @as(c_int, 1));
2695 \\ break :blk ref.*;2714 \\ break :blk ref.*;
2696 \\ });2715 \\ };
2697 \\ a |= (blk: {2716 \\ a |= blk: {
2698 \\ const ref = &a;2717 \\ const ref = &a;
2699 \\ ref.* = ref.* | @bitCast(c_uint, @as(c_int, 1));2718 \\ ref.* |= @bitCast(c_uint, @as(c_int, 1));
2700 \\ break :blk ref.*;2719 \\ break :blk ref.*;
2701 \\ });2720 \\ };
2702 \\ a ^= (blk: {2721 \\ a ^= blk: {
2703 \\ const ref = &a;2722 \\ const ref = &a;
2704 \\ ref.* = ref.* ^ @bitCast(c_uint, @as(c_int, 1));2723 \\ ref.* ^= @bitCast(c_uint, @as(c_int, 1));
2705 \\ break :blk ref.*;2724 \\ break :blk ref.*;
2706 \\ });2725 \\ };
2707 \\ a >>= @intCast(@import("std").math.Log2Int(c_uint), (blk: {2726 \\ a >>= @intCast(@import("std").math.Log2Int(c_uint), blk: {
2708 \\ const ref = &a;2727 \\ const ref = &a;
2709 \\ ref.* = ref.* >> @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));2728 \\ ref.* >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2710 \\ break :blk ref.*;2729 \\ break :blk ref.*;
2711 \\ }));2730 \\ });
2712 \\ a <<= @intCast(@import("std").math.Log2Int(c_uint), (blk: {2731 \\ a <<= @intCast(@import("std").math.Log2Int(c_uint), blk: {
2713 \\ const ref = &a;2732 \\ const ref = &a;
2714 \\ ref.* = ref.* << @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));2733 \\ ref.* <<= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2715 \\ break :blk ref.*;2734 \\ break :blk ref.*;
2716 \\ }));2735 \\ });
2717 \\}2736 \\}
2718 });2737 });
27192738
...@@ -2738,30 +2757,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2738,30 +2757,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2738 \\ i -= 1;2757 \\ i -= 1;
2739 \\ u +%= 1;2758 \\ u +%= 1;
2740 \\ u -%= 1;2759 \\ u -%= 1;
2741 \\ i = (blk: {2760 \\ i = blk: {
2742 \\ const ref = &i;2761 \\ const ref = &i;
2743 \\ const tmp = ref.*;2762 \\ const tmp = ref.*;
2744 \\ ref.* += 1;2763 \\ ref.* += 1;
2745 \\ break :blk tmp;2764 \\ break :blk tmp;
2746 \\ });2765 \\ };
2747 \\ i = (blk: {2766 \\ i = blk: {
2748 \\ const ref = &i;2767 \\ const ref = &i;
2749 \\ const tmp = ref.*;2768 \\ const tmp = ref.*;
2750 \\ ref.* -= 1;2769 \\ ref.* -= 1;
2751 \\ break :blk tmp;2770 \\ break :blk tmp;
2752 \\ });2771 \\ };
2753 \\ u = (blk: {2772 \\ u = blk: {
2754 \\ const ref = &u;2773 \\ const ref = &u;
2755 \\ const tmp = ref.*;2774 \\ const tmp = ref.*;
2756 \\ ref.* +%= 1;2775 \\ ref.* +%= 1;
2757 \\ break :blk tmp;2776 \\ break :blk tmp;
2758 \\ });2777 \\ };
2759 \\ u = (blk: {2778 \\ u = blk: {
2760 \\ const ref = &u;2779 \\ const ref = &u;
2761 \\ const tmp = ref.*;2780 \\ const tmp = ref.*;
2762 \\ ref.* -%= 1;2781 \\ ref.* -%= 1;
2763 \\ break :blk tmp;2782 \\ break :blk tmp;
2764 \\ });2783 \\ };
2765 \\}2784 \\}
2766 });2785 });
27672786
...@@ -2872,13 +2891,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2872,13 +2891,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2872 \\#define BAR (void*) a2891 \\#define BAR (void*) a
2873 \\#define BAZ (uint32_t)(2)2892 \\#define BAZ (uint32_t)(2)
2874 , &[_][]const u8{2893 , &[_][]const u8{
2875 \\pub fn FOO(bar: anytype) callconv(.Inline) @TypeOf(baz((@import("std").meta.cast(?*c_void, baz)))) {2894 \\pub fn FOO(bar: anytype) callconv(.Inline) @TypeOf(baz(@import("std").meta.cast(?*c_void, baz))) {
2876 \\ return baz((@import("std").meta.cast(?*c_void, baz)));2895 \\ return baz(@import("std").meta.cast(?*c_void, baz));
2877 \\}2896 \\}
2878 ,2897 ,
2879 \\pub const BAR = (@import("std").meta.cast(?*c_void, a));2898 \\pub const BAR = @import("std").meta.cast(?*c_void, a);
2880 ,2899 ,
2881 \\pub const BAZ = (@import("std").meta.cast(u32, 2));2900 \\pub const BAZ = @import("std").meta.cast(u32, 2);
2882 });2901 });
28832902
2884 cases.add("macro with cast to unsigned short, long, and long long",2903 cases.add("macro with cast to unsigned short, long, and long long",
...@@ -2886,9 +2905,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2886,9 +2905,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2886 \\#define CURLAUTH_BASIC ((unsigned long) 1)2905 \\#define CURLAUTH_BASIC ((unsigned long) 1)
2887 \\#define CURLAUTH_BASIC_BUT_ULONGLONG ((unsigned long long) 1)2906 \\#define CURLAUTH_BASIC_BUT_ULONGLONG ((unsigned long long) 1)
2888 , &[_][]const u8{2907 , &[_][]const u8{
2889 \\pub const CURLAUTH_BASIC_BUT_USHORT = (@import("std").meta.cast(c_ushort, 1));2908 \\pub const CURLAUTH_BASIC_BUT_USHORT = @import("std").meta.cast(c_ushort, 1);
2890 \\pub const CURLAUTH_BASIC = (@import("std").meta.cast(c_ulong, 1));2909 \\pub const CURLAUTH_BASIC = @import("std").meta.cast(c_ulong, 1);
2891 \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = (@import("std").meta.cast(c_ulonglong, 1));2910 \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = @import("std").meta.cast(c_ulonglong, 1);
2892 });2911 });
28932912
2894 cases.add("macro conditional operator",2913 cases.add("macro conditional operator",
...@@ -2905,7 +2924,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2905,7 +2924,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2905 , &[_][]const u8{2924 , &[_][]const u8{
2906 \\pub fn foo() callconv(.C) void {2925 \\pub fn foo() callconv(.C) void {
2907 \\ if (true) while (true) {2926 \\ if (true) while (true) {
2908 \\ if (!false) break;2927 \\ break;
2909 \\ };2928 \\ };
2910 \\}2929 \\}
2911 });2930 });
...@@ -2923,27 +2942,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2923,27 +2942,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2923 \\}2942 \\}
2924 });2943 });
29252944
2926 // TODO: detect to use different block labels here2945 // TODO fix zig fmt here
2927 cases.add("nested assignment",2946 // cases.add("nested assignment",
2928 \\int foo(int *p, int x) {2947 // \\int foo(int *p, int x) {
2929 \\ return *p++ = x;2948 // \\ return *p++ = x;
2930 \\}2949 // \\}
2931 , &[_][]const u8{2950 // , &[_][]const u8{
2932 \\pub export fn foo(arg_p: [*c]c_int, arg_x: c_int) c_int {2951 // \\pub export fn foo(arg_p: [*c]c_int, arg_x: c_int) c_int {
2933 \\ var p = arg_p;2952 // \\ var p = arg_p;
2934 \\ var x = arg_x;2953 // \\ var x = arg_x;
2935 \\ return blk: {2954 // \\ return blk: {
2936 \\ const tmp = x;2955 // \\ const tmp = x;
2937 \\ (blk_1: {2956 // \\ (blk_1: {
2938 \\ const ref = &p;2957 // \\ const ref = &p;
2939 \\ const tmp_2 = ref.*;2958 // \\ const tmp_2 = ref.*;
2940 \\ ref.* += 1;2959 // \\ ref.* += 1;
2941 \\ break :blk_1 tmp_2;2960 // \\ break :blk_1 tmp_2;
2942 \\ }).?.* = tmp;2961 // \\ }).?.* = tmp;
2943 \\ break :blk tmp;2962 // \\ break :blk tmp;
2944 \\ };2963 // \\ };
2945 \\}2964 // \\}
2946 });2965 // });
29472966
2948 cases.add("widening and truncating integer casting to different signedness",2967 cases.add("widening and truncating integer casting to different signedness",
2949 \\unsigned long foo(void) {2968 \\unsigned long foo(void) {
...@@ -3033,10 +3052,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3033,10 +3052,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3033 , &[_][]const u8{3052 , &[_][]const u8{
3034 \\pub export fn foo(arg_x: bool) bool {3053 \\pub export fn foo(arg_x: bool) bool {
3035 \\ var x = arg_x;3054 \\ var x = arg_x;
3036 \\ var a: bool = (@as(c_int, @boolToInt(x)) != @as(c_int, 1));3055 \\ var a: bool = @as(c_int, @boolToInt(x)) != @as(c_int, 1);
3037 \\ var b: bool = (@as(c_int, @boolToInt(a)) != @as(c_int, 0));3056 \\ var b: bool = @as(c_int, @boolToInt(a)) != @as(c_int, 0);
3038 \\ var c: bool = @ptrToInt(foo) != 0;3057 \\ var c: bool = @ptrToInt(foo) != 0;
3039 \\ return foo((@as(c_int, @boolToInt(c)) != @as(c_int, @boolToInt(b))));3058 \\ return foo(@as(c_int, @boolToInt(c)) != @as(c_int, @boolToInt(b)));
3040 \\}3059 \\}
3041 });3060 });
30423061
...@@ -3106,8 +3125,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3106,8 +3125,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3106 \\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen)3125 \\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen)
3107 \\3126 \\
3108 , &[_][]const u8{3127 , &[_][]const u8{
3109 \\pub fn DefaultScreen(dpy: anytype) callconv(.Inline) @TypeOf((@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen) {3128 \\pub fn DefaultScreen(dpy: anytype) callconv(.Inline) @TypeOf(@import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen) {
3110 \\ return (@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen;3129 \\ return @import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen;
3111 \\}3130 \\}
3112 });3131 });
31133132
...@@ -3115,9 +3134,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3115,9 +3134,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3115 \\#define NULL ((void*)0)3134 \\#define NULL ((void*)0)
3116 \\#define FOO ((int)0x8000)3135 \\#define FOO ((int)0x8000)
3117 , &[_][]const u8{3136 , &[_][]const u8{
3118 \\pub const NULL = (@import("std").meta.cast(?*c_void, 0));3137 \\pub const NULL = @import("std").meta.cast(?*c_void, 0);
3119 ,3138 ,
3120 \\pub const FOO = (@import("std").meta.cast(c_int, 0x8000));3139 \\pub const FOO = @import("std").meta.cast(c_int, 0x8000);
3121 });3140 });
31223141
3123 if (std.Target.current.abi == .msvc) {3142 if (std.Target.current.abi == .msvc) {