authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-23 18:31:55-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-01-23 18:31:55-05:00
log12c2de6ee205857e9ca0bcef4aa4d901fc5cc772
tree491f15df20182f428a92533e8efc2530c73c9dc2
parentf1b91bb41b2d810ecabf4c69cad91b24b3846b77
parent8a697262099d64b2ff222dc9271493fe3b5f2b76
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10662 from ziglang/doc-comments-zir

Doc comments zir

9 files changed, 212 insertions(+), 23 deletions(-)

lib/std/special/compiler_rt.zig+3-1
......@@ -26,7 +26,9 @@ comptime {
2626 if (builtin.zig_backend == .stage1) {
2727 _ = @import("compiler_rt/atomics.zig");
2828 }
29 _ = @import("compiler_rt/clear_cache.zig").clear_cache;
29 if (builtin.zig_backend != .stage2_llvm) { // TODO
30 _ = @import("compiler_rt/clear_cache.zig").clear_cache;
31 }
3032
3133 const __extenddftf2 = @import("compiler_rt/extendXfYf2.zig").__extenddftf2;
3234 @export(__extenddftf2, .{ .name = "__extenddftf2", .linkage = linkage });
lib/std/zig/Ast.zig+18
......@@ -2120,6 +2120,14 @@ pub const full = struct {
21202120 section_node: Node.Index,
21212121 init_node: Node.Index,
21222122 };
2123
2124 pub fn firstToken(var_decl: VarDecl) TokenIndex {
2125 return var_decl.visib_token orelse
2126 var_decl.extern_export_token orelse
2127 var_decl.threadlocal_token orelse
2128 var_decl.comptime_token orelse
2129 var_decl.ast.mut_token;
2130 }
21232131 };
21242132
21252133 pub const If = struct {
......@@ -2168,6 +2176,10 @@ pub const full = struct {
21682176 value_expr: Node.Index,
21692177 align_expr: Node.Index,
21702178 };
2179
2180 pub fn firstToken(cf: ContainerField) TokenIndex {
2181 return cf.comptime_token orelse cf.ast.name_token;
2182 }
21712183 };
21722184
21732185 pub const FnProto = struct {
......@@ -2197,6 +2209,12 @@ pub const full = struct {
21972209 type_expr: Node.Index,
21982210 };
21992211
2212 pub fn firstToken(fn_proto: FnProto) TokenIndex {
2213 return fn_proto.visib_token orelse
2214 fn_proto.extern_export_inline_token orelse
2215 fn_proto.ast.fn_token;
2216 }
2217
22002218 /// Abstracts over the fact that anytype and ... are not included
22012219 /// in the params slice, since they are simple identifiers and
22022220 /// not sub-expressions.
src/AstGen.zig+104-9
......@@ -1171,7 +1171,7 @@ fn fnProtoExpr(
11711171 const main_tokens = tree.nodes.items(.main_token);
11721172 const name_token = param.name_token orelse main_tokens[param_type_node];
11731173 const tag: Zir.Inst.Tag = if (is_comptime) .param_comptime else .param;
1174 const param_inst = try block_scope.addParam(&param_gz, tag, name_token, param_name);
1174 const param_inst = try block_scope.addParam(&param_gz, tag, name_token, param_name, param.first_doc_comment);
11751175 assert(param_inst_expected == param_inst);
11761176 }
11771177 }
......@@ -3080,9 +3080,9 @@ const WipMembers = struct {
30803080 /// struct, union, enum, and opaque decls all use same 4 bits per decl
30813081 const bits_per_decl = 4;
30823082 const decls_per_u32 = 32 / bits_per_decl;
3083 /// struct, union, enum, and opaque decls all have maximum size of 10 u32 slots
3084 /// (4 for src_hash + line + name + value + align + link_section + address_space)
3085 const max_decl_size = 10;
3083 /// struct, union, enum, and opaque decls all have maximum size of 11 u32 slots
3084 /// (4 for src_hash + line + name + value + doc_comment + align + link_section + address_space )
3085 const max_decl_size = 11;
30863086
30873087 pub fn init(gpa: Allocator, payload: *ArrayListUnmanaged(u32), decl_count: u32, field_count: u32, comptime bits_per_field: u32, comptime max_field_size: u32) Allocator.Error!Self {
30883088 const payload_top = @intCast(u32, payload.items.len);
......@@ -3236,6 +3236,9 @@ fn fnDecl(
32363236 const maybe_inline_token = fn_proto.extern_export_inline_token orelse break :blk false;
32373237 break :blk token_tags[maybe_inline_token] == .keyword_inline;
32383238 };
3239
3240 const doc_comment_index = try astgen.docCommentAsString(fn_proto.firstToken());
3241
32393242 const has_section_or_addrspace = fn_proto.ast.section_expr != 0 or fn_proto.ast.addrspace_expr != 0;
32403243 wip_members.nextDecl(is_pub, is_export, fn_proto.ast.align_expr != 0, has_section_or_addrspace);
32413244
......@@ -3294,7 +3297,7 @@ fn fnDecl(
32943297 const main_tokens = tree.nodes.items(.main_token);
32953298 const name_token = param.name_token orelse main_tokens[param_type_node];
32963299 const tag: Zir.Inst.Tag = if (is_comptime) .param_comptime else .param;
3297 const param_inst = try decl_gz.addParam(&param_gz, tag, name_token, param_name);
3300 const param_inst = try decl_gz.addParam(&param_gz, tag, name_token, param_name, param.first_doc_comment);
32983301 assert(param_inst_expected == param_inst);
32993302 break :param indexToRef(param_inst);
33003303 };
......@@ -3445,6 +3448,7 @@ fn fnDecl(
34453448 }
34463449 wip_members.appendToDecl(fn_name_str_index);
34473450 wip_members.appendToDecl(block_inst);
3451 wip_members.appendToDecl(doc_comment_index);
34483452 if (align_inst != .none) {
34493453 wip_members.appendToDecl(@enumToInt(align_inst));
34503454 }
......@@ -3519,6 +3523,8 @@ fn globalVarDecl(
35193523 break :blk lib_name_str.index;
35203524 } else 0;
35213525
3526 const doc_comment_index = try astgen.docCommentAsString(var_decl.firstToken());
3527
35223528 assert(var_decl.comptime_token == null); // handled by parser
35233529
35243530 const var_inst: Zir.Inst.Ref = if (var_decl.ast.init_node != 0) vi: {
......@@ -3594,6 +3600,7 @@ fn globalVarDecl(
35943600 }
35953601 wip_members.appendToDecl(name_str_index);
35963602 wip_members.appendToDecl(block_inst);
3603 wip_members.appendToDecl(doc_comment_index); // doc_comment wip
35973604 if (align_inst != .none) {
35983605 wip_members.appendToDecl(@enumToInt(align_inst));
35993606 }
......@@ -3648,6 +3655,7 @@ fn comptimeDecl(
36483655 }
36493656 wip_members.appendToDecl(0);
36503657 wip_members.appendToDecl(block_inst);
3658 wip_members.appendToDecl(0); // no doc comments on comptime decls
36513659}
36523660
36533661fn usingnamespaceDecl(
......@@ -3699,6 +3707,7 @@ fn usingnamespaceDecl(
36993707 }
37003708 wip_members.appendToDecl(0);
37013709 wip_members.appendToDecl(block_inst);
3710 wip_members.appendToDecl(0); // no doc comments on usingnamespace decls
37023711}
37033712
37043713fn testDecl(
......@@ -3802,6 +3811,7 @@ fn testDecl(
38023811 }
38033812 wip_members.appendToDecl(test_name);
38043813 wip_members.appendToDecl(block_inst);
3814 wip_members.appendToDecl(0); // no doc comments on test decls
38053815}
38063816
38073817fn structDeclInner(
......@@ -3857,7 +3867,7 @@ fn structDeclInner(
38573867 const field_count = @intCast(u32, container_decl.ast.members.len - decl_count);
38583868
38593869 const bits_per_field = 4;
3860 const max_field_size = 4;
3870 const max_field_size = 5;
38613871 var wip_members = try WipMembers.init(gpa, &astgen.scratch, decl_count, field_count, bits_per_field, max_field_size);
38623872 defer wip_members.deinit();
38633873
......@@ -3881,6 +3891,9 @@ fn structDeclInner(
38813891 try typeExpr(&block_scope, &namespace.base, member.ast.type_expr);
38823892 wip_members.appendToField(@enumToInt(field_type));
38833893
3894 const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
3895 wip_members.appendToField(doc_comment_index);
3896
38843897 known_has_bits = known_has_bits or nodeImpliesRuntimeBits(tree, member.ast.type_expr);
38853898
38863899 const have_align = member.ast.align_expr != 0;
......@@ -3979,7 +3992,7 @@ fn unionDeclInner(
39793992 .none;
39803993
39813994 const bits_per_field = 4;
3982 const max_field_size = 4;
3995 const max_field_size = 5;
39833996 var wip_members = try WipMembers.init(gpa, &astgen.scratch, decl_count, field_count, bits_per_field, max_field_size);
39843997 defer wip_members.deinit();
39853998
......@@ -3995,6 +4008,9 @@ fn unionDeclInner(
39954008 const field_name = try astgen.identAsString(member.ast.name_token);
39964009 wip_members.appendToField(field_name);
39974010
4011 const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
4012 wip_members.appendToField(doc_comment_index);
4013
39984014 const have_type = member.ast.type_expr != 0;
39994015 const have_align = member.ast.align_expr != 0;
40004016 const have_value = member.ast.value_expr != 0;
......@@ -4258,7 +4274,7 @@ fn containerDecl(
42584274 .none;
42594275
42604276 const bits_per_field = 1;
4261 const max_field_size = 2;
4277 const max_field_size = 3;
42624278 var wip_members = try WipMembers.init(gpa, &astgen.scratch, @intCast(u32, counts.decls), @intCast(u32, counts.total_fields), bits_per_field, max_field_size);
42634279 defer wip_members.deinit();
42644280
......@@ -4276,6 +4292,9 @@ fn containerDecl(
42764292 const field_name = try astgen.identAsString(member.ast.name_token);
42774293 wip_members.appendToField(field_name);
42784294
4295 const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
4296 wip_members.appendToField(doc_comment_index);
4297
42794298 const have_value = member.ast.value_expr != 0;
42804299 wip_members.nextField(bits_per_field, .{have_value});
42814300
......@@ -4506,8 +4525,11 @@ fn errorSetDecl(gz: *GenZir, rl: ResultLoc, node: Ast.Node.Index) InnerError!Zir
45064525 switch (token_tags[tok_i]) {
45074526 .doc_comment, .comma => {},
45084527 .identifier => {
4528 try astgen.extra.ensureUnusedCapacity(gpa, 2);
45094529 const str_index = try astgen.identAsString(tok_i);
4510 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);
45114533 fields_len += 1;
45124534 },
45134535 .r_brace => break,
......@@ -8784,6 +8806,72 @@ fn identAsString(astgen: *AstGen, ident_token: Ast.TokenIndex) !u32 {
87848806 }
87858807}
87868808
8809/// Adds a doc comment block to `string_bytes` by walking backwards from `end_token`.
8810/// `end_token` must point at the first token after the last doc coment line.
8811/// Returns 0 if no doc comment is present.
8812fn docCommentAsString(astgen: *AstGen, end_token: Ast.TokenIndex) !u32 {
8813 if (end_token == 0) return @as(u32, 0);
8814
8815 const token_tags = astgen.tree.tokens.items(.tag);
8816
8817 var tok = end_token - 1;
8818 while (token_tags[tok] == .doc_comment) {
8819 if (tok == 0) break;
8820 tok -= 1;
8821 } else {
8822 tok += 1;
8823 }
8824 return docCommentAsStringFromFirst(astgen, end_token, tok);
8825}
8826
8827/// end_token must be > the index of the last doc comment.
8828fn docCommentAsStringFromFirst(
8829 astgen: *AstGen,
8830 end_token: Ast.TokenIndex,
8831 start_token: Ast.TokenIndex,
8832) !u32 {
8833 if (start_token == end_token) return 0;
8834
8835 const gpa = astgen.gpa;
8836 const string_bytes = &astgen.string_bytes;
8837 const str_index = @intCast(u32, string_bytes.items.len);
8838 const token_starts = astgen.tree.tokens.items(.start);
8839 const token_tags = astgen.tree.tokens.items(.tag);
8840
8841 const total_bytes = token_starts[end_token] - token_starts[start_token];
8842 try string_bytes.ensureUnusedCapacity(gpa, total_bytes);
8843
8844 var current_token = start_token;
8845 while (current_token < end_token) : (current_token += 1) {
8846 switch (token_tags[current_token]) {
8847 .doc_comment => {
8848 const tok_bytes = astgen.tree.tokenSlice(current_token)[3..];
8849 string_bytes.appendSliceAssumeCapacity(tok_bytes);
8850 if (current_token != end_token - 1) {
8851 string_bytes.appendAssumeCapacity('\n');
8852 }
8853 },
8854 else => break,
8855 }
8856 }
8857
8858 const key = string_bytes.items[str_index..];
8859 const gop = try astgen.string_table.getOrPutContextAdapted(gpa, @as([]const u8, key), StringIndexAdapter{
8860 .bytes = string_bytes,
8861 }, StringIndexContext{
8862 .bytes = string_bytes,
8863 });
8864
8865 if (gop.found_existing) {
8866 string_bytes.shrinkRetainingCapacity(str_index);
8867 return gop.key_ptr.*;
8868 } else {
8869 gop.key_ptr.* = str_index;
8870 try string_bytes.append(gpa, 0);
8871 return str_index;
8872 }
8873}
8874
87878875const IndexSlice = struct { index: u32, len: u32 };
87888876
87898877fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice {
......@@ -9629,6 +9717,7 @@ const GenZir = struct {
96299717 /// Absolute token index. This function does the conversion to Decl offset.
96309718 abs_tok_index: Ast.TokenIndex,
96319719 name: u32,
9720 first_doc_comment: ?Ast.TokenIndex,
96329721 ) !Zir.Inst.Index {
96339722 const gpa = gz.astgen.gpa;
96349723 const param_body = param_gz.instructionsSlice();
......@@ -9636,8 +9725,14 @@ const GenZir = struct {
96369725 try gz.astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Param).Struct.fields.len +
96379726 param_body.len);
96389727
9728 const doc_comment_index = if (first_doc_comment) |first|
9729 try gz.astgen.docCommentAsStringFromFirst(abs_tok_index, first)
9730 else
9731 0;
9732
96399733 const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Param{
96409734 .name = name,
9735 .doc_comment = doc_comment_index,
96419736 .body_len = @intCast(u32, param_body.len),
96429737 });
96439738 gz.astgen.extra.appendSliceAssumeCapacity(param_body);
src/Module.zig+4-4
......@@ -558,14 +558,14 @@ pub const Decl = struct {
558558 if (!decl.has_align) return .none;
559559 assert(decl.zir_decl_index != 0);
560560 const zir = decl.getFileScope().zir;
561 return @intToEnum(Zir.Inst.Ref, zir.extra[decl.zir_decl_index + 7]);
561 return @intToEnum(Zir.Inst.Ref, zir.extra[decl.zir_decl_index + 8]);
562562 }
563563
564564 pub fn zirLinksectionRef(decl: Decl) Zir.Inst.Ref {
565565 if (!decl.has_linksection_or_addrspace) return .none;
566566 assert(decl.zir_decl_index != 0);
567567 const zir = decl.getFileScope().zir;
568 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align);
568 const extra_index = decl.zir_decl_index + 8 + @boolToInt(decl.has_align);
569569 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
570570 }
571571
......@@ -573,7 +573,7 @@ pub const Decl = struct {
573573 if (!decl.has_linksection_or_addrspace) return .none;
574574 assert(decl.zir_decl_index != 0);
575575 const zir = decl.getFileScope().zir;
576 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align) + 1;
576 const extra_index = decl.zir_decl_index + 8 + @boolToInt(decl.has_align) + 1;
577577 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
578578 }
579579
......@@ -4011,7 +4011,7 @@ pub fn scanNamespace(
40114011 cur_bit_bag >>= 4;
40124012
40134013 const decl_sub_index = extra_index;
4014 extra_index += 7; // src_hash(4) + line(1) + name(1) + value(1)
4014 extra_index += 8; // src_hash(4) + line(1) + name(1) + value(1) + doc_comment(1)
40154015 extra_index += @truncate(u1, flags >> 2); // Align
40164016 extra_index += @as(u2, @truncate(u1, flags >> 3)) * 2; // Link section or address space, consists of 2 Refs
40174017
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+15-2
......@@ -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,
......@@ -2760,7 +2767,11 @@ pub const Inst = struct {
27602767 };
27612768 };
27622769
2763 /// Trailing: field_name: u32 // for every field: null terminated string index
2770 /// Trailing:
2771 /// { // for every fields_len
2772 /// field_name: u32 // null terminated string index
2773 /// doc_comment: u32 // null terminated string index
2774 /// }
27642775 pub const ErrorSetDecl = struct {
27652776 fields_len: u32,
27662777 };
......@@ -2899,6 +2910,8 @@ pub const Inst = struct {
28992910 pub const Param = struct {
29002911 /// Null-terminated string index.
29012912 name: u32,
2913 /// 0 if no doc comment
2914 doc_comment: u32,
29022915 /// The body contains the type of the parameter.
29032916 body_len: u32,
29042917 };
......@@ -3001,7 +3014,7 @@ pub const DeclIterator = struct {
30013014 const sub_index = @intCast(u32, it.extra_index);
30023015 it.extra_index += 5; // src_hash(4) + line(1)
30033016 const name = it.zir.nullTerminatedString(it.zir.extra[it.extra_index]);
3004 it.extra_index += 2; // name(1) + value(1)
3017 it.extra_index += 3; // name(1) + value(1) + doc_comment(1)
30053018 it.extra_index += @truncate(u1, flags >> 2);
30063019 it.extra_index += @truncate(u1, flags >> 3);
30073020
src/print_zir.zig+47-3
......@@ -785,6 +785,12 @@ const Writer = struct {
785785 try stream.print("\"{}\", ", .{
786786 std.zig.fmtEscapes(self.code.nullTerminatedString(extra.data.name)),
787787 });
788
789 if (extra.data.doc_comment != 0) {
790 try stream.writeAll("\n");
791 try self.writeDocComment(stream, extra.data.doc_comment);
792 try stream.writeByteNTimes(' ', self.indent);
793 }
788794 try self.writeBracedBody(stream, body);
789795 try stream.writeAll(") ");
790796 try self.writeSrc(stream, inst_data.src());
......@@ -1207,6 +1213,10 @@ const Writer = struct {
12071213 extra_index += 1;
12081214 const field_type = @intToEnum(Zir.Inst.Ref, self.code.extra[extra_index]);
12091215 extra_index += 1;
1216 const doc_comment_index = self.code.extra[extra_index];
1217 extra_index += 1;
1218
1219 try self.writeDocComment(stream, doc_comment_index);
12101220
12111221 try stream.writeByteNTimes(' ', self.indent);
12121222 try self.writeFlag(stream, "comptime ", is_comptime);
......@@ -1332,6 +1342,10 @@ const Writer = struct {
13321342
13331343 const field_name = self.code.nullTerminatedString(self.code.extra[extra_index]);
13341344 extra_index += 1;
1345 const doc_comment_index = self.code.extra[extra_index];
1346 extra_index += 1;
1347
1348 try self.writeDocComment(stream, doc_comment_index);
13351349 try stream.writeByteNTimes(' ', self.indent);
13361350 try stream.print("{}", .{std.zig.fmtId(field_name)});
13371351
......@@ -1398,6 +1412,9 @@ const Writer = struct {
13981412 extra_index += 1;
13991413 const decl_index = self.code.extra[extra_index];
14001414 extra_index += 1;
1415 const doc_comment_index = self.code.extra[extra_index];
1416 extra_index += 1;
1417
14011418 const align_inst: Zir.Inst.Ref = if (!has_align) .none else inst: {
14021419 const inst = @intToEnum(Zir.Inst.Ref, self.code.extra[extra_index]);
14031420 extra_index += 1;
......@@ -1416,12 +1433,13 @@ const Writer = struct {
14161433
14171434 const pub_str = if (is_pub) "pub " else "";
14181435 const hash_bytes = @bitCast([16]u8, hash_u32s.*);
1419 try stream.writeByteNTimes(' ', self.indent);
14201436 if (decl_name_index == 0) {
1437 try stream.writeByteNTimes(' ', self.indent);
14211438 const name = if (is_exported) "usingnamespace" else "comptime";
14221439 try stream.writeAll(pub_str);
14231440 try stream.writeAll(name);
14241441 } else if (decl_name_index == 1) {
1442 try stream.writeByteNTimes(' ', self.indent);
14251443 try stream.writeAll("test");
14261444 } else {
14271445 const raw_decl_name = self.code.nullTerminatedString(decl_name_index);
......@@ -1431,6 +1449,10 @@ const Writer = struct {
14311449 raw_decl_name;
14321450 const test_str = if (raw_decl_name.len == 0) "test " else "";
14331451 const export_str = if (is_exported) "export " else "";
1452
1453 try self.writeDocComment(stream, doc_comment_index);
1454
1455 try stream.writeByteNTimes(' ', self.indent);
14341456 try stream.print("[{d}] {s}{s}{s}{}", .{
14351457 sub_index, pub_str, test_str, export_str, std.zig.fmtId(decl_name),
14361458 });
......@@ -1556,6 +1578,11 @@ const Writer = struct {
15561578 const field_name = self.code.nullTerminatedString(self.code.extra[extra_index]);
15571579 extra_index += 1;
15581580
1581 const doc_comment_index = self.code.extra[extra_index];
1582 extra_index += 1;
1583
1584 try self.writeDocComment(stream, doc_comment_index);
1585
15591586 try stream.writeByteNTimes(' ', self.indent);
15601587 try stream.print("{}", .{std.zig.fmtId(field_name)});
15611588
......@@ -1619,17 +1646,23 @@ const Writer = struct {
16191646 ) !void {
16201647 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
16211648 const extra = self.code.extraData(Zir.Inst.ErrorSetDecl, inst_data.payload_index);
1622 const fields = self.code.extra[extra.end..][0..extra.data.fields_len];
16231649
16241650 try stream.print("{s}, ", .{@tagName(name_strategy)});
16251651
16261652 try stream.writeAll("{\n");
16271653 self.indent += 2;
1628 for (fields) |str_index| {
1654
1655 var extra_index = @intCast(u32, extra.end);
1656 const extra_index_end = extra_index + (extra.data.fields_len * 2);
1657 while (extra_index < extra_index_end) : (extra_index += 2) {
1658 const str_index = self.code.extra[extra_index];
16291659 const name = self.code.nullTerminatedString(str_index);
1660 const doc_comment_index = self.code.extra[extra_index + 1];
1661 try self.writeDocComment(stream, doc_comment_index);
16301662 try stream.writeByteNTimes(' ', self.indent);
16311663 try stream.print("{},\n", .{std.zig.fmtId(name)});
16321664 }
1665
16331666 self.indent -= 2;
16341667 try stream.writeByteNTimes(' ', self.indent);
16351668 try stream.writeAll("}) ");
......@@ -2121,6 +2154,17 @@ const Writer = struct {
21212154 }
21222155 }
21232156
2157 fn writeDocComment(self: *Writer, stream: anytype, doc_comment_index: u32) !void {
2158 if (doc_comment_index != 0) {
2159 const doc_comment = self.code.nullTerminatedString(doc_comment_index);
2160 var it = std.mem.tokenize(u8, doc_comment, "\n");
2161 while (it.next()) |doc_line| {
2162 try stream.writeByteNTimes(' ', self.indent);
2163 try stream.print("///{s}\n", .{doc_line});
2164 }
2165 }
2166 }
2167
21242168 fn writeBody(self: *Writer, stream: anytype, body: []const Zir.Inst.Index) !void {
21252169 for (body) |inst| {
21262170 try stream.writeByteNTimes(' ', self.indent);
test/behavior/cast_llvm.zig+3-1
......@@ -1,8 +1,9 @@
1const builtin = @import("builtin");
12const std = @import("std");
23const expect = std.testing.expect;
34const mem = std.mem;
45const maxInt = std.math.maxInt;
5const native_endian = @import("builtin").target.cpu.arch.endian();
6const native_endian = builtin.target.cpu.arch.endian();
67
78test "pointer reinterpret const float to int" {
89 // The hex representation is 0x3fe3333333333303.
......@@ -46,6 +47,7 @@ fn incrementVoidPtrArray(array: ?*anyopaque, len: usize) void {
4647}
4748
4849test "compile time int to ptr of function" {
50 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) return error.SkipZigTest; // TODO
4951 try foobar(FUNCTION_CONSTANT);
5052}
5153
test/behavior/inttoptr.zig+3
......@@ -1,4 +1,7 @@
1const builtin = @import("builtin");
2
13test "casting random address to function pointer" {
4 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) return error.SkipZigTest; // TODO
25 randomAddressToFunction();
36 comptime randomAddressToFunction();
47}