authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2023-05-09 20:09:42+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-05-09 20:09:42+02:00
log8e9c9f6fdc76bcb6ea0eb4b73a6683041c7a08ad
tree34a6fda06b49496b3b580e70271e59f10c61faff
parente21739dd8caf7d6a9aefe68c37eddb6406bcb810
parenta714f07022cb8a5d98e700090990111a7d1e234a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #15623 from der-teufel-programming/autodoc-backing-int

Autodoc: backing integer types for packed structs

2 files changed, 38 insertions(+), 23 deletions(-)

lib/docs/main.js+18-16
...@@ -1845,18 +1845,19 @@ const NAV_MODES = {...@@ -1845,18 +1845,19 @@ const NAV_MODES = {
1845 let structObj = typeObj;1845 let structObj = typeObj;
1846 let name = "";1846 let name = "";
1847 if (opts.wantHtml) {1847 if (opts.wantHtml) {
1848 if (structObj.is_tuple) {1848 name = "<span class='tok-kw'>struct</span>";
1849 name = "<span class='tok-kw'>tuple</span> { ";
1850 } else {
1851 name = "<span class='tok-kw'>struct</span> { ";
1852 }
1853 } else {1849 } else {
1854 if (structObj.is_tuple) {1850 name = "struct";
1855 name = "tuple { ";1851 }
1852 if (structObj.backing_int !== null) {
1853 if (opts.wantHtml) {
1854 name = "<span class='tok-kw'>packed</span> " + name;
1856 } else {1855 } else {
1857 name = "struct { ";1856 name = "packed " + name;
1858 }1857 }
1858 name += " (" + exprName(structObj.backing_int, opts) + ")";
1859 }1859 }
1860 name += " { ";
1860 if (structObj.field_types.length > 1 && opts.wantHtml) { name += "</br>"; }1861 if (structObj.field_types.length > 1 && opts.wantHtml) { name += "</br>"; }
1861 let indent = "";1862 let indent = "";
1862 if (structObj.field_types.length > 1 && opts.wantHtml) {1863 if (structObj.field_types.length > 1 && opts.wantHtml) {
...@@ -1982,11 +1983,11 @@ const NAV_MODES = {...@@ -1982,11 +1983,11 @@ const NAV_MODES = {
1982 name += " (" + exprName(unionObj.tag, opts) + ")";1983 name += " (" + exprName(unionObj.tag, opts) + ")";
1983 }1984 }
1984 name += " { ";1985 name += " { ";
1985 if (unionObj.fields.length > 1 && opts.wantHtml) {1986 if (unionObj.field_types.length > 1 && opts.wantHtml) {
1986 name += "</br>";1987 name += "</br>";
1987 }1988 }
1988 let indent = "";1989 let indent = "";
1989 if (unionObj.fields.length > 1 && opts.wantHtml) {1990 if (unionObj.field_types.length > 1 && opts.wantHtml) {
1990 indent = "&nbsp;&nbsp;&nbsp;&nbsp;"1991 indent = "&nbsp;&nbsp;&nbsp;&nbsp;"
1991 }1992 }
1992 if (opts.indent) {1993 if (opts.indent) {
...@@ -1994,17 +1995,17 @@ const NAV_MODES = {...@@ -1994,17 +1995,17 @@ const NAV_MODES = {
1994 }1995 }
1995 let unionNode = getAstNode(unionObj.src);1996 let unionNode = getAstNode(unionObj.src);
1996 let field_end = ",";1997 let field_end = ",";
1997 if (unionObj.fields.length > 1 && opts.wantHtml) {1998 if (unionObj.field_types.length > 1 && opts.wantHtml) {
1998 field_end += "</br>";1999 field_end += "</br>";
1999 } else {2000 } else {
2000 field_end += " ";2001 field_end += " ";
2001 }2002 }
2002 for (let i = 0; i < unionObj.fields.length; i += 1) {2003 for (let i = 0; i < unionObj.field_types.length; i += 1) {
2003 let fieldNode = getAstNode(unionNode.fields[i]);2004 let fieldNode = getAstNode(unionNode.fields[i]);
2004 let fieldName = fieldNode.name;2005 let fieldName = fieldNode.name;
2005 let html = indent + escapeHtml(fieldName);2006 let html = indent + escapeHtml(fieldName);
20062007
2007 let fieldTypeExpr = unionObj.fields[i];2008 let fieldTypeExpr = unionObj.field_types[i];
2008 html += ": ";2009 html += ": ";
20092010
2010 html += exprName(fieldTypeExpr, { ...opts, indent: indent });2011 html += exprName(fieldTypeExpr, { ...opts, indent: indent });
...@@ -4494,9 +4495,10 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {...@@ -4494,9 +4495,10 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
4494 pubDecls: ty[4],4495 pubDecls: ty[4],
4495 field_types: ty[5],4496 field_types: ty[5],
4496 field_defaults: ty[6],4497 field_defaults: ty[6],
4497 is_tuple: ty[7],4498 backing_int: ty[7],
4498 line_number: ty[8],4499 is_tuple: ty[8],
4499 parent_container: ty[9],4500 line_number: ty[9],
4501 parent_container: ty[10],
4500 };4502 };
4501 case 10: // ComptimeExpr4503 case 10: // ComptimeExpr
4502 case 11: // ComptimeFloat4504 case 11: // ComptimeFloat
src/Autodoc.zig+20-7
...@@ -604,6 +604,7 @@ const DocData = struct {...@@ -604,6 +604,7 @@ const DocData = struct {
604 pubDecls: []usize = &.{}, // index into decls604 pubDecls: []usize = &.{}, // index into decls
605 field_types: []Expr = &.{}, // (use src->fields to find names)605 field_types: []Expr = &.{}, // (use src->fields to find names)
606 field_defaults: []?Expr = &.{}, // default values is specified606 field_defaults: []?Expr = &.{}, // default values is specified
607 backing_int: ?Expr = null, // backing integer if specified
607 is_tuple: bool,608 is_tuple: bool,
608 line_number: usize,609 line_number: usize,
609 parent_container: ?usize, // index into `types`610 parent_container: ?usize, // index into `types`
...@@ -2593,12 +2594,12 @@ fn walkInstruction(...@@ -2593,12 +2594,12 @@ fn walkInstruction(
25932594
2594 // We delay analysis because union tags can refer to2595 // We delay analysis because union tags can refer to
2595 // decls defined inside the union itself.2596 // decls defined inside the union itself.
2596 const tag_type_ref: Ref = if (small.has_tag_type) blk: {2597 const tag_type_ref: ?Ref = if (small.has_tag_type) blk: {
2597 const tag_type = file.zir.extra[extra_index];2598 const tag_type = file.zir.extra[extra_index];
2598 extra_index += 1;2599 extra_index += 1;
2599 const tag_ref = @intToEnum(Ref, tag_type);2600 const tag_ref = @intToEnum(Ref, tag_type);
2600 break :blk tag_ref;2601 break :blk tag_ref;
2601 } else .none;2602 } else null;
26022603
2603 const body_len = if (small.has_body_len) blk: {2604 const body_len = if (small.has_body_len) blk: {
2604 const body_len = file.zir.extra[extra_index];2605 const body_len = file.zir.extra[extra_index];
...@@ -2625,13 +2626,13 @@ fn walkInstruction(...@@ -2625,13 +2626,13 @@ fn walkInstruction(
2625 );2626 );
26262627
2627 // Analyze the tag once all decls have been analyzed2628 // Analyze the tag once all decls have been analyzed
2628 const tag_type = try self.walkRef(2629 const tag_type = if (tag_type_ref) |tt_ref| (try self.walkRef(
2629 file,2630 file,
2630 &scope,2631 &scope,
2631 parent_src,2632 parent_src,
2632 tag_type_ref,2633 tt_ref,
2633 false,2634 false,
2634 );2635 )).expr else null;
26352636
2636 // Fields2637 // Fields
2637 extra_index += body_len;2638 extra_index += body_len;
...@@ -2663,7 +2664,7 @@ fn walkInstruction(...@@ -2663,7 +2664,7 @@ fn walkInstruction(
2663 .privDecls = priv_decl_indexes.items,2664 .privDecls = priv_decl_indexes.items,
2664 .pubDecls = decl_indexes.items,2665 .pubDecls = decl_indexes.items,
2665 .fields = field_type_refs.items,2666 .fields = field_type_refs.items,
2666 .tag = tag_type.expr,2667 .tag = tag_type,
2667 .auto_enum = small.auto_enum_tag,2668 .auto_enum = small.auto_enum_tag,
2668 .parent_container = parent_scope.enclosing_type,2669 .parent_container = parent_scope.enclosing_type,
2669 },2670 },
...@@ -2854,13 +2855,24 @@ fn walkInstruction(...@@ -2854,13 +2855,24 @@ fn walkInstruction(
2854 break :blk fields_len;2855 break :blk fields_len;
2855 } else 0;2856 } else 0;
28562857
2857 // TODO: Expose explicit backing integer types in some way.2858 // We don't care about decls yet
2859 if (small.has_decls_len) extra_index += 1;
2860
2861 var backing_int: ?DocData.Expr = null;
2858 if (small.has_backing_int) {2862 if (small.has_backing_int) {
2859 const backing_int_body_len = file.zir.extra[extra_index];2863 const backing_int_body_len = file.zir.extra[extra_index];
2860 extra_index += 1; // backing_int_body_len2864 extra_index += 1; // backing_int_body_len
2861 if (backing_int_body_len == 0) {2865 if (backing_int_body_len == 0) {
2866 const backing_int_ref = @intToEnum(Ref, file.zir.extra[extra_index]);
2867 const backing_int_res = try self.walkRef(file, &scope, src_info, backing_int_ref, true);
2868 backing_int = backing_int_res.expr;
2862 extra_index += 1; // backing_int_ref2869 extra_index += 1; // backing_int_ref
2863 } else {2870 } else {
2871 const backing_int_body = file.zir.extra[extra_index..][0..backing_int_body_len];
2872 const break_inst = backing_int_body[backing_int_body.len - 1];
2873 const operand = data[break_inst].@"break".operand;
2874 const backing_int_res = try self.walkRef(file, &scope, src_info, operand, true);
2875 backing_int = backing_int_res.expr;
2864 extra_index += backing_int_body_len; // backing_int_body_inst2876 extra_index += backing_int_body_len; // backing_int_body_inst
2865 }2877 }
2866 }2878 }
...@@ -2903,6 +2915,7 @@ fn walkInstruction(...@@ -2903,6 +2915,7 @@ fn walkInstruction(
2903 .field_types = field_type_refs.items,2915 .field_types = field_type_refs.items,
2904 .field_defaults = field_default_refs.items,2916 .field_defaults = field_default_refs.items,
2905 .is_tuple = small.is_tuple,2917 .is_tuple = small.is_tuple,
2918 .backing_int = backing_int,
2906 .line_number = self.ast_nodes.items[self_ast_node_index].line,2919 .line_number = self.ast_nodes.items[self_ast_node_index].line,
2907 .parent_container = parent_scope.enclosing_type,2920 .parent_container = parent_scope.enclosing_type,
2908 },2921 },