Skip to content

Commit 3a0a9aa

Browse files
authored
Merge pull request #22245 from mlugg/zir-no-doc-comments
compiler: remove doc comments from Zir
2 parents 8f330ab + 847c34a commit 3a0a9aa

File tree

7 files changed

+60
-211
lines changed

7 files changed

+60
-211
lines changed

lib/std/zig/AstGen.zig

Lines changed: 15 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ fn fnProtoExpr(
13771377
const tag: Zir.Inst.Tag = if (is_comptime) .param_comptime else .param;
13781378
// We pass `prev_param_insts` as `&.{}` here because a function prototype can't refer to previous
13791379
// arguments (we haven't set up scopes here).
1380-
const param_inst = try block_scope.addParam(&param_gz, &.{}, tag, name_token, param_name, param.first_doc_comment);
1380+
const param_inst = try block_scope.addParam(&param_gz, &.{}, tag, name_token, param_name);
13811381
assert(param_inst_expected == param_inst);
13821382
}
13831383
}
@@ -4172,8 +4172,6 @@ fn fnDecl(
41724172
break :blk token_tags[maybe_noinline_token] == .keyword_noinline;
41734173
};
41744174

4175-
const doc_comment_index = try astgen.docCommentAsString(fn_proto.firstToken());
4176-
41774175
wip_members.nextDecl(decl_inst);
41784176

