| ... | @@ -1096,7 +1096,7 @@ test "sizeof" { | ... | @@ -1096,7 +1096,7 @@ test "sizeof" { |
| 1096 | | 1096 | |
| 1097 | pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal }; | 1097 | pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal }; |
| 1098 | | 1098 | |
| 1099 | fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: comptime_int, comptime radix: CIntLiteralRadix) type { | 1099 | fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type { |
| 1100 | const signed_decimal = [_]type{ c_int, c_long, c_longlong }; | 1100 | const signed_decimal = [_]type{ c_int, c_long, c_longlong }; |
| 1101 | const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong }; | 1101 | const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong }; |
| 1102 | const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong }; | 1102 | const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong }; |
| ... | @@ -1111,17 +1111,39 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: compt | ... | @@ -1111,17 +1111,39 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: compt |
| 1111 | var pos = mem.indexOfScalar(type, list, SuffixType).?; | 1111 | var pos = mem.indexOfScalar(type, list, SuffixType).?; |
| 1112 | | 1112 | |
| 1113 | while (pos < list.len) : (pos += 1) { | 1113 | while (pos < list.len) : (pos += 1) { |
| 1114 | if (target >= math.minInt(list[pos]) and target <= math.maxInt(list[pos])) { | 1114 | if (number >= math.minInt(list[pos]) and number <= math.maxInt(list[pos])) { |
| 1115 | return list[pos]; | 1115 | return list[pos]; |
| 1116 | } | 1116 | } |
| 1117 | } | 1117 | } |
| 1118 | @compileError("Integer literal does not fit in compatible types"); | 1118 | @compileError("Integer literal is too large"); |
| 1119 | } | 1119 | } |
| 1120 | | 1120 | |
| 1121 | /// Promote the type of an integer literal until it fits as C would. | 1121 | /// Promote the type of an integer literal until it fits as C would. |
| 1122 | /// This is for translate-c and is not intended for general use. | 1122 | /// This is for translate-c and is not intended for general use. |
| 1123 | pub fn promoteIntLiteral(comptime SuffixType: type, comptime target: comptime_int, comptime radix: CIntLiteralRadix) PromoteIntLiteralReturnType(SuffixType, target, radix) { | 1123 | pub fn promoteIntLiteral( |
| 1124 | return target; | 1124 | comptime SuffixType: type, |
| | 1125 | comptime number: comptime_int, |
| | 1126 | comptime radix: CIntLiteralRadix, |
| | 1127 | ) PromoteIntLiteralReturnType(SuffixType, number, radix) { |
| | 1128 | return number; |
| | 1129 | } |
| | 1130 | |
| | 1131 | test "promoteIntLiteral" { |
| | 1132 | const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hexadecimal); |
| | 1133 | testing.expectEqual(c_uint, @TypeOf(signed_hex)); |
| | 1134 | |
| | 1135 | if (math.maxInt(c_longlong) == math.maxInt(c_int)) return; |
| | 1136 | |
| | 1137 | const signed_decimal = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .decimal); |
| | 1138 | const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hexadecimal); |
| | 1139 | |
| | 1140 | if (math.maxInt(c_long) > math.maxInt(c_int)) { |
| | 1141 | testing.expectEqual(c_long, @TypeOf(signed_decimal)); |
| | 1142 | testing.expectEqual(c_ulong, @TypeOf(unsigned)); |
| | 1143 | } else { |
| | 1144 | testing.expectEqual(c_longlong, @TypeOf(signed_decimal)); |
| | 1145 | testing.expectEqual(c_ulonglong, @TypeOf(unsigned)); |
| | 1146 | } |
| 1125 | } | 1147 | } |
| 1126 | | 1148 | |
| 1127 | /// For a given function type, returns a tuple type which fields will | 1149 | /// For a given function type, returns a tuple type which fields will |