| ... | ... | @@ -384,16 +384,16 @@ pub const Macros = struct { |
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | fn L_SUFFIX_ReturnType(comptime number: anytype) type { |
| 387 | | switch (@TypeOf(number)) { |
| 388 | | comptime_int => return @TypeOf(promoteIntLiteral(c_long, number, .decimal)), |
| 389 | | comptime_float => return c_longdouble, |
| 387 | switch (@typeInfo(@TypeOf(number))) { |
| 388 | .Int, .ComptimeInt => return @TypeOf(promoteIntLiteral(c_long, number, .decimal)), |
| 389 | .Float, .ComptimeFloat => return c_longdouble, |
| 390 | 390 | else => @compileError("Invalid value for L suffix"), |
| 391 | 391 | } |
| 392 | 392 | } |
| 393 | 393 | pub fn L_SUFFIX(comptime number: anytype) L_SUFFIX_ReturnType(number) { |
| 394 | | switch (@TypeOf(number)) { |
| 395 | | comptime_int => return promoteIntLiteral(c_long, number, .decimal), |
| 396 | | comptime_float => @compileError("TODO: c_longdouble initialization from comptime_float not supported"), |
| 394 | switch (@typeInfo(@TypeOf(number))) { |
| 395 | .Int, .ComptimeInt => return promoteIntLiteral(c_long, number, .decimal), |
| 396 | .Float, .ComptimeFloat => @compileError("TODO: c_longdouble initialization from comptime_float not supported"), |
| 397 | 397 | else => @compileError("Invalid value for L suffix"), |
| 398 | 398 | } |
| 399 | 399 | } |
| ... | ... | @@ -644,3 +644,30 @@ test "CAST_OR_CALL calling" { |
| 644 | 644 | |
| 645 | 645 | try testing.expectEqual(Helper.identity(@as(c_uint, 100)), Macros.CAST_OR_CALL(Helper.identity, @as(c_uint, 100))); |
| 646 | 646 | } |
| 647 | |
| 648 | test "Extended C ABI casting" { |
| 649 | if (math.maxInt(c_long) > math.maxInt(c_char)) { |
| 650 | try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_char, math.maxInt(c_char) - 1))) == c_long); // c_char |
| 651 | } |
| 652 | if (math.maxInt(c_long) > math.maxInt(c_short)) { |
| 653 | try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_short, math.maxInt(c_short) - 1))) == c_long); // c_short |
| 654 | } |
| 655 | |
| 656 | if (math.maxInt(c_long) > math.maxInt(c_ushort)) { |
| 657 | try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_ushort, math.maxInt(c_ushort) - 1))) == c_long); //c_ushort |
| 658 | } |
| 659 | |
| 660 | if (math.maxInt(c_long) > math.maxInt(c_int)) { |
| 661 | try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_int, math.maxInt(c_int) - 1))) == c_long); // c_int |
| 662 | } |
| 663 | |
| 664 | if (math.maxInt(c_long) > math.maxInt(c_uint)) { |
| 665 | try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_uint, math.maxInt(c_uint) - 1))) == c_long); // c_uint |
| 666 | try testing.expect(@TypeOf(Macros.L_SUFFIX(math.maxInt(c_uint) + 1)) == c_long); // comptime_int -> c_long |
| 667 | } |
| 668 | |
| 669 | if (math.maxInt(c_longlong) > math.maxInt(c_long)) { |
| 670 | try testing.expect(@TypeOf(Macros.L_SUFFIX(@as(c_long, math.maxInt(c_long) - 1))) == c_long); // c_long |
| 671 | try testing.expect(@TypeOf(Macros.L_SUFFIX(math.maxInt(c_long) + 1)) == c_longlong); // comptime_int -> c_longlong |
| 672 | } |
| 673 | } |