| ... | ... | @@ -1,3 +1,5 @@ |
| 1 | //'use strict'; |
| 2 | |
| 1 | 3 | (function() { |
| 2 | 4 | var domStatus = document.getElementById("status"); |
| 3 | 5 | var domSectNav = document.getElementById("sectNav"); |
| ... | ... | @@ -101,6 +103,98 @@ |
| 101 | 103 | } |
| 102 | 104 | } |
| 103 | 105 | |
| 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 | 198 | function render() { |
| 105 | 199 | domStatus.classList.add("hidden"); |
| 106 | 200 | domFnProto.classList.add("hidden"); |
| ... | ... | @@ -154,38 +248,43 @@ |
| 154 | 248 | if (childDecl == null) { |
| 155 | 249 | return render404(); |
| 156 | 250 | } |
| 157 | | var container = getDeclContainerType(childDecl); |
| 158 | | if (container == null) { |
| 251 | |
| 252 | var childDeclValue = resolveDeclValue(childDecl); |
| 253 | if ("type" in childDeclValue){ |
| 159 | 254 | if (i + 1 === curNav.declNames.length) { |
| 160 | | curNav.declObjs.push(childDecl); |
| 255 | curNav.declObjs.push(zigAnalysis.types[childDeclValue.type]); |
| 161 | 256 | break; |
| 162 | 257 | } else { |
| 163 | 258 | return render404(); |
| 164 | 259 | } |
| 165 | 260 | } |
| 166 | | currentType = container; |
| 261 | currentType = childDecl; |
| 167 | 262 | curNav.declObjs.push(currentType); |
| 168 | 263 | } |
| 169 | 264 | |
| 170 | 265 | renderNav(); |
| 171 | 266 | |
| 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); |
| 175 | 274 | } |
| 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)]; |
| 182 | 281 | if (typeObj.kind === typeKinds.Fn) { |
| 183 | | return renderFn(lastDeclOrType); |
| 282 | return renderFn(last); |
| 184 | 283 | } else { |
| 185 | | return renderValue(lastDeclOrType); |
| 284 | return renderValue(last); |
| 186 | 285 | } |
| 187 | 286 | } else { |
| 188 | | renderType(lastDeclOrType); |
| 287 | renderType(last); |
| 189 | 288 | } |
| 190 | 289 | } |
| 191 | 290 | |
| ... | ... | @@ -210,7 +309,7 @@ |
| 210 | 309 | var typeObj = zigAnalysis.types[typeIndex]; |
| 211 | 310 | if (typeObj.kind !== typeKinds.Struct) |
| 212 | 311 | return false; |
| 213 | | return typeObj.fields == null || typeObj.fields.length === 0; |
| 312 | return !typeObj.fields; |
| 214 | 313 | } |
| 215 | 314 | |
| 216 | 315 | function typeIsGenericFn(typeIndex) { |
| ... | ... | @@ -642,14 +741,16 @@ |
| 642 | 741 | return "f" + typeObj.bits; |
| 643 | 742 | } |
| 644 | 743 | 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 | } |
| 653 | 754 | case typeKinds.ComptimeInt: |
| 654 | 755 | if (wantHtml) { |
| 655 | 756 | return '<span class="tok-type">comptime_int</span>'; |
| ... | ... | @@ -960,12 +1061,15 @@ |
| 960 | 1061 | |
| 961 | 1062 | function renderValue(decl) { |
| 962 | 1063 | |
| 963 | | var declTypeId = getDeclValTypeId(decl); |
| 1064 | var declTypeId = resolveDeclValueTypeId(decl); |
| 964 | 1065 | var declValueText = ""; |
| 965 | 1066 | switch(Object.keys(decl.value)[0]) { |
| 966 | 1067 | case "int": |
| 967 | 1068 | declValueText += decl.value.int.value; |
| 968 | 1069 | break; |
| 1070 | case "float": |
| 1071 | declValueText += decl.value.float.value; |
| 1072 | break; |
| 969 | 1073 | default: |
| 970 | 1074 | console.log("TODO: renderValue for ", Object.keys(decl.value)[0]); |
| 971 | 1075 | declValueText += "#TODO#"; |
| ... | ... | @@ -985,7 +1089,7 @@ |
| 985 | 1089 | } |
| 986 | 1090 | |
| 987 | 1091 | function renderVar(decl) { |
| 988 | | var declTypeId = getDeclValTypeId(decl); |
| 1092 | var declTypeId = resolveDeclValueTypeId(decl); |
| 989 | 1093 | domFnProtoCode.innerHTML = '<span class="tok-kw">var</span> ' + |
| 990 | 1094 | escapeHtml(decl.name) + ': ' + typeIndexName(declTypeId, true, true); |
| 991 | 1095 | |
| ... | ... | @@ -1006,27 +1110,28 @@ |
| 1006 | 1110 | var varsList = []; |
| 1007 | 1111 | var valsList = []; |
| 1008 | 1112 | |
| 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 | 1115 | var decl = zigAnalysis.decls[container.pubDecls[i]]; |
| 1011 | | var declValTypeId = getDeclValTypeId(decl); |
| 1116 | var declTypeId = resolveDeclValueTypeId(decl); |
| 1012 | 1117 | |
| 1013 | 1118 | if (decl.kind === 'var') { |
| 1014 | 1119 | varsList.push(decl); |
| 1015 | 1120 | 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)) { |
| 1019 | 1124 | errSetsList.push(decl); |
| 1020 | | } else if (typeIsStructWithNoFields(declValTypeId)) { |
| 1125 | } else if (typeIsStructWithNoFields(declTypeId)) { |
| 1021 | 1126 | namespacesList.push(decl); |
| 1022 | 1127 | } else { |
| 1023 | 1128 | typesList.push(decl); |
| 1024 | 1129 | } |
| 1025 | 1130 | } else { |
| 1026 | | var typeKind = zigAnalysis.types[declValTypeId].kind; |
| 1131 | var typeKind = zigAnalysis.types[declTypeId].kind; |
| 1027 | 1132 | if (typeKind === typeKinds.Fn) { |
| 1028 | 1133 | // TODO: this is broken but I don't understand functions yet |
| 1029 | | if (allCompTimeFnCallsHaveTypeResult(decl.type, declValTypeId)) { |
| 1134 | if (allCompTimeFnCallsHaveTypeResult(decl.type, declTypeId)) { |
| 1030 | 1135 | typesList.push(decl); |
| 1031 | 1136 | } else { |
| 1032 | 1137 | fnsList.push(decl); |
| ... | ... | @@ -1108,7 +1213,7 @@ |
| 1108 | 1213 | domSectFns.classList.remove("hidden"); |
| 1109 | 1214 | } |
| 1110 | 1215 | |
| 1111 | | if (container.fields != null && container.fields.length !== 0) { |
| 1216 | if (container.fields) { |
| 1112 | 1217 | resizeDomList(domListFields, container.fields.length, '<div></div>'); |
| 1113 | 1218 | |
| 1114 | 1219 | var containerNode = zigAnalysis.astNodes[container.src]; |
| ... | ... | @@ -1128,7 +1233,10 @@ |
| 1128 | 1233 | html += '<span class="tok-kw" style="color:red;">#FAILURE#</span>'; |
| 1129 | 1234 | } else if ("declRef" in field) { |
| 1130 | 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 | 1240 | var valTypeName = valType.name; |
| 1133 | 1241 | if (valType.kind === typeKinds.Struct) { |
| 1134 | 1242 | valTypeName = "struct"; |
| ... | ... | @@ -1175,7 +1283,7 @@ |
| 1175 | 1283 | tdNameA.setAttribute('href', navLinkDecl(decl.name)); |
| 1176 | 1284 | tdNameA.textContent = decl.name; |
| 1177 | 1285 | |
| 1178 | | tdType.innerHTML = typeIndexName(getDeclValTypeId(decl), true, true); |
| 1286 | tdType.innerHTML = typeIndexName(resolveDeclValueTypeId(decl), true, true); |
| 1179 | 1287 | |
| 1180 | 1288 | var docs = zigAnalysis.astNodes[decl.src].docs; |
| 1181 | 1289 | if (docs != null) { |
| ... | ... | @@ -1202,7 +1310,7 @@ |
| 1202 | 1310 | tdNameA.setAttribute('href', navLinkDecl(decl.name)); |
| 1203 | 1311 | tdNameA.textContent = decl.name; |
| 1204 | 1312 | |
| 1205 | | tdType.innerHTML = typeIndexName(getDeclValTypeId(decl), true, true); |
| 1313 | tdType.innerHTML = typeIndexName(resolveDeclValueTypeId(decl), true, true); |
| 1206 | 1314 | |
| 1207 | 1315 | var docs = zigAnalysis.astNodes[decl.src].docs; |
| 1208 | 1316 | if (docs != null) { |
| ... | ... | @@ -1304,7 +1412,7 @@ |
| 1304 | 1412 | } |
| 1305 | 1413 | |
| 1306 | 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 | 1416 | for (var i = 0; i < parentType.pubDecls.length; i += 1) { |
| 1309 | 1417 | var declIndex = parentType.pubDecls[i]; |
| 1310 | 1418 | var childDecl = zigAnalysis.decls[declIndex]; |
| ... | ... | @@ -1315,12 +1423,8 @@ |
| 1315 | 1423 | return null; |
| 1316 | 1424 | } |
| 1317 | 1425 | |
| 1318 | | function getDeclContainerType(decl) { |
| 1319 | | if (decl.type === typeTypeId) { |
| 1320 | | return zigAnalysis.types[getDeclValTypeId(decl)]; |
| 1321 | | } |
| 1322 | | return null; |
| 1323 | | } |
| 1426 | |
| 1427 | |
| 1324 | 1428 | |
| 1325 | 1429 | function computeCanonicalPackagePaths() { |
| 1326 | 1430 | var list = new Array(zigAnalysis.packages.length); |
| ... | ... | @@ -1350,37 +1454,6 @@ |
| 1350 | 1454 | return list; |
| 1351 | 1455 | } |
| 1352 | 1456 | |
| 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 | | } |
| 1384 | 1457 | |
| 1385 | 1458 | function computeCanonDeclPaths() { |
| 1386 | 1459 | var list = new Array(zigAnalysis.decls.length); |
| ... | ... | @@ -1397,14 +1470,15 @@ |
| 1397 | 1470 | while (stack.length !== 0) { |
| 1398 | 1471 | var item = stack.shift(); |
| 1399 | 1472 | |
| 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) { |
| 1402 | 1476 | var mainDeclIndex = item.type.pubDecls[declI]; |
| 1403 | 1477 | if (list[mainDeclIndex] != null) continue; |
| 1404 | 1478 | |
| 1405 | 1479 | var decl = zigAnalysis.decls[mainDeclIndex]; |
| 1406 | | var declValTypeId = getDeclValTypeId(decl); |
| 1407 | | if (decl.type === typeTypeId && |
| 1480 | var declValTypeId = resolveDeclValueTypeId(decl); |
| 1481 | if (declValTypeId === typeTypeId && |
| 1408 | 1482 | declCanRepresentTypeKind(zigAnalysis.types[declValTypeId].kind)) |
| 1409 | 1483 | { |
| 1410 | 1484 | canonTypeDecls[declValTypeId] = mainDeclIndex; |
| ... | ... | @@ -1414,11 +1488,12 @@ |
| 1414 | 1488 | pkgNames: pkgNames, |
| 1415 | 1489 | declNames: declNames, |
| 1416 | 1490 | }; |
| 1417 | | var containerType = getDeclContainerType(decl); |
| 1418 | | if (containerType != null) { |
| 1491 | |
| 1492 | var declType = zigAnalysis.types[declValTypeId]; |
| 1493 | if (isContainerType(declType)) { |
| 1419 | 1494 | stack.push({ |
| 1420 | 1495 | declNames: declNames, |
| 1421 | | type: containerType, |
| 1496 | type: declType, |
| 1422 | 1497 | }); |
| 1423 | 1498 | } |
| 1424 | 1499 | } |