authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2023-04-13 17:53:13+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-04-13 17:53:13+02:00
logfbcf1c0006dc6656deb3f04e2f52d1da4a599952
tree92543a4474cf2ef150c2941f52e932fad16e618e
parent83d1f6b15a84864232eceafb24b95b8d16282a3f
parentaea886e2f3df6dd7bd89e16d42a953d379b0550a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #15261 from der-teufel-programming/autodoc-defaults

Autodoc: default values for fields in structs, explicit values for tags in enums

2 files changed, 50 insertions(+), 15 deletions(-)

lib/docs/main.js+21-9
...@@ -623,7 +623,7 @@ const NAV_MODES = {...@@ -623,7 +623,7 @@ const NAV_MODES = {
623 function typeIsStructWithNoFields(typeIndex) {623 function typeIsStructWithNoFields(typeIndex) {
624 let typeObj = getType(typeIndex);624 let typeObj = getType(typeIndex);
625 if (typeObj.kind !== typeKinds.Struct) return false;625 if (typeObj.kind !== typeKinds.Struct) return false;
626 return typeObj.fields.length == 0;626 return typeObj.field_types.length == 0;
627 }627 }
628628
629 function typeIsGenericFn(typeIndex) {629 function typeIsGenericFn(typeIndex) {
...@@ -2705,6 +2705,7 @@ const NAV_MODES = {...@@ -2705,6 +2705,7 @@ const NAV_MODES = {
2705 let declValue = resolveValue(uns.value);2705 let declValue = resolveValue(uns.value);
2706 if (!("type" in declValue.expr)) continue;2706 if (!("type" in declValue.expr)) continue;
2707 let uns_container = getType(declValue.expr.type);2707 let uns_container = getType(declValue.expr.type);
2708 if (!isContainerType(uns_container)) continue;
2708 categorizeDecls(2709 categorizeDecls(
2709 uns_container.pubDecls,2710 uns_container.pubDecls,
2710 typesList,2711 typesList,
...@@ -2858,9 +2859,12 @@ const NAV_MODES = {...@@ -2858,9 +2859,12 @@ const NAV_MODES = {
2858 escapeHtml(fieldName);2859 escapeHtml(fieldName);
28592860
2860 if (container.kind === typeKinds.Enum) {2861 if (container.kind === typeKinds.Enum) {
2861 html += ' = <span class="tok-number">' + fieldName + "</span>";2862 let value = container.values[i];
2863 if (value !== null) {
2864 html += " = " + exprName(value, { wantHtml: true, wantLink: true });
2865 }
2862 } else {2866 } else {
2863 let fieldTypeExpr = container.fields[i];2867 let fieldTypeExpr = container.field_types[i];
2864 if (container.kind !== typeKinds.Struct || !container.is_tuple) {2868 if (container.kind !== typeKinds.Struct || !container.is_tuple) {
2865 html += ": ";2869 html += ": ";
2866 }2870 }
...@@ -2869,6 +2873,12 @@ const NAV_MODES = {...@@ -2869,6 +2873,12 @@ const NAV_MODES = {
2869 if (tsn) {2873 if (tsn) {
2870 html += "<span> (" + tsn + ")</span>";2874 html += "<span> (" + tsn + ")</span>";
2871 }2875 }
2876 if (container.kind === typeKinds.Struct && !container.is_tuple) {
2877 let defaultInitExpr = container.field_defaults[i];
2878 if (defaultInitExpr !== null) {
2879 html += " = " + exprName(defaultInitExpr, { wantHtml: true, wantLink: true });
2880 }
2881 }
2872 }2882 }
28732883
2874 html += ",</pre></div>";2884 html += ",</pre></div>";
...@@ -4057,10 +4067,11 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {...@@ -4057,10 +4067,11 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
4057 src: ty[2],4067 src: ty[2],
4058 privDecls: ty[3],4068 privDecls: ty[3],
4059 pubDecls: ty[4],4069 pubDecls: ty[4],
4060 fields: ty[5],4070 field_types: ty[5],
4061 is_tuple: ty[6],4071 field_defaults: ty[6],
4062 line_number: ty[7],4072 is_tuple: ty[7],
4063 outer_decl: ty[8],4073 line_number: ty[8],
4074 outer_decl: ty[9],
4064 };4075 };
4065 case 10: // ComptimeExpr4076 case 10: // ComptimeExpr
4066 case 11: // ComptimeFloat4077 case 11: // ComptimeFloat
...@@ -4099,7 +4110,8 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {...@@ -4099,7 +4110,8 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
4099 privDecls: ty[3],4110 privDecls: ty[3],
4100 pubDecls: ty[4],4111 pubDecls: ty[4],
4101 tag: ty[5],4112 tag: ty[5],
4102 nonexhaustive: ty[6],4113 values: ty[6],
4114 nonexhaustive: ty[7],
4103 };4115 };
4104 case 20: // Union4116 case 20: // Union
4105 return {4117 return {
...@@ -4108,7 +4120,7 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {...@@ -4108,7 +4120,7 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
4108 src: ty[2],4120 src: ty[2],
4109 privDecls: ty[3],4121 privDecls: ty[3],
4110 pubDecls: ty[4],4122 pubDecls: ty[4],
4111 fields: ty[5],4123 field_types: ty[5],
4112 tag: ty[6],4124 tag: ty[6],
4113 auto_tag: ty[7],4125 auto_tag: ty[7],
4114 };4126 };
src/Autodoc.zig+29-6
...@@ -586,7 +586,8 @@ const DocData = struct {...@@ -586,7 +586,8 @@ const DocData = struct {
586 src: usize, // index into astNodes586 src: usize, // index into astNodes
587 privDecls: []usize = &.{}, // index into decls587 privDecls: []usize = &.{}, // index into decls
588 pubDecls: []usize = &.{}, // index into decls588 pubDecls: []usize = &.{}, // index into decls
589 fields: ?[]Expr = null, // (use src->fields to find names)589 field_types: []Expr = &.{}, // (use src->fields to find names)
590 field_defaults: []?Expr = &.{}, // default values is specified
590 is_tuple: bool,591 is_tuple: bool,
591 line_number: usize,592 line_number: usize,
592 outer_decl: usize,593 outer_decl: usize,
...@@ -614,6 +615,7 @@ const DocData = struct {...@@ -614,6 +615,7 @@ const DocData = struct {
614 pubDecls: []usize = &.{}, // index into decls615 pubDecls: []usize = &.{}, // index into decls
615 // (use src->fields to find field names)616 // (use src->fields to find field names)
616 tag: ?Expr = null, // tag type if specified617 tag: ?Expr = null, // tag type if specified
618 values: []?Expr = &.{}, // tag values if specified
617 nonexhaustive: bool,619 nonexhaustive: bool,
618 },620 },
619 Union: struct {621 Union: struct {
...@@ -2705,6 +2707,7 @@ fn walkInstruction(...@@ -2705,6 +2707,7 @@ fn walkInstruction(
2705 extra_index += body_len;2707 extra_index += body_len;
27062708
2707 var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};2709 var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
2710 var field_values: std.ArrayListUnmanaged(?DocData.Expr) = .{};
2708 {2711 {
2709 var bit_bag_idx = extra_index;2712 var bit_bag_idx = extra_index;
2710 var cur_bit_bag: u32 = undefined;2713 var cur_bit_bag: u32 = undefined;
...@@ -2726,12 +2729,13 @@ fn walkInstruction(...@@ -2726,12 +2729,13 @@ fn walkInstruction(
2726 const doc_comment_index = file.zir.extra[extra_index];2729 const doc_comment_index = file.zir.extra[extra_index];
2727 extra_index += 1;2730 extra_index += 1;
27282731
2729 const value_ref: ?Ref = if (has_value) blk: {2732 const value_expr: ?DocData.Expr = if (has_value) blk: {
2730 const value_ref = file.zir.extra[extra_index];2733 const value_ref = file.zir.extra[extra_index];
2731 extra_index += 1;2734 extra_index += 1;
2732 break :blk @intToEnum(Ref, value_ref);2735 const value = try self.walkRef(file, &scope, src_info, @intToEnum(Ref, value_ref), false);
2736 break :blk value.expr;
2733 } else null;2737 } else null;
2734 _ = value_ref;2738 try field_values.append(self.arena, value_expr);
27352739
2736 const field_name = file.zir.nullTerminatedString(field_name_index);2740 const field_name = file.zir.nullTerminatedString(field_name_index);
27372741
...@@ -2756,6 +2760,7 @@ fn walkInstruction(...@@ -2756,6 +2760,7 @@ fn walkInstruction(
2756 .privDecls = priv_decl_indexes.items,2760 .privDecls = priv_decl_indexes.items,
2757 .pubDecls = decl_indexes.items,2761 .pubDecls = decl_indexes.items,
2758 .tag = tag_type,2762 .tag = tag_type,
2763 .values = field_values.items,
2759 .nonexhaustive = small.nonexhaustive,2764 .nonexhaustive = small.nonexhaustive,
2760 },2765 },
2761 };2766 };
...@@ -2831,6 +2836,7 @@ fn walkInstruction(...@@ -2831,6 +2836,7 @@ fn walkInstruction(
2831 );2836 );
28322837
2833 var field_type_refs: std.ArrayListUnmanaged(DocData.Expr) = .{};2838 var field_type_refs: std.ArrayListUnmanaged(DocData.Expr) = .{};
2839 var field_default_refs: std.ArrayListUnmanaged(?DocData.Expr) = .{};
2834 var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};2840 var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
2835 try self.collectStructFieldInfo(2841 try self.collectStructFieldInfo(
2836 file,2842 file,
...@@ -2838,6 +2844,7 @@ fn walkInstruction(...@@ -2838,6 +2844,7 @@ fn walkInstruction(
2838 src_info,2844 src_info,
2839 fields_len,2845 fields_len,
2840 &field_type_refs,2846 &field_type_refs,
2847 &field_default_refs,
2841 &field_name_indexes,2848 &field_name_indexes,
2842 extra_index,2849 extra_index,
2843 small.is_tuple,2850 small.is_tuple,
...@@ -2851,7 +2858,8 @@ fn walkInstruction(...@@ -2851,7 +2858,8 @@ fn walkInstruction(
2851 .src = self_ast_node_index,2858 .src = self_ast_node_index,
2852 .privDecls = priv_decl_indexes.items,2859 .privDecls = priv_decl_indexes.items,
2853 .pubDecls = decl_indexes.items,2860 .pubDecls = decl_indexes.items,
2854 .fields = field_type_refs.items,2861 .field_types = field_type_refs.items,
2862 .field_defaults = field_default_refs.items,
2855 .is_tuple = small.is_tuple,2863 .is_tuple = small.is_tuple,
2856 .line_number = self.ast_nodes.items[self_ast_node_index].line,2864 .line_number = self.ast_nodes.items[self_ast_node_index].line,
2857 .outer_decl = type_slot_index - 1,2865 .outer_decl = type_slot_index - 1,
...@@ -4309,6 +4317,7 @@ fn collectStructFieldInfo(...@@ -4309,6 +4317,7 @@ fn collectStructFieldInfo(
4309 parent_src: SrcLocInfo,4317 parent_src: SrcLocInfo,
4310 fields_len: usize,4318 fields_len: usize,
4311 field_type_refs: *std.ArrayListUnmanaged(DocData.Expr),4319 field_type_refs: *std.ArrayListUnmanaged(DocData.Expr),
4320 field_default_refs: *std.ArrayListUnmanaged(?DocData.Expr),
4312 field_name_indexes: *std.ArrayListUnmanaged(usize),4321 field_name_indexes: *std.ArrayListUnmanaged(usize),
4313 ei: usize,4322 ei: usize,
4314 is_tuple: bool,4323 is_tuple: bool,
...@@ -4406,9 +4415,23 @@ fn collectStructFieldInfo(...@@ -4406,9 +4415,23 @@ fn collectStructFieldInfo(
4406 };4415 };
44074416
4408 extra_index += field.align_body_len;4417 extra_index += field.align_body_len;
4409 extra_index += field.init_body_len;4418
4419 const default_expr: ?DocData.Expr = def: {
4420 if (field.init_body_len == 0) {
4421 break :def null;
4422 }
4423
4424 const body = file.zir.extra[extra_index..][0..field.init_body_len];
4425 extra_index += body.len;
4426
4427 const break_inst = body[body.len - 1];
4428 const operand = data[break_inst].@"break".operand;
4429 const walk_result = try self.walkRef(file, scope, parent_src, operand, false);
4430 break :def walk_result.expr;
4431 };
44104432
4411 try field_type_refs.append(self.arena, type_expr);4433 try field_type_refs.append(self.arena, type_expr);
4434 try field_default_refs.append(self.arena, default_expr);
44124435
4413 // ast node4436 // ast node
4414 {4437 {