authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-03-07 19:31:23+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:11-07:00
log1e2cd421b931b1de14492868647a88b7246722ae
treee475225d898fa08c4c5afef091bc329b57fb5cfe
parenteced8c065d8f90e81af5673ba8086078bd22b9d2

autodoc: add support for array decls


2 files changed, 78 insertions(+), 7 deletions(-)

lib/docs/main.js+7-4
......@@ -184,6 +184,10 @@
184184 return resolveTypeRefToTypeId(decl.value.float.typeRef);
185185 }
186186
187 if ("array" in decl.value) {
188 return resolveTypeRefToTypeId(decl.value.array.typeRef);
189 }
190
187191 if ("struct" in decl.value) {
188192 return resolveTypeRefToTypeId(decl.value.struct.typeRef);
189193 }
......@@ -694,7 +698,7 @@
694698 function shouldSkipParamName(typeIndex, paramName) {
695699 var typeObj = zigAnalysis.types[typeIndex];
696700 if (typeObj.kind === typeKinds.Pointer && getPtrSize(typeObj) === pointerSizeEnum.One) {
697 typeIndex = typeObj.elem;
701 typeIndex = typeObj.child;
698702 }
699703 return typeIndexName(typeIndex, false, true).toLowerCase() === paramName;
700704 }
......@@ -747,7 +751,7 @@
747751 name += typeObj.len;
748752 }
749753 name += "]";
750 name += typeValueName(typeObj.elem, wantHtml, wantSubLink, null);
754 name += typeValueName(typeObj.child, wantHtml, wantSubLink, null);
751755 return name;
752756 case typeKinds.Optional:
753757
......@@ -810,7 +814,7 @@
810814 }
811815 name += ") ";
812816 }
813 name += typeValueName(typeObj.elem, wantHtml, wantSubLink, null);
817 name += typeValueName(typeObj.child, wantHtml, wantSubLink, null);
814818 return name;
815819 case typeKinds.Float:
816820 if (wantHtml) {
......@@ -1328,7 +1332,6 @@
13281332 html += '<span class="tok-kw" style="color:red;">#FAILURE#</span>';
13291333 } else if ("declPath" in field) {
13301334 for (var j = field.declPath.length - 1; j >= 0; j--) {
1331 console.log("hello");
13321335 var decl = zigAnalysis.decls[field.declPath[j]];
13331336
13341337 html += '<a href="'+navLinkDecl(decl.name)+'">';
src/Autodoc.zig+71-3
......@@ -86,7 +86,6 @@ pub fn generateZirData(self: *Autodoc) !void {
8686 break :blk .{
8787 .Array = .{
8888 .len = 1,
89 .name = tmpbuf.toOwnedSlice(),
9089 .child = .{ .type = 0 },
9190 },
9291 };
......@@ -352,7 +351,6 @@ const DocData = struct {
352351 child: TypeRef,
353352 },
354353 Array: struct {
355 name: []const u8,
356354 len: usize,
357355 child: TypeRef,
358356 },
......@@ -522,6 +520,7 @@ const DocData = struct {
522520 value: f64, // direct value
523521 negated: bool = false,
524522 },
523 array: Array,
525524 call: usize, // index in `calls`
526525
527526 const Struct = struct {
......@@ -531,6 +530,11 @@ const DocData = struct {
531530 val: WalkResult,
532531 },
533532 };
533 const Array = struct {
534 typeRef: TypeRef,
535 data: []WalkResult,
536 };
537
534538 pub fn jsonStringify(
535539 self: WalkResult,
536540 options: std.json.StringifyOptions,
......@@ -586,6 +590,24 @@ const DocData = struct {
586590 try w.print("{d}{s}", .{ d, comma });
587591 }
588592 },
593 .array => |v| try std.json.stringify(
594 struct { @"array": Array }{ .@"array" = v },
595 options,
596 w,
597 ),
598
599 // try w.print("{ len: {},\n", .{v.len});
600
601 // if (options.whitespace) |ws| try ws.outputIndent(w);
602 // try w.print("typeRef: ", .{});
603 // try v.typeRef.jsonStringify(options, w);
604
605 // try w.print("{{ \"data\": [", .{});
606 // for (v.data) |d, i| {
607 // const comma = if (i == v.len - 1) "]}" else ",";
608 // try w.print("{d}{s}", .{ d, comma });
609 // }
610
589611 }
590612 }
591613 };
......@@ -615,6 +637,16 @@ fn walkInstruction(
615637 const path = str_tok.get(file.zir);
616638 // importFile cannot error out since all files
617639 // are already loaded at this point
640 if (file.pkg.table.get(path) != null) {
641 const cte_slot_index = self.comptime_exprs.items.len;
642 try self.comptime_exprs.append(self.arena, .{
643 .code = path,
644 .typeRef = .{ .type = @enumToInt(DocData.DocTypeKinds.Type) },
645 });
646 return DocData.WalkResult{
647 .comptimeExpr = cte_slot_index,
648 };
649 }
618650 const new_file = self.module.importFile(file, path) catch unreachable;
619651 const result = try self.files.getOrPut(self.arena, new_file.file);
620652 if (result.found_existing) {
......@@ -644,6 +676,28 @@ fn walkInstruction(
644676 },
645677 };
646678 },
679 .array_init => {
680 const pl_node = data[inst_index].pl_node;
681 const extra = file.zir.extraData(Zir.Inst.MultiOp, pl_node.payload_index);
682 const operands = file.zir.refSlice(extra.end, extra.data.operands_len);
683 const array_data = try self.arena.alloc(DocData.WalkResult, operands.len);
684 for (operands) |op, idx| {
685 array_data[idx] = try self.walkRef(file, parent_scope, op);
686 }
687
688 const type_slot_index = self.types.items.len;
689 try self.types.append(self.arena, .{
690 .Array = .{
691 .len = operands.len,
692 .child = typeOfWalkResult(array_data[0]),
693 },
694 });
695
696 return DocData.WalkResult{ .array = .{
697 .typeRef = .{ .type = type_slot_index },
698 .data = array_data,
699 } };
700 },
647701 .float => {
648702 const float = data[inst_index].float;
649703 return DocData.WalkResult{
......@@ -1555,7 +1609,8 @@ fn tryResolveDeclPath(
15551609 // with the final decl in `dp`.
15561610 // We then write the original value back as soon as we're done with the
15571611 // recoursive call. This will work out correctly even if the path
1558 // will not get fully resolved.
1612 // will not get fully resolved (also in the case that final_decl is
1613 // not resolved yet).
15591614 path[i] = final_decl_index;
15601615 try self.tryResolveDeclPath(file, path);
15611616 path[i] = decl_index;
......@@ -1870,6 +1925,19 @@ fn walkResultToTypeRef(wr: DocData.WalkResult) DocData.TypeRef {
18701925 };
18711926}
18721927
1928fn typeOfWalkResult(wr: DocData.WalkResult) DocData.TypeRef {
1929 return switch (wr) {
1930 else => std.debug.panic(
1931 "TODO: handle `{s}` in typeOfWalkResult\n",
1932 .{@tagName(wr)},
1933 ),
1934 .type => .{ .type = @enumToInt(DocData.DocTypeKinds.Type) },
1935 .int => |v| v.typeRef,
1936 .float => |v| v.typeRef,
1937 .array => |v| v.typeRef,
1938 };
1939}
1940
18731941//fn collectParamInfo(self: *Autodoc, file: *File, scope: *Scope, inst_idx: Zir.Index) void {
18741942
18751943//}