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,3 +1,5 @@
1//'use strict';
2
1(function() {3(function() {
2 var domStatus = document.getElementById("status");4 var domStatus = document.getElementById("status");
3 var domSectNav = document.getElementById("sectNav");5 var domSectNav = document.getElementById("sectNav");
...@@ -101,6 +103,98 @@...@@ -101,6 +103,98 @@
101 }103 }
102 }104 }
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
104 function render() {198 function render() {
105 domStatus.classList.add("hidden");199 domStatus.classList.add("hidden");
106 domFnProto.classList.add("hidden");200 domFnProto.classList.add("hidden");
...@@ -154,38 +248,43 @@...@@ -154,38 +248,43 @@
154 if (childDecl == null) {248 if (childDecl == null) {
155 return render404();249 return render404();
156 }250 }
157 var container = getDeclContainerType(childDecl);251
158 if (container == null) {252 var childDeclValue = resolveDeclValue(childDecl);
253 if ("type" in childDeclValue){
159 if (i + 1 === curNav.declNames.length) {254 if (i + 1 === curNav.declNames.length) {
160 curNav.declObjs.push(childDecl);255 curNav.declObjs.push(zigAnalysis.types[childDeclValue.type]);
161 break;256 break;
162 } else {257 } else {
163 return render404();258 return render404();
164 }259 }
165 }260 }
166 currentType = container;261 currentType = childDecl;
167 curNav.declObjs.push(currentType);262 curNav.declObjs.push(currentType);
168 }263 }
169264
170 renderNav();265 renderNav();
171266
172 var lastDeclOrType = curNav.declObjs[curNav.declObjs.length - 1];267 var last = curNav.declObjs[curNav.declObjs.length - 1];
173 if (lastDeclOrType.pubDecls != null) {268 var lastIsDecl = isDecl(last);
174 renderContainer(lastDeclOrType);269 var lastIsType = isType(last);
270 var lastIsContainerType = isContainerType(last);
271
272 if (lastIsContainerType) {
273 renderContainer(last);
175 }274 }
176 if (lastDeclOrType.kind == null) {275 if (!lastIsDecl && !lastIsType) {
177 return renderUnknownDecl(lastDeclOrType);276 return renderUnknownDecl(last);
178 } else if (lastDeclOrType.kind === 'var') {277 } else if (lastIsDecl && last.kind === 'var') {
179 return renderVar(lastDeclOrType);278 return renderVar(last);
180 } else if (lastDeclOrType.kind === 'const' && "value" in lastDeclOrType && !("type" in lastDeclOrType.value)) {279 } else if (lastIsDecl && last.kind === 'const' && !(declContainsType(last))) {
181 var typeObj = zigAnalysis.types[getDeclValTypeId(lastDeclOrType)];280 var typeObj = zigAnalysis.types[resolveDeclValueTypeId(last)];
182 if (typeObj.kind === typeKinds.Fn) {281 if (typeObj.kind === typeKinds.Fn) {
183 return renderFn(lastDeclOrType);282 return renderFn(last);
184 } else {283 } else {
185 return renderValue(lastDeclOrType);284 return renderValue(last);
186 }285 }
187 } else {286 } else {
188 renderType(lastDeclOrType);287 renderType(last);
189 }288 }
190 }289 }
191290
...@@ -210,7 +309,7 @@...@@ -210,7 +309,7 @@
210 var typeObj = zigAnalysis.types[typeIndex];309 var typeObj = zigAnalysis.types[typeIndex];
211 if (typeObj.kind !== typeKinds.Struct)310 if (typeObj.kind !== typeKinds.Struct)
212 return false;311 return false;
213 return typeObj.fields == null || typeObj.fields.length === 0;312 return !typeObj.fields;
214 }313 }
215314
216 function typeIsGenericFn(typeIndex) {315 function typeIsGenericFn(typeIndex) {
...@@ -642,14 +741,16 @@...@@ -642,14 +741,16 @@
642 return "f" + typeObj.bits;741 return "f" + typeObj.bits;
643 }742 }
644 case typeKinds.Int:743 case typeKinds.Int:
645 return '<span class="tok-type">' + typeObj.name + '</span>';744 var signed = (typeObj.i != null) ? 'i' : 'u';
646 // var signed = (typeObj.i != null) ? 'i' : 'u';745 var bits = typeObj[signed] || typeObj.name;
647 // var bits = typeObj[signed];746
648 // if (wantHtml) {747 var name = typeObj.name ? typeObj.name : signed + bits;
649 // return '<span class="tok-type">' + signed + bits + '</span>';748
650 // } else {749 if (wantHtml) {
651 // return signed + bits;750 return '<span class="tok-type">' + name + '</span>';
652 // }751 } else {
752 return name;
753 }
653 case typeKinds.ComptimeInt:754 case typeKinds.ComptimeInt:
654 if (wantHtml) {755 if (wantHtml) {
655 return '<span class="tok-type">comptime_int</span>';756 return '<span class="tok-type">comptime_int</span>';
...@@ -960,12 +1061,15 @@...@@ -960,12 +1061,15 @@
9601061
961 function renderValue(decl) {1062 function renderValue(decl) {
9621063
963 var declTypeId = getDeclValTypeId(decl);1064 var declTypeId = resolveDeclValueTypeId(decl);
964 var declValueText = "";1065 var declValueText = "";
965 switch(Object.keys(decl.value)[0]) {1066 switch(Object.keys(decl.value)[0]) {
966 case "int":1067 case "int":
967 declValueText += decl.value.int.value;1068 declValueText += decl.value.int.value;
968 break;1069 break;
1070 case "float":
1071 declValueText += decl.value.float.value;
1072 break;
969 default: 1073 default:
970 console.log("TODO: renderValue for ", Object.keys(decl.value)[0]);1074 console.log("TODO: renderValue for ", Object.keys(decl.value)[0]);
971 declValueText += "#TODO#";1075 declValueText += "#TODO#";
...@@ -985,7 +1089,7 @@...@@ -985,7 +1089,7 @@
985 }1089 }
9861090
987 function renderVar(decl) {1091 function renderVar(decl) {
988 var declTypeId = getDeclValTypeId(decl);1092 var declTypeId = resolveDeclValueTypeId(decl);
989 domFnProtoCode.innerHTML = '<span class="tok-kw">var</span> ' +1093 domFnProtoCode.innerHTML = '<span class="tok-kw">var</span> ' +
990 escapeHtml(decl.name) + ': ' + typeIndexName(declTypeId, true, true);1094 escapeHtml(decl.name) + ': ' + typeIndexName(declTypeId, true, true);
9911095
...@@ -1006,27 +1110,28 @@...@@ -1006,27 +1110,28 @@
1006 var varsList = [];1110 var varsList = [];
1007 var valsList = [];1111 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) {
1010 var decl = zigAnalysis.decls[container.pubDecls[i]];1115 var decl = zigAnalysis.decls[container.pubDecls[i]];
1011 var declValTypeId = getDeclValTypeId(decl);1116 var declTypeId = resolveDeclValueTypeId(decl);
10121117
1013 if (decl.kind === 'var') {1118 if (decl.kind === 'var') {
1014 varsList.push(decl);1119 varsList.push(decl);
1015 continue;1120 continue;
1016 } else if (decl.kind === 'const' && "value" in decl) {1121 } else if (decl.kind === 'const') {
1017 if (declValTypeId === typeTypeId) {1122 if (declTypeId === typeTypeId) {
1018 if (typeIsErrSet(declValTypeId)) {1123 if (typeIsErrSet(declTypeId)) {
1019 errSetsList.push(decl);1124 errSetsList.push(decl);
1020 } else if (typeIsStructWithNoFields(declValTypeId)) {1125 } else if (typeIsStructWithNoFields(declTypeId)) {
1021 namespacesList.push(decl);1126 namespacesList.push(decl);
1022 } else {1127 } else {
1023 typesList.push(decl);1128 typesList.push(decl);
1024 }1129 }
1025 } else {1130 } else {
1026 var typeKind = zigAnalysis.types[declValTypeId].kind;1131 var typeKind = zigAnalysis.types[declTypeId].kind;
1027 if (typeKind === typeKinds.Fn) {1132 if (typeKind === typeKinds.Fn) {
1028 // TODO: this is broken but I don't understand functions yet1133 // TODO: this is broken but I don't understand functions yet
1029 if (allCompTimeFnCallsHaveTypeResult(decl.type, declValTypeId)) {1134 if (allCompTimeFnCallsHaveTypeResult(decl.type, declTypeId)) {
1030 typesList.push(decl);1135 typesList.push(decl);
1031 } else {1136 } else {
1032 fnsList.push(decl);1137 fnsList.push(decl);
...@@ -1108,7 +1213,7 @@...@@ -1108,7 +1213,7 @@
1108 domSectFns.classList.remove("hidden");1213 domSectFns.classList.remove("hidden");
1109 }1214 }
11101215
1111 if (container.fields != null && container.fields.length !== 0) {1216 if (container.fields) {
1112 resizeDomList(domListFields, container.fields.length, '<div></div>');1217 resizeDomList(domListFields, container.fields.length, '<div></div>');
11131218
1114 var containerNode = zigAnalysis.astNodes[container.src];1219 var containerNode = zigAnalysis.astNodes[container.src];
...@@ -1128,7 +1233,10 @@...@@ -1128,7 +1233,10 @@
1128 html += '<span class="tok-kw" style="color:red;">#FAILURE#</span>';1233 html += '<span class="tok-kw" style="color:red;">#FAILURE#</span>';
1129 } else if ("declRef" in field) {1234 } else if ("declRef" in field) {
1130 var decl = zigAnalysis.decls[field.declRef];1235 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
1132 var valTypeName = valType.name;1240 var valTypeName = valType.name;
1133 if (valType.kind === typeKinds.Struct) {1241 if (valType.kind === typeKinds.Struct) {
1134 valTypeName = "struct"; 1242 valTypeName = "struct";
...@@ -1175,7 +1283,7 @@...@@ -1175,7 +1283,7 @@
1175 tdNameA.setAttribute('href', navLinkDecl(decl.name));1283 tdNameA.setAttribute('href', navLinkDecl(decl.name));
1176 tdNameA.textContent = decl.name;1284 tdNameA.textContent = decl.name;
11771285
1178 tdType.innerHTML = typeIndexName(getDeclValTypeId(decl), true, true);1286 tdType.innerHTML = typeIndexName(resolveDeclValueTypeId(decl), true, true);
11791287
1180 var docs = zigAnalysis.astNodes[decl.src].docs;1288 var docs = zigAnalysis.astNodes[decl.src].docs;
1181 if (docs != null) {1289 if (docs != null) {
...@@ -1202,7 +1310,7 @@...@@ -1202,7 +1310,7 @@
1202 tdNameA.setAttribute('href', navLinkDecl(decl.name));1310 tdNameA.setAttribute('href', navLinkDecl(decl.name));
1203 tdNameA.textContent = decl.name;1311 tdNameA.textContent = decl.name;
12041312
1205 tdType.innerHTML = typeIndexName(getDeclValTypeId(decl), true, true);1313 tdType.innerHTML = typeIndexName(resolveDeclValueTypeId(decl), true, true);
12061314
1207 var docs = zigAnalysis.astNodes[decl.src].docs;1315 var docs = zigAnalysis.astNodes[decl.src].docs;
1208 if (docs != null) {1316 if (docs != null) {
...@@ -1304,7 +1412,7 @@...@@ -1304,7 +1412,7 @@
1304 }1412 }
13051413
1306 function findSubDecl(parentType, childName) {1414 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");
1308 for (var i = 0; i < parentType.pubDecls.length; i += 1) {1416 for (var i = 0; i < parentType.pubDecls.length; i += 1) {
1309 var declIndex = parentType.pubDecls[i];1417 var declIndex = parentType.pubDecls[i];
1310 var childDecl = zigAnalysis.decls[declIndex];1418 var childDecl = zigAnalysis.decls[declIndex];
...@@ -1315,12 +1423,8 @@...@@ -1315,12 +1423,8 @@
1315 return null;1423 return null;
1316 }1424 }
13171425
1318 function getDeclContainerType(decl) {1426
1319 if (decl.type === typeTypeId) {1427
1320 return zigAnalysis.types[getDeclValTypeId(decl)];
1321 }
1322 return null;
1323 }
13241428
1325 function computeCanonicalPackagePaths() {1429 function computeCanonicalPackagePaths() {
1326 var list = new Array(zigAnalysis.packages.length);1430 var list = new Array(zigAnalysis.packages.length);
...@@ -1350,37 +1454,6 @@...@@ -1350,37 +1454,6 @@
1350 return list;1454 return list;
1351 }1455 }
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
1385 function computeCanonDeclPaths() {1458 function computeCanonDeclPaths() {
1386 var list = new Array(zigAnalysis.decls.length);1459 var list = new Array(zigAnalysis.decls.length);
...@@ -1397,14 +1470,15 @@...@@ -1397,14 +1470,15 @@
1397 while (stack.length !== 0) {1470 while (stack.length !== 0) {
1398 var item = stack.shift();1471 var item = stack.shift();
13991472
1400 if (item.type.pubDecls != null) {1473 if (isContainerType(item.type)) {
1401 for (var declI = 0; declI < item.type.pubDecls.length; declI += 1) {1474 var len = item.type.pubDecls ? item.type.pubDecls.length : 0;
1475 for (var declI = 0; declI < len; declI += 1) {
1402 var mainDeclIndex = item.type.pubDecls[declI];1476 var mainDeclIndex = item.type.pubDecls[declI];
1403 if (list[mainDeclIndex] != null) continue;1477 if (list[mainDeclIndex] != null) continue;
14041478
1405 var decl = zigAnalysis.decls[mainDeclIndex];1479 var decl = zigAnalysis.decls[mainDeclIndex];
1406 var declValTypeId = getDeclValTypeId(decl);1480 var declValTypeId = resolveDeclValueTypeId(decl);
1407 if (decl.type === typeTypeId &&1481 if (declValTypeId === typeTypeId &&
1408 declCanRepresentTypeKind(zigAnalysis.types[declValTypeId].kind))1482 declCanRepresentTypeKind(zigAnalysis.types[declValTypeId].kind))
1409 {1483 {
1410 canonTypeDecls[declValTypeId] = mainDeclIndex;1484 canonTypeDecls[declValTypeId] = mainDeclIndex;
...@@ -1414,11 +1488,12 @@...@@ -1414,11 +1488,12 @@
1414 pkgNames: pkgNames,1488 pkgNames: pkgNames,
1415 declNames: declNames,1489 declNames: declNames,
1416 };1490 };
1417 var containerType = getDeclContainerType(decl);1491
1418 if (containerType != null) {1492 var declType = zigAnalysis.types[declValTypeId];
1493 if (isContainerType(declType)) {
1419 stack.push({1494 stack.push({
1420 declNames: declNames,1495 declNames: declNames,
1421 type: containerType,1496 type: declType,
1422 });1497 });
1423 }1498 }
1424 }1499 }
src/Autodoc.zig+26-2
...@@ -88,13 +88,14 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -88,13 +88,14 @@ pub fn generateZirData(self: *Autodoc) !void {
88 .c_longlong_type,88 .c_longlong_type,
89 .c_ulonglong_type,89 .c_ulonglong_type,
90 .c_longdouble_type,90 .c_longdouble_type,
91 .comptime_int_type,
92 => @enumToInt(std.builtin.TypeId.Int),91 => @enumToInt(std.builtin.TypeId.Int),
93 .f16_type,92 .f16_type,
94 .f32_type,93 .f32_type,
95 .f64_type,94 .f64_type,
96 .f128_type,95 .f128_type,
97 => @enumToInt(std.builtin.TypeId.Float),96 => @enumToInt(std.builtin.TypeId.Float),
97 .comptime_int_type => @enumToInt(std.builtin.TypeId.ComptimeInt),
98 .comptime_float_type => @enumToInt(std.builtin.TypeId.ComptimeFloat),
98 .bool_type => @enumToInt(std.builtin.TypeId.Bool),99 .bool_type => @enumToInt(std.builtin.TypeId.Bool),
99 .void_type => @enumToInt(std.builtin.TypeId.Void),100 .void_type => @enumToInt(std.builtin.TypeId.Void),
100 .type_type => @enumToInt(std.builtin.TypeId.Type),101 .type_type => @enumToInt(std.builtin.TypeId.Type),
...@@ -275,7 +276,11 @@ const DocData = struct {...@@ -275,7 +276,11 @@ const DocData = struct {
275 value: usize, // direct value276 value: usize, // direct value
276 negated: bool = false,277 negated: bool = false,
277 },278 },
278279 float: struct {
280 typeRef: TypeRef,
281 value: f64, // direct value
282 negated: bool = false,
283 },
279 pub fn jsonStringify(284 pub fn jsonStringify(
280 self: WalkResult,285 self: WalkResult,
281 options: std.json.StringifyOptions,286 options: std.json.StringifyOptions,
...@@ -304,6 +309,16 @@ const DocData = struct {...@@ -304,6 +309,16 @@ const DocData = struct {
304 \\, "value": {s}{} }} }}309 \\, "value": {s}{} }} }}
305 , .{ neg, v.value });310 , .{ neg, v.value });
306 },311 },
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 },
307 .bool => |v| {322 .bool => |v| {
308 try w.print(323 try w.print(
309 \\{{ "bool":{} }}324 \\{{ "bool":{} }}
...@@ -350,6 +365,15 @@ fn walkInstruction(...@@ -350,6 +365,15 @@ fn walkInstruction(
350 },365 },
351 };366 };
352 },367 },
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 },
353 .negate => {377 .negate => {
354 const un_node = data[inst_index].un_node;378 const un_node = data[inst_index].un_node;
355 var operand = try self.walkRef(zir, parent_scope, un_node.operand);379 var operand = try self.walkRef(zir, parent_scope, un_node.operand);