authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-05-20 21:58:39+02:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-05-20 21:59:46+02:00
log563ea60a86a733f53f2394a11cb9ec4e56063fa3
treeea6355892f8fa02ef972d6651551eec513d290ed
parent5b850d5c9251900962e52154eb0fb9c2a344476d
signaturelock-open Commit is signed but in an unrecognized format.

translate-c: use inline keyword instead of callconv(.Inline)


2 files changed, 43 insertions(+), 61 deletions(-)

src/translate_c/ast.zig+14-32
...@@ -2689,6 +2689,7 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {...@@ -2689,6 +2689,7 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {
2689fn renderMacroFunc(c: *Context, node: Node) !NodeIndex {2689fn renderMacroFunc(c: *Context, node: Node) !NodeIndex {
2690 const payload = node.castTag(.pub_inline_fn).?.data;2690 const payload = node.castTag(.pub_inline_fn).?.data;
2691 _ = try c.addToken(.keyword_pub, "pub");2691 _ = try c.addToken(.keyword_pub, "pub");
2692 _ = try c.addToken(.keyword_inline, "inline");
2692 const fn_token = try c.addToken(.keyword_fn, "fn");2693 const fn_token = try c.addToken(.keyword_fn, "fn");
2693 _ = try c.addIdentifier(payload.name);2694 _ = try c.addIdentifier(payload.name);
26942695
...@@ -2697,50 +2698,31 @@ fn renderMacroFunc(c: *Context, node: Node) !NodeIndex {...@@ -2697,50 +2698,31 @@ fn renderMacroFunc(c: *Context, node: Node) !NodeIndex {
2697 var span: NodeSubRange = undefined;2698 var span: NodeSubRange = undefined;
2698 if (params.items.len > 1) span = try c.listToSpan(params.items);2699 if (params.items.len > 1) span = try c.listToSpan(params.items);
26992700
2700 const callconv_expr = blk: {
2701 _ = try c.addToken(.keyword_callconv, "callconv");
2702 _ = try c.addToken(.l_paren, "(");
2703 _ = try c.addToken(.period, ".");
2704 const res = try c.addNode(.{
2705 .tag = .enum_literal,
2706 .main_token = try c.addToken(.identifier, "Inline"),
2707 .data = undefined,
2708 });
2709 _ = try c.addToken(.r_paren, ")");
2710 break :blk res;
2711 };
2712 const return_type_expr = try renderNodeGrouped(c, payload.return_type);2701 const return_type_expr = try renderNodeGrouped(c, payload.return_type);
27132702
2714 const fn_proto = try blk: {2703 const fn_proto = blk: {
2715 if (params.items.len < 2)2704 if (params.items.len < 2) {
2716 break :blk c.addNode(.{2705 break :blk try c.addNode(.{
2717 .tag = .fn_proto_one,2706 .tag = .fn_proto_simple,
2718 .main_token = fn_token,2707 .main_token = fn_token,
2719 .data = .{2708 .data = .{
2720 .lhs = try c.addExtra(std.zig.ast.Node.FnProtoOne{2709 .lhs = params.items[0],
2721 .param = params.items[0],
2722 .align_expr = 0,
2723 .section_expr = 0,
2724 .callconv_expr = callconv_expr,
2725 }),
2726 .rhs = return_type_expr,2710 .rhs = return_type_expr,
2727 },2711 },
2728 })2712 });
2729 else2713 } else {
2730 break :blk c.addNode(.{2714 break :blk try c.addNode(.{
2731 .tag = .fn_proto,2715 .tag = .fn_proto_multi,
2732 .main_token = fn_token,2716 .main_token = fn_token,
2733 .data = .{2717 .data = .{
2734 .lhs = try c.addExtra(std.zig.ast.Node.FnProto{2718 .lhs = try c.addExtra(std.zig.ast.Node.SubRange{
2735 .params_start = span.start,2719 .start = span.start,
2736 .params_end = span.end,2720 .end = span.end,
2737 .align_expr = 0,
2738 .section_expr = 0,
2739 .callconv_expr = callconv_expr,
2740 }),2721 }),
2741 .rhs = return_type_expr,2722 .rhs = return_type_expr,
2742 },2723 },
2743 });2724 });
2725 }
2744 };2726 };
2745 return c.addNode(.{2727 return c.addNode(.{
2746 .tag = .fn_decl,2728 .tag = .fn_decl,
test/translate_c.zig+29-29
...@@ -232,7 +232,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -232,7 +232,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
232 cases.add("use cast param as macro fn return type",232 cases.add("use cast param as macro fn return type",
233 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((u32)(x) + SYS_BASE_CACHED)233 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((u32)(x) + SYS_BASE_CACHED)
234 , &[_][]const u8{234 , &[_][]const u8{
235 \\pub fn MEM_PHYSICAL_TO_K0(x: anytype) callconv(.Inline) ?*c_void {235 \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*c_void {
236 \\ return @import("std").meta.cast(?*c_void, @import("std").meta.cast(u32, x) + SYS_BASE_CACHED);236 \\ return @import("std").meta.cast(?*c_void, @import("std").meta.cast(u32, x) + SYS_BASE_CACHED);
237 \\}237 \\}
238 });238 });
...@@ -273,7 +273,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -273,7 +273,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
273 ,273 ,
274 \\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));274 \\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));
275 ,275 ,
276 \\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))) {276 \\pub inline fn _AL_READ3BYTES(p: anytype) @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))) {
277 \\ 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));277 \\ 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));
278 \\}278 \\}
279 });279 });
...@@ -345,7 +345,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -345,7 +345,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
345 \\};345 \\};
346 \\pub const Color = struct_Color;346 \\pub const Color = struct_Color;
347 ,347 ,
348 \\pub fn CLITERAL(type_1: anytype) callconv(.Inline) @TypeOf(type_1) {348 \\pub inline fn CLITERAL(type_1: anytype) @TypeOf(type_1) {
349 \\ return type_1;349 \\ return type_1;
350 \\}350 \\}
351 ,351 ,
...@@ -380,7 +380,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -380,7 +380,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
380 cases.add("correct semicolon after infixop",380 cases.add("correct semicolon after infixop",
381 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)381 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
382 , &[_][]const u8{382 , &[_][]const u8{
383 \\pub fn __ferror_unlocked_body(_fp: anytype) callconv(.Inline) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {383 \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {
384 \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0);384 \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0);
385 \\}385 \\}
386 });386 });
...@@ -389,7 +389,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -389,7 +389,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
389 \\#define FOO(x) ((x >= 0) + (x >= 0))389 \\#define FOO(x) ((x >= 0) + (x >= 0))
390 \\#define BAR 1 && 2 > 4390 \\#define BAR 1 && 2 > 4
391 , &[_][]const u8{391 , &[_][]const u8{
392 \\pub fn FOO(x: anytype) callconv(.Inline) @TypeOf(@boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0))) {392 \\pub inline fn FOO(x: anytype) @TypeOf(@boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0))) {
393 \\ return @boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0));393 \\ return @boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0));
394 \\}394 \\}
395 ,395 ,
...@@ -438,7 +438,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -438,7 +438,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
438 \\ break :blk bar;438 \\ break :blk bar;
439 \\};439 \\};
440 ,440 ,
441 \\pub fn bar(x: anytype) callconv(.Inline) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {441 \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {
442 \\ return blk: {442 \\ return blk: {
443 \\ _ = &x;443 \\ _ = &x;
444 \\ _ = @as(c_int, 3);444 \\ _ = @as(c_int, 3);
...@@ -1782,13 +1782,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1782,13 +1782,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1782 , &[_][]const u8{1782 , &[_][]const u8{
1783 \\pub extern var fn_ptr: ?fn () callconv(.C) void;1783 \\pub extern var fn_ptr: ?fn () callconv(.C) void;
1784 ,1784 ,
1785 \\pub fn foo() callconv(.Inline) void {1785 \\pub inline fn foo() void {
1786 \\ return fn_ptr.?();1786 \\ return fn_ptr.?();
1787 \\}1787 \\}
1788 ,1788 ,
1789 \\pub extern var fn_ptr2: ?fn (c_int, f32) callconv(.C) u8;1789 \\pub extern var fn_ptr2: ?fn (c_int, f32) callconv(.C) u8;
1790 ,1790 ,
1791 \\pub fn bar(arg_1: c_int, arg_2: f32) callconv(.Inline) u8 {1791 \\pub inline fn bar(arg_1: c_int, arg_2: f32) u8 {
1792 \\ return fn_ptr2.?(arg_1, arg_2);1792 \\ return fn_ptr2.?(arg_1, arg_2);
1793 \\}1793 \\}
1794 });1794 });
...@@ -1821,7 +1821,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1821,7 +1821,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1821 ,1821 ,
1822 \\pub const glClearPFN = PFNGLCLEARPROC;1822 \\pub const glClearPFN = PFNGLCLEARPROC;
1823 ,1823 ,
1824 \\pub fn glClearUnion(arg_2: GLbitfield) callconv(.Inline) void {1824 \\pub inline fn glClearUnion(arg_2: GLbitfield) void {
1825 \\ return glProcs.gl.Clear.?(arg_2);1825 \\ return glProcs.gl.Clear.?(arg_2);
1826 \\}1826 \\}
1827 ,1827 ,
...@@ -1842,15 +1842,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1842,15 +1842,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1842 , &[_][]const u8{1842 , &[_][]const u8{
1843 \\pub extern var c: c_int;1843 \\pub extern var c: c_int;
1844 ,1844 ,
1845 \\pub fn BASIC(c_1: anytype) callconv(.Inline) @TypeOf(c_1 * @as(c_int, 2)) {1845 \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * @as(c_int, 2)) {
1846 \\ return c_1 * @as(c_int, 2);1846 \\ return c_1 * @as(c_int, 2);
1847 \\}1847 \\}
1848 ,1848 ,
1849 \\pub fn FOO(L: anytype, b: anytype) callconv(.Inline) @TypeOf(L + b) {1849 \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) {
1850 \\ return L + b;1850 \\ return L + b;
1851 \\}1851 \\}
1852 ,1852 ,
1853 \\pub fn BAR() callconv(.Inline) @TypeOf(c * c) {1853 \\pub inline fn BAR() @TypeOf(c * c) {
1854 \\ return c * c;1854 \\ return c * c;
1855 \\}1855 \\}
1856 });1856 });
...@@ -2549,7 +2549,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2549,7 +2549,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2549 cases.add("macro call",2549 cases.add("macro call",
2550 \\#define CALL(arg) bar(arg)2550 \\#define CALL(arg) bar(arg)
2551 , &[_][]const u8{2551 , &[_][]const u8{
2552 \\pub fn CALL(arg: anytype) callconv(.Inline) @TypeOf(bar(arg)) {2552 \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) {
2553 \\ return bar(arg);2553 \\ return bar(arg);
2554 \\}2554 \\}
2555 });2555 });
...@@ -2557,7 +2557,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2557,7 +2557,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2557 cases.add("macro call with no args",2557 cases.add("macro call with no args",
2558 \\#define CALL(arg) bar()2558 \\#define CALL(arg) bar()
2559 , &[_][]const u8{2559 , &[_][]const u8{
2560 \\pub fn CALL(arg: anytype) callconv(.Inline) @TypeOf(bar()) {2560 \\pub inline fn CALL(arg: anytype) @TypeOf(bar()) {
2561 \\ return bar();2561 \\ return bar();
2562 \\}2562 \\}
2563 });2563 });
...@@ -3120,7 +3120,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3120,7 +3120,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3120 \\#define BAR (void*) a3120 \\#define BAR (void*) a
3121 \\#define BAZ (uint32_t)(2)3121 \\#define BAZ (uint32_t)(2)
3122 , &[_][]const u8{3122 , &[_][]const u8{
3123 \\pub fn FOO(bar: anytype) callconv(.Inline) @TypeOf(baz(@import("std").meta.cast(?*c_void, baz))) {3123 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").meta.cast(?*c_void, baz))) {
3124 \\ return baz(@import("std").meta.cast(?*c_void, baz));3124 \\ return baz(@import("std").meta.cast(?*c_void, baz));
3125 \\}3125 \\}
3126 ,3126 ,
...@@ -3160,11 +3160,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3160,11 +3160,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3160 \\#define MIN(a, b) ((b) < (a) ? (b) : (a))3160 \\#define MIN(a, b) ((b) < (a) ? (b) : (a))
3161 \\#define MAX(a, b) ((b) > (a) ? (b) : (a))3161 \\#define MAX(a, b) ((b) > (a) ? (b) : (a))
3162 , &[_][]const u8{3162 , &[_][]const u8{
3163 \\pub fn MIN(a: anytype, b: anytype) callconv(.Inline) @TypeOf(if (b < a) b else a) {3163 \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) {
3164 \\ return if (b < a) b else a;3164 \\ return if (b < a) b else a;
3165 \\}3165 \\}
3166 ,3166 ,
3167 \\pub fn MAX(a: anytype, b: anytype) callconv(.Inline) @TypeOf(if (b > a) b else a) {3167 \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) {
3168 \\ return if (b > a) b else a;3168 \\ return if (b > a) b else a;
3169 \\}3169 \\}
3170 });3170 });
...@@ -3351,7 +3351,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3351,7 +3351,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3351 \\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen)3351 \\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen)
3352 \\3352 \\
3353 , &[_][]const u8{3353 , &[_][]const u8{
3354 \\pub fn DefaultScreen(dpy: anytype) callconv(.Inline) @TypeOf(@import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen) {3354 \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen) {
3355 \\ return @import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen;3355 \\ return @import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen;
3356 \\}3356 \\}
3357 });3357 });
...@@ -3501,17 +3501,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3501,17 +3501,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3501 });3501 });
35023502
3503 cases.add("global assembly",3503 cases.add("global assembly",
3504 \\__asm__(".globl func\n\t"3504 \\__asm__(".globl func\n\t"
3505 \\ ".type func, @function\n\t"3505 \\ ".type func, @function\n\t"
3506 \\ "func:\n\t"3506 \\ "func:\n\t"
3507 \\ ".cfi_startproc\n\t"3507 \\ ".cfi_startproc\n\t"
3508 \\ "movl $42, %eax\n\t"3508 \\ "movl $42, %eax\n\t"
3509 \\ "ret\n\t"3509 \\ "ret\n\t"
3510 \\ ".cfi_endproc");3510 \\ ".cfi_endproc");
3511 , &[_][]const u8{3511 , &[_][]const u8{
3512 \\comptime {3512 \\comptime {
3513 \\ asm (".globl func\n\t.type func, @function\n\tfunc:\n\t.cfi_startproc\n\tmovl $42, %eax\n\tret\n\t.cfi_endproc");3513 \\ asm (".globl func\n\t.type func, @function\n\tfunc:\n\t.cfi_startproc\n\tmovl $42, %eax\n\tret\n\t.cfi_endproc");
3514 \\}3514 \\}
3515 });3515 });
35163516
3517 cases.add("Demote function that initializes opaque struct",3517 cases.add("Demote function that initializes opaque struct",