authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-20 17:38:06-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-20 17:38:06-07:00
log30c9808391bead620eaf24991d7a9ba21e4266b9
treebe1503cdce50bcdd35a993e1e4b46f04572bb05c
parenta62db38d905ad4962c3b37e013c8745ed6ecbfb9

AstGen: implement anytype parameters


3 files changed, 20 insertions(+), 12 deletions(-)

lib/std/zig/ast.zig+7-6
......@@ -2172,6 +2172,10 @@ pub const full = struct {
21722172 };
21732173 it.param_i += 1;
21742174 it.tok_i = it.tree.lastToken(param_type) + 1;
2175 // Look for anytype and ... params afterwards.
2176 if (token_tags[it.tok_i] == .comma) {
2177 it.tok_i += 1;
2178 }
21752179 it.tok_flag = true;
21762180 return Param{
21772181 .first_doc_comment = first_doc_comment,
......@@ -2181,10 +2185,7 @@ pub const full = struct {
21812185 .type_expr = param_type,
21822186 };
21832187 }
2184 // Look for anytype and ... params afterwards.
2185 if (token_tags[it.tok_i] == .comma) {
2186 it.tok_i += 1;
2187 } else {
2188 if (token_tags[it.tok_i] == .r_paren) {
21882189 return null;
21892190 }
21902191 if (token_tags[it.tok_i] == .doc_comment) {
......@@ -2236,8 +2237,8 @@ pub const full = struct {
22362237 .tree = &tree,
22372238 .fn_proto = &fn_proto,
22382239 .param_i = 0,
2239 .tok_i = undefined,
2240 .tok_flag = false,
2240 .tok_i = fn_proto.lparen + 1,
2241 .tok_flag = true,
22412242 };
22422243 }
22432244 };
src/AstGen.zig+9-6
......@@ -2334,7 +2334,11 @@ fn fnDecl(
23342334 var count: usize = 0;
23352335 var it = fn_proto.iterate(tree.*);
23362336 while (it.next()) |param| {
2337 if (param.anytype_ellipsis3) |some| if (token_tags[some] == .ellipsis3) break;
2337 if (param.anytype_ellipsis3) |token| switch (token_tags[token]) {
2338 .ellipsis3 => break,
2339 .keyword_anytype => {},
2340 else => unreachable,
2341 };
23382342 count += 1;
23392343 }
23402344 break :blk count;
......@@ -2358,11 +2362,10 @@ fn fnDecl(
23582362 while (it.next()) |param| : (param_type_i += 1) {
23592363 if (param.anytype_ellipsis3) |token| {
23602364 switch (token_tags[token]) {
2361 .keyword_anytype => return astgen.failTok(
2362 token,
2363 "TODO implement anytype parameter",
2364 .{},
2365 ),
2365 .keyword_anytype => {
2366 param_types[param_type_i] = .none;
2367 continue;
2368 },
23662369 .ellipsis3 => {
23672370 is_var_args = true;
23682371 break;
src/Zir.zig+4
......@@ -1816,6 +1816,7 @@ pub const Inst = struct {
18161816
18171817 /// Trailing:
18181818 /// 0. param_type: Ref // for each param_types_len
1819 /// - `none` indicates that the param type is `anytype`.
18191820 /// 1. body: Index // for each body_len
18201821 pub const Func = struct {
18211822 return_type: Ref,
......@@ -3304,7 +3305,10 @@ const Writer = struct {
33043305
33053306 try stream.writeAll(", {\n");
33063307 self.indent += 2;
3308 const prev_param_count = self.param_count;
3309 self.param_count = param_types.len;
33073310 try self.writeBody(stream, body);
3311 self.param_count = prev_param_count;
33083312 self.indent -= 2;
33093313 try stream.writeByteNTimes(' ', self.indent);
33103314 try stream.writeAll("}) ");