From 30c9808391bead620eaf24991d7a9ba21e4266b9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 20 Apr 2021 17:38:06 -0700 Subject: [PATCH] AstGen: implement anytype parameters --- lib/std/zig/ast.zig | 13 +++++++------ src/AstGen.zig | 15 +++++++++------ src/Zir.zig | 4 ++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index a0e7754896..31a48bfb3f 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -2172,6 +2172,10 @@ pub const full = struct { }; it.param_i += 1; it.tok_i = it.tree.lastToken(param_type) + 1; + // Look for anytype and ... params afterwards. + if (token_tags[it.tok_i] == .comma) { + it.tok_i += 1; + } it.tok_flag = true; return Param{ .first_doc_comment = first_doc_comment, @@ -2181,10 +2185,7 @@ pub const full = struct { .type_expr = param_type, }; } - // Look for anytype and ... params afterwards. - if (token_tags[it.tok_i] == .comma) { - it.tok_i += 1; - } else { + if (token_tags[it.tok_i] == .r_paren) { return null; } if (token_tags[it.tok_i] == .doc_comment) { @@ -2236,8 +2237,8 @@ pub const full = struct { .tree = &tree, .fn_proto = &fn_proto, .param_i = 0, - .tok_i = undefined, - .tok_flag = false, + .tok_i = fn_proto.lparen + 1, + .tok_flag = true, }; } }; diff --git a/src/AstGen.zig b/src/AstGen.zig index c6fb2dd80d..5fe167bc6c 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -2334,7 +2334,11 @@ fn fnDecl( var count: usize = 0; var it = fn_proto.iterate(tree.*); while (it.next()) |param| { - if (param.anytype_ellipsis3) |some| if (token_tags[some] == .ellipsis3) break; + if (param.anytype_ellipsis3) |token| switch (token_tags[token]) { + .ellipsis3 => break, + .keyword_anytype => {}, + else => unreachable, + }; count += 1; } break :blk count; @@ -2358,11 +2362,10 @@ fn fnDecl( while (it.next()) |param| : (param_type_i += 1) { if (param.anytype_ellipsis3) |token| { switch (token_tags[token]) { - .keyword_anytype => return astgen.failTok( - token, - "TODO implement anytype parameter", - .{}, - ), + .keyword_anytype => { + param_types[param_type_i] = .none; + continue; + }, .ellipsis3 => { is_var_args = true; break; diff --git a/src/Zir.zig b/src/Zir.zig index 32626d306a..672979f3b7 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -1816,6 +1816,7 @@ pub const Inst = struct { /// Trailing: /// 0. param_type: Ref // for each param_types_len + /// - `none` indicates that the param type is `anytype`. /// 1. body: Index // for each body_len pub const Func = struct { return_type: Ref, @@ -3304,7 +3305,10 @@ const Writer = struct { try stream.writeAll(", {\n"); self.indent += 2; + const prev_param_count = self.param_count; + self.param_count = param_types.len; try self.writeBody(stream, body); + self.param_count = prev_param_count; self.indent -= 2; try stream.writeByteNTimes(' ', self.indent); try stream.writeAll("}) ");