authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-06-11 14:51:08+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-11 22:04:15-07:00
log54460e39ace2140e6bfcb0bf4ae1709d128f9e8d
tree903dad52a1f4610e33c6ccb2953b1fe6c1b82604
parent5b6906c22eb44b35cdce0368a36b035d6734df04

Autodoc: make it work under InternPool


3 files changed, 38 insertions(+), 36 deletions(-)

src/Autodoc.zig+23-22
......@@ -8,6 +8,7 @@ const CompilationModule = @import("Module.zig");
88const File = CompilationModule.File;
99const Module = @import("Package.zig");
1010const Tokenizer = std.zig.Tokenizer;
11const InternPool = @import("InternPool.zig");
1112const Zir = @import("Zir.zig");
1213const Ref = Zir.Inst.Ref;
1314const log = std.log.scoped(.autodoc);
......@@ -106,18 +107,20 @@ pub fn generateZirData(self: *Autodoc) !void {
106107 const file = self.comp_module.import_table.get(abs_root_src_path).?; // file is expected to be present in the import table
107108 // Append all the types in Zir.Inst.Ref.
108109 {
109 try self.types.append(self.arena, .{
110 .ComptimeExpr = .{ .name = "ComptimeExpr" },
111 });
112
113 // this skips Ref.none but it's ok becuse we replaced it with ComptimeExpr
114 var i: u32 = 1;
115 while (i <= @enumToInt(Ref.anyerror_void_error_union_type)) : (i += 1) {
110 comptime std.debug.assert(@enumToInt(InternPool.Index.first_type) == 0);
111 var i: u32 = 0;
112 while (i <= @enumToInt(InternPool.Index.last_type)) : (i += 1) {
113 const ip_index = @intToEnum(InternPool.Index, i);
116114 var tmpbuf = std.ArrayList(u8).init(self.arena);
117 try Ref.typed_value_map[i].val.fmtDebug().format("", .{}, tmpbuf.writer());
115 if (ip_index == .generic_poison_type) {
116 // Not a real type, doesn't have a normal name
117 try tmpbuf.writer().writeAll("(generic poison)");
118 } else {
119 try ip_index.toType().fmt(self.comp_module).format("", .{}, tmpbuf.writer());
120 }
118121 try self.types.append(
119122 self.arena,
120 switch (@intToEnum(Ref, i)) {
123 switch (ip_index) {
121124 else => blk: {
122125 // TODO: map the remaining refs to a correct type
123126 // instead of just assinging "array" to them.
......@@ -1038,7 +1041,7 @@ fn walkInstruction(
10381041 .ret_load => {
10391042 const un_node = data[inst_index].un_node;
10401043 const res_ptr_ref = un_node.operand;
1041 const res_ptr_inst = @enumToInt(res_ptr_ref) - Ref.typed_value_map.len;
1044 const res_ptr_inst = Zir.refToIndex(res_ptr_ref).?;
10421045 // TODO: this instruction doesn't let us know trivially if there's
10431046 // branching involved or not. For now here's the strat:
10441047 // We search backwarts until `ret_ptr` for `store_node`,
......@@ -2155,11 +2158,10 @@ fn walkInstruction(
21552158 const lhs_ref = blk: {
21562159 var lhs_extra = extra;
21572160 while (true) {
2158 if (@enumToInt(lhs_extra.data.lhs) < Ref.typed_value_map.len) {
2161 const lhs = Zir.refToIndex(lhs_extra.data.lhs) orelse {
21592162 break :blk lhs_extra.data.lhs;
2160 }
2163 };
21612164
2162 const lhs = @enumToInt(lhs_extra.data.lhs) - Ref.typed_value_map.len;
21632165 if (tags[lhs] != .field_val and
21642166 tags[lhs] != .field_ptr and
21652167 tags[lhs] != .field_type) break :blk lhs_extra.data.lhs;
......@@ -2186,8 +2188,7 @@ fn walkInstruction(
21862188 // TODO: double check that we really don't need type info here
21872189
21882190 const wr = blk: {
2189 if (@enumToInt(lhs_ref) >= Ref.typed_value_map.len) {
2190 const lhs_inst = @enumToInt(lhs_ref) - Ref.typed_value_map.len;
2191 if (Zir.refToIndex(lhs_ref)) |lhs_inst| {
21912192 if (tags[lhs_inst] == .call or tags[lhs_inst] == .field_call) {
21922193 break :blk DocData.WalkResult{
21932194 .expr = .{
......@@ -4670,16 +4671,19 @@ fn walkRef(
46704671 ref: Ref,
46714672 need_type: bool, // true when the caller needs also a typeRef for the return value
46724673) AutodocErrors!DocData.WalkResult {
4673 const enum_value = @enumToInt(ref);
4674 if (enum_value <= @enumToInt(Ref.anyerror_void_error_union_type)) {
4674 if (ref == .none) {
4675 return .{ .expr = .{ .comptimeExpr = 0 } };
4676 } else if (@enumToInt(ref) <= @enumToInt(InternPool.Index.last_type)) {
46754677 // We can just return a type that indexes into `types` with the
46764678 // enum value because in the beginning we pre-filled `types` with
46774679 // the types that are listed in `Ref`.
46784680 return DocData.WalkResult{
46794681 .typeRef = .{ .type = @enumToInt(std.builtin.TypeId.Type) },
4680 .expr = .{ .type = enum_value },
4682 .expr = .{ .type = @enumToInt(ref) },
46814683 };
4682 } else if (enum_value < Ref.typed_value_map.len) {
4684 } else if (Zir.refToIndex(ref)) |zir_index| {
4685 return self.walkInstruction(file, parent_scope, parent_src, zir_index, need_type);
4686 } else {
46834687 switch (ref) {
46844688 else => {
46854689 panicWithContext(
......@@ -4772,9 +4776,6 @@ fn walkRef(
47724776 // } };
47734777 // },
47744778 }
4775 } else {
4776 const zir_index = enum_value - Ref.typed_value_map.len;
4777 return self.walkInstruction(file, parent_scope, parent_src, zir_index, need_type);
47784779 }
47794780}
47804781
src/Compilation.zig-1
......@@ -2074,7 +2074,6 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
20742074 if (!build_options.only_c and !build_options.only_core_functionality) {
20752075 if (comp.emit_docs) |doc_location| {
20762076 if (comp.bin_file.options.module) |module| {
2077 if (true) @panic("TODO: get autodoc working again in this branch");
20782077 var autodoc = Autodoc.init(module, doc_location);
20792078 defer autodoc.deinit();
20802079 try autodoc.generateZirData();
src/type.zig+15-13
......@@ -315,23 +315,25 @@ pub const Type = struct {
315315 .comptime_float,
316316 .noreturn,
317317 => return writer.writeAll(@tagName(s)),
318
318319 .null,
319320 .undefined,
320321 => try writer.print("@TypeOf({s})", .{@tagName(s)}),
322
321323 .enum_literal => try writer.print("@TypeOf(.{s})", .{@tagName(s)}),
322 .atomic_order,
323 .atomic_rmw_op,
324 .calling_convention,
325 .address_space,
326 .float_mode,
327 .reduce_op,
328 .call_modifier,
329 .prefetch_options,
330 .export_options,
331 .extern_options,
332 .type_info,
333 .generic_poison,
334 => unreachable,
324 .atomic_order => try writer.writeAll("std.builtin.AtomicOrder"),
325 .atomic_rmw_op => try writer.writeAll("std.builtin.AtomicRmwOp"),
326 .calling_convention => try writer.writeAll("std.builtin.CallingConvention"),
327 .address_space => try writer.writeAll("std.builtin.AddressSpace"),
328 .float_mode => try writer.writeAll("std.builtin.FloatMode"),
329 .reduce_op => try writer.writeAll("std.builtin.ReduceOp"),
330 .call_modifier => try writer.writeAll("std.builtin.CallModifier"),
331 .prefetch_options => try writer.writeAll("std.builtin.PrefetchOptions"),
332 .export_options => try writer.writeAll("std.builtin.ExportOptions"),
333 .extern_options => try writer.writeAll("std.builtin.ExternOptions"),
334 .type_info => try writer.writeAll("std.builtin.Type"),
335
336 .generic_poison => unreachable,
335337 },
336338 .struct_type => |struct_type| {
337339 if (mod.structPtrUnwrap(struct_type.index)) |struct_obj| {