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 @@...@@ -184,6 +184,10 @@
184 return resolveTypeRefToTypeId(decl.value.float.typeRef);184 return resolveTypeRefToTypeId(decl.value.float.typeRef);
185 }185 }
186186
187 if ("array" in decl.value) {
188 return resolveTypeRefToTypeId(decl.value.array.typeRef);
189 }
190
187 if ("struct" in decl.value) {191 if ("struct" in decl.value) {
188 return resolveTypeRefToTypeId(decl.value.struct.typeRef);192 return resolveTypeRefToTypeId(decl.value.struct.typeRef);
189 }193 }
...@@ -694,7 +698,7 @@...@@ -694,7 +698,7 @@
694 function shouldSkipParamName(typeIndex, paramName) {698 function shouldSkipParamName(typeIndex, paramName) {
695 var typeObj = zigAnalysis.types[typeIndex];699 var typeObj = zigAnalysis.types[typeIndex];
696 if (typeObj.kind === typeKinds.Pointer && getPtrSize(typeObj) === pointerSizeEnum.One) {700 if (typeObj.kind === typeKinds.Pointer && getPtrSize(typeObj) === pointerSizeEnum.One) {
697 typeIndex = typeObj.elem;701 typeIndex = typeObj.child;
698 }702 }
699 return typeIndexName(typeIndex, false, true).toLowerCase() === paramName;703 return typeIndexName(typeIndex, false, true).toLowerCase() === paramName;
700 }704 }
...@@ -747,7 +751,7 @@...@@ -747,7 +751,7 @@
747 name += typeObj.len;751 name += typeObj.len;
748 }752 }
749 name += "]";753 name += "]";
750 name += typeValueName(typeObj.elem, wantHtml, wantSubLink, null);754 name += typeValueName(typeObj.child, wantHtml, wantSubLink, null);
751 return name;755 return name;
752 case typeKinds.Optional:756 case typeKinds.Optional:
753757
...@@ -810,7 +814,7 @@...@@ -810,7 +814,7 @@
810 }814 }
811 name += ") ";815 name += ") ";
812 }816 }
813 name += typeValueName(typeObj.elem, wantHtml, wantSubLink, null);817 name += typeValueName(typeObj.child, wantHtml, wantSubLink, null);
814 return name;818 return name;
815 case typeKinds.Float:819 case typeKinds.Float:
816 if (wantHtml) {820 if (wantHtml) {
...@@ -1328,7 +1332,6 @@...@@ -1328,7 +1332,6 @@
1328 html += '<span class="tok-kw" style="color:red;">#FAILURE#</span>';1332 html += '<span class="tok-kw" style="color:red;">#FAILURE#</span>';
1329 } else if ("declPath" in field) {1333 } else if ("declPath" in field) {
1330 for (var j = field.declPath.length - 1; j >= 0; j--) {1334 for (var j = field.declPath.length - 1; j >= 0; j--) {
1331 console.log("hello");
1332 var decl = zigAnalysis.decls[field.declPath[j]];1335 var decl = zigAnalysis.decls[field.declPath[j]];
13331336
1334 html += '<a href="'+navLinkDecl(decl.name)+'">';1337 html += '<a href="'+navLinkDecl(decl.name)+'">';
src/Autodoc.zig+71-3
...@@ -86,7 +86,6 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -86,7 +86,6 @@ pub fn generateZirData(self: *Autodoc) !void {
86 break :blk .{86 break :blk .{
87 .Array = .{87 .Array = .{
88 .len = 1,88 .len = 1,
89 .name = tmpbuf.toOwnedSlice(),
90 .child = .{ .type = 0 },89 .child = .{ .type = 0 },
91 },90 },
92 };91 };
...@@ -352,7 +351,6 @@ const DocData = struct {...@@ -352,7 +351,6 @@ const DocData = struct {
352 child: TypeRef,351 child: TypeRef,
353 },352 },
354 Array: struct {353 Array: struct {
355 name: []const u8,
356 len: usize,354 len: usize,
357 child: TypeRef,355 child: TypeRef,
358 },356 },
...@@ -522,6 +520,7 @@ const DocData = struct {...@@ -522,6 +520,7 @@ const DocData = struct {
522 value: f64, // direct value520 value: f64, // direct value
523 negated: bool = false,521 negated: bool = false,
524 },522 },
523 array: Array,
525 call: usize, // index in `calls`524 call: usize, // index in `calls`
526525
527 const Struct = struct {526 const Struct = struct {
...@@ -531,6 +530,11 @@ const DocData = struct {...@@ -531,6 +530,11 @@ const DocData = struct {
531 val: WalkResult,530 val: WalkResult,
532 },531 },
533 };532 };
533 const Array = struct {
534 typeRef: TypeRef,
535 data: []WalkResult,
536 };
537
534 pub fn jsonStringify(538 pub fn jsonStringify(
535 self: WalkResult,539 self: WalkResult,
536 options: std.json.StringifyOptions,540 options: std.json.StringifyOptions,
...@@ -586,6 +590,24 @@ const DocData = struct {...@@ -586,6 +590,24 @@ const DocData = struct {
586 try w.print("{d}{s}", .{ d, comma });590 try w.print("{d}{s}", .{ d, comma });
587 }591 }
588 },592 },
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
589 }611 }
590 }612 }
591 };613 };
...@@ -615,6 +637,16 @@ fn walkInstruction(...@@ -615,6 +637,16 @@ fn walkInstruction(
615 const path = str_tok.get(file.zir);637 const path = str_tok.get(file.zir);
616 // importFile cannot error out since all files638 // importFile cannot error out since all files
617 // are already loaded at this point639 // 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 }
618 const new_file = self.module.importFile(file, path) catch unreachable;650 const new_file = self.module.importFile(file, path) catch unreachable;
619 const result = try self.files.getOrPut(self.arena, new_file.file);651 const result = try self.files.getOrPut(self.arena, new_file.file);
620 if (result.found_existing) {652 if (result.found_existing) {
...@@ -644,6 +676,28 @@ fn walkInstruction(...@@ -644,6 +676,28 @@ fn walkInstruction(
644 },676 },
645 };677 };
646 },678 },
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 },
647 .float => {701 .float => {
648 const float = data[inst_index].float;702 const float = data[inst_index].float;
649 return DocData.WalkResult{703 return DocData.WalkResult{
...@@ -1555,7 +1609,8 @@ fn tryResolveDeclPath(...@@ -1555,7 +1609,8 @@ fn tryResolveDeclPath(
1555 // with the final decl in `dp`.1609 // with the final decl in `dp`.
1556 // We then write the original value back as soon as we're done with the1610 // We then write the original value back as soon as we're done with the
1557 // recoursive call. This will work out correctly even if the path1611 // 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).
1559 path[i] = final_decl_index;1614 path[i] = final_decl_index;
1560 try self.tryResolveDeclPath(file, path);1615 try self.tryResolveDeclPath(file, path);
1561 path[i] = decl_index;1616 path[i] = decl_index;
...@@ -1870,6 +1925,19 @@ fn walkResultToTypeRef(wr: DocData.WalkResult) DocData.TypeRef {...@@ -1870,6 +1925,19 @@ fn walkResultToTypeRef(wr: DocData.WalkResult) DocData.TypeRef {
1870 };1925 };
1871}1926}
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
1873//fn collectParamInfo(self: *Autodoc, file: *File, scope: *Scope, inst_idx: Zir.Index) void {1941//fn collectParamInfo(self: *Autodoc, file: *File, scope: *Scope, inst_idx: Zir.Index) void {
18741942
1875//}1943//}