authorgravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2023-09-03 17:20:23+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-09-03 17:20:23+02:00
log555028086a60792145c6d8cc751646f7bcf5515a
tree26c9bc56f64e2c3471fd368c5338bd2a6da2bf1b
parent86a9e1deaff4644ade68fe8a0eff70d61ca00e9a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

autodoc: Implement `@call`, `@unionInit`, `@mulAdd` support (#16918)

* autodoc: Implement `@call`, `@unionInit`, `@mulAdd` support * autodoc: Implement builtinIndex in ex

2 files changed, 238 insertions(+), 2 deletions(-)

lib/docs/main.js+72-2
......@@ -1437,7 +1437,9 @@ Happy writing!
14371437 }
14381438
14391439 case "float": {
1440 yield { src: expr.float, tag: Tag.number_literal };
1440 let float = expr.float;
1441 if (Number.isSafeInteger(float)) float = float.toFixed(1);
1442 yield { src: float, tag: Tag.number_literal };
14411443 return;
14421444 }
14431445
......@@ -1495,7 +1497,7 @@ Happy writing!
14951497 case "struct": {
14961498 yield Tok.period;
14971499 yield Tok.l_brace;
1498 yield Tok.space;
1500 if (expr.struct.length > 0) yield Tok.space;
14991501
15001502 for (let i = 0; i < expr.struct.length; i++) {
15011503 const fv = expr.struct[i];
......@@ -1579,6 +1581,10 @@ Happy writing!
15791581 yield { src: "/", tag: Tag.slash };
15801582 break;
15811583 }
1584 case "xor": {
1585 yield { src: "^", tag: Tag.caret };
1586 break;
1587 }
15821588 case "shl": {
15831589 yield { src: "<<", tag: Tag.angle_bracket_angle_bracket_left };
15841590 break;
......@@ -1655,6 +1661,12 @@ Happy writing!
16551661 return;
16561662 }
16571663
1664 case "builtinIndex": {
1665 const builtin = zigAnalysis.exprs[expr.builtinIndex];
1666 yield* ex(builtin, opts);
1667 return;
1668 }
1669
16581670 case "builtinBinIndex": {
16591671 const builtinBinIndex = zigAnalysis.exprs[expr.builtinBinIndex];
16601672 yield* ex(builtinBinIndex, opts);
......@@ -1793,6 +1805,64 @@ Happy writing!
17931805 return;
17941806 }
17951807
1808 case "unionInit": {
1809 let ui = expr.unionInit;
1810 let type = zigAnalysis.exprs[ui.type];
1811 let field = zigAnalysis.exprs[ui.field];
1812 let init = zigAnalysis.exprs[ui.init];
1813 yield { src: "@unionInit", tag: Tag.builtin };
1814 yield Tok.l_paren;
1815 yield* ex(type, opts);
1816 yield Tok.comma;
1817 yield Tok.space;
1818 yield* ex(field, opts);
1819 yield Tok.comma;
1820 yield Tok.space;
1821 yield* ex(init, opts);
1822 yield Tok.r_paren;
1823 return;
1824 }
1825
1826 case "builtinCall": {
1827 let bcall = expr.builtinCall;
1828 let mods = zigAnalysis.exprs[bcall.modifier];
1829 let calee = zigAnalysis.exprs[bcall.function];
1830 let args = zigAnalysis.exprs[bcall.args];
1831 yield { src: "@call", tag: Tag.builtin };
1832 yield Tok.l_paren;
1833 yield* ex(mods, opts);
1834 yield Tok.comma;
1835 yield Tok.space;
1836 yield* ex(calee, opts);
1837 yield Tok.comma;
1838 yield Tok.space;
1839 yield* ex(args, opts);
1840 yield Tok.r_paren;
1841 return;
1842 }
1843
1844 case "mulAdd": {
1845 let muladd = expr.mulAdd;
1846 let mul1 = zigAnalysis.exprs[muladd.mulend1];
1847 let mul2 = zigAnalysis.exprs[muladd.mulend2];
1848 let add = zigAnalysis.exprs[muladd.addend];
1849 let type = zigAnalysis.exprs[muladd.type];
1850 yield { src: "@mulAdd", tag: Tag.builtin };
1851 yield Tok.l_paren;
1852 yield* ex(type, opts);
1853 yield Tok.comma;
1854 yield Tok.space;
1855 yield* ex(mul1, opts);
1856 yield Tok.comma;
1857 yield Tok.space;
1858 yield* ex(mul2, opts);
1859 yield Tok.comma;
1860 yield Tok.space;
1861 yield* ex(add, opts);
1862 yield Tok.r_paren;
1863 return;
1864 }
1865
17961866 case "enumLiteral": {
17971867 let literal = expr.enumLiteral;
17981868 yield Tok.period;
src/Autodoc.zig+166
......@@ -789,6 +789,9 @@ const DocData = struct {
789789 builtinIndex: usize,
790790 builtinBin: BuiltinBin,
791791 builtinBinIndex: usize,
792 unionInit: UnionInit,
793 builtinCall: BuiltinCall,
794 mulAdd: MulAdd,
792795 switchIndex: usize, // index in `exprs`
793796 switchOp: SwitchOp,
794797 binOp: BinOp,
......@@ -810,10 +813,26 @@ const DocData = struct {
810813 lhs: usize, // index in `exprs`
811814 rhs: usize, // index in `exprs`
812815 };
816 const UnionInit = struct {
817 type: usize, // index in `exprs`
818 field: usize, // index in `exprs`
819 init: usize, // index in `exprs`
820 };
813821 const Builtin = struct {
814822 name: []const u8 = "", // fn name
815823 param: usize, // index in `exprs`
816824 };
825 const BuiltinCall = struct {
826 modifier: usize, // index in `exprs`
827 function: usize, // index in `exprs`
828 args: usize, // index in `exprs`
829 };
830 const MulAdd = struct {
831 mulend1: usize, // index in `exprs`
832 mulend2: usize, // index in `exprs`
833 addend: usize, // index in `exprs`
834 type: usize, // index in `exprs`
835 };
817836 const Slice = struct {
818837 lhs: usize, // index in `exprs`
819838 start: usize,
......@@ -1519,6 +1538,7 @@ fn walkInstruction(
15191538 .shr,
15201539 .bit_or,
15211540 .bit_and,
1541 .xor,
15221542 // @check still not working when applied in std
15231543 // .array_cat,
15241544 // .array_mul,
......@@ -1798,6 +1818,152 @@ fn walkInstruction(
17981818 .expr = .{ .builtinBinIndex = binop_index },
17991819 };
18001820 },
1821 .mul_add => {
1822 const pl_node = data[inst_index].pl_node;
1823 const extra = file.zir.extraData(Zir.Inst.MulAdd, pl_node.payload_index);
1824
1825 var mul1: DocData.WalkResult = try self.walkRef(
1826 file,
1827 parent_scope,
1828 parent_src,
1829 extra.data.mulend1,
1830 false,
1831 call_ctx,
1832 );
1833 var mul2: DocData.WalkResult = try self.walkRef(
1834 file,
1835 parent_scope,
1836 parent_src,
1837 extra.data.mulend2,
1838 false,
1839 call_ctx,
1840 );
1841 var add: DocData.WalkResult = try self.walkRef(
1842 file,
1843 parent_scope,
1844 parent_src,
1845 extra.data.addend,
1846 false,
1847 call_ctx,
1848 );
1849
1850 const mul1_index = self.exprs.items.len;
1851 try self.exprs.append(self.arena, mul1.expr);
1852 const mul2_index = self.exprs.items.len;
1853 try self.exprs.append(self.arena, mul2.expr);
1854 const add_index = self.exprs.items.len;
1855 try self.exprs.append(self.arena, add.expr);
1856
1857 var type_index: usize = self.exprs.items.len;
1858 try self.exprs.append(self.arena, add.typeRef orelse .{ .type = @intFromEnum(Ref.type_type) });
1859
1860 return DocData.WalkResult{
1861 .typeRef = add.typeRef,
1862 .expr = .{
1863 .mulAdd = .{
1864 .mulend1 = mul1_index,
1865 .mulend2 = mul2_index,
1866 .addend = add_index,
1867 .type = type_index,
1868 },
1869 },
1870 };
1871 },
1872 .union_init => {
1873 const pl_node = data[inst_index].pl_node;
1874 const extra = file.zir.extraData(Zir.Inst.UnionInit, pl_node.payload_index);
1875
1876 var union_type: DocData.WalkResult = try self.walkRef(
1877 file,
1878 parent_scope,
1879 parent_src,
1880 extra.data.union_type,
1881 false,
1882 call_ctx,
1883 );
1884 var field_name: DocData.WalkResult = try self.walkRef(
1885 file,
1886 parent_scope,
1887 parent_src,
1888 extra.data.field_name,
1889 false,
1890 call_ctx,
1891 );
1892 var init: DocData.WalkResult = try self.walkRef(
1893 file,
1894 parent_scope,
1895 parent_src,
1896 extra.data.init,
1897 false,
1898 call_ctx,
1899 );
1900
1901 const union_type_index = self.exprs.items.len;
1902 try self.exprs.append(self.arena, union_type.expr);
1903 const field_name_index = self.exprs.items.len;
1904 try self.exprs.append(self.arena, field_name.expr);
1905 const init_index = self.exprs.items.len;
1906 try self.exprs.append(self.arena, init.expr);
1907
1908 return DocData.WalkResult{
1909 .typeRef = union_type.expr,
1910 .expr = .{
1911 .unionInit = .{
1912 .type = union_type_index,
1913 .field = field_name_index,
1914 .init = init_index,
1915 },
1916 },
1917 };
1918 },
1919 .builtin_call => {
1920 const pl_node = data[inst_index].pl_node;
1921 const extra = file.zir.extraData(Zir.Inst.BuiltinCall, pl_node.payload_index);
1922
1923 var modifier: DocData.WalkResult = try self.walkRef(
1924 file,
1925 parent_scope,
1926 parent_src,
1927 extra.data.modifier,
1928 false,
1929 call_ctx,
1930 );
1931
1932 var callee: DocData.WalkResult = try self.walkRef(
1933 file,
1934 parent_scope,
1935 parent_src,
1936 extra.data.callee,
1937 false,
1938 call_ctx,
1939 );
1940
1941 var args: DocData.WalkResult = try self.walkRef(
1942 file,
1943 parent_scope,
1944 parent_src,
1945 extra.data.args,
1946 false,
1947 call_ctx,
1948 );
1949
1950 const modifier_index = self.exprs.items.len;
1951 try self.exprs.append(self.arena, modifier.expr);
1952 const function_index = self.exprs.items.len;
1953 try self.exprs.append(self.arena, callee.expr);
1954 const args_index = self.exprs.items.len;
1955 try self.exprs.append(self.arena, args.expr);
1956
1957 return DocData.WalkResult{
1958 .expr = .{
1959 .builtinCall = .{
1960 .modifier = modifier_index,
1961 .function = function_index,
1962 .args = args_index,
1963 },
1964 },
1965 };
1966 },
18011967 .error_union_type => {
18021968 const pl_node = data[inst_index].pl_node;
18031969 const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);