From 3e095d8ef32fc93f5050cead708849846d626d1d Mon Sep 17 00:00:00 2001 From: Vexu Date: Sat, 11 Jul 2020 22:04:38 +0300 Subject: [PATCH] use 'anytype' in translate-c --- lib/std/zig/ast.zig | 8 ++++---- lib/std/zig/parse.zig | 6 +++--- lib/std/zig/render.zig | 2 +- src-self-hosted/translate_c.zig | 9 ++++----- test/translate_c.zig | 20 ++++++++++---------- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index 03153f541a..7cb4936444 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -993,7 +993,7 @@ pub const Node = struct { param_type: ParamType, pub const ParamType = union(enum) { - var_type: *Node, + any_type: *Node, var_args: TokenIndex, type_expr: *Node, }; @@ -1004,7 +1004,7 @@ pub const Node = struct { if (i < 1) { switch (self.param_type) { .var_args => return null, - .var_type, .type_expr => |node| return node, + .any_type, .type_expr => |node| return node, } } i -= 1; @@ -1018,14 +1018,14 @@ pub const Node = struct { if (self.name_token) |name_token| return name_token; switch (self.param_type) { .var_args => |tok| return tok, - .var_type, .type_expr => |node| return node.firstToken(), + .any_type, .type_expr => |node| return node.firstToken(), } } pub fn lastToken(self: *const ParamDecl) TokenIndex { switch (self.param_type) { .var_args => |tok| return tok, - .var_type, .type_expr => |node| return node.lastToken(), + .any_type, .type_expr => |node| return node.lastToken(), } } }; diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig index 29b60c0cee..f5f0c10826 100644 --- a/lib/std/zig/parse.zig +++ b/lib/std/zig/parse.zig @@ -519,7 +519,7 @@ const Parser = struct { const callconv_expr = try p.parseCallconv(); const exclamation_token = p.eatToken(.Bang); - const return_type_expr = (try p.parseVarType()) orelse + const return_type_expr = (try p.parseAnyType()) orelse try p.expectNodeRecoverable(parseTypeExpr, .{ // most likely the user forgot to specify the return type. // Mark return type as invalid and try to continue. @@ -2028,7 +2028,7 @@ const Parser = struct { fn parseParamType(p: *Parser) !?Node.FnProto.ParamDecl.ParamType { // TODO cast from tuple to error union is broken const P = Node.FnProto.ParamDecl.ParamType; - if (try p.parseVarType()) |node| return P{ .var_type = node }; + if (try p.parseAnyType()) |node| return P{ .any_type = node }; if (p.eatToken(.Ellipsis3)) |token| return P{ .var_args = token }; if (try p.parseTypeExpr()) |node| return P{ .type_expr = node }; return null; @@ -3057,7 +3057,7 @@ const Parser = struct { return &node.base; } - fn parseVarType(p: *Parser) !?*Node { + fn parseAnyType(p: *Parser) !?*Node { const token = p.eatToken(.Keyword_anytype) orelse p.eatToken(.Keyword_var) orelse return null; // TODO remove in next release cycle const node = try p.arena.allocator.create(Node.AnyType); diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 3150d6a3f4..f24886c0ab 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -2198,7 +2198,7 @@ fn renderParamDecl( } switch (param_decl.param_type) { .var_args => |token| try renderToken(tree, stream, token, indent, start_col, space), - .var_type, .type_expr => |node| try renderExpression(allocator, stream, tree, indent, start_col, node, space), + .any_type, .type_expr => |node| try renderExpression(allocator, stream, tree, indent, start_col, node, space), } } diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 396ee09ef9..4ae2d74336 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -5215,10 +5215,9 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, const param_name_tok = try appendIdentifier(c, mangled_name); _ = try appendToken(c, .Colon, ":"); - const token_index = try appendToken(c, .Keyword_var, "var"); - const identifier = try c.arena.create(ast.Node.Identifier); - identifier.* = .{ - .token = token_index, + const any_type = try c.arena.create(ast.Node.AnyType); + any_type.* = .{ + .token = try appendToken(c, .Keyword_anytype, "anytype"), }; (try fn_params.addOne()).* = .{ @@ -5226,7 +5225,7 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, .comptime_token = null, .noalias_token = null, .name_token = param_name_tok, - .param_type = .{ .type_expr = &identifier.base }, + .param_type = .{ .any_type = &any_type.base }, }; if (it.peek().?.id != .Comma) diff --git a/test/translate_c.zig b/test/translate_c.zig index a4e2a33000..738f9523ad 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -21,7 +21,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { cases.add("correct semicolon after infixop", \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0) , &[_][]const u8{ - \\pub inline fn __ferror_unlocked_body(_fp: var) @TypeOf(((_fp.*._flags) & _IO_ERR_SEEN) != 0) { + \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf(((_fp.*._flags) & _IO_ERR_SEEN) != 0) { \\ return ((_fp.*._flags) & _IO_ERR_SEEN) != 0; \\} }); @@ -30,7 +30,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define FOO(x) ((x >= 0) + (x >= 0)) \\#define BAR 1 && 2 > 4 , &[_][]const u8{ - \\pub inline fn FOO(x: var) @TypeOf(@boolToInt(x >= 0) + @boolToInt(x >= 0)) { + \\pub inline fn FOO(x: anytype) @TypeOf(@boolToInt(x >= 0) + @boolToInt(x >= 0)) { \\ return @boolToInt(x >= 0) + @boolToInt(x >= 0); \\} , @@ -81,7 +81,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ break :blk bar; \\}; , - \\pub inline fn bar(x: var) @TypeOf(baz(1, 2)) { + \\pub inline fn bar(x: anytype) @TypeOf(baz(1, 2)) { \\ return blk: { \\ _ = &x; \\ _ = 3; @@ -1483,11 +1483,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub extern var c: c_int; , - \\pub inline fn BASIC(c_1: var) @TypeOf(c_1 * 2) { + \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * 2) { \\ return c_1 * 2; \\} , - \\pub inline fn FOO(L: var, b: var) @TypeOf(L + b) { + \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) { \\ return L + b; \\} }); @@ -2123,7 +2123,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { cases.add("macro call", \\#define CALL(arg) bar(arg) , &[_][]const u8{ - \\pub inline fn CALL(arg: var) @TypeOf(bar(arg)) { + \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) { \\ return bar(arg); \\} }); @@ -2683,7 +2683,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define FOO(bar) baz((void *)(baz)) \\#define BAR (void*) a , &[_][]const u8{ - \\pub inline fn FOO(bar: var) @TypeOf(baz((@import("std").meta.cast(?*c_void, baz)))) { + \\pub inline fn FOO(bar: anytype) @TypeOf(baz((@import("std").meta.cast(?*c_void, baz)))) { \\ return baz((@import("std").meta.cast(?*c_void, baz))); \\} , @@ -2713,11 +2713,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define MIN(a, b) ((b) < (a) ? (b) : (a)) \\#define MAX(a, b) ((b) > (a) ? (b) : (a)) , &[_][]const u8{ - \\pub inline fn MIN(a: var, b: var) @TypeOf(if (b < a) b else a) { + \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) { \\ return if (b < a) b else a; \\} , - \\pub inline fn MAX(a: var, b: var) @TypeOf(if (b > a) b else a) { + \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) { \\ return if (b > a) b else a; \\} }); @@ -2905,7 +2905,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen) \\ , &[_][]const u8{ - \\pub inline fn DefaultScreen(dpy: var) @TypeOf((@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen) { + \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf((@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen) { \\ return (@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen; \\} });