authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-01-29 19:34:59+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:10-07:00
log07ed8886b54ad09631d05cc835aa98bc81cb6ab4
tree4135418a51193f6b99ea94a5a10cb33d9964216e
parent9ad9664ea86897891696b35cc3f45affb06944f1

autodocs: refactoring: moved some fn arguments inside of self


1 files changed, 74 insertions(+), 90 deletions(-)

src/Autodoc.zig+74-90
...@@ -5,21 +5,28 @@ const Module = @import("Module.zig");...@@ -5,21 +5,28 @@ const Module = @import("Module.zig");
5const Zir = @import("Zir.zig");5const Zir = @import("Zir.zig");
66
7module: *Module,7module: *Module,
8doc_location: Compilation.EmitLoc,8doc_location: ?Compilation.EmitLoc,
99arena: std.mem.Allocator,
10pub fn init(m: *Module, dl: Compilation.EmitLoc) Autodoc {10types: std.ArrayListUnmanaged(DocData.Type) = .{},
11decls: std.ArrayListUnmanaged(DocData.Decl) = .{},
12ast_nodes: std.ArrayListUnmanaged(DocData.AstNode) = .{},
13
14var arena_allocator: std.heap.ArenaAllocator = undefined;
15pub fn init(m: *Module, doc_location: ?Compilation.EmitLoc) Autodoc {
16 arena_allocator = std.heap.ArenaAllocator.init(m.gpa);
11 return .{17 return .{
12 .doc_location = dl,
13 .module = m,18 .module = m,
19 .doc_location = doc_location,
20 .arena = arena_allocator.allocator(),
14 };21 };
15}22}
1623
17pub fn generateZirData(self: Autodoc) !void {24pub fn generateZirData(self: *Autodoc) !void {
18 const gpa = self.module.gpa;25 if (self.doc_location) |loc| {
19 std.debug.print("yay, you called me!\n", .{});26 if (loc.directory) |dir| {
20 if (self.doc_location.directory) |dir| {27 if (dir.path) |path| {
21 if (dir.path) |path| {28 std.debug.print("path: {s}\n", .{path});
22 std.debug.print("path: {s}\n", .{path});29 }
23 }30 }
24 }31 }
25 std.debug.print("basename: {s}\n", .{self.doc_location.basename});32 std.debug.print("basename: {s}\n", .{self.doc_location.basename});
...@@ -31,28 +38,21 @@ pub fn generateZirData(self: Autodoc) !void {...@@ -31,28 +38,21 @@ pub fn generateZirData(self: Autodoc) !void {
31 else38 else
32 std.os.getcwd(&buf) catch unreachable;39 std.os.getcwd(&buf) catch unreachable;
33 const root_file_path = self.module.main_pkg.root_src_path;40 const root_file_path = self.module.main_pkg.root_src_path;
34 const abs_root_path = try std.fs.path.join(gpa, &.{ dir, root_file_path });41 const abs_root_path = try std.fs.path.join(self.arena, &.{ dir, root_file_path });
35 defer gpa.free(abs_root_path);42 defer self.arena.free(abs_root_path);
36 const zir = self.module.import_table.get(abs_root_path).?.zir;43 const zir = self.module.import_table.get(abs_root_path).?.zir;
3744
38 var types = std.ArrayList(DocData.Type).init(gpa);
39 var decls = std.ArrayList(DocData.Decl).init(gpa);
40 var ast_nodes = std.ArrayList(DocData.AstNode).init(gpa);
41
42 // var decl_map = std.AutoHashMap(Zir.Inst.Index, usize); // values are positions in the `decls` array45 // var decl_map = std.AutoHashMap(Zir.Inst.Index, usize); // values are positions in the `decls` array
4346
44 try types.append(.{
45 .kind = 0,
46 .name = "type",
47 });
48 // append all the types in Zir.Inst.Ref47 // append all the types in Zir.Inst.Ref
49 {48 {
50 // we don't count .none49
50 // TODO: we don't want to add .none, but the index math has to check out
51 var i: u32 = 1;51 var i: u32 = 1;
52 while (i <= @enumToInt(Zir.Inst.Ref.anyerror_void_error_union_type)) : (i += 1) {52 while (i <= @enumToInt(Zir.Inst.Ref.anyerror_void_error_union_type)) : (i += 1) {
53 var tmpbuf = std.ArrayList(u8).init(gpa);53 var tmpbuf = std.ArrayList(u8).init(self.arena);
54 try Zir.Inst.Ref.typed_value_map[i].val.format("", .{}, tmpbuf.writer());54 try Zir.Inst.Ref.typed_value_map[i].val.format("", .{}, tmpbuf.writer());
55 try types.append(.{55 try self.types.append(self.arena, .{
56 .kind = 0,56 .kind = 0,
57 .name = tmpbuf.toOwnedSlice(),57 .name = tmpbuf.toOwnedSlice(),
58 });58 });
...@@ -60,14 +60,14 @@ pub fn generateZirData(self: Autodoc) !void {...@@ -60,14 +60,14 @@ pub fn generateZirData(self: Autodoc) !void {
60 }60 }
6161
62 var root_scope: Scope = .{ .parent = null };62 var root_scope: Scope = .{ .parent = null };
63 try ast_nodes.append(.{ .name = "(root)" });63 try self.ast_nodes.append(self.arena, .{ .name = "(root)" });
64 const main_type_index = try walkInstruction(zir, gpa, &root_scope, &types, &decls, &ast_nodes, Zir.main_struct_inst);64 const main_type_index = try self.walkInstruction(zir, &root_scope, Zir.main_struct_inst);
6565
66 var data = DocData{66 var data = DocData{
67 .files = &[1][]const u8{root_file_path},67 .files = &[1][]const u8{root_file_path},
68 .types = types.items,68 .types = self.types.items,
69 .decls = decls.items,69 .decls = self.decls.items,
70 .astNodes = ast_nodes.items,70 .astNodes = self.ast_nodes.items,
71 };71 };
7272
73 data.packages[0].main = main_type_index.type;73 data.packages[0].main = main_type_index.type;
...@@ -122,11 +122,11 @@ const Scope = struct {...@@ -122,11 +122,11 @@ const Scope = struct {
122122
123 pub fn insertDeclRef(123 pub fn insertDeclRef(
124 self: *Scope,124 self: *Scope,
125 gpa: std.mem.Allocator,125 arena: std.mem.Allocator,
126 decl_name_index: u32, // decl name126 decl_name_index: u32, // decl name
127 decls_slot_index: usize,127 decls_slot_index: usize,
128 ) !void {128 ) !void {
129 try self.map.put(gpa, decl_name_index, decls_slot_index);129 try self.map.put(arena, decl_name_index, decls_slot_index);
130 }130 }
131};131};
132132
...@@ -221,19 +221,16 @@ const DocData = struct {...@@ -221,19 +221,16 @@ const DocData = struct {
221};221};
222222
223fn walkInstruction(223fn walkInstruction(
224 self: *Autodoc,
224 zir: Zir,225 zir: Zir,
225 gpa: std.mem.Allocator,
226 parent_scope: *Scope,226 parent_scope: *Scope,
227 types: *std.ArrayList(DocData.Type),
228 decls: *std.ArrayList(DocData.Decl),
229 ast_nodes: *std.ArrayList(DocData.AstNode),
230 inst_index: usize,227 inst_index: usize,
231) error{OutOfMemory}!DocData.WalkResult {228) error{OutOfMemory}!DocData.WalkResult {
232 const tags = zir.instructions.items(.tag);229 const tags = zir.instructions.items(.tag);
233 const data = zir.instructions.items(.data);230 const data = zir.instructions.items(.data);
234231
235 // We assume that the topmost ast_node entry corresponds to our decl232 // We assume that the topmost ast_node entry corresponds to our decl
236 const self_ast_node_index = ast_nodes.items.len - 1;233 const self_ast_node_index = self.ast_nodes.items.len - 1;
237234
238 switch (tags[inst_index]) {235 switch (tags[inst_index]) {
239 else => {236 else => {
...@@ -252,13 +249,13 @@ fn walkInstruction(...@@ -252,13 +249,13 @@ fn walkInstruction(
252 const int_type = data[inst_index].int_type;249 const int_type = data[inst_index].int_type;
253 const sign = if (int_type.signedness == .unsigned) "u" else "i";250 const sign = if (int_type.signedness == .unsigned) "u" else "i";
254 const bits = int_type.bit_count;251 const bits = int_type.bit_count;
255 const name = try std.fmt.allocPrint(gpa, "{s}{}", .{ sign, bits });252 const name = try std.fmt.allocPrint(self.arena, "{s}{}", .{ sign, bits });
256253
257 try types.append(.{254 try self.types.append(self.arena, .{
258 .kind = @enumToInt(std.builtin.TypeId.Int),255 .kind = @enumToInt(std.builtin.TypeId.Int),
259 .name = name,256 .name = name,
260 });257 });
261 return DocData.WalkResult{ .type = types.items.len - 1 };258 return DocData.WalkResult{ .type = self.types.items.len - 1 };
262 },259 },
263 .block_inline => {260 .block_inline => {
264 const pl_node = data[inst_index].pl_node;261 const pl_node = data[inst_index].pl_node;
...@@ -273,7 +270,7 @@ fn walkInstruction(...@@ -273,7 +270,7 @@ fn walkInstruction(
273270
274 const break_operand = data[break_index].@"break".operand;271 const break_operand = data[break_index].@"break".operand;
275 return if (Zir.refToIndex(break_operand)) |bi|272 return if (Zir.refToIndex(break_operand)) |bi|
276 walkInstruction(zir, gpa, parent_scope, types, decls, ast_nodes, bi)273 self.walkInstruction(zir, parent_scope, bi)
277 else if (@enumToInt(break_operand) <= @enumToInt(Zir.Inst.Ref.anyerror_void_error_union_type))274 else if (@enumToInt(break_operand) <= @enumToInt(Zir.Inst.Ref.anyerror_void_error_union_type))
278 // we append all the types in ref first, so we can just do this if we encounter a ref that is a type275 // we append all the types in ref first, so we can just do this if we encounter a ref that is a type
279 return DocData.WalkResult{ .type = @enumToInt(break_operand) }276 return DocData.WalkResult{ .type = @enumToInt(break_operand) }
...@@ -322,58 +319,50 @@ fn walkInstruction(...@@ -322,58 +319,50 @@ fn walkInstruction(
322 break :blk decls_len;319 break :blk decls_len;
323 } else 0;320 } else 0;
324321
325 var decl_indexes = std.ArrayList(usize).init(gpa);322 var decl_indexes: std.ArrayListUnmanaged(usize) = .{};
326 var priv_decl_indexes = std.ArrayList(usize).init(gpa);323 var priv_decl_indexes: std.ArrayListUnmanaged(usize) = .{};
327324
328 const decls_first_index = decls.items.len;325 const decls_first_index = self.decls.items.len;
329 // Decl name lookahead for reserving slots in `scope` (and `decls`).326 // Decl name lookahead for reserving slots in `scope` (and `decls`).
330 // Done to make sure that all decl refs can be resolved correctly,327 // Done to make sure that all decl refs can be resolved correctly,
331 // even if we haven't fully analyzed the decl yet.328 // even if we haven't fully analyzed the decl yet.
332 {329 {
333 var it = zir.declIterator(@intCast(u32, inst_index));330 var it = zir.declIterator(@intCast(u32, inst_index));
334 try decls.resize(decls_first_index + it.decls_len);331 try self.decls.resize(self.arena, decls_first_index + it.decls_len);
335 var decls_slot_index = decls_first_index;332 var decls_slot_index = decls_first_index;
336 while (it.next()) |d| : (decls_slot_index += 1) {333 while (it.next()) |d| : (decls_slot_index += 1) {
337 const decl_name_index = zir.extra[d.sub_index + 5];334 const decl_name_index = zir.extra[d.sub_index + 5];
338 try scope.insertDeclRef(gpa, decl_name_index, decls_slot_index);335 try scope.insertDeclRef(self.arena, decl_name_index, decls_slot_index);
339 }336 }
340 }337 }
341338
342 extra_index = try walkDecls(339 extra_index = try self.walkDecls(
343 zir,340 zir,
344 gpa,
345 &scope,341 &scope,
346 decls,
347 decls_first_index,342 decls_first_index,
348 decls_len,343 decls_len,
349 &decl_indexes,344 &decl_indexes,
350 &priv_decl_indexes,345 &priv_decl_indexes,
351 types,
352 ast_nodes,
353 extra_index,346 extra_index,
354 );347 );
355348
356 // const body = zir.extra[extra_index..][0..body_len];349 // const body = zir.extra[extra_index..][0..body_len];
357 extra_index += body_len;350 extra_index += body_len;
358351
359 var field_type_indexes = std.ArrayList(DocData.WalkResult).init(gpa);352 var field_type_indexes: std.ArrayListUnmanaged(DocData.WalkResult) = .{};
360 var field_name_indexes = std.ArrayList(usize).init(gpa);353 var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
361 try collectFieldInfo(354 try self.collectFieldInfo(
362 zir,355 zir,
363 gpa,
364 &scope,356 &scope,
365 types,
366 decls,
367 fields_len,357 fields_len,
368 &field_type_indexes,358 &field_type_indexes,
369 &field_name_indexes,359 &field_name_indexes,
370 ast_nodes,
371 extra_index,360 extra_index,
372 );361 );
373362
374 ast_nodes.items[self_ast_node_index].fields = field_name_indexes.items;363 self.ast_nodes.items[self_ast_node_index].fields = field_name_indexes.items;
375364
376 try types.append(.{365 try self.types.append(self.arena, .{
377 .kind = @enumToInt(std.builtin.TypeId.Struct),366 .kind = @enumToInt(std.builtin.TypeId.Struct),
378 .name = "todo_name",367 .name = "todo_name",
379 .src = self_ast_node_index,368 .src = self_ast_node_index,
...@@ -382,7 +371,7 @@ fn walkInstruction(...@@ -382,7 +371,7 @@ fn walkInstruction(
382 .fields = field_type_indexes.items,371 .fields = field_type_indexes.items,
383 });372 });
384373
385 return DocData.WalkResult{ .type = types.items.len - 1 };374 return DocData.WalkResult{ .type = self.types.items.len - 1 };
386 },375 },
387 }376 }
388 },377 },
...@@ -390,16 +379,13 @@ fn walkInstruction(...@@ -390,16 +379,13 @@ fn walkInstruction(
390}379}
391380
392fn walkDecls(381fn walkDecls(
382 self: *Autodoc,
393 zir: Zir,383 zir: Zir,
394 gpa: std.mem.Allocator,
395 scope: *Scope,384 scope: *Scope,
396 decls: *std.ArrayList(DocData.Decl),
397 decls_first_index: usize,385 decls_first_index: usize,
398 decls_len: u32,386 decls_len: u32,
399 decl_indexes: *std.ArrayList(usize),387 decl_indexes: *std.ArrayListUnmanaged(usize),
400 priv_decl_indexes: *std.ArrayList(usize),388 priv_decl_indexes: *std.ArrayListUnmanaged(usize),
401 types: *std.ArrayList(DocData.Type),
402 ast_nodes: *std.ArrayList(DocData.AstNode),
403 extra_start: usize,389 extra_start: usize,
404) error{OutOfMemory}!usize {390) error{OutOfMemory}!usize {
405 const bit_bags_count = std.math.divCeil(usize, decls_len, 8) catch unreachable;391 const bit_bags_count = std.math.divCeil(usize, decls_len, 8) catch unreachable;
...@@ -478,8 +464,8 @@ fn walkDecls(...@@ -478,8 +464,8 @@ fn walkDecls(
478464
479 // astnode465 // astnode
480 const ast_node_index = idx: {466 const ast_node_index = idx: {
481 const idx = ast_nodes.items.len;467 const idx = self.ast_nodes.items.len;
482 try ast_nodes.append(.{468 try self.ast_nodes.append(self.arena, .{
483 .file = 0,469 .file = 0,
484 .line = 0,470 .line = 0,
485 .col = 0,471 .col = 0,
...@@ -489,16 +475,16 @@ fn walkDecls(...@@ -489,16 +475,16 @@ fn walkDecls(
489 break :idx idx;475 break :idx idx;
490 };476 };
491477
492 const walk_result = try walkInstruction(zir, gpa, scope, types, decls, ast_nodes, decl_index);478 const walk_result = try self.walkInstruction(zir, scope, decl_index);
493 const type_index = walk_result.type;479 const type_index = walk_result.type;
494480
495 if (is_pub) {481 if (is_pub) {
496 try decl_indexes.append(decls_slot_index);482 try decl_indexes.append(self.arena, decls_slot_index);
497 } else {483 } else {
498 try priv_decl_indexes.append(decls_slot_index);484 try priv_decl_indexes.append(self.arena, decls_slot_index);
499 }485 }
500486
501 decls.items[decls_slot_index] = .{487 self.decls.items[decls_slot_index] = .{
502 .name = name,488 .name = name,
503 .src = ast_node_index,489 .src = ast_node_index,
504 .type = 0,490 .type = 0,
...@@ -511,15 +497,12 @@ fn walkDecls(...@@ -511,15 +497,12 @@ fn walkDecls(
511}497}
512498
513fn collectFieldInfo(499fn collectFieldInfo(
500 self: *Autodoc,
514 zir: Zir,501 zir: Zir,
515 gpa: std.mem.Allocator,
516 scope: *Scope,502 scope: *Scope,
517 types: *std.ArrayList(DocData.Type),
518 decls: *std.ArrayList(DocData.Decl),
519 fields_len: usize,503 fields_len: usize,
520 field_type_indexes: *std.ArrayList(DocData.WalkResult),504 field_type_indexes: *std.ArrayListUnmanaged(DocData.WalkResult),
521 field_name_indexes: *std.ArrayList(usize),505 field_name_indexes: *std.ArrayListUnmanaged(usize),
522 ast_nodes: *std.ArrayList(DocData.AstNode),
523 ei: usize,506 ei: usize,
524) !void {507) !void {
525 if (fields_len == 0) return;508 if (fields_len == 0) return;
...@@ -559,15 +542,17 @@ fn collectFieldInfo(...@@ -559,15 +542,17 @@ fn collectFieldInfo(
559 {542 {
560 switch (field_type) {543 switch (field_type) {
561 .void_type => {544 .void_type => {
562 try field_type_indexes.append(.{ .type = types.items.len });545 try field_type_indexes.append(self.arena, .{
563 try types.append(.{546 .type = self.types.items.len,
547 });
548 try self.types.append(self.arena, .{
564 .kind = @enumToInt(std.builtin.TypeId.Void),549 .kind = @enumToInt(std.builtin.TypeId.Void),
565 .name = "void",550 .name = "void",
566 });551 });
567 },552 },
568 .usize_type => {553 .usize_type => {
569 try field_type_indexes.append(.{ .type = types.items.len });554 try field_type_indexes.append(self.arena, .{ .type = self.types.items.len });
570 try types.append(.{555 try self.types.append(self.arena, .{
571 .kind = @enumToInt(std.builtin.TypeId.Int),556 .kind = @enumToInt(std.builtin.TypeId.Int),
572 .name = "usize",557 .name = "usize",
573 });558 });
...@@ -580,19 +565,18 @@ fn collectFieldInfo(...@@ -580,19 +565,18 @@ fn collectFieldInfo(
580 "TODO: handle ref type: {s}",565 "TODO: handle ref type: {s}",
581 .{@tagName(field_type)},566 .{@tagName(field_type)},
582 );567 );
583 try field_type_indexes.append(DocData.WalkResult{ .failure = true });568 try field_type_indexes.append(
569 self.arena,
570 DocData.WalkResult{ .failure = true },
571 );
584 } else {572 } else {
585 const zir_index = enum_value - Zir.Inst.Ref.typed_value_map.len;573 const zir_index = enum_value - Zir.Inst.Ref.typed_value_map.len;
586 const walk_result = try walkInstruction(574 const walk_result = try self.walkInstruction(
587 zir,575 zir,
588 gpa,
589 scope,576 scope,
590 types,
591 decls,
592 ast_nodes,
593 zir_index,577 zir_index,
594 );578 );
595 try field_type_indexes.append(walk_result);579 try field_type_indexes.append(self.arena, walk_result);
596 }580 }
597 },581 },
598 }582 }
...@@ -600,12 +584,12 @@ fn collectFieldInfo(...@@ -600,12 +584,12 @@ fn collectFieldInfo(
600584
601 // ast node585 // ast node
602 {586 {
603 try field_name_indexes.append(ast_nodes.items.len);587 try field_name_indexes.append(self.arena, self.ast_nodes.items.len);
604 const doc_comment: ?[]const u8 = if (doc_comment_index != 0)588 const doc_comment: ?[]const u8 = if (doc_comment_index != 0)
605 zir.nullTerminatedString(doc_comment_index)589 zir.nullTerminatedString(doc_comment_index)
606 else590 else
607 null;591 null;
608 try ast_nodes.append(.{592 try self.ast_nodes.append(self.arena, .{
609 .name = field_name,593 .name = field_name,
610 .docs = doc_comment,594 .docs = doc_comment,
611 });595 });