authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-08-06 00:24:32+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-08-06 00:24:32+02:00
logab6b53a259180aab9c9fc4c57d981ba9ecd6e2fa
tree25761ee34145125a3d056a9e1b0a859b1f26735c
parent18440cb239755c983f672b4902147c12e819e029
parente02f51762fc9d09caf37c78a0383164ff8f46b81
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12340 from rudedogg/master

autodoc: Run through prettier formatter with default settings

1 files changed, 3100 insertions(+), 2971 deletions(-)

lib/docs/main.js+3100-2971
...@@ -1,3259 +1,3388 @@...@@ -1,3259 +1,3388 @@
1'use strict';1"use strict";
22
3var zigAnalysis;3var zigAnalysis;
44
5(function() {5(function () {
6 let domStatus = (document.getElementById("status"));6 let domStatus = document.getElementById("status");
7 let domSectNav = (document.getElementById("sectNav"));7 let domSectNav = document.getElementById("sectNav");
8 let domListNav = (document.getElementById("listNav"));8 let domListNav = document.getElementById("listNav");
9 let domSectMainPkg = (document.getElementById("sectMainPkg"));9 let domSectMainPkg = document.getElementById("sectMainPkg");
10 let domSectPkgs = (document.getElementById("sectPkgs"));10 let domSectPkgs = document.getElementById("sectPkgs");
11 let domListPkgs = (document.getElementById("listPkgs"));11 let domListPkgs = document.getElementById("listPkgs");
12 let domSectTypes = (document.getElementById("sectTypes"));12 let domSectTypes = document.getElementById("sectTypes");
13 let domListTypes = (document.getElementById("listTypes"));13 let domListTypes = document.getElementById("listTypes");
14 let domSectTests = (document.getElementById("sectTests"));14 let domSectTests = document.getElementById("sectTests");
15 let domListTests = (document.getElementById("listTests"));15 let domListTests = document.getElementById("listTests");
16 let domSectNamespaces = (document.getElementById("sectNamespaces"));16 let domSectNamespaces = document.getElementById("sectNamespaces");
17 let domListNamespaces = (document.getElementById("listNamespaces"));17 let domListNamespaces = document.getElementById("listNamespaces");
18 let domSectErrSets = (document.getElementById("sectErrSets"));18 let domSectErrSets = document.getElementById("sectErrSets");
19 let domListErrSets = (document.getElementById("listErrSets"));19 let domListErrSets = document.getElementById("listErrSets");
20 let domSectFns = (document.getElementById("sectFns"));20 let domSectFns = document.getElementById("sectFns");
21 let domListFns = (document.getElementById("listFns"));21 let domListFns = document.getElementById("listFns");
22 let domSectFields = (document.getElementById("sectFields"));22 let domSectFields = document.getElementById("sectFields");
23 let domListFields = (document.getElementById("listFields"));23 let domListFields = document.getElementById("listFields");
24 let domSectGlobalVars = (document.getElementById("sectGlobalVars"));24 let domSectGlobalVars = document.getElementById("sectGlobalVars");
25 let domListGlobalVars = (document.getElementById("listGlobalVars"));25 let domListGlobalVars = document.getElementById("listGlobalVars");
26 let domSectValues = (document.getElementById("sectValues"));26 let domSectValues = document.getElementById("sectValues");
27 let domListValues = (document.getElementById("listValues"));27 let domListValues = document.getElementById("listValues");
28 let domFnProto = (document.getElementById("fnProto"));28 let domFnProto = document.getElementById("fnProto");
29 let domFnProtoCode = (document.getElementById("fnProtoCode"));29 let domFnProtoCode = document.getElementById("fnProtoCode");
30 let domSectParams = (document.getElementById("sectParams"));30 let domSectParams = document.getElementById("sectParams");
31 let domListParams = (document.getElementById("listParams"));31 let domListParams = document.getElementById("listParams");
32 let domTldDocs = (document.getElementById("tldDocs"));32 let domTldDocs = document.getElementById("tldDocs");
33 let domSectFnErrors = (document.getElementById("sectFnErrors"));33 let domSectFnErrors = document.getElementById("sectFnErrors");
34 let domListFnErrors = (document.getElementById("listFnErrors"));34 let domListFnErrors = document.getElementById("listFnErrors");
35 let domTableFnErrors =(document.getElementById("tableFnErrors"));35 let domTableFnErrors = document.getElementById("tableFnErrors");
36 let domFnErrorsAnyError = (document.getElementById("fnErrorsAnyError"));36 let domFnErrorsAnyError = document.getElementById("fnErrorsAnyError");
37 let domFnExamples = (document.getElementById("fnExamples"));37 let domFnExamples = document.getElementById("fnExamples");
38 // let domListFnExamples = (document.getElementById("listFnExamples"));38 // let domListFnExamples = (document.getElementById("listFnExamples"));
39 let domFnNoExamples = (document.getElementById("fnNoExamples"));39 let domFnNoExamples = document.getElementById("fnNoExamples");
40 let domDeclNoRef = (document.getElementById("declNoRef"));40 let domDeclNoRef = document.getElementById("declNoRef");
41 let domSearch = (document.getElementById("search"));41 let domSearch = document.getElementById("search");
42 let domSectSearchResults = (document.getElementById("sectSearchResults"));42 let domSectSearchResults = document.getElementById("sectSearchResults");
4343
44 let domListSearchResults = (document.getElementById("listSearchResults"));44 let domListSearchResults = document.getElementById("listSearchResults");
45 let domSectSearchNoResults = (document.getElementById("sectSearchNoResults"));45 let domSectSearchNoResults = document.getElementById("sectSearchNoResults");
46 let domSectInfo = (document.getElementById("sectInfo"));46 let domSectInfo = document.getElementById("sectInfo");
47 // let domTdTarget = (document.getElementById("tdTarget"));47 // let domTdTarget = (document.getElementById("tdTarget"));
48 let domPrivDeclsBox = (document.getElementById("privDeclsBox"));48 let domPrivDeclsBox = document.getElementById("privDeclsBox");
49 let domTdZigVer = (document.getElementById("tdZigVer"));49 let domTdZigVer = document.getElementById("tdZigVer");
50 let domHdrName = (document.getElementById("hdrName"));50 let domHdrName = document.getElementById("hdrName");
51 let domHelpModal = (document.getElementById("helpDialog"));51 let domHelpModal = document.getElementById("helpDialog");
5252
53 53 let searchTimer = null;
54 let searchTimer = null;54
5555 let escapeHtmlReplacements = {
56 56 "&": "&amp;",
57 let escapeHtmlReplacements = { "&": "&amp;", '"': "&quot;", "<": "&lt;", ">": "&gt;" };57 '"': "&quot;",
5858 "<": "&lt;",
59 let typeKinds = (indexTypeKinds());59 ">": "&gt;",
60 let typeTypeId = (findTypeTypeId());60 };
61 let pointerSizeEnum = { One: 0, Many: 1, Slice: 2, C: 3 };61
6262 let typeKinds = indexTypeKinds();
63 // for each package, is an array with packages to get to this one63 let typeTypeId = findTypeTypeId();
64 let canonPkgPaths = computeCanonicalPackagePaths();64 let pointerSizeEnum = { One: 0, Many: 1, Slice: 2, C: 3 };
6565
66 66 // for each package, is an array with packages to get to this one
6767 let canonPkgPaths = computeCanonicalPackagePaths();
68 // for each decl, is an array with {declNames, pkgNames} to get to this one68
69 69 // for each decl, is an array with {declNames, pkgNames} to get to this one
70 let canonDeclPaths = null; // lazy; use getCanonDeclPath70
7171 let canonDeclPaths = null; // lazy; use getCanonDeclPath
72 // for each type, is an array with {declNames, pkgNames} to get to this one72
73 73 // for each type, is an array with {declNames, pkgNames} to get to this one
74 let canonTypeDecls = null; // lazy; use getCanonTypeDecl74
7575 let canonTypeDecls = null; // lazy; use getCanonTypeDecl
76 76
7777 let curNav = {
78 78 showPrivDecls: false,
79 let curNav = {79 // each element is a package name, e.g. @import("a") then within there @import("b")
80 showPrivDecls: false,80 // starting implicitly from root package
81 // each element is a package name, e.g. @import("a") then within there @import("b")81 pkgNames: [],
82 // starting implicitly from root package82 // same as above except actual packages, not names
83 pkgNames: [],83 pkgObjs: [],
84 // same as above except actual packages, not names84 // Each element is a decl name, `a.b.c`, a is 0, b is 1, c is 2, etc.
85 pkgObjs: [],85 // empty array means refers to the package itself
86 // Each element is a decl name, `a.b.c`, a is 0, b is 1, c is 2, etc.86 declNames: [],
87 // empty array means refers to the package itself87 // these will be all types, except the last one may be a type or a decl
88 declNames: [],88 declObjs: [],
89 // these will be all types, except the last one may be a type or a decl89
90 declObjs: [],90 // (a, b, c, d) comptime call; result is the value the docs refer to
9191 callName: null,
92 // (a, b, c, d) comptime call; result is the value the docs refer to92 };
93 callName: null,93
94 };94 let curNavSearch = "";
9595 let curSearchIndex = -1;
96 let curNavSearch = "";96 let imFeelingLucky = false;
97 let curSearchIndex = -1;97
98 let imFeelingLucky = false;98 let rootIsStd = detectRootIsStd();
99
100 // map of decl index to list of non-generic fn indexes
101 // let nodesToFnsMap = indexNodesToFns();
102 // map of decl index to list of comptime fn calls
103 // let nodesToCallsMap = indexNodesToCalls();
104
105 domSearch.addEventListener("keydown", onSearchKeyDown, false);
106 domPrivDeclsBox.addEventListener(
107 "change",
108 function () {
109 if (this.checked != curNav.showPrivDecls) {
110 if (
111 this.checked &&
112 location.hash.length > 1 &&
113 location.hash[1] != "*"
114 ) {
115 location.hash = "#*" + location.hash.substring(1);
116 return;
117 }
118 if (
119 !this.checked &&
120 location.hash.length > 1 &&
121 location.hash[1] == "*"
122 ) {
123 location.hash = "#" + location.hash.substring(2);
124 return;
125 }
126 }
127 },
128 false
129 );
130
131 if (location.hash == "") {
132 location.hash = "#root";
133 }
134
135 window.addEventListener("hashchange", onHashChange, false);
136 window.addEventListener("keydown", onWindowKeyDown, false);
137 onHashChange();
138
139 function renderTitle() {
140 let list = curNav.pkgNames.concat(curNav.declNames);
141 let suffix = " - Zig";
142 if (list.length === 0) {
143 if (rootIsStd) {
144 document.title = "std" + suffix;
145 } else {
146 document.title = zigAnalysis.params.rootName + suffix;
147 }
148 } else {
149 document.title = list.join(".") + suffix;
150 }
151 }
99152
100 let rootIsStd = detectRootIsStd();153 function isDecl(x) {
154 return "value" in x;
155 }
101156
102 // map of decl index to list of non-generic fn indexes157 function isType(x) {
103 // let nodesToFnsMap = indexNodesToFns();158 return "kind" in x && !("value" in x);
104 // map of decl index to list of comptime fn calls159 }
105 // let nodesToCallsMap = indexNodesToCalls();
106160
107 domSearch.addEventListener('keydown', onSearchKeyDown, false);161 function isContainerType(x) {
108 domPrivDeclsBox.addEventListener('change', function() {162 return isType(x) && typeKindIsContainer(x.kind);
109 if (this.checked != curNav.showPrivDecls) {163 }
110 if (this.checked && location.hash.length > 1 && location.hash[1] != '*'){
111 location.hash = "#*" + location.hash.substring(1);
112 return;
113 }
114 if (!this.checked && location.hash.length > 1 && location.hash[1] == '*') {
115 location.hash = "#" + location.hash.substring(2);
116 return;
117 }
118 }
119 }, false);
120
121 if (location.hash == "") {
122 location.hash = "#root";
123 }
124
125 window.addEventListener('hashchange', onHashChange, false);
126 window.addEventListener('keydown', onWindowKeyDown, false);
127 onHashChange();
128
129 function renderTitle() {
130 let list = curNav.pkgNames.concat(curNav.declNames);
131 let suffix = " - Zig";
132 if (list.length === 0) {
133 if (rootIsStd) {
134 document.title = "std" + suffix;
135 } else {
136 document.title = zigAnalysis.params.rootName + suffix;
137 }
138 } else {
139 document.title = list.join('.') + suffix;
140 }
141 }
142164
143 165 function typeShorthandName(expr) {
144 function isDecl(x) {166 let resolvedExpr = resolveValue({ expr: expr });
145 return "value" in x;167 if (!("type" in resolvedExpr)) {
168 return null;
146 }169 }
170 let type = zigAnalysis.types[resolvedExpr.type];
171
172 outer: for (let i = 0; i < 10000; i += 1) {
173 switch (type.kind) {
174 case typeKinds.Optional:
175 case typeKinds.Pointer:
176 let child = type.child;
177 let resolvedChild = resolveValue(child);
178 if ("type" in resolvedChild) {
179 type = zigAnalysis.types[resolvedChild.type];
180 continue;
181 } else {
182 return null;
183 }
184 default:
185 break outer;
186 }
147187
148 188 if (i == 9999) throw "Exhausted typeShorthandName quota";
149 function isType(x) {
150 return "kind" in x && !("value" in x);
151 }189 }
152190
153 191 let name = undefined;
154 function isContainerType(x) {192 if (type.kind === typeKinds.Struct) {
155 return isType(x) && typeKindIsContainer((x).kind) ;193 name = "struct";
194 } else if (type.kind === typeKinds.Enum) {
195 name = "enum";
196 } else if (type.kind === typeKinds.Union) {
197 name = "union";
198 } else {
199 console.log("TODO: unhalndled case in typeShortName");
200 return null;
156 }201 }
157202
158 203 return escapeHtml(name);
159 function typeShorthandName(expr) {204 }
160 let resolvedExpr = resolveValue({expr: expr});205
161 if (!("type" in resolvedExpr)) {206 function typeKindIsContainer(typeKind) {
162 return null;207 return (
163 }208 typeKind === typeKinds.Struct ||
164 let type = (zigAnalysis.types[resolvedExpr.type]);209 typeKind === typeKinds.Union ||
165210 typeKind === typeKinds.Enum
166 outer: for (let i = 0; i < 10000; i += 1) {211 );
167 switch (type.kind) {212 }
168 case typeKinds.Optional:213
169 case typeKinds.Pointer:214 function declCanRepresentTypeKind(typeKind) {
170 let child = (type).child;215 return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);
171 let resolvedChild = resolveValue(child);216 }
172 if ("type" in resolvedChild) {217
173 type = zigAnalysis.types[resolvedChild.type];218 //
174 continue;219 // function findCteInRefPath(path) {
175 } else {220 // for (let i = path.length - 1; i >= 0; i -= 1) {
176 return null;221 // const ref = path[i];
177 }222 // if ("string" in ref) continue;
178 default:223 // if ("comptimeExpr" in ref) return ref;
179 break outer;224 // if ("refPath" in ref) return findCteInRefPath(ref.refPath);
180 }225 // return null;
181226 // }
182 if (i == 9999) throw "Exhausted typeShorthandName quota";227
183 }228 // return null;
184229 // }
230
231 function resolveValue(value) {
232 let i = 0;
233 while (i < 1000) {
234 i += 1;
235
236 if ("refPath" in value.expr) {
237 value = { expr: value.expr.refPath[value.expr.refPath.length - 1] };
238 continue;
239 }
185240
241 if ("declRef" in value.expr) {
242 value = zigAnalysis.decls[value.expr.declRef].value;
243 continue;
244 }
186245
187 let name = undefined;246 if ("as" in value.expr) {
188 if (type.kind === typeKinds.Struct) {247 value = {
189 name = "struct";248 typeRef: zigAnalysis.exprs[value.expr.as.typeRefArg],
190 } else if (type.kind === typeKinds.Enum) {249 expr: zigAnalysis.exprs[value.expr.as.exprArg],
191 name = "enum";250 };
192 } else if (type.kind === typeKinds.Union) {251 continue;
193 name = "union";252 }
194 } else {
195 console.log("TODO: unhalndled case in typeShortName");
196 return null;
197 }
198253
199 return escapeHtml(name);254 return value;
200 }255 }
201256 console.assert(false);
202 257 return {};
203 function typeKindIsContainer(typeKind) {258 }
204 return typeKind === typeKinds.Struct ||259
205 typeKind === typeKinds.Union ||260 // function typeOfDecl(decl){
206 typeKind === typeKinds.Enum;261 // return decl.value.typeRef;
262 //
263 // let i = 0;
264 // while(i < 1000) {
265 // i += 1;
266 // console.assert(isDecl(decl));
267 // if ("type" in decl.value) {
268 // return ({ type: typeTypeId });
269 // }
270 //
271 //// if ("string" in decl.value) {
272 //// return ({ type: {
273 //// kind: typeKinds.Pointer,
274 //// size: pointerSizeEnum.One,
275 //// child: });
276 //// }
277 //
278 // if ("refPath" in decl.value) {
279 // decl = ({
280 // value: decl.value.refPath[decl.value.refPath.length -1]
281 // });
282 // continue;
283 // }
284 //
285 // if ("declRef" in decl.value) {
286 // decl = zigAnalysis.decls[decl.value.declRef];
287 // continue;
288 // }
289 //
290 // if ("int" in decl.value) {
291 // return decl.value.int.typeRef;
292 // }
293 //
294 // if ("float" in decl.value) {
295 // return decl.value.float.typeRef;
296 // }
297 //
298 // if ("array" in decl.value) {
299 // return decl.value.array.typeRef;
300 // }
301 //
302 // if ("struct" in decl.value) {
303 // return decl.value.struct.typeRef;
304 // }
305 //
306 // if ("comptimeExpr" in decl.value) {
307 // const cte = zigAnalysis.comptimeExprs[decl.value.comptimeExpr];
308 // return cte.typeRef;
309 // }
310 //
311 // if ("call" in decl.value) {
312 // const fn_call = zigAnalysis.calls[decl.value.call];
313 // let fn_decl = undefined;
314 // if ("declRef" in fn_call.func) {
315 // fn_decl = zigAnalysis.decls[fn_call.func.declRef];
316 // } else if ("refPath" in fn_call.func) {
317 // console.assert("declRef" in fn_call.func.refPath[fn_call.func.refPath.length -1]);
318 // fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef];
319 // } else throw {};
320 //
321 // const fn_decl_value = resolveValue(fn_decl.value);
322 // console.assert("type" in fn_decl_value); //TODO handle comptimeExpr
323 // const fn_type = (zigAnalysis.types[fn_decl_value.type]);
324 // console.assert(fn_type.kind === typeKinds.Fn);
325 // return fn_type.ret;
326 // }
327 //
328 // if ("void" in decl.value) {
329 // return ({ type: typeTypeId });
330 // }
331 //
332 // if ("bool" in decl.value) {
333 // return ({ type: typeKinds.Bool });
334 // }
335 //
336 // console.log("TODO: handle in `typeOfDecl` more cases: ", decl);
337 // console.assert(false);
338 // throw {};
339 // }
340 // console.assert(false);
341 // return ({});
342 // }
343
344 function render() {
345 domStatus.classList.add("hidden");
346 domFnProto.classList.add("hidden");
347 domSectParams.classList.add("hidden");
348 domTldDocs.classList.add("hidden");
349 domSectMainPkg.classList.add("hidden");
350 domSectPkgs.classList.add("hidden");
351 domSectTypes.classList.add("hidden");
352 domSectTests.classList.add("hidden");
353 domSectNamespaces.classList.add("hidden");
354 domSectErrSets.classList.add("hidden");
355 domSectFns.classList.add("hidden");
356 domSectFields.classList.add("hidden");
357 domSectSearchResults.classList.add("hidden");
358 domSectSearchNoResults.classList.add("hidden");
359 domSectInfo.classList.add("hidden");
360 domHdrName.classList.add("hidden");
361 domSectNav.classList.add("hidden");
362 domSectFnErrors.classList.add("hidden");
363 domFnExamples.classList.add("hidden");
364 domFnNoExamples.classList.add("hidden");
365 domDeclNoRef.classList.add("hidden");
366 domFnErrorsAnyError.classList.add("hidden");
367 domTableFnErrors.classList.add("hidden");
368 domSectGlobalVars.classList.add("hidden");
369 domSectValues.classList.add("hidden");
370
371 renderTitle();
372 renderInfo();
373 renderPkgList();
374
375 domPrivDeclsBox.checked = curNav.showPrivDecls;
376
377 if (curNavSearch !== "") {
378 return renderSearch();
207 }379 }
208380
209 381 let rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];
210 function declCanRepresentTypeKind(typeKind) {382 let pkg = rootPkg;
211 return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);383 curNav.pkgObjs = [pkg];
384 for (let i = 0; i < curNav.pkgNames.length; i += 1) {
385 let childPkg = zigAnalysis.packages[pkg.table[curNav.pkgNames[i]]];
386 if (childPkg == null) {
387 return render404();
388 }
389 pkg = childPkg;
390 curNav.pkgObjs.push(pkg);
212 }391 }
213392
214 // 393 let currentType = zigAnalysis.types[pkg.main];
215 // function findCteInRefPath(path) {394 curNav.declObjs = [currentType];
216 // for (let i = path.length - 1; i >= 0; i -= 1) {395 for (let i = 0; i < curNav.declNames.length; i += 1) {
217 // const ref = path[i];396 let childDecl = findSubDecl(currentType, curNav.declNames[i]);
218 // if ("string" in ref) continue;397 if (childDecl == null) {
219 // if ("comptimeExpr" in ref) return ref;398 return render404();
220 // if ("refPath" in ref) return findCteInRefPath(ref.refPath);399 }
221 // return null;
222 // }
223
224 // return null;
225 // }
226
227
228 function resolveValue(value) {
229 let i = 0;
230 while(i < 1000) {
231 i += 1;
232
233 if ("refPath" in value.expr) {
234 value = {expr: value.expr.refPath[value.expr.refPath.length -1]};
235 continue;
236 }
237
238 if ("declRef" in value.expr) {
239 value = zigAnalysis.decls[value.expr.declRef].value;
240 continue;
241 }
242
243 if ("as" in value.expr) {
244 value = {
245 typeRef: zigAnalysis.exprs[value.expr.as.typeRefArg],
246 expr: zigAnalysis.exprs[value.expr.as.exprArg],
247 };
248 continue;
249 }
250
251 return value;
252
253 }
254 console.assert(false);
255 return ({});
256 }
257
258
259// function typeOfDecl(decl){
260// return decl.value.typeRef;
261//
262// let i = 0;
263// while(i < 1000) {
264// i += 1;
265// console.assert(isDecl(decl));
266// if ("type" in decl.value) {
267// return ({ type: typeTypeId });
268// }
269//
270//// if ("string" in decl.value) {
271//// return ({ type: {
272//// kind: typeKinds.Pointer,
273//// size: pointerSizeEnum.One,
274//// child: });
275//// }
276//
277// if ("refPath" in decl.value) {
278// decl = ({
279// value: decl.value.refPath[decl.value.refPath.length -1]
280// });
281// continue;
282// }
283//
284// if ("declRef" in decl.value) {
285// decl = zigAnalysis.decls[decl.value.declRef];
286// continue;
287// }
288//
289// if ("int" in decl.value) {
290// return decl.value.int.typeRef;
291// }
292//
293// if ("float" in decl.value) {
294// return decl.value.float.typeRef;
295// }
296//
297// if ("array" in decl.value) {
298// return decl.value.array.typeRef;
299// }
300//
301// if ("struct" in decl.value) {
302// return decl.value.struct.typeRef;
303// }
304//
305// if ("comptimeExpr" in decl.value) {
306// const cte = zigAnalysis.comptimeExprs[decl.value.comptimeExpr];
307// return cte.typeRef;
308// }
309//
310// if ("call" in decl.value) {
311// const fn_call = zigAnalysis.calls[decl.value.call];
312// let fn_decl = undefined;
313// if ("declRef" in fn_call.func) {
314// fn_decl = zigAnalysis.decls[fn_call.func.declRef];
315// } else if ("refPath" in fn_call.func) {
316// console.assert("declRef" in fn_call.func.refPath[fn_call.func.refPath.length -1]);
317// fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef];
318// } else throw {};
319//
320// const fn_decl_value = resolveValue(fn_decl.value);
321// console.assert("type" in fn_decl_value); //TODO handle comptimeExpr
322// const fn_type = (zigAnalysis.types[fn_decl_value.type]);
323// console.assert(fn_type.kind === typeKinds.Fn);
324// return fn_type.ret;
325// }
326//
327// if ("void" in decl.value) {
328// return ({ type: typeTypeId });
329// }
330//
331// if ("bool" in decl.value) {
332// return ({ type: typeKinds.Bool });
333// }
334//
335// console.log("TODO: handle in `typeOfDecl` more cases: ", decl);
336// console.assert(false);
337// throw {};
338// }
339// console.assert(false);
340// return ({});
341// }
342
343 function render() {
344 domStatus.classList.add("hidden");
345 domFnProto.classList.add("hidden");
346 domSectParams.classList.add("hidden");
347 domTldDocs.classList.add("hidden");
348 domSectMainPkg.classList.add("hidden");
349 domSectPkgs.classList.add("hidden");
350 domSectTypes.classList.add("hidden");
351 domSectTests.classList.add("hidden");
352 domSectNamespaces.classList.add("hidden");
353 domSectErrSets.classList.add("hidden");
354 domSectFns.classList.add("hidden");
355 domSectFields.classList.add("hidden");
356 domSectSearchResults.classList.add("hidden");
357 domSectSearchNoResults.classList.add("hidden");
358 domSectInfo.classList.add("hidden");
359 domHdrName.classList.add("hidden");
360 domSectNav.classList.add("hidden");
361 domSectFnErrors.classList.add("hidden");
362 domFnExamples.classList.add("hidden");
363 domFnNoExamples.classList.add("hidden");
364 domDeclNoRef.classList.add("hidden");
365 domFnErrorsAnyError.classList.add("hidden");
366 domTableFnErrors.classList.add("hidden");
367 domSectGlobalVars.classList.add("hidden");
368 domSectValues.classList.add("hidden");
369
370 renderTitle();
371 renderInfo();
372 renderPkgList();
373
374 domPrivDeclsBox.checked = curNav.showPrivDecls;
375
376 if (curNavSearch !== "") {
377 return renderSearch();
378 }
379
380 let rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];
381 let pkg = rootPkg;
382 curNav.pkgObjs = [pkg];
383 for (let i = 0; i < curNav.pkgNames.length; i += 1) {
384 let childPkg = zigAnalysis.packages[pkg.table[curNav.pkgNames[i]]];
385 if (childPkg == null) {
386 return render404();
387 }
388 pkg = childPkg;
389 curNav.pkgObjs.push(pkg);
390 }
391
392
393 let currentType = zigAnalysis.types[pkg.main];
394 curNav.declObjs = [currentType];
395 for (let i = 0; i < curNav.declNames.length; i += 1) {
396
397
398 let childDecl = findSubDecl((currentType), curNav.declNames[i]);
399 if (childDecl == null) {
400 return render404();
401 }
402
403 let childDeclValue = resolveValue((childDecl).value).expr;
404 if ("type" in childDeclValue) {
405
406 const t = zigAnalysis.types[childDeclValue.type];
407 if (t.kind != typeKinds.Fn) {
408 childDecl = t;
409 }
410 }
411
412 currentType = (childDecl);
413 curNav.declObjs.push(currentType);
414 }
415
416 renderNav();
417
418 let last = curNav.declObjs[curNav.declObjs.length - 1];
419 let lastIsDecl = isDecl(last);
420 let lastIsType = isType(last);
421 let lastIsContainerType = isContainerType(last);
422
423 if (lastIsContainerType) {
424 return renderContainer((last));
425 }
426
427 if (!lastIsDecl && !lastIsType) {
428 return renderUnknownDecl((last));
429 }
430
431 if (lastIsType) {
432 return renderType((last));
433 }
434400
435 if (lastIsDecl && last.kind === 'var') {401 let childDeclValue = resolveValue(childDecl.value).expr;
436 return renderVar((last));402 if ("type" in childDeclValue) {
403 const t = zigAnalysis.types[childDeclValue.type];
404 if (t.kind != typeKinds.Fn) {
405 childDecl = t;
437 }406 }
407 }
438408
439 if (lastIsDecl && last.kind === 'const') {409 currentType = childDecl;
440 let typeObj = zigAnalysis.types[resolveValue((last).value).expr.type];410 curNav.declObjs.push(currentType);
441 if (typeObj && typeObj.kind === typeKinds.Fn) {
442 return renderFn((last));
443 }
444
445 return renderValue((last));
446 }
447 }411 }
448412
449 413 renderNav();
450 function renderUnknownDecl(decl) {
451 domDeclNoRef.classList.remove("hidden");
452414
453 let docs = zigAnalysis.astNodes[decl.src].docs;415 let last = curNav.declObjs[curNav.declObjs.length - 1];
454 if (docs != null) {416 let lastIsDecl = isDecl(last);
455 domTldDocs.innerHTML = markdown(docs);417 let lastIsType = isType(last);
456 } else {418 let lastIsContainerType = isContainerType(last);
457 domTldDocs.innerHTML = '<p>There are no doc comments for this declaration.</p>';
458 }
459 domTldDocs.classList.remove("hidden");
460 }
461419
462 420 if (lastIsContainerType) {
463 function typeIsErrSet(typeIndex) {421 return renderContainer(last);
464 let typeObj = zigAnalysis.types[typeIndex];
465 return typeObj.kind === typeKinds.ErrorSet;
466 }422 }
467423
468 424 if (!lastIsDecl && !lastIsType) {
469 function typeIsStructWithNoFields(typeIndex) {425 return renderUnknownDecl(last);
470 let typeObj = zigAnalysis.types[typeIndex];
471 if (typeObj.kind !== typeKinds.Struct)
472 return false;
473 return (typeObj).fields.length == 0;
474 }426 }
475427
476 428 if (lastIsType) {
477 function typeIsGenericFn(typeIndex) {429 return renderType(last);
478 let typeObj = zigAnalysis.types[typeIndex];
479 if (typeObj.kind !== typeKinds.Fn) {
480 return false;
481 }
482 return (typeObj).generic_ret != null;
483 }430 }
484431
485 432 if (lastIsDecl && last.kind === "var") {
486 function renderFn(fnDecl) {433 return renderVar(last);
487 if ("refPath" in fnDecl.value.expr) {434 }
488 let last = fnDecl.value.expr.refPath.length - 1;
489 let lastExpr = fnDecl.value.expr.refPath[last];
490 console.assert("declRef" in lastExpr);
491 fnDecl = zigAnalysis.decls[lastExpr.declRef];
492 }
493
494 let value = resolveValue(fnDecl.value);
495 console.assert("type" in value.expr);
496 let typeObj = (zigAnalysis.types[value.expr.type]);
497
498 domFnProtoCode.innerHTML = exprName(value.expr, {
499 wantHtml: true,
500 wantLink: true,
501 fnDecl,
502 });
503
504 let docsSource = null;
505 let srcNode = zigAnalysis.astNodes[fnDecl.src];
506 if (srcNode.docs != null) {
507 docsSource = srcNode.docs;
508 }
509
510 renderFnParamDocs(fnDecl, typeObj);
511
512 let retExpr = resolveValue({expr:typeObj.ret}).expr;
513 if ("type" in retExpr) {
514 let retIndex = retExpr.type;
515 let errSetTypeIndex = (null);
516 let retType = zigAnalysis.types[retIndex];
517 if (retType.kind === typeKinds.ErrorSet) {
518 errSetTypeIndex = retIndex;
519 } else if (retType.kind === typeKinds.ErrorUnion) {
520 errSetTypeIndex = (retType).err.type;
521 }
522 if (errSetTypeIndex != null) {
523 let errSetType = (zigAnalysis.types[errSetTypeIndex]);
524 renderErrorSet(errSetType);
525 }
526 }
527
528 let protoSrcIndex = fnDecl.src;
529 if (typeIsGenericFn(value.expr.type)) {
530 // does the generic_ret contain a container?
531 var resolvedGenericRet = resolveValue({expr: typeObj.generic_ret});
532
533 if ("call" in resolvedGenericRet.expr){
534 let call = zigAnalysis.calls[resolvedGenericRet.expr.call];
535 let resolvedFunc = resolveValue({expr: call.func});
536 if (!("type" in resolvedFunc.expr)) return;
537 let callee = zigAnalysis.types[resolvedFunc.expr.type];
538 if (!callee.generic_ret) return;
539 resolvedGenericRet = resolveValue({expr: callee.generic_ret});
540 }
541435
542 // TODO: see if unwrapping the `as` here is a good idea or not.436 if (lastIsDecl && last.kind === "const") {
543 if ("as" in resolvedGenericRet.expr) {437 let typeObj = zigAnalysis.types[resolveValue(last.value).expr.type];
544 resolvedGenericRet = {438 if (typeObj && typeObj.kind === typeKinds.Fn) {
545 expr: zigAnalysis.exprs[resolvedGenericRet.expr.as.exprArg]439 return renderFn(last);
546 };440 }
547 }
548441
549 if (!("type" in resolvedGenericRet.expr)) return;442 return renderValue(last);
550 const genericType = zigAnalysis.types[resolvedGenericRet.expr.type];443 }
551 if (isContainerType(genericType)) {444 }
552 renderContainer(genericType)
553 }
554445
446 function renderUnknownDecl(decl) {
447 domDeclNoRef.classList.remove("hidden");
555448
449 let docs = zigAnalysis.astNodes[decl.src].docs;
450 if (docs != null) {
451 domTldDocs.innerHTML = markdown(docs);
452 } else {
453 domTldDocs.innerHTML =
454 "<p>There are no doc comments for this declaration.</p>";
455 }
456 domTldDocs.classList.remove("hidden");
457 }
458
459 function typeIsErrSet(typeIndex) {
460 let typeObj = zigAnalysis.types[typeIndex];
461 return typeObj.kind === typeKinds.ErrorSet;
462 }
463
464 function typeIsStructWithNoFields(typeIndex) {
465 let typeObj = zigAnalysis.types[typeIndex];
466 if (typeObj.kind !== typeKinds.Struct) return false;
467 return typeObj.fields.length == 0;
468 }
469
470 function typeIsGenericFn(typeIndex) {
471 let typeObj = zigAnalysis.types[typeIndex];
472 if (typeObj.kind !== typeKinds.Fn) {
473 return false;
474 }
475 return typeObj.generic_ret != null;
476 }
477
478 function renderFn(fnDecl) {
479 if ("refPath" in fnDecl.value.expr) {
480 let last = fnDecl.value.expr.refPath.length - 1;
481 let lastExpr = fnDecl.value.expr.refPath[last];
482 console.assert("declRef" in lastExpr);
483 fnDecl = zigAnalysis.decls[lastExpr.declRef];
484 }
556485
486 let value = resolveValue(fnDecl.value);
487 console.assert("type" in value.expr);
488 let typeObj = zigAnalysis.types[value.expr.type];
557489
490 domFnProtoCode.innerHTML = exprName(value.expr, {
491 wantHtml: true,
492 wantLink: true,
493 fnDecl,
494 });
558495
559 // old code496 let docsSource = null;
560 // let instantiations = nodesToFnsMap[protoSrcIndex];497 let srcNode = zigAnalysis.astNodes[fnDecl.src];
561 // let calls = nodesToCallsMap[protoSrcIndex];498 if (srcNode.docs != null) {
562 // if (instantiations == null && calls == null) {499 docsSource = srcNode.docs;
563 // domFnNoExamples.classList.remove("hidden");500 }
564 // } else if (calls != null) {
565 // // if (fnObj.combined === undefined) fnObj.combined = allCompTimeFnCallsResult(calls);
566 // if (fnObj.combined != null) renderContainer(fnObj.combined);
567501
568 // resizeDomList(domListFnExamples, calls.length, '<li></li>');502 renderFnParamDocs(fnDecl, typeObj);
503
504 let retExpr = resolveValue({ expr: typeObj.ret }).expr;
505 if ("type" in retExpr) {
506 let retIndex = retExpr.type;
507 let errSetTypeIndex = null;
508 let retType = zigAnalysis.types[retIndex];
509 if (retType.kind === typeKinds.ErrorSet) {
510 errSetTypeIndex = retIndex;
511 } else if (retType.kind === typeKinds.ErrorUnion) {
512 errSetTypeIndex = retType.err.type;
513 }
514 if (errSetTypeIndex != null) {
515 let errSetType = zigAnalysis.types[errSetTypeIndex];
516 renderErrorSet(errSetType);
517 }
518 }
569519
570 // for (let callI = 0; callI < calls.length; callI += 1) {520 let protoSrcIndex = fnDecl.src;
571 // let liDom = domListFnExamples.children[callI];521 if (typeIsGenericFn(value.expr.type)) {
572 // liDom.innerHTML = getCallHtml(fnDecl, calls[callI]);522 // does the generic_ret contain a container?
573 // }523 var resolvedGenericRet = resolveValue({ expr: typeObj.generic_ret });
524
525 if ("call" in resolvedGenericRet.expr) {
526 let call = zigAnalysis.calls[resolvedGenericRet.expr.call];
527 let resolvedFunc = resolveValue({ expr: call.func });
528 if (!("type" in resolvedFunc.expr)) return;
529 let callee = zigAnalysis.types[resolvedFunc.expr.type];
530 if (!callee.generic_ret) return;
531 resolvedGenericRet = resolveValue({ expr: callee.generic_ret });
532 }
574533
575 // domFnExamples.classList.remove("hidden");534 // TODO: see if unwrapping the `as` here is a good idea or not.
576 // } else if (instantiations != null) {535 if ("as" in resolvedGenericRet.expr) {
577 // // TODO536 resolvedGenericRet = {
578 // }537 expr: zigAnalysis.exprs[resolvedGenericRet.expr.as.exprArg],
579 } else {538 };
539 }
580540
581 domFnExamples.classList.add("hidden");541 if (!("type" in resolvedGenericRet.expr)) return;
582 domFnNoExamples.classList.add("hidden");542 const genericType = zigAnalysis.types[resolvedGenericRet.expr.type];
583 }543 if (isContainerType(genericType)) {
544 renderContainer(genericType);
545 }
584546
585 let protoSrcNode = zigAnalysis.astNodes[protoSrcIndex];547 // old code
586 if (docsSource == null && protoSrcNode != null && protoSrcNode.docs != null) {548 // let instantiations = nodesToFnsMap[protoSrcIndex];
587 docsSource = protoSrcNode.docs;549 // let calls = nodesToCallsMap[protoSrcIndex];
588 }550 // if (instantiations == null && calls == null) {
589 if (docsSource != null) {551 // domFnNoExamples.classList.remove("hidden");
590 domTldDocs.innerHTML = markdown(docsSource);552 // } else if (calls != null) {
591 domTldDocs.classList.remove("hidden");553 // // if (fnObj.combined === undefined) fnObj.combined = allCompTimeFnCallsResult(calls);
592 }554 // if (fnObj.combined != null) renderContainer(fnObj.combined);
593 domFnProto.classList.remove("hidden");555
556 // resizeDomList(domListFnExamples, calls.length, '<li></li>');
557
558 // for (let callI = 0; callI < calls.length; callI += 1) {
559 // let liDom = domListFnExamples.children[callI];
560 // liDom.innerHTML = getCallHtml(fnDecl, calls[callI]);
561 // }
562
563 // domFnExamples.classList.remove("hidden");
564 // } else if (instantiations != null) {
565 // // TODO
566 // }
567 } else {
568 domFnExamples.classList.add("hidden");
569 domFnNoExamples.classList.add("hidden");
594 }570 }
595571
596 572 let protoSrcNode = zigAnalysis.astNodes[protoSrcIndex];
597 function renderFnParamDocs(fnDecl, typeObj) {573 if (
598 let docCount = 0;574 docsSource == null &&
599575 protoSrcNode != null &&
600 let fnNode = zigAnalysis.astNodes[fnDecl.src];576 protoSrcNode.docs != null
601 let fields = (fnNode.fields);577 ) {
602 let isVarArgs = fnNode.varArgs;578 docsSource = protoSrcNode.docs;
603579 }
604 for (let i = 0; i < fields.length; i += 1) {580 if (docsSource != null) {
605 let field = fields[i];581 domTldDocs.innerHTML = markdown(docsSource);
606 let fieldNode = zigAnalysis.astNodes[field];582 domTldDocs.classList.remove("hidden");
607 if (fieldNode.docs != null) {583 }
608 docCount += 1;584 domFnProto.classList.remove("hidden");
609 }585 }
610 }
611 if (docCount == 0) {
612 return;
613 }
614
615 resizeDomList(domListParams, docCount, '<div></div>');
616 let domIndex = 0;
617586
618 for (let i = 0; i < fields.length; i += 1) {587 function renderFnParamDocs(fnDecl, typeObj) {
619 let field = fields[i];588 let docCount = 0;
620 let fieldNode = zigAnalysis.astNodes[field];
621 let docs = fieldNode.docs;
622 if (fieldNode.docs == null) {
623 continue;
624 }
625 let docsNonEmpty = docs !== "";
626 let divDom = domListParams.children[domIndex];
627 domIndex += 1;
628589
590 let fnNode = zigAnalysis.astNodes[fnDecl.src];
591 let fields = fnNode.fields;
592 let isVarArgs = fnNode.varArgs;
629593
630 let value = typeObj.params[i];594 for (let i = 0; i < fields.length; i += 1) {
631 let preClass = docsNonEmpty ? ' class="fieldHasDocs"' : "";595 let field = fields[i];
632 let html = '<pre' + preClass + '>' + escapeHtml((fieldNode.name)) + ": ";596 let fieldNode = zigAnalysis.astNodes[field];
633 if (isVarArgs && i === typeObj.params.length - 1) {597 if (fieldNode.docs != null) {
634 html += '...';598 docCount += 1;
635 } else {599 }
636 let name = exprName(value, {wantHtml: false, wantLink: false});600 }
637 html += '<span class="tok-kw">' + name + '</span>';601 if (docCount == 0) {
638 }602 return;
603 }
639604
640 html += ',</pre>';605 resizeDomList(domListParams, docCount, "<div></div>");
606 let domIndex = 0;
641607
642 if (docsNonEmpty) {608 for (let i = 0; i < fields.length; i += 1) {
643 html += '<div class="fieldDocs">' + markdown(docs) + '</div>';609 let field = fields[i];
644 }610 let fieldNode = zigAnalysis.astNodes[field];
645 divDom.innerHTML = html;611 let docs = fieldNode.docs;
646 }612 if (fieldNode.docs == null) {
647 domSectParams.classList.remove("hidden");613 continue;
648 }614 }
649615 let docsNonEmpty = docs !== "";
650 function renderNav() {616 let divDom = domListParams.children[domIndex];
651 let len = curNav.pkgNames.length + curNav.declNames.length;617 domIndex += 1;
652 resizeDomList(domListNav, len, '<li><a href="#"></a></li>');618
653 let list = [];619 let value = typeObj.params[i];
654 let hrefPkgNames = [];620 let preClass = docsNonEmpty ? ' class="fieldHasDocs"' : "";
655 let hrefDeclNames = ([]);621 let html = "<pre" + preClass + ">" + escapeHtml(fieldNode.name) + ": ";
656 for (let i = 0; i < curNav.pkgNames.length; i += 1) {622 if (isVarArgs && i === typeObj.params.length - 1) {
657 hrefPkgNames.push(curNav.pkgNames[i]);623 html += "...";
658 let name = curNav.pkgNames[i];624 } else {
659 if (name == "root") name = zigAnalysis.rootPkgName;625 let name = exprName(value, { wantHtml: false, wantLink: false });
660 list.push({626 html += '<span class="tok-kw">' + name + "</span>";
661 name: name,627 }
662 link: navLink(hrefPkgNames, hrefDeclNames),
663 });
664 }
665 for (let i = 0; i < curNav.declNames.length; i += 1) {
666 hrefDeclNames.push(curNav.declNames[i]);
667 list.push({
668 name: curNav.declNames[i],
669 link: navLink(hrefPkgNames, hrefDeclNames),
670 });
671 }
672628
673 for (let i = 0; i < list.length; i += 1) {629 html += ",</pre>";
674 let liDom = domListNav.children[i];
675 let aDom = liDom.children[0];
676 aDom.textContent = list[i].name;
677 aDom.setAttribute('href', list[i].link);
678 if (i + 1 == list.length) {
679 aDom.classList.add("active");
680 } else {
681 aDom.classList.remove("active");
682 }
683 }
684630
685 domSectNav.classList.remove("hidden");631 if (docsNonEmpty) {
632 html += '<div class="fieldDocs">' + markdown(docs) + "</div>";
633 }
634 divDom.innerHTML = html;
686 }635 }
687636 domSectParams.classList.remove("hidden");
688 function renderInfo() {637 }
689 domTdZigVer.textContent = zigAnalysis.params.zigVersion;638
690 //domTdTarget.textContent = zigAnalysis.params.builds[0].target;639 function renderNav() {
691640 let len = curNav.pkgNames.length + curNav.declNames.length;
692 domSectInfo.classList.remove("hidden");641 resizeDomList(domListNav, len, '<li><a href="#"></a></li>');
642 let list = [];
643 let hrefPkgNames = [];
644 let hrefDeclNames = [];
645 for (let i = 0; i < curNav.pkgNames.length; i += 1) {
646 hrefPkgNames.push(curNav.pkgNames[i]);
647 let name = curNav.pkgNames[i];
648 if (name == "root") name = zigAnalysis.rootPkgName;
649 list.push({
650 name: name,
651 link: navLink(hrefPkgNames, hrefDeclNames),
652 });
693 }653 }
694654 for (let i = 0; i < curNav.declNames.length; i += 1) {
695 function render404() {655 hrefDeclNames.push(curNav.declNames[i]);
696 domStatus.textContent = "404 Not Found";656 list.push({
697 domStatus.classList.remove("hidden");657 name: curNav.declNames[i],
658 link: navLink(hrefPkgNames, hrefDeclNames),
659 });
698 }660 }
699661
700 function renderPkgList() {662 for (let i = 0; i < list.length; i += 1) {
701 let rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];663 let liDom = domListNav.children[i];
702 let list = [];664 let aDom = liDom.children[0];
703 for (let key in rootPkg.table) {665 aDom.textContent = list[i].name;
704 let pkgIndex = rootPkg.table[key];666 aDom.setAttribute("href", list[i].link);
705 if (zigAnalysis.packages[pkgIndex] == null) continue;667 if (i + 1 == list.length) {
706 if (key == zigAnalysis.params.rootName) continue;668 aDom.classList.add("active");
707 list.push({669 } else {
708 name: key,670 aDom.classList.remove("active");
709 pkg: pkgIndex,671 }
710 });
711 }
712
713 {
714 let aDom = domSectMainPkg.children[1].children[0].children[0];
715 aDom.textContent = zigAnalysis.rootPkgName;
716 aDom.setAttribute('href', navLinkPkg(zigAnalysis.rootPkg));
717 if (zigAnalysis.params.rootName === curNav.pkgNames[0]) {
718 aDom.classList.add("active");
719 } else {
720 aDom.classList.remove("active");
721 }
722 domSectMainPkg.classList.remove("hidden");
723 }
724
725 list.sort(function(a, b) {
726 return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
727 });
728
729 if (list.length !== 0) {
730 resizeDomList(domListPkgs, list.length, '<li><a href="#"></a></li>');
731 for (let i = 0; i < list.length; i += 1) {
732 let liDom = domListPkgs.children[i];
733 let aDom = liDom.children[0];
734 aDom.textContent = list[i].name;
735 aDom.setAttribute('href', navLinkPkg(list[i].pkg));
736 if (list[i].name === curNav.pkgNames[0]) {
737 aDom.classList.add("active");
738 } else {
739 aDom.classList.remove("active");
740 }
741 }
742
743 domSectPkgs.classList.remove("hidden");
744 }
745 }672 }
746673
747 674 domSectNav.classList.remove("hidden");
675 }
676
677 function renderInfo() {
678 domTdZigVer.textContent = zigAnalysis.params.zigVersion;
679 //domTdTarget.textContent = zigAnalysis.params.builds[0].target;
680
681 domSectInfo.classList.remove("hidden");
682 }
683
684 function render404() {
685 domStatus.textContent = "404 Not Found";
686 domStatus.classList.remove("hidden");
687 }
688
689 function renderPkgList() {
690 let rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];
691 let list = [];
692 for (let key in rootPkg.table) {
693 let pkgIndex = rootPkg.table[key];
694 if (zigAnalysis.packages[pkgIndex] == null) continue;
695 if (key == zigAnalysis.params.rootName) continue;
696 list.push({
697 name: key,
698 pkg: pkgIndex,
699 });
700 }
748701
749 function navLink(pkgNames, declNames, callName) {702 {
750 let base = '#';703 let aDom = domSectMainPkg.children[1].children[0].children[0];
751 if (curNav.showPrivDecls) {704 aDom.textContent = zigAnalysis.rootPkgName;
752 base += "*";705 aDom.setAttribute("href", navLinkPkg(zigAnalysis.rootPkg));
753 }706 if (zigAnalysis.params.rootName === curNav.pkgNames[0]) {
707 aDom.classList.add("active");
708 } else {
709 aDom.classList.remove("active");
710 }
711 domSectMainPkg.classList.remove("hidden");
712 }
754713
755 if (pkgNames.length === 0 && declNames.length === 0) {714 list.sort(function (a, b) {
756 return base;715 return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
757 } else if (declNames.length === 0 && callName == null) {716 });
758 return base + pkgNames.join('.');717
759 } else if (callName == null) {718 if (list.length !== 0) {
760 return base + pkgNames.join('.') + ';' + declNames.join('.');719 resizeDomList(domListPkgs, list.length, '<li><a href="#"></a></li>');
720 for (let i = 0; i < list.length; i += 1) {
721 let liDom = domListPkgs.children[i];
722 let aDom = liDom.children[0];
723 aDom.textContent = list[i].name;
724 aDom.setAttribute("href", navLinkPkg(list[i].pkg));
725 if (list[i].name === curNav.pkgNames[0]) {
726 aDom.classList.add("active");
761 } else {727 } else {
762 return base + pkgNames.join('.') + ';' + declNames.join('.') + ';' + callName;728 aDom.classList.remove("active");
763 }729 }
764 }730 }
765731
766 732 domSectPkgs.classList.remove("hidden");
767 function navLinkPkg(pkgIndex) {
768 return navLink(canonPkgPaths[pkgIndex], []);
769 }733 }
734 }
770735
771 736 function navLink(pkgNames, declNames, callName) {
772 function navLinkDecl(childName) {737 let base = "#";
773 return navLink(curNav.pkgNames, curNav.declNames.concat([childName]));738 if (curNav.showPrivDecls) {
739 base += "*";
774 }740 }
775741
776 // 742 if (pkgNames.length === 0 && declNames.length === 0) {
777 // function navLinkCall(callObj) {743 return base;
778 // let declNamesCopy = curNav.declNames.concat([]);744 } else if (declNames.length === 0 && callName == null) {
779 // let callName = (declNamesCopy.pop());745 return base + pkgNames.join(".");
780746 } else if (callName == null) {
781 // callName += '(';747 return base + pkgNames.join(".") + ";" + declNames.join(".");
782 // for (let arg_i = 0; arg_i < callObj.args.length; arg_i += 1) {748 } else {
783 // if (arg_i !== 0) callName += ',';749 return (
784 // let argObj = callObj.args[arg_i];750 base + pkgNames.join(".") + ";" + declNames.join(".") + ";" + callName
785 // callName += getValueText(argObj, argObj, false, false);751 );
786 // }752 }
787 // callName += ')';753 }
788754
789 // declNamesCopy.push(callName);755 function navLinkPkg(pkgIndex) {
790 // return navLink(curNav.pkgNames, declNamesCopy);756 return navLink(canonPkgPaths[pkgIndex], []);
791 // }757 }
758
759 function navLinkDecl(childName) {
760 return navLink(curNav.pkgNames, curNav.declNames.concat([childName]));
761 }
762
763 //
764 // function navLinkCall(callObj) {
765 // let declNamesCopy = curNav.declNames.concat([]);
766 // let callName = (declNamesCopy.pop());
767
768 // callName += '(';
769 // for (let arg_i = 0; arg_i < callObj.args.length; arg_i += 1) {
770 // if (arg_i !== 0) callName += ',';
771 // let argObj = callObj.args[arg_i];
772 // callName += getValueText(argObj, argObj, false, false);
773 // }
774 // callName += ')';
775
776 // declNamesCopy.push(callName);
777 // return navLink(curNav.pkgNames, declNamesCopy);
778 // }
779
780 function resizeDomListDl(dlDom, desiredLen) {
781 // add the missing dom entries
782 for (let i = dlDom.childElementCount / 2; i < desiredLen; i += 1) {
783 dlDom.insertAdjacentHTML("beforeend", "<dt></dt><dd></dd>");
784 }
785 // remove extra dom entries
786 while (desiredLen < dlDom.childElementCount / 2) {
787 dlDom.removeChild(dlDom.lastChild);
788 dlDom.removeChild(dlDom.lastChild);
789 }
790 }
792791
793 792 function resizeDomList(listDom, desiredLen, templateHtml) {
794 function resizeDomListDl(dlDom, desiredLen) {793 // add the missing dom entries
795 // add the missing dom entries794 for (let i = listDom.childElementCount; i < desiredLen; i += 1) {
796 for (let i = dlDom.childElementCount / 2; i < desiredLen; i += 1) {795 listDom.insertAdjacentHTML("beforeend", templateHtml);
797 dlDom.insertAdjacentHTML('beforeend', '<dt></dt><dd></dd>');796 }
798 }797 // remove extra dom entries
799 // remove extra dom entries798 while (desiredLen < listDom.childElementCount) {
800 while (desiredLen < dlDom.childElementCount / 2) {799 listDom.removeChild(listDom.lastChild);
801 dlDom.removeChild(dlDom.lastChild);
802 dlDom.removeChild(dlDom.lastChild);
803 }
804 }800 }
801 }
805802
806 803 function walkResultTypeRef(wr) {
807 function resizeDomList(listDom, desiredLen, templateHtml) {804 if (wr.typeRef) return wr.typeRef;
808 // add the missing dom entries805 let resolved = resolveValue(wr);
809 for (let i = listDom.childElementCount; i < desiredLen; i += 1) {806 if (wr === resolved) {
810 listDom.insertAdjacentHTML('beforeend', templateHtml);807 return { type: 0 };
808 }
809 return walkResultTypeRef(resolved);
810 }
811
812 function exprName(expr, opts) {
813 switch (Object.keys(expr)[0]) {
814 default:
815 throw "this expression is not implemented yet";
816 case "bool": {
817 if (expr.bool) {
818 return "true";
819 }
820 return "false";
821 }
822 case "&": {
823 return "&" + exprName(zigAnalysis.exprs[expr["&"]]);
824 }
825 case "compileError": {
826 let compileError = expr.compileError;
827 return compileError;
828 }
829 case "enumLiteral": {
830 let literal = expr.enumLiteral;
831 return "." + literal;
832 }
833 case "void": {
834 return "void";
835 }
836 case "slice": {
837 let payloadHtml = "";
838 const lhsExpr = zigAnalysis.exprs[expr.slice.lhs];
839 const startExpr = zigAnalysis.exprs[expr.slice.start];
840 let decl = exprName(lhsExpr);
841 let start = exprName(startExpr);
842 let end = "";
843 let sentinel = "";
844 if (expr.slice["end"]) {
845 const endExpr = zigAnalysis.exprs[expr.slice.end];
846 let end_ = exprName(endExpr);
847 end += end_;
848 }
849 if (expr.slice["sentinel"]) {
850 const sentinelExpr = zigAnalysis.exprs[expr.slice.sentinel];
851 let sentinel_ = exprName(sentinelExpr);
852 sentinel += " :" + sentinel_;
853 }
854 payloadHtml += decl + "[" + start + ".." + end + sentinel + "]";
855 return payloadHtml;
856 }
857 case "sliceIndex": {
858 const sliceIndex = zigAnalysis.exprs[expr.sliceIndex];
859 return exprName(sliceIndex, opts);
860 }
861 case "cmpxchg": {
862 const typeIndex = zigAnalysis.exprs[expr.cmpxchg.type];
863 const ptrIndex = zigAnalysis.exprs[expr.cmpxchg.ptr];
864 const expectedValueIndex =
865 zigAnalysis.exprs[expr.cmpxchg.expected_value];
866 const newValueIndex = zigAnalysis.exprs[expr.cmpxchg.new_value];
867 const successOrderIndex = zigAnalysis.exprs[expr.cmpxchg.success_order];
868 const failureOrderIndex = zigAnalysis.exprs[expr.cmpxchg.failure_order];
869
870 const type = exprName(typeIndex, opts);
871 const ptr = exprName(ptrIndex, opts);
872 const expectedValue = exprName(expectedValueIndex, opts);
873 const newValue = exprName(newValueIndex, opts);
874 const successOrder = exprName(successOrderIndex, opts);
875 const failureOrder = exprName(failureOrderIndex, opts);
876
877 let fnName = "@";
878
879 switch (expr.cmpxchg.name) {
880 case "cmpxchg_strong": {
881 fnName += "cmpxchgStrong";
882 break;
883 }
884 case "cmpxchg_weak": {
885 fnName += "cmpxchgWeak";
886 break;
887 }
888 default: {
889 console.log("There's only cmpxchg_strong and cmpxchg_weak");
890 }
811 }891 }
812 // remove extra dom entries892
813 while (desiredLen < listDom.childElementCount) {893 return (
814 listDom.removeChild(listDom.lastChild);894 fnName +
895 "(" +
896 type +
897 ", " +
898 ptr +
899 ", " +
900 expectedValue +
901 ", " +
902 newValue +
903 ", " +
904 "." +
905 successOrder +
906 ", " +
907 "." +
908 failureOrder +
909 ")"
910 );
911 }
912 case "cmpxchgIndex": {
913 const cmpxchgIndex = zigAnalysis.exprs[expr.cmpxchgIndex];
914 return exprName(cmpxchgIndex, opts);
915 }
916 case "switchOp": {
917 let condExpr = zigAnalysis.exprs[expr.switchOp.cond_index];
918 let ast = zigAnalysis.astNodes[expr.switchOp.ast];
919 let file_name = expr.switchOp.file_name;
920 let outer_decl_index = expr.switchOp.outer_decl;
921 let outer_decl = zigAnalysis.types[outer_decl_index];
922 let line = 0;
923 // console.log(expr.switchOp)
924 // console.log(outer_decl)
925 while (outer_decl_index !== 0 && outer_decl.line_number > 0) {
926 line += outer_decl.line_number;
927 outer_decl_index = outer_decl.outer_decl;
928 outer_decl = zigAnalysis.types[outer_decl_index];
929 // console.log(outer_decl)
930 }
931 line += ast.line + 1;
932 let payloadHtml = "";
933 let cond = exprName(condExpr, opts);
934
935 payloadHtml +=
936 "</br>" +
937 "node_name: " +
938 ast.name +
939 "</br>" +
940 "file: " +
941 file_name +
942 "</br>" +
943 "line: " +
944 line +
945 "</br>";
946 payloadHtml +=
947 "switch(" +
948 cond +
949 ") {" +
950 '<a href="https://github.com/ziglang/zig/tree/master/lib/std/' +
951 file_name +
952 "#L" +
953 line +
954 '">' +
955 "..." +
956 "</a>}";
957 return payloadHtml;
958 }
959 case "switchIndex": {
960 const switchIndex = zigAnalysis.exprs[expr.switchIndex];
961 return exprName(switchIndex, opts);
962 }
963 case "refPath": {
964 let name = exprName(expr.refPath[0]);
965 for (let i = 1; i < expr.refPath.length; i++) {
966 let component = undefined;
967 if ("string" in expr.refPath[i]) {
968 component = expr.refPath[i].string;
969 } else {
970 component = exprName(expr.refPath[i]);
971 }
972 name += "." + component;
815 }973 }
816 }974 return name;
817 975 }
818 function walkResultTypeRef(wr) {976 case "fieldRef": {
819 if (wr.typeRef) return wr.typeRef;977 const enumObj = exprName({ type: expr.fieldRef.type }, opts);
820 let resolved = resolveValue(wr);978 const field =
821 if (wr === resolved) {979 zigAnalysis.astNodes[enumObj.ast].fields[expr.fieldRef.index];
822 return {type: 0};980 const name = zigAnalysis.astNodes[field].name;
823 }981 return name;
824 return walkResultTypeRef(resolved);982 }
825 }983 case "enumToInt": {
826 984 const enumToInt = zigAnalysis.exprs[expr.enumToInt];
827 function exprName(expr, opts) {985 return "@enumToInt(" + exprName(enumToInt, opts) + ")";
828 switch (Object.keys(expr)[0]) {986 }
829 default: throw "this expression is not implemented yet";987 case "bitSizeOf": {
830 case "bool": {988 const bitSizeOf = zigAnalysis.exprs[expr.bitSizeOf];
831 if (expr.bool) {989 return "@bitSizeOf(" + exprName(bitSizeOf, opts) + ")";
832 return "true";990 }
833 }991 case "sizeOf": {
834 return "false";992 const sizeOf = zigAnalysis.exprs[expr.sizeOf];
993 return "@sizeOf(" + exprName(sizeOf, opts) + ")";
994 }
995 case "builtinIndex": {
996 const builtinIndex = zigAnalysis.exprs[expr.builtinIndex];
997 return exprName(builtinIndex, opts);
998 }
999 case "builtin": {
1000 const param_expr = zigAnalysis.exprs[expr.builtin.param];
1001 let param = exprName(param_expr, opts);
1002
1003 let payloadHtml = "@";
1004 switch (expr.builtin.name) {
1005 case "align_of": {
1006 payloadHtml += "alignOf";
1007 break;
835 }1008 }
836 case "&": {1009 case "bool_to_int": {
837 return "&" + exprName(zigAnalysis.exprs[expr["&"]]);1010 payloadHtml += "boolToInt";
1011 break;
838 }1012 }
839 case "compileError": {1013 case "embed_file": {
840 let compileError = expr.compileError;1014 payloadHtml += "embedFile";
841 return compileError;1015 break;
842 }1016 }
843 case "enumLiteral": {1017 case "error_name": {
844 let literal = expr.enumLiteral;1018 payloadHtml += "errorName";
845 return "." + literal;1019 break;
846 }1020 }
847 case "void": {1021 case "panic": {
848 return "void";1022 payloadHtml += "panic";
1023 break;
849 }1024 }
850 case "slice":{1025 case "set_cold": {
851 let payloadHtml = "";1026 payloadHtml += "setCold";
852 const lhsExpr = zigAnalysis.exprs[expr.slice.lhs];1027 break;
853 const startExpr = zigAnalysis.exprs[expr.slice.start];
854 let decl = exprName(lhsExpr);
855 let start = exprName(startExpr);
856 let end = "";
857 let sentinel = "";
858 if (expr.slice['end']) {
859 const endExpr = zigAnalysis.exprs[expr.slice.end];
860 let end_ = exprName(endExpr);
861 end += end_;
862 }
863 if (expr.slice['sentinel']) {
864 const sentinelExpr = zigAnalysis.exprs[expr.slice.sentinel];
865 let sentinel_ = exprName(sentinelExpr);
866 sentinel += " :" + sentinel_;
867 }
868 payloadHtml += decl + "["+ start + ".." + end + sentinel + "]";
869 return payloadHtml;
870 }1028 }
871 case "sliceIndex": {1029 case "set_runtime_safety": {
872 const sliceIndex = zigAnalysis.exprs[expr.sliceIndex];1030 payloadHtml += "setRuntimeSafety";
873 return exprName(sliceIndex, opts);1031 break;
874 }
875 case "cmpxchg":{
876 const typeIndex = zigAnalysis.exprs[expr.cmpxchg.type];
877 const ptrIndex = zigAnalysis.exprs[expr.cmpxchg.ptr];
878 const expectedValueIndex = zigAnalysis.exprs[expr.cmpxchg.expected_value];
879 const newValueIndex = zigAnalysis.exprs[expr.cmpxchg.new_value];
880 const successOrderIndex = zigAnalysis.exprs[expr.cmpxchg.success_order];
881 const failureOrderIndex = zigAnalysis.exprs[expr.cmpxchg.failure_order];
882
883 const type = exprName(typeIndex, opts);
884 const ptr = exprName(ptrIndex, opts);
885 const expectedValue = exprName(expectedValueIndex, opts);
886 const newValue = exprName(newValueIndex, opts);
887 const successOrder = exprName(successOrderIndex, opts);
888 const failureOrder = exprName(failureOrderIndex, opts);
889
890 let fnName = "@";
891
892 switch (expr.cmpxchg.name) {
893 case "cmpxchg_strong": {
894 fnName += "cmpxchgStrong"
895 break;
896 }
897 case "cmpxchg_weak": {
898 fnName += "cmpxchgWeak"
899 break;
900 }
901 default: {
902 console.log("There's only cmpxchg_strong and cmpxchg_weak");
903 }
904 }
905
906 return fnName + "(" + type + ", " + ptr + ", " + expectedValue + ", "+ newValue + ", "+"." +successOrder + ", "+ "." +failureOrder + ")";
907 }
908 case "cmpxchgIndex": {
909 const cmpxchgIndex = zigAnalysis.exprs[expr.cmpxchgIndex];
910 return exprName(cmpxchgIndex, opts);
911 }
912 case "switchOp":{
913 let condExpr = zigAnalysis.exprs[expr.switchOp.cond_index];
914 let ast = zigAnalysis.astNodes[expr.switchOp.ast];
915 let file_name = expr.switchOp.file_name;
916 let outer_decl_index = expr.switchOp.outer_decl;
917 let outer_decl = zigAnalysis.types[outer_decl_index];
918 let line = 0;
919 // console.log(expr.switchOp)
920 // console.log(outer_decl)
921 while (outer_decl_index !== 0 && outer_decl.line_number > 0) {
922 line += outer_decl.line_number;
923 outer_decl_index = outer_decl.outer_decl;
924 outer_decl = zigAnalysis.types[outer_decl_index];
925 // console.log(outer_decl)
926 }
927 line += ast.line + 1;
928 let payloadHtml = "";
929 let cond = exprName(condExpr, opts);
930
931 payloadHtml += "</br>" + "node_name: " + ast.name + "</br>" + "file: " + file_name + "</br>" + "line: " + line + "</br>";
932 payloadHtml += "switch(" + cond + ") {" + "<a href=\"https://github.com/ziglang/zig/tree/master/lib/std/" + file_name + "#L" + line + "\">" +"..." + "</a>}";
933 return payloadHtml;
934 }1032 }
935 case "switchIndex": {1033 case "sqrt": {
936 const switchIndex = zigAnalysis.exprs[expr.switchIndex];1034 payloadHtml += "sqrt";
937 return exprName(switchIndex, opts);1035 break;
938 }1036 }
939 case "refPath" : {1037 case "sin": {
940 let name = exprName(expr.refPath[0]);1038 payloadHtml += "sin";
941 for (let i = 1; i < expr.refPath.length; i++) {1039 break;
942 let component = undefined;
943 if ("string" in expr.refPath[i]) {
944 component = expr.refPath[i].string;
945 } else {
946 component = exprName(expr.refPath[i]);
947 }
948 name += "." + component;
949 }
950 return name;
951 }1040 }
952 case "fieldRef" : {1041 case "cos": {
953 const enumObj = exprName({"type":expr.fieldRef.type} ,opts);1042 payloadHtml += "cos";
954 const field = zigAnalysis.astNodes[enumObj.ast].fields[expr.fieldRef.index];1043 break;
955 const name = zigAnalysis.astNodes[field].name;
956 return name
957 }1044 }
958 case "enumToInt" : {1045 case "tan": {
959 const enumToInt = zigAnalysis.exprs[expr.enumToInt];1046 payloadHtml += "tan";
960 return "@enumToInt(" + exprName(enumToInt, opts) + ")";1047 break;
961 }1048 }
962 case "bitSizeOf" : {1049 case "exp": {
963 const bitSizeOf = zigAnalysis.exprs[expr.bitSizeOf];1050 payloadHtml += "exp";
964 return "@bitSizeOf(" + exprName(bitSizeOf, opts) + ")";1051 break;
965 }1052 }
966 case "sizeOf" : {1053 case "exp2": {
967 const sizeOf = zigAnalysis.exprs[expr.sizeOf];1054 payloadHtml += "exp2";
968 return "@sizeOf(" + exprName(sizeOf, opts) + ")";1055 break;
969 }1056 }
970 case "builtinIndex" : {1057 case "log": {
971 const builtinIndex = zigAnalysis.exprs[expr.builtinIndex];1058 payloadHtml += "log";
972 return exprName(builtinIndex, opts);1059 break;
973 }1060 }
974 case "builtin": {1061 case "log2": {
975 const param_expr = zigAnalysis.exprs[expr.builtin.param];1062 payloadHtml += "log2";
976 let param = exprName(param_expr, opts);1063 break;
977
978
979 let payloadHtml = "@";
980 switch (expr.builtin.name) {
981 case "align_of": {
982 payloadHtml += "alignOf";
983 break;
984 }
985 case "bool_to_int": {
986 payloadHtml += "boolToInt";
987 break;
988 }
989 case "embed_file": {
990 payloadHtml += "embedFile";
991 break;
992 }
993 case "error_name": {
994 payloadHtml += "errorName";
995 break;
996 }
997 case "panic": {
998 payloadHtml += "panic";
999 break;
1000 }
1001 case "set_cold": {
1002 payloadHtml += "setCold";
1003 break;
1004 }
1005 case "set_runtime_safety": {
1006 payloadHtml += "setRuntimeSafety";
1007 break;
1008 }
1009 case "sqrt": {
1010 payloadHtml += "sqrt";
1011 break;
1012 }
1013 case "sin": {
1014 payloadHtml += "sin";
1015 break;
1016 }
1017 case "cos": {
1018 payloadHtml += "cos";
1019 break;
1020 }
1021 case "tan": {
1022 payloadHtml += "tan";
1023 break;
1024 }
1025 case "exp": {
1026 payloadHtml += "exp";
1027 break;
1028 }
1029 case "exp2": {
1030 payloadHtml += "exp2";
1031 break;
1032 }
1033 case "log": {
1034 payloadHtml += "log";
1035 break;
1036 }
1037 case "log2": {
1038 payloadHtml += "log2";
1039 break;
1040 }
1041 case "log10": {
1042 payloadHtml += "log10";
1043 break;
1044 }
1045 case "fabs": {
1046 payloadHtml += "fabs";
1047 break;
1048 }
1049 case "floor": {
1050 payloadHtml += "floor";
1051 break;
1052 }
1053 case "ceil": {
1054 payloadHtml += "ceil";
1055 break;
1056 }
1057 case "trunc": {
1058 payloadHtml += "trunc";
1059 break;
1060 }
1061 case "round": {
1062 payloadHtml += "round";
1063 break;
1064 }
1065 case "tag_name": {
1066 payloadHtml += "tagName";
1067 break;
1068 }
1069 case "reify": {
1070 payloadHtml += "Type";
1071 break;
1072 }
1073 case "type_name": {
1074 payloadHtml += "typeName";
1075 break;
1076 }
1077 case "frame_type": {
1078 payloadHtml += "Frame";
1079 break;
1080 }
1081 case "frame_size": {
1082 payloadHtml += "frameSize";
1083 break;
1084 }
1085 case "ptr_to_int": {
1086 payloadHtml += "ptrToInt";
1087 break;
1088 }
1089 case "error_to_int": {
1090 payloadHtml += "errorToInt";
1091 break;
1092 }
1093 case "int_to_error": {
1094 payloadHtml += "intToError";
1095 break;
1096 }
1097 case "maximum": {
1098 payloadHtml += "maximum";
1099 break;
1100 }
1101 case "minimum": {
1102 payloadHtml += "minimum";
1103 break;
1104 }
1105 case "bit_not": {
1106 return "~" + param;
1107 }
1108 case "clz": {
1109 return "@clz(T" + ", " + param + ")";
1110 }
1111 case "ctz": {
1112 return "@ctz(T" + ", " + param + ")";
1113 }
1114 case "pop_count": {
1115 return "@popCount(T" + ", " + param + ")";
1116 }
1117 case "byte_swap": {
1118 return "@byteSwap(T" + ", " + param + ")";
1119 }
1120 case "bit_reverse": {
1121 return "@bitReverse(T" + ", " + param + ")";
1122 }
1123 default: console.log("builtin function not handled yet or doesn't exist!");
1124 };
1125 return payloadHtml + "(" + param + ")";
1126
1127 }1064 }
1128 case "builtinBinIndex" : {1065 case "log10": {
1129 const builtinBinIndex = zigAnalysis.exprs[expr.builtinBinIndex];1066 payloadHtml += "log10";
1130 return exprName(builtinBinIndex, opts);1067 break;
1131 }1068 }
1132 case "builtinBin": {1069 case "fabs": {
1133 const lhsOp = zigAnalysis.exprs[expr.builtinBin.lhs];1070 payloadHtml += "fabs";
1134 const rhsOp = zigAnalysis.exprs[expr.builtinBin.rhs];1071 break;
1135 let lhs = exprName(lhsOp, opts);
1136 let rhs = exprName(rhsOp, opts);
1137
1138 let payloadHtml = "@";
1139 switch (expr.builtinBin.name) {
1140 case "float_to_int": {
1141 payloadHtml += "floatToInt";
1142 break;
1143 }
1144 case "int_to_float": {
1145 payloadHtml += "intToFloat";
1146 break;
1147 }
1148 case "int_to_ptr": {
1149 payloadHtml += "intToPtr";
1150 break;
1151 }
1152 case "int_to_enum": {
1153 payloadHtml += "intToEnum";
1154 break;
1155 }
1156 case "float_cast": {
1157 payloadHtml += "floatCast";
1158 break;
1159 }
1160 case "int_cast": {
1161 payloadHtml += "intCast";
1162 break;
1163 }
1164 case "ptr_cast": {
1165 payloadHtml += "ptrCast";
1166 break;
1167 }
1168 case "truncate": {
1169 payloadHtml += "truncate";
1170 break;
1171 }
1172 case "align_cast": {
1173 payloadHtml += "alignCast";
1174 break;
1175 }
1176 case "has_decl": {
1177 payloadHtml += "hasDecl";
1178 break;
1179 }
1180 case "has_field": {
1181 payloadHtml += "hasField";
1182 break;
1183 }
1184 case "bit_reverse": {
1185 payloadHtml += "bitReverse";
1186 break;
1187 }
1188 case "div_exact": {
1189 payloadHtml += "divExact";
1190 break;
1191 }
1192 case "div_floor": {
1193 payloadHtml += "divFloor";
1194 break;
1195 }
1196 case "div_trunc": {
1197 payloadHtml += "divTrunc";
1198 break;
1199 }
1200 case "mod": {
1201 payloadHtml += "mod";
1202 break;
1203 }
1204 case "rem": {
1205 payloadHtml += "rem";
1206 break;
1207 }
1208 case "mod_rem": {
1209 payloadHtml += "rem";
1210 break;
1211 }
1212 case "shl_exact": {
1213 payloadHtml += "shlExact";
1214 break;
1215 }
1216 case "shr_exact": {
1217 payloadHtml += "shrExact";
1218 break;
1219 }
1220 case "bitcast" : {
1221 payloadHtml += "bitCast";
1222 break;
1223 }
1224 case "align_cast" : {
1225 payloadHtml += "alignCast";
1226 break;
1227 }
1228 case "vector_type" : {
1229 payloadHtml += "Vector";
1230 break;
1231 }
1232 case "reduce": {
1233 payloadHtml += "reduce";
1234 break;
1235 }
1236 case "splat": {
1237 payloadHtml += "splat";
1238 break;
1239 }
1240 case "offset_of": {
1241 payloadHtml += "offsetOf";
1242 break;
1243 }
1244 case "bit_offset_of": {
1245 payloadHtml += "bitOffsetOf";
1246 break;
1247 }
1248 default: console.log("builtin function not handled yet or doesn't exist!");
1249 };
1250 return payloadHtml + "(" + lhs + ", " + rhs + ")";
1251
1252 }1072 }
1253 case "binOpIndex" : {1073 case "floor": {
1254 const binOpIndex = zigAnalysis.exprs[expr.binOpIndex];1074 payloadHtml += "floor";
1255 return exprName(binOpIndex, opts);1075 break;
1256 }1076 }
1257 case "binOp": {1077 case "ceil": {
1258 const lhsOp = zigAnalysis.exprs[expr.binOp.lhs];1078 payloadHtml += "ceil";
1259 const rhsOp = zigAnalysis.exprs[expr.binOp.rhs];1079 break;
1260 let lhs = exprName(lhsOp, opts);
1261 let rhs = exprName(rhsOp, opts);
1262
1263 let print_lhs = "";
1264 let print_rhs = "";
1265
1266 if (lhsOp['binOpIndex']) {
1267 print_lhs = "(" + lhs + ")";
1268 } else {
1269 print_lhs = lhs;
1270 }
1271 if (rhsOp['binOpIndex']) {
1272 print_rhs = "(" + rhs + ")";
1273 } else {
1274 print_rhs = rhs;
1275 }
1276
1277 let operator = "";
1278
1279 switch (expr.binOp.name) {
1280 case "add": {
1281 operator += "+";
1282 break;
1283 }
1284 case "addwrap": {
1285 operator += "+%";
1286 break;
1287 }
1288 case "add_sat": {
1289 operator += "+|";
1290 break;
1291 }
1292 case "sub": {
1293 operator += "-";
1294 break;
1295 }
1296 case "subwrap": {
1297 operator += "-%";
1298 break;
1299 }
1300 case "sub_sat": {
1301 operator += "-|";
1302 break;
1303 }
1304 case "mul": {
1305 operator += "*";
1306 break;
1307 }
1308 case "mulwrap": {
1309 operator += "*%";
1310 break;
1311 }
1312 case "mul_sat": {
1313 operator += "*|";
1314 break;
1315 }
1316 case "div": {
1317 operator += "/";
1318 break;
1319 }
1320 case "shl": {
1321 operator += "<<";
1322 break;
1323 }
1324 case "shl_sat": {
1325 operator += "<<|";
1326 break;
1327 }
1328 case "shr": {
1329 operator += ">>";
1330 break;
1331 }
1332 case "bit_or" : {
1333 operator += "|";
1334 break;
1335 }
1336 case "bit_and" : {
1337 operator += "&";
1338 break;
1339 }
1340 case "array_cat" : {
1341 operator += "++";
1342 break;
1343 }
1344 case "array_mul" : {
1345 operator += "**";
1346 break;
1347 }
1348 default: console.log("operator not handled yet or doesn't exist!");
1349 };
1350
1351 return print_lhs + " " + operator + " " + print_rhs;
1352
1353 }1080 }
1354 case "errorSets": {1081 case "trunc": {
1355 const errUnionObj = zigAnalysis.types[expr.errorSets];1082 payloadHtml += "trunc";
1356 let lhs = exprName(errUnionObj.lhs, opts);1083 break;
1357 let rhs = exprName(errUnionObj.rhs, opts);
1358 return lhs + " || " + rhs;
1359
1360 }1084 }
1361 case "errorUnion": {1085 case "round": {
1362 const errUnionObj = zigAnalysis.types[expr.errorUnion];1086 payloadHtml += "round";
1363 let lhs = exprName(errUnionObj.lhs, opts);1087 break;
1364 let rhs = exprName(errUnionObj.rhs, opts);
1365 return lhs + "!" + rhs;
1366
1367 }1088 }
1368 case "struct": {1089 case "tag_name": {
1369 const struct_name = zigAnalysis.decls[expr.struct[0].val.typeRef.refPath[0].declRef].name;1090 payloadHtml += "tagName";
1370 let struct_body = "";1091 break;
1371 struct_body += struct_name + "{ ";
1372 for (let i = 0; i < expr.struct.length; i++) {
1373 const val = expr.struct[i].name
1374 const exprArg = zigAnalysis.exprs[expr.struct[i].val.expr.as.exprArg];
1375 let value_field = exprArg[Object.keys(exprArg)[0]];
1376 if (value_field instanceof Object) {
1377 value_field = zigAnalysis.decls[value_field[0].val.typeRef.refPath[0].declRef].name;
1378 };
1379 struct_body += "." + val + " = " + value_field;
1380 if (i !== expr.struct.length - 1) {
1381 struct_body += ", ";
1382 } else {
1383 struct_body += " ";
1384 }
1385 }
1386 struct_body += "}";
1387 return struct_body;
1388 }
1389 case "typeOf_peer": {
1390 let payloadHtml = "@TypeOf("
1391 for (let i = 0; i < expr.typeOf_peer.length; i++) {
1392 let elem = zigAnalysis.exprs[expr.typeOf_peer[i]];
1393 payloadHtml += exprName(elem, {wantHtml: true, wantLink:true});
1394 if (i !== expr.typeOf_peer.length - 1) {
1395 payloadHtml += ", ";
1396 }
1397 }
1398 payloadHtml += ")";
1399 return payloadHtml;
1400
1401 }1092 }
1402 case "alignOf": {1093 case "reify": {
1403 const alignRefArg = zigAnalysis.exprs[expr.alignOf];1094 payloadHtml += "Type";
1404 let payloadHtml = "@alignOf(" + exprName(alignRefArg, {wantHtml: true, wantLink:true}) + ")";1095 break;
1405 return payloadHtml;
1406 }1096 }
1407 case "typeOf": {1097 case "type_name": {
1408 const typeRefArg = zigAnalysis.exprs[expr.typeOf];1098 payloadHtml += "typeName";
1409 let payloadHtml = "@TypeOf(" + exprName(typeRefArg, {wantHtml: true, wantLink:true}) + ")";1099 break;
1410 return payloadHtml;
1411 }1100 }
1412 case "typeInfo": {1101 case "frame_type": {
1413 const typeRefArg = zigAnalysis.exprs[expr.typeInfo];1102 payloadHtml += "Frame";
1414 let payloadHtml = "@typeInfo(" + exprName(typeRefArg, {wantHtml: true, wantLink:true}) + ")";1103 break;
1415 return payloadHtml;
1416 }1104 }
1417 case "null": {1105 case "frame_size": {
1418 return "null";1106 payloadHtml += "frameSize";
1107 break;
1419 }1108 }
1420 case "array": {1109 case "ptr_to_int": {
1421 let payloadHtml = ".{";1110 payloadHtml += "ptrToInt";
1422 for (let i = 0; i < expr.array.length; i++) {1111 break;
1423 if (i != 0) payloadHtml += ", ";
1424 let elem = zigAnalysis.exprs[expr.array[i]];
1425 payloadHtml += exprName(elem, opts);
1426 }
1427 return payloadHtml + "}";
1428 }1112 }
1429 case "comptimeExpr": {1113 case "error_to_int": {
1430 return zigAnalysis.comptimeExprs[expr.comptimeExpr].code;1114 payloadHtml += "errorToInt";
1115 break;
1431 }1116 }
1432 case "call": {1117 case "int_to_error": {
1433 let call = zigAnalysis.calls[expr.call];1118 payloadHtml += "intToError";
1434 let payloadHtml = "";1119 break;
1435
1436
1437 switch(Object.keys(call.func)[0]){
1438 default: throw "TODO";
1439 case "declRef":
1440 case "refPath": {
1441 payloadHtml += exprName(call.func, opts);
1442 break;
1443 }
1444 }
1445 payloadHtml += "(";
1446
1447 for (let i = 0; i < call.args.length; i++) {
1448 if (i != 0) payloadHtml += ", ";
1449 payloadHtml += exprName(call.args[i], opts);
1450 }
1451
1452 payloadHtml += ")";
1453 return payloadHtml;
1454 }1120 }
1455 case "as": {1121 case "maximum": {
1456 // @Check : this should be done in backend because there are legit @as() calls1122 payloadHtml += "maximum";
1457 // const typeRefArg = zigAnalysis.exprs[expr.as.typeRefArg];1123 break;
1458 const exprArg = zigAnalysis.exprs[expr.as.exprArg];
1459 // return "@as(" + exprName(typeRefArg, opts) +
1460 // ", " + exprName(exprArg, opts) + ")";
1461 return exprName(exprArg, opts);
1462 }1124 }
1463 case "declRef": {1125 case "minimum": {
1464 return zigAnalysis.decls[expr.declRef].name;1126 payloadHtml += "minimum";
1127 break;
1465 }1128 }
1466 case "refPath": {1129 case "bit_not": {
1467 return expr.refPath.map(x => exprName(x, opts)).join(".");1130 return "~" + param;
1468 }1131 }
1469 case "int": {1132 case "clz": {
1470 return "" + expr.int;1133 return "@clz(T" + ", " + param + ")";
1471 }1134 }
1472 case "float": {1135 case "ctz": {
1473 return "" + expr.float.toFixed(2);1136 return "@ctz(T" + ", " + param + ")";
1474 }1137 }
1475 case "float128": {1138 case "pop_count": {
1476 return "" + expr.float128.toFixed(2);1139 return "@popCount(T" + ", " + param + ")";
1477 }1140 }
1478 case "undefined": {1141 case "byte_swap": {
1479 return "undefined";1142 return "@byteSwap(T" + ", " + param + ")";
1480 }1143 }
1481 case "string": {1144 case "bit_reverse": {
1482 return "\"" + escapeHtml(expr.string) + "\"";1145 return "@bitReverse(T" + ", " + param + ")";
1483 }1146 }
14841147 default:
1485 case "anytype": {1148 console.log("builtin function not handled yet or doesn't exist!");
1486 return "anytype";1149 }
1150 return payloadHtml + "(" + param + ")";
1151 }
1152 case "builtinBinIndex": {
1153 const builtinBinIndex = zigAnalysis.exprs[expr.builtinBinIndex];
1154 return exprName(builtinBinIndex, opts);
1155 }
1156 case "builtinBin": {
1157 const lhsOp = zigAnalysis.exprs[expr.builtinBin.lhs];
1158 const rhsOp = zigAnalysis.exprs[expr.builtinBin.rhs];
1159 let lhs = exprName(lhsOp, opts);
1160 let rhs = exprName(rhsOp, opts);
1161
1162 let payloadHtml = "@";
1163 switch (expr.builtinBin.name) {
1164 case "float_to_int": {
1165 payloadHtml += "floatToInt";
1166 break;
1487 }1167 }
14881168 case "int_to_float": {
1489 case "this":{1169 payloadHtml += "intToFloat";
1490 return "@This()";1170 break;
1491 }1171 }
1172 case "int_to_ptr": {
1173 payloadHtml += "intToPtr";
1174 break;
1175 }
1176 case "int_to_enum": {
1177 payloadHtml += "intToEnum";
1178 break;
1179 }
1180 case "float_cast": {
1181 payloadHtml += "floatCast";
1182 break;
1183 }
1184 case "int_cast": {
1185 payloadHtml += "intCast";
1186 break;
1187 }
1188 case "ptr_cast": {
1189 payloadHtml += "ptrCast";
1190 break;
1191 }
1192 case "truncate": {
1193 payloadHtml += "truncate";
1194 break;
1195 }
1196 case "align_cast": {
1197 payloadHtml += "alignCast";
1198 break;
1199 }
1200 case "has_decl": {
1201 payloadHtml += "hasDecl";
1202 break;
1203 }
1204 case "has_field": {
1205 payloadHtml += "hasField";
1206 break;
1207 }
1208 case "bit_reverse": {
1209 payloadHtml += "bitReverse";
1210 break;
1211 }
1212 case "div_exact": {
1213 payloadHtml += "divExact";
1214 break;
1215 }
1216 case "div_floor": {
1217 payloadHtml += "divFloor";
1218 break;
1219 }
1220 case "div_trunc": {
1221 payloadHtml += "divTrunc";
1222 break;
1223 }
1224 case "mod": {
1225 payloadHtml += "mod";
1226 break;
1227 }
1228 case "rem": {
1229 payloadHtml += "rem";
1230 break;
1231 }
1232 case "mod_rem": {
1233 payloadHtml += "rem";
1234 break;
1235 }
1236 case "shl_exact": {
1237 payloadHtml += "shlExact";
1238 break;
1239 }
1240 case "shr_exact": {
1241 payloadHtml += "shrExact";
1242 break;
1243 }
1244 case "bitcast": {
1245 payloadHtml += "bitCast";
1246 break;
1247 }
1248 case "align_cast": {
1249 payloadHtml += "alignCast";
1250 break;
1251 }
1252 case "vector_type": {
1253 payloadHtml += "Vector";
1254 break;
1255 }
1256 case "reduce": {
1257 payloadHtml += "reduce";
1258 break;
1259 }
1260 case "splat": {
1261 payloadHtml += "splat";
1262 break;
1263 }
1264 case "offset_of": {
1265 payloadHtml += "offsetOf";
1266 break;
1267 }
1268 case "bit_offset_of": {
1269 payloadHtml += "bitOffsetOf";
1270 break;
1271 }
1272 default:
1273 console.log("builtin function not handled yet or doesn't exist!");
1274 }
1275 return payloadHtml + "(" + lhs + ", " + rhs + ")";
1276 }
1277 case "binOpIndex": {
1278 const binOpIndex = zigAnalysis.exprs[expr.binOpIndex];
1279 return exprName(binOpIndex, opts);
1280 }
1281 case "binOp": {
1282 const lhsOp = zigAnalysis.exprs[expr.binOp.lhs];
1283 const rhsOp = zigAnalysis.exprs[expr.binOp.rhs];
1284 let lhs = exprName(lhsOp, opts);
1285 let rhs = exprName(rhsOp, opts);
14921286
1493 case "type": {1287 let print_lhs = "";
1494 let name = "";1288 let print_rhs = "";
1495
1496 let typeObj = expr.type;
1497 if (typeof typeObj === 'number') typeObj = zigAnalysis.types[typeObj];
1498 switch (typeObj.kind) {
1499 default: throw "TODO";
1500 case typeKinds.Struct:
1501 {
1502 let structObj = (typeObj);
1503 return structObj;
1504 }
1505 case typeKinds.Enum:
1506 {
1507 let enumObj = (typeObj);
1508 return enumObj;
1509 }
1510 case typeKinds.Opaque:
1511 {
1512 let opaqueObj = (typeObj);
1513
1514 return opaqueObj.name;
1515 }
1516 case typeKinds.ComptimeExpr:
1517 {
1518 return "anyopaque";
1519 }
1520 case typeKinds.Array:
1521 {
1522 let arrayObj = typeObj;
1523 let name = "[";
1524 let lenName = exprName(arrayObj.len, opts);
1525 let sentinel = arrayObj.sentinel ? ":"+exprName(arrayObj.sentinel, opts) : "";
1526 // let is_mutable = arrayObj.is_multable ? "const " : "";
1527
1528 if (opts.wantHtml) {
1529 name +=
1530 '<span class="tok-number">' + lenName + sentinel + "</span>";
1531 } else {
1532 name += lenName + sentinel;
1533 }
1534 name += "]";
1535 // name += is_mutable;
1536 name += exprName(arrayObj.child, opts);
1537 return name;
1538 }
1539 case typeKinds.Optional:
1540 return "?" + exprName((typeObj).child, opts);
1541 case typeKinds.Pointer:
1542 {
1543 let ptrObj = (typeObj);
1544 let sentinel = ptrObj.sentinel ? ":"+exprName(ptrObj.sentinel, opts) : "";
1545 let is_mutable = !ptrObj.is_mutable ? "const " : "";
1546 let name = "";
1547 switch (ptrObj.size) {
1548 default:
1549 console.log("TODO: implement unhandled pointer size case");
1550 case pointerSizeEnum.One:
1551 name += "*";
1552 name += is_mutable;
1553 break;
1554 case pointerSizeEnum.Many:
1555 name += "[*";
1556 name += sentinel;
1557 name += "]";
1558 name += is_mutable;
1559 break;
1560 case pointerSizeEnum.Slice:
1561 if (ptrObj.is_ref) {
1562 name += "*";
1563 }
1564 name += "[";
1565 name += sentinel;
1566 name += "]";
1567 name += is_mutable;
1568 break;
1569 case pointerSizeEnum.C:
1570 name += "[*c";
1571 name += sentinel;
1572 name += "]";
1573 name += is_mutable;
1574 break;
1575 }
1576 // @check: after the major changes in arrays the consts are came from switch above
1577 // if (!ptrObj.is_mutable) {
1578 // if (opts.wantHtml) {
1579 // name += '<span class="tok-kw">const</span> ';
1580 // } else {
1581 // name += "const ";
1582 // }
1583 // }
1584 if (ptrObj.is_allowzero) {
1585 name += "allowzero ";
1586 }
1587 if (ptrObj.is_volatile) {
1588 name += "volatile ";
1589 }
1590 if (ptrObj.has_addrspace) {
1591 name += "addrspace(";
1592 name += "." + "";
1593 name += ") ";
1594 }
1595 if (ptrObj.has_align) {
1596 let align = exprName(ptrObj.align, opts);
1597 if (opts.wantHtml) {
1598 name += '<span class="tok-kw">align</span>(';
1599 } else {
1600 name += "align(";
1601 }
1602 if (opts.wantHtml) {
1603 name += '<span class="tok-number">' + align + '</span>';
1604 } else {
1605 name += align;
1606 }
1607 if (ptrObj.hostIntBytes != null) {
1608 name += ":";
1609 if (opts.wantHtml) {
1610 name += '<span class="tok-number">' + ptrObj.bitOffsetInHost + '</span>';
1611 } else {
1612 name += ptrObj.bitOffsetInHost;
1613 }
1614 name += ":";
1615 if (opts.wantHtml) {
1616 name += '<span class="tok-number">' + ptrObj.hostIntBytes + '</span>';
1617 } else {
1618 name += ptrObj.hostIntBytes;
1619 }
1620 }
1621 name += ") ";
1622 }
1623 //name += typeValueName(ptrObj.child, wantHtml, wantSubLink, null);
1624 name += exprName(ptrObj.child, opts);
1625 return name;
1626 }
1627 case typeKinds.Float:
1628 {
1629 let floatObj = (typeObj);
1630
1631 if (opts.wantHtml) {
1632 return '<span class="tok-type">' + floatObj.name + '</span>';
1633 } else {
1634 return floatObj.name;
1635 }
1636 }
1637 case typeKinds.Int:
1638 {
1639 let intObj = (typeObj);
1640 let name = intObj.name;
1641 if (opts.wantHtml) {
1642 return '<span class="tok-type">' + name + '</span>';
1643 } else {
1644 return name;
1645 }
1646 }
1647 case typeKinds.ComptimeInt:
1648 if (opts.wantHtml) {
1649 return '<span class="tok-type">comptime_int</span>';
1650 } else {
1651 return "comptime_int";
1652 }
1653 case typeKinds.ComptimeFloat:
1654 if (opts.wantHtml) {
1655 return '<span class="tok-type">comptime_float</span>';
1656 } else {
1657 return "comptime_float";
1658 }
1659 case typeKinds.Type:
1660 if (opts.wantHtml) {
1661 return '<span class="tok-type">type</span>';
1662 } else {
1663 return "type";
1664 }
1665 case typeKinds.Bool:
1666 if (opts.wantHtml) {
1667 return '<span class="tok-type">bool</span>';
1668 } else {
1669 return "bool";
1670 }
1671 case typeKinds.Void:
1672 if (opts.wantHtml) {
1673 return '<span class="tok-type">void</span>';
1674 } else {
1675 return "void";
1676 }
1677 case typeKinds.EnumLiteral:
1678 if (opts.wantHtml) {
1679 return '<span class="tok-type">(enum literal)</span>';
1680 } else {
1681 return "(enum literal)";
1682 }
1683 case typeKinds.NoReturn:
1684 if (opts.wantHtml) {
1685 return '<span class="tok-type">noreturn</span>';
1686 } else {
1687 return "noreturn";
1688 }
1689 case typeKinds.ErrorSet:
1690 {
1691 let errSetObj = (typeObj);
1692 if (errSetObj.fields == null) {
1693 return '<span class="tok-type">anyerror</span>';
1694 } else {
1695 // throw "TODO";
1696 let html = "error{" + errSetObj.fields[0].name + "}";
1697 return html;
1698 }
1699 }
1700
1701 case typeKinds.ErrorUnion:
1702 {
1703 let errUnionObj = (typeObj);
1704 let lhs = exprName(errUnionObj.lhs, opts);
1705 let rhs = exprName(errUnionObj.rhs, opts);
1706 return lhs + "!" + rhs;
1707 }
1708 case typeKinds.InferredErrorUnion:
1709 {
1710 let errUnionObj = (typeObj);
1711 let payload = exprName(errUnionObj.payload, opts);
1712 return "!" + payload;
1713 }
1714 case typeKinds.Fn:
1715 {
1716 let fnObj = (typeObj);
1717 let payloadHtml = "";
1718 if (opts.wantHtml) {
1719 if (fnObj.is_extern) {
1720 payloadHtml += "pub extern ";
1721 }
1722 if (fnObj.has_lib_name) {
1723 payloadHtml += "\"" + fnObj.lib_name +"\" ";
1724 }
1725 payloadHtml += '<span class="tok-kw">fn</span>';
1726 if (opts.fnDecl) {
1727 payloadHtml += ' <span class="tok-fn">';
1728 if (opts.linkFnNameDecl) {
1729 payloadHtml += '<a href="' + opts.linkFnNameDecl + '">' +
1730 escapeHtml(opts.fnDecl.name) + '</a>';
1731 } else {
1732 payloadHtml += escapeHtml(opts.fnDecl.name);
1733 }
1734 payloadHtml += '</span>';
1735 }
1736 } else {
1737 payloadHtml += 'fn ';
1738 }
1739 payloadHtml += '(';
1740 if (fnObj.params) {
1741 let fields = null;
1742 let isVarArgs = false;
1743 let fnNode = zigAnalysis.astNodes[fnObj.src];
1744 fields = fnNode.fields;
1745 isVarArgs = fnNode.varArgs;
1746
1747 for (let i = 0; i < fnObj.params.length; i += 1) {
1748 if (i != 0) {
1749 payloadHtml += ', ';
1750 }
1751
1752 payloadHtml += "<span class='argBreaker'><br>&nbsp;&nbsp;&nbsp;&nbsp;</span>"
1753 let value = fnObj.params[i];
1754 let paramValue = resolveValue({expr: value});
1755
1756 if (fields != null) {
1757 let paramNode = zigAnalysis.astNodes[fields[i]];
1758
1759 if (paramNode.varArgs) {
1760 payloadHtml += '...';
1761 continue;
1762 }
1763
1764 if (paramNode.noalias) {
1765 if (opts.wantHtml) {
1766 payloadHtml += '<span class="tok-kw">noalias</span> ';
1767 } else {
1768 payloadHtml += 'noalias ';
1769 }
1770 }
1771
1772 if (paramNode.comptime) {
1773 if (opts.wantHtml) {
1774 payloadHtml += '<span class="tok-kw">comptime</span> ';
1775 } else {
1776 payloadHtml += 'comptime ';
1777 }
1778 }
1779
1780 let paramName = paramNode.name;
1781 if (paramName != null) {
1782 // skip if it matches the type name
1783 if (!shouldSkipParamName(paramValue, paramName)) {
1784 payloadHtml += paramName + ': ';
1785 }
1786 }
1787 }
1788
1789 if (isVarArgs && i === fnObj.params.length - 1) {
1790 payloadHtml += '...';
1791 }
1792 else if ("alignOf" in value) {
1793 if (opts.wantHtml) {
1794 payloadHtml += '<a href="">';
1795 payloadHtml +=
1796 '<span class="tok-kw" style="color:lightblue;">'
1797 + exprName(value, opts) + '</span>';
1798 payloadHtml += '</a>';
1799 } else {
1800 payloadHtml += exprName(value, opts);
1801 }
1802
1803 }
1804 else if ("typeOf" in value) {
1805 if (opts.wantHtml) {
1806 payloadHtml += '<a href="">';
1807 payloadHtml +=
1808 '<span class="tok-kw" style="color:lightblue;">'
1809 + exprName(value, opts) + '</span>';
1810 payloadHtml += '</a>';
1811 } else {
1812 payloadHtml += exprName(value, opts);
1813 }
1814
1815 }
1816 else if ("typeOf_peer" in value) {
1817 if (opts.wantHtml) {
1818 payloadHtml += '<a href="">';
1819 payloadHtml +=
1820 '<span class="tok-kw" style="color:lightblue;">'
1821 + exprName(value, opts) + '</span>';
1822 payloadHtml += '</a>';
1823 } else {
1824 payloadHtml += exprName(value, opts);
1825 }
1826
1827 }
1828 else if ("declRef" in value) {
1829 if (opts.wantHtml) {
1830 payloadHtml += '<a href="">';
1831 payloadHtml +=
1832 '<span class="tok-kw" style="color:lightblue;">'
1833 + exprName(value, opts) + '</span>';
1834 payloadHtml += '</a>';
1835 } else {
1836 payloadHtml += exprName(value, opts);
1837 }
1838
1839 }
1840 else if ("call" in value) {
1841 if (opts.wantHtml) {
1842 payloadHtml += '<a href="">';
1843 payloadHtml +=
1844 '<span class="tok-kw" style="color:lightblue;">'
1845 + exprName(value, opts) + '</span>';
1846 payloadHtml += '</a>';
1847 } else {
1848 payloadHtml += exprName(value, opts);
1849 }
1850 }
1851 else if ("refPath" in value) {
1852 if (opts.wantHtml) {
1853 payloadHtml += '<a href="">';
1854 payloadHtml +=
1855 '<span class="tok-kw" style="color:lightblue;">'
1856 + exprName(value, opts) + '</span>';
1857 payloadHtml += '</a>';
1858 } else {
1859 payloadHtml += exprName(value, opts);
1860 }
1861 } else if ("type" in value) {
1862 let name = exprName(value, {
1863 wantHtml: false,
1864 wantLink: false,
1865 fnDecl: opts.fnDecl,
1866 linkFnNameDecl: opts.linkFnNameDecl,
1867 });
1868 payloadHtml += '<span class="tok-kw">' + name + '</span>';
1869 } else if ("binOpIndex" in value) {
1870 payloadHtml += exprName(value, opts);
1871 }else if ("comptimeExpr" in value) {
1872 let comptimeExpr = zigAnalysis.comptimeExprs[value.comptimeExpr].code;
1873 if (opts.wantHtml) {
1874 payloadHtml += '<span class="tok-kw">' + comptimeExpr + '</span>';
1875 } else {
1876 payloadHtml += comptimeExpr;
1877 }
1878 } else if (opts.wantHtml) {
1879 payloadHtml += '<span class="tok-kw">anytype</span>';
1880 } else {
1881 payloadHtml += 'anytype';
1882 }
1883 }
1884 }
1885
1886 payloadHtml += "<span class='argBreaker'>,<br></span>"
1887 payloadHtml += ') ';
1888
1889 if (fnObj.has_align) {
1890 let align = zigAnalysis.exprs[fnObj.align]
1891 payloadHtml += "align(" + exprName(align, opts) + ") ";
1892 }
1893 if (fnObj.has_cc) {
1894 let cc = zigAnalysis.exprs[fnObj.cc]
1895 if (cc) {
1896 payloadHtml += "callconv(." + cc.enumLiteral + ") ";
1897 }
1898 }
18991289
1900 if (fnObj.is_inferred_error) {1290 if (lhsOp["binOpIndex"]) {
1901 payloadHtml += "!";1291 print_lhs = "(" + lhs + ")";
1902 }1292 } else {
1903 if (fnObj.ret != null) {1293 print_lhs = lhs;
1904 payloadHtml += exprName(fnObj.ret, opts);
1905 } else if (opts.wantHtml) {
1906 payloadHtml += '<span class="tok-kw">anytype</span>';
1907 } else {
1908 payloadHtml += 'anytype';
1909 }
1910 return payloadHtml;
1911 }
1912 // if (wantHtml) {
1913 // return escapeHtml(typeObj.name);
1914 // } else {
1915 // return typeObj.name;
1916 // }
1917 }
1918 }1294 }
19191295 if (rhsOp["binOpIndex"]) {
1296 print_rhs = "(" + rhs + ")";
1297 } else {
1298 print_rhs = rhs;
1920 }1299 }
1921 }
19221300
1301 let operator = "";
19231302
1924 1303 switch (expr.binOp.name) {
1925 function shouldSkipParamName(typeRef, paramName) {1304 case "add": {
1926 let resolvedTypeRef = resolveValue({expr: typeRef});1305 operator += "+";
1927 if ("type" in resolvedTypeRef) {1306 break;
1928 let typeObj = zigAnalysis.types[resolvedTypeRef.type];1307 }
1929 if (typeObj.kind === typeKinds.Pointer){1308 case "addwrap": {
1930 let ptrObj = (typeObj);1309 operator += "+%";
1931 if (getPtrSize(ptrObj) === pointerSizeEnum.One) {1310 break;
1932 const value = resolveValue(ptrObj.child);1311 }
1933 return typeValueName(value, false, true).toLowerCase() === paramName;1312 case "add_sat": {
1934 }1313 operator += "+|";
1935 }1314 break;
1315 }
1316 case "sub": {
1317 operator += "-";
1318 break;
1319 }
1320 case "subwrap": {
1321 operator += "-%";
1322 break;
1323 }
1324 case "sub_sat": {
1325 operator += "-|";
1326 break;
1327 }
1328 case "mul": {
1329 operator += "*";
1330 break;
1331 }
1332 case "mulwrap": {
1333 operator += "*%";
1334 break;
1335 }
1336 case "mul_sat": {
1337 operator += "*|";
1338 break;
1339 }
1340 case "div": {
1341 operator += "/";
1342 break;
1343 }
1344 case "shl": {
1345 operator += "<<";
1346 break;
1347 }
1348 case "shl_sat": {
1349 operator += "<<|";
1350 break;
1351 }
1352 case "shr": {
1353 operator += ">>";
1354 break;
1355 }
1356 case "bit_or": {
1357 operator += "|";
1358 break;
1359 }
1360 case "bit_and": {
1361 operator += "&";
1362 break;
1363 }
1364 case "array_cat": {
1365 operator += "++";
1366 break;
1367 }
1368 case "array_mul": {
1369 operator += "**";
1370 break;
1371 }
1372 default:
1373 console.log("operator not handled yet or doesn't exist!");
1936 }1374 }
1937 return false;
1938 }
19391375
1940 1376 return print_lhs + " " + operator + " " + print_rhs;
1941 function getPtrSize(typeObj) {1377 }
1942 return (typeObj.size == null) ? pointerSizeEnum.One : typeObj.size;1378 case "errorSets": {
1943 }1379 const errUnionObj = zigAnalysis.types[expr.errorSets];
19441380 let lhs = exprName(errUnionObj.lhs, opts);
1945 1381 let rhs = exprName(errUnionObj.rhs, opts);
1946 function renderType(typeObj) {1382 return lhs + " || " + rhs;
1947 let name;1383 }
1948 if (rootIsStd && typeObj === zigAnalysis.types[zigAnalysis.packages[zigAnalysis.rootPkg].main]) {1384 case "errorUnion": {
1949 name = "std";1385 const errUnionObj = zigAnalysis.types[expr.errorUnion];
1950 } else {1386 let lhs = exprName(errUnionObj.lhs, opts);
1951 name = exprName({type:typeObj}, false, false);1387 let rhs = exprName(errUnionObj.rhs, opts);
1952 }1388 return lhs + "!" + rhs;
1953 if (name != null && name != "") {1389 }
1954 domHdrName.innerText = name + " (" + zigAnalysis.typeKinds[typeObj.kind] + ")";1390 case "struct": {
1955 domHdrName.classList.remove("hidden");1391 const struct_name =
1956 }1392 zigAnalysis.decls[expr.struct[0].val.typeRef.refPath[0].declRef].name;
1957 if (typeObj.kind == typeKinds.ErrorSet) {1393 let struct_body = "";
1958 renderErrorSet((typeObj));1394 struct_body += struct_name + "{ ";
1395 for (let i = 0; i < expr.struct.length; i++) {
1396 const val = expr.struct[i].name;
1397 const exprArg = zigAnalysis.exprs[expr.struct[i].val.expr.as.exprArg];
1398 let value_field = exprArg[Object.keys(exprArg)[0]];
1399 if (value_field instanceof Object) {
1400 value_field =
1401 zigAnalysis.decls[value_field[0].val.typeRef.refPath[0].declRef]
1402 .name;
1403 }
1404 struct_body += "." + val + " = " + value_field;
1405 if (i !== expr.struct.length - 1) {
1406 struct_body += ", ";
1407 } else {
1408 struct_body += " ";
1409 }
1959 }1410 }
1960 }1411 struct_body += "}";
19611412 return struct_body;
1962 1413 }
1963 function renderErrorSet(errSetType) {1414 case "typeOf_peer": {
1964 if (errSetType.fields == null) {1415 let payloadHtml = "@TypeOf(";
1965 domFnErrorsAnyError.classList.remove("hidden");1416 for (let i = 0; i < expr.typeOf_peer.length; i++) {
1966 } else {1417 let elem = zigAnalysis.exprs[expr.typeOf_peer[i]];
1967 let errorList = [];1418 payloadHtml += exprName(elem, { wantHtml: true, wantLink: true });
1968 for (let i = 0; i < errSetType.fields.length; i += 1) {1419 if (i !== expr.typeOf_peer.length - 1) {
1969 let errObj = errSetType.fields[i];1420 payloadHtml += ", ";
1970 //let srcObj = zigAnalysis.astNodes[errObj.src];1421 }
1971 errorList.push(errObj);
1972 }
1973 errorList.sort(function(a, b) {
1974 return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
1975 });
1976
1977 resizeDomListDl(domListFnErrors, errorList.length);
1978 for (let i = 0; i < errorList.length; i += 1) {
1979 let nameTdDom = domListFnErrors.children[i * 2 + 0];
1980 let descTdDom = domListFnErrors.children[i * 2 + 1];
1981 nameTdDom.textContent = errorList[i].name;
1982 let docs = errorList[i].docs;
1983 if (docs != null) {
1984 descTdDom.innerHTML = markdown(docs);
1985 } else {
1986 descTdDom.textContent = "";
1987 }
1988 }
1989 domTableFnErrors.classList.remove("hidden");
1990 }1422 }
1991 domSectFnErrors.classList.remove("hidden");1423 payloadHtml += ")";
1992 }1424 return payloadHtml;
1993
1994// function allCompTimeFnCallsHaveTypeResult(typeIndex, value) {
1995// let srcIndex = zigAnalysis.fns[value].src;
1996// let calls = nodesToCallsMap[srcIndex];
1997// if (calls == null) return false;
1998// for (let i = 0; i < calls.length; i += 1) {
1999// let call = zigAnalysis.calls[calls[i]];
2000// if (call.result.type !== typeTypeId) return false;
2001// }
2002// return true;
2003// }
2004//
2005// function allCompTimeFnCallsResult(calls) {
2006// let firstTypeObj = null;
2007// let containerObj = {
2008// privDecls: [],
2009// };
2010// for (let callI = 0; callI < calls.length; callI += 1) {
2011// let call = zigAnalysis.calls[calls[callI]];
2012// if (call.result.type !== typeTypeId) return null;
2013// let typeObj = zigAnalysis.types[call.result.value];
2014// if (!typeKindIsContainer(typeObj.kind)) return null;
2015// if (firstTypeObj == null) {
2016// firstTypeObj = typeObj;
2017// containerObj.src = typeObj.src;
2018// } else if (firstTypeObj.src !== typeObj.src) {
2019// return null;
2020// }
2021//
2022// if (containerObj.fields == null) {
2023// containerObj.fields = (typeObj.fields || []).concat([]);
2024// } else for (let fieldI = 0; fieldI < typeObj.fields.length; fieldI += 1) {
2025// let prev = containerObj.fields[fieldI];
2026// let next = typeObj.fields[fieldI];
2027// if (prev === next) continue;
2028// if (typeof(prev) === 'object') {
2029// if (prev[next] == null) prev[next] = typeObj;
2030// } else {
2031// containerObj.fields[fieldI] = {};
2032// containerObj.fields[fieldI][prev] = firstTypeObj;
2033// containerObj.fields[fieldI][next] = typeObj;
2034// }
2035// }
2036//
2037// if (containerObj.pubDecls == null) {
2038// containerObj.pubDecls = (typeObj.pubDecls || []).concat([]);
2039// } else for (let declI = 0; declI < typeObj.pubDecls.length; declI += 1) {
2040// let prev = containerObj.pubDecls[declI];
2041// let next = typeObj.pubDecls[declI];
2042// if (prev === next) continue;
2043// // TODO instead of showing "examples" as the public declarations,
2044// // do logic like this:
2045// //if (typeof(prev) !== 'object') {
2046// // let newDeclId = zigAnalysis.decls.length;
2047// // prev = clone(zigAnalysis.decls[prev]);
2048// // prev.id = newDeclId;
2049// // zigAnalysis.decls.push(prev);
2050// // containerObj.pubDecls[declI] = prev;
2051// //}
2052// //mergeDecls(prev, next, firstTypeObj, typeObj);
2053// }
2054// }
2055// for (let declI = 0; declI < containerObj.pubDecls.length; declI += 1) {
2056// let decl = containerObj.pubDecls[declI];
2057// if (typeof(decl) === 'object') {
2058// containerObj.pubDecls[declI] = containerObj.pubDecls[declI].id;
2059// }
2060// }
2061// return containerObj;
2062// }
2063
2064
2065
2066
2067 function renderValue(decl) {
2068 let resolvedValue = resolveValue(decl.value)
2069
2070 if (resolvedValue.expr.fieldRef) {
2071 const declRef = decl.value.expr.refPath[0].declRef;
2072 const type = zigAnalysis.decls[declRef];
2073 domFnProtoCode.innerHTML = '<span class="tok-kw">const</span> ' +
2074 escapeHtml(decl.name) + ': ' + type.name +
2075 " = " + exprName(decl.value.expr, {wantHtml: true, wantLink:true}) + ";";
2076 } else if (resolvedValue.expr.string !== undefined || resolvedValue.expr.call !== undefined || resolvedValue.expr.comptimeExpr) {
2077 domFnProtoCode.innerHTML = '<span class="tok-kw">const</span> ' +
2078 escapeHtml(decl.name) + ': ' + exprName(resolvedValue.expr, {wantHtml: true, wantLink:true}) +
2079 " = " + exprName(decl.value.expr, {wantHtml: true, wantLink:true}) + ";";
2080 } else if (resolvedValue.expr.compileError) {
2081 domFnProtoCode.innerHTML = '<span class="tok-kw">const</span> ' +
2082 escapeHtml(decl.name) + " = " + exprName(decl.value.expr, {wantHtml: true, wantLink:true}) + ";";
2083 }
2084 else {
2085 domFnProtoCode.innerHTML = '<span class="tok-kw">const</span> ' +
2086 escapeHtml(decl.name) + ': ' + exprName(resolvedValue.typeRef, {wantHtml: true, wantLink:true}) +
2087 " = " + exprName(decl.value.expr, {wantHtml: true, wantLink:true}) + ";";
2088 }1425 }
20891426 case "alignOf": {
2090 let docs = zigAnalysis.astNodes[decl.src].docs;1427 const alignRefArg = zigAnalysis.exprs[expr.alignOf];
2091 if (docs != null) {1428 let payloadHtml =
2092 domTldDocs.innerHTML = markdown(docs);1429 "@alignOf(" +
2093 domTldDocs.classList.remove("hidden");1430 exprName(alignRefArg, { wantHtml: true, wantLink: true }) +
1431 ")";
1432 return payloadHtml;
1433 }
1434 case "typeOf": {
1435 const typeRefArg = zigAnalysis.exprs[expr.typeOf];
1436 let payloadHtml =
1437 "@TypeOf(" +
1438 exprName(typeRefArg, { wantHtml: true, wantLink: true }) +
1439 ")";
1440 return payloadHtml;
1441 }
1442 case "typeInfo": {
1443 const typeRefArg = zigAnalysis.exprs[expr.typeInfo];
1444 let payloadHtml =
1445 "@typeInfo(" +
1446 exprName(typeRefArg, { wantHtml: true, wantLink: true }) +
1447 ")";
1448 return payloadHtml;
1449 }
1450 case "null": {
1451 return "null";
1452 }
1453 case "array": {
1454 let payloadHtml = ".{";
1455 for (let i = 0; i < expr.array.length; i++) {
1456 if (i != 0) payloadHtml += ", ";
1457 let elem = zigAnalysis.exprs[expr.array[i]];
1458 payloadHtml += exprName(elem, opts);
1459 }
1460 return payloadHtml + "}";
1461 }
1462 case "comptimeExpr": {
1463 return zigAnalysis.comptimeExprs[expr.comptimeExpr].code;
1464 }
1465 case "call": {
1466 let call = zigAnalysis.calls[expr.call];
1467 let payloadHtml = "";
1468
1469 switch (Object.keys(call.func)[0]) {
1470 default:
1471 throw "TODO";
1472 case "declRef":
1473 case "refPath": {
1474 payloadHtml += exprName(call.func, opts);
1475 break;
1476 }
2094 }1477 }
1478 payloadHtml += "(";
20951479
2096 domFnProto.classList.remove("hidden");1480 for (let i = 0; i < call.args.length; i++) {
2097 }1481 if (i != 0) payloadHtml += ", ";
20981482 payloadHtml += exprName(call.args[i], opts);
2099
2100 function renderVar(decl) {
2101 let declTypeRef = typeOfDecl(decl);
2102 domFnProtoCode.innerHTML = '<span class="tok-kw">var</span> ' +
2103 escapeHtml(decl.name) + ': ' + typeValueName(declTypeRef, true, true);
2104
2105 let docs = zigAnalysis.astNodes[decl.src].docs;
2106 if (docs != null) {
2107 domTldDocs.innerHTML = markdown(docs);
2108 domTldDocs.classList.remove("hidden");
2109 }1483 }
21101484
2111 domFnProto.classList.remove("hidden");1485 payloadHtml += ")";
2112 }1486 return payloadHtml;
21131487 }
1488 case "as": {
1489 // @Check : this should be done in backend because there are legit @as() calls
1490 // const typeRefArg = zigAnalysis.exprs[expr.as.typeRefArg];
1491 const exprArg = zigAnalysis.exprs[expr.as.exprArg];
1492 // return "@as(" + exprName(typeRefArg, opts) +
1493 // ", " + exprName(exprArg, opts) + ")";
1494 return exprName(exprArg, opts);
1495 }
1496 case "declRef": {
1497 return zigAnalysis.decls[expr.declRef].name;
1498 }
1499 case "refPath": {
1500 return expr.refPath.map((x) => exprName(x, opts)).join(".");
1501 }
1502 case "int": {
1503 return "" + expr.int;
1504 }
1505 case "float": {
1506 return "" + expr.float.toFixed(2);
1507 }
1508 case "float128": {
1509 return "" + expr.float128.toFixed(2);
1510 }
1511 case "undefined": {
1512 return "undefined";
1513 }
1514 case "string": {
1515 return '"' + escapeHtml(expr.string) + '"';
1516 }
21141517
2115 1518 case "anytype": {
2116 function categorizeDecls(decls,1519 return "anytype";
2117 typesList, namespacesList, errSetsList,1520 }
2118 fnsList, varsList, valsList, testsList) {
21191521
2120 for (let i = 0; i < decls.length; i += 1) {1522 case "this": {
2121 let decl = zigAnalysis.decls[decls[i]];1523 return "@This()";
2122 let declValue = resolveValue(decl.value);1524 }
21231525
2124 if (decl.isTest) {1526 case "type": {
2125 testsList.push(decl);1527 let name = "";
2126 continue;1528
2127 }1529 let typeObj = expr.type;
1530 if (typeof typeObj === "number") typeObj = zigAnalysis.types[typeObj];
1531 switch (typeObj.kind) {
1532 default:
1533 throw "TODO";
1534 case typeKinds.Struct: {
1535 let structObj = typeObj;
1536 return structObj;
1537 }
1538 case typeKinds.Enum: {
1539 let enumObj = typeObj;
1540 return enumObj;
1541 }
1542 case typeKinds.Opaque: {
1543 let opaqueObj = typeObj;
21281544
2129 if (decl.kind === 'var') {1545 return opaqueObj.name;
2130 varsList.push(decl);1546 }
2131 continue;1547 case typeKinds.ComptimeExpr: {
1548 return "anyopaque";
1549 }
1550 case typeKinds.Array: {
1551 let arrayObj = typeObj;
1552 let name = "[";
1553 let lenName = exprName(arrayObj.len, opts);
1554 let sentinel = arrayObj.sentinel
1555 ? ":" + exprName(arrayObj.sentinel, opts)
1556 : "";
1557 // let is_mutable = arrayObj.is_multable ? "const " : "";
1558
1559 if (opts.wantHtml) {
1560 name +=
1561 '<span class="tok-number">' + lenName + sentinel + "</span>";
1562 } else {
1563 name += lenName + sentinel;
2132 }1564 }
21331565 name += "]";
2134 if (decl.kind === 'const') {1566 // name += is_mutable;
2135 if ("type" in declValue.expr) {1567 name += exprName(arrayObj.child, opts);
2136 // We have the actual type expression at hand.1568 return name;
2137 const typeExpr = zigAnalysis.types[declValue.expr.type];1569 }
2138 if (typeExpr.kind == typeKinds.Fn) {1570 case typeKinds.Optional:
2139 const funcRetExpr = resolveValue({1571 return "?" + exprName(typeObj.child, opts);
2140 expr: (typeExpr).ret1572 case typeKinds.Pointer: {
2141 });1573 let ptrObj = typeObj;
2142 if ("type" in funcRetExpr.expr && funcRetExpr.expr.type == typeTypeId) {1574 let sentinel = ptrObj.sentinel
2143 if (typeIsErrSet(declValue.expr.type)) {1575 ? ":" + exprName(ptrObj.sentinel, opts)
2144 errSetsList.push(decl);1576 : "";
2145 } else if (typeIsStructWithNoFields(declValue.expr.type)) {1577 let is_mutable = !ptrObj.is_mutable ? "const " : "";
2146 namespacesList.push(decl);1578 let name = "";
2147 } else {1579 switch (ptrObj.size) {
2148 typesList.push(decl);1580 default:
2149 }1581 console.log("TODO: implement unhandled pointer size case");
2150 } else {1582 case pointerSizeEnum.One:
2151 fnsList.push(decl);1583 name += "*";
2152 }1584 name += is_mutable;
2153 } else {1585 break;
2154 if (typeIsErrSet(declValue.expr.type)) {1586 case pointerSizeEnum.Many:
2155 errSetsList.push(decl);1587 name += "[*";
2156 } else if (typeIsStructWithNoFields(declValue.expr.type)) {1588 name += sentinel;
2157 namespacesList.push(decl);1589 name += "]";
2158 } else {1590 name += is_mutable;
2159 typesList.push(decl);1591 break;
2160 }1592 case pointerSizeEnum.Slice:
2161 }1593 if (ptrObj.is_ref) {
2162 } else if ("typeRef" in declValue) {1594 name += "*";
2163 if ("type" in declValue.typeRef && declValue.typeRef == typeTypeId) {1595 }
2164 // We don't know what the type expression is, but we know it's a type.1596 name += "[";
2165 typesList.push(decl);1597 name += sentinel;
2166 } else {1598 name += "]";
2167 valsList.push(decl);1599 name += is_mutable;
2168 }1600 break;
1601 case pointerSizeEnum.C:
1602 name += "[*c";
1603 name += sentinel;
1604 name += "]";
1605 name += is_mutable;
1606 break;
1607 }
1608 // @check: after the major changes in arrays the consts are came from switch above
1609 // if (!ptrObj.is_mutable) {
1610 // if (opts.wantHtml) {
1611 // name += '<span class="tok-kw">const</span> ';
1612 // } else {
1613 // name += "const ";
1614 // }
1615 // }
1616 if (ptrObj.is_allowzero) {
1617 name += "allowzero ";
1618 }
1619 if (ptrObj.is_volatile) {
1620 name += "volatile ";
1621 }
1622 if (ptrObj.has_addrspace) {
1623 name += "addrspace(";
1624 name += "." + "";
1625 name += ") ";
1626 }
1627 if (ptrObj.has_align) {
1628 let align = exprName(ptrObj.align, opts);
1629 if (opts.wantHtml) {
1630 name += '<span class="tok-kw">align</span>(';
1631 } else {
1632 name += "align(";
1633 }
1634 if (opts.wantHtml) {
1635 name += '<span class="tok-number">' + align + "</span>";
1636 } else {
1637 name += align;
1638 }
1639 if (ptrObj.hostIntBytes != null) {
1640 name += ":";
1641 if (opts.wantHtml) {
1642 name +=
1643 '<span class="tok-number">' +
1644 ptrObj.bitOffsetInHost +
1645 "</span>";
2169 } else {1646 } else {
2170 valsList.push(decl);1647 name += ptrObj.bitOffsetInHost;
2171 }1648 }
1649 name += ":";
1650 if (opts.wantHtml) {
1651 name +=
1652 '<span class="tok-number">' +
1653 ptrObj.hostIntBytes +
1654 "</span>";
1655 } else {
1656 name += ptrObj.hostIntBytes;
1657 }
1658 }
1659 name += ") ";
2172 }1660 }
2173 }1661 //name += typeValueName(ptrObj.child, wantHtml, wantSubLink, null);
2174 }1662 name += exprName(ptrObj.child, opts);
1663 return name;
1664 }
1665 case typeKinds.Float: {
1666 let floatObj = typeObj;
21751667
2176 1668 if (opts.wantHtml) {
2177 function renderContainer(container) {1669 return '<span class="tok-type">' + floatObj.name + "</span>";
2178 1670 } else {
2179 let typesList = [];1671 return floatObj.name;
2180
2181 let namespacesList = [];
2182
2183 let errSetsList = [];
2184
2185 let fnsList = [];
2186
2187 let varsList = [];
2188
2189 let valsList = [];
2190
2191 let testsList = [];
2192
2193 categorizeDecls(container.pubDecls,
2194 typesList, namespacesList, errSetsList,
2195 fnsList, varsList, valsList, testsList);
2196 if (curNav.showPrivDecls) categorizeDecls(container.privDecls,
2197 typesList, namespacesList, errSetsList,
2198 fnsList, varsList, valsList, testsList);
2199
2200
2201 typesList.sort(byNameProperty);
2202 namespacesList.sort(byNameProperty);
2203 errSetsList.sort(byNameProperty);
2204 fnsList.sort(byNameProperty);
2205 varsList.sort(byNameProperty);
2206 valsList.sort(byNameProperty);
2207 testsList.sort(byNameProperty);
2208
2209 if (container.src != null) {
2210 let docs = zigAnalysis.astNodes[container.src].docs;
2211 if (docs != null) {
2212 domTldDocs.innerHTML = markdown(docs);
2213 domTldDocs.classList.remove("hidden");
2214 }1672 }
2215 }1673 }
22161674 case typeKinds.Int: {
2217 if (typesList.length !== 0) {1675 let intObj = typeObj;
2218 window.x = typesList;1676 let name = intObj.name;
2219 resizeDomList(domListTypes, typesList.length, '<li><a href="#"></a></li>');1677 if (opts.wantHtml) {
2220 for (let i = 0; i < typesList.length; i += 1) {1678 return '<span class="tok-type">' + name + "</span>";
2221 let liDom = domListTypes.children[i];1679 } else {
2222 let aDom = liDom.children[0];1680 return name;
2223 let decl = typesList[i];
2224 aDom.textContent = decl.name;
2225 aDom.setAttribute('href', navLinkDecl(decl.name));
2226 }1681 }
2227 domSectTypes.classList.remove("hidden");1682 }
2228 }1683 case typeKinds.ComptimeInt:
2229 if (namespacesList.length !== 0) {1684 if (opts.wantHtml) {
2230 resizeDomList(domListNamespaces, namespacesList.length, '<li><a href="#"></a></li>');1685 return '<span class="tok-type">comptime_int</span>';
2231 for (let i = 0; i < namespacesList.length; i += 1) {1686 } else {
2232 let liDom = domListNamespaces.children[i];1687 return "comptime_int";
2233 let aDom = liDom.children[0];
2234 let decl = namespacesList[i];
2235 aDom.textContent = decl.name;
2236 aDom.setAttribute('href', navLinkDecl(decl.name));
2237 }1688 }
2238 domSectNamespaces.classList.remove("hidden");1689 case typeKinds.ComptimeFloat:
2239 }1690 if (opts.wantHtml) {
22401691 return '<span class="tok-type">comptime_float</span>';
2241 if (errSetsList.length !== 0) {1692 } else {
2242 resizeDomList(domListErrSets, errSetsList.length, '<li><a href="#"></a></li>');1693 return "comptime_float";
2243 for (let i = 0; i < errSetsList.length; i += 1) {
2244 let liDom = domListErrSets.children[i];
2245 let aDom = liDom.children[0];
2246 let decl = errSetsList[i];
2247 aDom.textContent = decl.name;
2248 aDom.setAttribute('href', navLinkDecl(decl.name));
2249 }1694 }
2250 domSectErrSets.classList.remove("hidden");1695 case typeKinds.Type:
2251 }1696 if (opts.wantHtml) {
22521697 return '<span class="tok-type">type</span>';
2253 if (fnsList.length !== 0) {1698 } else {
2254 resizeDomList(domListFns, fnsList.length, '<div><dt></dt><dd></dd></div>');1699 return "type";
22551700 }
2256 for (let i = 0; i < fnsList.length; i += 1) {1701 case typeKinds.Bool:
2257 let decl = fnsList[i];1702 if (opts.wantHtml) {
2258 let trDom = domListFns.children[i];1703 return '<span class="tok-type">bool</span>';
22591704 } else {
2260 let tdFnCode = trDom.children[0];1705 return "bool";
2261 let tdDesc = trDom.children[1];1706 }
22621707 case typeKinds.Void:
2263 let declType = resolveValue(decl.value);1708 if (opts.wantHtml) {
2264 console.assert("type" in declType.expr);1709 return '<span class="tok-type">void</span>';
22651710 } else {
2266 tdFnCode.innerHTML = exprName(declType.expr,{1711 return "void";
2267 wantHtml: true,1712 }
2268 wantLink: true,1713 case typeKinds.EnumLiteral:
2269 fnDecl: decl,1714 if (opts.wantHtml) {
2270 linkFnNameDecl: navLinkDecl(decl.name),1715 return '<span class="tok-type">(enum literal)</span>';
2271 });1716 } else {
1717 return "(enum literal)";
1718 }
1719 case typeKinds.NoReturn:
1720 if (opts.wantHtml) {
1721 return '<span class="tok-type">noreturn</span>';
1722 } else {
1723 return "noreturn";
1724 }
1725 case typeKinds.ErrorSet: {
1726 let errSetObj = typeObj;
1727 if (errSetObj.fields == null) {
1728 return '<span class="tok-type">anyerror</span>';
1729 } else {
1730 // throw "TODO";
1731 let html = "error{" + errSetObj.fields[0].name + "}";
1732 return html;
1733 }
1734 }
22721735
2273 let docs = zigAnalysis.astNodes[decl.src].docs;1736 case typeKinds.ErrorUnion: {
2274 if (docs != null) {1737 let errUnionObj = typeObj;
2275 tdDesc.innerHTML = shortDescMarkdown(docs);1738 let lhs = exprName(errUnionObj.lhs, opts);
1739 let rhs = exprName(errUnionObj.rhs, opts);
1740 return lhs + "!" + rhs;
1741 }
1742 case typeKinds.InferredErrorUnion: {
1743 let errUnionObj = typeObj;
1744 let payload = exprName(errUnionObj.payload, opts);
1745 return "!" + payload;
1746 }
1747 case typeKinds.Fn: {
1748 let fnObj = typeObj;
1749 let payloadHtml = "";
1750 if (opts.wantHtml) {
1751 if (fnObj.is_extern) {
1752 payloadHtml += "pub extern ";
1753 }
1754 if (fnObj.has_lib_name) {
1755 payloadHtml += '"' + fnObj.lib_name + '" ';
1756 }
1757 payloadHtml += '<span class="tok-kw">fn</span>';
1758 if (opts.fnDecl) {
1759 payloadHtml += ' <span class="tok-fn">';
1760 if (opts.linkFnNameDecl) {
1761 payloadHtml +=
1762 '<a href="' +
1763 opts.linkFnNameDecl +
1764 '">' +
1765 escapeHtml(opts.fnDecl.name) +
1766 "</a>";
2276 } else {1767 } else {
2277 tdDesc.textContent = "";1768 payloadHtml += escapeHtml(opts.fnDecl.name);
2278 }1769 }
2279 }1770 payloadHtml += "</span>";
2280 domSectFns.classList.remove("hidden");1771 }
2281 }1772 } else {
1773 payloadHtml += "fn ";
1774 }
1775 payloadHtml += "(";
1776 if (fnObj.params) {
1777 let fields = null;
1778 let isVarArgs = false;
1779 let fnNode = zigAnalysis.astNodes[fnObj.src];
1780 fields = fnNode.fields;
1781 isVarArgs = fnNode.varArgs;
1782
1783 for (let i = 0; i < fnObj.params.length; i += 1) {
1784 if (i != 0) {
1785 payloadHtml += ", ";
1786 }
1787
1788 payloadHtml +=
1789 "<span class='argBreaker'><br>&nbsp;&nbsp;&nbsp;&nbsp;</span>";
1790 let value = fnObj.params[i];
1791 let paramValue = resolveValue({ expr: value });
22821792
2283 let containerNode = zigAnalysis.astNodes[container.src];1793 if (fields != null) {
2284 if (containerNode.fields && containerNode.fields.length > 0) {1794 let paramNode = zigAnalysis.astNodes[fields[i]];
2285 resizeDomList(domListFields, containerNode.fields.length, '<div></div>');
22861795
2287 for (let i = 0; i < containerNode.fields.length; i += 1) {1796 if (paramNode.varArgs) {
2288 let fieldNode = zigAnalysis.astNodes[containerNode.fields[i]];1797 payloadHtml += "...";
2289 let divDom = domListFields.children[i];1798 continue;
2290 let fieldName = (fieldNode.name);1799 }
2291 let docs = fieldNode.docs;
2292 let docsNonEmpty = docs != null && docs !== "";
2293 let extraPreClass = docsNonEmpty ? " fieldHasDocs" : "";
22941800
2295 let html = '<div class="mobile-scroll-container"><pre class="scroll-item' + extraPreClass + '">' + escapeHtml(fieldName);1801 if (paramNode.noalias) {
1802 if (opts.wantHtml) {
1803 payloadHtml += '<span class="tok-kw">noalias</span> ';
1804 } else {
1805 payloadHtml += "noalias ";
1806 }
1807 }
22961808
2297 if (container.kind === typeKinds.Enum) {1809 if (paramNode.comptime) {
2298 html += ' = <span class="tok-number">' + fieldName + '</span>';1810 if (opts.wantHtml) {
2299 } else {1811 payloadHtml += '<span class="tok-kw">comptime</span> ';
2300 let fieldTypeExpr = container.fields[i];1812 } else {
2301 html += ": ";1813 payloadHtml += "comptime ";
2302 let name = exprName(fieldTypeExpr, false, false);1814 }
2303 html += '<span class="tok-kw">'+ name +'</span>';1815 }
2304 let tsn = typeShorthandName(fieldTypeExpr);
2305 if (tsn) {
2306 html += '<span> ('+ tsn +')</span>';
23071816
1817 let paramName = paramNode.name;
1818 if (paramName != null) {
1819 // skip if it matches the type name
1820 if (!shouldSkipParamName(paramValue, paramName)) {
1821 payloadHtml += paramName + ": ";
2308 }1822 }
1823 }
2309 }1824 }
23101825
2311 html += ',</pre></div>';1826 if (isVarArgs && i === fnObj.params.length - 1) {
23121827 payloadHtml += "...";
2313 if (docsNonEmpty) {1828 } else if ("alignOf" in value) {
2314 html += '<div class="fieldDocs">' + markdown(docs) + '</div>';1829 if (opts.wantHtml) {
1830 payloadHtml += '<a href="">';
1831 payloadHtml +=
1832 '<span class="tok-kw" style="color:lightblue;">' +
1833 exprName(value, opts) +
1834 "</span>";
1835 payloadHtml += "</a>";
1836 } else {
1837 payloadHtml += exprName(value, opts);
1838 }
1839 } else if ("typeOf" in value) {
1840 if (opts.wantHtml) {
1841 payloadHtml += '<a href="">';
1842 payloadHtml +=
1843 '<span class="tok-kw" style="color:lightblue;">' +
1844 exprName(value, opts) +
1845 "</span>";
1846 payloadHtml += "</a>";
1847 } else {
1848 payloadHtml += exprName(value, opts);
1849 }
1850 } else if ("typeOf_peer" in value) {
1851 if (opts.wantHtml) {
1852 payloadHtml += '<a href="">';
1853 payloadHtml +=
1854 '<span class="tok-kw" style="color:lightblue;">' +
1855 exprName(value, opts) +
1856 "</span>";
1857 payloadHtml += "</a>";
1858 } else {
1859 payloadHtml += exprName(value, opts);
1860 }
1861 } else if ("declRef" in value) {
1862 if (opts.wantHtml) {
1863 payloadHtml += '<a href="">';
1864 payloadHtml +=
1865 '<span class="tok-kw" style="color:lightblue;">' +
1866 exprName(value, opts) +
1867 "</span>";
1868 payloadHtml += "</a>";
1869 } else {
1870 payloadHtml += exprName(value, opts);
1871 }
1872 } else if ("call" in value) {
1873 if (opts.wantHtml) {
1874 payloadHtml += '<a href="">';
1875 payloadHtml +=
1876 '<span class="tok-kw" style="color:lightblue;">' +
1877 exprName(value, opts) +
1878 "</span>";
1879 payloadHtml += "</a>";
1880 } else {
1881 payloadHtml += exprName(value, opts);
1882 }
1883 } else if ("refPath" in value) {
1884 if (opts.wantHtml) {
1885 payloadHtml += '<a href="">';
1886 payloadHtml +=
1887 '<span class="tok-kw" style="color:lightblue;">' +
1888 exprName(value, opts) +
1889 "</span>";
1890 payloadHtml += "</a>";
1891 } else {
1892 payloadHtml += exprName(value, opts);
1893 }
1894 } else if ("type" in value) {
1895 let name = exprName(value, {
1896 wantHtml: false,
1897 wantLink: false,
1898 fnDecl: opts.fnDecl,
1899 linkFnNameDecl: opts.linkFnNameDecl,
1900 });
1901 payloadHtml += '<span class="tok-kw">' + name + "</span>";
1902 } else if ("binOpIndex" in value) {
1903 payloadHtml += exprName(value, opts);
1904 } else if ("comptimeExpr" in value) {
1905 let comptimeExpr =
1906 zigAnalysis.comptimeExprs[value.comptimeExpr].code;
1907 if (opts.wantHtml) {
1908 payloadHtml +=
1909 '<span class="tok-kw">' + comptimeExpr + "</span>";
1910 } else {
1911 payloadHtml += comptimeExpr;
1912 }
1913 } else if (opts.wantHtml) {
1914 payloadHtml += '<span class="tok-kw">anytype</span>';
1915 } else {
1916 payloadHtml += "anytype";
2315 }1917 }
2316 divDom.innerHTML = html;1918 }
2317 }1919 }
2318 domSectFields.classList.remove("hidden");
2319 }
2320
2321 if (varsList.length !== 0) {
2322 resizeDomList(domListGlobalVars, varsList.length,
2323 '<tr><td><a href="#"></a></td><td></td><td></td></tr>');
2324 for (let i = 0; i < varsList.length; i += 1) {
2325 let decl = varsList[i];
2326 let trDom = domListGlobalVars.children[i];
23271920
2328 let tdName = trDom.children[0];1921 payloadHtml += "<span class='argBreaker'>,<br></span>";
2329 let tdNameA = tdName.children[0];1922 payloadHtml += ") ";
2330 let tdType = trDom.children[1];
2331 let tdDesc = trDom.children[2];
23321923
2333 tdNameA.setAttribute('href', navLinkDecl(decl.name));1924 if (fnObj.has_align) {
2334 tdNameA.textContent = decl.name;1925 let align = zigAnalysis.exprs[fnObj.align];
23351926 payloadHtml += "align(" + exprName(align, opts) + ") ";
2336 tdType.innerHTML = typeValueName(typeOfDecl(decl), true, true);1927 }
1928 if (fnObj.has_cc) {
1929 let cc = zigAnalysis.exprs[fnObj.cc];
1930 if (cc) {
1931 payloadHtml += "callconv(." + cc.enumLiteral + ") ";
1932 }
1933 }
23371934
2338 let docs = zigAnalysis.astNodes[decl.src].docs;1935 if (fnObj.is_inferred_error) {
2339 if (docs != null) {1936 payloadHtml += "!";
2340 tdDesc.innerHTML = shortDescMarkdown(docs);1937 }
2341 } else {1938 if (fnObj.ret != null) {
2342 tdDesc.textContent = "";1939 payloadHtml += exprName(fnObj.ret, opts);
2343 }1940 } else if (opts.wantHtml) {
1941 payloadHtml += '<span class="tok-kw">anytype</span>';
1942 } else {
1943 payloadHtml += "anytype";
2344 }1944 }
2345 domSectGlobalVars.classList.remove("hidden");1945 return payloadHtml;
1946 }
1947 // if (wantHtml) {
1948 // return escapeHtml(typeObj.name);
1949 // } else {
1950 // return typeObj.name;
1951 // }
1952 }
1953 }
1954 }
1955 }
1956
1957 function shouldSkipParamName(typeRef, paramName) {
1958 let resolvedTypeRef = resolveValue({ expr: typeRef });
1959 if ("type" in resolvedTypeRef) {
1960 let typeObj = zigAnalysis.types[resolvedTypeRef.type];
1961 if (typeObj.kind === typeKinds.Pointer) {
1962 let ptrObj = typeObj;
1963 if (getPtrSize(ptrObj) === pointerSizeEnum.One) {
1964 const value = resolveValue(ptrObj.child);
1965 return typeValueName(value, false, true).toLowerCase() === paramName;
2346 }1966 }
1967 }
1968 }
1969 return false;
1970 }
1971
1972 function getPtrSize(typeObj) {
1973 return typeObj.size == null ? pointerSizeEnum.One : typeObj.size;
1974 }
1975
1976 function renderType(typeObj) {
1977 let name;
1978 if (
1979 rootIsStd &&
1980 typeObj ===
1981 zigAnalysis.types[zigAnalysis.packages[zigAnalysis.rootPkg].main]
1982 ) {
1983 name = "std";
1984 } else {
1985 name = exprName({ type: typeObj }, false, false);
1986 }
1987 if (name != null && name != "") {
1988 domHdrName.innerText =
1989 name + " (" + zigAnalysis.typeKinds[typeObj.kind] + ")";
1990 domHdrName.classList.remove("hidden");
1991 }
1992 if (typeObj.kind == typeKinds.ErrorSet) {
1993 renderErrorSet(typeObj);
1994 }
1995 }
23471996
2348 if (valsList.length !== 0) {1997 function renderErrorSet(errSetType) {
2349 resizeDomList(domListValues, valsList.length,1998 if (errSetType.fields == null) {
2350 '<tr><td><a href="#"></a></td><td></td><td></td></tr>');1999 domFnErrorsAnyError.classList.remove("hidden");
2351 for (let i = 0; i < valsList.length; i += 1) {2000 } else {
2352 let decl = valsList[i];2001 let errorList = [];
2353 let trDom = domListValues.children[i];2002 for (let i = 0; i < errSetType.fields.length; i += 1) {
2003 let errObj = errSetType.fields[i];
2004 //let srcObj = zigAnalysis.astNodes[errObj.src];
2005 errorList.push(errObj);
2006 }
2007 errorList.sort(function (a, b) {
2008 return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
2009 });
2010
2011 resizeDomListDl(domListFnErrors, errorList.length);
2012 for (let i = 0; i < errorList.length; i += 1) {
2013 let nameTdDom = domListFnErrors.children[i * 2 + 0];
2014 let descTdDom = domListFnErrors.children[i * 2 + 1];
2015 nameTdDom.textContent = errorList[i].name;
2016 let docs = errorList[i].docs;
2017 if (docs != null) {
2018 descTdDom.innerHTML = markdown(docs);
2019 } else {
2020 descTdDom.textContent = "";
2021 }
2022 }
2023 domTableFnErrors.classList.remove("hidden");
2024 }
2025 domSectFnErrors.classList.remove("hidden");
2026 }
2027
2028 // function allCompTimeFnCallsHaveTypeResult(typeIndex, value) {
2029 // let srcIndex = zigAnalysis.fns[value].src;
2030 // let calls = nodesToCallsMap[srcIndex];
2031 // if (calls == null) return false;
2032 // for (let i = 0; i < calls.length; i += 1) {
2033 // let call = zigAnalysis.calls[calls[i]];
2034 // if (call.result.type !== typeTypeId) return false;
2035 // }
2036 // return true;
2037 // }
2038 //
2039 // function allCompTimeFnCallsResult(calls) {
2040 // let firstTypeObj = null;
2041 // let containerObj = {
2042 // privDecls: [],
2043 // };
2044 // for (let callI = 0; callI < calls.length; callI += 1) {
2045 // let call = zigAnalysis.calls[calls[callI]];
2046 // if (call.result.type !== typeTypeId) return null;
2047 // let typeObj = zigAnalysis.types[call.result.value];
2048 // if (!typeKindIsContainer(typeObj.kind)) return null;
2049 // if (firstTypeObj == null) {
2050 // firstTypeObj = typeObj;
2051 // containerObj.src = typeObj.src;
2052 // } else if (firstTypeObj.src !== typeObj.src) {
2053 // return null;
2054 // }
2055 //
2056 // if (containerObj.fields == null) {
2057 // containerObj.fields = (typeObj.fields || []).concat([]);
2058 // } else for (let fieldI = 0; fieldI < typeObj.fields.length; fieldI += 1) {
2059 // let prev = containerObj.fields[fieldI];
2060 // let next = typeObj.fields[fieldI];
2061 // if (prev === next) continue;
2062 // if (typeof(prev) === 'object') {
2063 // if (prev[next] == null) prev[next] = typeObj;
2064 // } else {
2065 // containerObj.fields[fieldI] = {};
2066 // containerObj.fields[fieldI][prev] = firstTypeObj;
2067 // containerObj.fields[fieldI][next] = typeObj;
2068 // }
2069 // }
2070 //
2071 // if (containerObj.pubDecls == null) {
2072 // containerObj.pubDecls = (typeObj.pubDecls || []).concat([]);
2073 // } else for (let declI = 0; declI < typeObj.pubDecls.length; declI += 1) {
2074 // let prev = containerObj.pubDecls[declI];
2075 // let next = typeObj.pubDecls[declI];
2076 // if (prev === next) continue;
2077 // // TODO instead of showing "examples" as the public declarations,
2078 // // do logic like this:
2079 // //if (typeof(prev) !== 'object') {
2080 // // let newDeclId = zigAnalysis.decls.length;
2081 // // prev = clone(zigAnalysis.decls[prev]);
2082 // // prev.id = newDeclId;
2083 // // zigAnalysis.decls.push(prev);
2084 // // containerObj.pubDecls[declI] = prev;
2085 // //}
2086 // //mergeDecls(prev, next, firstTypeObj, typeObj);
2087 // }
2088 // }
2089 // for (let declI = 0; declI < containerObj.pubDecls.length; declI += 1) {
2090 // let decl = containerObj.pubDecls[declI];
2091 // if (typeof(decl) === 'object') {
2092 // containerObj.pubDecls[declI] = containerObj.pubDecls[declI].id;
2093 // }
2094 // }
2095 // return containerObj;
2096 // }
2097
2098 function renderValue(decl) {
2099 let resolvedValue = resolveValue(decl.value);
2100
2101 if (resolvedValue.expr.fieldRef) {
2102 const declRef = decl.value.expr.refPath[0].declRef;
2103 const type = zigAnalysis.decls[declRef];
2104 domFnProtoCode.innerHTML =
2105 '<span class="tok-kw">const</span> ' +
2106 escapeHtml(decl.name) +
2107 ": " +
2108 type.name +
2109 " = " +
2110 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +
2111 ";";
2112 } else if (
2113 resolvedValue.expr.string !== undefined ||
2114 resolvedValue.expr.call !== undefined ||
2115 resolvedValue.expr.comptimeExpr
2116 ) {
2117 domFnProtoCode.innerHTML =
2118 '<span class="tok-kw">const</span> ' +
2119 escapeHtml(decl.name) +
2120 ": " +
2121 exprName(resolvedValue.expr, { wantHtml: true, wantLink: true }) +
2122 " = " +
2123 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +
2124 ";";
2125 } else if (resolvedValue.expr.compileError) {
2126 domFnProtoCode.innerHTML =
2127 '<span class="tok-kw">const</span> ' +
2128 escapeHtml(decl.name) +
2129 " = " +
2130 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +
2131 ";";
2132 } else {
2133 domFnProtoCode.innerHTML =
2134 '<span class="tok-kw">const</span> ' +
2135 escapeHtml(decl.name) +
2136 ": " +
2137 exprName(resolvedValue.typeRef, { wantHtml: true, wantLink: true }) +
2138 " = " +
2139 exprName(decl.value.expr, { wantHtml: true, wantLink: true }) +
2140 ";";
2141 }
23542142
2355 let tdName = trDom.children[0];2143 let docs = zigAnalysis.astNodes[decl.src].docs;
2356 let tdNameA = tdName.children[0];2144 if (docs != null) {
2357 let tdType = trDom.children[1];2145 domTldDocs.innerHTML = markdown(docs);
2358 let tdDesc = trDom.children[2];2146 domTldDocs.classList.remove("hidden");
2147 }
23592148
2360 tdNameA.setAttribute('href', navLinkDecl(decl.name));2149 domFnProto.classList.remove("hidden");
2361 tdNameA.textContent = decl.name;2150 }
2151
2152 function renderVar(decl) {
2153 let declTypeRef = typeOfDecl(decl);
2154 domFnProtoCode.innerHTML =
2155 '<span class="tok-kw">var</span> ' +
2156 escapeHtml(decl.name) +
2157 ": " +
2158 typeValueName(declTypeRef, true, true);
2159
2160 let docs = zigAnalysis.astNodes[decl.src].docs;
2161 if (docs != null) {
2162 domTldDocs.innerHTML = markdown(docs);
2163 domTldDocs.classList.remove("hidden");
2164 }
23622165
2363 tdType.innerHTML = exprName(walkResultTypeRef(decl.value),2166 domFnProto.classList.remove("hidden");
2364 {wantHtml:true, wantLink:true});2167 }
2168
2169 function categorizeDecls(
2170 decls,
2171 typesList,
2172 namespacesList,
2173 errSetsList,
2174 fnsList,
2175 varsList,
2176 valsList,
2177 testsList
2178 ) {
2179 for (let i = 0; i < decls.length; i += 1) {
2180 let decl = zigAnalysis.decls[decls[i]];
2181 let declValue = resolveValue(decl.value);
2182
2183 if (decl.isTest) {
2184 testsList.push(decl);
2185 continue;
2186 }
23652187
2366 let docs = zigAnalysis.astNodes[decl.src].docs;2188 if (decl.kind === "var") {
2367 if (docs != null) {2189 varsList.push(decl);
2368 tdDesc.innerHTML = shortDescMarkdown(docs);2190 continue;
2369 } else {2191 }
2370 tdDesc.textContent = "";2192
2371 }2193 if (decl.kind === "const") {
2194 if ("type" in declValue.expr) {
2195 // We have the actual type expression at hand.
2196 const typeExpr = zigAnalysis.types[declValue.expr.type];
2197 if (typeExpr.kind == typeKinds.Fn) {
2198 const funcRetExpr = resolveValue({
2199 expr: typeExpr.ret,
2200 });
2201 if (
2202 "type" in funcRetExpr.expr &&
2203 funcRetExpr.expr.type == typeTypeId
2204 ) {
2205 if (typeIsErrSet(declValue.expr.type)) {
2206 errSetsList.push(decl);
2207 } else if (typeIsStructWithNoFields(declValue.expr.type)) {
2208 namespacesList.push(decl);
2209 } else {
2210 typesList.push(decl);
2211 }
2212 } else {
2213 fnsList.push(decl);
2214 }
2215 } else {
2216 if (typeIsErrSet(declValue.expr.type)) {
2217 errSetsList.push(decl);
2218 } else if (typeIsStructWithNoFields(declValue.expr.type)) {
2219 namespacesList.push(decl);
2220 } else {
2221 typesList.push(decl);
2372 }2222 }
2373 domSectValues.classList.remove("hidden");2223 }
2224 } else if ("typeRef" in declValue) {
2225 if ("type" in declValue.typeRef && declValue.typeRef == typeTypeId) {
2226 // We don't know what the type expression is, but we know it's a type.
2227 typesList.push(decl);
2228 } else {
2229 valsList.push(decl);
2230 }
2231 } else {
2232 valsList.push(decl);
2374 }2233 }
2234 }
2235 }
2236 }
2237
2238 function renderContainer(container) {
2239 let typesList = [];
2240
2241 let namespacesList = [];
2242
2243 let errSetsList = [];
2244
2245 let fnsList = [];
2246
2247 let varsList = [];
2248
2249 let valsList = [];
2250
2251 let testsList = [];
2252
2253 categorizeDecls(
2254 container.pubDecls,
2255 typesList,
2256 namespacesList,
2257 errSetsList,
2258 fnsList,
2259 varsList,
2260 valsList,
2261 testsList
2262 );
2263 if (curNav.showPrivDecls)
2264 categorizeDecls(
2265 container.privDecls,
2266 typesList,
2267 namespacesList,
2268 errSetsList,
2269 fnsList,
2270 varsList,
2271 valsList,
2272 testsList
2273 );
2274
2275 typesList.sort(byNameProperty);
2276 namespacesList.sort(byNameProperty);
2277 errSetsList.sort(byNameProperty);
2278 fnsList.sort(byNameProperty);
2279 varsList.sort(byNameProperty);
2280 valsList.sort(byNameProperty);
2281 testsList.sort(byNameProperty);
2282
2283 if (container.src != null) {
2284 let docs = zigAnalysis.astNodes[container.src].docs;
2285 if (docs != null) {
2286 domTldDocs.innerHTML = markdown(docs);
2287 domTldDocs.classList.remove("hidden");
2288 }
2289 }
23752290
2376 if (testsList.length !== 0) {2291 if (typesList.length !== 0) {
2377 resizeDomList(domListTests, testsList.length,2292 window.x = typesList;
2378 '<tr><td><a href="#"></a></td><td></td><td></td></tr>');2293 resizeDomList(
2379 for (let i = 0; i < testsList.length; i += 1) {2294 domListTypes,
2380 let decl = testsList[i];2295 typesList.length,
2381 let trDom = domListTests.children[i];2296 '<li><a href="#"></a></li>'
23822297 );
2383 let tdName = trDom.children[0];2298 for (let i = 0; i < typesList.length; i += 1) {
2384 let tdNameA = tdName.children[0];2299 let liDom = domListTypes.children[i];
2385 let tdType = trDom.children[1];2300 let aDom = liDom.children[0];
2386 let tdDesc = trDom.children[2];2301 let decl = typesList[i];
2302 aDom.textContent = decl.name;
2303 aDom.setAttribute("href", navLinkDecl(decl.name));
2304 }
2305 domSectTypes.classList.remove("hidden");
2306 }
2307 if (namespacesList.length !== 0) {
2308 resizeDomList(
2309 domListNamespaces,
2310 namespacesList.length,
2311 '<li><a href="#"></a></li>'
2312 );
2313 for (let i = 0; i < namespacesList.length; i += 1) {
2314 let liDom = domListNamespaces.children[i];
2315 let aDom = liDom.children[0];
2316 let decl = namespacesList[i];
2317 aDom.textContent = decl.name;
2318 aDom.setAttribute("href", navLinkDecl(decl.name));
2319 }
2320 domSectNamespaces.classList.remove("hidden");
2321 }
23872322
2388 tdNameA.setAttribute('href', navLinkDecl(decl.name));2323 if (errSetsList.length !== 0) {
2389 tdNameA.textContent = decl.name;2324 resizeDomList(
2325 domListErrSets,
2326 errSetsList.length,
2327 '<li><a href="#"></a></li>'
2328 );
2329 for (let i = 0; i < errSetsList.length; i += 1) {
2330 let liDom = domListErrSets.children[i];
2331 let aDom = liDom.children[0];
2332 let decl = errSetsList[i];
2333 aDom.textContent = decl.name;
2334 aDom.setAttribute("href", navLinkDecl(decl.name));
2335 }
2336 domSectErrSets.classList.remove("hidden");
2337 }
23902338
2391 tdType.innerHTML = exprName(walkResultTypeRef(decl.value),2339 if (fnsList.length !== 0) {
2392 {wantHtml:true, wantLink:true});2340 resizeDomList(
2341 domListFns,
2342 fnsList.length,
2343 "<div><dt></dt><dd></dd></div>"
2344 );
23932345
2394 let docs = zigAnalysis.astNodes[decl.src].docs;2346 for (let i = 0; i < fnsList.length; i += 1) {
2395 if (docs != null) {2347 let decl = fnsList[i];
2396 tdDesc.innerHTML = shortDescMarkdown(docs);2348 let trDom = domListFns.children[i];
2397 } else {
2398 tdDesc.textContent = "";
2399 }
2400 }
2401 domSectTests.classList.remove("hidden");
2402 }
2403 }
24042349
2350 let tdFnCode = trDom.children[0];
2351 let tdDesc = trDom.children[1];
24052352
2406 2353 let declType = resolveValue(decl.value);
2407 function operatorCompare(a, b) {2354 console.assert("type" in declType.expr);
2408 if (a === b) {2355
2409 return 0;2356 tdFnCode.innerHTML = exprName(declType.expr, {
2410 } else if (a < b) {2357 wantHtml: true,
2411 return -1;2358 wantLink: true,
2359 fnDecl: decl,
2360 linkFnNameDecl: navLinkDecl(decl.name),
2361 });
2362
2363 let docs = zigAnalysis.astNodes[decl.src].docs;
2364 if (docs != null) {
2365 tdDesc.innerHTML = shortDescMarkdown(docs);
2412 } else {2366 } else {
2413 return 1;2367 tdDesc.textContent = "";
2414 }2368 }
2369 }
2370 domSectFns.classList.remove("hidden");
2415 }2371 }
24162372
2417 function detectRootIsStd() {2373 let containerNode = zigAnalysis.astNodes[container.src];
2418 let rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];2374 if (containerNode.fields && containerNode.fields.length > 0) {
2419 if (rootPkg.table["std"] == null) {2375 resizeDomList(domListFields, containerNode.fields.length, "<div></div>");
2420 // no std mapped into the root package2376
2421 return false;2377 for (let i = 0; i < containerNode.fields.length; i += 1) {
2378 let fieldNode = zigAnalysis.astNodes[containerNode.fields[i]];
2379 let divDom = domListFields.children[i];
2380 let fieldName = fieldNode.name;
2381 let docs = fieldNode.docs;
2382 let docsNonEmpty = docs != null && docs !== "";
2383 let extraPreClass = docsNonEmpty ? " fieldHasDocs" : "";
2384
2385 let html =
2386 '<div class="mobile-scroll-container"><pre class="scroll-item' +
2387 extraPreClass +
2388 '">' +
2389 escapeHtml(fieldName);
2390
2391 if (container.kind === typeKinds.Enum) {
2392 html += ' = <span class="tok-number">' + fieldName + "</span>";
2393 } else {
2394 let fieldTypeExpr = container.fields[i];
2395 html += ": ";
2396 let name = exprName(fieldTypeExpr, false, false);
2397 html += '<span class="tok-kw">' + name + "</span>";
2398 let tsn = typeShorthandName(fieldTypeExpr);
2399 if (tsn) {
2400 html += "<span> (" + tsn + ")</span>";
2401 }
2422 }2402 }
2423 let stdPkg = zigAnalysis.packages[rootPkg.table["std"]];
2424 if (stdPkg == null) return false;
2425 return rootPkg.file === stdPkg.file;
2426 }
24272403
2428 function indexTypeKinds() {2404 html += ",</pre></div>";
2429 let map = ({});
2430 for (let i = 0; i < zigAnalysis.typeKinds.length; i += 1) {
2431 map[zigAnalysis.typeKinds[i]] = i;
2432 }
2433 // This is just for debugging purposes, not needed to function
2434 let assertList = ["Type","Void","Bool","NoReturn","Int","Float","Pointer","Array","Struct",
2435 "ComptimeFloat","ComptimeInt","Undefined","Null","Optional","ErrorUnion","ErrorSet","Enum",
2436 "Union","Fn","BoundFn","Opaque","Frame","AnyFrame","Vector","EnumLiteral"];
2437 for (let i = 0; i < assertList.length; i += 1) {
2438 if (map[assertList[i]] == null) throw new Error("No type kind '" + assertList[i] + "' found");
2439 }
2440 return map;
2441 }
24422405
2443 function findTypeTypeId() {2406 if (docsNonEmpty) {
2444 for (let i = 0; i < zigAnalysis.types.length; i += 1) {2407 html += '<div class="fieldDocs">' + markdown(docs) + "</div>";
2445 if (zigAnalysis.types[i].kind == typeKinds.Type) {
2446 return i;
2447 }
2448 }2408 }
2449 throw new Error("No type 'type' found");2409 divDom.innerHTML = html;
2410 }
2411 domSectFields.classList.remove("hidden");
2450 }2412 }
24512413
2452 function updateCurNav() {2414 if (varsList.length !== 0) {
2415 resizeDomList(
2416 domListGlobalVars,
2417 varsList.length,
2418 '<tr><td><a href="#"></a></td><td></td><td></td></tr>'
2419 );
2420 for (let i = 0; i < varsList.length; i += 1) {
2421 let decl = varsList[i];
2422 let trDom = domListGlobalVars.children[i];
24532423
2454 curNav = {2424 let tdName = trDom.children[0];
2455 showPrivDecls: false,2425 let tdNameA = tdName.children[0];
2456 pkgNames: [],2426 let tdType = trDom.children[1];
2457 pkgObjs: [],2427 let tdDesc = trDom.children[2];
2458 declNames: [],
2459 declObjs: [],
2460 callName: null,
2461 };
2462 curNavSearch = "";
24632428
2464 if (location.hash[0] === '#' && location.hash.length > 1) {2429 tdNameA.setAttribute("href", navLinkDecl(decl.name));
2465 let query = location.hash.substring(1);2430 tdNameA.textContent = decl.name;
2466 if (query[0] === '*') {
2467 curNav.showPrivDecls = true;
2468 query = query.substring(1);
2469 }
24702431
2471 let qpos = query.indexOf("?");2432 tdType.innerHTML = typeValueName(typeOfDecl(decl), true, true);
2472 let nonSearchPart;
2473 if (qpos === -1) {
2474 nonSearchPart = query;
2475 } else {
2476 nonSearchPart = query.substring(0, qpos);
2477 curNavSearch = decodeURIComponent(query.substring(qpos + 1));
2478 }
24792433
2480 let parts = nonSearchPart.split(";");2434 let docs = zigAnalysis.astNodes[decl.src].docs;
2481 curNav.pkgNames = decodeURIComponent(parts[0]).split(".");2435 if (docs != null) {
2482 if (parts[1] != null) {2436 tdDesc.innerHTML = shortDescMarkdown(docs);
2483 curNav.declNames = decodeURIComponent(parts[1]).split(".");2437 } else {
2484 }2438 tdDesc.textContent = "";
2485 }2439 }
2440 }
2441 domSectGlobalVars.classList.remove("hidden");
2486 }2442 }
24872443
2488 function onHashChange() {2444 if (valsList.length !== 0) {
2489 updateCurNav();2445 resizeDomList(
2490 if (domSearch.value !== curNavSearch) {2446 domListValues,
2491 domSearch.value = curNavSearch;2447 valsList.length,
2492 }2448 '<tr><td><a href="#"></a></td><td></td><td></td></tr>'
2493 render();2449 );
2494 if (imFeelingLucky) {2450 for (let i = 0; i < valsList.length; i += 1) {
2495 imFeelingLucky = false;2451 let decl = valsList[i];
2496 activateSelectedResult();2452 let trDom = domListValues.children[i];
2497 }2453
2498 }2454 let tdName = trDom.children[0];
2455 let tdNameA = tdName.children[0];
2456 let tdType = trDom.children[1];
2457 let tdDesc = trDom.children[2];
2458
2459 tdNameA.setAttribute("href", navLinkDecl(decl.name));
2460 tdNameA.textContent = decl.name;
2461
2462 tdType.innerHTML = exprName(walkResultTypeRef(decl.value), {
2463 wantHtml: true,
2464 wantLink: true,
2465 });
24992466
2500 2467 let docs = zigAnalysis.astNodes[decl.src].docs;
2501 function findSubDecl(parentType, childName) {2468 if (docs != null) {
2502 {2469 tdDesc.innerHTML = shortDescMarkdown(docs);
2503 // Generic functions2470 } else {
2504 if ("value" in parentType) {2471 tdDesc.textContent = "";
2505 const rv = resolveValue(parentType.value);
2506 if ("type" in rv.expr) {
2507 const t = zigAnalysis.types[rv.expr.type];
2508 if (t.kind == typeKinds.Fn && t.generic_ret != null) {
2509 const rgr = resolveValue({expr: t.generic_ret});
2510 if ("type" in rgr.expr) {
2511 parentType = zigAnalysis.types[rgr.expr.type];
2512 }
2513 }
2514 }
2515 }
2516 }2472 }
2473 }
2474 domSectValues.classList.remove("hidden");
2475 }
25172476
2477 if (testsList.length !== 0) {
2478 resizeDomList(
2479 domListTests,
2480 testsList.length,
2481 '<tr><td><a href="#"></a></td><td></td><td></td></tr>'
2482 );
2483 for (let i = 0; i < testsList.length; i += 1) {
2484 let decl = testsList[i];
2485 let trDom = domListTests.children[i];
2486
2487 let tdName = trDom.children[0];
2488 let tdNameA = tdName.children[0];
2489 let tdType = trDom.children[1];
2490 let tdDesc = trDom.children[2];
2491
2492 tdNameA.setAttribute("href", navLinkDecl(decl.name));
2493 tdNameA.textContent = decl.name;
2494
2495 tdType.innerHTML = exprName(walkResultTypeRef(decl.value), {
2496 wantHtml: true,
2497 wantLink: true,
2498 });
25182499
2519 if (!parentType.pubDecls) return null;2500 let docs = zigAnalysis.astNodes[decl.src].docs;
2520 for (let i = 0; i < parentType.pubDecls.length; i += 1) {2501 if (docs != null) {
2521 let declIndex = parentType.pubDecls[i];2502 tdDesc.innerHTML = shortDescMarkdown(docs);
2522 let childDecl = zigAnalysis.decls[declIndex];2503 } else {
2523 if (childDecl.name === childName) {2504 tdDesc.textContent = "";
2524 return childDecl;
2525 }
2526 }
2527 if (!parentType.privDecls) return null;
2528 for (let i = 0; i < parentType.privDecls.length; i += 1) {
2529 let declIndex = parentType.privDecls[i];
2530 let childDecl = zigAnalysis.decls[declIndex];
2531 if (childDecl.name === childName) {
2532 return childDecl;
2533 }
2534 }2505 }
2535 return null;2506 }
2507 domSectTests.classList.remove("hidden");
2536 }2508 }
2509 }
25372510
2511 function operatorCompare(a, b) {
2512 if (a === b) {
2513 return 0;
2514 } else if (a < b) {
2515 return -1;
2516 } else {
2517 return 1;
2518 }
2519 }
25382520
2521 function detectRootIsStd() {
2522 let rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];
2523 if (rootPkg.table["std"] == null) {
2524 // no std mapped into the root package
2525 return false;
2526 }
2527 let stdPkg = zigAnalysis.packages[rootPkg.table["std"]];
2528 if (stdPkg == null) return false;
2529 return rootPkg.file === stdPkg.file;
2530 }
2531
2532 function indexTypeKinds() {
2533 let map = {};
2534 for (let i = 0; i < zigAnalysis.typeKinds.length; i += 1) {
2535 map[zigAnalysis.typeKinds[i]] = i;
2536 }
2537 // This is just for debugging purposes, not needed to function
2538 let assertList = [
2539 "Type",
2540 "Void",
2541 "Bool",
2542 "NoReturn",
2543 "Int",
2544 "Float",
2545 "Pointer",
2546 "Array",
2547 "Struct",
2548 "ComptimeFloat",
2549 "ComptimeInt",
2550 "Undefined",
2551 "Null",
2552 "Optional",
2553 "ErrorUnion",
2554 "ErrorSet",
2555 "Enum",
2556 "Union",
2557 "Fn",
2558 "BoundFn",
2559 "Opaque",
2560 "Frame",
2561 "AnyFrame",
2562 "Vector",
2563 "EnumLiteral",
2564 ];
2565 for (let i = 0; i < assertList.length; i += 1) {
2566 if (map[assertList[i]] == null)
2567 throw new Error("No type kind '" + assertList[i] + "' found");
2568 }
2569 return map;
2570 }
25392571
2572 function findTypeTypeId() {
2573 for (let i = 0; i < zigAnalysis.types.length; i += 1) {
2574 if (zigAnalysis.types[i].kind == typeKinds.Type) {
2575 return i;
2576 }
2577 }
2578 throw new Error("No type 'type' found");
2579 }
2580
2581 function updateCurNav() {
2582 curNav = {
2583 showPrivDecls: false,
2584 pkgNames: [],
2585 pkgObjs: [],
2586 declNames: [],
2587 declObjs: [],
2588 callName: null,
2589 };
2590 curNavSearch = "";
25402591
2541 function computeCanonicalPackagePaths() {2592 if (location.hash[0] === "#" && location.hash.length > 1) {
2542 let list = new Array(zigAnalysis.packages.length);2593 let query = location.hash.substring(1);
2543 // Now we try to find all the packages from root.2594 if (query[0] === "*") {
2544 let rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];2595 curNav.showPrivDecls = true;
2545 // Breadth-first to keep the path shortest possible.2596 query = query.substring(1);
2546 let stack = [{2597 }
2547 path: ([]),
2548 pkg: rootPkg,
2549 }];
2550 while (stack.length !== 0) {
2551 let item = (stack.shift());
2552 for (let key in item.pkg.table) {
2553 let childPkgIndex = item.pkg.table[key];
2554 if (list[childPkgIndex] != null) continue;
2555 let childPkg = zigAnalysis.packages[childPkgIndex];
2556 if (childPkg == null) continue;
25572598
2558 let newPath = item.path.concat([key])2599 let qpos = query.indexOf("?");
2559 list[childPkgIndex] = newPath;2600 let nonSearchPart;
2560 stack.push({2601 if (qpos === -1) {
2561 path: newPath,2602 nonSearchPart = query;
2562 pkg: childPkg,2603 } else {
2563 });2604 nonSearchPart = query.substring(0, qpos);
2564 }2605 curNavSearch = decodeURIComponent(query.substring(qpos + 1));
2565 }2606 }
2566 return list;
2567 }
2568
2569
2570
2571 function computeCanonDeclPaths() {
2572 let list = new Array(zigAnalysis.decls.length);
2573 canonTypeDecls = new Array(zigAnalysis.types.length);
2574
2575 for (let pkgI = 0; pkgI < zigAnalysis.packages.length; pkgI += 1) {
2576 if (pkgI === zigAnalysis.rootPkg && rootIsStd) continue;
2577 let pkg = zigAnalysis.packages[pkgI];
2578 let pkgNames = canonPkgPaths[pkgI];
2579 if (pkgNames === undefined) continue;
2580
2581 let stack = [{
2582 declNames: ([]),
2583 type: zigAnalysis.types[pkg.main],
2584 }];
2585 while (stack.length !== 0) {
2586 let item = (stack.shift());
2587
2588 if (isContainerType(item.type)) {
2589 let t = (item.type);
2590
2591 let len = t.pubDecls ? t.pubDecls.length : 0;
2592 for (let declI = 0; declI < len; declI += 1) {
2593 let mainDeclIndex = t.pubDecls[declI];
2594 if (list[mainDeclIndex] != null) continue;
2595
2596 let decl = zigAnalysis.decls[mainDeclIndex];
2597 let declVal = resolveValue(decl.value);
2598 let declNames = item.declNames.concat([decl.name]);
2599 list[mainDeclIndex] = {
2600 pkgNames: pkgNames,
2601 declNames: declNames,
2602 };
2603 if ("type" in declVal.expr) {
2604 let value = zigAnalysis.types[declVal.expr.type];
2605 if (declCanRepresentTypeKind(value.kind))
2606 {
2607 canonTypeDecls[declVal.type] = mainDeclIndex;
2608 }
2609
2610 if (isContainerType(value)) {
2611 stack.push({
2612 declNames: declNames,
2613 type:value,
2614 });
2615 }
2616
2617
2618 // Generic fun/ction
2619 if (value.kind == typeKinds.Fn && value.generic_ret != null) {
2620 let resolvedVal = resolveValue({ expr: value.generic_ret});
2621 if ("type" in resolvedVal.expr) {
2622 let generic_type = zigAnalysis.types[resolvedVal.expr.type];
2623 if (isContainerType(generic_type)){
2624 stack.push({
2625 declNames: declNames,
2626 type: generic_type,
2627 });
2628 }
2629 }
2630 }
2631 }
2632 }
2633 }
2634 }
2635 }
2636 return list;
2637 }
26382607
2639 2608 let parts = nonSearchPart.split(";");
2640 function getCanonDeclPath(index) {2609 curNav.pkgNames = decodeURIComponent(parts[0]).split(".");
2641 if (canonDeclPaths == null) {2610 if (parts[1] != null) {
2642 canonDeclPaths = computeCanonDeclPaths();2611 curNav.declNames = decodeURIComponent(parts[1]).split(".");
2643 }2612 }
2644 //let cd = (canonDeclPaths);
2645 return canonDeclPaths[index];
2646 }2613 }
2614 }
26472615
2648 2616 function onHashChange() {
2649 function getCanonTypeDecl(index) {2617 updateCurNav();
2650 getCanonDeclPath(0);2618 if (domSearch.value !== curNavSearch) {
2651 //let ct = (canonTypeDecls);2619 domSearch.value = curNavSearch;
2652 return canonTypeDecls[index];
2653 }2620 }
26542621 render();
2655 2622 if (imFeelingLucky) {
2656 function escapeHtml(text) {2623 imFeelingLucky = false;
2657 return text.replace(/[&"<>]/g, function (m) {2624 activateSelectedResult();
2658 return escapeHtmlReplacements[m];
2659 });
2660 }2625 }
26612626 }
26622627
2663 function shortDescMarkdown(docs) {2628 function findSubDecl(parentType, childName) {
2664 const trimmed_docs = docs.trim();2629 {
2665 let index = trimmed_docs.indexOf('.');2630 // Generic functions
2666 if (index < 0) {2631 if ("value" in parentType) {
2667 index = trimmed_docs.indexOf('\n');2632 const rv = resolveValue(parentType.value);
2668 if (index < 0) {2633 if ("type" in rv.expr) {
2669 index = trimmed_docs.length;2634 const t = zigAnalysis.types[rv.expr.type];
2635 if (t.kind == typeKinds.Fn && t.generic_ret != null) {
2636 const rgr = resolveValue({ expr: t.generic_ret });
2637 if ("type" in rgr.expr) {
2638 parentType = zigAnalysis.types[rgr.expr.type];
2670 }2639 }
2671 } else {2640 }
2672 index += 1; // include the period
2673 }2641 }
2674 const slice = trimmed_docs.slice(0, index);2642 }
2675 return markdown(slice);
2676 }2643 }
26772644
2645 if (!parentType.pubDecls) return null;
2646 for (let i = 0; i < parentType.pubDecls.length; i += 1) {
2647 let declIndex = parentType.pubDecls[i];
2648 let childDecl = zigAnalysis.decls[declIndex];
2649 if (childDecl.name === childName) {
2650 return childDecl;
2651 }
2652 }
2653 if (!parentType.privDecls) return null;
2654 for (let i = 0; i < parentType.privDecls.length; i += 1) {
2655 let declIndex = parentType.privDecls[i];
2656 let childDecl = zigAnalysis.decls[declIndex];
2657 if (childDecl.name === childName) {
2658 return childDecl;
2659 }
2660 }
2661 return null;
2662 }
2663
2664 function computeCanonicalPackagePaths() {
2665 let list = new Array(zigAnalysis.packages.length);
2666 // Now we try to find all the packages from root.
2667 let rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];
2668 // Breadth-first to keep the path shortest possible.
2669 let stack = [
2670 {
2671 path: [],
2672 pkg: rootPkg,
2673 },
2674 ];
2675 while (stack.length !== 0) {
2676 let item = stack.shift();
2677 for (let key in item.pkg.table) {
2678 let childPkgIndex = item.pkg.table[key];
2679 if (list[childPkgIndex] != null) continue;
2680 let childPkg = zigAnalysis.packages[childPkgIndex];
2681 if (childPkg == null) continue;
2682
2683 let newPath = item.path.concat([key]);
2684 list[childPkgIndex] = newPath;
2685 stack.push({
2686 path: newPath,
2687 pkg: childPkg,
2688 });
2689 }
2690 }
2691 return list;
2692 }
26782693
2679 function markdown(input) {2694 function computeCanonDeclPaths() {
2680 const raw_lines = input.split('\n'); // zig allows no '\r', so we don't need to split on CR2695 let list = new Array(zigAnalysis.decls.length);
2681 2696 canonTypeDecls = new Array(zigAnalysis.types.length);
2682 const lines = [];
2683
2684 // PHASE 1:
2685 // Dissect lines and determine the type for each line.
2686 // Also computes indentation level and removes unnecessary whitespace
26872697
2688 let is_reading_code = false;2698 for (let pkgI = 0; pkgI < zigAnalysis.packages.length; pkgI += 1) {
2689 let code_indent = 0;2699 if (pkgI === zigAnalysis.rootPkg && rootIsStd) continue;
2690 for (let line_no = 0; line_no < raw_lines.length; line_no++) {2700 let pkg = zigAnalysis.packages[pkgI];
2691 const raw_line = raw_lines[line_no];2701 let pkgNames = canonPkgPaths[pkgI];
2702 if (pkgNames === undefined) continue;
26922703
2693 const line = {2704 let stack = [
2694 indent: 0,2705 {
2695 raw_text: raw_line,2706 declNames: [],
2696 text: raw_line.trim(),2707 type: zigAnalysis.types[pkg.main],
2697 type: "p", // p, h1 … h6, code, ul, ol, blockquote, skip, empty2708 },
2698 ordered_number: -1, // NOTE: hack to make the type checker happy2709 ];
2710 while (stack.length !== 0) {
2711 let item = stack.shift();
2712
2713 if (isContainerType(item.type)) {
2714 let t = item.type;
2715
2716 let len = t.pubDecls ? t.pubDecls.length : 0;
2717 for (let declI = 0; declI < len; declI += 1) {
2718 let mainDeclIndex = t.pubDecls[declI];
2719 if (list[mainDeclIndex] != null) continue;
2720
2721 let decl = zigAnalysis.decls[mainDeclIndex];
2722 let declVal = resolveValue(decl.value);
2723 let declNames = item.declNames.concat([decl.name]);
2724 list[mainDeclIndex] = {
2725 pkgNames: pkgNames,
2726 declNames: declNames,
2699 };2727 };
2728 if ("type" in declVal.expr) {
2729 let value = zigAnalysis.types[declVal.expr.type];
2730 if (declCanRepresentTypeKind(value.kind)) {
2731 canonTypeDecls[declVal.type] = mainDeclIndex;
2732 }
27002733
2701 if (!is_reading_code) {2734 if (isContainerType(value)) {
2702 while ((line.indent < line.raw_text.length) && line.raw_text[line.indent] == ' ') {2735 stack.push({
2703 line.indent += 1;2736 declNames: declNames,
2704 }2737 type: value,
2738 });
2739 }
27052740
2706 if (line.text.startsWith("######")) {2741 // Generic fun/ction
2707 line.type = "h6";2742 if (value.kind == typeKinds.Fn && value.generic_ret != null) {
2708 line.text = line.text.substr(6);2743 let resolvedVal = resolveValue({ expr: value.generic_ret });
2709 }2744 if ("type" in resolvedVal.expr) {
2710 else if (line.text.startsWith("#####")) {2745 let generic_type = zigAnalysis.types[resolvedVal.expr.type];
2711 line.type = "h5";2746 if (isContainerType(generic_type)) {
2712 line.text = line.text.substr(5);2747 stack.push({
2713 }2748 declNames: declNames,
2714 else if (line.text.startsWith("####")) {2749 type: generic_type,
2715 line.type = "h4";2750 });
2716 line.text = line.text.substr(4);2751 }
2717 }
2718 else if (line.text.startsWith("###")) {
2719 line.type = "h3";
2720 line.text = line.text.substr(3);
2721 }
2722 else if (line.text.startsWith("##")) {
2723 line.type = "h2";
2724 line.text = line.text.substr(2);
2725 }
2726 else if (line.text.startsWith("#")) {
2727 line.type = "h1";
2728 line.text = line.text.substr(1);
2729 }
2730 else if (line.text.startsWith("-")) {
2731 line.type = "ul";
2732 line.text = line.text.substr(1);
2733 }
2734 else if (line.text.match(/^\d+\..*$/)) { // if line starts with {number}{dot}
2735 const match = (line.text.match(/(\d+)\./));
2736 line.type = "ul";
2737 line.text = line.text.substr(match[0].length);
2738 line.ordered_number = Number(match[1].length);
2739 }
2740 else if (line.text == "```") {
2741 line.type = "skip";
2742 is_reading_code = true;
2743 code_indent = line.indent;
2744 }
2745 else if (line.text == "") {
2746 line.type = "empty";
2747 }
2748 }
2749 else {
2750 if (line.text == "```") {
2751 is_reading_code = false;
2752 line.type = "skip";
2753 } else {
2754 line.type = "code";
2755 line.text = line.raw_text.substr(code_indent); // remove the indent of the ``` from all the code block
2756 }2752 }
2753 }
2757 }2754 }
2755 }
2756 }
2757 }
2758 }
2759 return list;
2760 }
27582761
2759 if (line.type != "skip") {2762 function getCanonDeclPath(index) {
2760 lines.push(line);2763 if (canonDeclPaths == null) {
2761 }2764 canonDeclPaths = computeCanonDeclPaths();
2765 }
2766 //let cd = (canonDeclPaths);
2767 return canonDeclPaths[index];
2768 }
2769
2770 function getCanonTypeDecl(index) {
2771 getCanonDeclPath(0);
2772 //let ct = (canonTypeDecls);
2773 return canonTypeDecls[index];
2774 }
2775
2776 function escapeHtml(text) {
2777 return text.replace(/[&"<>]/g, function (m) {
2778 return escapeHtmlReplacements[m];
2779 });
2780 }
2781
2782 function shortDescMarkdown(docs) {
2783 const trimmed_docs = docs.trim();
2784 let index = trimmed_docs.indexOf(".");
2785 if (index < 0) {
2786 index = trimmed_docs.indexOf("\n");
2787 if (index < 0) {
2788 index = trimmed_docs.length;
2789 }
2790 } else {
2791 index += 1; // include the period
2792 }
2793 const slice = trimmed_docs.slice(0, index);
2794 return markdown(slice);
2795 }
2796
2797 function markdown(input) {
2798 const raw_lines = input.split("\n"); // zig allows no '\r', so we don't need to split on CR
2799
2800 const lines = [];
2801
2802 // PHASE 1:
2803 // Dissect lines and determine the type for each line.
2804 // Also computes indentation level and removes unnecessary whitespace
2805
2806 let is_reading_code = false;
2807 let code_indent = 0;
2808 for (let line_no = 0; line_no < raw_lines.length; line_no++) {
2809 const raw_line = raw_lines[line_no];
2810
2811 const line = {
2812 indent: 0,
2813 raw_text: raw_line,
2814 text: raw_line.trim(),
2815 type: "p", // p, h1 … h6, code, ul, ol, blockquote, skip, empty
2816 ordered_number: -1, // NOTE: hack to make the type checker happy
2817 };
2818
2819 if (!is_reading_code) {
2820 while (
2821 line.indent < line.raw_text.length &&
2822 line.raw_text[line.indent] == " "
2823 ) {
2824 line.indent += 1;
2825 }
2826
2827 if (line.text.startsWith("######")) {
2828 line.type = "h6";
2829 line.text = line.text.substr(6);
2830 } else if (line.text.startsWith("#####")) {
2831 line.type = "h5";
2832 line.text = line.text.substr(5);
2833 } else if (line.text.startsWith("####")) {
2834 line.type = "h4";
2835 line.text = line.text.substr(4);
2836 } else if (line.text.startsWith("###")) {
2837 line.type = "h3";
2838 line.text = line.text.substr(3);
2839 } else if (line.text.startsWith("##")) {
2840 line.type = "h2";
2841 line.text = line.text.substr(2);
2842 } else if (line.text.startsWith("#")) {
2843 line.type = "h1";
2844 line.text = line.text.substr(1);
2845 } else if (line.text.startsWith("-")) {
2846 line.type = "ul";
2847 line.text = line.text.substr(1);
2848 } else if (line.text.match(/^\d+\..*$/)) {
2849 // if line starts with {number}{dot}
2850 const match = line.text.match(/(\d+)\./);
2851 line.type = "ul";
2852 line.text = line.text.substr(match[0].length);
2853 line.ordered_number = Number(match[1].length);
2854 } else if (line.text == "```") {
2855 line.type = "skip";
2856 is_reading_code = true;
2857 code_indent = line.indent;
2858 } else if (line.text == "") {
2859 line.type = "empty";
2860 }
2861 } else {
2862 if (line.text == "```") {
2863 is_reading_code = false;
2864 line.type = "skip";
2865 } else {
2866 line.type = "code";
2867 line.text = line.raw_text.substr(code_indent); // remove the indent of the ``` from all the code block
2762 }2868 }
2869 }
27632870
2764 // PHASE 2:2871 if (line.type != "skip") {
2765 // Render HTML from markdown lines.2872 lines.push(line);
2766 // Look at each line and emit fitting HTML code2873 }
27672874 }
2768
2769 function markdownInlines(innerText) {
2770
2771 // inline types:
2772 // **{INLINE}** : <strong>
2773 // __{INLINE}__ : <u>
2774 // ~~{INLINE}~~ : <s>
2775 // *{INLINE}* : <emph>
2776 // _{INLINE}_ : <emph>
2777 // `{TEXT}` : <code>
2778 // [{INLINE}]({URL}) : <a>
2779 // ![{TEXT}]({URL}) : <img>
2780 // [[std;format.fmt]] : <a> (inner link)
2781
2782
2783
2784 const formats = [
2785 {
2786 marker: "**",
2787 tag: "strong",
2788 },
2789 {
2790 marker: "~~",
2791 tag: "s",
2792 },
2793 {
2794 marker: "__",
2795 tag: "u",
2796 },
2797 {
2798 marker: "*",
2799 tag: "em",
2800 }
2801 ];
28022875
2803 2876 // PHASE 2:
2804 const stack = [];2877 // Render HTML from markdown lines.
2878 // Look at each line and emit fitting HTML code
2879
2880 function markdownInlines(innerText) {
2881 // inline types:
2882 // **{INLINE}** : <strong>
2883 // __{INLINE}__ : <u>
2884 // ~~{INLINE}~~ : <s>
2885 // *{INLINE}* : <emph>
2886 // _{INLINE}_ : <emph>
2887 // `{TEXT}` : <code>
2888 // [{INLINE}]({URL}) : <a>
2889 // ![{TEXT}]({URL}) : <img>
2890 // [[std;format.fmt]] : <a> (inner link)
2891
2892 const formats = [
2893 {
2894 marker: "**",
2895 tag: "strong",
2896 },
2897 {
2898 marker: "~~",
2899 tag: "s",
2900 },
2901 {
2902 marker: "__",
2903 tag: "u",
2904 },
2905 {
2906 marker: "*",
2907 tag: "em",
2908 },
2909 ];
28052910
2806 let innerHTML = "";2911 const stack = [];
2807 let currentRun = "";
28082912
2809 function flushRun() {2913 let innerHTML = "";
2810 if (currentRun != "") {2914 let currentRun = "";
2811 innerHTML += escapeHtml(currentRun);
2812 }
2813 currentRun = "";
2814 }
28152915
2816 let parsing_code = false;2916 function flushRun() {
2817 let codetag = "";2917 if (currentRun != "") {
2818 let in_code = false;2918 innerHTML += escapeHtml(currentRun);
28192919 }
2820 for (let i = 0; i < innerText.length; i++) {2920 currentRun = "";
28212921 }
2822 if (parsing_code && in_code) {
2823 if (innerText.substr(i, codetag.length) == codetag) {
2824 // remove leading and trailing whitespace if string both starts and ends with one.
2825 if (currentRun[0] == " " && currentRun[currentRun.length - 1] == " ") {
2826 currentRun = currentRun.substr(1, currentRun.length - 2);
2827 }
2828 flushRun();
2829 i += codetag.length - 1;
2830 in_code = false;
2831 parsing_code = false;
2832 innerHTML += "</code>";
2833 codetag = "";
2834 } else {
2835 currentRun += innerText[i];
2836 }
2837 continue;
2838 }
28392922
2840 if (innerText[i] == "`") {2923 let parsing_code = false;
2841 flushRun();2924 let codetag = "";
2842 if (!parsing_code) {2925 let in_code = false;
2843 innerHTML += "<code>";
2844 }
2845 parsing_code = true;
2846 codetag += "`";
2847 continue;
2848 }
28492926
2850 if (parsing_code) {2927 for (let i = 0; i < innerText.length; i++) {
2851 currentRun += innerText[i];2928 if (parsing_code && in_code) {
2852 in_code = true;2929 if (innerText.substr(i, codetag.length) == codetag) {
2853 } else {2930 // remove leading and trailing whitespace if string both starts and ends with one.
2854 let any = false;2931 if (
2855 for (let idx = (stack.length > 0 ? -1 : 0); idx < formats.length; idx++) {2932 currentRun[0] == " " &&
2856 const fmt = idx >= 0 ? formats[idx] : stack[stack.length - 1];2933 currentRun[currentRun.length - 1] == " "
2857 if (innerText.substr(i, fmt.marker.length) == fmt.marker) {2934 ) {
2858 flushRun();2935 currentRun = currentRun.substr(1, currentRun.length - 2);
2859 if (stack[stack.length - 1] == fmt) {
2860 stack.pop();
2861 innerHTML += "</" + fmt.tag + ">";
2862 } else {
2863 stack.push(fmt);
2864 innerHTML += "<" + fmt.tag + ">";
2865 }
2866 i += fmt.marker.length - 1;
2867 any = true;
2868 break;
2869 }
2870 }
2871 if (!any) {
2872 currentRun += innerText[i];
2873 }
2874 }
2875 }2936 }
2876 flushRun();2937 flushRun();
28772938 i += codetag.length - 1;
2878 while (stack.length > 0) {2939 in_code = false;
2879 const fmt = (stack.pop());2940 parsing_code = false;
2880 innerHTML += "</" + fmt.tag + ">";2941 innerHTML += "</code>";
2881 }2942 codetag = "";
28822943 } else {
2883 return innerHTML;2944 currentRun += innerText[i];
2884 }2945 }
28852946 continue;
2886
2887 function previousLineIs(type, line_no) {
2888 if (line_no > 0) {
2889 return (lines[line_no - 1].type == type);
2890 } else {
2891 return false;
2892 }
2893 }2947 }
28942948
2895 2949 if (innerText[i] == "`") {
2896 function nextLineIs(type, line_no) {2950 flushRun();
2897 if (line_no < (lines.length - 1)) {2951 if (!parsing_code) {
2898 return (lines[line_no + 1].type == type);2952 innerHTML += "<code>";
2899 } else {2953 }
2900 return false;2954 parsing_code = true;
2901 }2955 codetag += "`";
2956 continue;
2902 }2957 }
29032958
2904 2959 if (parsing_code) {
2905 function getPreviousLineIndent(line_no) {2960 currentRun += innerText[i];
2906 if (line_no > 0) {2961 in_code = true;
2907 return lines[line_no - 1].indent;2962 } else {
2908 } else {2963 let any = false;
2909 return 0;2964 for (
2965 let idx = stack.length > 0 ? -1 : 0;
2966 idx < formats.length;
2967 idx++
2968 ) {
2969 const fmt = idx >= 0 ? formats[idx] : stack[stack.length - 1];
2970 if (innerText.substr(i, fmt.marker.length) == fmt.marker) {
2971 flushRun();
2972 if (stack[stack.length - 1] == fmt) {
2973 stack.pop();
2974 innerHTML += "</" + fmt.tag + ">";
2975 } else {
2976 stack.push(fmt);
2977 innerHTML += "<" + fmt.tag + ">";
2978 }
2979 i += fmt.marker.length - 1;
2980 any = true;
2981 break;
2910 }2982 }
2983 }
2984 if (!any) {
2985 currentRun += innerText[i];
2986 }
2911 }2987 }
2988 }
2989 flushRun();
29122990
2913 2991 while (stack.length > 0) {
2914 function getNextLineIndent(line_no) {2992 const fmt = stack.pop();
2915 if (line_no < (lines.length - 1)) {2993 innerHTML += "</" + fmt.tag + ">";
2916 return lines[line_no + 1].indent;2994 }
2917 } else {
2918 return 0;
2919 }
2920 }
29212995
2922 let html = "";2996 return innerHTML;
2923 for (let line_no = 0; line_no < lines.length; line_no++) {2997 }
2924 const line = lines[line_no];
29252998
2999 function previousLineIs(type, line_no) {
3000 if (line_no > 0) {
3001 return lines[line_no - 1].type == type;
3002 } else {
3003 return false;
3004 }
3005 }
29263006
3007 function nextLineIs(type, line_no) {
3008 if (line_no < lines.length - 1) {
3009 return lines[line_no + 1].type == type;
3010 } else {
3011 return false;
3012 }
3013 }
29273014
2928 switch (line.type) {3015 function getPreviousLineIndent(line_no) {
2929 case "h1":3016 if (line_no > 0) {
2930 case "h2":3017 return lines[line_no - 1].indent;
2931 case "h3":3018 } else {
2932 case "h4":3019 return 0;
2933 case "h5":3020 }
2934 case "h6":3021 }
2935 html += "<" + line.type + ">" + markdownInlines(line.text) + "</" + line.type + ">\n";
2936 break;
29373022
2938 case "ul":3023 function getNextLineIndent(line_no) {
2939 case "ol":3024 if (line_no < lines.length - 1) {
2940 if (!previousLineIs("ul", line_no) || getPreviousLineIndent(line_no) < line.indent) {3025 return lines[line_no + 1].indent;
2941 html += "<" + line.type + ">\n";3026 } else {
2942 }3027 return 0;
3028 }
3029 }
29433030
2944 html += "<li>" + markdownInlines(line.text) + "</li>\n";3031 let html = "";
3032 for (let line_no = 0; line_no < lines.length; line_no++) {
3033 const line = lines[line_no];
3034
3035 switch (line.type) {
3036 case "h1":
3037 case "h2":
3038 case "h3":
3039 case "h4":
3040 case "h5":
3041 case "h6":
3042 html +=
3043 "<" +
3044 line.type +
3045 ">" +
3046 markdownInlines(line.text) +
3047 "</" +
3048 line.type +
3049 ">\n";
3050 break;
3051
3052 case "ul":
3053 case "ol":
3054 if (
3055 !previousLineIs("ul", line_no) ||
3056 getPreviousLineIndent(line_no) < line.indent
3057 ) {
3058 html += "<" + line.type + ">\n";
3059 }
29453060
2946 if (!nextLineIs("ul", line_no) || getNextLineIndent(line_no) < line.indent) {3061 html += "<li>" + markdownInlines(line.text) + "</li>\n";
2947 html += "</" + line.type + ">\n";
2948 }
2949 break;
29503062
2951 case "p":3063 if (
2952 if (!previousLineIs("p", line_no)) {3064 !nextLineIs("ul", line_no) ||
2953 html += "<p>\n";3065 getNextLineIndent(line_no) < line.indent
2954 }3066 ) {
2955 html += markdownInlines(line.text) + "\n";3067 html += "</" + line.type + ">\n";
2956 if (!nextLineIs("p", line_no)) {3068 }
2957 html += "</p>\n";3069 break;
2958 }
2959 break;
29603070
2961 case "code":3071 case "p":
2962 if (!previousLineIs("code", line_no)) {3072 if (!previousLineIs("p", line_no)) {
2963 html += "<pre><code>";3073 html += "<p>\n";
2964 }3074 }
2965 html += escapeHtml(line.text) + "\n";3075 html += markdownInlines(line.text) + "\n";
2966 if (!nextLineIs("code", line_no)) {3076 if (!nextLineIs("p", line_no)) {
2967 html += "</code></pre>\n";3077 html += "</p>\n";
2968 }3078 }
2969 break;3079 break;
2970 }
2971 }
29723080
2973 return html;3081 case "code":
3082 if (!previousLineIs("code", line_no)) {
3083 html += "<pre><code>";
3084 }
3085 html += escapeHtml(line.text) + "\n";
3086 if (!nextLineIs("code", line_no)) {
3087 html += "</code></pre>\n";
3088 }
3089 break;
3090 }
2974 }3091 }
29753092
2976 function activateSelectedResult() {3093 return html;
2977 if (domSectSearchResults.classList.contains("hidden")) {3094 }
2978 return;
2979 }
29803095
2981 let liDom = domListSearchResults.children[curSearchIndex];3096 function activateSelectedResult() {
2982 if (liDom == null && domListSearchResults.children.length !== 0) {3097 if (domSectSearchResults.classList.contains("hidden")) {
2983 liDom = domListSearchResults.children[0];3098 return;
2984 }
2985 if (liDom != null) {
2986 let aDom = liDom.children[0];
2987 location.href = (aDom.getAttribute("href"));
2988 curSearchIndex = -1;
2989 }
2990 domSearch.blur();
2991 }3099 }
29923100
2993 3101 let liDom = domListSearchResults.children[curSearchIndex];
2994 function onSearchKeyDown(ev) {3102 if (liDom == null && domListSearchResults.children.length !== 0) {
2995 switch (getKeyString(ev)) {3103 liDom = domListSearchResults.children[0];
2996 case "Enter":
2997 // detect if this search changes anything
2998 let terms1 = getSearchTerms();
2999 startSearch();
3000 updateCurNav();
3001 let terms2 = getSearchTerms();
3002 // we might have to wait for onHashChange to trigger
3003 imFeelingLucky = (terms1.join(' ') !== terms2.join(' '));
3004 if (!imFeelingLucky) activateSelectedResult();
3005
3006 ev.preventDefault();
3007 ev.stopPropagation();
3008 return;
3009 case "Esc":
3010 domSearch.value = "";
3011 domSearch.blur();
3012 curSearchIndex = -1;
3013 ev.preventDefault();
3014 ev.stopPropagation();
3015 startSearch();
3016 return;
3017 case "Up":
3018 moveSearchCursor(-1);
3019 ev.preventDefault();
3020 ev.stopPropagation();
3021 return;
3022 case "Down":
3023 moveSearchCursor(1);
3024 ev.preventDefault();
3025 ev.stopPropagation();
3026 return;
3027 default:
3028 if (ev.shiftKey || ev.ctrlKey || ev.altKey) return;
3029
3030 curSearchIndex = -1;
3031 ev.stopPropagation();
3032 startAsyncSearch();
3033 return;
3034 }
3035 }3104 }
30363105 if (liDom != null) {
30373106 let aDom = liDom.children[0];
3038 3107 location.href = aDom.getAttribute("href");
3039 function moveSearchCursor(dir) {3108 curSearchIndex = -1;
3040 if (curSearchIndex < 0 || curSearchIndex >= domListSearchResults.children.length) {
3041 if (dir > 0) {
3042 curSearchIndex = -1 + dir;
3043 } else if (dir < 0) {
3044 curSearchIndex = domListSearchResults.children.length + dir;
3045 }
3046 } else {
3047 curSearchIndex += dir;
3048 }
3049 if (curSearchIndex < 0) {
3050 curSearchIndex = 0;
3051 }
3052 if (curSearchIndex >= domListSearchResults.children.length) {
3053 curSearchIndex = domListSearchResults.children.length - 1;
3054 }
3055 renderSearchCursor();
3056 }3109 }
30573110 domSearch.blur();
3058 3111 }
3059 function getKeyString(ev) {3112
3060 let name;3113 function onSearchKeyDown(ev) {
3061 let ignoreShift = false;3114 switch (getKeyString(ev)) {
3062 switch (ev.which) {3115 case "Enter":
3063 case 13:3116 // detect if this search changes anything
3064 name = "Enter";3117 let terms1 = getSearchTerms();
3065 break;3118 startSearch();
3066 case 27:3119 updateCurNav();
3067 name = "Esc";3120 let terms2 = getSearchTerms();
3068 break;3121 // we might have to wait for onHashChange to trigger
3069 case 38:3122 imFeelingLucky = terms1.join(" ") !== terms2.join(" ");
3070 name = "Up";3123 if (!imFeelingLucky) activateSelectedResult();
3071 break;3124
3072 case 40:3125 ev.preventDefault();
3073 name = "Down";3126 ev.stopPropagation();
3074 break;3127 return;
3075 default:3128 case "Esc":
3076 ignoreShift = true;3129 domSearch.value = "";
3077 name = (ev.key != null) ? ev.key : String.fromCharCode(ev.charCode || ev.keyCode);3130 domSearch.blur();
3078 }3131 curSearchIndex = -1;
3079 if (!ignoreShift && ev.shiftKey) name = "Shift+" + name;3132 ev.preventDefault();
3080 if (ev.altKey) name = "Alt+" + name;3133 ev.stopPropagation();
3081 if (ev.ctrlKey) name = "Ctrl+" + name;3134 startSearch();
3082 return name;3135 return;
3136 case "Up":
3137 moveSearchCursor(-1);
3138 ev.preventDefault();
3139 ev.stopPropagation();
3140 return;
3141 case "Down":
3142 moveSearchCursor(1);
3143 ev.preventDefault();
3144 ev.stopPropagation();
3145 return;
3146 default:
3147 if (ev.shiftKey || ev.ctrlKey || ev.altKey) return;
3148
3149 curSearchIndex = -1;
3150 ev.stopPropagation();
3151 startAsyncSearch();
3152 return;
3083 }3153 }
30843154 }
3085 3155
3086 function onWindowKeyDown(ev) {3156 function moveSearchCursor(dir) {
3087 switch (getKeyString(ev)) {3157 if (
3088 case "Esc":3158 curSearchIndex < 0 ||
3089 if (!domHelpModal.classList.contains("hidden")) {3159 curSearchIndex >= domListSearchResults.children.length
3090 domHelpModal.classList.add("hidden");3160 ) {
3091 ev.preventDefault();3161 if (dir > 0) {
3092 ev.stopPropagation();3162 curSearchIndex = -1 + dir;
3093 }3163 } else if (dir < 0) {
3094 break;3164 curSearchIndex = domListSearchResults.children.length + dir;
3095 case "s":3165 }
3096 domSearch.focus();3166 } else {
3097 domSearch.select();3167 curSearchIndex += dir;
3098 ev.preventDefault();3168 }
3099 ev.stopPropagation();3169 if (curSearchIndex < 0) {
3100 startAsyncSearch();3170 curSearchIndex = 0;
3101 break;3171 }
3102 case "?":3172 if (curSearchIndex >= domListSearchResults.children.length) {
3103 ev.preventDefault();3173 curSearchIndex = domListSearchResults.children.length - 1;
3104 ev.stopPropagation();3174 }
3105 showHelpModal();3175 renderSearchCursor();
3106 break;3176 }
3107 }3177
3178 function getKeyString(ev) {
3179 let name;
3180 let ignoreShift = false;
3181 switch (ev.which) {
3182 case 13:
3183 name = "Enter";
3184 break;
3185 case 27:
3186 name = "Esc";
3187 break;
3188 case 38:
3189 name = "Up";
3190 break;
3191 case 40:
3192 name = "Down";
3193 break;
3194 default:
3195 ignoreShift = true;
3196 name =
3197 ev.key != null
3198 ? ev.key
3199 : String.fromCharCode(ev.charCode || ev.keyCode);
3200 }
3201 if (!ignoreShift && ev.shiftKey) name = "Shift+" + name;
3202 if (ev.altKey) name = "Alt+" + name;
3203 if (ev.ctrlKey) name = "Ctrl+" + name;
3204 return name;
3205 }
3206
3207 function onWindowKeyDown(ev) {
3208 switch (getKeyString(ev)) {
3209 case "Esc":
3210 if (!domHelpModal.classList.contains("hidden")) {
3211 domHelpModal.classList.add("hidden");
3212 ev.preventDefault();
3213 ev.stopPropagation();
3214 }
3215 break;
3216 case "s":
3217 domSearch.focus();
3218 domSearch.select();
3219 ev.preventDefault();
3220 ev.stopPropagation();
3221 startAsyncSearch();
3222 break;
3223 case "?":
3224 ev.preventDefault();
3225 ev.stopPropagation();
3226 showHelpModal();
3227 break;
3108 }3228 }
3229 }
31093230
3110function showHelpModal() {3231 function showHelpModal() {
3111 domHelpModal.classList.remove("hidden");3232 domHelpModal.classList.remove("hidden");
3112 domHelpModal.style.left = (window.innerWidth / 2 - domHelpModal.clientWidth / 2) + "px";3233 domHelpModal.style.left =
3113 domHelpModal.style.top = (window.innerHeight / 2 - domHelpModal.clientHeight / 2) + "px";3234 window.innerWidth / 2 - domHelpModal.clientWidth / 2 + "px";
3235 domHelpModal.style.top =
3236 window.innerHeight / 2 - domHelpModal.clientHeight / 2 + "px";
3114 domHelpModal.focus();3237 domHelpModal.focus();
3115}3238 }
31163239
3117function clearAsyncSearch() {3240 function clearAsyncSearch() {
3118 if (searchTimer != null) {3241 if (searchTimer != null) {
3119 clearTimeout(searchTimer);3242 clearTimeout(searchTimer);
3120 searchTimer = null;3243 searchTimer = null;
3121 }3244 }
3122}3245 }
31233246
3124function startAsyncSearch() {3247 function startAsyncSearch() {
3125 clearAsyncSearch();3248 clearAsyncSearch();
3126 searchTimer = setTimeout(startSearch, 100);3249 searchTimer = setTimeout(startSearch, 100);
3127}3250 }
3128function startSearch() {3251 function startSearch() {
3129 clearAsyncSearch();3252 clearAsyncSearch();
3130 let oldHash = location.hash;3253 let oldHash = location.hash;
3131 let parts = oldHash.split("?");3254 let parts = oldHash.split("?");
3132 let newPart2 = (domSearch.value === "") ? "" : ("?" + domSearch.value);3255 let newPart2 = domSearch.value === "" ? "" : "?" + domSearch.value;
3133 location.hash = (parts.length === 1) ? (oldHash + newPart2) : (parts[0] + newPart2);3256 location.hash =
3134}3257 parts.length === 1 ? oldHash + newPart2 : parts[0] + newPart2;
3135function getSearchTerms() {3258 }
3259 function getSearchTerms() {
3136 let list = curNavSearch.trim().split(/[ \r\n\t]+/);3260 let list = curNavSearch.trim().split(/[ \r\n\t]+/);
3137 list.sort();3261 list.sort();
3138 return list;3262 return list;
3139}3263 }
3140function renderSearch() {3264 function renderSearch() {
3141 let matchedItems = [];3265 let matchedItems = [];
3142 let ignoreCase = (curNavSearch.toLowerCase() === curNavSearch);3266 let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;
3143 let terms = getSearchTerms();3267 let terms = getSearchTerms();
31443268
3145 decl_loop: for (let declIndex = 0; declIndex < zigAnalysis.decls.length; declIndex += 1) {3269 decl_loop: for (
3146 let canonPath = getCanonDeclPath(declIndex);3270 let declIndex = 0;
3147 if (canonPath == null) continue;3271 declIndex < zigAnalysis.decls.length;
31483272 declIndex += 1
3149 let decl = zigAnalysis.decls[declIndex];3273 ) {
3150 let lastPkgName = canonPath.pkgNames[canonPath.pkgNames.length - 1];3274 let canonPath = getCanonDeclPath(declIndex);
3151 let fullPathSearchText = lastPkgName + "." + canonPath.declNames.join('.');3275 if (canonPath == null) continue;
3152 let astNode = zigAnalysis.astNodes[decl.src];3276
3153 let fileAndDocs = "" //zigAnalysis.files[astNode.file];3277 let decl = zigAnalysis.decls[declIndex];
3154 // TODO: understand what this piece of code is trying to achieve3278 let lastPkgName = canonPath.pkgNames[canonPath.pkgNames.length - 1];
3155 // also right now `files` are expressed as a hashmap.3279 let fullPathSearchText =
3156 if (astNode.docs != null) {3280 lastPkgName + "." + canonPath.declNames.join(".");
3157 fileAndDocs += "\n" + astNode.docs;3281 let astNode = zigAnalysis.astNodes[decl.src];
3158 }3282 let fileAndDocs = ""; //zigAnalysis.files[astNode.file];
3159 let fullPathSearchTextLower = fullPathSearchText;3283 // TODO: understand what this piece of code is trying to achieve
3160 if (ignoreCase) {3284 // also right now `files` are expressed as a hashmap.
3161 fullPathSearchTextLower = fullPathSearchTextLower.toLowerCase();3285 if (astNode.docs != null) {
3162 fileAndDocs = fileAndDocs.toLowerCase();3286 fileAndDocs += "\n" + astNode.docs;
3163 }3287 }
31643288 let fullPathSearchTextLower = fullPathSearchText;
3165 let points = 0;3289 if (ignoreCase) {
3166 for (let termIndex = 0; termIndex < terms.length; termIndex += 1) {3290 fullPathSearchTextLower = fullPathSearchTextLower.toLowerCase();
3167 let term = terms[termIndex];3291 fileAndDocs = fileAndDocs.toLowerCase();
3292 }
31683293
3169 // exact, case sensitive match of full decl path3294 let points = 0;
3170 if (fullPathSearchText === term) {3295 for (let termIndex = 0; termIndex < terms.length; termIndex += 1) {
3171 points += 4;3296 let term = terms[termIndex];
3172 continue;
3173 }
3174 // exact, case sensitive match of just decl name
3175 if (decl.name == term) {
3176 points += 3;
3177 continue;
3178 }
3179 // substring, case insensitive match of full decl path
3180 if (fullPathSearchTextLower.indexOf(term) >= 0) {
3181 points += 2;
3182 continue;
3183 }
3184 if (fileAndDocs.indexOf(term) >= 0) {
3185 points += 1;
3186 continue;
3187 }
31883297
3189 continue decl_loop;3298 // exact, case sensitive match of full decl path
3299 if (fullPathSearchText === term) {
3300 points += 4;
3301 continue;
3302 }
3303 // exact, case sensitive match of just decl name
3304 if (decl.name == term) {
3305 points += 3;
3306 continue;
3307 }
3308 // substring, case insensitive match of full decl path
3309 if (fullPathSearchTextLower.indexOf(term) >= 0) {
3310 points += 2;
3311 continue;
3312 }
3313 if (fileAndDocs.indexOf(term) >= 0) {
3314 points += 1;
3315 continue;
3190 }3316 }
31913317
3192 matchedItems.push({3318 continue decl_loop;
3193 decl: decl,3319 }
3194 path: canonPath,3320
3195 points: points,3321 matchedItems.push({
3196 });3322 decl: decl,
3323 path: canonPath,
3324 points: points,
3325 });
3197 }3326 }
31983327
3199 if (matchedItems.length !== 0) {3328 if (matchedItems.length !== 0) {
3200 resizeDomList(domListSearchResults, matchedItems.length, '<li><a href="#"></a></li>');3329 resizeDomList(
32013330 domListSearchResults,
3202 matchedItems.sort(function(a, b) {3331 matchedItems.length,
3203 let cmp = operatorCompare(b.points, a.points);3332 '<li><a href="#"></a></li>'
3204 if (cmp != 0) return cmp;3333 );
3205 return operatorCompare(a.decl.name, b.decl.name);3334
3206 });3335 matchedItems.sort(function (a, b) {
32073336 let cmp = operatorCompare(b.points, a.points);
3208 for (let i = 0; i < matchedItems.length; i += 1) {3337 if (cmp != 0) return cmp;
3209 let liDom = domListSearchResults.children[i];3338 return operatorCompare(a.decl.name, b.decl.name);
3210 let aDom = liDom.children[0];3339 });
3211 let match = matchedItems[i];3340
3212 let lastPkgName = match.path.pkgNames[match.path.pkgNames.length - 1];3341 for (let i = 0; i < matchedItems.length; i += 1) {
3213 aDom.textContent = lastPkgName + "." + match.path.declNames.join('.');3342 let liDom = domListSearchResults.children[i];
3214 aDom.setAttribute('href', navLink(match.path.pkgNames, match.path.declNames));3343 let aDom = liDom.children[0];
3215 }3344 let match = matchedItems[i];
3216 renderSearchCursor();3345 let lastPkgName = match.path.pkgNames[match.path.pkgNames.length - 1];
3346 aDom.textContent = lastPkgName + "." + match.path.declNames.join(".");
3347 aDom.setAttribute(
3348 "href",
3349 navLink(match.path.pkgNames, match.path.declNames)
3350 );
3351 }
3352 renderSearchCursor();
32173353
3218 domSectSearchResults.classList.remove("hidden");3354 domSectSearchResults.classList.remove("hidden");
3219 } else {3355 } else {
3220 domSectSearchNoResults.classList.remove("hidden");3356 domSectSearchNoResults.classList.remove("hidden");
3221 }3357 }
3222}3358 }
32233359
3224function renderSearchCursor() {3360 function renderSearchCursor() {
3225 for (let i = 0; i < domListSearchResults.children.length; i += 1) {3361 for (let i = 0; i < domListSearchResults.children.length; i += 1) {
3226 let liDom = (domListSearchResults.children[i]);3362 let liDom = domListSearchResults.children[i];
3227 if (curSearchIndex === i) {3363 if (curSearchIndex === i) {
3228 liDom.classList.add("selected");3364 liDom.classList.add("selected");
3229 } else {3365 } else {
3230 liDom.classList.remove("selected");3366 liDom.classList.remove("selected");
3231 }3367 }
3232 }3368 }
3233}3369 }
32343370
32353371 // function indexNodesToCalls() {
32363372 // let map = {};
3237// function indexNodesToCalls() {3373 // for (let i = 0; i < zigAnalysis.calls.length; i += 1) {
3238// let map = {};3374 // let call = zigAnalysis.calls[i];
3239// for (let i = 0; i < zigAnalysis.calls.length; i += 1) {3375 // let fn = zigAnalysis.fns[call.fn];
3240// let call = zigAnalysis.calls[i];3376 // if (map[fn.src] == null) {
3241// let fn = zigAnalysis.fns[call.fn];3377 // map[fn.src] = [i];
3242// if (map[fn.src] == null) {3378 // } else {
3243// map[fn.src] = [i];3379 // map[fn.src].push(i);
3244// } else {3380 // }
3245// map[fn.src].push(i);3381 // }
3246// }3382 // return map;
3247// }3383 // }
3248// return map;3384
3249// }3385 function byNameProperty(a, b) {
3250
3251
3252
3253function byNameProperty(a, b) {
3254 return operatorCompare(a.name, b.name);3386 return operatorCompare(a.name, b.name);
3255}3387 }
3256
3257
3258
3259})();3388})();