autodoc: Handling of explicit values for enum fields

Fixes to frontend handling of structs
This commit is contained in:
Krzysztof Wolicki 2023-04-12 19:50:50 +02:00 committed by Loris Cro
parent 97b3b36c65
commit 8bbf62c3b9
2 changed files with 18 additions and 9 deletions

View File

@ -623,7 +623,7 @@ const NAV_MODES = {
function typeIsStructWithNoFields(typeIndex) {
let typeObj = getType(typeIndex);
if (typeObj.kind !== typeKinds.Struct) return false;
return typeObj.fields.length == 0;
return typeObj.field_types.length == 0;
}
function typeIsGenericFn(typeIndex) {
@ -2705,6 +2705,7 @@ const NAV_MODES = {
let declValue = resolveValue(uns.value);
if (!("type" in declValue.expr)) continue;
let uns_container = getType(declValue.expr.type);
if (!isContainerType(uns_container)) continue;
categorizeDecls(
uns_container.pubDecls,
typesList,
@ -2858,7 +2859,10 @@ const NAV_MODES = {
escapeHtml(fieldName);
if (container.kind === typeKinds.Enum) {
html += ' = <span class="tok-number">' + fieldName + "</span>";
let value = container.values[i];
if (value !== null) {
html += " = " + exprName(value, { wantHtml: true, wantLink: true });
}
} else {
let fieldTypeExpr = container.field_types[i];
if (container.kind !== typeKinds.Struct || !container.is_tuple) {
@ -4106,7 +4110,8 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
privDecls: ty[3],
pubDecls: ty[4],
tag: ty[5],
nonexhaustive: ty[6],
values: ty[6],
nonexhaustive: ty[7],
};
case 20: // Union
return {
@ -4115,7 +4120,7 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
src: ty[2],
privDecls: ty[3],
pubDecls: ty[4],
fields: ty[5],
field_types: ty[5],
tag: ty[6],
auto_tag: ty[7],
};

View File

@ -586,8 +586,8 @@ const DocData = struct {
src: usize, // index into astNodes
privDecls: []usize = &.{}, // index into decls
pubDecls: []usize = &.{}, // index into decls
field_types: ?[]Expr = null, // (use src->fields to find names)
field_defaults: ?[]?Expr = null,
field_types: []Expr = &.{}, // (use src->fields to find names)
field_defaults: []?Expr = &.{}, // default values is specified
is_tuple: bool,
line_number: usize,
outer_decl: usize,
@ -615,6 +615,7 @@ const DocData = struct {
pubDecls: []usize = &.{}, // index into decls
// (use src->fields to find field names)
tag: ?Expr = null, // tag type if specified
values: []?Expr = &.{}, // tag values if specified
nonexhaustive: bool,
},
Union: struct {
@ -2706,6 +2707,7 @@ fn walkInstruction(
extra_index += body_len;
var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
var field_values: std.ArrayListUnmanaged(?DocData.Expr) = .{};
{
var bit_bag_idx = extra_index;
var cur_bit_bag: u32 = undefined;
@ -2727,12 +2729,13 @@ fn walkInstruction(
const doc_comment_index = file.zir.extra[extra_index];
extra_index += 1;
const value_ref: ?Ref = if (has_value) blk: {
const value_expr: ?DocData.Expr = if (has_value) blk: {
const value_ref = file.zir.extra[extra_index];
extra_index += 1;
break :blk @intToEnum(Ref, value_ref);
const value = try self.walkRef(file, &scope, src_info, @intToEnum(Ref, value_ref), false);
break :blk value.expr;
} else null;
_ = value_ref;
try field_values.append(self.arena, value_expr);
const field_name = file.zir.nullTerminatedString(field_name_index);
@ -2757,6 +2760,7 @@ fn walkInstruction(
.privDecls = priv_decl_indexes.items,
.pubDecls = decl_indexes.items,
.tag = tag_type,
.values = field_values.items,
.nonexhaustive = small.nonexhaustive,
},
};