authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-06-11 19:13:55+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:12-07:00
log947eff6e395c7f4ffd6ef6cc10f1312f73f78d3b
treeaafa9ecd932255b2740fd1c356e45f3c50787891
parent54fe5ea4f34886d0d7951d2a0fe1353174a6df11

autodoc: handle special case of autodoc for std


2 files changed, 55 insertions(+), 13 deletions(-)

lib/docs/main.js+17
......@@ -2541,6 +2541,8 @@ var zigAnalysis;
25412541 if (pkgI === zigAnalysis.rootPkg && rootIsStd) continue;
25422542 let pkg = zigAnalysis.packages[pkgI];
25432543 let pkgNames = canonPkgPaths[pkgI];
2544 if (pkgNames === undefined) continue;
2545
25442546 let stack = [{
25452547 declNames: ([]),
25462548 type: zigAnalysis.types[pkg.main],
......@@ -2576,6 +2578,21 @@ var zigAnalysis;
25762578 type:value,
25772579 });
25782580 }
2581
2582
2583 // Generic function
2584 if (value.kind == typeKinds.Fn && value.generic_ret != null) {
2585 let resolvedVal = resolveValue({ expr: value.generic_ret});
2586 if ("type" in resolvedVal.expr) {
2587 let generic_type = zigAnalysis.types[resolvedVal.expr.type];
2588 if (isContainerType(generic_type)){
2589 stack.push({
2590 declNames: declNames,
2591 type: generic_type,
2592 });
2593 }
2594 }
2595 }
25792596 }
25802597 }
25812598 }
src/Autodoc.zig+38-13
......@@ -65,7 +65,6 @@ pub fn generateZirData(self: *Autodoc) !void {
6565 std.debug.print("path: {s}\n", .{path});
6666 }
6767 }
68 std.debug.print("basename: {s}\n", .{self.doc_location.basename});
6968
7069 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
7170 const dir =
......@@ -178,9 +177,16 @@ pub fn generateZirData(self: *Autodoc) !void {
178177 try self.packages.put(self.arena, self.module.main_pkg, .{
179178 .name = "root",
180179 .main = main_type_index,
181 .table = std.StringHashMapUnmanaged(usize){},
180 .table = .{},
182181 });
183 try self.packages.entries.items(.value)[0].table.put(self.arena, "root", 0);
182 try self.packages.entries.items(.value)[0].table.put(
183 self.arena,
184 self.module.main_pkg,
185 .{
186 .name = "root",
187 .value = 0,
188 },
189 );
184190 }
185191
186192 var root_scope = Scope{ .parent = null, .enclosing_type = main_type_index };
......@@ -377,7 +383,11 @@ const DocData = struct {
377383 name: []const u8 = "(root)",
378384 file: usize = 0, // index into `files`
379385 main: usize = 0, // index into `types`
380 table: std.StringHashMapUnmanaged(usize),
386 table: std.AutoHashMapUnmanaged(*Package, TableEntry),
387 pub const TableEntry = struct {
388 name: []const u8,
389 value: usize,
390 };
381391
382392 pub fn jsonStringify(
383393 self: DocPackage,
......@@ -742,10 +752,19 @@ fn walkInstruction(
742752 },
743753 .import => {
744754 const str_tok = data[inst_index].str_tok;
745 const path = str_tok.get(file.zir);
755 var path = str_tok.get(file.zir);
756
757 const maybe_other_package: ?*Package = blk: {
758 if (self.module.main_pkg_in_std and std.mem.eql(u8, path, "std")) {
759 path = "root";
760 break :blk self.module.main_pkg;
761 } else {
762 break :blk file.pkg.table.get(path);
763 }
764 };
746765 // importFile cannot error out since all files
747766 // are already loaded at this point
748 if (file.pkg.table.get(path)) |other_package| {
767 if (maybe_other_package) |other_package| {
749768 const result = try self.packages.getOrPut(self.arena, other_package);
750769
751770 // Immediately add this package to the import table of our
......@@ -758,8 +777,11 @@ fn walkInstruction(
758777 // We're bailing for now, but maybe we shouldn't?
759778 _ = try current_package.table.getOrPutValue(
760779 self.arena,
761 path,
762 self.packages.getIndex(other_package).?,
780 other_package,
781 .{
782 .name = path,
783 .value = self.packages.getIndex(other_package).?,
784 },
763785 );
764786 }
765787
......@@ -775,7 +797,7 @@ fn walkInstruction(
775797 result.value_ptr.* = .{
776798 .name = path,
777799 .main = main_type_index,
778 .table = std.StringHashMapUnmanaged(usize){},
800 .table = .{},
779801 };
780802
781803 // TODO: Add this package as a dependency to the current pakcage
......@@ -3777,12 +3799,15 @@ fn writeFileTableToJson(map: std.AutoArrayHashMapUnmanaged(*File, usize), jsw: a
37773799 try jsw.endObject();
37783800}
37793801
3780fn writePackageTableToJson(map: std.StringHashMapUnmanaged(usize), jsw: anytype) !void {
3802fn writePackageTableToJson(
3803 map: std.AutoHashMapUnmanaged(*Package, DocData.DocPackage.TableEntry),
3804 jsw: anytype,
3805) !void {
37813806 try jsw.beginObject();
3782 var it = map.iterator();
3807 var it = map.valueIterator();
37833808 while (it.next()) |entry| {
3784 try jsw.objectField(entry.key_ptr.*);
3785 try jsw.emitNumber(entry.value_ptr.*);
3809 try jsw.objectField(entry.name);
3810 try jsw.emitNumber(entry.value);
37863811 }
37873812 try jsw.endObject();
37883813}