Commit 3e095d8ef3

Vexu <git@vexu.eu>
2020-07-11 21:04:38
use 'anytype' in translate-c
1 parent 1a989ba
Changed files (5)
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(),
                 }
             }
         };
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);
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),
     }
 }
 
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)
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;
         \\}
     });