authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-16 17:57:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-16 17:57:51-07:00
log9375ad0d3b5e97fa2889622862dbc4724b9e9243
treeea33250955825b026055467316e97fc0538be68b
parentc8ae581fef6506a8234cdba1355ba7f0f449031a

AstGen: implement global var decls

And fix bug with using `ensureCapacity` when I wanted `ensureUnusedCapacity`.

3 files changed, 18 insertions(+), 16 deletions(-)

src/AstGen.zig+13-15
......@@ -1281,6 +1281,7 @@ fn blockExprStmts(
12811281 .bit_or,
12821282 .block,
12831283 .block_inline,
1284 .block_inline_var,
12841285 .loop,
12851286 .bool_br_and,
12861287 .bool_br_or,
......@@ -2020,7 +2021,7 @@ fn fnDecl(
20202021 // Iterate over the parameters. We put the param names as the first N
20212022 // items inside `extra` so that debug info later can refer to the parameter names
20222023 // even while the respective source code is unloaded.
2023 try astgen.extra.ensureCapacity(gpa, param_count);
2024 try astgen.extra.ensureUnusedCapacity(gpa, param_count);
20242025
20252026 {
20262027 var params_scope = &fn_gz.base;
......@@ -2080,7 +2081,7 @@ fn fnDecl(
20802081 };
20812082
20822083 const fn_name_token = fn_proto.name_token orelse {
2083 @panic("TODO handle missing function names in the parser");
2084 return astgen.failTok(fn_proto.ast.fn_token, "missing function name", .{});
20842085 };
20852086 const fn_name_str_index = try gz.identAsString(fn_name_token);
20862087
......@@ -2173,16 +2174,13 @@ fn globalVarDecl(
21732174 var_decl.ast.init_node,
21742175 );
21752176
2176 if (!is_mutable) {
2177 // const globals are just their instruction. mutable globals have
2178 // a special ZIR form.
2179 const block_inst = try gz.addBlock(.block_inline, node);
2180 _ = try block_scope.addBreak(.break_inline, block_inst, init_inst);
2181 try block_scope.setBlockBody(block_inst);
2182 break :vi block_inst;
2183 }
2184
2185 @panic("TODO astgen global variable");
2177 const tag: Zir.Inst.Tag = if (is_mutable) .block_inline_var else .block_inline;
2178 // const globals are just their instruction. mutable globals have
2179 // a special ZIR form.
2180 const block_inst = try gz.addBlock(tag, node);
2181 _ = try block_scope.addBreak(.break_inline, block_inst, init_inst);
2182 try block_scope.setBlockBody(block_inst);
2183 break :vi block_inst;
21862184 } else if (!is_extern) {
21872185 return astgen.failNode(node, "variables must be initialized", .{});
21882186 } else if (var_decl.ast.type_node != 0) {
......@@ -2190,7 +2188,7 @@ fn globalVarDecl(
21902188
21912189 const type_inst = try typeExpr(gz, scope, var_decl.ast.type_node);
21922190
2193 @panic("TODO AstGen extern global variable");
2191 return astgen.failNode(node, "TODO AstGen extern global variable", .{});
21942192 } else {
21952193 return astgen.failNode(node, "unable to infer variable type", .{});
21962194 };
......@@ -2588,7 +2586,7 @@ fn containerDecl(
25882586 );
25892587 }
25902588 if (counts.values == 0 and counts.decls == 0 and arg_inst == .none) {
2591 @panic("AstGen simple enum");
2589 return astgen.failNode(node, "TODO AstGen simple enums", .{});
25922590 }
25932591 // In this case we must generate ZIR code for the tag values, similar to
25942592 // how structs are handled above.
......@@ -2698,7 +2696,7 @@ fn errorSetDecl(
26982696 const main_tokens = tree.nodes.items(.main_token);
26992697 const token_tags = tree.tokens.items(.tag);
27002698
2701 @panic("TODO AstGen errorSetDecl");
2699 return astgen.failNode(node, "TODO AstGen errorSetDecl", .{});
27022700}
27032701
27042702fn orelseCatchExpr(
src/Sema.zig+1-1
......@@ -367,7 +367,7 @@ pub fn analyzeBody(
367367 i = 0;
368368 continue;
369369 },
370 .block_inline => blk: {
370 .block_inline, .block_inline_var => blk: {
371371 // Directly analyze the block body without introducing a new block.
372372 const inst_data = datas[inst].pl_node;
373373 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);
src/Zir.zig+4
......@@ -208,6 +208,8 @@ pub const Inst = struct {
208208 /// a noreturn instruction.
209209 /// Uses the `pl_node` union field. Payload is `Block`.
210210 block_inline,
211 /// Same as `block_inline` but it additionally marks a decl as being a variable.
212 block_inline_var,
211213 /// Boolean AND. See also `bit_and`.
212214 /// Uses the `pl_node` union field. Payload is `Bin`.
213215 bool_and,
......@@ -753,6 +755,7 @@ pub const Inst = struct {
753755 .bit_or,
754756 .block,
755757 .block_inline,
758 .block_inline_var,
756759 .loop,
757760 .bool_br_and,
758761 .bool_br_or,
......@@ -1830,6 +1833,7 @@ const Writer = struct {
18301833
18311834 .block,
18321835 .block_inline,
1836 .block_inline_var,
18331837 .loop,
18341838 .validate_struct_init_ptr,
18351839 => try self.writePlNodeBlock(stream, inst),