authorgravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2023-05-05 02:41:23+02:00
committergravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2023-05-05 02:41:23+02:00
logc63338712e0267678d400e58b61853954d0e40e4
treea92f2e71de4a2d1eb81d68c1a872c9b306005ad3
parente363ffad903b0f66283fe25de98f3e181d371fc7

autodoc: Save switches as comptimeExpr with the code instead of analyzing it deeply;

simplified getBlockSource;

1 files changed, 27 insertions(+), 44 deletions(-)

src/Autodoc.zig+27-44
...@@ -1876,12 +1876,14 @@ fn walkInstruction(...@@ -1876,12 +1876,14 @@ fn walkInstruction(
1876 // WIP1876 // WIP
1877 const pl_node = data[inst_index].pl_node;1877 const pl_node = data[inst_index].pl_node;
1878 const extra = file.zir.extraData(Zir.Inst.SwitchBlock, pl_node.payload_index);1878 const extra = file.zir.extraData(Zir.Inst.SwitchBlock, pl_node.payload_index);
1879 const cond_index = self.exprs.items.len;
18801879
1881 _ = try self.walkRef(file, parent_scope, parent_src, extra.data.operand, false);1880 const switch_cond = try self.walkRef(file, parent_scope, parent_src, extra.data.operand, false);
1881 const cond_index = self.exprs.items.len;
1882 try self.exprs.append(self.arena, switch_cond.expr);
1883 _ = cond_index;
18821884
1883 const ast_index = self.ast_nodes.items.len;1885 // const ast_index = self.ast_nodes.items.len;
1884 const type_index = self.types.items.len - 1;1886 // const type_index = self.types.items.len - 1;
18851887
1886 // const ast_line = self.ast_nodes.items[ast_index - 1];1888 // const ast_line = self.ast_nodes.items[ast_index - 1];
18871889
...@@ -1894,12 +1896,18 @@ fn walkInstruction(...@@ -1894,12 +1896,18 @@ fn walkInstruction(
1894 // log.debug("{s}", .{sep});1896 // log.debug("{s}", .{sep});
18951897
1896 const switch_index = self.exprs.items.len;1898 const switch_index = self.exprs.items.len;
1897 try self.exprs.append(self.arena, .{ .switchOp = .{1899
1898 .cond_index = cond_index,1900 // const src_loc = try self.srcLocInfo(file, pl_node.src_node, parent_src);
1899 .file_name = file.sub_file_path,1901
1900 .src = ast_index,1902 const switch_expr = try self.getBlockSource(file, parent_src, pl_node.src_node);
1901 .outer_decl = type_index,1903 try self.exprs.append(self.arena, .{ .comptimeExpr = self.comptime_exprs.items.len });
1902 } });1904 try self.comptime_exprs.append(self.arena, .{ .code = switch_expr });
1905 // try self.exprs.append(self.arena, .{ .switchOp = .{
1906 // .cond_index = cond_index,
1907 // .file_name = file.sub_file_path,
1908 // .src = ast_index,
1909 // .outer_decl = type_index,
1910 // } });
19031911
1904 return DocData.WalkResult{1912 return DocData.WalkResult{
1905 .typeRef = .{ .type = @enumToInt(Ref.type_type) },1913 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
...@@ -2157,7 +2165,8 @@ fn walkInstruction(...@@ -2157,7 +2165,8 @@ fn walkInstruction(
2157 .typeRef = .{ .type = @enumToInt(Ref.type_type) },2165 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
2158 .expr = .{ .comptimeExpr = self.comptime_exprs.items.len },2166 .expr = .{ .comptimeExpr = self.comptime_exprs.items.len },
2159 };2167 };
2160 const block_expr = try self.getBlockSource(file, parent_src, inst_index);2168 const pl_node = data[inst_index].pl_node;
2169 const block_expr = try self.getBlockSource(file, parent_src, pl_node.src_node);
2161 try self.comptime_exprs.append(self.arena, .{2170 try self.comptime_exprs.append(self.arena, .{
2162 .code = block_expr,2171 .code = block_expr,
2163 });2172 });
...@@ -2173,7 +2182,8 @@ fn walkInstruction(...@@ -2173,7 +2182,8 @@ fn walkInstruction(
2173 .typeRef = .{ .type = @enumToInt(Ref.type_type) },2182 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
2174 .expr = .{ .comptimeExpr = self.comptime_exprs.items.len },2183 .expr = .{ .comptimeExpr = self.comptime_exprs.items.len },
2175 };2184 };
2176 const block_inline_expr = try self.getBlockSource(file, parent_src, inst_index);2185 const pl_node = data[inst_index].pl_node;
2186 const block_inline_expr = try self.getBlockSource(file, parent_src, pl_node.src_node);
2177 try self.comptime_exprs.append(self.arena, .{2187 try self.comptime_exprs.append(self.arena, .{
2178 .code = block_inline_expr,2188 .code = block_inline_expr,
2179 });2189 });
...@@ -3341,12 +3351,11 @@ fn analyzeDecltest(...@@ -3341,12 +3351,11 @@ fn analyzeDecltest(
3341 const value_index = file.zir.extra[d.sub_index + 6];3351 const value_index = file.zir.extra[d.sub_index + 6];
3342 const decl_name_index = file.zir.extra[d.sub_index + 7];3352 const decl_name_index = file.zir.extra[d.sub_index + 7];
33433353
3344 // This is known to work because decl values are always block_inlines
3345 const test_source_code = try self.getBlockSource(file, parent_src, value_index);
3346
3347 const value_pl_node = data[value_index].pl_node;3354 const value_pl_node = data[value_index].pl_node;
3348 const decl_src = try self.srcLocInfo(file, value_pl_node.src_node, parent_src);3355 const decl_src = try self.srcLocInfo(file, value_pl_node.src_node, parent_src);
33493356
3357 const test_source_code = try self.getBlockSource(file, parent_src, value_pl_node.src_node);
3358
3350 const decl_name: ?[]const u8 = if (decl_name_index != 0)3359 const decl_name: ?[]const u8 = if (decl_name_index != 0)
3351 file.zir.nullTerminatedString(decl_name_index)3360 file.zir.nullTerminatedString(decl_name_index)
3352 else3361 else
...@@ -4796,37 +4805,11 @@ fn getBlockSource(...@@ -4796,37 +4805,11 @@ fn getBlockSource(
4796 self: Autodoc,4805 self: Autodoc,
4797 file: *File,4806 file: *File,
4798 parent_src: SrcLocInfo,4807 parent_src: SrcLocInfo,
4799 value_index: usize,4808 block_src_node: i32,
4800) AutodocErrors![]const u8 {4809) AutodocErrors![]const u8 {
4801 const data = file.zir.instructions.items(.data);
4802 const tags = file.zir.instructions.items(.tag);
4803 const tree = try file.getTree(self.comp_module.gpa);4810 const tree = try file.getTree(self.comp_module.gpa);
48044811 const block_src = try self.srcLocInfo(file, block_src_node, parent_src);
4805 // This is known to work because decl values are always block_inlines4812 return tree.getNodeSource(block_src.src_node);
4806 const block_pl_node = data[value_index].pl_node;
4807 const block_src = try self.srcLocInfo(file, block_pl_node.src_node, parent_src);
4808
4809 const blk_index = idx: {
4810 const pl_node = data[value_index].pl_node;
4811 const extra = file.zir.extraData(Zir.Inst.Block, pl_node.payload_index);
4812 const end_index = file.zir.extra[extra.end..][extra.data.body_len - 1];
4813
4814 if (tags[end_index] == .break_inline) {
4815 break :idx @as(usize, Zir.refToIndex(data[end_index].@"break".operand).?);
4816 } else {
4817 break :idx null;
4818 }
4819 };
4820
4821 const blk_source = src: {
4822 if (blk_index) |idx| {
4823 const pl_node = data[idx].pl_node;
4824 break :src try self.srcLocInfo(file, pl_node.src_node, block_src);
4825 } else {
4826 break :src block_src;
4827 }
4828 };
4829 return tree.getNodeSource(blk_source.src_node);
4830}4813}
48314814
4832fn getTLDocComment(self: *Autodoc, file: *File) ![]const u8 {4815fn getTLDocComment(self: *Autodoc, file: *File) ![]const u8 {