authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-01-30 19:12:56+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:10-07:00
log0135d2271600db92d1ae727b2a5935d0fc711c13
tree762b41dafac87eb5f581ab48cbc3d56347149c0b
parent38281c8ed421b8febadb2d4e05291fcf4a871ccc

autodocs: add support for indirect decl references


3 files changed, 97 insertions(+), 37 deletions(-)

lib/docs/main.js+48-26
......@@ -147,10 +147,10 @@
147147 curNav.pkgObjs.push(pkg);
148148 }
149149
150 var decl = zigAnalysis.types[pkg.main];
151 curNav.declObjs = [decl];
150 var currentType = zigAnalysis.types[pkg.main];
151 curNav.declObjs = [currentType];
152152 for (var i = 0; i < curNav.declNames.length; i += 1) {
153 var childDecl = findSubDecl(decl, curNav.declNames[i]);
153 var childDecl = findSubDecl(currentType, curNav.declNames[i]);
154154 if (childDecl == null) {
155155 return render404();
156156 }
......@@ -163,29 +163,29 @@
163163 return render404();
164164 }
165165 }
166 decl = container;
167 curNav.declObjs.push(decl);
166 currentType = container;
167 curNav.declObjs.push(currentType);
168168 }
169169
170170 renderNav();
171171
172 var lastDecl = curNav.declObjs[curNav.declObjs.length - 1];
173 if (lastDecl.pubDecls != null) {
174 renderContainer(lastDecl);
172 var lastDeclType = curNav.declObjs[curNav.declObjs.length - 1];
173 if (lastDeclType.pubDecls != null) {
174 renderContainer(lastDeclType);
175175 }
176 if (lastDecl.kind == null) {
177 return renderUnknownDecl(lastDecl);
178 } else if (lastDecl.kind === 'var') {
179 return renderVar(lastDecl);
180 } else if (lastDecl.kind === 'const' && lastDecl.type != null) {
181 var typeObj = zigAnalysis.types[lastDecl.type];
176 if (lastDeclType.kind == null) {
177 return renderUnknownDecl(lastDeclType);
178 } else if (lastDeclType.kind === 'var') {
179 return renderVar(lastDeclType);
180 } else if (lastDeclType.kind === 'const' && lastDeclType.type != null) {
181 var typeObj = zigAnalysis.types[lastDeclType.type];
182182 if (typeObj.kind === typeKinds.Fn) {
183 return renderFn(lastDecl);
183 return renderFn(lastDeclType);
184184 } else {
185 return renderValue(lastDecl);
185 return renderValue(lastDeclType);
186186 }
187187 } else {
188 renderType(lastDecl);
188 renderType(lastDeclType);
189189 }
190190 }
191191
......@@ -994,15 +994,18 @@
994994
995995 for (var i = 0; i < container.pubDecls.length; i += 1) {
996996 var decl = zigAnalysis.decls[container.pubDecls[i]];
997 var declValTypeId = getDeclValTypeId(decl);
997998
998999 if (decl.kind === 'var') {
9991000 varsList.push(decl);
10001001 continue;
10011002 } else if (decl.kind === 'const' && decl.type != null) {
10021003 if (decl.type === typeTypeId) {
1003 if (typeIsErrSet(decl.value)) {
1004 // todo: this actually makes sense for decl_vals too
1005 // the problem is, how should we get to the final type though?
1006 if (typeIsErrSet(declValTypeId)) {
10041007 errSetsList.push(decl);
1005 } else if (typeIsStructWithNoFields(decl.value)) {
1008 } else if (typeIsStructWithNoFields(declValTypeId)) {
10061009 namespacesList.push(decl);
10071010 } else {
10081011 typesList.push(decl);
......@@ -1010,7 +1013,7 @@
10101013 } else {
10111014 var typeKind = zigAnalysis.types[decl.type].kind;
10121015 if (typeKind === typeKinds.Fn) {
1013 if (allCompTimeFnCallsHaveTypeResult(decl.type, decl.value)) {
1016 if (allCompTimeFnCallsHaveTypeResult(decl.type, declValTypeId)) {
10141017 typesList.push(decl);
10151018 } else {
10161019 fnsList.push(decl);
......@@ -1111,10 +1114,20 @@
11111114 if (field.failure === true) {
11121115 html += '<span class="tok-kw" style="color:red;">#FAILURE#</span>';
11131116 } else if ("decl_ref" in field) {
1114 var name = zigAnalysis.decls[field.decl_ref].name;
1115 html += '<a href="'+navLinkDecl(name)+'">';
1116 html += '<span class="tok-kw" style="color:lightblue;">'+ name +'</span>';
1117 var decl = zigAnalysis.decls[field.decl_ref];
1118 var valType = zigAnalysis.types[getDeclValTypeId(decl)];
1119 var valTypeName = valType.name;
1120 if (valType.kind === typeKinds.Struct) {
1121 valTypeName = "struct";
1122 }
1123
1124 html += '<a href="'+navLinkDecl(decl.name)+'">';
1125 html += '<span class="tok-kw" style="color:lightblue;">' + decl.name + '</span>';
11171126 html += '</a>';
1127 html += ' ('+ valTypeName +')';
1128 } else if ("type" in field) {
1129 var name = zigAnalysis.types[field.type].name;
1130 html += '<span class="tok-kw">' + name + '</span>';
11181131 } else {
11191132 html += '<span class="tok-kw">var</span>';
11201133 }
......@@ -1291,7 +1304,7 @@
12911304
12921305 function getDeclContainerType(decl) {
12931306 if (decl.type === typeTypeId) {
1294 return zigAnalysis.types[decl.value];
1307 return zigAnalysis.types[getDeclValTypeId(decl)];
12951308 }
12961309 return null;
12971310 }
......@@ -1334,6 +1347,14 @@
13341347 return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);
13351348 }
13361349
1350 function getDeclValTypeId(decl) {
1351 while ( "decl_ref" in decl.value) {
1352 decl = zigAnalysis.decls[decl.value.decl_ref];
1353 }
1354 console.assert("type" in decl.value);
1355 return decl.value.type;
1356 }
1357
13371358 function computeCanonDeclPaths() {
13381359 var list = new Array(zigAnalysis.decls.length);
13391360 canonTypeDecls = new Array(zigAnalysis.types.length);
......@@ -1355,10 +1376,11 @@
13551376 if (list[mainDeclIndex] != null) continue;
13561377
13571378 var decl = zigAnalysis.decls[mainDeclIndex];
1379 var declValTypeId = getDeclValTypeId(decl);
13581380 if (decl.type === typeTypeId &&
1359 declCanRepresentTypeKind(zigAnalysis.types[decl.value].kind))
1381 declCanRepresentTypeKind(zigAnalysis.types[declValTypeId].kind))
13601382 {
1361 canonTypeDecls[decl.value] = mainDeclIndex;
1383 canonTypeDecls[declValTypeId] = mainDeclIndex;
13621384 }
13631385 var declNames = item.declNames.concat([decl.name]);
13641386 list[mainDeclIndex] = {
src/Autodoc.zig+48-11
......@@ -21,6 +21,10 @@ pub fn init(m: *Module, doc_location: ?Compilation.EmitLoc) Autodoc {
2121 };
2222}
2323
24pub fn deinit(_: *Autodoc) void {
25 arena_allocator.deinit();
26}
27
2428pub fn generateZirData(self: *Autodoc) !void {
2529 if (self.doc_location) |loc| {
2630 if (loc.directory) |dir| {
......@@ -48,13 +52,51 @@ pub fn generateZirData(self: *Autodoc) !void {
4852 {
4953
5054 // TODO: we don't want to add .none, but the index math has to check out
51 var i: u32 = 1;
55 var i: u32 = 0;
5256 while (i <= @enumToInt(Zir.Inst.Ref.anyerror_void_error_union_type)) : (i += 1) {
5357 var tmpbuf = std.ArrayList(u8).init(self.arena);
5458 try Zir.Inst.Ref.typed_value_map[i].val.format("", .{}, tmpbuf.writer());
5559 try self.types.append(self.arena, .{
56 .kind = 0,
5760 .name = tmpbuf.toOwnedSlice(),
61 .kind = switch (@intToEnum(Zir.Inst.Ref, i)) {
62 else => |t| blk: {
63 std.debug.print("TODO: categorize `{s}` in typeKinds\n", .{
64 @tagName(t),
65 });
66 break :blk 7;
67 },
68 .u1_type,
69 .u8_type,
70 .i8_type,
71 .u16_type,
72 .i16_type,
73 .u32_type,
74 .i32_type,
75 .u64_type,
76 .i64_type,
77 .u128_type,
78 .i128_type,
79 .usize_type,
80 .isize_type,
81 .c_short_type,
82 .c_ushort_type,
83 .c_int_type,
84 .c_uint_type,
85 .c_long_type,
86 .c_ulong_type,
87 .c_longlong_type,
88 .c_ulonglong_type,
89 .c_longdouble_type,
90 => @enumToInt(std.builtin.TypeId.Int),
91 .f16_type,
92 .f32_type,
93 .f64_type,
94 .f128_type,
95 => @enumToInt(std.builtin.TypeId.Float),
96 .bool_type => @enumToInt(std.builtin.TypeId.Bool),
97 .void_type => @enumToInt(std.builtin.TypeId.Void),
98 .type_type => @enumToInt(std.builtin.TypeId.Type),
99 },
58100 });
59101 }
60102 }
......@@ -167,7 +209,7 @@ const DocData = struct {
167209 kind: []const u8, // TODO: where do we find this info?
168210 src: usize, // index into astNodes
169211 type: usize, // index into types
170 value: usize,
212 value: WalkResult,
171213 };
172214
173215 const AstNode = struct {
......@@ -476,7 +518,6 @@ fn walkDecls(
476518 };
477519
478520 const walk_result = try self.walkInstruction(zir, scope, decl_index);
479 const type_index = walk_result.type;
480521
481522 if (is_pub) {
482523 try decl_indexes.append(self.arena, decls_slot_index);
......@@ -487,8 +528,8 @@ fn walkDecls(
487528 self.decls.items[decls_slot_index] = .{
488529 .name = name,
489530 .src = ast_node_index,
490 .type = 0,
491 .value = type_index,
531 .type = @enumToInt(Zir.Inst.Ref.type_type),
532 .value = walk_result,
492533 .kind = "const", // find where this information can be found
493534 };
494535 }
......@@ -561,13 +602,9 @@ fn collectFieldInfo(
561602 else => {
562603 const enum_value = @enumToInt(field_type);
563604 if (enum_value < Zir.Inst.Ref.typed_value_map.len) {
564 std.debug.print(
565 "TODO: handle ref type: {s}",
566 .{@tagName(field_type)},
567 );
568605 try field_type_indexes.append(
569606 self.arena,
570 DocData.WalkResult{ .failure = true },
607 DocData.WalkResult{ .type = enum_value },
571608 );
572609 } else {
573610 const zir_index = enum_value - Zir.Inst.Ref.typed_value_map.len;
src/Compilation.zig+1
......@@ -2870,6 +2870,7 @@ pub fn performAllTheWork(
28702870 if (comp.emit_docs) |doc_location| {
28712871 if (comp.bin_file.options.module) |module| {
28722872 var autodoc = Autodoc.init(module, doc_location);
2873 defer autodoc.deinit();
28732874 try autodoc.generateZirData();
28742875 }
28752876 }