diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-10-09 16:52:29 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-10-09 18:09:35 -0400 |
| commit | 13ae7d47b77c4ac7b1769f3ac3b394c32ffebe6e (patch) | |
| tree | cdbd83aa9c9dd363336b825b26219e00e0ab1f80 /lib/std | |
| parent | 42f2814d9acc014c84034468b2923195aa547ce1 (diff) | |
| download | zig-13ae7d47b77c4ac7b1769f3ac3b394c32ffebe6e.tar.gz zig-13ae7d47b77c4ac7b1769f3ac3b394c32ffebe6e.zip | |
generated docs: refactor how type kinds work
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/special/docs/main.js | 151 |
1 files changed, 47 insertions, 104 deletions
diff --git a/lib/std/special/docs/main.js b/lib/std/special/docs/main.js index 84d44f7b5b..8d3bdcb172 100644 --- a/lib/std/special/docs/main.js +++ b/lib/std/special/docs/main.js @@ -39,20 +39,8 @@ var searchTimer = null; var escapeHtmlReplacements = { "&": "&", '"': """, "<": "<", ">": ">" }; - var typeKindTypeId; - var typeKindFnId; - var typeKindPtrId; - var typeKindFloatId; - var typeKindIntId; - var typeKindBoolId; - var typeKindVoidId; - var typeKindNoReturnId; - var typeKindErrSetId; - var typeKindErrUnionId; - var typeKindStructId; - var typeKindUnionId; - var typeKindEnumId; - findTypeKinds(); + var typeKinds = indexTypeKinds(); + var typeTypeId = findTypeTypeId(); // for each package, is an array with packages to get to this one var canonPkgPaths = computeCanonicalPackagePaths(); @@ -77,7 +65,6 @@ var curSearchIndex = -1; var rootIsStd = detectRootIsStd(); - var typeTypeId = findTypeTypeId(); // map of decl index to list of non-generic fn indexes var nodesToFnsMap = indexNodesToFns(); @@ -173,7 +160,7 @@ } if (lastDecl.type != null) { var typeObj = zigAnalysis.types[lastDecl.type]; - if (typeObj.kind === typeKindFnId) { + if (typeObj.kind === typeKinds.Fn) { return renderFn(lastDecl); } throw new Error("docs for this decl which is not a container"); @@ -188,19 +175,19 @@ function typeIsErrSet(typeIndex) { var typeObj = zigAnalysis.types[typeIndex]; - return typeObj.kind === typeKindErrSetId; + return typeObj.kind === typeKinds.ErrorSet; } function typeIsStructWithNoFields(typeIndex) { var typeObj = zigAnalysis.types[typeIndex]; - if (typeObj.kind !== typeKindStructId) + if (typeObj.kind !== typeKinds.Struct) return false; return typeObj.fields == null || typeObj.fields.length === 0; } function typeIsGenericFn(typeIndex) { var typeObj = zigAnalysis.types[typeIndex]; - if (typeObj.kind !== typeKindFnId) { + if (typeObj.kind !== typeKinds.Fn) { return false; } return typeObj.generic; @@ -219,9 +206,9 @@ var errSetTypeIndex = null; if (typeObj.ret != null) { var retType = zigAnalysis.types[typeObj.ret]; - if (retType.kind === typeKindErrSetId) { + if (retType.kind === typeKinds.ErrorSet) { errSetTypeIndex = typeObj.ret; - } else if (retType.kind === typeKindErrUnionId) { + } else if (retType.kind === typeKinds.Union) { errSetTypeIndex = retType.err; } } @@ -404,7 +391,7 @@ function typeName(typeObj, wantHtml, wantSubLink, fnDecl, skipFnName) { switch (typeObj.kind) { - case typeKindPtrId: + case typeKinds.Pointer: var name = ""; switch (typeObj.len) { case 0: @@ -464,13 +451,13 @@ } name += typeIndexName(typeObj.elem, wantHtml, wantSubLink, null); return name; - case typeKindFloatId: + case typeKinds.Float: if (wantHtml) { return '<span class="tok-type">f' + typeObj.bits + '</span>'; } else { return "f" + typeObj.bits; } - case typeKindIntId: + case typeKinds.Int: var signed = (typeObj.i != null) ? 'i' : 'u'; var bits = typeObj[signed]; if (wantHtml) { @@ -478,31 +465,43 @@ } else { return signed + bits; } - case typeKindTypeId: + case typeKinds.ComptimeInt: + if (wantHtml) { + return '<span class="tok-type">comptime_int</span>'; + } else { + return "comptime_int"; + } + case typeKinds.ComptimeFloat: + if (wantHtml) { + return '<span class="tok-type">comptime_float</span>'; + } else { + return "comptime_float"; + } + case typeKinds.Type: if (wantHtml) { return '<span class="tok-type">type</span>'; } else { return "type"; } - case typeKindBoolId: + case typeKinds.Bool: if (wantHtml) { return '<span class="tok-type">bool</span>'; } else { return "bool"; } - case typeKindVoidId: + case typeKinds.Void: if (wantHtml) { return '<span class="tok-type">void</span>'; } else { return "void"; } - case typeKindNoReturnId: + case typeKinds.NoReturn: if (wantHtml) { return '<span class="tok-type">noreturn</span>'; } else { return "noreturn"; } - case typeKindErrSetId: + case typeKinds.ErrorSet: if (typeObj.errors == null) { if (wantHtml) { return '<span class="tok-type">anyerror</span>'; @@ -516,7 +515,7 @@ return typeObj.name; } } - case typeKindErrUnionId: + case typeKinds.Union: var errSetTypeObj = zigAnalysis.types[typeObj.err]; var payloadHtml = typeIndexName(typeObj.payload, wantHtml, wantSubLink, null); if (fnDecl != null && errSetTypeObj.fn === fnDecl.value) { @@ -525,7 +524,7 @@ } else { return typeIndexName(typeObj.err, wantHtml, wantSubLink, null) + "!" + payloadHtml; } - case typeKindFnId: + case typeKinds.Fn: var payloadHtml = ""; if (wantHtml) { payloadHtml += '<span class="tok-kw">fn</span>'; @@ -576,7 +575,7 @@ domHdrName.innerText = name + " (" + zigAnalysis.typeKinds[typeObj.kind] + ")"; domHdrName.classList.remove("hidden"); } - if (typeObj.kind == typeKindErrSetId) { + if (typeObj.kind == typeKinds.ErrorSet) { renderErrorSet(typeObj); } } @@ -662,7 +661,7 @@ } } else { var typeKind = zigAnalysis.types[decl.type].kind; - if (typeKind === typeKindFnId) { + if (typeKind === typeKinds.Fn) { if (allCompTimeFnCallsHaveTypeResult(decl.type, decl.value)) { typesList.push(decl); } else { @@ -817,80 +816,24 @@ return rootPkg.file === stdPkg.file; } - function findTypeKinds() { + function indexTypeKinds() { + var map = {}; for (var i = 0; i < zigAnalysis.typeKinds.length; i += 1) { - if (zigAnalysis.typeKinds[i] === "Type") { - typeKindTypeId = i; - } else if (zigAnalysis.typeKinds[i] === "Fn") { - typeKindFnId = i; - } else if (zigAnalysis.typeKinds[i] === "Pointer") { - typeKindPtrId = i; - } else if (zigAnalysis.typeKinds[i] === "Float") { - typeKindFloatId = i; - } else if (zigAnalysis.typeKinds[i] === "Int") { - typeKindIntId = i; - } else if (zigAnalysis.typeKinds[i] === "Bool") { - typeKindBoolId = i; - } else if (zigAnalysis.typeKinds[i] === "Void") { - typeKindVoidId = i; - } else if (zigAnalysis.typeKinds[i] === "NoReturn") { - typeKindNoReturnId = i; - } else if (zigAnalysis.typeKinds[i] === "ErrorSet") { - typeKindErrSetId = i; - } else if (zigAnalysis.typeKinds[i] === "ErrorUnion") { - typeKindErrUnionId = i; - } else if (zigAnalysis.typeKinds[i] === "Struct") { - typeKindStructId = i; - } else if (zigAnalysis.typeKinds[i] === "Union") { - typeKindUnionId = i; - } else if (zigAnalysis.typeKinds[i] === "Enum") { - typeKindEnumId = i; - } - } - if (typeKindTypeId == null) { - throw new Error("No type kind 'Type' found"); - } - if (typeKindFnId == null) { - throw new Error("No type kind 'Fn' found"); + map[zigAnalysis.typeKinds[i]] = i; } - if (typeKindPtrId == null) { - throw new Error("No type kind 'Pointer' found"); - } - if (typeKindFloatId == null) { - throw new Error("No type kind 'Float' found"); - } - if (typeKindIntId == null) { - throw new Error("No type kind 'Int' found"); - } - if (typeKindBoolId == null) { - throw new Error("No type kind 'Bool' found"); - } - if (typeKindVoidId == null) { - throw new Error("No type kind 'Void' found"); - } - if (typeKindNoReturnId == null) { - throw new Error("No type kind 'Void' found"); - } - if (typeKindErrSetId == null) { - throw new Error("No type kind 'ErrorSet' found"); - } - if (typeKindErrUnionId == null) { - throw new Error("No type kind 'ErrorUnion' found"); - } - if (typeKindStructId == null) { - throw new Error("No type kind 'Struct' found"); - } - if (typeKindUnionId == null) { - throw new Error("No type kind 'Union' found"); - } - if (typeKindEnumId == null) { - throw new Error("No type kind 'Enum' found"); + // This is just for debugging purposes, not needed to function + var assertList = ["Type","Void","Bool","NoReturn","Int","Float","Pointer","Array","Struct", + "ComptimeFloat","ComptimeInt","Undefined","Null","Optional","ErrorUnion","ErrorSet","Enum", + "Union","Fn","BoundFn","ArgTuple","Opaque","Frame","AnyFrame","Vector","EnumLiteral"]; + for (var i = 0; i < assertList.length; i += 1) { + if (map[assertList[i]] == null) throw new Error("No type kind '" + assertList[i] + "' found"); } + return map; } function findTypeTypeId() { for (var i = 0; i < zigAnalysis.types.length; i += 1) { - if (zigAnalysis.types[i].kind == typeKindTypeId) { + if (zigAnalysis.types[i].kind == typeKinds.Type) { return i; } } @@ -976,10 +919,10 @@ } function declCanRepresentTypeKind(typeKind) { - return typeKind === typeKindErrSetId || - typeKind === typeKindStructId || - typeKind === typeKindUnionId || - typeKind === typeKindEnumId; + return typeKind === typeKinds.ErrorSet || + typeKind === typeKinds.Struct || + typeKind === typeKinds.Union || + typeKind === typeKinds.Enum; } function computeCanonDeclPaths() { |
