authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-18 13:07:02-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:12-07:00
log22d61faf17490a375c0613d1567a0b0d7400ddcb
tree77f477b361ba1f2e679f73bcba534b838c01dbc5
parentad7bc57919b37f4a25080a5f884a8e469e0a86a4

Autodoc: update to new ZIR encoding

* error_to_int, int_to_error moved to extended * struct field encodings are different

1 files changed, 77 insertions(+), 25 deletions(-)

src/Autodoc.zig+77-25
......@@ -1233,8 +1233,6 @@ fn walkInstruction(
12331233 .frame_type,
12341234 .frame_size,
12351235 .ptr_to_int,
1236 .error_to_int,
1237 .int_to_error,
12381236 .minimum,
12391237 .maximum,
12401238 .bit_not,
......@@ -2521,12 +2519,6 @@ fn walkInstruction(
25212519 } else null;
25222520 _ = src_node;
25232521
2524 const body_len = if (small.has_body_len) blk: {
2525 const body_len = file.zir.extra[extra_index];
2526 extra_index += 1;
2527 break :blk body_len;
2528 } else 0;
2529
25302522 const fields_len = if (small.has_fields_len) blk: {
25312523 const fields_len = file.zir.extra[extra_index];
25322524 extra_index += 1;
......@@ -2570,9 +2562,6 @@ fn walkInstruction(
25702562 extra_index,
25712563 );
25722564
2573 // const body = file.zir.extra[extra_index..][0..body_len];
2574 extra_index += body_len;
2575
25762565 var field_type_refs: std.ArrayListUnmanaged(DocData.Expr) = .{};
25772566 var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
25782567 try self.collectStructFieldInfo(
......@@ -2614,6 +2603,24 @@ fn walkInstruction(
26142603 .expr = .{ .this = parent_scope.enclosing_type },
26152604 };
26162605 },
2606 .error_to_int,
2607 .int_to_error,
2608 => {
2609 const extra = file.zir.extraData(Zir.Inst.UnNode, extended.operand).data;
2610 const bin_index = self.exprs.items.len;
2611 try self.exprs.append(self.arena, .{ .builtin = .{ .param = 0 } });
2612 const param = try self.walkRef(file, parent_scope, extra.operand, false);
2613
2614 const param_index = self.exprs.items.len;
2615 try self.exprs.append(self.arena, param.expr);
2616
2617 self.exprs.items[bin_index] = .{ .builtin = .{ .name = @tagName(tags[inst_index]), .param = param_index } };
2618
2619 return DocData.WalkResult{
2620 .typeRef = param.typeRef orelse .{ .type = @enumToInt(Ref.type_type) },
2621 .expr = .{ .builtinIndex = bin_index },
2622 };
2623 },
26172624 }
26182625 },
26192626 }
......@@ -3627,6 +3634,17 @@ fn collectStructFieldInfo(
36273634 const bits_per_field = 4;
36283635 const fields_per_u32 = 32 / bits_per_field;
36293636 const bit_bags_count = std.math.divCeil(usize, fields_len, fields_per_u32) catch unreachable;
3637
3638 const Field = struct {
3639 field_name: u32,
3640 doc_comment_index: u32,
3641 type_body_len: u32 = 0,
3642 align_body_len: u32 = 0,
3643 init_body_len: u32 = 0,
3644 type_ref: Zir.Inst.Ref = .none,
3645 };
3646 const fields = try self.arena.alloc(Field, fields_len);
3647
36303648 var bit_bag_index: usize = extra_index;
36313649 extra_index += bit_bags_count;
36323650
......@@ -3643,35 +3661,69 @@ fn collectStructFieldInfo(
36433661 cur_bit_bag >>= 1;
36443662 // const is_comptime = @truncate(u1, cur_bit_bag) != 0;
36453663 cur_bit_bag >>= 1;
3646 const unused = @truncate(u1, cur_bit_bag) != 0;
3664 const has_type_body = @truncate(u1, cur_bit_bag) != 0;
36473665 cur_bit_bag >>= 1;
3648 _ = unused;
36493666
3650 const field_name = file.zir.nullTerminatedString(file.zir.extra[extra_index]);
3651 extra_index += 1;
3652 const field_type = @intToEnum(Zir.Inst.Ref, file.zir.extra[extra_index]);
3667 const field_name = file.zir.extra[extra_index];
36533668 extra_index += 1;
36543669 const doc_comment_index = file.zir.extra[extra_index];
36553670 extra_index += 1;
36563671
3657 if (has_align) extra_index += 1;
3658 if (has_default) extra_index += 1;
3672 fields[field_i] = .{
3673 .field_name = field_name,
3674 .doc_comment_index = doc_comment_index,
3675 };
36593676
3660 // type
3661 {
3662 const walk_result = try self.walkRef(file, scope, field_type, false);
3663 try field_type_refs.append(self.arena, walk_result.expr);
3677 if (has_type_body) {
3678 fields[field_i].type_body_len = file.zir.extra[extra_index];
3679 } else {
3680 fields[field_i].type_ref = @intToEnum(Zir.Inst.Ref, file.zir.extra[extra_index]);
3681 }
3682 extra_index += 1;
3683
3684 if (has_align) {
3685 fields[field_i].align_body_len = file.zir.extra[extra_index];
3686 extra_index += 1;
36643687 }
3688 if (has_default) {
3689 fields[field_i].init_body_len = file.zir.extra[extra_index];
3690 extra_index += 1;
3691 }
3692 }
3693
3694 const data = file.zir.instructions.items(.data);
3695
3696 for (fields) |field| {
3697 const type_expr = expr: {
3698 if (field.type_ref != .none) {
3699 const walk_result = try self.walkRef(file, scope, field.type_ref, false);
3700 break :expr walk_result.expr;
3701 }
3702
3703 std.debug.assert(field.type_body_len != 0);
3704 const body = file.zir.extra[extra_index..][0..field.type_body_len];
3705 extra_index += body.len;
3706
3707 const break_inst = body[body.len - 1];
3708 const operand = data[break_inst].@"break".operand;
3709 const walk_result = try self.walkRef(file, scope, operand, false);
3710 break :expr walk_result.expr;
3711 };
3712
3713 extra_index += field.align_body_len;
3714 extra_index += field.init_body_len;
3715
3716 try field_type_refs.append(self.arena, type_expr);
36653717
36663718 // ast node
36673719 {
36683720 try field_name_indexes.append(self.arena, self.ast_nodes.items.len);
3669 const doc_comment: ?[]const u8 = if (doc_comment_index != 0)
3670 file.zir.nullTerminatedString(doc_comment_index)
3721 const doc_comment: ?[]const u8 = if (field.doc_comment_index != 0)
3722 file.zir.nullTerminatedString(field.doc_comment_index)
36713723 else
36723724 null;
36733725 try self.ast_nodes.append(self.arena, .{
3674 .name = field_name,
3726 .name = file.zir.nullTerminatedString(field.field_name),
36753727 .docs = doc_comment,
36763728 });
36773729 }