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(...@@ -1281,6 +1281,7 @@ fn blockExprStmts(
1281 .bit_or,1281 .bit_or,
1282 .block,1282 .block,
1283 .block_inline,1283 .block_inline,
1284 .block_inline_var,
1284 .loop,1285 .loop,
1285 .bool_br_and,1286 .bool_br_and,
1286 .bool_br_or,1287 .bool_br_or,
...@@ -2020,7 +2021,7 @@ fn fnDecl(...@@ -2020,7 +2021,7 @@ fn fnDecl(
2020 // Iterate over the parameters. We put the param names as the first N2021 // Iterate over the parameters. We put the param names as the first N
2021 // items inside `extra` so that debug info later can refer to the parameter names2022 // items inside `extra` so that debug info later can refer to the parameter names
2022 // even while the respective source code is unloaded.2023 // 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
2025 {2026 {
2026 var params_scope = &fn_gz.base;2027 var params_scope = &fn_gz.base;
...@@ -2080,7 +2081,7 @@ fn fnDecl(...@@ -2080,7 +2081,7 @@ fn fnDecl(
2080 };2081 };
20812082
2082 const fn_name_token = fn_proto.name_token orelse {2083 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", .{});
2084 };2085 };
2085 const fn_name_str_index = try gz.identAsString(fn_name_token);2086 const fn_name_str_index = try gz.identAsString(fn_name_token);
20862087
...@@ -2173,16 +2174,13 @@ fn globalVarDecl(...@@ -2173,16 +2174,13 @@ fn globalVarDecl(
2173 var_decl.ast.init_node,2174 var_decl.ast.init_node,
2174 );2175 );
21752176
2176 if (!is_mutable) {2177 const tag: Zir.Inst.Tag = if (is_mutable) .block_inline_var else .block_inline;
2177 // const globals are just their instruction. mutable globals have2178 // const globals are just their instruction. mutable globals have
2178 // a special ZIR form.2179 // a special ZIR form.
2179 const block_inst = try gz.addBlock(.block_inline, node);2180 const block_inst = try gz.addBlock(tag, node);
2180 _ = try block_scope.addBreak(.break_inline, block_inst, init_inst);2181 _ = try block_scope.addBreak(.break_inline, block_inst, init_inst);
2181 try block_scope.setBlockBody(block_inst);2182 try block_scope.setBlockBody(block_inst);
2182 break :vi block_inst;2183 break :vi block_inst;
2183 }
2184
2185 @panic("TODO astgen global variable");
2186 } else if (!is_extern) {2184 } else if (!is_extern) {
2187 return astgen.failNode(node, "variables must be initialized", .{});2185 return astgen.failNode(node, "variables must be initialized", .{});
2188 } else if (var_decl.ast.type_node != 0) {2186 } else if (var_decl.ast.type_node != 0) {
...@@ -2190,7 +2188,7 @@ fn globalVarDecl(...@@ -2190,7 +2188,7 @@ fn globalVarDecl(
21902188
2191 const type_inst = try typeExpr(gz, scope, var_decl.ast.type_node);2189 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", .{});
2194 } else {2192 } else {
2195 return astgen.failNode(node, "unable to infer variable type", .{});2193 return astgen.failNode(node, "unable to infer variable type", .{});
2196 };2194 };
...@@ -2588,7 +2586,7 @@ fn containerDecl(...@@ -2588,7 +2586,7 @@ fn containerDecl(
2588 );2586 );
2589 }2587 }
2590 if (counts.values == 0 and counts.decls == 0 and arg_inst == .none) {2588 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", .{});
2592 }2590 }
2593 // In this case we must generate ZIR code for the tag values, similar to2591 // In this case we must generate ZIR code for the tag values, similar to
2594 // how structs are handled above.2592 // how structs are handled above.
...@@ -2698,7 +2696,7 @@ fn errorSetDecl(...@@ -2698,7 +2696,7 @@ fn errorSetDecl(
2698 const main_tokens = tree.nodes.items(.main_token);2696 const main_tokens = tree.nodes.items(.main_token);
2699 const token_tags = tree.tokens.items(.tag);2697 const token_tags = tree.tokens.items(.tag);
27002698
2701 @panic("TODO AstGen errorSetDecl");2699 return astgen.failNode(node, "TODO AstGen errorSetDecl", .{});
2702}2700}
27032701
2704fn orelseCatchExpr(2702fn orelseCatchExpr(
src/Sema.zig+1-1
...@@ -367,7 +367,7 @@ pub fn analyzeBody(...@@ -367,7 +367,7 @@ pub fn analyzeBody(
367 i = 0;367 i = 0;
368 continue;368 continue;
369 },369 },
370 .block_inline => blk: {370 .block_inline, .block_inline_var => blk: {
371 // Directly analyze the block body without introducing a new block.371 // Directly analyze the block body without introducing a new block.
372 const inst_data = datas[inst].pl_node;372 const inst_data = datas[inst].pl_node;
373 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);373 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);
src/Zir.zig+4
...@@ -208,6 +208,8 @@ pub const Inst = struct {...@@ -208,6 +208,8 @@ pub const Inst = struct {
208 /// a noreturn instruction.208 /// a noreturn instruction.
209 /// Uses the `pl_node` union field. Payload is `Block`.209 /// Uses the `pl_node` union field. Payload is `Block`.
210 block_inline,210 block_inline,
211 /// Same as `block_inline` but it additionally marks a decl as being a variable.
212 block_inline_var,
211 /// Boolean AND. See also `bit_and`.213 /// Boolean AND. See also `bit_and`.
212 /// Uses the `pl_node` union field. Payload is `Bin`.214 /// Uses the `pl_node` union field. Payload is `Bin`.
213 bool_and,215 bool_and,
...@@ -753,6 +755,7 @@ pub const Inst = struct {...@@ -753,6 +755,7 @@ pub const Inst = struct {
753 .bit_or,755 .bit_or,
754 .block,756 .block,
755 .block_inline,757 .block_inline,
758 .block_inline_var,
756 .loop,759 .loop,
757 .bool_br_and,760 .bool_br_and,
758 .bool_br_or,761 .bool_br_or,
...@@ -1830,6 +1833,7 @@ const Writer = struct {...@@ -1830,6 +1833,7 @@ const Writer = struct {
18301833
1831 .block,1834 .block,
1832 .block_inline,1835 .block_inline,
1836 .block_inline_var,
1833 .loop,1837 .loop,
1834 .validate_struct_init_ptr,1838 .validate_struct_init_ptr,
1835 => try self.writePlNodeBlock(stream, inst),1839 => try self.writePlNodeBlock(stream, inst),