Commit 5f7c7191ab

Andrew Kelley <andrew@ziglang.org>
2020-08-14 20:28:40
stage2: astgen for non-labeled blocks
1 parent 4adc052
Changed files (3)
src-self-hosted/astgen.zig
@@ -107,11 +107,17 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
         .NullLiteral => return rlWrap(mod, scope, rl, try nullLiteral(mod, scope, node.castTag(.NullLiteral).?)),
         .OptionalType => return rlWrap(mod, scope, rl, try optionalType(mod, scope, node.castTag(.OptionalType).?)),
         .UnwrapOptional => return unwrapOptional(mod, scope, rl, node.castTag(.UnwrapOptional).?),
+        .Block => return blockExpr(mod, scope, rl, node.castTag(.Block).?),
         else => return mod.failNode(scope, node, "TODO implement astgen.Expr for {}", .{@tagName(node.tag)}),
     }
 }
 
-pub fn blockExpr(mod: *Module, parent_scope: *Scope, block_node: *ast.Node.Block) !void {
+pub fn blockExpr(
+    mod: *Module,
+    parent_scope: *Scope,
+    rl: ResultLoc,
+    block_node: *ast.Node.Block,
+) InnerError!*zir.Inst {
     const tracy = trace(@src());
     defer tracy.end();
 
@@ -122,9 +128,11 @@ pub fn blockExpr(mod: *Module, parent_scope: *Scope, block_node: *ast.Node.Block
     var block_arena = std.heap.ArenaAllocator.init(mod.gpa);
     defer block_arena.deinit();
 
+    const tree = parent_scope.tree();
+
     var scope = parent_scope;
     for (block_node.statements()) |statement| {
-        const src = scope.tree().token_locs[statement.firstToken()].start;
+        const src = tree.token_locs[statement.firstToken()].start;
         _ = try addZIRNoOp(mod, scope, src, .dbg_stmt);
         switch (statement.tag) {
             .VarDecl => {
@@ -154,6 +162,12 @@ pub fn blockExpr(mod: *Module, parent_scope: *Scope, block_node: *ast.Node.Block
             },
         }
     }
+
+    const src = tree.token_locs[block_node.firstToken()].start;
+    return addZIRInstConst(mod, parent_scope, src, .{
+        .ty = Type.initTag(.void),
+        .val = Value.initTag(.void_value),
+    });
 }
 
 fn varDecl(
src-self-hosted/link.zig
@@ -1887,7 +1887,9 @@ pub const File = struct {
                 else => false,
             };
             if (is_fn) {
-                //typed_value.val.cast(Value.Payload.Function).?.func.dump(module.*);
+                //if (mem.eql(u8, mem.spanZ(decl.name), "add")) {
+                //    typed_value.val.cast(Value.Payload.Function).?.func.dump(module.*);
+                //}
 
                 // For functions we need to add a prologue to the debug line program.
                 try dbg_line_buffer.ensureCapacity(26);
src-self-hosted/Module.zig
@@ -1343,7 +1343,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
 
                 const body_block = body_node.cast(ast.Node.Block).?;
 
-                try astgen.blockExpr(self, params_scope, body_block);
+                _ = try astgen.blockExpr(self, params_scope, .none, body_block);
 
                 if (!fn_type.fnReturnType().isNoReturn() and (gen_scope.instructions.items.len == 0 or
                     !gen_scope.instructions.items[gen_scope.instructions.items.len - 1].tag.isNoReturn()))