authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-05-24 18:03:04+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:11-07:00
logd707cd6e6df0aca14a9faaf2b411b3c4a3f08d10
treec8fbba7edb6f23eaaf89ea2c2780f2773ab69976
parentbde1caa4ceecb976e728c04c5aebb34f260d9b70

autodoc: improve decl categorization and fix enumliteral arrays/ptrs


2 files changed, 48 insertions(+), 14 deletions(-)

lib/docs/main.js+29-13
...@@ -1045,7 +1045,11 @@ var zigAnalysis;...@@ -1045,7 +1045,11 @@ var zigAnalysis;
1045 */1045 */
1046 function walkResultTypeRef(wr) {1046 function walkResultTypeRef(wr) {
1047 if (wr.typeRef) return wr.typeRef;1047 if (wr.typeRef) return wr.typeRef;
1048 return walkResultTypeRef(resolveValue(wr));1048 let resolved = resolveValue(wr);
1049 if (wr === resolved) {
1050 return {type: 0};
1051 }
1052 return walkResultTypeRef(resolved);
1049 }1053 }
1050 /**1054 /**
1051 * @typedef {{1055 * @typedef {{
...@@ -1128,6 +1132,10 @@ var zigAnalysis;...@@ -1128,6 +1132,10 @@ var zigAnalysis;
11281132
1129 switch (typeObj.kind) {1133 switch (typeObj.kind) {
1130 default: throw "TODO";1134 default: throw "TODO";
1135 case typeKinds.ComptimeExpr:
1136 {
1137 return "[ComptimeExpr]";
1138 }
1131 case typeKinds.Array:1139 case typeKinds.Array:
1132 {1140 {
1133 let arrayObj = /** @type {ArrayType} */ (typeObj);1141 let arrayObj = /** @type {ArrayType} */ (typeObj);
...@@ -1660,7 +1668,7 @@ var zigAnalysis;...@@ -1660,7 +1668,7 @@ var zigAnalysis;
1660 const funcRetExpr = resolveValue({1668 const funcRetExpr = resolveValue({
1661 expr: /** @type {Fn} */(typeExpr).ret1669 expr: /** @type {Fn} */(typeExpr).ret
1662 });1670 });
1663 if ("type" in funcRetExpr && funcRetExpr.type != typeTypeId) {1671 if ("type" in funcRetExpr.expr && funcRetExpr.expr.type == typeTypeId) {
1664 if (typeIsErrSet(declValue.expr.type)) {1672 if (typeIsErrSet(declValue.expr.type)) {
1665 errSetsList.push(decl);1673 errSetsList.push(decl);
1666 } else if (typeIsStructWithNoFields(declValue.expr.type)) {1674 } else if (typeIsStructWithNoFields(declValue.expr.type)) {
...@@ -1671,9 +1679,15 @@ var zigAnalysis;...@@ -1671,9 +1679,15 @@ var zigAnalysis;
1671 } else {1679 } else {
1672 fnsList.push(decl);1680 fnsList.push(decl);
1673 }1681 }
1674 } else {1682 } else {
1675 typesList.push(decl);1683 if (typeIsErrSet(declValue.expr.type)) {
1676 }1684 errSetsList.push(decl);
1685 } else if (typeIsStructWithNoFields(declValue.expr.type)) {
1686 namespacesList.push(decl);
1687 } else {
1688 typesList.push(decl);
1689 }
1690 }
1677 } else if ("typeRef" in declValue) {1691 } else if ("typeRef" in declValue) {
1678 if ("type" in declValue.typeRef && declValue.typeRef == typeTypeId) {1692 if ("type" in declValue.typeRef && declValue.typeRef == typeTypeId) {
1679 // We don't know what the type expression is, but we know it's a type.1693 // We don't know what the type expression is, but we know it's a type.
...@@ -2098,14 +2112,14 @@ var zigAnalysis;...@@ -2098,14 +2112,14 @@ var zigAnalysis;
2098 if (list[mainDeclIndex] != null) continue;2112 if (list[mainDeclIndex] != null) continue;
20992113
2100 let decl = zigAnalysis.decls[mainDeclIndex];2114 let decl = zigAnalysis.decls[mainDeclIndex];
2101 let declVal = resolveValue(decl.value);2115 let declVal = decl.value; //resolveValue(decl.value);
2102 let declNames = item.declNames.concat([decl.name]);2116 let declNames = item.declNames.concat([decl.name]);
2103 list[mainDeclIndex] = {2117 list[mainDeclIndex] = {
2104 pkgNames: pkgNames,2118 pkgNames: pkgNames,
2105 declNames: declNames,2119 declNames: declNames,
2106 };2120 };
2107 if ("type" in declVal) {2121 if ("type" in declVal.expr) {
2108 let value = zigAnalysis.types[declVal.type];2122 let value = zigAnalysis.types[declVal.expr.type];
2109 if (declCanRepresentTypeKind(value.kind))2123 if (declCanRepresentTypeKind(value.kind))
2110 {2124 {
2111 canonTypeDecls[declVal.type] = mainDeclIndex;2125 canonTypeDecls[declVal.type] = mainDeclIndex;
...@@ -2130,15 +2144,15 @@ var zigAnalysis;...@@ -2130,15 +2144,15 @@ var zigAnalysis;
2130 if (canonDeclPaths == null) {2144 if (canonDeclPaths == null) {
2131 canonDeclPaths = computeCanonDeclPaths();2145 canonDeclPaths = computeCanonDeclPaths();
2132 }2146 }
2133 let cd = /** @type {CanonDecl[]}*/(canonDeclPaths);2147 //let cd = /** @type {CanonDecl[]}*/(canonDeclPaths);
2134 return cd[index];2148 return canonDeclPaths[index];
2135 }2149 }
21362150
2137 /** @param {number} index */2151 /** @param {number} index */
2138 function getCanonTypeDecl(index) {2152 function getCanonTypeDecl(index) {
2139 getCanonDeclPath(0);2153 getCanonDeclPath(0);
2140 let ct = /** @type {number[]}*/(canonTypeDecls);2154 //let ct = /** @type {number[]}*/(canonTypeDecls);
2141 return ct[index];2155 return canonTypeDecls[index];
2142 }2156 }
21432157
2144 /** @param {string} text */2158 /** @param {string} text */
...@@ -2646,7 +2660,9 @@ function renderSearch() {...@@ -2646,7 +2660,9 @@ function renderSearch() {
2646 let lastPkgName = canonPath.pkgNames[canonPath.pkgNames.length - 1];2660 let lastPkgName = canonPath.pkgNames[canonPath.pkgNames.length - 1];
2647 let fullPathSearchText = lastPkgName + "." + canonPath.declNames.join('.');2661 let fullPathSearchText = lastPkgName + "." + canonPath.declNames.join('.');
2648 let astNode = zigAnalysis.astNodes[decl.src];2662 let astNode = zigAnalysis.astNodes[decl.src];
2649 let fileAndDocs = zigAnalysis.files[astNode.file];2663 let fileAndDocs = "" //zigAnalysis.files[astNode.file];
2664 // TODO: understand what this piece of code is trying to achieve
2665 // also right now `files` are expressed as a hashmap.
2650 if (astNode.docs != null) {2666 if (astNode.docs != null) {
2651 fileAndDocs += "\n" + astNode.docs;2667 fileAndDocs += "\n" + astNode.docs;
2652 }2668 }
src/Autodoc.zig+19-1
...@@ -474,6 +474,7 @@ const DocData = struct {...@@ -474,6 +474,7 @@ const DocData = struct {
474 .Int => |v| try printTypeBody(v, options, w),474 .Int => |v| try printTypeBody(v, options, w),
475 .Float => |v| try printTypeBody(v, options, w),475 .Float => |v| try printTypeBody(v, options, w),
476 .Type => |v| try printTypeBody(v, options, w),476 .Type => |v| try printTypeBody(v, options, w),
477 .EnumLiteral => |v| try printTypeBody(v, options, w),
477 .Pointer => |v| {478 .Pointer => |v| {
478 if (options.whitespace) |ws| try ws.outputIndent(w);479 if (options.whitespace) |ws| try ws.outputIndent(w);
479 try w.print(480 try w.print(
...@@ -807,7 +808,15 @@ fn walkInstruction(...@@ -807,7 +808,15 @@ fn walkInstruction(
807 .enum_literal => {808 .enum_literal => {
808 const str_tok = data[inst_index].str_tok;809 const str_tok = data[inst_index].str_tok;
809 const literal = file.zir.nullTerminatedString(str_tok.start);810 const literal = file.zir.nullTerminatedString(str_tok.start);
810 return DocData.WalkResult{ .expr = .{ .enumLiteral = literal } };811 const type_index = self.types.items.len;
812 try self.types.append(self.arena, .{
813 .EnumLiteral = .{ .name = "todo enum literal" },
814 });
815
816 return DocData.WalkResult{
817 .typeRef = .{ .type = type_index },
818 .expr = .{ .enumLiteral = literal },
819 };
811 },820 },
812 .int => {821 .int => {
813 const int = data[inst_index].int;822 const int = data[inst_index].int;
...@@ -1019,6 +1028,15 @@ fn walkInstruction(...@@ -1019,6 +1028,15 @@ fn walkInstruction(
1019 array_data[idx] = expr_index;1028 array_data[idx] = expr_index;
1020 }1029 }
10211030
1031 if (array_type == null) {
1032 panicWithContext(
1033 file,
1034 inst_index,
1035 "array_type was null!!",
1036 .{},
1037 );
1038 }
1039
1022 const type_slot_index = self.types.items.len;1040 const type_slot_index = self.types.items.len;
1023 try self.types.append(self.arena, .{1041 try self.types.append(self.arena, .{
1024 .Array = .{1042 .Array = .{