| ... | ... | @@ -73,7 +73,8 @@ |
| 73 | 73 | name: string, |
| 74 | 74 | src: number, |
| 75 | 75 | ret: WalkResult, |
| 76 | | params: WalkResult[] |
| 76 | params: WalkResult[], |
| 77 | generic: boolean, |
| 77 | 78 | }} Fn |
| 78 | 79 | */ |
| 79 | 80 | |
| ... | ... | @@ -189,18 +190,15 @@ |
| 189 | 190 | |
| 190 | 191 | /** |
| 191 | 192 | * @typedef {{ |
| 192 | | name: string, |
| 193 | | src?: number, |
| 194 | | privDecls: number[], |
| 195 | | pubDecls: number[], |
| 196 | | fields?: WalkResult[], |
| 193 | typeRef: WalkResult, |
| 194 | fieldVals: WalkResult[], |
| 197 | 195 | }} Struct |
| 198 | 196 | */ |
| 199 | 197 | |
| 200 | 198 | /** |
| 201 | 199 | * @typedef {{ |
| 202 | | len: WalkResult, |
| 203 | | child: WalkResult, |
| 200 | typeRef: WalkResult, |
| 201 | data: WalkResult[], |
| 204 | 202 | }} ZigArray |
| 205 | 203 | */ |
| 206 | 204 | |
| ... | ... | @@ -280,7 +278,7 @@ var zigAnalysis; |
| 280 | 278 | var domSectSearchNoResults = /** @type HTMLElement */(document.getElementById("sectSearchNoResults")); |
| 281 | 279 | var domSectInfo = /** @type HTMLElement */(document.getElementById("sectInfo")); |
| 282 | 280 | var domTdTarget = /** @type HTMLElement */(document.getElementById("tdTarget")); |
| 283 | | var domPrivDeclsBox = /** @type HTMLCheckboxElement */(document.getElementById("privDeclsBox")); |
| 281 | var domPrivDeclsBox = /** @type HTMLInputElement */(document.getElementById("privDeclsBox")); |
| 284 | 282 | var domTdZigVer = /** @type HTMLElement */(document.getElementById("tdZigVer")); |
| 285 | 283 | var domHdrName = /** @type HTMLElement */(document.getElementById("hdrName")); |
| 286 | 284 | var domHelpModal = /** @type HTMLElement */(document.getElementById("helpDialog")); |
| ... | ... | @@ -313,7 +311,7 @@ var zigAnalysis; |
| 313 | 311 | * pkgNames: string[], |
| 314 | 312 | * pkgObjs: Package[], |
| 315 | 313 | * declNames: string[], |
| 316 | | * declObjs: Decl[], |
| 314 | * declObjs: (Decl | Type)[], |
| 317 | 315 | * callName: any, |
| 318 | 316 | * }} CurNav |
| 319 | 317 | */ |
| ... | ... | @@ -378,53 +376,68 @@ var zigAnalysis; |
| 378 | 376 | } |
| 379 | 377 | } |
| 380 | 378 | |
| 379 | /** @param {Type | Decl} x */ |
| 381 | 380 | function isDecl(x) { |
| 382 | 381 | return "value" in x; |
| 383 | 382 | } |
| 384 | 383 | |
| 384 | /** @param {Type | Decl} x */ |
| 385 | 385 | function isType(x) { |
| 386 | 386 | return "kind" in x && !("value" in x); |
| 387 | 387 | } |
| 388 | 388 | |
| 389 | /** @param {Type | Decl} x */ |
| 389 | 390 | function isContainerType(x) { |
| 390 | | return isType(x) && typeKindIsContainer(x.kind) ; |
| 391 | return isType(x) && typeKindIsContainer(/** @type {Type} */(x).kind) ; |
| 391 | 392 | } |
| 392 | 393 | |
| 394 | /** @param {Type} type */ |
| 393 | 395 | function typeShorthandName(type) { |
| 394 | | var name = type.name; |
| 396 | var name = undefined; |
| 395 | 397 | if (type.kind === typeKinds.Struct) { |
| 396 | 398 | name = "struct"; |
| 397 | 399 | } else if (type.kind === typeKinds.Enum) { |
| 398 | 400 | name = "enum"; |
| 399 | 401 | } else if (type.kind === typeKinds.Union) { |
| 400 | | name= "union"; |
| 402 | name = "union"; |
| 403 | } else { |
| 404 | name = /** @type {any} */(type).name; |
| 401 | 405 | } |
| 402 | | |
| 403 | 406 | return escapeHtml(name); |
| 404 | 407 | } |
| 405 | 408 | |
| 409 | /** @param {number} typeKind */ |
| 406 | 410 | function typeKindIsContainer(typeKind) { |
| 407 | 411 | return typeKind === typeKinds.Struct || |
| 408 | 412 | typeKind === typeKinds.Union || |
| 409 | 413 | typeKind === typeKinds.Enum; |
| 410 | 414 | } |
| 411 | 415 | |
| 416 | /** @param {number} typeKind */ |
| 412 | 417 | function declCanRepresentTypeKind(typeKind) { |
| 413 | 418 | return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind); |
| 414 | 419 | } |
| 415 | 420 | |
| 421 | /** |
| 422 | * @param {WalkResult[]} path |
| 423 | * @return {WalkResult | null} |
| 424 | */ |
| 416 | 425 | function findCteInRefPath(path) { |
| 417 | 426 | for (var i = path.length - 1; i >= 0; i -= 1) { |
| 418 | 427 | const ref = path[i]; |
| 419 | 428 | if ("string" in ref) continue; |
| 420 | 429 | if ("comptimeExpr" in ref) return ref; |
| 421 | | if ("refPath" in ref) return findCteinRefPath(ref.refPath); |
| 430 | if ("refPath" in ref) return findCteInRefPath(ref.refPath); |
| 422 | 431 | return null; |
| 423 | 432 | } |
| 424 | 433 | |
| 425 | 434 | return null; |
| 426 | 435 | } |
| 427 | 436 | |
| 437 | /** |
| 438 | * @param {WalkResult} value |
| 439 | * @return {WalkResult} |
| 440 | */ |
| 428 | 441 | function resolveValue(value) { |
| 429 | 442 | var i = 0; |
| 430 | 443 | while(i < 1000) { |
| ... | ... | @@ -444,21 +457,26 @@ var zigAnalysis; |
| 444 | 457 | |
| 445 | 458 | } |
| 446 | 459 | console.assert(false); |
| 460 | return /** @type {WalkResult} */({}); |
| 447 | 461 | } |
| 448 | 462 | |
| 463 | /** |
| 464 | * @param {Decl} decl |
| 465 | * @return {WalkResult} |
| 466 | */ |
| 449 | 467 | function typeOfDecl(decl){ |
| 450 | 468 | var i = 0; |
| 451 | 469 | while(i < 1000) { |
| 452 | 470 | i += 1; |
| 453 | 471 | console.assert(isDecl(decl)); |
| 454 | 472 | if ("type" in decl.value) { |
| 455 | | return { type: typeTypeId }; |
| 473 | return /** @type {WalkResult} */({ type: typeTypeId }); |
| 456 | 474 | } |
| 457 | 475 | |
| 458 | 476 | if ("refPath" in decl.value) { |
| 459 | | decl = { |
| 477 | decl = /** @type {Decl} */({ |
| 460 | 478 | value: decl.value.refPath[decl.value.refPath.length -1] |
| 461 | | }; |
| 479 | }); |
| 462 | 480 | continue; |
| 463 | 481 | } |
| 464 | 482 | |
| ... | ... | @@ -495,22 +513,22 @@ var zigAnalysis; |
| 495 | 513 | fn_decl = zigAnalysis.decls[fn_call.func.declRef]; |
| 496 | 514 | } else if ("refPath" in fn_call.func) { |
| 497 | 515 | console.assert("declRef" in fn_call.func.refPath[fn_call.func.refPath.length -1]); |
| 498 | | fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef.value]; |
| 516 | fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef]; |
| 499 | 517 | } else throw {}; |
| 500 | 518 | |
| 501 | 519 | const fn_decl_value = resolveValue(fn_decl.value); |
| 502 | 520 | console.assert("type" in fn_decl_value); //TODO handle comptimeExpr |
| 503 | | const fn_type = zigAnalysis.types[fn_decl_value.type]; |
| 521 | const fn_type = /** @type {Fn} */(zigAnalysis.types[fn_decl_value.type]); |
| 504 | 522 | console.assert(fn_type.kind === typeKinds.Fn); |
| 505 | 523 | return fn_type.ret; |
| 506 | 524 | } |
| 507 | 525 | |
| 508 | 526 | if ("void" in decl.value) { |
| 509 | | return { type: typeTypeId }; |
| 527 | return /** @type {WalkResult} */({ type: typeTypeId }); |
| 510 | 528 | } |
| 511 | 529 | |
| 512 | 530 | if ("bool" in decl.value) { |
| 513 | | return { type: typeKinds.Bool }; |
| 531 | return /** @type {WalkResult} */({ type: typeKinds.Bool }); |
| 514 | 532 | } |
| 515 | 533 | |
| 516 | 534 | console.log("TODO: handle in `typeOfDecl` more cases: ", decl); |
| ... | ... | @@ -518,6 +536,7 @@ var zigAnalysis; |
| 518 | 536 | throw {}; |
| 519 | 537 | } |
| 520 | 538 | console.assert(false); |
| 539 | return /** @type {WalkResult} */({}); |
| 521 | 540 | } |
| 522 | 541 | |
| 523 | 542 | function render() { |
| ... | ... | @@ -569,15 +588,18 @@ var zigAnalysis; |
| 569 | 588 | curNav.pkgObjs.push(pkg); |
| 570 | 589 | } |
| 571 | 590 | |
| 591 | /** @type {Decl | Type} */ |
| 572 | 592 | var currentType = zigAnalysis.types[pkg.main]; |
| 573 | 593 | curNav.declObjs = [currentType]; |
| 574 | 594 | for (var i = 0; i < curNav.declNames.length; i += 1) { |
| 575 | | var childDecl = findSubDecl(currentType, curNav.declNames[i]); |
| 595 | |
| 596 | /** @type {Decl | Type | null} */ |
| 597 | var childDecl = findSubDecl(/** @type {ContainerType} */(currentType), curNav.declNames[i]); |
| 576 | 598 | if (childDecl == null) { |
| 577 | 599 | return render404(); |
| 578 | 600 | } |
| 579 | 601 | |
| 580 | | var childDeclValue = resolveValue(childDecl.value); |
| 602 | var childDeclValue = resolveValue(/** @type {Decl} */(childDecl).value); |
| 581 | 603 | if ("type" in childDeclValue) { |
| 582 | 604 | |
| 583 | 605 | const t = zigAnalysis.types[childDeclValue.type]; |
| ... | ... | @@ -586,7 +608,7 @@ var zigAnalysis; |
| 586 | 608 | } |
| 587 | 609 | } |
| 588 | 610 | |
| 589 | | currentType = childDecl; |
| 611 | currentType = /** @type {Decl | Type} */(childDecl); |
| 590 | 612 | curNav.declObjs.push(currentType); |
| 591 | 613 | } |
| 592 | 614 | |
| ... | ... | @@ -598,31 +620,32 @@ var zigAnalysis; |
| 598 | 620 | var lastIsContainerType = isContainerType(last); |
| 599 | 621 | |
| 600 | 622 | if (lastIsContainerType) { |
| 601 | | return renderContainer(last); |
| 623 | return renderContainer(/** @type {ContainerType} */(last)); |
| 602 | 624 | } |
| 603 | 625 | |
| 604 | 626 | if (!lastIsDecl && !lastIsType) { |
| 605 | | return renderUnknownDecl(last); |
| 627 | return renderUnknownDecl(/** @type {Decl} */(last)); |
| 606 | 628 | } |
| 607 | 629 | |
| 608 | 630 | if (lastIsType) { |
| 609 | | return renderType(last); |
| 631 | return renderType(/** @type {Type} */(last)); |
| 610 | 632 | } |
| 611 | 633 | |
| 612 | 634 | if (lastIsDecl && last.kind === 'var') { |
| 613 | | return renderVar(last); |
| 635 | return renderVar(/** @type {Decl} */(last)); |
| 614 | 636 | } |
| 615 | 637 | |
| 616 | 638 | if (lastIsDecl && last.kind === 'const') { |
| 617 | | var typeObj = zigAnalysis.types[resolveValue(last.value).type]; |
| 639 | var typeObj = zigAnalysis.types[resolveValue(/** @type {Decl} */(last).value).type]; |
| 618 | 640 | if (typeObj && typeObj.kind === typeKinds.Fn) { |
| 619 | | return renderFn(last); |
| 641 | return renderFn(/** @type {Decl} */(last)); |
| 620 | 642 | } |
| 621 | 643 | |
| 622 | | return renderValue(last); |
| 644 | return renderValue(/** @type {Decl} */(last)); |
| 623 | 645 | } |
| 624 | 646 | } |
| 625 | 647 | |
| 648 | /** @param {Decl} decl */ |
| 626 | 649 | function renderUnknownDecl(decl) { |
| 627 | 650 | domDeclNoRef.classList.remove("hidden"); |
| 628 | 651 | |
| ... | ... | @@ -635,31 +658,34 @@ var zigAnalysis; |
| 635 | 658 | domTldDocs.classList.remove("hidden"); |
| 636 | 659 | } |
| 637 | 660 | |
| 661 | /** @param {number} typeIndex */ |
| 638 | 662 | function typeIsErrSet(typeIndex) { |
| 639 | 663 | var typeObj = zigAnalysis.types[typeIndex]; |
| 640 | 664 | return typeObj.kind === typeKinds.ErrorSet; |
| 641 | 665 | } |
| 642 | 666 | |
| 667 | /** @param {number} typeIndex */ |
| 643 | 668 | function typeIsStructWithNoFields(typeIndex) { |
| 644 | 669 | var typeObj = zigAnalysis.types[typeIndex]; |
| 645 | 670 | if (typeObj.kind !== typeKinds.Struct) |
| 646 | 671 | return false; |
| 647 | | return typeObj.fields.length == 0; |
| 672 | return /** @type {ContainerType} */(typeObj).fields.length == 0; |
| 648 | 673 | } |
| 649 | 674 | |
| 675 | /** @param {number} typeIndex */ |
| 650 | 676 | function typeIsGenericFn(typeIndex) { |
| 651 | 677 | var typeObj = zigAnalysis.types[typeIndex]; |
| 652 | 678 | if (typeObj.kind !== typeKinds.Fn) { |
| 653 | 679 | return false; |
| 654 | 680 | } |
| 655 | | return typeObj.generic; |
| 681 | return /** @type {Fn} */(typeObj).generic; |
| 656 | 682 | } |
| 657 | 683 | |
| 658 | 684 | /** @param {Decl} fnDecl */ |
| 659 | 685 | function renderFn(fnDecl) { |
| 660 | 686 | var value = resolveValue(fnDecl.value); |
| 661 | 687 | console.assert("type" in value); |
| 662 | | var typeObj = zigAnalysis.types[value.type]; |
| 688 | var typeObj = /** @type {Fn} */(zigAnalysis.types[value.type]); |
| 663 | 689 | |
| 664 | 690 | domFnProtoCode.innerHTML = typeValueName(value, true, true, fnDecl); |
| 665 | 691 | |
| ... | ... | @@ -672,12 +698,12 @@ var zigAnalysis; |
| 672 | 698 | var retIndex = resolveValue(typeObj.ret).type; |
| 673 | 699 | renderFnParamDocs(fnDecl, typeObj); |
| 674 | 700 | |
| 675 | | var errSetTypeIndex = null; |
| 701 | var errSetTypeIndex = /** @type {number | null} */(null); |
| 676 | 702 | var retType = zigAnalysis.types[retIndex]; |
| 677 | 703 | if (retType.kind === typeKinds.ErrorSet) { |
| 678 | 704 | errSetTypeIndex = retIndex; |
| 679 | 705 | } else if (retType.kind === typeKinds.ErrorUnion) { |
| 680 | | errSetTypeIndex = retType.err; |
| 706 | errSetTypeIndex = /** @type {ErrUnionType} */(retType).err.type; |
| 681 | 707 | } |
| 682 | 708 | if (errSetTypeIndex != null) { |
| 683 | 709 | var errSetType = /** @type {ErrSetType} */(zigAnalysis.types[errSetTypeIndex]); |