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");...@@ -8,6 +8,7 @@ const CompilationModule = @import("Module.zig");
8const File = CompilationModule.File;8const File = CompilationModule.File;
9const Module = @import("Package.zig");9const Module = @import("Package.zig");
10const Tokenizer = std.zig.Tokenizer;10const Tokenizer = std.zig.Tokenizer;
11const InternPool = @import("InternPool.zig");
11const Zir = @import("Zir.zig");12const Zir = @import("Zir.zig");
12const Ref = Zir.Inst.Ref;13const Ref = Zir.Inst.Ref;
13const log = std.log.scoped(.autodoc);14const log = std.log.scoped(.autodoc);
...@@ -106,18 +107,20 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -106,18 +107,20 @@ pub fn generateZirData(self: *Autodoc) !void {
106 const file = self.comp_module.import_table.get(abs_root_src_path).?; // file is expected to be present in the import table107 const file = self.comp_module.import_table.get(abs_root_src_path).?; // file is expected to be present in the import table
107 // Append all the types in Zir.Inst.Ref.108 // Append all the types in Zir.Inst.Ref.
108 {109 {
109 try self.types.append(self.arena, .{110 comptime std.debug.assert(@enumToInt(InternPool.Index.first_type) == 0);
110 .ComptimeExpr = .{ .name = "ComptimeExpr" },111 var i: u32 = 0;
111 });112 while (i <= @enumToInt(InternPool.Index.last_type)) : (i += 1) {
112113 const ip_index = @intToEnum(InternPool.Index, i);
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) {
116 var tmpbuf = std.ArrayList(u8).init(self.arena);114 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 }
118 try self.types.append(121 try self.types.append(
119 self.arena,122 self.arena,
120 switch (@intToEnum(Ref, i)) {123 switch (ip_index) {
121 else => blk: {124 else => blk: {
122 // TODO: map the remaining refs to a correct type125 // TODO: map the remaining refs to a correct type
123 // instead of just assinging "array" to them.126 // instead of just assinging "array" to them.
...@@ -1038,7 +1041,7 @@ fn walkInstruction(...@@ -1038,7 +1041,7 @@ fn walkInstruction(
1038 .ret_load => {1041 .ret_load => {
1039 const un_node = data[inst_index].un_node;1042 const un_node = data[inst_index].un_node;
1040 const res_ptr_ref = un_node.operand;1043 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).?;
1042 // TODO: this instruction doesn't let us know trivially if there's1045 // TODO: this instruction doesn't let us know trivially if there's
1043 // branching involved or not. For now here's the strat:1046 // branching involved or not. For now here's the strat:
1044 // We search backwarts until `ret_ptr` for `store_node`,1047 // We search backwarts until `ret_ptr` for `store_node`,
...@@ -2155,11 +2158,10 @@ fn walkInstruction(...@@ -2155,11 +2158,10 @@ fn walkInstruction(
2155 const lhs_ref = blk: {2158 const lhs_ref = blk: {
2156 var lhs_extra = extra;2159 var lhs_extra = extra;
2157 while (true) {2160 while (true) {
2158 if (@enumToInt(lhs_extra.data.lhs) < Ref.typed_value_map.len) {2161 const lhs = Zir.refToIndex(lhs_extra.data.lhs) orelse {
2159 break :blk lhs_extra.data.lhs;2162 break :blk lhs_extra.data.lhs;
2160 }2163 };
21612164
2162 const lhs = @enumToInt(lhs_extra.data.lhs) - Ref.typed_value_map.len;
2163 if (tags[lhs] != .field_val and2165 if (tags[lhs] != .field_val and
2164 tags[lhs] != .field_ptr and2166 tags[lhs] != .field_ptr and
2165 tags[lhs] != .field_type) break :blk lhs_extra.data.lhs;2167 tags[lhs] != .field_type) break :blk lhs_extra.data.lhs;
...@@ -2186,8 +2188,7 @@ fn walkInstruction(...@@ -2186,8 +2188,7 @@ fn walkInstruction(
2186 // TODO: double check that we really don't need type info here2188 // TODO: double check that we really don't need type info here
21872189
2188 const wr = blk: {2190 const wr = blk: {
2189 if (@enumToInt(lhs_ref) >= Ref.typed_value_map.len) {2191 if (Zir.refToIndex(lhs_ref)) |lhs_inst| {
2190 const lhs_inst = @enumToInt(lhs_ref) - Ref.typed_value_map.len;
2191 if (tags[lhs_inst] == .call or tags[lhs_inst] == .field_call) {2192 if (tags[lhs_inst] == .call or tags[lhs_inst] == .field_call) {
2192 break :blk DocData.WalkResult{2193 break :blk DocData.WalkResult{
2193 .expr = .{2194 .expr = .{
...@@ -4670,16 +4671,19 @@ fn walkRef(...@@ -4670,16 +4671,19 @@ fn walkRef(
4670 ref: Ref,4671 ref: Ref,
4671 need_type: bool, // true when the caller needs also a typeRef for the return value4672 need_type: bool, // true when the caller needs also a typeRef for the return value
4672) AutodocErrors!DocData.WalkResult {4673) AutodocErrors!DocData.WalkResult {
4673 const enum_value = @enumToInt(ref);4674 if (ref == .none) {
4674 if (enum_value <= @enumToInt(Ref.anyerror_void_error_union_type)) {4675 return .{ .expr = .{ .comptimeExpr = 0 } };
4676 } else if (@enumToInt(ref) <= @enumToInt(InternPool.Index.last_type)) {
4675 // We can just return a type that indexes into `types` with the4677 // We can just return a type that indexes into `types` with the
4676 // enum value because in the beginning we pre-filled `types` with4678 // enum value because in the beginning we pre-filled `types` with
4677 // the types that are listed in `Ref`.4679 // the types that are listed in `Ref`.
4678 return DocData.WalkResult{4680 return DocData.WalkResult{
4679 .typeRef = .{ .type = @enumToInt(std.builtin.TypeId.Type) },4681 .typeRef = .{ .type = @enumToInt(std.builtin.TypeId.Type) },
4680 .expr = .{ .type = enum_value },4682 .expr = .{ .type = @enumToInt(ref) },
4681 };4683 };
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 {
4683 switch (ref) {4687 switch (ref) {
4684 else => {4688 else => {
4685 panicWithContext(4689 panicWithContext(
...@@ -4772,9 +4776,6 @@ fn walkRef(...@@ -4772,9 +4776,6 @@ fn walkRef(
4772 // } };4776 // } };
4773 // },4777 // },
4774 }4778 }
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);
4778 }4779 }
4779}4780}
47804781
src/Compilation.zig-1
...@@ -2074,7 +2074,6 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void...@@ -2074,7 +2074,6 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
2074 if (!build_options.only_c and !build_options.only_core_functionality) {2074 if (!build_options.only_c and !build_options.only_core_functionality) {
2075 if (comp.emit_docs) |doc_location| {2075 if (comp.emit_docs) |doc_location| {
2076 if (comp.bin_file.options.module) |module| {2076 if (comp.bin_file.options.module) |module| {
2077 if (true) @panic("TODO: get autodoc working again in this branch");
2078 var autodoc = Autodoc.init(module, doc_location);2077 var autodoc = Autodoc.init(module, doc_location);
2079 defer autodoc.deinit();2078 defer autodoc.deinit();
2080 try autodoc.generateZirData();2079 try autodoc.generateZirData();
src/type.zig+15-13
...@@ -315,23 +315,25 @@ pub const Type = struct {...@@ -315,23 +315,25 @@ pub const Type = struct {
315 .comptime_float,315 .comptime_float,
316 .noreturn,316 .noreturn,
317 => return writer.writeAll(@tagName(s)),317 => return writer.writeAll(@tagName(s)),
318
318 .null,319 .null,
319 .undefined,320 .undefined,
320 => try writer.print("@TypeOf({s})", .{@tagName(s)}),321 => try writer.print("@TypeOf({s})", .{@tagName(s)}),
322
321 .enum_literal => try writer.print("@TypeOf(.{s})", .{@tagName(s)}),323 .enum_literal => try writer.print("@TypeOf(.{s})", .{@tagName(s)}),
322 .atomic_order,324 .atomic_order => try writer.writeAll("std.builtin.AtomicOrder"),
323 .atomic_rmw_op,325 .atomic_rmw_op => try writer.writeAll("std.builtin.AtomicRmwOp"),
324 .calling_convention,326 .calling_convention => try writer.writeAll("std.builtin.CallingConvention"),
325 .address_space,327 .address_space => try writer.writeAll("std.builtin.AddressSpace"),
326 .float_mode,328 .float_mode => try writer.writeAll("std.builtin.FloatMode"),
327 .reduce_op,329 .reduce_op => try writer.writeAll("std.builtin.ReduceOp"),
328 .call_modifier,330 .call_modifier => try writer.writeAll("std.builtin.CallModifier"),
329 .prefetch_options,331 .prefetch_options => try writer.writeAll("std.builtin.PrefetchOptions"),
330 .export_options,332 .export_options => try writer.writeAll("std.builtin.ExportOptions"),
331 .extern_options,333 .extern_options => try writer.writeAll("std.builtin.ExternOptions"),
332 .type_info,334 .type_info => try writer.writeAll("std.builtin.Type"),
333 .generic_poison,335
334 => unreachable,336 .generic_poison => unreachable,
335 },337 },
336 .struct_type => |struct_type| {338 .struct_type => |struct_type| {
337 if (mod.structPtrUnwrap(struct_type.index)) |struct_obj| {339 if (mod.structPtrUnwrap(struct_type.index)) |struct_obj| {