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");
55const Zir = @import("Zir.zig");
66
77module: *Module,
8doc_location: Compilation.EmitLoc,
9
10pub fn init(m: *Module, dl: Compilation.EmitLoc) Autodoc {
8doc_location: ?Compilation.EmitLoc,
9arena: std.mem.Allocator,
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);
1117 return .{
12 .doc_location = dl,
1318 .module = m,
19 .doc_location = doc_location,
20 .arena = arena_allocator.allocator(),
1421 };
1522}
1623
17pub fn generateZirData(self: Autodoc) !void {
18 const gpa = self.module.gpa;
19 std.debug.print("yay, you called me!\n", .{});
20 if (self.doc_location.directory) |dir| {
21 if (dir.path) |path| {
22 std.debug.print("path: {s}\n", .{path});
24pub fn generateZirData(self: *Autodoc) !void {
25 if (self.doc_location) |loc| {
26 if (loc.directory) |dir| {
27 if (dir.path) |path| {
28 std.debug.print("path: {s}\n", .{path});
29 }
2330 }
2431 }
2532 std.debug.print("basename: {s}\n", .{self.doc_location.basename});
......@@ -31,28 +38,21 @@ pub fn generateZirData(self: Autodoc) !void {
3138 else
3239 std.os.getcwd(&buf) catch unreachable;
3340 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 });
35 defer gpa.free(abs_root_path);
41 const abs_root_path = try std.fs.path.join(self.arena, &.{ dir, root_file_path });
42 defer self.arena.free(abs_root_path);
3643 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
4245 // 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 });
4847 // append all the types in Zir.Inst.Ref
4948 {
50 // we don't count .none
49
50 // TODO: we don't want to add .none, but the index math has to check out
5151 var i: u32 = 1;
5252 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);
5454 try Zir.Inst.Ref.typed_value_map[i].val.format("", .{}, tmpbuf.writer());
55 try types.append(.{
55 try self.types.append(self.arena, .{
5656 .kind = 0,
5757 .name = tmpbuf.toOwnedSlice(),
5858 });
......@@ -60,14 +60,14 @@ pub fn generateZirData(self: Autodoc) !void {
6060 }
6161
6262 var root_scope: Scope = .{ .parent = null };
63 try ast_nodes.append(.{ .name = "(root)" });
64 const main_type_index = try walkInstruction(zir, gpa, &root_scope, &types, &decls, &ast_nodes, Zir.main_struct_inst);
63 try self.ast_nodes.append(self.arena, .{ .name = "(root)" });
64 const main_type_index = try self.walkInstruction(zir, &root_scope, Zir.main_struct_inst);
6565
6666 var data = DocData{
6767 .files = &[1][]const u8{root_file_path},
68 .types = types.items,
69 .decls = decls.items,
70 .astNodes = ast_nodes.items,
68 .types = self.types.items,
69 .decls = self.decls.items,
70 .astNodes = self.ast_nodes.items,
7171 };
7272
7373 data.packages[0].main = main_type_index.type;
......@@ -122,11 +122,11 @@ const Scope = struct {
122122
123123 pub fn insertDeclRef(
124124 self: *Scope,
125 gpa: std.mem.Allocator,
125 arena: std.mem.Allocator,
126126 decl_name_index: u32, // decl name
127127 decls_slot_index: usize,
128128 ) !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);
130130 }
131131};
132132
......@@ -221,19 +221,16 @@ const DocData = struct {
221221};
222222
223223fn walkInstruction(
224 self: *Autodoc,
224225 zir: Zir,
225 gpa: std.mem.Allocator,
226226 parent_scope: *Scope,
227 types: *std.ArrayList(DocData.Type),
228 decls: *std.ArrayList(DocData.Decl),
229 ast_nodes: *std.ArrayList(DocData.AstNode),
230227 inst_index: usize,
231228) error{OutOfMemory}!DocData.WalkResult {
232229 const tags = zir.instructions.items(.tag);
233230 const data = zir.instructions.items(.data);
234231
235232 // 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
238235 switch (tags[inst_index]) {
239236 else => {
......@@ -252,13 +249,13 @@ fn walkInstruction(
252249 const int_type = data[inst_index].int_type;
253250 const sign = if (int_type.signedness == .unsigned) "u" else "i";
254251 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, .{
258255 .kind = @enumToInt(std.builtin.TypeId.Int),
259256 .name = name,
260257 });
261 return DocData.WalkResult{ .type = types.items.len - 1 };
258 return DocData.WalkResult{ .type = self.types.items.len - 1 };
262259 },
263260 .block_inline => {
264261 const pl_node = data[inst_index].pl_node;
......@@ -273,7 +270,7 @@ fn walkInstruction(
273270
274271 const break_operand = data[break_index].@"break".operand;
275272 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)
277274 else if (@enumToInt(break_operand) <= @enumToInt(Zir.Inst.Ref.anyerror_void_error_union_type))
278275 // we append all the types in ref first, so we can just do this if we encounter a ref that is a type
279276 return DocData.WalkResult{ .type = @enumToInt(break_operand) }
......@@ -322,58 +319,50 @@ fn walkInstruction(
322319 break :blk decls_len;
323320 } else 0;
324321
325 var decl_indexes = std.ArrayList(usize).init(gpa);
326 var priv_decl_indexes = std.ArrayList(usize).init(gpa);
322 var decl_indexes: std.ArrayListUnmanaged(usize) = .{};
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;
329326 // Decl name lookahead for reserving slots in `scope` (and `decls`).
330327 // Done to make sure that all decl refs can be resolved correctly,
331328 // even if we haven't fully analyzed the decl yet.
332329 {
333330 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);
335332 var decls_slot_index = decls_first_index;
336333 while (it.next()) |d| : (decls_slot_index += 1) {
337334 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);
339336 }
340337 }
341338
342 extra_index = try walkDecls(
339 extra_index = try self.walkDecls(
343340 zir,
344 gpa,
345341 &scope,
346 decls,
347342 decls_first_index,
348343 decls_len,
349344 &decl_indexes,
350345 &priv_decl_indexes,
351 types,
352 ast_nodes,
353346 extra_index,
354347 );
355348
356349 // const body = zir.extra[extra_index..][0..body_len];
357350 extra_index += body_len;
358351
359 var field_type_indexes = std.ArrayList(DocData.WalkResult).init(gpa);
360 var field_name_indexes = std.ArrayList(usize).init(gpa);
361 try collectFieldInfo(
352 var field_type_indexes: std.ArrayListUnmanaged(DocData.WalkResult) = .{};
353 var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
354 try self.collectFieldInfo(
362355 zir,
363 gpa,
364356 &scope,
365 types,
366 decls,
367357 fields_len,
368358 &field_type_indexes,
369359 &field_name_indexes,
370 ast_nodes,
371360 extra_index,
372361 );
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, .{
377366 .kind = @enumToInt(std.builtin.TypeId.Struct),
378367 .name = "todo_name",
379368 .src = self_ast_node_index,
......@@ -382,7 +371,7 @@ fn walkInstruction(
382371 .fields = field_type_indexes.items,
383372 });
384373
385 return DocData.WalkResult{ .type = types.items.len - 1 };
374 return DocData.WalkResult{ .type = self.types.items.len - 1 };
386375 },
387376 }
388377 },
......@@ -390,16 +379,13 @@ fn walkInstruction(
390379}
391380
392381fn walkDecls(
382 self: *Autodoc,
393383 zir: Zir,
394 gpa: std.mem.Allocator,
395384 scope: *Scope,
396 decls: *std.ArrayList(DocData.Decl),
397385 decls_first_index: usize,
398386 decls_len: u32,
399 decl_indexes: *std.ArrayList(usize),
400 priv_decl_indexes: *std.ArrayList(usize),
401 types: *std.ArrayList(DocData.Type),
402 ast_nodes: *std.ArrayList(DocData.AstNode),
387 decl_indexes: *std.ArrayListUnmanaged(usize),
388 priv_decl_indexes: *std.ArrayListUnmanaged(usize),
403389 extra_start: usize,
404390) error{OutOfMemory}!usize {
405391 const bit_bags_count = std.math.divCeil(usize, decls_len, 8) catch unreachable;
......@@ -478,8 +464,8 @@ fn walkDecls(
478464
479465 // astnode
480466 const ast_node_index = idx: {
481 const idx = ast_nodes.items.len;
482 try ast_nodes.append(.{
467 const idx = self.ast_nodes.items.len;
468 try self.ast_nodes.append(self.arena, .{
483469 .file = 0,
484470 .line = 0,
485471 .col = 0,
......@@ -489,16 +475,16 @@ fn walkDecls(
489475 break :idx idx;
490476 };
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);
493479 const type_index = walk_result.type;
494480
495481 if (is_pub) {
496 try decl_indexes.append(decls_slot_index);
482 try decl_indexes.append(self.arena, decls_slot_index);
497483 } else {
498 try priv_decl_indexes.append(decls_slot_index);
484 try priv_decl_indexes.append(self.arena, decls_slot_index);
499485 }
500486
501 decls.items[decls_slot_index] = .{
487 self.decls.items[decls_slot_index] = .{
502488 .name = name,
503489 .src = ast_node_index,
504490 .type = 0,
......@@ -511,15 +497,12 @@ fn walkDecls(
511497}
512498
513499fn collectFieldInfo(
500 self: *Autodoc,
514501 zir: Zir,
515 gpa: std.mem.Allocator,
516502 scope: *Scope,
517 types: *std.ArrayList(DocData.Type),
518 decls: *std.ArrayList(DocData.Decl),
519503 fields_len: usize,
520 field_type_indexes: *std.ArrayList(DocData.WalkResult),
521 field_name_indexes: *std.ArrayList(usize),
522 ast_nodes: *std.ArrayList(DocData.AstNode),
504 field_type_indexes: *std.ArrayListUnmanaged(DocData.WalkResult),
505 field_name_indexes: *std.ArrayListUnmanaged(usize),
523506 ei: usize,
524507) !void {
525508 if (fields_len == 0) return;
......@@ -559,15 +542,17 @@ fn collectFieldInfo(
559542 {
560543 switch (field_type) {
561544 .void_type => {
562 try field_type_indexes.append(.{ .type = types.items.len });
563 try types.append(.{
545 try field_type_indexes.append(self.arena, .{
546 .type = self.types.items.len,
547 });
548 try self.types.append(self.arena, .{
564549 .kind = @enumToInt(std.builtin.TypeId.Void),
565550 .name = "void",
566551 });
567552 },
568553 .usize_type => {
569 try field_type_indexes.append(.{ .type = types.items.len });
570 try types.append(.{
554 try field_type_indexes.append(self.arena, .{ .type = self.types.items.len });
555 try self.types.append(self.arena, .{
571556 .kind = @enumToInt(std.builtin.TypeId.Int),
572557 .name = "usize",
573558 });
......@@ -580,19 +565,18 @@ fn collectFieldInfo(
580565 "TODO: handle ref type: {s}",
581566 .{@tagName(field_type)},
582567 );
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 );
584572 } else {
585573 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(
587575 zir,
588 gpa,
589576 scope,
590 types,
591 decls,
592 ast_nodes,
593577 zir_index,
594578 );
595 try field_type_indexes.append(walk_result);
579 try field_type_indexes.append(self.arena, walk_result);
596580 }
597581 },
598582 }
......@@ -600,12 +584,12 @@ fn collectFieldInfo(
600584
601585 // ast node
602586 {
603 try field_name_indexes.append(ast_nodes.items.len);
587 try field_name_indexes.append(self.arena, self.ast_nodes.items.len);
604588 const doc_comment: ?[]const u8 = if (doc_comment_index != 0)
605589 zir.nullTerminatedString(doc_comment_index)
606590 else
607591 null;
608 try ast_nodes.append(.{
592 try self.ast_nodes.append(self.arena, .{
609593 .name = field_name,
610594 .docs = doc_comment,
611595 });