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

Autodoc: backing integer types for packed structs
This commit is contained in:
Loris Cro 2023-05-09 20:09:42 +02:00 committed by GitHub
commit 8e9c9f6fdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 25 deletions

View File

@ -1845,18 +1845,19 @@ const NAV_MODES = {
let structObj = typeObj;
let name = "";
if (opts.wantHtml) {
if (structObj.is_tuple) {
name = "<span class='tok-kw'>tuple</span> { ";
} else {
name = "<span class='tok-kw'>struct</span> { ";
}
name = "<span class='tok-kw'>struct</span>";
} else {
if (structObj.is_tuple) {
name = "tuple { ";
} else {
name = "struct { ";
}
name = "struct";
}
if (structObj.backing_int !== null) {
if (opts.wantHtml) {
name = "<span class='tok-kw'>packed</span> " + name;
} else {
name = "packed " + name;
}
name += " (" + exprName(structObj.backing_int, opts) + ")";
}
name += " { ";
if (structObj.field_types.length > 1 && opts.wantHtml) { name += "</br>"; }
let indent = "";
if (structObj.field_types.length > 1 && opts.wantHtml) {
@ -1982,11 +1983,11 @@ const NAV_MODES = {
name += " (" + exprName(unionObj.tag, opts) + ")";
}
name += " { ";
if (unionObj.fields.length > 1 && opts.wantHtml) {
if (unionObj.field_types.length > 1 && opts.wantHtml) {
name += "</br>";
}
let indent = "";
if (unionObj.fields.length > 1 && opts.wantHtml) {
if (unionObj.field_types.length > 1 && opts.wantHtml) {
indent = "&nbsp;&nbsp;&nbsp;&nbsp;"
}
if (opts.indent) {
@ -1994,17 +1995,17 @@ const NAV_MODES = {
}
let unionNode = getAstNode(unionObj.src);
let field_end = ",";
if (unionObj.fields.length > 1 && opts.wantHtml) {
if (unionObj.field_types.length > 1 && opts.wantHtml) {
field_end += "</br>";
} else {
field_end += " ";
}
for (let i = 0; i < unionObj.fields.length; i += 1) {
for (let i = 0; i < unionObj.field_types.length; i += 1) {
let fieldNode = getAstNode(unionNode.fields[i]);
let fieldName = fieldNode.name;
let html = indent + escapeHtml(fieldName);
let fieldTypeExpr = unionObj.fields[i];
let fieldTypeExpr = unionObj.field_types[i];
html += ": ";
html += exprName(fieldTypeExpr, { ...opts, indent: indent });
@ -4494,9 +4495,10 @@ function addDeclToSearchResults(decl, declIndex, modNames, item, list, stack) {
pubDecls: ty[4],
field_types: ty[5],
field_defaults: ty[6],
is_tuple: ty[7],
line_number: ty[8],
parent_container: ty[9],
backing_int: ty[7],
is_tuple: ty[8],
line_number: ty[9],
parent_container: ty[10],
};
case 10: // ComptimeExpr
case 11: // ComptimeFloat

View File

@ -604,6 +604,7 @@ const DocData = struct {
pubDecls: []usize = &.{}, // index into decls
field_types: []Expr = &.{}, // (use src->fields to find names)
field_defaults: []?Expr = &.{}, // default values is specified
backing_int: ?Expr = null, // backing integer if specified
is_tuple: bool,
line_number: usize,
parent_container: ?usize, // index into `types`
@ -2593,12 +2594,12 @@ fn walkInstruction(
// We delay analysis because union tags can refer to
// decls defined inside the union itself.
const tag_type_ref: Ref = if (small.has_tag_type) blk: {
const tag_type_ref: ?Ref = if (small.has_tag_type) blk: {
const tag_type = file.zir.extra[extra_index];
extra_index += 1;
const tag_ref = @intToEnum(Ref, tag_type);
break :blk tag_ref;
} else .none;
} else null;
const body_len = if (small.has_body_len) blk: {
const body_len = file.zir.extra[extra_index];
@ -2625,13 +2626,13 @@ fn walkInstruction(
);
// Analyze the tag once all decls have been analyzed
const tag_type = try self.walkRef(
const tag_type = if (tag_type_ref) |tt_ref| (try self.walkRef(
file,
&scope,
parent_src,
tag_type_ref,
tt_ref,
false,
);
)).expr else null;
// Fields
extra_index += body_len;
@ -2663,7 +2664,7 @@ fn walkInstruction(
.privDecls = priv_decl_indexes.items,
.pubDecls = decl_indexes.items,
.fields = field_type_refs.items,
.tag = tag_type.expr,
.tag = tag_type,
.auto_enum = small.auto_enum_tag,
.parent_container = parent_scope.enclosing_type,
},
@ -2854,13 +2855,24 @@ fn walkInstruction(
break :blk fields_len;
} else 0;
// TODO: Expose explicit backing integer types in some way.
// We don't care about decls yet
if (small.has_decls_len) extra_index += 1;
var backing_int: ?DocData.Expr = null;
if (small.has_backing_int) {
const backing_int_body_len = file.zir.extra[extra_index];
extra_index += 1; // backing_int_body_len
if (backing_int_body_len == 0) {
const backing_int_ref = @intToEnum(Ref, file.zir.extra[extra_index]);
const backing_int_res = try self.walkRef(file, &scope, src_info, backing_int_ref, true);
backing_int = backing_int_res.expr;
extra_index += 1; // backing_int_ref
} else {
const backing_int_body = file.zir.extra[extra_index..][0..backing_int_body_len];
const break_inst = backing_int_body[backing_int_body.len - 1];
const operand = data[break_inst].@"break".operand;
const backing_int_res = try self.walkRef(file, &scope, src_info, operand, true);
backing_int = backing_int_res.expr;
extra_index += backing_int_body_len; // backing_int_body_inst
}
}
@ -2903,6 +2915,7 @@ fn walkInstruction(
.field_types = field_type_refs.items,
.field_defaults = field_default_refs.items,
.is_tuple = small.is_tuple,
.backing_int = backing_int,
.line_number = self.ast_nodes.items[self_ast_node_index].line,
.parent_container = parent_scope.enclosing_type,
},