authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-01-20 19:59:40+01:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-01-21 22:00:57+01:00
log98fddd1c54171b080eb254527203b6f4e4e2f6ca
treee03dd6476556c97102fb7a42da0ae06be92427a1
parent3010ccfca5f65a8ee0d70559a38d39c2c56f7367

add field doc comments to zir

Doc comment information is stored in `extra` unconditionally for each field. This commmit covers Structs, Enums, Unions, and ErrSets.

4 files changed, 79 insertions(+), 20 deletions(-)

src/AstGen.zig+21-6
......@@ -3193,7 +3193,7 @@ fn fnDecl(
31933193 // missing function name already happened in scanDecls()
31943194 const fn_name_token = fn_proto.name_token orelse return error.AnalysisFail;
31953195 const fn_name_str_index = try astgen.identAsString(fn_name_token);
3196 const doc_comment_index = try docCommentAsString(astgen, fn_name_token - 1);
3196 const doc_comment_index = try astgen.docCommentAsString(fn_name_token - 1);
31973197
31983198 // We insert this at the beginning so that its instruction index marks the
31993199 // start of the top level declaration.
......@@ -3474,7 +3474,7 @@ fn globalVarDecl(
34743474
34753475 const name_token = var_decl.ast.mut_token + 1;
34763476 const name_str_index = try astgen.identAsString(name_token);
3477 const doc_comment_index = try docCommentAsString(astgen, var_decl.ast.mut_token);
3477 const doc_comment_index = try astgen.docCommentAsString(var_decl.ast.mut_token);
34783478
34793479 var block_scope: GenZir = .{
34803480 .parent = scope,
......@@ -3864,7 +3864,7 @@ fn structDeclInner(
38643864 const field_count = @intCast(u32, container_decl.ast.members.len - decl_count);
38653865
38663866 const bits_per_field = 4;
3867 const max_field_size = 4;
3867 const max_field_size = 5;
38683868 var wip_members = try WipMembers.init(gpa, &astgen.scratch, decl_count, field_count, bits_per_field, max_field_size);
38693869 defer wip_members.deinit();
38703870
......@@ -3888,6 +3888,9 @@ fn structDeclInner(
38883888 try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
38893889 wip_members.appendToField(@enumToInt(field_type));
38903890
3891 const doc_comment_index = try astgen.docCommentAsString(member.ast.name_token);
3892 wip_members.appendToField(doc_comment_index);
3893
38913894 known_has_bits = known_has_bits or nodeImpliesRuntimeBits(tree, member.ast.type_expr);
38923895
38933896 const have_align = member.ast.align_expr != 0;
......@@ -3986,7 +3989,7 @@ fn unionDeclInner(
39863989 .none;
39873990
39883991 const bits_per_field = 4;
3989 const max_field_size = 4;
3992 const max_field_size = 5;
39903993 var wip_members = try WipMembers.init(gpa, &astgen.scratch, decl_count, field_count, bits_per_field, max_field_size);
39913994 defer wip_members.deinit();
39923995
......@@ -4002,6 +4005,9 @@ fn unionDeclInner(
40024005 const field_name = try astgen.identAsString(member.ast.name_token);
40034006 wip_members.appendToField(field_name);
40044007
4008 const doc_comment_index = try astgen.docCommentAsString(member.ast.name_token);
4009 wip_members.appendToField(doc_comment_index);
4010
40054011 const have_type = member.ast.type_expr != 0;
40064012 const have_align = member.ast.align_expr != 0;
40074013 const have_value = member.ast.value_expr != 0;
......@@ -4265,7 +4271,7 @@ fn containerDecl(
42654271 .none;
42664272
42674273 const bits_per_field = 1;
4268 const max_field_size = 2;
4274 const max_field_size = 3;
42694275 var wip_members = try WipMembers.init(gpa, &astgen.scratch, @intCast(u32, counts.decls), @intCast(u32, counts.total_fields), bits_per_field, max_field_size);
42704276 defer wip_members.deinit();
42714277
......@@ -4283,6 +4289,9 @@ fn containerDecl(
42834289 const field_name = try astgen.identAsString(member.ast.name_token);
42844290 wip_members.appendToField(field_name);
42854291
4292 const doc_comment_index = try astgen.docCommentAsString(member.ast.name_token);
4293 wip_members.appendToField(doc_comment_index);
4294
42864295 const have_value = member.ast.value_expr != 0;
42874296 wip_members.nextField(bits_per_field, .{have_value});
42884297
......@@ -4513,8 +4522,14 @@ fn errorSetDecl(gz: *GenZir, rl: ResultLoc, node: Ast.Node.Index) InnerError!Zir
45134522 switch (token_tags[tok_i]) {
45144523 .doc_comment, .comma => {},
45154524 .identifier => {
4525 // TODO: maybe consider not using `docCommentAsString`
4526 // since we're already visiting the first doc comment
4527 // token.
4528 try astgen.extra.ensureUnusedCapacity(gpa, 2);
45164529 const str_index = try astgen.identAsString(tok_i);
4517 try astgen.extra.append(gpa, str_index);
4530 astgen.extra.appendAssumeCapacity(str_index);
4531 const doc_comment_index = try astgen.docCommentAsString(tok_i);
4532 astgen.extra.appendAssumeCapacity(doc_comment_index);
45184533 fields_len += 1;
45194534 },
45204535 .r_brace => break,
src/Sema.zig+15-3
......@@ -1916,6 +1916,9 @@ fn zirEnumDecl(
19161916 const field_name_zir = sema.code.nullTerminatedString(sema.code.extra[extra_index]);
19171917 extra_index += 1;
19181918
1919 // doc comment
1920 extra_index += 1;
1921
19191922 // This string needs to outlive the ZIR code.
19201923 const field_name = try new_decl_arena_allocator.dupe(u8, field_name_zir);
19211924
......@@ -2103,7 +2106,6 @@ fn zirErrorSetDecl(
21032106 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
21042107 const src = inst_data.src();
21052108 const extra = sema.code.extraData(Zir.Inst.ErrorSetDecl, inst_data.payload_index);
2106 const fields = sema.code.extra[extra.end..][0..extra.data.fields_len];
21072109
21082110 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
21092111 errdefer new_decl_arena.deinit();
......@@ -2121,8 +2123,12 @@ fn zirErrorSetDecl(
21212123 errdefer sema.mod.abortAnonDecl(new_decl);
21222124
21232125 var names = Module.ErrorSet.NameMap{};
2124 try names.ensureUnusedCapacity(new_decl_arena_allocator, fields.len);
2125 for (fields) |str_index| {
2126 try names.ensureUnusedCapacity(new_decl_arena_allocator, extra.data.fields_len);
2127
2128 var extra_index = @intCast(u32, extra.end);
2129 const extra_index_end = extra_index + (extra.data.fields_len * 2);
2130 while (extra_index < extra_index_end) : (extra_index += 2) { // +2 to skip over doc_string
2131 const str_index = sema.code.extra[extra_index];
21262132 const name = try new_decl_arena_allocator.dupe(u8, sema.code.nullTerminatedString(str_index));
21272133
21282134 // TODO: This check should be performed in AstGen instead.
......@@ -16313,6 +16319,9 @@ fn semaStructFields(
1631316319 const field_type_ref = @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
1631416320 extra_index += 1;
1631516321
16322 // doc_comment
16323 extra_index += 1;
16324
1631616325 // This string needs to outlive the ZIR code.
1631716326 const field_name = try decl_arena_allocator.dupe(u8, field_name_zir);
1631816327 const field_ty: Type = if (field_type_ref == .none)
......@@ -16502,6 +16511,9 @@ fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void {
1650216511 const field_name_zir = zir.nullTerminatedString(zir.extra[extra_index]);
1650316512 extra_index += 1;
1650416513
16514 // doc_comment
16515 extra_index += 1;
16516
1650516517 const field_type_ref: Zir.Inst.Ref = if (has_type) blk: {
1650616518 const field_type_ref = @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
1650716519 extra_index += 1;
src/Zir.zig+7
......@@ -2571,6 +2571,7 @@ pub const Inst = struct {
25712571 /// - if there is a 0 byte at the position `name` indexes, it indicates
25722572 /// this is a test decl, and the name starts at `name+1`.
25732573 /// value: Index,
2574 /// doc_comment: u32, // 0 if no doc comment
25742575 /// align: Ref, // if corresponding bit is set
25752576 /// link_section_or_address_space: { // if corresponding bit is set.
25762577 /// link_section: Ref,
......@@ -2588,6 +2589,7 @@ pub const Inst = struct {
25882589 /// field_name: u32,
25892590 /// field_type: Ref,
25902591 /// - if none, means `anytype`.
2592 /// doc_comment: u32, // 0 if no doc comment
25912593 /// align: Ref, // if corresponding bit is set
25922594 /// default_value: Ref, // if corresponding bit is set
25932595 /// }
......@@ -2638,6 +2640,7 @@ pub const Inst = struct {
26382640 /// - if there is a 0 byte at the position `name` indexes, it indicates
26392641 /// this is a test decl, and the name starts at `name+1`.
26402642 /// value: Index,
2643 /// doc_comment: u32, // 0 if no doc_comment
26412644 /// align: Ref, // if corresponding bit is set
26422645 /// link_section_or_address_space: { // if corresponding bit is set.
26432646 /// link_section: Ref,
......@@ -2649,6 +2652,7 @@ pub const Inst = struct {
26492652 /// - the bit is whether corresponding field has an value expression
26502653 /// 9. fields: { // for every fields_len
26512654 /// field_name: u32,
2655 /// doc_comment: u32, // 0 if no doc_comment
26522656 /// value: Ref, // if corresponding bit is set
26532657 /// }
26542658 pub const EnumDecl = struct {
......@@ -2686,6 +2690,7 @@ pub const Inst = struct {
26862690 /// - if there is a 0 byte at the position `name` indexes, it indicates
26872691 /// this is a test decl, and the name starts at `name+1`.
26882692 /// value: Index,
2693 /// doc_comment: u32, // 0 if no doc comment
26892694 /// align: Ref, // if corresponding bit is set
26902695 /// link_section_or_address_space: { // if corresponding bit is set.
26912696 /// link_section: Ref,
......@@ -2701,6 +2706,7 @@ pub const Inst = struct {
27012706 /// 0bX000: unused
27022707 /// 9. fields: { // for every fields_len
27032708 /// field_name: u32, // null terminated string index
2709 /// doc_comment: u32, // 0 if no doc comment
27042710 /// field_type: Ref, // if corresponding bit is set
27052711 /// - if none, means `anytype`.
27062712 /// align: Ref, // if corresponding bit is set
......@@ -2745,6 +2751,7 @@ pub const Inst = struct {
27452751 /// - if there is a 0 byte at the position `name` indexes, it indicates
27462752 /// this is a test decl, and the name starts at `name+1`.
27472753 /// value: Index,
2754 /// doc_comment: u32, // 0 if no doc comment,
27482755 /// align: Ref, // if corresponding bit is set
27492756 /// link_section_or_address_space: { // if corresponding bit is set.
27502757 /// link_section: Ref,
src/print_zir.zig+36-11
......@@ -1207,6 +1207,10 @@ const Writer = struct {
12071207 extra_index += 1;
12081208 const field_type = @intToEnum(Zir.Inst.Ref, self.code.extra[extra_index]);
12091209 extra_index += 1;
1210 const doc_comment_index = self.code.extra[extra_index];
1211 extra_index += 1;
1212
1213 try self.writeDocComment(stream, doc_comment_index);
12101214
12111215 try stream.writeByteNTimes(' ', self.indent);
12121216 try self.writeFlag(stream, "comptime ", is_comptime);
......@@ -1332,6 +1336,10 @@ const Writer = struct {
13321336
13331337 const field_name = self.code.nullTerminatedString(self.code.extra[extra_index]);
13341338 extra_index += 1;
1339 const doc_comment_index = self.code.extra[extra_index];
1340 extra_index += 1;
1341
1342 try self.writeDocComment(stream, doc_comment_index);
13351343 try stream.writeByteNTimes(' ', self.indent);
13361344 try stream.print("{}", .{std.zig.fmtId(field_name)});
13371345
......@@ -1419,12 +1427,13 @@ const Writer = struct {
14191427
14201428 const pub_str = if (is_pub) "pub " else "";
14211429 const hash_bytes = @bitCast([16]u8, hash_u32s.*);
1422 try stream.writeByteNTimes(' ', self.indent);
14231430 if (decl_name_index == 0) {
1431 try stream.writeByteNTimes(' ', self.indent);
14241432 const name = if (is_exported) "usingnamespace" else "comptime";
14251433 try stream.writeAll(pub_str);
14261434 try stream.writeAll(name);
14271435 } else if (decl_name_index == 1) {
1436 try stream.writeByteNTimes(' ', self.indent);
14281437 try stream.writeAll("test");
14291438 } else {
14301439 const raw_decl_name = self.code.nullTerminatedString(decl_name_index);
......@@ -1435,15 +1444,9 @@ const Writer = struct {
14351444 const test_str = if (raw_decl_name.len == 0) "test " else "";
14361445 const export_str = if (is_exported) "export " else "";
14371446
1438 if (doc_comment_index != 0) {
1439 const doc_comment = self.code.nullTerminatedString(doc_comment_index);
1440 var it = std.mem.tokenize(u8, doc_comment, "\n");
1441 while (it.next()) |doc_line| {
1442 try stream.print("///{s}\n", .{doc_line});
1443 try stream.writeByteNTimes(' ', self.indent);
1444 }
1445 }
1447 try self.writeDocComment(stream, doc_comment_index);
14461448
1449 try stream.writeByteNTimes(' ', self.indent);
14471450 try stream.print("[{d}] {s}{s}{s}{}", .{
14481451 sub_index, pub_str, test_str, export_str, std.zig.fmtId(decl_name),
14491452 });
......@@ -1569,6 +1572,11 @@ const Writer = struct {
15691572 const field_name = self.code.nullTerminatedString(self.code.extra[extra_index]);
15701573 extra_index += 1;
15711574
1575 const doc_comment_index = self.code.extra[extra_index];
1576 extra_index += 1;
1577
1578 try self.writeDocComment(stream, doc_comment_index);
1579
15721580 try stream.writeByteNTimes(' ', self.indent);
15731581 try stream.print("{}", .{std.zig.fmtId(field_name)});
15741582
......@@ -1632,17 +1640,23 @@ const Writer = struct {
16321640 ) !void {
16331641 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
16341642 const extra = self.code.extraData(Zir.Inst.ErrorSetDecl, inst_data.payload_index);
1635 const fields = self.code.extra[extra.end..][0..extra.data.fields_len];
16361643
16371644 try stream.print("{s}, ", .{@tagName(name_strategy)});
16381645
16391646 try stream.writeAll("{\n");
16401647 self.indent += 2;
1641 for (fields) |str_index| {
1648
1649 var extra_index = @intCast(u32, extra.end);
1650 const extra_index_end = extra_index + (extra.data.fields_len * 2);
1651 while (extra_index < extra_index_end) : (extra_index += 2) {
1652 const str_index = self.code.extra[extra_index];
16421653 const name = self.code.nullTerminatedString(str_index);
1654 const doc_comment_index = self.code.extra[extra_index + 1];
1655 try self.writeDocComment(stream, doc_comment_index);
16431656 try stream.writeByteNTimes(' ', self.indent);
16441657 try stream.print("{},\n", .{std.zig.fmtId(name)});
16451658 }
1659
16461660 self.indent -= 2;
16471661 try stream.writeByteNTimes(' ', self.indent);
16481662 try stream.writeAll("}) ");
......@@ -2134,6 +2148,17 @@ const Writer = struct {
21342148 }
21352149 }
21362150
2151 fn writeDocComment(self: *Writer, stream: anytype, doc_comment_index: u32) !void {
2152 if (doc_comment_index != 0) {
2153 const doc_comment = self.code.nullTerminatedString(doc_comment_index);
2154 var it = std.mem.tokenize(u8, doc_comment, "\n");
2155 while (it.next()) |doc_line| {
2156 try stream.writeByteNTimes(' ', self.indent);
2157 try stream.print("///{s}\n", .{doc_line});
2158 }
2159 }
2160 }
2161
21372162 fn writeBody(self: *Writer, stream: anytype, body: []const Zir.Inst.Index) !void {
21382163 for (body) |inst| {
21392164 try stream.writeByteNTimes(' ', self.indent);