41794177
// Note that the capacity here may not be sufficient, as this does not include `anytype` parameters.
@@ -4263,7 +4261,7 @@ fn fnDecl(
42634261
const main_tokens = tree.nodes.items(.main_token);
42644262
const name_token = param.name_token orelse main_tokens[param_type_node];
42654263
const tag: Zir.Inst.Tag = if (is_comptime) .param_comptime else .param;
4266-
const param_inst = try decl_gz.addParam(&param_gz, param_insts.items, tag, name_token, param_name, param.first_doc_comment);
4264+
const param_inst = try decl_gz.addParam(&param_gz, param_insts.items, tag, name_token, param_name);
42674265
assert(param_inst_expected == param_inst);
42684266
break :param param_inst.toRef();
42694267
};
@@ -4525,7 +4523,6 @@ fn fnDecl(
45254523
decl_gz.decl_line,
45264524
is_pub,
45274525
is_export,
4528-
doc_comment_index,
45294526
&decl_gz,
45304527
// align, linksection, and addrspace are passed in the func instruction in this case.
45314528
// TODO: move them from the function instruction to the declaration instruction?
@@ -4598,8 +4595,6 @@ fn globalVarDecl(
45984595
break :blk lib_name_str.index;
45994596
} else .empty;
46004597

4601-
const doc_comment_index = try astgen.docCommentAsString(var_decl.firstToken());
4602-
46034598
assert(var_decl.comptime_token == null); // handled by parser
46044599

46054600
const var_inst: Zir.Inst.Ref = if (var_decl.ast.init_node != 0) vi: {
@@ -4698,7 +4693,6 @@ fn globalVarDecl(
46984693
block_scope.decl_line,
46994694
is_pub,
47004695
is_export,
4701-
doc_comment_index,
47024696
&block_scope,
47034697
.{
47044698
.align_gz = &align_gz,
@@ -4756,7 +4750,6 @@ fn comptimeDecl(
47564750
decl_block.decl_line,
47574751
false,
47584752
false,
4759-
.empty,
47604753
&decl_block,
47614754
null,
47624755
);
@@ -4814,7 +4807,6 @@ fn usingnamespaceDecl(
48144807
decl_block.decl_line,
48154808
is_pub,
48164809
false,
4817-
.empty,
48184810
&decl_block,
48194811
null,
48204812
);
@@ -4932,7 +4924,7 @@ fn testDecl(
49324924
return astgen.failTok(test_name_token, "use of undeclared identifier '{s}'", .{ident_name});
49334925
}
49344926

4935-
break :blk .{ .decltest = name_str_index };
4927+
break :blk .{ .decltest = test_name_token };
49364928
},
49374929
};
49384930

@@ -5021,7 +5013,6 @@ fn testDecl(
50215013
decl_block.decl_line,
50225014
false,
50235015
false,
5024-
.empty,
50255016
&decl_block,
50265017
null,
50275018
);
@@ -5174,9 +5165,6 @@ fn structDeclInner(
51745165
assert(!member.ast.tuple_like);
51755166
wip_members.appendToField(@intFromEnum(field_name));
51765167

5177-
const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
5178-
wip_members.appendToField(@intFromEnum(doc_comment_index));
5179-
51805168
if (member.ast.type_expr == 0) {
51815169
return astgen.failTok(member.ast.main_token, "struct field missing type", .{});
51825170
}
@@ -5448,7 +5436,7 @@ fn unionDeclInner(
54485436
.none;
54495437

54505438
const bits_per_field = 4;
5451-
const max_field_size = 5;
5439+
const max_field_size = 4;
54525440
var any_aligned_fields = false;
54535441
var wip_members = try WipMembers.init(gpa, &astgen.scratch, decl_count, field_count, bits_per_field, max_field_size);
54545442
defer wip_members.deinit();
@@ -5479,9 +5467,6 @@ fn unionDeclInner(
54795467
const field_name = try astgen.identAsString(member.ast.main_token);
54805468
wip_members.appendToField(@intFromEnum(field_name));
54815469

5482-
const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
5483-
wip_members.appendToField(@intFromEnum(doc_comment_index));
5484-
54855470
const have_type = member.ast.type_expr != 0;
54865471
const have_align = member.ast.align_expr != 0;
54875472
const have_value = member.ast.value_expr != 0;
@@ -5744,7 +5729,7 @@ fn containerDecl(
57445729
.none;
57455730

57465731
const bits_per_field = 1;
5747-
const max_field_size = 3;
5732+
const max_field_size = 2;
57485733
var wip_members = try WipMembers.init(gpa, &astgen.scratch, @intCast(counts.decls), @intCast(counts.total_fields), bits_per_field, max_field_size);
57495734
defer wip_members.deinit();
57505735

@@ -5772,9 +5757,6 @@ fn containerDecl(
57725757
const field_name = try astgen.identAsString(member.ast.main_token);
57735758
wip_members.appendToField(@intFromEnum(field_name));
57745759

5775-
const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
5776-
wip_members.appendToField(@intFromEnum(doc_comment_index));
5777-
57785760
const have_value = member.ast.value_expr != 0;
57795761
wip_members.nextField(bits_per_field, .{have_value});
57805762

@@ -6054,10 +6036,7 @@ fn errorSetDecl(gz: *GenZir, ri: ResultInfo, node: Ast.Node.Index) InnerError!Zi
60546036
}
60556037
gop.value_ptr.* = tok_i;
60566038

6057-
try astgen.extra.ensureUnusedCapacity(gpa, 2);
6058-
astgen.extra.appendAssumeCapacity(@intFromEnum(str_index));
6059-
const doc_comment_index = try astgen.docCommentAsString(tok_i);
6060-
astgen.extra.appendAssumeCapacity(@intFromEnum(doc_comment_index));
6039+
try astgen.extra.append(gpa, @intFromEnum(str_index));
60616040
fields_len += 1;
60626041
},
60636042
.r_brace => break,
@@ -11719,73 +11698,6 @@ fn identAsString(astgen: *AstGen, ident_token: Ast.TokenIndex) !Zir.NullTerminat
1171911698
}
1172011699
}
1172111700

11722-
/// Adds a doc comment block to `string_bytes` by walking backwards from `end_token`.
11723-
/// `end_token` must point at the first token after the last doc comment line.
11724-
/// Returns 0 if no doc comment is present.
11725-
fn docCommentAsString(astgen: *AstGen, end_token: Ast.TokenIndex) !Zir.NullTerminatedString {
11726-
if (end_token == 0) return .empty;
11727-
11728-
const token_tags = astgen.tree.tokens.items(.tag);
11729-
11730-
var tok = end_token - 1;
11731-
while (token_tags[tok] == .doc_comment) {
11732-
if (tok == 0) break;
11733-
tok -= 1;
11734-
} else {
11735-
tok += 1;
11736-
}
11737-
11738-
return docCommentAsStringFromFirst(astgen, end_token, tok);
11739-
}
11740-
11741-
/// end_token must be > the index of the last doc comment.
11742-
fn docCommentAsStringFromFirst(
11743-
astgen: *AstGen,
11744-
end_token: Ast.TokenIndex,
11745-
start_token: Ast.TokenIndex,
11746-
) !Zir.NullTerminatedString {
11747-
if (start_token == end_token) return .empty;
11748-
11749-
const gpa = astgen.gpa;
11750-
const string_bytes = &astgen.string_bytes;
11751-
const str_index: u32 = @intCast(string_bytes.items.len);
11752-
const token_starts = astgen.tree.tokens.items(.start);
11753-
const token_tags = astgen.tree.tokens.items(.tag);
11754-
11755-
const total_bytes = token_starts[end_token] - token_starts[start_token];
11756-
try string_bytes.ensureUnusedCapacity(gpa, total_bytes);
11757-
11758-
var current_token = start_token;
11759-
while (current_token < end_token) : (current_token += 1) {
11760-
switch (token_tags[current_token]) {
11761-
.doc_comment => {
11762-
const tok_bytes = astgen.tree.tokenSlice(current_token)[3..];
11763-
string_bytes.appendSliceAssumeCapacity(tok_bytes);
11764-
if (current_token != end_token - 1) {
11765-
string_bytes.appendAssumeCapacity('\n');
11766-
}
11767-
},
11768-
else => break,
11769-
}
11770-
}
11771-
11772-
const key: []const u8 = string_bytes.items[str_index..];
11773-
const gop = try astgen.string_table.getOrPutContextAdapted(gpa, key, StringIndexAdapter{
11774-
.bytes = string_bytes,
11775-
}, StringIndexContext{
11776-
.bytes = string_bytes,
11777-
});
11778-
11779-
if (gop.found_existing) {
11780-
string_bytes.shrinkRetainingCapacity(str_index);
11781-
return @enumFromInt(gop.key_ptr.*);
11782-
} else {
11783-
gop.key_ptr.* = str_index;
11784-
try string_bytes.append(gpa, 0);
11785-
return @enumFromInt(str_index);
11786-
}
11787-
}
11788-
1178911701
const IndexSlice = struct { index: Zir.NullTerminatedString, len: u32 };
1179011702

1179111703
fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice {
@@ -12722,22 +12634,15 @@ const GenZir = struct {
1272212634
/// Absolute token index. This function does the conversion to Decl offset.
1272312635
abs_tok_index: Ast.TokenIndex,
1272412636
name: Zir.NullTerminatedString,
12725-
first_doc_comment: ?Ast.TokenIndex,
1272612637
) !Zir.Inst.Index {
1272712638
const gpa = gz.astgen.gpa;
1272812639
const param_body = param_gz.instructionsSlice();
1272912640
const body_len = gz.astgen.countBodyLenAfterFixupsExtraRefs(param_body, prev_param_insts);
1273012641
try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
1273112642
try gz.astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Param).@"struct".fields.len + body_len);
1273212643

12733-
const doc_comment_index = if (first_doc_comment) |first|
12734-
try gz.astgen.docCommentAsStringFromFirst(abs_tok_index, first)
12735-
else
12736-
.empty;
12737-
1273812644
const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Param{
1273912645
.name = name,
12740-
.doc_comment = doc_comment_index,
1274112646
.body_len = @intCast(body_len),
1274212647
});
1274312648
gz.astgen.appendBodyWithFixupsExtraRefsArrayList(&gz.astgen.extra, param_body, prev_param_insts);
@@ -14143,8 +14048,8 @@ fn lowerAstErrors(astgen: *AstGen) !void {
1414314048
const DeclarationName = union(enum) {
1414414049
named: Ast.TokenIndex,
1414514050
named_test: Ast.TokenIndex,
14051+
decltest: Ast.TokenIndex,
1414614052
unnamed_test,
14147-
decltest: Zir.NullTerminatedString,
1414814053
@"comptime",
1414914054
@"usingnamespace",
1415014055
};
@@ -14174,7 +14079,6 @@ fn addFailedDeclaration(
1417414079
gz.astgen.source_line,
1417514080
is_pub,
1417614081
false, // we don't care about exports since semantic analysis will fail
14177-
.empty,
1417814082
&decl_gz,
1417914083
null,
1418014084
);
@@ -14189,7 +14093,6 @@ fn setDeclaration(
1418914093
src_line: u32,
1419014094
is_pub: bool,
1419114095
is_export: bool,
14192-
doc_comment: Zir.NullTerminatedString,
1419314096
value_gz: *GenZir,
1419414097
/// May be `null` if all these blocks would be empty.
1419514098
/// If `null`, then `value_gz` must have nothing stacked on it.
@@ -14218,11 +14121,6 @@ fn setDeclaration(
1421814121
const linksection_len = astgen.countBodyLenAfterFixups(linksection_body);
1421914122
const addrspace_len = astgen.countBodyLenAfterFixups(addrspace_body);
1422014123

14221-
const true_doc_comment: Zir.NullTerminatedString = switch (name) {
14222-
.decltest => |test_name| test_name,
14223-
else => doc_comment,
14224-
};
14225-
1422614124
const src_hash_arr: [4]u32 = @bitCast(src_hash);
1422714125

1422814126
const extra: Zir.Inst.Declaration = .{
@@ -14233,8 +14131,14 @@ fn setDeclaration(
1423314131
.name = switch (name) {
1423414132
.named => |tok| @enumFromInt(@intFromEnum(try astgen.identAsString(tok))),
1423514133
.named_test => |tok| @enumFromInt(@intFromEnum(try astgen.testNameString(tok))),
14134+
.decltest => |tok| @enumFromInt(str_idx: {
14135+
const idx = astgen.string_bytes.items.len;
14136+
try astgen.string_bytes.append(gpa, 0); // indicates this is a test
14137+
try astgen.appendIdentStr(tok, &astgen.string_bytes);
14138+
try astgen.string_bytes.append(gpa, 0); // end of the string
14139+
break :str_idx idx;
14140+
}),
1423614141
.unnamed_test => .unnamed_test,
14237-
.decltest => .decltest,
1423814142
.@"comptime" => .@"comptime",
1423914143
.@"usingnamespace" => .@"usingnamespace",
1424014144
},
@@ -14243,14 +14147,11 @@ fn setDeclaration(
1424314147
.value_body_len = @intCast(value_len),
1424414148
.is_pub = is_pub,
1424514149
.is_export = is_export,
14246-
.has_doc_comment = true_doc_comment != .empty,
14150+
.test_is_decltest = name == .decltest,
1424714151
.has_align_linksection_addrspace = align_len != 0 or linksection_len != 0 or addrspace_len != 0,
1424814152
},
1424914153
};
1425014154
astgen.instructions.items(.data)[@intFromEnum(decl_inst)].declaration.payload_index = try astgen.addExtra(extra);
14251-
if (extra.flags.has_doc_comment) {
14252-
try astgen.extra.append(gpa, @intFromEnum(true_doc_comment));
14253-
}
1425414155
if (extra.flags.has_align_linksection_addrspace) {
1425514156
try astgen.extra.appendSlice(gpa, &.{
1425614157
align_len,

0 commit comments

Comments
 (0)