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 {...@@ -2172,6 +2172,10 @@ pub const full = struct {
2172 };2172 };
2173 it.param_i += 1;2173 it.param_i += 1;
2174 it.tok_i = it.tree.lastToken(param_type) + 1;2174 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 }
2175 it.tok_flag = true;2179 it.tok_flag = true;
2176 return Param{2180 return Param{
2177 .first_doc_comment = first_doc_comment,2181 .first_doc_comment = first_doc_comment,
...@@ -2181,10 +2185,7 @@ pub const full = struct {...@@ -2181,10 +2185,7 @@ pub const full = struct {
2181 .type_expr = param_type,2185 .type_expr = param_type,
2182 };2186 };
2183 }2187 }
2184 // Look for anytype and ... params afterwards.2188 if (token_tags[it.tok_i] == .r_paren) {
2185 if (token_tags[it.tok_i] == .comma) {
2186 it.tok_i += 1;
2187 } else {
2188 return null;2189 return null;
2189 }2190 }
2190 if (token_tags[it.tok_i] == .doc_comment) {2191 if (token_tags[it.tok_i] == .doc_comment) {
...@@ -2236,8 +2237,8 @@ pub const full = struct {...@@ -2236,8 +2237,8 @@ pub const full = struct {
2236 .tree = &tree,2237 .tree = &tree,
2237 .fn_proto = &fn_proto,2238 .fn_proto = &fn_proto,
2238 .param_i = 0,2239 .param_i = 0,
2239 .tok_i = undefined,2240 .tok_i = fn_proto.lparen + 1,
2240 .tok_flag = false,2241 .tok_flag = true,
2241 };2242 };
2242 }2243 }
2243 };2244 };
src/AstGen.zig+9-6
...@@ -2334,7 +2334,11 @@ fn fnDecl(...@@ -2334,7 +2334,11 @@ fn fnDecl(
2334 var count: usize = 0;2334 var count: usize = 0;
2335 var it = fn_proto.iterate(tree.*);2335 var it = fn_proto.iterate(tree.*);
2336 while (it.next()) |param| {2336 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 };
2338 count += 1;2342 count += 1;
2339 }2343 }
2340 break :blk count;2344 break :blk count;
...@@ -2358,11 +2362,10 @@ fn fnDecl(...@@ -2358,11 +2362,10 @@ fn fnDecl(
2358 while (it.next()) |param| : (param_type_i += 1) {2362 while (it.next()) |param| : (param_type_i += 1) {
2359 if (param.anytype_ellipsis3) |token| {2363 if (param.anytype_ellipsis3) |token| {
2360 switch (token_tags[token]) {2364 switch (token_tags[token]) {
2361 .keyword_anytype => return astgen.failTok(2365 .keyword_anytype => {
2362 token,2366 param_types[param_type_i] = .none;
2363 "TODO implement anytype parameter",2367 continue;
2364 .{},2368 },
2365 ),
2366 .ellipsis3 => {2369 .ellipsis3 => {
2367 is_var_args = true;2370 is_var_args = true;
2368 break;2371 break;
src/Zir.zig+4
...@@ -1816,6 +1816,7 @@ pub const Inst = struct {...@@ -1816,6 +1816,7 @@ pub const Inst = struct {
18161816
1817 /// Trailing:1817 /// Trailing:
1818 /// 0. param_type: Ref // for each param_types_len1818 /// 0. param_type: Ref // for each param_types_len
1819 /// - `none` indicates that the param type is `anytype`.
1819 /// 1. body: Index // for each body_len1820 /// 1. body: Index // for each body_len
1820 pub const Func = struct {1821 pub const Func = struct {
1821 return_type: Ref,1822 return_type: Ref,
...@@ -3304,7 +3305,10 @@ const Writer = struct {...@@ -3304,7 +3305,10 @@ const Writer = struct {
33043305
3305 try stream.writeAll(", {\n");3306 try stream.writeAll(", {\n");
3306 self.indent += 2;3307 self.indent += 2;
3308 const prev_param_count = self.param_count;
3309 self.param_count = param_types.len;
3307 try self.writeBody(stream, body);3310 try self.writeBody(stream, body);
3311 self.param_count = prev_param_count;
3308 self.indent -= 2;3312 self.indent -= 2;
3309 try stream.writeByteNTimes(' ', self.indent);3313 try stream.writeByteNTimes(' ', self.indent);
3310 try stream.writeAll("}) ");3314 try stream.writeAll("}) ");