| ... | ... | @@ -232,12 +232,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 232 | 232 | \\ | (*((unsigned char *)(p) + 1) << 8) \ |
| 233 | 233 | \\ | (*((unsigned char *)(p) + 2) << 16)) |
| 234 | 234 | , &[_][]const u8{ |
| 235 | | \\pub const FOO = (foo + 2).*; |
| 235 | \\pub const FOO = (foo + @as(c_int, 2)).*; |
| 236 | 236 | , |
| 237 | | \\pub const VALUE = ((((1 + (2 * 3)) + (4 * 5)) + 6) << 7) | @boolToInt(8 == 9); |
| 237 | \\pub const VALUE = ((((@as(c_int, 1) + (@as(c_int, 2) * @as(c_int, 3))) + (@as(c_int, 4) * @as(c_int, 5))) + @as(c_int, 6)) << @as(c_int, 7)) | @boolToInt(@as(c_int, 8) == @as(c_int, 9)); |
| 238 | 238 | , |
| 239 | | \\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)) { |
| 240 | | \\ 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); |
| 239 | \\pub fn _AL_READ3BYTES(p: anytype) callconv(.Inline) @TypeOf((@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16))) { |
| 240 | \\ return (@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16)); |
| 241 | 241 | \\} |
| 242 | 242 | }); |
| 243 | 243 | |
| ... | ... | @@ -312,14 +312,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 312 | 312 | \\ return type_1; |
| 313 | 313 | \\} |
| 314 | 314 | , |
| 315 | | \\pub const LIGHTGRAY = @import("std").mem.zeroInit(CLITERAL(Color), .{ 200, 200, 200, 255 }); |
| 315 | \\pub const LIGHTGRAY = @import("std").mem.zeroInit(CLITERAL(Color), .{ @as(c_int, 200), @as(c_int, 200), @as(c_int, 200), @as(c_int, 255) }); |
| 316 | 316 | , |
| 317 | 317 | \\pub const struct_boom_t = extern struct { |
| 318 | 318 | \\ i1: c_int, |
| 319 | 319 | \\}; |
| 320 | 320 | \\pub const boom_t = struct_boom_t; |
| 321 | 321 | , |
| 322 | | \\pub const FOO = @import("std").mem.zeroInit(boom_t, .{1}); |
| 322 | \\pub const FOO = @import("std").mem.zeroInit(boom_t, .{@as(c_int, 1)}); |
| 323 | 323 | }); |
| 324 | 324 | |
| 325 | 325 | cases.add("complex switch", |
| ... | ... | @@ -343,8 +343,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 343 | 343 | cases.add("correct semicolon after infixop", |
| 344 | 344 | \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0) |
| 345 | 345 | , &[_][]const u8{ |
| 346 | | \\pub fn __ferror_unlocked_body(_fp: anytype) callconv(.Inline) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != 0) { |
| 347 | | \\ return (_fp.*._flags & _IO_ERR_SEEN) != 0; |
| 346 | \\pub fn __ferror_unlocked_body(_fp: anytype) callconv(.Inline) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) { |
| 347 | \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0); |
| 348 | 348 | \\} |
| 349 | 349 | }); |
| 350 | 350 | |
| ... | ... | @@ -352,11 +352,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 352 | 352 | \\#define FOO(x) ((x >= 0) + (x >= 0)) |
| 353 | 353 | \\#define BAR 1 && 2 > 4 |
| 354 | 354 | , &[_][]const u8{ |
| 355 | | \\pub fn FOO(x: anytype) callconv(.Inline) @TypeOf(@boolToInt(x >= 0) + @boolToInt(x >= 0)) { |
| 356 | | \\ return @boolToInt(x >= 0) + @boolToInt(x >= 0); |
| 355 | \\pub fn FOO(x: anytype) callconv(.Inline) @TypeOf(@boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0))) { |
| 356 | \\ return @boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0)); |
| 357 | 357 | \\} |
| 358 | 358 | , |
| 359 | | \\pub const BAR = (1 != 0) and (2 > 4); |
| 359 | \\pub const BAR = (@as(c_int, 1) != 0) and (@as(c_int, 2) > @as(c_int, 4)); |
| 360 | 360 | }); |
| 361 | 361 | |
| 362 | 362 | cases.add("struct with aligned fields", |
| ... | ... | @@ -401,15 +401,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 401 | 401 | \\ break :blk bar; |
| 402 | 402 | \\}; |
| 403 | 403 | , |
| 404 | | \\pub fn bar(x: anytype) callconv(.Inline) @TypeOf(baz(1, 2)) { |
| 404 | \\pub fn bar(x: anytype) callconv(.Inline) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) { |
| 405 | 405 | \\ return blk: { |
| 406 | 406 | \\ _ = &x; |
| 407 | | \\ _ = 3; |
| 408 | | \\ _ = 4 == 4; |
| 409 | | \\ _ = 5 * 6; |
| 410 | | \\ _ = baz(1, 2); |
| 411 | | \\ _ = 2 % 2; |
| 412 | | \\ break :blk baz(1, 2); |
| 407 | \\ _ = @as(c_int, 3); |
| 408 | \\ _ = @as(c_int, 4) == @as(c_int, 4); |
| 409 | \\ _ = @as(c_int, 5) * @as(c_int, 6); |
| 410 | \\ _ = baz(@as(c_int, 1), @as(c_int, 2)); |
| 411 | \\ _ = @as(c_int, 2) % @as(c_int, 2); |
| 412 | \\ break :blk baz(@as(c_int, 1), @as(c_int, 2)); |
| 413 | 413 | \\ }; |
| 414 | 414 | \\} |
| 415 | 415 | }); |
| ... | ... | @@ -418,9 +418,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 418 | 418 | \\#define foo 1 |
| 419 | 419 | \\#define inline 2 |
| 420 | 420 | , &[_][]const u8{ |
| 421 | | \\pub const foo = 1; |
| 421 | \\pub const foo = @as(c_int, 1); |
| 422 | 422 | , |
| 423 | | \\pub const @"inline" = 2; |
| 423 | \\pub const @"inline" = @as(c_int, 2); |
| 424 | 424 | }); |
| 425 | 425 | |
| 426 | 426 | cases.add("macro line continuation", |
| ... | ... | @@ -507,7 +507,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 507 | 507 | cases.add("#define hex literal with capital X", |
| 508 | 508 | \\#define VAL 0XF00D |
| 509 | 509 | , &[_][]const u8{ |
| 510 | | \\pub const VAL = 0xF00D; |
| 510 | \\pub const VAL = @import("std").meta.promoteIntLiteral(c_int, 0xF00D, .hexadecimal); |
| 511 | 511 | }); |
| 512 | 512 | |
| 513 | 513 | cases.add("anonymous struct & unions", |
| ... | ... | @@ -878,7 +878,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 878 | 878 | cases.add("macro with left shift", |
| 879 | 879 | \\#define REDISMODULE_READ (1<<0) |
| 880 | 880 | , &[_][]const u8{ |
| 881 | | \\pub const REDISMODULE_READ = 1 << 0; |
| 881 | \\pub const REDISMODULE_READ = @as(c_int, 1) << @as(c_int, 0); |
| 882 | 882 | }); |
| 883 | 883 | |
| 884 | 884 | cases.add("macro with right shift", |
| ... | ... | @@ -887,7 +887,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 887 | 887 | , &[_][]const u8{ |
| 888 | 888 | \\pub const FLASH_SIZE = @as(c_ulong, 0x200000); |
| 889 | 889 | , |
| 890 | | \\pub const FLASH_BANK_SIZE = FLASH_SIZE >> 1; |
| 890 | \\pub const FLASH_BANK_SIZE = FLASH_SIZE >> @as(c_int, 1); |
| 891 | 891 | }); |
| 892 | 892 | |
| 893 | 893 | cases.add("double define struct", |
| ... | ... | @@ -955,14 +955,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 955 | 955 | cases.add("#define an unsigned integer literal", |
| 956 | 956 | \\#define CHANNEL_COUNT 24 |
| 957 | 957 | , &[_][]const u8{ |
| 958 | | \\pub const CHANNEL_COUNT = 24; |
| 958 | \\pub const CHANNEL_COUNT = @as(c_int, 24); |
| 959 | 959 | }); |
| 960 | 960 | |
| 961 | 961 | cases.add("#define referencing another #define", |
| 962 | 962 | \\#define THING2 THING1 |
| 963 | 963 | \\#define THING1 1234 |
| 964 | 964 | , &[_][]const u8{ |
| 965 | | \\pub const THING1 = 1234; |
| 965 | \\pub const THING1 = @as(c_int, 1234); |
| 966 | 966 | , |
| 967 | 967 | \\pub const THING2 = THING1; |
| 968 | 968 | }); |
| ... | ... | @@ -1008,7 +1008,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1008 | 1008 | cases.add("macro with parens around negative number", |
| 1009 | 1009 | \\#define LUA_GLOBALSINDEX (-10002) |
| 1010 | 1010 | , &[_][]const u8{ |
| 1011 | | \\pub const LUA_GLOBALSINDEX = -10002; |
| 1011 | \\pub const LUA_GLOBALSINDEX = -@as(c_int, 10002); |
| 1012 | 1012 | }); |
| 1013 | 1013 | |
| 1014 | 1014 | cases.add( |
| ... | ... | @@ -1091,8 +1091,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1091 | 1091 | \\#define foo 1 //foo |
| 1092 | 1092 | \\#define bar /* bar */ 2 |
| 1093 | 1093 | , &[_][]const u8{ |
| 1094 | | "pub const foo = 1;", |
| 1095 | | "pub const bar = 2;", |
| 1094 | "pub const foo = @as(c_int, 1);", |
| 1095 | "pub const bar = @as(c_int, 2);", |
| 1096 | 1096 | }); |
| 1097 | 1097 | |
| 1098 | 1098 | cases.add("string prefix", |
| ... | ... | @@ -1722,7 +1722,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1722 | 1722 | cases.add("comment after integer literal", |
| 1723 | 1723 | \\#define SDL_INIT_VIDEO 0x00000020 /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */ |
| 1724 | 1724 | , &[_][]const u8{ |
| 1725 | | \\pub const SDL_INIT_VIDEO = 0x00000020; |
| 1725 | \\pub const SDL_INIT_VIDEO = @as(c_int, 0x00000020); |
| 1726 | 1726 | }); |
| 1727 | 1727 | |
| 1728 | 1728 | cases.add("u integer suffix after hex literal", |
| ... | ... | @@ -1836,8 +1836,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1836 | 1836 | , &[_][]const u8{ |
| 1837 | 1837 | \\pub extern var c: c_int; |
| 1838 | 1838 | , |
| 1839 | | \\pub fn BASIC(c_1: anytype) callconv(.Inline) @TypeOf(c_1 * 2) { |
| 1840 | | \\ return c_1 * 2; |
| 1839 | \\pub fn BASIC(c_1: anytype) callconv(.Inline) @TypeOf(c_1 * @as(c_int, 2)) { |
| 1840 | \\ return c_1 * @as(c_int, 2); |
| 1841 | 1841 | \\} |
| 1842 | 1842 | , |
| 1843 | 1843 | \\pub fn FOO(L: anytype, b: anytype) callconv(.Inline) @TypeOf(L + b) { |
| ... | ... | @@ -2481,7 +2481,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2481 | 2481 | \\ return array[@intCast(c_uint, index)]; |
| 2482 | 2482 | \\} |
| 2483 | 2483 | , |
| 2484 | | \\pub const ACCESS = array[2]; |
| 2484 | \\pub const ACCESS = array[@as(c_int, 2)]; |
| 2485 | 2485 | }); |
| 2486 | 2486 | |
| 2487 | 2487 | cases.add("cast signed array index to unsigned", |
| ... | ... | @@ -3097,7 +3097,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3097 | 3097 | , |
| 3098 | 3098 | \\pub const BAR = @import("std").meta.cast(?*c_void, a); |
| 3099 | 3099 | , |
| 3100 | | \\pub const BAZ = @import("std").meta.cast(u32, 2); |
| 3100 | \\pub const BAZ = @import("std").meta.cast(u32, @as(c_int, 2)); |
| 3101 | 3101 | }); |
| 3102 | 3102 | |
| 3103 | 3103 | cases.add("macro with cast to unsigned short, long, and long long", |
| ... | ... | @@ -3105,9 +3105,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3105 | 3105 | \\#define CURLAUTH_BASIC ((unsigned long) 1) |
| 3106 | 3106 | \\#define CURLAUTH_BASIC_BUT_ULONGLONG ((unsigned long long) 1) |
| 3107 | 3107 | , &[_][]const u8{ |
| 3108 | | \\pub const CURLAUTH_BASIC_BUT_USHORT = @import("std").meta.cast(c_ushort, 1); |
| 3109 | | \\pub const CURLAUTH_BASIC = @import("std").meta.cast(c_ulong, 1); |
| 3110 | | \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = @import("std").meta.cast(c_ulonglong, 1); |
| 3108 | \\pub const CURLAUTH_BASIC_BUT_USHORT = @import("std").meta.cast(c_ushort, @as(c_int, 1)); |
| 3109 | \\pub const CURLAUTH_BASIC = @import("std").meta.cast(c_ulong, @as(c_int, 1)); |
| 3110 | \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = @import("std").meta.cast(c_ulonglong, @as(c_int, 1)); |
| 3111 | 3111 | }); |
| 3112 | 3112 | |
| 3113 | 3113 | cases.add("macro conditional operator", |
| ... | ... | @@ -3202,7 +3202,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3202 | 3202 | \\ bar_1 = 2; |
| 3203 | 3203 | \\} |
| 3204 | 3204 | , |
| 3205 | | \\pub const bar = 4; |
| 3205 | \\pub const bar = @as(c_int, 4); |
| 3206 | 3206 | }); |
| 3207 | 3207 | |
| 3208 | 3208 | cases.add("don't export inline functions", |
| ... | ... | @@ -3331,9 +3331,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3331 | 3331 | \\#define NULL ((void*)0) |
| 3332 | 3332 | \\#define FOO ((int)0x8000) |
| 3333 | 3333 | , &[_][]const u8{ |
| 3334 | | \\pub const NULL = @import("std").meta.cast(?*c_void, 0); |
| 3334 | \\pub const NULL = @import("std").meta.cast(?*c_void, @as(c_int, 0)); |
| 3335 | 3335 | , |
| 3336 | | \\pub const FOO = @import("std").meta.cast(c_int, 0x8000); |
| 3336 | \\pub const FOO = @import("std").meta.cast(c_int, @import("std").meta.promoteIntLiteral(c_int, 0x8000, .hexadecimal)); |
| 3337 | 3337 | }); |
| 3338 | 3338 | |
| 3339 | 3339 | if (std.Target.current.abi == .msvc) { |
| ... | ... | @@ -3398,4 +3398,24 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3398 | 3398 | \\ unnamed_0: struct_unnamed_2, |
| 3399 | 3399 | \\}; |
| 3400 | 3400 | }); |
| 3401 | |
| 3402 | cases.add("integer literal promotion", |
| 3403 | \\#define GUARANTEED_TO_FIT_1 1024 |
| 3404 | \\#define GUARANTEED_TO_FIT_2 10241024L |
| 3405 | \\#define GUARANTEED_TO_FIT_3 20482048LU |
| 3406 | \\#define MAY_NEED_PROMOTION_1 10241024 |
| 3407 | \\#define MAY_NEED_PROMOTION_2 307230723072L |
| 3408 | \\#define MAY_NEED_PROMOTION_3 819281928192LU |
| 3409 | \\#define MAY_NEED_PROMOTION_HEX 0x80000000 |
| 3410 | \\#define MAY_NEED_PROMOTION_OCT 020000000000 |
| 3411 | , &[_][]const u8{ |
| 3412 | \\pub const GUARANTEED_TO_FIT_1 = @as(c_int, 1024); |
| 3413 | \\pub const GUARANTEED_TO_FIT_2 = @as(c_long, 10241024); |
| 3414 | \\pub const GUARANTEED_TO_FIT_3 = @as(c_ulong, 20482048); |
| 3415 | \\pub const MAY_NEED_PROMOTION_1 = @import("std").meta.promoteIntLiteral(c_int, 10241024, .decimal); |
| 3416 | \\pub const MAY_NEED_PROMOTION_2 = @import("std").meta.promoteIntLiteral(c_long, 307230723072, .decimal); |
| 3417 | \\pub const MAY_NEED_PROMOTION_3 = @import("std").meta.promoteIntLiteral(c_ulong, 819281928192, .decimal); |
| 3418 | \\pub const MAY_NEED_PROMOTION_HEX = @import("std").meta.promoteIntLiteral(c_int, 0x80000000, .hexadecimal); |
| 3419 | \\pub const MAY_NEED_PROMOTION_OCT = @import("std").meta.promoteIntLiteral(c_int, 0o20000000000, .octal); |
| 3420 | }); |
| 3401 | 3421 | } |