authorgravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2023-04-12 19:50:50+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2023-04-13 17:42:29+02:00
log8bbf62c3b98da474dbbde907819a74133da9f430
tree11969c11103cc3ce4fc687b048d52fa6661fdd1d
parent97b3b36c65faad1fa66df82e53e3f464b9cedb44

autodoc: Handling of explicit values for enum fields

Fixes to frontend handling of structs

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

lib/docs/main.js+9-4
......@@ -623,7 +623,7 @@ const NAV_MODES = {
623623 function typeIsStructWithNoFields(typeIndex) {
624624 let typeObj = getType(typeIndex);
625625 if (typeObj.kind !== typeKinds.Struct) return false;
626 return typeObj.fields.length == 0;
626 return typeObj.field_types.length == 0;
627627 }
628628
629629 function typeIsGenericFn(typeIndex) {
......@@ -2705,6 +2705,7 @@ const NAV_MODES = {
27052705 let declValue = resolveValue(uns.value);
27062706 if (!("type" in declValue.expr)) continue;
27072707 let uns_container = getType(declValue.expr.type);
2708 if (!isContainerType(uns_container)) continue;
27082709 categorizeDecls(
27092710 uns_container.pubDecls,
27102711 typesList,
......@@ -2858,7 +2859,10 @@ const NAV_MODES = {
28582859 escapeHtml(fieldName);
28592860
28602861 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 }
28622866 } else {
28632867 let fieldTypeExpr = container.field_types[i];
28642868 if (container.kind !== typeKinds.Struct || !container.is_tuple) {
......@@ -4106,7 +4110,8 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
41064110 privDecls: ty[3],
41074111 pubDecls: ty[4],
41084112 tag: ty[5],
4109 nonexhaustive: ty[6],
4113 values: ty[6],
4114 nonexhaustive: ty[7],
41104115 };
41114116 case 20: // Union
41124117 return {
......@@ -4115,7 +4120,7 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
41154120 src: ty[2],
41164121 privDecls: ty[3],
41174122 pubDecls: ty[4],
4118 fields: ty[5],
4123 field_types: ty[5],
41194124 tag: ty[6],
41204125 auto_tag: ty[7],
41214126 };
src/Autodoc.zig+9-5
......@@ -586,8 +586,8 @@ const DocData = struct {
586586 src: usize, // index into astNodes
587587 privDecls: []usize = &.{}, // index into decls
588588 pubDecls: []usize = &.{}, // index into decls
589 field_types: ?[]Expr = null, // (use src->fields to find names)
590 field_defaults: ?[]?Expr = null,
589 field_types: []Expr = &.{}, // (use src->fields to find names)
590 field_defaults: []?Expr = &.{}, // default values is specified
591591 is_tuple: bool,
592592 line_number: usize,
593593 outer_decl: usize,
......@@ -615,6 +615,7 @@ const DocData = struct {
615615 pubDecls: []usize = &.{}, // index into decls
616616 // (use src->fields to find field names)
617617 tag: ?Expr = null, // tag type if specified
618 values: []?Expr = &.{}, // tag values if specified
618619 nonexhaustive: bool,
619620 },
620621 Union: struct {
......@@ -2706,6 +2707,7 @@ fn walkInstruction(
27062707 extra_index += body_len;
27072708
27082709 var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
2710 var field_values: std.ArrayListUnmanaged(?DocData.Expr) = .{};
27092711 {
27102712 var bit_bag_idx = extra_index;
27112713 var cur_bit_bag: u32 = undefined;
......@@ -2727,12 +2729,13 @@ fn walkInstruction(
27272729 const doc_comment_index = file.zir.extra[extra_index];
27282730 extra_index += 1;
27292731
2730 const value_ref: ?Ref = if (has_value) blk: {
2732 const value_expr: ?DocData.Expr = if (has_value) blk: {
27312733 const value_ref = file.zir.extra[extra_index];
27322734 extra_index += 1;
2733 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;
27342737 } else null;
2735 _ = value_ref;
2738 try field_values.append(self.arena, value_expr);
27362739
27372740 const field_name = file.zir.nullTerminatedString(field_name_index);
27382741
......@@ -2757,6 +2760,7 @@ fn walkInstruction(
27572760 .privDecls = priv_decl_indexes.items,
27582761 .pubDecls = decl_indexes.items,
27592762 .tag = tag_type,
2763 .values = field_values.items,
27602764 .nonexhaustive = small.nonexhaustive,
27612765 },
27622766 };