authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-02-04 19:28:12+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:10-07:00
loga04045c709b6b9a8df830e9b270505663164a89e
treec87fb40b9f34655e020206a0bb7e8b2373b959c0
parentce40f34cbca3b013e5db766487833a1e5ced6658

autodocs: fix rendering of non-type decls


2 files changed, 185 insertions(+), 86 deletions(-)

lib/docs/main.js+159-84
......@@ -1,3 +1,5 @@
1//'use strict';
2
13(function() {
24 var domStatus = document.getElementById("status");
35 var domSectNav = document.getElementById("sectNav");
......@@ -101,6 +103,98 @@
101103 }
102104 }
103105
106 function isDecl(x) {
107 return "value" in x;
108 }
109
110 function isType(x) {
111 return "kind" in x && !("value" in x);
112 }
113
114 function isContainerType(x) {
115 return isType(x) && typeKindIsContainer(x.kind) ;
116 }
117
118 function declContainsType(x){
119 console.assert("value" in x);
120
121 }
122
123 function typeKindIsContainer(typeKind) {
124 return typeKind === typeKinds.Struct ||
125 typeKind === typeKinds.Union ||
126 typeKind === typeKinds.Enum;
127 }
128
129 function declCanRepresentTypeKind(typeKind) {
130 return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);
131 }
132
133 function resolveDeclValue(decl) {
134 var i = 0;
135 while(i < 1000) {
136 i += 1;
137
138 if ("declRef" in decl.value) {
139 decl = zigAnalysis.decls[decl.value.declRef];
140 continue;
141 }
142
143 return decl.value;
144
145 }
146 console.assert(false);
147 }
148
149 function resolveDeclValueTypeId(decl){
150 var i = 0;
151 while(i < 1000) {
152 i += 1;
153 console.assert(isDecl(decl));
154 if ("type" in decl.value) {
155 return typeTypeId;
156 }
157
158 if ("declRef" in decl.value) {
159 decl = zigAnalysis.decls[decl.value.declRef];
160 continue;
161 }
162
163 if ("int" in decl.value) {
164 return resolveTypeRefToTypeId(decl.value.int.typeRef);
165 }
166
167 if ("float" in decl.value) {
168 return resolveTypeRefToTypeId(decl.value.float.typeRef);
169 }
170
171 if ("struct" in decl.value) {
172 return resolveTypeRefToTypeId(decl.value.struct.typeRef);
173 }
174
175 console.log("TODO: handle in `resolveDeclValueTypeId` more cases: ", decl);
176 console.assert(false);
177 }
178 console.assert(false);
179 }
180
181 function resolveTypeRefToTypeId(ref) {
182 if ("unspecified" in ref) {
183 console.log("found an unspecified type!")
184 return -1;
185 }
186
187 if ("declRef" in ref) {
188 return resolveDeclValueTypeId(ref.declRef);
189 }
190
191 if ("type" in ref) {
192 return ref.type;
193 }
194
195 console.assert(false);
196 }
197
104198 function render() {
105199 domStatus.classList.add("hidden");
106200 domFnProto.classList.add("hidden");
......@@ -154,38 +248,43 @@
154248 if (childDecl == null) {
155249 return render404();
156250 }
157 var container = getDeclContainerType(childDecl);
158 if (container == null) {
251
252 var childDeclValue = resolveDeclValue(childDecl);
253 if ("type" in childDeclValue){
159254 if (i + 1 === curNav.declNames.length) {
160 curNav.declObjs.push(childDecl);
255 curNav.declObjs.push(zigAnalysis.types[childDeclValue.type]);
161256 break;
162257 } else {
163258 return render404();
164259 }
165260 }
166 currentType = container;
261 currentType = childDecl;
167262 curNav.declObjs.push(currentType);
168263 }
169264
170265 renderNav();
171266
172 var lastDeclOrType = curNav.declObjs[curNav.declObjs.length - 1];
173 if (lastDeclOrType.pubDecls != null) {
174 renderContainer(lastDeclOrType);
267 var last = curNav.declObjs[curNav.declObjs.length - 1];
268 var lastIsDecl = isDecl(last);
269 var lastIsType = isType(last);
270 var lastIsContainerType = isContainerType(last);
271
272 if (lastIsContainerType) {
273 renderContainer(last);
175274 }
176 if (lastDeclOrType.kind == null) {
177 return renderUnknownDecl(lastDeclOrType);
178 } else if (lastDeclOrType.kind === 'var') {
179 return renderVar(lastDeclOrType);
180 } else if (lastDeclOrType.kind === 'const' && "value" in lastDeclOrType && !("type" in lastDeclOrType.value)) {
181 var typeObj = zigAnalysis.types[getDeclValTypeId(lastDeclOrType)];
275 if (!lastIsDecl && !lastIsType) {
276 return renderUnknownDecl(last);
277 } else if (lastIsDecl && last.kind === 'var') {
278 return renderVar(last);
279 } else if (lastIsDecl && last.kind === 'const' && !(declContainsType(last))) {
280 var typeObj = zigAnalysis.types[resolveDeclValueTypeId(last)];
182281 if (typeObj.kind === typeKinds.Fn) {
183 return renderFn(lastDeclOrType);
282 return renderFn(last);
184283 } else {
185 return renderValue(lastDeclOrType);
284 return renderValue(last);
186285 }
187286 } else {
188 renderType(lastDeclOrType);
287 renderType(last);
189288 }
190289 }
191290
......@@ -210,7 +309,7 @@
210309 var typeObj = zigAnalysis.types[typeIndex];
211310 if (typeObj.kind !== typeKinds.Struct)
212311 return false;
213 return typeObj.fields == null || typeObj.fields.length === 0;
312 return !typeObj.fields;
214313 }
215314
216315 function typeIsGenericFn(typeIndex) {
......@@ -642,14 +741,16 @@
642741 return "f" + typeObj.bits;
643742 }
644743 case typeKinds.Int:
645 return '<span class="tok-type">' + typeObj.name + '</span>';
646 // var signed = (typeObj.i != null) ? 'i' : 'u';
647 // var bits = typeObj[signed];
648 // if (wantHtml) {
649 // return '<span class="tok-type">' + signed + bits + '</span>';
650 // } else {
651 // return signed + bits;
652 // }
744 var signed = (typeObj.i != null) ? 'i' : 'u';
745 var bits = typeObj[signed] || typeObj.name;
746
747 var name = typeObj.name ? typeObj.name : signed + bits;
748
749 if (wantHtml) {
750 return '<span class="tok-type">' + name + '</span>';
751 } else {
752 return name;
753 }
653754 case typeKinds.ComptimeInt:
654755 if (wantHtml) {
655756 return '<span class="tok-type">comptime_int</span>';
......@@ -960,12 +1061,15 @@
9601061
9611062 function renderValue(decl) {
9621063
963 var declTypeId = getDeclValTypeId(decl);
1064 var declTypeId = resolveDeclValueTypeId(decl);
9641065 var declValueText = "";
9651066 switch(Object.keys(decl.value)[0]) {
9661067 case "int":
9671068 declValueText += decl.value.int.value;
9681069 break;
1070 case "float":
1071 declValueText += decl.value.float.value;
1072 break;
9691073 default:
9701074 console.log("TODO: renderValue for ", Object.keys(decl.value)[0]);
9711075 declValueText += "#TODO#";
......@@ -985,7 +1089,7 @@
9851089 }
9861090
9871091 function renderVar(decl) {
988 var declTypeId = getDeclValTypeId(decl);
1092 var declTypeId = resolveDeclValueTypeId(decl);
9891093 domFnProtoCode.innerHTML = '<span class="tok-kw">var</span> ' +
9901094 escapeHtml(decl.name) + ': ' + typeIndexName(declTypeId, true, true);
9911095
......@@ -1006,27 +1110,28 @@
10061110 var varsList = [];
10071111 var valsList = [];
10081112
1009 for (var i = 0; i < container.pubDecls.length; i += 1) {
1113 var declLen = container.pubDecls ? container.pubDecls.length : 0;
1114 for (var i = 0; i < declLen; i += 1) {
10101115 var decl = zigAnalysis.decls[container.pubDecls[i]];
1011 var declValTypeId = getDeclValTypeId(decl);
1116 var declTypeId = resolveDeclValueTypeId(decl);
10121117
10131118 if (decl.kind === 'var') {
10141119 varsList.push(decl);
10151120 continue;
1016 } else if (decl.kind === 'const' && "value" in decl) {
1017 if (declValTypeId === typeTypeId) {
1018 if (typeIsErrSet(declValTypeId)) {
1121 } else if (decl.kind === 'const') {
1122 if (declTypeId === typeTypeId) {
1123 if (typeIsErrSet(declTypeId)) {
10191124 errSetsList.push(decl);
1020 } else if (typeIsStructWithNoFields(declValTypeId)) {
1125 } else if (typeIsStructWithNoFields(declTypeId)) {
10211126 namespacesList.push(decl);
10221127 } else {
10231128 typesList.push(decl);
10241129 }
10251130 } else {
1026 var typeKind = zigAnalysis.types[declValTypeId].kind;
1131 var typeKind = zigAnalysis.types[declTypeId].kind;
10271132 if (typeKind === typeKinds.Fn) {
10281133 // TODO: this is broken but I don't understand functions yet
1029 if (allCompTimeFnCallsHaveTypeResult(decl.type, declValTypeId)) {
1134 if (allCompTimeFnCallsHaveTypeResult(decl.type, declTypeId)) {
10301135 typesList.push(decl);
10311136 } else {
10321137 fnsList.push(decl);
......@@ -1108,7 +1213,7 @@
11081213 domSectFns.classList.remove("hidden");
11091214 }
11101215
1111 if (container.fields != null && container.fields.length !== 0) {
1216 if (container.fields) {
11121217 resizeDomList(domListFields, container.fields.length, '<div></div>');
11131218
11141219 var containerNode = zigAnalysis.astNodes[container.src];
......@@ -1128,7 +1233,10 @@
11281233 html += '<span class="tok-kw" style="color:red;">#FAILURE#</span>';
11291234 } else if ("declRef" in field) {
11301235 var decl = zigAnalysis.decls[field.declRef];
1131 var valType = zigAnalysis.types[getDeclValTypeId(decl)];
1236 var val = resolveDeclValue(decl);
1237 console.assert("type" in val);
1238 var valType = zigAnalysis.types[val.type];
1239
11321240 var valTypeName = valType.name;
11331241 if (valType.kind === typeKinds.Struct) {
11341242 valTypeName = "struct";
......@@ -1175,7 +1283,7 @@
11751283 tdNameA.setAttribute('href', navLinkDecl(decl.name));
11761284 tdNameA.textContent = decl.name;
11771285
1178 tdType.innerHTML = typeIndexName(getDeclValTypeId(decl), true, true);
1286 tdType.innerHTML = typeIndexName(resolveDeclValueTypeId(decl), true, true);
11791287
11801288 var docs = zigAnalysis.astNodes[decl.src].docs;
11811289 if (docs != null) {
......@@ -1202,7 +1310,7 @@
12021310 tdNameA.setAttribute('href', navLinkDecl(decl.name));
12031311 tdNameA.textContent = decl.name;
12041312
1205 tdType.innerHTML = typeIndexName(getDeclValTypeId(decl), true, true);
1313 tdType.innerHTML = typeIndexName(resolveDeclValueTypeId(decl), true, true);
12061314
12071315 var docs = zigAnalysis.astNodes[decl.src].docs;
12081316 if (docs != null) {
......@@ -1304,7 +1412,7 @@
13041412 }
13051413
13061414 function findSubDecl(parentType, childName) {
1307 if (parentType.pubDecls == null) throw new Error("parent object has no public decls");
1415 if (!parentType.pubDecls) throw new Error("parent object has no public decls");
13081416 for (var i = 0; i < parentType.pubDecls.length; i += 1) {
13091417 var declIndex = parentType.pubDecls[i];
13101418 var childDecl = zigAnalysis.decls[declIndex];
......@@ -1315,12 +1423,8 @@
13151423 return null;
13161424 }
13171425
1318 function getDeclContainerType(decl) {
1319 if (decl.type === typeTypeId) {
1320 return zigAnalysis.types[getDeclValTypeId(decl)];
1321 }
1322 return null;
1323 }
1426
1427
13241428
13251429 function computeCanonicalPackagePaths() {
13261430 var list = new Array(zigAnalysis.packages.length);
......@@ -1350,37 +1454,6 @@
13501454 return list;
13511455 }
13521456
1353 function typeKindIsContainer(typeKind) {
1354 return typeKind === typeKinds.Struct ||
1355 typeKind === typeKinds.Union ||
1356 typeKind === typeKinds.Enum;
1357 }
1358
1359 function declCanRepresentTypeKind(typeKind) {
1360 return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);
1361 }
1362
1363 // Handles both WalkResult and TypeRef
1364 function getDeclValTypeId(decl) {
1365 var val = decl.value;
1366 while (true) {
1367 if ( "declRef" in val) {
1368 val = zigAnalysis.decls[val.declRef].value;
1369 continue;
1370 }
1371
1372 if ("int" in val) {
1373 val = val.int.typeRef;
1374 }
1375
1376 if ("type" in val) {
1377 return val.type;
1378 }
1379
1380 console.assert("type" in val);
1381 }
1382 return val.type;
1383 }
13841457
13851458 function computeCanonDeclPaths() {
13861459 var list = new Array(zigAnalysis.decls.length);
......@@ -1397,14 +1470,15 @@
13971470 while (stack.length !== 0) {
13981471 var item = stack.shift();
13991472
1400 if (item.type.pubDecls != null) {
1401 for (var declI = 0; declI < item.type.pubDecls.length; declI += 1) {
1473 if (isContainerType(item.type)) {
1474 var len = item.type.pubDecls ? item.type.pubDecls.length : 0;
1475 for (var declI = 0; declI < len; declI += 1) {
14021476 var mainDeclIndex = item.type.pubDecls[declI];
14031477 if (list[mainDeclIndex] != null) continue;
14041478
14051479 var decl = zigAnalysis.decls[mainDeclIndex];
1406 var declValTypeId = getDeclValTypeId(decl);
1407 if (decl.type === typeTypeId &&
1480 var declValTypeId = resolveDeclValueTypeId(decl);
1481 if (declValTypeId === typeTypeId &&
14081482 declCanRepresentTypeKind(zigAnalysis.types[declValTypeId].kind))
14091483 {
14101484 canonTypeDecls[declValTypeId] = mainDeclIndex;
......@@ -1414,11 +1488,12 @@
14141488 pkgNames: pkgNames,
14151489 declNames: declNames,
14161490 };
1417 var containerType = getDeclContainerType(decl);
1418 if (containerType != null) {
1491
1492 var declType = zigAnalysis.types[declValTypeId];
1493 if (isContainerType(declType)) {
14191494 stack.push({
14201495 declNames: declNames,
1421 type: containerType,
1496 type: declType,
14221497 });
14231498 }
14241499 }
src/Autodoc.zig+26-2
......@@ -88,13 +88,14 @@ pub fn generateZirData(self: *Autodoc) !void {
8888 .c_longlong_type,
8989 .c_ulonglong_type,
9090 .c_longdouble_type,
91 .comptime_int_type,
9291 => @enumToInt(std.builtin.TypeId.Int),
9392 .f16_type,
9493 .f32_type,
9594 .f64_type,
9695 .f128_type,
9796 => @enumToInt(std.builtin.TypeId.Float),
97 .comptime_int_type => @enumToInt(std.builtin.TypeId.ComptimeInt),
98 .comptime_float_type => @enumToInt(std.builtin.TypeId.ComptimeFloat),
9899 .bool_type => @enumToInt(std.builtin.TypeId.Bool),
99100 .void_type => @enumToInt(std.builtin.TypeId.Void),
100101 .type_type => @enumToInt(std.builtin.TypeId.Type),
......@@ -275,7 +276,11 @@ const DocData = struct {
275276 value: usize, // direct value
276277 negated: bool = false,
277278 },
278
279 float: struct {
280 typeRef: TypeRef,
281 value: f64, // direct value
282 negated: bool = false,
283 },
279284 pub fn jsonStringify(
280285 self: WalkResult,
281286 options: std.json.StringifyOptions,
......@@ -304,6 +309,16 @@ const DocData = struct {
304309 \\, "value": {s}{} }} }}
305310 , .{ neg, v.value });
306311 },
312 .float => |v| {
313 const neg = if (v.negated) "-" else "";
314 try w.print(
315 \\{{ "float": {{ "typeRef":
316 , .{});
317 try v.typeRef.jsonStringify(options, w);
318 try w.print(
319 \\, "value": {s}{} }} }}
320 , .{ neg, v.value });
321 },
307322 .bool => |v| {
308323 try w.print(
309324 \\{{ "bool":{} }}
......@@ -350,6 +365,15 @@ fn walkInstruction(
350365 },
351366 };
352367 },
368 .float => {
369 const float = data[inst_index].float;
370 return DocData.WalkResult{
371 .float = .{
372 .typeRef = .{ .type = @enumToInt(Ref.comptime_float_type) },
373 .value = float,
374 },
375 };
376 },
353377 .negate => {
354378 const un_node = data[inst_index].un_node;
355379 var operand = try self.walkRef(zir, parent_scope, un_node.operand);