authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2023-01-24 19:18:52+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-01-24 19:18:52+01:00
log4e80253e207258f01620b15e44866bc46708e6b1
tree624ec42e610306e78745b96c118139b460c06b7a
parentaf820bbb94a06c5bae34a38bda0f964620413e92
parentdd8e16b9065bc33195a398744c663718bec57d4d
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14395 from der-teufel-programming/master

autodoc: Add Struct, Union, Enum handling in exprName, add tags for Enum and Union, anonymous types "work"

2 files changed, 182 insertions(+), 9 deletions(-)

lib/docs/main.js+160-3
...@@ -1641,7 +1641,11 @@ const NAV_MODES = {...@@ -1641,7 +1641,11 @@ const NAV_MODES = {
1641 return payloadHtml;1641 return payloadHtml;
1642 }1642 }
1643 case "null": {1643 case "null": {
1644 return "null";1644 if (opts.wantHtml) {
1645 return '<span class="tok-null">null</span>';
1646 } else {
1647 return "null";
1648 }
1645 }1649 }
1646 case "array": {1650 case "array": {
1647 let payloadHtml = ".{";1651 let payloadHtml = ".{";
...@@ -1766,11 +1770,160 @@ const NAV_MODES = {...@@ -1766,11 +1770,160 @@ const NAV_MODES = {
1766 throw "TODO";1770 throw "TODO";
1767 case typeKinds.Struct: {1771 case typeKinds.Struct: {
1768 let structObj = typeObj;1772 let structObj = typeObj;
1769 return structObj;1773 let name = "";
1774 if (opts.wantHtml) {
1775 name = "<span class='tok-kw'>struct</span> { ";
1776 } else {
1777 name = "struct { ";
1778 }
1779 if (structObj.fields.length > 1 && opts.wantHtml) {name += "</br>";}
1780 let indent = "";
1781 if (structObj.fields.length > 1 && opts.wantHtml) {
1782 indent = "&nbsp;&nbsp;&nbsp;&nbsp;"
1783 }
1784 if (opts.indent) {
1785 indent = opts.indent + indent;
1786 }
1787 let structNode = getAstNode(structObj.src);
1788 let field_end = ",";
1789 if (structObj.fields.length > 1 && opts.wantHtml) {
1790 field_end += "</br>";
1791 } else {
1792 field_end += " ";
1793 }
1794
1795 for(let i = 0; i < structObj.fields.length; i += 1) {
1796 let fieldNode = getAstNode(structNode.fields[i]);
1797 let fieldName = fieldNode.name;
1798 let html = indent + escapeHtml(fieldName);
1799
1800 let fieldTypeExpr = structObj.fields[i];
1801 html += ": ";
1802
1803 html += exprName(fieldTypeExpr, {...opts, indent: indent});
1804
1805 html += field_end;
1806
1807 name += html;
1808 }
1809 if (opts.indent) {
1810 name += opts.indent;
1811 }
1812 name += "}";
1813 return name;
1770 }1814 }
1771 case typeKinds.Enum: {1815 case typeKinds.Enum: {
1772 let enumObj = typeObj;1816 let enumObj = typeObj;
1773 return enumObj;1817 let name = "";
1818 if (opts.wantHtml) {
1819 name = "<span class='tok-kw'>enum</span>";
1820 } else {
1821 name = "enum";
1822 }
1823 if (enumObj.tag) {
1824 name += " (" + exprName(enumObj.tag, opts) + ")";
1825 }
1826 name += " { ";
1827 let enumNode = getAstNode(enumObj.src);
1828 let fields_len = enumNode.fields.length;
1829 if (enumObj.nonexhaustive) {
1830 fields_len += 1;
1831 }
1832 if (fields_len > 1 && opts.wantHtml) {name += "</br>";}
1833 let indent = "";
1834 if (fields_len > 1) {
1835 if (opts.wantHtml) {
1836 indent = "&nbsp;&nbsp;&nbsp;&nbsp;";
1837 } else {
1838 indent = " ";
1839 }
1840 }
1841 if (opts.indent) {
1842 indent = opts.indent + indent;
1843 }
1844 let field_end = ",";
1845 if (fields_len > 1 && opts.wantHtml) {
1846 field_end += "</br>";
1847 } else {
1848 field_end += " ";
1849 }
1850 for(let i = 0; i < enumNode.fields.length; i += 1) {
1851 let fieldNode = getAstNode(enumNode.fields[i]);
1852 let fieldName = fieldNode.name;
1853 let html = indent + escapeHtml(fieldName);
1854
1855 html += field_end;
1856
1857 name += html;
1858 }
1859 if (enumObj.nonexhaustive) {
1860 name += indent + "_" + field_end;
1861 }
1862 if (opts.indent) {
1863 name += opts.indent;
1864 }
1865 name += "}";
1866 return name;
1867 }
1868 case typeKinds.Union: {
1869 let unionObj = typeObj;
1870 let name = "";
1871 if (opts.wantHtml) {
1872 name = "<span class='tok-kw'>union</span>";
1873 } else {
1874 name = "union";
1875 }
1876 if (unionObj.auto_tag) {
1877 if (opts.wantHtml) {
1878 name += " (<span class='tok-kw'>enum</span>";
1879 } else {
1880 name += " (enum";
1881 }
1882 if (unionObj.tag) {
1883 name += "(" + exprName(unionObj.tag, opts) + "))";
1884 } else {
1885 name += ")";
1886 }
1887 } else if (unionObj.tag) {
1888 name += " (" + exprName(unionObj.tag, opts) + ")";
1889 }
1890 name += " { ";
1891 if (unionObj.fields.length > 1 && opts.wantHtml) {
1892 name += "</br>";
1893 }
1894 let indent = "";
1895 if (unionObj.fields.length > 1 && opts.wantHtml) {
1896 indent = "&nbsp;&nbsp;&nbsp;&nbsp;"
1897 }
1898 if (opts.indent) {
1899 indent = opts.indent + indent;
1900 }
1901 let unionNode = getAstNode(unionObj.src);
1902 let field_end = ",";
1903 if (unionObj.fields.length > 1 && opts.wantHtml) {
1904 field_end += "</br>";
1905 } else {
1906 field_end += " ";
1907 }
1908 for(let i = 0; i < unionObj.fields.length; i += 1) {
1909 let fieldNode = getAstNode(unionNode.fields[i]);
1910 let fieldName = fieldNode.name;
1911 let html = indent + escapeHtml(fieldName);
1912
1913 let fieldTypeExpr = unionObj.fields[i];
1914 html += ": ";
1915
1916 html += exprName(fieldTypeExpr, {...opts, indent: indent});
1917
1918 html += field_end;
1919
1920 name += html;
1921 }
1922 if (opts.indent) {
1923 name += opts.indent;
1924 }
1925 name += "}";
1926 return name;
1774 }1927 }
1775 case typeKinds.Opaque: {1928 case typeKinds.Opaque: {
1776 let opaqueObj = typeObj;1929 let opaqueObj = typeObj;
...@@ -3831,6 +3984,8 @@ const NAV_MODES = {...@@ -3831,6 +3984,8 @@ const NAV_MODES = {
3831 src: ty[2],3984 src: ty[2],
3832 privDecls: ty[3],3985 privDecls: ty[3],
3833 pubDecls: ty[4],3986 pubDecls: ty[4],
3987 tag: ty[5],
3988 nonexhaustive: ty[6],
3834 };3989 };
3835 case 20: // Union3990 case 20: // Union
3836 return {3991 return {
...@@ -3840,6 +3995,8 @@ const NAV_MODES = {...@@ -3840,6 +3995,8 @@ const NAV_MODES = {
3840 privDecls: ty[3],3995 privDecls: ty[3],
3841 pubDecls: ty[4],3996 pubDecls: ty[4],
3842 fields: ty[5],3997 fields: ty[5],
3998 tag: ty[6],
3999 auto_tag: ty[7],
3843 };4000 };
3844 case 21: // Fn4001 case 21: // Fn
3845 return {4002 return {
src/Autodoc.zig+22-6
...@@ -586,6 +586,8 @@ const DocData = struct {...@@ -586,6 +586,8 @@ const DocData = struct {
586 privDecls: []usize = &.{}, // index into decls586 privDecls: []usize = &.{}, // index into decls
587 pubDecls: []usize = &.{}, // index into decls587 pubDecls: []usize = &.{}, // index into decls
588 // (use src->fields to find field names)588 // (use src->fields to find field names)
589 tag: ?Expr = null, // tag type if specified
590 nonexhaustive: bool,
589 },591 },
590 Union: struct {592 Union: struct {
591 name: []const u8,593 name: []const u8,
...@@ -593,6 +595,8 @@ const DocData = struct {...@@ -593,6 +595,8 @@ const DocData = struct {
593 privDecls: []usize = &.{}, // index into decls595 privDecls: []usize = &.{}, // index into decls
594 pubDecls: []usize = &.{}, // index into decls596 pubDecls: []usize = &.{}, // index into decls
595 fields: []Expr = &.{}, // (use src->fields to find names)597 fields: []Expr = &.{}, // (use src->fields to find names)
598 tag: ?Expr, // tag type if specified
599 auto_enum: bool, // tag is an auto enum
596 },600 },
597 Fn: struct {601 Fn: struct {
598 name: []const u8,602 name: []const u8,
...@@ -2559,12 +2563,13 @@ fn walkInstruction(...@@ -2559,12 +2563,13 @@ fn walkInstruction(
2559 else2563 else
2560 parent_src;2564 parent_src;
25612565
2562 const tag_type: ?Ref = if (small.has_tag_type) blk: {2566 const tag_type: ?DocData.Expr = if (small.has_tag_type) blk: {
2563 const tag_type = file.zir.extra[extra_index];2567 const tag_type = file.zir.extra[extra_index];
2564 extra_index += 1;2568 extra_index += 1;
2565 break :blk @intToEnum(Ref, tag_type);2569 const tag_ref = @intToEnum(Ref, tag_type);
2570 const wr = try self.walkRef(file, parent_scope, parent_src, tag_ref, false);
2571 break :blk wr.expr;
2566 } else null;2572 } else null;
2567 _ = tag_type;
25682573
2569 const body_len = if (small.has_body_len) blk: {2574 const body_len = if (small.has_body_len) blk: {
2570 const body_len = file.zir.extra[extra_index];2575 const body_len = file.zir.extra[extra_index];
...@@ -2652,6 +2657,8 @@ fn walkInstruction(...@@ -2652,6 +2657,8 @@ fn walkInstruction(
2652 .privDecls = priv_decl_indexes.items,2657 .privDecls = priv_decl_indexes.items,
2653 .pubDecls = decl_indexes.items,2658 .pubDecls = decl_indexes.items,
2654 .fields = field_type_refs.items,2659 .fields = field_type_refs.items,
2660 .tag = tag_type,
2661 .auto_enum = small.auto_enum_tag,
2655 },2662 },
2656 };2663 };
26572664
...@@ -2698,12 +2705,13 @@ fn walkInstruction(...@@ -2698,12 +2705,13 @@ fn walkInstruction(
2698 else2705 else
2699 parent_src;2706 parent_src;
27002707
2701 const tag_type: ?Ref = if (small.has_tag_type) blk: {2708 const tag_type: ?DocData.Expr = if (small.has_tag_type) blk: {
2702 const tag_type = file.zir.extra[extra_index];2709 const tag_type = file.zir.extra[extra_index];
2703 extra_index += 1;2710 extra_index += 1;
2704 break :blk @intToEnum(Ref, tag_type);2711 const tag_ref = @intToEnum(Ref, tag_type);
2712 const wr = try self.walkRef(file, parent_scope, parent_src, tag_ref, false);
2713 break :blk wr.expr;
2705 } else null;2714 } else null;
2706 _ = tag_type;
27072715
2708 const body_len = if (small.has_body_len) blk: {2716 const body_len = if (small.has_body_len) blk: {
2709 const body_len = file.zir.extra[extra_index];2717 const body_len = file.zir.extra[extra_index];
...@@ -2816,6 +2824,8 @@ fn walkInstruction(...@@ -2816,6 +2824,8 @@ fn walkInstruction(
2816 .src = self_ast_node_index,2824 .src = self_ast_node_index,
2817 .privDecls = priv_decl_indexes.items,2825 .privDecls = priv_decl_indexes.items,
2818 .pubDecls = decl_indexes.items,2826 .pubDecls = decl_indexes.items,
2827 .tag = tag_type,
2828 .nonexhaustive = small.nonexhaustive,
2819 },2829 },
2820 };2830 };
2821 if (self.ref_paths_pending_on_types.get(type_slot_index)) |paths| {2831 if (self.ref_paths_pending_on_types.get(type_slot_index)) |paths| {
...@@ -4185,6 +4195,12 @@ fn collectStructFieldInfo(...@@ -4185,6 +4195,12 @@ fn collectStructFieldInfo(
41854195
4186 const break_inst = body[body.len - 1];4196 const break_inst = body[body.len - 1];
4187 const operand = data[break_inst].@"break".operand;4197 const operand = data[break_inst].@"break".operand;
4198 try self.ast_nodes.append(self.arena, .{
4199 .file = self.files.getIndex(file).?,
4200 .line = parent_src.line,
4201 .col = 0,
4202 .fields = null, // walkInstruction will fill `fields` if necessary
4203 });
4188 const walk_result = try self.walkRef(file, scope, parent_src, operand, false);4204 const walk_result = try self.walkRef(file, scope, parent_src, operand, false);
4189 break :expr walk_result.expr;4205 break :expr walk_result.expr;
4190 };4206 };