authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-23 20:31:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-23 20:31:12-07:00
log49be88859d3cef22c5cc27c908264a235c78a4d0
tree3913a10c923c72dafa3f8914cd4d47c1e0795e87
parent9a271347fe1e302c132fa63bbe27bfffcf3b132d

AstGen: implement nosuspend expressions

Also implement ZIR error notes

3 files changed, 53 insertions(+), 3 deletions(-)

BRANCH_TODO+3
...@@ -37,6 +37,9 @@...@@ -37,6 +37,9 @@
37 * when handling decls, catch the error and continue, so that37 * when handling decls, catch the error and continue, so that
38 AstGen can report more than one compile error.38 AstGen can report more than one compile error.
3939
40 * AstGen: inside a nosuspend block, emit function calls as nosuspend calls
41 * AstGen: add result location pointers to function calls
42
40 const container_name_hash: Scope.NameHash = if (found_pkg) |pkg|43 const container_name_hash: Scope.NameHash = if (found_pkg) |pkg|
41 pkg.namespace_hash44 pkg.namespace_hash
42 else45 else
src/AstGen.zig+19-1
...@@ -890,7 +890,25 @@ pub fn nosuspendExpr(...@@ -890,7 +890,25 @@ pub fn nosuspendExpr(
890 node: ast.Node.Index,890 node: ast.Node.Index,
891) InnerError!Zir.Inst.Ref {891) InnerError!Zir.Inst.Ref {
892 const astgen = gz.astgen;892 const astgen = gz.astgen;
893 return astgen.failNode(node, "TODO AstGen nosuspendExpr", .{});893 const gpa = astgen.gpa;
894 const tree = &astgen.file.tree;
895 const node_datas = tree.nodes.items(.data);
896 const body_node = node_datas[node].lhs;
897 assert(body_node != 0);
898 if (gz.nosuspend_node != 0) {
899 return astgen.failNodeNotes(node, "redundant nosuspend block", .{}, &[_]u32{
900 try astgen.errNoteNode(gz.nosuspend_node, "other nosuspend block here", .{}),
901 });
902 }
903 if (gz.suspend_node != 0) {
904 return astgen.failNodeNotes(node, "inside a suspend block, nosuspend is implied", .{}, &[_]u32{
905 try astgen.errNoteNode(gz.suspend_node, "suspend block here", .{}),
906 });
907 }
908 gz.nosuspend_node = node;
909 const result = try expr(gz, scope, rl, body_node);
910 gz.nosuspend_node = 0;
911 return rvalue(gz, scope, rl, result, node);
894}912}
895913
896pub fn suspendExpr(914pub fn suspendExpr(
src/Compilation.zig+31-2
...@@ -441,8 +441,37 @@ pub const AllErrors = struct {...@@ -441,8 +441,37 @@ pub const AllErrors = struct {
441 const item = file.zir.extraData(Zir.Inst.CompileErrors.Item, extra_index);441 const item = file.zir.extraData(Zir.Inst.CompileErrors.Item, extra_index);
442 extra_index = item.end;442 extra_index = item.end;
443443
444 var notes: []Message = &[0]Message{};
444 if (item.data.notes != 0) {445 if (item.data.notes != 0) {
445 @panic("TODO implement AllErrors for Zir notes");446 const block = file.zir.extraData(Zir.Inst.Block, item.data.notes);
447 const body = file.zir.extra[block.end..][0..block.data.body_len];
448 notes = try arena.alloc(Message, body.len);
449 for (notes) |*note, i| {
450 const note_item = file.zir.extraData(Zir.Inst.CompileErrors.Item, body[i]);
451 const msg = file.zir.nullTerminatedString(note_item.data.msg);
452 const byte_offset = blk: {
453 const token_starts = file.tree.tokens.items(.start);
454 if (note_item.data.node != 0) {
455 const main_tokens = file.tree.nodes.items(.main_token);
456 const main_token = main_tokens[note_item.data.node];
457 break :blk token_starts[main_token];
458 }
459 break :blk token_starts[note_item.data.token] + note_item.data.byte_offset;
460 };
461 const loc = std.zig.findLineColumn(source, byte_offset);
462
463 note.* = .{
464 .src = .{
465 .src_path = try arena.dupe(u8, file.sub_file_path),
466 .msg = try arena.dupe(u8, msg),
467 .byte_offset = byte_offset,
468 .line = @intCast(u32, loc.line),
469 .column = @intCast(u32, loc.column),
470 .notes = &.{}, // TODO rework this function to be recursive
471 .source_line = try arena.dupe(u8, loc.source_line),
472 },
473 };
474 }
446 }475 }
447476
448 const msg = file.zir.nullTerminatedString(item.data.msg);477 const msg = file.zir.nullTerminatedString(item.data.msg);
...@@ -464,7 +493,7 @@ pub const AllErrors = struct {...@@ -464,7 +493,7 @@ pub const AllErrors = struct {
464 .byte_offset = byte_offset,493 .byte_offset = byte_offset,
465 .line = @intCast(u32, loc.line),494 .line = @intCast(u32, loc.line),
466 .column = @intCast(u32, loc.column),495 .column = @intCast(u32, loc.column),
467 .notes = &.{}, // TODO496 .notes = notes,
468 .source_line = try arena.dupe(u8, loc.source_line),497 .source_line = try arena.dupe(u8, loc.source_line),
469 },498 },
470 });499 });