authorgravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2023-09-12 17:50:40+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2023-09-16 17:35:11+02:00
logf2026e7dd650e07defeefc9913f6fb08f9b45c21
treedef1bb547586b07889044de3a5c5782ddeb849f0
parent9a326b22d58a81a6d2b02c9f1c4be82244cfc89a

autodoc: Implement builtin function rendering.

Implement unary ops handling. Fix getType in main.js Minor cleanup of builtin function handling.

2 files changed, 181 insertions(+), 72 deletions(-)

lib/docs/main.js+114-38
......@@ -1522,6 +1522,47 @@ Happy writing!
15221522 return;
15231523 }
15241524
1525 case "unOpIndex": {
1526 const unOp = zigAnalysis.exprs[expr.unOpIndex];
1527 yield* ex(unOp, opts);
1528 return;
1529 }
1530
1531 case "unOp": {
1532 const param = zigAnalysis.exprs[expr.unOp.param];
1533
1534 switch (expr.unOp.name) {
1535 case "bit_not": {
1536 yield { src: "~", tag: Tag.tilde };
1537 break;
1538 }
1539 case "bool_not": {
1540 yield { src: "!", tag: Tag.bang };
1541 break;
1542 }
1543 case "negate_wrap": {
1544 yield { src: "-%", tag: Tag.minus_percent };
1545 break;
1546 }
1547 case "negate": {
1548 yield { src: "-", tag: Tag.minus };
1549 break;
1550 }
1551 default:
1552 throw "unOp: `" + expr.unOp.name + "` not implemented yet!"
1553 }
1554
1555 if (param["binOpIndex"] !== undefined) {
1556 yield Tok.l_paren;
1557 yield* ex(param, opts);
1558 yield Tok.r_paren;
1559 } else {
1560 yield* ex(param, opts);
1561 }
1562
1563 return;
1564 }
1565
15251566 case "binOpIndex": {
15261567 const binOp = zigAnalysis.exprs[expr.binOpIndex];
15271568 yield* ex(binOp, opts);
......@@ -1669,6 +1710,52 @@ Happy writing!
16691710 return;
16701711 }
16711712
1713 case "builtin": {
1714 const builtin = expr.builtin;
1715 let name = "@";
1716 const param = zigAnalysis.exprs[builtin.param];
1717 switch (builtin.name) {
1718 case "align_of": { name += "alignOf"; break; }
1719 case "int_from_bool": { name += "intFromBool"; break; }
1720 case "embed_file": { name += "embedFile"; break; }
1721 case "error_name": { name += "errorName"; break; }
1722 case "panic": { name += "panic"; break; }
1723 case "set_runtime_safety": { name += "setRuntimeSafety"; break; }
1724 case "sqrt": { name += "sqrt"; break; }
1725 case "sin": { name += "sin"; break; }
1726 case "cos": { name += "cos"; break; }
1727 case "tan": { name += "tan"; break; }
1728 case "exp": { name += "exp"; break; }
1729 case "exp2": { name += "exp2"; break; }
1730 case "log": { name += "log"; break; }
1731 case "log2": { name += "log2"; break; }
1732 case "log10": { name += "log10"; break; }
1733 case "fabs": { name += "fabs"; break; }
1734 case "floor": { name += "floor"; break; }
1735 case "ceil": { name += "ceil"; break; }
1736 case "trunc": { name += "trunc"; break; }
1737 case "round": { name += "round"; break; }
1738 case "tag_name": { name += "tagName"; break; }
1739 case "type_name": { name += "typeName"; break; }
1740 case "type_info": { name += "typeInfo"; break; }
1741 case "frame_type": { name += "Frame"; break; }
1742 case "frame_size": { name += "frameSize"; break; }
1743 case "int_from_ptr": { name += "intFromPtr"; break; }
1744 case "int_from_enum": { name += "intFromEnum"; break; }
1745 case "clz": { name += "clz"; break; }
1746 case "ctz": { name += "ctz"; break; }
1747 case "pop_count": { name += "popCount"; break; }
1748 case "byte_swap": { name += "byteSwap"; break; }
1749 case "bit_reverse": { name += "bitReverse"; break; }
1750 default: throw "builtin: `" + builtin.name + "` not implemented yet!";
1751 }
1752 yield { src: name, tag: Tag.builtin };
1753 yield Tok.l_paren;
1754 yield* ex(param, opts);
1755 yield Tok.r_paren;
1756 return;
1757 }
1758
16721759 case "builtinBinIndex": {
16731760 const builtinBinIndex = zigAnalysis.exprs[expr.builtinBinIndex];
16741761 yield* ex(builtinBinIndex, opts);
......@@ -1899,15 +1986,6 @@ Happy writing!
18991986 return;
19001987 }
19011988
1902 case "typeInfo": {
1903 const arg = zigAnalysis.exprs[expr.typeInfo];
1904 yield { src: "@typeInfo", tag: Tag.builtin };
1905 yield Tok.l_paren;
1906 yield* ex(arg, opts);
1907 yield Tok.r_paren;
1908 return;
1909 }
1910
19111989 case "switchIndex": {
19121990 const switchIndex = zigAnalysis.exprs[expr.switchIndex];
19131991 yield* ex(switchIndex, opts);
......@@ -4560,16 +4638,16 @@ Happy writing!
45604638 switch (ty[0]) {
45614639 default:
45624640 throw "unhandled type kind!";
4563 case 0: // Unanalyzed
4641 case typeKinds.Unanalyzed:
45644642 throw "unanalyzed type!";
4565 case 1: // Type
4566 case 2: // Void
4567 case 3: // Bool
4568 case 4: // NoReturn
4569 case 5: // Int
4570 case 6: // Float
4643 case typeKinds.Type:
4644 case typeKinds.Void:
4645 case typeKinds.Bool:
4646 case typeKinds.NoReturn:
4647 case typeKinds.Int:
4648 case typeKinds.Float:
45714649 return { kind: ty[0], name: ty[1] };
4572 case 7: // Pointer
4650 case typeKinds.Pointer:
45734651 return {
45744652 kind: ty[0],
45754653 size: ty[1],
......@@ -4588,14 +4666,14 @@ Happy writing!
45884666 has_addrspace: ty[14],
45894667 has_bit_range: ty[15],
45904668 };
4591 case 8: // Array
4669 case typeKinds.Array:
45924670 return {
45934671 kind: ty[0],
45944672 len: ty[1],
45954673 child: ty[2],
45964674 sentinel: ty[3],
45974675 };
4598 case 9: // Struct
4676 case typeKinds.Struct:
45994677 return {
46004678 kind: ty[0],
46014679 name: ty[1],
......@@ -4610,36 +4688,36 @@ Happy writing!
46104688 parent_container: ty[10],
46114689 layout: ty[11],
46124690 };
4613 case 10: // ComptimeExpr
4614 case 11: // ComptimeFloat
4615 case 12: // ComptimeInt
4616 case 13: // Undefined
4617 case 14: // Null
4691 case typeKinds.ComptimeExpr:
4692 case typeKinds.ComptimeFloat:
4693 case typeKinds.ComptimeInt:
4694 case typeKinds.Undefined:
4695 case typeKinds.Null:
46184696 return { kind: ty[0], name: ty[1] };
4619 case 15: // Optional
4697 case typeKinds.Optional:
46204698 return {
46214699 kind: ty[0],
46224700 name: ty[1],
46234701 child: ty[2],
46244702 };
4625 case 16: // ErrorUnion
4703 case typeKinds.ErrorUnion:
46264704 return {
46274705 kind: ty[0],
46284706 lhs: ty[1],
46294707 rhs: ty[2],
46304708 };
4631 case 17: // InferredErrorUnion
4709 case typeKinds.InferredErrorUnion:
46324710 return {
46334711 kind: ty[0],
46344712 payload: ty[1],
46354713 };
4636 case 18: // ErrorSet
4714 case typeKinds.ErrorSet:
46374715 return {
46384716 kind: ty[0],
46394717 name: ty[1],
46404718 fields: ty[2],
46414719 };
4642 case 19: // Enum
4720 case typeKinds.Enum:
46434721 return {
46444722 kind: ty[0],
46454723 name: ty[1],
......@@ -4651,7 +4729,7 @@ Happy writing!
46514729 nonexhaustive: ty[7],
46524730 parent_container: ty[8],
46534731 };
4654 case 20: // Union
4732 case typeKinds.Union:
46554733 return {
46564734 kind: ty[0],
46574735 name: ty[1],
......@@ -4664,7 +4742,7 @@ Happy writing!
46644742 parent_container: ty[8],
46654743 layout: ty[9],
46664744 };
4667 case 21: // Fn
4745 case typeKinds.Fn:
46684746 return {
46694747 kind: ty[0],
46704748 name: ty[1],
......@@ -4683,9 +4761,7 @@ Happy writing!
46834761 is_test: ty[14],
46844762 is_extern: ty[15],
46854763 };
4686 case 22: // BoundFn
4687 return { kind: ty[0], name: ty[1] };
4688 case 23: // Opaque
4764 case typeKinds.Opaque:
46894765 return {
46904766 kind: ty[0],
46914767 name: ty[1],
......@@ -4694,10 +4770,10 @@ Happy writing!
46944770 pubDecls: ty[4],
46954771 parent_container: ty[5],
46964772 };
4697 case 24: // Frame
4698 case 25: // AnyFrame
4699 case 26: // Vector
4700 case 27: // EnumLiteral
4773 case typeKinds.Frame:
4774 case typeKinds.AnyFrame:
4775 case typeKinds.Vector:
4776 case typeKinds.EnumLiteral:
47014777 return { kind: ty[0], name: ty[1] };
47024778 }
47034779 }
src/Autodoc.zig+67-34
......@@ -766,15 +766,12 @@ const DocData = struct {
766766 array: []usize, // index in `exprs`
767767 call: usize, // index in `calls`
768768 enumLiteral: []const u8, // direct value
769 alignOf: usize, // index in `exprs`
770769 typeOf: usize, // index in `exprs`
771 typeInfo: usize, // index in `exprs`
772770 typeOf_peer: []usize,
773771 errorUnion: usize, // index in `types`
774772 as: As,
775773 sizeOf: usize, // index in `exprs`
776774 bitSizeOf: usize, // index in `exprs`
777 intFromEnum: usize, // index in `exprs`
778775 compileError: usize, // index in `exprs`
779776 optionalPayload: usize, // index in `exprs`
780777 elemVal: ElemVal,
......@@ -794,9 +791,15 @@ const DocData = struct {
794791 mulAdd: MulAdd,
795792 switchIndex: usize, // index in `exprs`
796793 switchOp: SwitchOp,
794 unOp: UnOp,
795 unOpIndex: usize,
797796 binOp: BinOp,
798797 binOpIndex: usize,
799798 load: usize, // index in `exprs`
799 const UnOp = struct {
800 param: usize, // index in `exprs`
801 name: []const u8 = "", // tag name
802 };
800803 const BinOp = struct {
801804 lhs: usize, // index in `exprs`
802805 rhs: usize, // index in `exprs`
......@@ -1671,8 +1674,7 @@ fn walkInstruction(
16711674 .frame_type,
16721675 .frame_size,
16731676 .int_from_ptr,
1674 .bit_not,
1675 .bool_not,
1677 .type_info,
16761678 // @check
16771679 .clz,
16781680 .ctz,
......@@ -1695,13 +1697,49 @@ fn walkInstruction(
16951697 const param_index = self.exprs.items.len;
16961698 try self.exprs.append(self.arena, param.expr);
16971699
1698 self.exprs.items[bin_index] = .{ .builtin = .{ .name = @tagName(tags[inst_index]), .param = param_index } };
1700 self.exprs.items[bin_index] = .{
1701 .builtin = .{
1702 .name = @tagName(tags[inst_index]),
1703 .param = param_index,
1704 },
1705 };
16991706
17001707 return DocData.WalkResult{
17011708 .typeRef = param.typeRef orelse .{ .type = @intFromEnum(Ref.type_type) },
17021709 .expr = .{ .builtinIndex = bin_index },
17031710 };
17041711 },
1712 .bit_not,
1713 .bool_not,
1714 .negate_wrap,
1715 => {
1716 const un_node = data[inst_index].un_node;
1717 const un_index = self.exprs.items.len;
1718 try self.exprs.append(self.arena, .{ .unOp = .{ .param = 0 } });
1719 const param = try self.walkRef(
1720 file,
1721 parent_scope,
1722 parent_src,
1723 un_node.operand,
1724 false,
1725 call_ctx,
1726 );
1727
1728 const param_index = self.exprs.items.len;
1729 try self.exprs.append(self.arena, param.expr);
1730
1731 self.exprs.items[un_index] = .{
1732 .unOp = .{
1733 .name = @tagName(tags[inst_index]),
1734 .param = param_index,
1735 },
1736 };
1737
1738 return DocData.WalkResult{
1739 .typeRef = param.typeRef,
1740 .expr = .{ .unOpIndex = un_index },
1741 };
1742 },
17051743 .bool_br_and, .bool_br_or => {
17061744 const bool_br = data[inst_index].bool_br;
17071745
......@@ -2407,12 +2445,20 @@ fn walkInstruction(
24072445 .int => |*int| int.negated = true,
24082446 .int_big => |*int_big| int_big.negated = true,
24092447 else => {
2410 printWithContext(
2411 file,
2412 inst_index,
2413 "TODO: support negation for more types",
2414 .{},
2415 );
2448 const un_index = self.exprs.items.len;
2449 try self.exprs.append(self.arena, .{ .unOp = .{ .param = 0 } });
2450 const param_index = self.exprs.items.len;
2451 try self.exprs.append(self.arena, operand.expr);
2452 self.exprs.items[un_index] = .{
2453 .unOp = .{
2454 .name = @tagName(tags[inst_index]),
2455 .param = param_index,
2456 },
2457 };
2458 return DocData.WalkResult{
2459 .typeRef = operand.typeRef,
2460 .expr = .{ .unOpIndex = un_index },
2461 };
24162462 },
24172463 }
24182464 return operand;
......@@ -2466,12 +2512,20 @@ fn walkInstruction(
24662512 false,
24672513 call_ctx,
24682514 );
2515 const builtin_index = self.exprs.items.len;
2516 try self.exprs.append(self.arena, .{ .builtin = .{ .param = 0 } });
24692517 const operand_index = self.exprs.items.len;
24702518 try self.exprs.append(self.arena, operand.expr);
2519 self.exprs.items[builtin_index] = .{
2520 .builtin = .{
2521 .name = @tagName(tags[inst_index]),
2522 .param = operand_index,
2523 },
2524 };
24712525
24722526 return DocData.WalkResult{
24732527 .typeRef = .{ .type = @intFromEnum(Ref.comptime_int_type) },
2474 .expr = .{ .intFromEnum = operand_index },
2528 .expr = .{ .builtinIndex = builtin_index },
24752529 };
24762530 },
24772531 .switch_block => {
......@@ -2564,27 +2618,6 @@ fn walkInstruction(
25642618 .expr = .{ .typeOf = operand_index },
25652619 };
25662620 },
2567 .type_info => {
2568 // @check
2569 const un_node = data[inst_index].un_node;
2570
2571 const operand = try self.walkRef(
2572 file,
2573 parent_scope,
2574 parent_src,
2575 un_node.operand,
2576 need_type,
2577 call_ctx,
2578 );
2579
2580 const operand_index = self.exprs.items.len;
2581 try self.exprs.append(self.arena, operand.expr);
2582
2583 return DocData.WalkResult{
2584 .typeRef = operand.typeRef,
2585 .expr = .{ .typeInfo = operand_index },
2586 };
2587 },
25882621 .as_node, .as_shift_operand => {
25892622 const pl_node = data[inst_index].pl_node;
25902623 const extra = file.zir.extraData(Zir.Inst.As, pl_node.payload_index);