authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-09-10 21:08:26+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-09-11 21:35:01+02:00
log2a96f80d03c70546e5166e7752ee2b4b64c7cc5f
tree7f70dd408413dba97d039afdee3a62a261c322e4
parent81939a4939638fb296bc874afcef2f0f141f5c0c

autodoc: reduce json payload size

this commit removes whitespace and changes Decl, AstNode and Type to be json arrays instead of json objects. This change reduces json payload size for the stdlib from 25mb to < 10mb.

2 files changed, 309 insertions(+), 80 deletions(-)

lib/docs/main.js+226-61
......@@ -203,7 +203,7 @@ var zigAnalysis;
203203 if (!("type" in resolvedExpr)) {
204204 return null;
205205 }
206 let type = zigAnalysis.types[resolvedExpr.type];
206 let type = getType(resolvedExpr.type);
207207
208208 outer: for (let i = 0; i < 10000; i += 1) {
209209 switch (type.kind) {
......@@ -212,7 +212,7 @@ var zigAnalysis;
212212 let child = type.child;
213213 let resolvedChild = resolveValue(child);
214214 if ("type" in resolvedChild) {
215 type = zigAnalysis.types[resolvedChild.type];
215 type = getType(resolvedChild.type);
216216 continue;
217217 } else {
218218 return null;
......@@ -276,7 +276,7 @@ var zigAnalysis;
276276 }
277277
278278 if ("declRef" in value.expr) {
279 value = zigAnalysis.decls[value.expr.declRef].value;
279 value = getDecl(value.expr.declRef).value;
280280 continue;
281281 }
282282
......@@ -430,7 +430,7 @@ var zigAnalysis;
430430 curNav.pkgObjs.push(pkg);
431431 }
432432
433 let currentType = zigAnalysis.types[pkg.main];
433 let currentType = getType(pkg.main);
434434 curNav.declObjs = [currentType];
435435 for (let i = 0; i < curNav.declNames.length; i += 1) {
436436 let childDecl = findSubDecl(currentType, curNav.declNames[i]);
......@@ -440,7 +440,7 @@ var zigAnalysis;
440440
441441 let childDeclValue = resolveValue(childDecl.value).expr;
442442 if ("type" in childDeclValue) {
443 const t = zigAnalysis.types[childDeclValue.type];
443 const t = getType(childDeclValue.type);
444444 if (t.kind != typeKinds.Fn) {
445445 childDecl = t;
446446 }
......@@ -478,7 +478,7 @@ var zigAnalysis;
478478 }
479479
480480 if (lastIsDecl && last.kind === "const") {
481 let typeObj = zigAnalysis.types[resolveValue(last.value).expr.type];
481 let typeObj = getType(resolveValue(last.value).expr.type);
482482 if (typeObj && typeObj.kind === typeKinds.Fn) {
483483 return renderFn(last);
484484 }
......@@ -489,8 +489,8 @@ var zigAnalysis;
489489 }
490490
491491 function renderDocTest(decl) {
492 if (!("decltest" in decl)) return;
493 const astNode = zigAnalysis.astNodes[decl.decltest];
492 if (!decl.decltest) return;
493 const astNode = getAstNode(decl.decltest);
494494 domSectDocTests.classList.remove("hidden");
495495 domDocTestsCode.innerHTML = astNode.code;
496496 }
......@@ -498,7 +498,7 @@ var zigAnalysis;
498498 function renderUnknownDecl(decl) {
499499 domDeclNoRef.classList.remove("hidden");
500500
501 let docs = zigAnalysis.astNodes[decl.src].docs;
501 let docs = getAstNode(decl.src).docs;
502502 if (docs != null) {
503503 domTldDocs.innerHTML = markdown(docs);
504504 } else {
......@@ -509,18 +509,18 @@ var zigAnalysis;
509509 }
510510
511511 function typeIsErrSet(typeIndex) {
512 let typeObj = zigAnalysis.types[typeIndex];
512 let typeObj = getType(typeIndex);
513513 return typeObj.kind === typeKinds.ErrorSet;
514514 }
515515
516516 function typeIsStructWithNoFields(typeIndex) {
517 let typeObj = zigAnalysis.types[typeIndex];
517 let typeObj = getType(typeIndex);
518518 if (typeObj.kind !== typeKinds.Struct) return false;
519519 return typeObj.fields.length == 0;
520520 }
521521
522522 function typeIsGenericFn(typeIndex) {
523 let typeObj = zigAnalysis.types[typeIndex];
523 let typeObj = getType(typeIndex);
524524 if (typeObj.kind !== typeKinds.Fn) {
525525 return false;
526526 }
......@@ -532,12 +532,12 @@ var zigAnalysis;
532532 let last = fnDecl.value.expr.refPath.length - 1;
533533 let lastExpr = fnDecl.value.expr.refPath[last];
534534 console.assert("declRef" in lastExpr);
535 fnDecl = zigAnalysis.decls[lastExpr.declRef];
535 fnDecl = getDecl(lastExpr.declRef);
536536 }
537537
538538 let value = resolveValue(fnDecl.value);
539539 console.assert("type" in value.expr);
540 let typeObj = zigAnalysis.types[value.expr.type];
540 let typeObj = getType(value.expr.type);
541541
542542 domFnProtoCode.innerHTML = exprName(value.expr, {
543543 wantHtml: true,
......@@ -546,7 +546,7 @@ var zigAnalysis;
546546 });
547547
548548 let docsSource = null;
549 let srcNode = zigAnalysis.astNodes[fnDecl.src];
549 let srcNode = getAstNode(fnDecl.src);
550550 if (srcNode.docs != null) {
551551 docsSource = srcNode.docs;
552552 }
......@@ -557,14 +557,14 @@ var zigAnalysis;
557557 if ("type" in retExpr) {
558558 let retIndex = retExpr.type;
559559 let errSetTypeIndex = null;
560 let retType = zigAnalysis.types[retIndex];
560 let retType = getType(retIndex);
561561 if (retType.kind === typeKinds.ErrorSet) {
562562 errSetTypeIndex = retIndex;
563563 } else if (retType.kind === typeKinds.ErrorUnion) {
564564 errSetTypeIndex = retType.err.type;
565565 }
566566 if (errSetTypeIndex != null) {
567 let errSetType = zigAnalysis.types[errSetTypeIndex];
567 let errSetType = getType(errSetTypeIndex);
568568 renderErrorSet(errSetType);
569569 }
570570 }
......@@ -578,7 +578,7 @@ var zigAnalysis;
578578 let call = zigAnalysis.calls[resolvedGenericRet.expr.call];
579579 let resolvedFunc = resolveValue({ expr: call.func });
580580 if (!("type" in resolvedFunc.expr)) return;
581 let callee = zigAnalysis.types[resolvedFunc.expr.type];
581 let callee = getType(resolvedFunc.expr.type);
582582 if (!callee.generic_ret) return;
583583 resolvedGenericRet = resolveValue({ expr: callee.generic_ret });
584584 }
......@@ -591,7 +591,7 @@ var zigAnalysis;
591591 }
592592
593593 if (!("type" in resolvedGenericRet.expr)) return;
594 const genericType = zigAnalysis.types[resolvedGenericRet.expr.type];
594 const genericType = getType(resolvedGenericRet.expr.type);
595595 if (isContainerType(genericType)) {
596596 renderContainer(genericType);
597597 }
......@@ -621,7 +621,7 @@ var zigAnalysis;
621621 domFnNoExamples.classList.add("hidden");
622622 }
623623
624 let protoSrcNode = zigAnalysis.astNodes[protoSrcIndex];
624 let protoSrcNode = getAstNode(protoSrcIndex);
625625 if (
626626 docsSource == null &&
627627 protoSrcNode != null &&
......@@ -639,13 +639,13 @@ var zigAnalysis;
639639 function renderFnParamDocs(fnDecl, typeObj) {
640640 let docCount = 0;
641641
642 let fnNode = zigAnalysis.astNodes[fnDecl.src];
642 let fnNode = getAstNode(fnDecl.src);
643643 let fields = fnNode.fields;
644644 let isVarArgs = fnNode.varArgs;
645645
646646 for (let i = 0; i < fields.length; i += 1) {
647647 let field = fields[i];
648 let fieldNode = zigAnalysis.astNodes[field];
648 let fieldNode = getAstNode(field);
649649 if (fieldNode.docs != null) {
650650 docCount += 1;
651651 }
......@@ -659,7 +659,7 @@ var zigAnalysis;
659659
660660 for (let i = 0; i < fields.length; i += 1) {
661661 let field = fields[i];
662 let fieldNode = zigAnalysis.astNodes[field];
662 let fieldNode = getAstNode(field);
663663 let docs = fieldNode.docs;
664664 if (fieldNode.docs == null) {
665665 continue;
......@@ -967,17 +967,17 @@ var zigAnalysis;
967967 }
968968 case "switchOp": {
969969 let condExpr = zigAnalysis.exprs[expr.switchOp.cond_index];
970 let ast = zigAnalysis.astNodes[expr.switchOp.ast];
970 let ast = getAstNode(expr.switchOp.src);
971971 let file_name = expr.switchOp.file_name;
972972 let outer_decl_index = expr.switchOp.outer_decl;
973 let outer_decl = zigAnalysis.types[outer_decl_index];
973 let outer_decl = getType(outer_decl_index);
974974 let line = 0;
975975 // console.log(expr.switchOp)
976976 // console.log(outer_decl)
977977 while (outer_decl_index !== 0 && outer_decl.line_number > 0) {
978978 line += outer_decl.line_number;
979979 outer_decl_index = outer_decl.outer_decl;
980 outer_decl = zigAnalysis.types[outer_decl_index];
980 outer_decl = getType(outer_decl_index);
981981 // console.log(outer_decl)
982982 }
983983 line += ast.line + 1;
......@@ -1028,8 +1028,8 @@ var zigAnalysis;
10281028 case "fieldRef": {
10291029 const enumObj = exprName({ type: expr.fieldRef.type }, opts);
10301030 const field =
1031 zigAnalysis.astNodes[enumObj.ast].fields[expr.fieldRef.index];
1032 const name = zigAnalysis.astNodes[field].name;
1031 getAstNode(enumObj.src).fields[expr.fieldRef.index];
1032 const name = getAstNode(field).name;
10331033 return name;
10341034 }
10351035 case "enumToInt": {
......@@ -1452,13 +1452,13 @@ var zigAnalysis;
14521452 return print_lhs + " " + operator + " " + print_rhs;
14531453 }
14541454 case "errorSets": {
1455 const errUnionObj = zigAnalysis.types[expr.errorSets];
1455 const errUnionObj = getType(expr.errorSets);
14561456 let lhs = exprName(errUnionObj.lhs, opts);
14571457 let rhs = exprName(errUnionObj.rhs, opts);
14581458 return lhs + " || " + rhs;
14591459 }
14601460 case "errorUnion": {
1461 const errUnionObj = zigAnalysis.types[expr.errorUnion];
1461 const errUnionObj = getType(expr.errorUnion);
14621462 let lhs = exprName(errUnionObj.lhs, opts);
14631463 let rhs = exprName(errUnionObj.rhs, opts);
14641464 return lhs + "!" + rhs;
......@@ -1574,7 +1574,7 @@ var zigAnalysis;
15741574 return exprName(exprArg, opts);
15751575 }
15761576 case "declRef": {
1577 return zigAnalysis.decls[expr.declRef].name;
1577 return getDecl(expr.declRef).name;
15781578 }
15791579 case "refPath": {
15801580 return expr.refPath.map((x) => exprName(x, opts)).join(".");
......@@ -1611,7 +1611,7 @@ var zigAnalysis;
16111611 let name = "";
16121612
16131613 let typeObj = expr.type;
1614 if (typeof typeObj === "number") typeObj = zigAnalysis.types[typeObj];
1614 if (typeof typeObj === "number") typeObj = getType(typeObj);
16151615 switch (typeObj.kind) {
16161616 default:
16171617 throw "TODO";
......@@ -1865,7 +1865,7 @@ var zigAnalysis;
18651865 if (fnObj.params) {
18661866 let fields = null;
18671867 let isVarArgs = false;
1868 let fnNode = zigAnalysis.astNodes[fnObj.src];
1868 let fnNode = getAstNode(fnObj.src);
18691869 fields = fnNode.fields;
18701870 isVarArgs = fnNode.varArgs;
18711871
......@@ -1880,7 +1880,7 @@ var zigAnalysis;
18801880 let paramValue = resolveValue({ expr: value });
18811881
18821882 if (fields != null) {
1883 let paramNode = zigAnalysis.astNodes[fields[i]];
1883 let paramNode = getAstNode(fields[i]);
18841884
18851885 if (paramNode.varArgs) {
18861886 payloadHtml += "...";
......@@ -2046,7 +2046,7 @@ var zigAnalysis;
20462046 function shouldSkipParamName(typeRef, paramName) {
20472047 let resolvedTypeRef = resolveValue({ expr: typeRef });
20482048 if ("type" in resolvedTypeRef) {
2049 let typeObj = zigAnalysis.types[resolvedTypeRef.type];
2049 let typeObj = getType(resolvedTypeRef.type);
20502050 if (typeObj.kind === typeKinds.Pointer) {
20512051 let ptrObj = typeObj;
20522052 if (getPtrSize(ptrObj) === pointerSizeEnum.One) {
......@@ -2067,7 +2067,7 @@ var zigAnalysis;
20672067 if (
20682068 rootIsStd &&
20692069 typeObj ===
2070 zigAnalysis.types[zigAnalysis.packages[zigAnalysis.rootPkg].main]
2070 getType(zigAnalysis.packages[zigAnalysis.rootPkg].main)
20712071 ) {
20722072 name = "std";
20732073 } else {
......@@ -2189,7 +2189,7 @@ var zigAnalysis;
21892189
21902190 if (resolvedValue.expr.fieldRef) {
21912191 const declRef = decl.value.expr.refPath[0].declRef;
2192 const type = zigAnalysis.decls[declRef];
2192 const type = getDecl(declRef);
21932193 domFnProtoCode.innerHTML =
21942194 '<span class="tok-kw">const</span> ' +
21952195 escapeHtml(decl.name) +
......@@ -2229,7 +2229,7 @@ var zigAnalysis;
22292229 ";";
22302230 }
22312231
2232 let docs = zigAnalysis.astNodes[decl.src].docs;
2232 let docs = getAstNode(decl.src).docs;
22332233 if (docs != null) {
22342234 domTldDocs.innerHTML = markdown(docs);
22352235 domTldDocs.classList.remove("hidden");
......@@ -2246,7 +2246,7 @@ var zigAnalysis;
22462246 ": " +
22472247 typeValueName(declTypeRef, true, true);
22482248
2249 let docs = zigAnalysis.astNodes[decl.src].docs;
2249 let docs = getAstNode(decl.src).docs;
22502250 if (docs != null) {
22512251 domTldDocs.innerHTML = markdown(docs);
22522252 domTldDocs.classList.remove("hidden");
......@@ -2266,7 +2266,7 @@ var zigAnalysis;
22662266 testsList
22672267 ) {
22682268 for (let i = 0; i < decls.length; i += 1) {
2269 let decl = zigAnalysis.decls[decls[i]];
2269 let decl = getDecl(decls[i]);
22702270 let declValue = resolveValue(decl.value);
22712271
22722272 if (decl.isTest) {
......@@ -2282,7 +2282,7 @@ var zigAnalysis;
22822282 if (decl.kind === "const") {
22832283 if ("type" in declValue.expr) {
22842284 // We have the actual type expression at hand.
2285 const typeExpr = zigAnalysis.types[declValue.expr.type];
2285 const typeExpr = getType(declValue.expr.type);
22862286 if (typeExpr.kind == typeKinds.Fn) {
22872287 const funcRetExpr = resolveValue({
22882288 expr: typeExpr.ret,
......@@ -2310,7 +2310,7 @@ var zigAnalysis;
23102310 typesList.push(decl);
23112311 }
23122312 }
2313 } else if ("typeRef" in declValue) {
2313 } else if (declValue.typeRef) {
23142314 if ("type" in declValue.typeRef && declValue.typeRef == typeTypeId) {
23152315 // We don't know what the type expression is, but we know it's a type.
23162316 typesList.push(decl);
......@@ -2324,7 +2324,7 @@ var zigAnalysis;
23242324 }
23252325 }
23262326 function renderSourceFileLink(decl) {
2327 let srcNode = zigAnalysis.astNodes[decl.src];
2327 let srcNode = getAstNode(decl.src);
23282328
23292329 return "<a style=\"float: right;\" href=\"" +
23302330 sourceFileUrlTemplate.replace("{{file}}",
......@@ -2377,7 +2377,7 @@ var zigAnalysis;
23772377 testsList.sort(byNameProperty);
23782378
23792379 if (container.src != null) {
2380 let docs = zigAnalysis.astNodes[container.src].docs;
2380 let docs = getAstNode(container.src).docs;
23812381 if (docs != null) {
23822382 domTldDocs.innerHTML = markdown(docs);
23832383 domTldDocs.classList.remove("hidden");
......@@ -2457,7 +2457,7 @@ var zigAnalysis;
24572457 });
24582458 tdFnSrc.innerHTML = renderSourceFileLink(decl);
24592459
2460 let docs = zigAnalysis.astNodes[decl.src].docs;
2460 let docs = getAstNode(decl.src).docs;
24612461 if (docs != null) {
24622462 tdDesc.innerHTML = shortDescMarkdown(docs);
24632463 } else {
......@@ -2467,12 +2467,12 @@ var zigAnalysis;
24672467 domSectFns.classList.remove("hidden");
24682468 }
24692469
2470 let containerNode = zigAnalysis.astNodes[container.src];
2470 let containerNode = getAstNode(container.src);
24712471 if (containerNode.fields && containerNode.fields.length > 0) {
24722472 resizeDomList(domListFields, containerNode.fields.length, "<div></div>");
24732473
24742474 for (let i = 0; i < containerNode.fields.length; i += 1) {
2475 let fieldNode = zigAnalysis.astNodes[containerNode.fields[i]];
2475 let fieldNode = getAstNode(containerNode.fields[i]);
24762476 let divDom = domListFields.children[i];
24772477 let fieldName = fieldNode.name;
24782478 let docs = fieldNode.docs;
......@@ -2528,7 +2528,7 @@ var zigAnalysis;
25282528
25292529 tdType.innerHTML = typeValueName(typeOfDecl(decl), true, true);
25302530
2531 let docs = zigAnalysis.astNodes[decl.src].docs;
2531 let docs = getAstNode(decl.src).docs;
25322532 if (docs != null) {
25332533 tdDesc.innerHTML = shortDescMarkdown(docs);
25342534 } else {
......@@ -2561,7 +2561,7 @@ var zigAnalysis;
25612561 wantLink: true,
25622562 });
25632563
2564 let docs = zigAnalysis.astNodes[decl.src].docs;
2564 let docs = getAstNode(decl.src).docs;
25652565 if (docs != null) {
25662566 tdDesc.innerHTML = shortDescMarkdown(docs);
25672567 } else {
......@@ -2594,7 +2594,7 @@ var zigAnalysis;
25942594 wantLink: true,
25952595 });
25962596
2597 let docs = zigAnalysis.astNodes[decl.src].docs;
2597 let docs = getAstNode(decl.src).docs;
25982598 if (docs != null) {
25992599 tdDesc.innerHTML = shortDescMarkdown(docs);
26002600 } else {
......@@ -2668,7 +2668,7 @@ var zigAnalysis;
26682668
26692669 function findTypeTypeId() {
26702670 for (let i = 0; i < zigAnalysis.types.length; i += 1) {
2671 if (zigAnalysis.types[i].kind == typeKinds.Type) {
2671 if (getType(i).kind == typeKinds.Type) {
26722672 return i;
26732673 }
26742674 }
......@@ -2732,11 +2732,11 @@ var zigAnalysis;
27322732 if ("value" in parentType) {
27332733 const rv = resolveValue(parentType.value);
27342734 if ("type" in rv.expr) {
2735 const t = zigAnalysis.types[rv.expr.type];
2735 const t = getType(rv.expr.type);
27362736 if (t.kind == typeKinds.Fn && t.generic_ret != null) {
27372737 const rgr = resolveValue({ expr: t.generic_ret });
27382738 if ("type" in rgr.expr) {
2739 parentType = zigAnalysis.types[rgr.expr.type];
2739 parentType = getType(rgr.expr.type);
27402740 }
27412741 }
27422742 }
......@@ -2746,7 +2746,7 @@ var zigAnalysis;
27462746 if (!parentType.pubDecls) return null;
27472747 for (let i = 0; i < parentType.pubDecls.length; i += 1) {
27482748 let declIndex = parentType.pubDecls[i];
2749 let childDecl = zigAnalysis.decls[declIndex];
2749 let childDecl = getDecl(declIndex);
27502750 if (childDecl.name === childName) {
27512751 return childDecl;
27522752 }
......@@ -2754,7 +2754,7 @@ var zigAnalysis;
27542754 if (!parentType.privDecls) return null;
27552755 for (let i = 0; i < parentType.privDecls.length; i += 1) {
27562756 let declIndex = parentType.privDecls[i];
2757 let childDecl = zigAnalysis.decls[declIndex];
2757 let childDecl = getDecl(declIndex);
27582758 if (childDecl.name === childName) {
27592759 return childDecl;
27602760 }
......@@ -2805,7 +2805,7 @@ var zigAnalysis;
28052805 let stack = [
28062806 {
28072807 declNames: [],
2808 type: zigAnalysis.types[pkg.main],
2808 type: getType(pkg.main),
28092809 },
28102810 ];
28112811 while (stack.length !== 0) {
......@@ -2819,7 +2819,7 @@ var zigAnalysis;
28192819 let mainDeclIndex = t.pubDecls[declI];
28202820 if (list[mainDeclIndex] != null) continue;
28212821
2822 let decl = zigAnalysis.decls[mainDeclIndex];
2822 let decl = getDecl(mainDeclIndex);
28232823 let declVal = resolveValue(decl.value);
28242824 let declNames = item.declNames.concat([decl.name]);
28252825 list[mainDeclIndex] = {
......@@ -2827,7 +2827,7 @@ var zigAnalysis;
28272827 declNames: declNames,
28282828 };
28292829 if ("type" in declVal.expr) {
2830 let value = zigAnalysis.types[declVal.expr.type];
2830 let value = getType(declVal.expr.type);
28312831 if (declCanRepresentTypeKind(value.kind)) {
28322832 canonTypeDecls[declVal.type] = mainDeclIndex;
28332833 }
......@@ -2843,7 +2843,7 @@ var zigAnalysis;
28432843 if (value.kind == typeKinds.Fn && value.generic_ret != null) {
28442844 let resolvedVal = resolveValue({ expr: value.generic_ret });
28452845 if ("type" in resolvedVal.expr) {
2846 let generic_type = zigAnalysis.types[resolvedVal.expr.type];
2846 let generic_type = getType(resolvedVal.expr.type);
28472847 if (isContainerType(generic_type)) {
28482848 stack.push({
28492849 declNames: declNames,
......@@ -3394,11 +3394,11 @@ var zigAnalysis;
33943394 let canonPath = getCanonDeclPath(declIndex);
33953395 if (canonPath == null) continue;
33963396
3397 let decl = zigAnalysis.decls[declIndex];
3397 let decl = getDecl(declIndex);
33983398 let lastPkgName = canonPath.pkgNames[canonPath.pkgNames.length - 1];
33993399 let fullPathSearchText =
34003400 lastPkgName + "." + canonPath.declNames.join(".");
3401 let astNode = zigAnalysis.astNodes[decl.src];
3401 let astNode = getAstNode(decl.src);
34023402 let fileAndDocs = ""; //zigAnalysis.files[astNode.file];
34033403 // TODO: understand what this piece of code is trying to achieve
34043404 // also right now `files` are expressed as a hashmap.
......@@ -3513,4 +3513,169 @@ var zigAnalysis;
35133513 function byNameProperty(a, b) {
35143514 return operatorCompare(a.name, b.name);
35153515 }
3516
3517
3518 function getDecl(idx) {
3519 const decl = zigAnalysis.decls[idx];
3520 return {
3521 name: decl[0],
3522 kind: decl[1],
3523 isTest: decl[2],
3524 src: decl[3],
3525 value: decl[4],
3526 decltest: decl[5],
3527 };
3528 }
3529
3530 function getAstNode(idx) {
3531 const ast = zigAnalysis.astNodes[idx];
3532 return {
3533 file: ast[0],
3534 line: ast[1],
3535 col: ast[2],
3536 name: ast[3],
3537 code: ast[4],
3538 docs: ast[5],
3539 fields: ast[6],
3540 comptime: ast[7],
3541 };
3542 }
3543
3544 function getType(idx){
3545 const ty = zigAnalysis.types[idx];
3546 switch(ty[0]) {
3547 default:
3548 throw "unhandled type kind!";
3549 case 0: // Unanalyzed
3550 throw "unanalyzed type!";
3551 case 1: // Type
3552 case 2: // Void
3553 case 3: // Bool
3554 case 4: // NoReturn
3555 case 5: // Int
3556 case 6: // Float
3557 return { kind: ty[0], name: ty[1]};
3558 case 7: // Pointer
3559 return {
3560 kind: ty[0],
3561 size: ty[1],
3562 child: ty[2],
3563 sentinel: ty[3],
3564 align: ty[4],
3565 address_space: ty[5],
3566 bit_start: ty[6],
3567 host_size: ty[7],
3568 is_ref: ty[8],
3569 is_allowzero: ty[9],
3570 is_mutable: ty[10],
3571 is_volatile: ty[11],
3572 has_sentinel: ty[12],
3573 has_align: ty[13],
3574 has_addrspace: ty[14],
3575 has_bit_range: ty[15],
3576 };
3577 case 8: // Array
3578 return {
3579 kind: ty[0],
3580 len: ty[1],
3581 child: ty[2],
3582 sentinel: ty[3],
3583 };
3584 case 9: // Struct
3585 return {
3586 kind: ty[0],
3587 name: ty[1],
3588 src: ty[2],
3589 privDecls: ty[3],
3590 pubDecls: ty[4],
3591 fields: ty[5],
3592 line_number: ty[6],
3593 outer_decl: ty[7],
3594 };
3595 case 10: // ComptimeExpr
3596 case 11: // ComptimeFloat
3597 case 12: // ComptimeInt
3598 case 13: // Undefined
3599 case 14: // Null
3600 return { kind: ty[0], name: ty[1] };
3601 case 15: // Optional
3602 return {
3603 kind: ty[0],
3604 name: ty[1],
3605 child: ty[2],
3606 };
3607 case 16: // ErrorUnion
3608 return {
3609 kind: ty[0],
3610 lhs: ty[1],
3611 rhs: ty[2],
3612 };
3613 case 17: // InferredErrorUnion
3614 return {
3615 kind: ty[0],
3616 payload: ty[1],
3617 };
3618 case 18: // ErrorSet
3619 return {
3620 kind: ty[0],
3621 name: ty[1],
3622 fields: ty[2],
3623 };
3624 case 19: // Enum
3625 return {
3626 kind: ty[0],
3627 name: ty[1],
3628 src: ty[2],
3629 privDecls: ty[3],
3630 pubDecls: ty[4],
3631 };
3632 case 20: // Union
3633 return {
3634 kind: ty[0],
3635 name: ty[1],
3636 src: ty[2],
3637 privDecls: ty[3],
3638 pubDecls: ty[4],
3639 fields: ty[5],
3640 };
3641 case 21: // Fn
3642 return {
3643 kind: ty[0],
3644 name: ty[1],
3645 src: ty[2],
3646 ret: ty[3],
3647 generic_ret: ty[4],
3648 params: ty[5],
3649 lib_name: ty[6],
3650 is_var_args: ty[7],
3651 is_inferred_error: ty[8],
3652 has_lib_name: ty[9],
3653 has_cc: ty[10],
3654 cc: ty[11],
3655 align: ty[12],
3656 has_align: ty[13],
3657 is_test: ty[14],
3658 is_extern: ty[15],
3659 };
3660 case 22: // BoundFn
3661 return { kind: ty[0], name: ty[1] };
3662 case 23: // Opaque
3663 return {
3664 kind: ty[0],
3665 name: ty[1],
3666 src: ty[2],
3667 privDecls: ty[3],
3668 pubDecls: ty[4],
3669 };
3670 case 24: // Frame
3671 case 25: // AnyFrame
3672 case 26: // Vector
3673 case 27: // EnumLiteral
3674 return { kind: ty[0], name: ty[1] };
3675 }
3676 }
3677
35163678})();
3679
3680
3681
src/Autodoc.zig+83-19
......@@ -280,8 +280,8 @@ pub fn generateZirData(self: *Autodoc) !void {
280280 try std.json.stringify(
281281 data,
282282 .{
283 .whitespace = .{ .indent = if (builtin.mode == .Debug) .{ .Space = 4 } else .None },
284 .emit_null_optional_fields = false,
283 .whitespace = .{ .indent = .None, .separator = false },
284 .emit_null_optional_fields = true,
285285 },
286286 out,
287287 );
......@@ -404,6 +404,7 @@ const DocData = struct {
404404 w: anytype,
405405 ) !void {
406406 var jsw = std.json.writeStream(w, 15);
407 if (opts.whitespace) |ws| jsw.whitespace = ws;
407408 try jsw.beginObject();
408409 inline for (comptime std.meta.tags(std.meta.FieldEnum(DocData))) |f| {
409410 const f_name = @tagName(f);
......@@ -449,6 +450,8 @@ const DocData = struct {
449450 w: anytype,
450451 ) !void {
451452 var jsw = std.json.writeStream(w, 15);
453 if (opts.whitespace) |ws| jsw.whitespace = ws;
454
452455 try jsw.beginObject();
453456 inline for (comptime std.meta.tags(std.meta.FieldEnum(DocPackage))) |f| {
454457 const f_name = @tagName(f);
......@@ -474,6 +477,22 @@ const DocData = struct {
474477 // The index in astNodes of the `test declname { }` node
475478 decltest: ?usize = null,
476479 _analyzed: bool, // omitted in json data
480
481 pub fn jsonStringify(
482 self: Decl,
483 opts: std.json.StringifyOptions,
484 w: anytype,
485 ) !void {
486 var jsw = std.json.writeStream(w, 15);
487 if (opts.whitespace) |ws| jsw.whitespace = ws;
488 try jsw.beginArray();
489 inline for (comptime std.meta.fields(Decl)) |f| {
490 try jsw.arrayElem();
491 try std.json.stringify(@field(self, f.name), opts, w);
492 jsw.state_index -= 1;
493 }
494 try jsw.endArray();
495 }
477496 };
478497
479498 const AstNode = struct {
......@@ -485,6 +504,22 @@ const DocData = struct {
485504 docs: ?[]const u8 = null,
486505 fields: ?[]usize = null, // index into astNodes
487506 @"comptime": bool = false,
507
508 pub fn jsonStringify(
509 self: AstNode,
510 opts: std.json.StringifyOptions,
511 w: anytype,
512 ) !void {
513 var jsw = std.json.writeStream(w, 15);
514 if (opts.whitespace) |ws| jsw.whitespace = ws;
515 try jsw.beginArray();
516 inline for (comptime std.meta.fields(AstNode)) |f| {
517 try jsw.arrayElem();
518 try std.json.stringify(@field(self, f.name), opts, w);
519 jsw.state_index -= 1;
520 }
521 try jsw.endArray();
522 }
488523 };
489524
490525 const Type = union(enum) {
......@@ -525,7 +560,6 @@ const DocData = struct {
525560 fields: ?[]Expr = null, // (use src->fields to find names)
526561 line_number: usize,
527562 outer_decl: usize,
528 ast: usize,
529563 },
530564 ComptimeExpr: struct { name: []const u8 },
531565 ComptimeFloat: struct { name: []const u8 },
......@@ -548,7 +582,6 @@ const DocData = struct {
548582 src: usize, // index into astNodes
549583 privDecls: []usize = &.{}, // index into decls
550584 pubDecls: []usize = &.{}, // index into decls
551 ast: usize,
552585 // (use src->fields to find field names)
553586 },
554587 Union: struct {
......@@ -557,7 +590,6 @@ const DocData = struct {
557590 privDecls: []usize = &.{}, // index into decls
558591 pubDecls: []usize = &.{}, // index into decls
559592 fields: []Expr = &.{}, // (use src->fields to find names)
560 ast: usize,
561593 },
562594 Fn: struct {
563595 name: []const u8,
......@@ -582,7 +614,6 @@ const DocData = struct {
582614 src: usize, // index into astNodes
583615 privDecls: []usize = &.{}, // index into decls
584616 pubDecls: []usize = &.{}, // index into decls
585 ast: usize,
586617 },
587618 Frame: struct { name: []const u8 },
588619 AnyFrame: struct { name: []const u8 },
......@@ -601,14 +632,15 @@ const DocData = struct {
601632 ) !void {
602633 const active_tag = std.meta.activeTag(self);
603634 var jsw = std.json.writeStream(w, 15);
604 try jsw.beginObject();
605 try jsw.objectField("kind");
635 if (opts.whitespace) |ws| jsw.whitespace = ws;
636 try jsw.beginArray();
637 try jsw.arrayElem();
606638 try jsw.emitNumber(@enumToInt(active_tag));
607639 inline for (comptime std.meta.fields(Type)) |case| {
608640 if (@field(Type, case.name) == active_tag) {
609641 const current_value = @field(self, case.name);
610642 inline for (comptime std.meta.fields(case.field_type)) |f| {
611 try jsw.objectField(f.name);
643 try jsw.arrayElem();
612644 if (f.field_type == std.builtin.TypeInfo.Pointer.Size) {
613645 try jsw.emitNumber(@enumToInt(@field(current_value, f.name)));
614646 } else {
......@@ -618,7 +650,7 @@ const DocData = struct {
618650 }
619651 }
620652 }
621 try jsw.endObject();
653 try jsw.endArray();
622654 }
623655 };
624656
......@@ -686,7 +718,7 @@ const DocData = struct {
686718 const SwitchOp = struct {
687719 cond_index: usize,
688720 file_name: []const u8,
689 ast: usize,
721 src: usize,
690722 outer_decl: usize, // index in `types`
691723 };
692724 const BuiltinBin = struct {
......@@ -704,7 +736,15 @@ const DocData = struct {
704736 end: ?usize = null,
705737 sentinel: ?usize = null, // index in `exprs`
706738 };
707 const Cmpxchg = struct { name: []const u8, type: usize, ptr: usize, expected_value: usize, new_value: usize, success_order: usize, failure_order: usize };
739 const Cmpxchg = struct {
740 name: []const u8,
741 type: usize,
742 ptr: usize,
743 expected_value: usize,
744 new_value: usize,
745 success_order: usize,
746 failure_order: usize,
747 };
708748 const As = struct {
709749 typeRefArg: ?usize, // index in `exprs`
710750 exprArg: usize, // index in `exprs`
......@@ -721,11 +761,12 @@ const DocData = struct {
721761
722762 pub fn jsonStringify(
723763 self: Expr,
724 opt: std.json.StringifyOptions,
764 opts: std.json.StringifyOptions,
725765 w: anytype,
726766 ) !void {
727767 const active_tag = std.meta.activeTag(self);
728768 var jsw = std.json.writeStream(w, 15);
769 if (opts.whitespace) |ws| jsw.whitespace = ws;
729770 try jsw.beginObject();
730771 try jsw.objectField(@tagName(active_tag));
731772 switch (self) {
......@@ -742,7 +783,7 @@ const DocData = struct {
742783 if (comptime std.mem.eql(u8, case.name, "builtinField"))
743784 continue;
744785 if (@field(Expr, case.name) == active_tag) {
745 try std.json.stringify(@field(self, case.name), opt, w);
786 try std.json.stringify(@field(self, case.name), opts, w);
746787 jsw.state_index -= 1;
747788 // TODO: we should not reach into the state of the
748789 // json writer, but alas, this is what's
......@@ -1874,7 +1915,12 @@ fn walkInstruction(
18741915 // log.debug("{s}", .{sep});
18751916
18761917 const switch_index = self.exprs.items.len;
1877 try self.exprs.append(self.arena, .{ .switchOp = .{ .cond_index = cond_index, .file_name = file.sub_file_path, .ast = ast_index, .outer_decl = type_index } });
1918 try self.exprs.append(self.arena, .{ .switchOp = .{
1919 .cond_index = cond_index,
1920 .file_name = file.sub_file_path,
1921 .src = ast_index,
1922 .outer_decl = type_index,
1923 } });
18781924
18791925 return DocData.WalkResult{
18801926 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
......@@ -2505,7 +2551,6 @@ fn walkInstruction(
25052551 .src = self_ast_node_index,
25062552 .privDecls = priv_decl_indexes.items,
25072553 .pubDecls = decl_indexes.items,
2508 .ast = self_ast_node_index,
25092554 },
25102555 };
25112556 if (self.ref_paths_pending_on_types.get(type_slot_index)) |paths| {
......@@ -2644,7 +2689,13 @@ fn walkInstruction(
26442689 self.ast_nodes.items[self_ast_node_index].fields = field_name_indexes.items;
26452690
26462691 self.types.items[type_slot_index] = .{
2647 .Union = .{ .name = "todo_name", .src = self_ast_node_index, .privDecls = priv_decl_indexes.items, .pubDecls = decl_indexes.items, .fields = field_type_refs.items, .ast = self_ast_node_index },
2692 .Union = .{
2693 .name = "todo_name",
2694 .src = self_ast_node_index,
2695 .privDecls = priv_decl_indexes.items,
2696 .pubDecls = decl_indexes.items,
2697 .fields = field_type_refs.items,
2698 },
26482699 };
26492700
26502701 if (self.ref_paths_pending_on_types.get(type_slot_index)) |paths| {
......@@ -2796,7 +2847,12 @@ fn walkInstruction(
27962847 self.ast_nodes.items[self_ast_node_index].fields = field_name_indexes.items;
27972848
27982849 self.types.items[type_slot_index] = .{
2799 .Enum = .{ .name = "todo_name", .src = self_ast_node_index, .privDecls = priv_decl_indexes.items, .pubDecls = decl_indexes.items, .ast = self_ast_node_index },
2850 .Enum = .{
2851 .name = "todo_name",
2852 .src = self_ast_node_index,
2853 .privDecls = priv_decl_indexes.items,
2854 .pubDecls = decl_indexes.items,
2855 },
28002856 };
28012857 if (self.ref_paths_pending_on_types.get(type_slot_index)) |paths| {
28022858 for (paths.items) |resume_info| {
......@@ -2910,7 +2966,15 @@ fn walkInstruction(
29102966 self.ast_nodes.items[self_ast_node_index].fields = field_name_indexes.items;
29112967
29122968 self.types.items[type_slot_index] = .{
2913 .Struct = .{ .name = "todo_name", .src = self_ast_node_index, .privDecls = priv_decl_indexes.items, .pubDecls = decl_indexes.items, .fields = field_type_refs.items, .line_number = self.ast_nodes.items[self_ast_node_index].line, .outer_decl = type_slot_index - 1, .ast = self_ast_node_index },
2969 .Struct = .{
2970 .name = "todo_name",
2971 .src = self_ast_node_index,
2972 .privDecls = priv_decl_indexes.items,
2973 .pubDecls = decl_indexes.items,
2974 .fields = field_type_refs.items,
2975 .line_number = self.ast_nodes.items[self_ast_node_index].line,
2976 .outer_decl = type_slot_index - 1,
2977 },
29142978 };
29152979 if (self.ref_paths_pending_on_types.get(type_slot_index)) |paths| {
29162980 for (paths.items) |resume_info| {