| ... | ... | @@ -17,13 +17,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 17 | 17 | \\ char b = 123; |
| 18 | 18 | \\ const int c; |
| 19 | 19 | \\ const unsigned d = 440; |
| 20 | \\ int e = 10; |
| 21 | \\ unsigned int f = 10u; |
| 20 | 22 | \\} |
| 21 | 23 | , &[_][]const u8{ |
| 22 | 24 | \\pub export fn foo() void { |
| 23 | 25 | \\ var a: c_int = undefined; |
| 24 | | \\ var b: u8 = @intCast(u8, 123); |
| 26 | \\ var b: u8 = @bitCast(u8, @truncate(i8, @as(c_int, 123))); |
| 25 | 27 | \\ const c: c_int = undefined; |
| 26 | | \\ const d: c_uint = @intCast(c_uint, 440); |
| 28 | \\ const d: c_uint = @bitCast(c_uint, @as(c_int, 440)); |
| 29 | \\ var e: c_int = 10; |
| 30 | \\ var f: c_uint = 10; |
| 27 | 31 | \\} |
| 28 | 32 | }); |
| 29 | 33 | |
| ... | ... | @@ -39,14 +43,24 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 39 | 43 | , &[_][]const u8{ |
| 40 | 44 | \\pub export fn foo() void { |
| 41 | 45 | \\ var a: c_int = undefined; |
| 42 | | \\ _ = 1; |
| 46 | \\ _ = @as(c_int, 1); |
| 43 | 47 | \\ _ = "hey"; |
| 44 | | \\ _ = (1 + 1); |
| 45 | | \\ _ = (1 - 1); |
| 48 | \\ _ = (@as(c_int, 1) + @as(c_int, 1)); |
| 49 | \\ _ = (@as(c_int, 1) - @as(c_int, 1)); |
| 46 | 50 | \\ a = 1; |
| 47 | 51 | \\} |
| 48 | 52 | }); |
| 49 | 53 | |
| 54 | cases.add("function with no prototype", |
| 55 | \\int foo() { |
| 56 | \\ return 5; |
| 57 | \\} |
| 58 | , &[_][]const u8{ |
| 59 | \\pub export fn foo() c_int { |
| 60 | \\ return 5; |
| 61 | \\} |
| 62 | }); |
| 63 | |
| 50 | 64 | cases.add("variables", |
| 51 | 65 | \\extern int extern_var; |
| 52 | 66 | \\static const int int_var = 13; |
| ... | ... | @@ -474,26 +488,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 474 | 488 | \\} |
| 475 | 489 | }); |
| 476 | 490 | |
| 477 | | cases.add("ignore result, no function arguments", |
| 478 | | \\void foo() { |
| 479 | | \\ int a; |
| 480 | | \\ 1; |
| 481 | | \\ "hey"; |
| 482 | | \\ 1 + 1; |
| 483 | | \\ 1 - 1; |
| 484 | | \\ a = 1; |
| 485 | | \\} |
| 486 | | , &[_][]const u8{ |
| 487 | | \\pub export fn foo() void { |
| 488 | | \\ var a: c_int = undefined; |
| 489 | | \\ _ = 1; |
| 490 | | \\ _ = "hey"; |
| 491 | | \\ _ = (1 + 1); |
| 492 | | \\ _ = (1 - 1); |
| 493 | | \\ a = 1; |
| 494 | | \\} |
| 495 | | }); |
| 496 | | |
| 497 | 491 | cases.add("constant size array", |
| 498 | 492 | \\void func(int array[20]); |
| 499 | 493 | , &[_][]const u8{ |
| ... | ... | @@ -582,7 +576,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 582 | 576 | \\pub export fn foo() void { |
| 583 | 577 | \\ { |
| 584 | 578 | \\ var i: c_int = 0; |
| 585 | | \\ while (i != 0) : (i = (i + 1)) {} |
| 579 | \\ while (i != 0) : (i = (i + @as(c_int, 1))) {} |
| 586 | 580 | \\ } |
| 587 | 581 | \\} |
| 588 | 582 | }); |
| ... | ... | @@ -721,7 +715,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 721 | 715 | \\} |
| 722 | 716 | , &[_][]const u8{ |
| 723 | 717 | \\pub export fn foo() c_int { |
| 724 | | \\ return (1 << @as(@import("std").math.Log2Int(c_int), 2)) >> @as(@import("std").math.Log2Int(c_int), 1); |
| 718 | \\ return (@as(c_int, 1) << @as(@import("std").math.Log2Int(c_int), 2)) >> @as(@import("std").math.Log2Int(c_int), 1); |
| 725 | 719 | \\} |
| 726 | 720 | }); |
| 727 | 721 | |
| ... | ... | @@ -789,7 +783,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 789 | 783 | \\ var a: c_int = undefined; |
| 790 | 784 | \\ var b: f32 = undefined; |
| 791 | 785 | \\ var c: ?*c_void = undefined; |
| 792 | | \\ return !(a == 0); |
| 786 | \\ return !(a == @as(c_int, 0)); |
| 793 | 787 | \\ return !(a != 0); |
| 794 | 788 | \\ return !(b != 0); |
| 795 | 789 | \\ return !(c != null); |
| ... | ... | @@ -864,7 +858,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 864 | 858 | , &[_][]const u8{ |
| 865 | 859 | \\pub fn foo() void { |
| 866 | 860 | \\ var arr: [10]u8 = .{ |
| 867 | | \\ @intCast(u8, 1), |
| 861 | \\ @bitCast(u8, @truncate(i8, @as(c_int, 1))), |
| 868 | 862 | \\ } ++ .{0} ** 9; |
| 869 | 863 | \\ var arr1: [10][*c]u8 = .{ |
| 870 | 864 | \\ null, |
| ... | ... | @@ -1103,18 +1097,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1103 | 1097 | \\ unsigned d = 440; |
| 1104 | 1098 | \\} |
| 1105 | 1099 | , &[_][]const u8{ |
| 1106 | | \\pub var a: c_long = @intCast(c_long, 2); |
| 1107 | | \\pub var b: c_long = @intCast(c_long, 2); |
| 1100 | \\pub var a: c_long = @bitCast(c_long, @as(c_long, @as(c_int, 2))); |
| 1101 | \\pub var b: c_long = @bitCast(c_long, @as(c_long, @as(c_int, 2))); |
| 1108 | 1102 | \\pub var c: c_int = 4; |
| 1109 | 1103 | \\pub export fn foo(arg_c_1: u8) void { |
| 1110 | 1104 | \\ var c_1 = arg_c_1; |
| 1111 | 1105 | \\ var a_2: c_int = undefined; |
| 1112 | | \\ var b_3: u8 = @intCast(u8, 123); |
| 1113 | | \\ b_3 = @intCast(u8, a_2); |
| 1106 | \\ var b_3: u8 = @bitCast(u8, @truncate(i8, @as(c_int, 123))); |
| 1107 | \\ b_3 = @bitCast(u8, @truncate(i8, a_2)); |
| 1114 | 1108 | \\ { |
| 1115 | 1109 | \\ var d: c_int = 5; |
| 1116 | 1110 | \\ } |
| 1117 | | \\ var d: c_uint = @intCast(c_uint, 440); |
| 1111 | \\ var d: c_uint = @bitCast(c_uint, @as(c_int, 440)); |
| 1118 | 1112 | \\} |
| 1119 | 1113 | }); |
| 1120 | 1114 | |
| ... | ... | @@ -1125,11 +1119,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1125 | 1119 | \\} |
| 1126 | 1120 | , &[_][]const u8{ |
| 1127 | 1121 | \\pub export fn foo() c_int { |
| 1128 | | \\ _ = 2; |
| 1129 | | \\ _ = 4; |
| 1130 | | \\ _ = 2; |
| 1131 | | \\ _ = 4; |
| 1132 | | \\ return 6; |
| 1122 | \\ _ = @as(c_int, 2); |
| 1123 | \\ _ = @as(c_int, 4); |
| 1124 | \\ _ = @as(c_int, 2); |
| 1125 | \\ _ = @as(c_int, 4); |
| 1126 | \\ return @as(c_int, 6); |
| 1133 | 1127 | \\} |
| 1134 | 1128 | }); |
| 1135 | 1129 | |
| ... | ... | @@ -1144,36 +1138,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1144 | 1138 | \\ var a: c_int = undefined; |
| 1145 | 1139 | \\ var b: c_int = undefined; |
| 1146 | 1140 | \\ a = blk: { |
| 1147 | | \\ const tmp = 2; |
| 1141 | \\ const tmp = @as(c_int, 2); |
| 1148 | 1142 | \\ b = tmp; |
| 1149 | 1143 | \\ break :blk tmp; |
| 1150 | 1144 | \\ }; |
| 1151 | 1145 | \\} |
| 1152 | 1146 | }); |
| 1153 | 1147 | |
| 1154 | | cases.add("if statements", |
| 1155 | | \\int foo() { |
| 1156 | | \\ if (2) { |
| 1157 | | \\ int a = 2; |
| 1158 | | \\ } |
| 1159 | | \\ if (2, 5) { |
| 1160 | | \\ int a = 2; |
| 1161 | | \\ } |
| 1162 | | \\} |
| 1163 | | , &[_][]const u8{ |
| 1164 | | \\pub export fn foo() c_int { |
| 1165 | | \\ if (2 != 0) { |
| 1166 | | \\ var a: c_int = 2; |
| 1167 | | \\ } |
| 1168 | | \\ if ((blk: { |
| 1169 | | \\ _ = 2; |
| 1170 | | \\ break :blk 5; |
| 1171 | | \\ }) != 0) { |
| 1172 | | \\ var a: c_int = 2; |
| 1173 | | \\ } |
| 1174 | | \\} |
| 1175 | | }); |
| 1176 | | |
| 1177 | 1148 | cases.add("while loops", |
| 1178 | 1149 | \\int foo() { |
| 1179 | 1150 | \\ int a = 5; |
| ... | ... | @@ -1195,21 +1166,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1195 | 1166 | , &[_][]const u8{ |
| 1196 | 1167 | \\pub export fn foo() c_int { |
| 1197 | 1168 | \\ var a: c_int = 5; |
| 1198 | | \\ while (2 != 0) a = 2; |
| 1199 | | \\ while (4 != 0) { |
| 1169 | \\ while (@as(c_int, 2) != 0) a = 2; |
| 1170 | \\ while (@as(c_int, 4) != 0) { |
| 1200 | 1171 | \\ var a_1: c_int = 4; |
| 1201 | 1172 | \\ a_1 = 9; |
| 1202 | | \\ _ = 6; |
| 1173 | \\ _ = @as(c_int, 6); |
| 1203 | 1174 | \\ return a_1; |
| 1204 | 1175 | \\ } |
| 1205 | 1176 | \\ while (true) { |
| 1206 | 1177 | \\ var a_1: c_int = 2; |
| 1207 | 1178 | \\ a_1 = 12; |
| 1208 | | \\ if (!(4 != 0)) break; |
| 1179 | \\ if (!(@as(c_int, 4) != 0)) break; |
| 1209 | 1180 | \\ } |
| 1210 | 1181 | \\ while (true) { |
| 1211 | 1182 | \\ a = 7; |
| 1212 | | \\ if (!(4 != 0)) break; |
| 1183 | \\ if (!(@as(c_int, 4) != 0)) break; |
| 1213 | 1184 | \\ } |
| 1214 | 1185 | \\} |
| 1215 | 1186 | }); |
| ... | ... | @@ -1227,21 +1198,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1227 | 1198 | \\ { |
| 1228 | 1199 | \\ var i: c_int = 2; |
| 1229 | 1200 | \\ var b: c_int = 4; |
| 1230 | | \\ while ((i + 2) != 0) : (i = 2) { |
| 1201 | \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) { |
| 1231 | 1202 | \\ var a: c_int = 2; |
| 1232 | 1203 | \\ a = 6; |
| 1233 | | \\ _ = 5; |
| 1234 | | \\ _ = 7; |
| 1204 | \\ _ = @as(c_int, 5); |
| 1205 | \\ _ = @as(c_int, 7); |
| 1235 | 1206 | \\ } |
| 1236 | 1207 | \\ } |
| 1237 | | \\ var i: u8 = @intCast(u8, 2); |
| 1208 | \\ var i: u8 = @bitCast(u8, @truncate(i8, @as(c_int, 2))); |
| 1238 | 1209 | \\} |
| 1239 | 1210 | }); |
| 1240 | 1211 | |
| 1241 | 1212 | cases.add("shadowing primitive types", |
| 1242 | 1213 | \\unsigned anyerror = 2; |
| 1243 | 1214 | , &[_][]const u8{ |
| 1244 | | \\pub export var _anyerror: c_uint = @intCast(c_uint, 2); |
| 1215 | \\pub export var _anyerror: c_uint = @bitCast(c_uint, @as(c_int, 2)); |
| 1245 | 1216 | }); |
| 1246 | 1217 | |
| 1247 | 1218 | cases.add("floats", |
| ... | ... | @@ -1253,7 +1224,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1253 | 1224 | \\pub export var a: f32 = @floatCast(f32, 3.1415); |
| 1254 | 1225 | \\pub export var b: f64 = 3.1415; |
| 1255 | 1226 | \\pub export var c: c_int = @floatToInt(c_int, 3.1415); |
| 1256 | | \\pub export var d: f64 = @intToFloat(f64, 3); |
| 1227 | \\pub export var d: f64 = @intToFloat(f64, @as(c_int, 3)); |
| 1257 | 1228 | }); |
| 1258 | 1229 | |
| 1259 | 1230 | cases.add("conditional operator", |
| ... | ... | @@ -1263,8 +1234,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1263 | 1234 | \\} |
| 1264 | 1235 | , &[_][]const u8{ |
| 1265 | 1236 | \\pub export fn bar() c_int { |
| 1266 | | \\ if ((if (2 != 0) 5 else (if (5 != 0) 4 else 6)) != 0) _ = 2; |
| 1267 | | \\ return if (2 != 0) 5 else if (5 != 0) 4 else 6; |
| 1237 | \\ if ((if (@as(c_int, 2) != 0) @as(c_int, 5) else (if (@as(c_int, 5) != 0) @as(c_int, 4) else @as(c_int, 6))) != 0) _ = @as(c_int, 2); |
| 1238 | \\ return if (@as(c_int, 2) != 0) @as(c_int, 5) else if (@as(c_int, 5) != 0) @as(c_int, 4) else @as(c_int, 6); |
| 1268 | 1239 | \\} |
| 1269 | 1240 | }); |
| 1270 | 1241 | |
| ... | ... | @@ -1303,7 +1274,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1303 | 1274 | \\ } |
| 1304 | 1275 | \\ res = 2; |
| 1305 | 1276 | \\ } |
| 1306 | | \\ res = (3 * i); |
| 1277 | \\ res = (@as(c_int, 3) * i); |
| 1307 | 1278 | \\ break :__switch; |
| 1308 | 1279 | \\ } |
| 1309 | 1280 | \\ res = 5; |
| ... | ... | @@ -1397,6 +1368,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1397 | 1368 | \\} |
| 1398 | 1369 | }); |
| 1399 | 1370 | |
| 1371 | // TODO translate-c should in theory be able to figure out to drop all these casts |
| 1400 | 1372 | cases.add("escape sequences", |
| 1401 | 1373 | \\const char *escapes() { |
| 1402 | 1374 | \\char a = '\'', |
| ... | ... | @@ -1415,17 +1387,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1415 | 1387 | \\ |
| 1416 | 1388 | , &[_][]const u8{ |
| 1417 | 1389 | \\pub export fn escapes() [*c]const u8 { |
| 1418 | | \\ var a: u8 = @intCast(u8, '\''); |
| 1419 | | \\ var b: u8 = @intCast(u8, '\\'); |
| 1420 | | \\ var c: u8 = @intCast(u8, '\x07'); |
| 1421 | | \\ var d: u8 = @intCast(u8, '\x08'); |
| 1422 | | \\ var e: u8 = @intCast(u8, '\x0c'); |
| 1423 | | \\ var f: u8 = @intCast(u8, '\n'); |
| 1424 | | \\ var g: u8 = @intCast(u8, '\r'); |
| 1425 | | \\ var h: u8 = @intCast(u8, '\t'); |
| 1426 | | \\ var i: u8 = @intCast(u8, '\x0b'); |
| 1427 | | \\ var j: u8 = @intCast(u8, '\x00'); |
| 1428 | | \\ var k: u8 = @intCast(u8, '\"'); |
| 1390 | \\ var a: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\''))); |
| 1391 | \\ var b: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\\'))); |
| 1392 | \\ var c: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\x07'))); |
| 1393 | \\ var d: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\x08'))); |
| 1394 | \\ var e: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\x0c'))); |
| 1395 | \\ var f: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\n'))); |
| 1396 | \\ var g: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\r'))); |
| 1397 | \\ var h: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\t'))); |
| 1398 | \\ var i: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\x0b'))); |
| 1399 | \\ var j: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\x00'))); |
| 1400 | \\ var k: u8 = @bitCast(u8, @truncate(i8, @as(c_int, '\"'))); |
| 1429 | 1401 | \\ return "\'\\\x07\x08\x0c\n\r\t\x0b\x00\""; |
| 1430 | 1402 | \\} |
| 1431 | 1403 | }); |
| ... | ... | @@ -1446,12 +1418,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1446 | 1418 | \\pub export fn foo() void { |
| 1447 | 1419 | \\ var a: c_int = 2; |
| 1448 | 1420 | \\ while (true) { |
| 1449 | | \\ a = (a - 1); |
| 1421 | \\ a = (a - @as(c_int, 1)); |
| 1450 | 1422 | \\ if (!(a != 0)) break; |
| 1451 | 1423 | \\ } |
| 1452 | 1424 | \\ var b: c_int = 2; |
| 1453 | 1425 | \\ while (true) { |
| 1454 | | \\ b = (b - 1); |
| 1426 | \\ b = (b - @as(c_int, 1)); |
| 1455 | 1427 | \\ if (!(b != 0)) break; |
| 1456 | 1428 | \\ } |
| 1457 | 1429 | \\} |
| ... | ... | @@ -1602,7 +1574,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1602 | 1574 | \\pub const yes = [*c]u8; |
| 1603 | 1575 | \\pub export fn foo() void { |
| 1604 | 1576 | \\ var a: yes = undefined; |
| 1605 | | \\ if (a != null) _ = 2; |
| 1577 | \\ if (a != null) _ = @as(c_int, 2); |
| 1606 | 1578 | \\} |
| 1607 | 1579 | }); |
| 1608 | 1580 | |
| ... | ... | @@ -1695,7 +1667,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1695 | 1667 | \\} |
| 1696 | 1668 | }); |
| 1697 | 1669 | |
| 1698 | | cases.add("if statement", |
| 1670 | cases.add("simple if statement", |
| 1699 | 1671 | \\int max(int a, int b) { |
| 1700 | 1672 | \\ if (a < b) |
| 1701 | 1673 | \\ return b; |
| ... | ... | @@ -1717,6 +1689,29 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1717 | 1689 | \\} |
| 1718 | 1690 | }); |
| 1719 | 1691 | |
| 1692 | cases.add("if statements", |
| 1693 | \\int foo() { |
| 1694 | \\ if (2) { |
| 1695 | \\ int a = 2; |
| 1696 | \\ } |
| 1697 | \\ if (2, 5) { |
| 1698 | \\ int a = 2; |
| 1699 | \\ } |
| 1700 | \\} |
| 1701 | , &[_][]const u8{ |
| 1702 | \\pub export fn foo() c_int { |
| 1703 | \\ if (@as(c_int, 2) != 0) { |
| 1704 | \\ var a: c_int = 2; |
| 1705 | \\ } |
| 1706 | \\ if ((blk: { |
| 1707 | \\ _ = @as(c_int, 2); |
| 1708 | \\ break :blk @as(c_int, 5); |
| 1709 | \\ }) != 0) { |
| 1710 | \\ var a: c_int = 2; |
| 1711 | \\ } |
| 1712 | \\} |
| 1713 | }); |
| 1714 | |
| 1720 | 1715 | cases.add("if on non-bool", |
| 1721 | 1716 | \\enum SomeEnum { A, B, C }; |
| 1722 | 1717 | \\int if_none_bool(int a, float b, void *c, enum SomeEnum d) { |
| ... | ... | @@ -1764,7 +1759,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1764 | 1759 | , &[_][]const u8{ |
| 1765 | 1760 | \\pub export fn abs(arg_a: c_int) c_int { |
| 1766 | 1761 | \\ var a = arg_a; |
| 1767 | | \\ return if (a < 0) -a else a; |
| 1762 | \\ return if (a < @as(c_int, 0)) -a else a; |
| 1768 | 1763 | \\} |
| 1769 | 1764 | }); |
| 1770 | 1765 | |
| ... | ... | @@ -1845,7 +1840,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1845 | 1840 | , &[_][]const u8{ |
| 1846 | 1841 | \\pub export fn foo() void { |
| 1847 | 1842 | \\ var i: c_int = 0; |
| 1848 | | \\ var u: c_uint = @intCast(c_uint, 0); |
| 1843 | \\ var u: c_uint = @bitCast(c_uint, @as(c_int, 0)); |
| 1849 | 1844 | \\ i += 1; |
| 1850 | 1845 | \\ i -= 1; |
| 1851 | 1846 | \\ u +%= 1; |
| ... | ... | @@ -1885,7 +1880,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1885 | 1880 | \\pub export fn log2(arg_a: c_uint) c_int { |
| 1886 | 1881 | \\ var a = arg_a; |
| 1887 | 1882 | \\ var i: c_int = 0; |
| 1888 | | \\ while (a > @intCast(c_uint, 0)) { |
| 1883 | \\ while (a > @bitCast(c_uint, @as(c_int, 0))) { |
| 1889 | 1884 | \\ a >>= @as(@import("std").math.Log2Int(c_int), 1); |
| 1890 | 1885 | \\ } |
| 1891 | 1886 | \\ return i; |
| ... | ... | @@ -1905,7 +1900,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1905 | 1900 | \\pub export fn log2(arg_a: u32) c_int { |
| 1906 | 1901 | \\ var a = arg_a; |
| 1907 | 1902 | \\ var i: c_int = 0; |
| 1908 | | \\ while (a > @intCast(c_uint, 0)) { |
| 1903 | \\ while (a > @bitCast(c_uint, @as(c_int, 0))) { |
| 1909 | 1904 | \\ a >>= @as(@import("std").math.Log2Int(c_int), 1); |
| 1910 | 1905 | \\ } |
| 1911 | 1906 | \\ return i; |
| ... | ... | @@ -1929,42 +1924,42 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1929 | 1924 | \\ var a: c_int = 0; |
| 1930 | 1925 | \\ a += (blk: { |
| 1931 | 1926 | \\ const ref = &a; |
| 1932 | | \\ ref.* = ref.* + 1; |
| 1927 | \\ ref.* = ref.* + @as(c_int, 1); |
| 1933 | 1928 | \\ break :blk ref.*; |
| 1934 | 1929 | \\ }); |
| 1935 | 1930 | \\ a -= (blk: { |
| 1936 | 1931 | \\ const ref = &a; |
| 1937 | | \\ ref.* = ref.* - 1; |
| 1932 | \\ ref.* = ref.* - @as(c_int, 1); |
| 1938 | 1933 | \\ break :blk ref.*; |
| 1939 | 1934 | \\ }); |
| 1940 | 1935 | \\ a *= (blk: { |
| 1941 | 1936 | \\ const ref = &a; |
| 1942 | | \\ ref.* = ref.* * 1; |
| 1937 | \\ ref.* = ref.* * @as(c_int, 1); |
| 1943 | 1938 | \\ break :blk ref.*; |
| 1944 | 1939 | \\ }); |
| 1945 | 1940 | \\ a &= (blk: { |
| 1946 | 1941 | \\ const ref = &a; |
| 1947 | | \\ ref.* = ref.* & 1; |
| 1942 | \\ ref.* = ref.* & @as(c_int, 1); |
| 1948 | 1943 | \\ break :blk ref.*; |
| 1949 | 1944 | \\ }); |
| 1950 | 1945 | \\ a |= (blk: { |
| 1951 | 1946 | \\ const ref = &a; |
| 1952 | | \\ ref.* = ref.* | 1; |
| 1947 | \\ ref.* = ref.* | @as(c_int, 1); |
| 1953 | 1948 | \\ break :blk ref.*; |
| 1954 | 1949 | \\ }); |
| 1955 | 1950 | \\ a ^= (blk: { |
| 1956 | 1951 | \\ const ref = &a; |
| 1957 | | \\ ref.* = ref.* ^ 1; |
| 1952 | \\ ref.* = ref.* ^ @as(c_int, 1); |
| 1958 | 1953 | \\ break :blk ref.*; |
| 1959 | 1954 | \\ }); |
| 1960 | 1955 | \\ a >>= @as(@import("std").math.Log2Int(c_int), (blk: { |
| 1961 | 1956 | \\ const ref = &a; |
| 1962 | | \\ ref.* = ref.* >> @as(@import("std").math.Log2Int(c_int), 1); |
| 1957 | \\ ref.* = ref.* >> @as(@import("std").math.Log2Int(c_int), @as(c_int, 1)); |
| 1963 | 1958 | \\ break :blk ref.*; |
| 1964 | 1959 | \\ })); |
| 1965 | 1960 | \\ a <<= @as(@import("std").math.Log2Int(c_int), (blk: { |
| 1966 | 1961 | \\ const ref = &a; |
| 1967 | | \\ ref.* = ref.* << @as(@import("std").math.Log2Int(c_int), 1); |
| 1962 | \\ ref.* = ref.* << @as(@import("std").math.Log2Int(c_int), @as(c_int, 1)); |
| 1968 | 1963 | \\ break :blk ref.*; |
| 1969 | 1964 | \\ })); |
| 1970 | 1965 | \\} |
| ... | ... | @@ -1984,45 +1979,45 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1984 | 1979 | \\} |
| 1985 | 1980 | , &[_][]const u8{ |
| 1986 | 1981 | \\pub export fn foo() void { |
| 1987 | | \\ var a: c_uint = @intCast(c_uint, 0); |
| 1982 | \\ var a: c_uint = @bitCast(c_uint, @as(c_int, 0)); |
| 1988 | 1983 | \\ a +%= (blk: { |
| 1989 | 1984 | \\ const ref = &a; |
| 1990 | | \\ ref.* = ref.* +% @intCast(c_uint, 1); |
| 1985 | \\ ref.* = ref.* +% @bitCast(c_uint, @as(c_int, 1)); |
| 1991 | 1986 | \\ break :blk ref.*; |
| 1992 | 1987 | \\ }); |
| 1993 | 1988 | \\ a -%= (blk: { |
| 1994 | 1989 | \\ const ref = &a; |
| 1995 | | \\ ref.* = ref.* -% @intCast(c_uint, 1); |
| 1990 | \\ ref.* = ref.* -% @bitCast(c_uint, @as(c_int, 1)); |
| 1996 | 1991 | \\ break :blk ref.*; |
| 1997 | 1992 | \\ }); |
| 1998 | 1993 | \\ a *%= (blk: { |
| 1999 | 1994 | \\ const ref = &a; |
| 2000 | | \\ ref.* = ref.* *% @intCast(c_uint, 1); |
| 1995 | \\ ref.* = ref.* *% @bitCast(c_uint, @as(c_int, 1)); |
| 2001 | 1996 | \\ break :blk ref.*; |
| 2002 | 1997 | \\ }); |
| 2003 | 1998 | \\ a &= (blk: { |
| 2004 | 1999 | \\ const ref = &a; |
| 2005 | | \\ ref.* = ref.* & @intCast(c_uint, 1); |
| 2000 | \\ ref.* = ref.* & @bitCast(c_uint, @as(c_int, 1)); |
| 2006 | 2001 | \\ break :blk ref.*; |
| 2007 | 2002 | \\ }); |
| 2008 | 2003 | \\ a |= (blk: { |
| 2009 | 2004 | \\ const ref = &a; |
| 2010 | | \\ ref.* = ref.* | @intCast(c_uint, 1); |
| 2005 | \\ ref.* = ref.* | @bitCast(c_uint, @as(c_int, 1)); |
| 2011 | 2006 | \\ break :blk ref.*; |
| 2012 | 2007 | \\ }); |
| 2013 | 2008 | \\ a ^= (blk: { |
| 2014 | 2009 | \\ const ref = &a; |
| 2015 | | \\ ref.* = ref.* ^ @intCast(c_uint, 1); |
| 2010 | \\ ref.* = ref.* ^ @bitCast(c_uint, @as(c_int, 1)); |
| 2016 | 2011 | \\ break :blk ref.*; |
| 2017 | 2012 | \\ }); |
| 2018 | 2013 | \\ a >>= @as(@import("std").math.Log2Int(c_uint), (blk: { |
| 2019 | 2014 | \\ const ref = &a; |
| 2020 | | \\ ref.* = ref.* >> @as(@import("std").math.Log2Int(c_int), 1); |
| 2015 | \\ ref.* = ref.* >> @as(@import("std").math.Log2Int(c_int), @as(c_int, 1)); |
| 2021 | 2016 | \\ break :blk ref.*; |
| 2022 | 2017 | \\ })); |
| 2023 | 2018 | \\ a <<= @as(@import("std").math.Log2Int(c_uint), (blk: { |
| 2024 | 2019 | \\ const ref = &a; |
| 2025 | | \\ ref.* = ref.* << @as(@import("std").math.Log2Int(c_int), 1); |
| 2020 | \\ ref.* = ref.* << @as(@import("std").math.Log2Int(c_int), @as(c_int, 1)); |
| 2026 | 2021 | \\ break :blk ref.*; |
| 2027 | 2022 | \\ })); |
| 2028 | 2023 | \\} |
| ... | ... | @@ -2044,7 +2039,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2044 | 2039 | , &[_][]const u8{ |
| 2045 | 2040 | \\pub export fn foo() void { |
| 2046 | 2041 | \\ var i: c_int = 0; |
| 2047 | | \\ var u: c_uint = @intCast(c_uint, 0); |
| 2042 | \\ var u: c_uint = @bitCast(c_uint, @as(c_int, 0)); |
| 2048 | 2043 | \\ i += 1; |
| 2049 | 2044 | \\ i -= 1; |
| 2050 | 2045 | \\ u +%= 1; |
| ... | ... | @@ -2115,19 +2110,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2115 | 2110 | \\ fn_int(@floatToInt(c_int, 3)); |
| 2116 | 2111 | \\ fn_int(@floatToInt(c_int, 3)); |
| 2117 | 2112 | \\ fn_int(@floatToInt(c_int, 3)); |
| 2118 | | \\ fn_int(1094861636); |
| 2119 | | \\ fn_f32(@intToFloat(f32, 3)); |
| 2120 | | \\ fn_f64(@intToFloat(f64, 3)); |
| 2121 | | \\ fn_char(@intCast(u8, '3')); |
| 2122 | | \\ fn_char(@intCast(u8, '\x01')); |
| 2123 | | \\ fn_char(@intCast(u8, 0)); |
| 2113 | \\ fn_int(@as(c_int, 1094861636)); |
| 2114 | \\ fn_f32(@intToFloat(f32, @as(c_int, 3))); |
| 2115 | \\ fn_f64(@intToFloat(f64, @as(c_int, 3))); |
| 2116 | \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3')))); |
| 2117 | \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01')))); |
| 2118 | \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0)))); |
| 2124 | 2119 | \\ fn_f32(3); |
| 2125 | 2120 | \\ fn_f64(3); |
| 2126 | | \\ fn_bool(123 != 0); |
| 2127 | | \\ fn_bool(0 != 0); |
| 2121 | \\ fn_bool(@as(c_int, 123) != 0); |
| 2122 | \\ fn_bool(@as(c_int, 0) != 0); |
| 2128 | 2123 | \\ fn_bool(@ptrToInt(&fn_int) != 0); |
| 2129 | 2124 | \\ fn_int(@intCast(c_int, @ptrToInt(&fn_int))); |
| 2130 | | \\ fn_ptr(@intToPtr(?*c_void, 42)); |
| 2125 | \\ fn_ptr(@intToPtr(?*c_void, @as(c_int, 42))); |
| 2131 | 2126 | \\} |
| 2132 | 2127 | }); |
| 2133 | 2128 | |
| ... | ... | @@ -2223,8 +2218,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2223 | 2218 | \\} |
| 2224 | 2219 | , &[_][]const u8{ |
| 2225 | 2220 | \\pub fn foo() void { |
| 2226 | | \\ if (1 != 0) while (true) { |
| 2227 | | \\ if (!(0 != 0)) break; |
| 2221 | \\ if (@as(c_int, 1) != 0) while (true) { |
| 2222 | \\ if (!(@as(c_int, 0) != 0)) break; |
| 2228 | 2223 | \\ }; |
| 2229 | 2224 | \\} |
| 2230 | 2225 | }); |
| ... | ... | @@ -2263,4 +2258,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2263 | 2258 | \\ }; |
| 2264 | 2259 | \\} |
| 2265 | 2260 | }); |
| 2261 | |
| 2262 | cases.add("widening and truncating integer casting to different signedness", |
| 2263 | \\unsigned long foo(void) { |
| 2264 | \\ return -1; |
| 2265 | \\} |
| 2266 | \\unsigned short bar(long x) { |
| 2267 | \\ return x; |
| 2268 | \\} |
| 2269 | , &[_][]const u8{ |
| 2270 | \\pub export fn foo() c_ulong { |
| 2271 | \\ return @bitCast(c_ulong, @as(c_long, -@as(c_int, 1))); |
| 2272 | \\} |
| 2273 | \\pub export fn bar(arg_x: c_long) c_ushort { |
| 2274 | \\ var x = arg_x; |
| 2275 | \\ return @bitCast(c_ushort, @truncate(c_short, x)); |
| 2276 | \\} |
| 2277 | }); |
| 2266 | 2278 | } |