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;...@@ -2541,6 +2541,8 @@ var zigAnalysis;
2541 if (pkgI === zigAnalysis.rootPkg && rootIsStd) continue;2541 if (pkgI === zigAnalysis.rootPkg && rootIsStd) continue;
2542 let pkg = zigAnalysis.packages[pkgI];2542 let pkg = zigAnalysis.packages[pkgI];
2543 let pkgNames = canonPkgPaths[pkgI];2543 let pkgNames = canonPkgPaths[pkgI];
2544 if (pkgNames === undefined) continue;
2545
2544 let stack = [{2546 let stack = [{
2545 declNames: ([]),2547 declNames: ([]),
2546 type: zigAnalysis.types[pkg.main],2548 type: zigAnalysis.types[pkg.main],
...@@ -2576,6 +2578,21 @@ var zigAnalysis;...@@ -2576,6 +2578,21 @@ var zigAnalysis;
2576 type:value,2578 type:value,
2577 });2579 });
2578 }2580 }
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 }
2579 }2596 }
2580 }2597 }
2581 }2598 }
src/Autodoc.zig+38-13
...@@ -65,7 +65,6 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -65,7 +65,6 @@ pub fn generateZirData(self: *Autodoc) !void {
65 std.debug.print("path: {s}\n", .{path});65 std.debug.print("path: {s}\n", .{path});
66 }66 }
67 }67 }
68 std.debug.print("basename: {s}\n", .{self.doc_location.basename});
6968
70 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;69 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
71 const dir =70 const dir =
...@@ -178,9 +177,16 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -178,9 +177,16 @@ pub fn generateZirData(self: *Autodoc) !void {
178 try self.packages.put(self.arena, self.module.main_pkg, .{177 try self.packages.put(self.arena, self.module.main_pkg, .{
179 .name = "root",178 .name = "root",
180 .main = main_type_index,179 .main = main_type_index,
181 .table = std.StringHashMapUnmanaged(usize){},180 .table = .{},
182 });181 });
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 );
184 }190 }
185191
186 var root_scope = Scope{ .parent = null, .enclosing_type = main_type_index };192 var root_scope = Scope{ .parent = null, .enclosing_type = main_type_index };
...@@ -377,7 +383,11 @@ const DocData = struct {...@@ -377,7 +383,11 @@ const DocData = struct {
377 name: []const u8 = "(root)",383 name: []const u8 = "(root)",
378 file: usize = 0, // index into `files`384 file: usize = 0, // index into `files`
379 main: usize = 0, // index into `types`385 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
382 pub fn jsonStringify(392 pub fn jsonStringify(
383 self: DocPackage,393 self: DocPackage,
...@@ -742,10 +752,19 @@ fn walkInstruction(...@@ -742,10 +752,19 @@ fn walkInstruction(
742 },752 },
743 .import => {753 .import => {
744 const str_tok = data[inst_index].str_tok;754 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 };
746 // importFile cannot error out since all files765 // importFile cannot error out since all files
747 // are already loaded at this point766 // are already loaded at this point
748 if (file.pkg.table.get(path)) |other_package| {767 if (maybe_other_package) |other_package| {
749 const result = try self.packages.getOrPut(self.arena, other_package);768 const result = try self.packages.getOrPut(self.arena, other_package);
750769
751 // Immediately add this package to the import table of our770 // Immediately add this package to the import table of our
...@@ -758,8 +777,11 @@ fn walkInstruction(...@@ -758,8 +777,11 @@ fn walkInstruction(
758 // We're bailing for now, but maybe we shouldn't?777 // We're bailing for now, but maybe we shouldn't?
759 _ = try current_package.table.getOrPutValue(778 _ = try current_package.table.getOrPutValue(
760 self.arena,779 self.arena,
761 path,780 other_package,
762 self.packages.getIndex(other_package).?,781 .{
782 .name = path,
783 .value = self.packages.getIndex(other_package).?,
784 },
763 );785 );
764 }786 }
765787
...@@ -775,7 +797,7 @@ fn walkInstruction(...@@ -775,7 +797,7 @@ fn walkInstruction(
775 result.value_ptr.* = .{797 result.value_ptr.* = .{
776 .name = path,798 .name = path,
777 .main = main_type_index,799 .main = main_type_index,
778 .table = std.StringHashMapUnmanaged(usize){},800 .table = .{},
779 };801 };
780802
781 // TODO: Add this package as a dependency to the current pakcage803 // TODO: Add this package as a dependency to the current pakcage
...@@ -3777,12 +3799,15 @@ fn writeFileTableToJson(map: std.AutoArrayHashMapUnmanaged(*File, usize), jsw: a...@@ -3777,12 +3799,15 @@ fn writeFileTableToJson(map: std.AutoArrayHashMapUnmanaged(*File, usize), jsw: a
3777 try jsw.endObject();3799 try jsw.endObject();
3778}3800}
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 {
3781 try jsw.beginObject();3806 try jsw.beginObject();
3782 var it = map.iterator();3807 var it = map.valueIterator();
3783 while (it.next()) |entry| {3808 while (it.next()) |entry| {
3784 try jsw.objectField(entry.key_ptr.*);3809 try jsw.objectField(entry.name);
3785 try jsw.emitNumber(entry.value_ptr.*);3810 try jsw.emitNumber(entry.value);
3786 }3811 }
3787 try jsw.endObject();3812 try jsw.endObject();
3788}3813}