authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-14 16:42:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-14 22:50:00-04:00
logb49d3672f3fc722925a6dad4070f7faa92dd1878
treeb76689f617227739552575ca12e0a37a389b5883
parent9a5a1013a833229e1d12588615c1a05644f76cc5

stage2 astgen for LabeledBlock


2 files changed, 17 insertions(+), 6 deletions(-)

src-self-hosted/Module.zig+1
...@@ -737,6 +737,7 @@ pub const Scope = struct {...@@ -737,6 +737,7 @@ pub const Scope = struct {
737 arena: *Allocator,737 arena: *Allocator,
738 /// The first N instructions in a function body ZIR are arg instructions.738 /// The first N instructions in a function body ZIR are arg instructions.
739 instructions: std.ArrayListUnmanaged(*zir.Inst) = .{},739 instructions: std.ArrayListUnmanaged(*zir.Inst) = .{},
740 label: ?ast.TokenIndex = null,
740 };741 };
741742
742 /// This is always a `const` local and importantly the `inst` is a value type, not a pointer.743 /// This is always a `const` local and importantly the `inst` is a value type, not a pointer.
src-self-hosted/astgen.zig+16-6
...@@ -129,14 +129,24 @@ fn labeledBlockExpr(...@@ -129,14 +129,24 @@ fn labeledBlockExpr(
129 const tracy = trace(@src());129 const tracy = trace(@src());
130 defer tracy.end();130 defer tracy.end();
131131
132 const statements = block_node.statements();132 var block_scope: Scope.GenZIR = .{
133 .parent = parent_scope,
134 .decl = parent_scope.decl().?,
135 .arena = parent_scope.arena(),
136 .instructions = .{},
137 .label = block_node.label,
138 };
139 defer block_scope.instructions.deinit(mod.gpa);
133140
134 if (statements.len == 0) {141 try blockExprStmts(mod, &block_scope.base, &block_node.base, block_node.statements());
135 // Hot path for `{}`.
136 return rlWrapVoid(mod, parent_scope, rl, &block_node.base, {});
137 }
138142
139 return mod.failNode(parent_scope, &block_node.base, "TODO implement labeled blocks", .{});143 const tree = parent_scope.tree();
144 const src = tree.token_locs[block_node.lbrace].start;
145 const block = try addZIRInstBlock(mod, parent_scope, src, .{
146 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),
147 });
148
149 return &block.base;
140}150}
141151
142fn blockExprStmts(mod: *Module, parent_scope: *Scope, node: *ast.Node, statements: []*ast.Node) !void {152fn blockExprStmts(mod: *Module, parent_scope: *Scope, node: *ast.Node, statements: []*ast.Node) !void {