| ... | ... | @@ -5,21 +5,28 @@ const Module = @import("Module.zig"); |
| 5 | 5 | const Zir = @import("Zir.zig"); |
| 6 | 6 | |
| 7 | 7 | module: *Module, |
| 8 | | doc_location: Compilation.EmitLoc, |
| 9 | | |
| 10 | | pub fn init(m: *Module, dl: Compilation.EmitLoc) Autodoc { |
| 8 | doc_location: ?Compilation.EmitLoc, |
| 9 | arena: std.mem.Allocator, |
| 10 | types: std.ArrayListUnmanaged(DocData.Type) = .{}, |
| 11 | decls: std.ArrayListUnmanaged(DocData.Decl) = .{}, |
| 12 | ast_nodes: std.ArrayListUnmanaged(DocData.AstNode) = .{}, |
| 13 | |
| 14 | var arena_allocator: std.heap.ArenaAllocator = undefined; |
| 15 | pub fn init(m: *Module, doc_location: ?Compilation.EmitLoc) Autodoc { |
| 16 | arena_allocator = std.heap.ArenaAllocator.init(m.gpa); |
| 11 | 17 | return .{ |
| 12 | | .doc_location = dl, |
| 13 | 18 | .module = m, |
| 19 | .doc_location = doc_location, |
| 20 | .arena = arena_allocator.allocator(), |
| 14 | 21 | }; |
| 15 | 22 | } |
| 16 | 23 | |
| 17 | | pub 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}); |
| 24 | pub 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 | } |
| 23 | 30 | } |
| 24 | 31 | } |
| 25 | 32 | std.debug.print("basename: {s}\n", .{self.doc_location.basename}); |
| ... | ... | @@ -31,28 +38,21 @@ pub fn generateZirData(self: Autodoc) !void { |
| 31 | 38 | else |
| 32 | 39 | std.os.getcwd(&buf) catch unreachable; |
| 33 | 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 }); |
| 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); |
| 36 | 43 | const zir = self.module.import_table.get(abs_root_path).?.zir; |
| 37 | 44 | |
| 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 | 45 | // var decl_map = std.AutoHashMap(Zir.Inst.Index, usize); // values are positions in the `decls` array |
| 43 | 46 | |
| 44 | | try types.append(.{ |
| 45 | | .kind = 0, |
| 46 | | .name = "type", |
| 47 | | }); |
| 48 | 47 | // append all the types in Zir.Inst.Ref |
| 49 | 48 | { |
| 50 | | // we don't count .none |
| 49 | |
| 50 | // TODO: we don't want to add .none, but the index math has to check out |
| 51 | 51 | var i: u32 = 1; |
| 52 | 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 | 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 | 56 | .kind = 0, |
| 57 | 57 | .name = tmpbuf.toOwnedSlice(), |
| 58 | 58 | }); |
| ... | ... | @@ -60,14 +60,14 @@ pub fn generateZirData(self: Autodoc) !void { |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | 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); |
| 65 | 65 | |
| 66 | 66 | var data = DocData{ |
| 67 | 67 | .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, |
| 71 | 71 | }; |
| 72 | 72 | |
| 73 | 73 | data.packages[0].main = main_type_index.type; |
| ... | ... | @@ -122,11 +122,11 @@ const Scope = struct { |
| 122 | 122 | |
| 123 | 123 | pub fn insertDeclRef( |
| 124 | 124 | self: *Scope, |
| 125 | | gpa: std.mem.Allocator, |
| 125 | arena: std.mem.Allocator, |
| 126 | 126 | decl_name_index: u32, // decl name |
| 127 | 127 | decls_slot_index: usize, |
| 128 | 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 | }; |
| 132 | 132 | |
| ... | ... | @@ -221,19 +221,16 @@ const DocData = struct { |
| 221 | 221 | }; |
| 222 | 222 | |
| 223 | 223 | fn walkInstruction( |
| 224 | self: *Autodoc, |
| 224 | 225 | zir: Zir, |
| 225 | | gpa: std.mem.Allocator, |
| 226 | 226 | parent_scope: *Scope, |
| 227 | | types: *std.ArrayList(DocData.Type), |
| 228 | | decls: *std.ArrayList(DocData.Decl), |
| 229 | | ast_nodes: *std.ArrayList(DocData.AstNode), |
| 230 | 227 | inst_index: usize, |
| 231 | 228 | ) error{OutOfMemory}!DocData.WalkResult { |
| 232 | 229 | const tags = zir.instructions.items(.tag); |
| 233 | 230 | const data = zir.instructions.items(.data); |
| 234 | 231 | |
| 235 | 232 | // 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; |
| 237 | 234 | |
| 238 | 235 | switch (tags[inst_index]) { |
| 239 | 236 | else => { |
| ... | ... | @@ -252,13 +249,13 @@ fn walkInstruction( |
| 252 | 249 | const int_type = data[inst_index].int_type; |
| 253 | 250 | const sign = if (int_type.signedness == .unsigned) "u" else "i"; |
| 254 | 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 }); |
| 256 | 253 | |
| 257 | | try types.append(.{ |
| 254 | try self.types.append(self.arena, .{ |
| 258 | 255 | .kind = @enumToInt(std.builtin.TypeId.Int), |
| 259 | 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 | 260 | .block_inline => { |
| 264 | 261 | const pl_node = data[inst_index].pl_node; |
| ... | ... | @@ -273,7 +270,7 @@ fn walkInstruction( |
| 273 | 270 | |
| 274 | 271 | const break_operand = data[break_index].@"break".operand; |
| 275 | 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 | 274 | else if (@enumToInt(break_operand) <= @enumToInt(Zir.Inst.Ref.anyerror_void_error_union_type)) |
| 278 | 275 | // we append all the types in ref first, so we can just do this if we encounter a ref that is a type |
| 279 | 276 | return DocData.WalkResult{ .type = @enumToInt(break_operand) } |
| ... | ... | @@ -322,58 +319,50 @@ fn walkInstruction( |
| 322 | 319 | break :blk decls_len; |
| 323 | 320 | } else 0; |
| 324 | 321 | |
| 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) = .{}; |
| 327 | 324 | |
| 328 | | const decls_first_index = decls.items.len; |
| 325 | const decls_first_index = self.decls.items.len; |
| 329 | 326 | // Decl name lookahead for reserving slots in `scope` (and `decls`). |
| 330 | 327 | // Done to make sure that all decl refs can be resolved correctly, |
| 331 | 328 | // even if we haven't fully analyzed the decl yet. |
| 332 | 329 | { |
| 333 | 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 | 332 | var decls_slot_index = decls_first_index; |
| 336 | 333 | while (it.next()) |d| : (decls_slot_index += 1) { |
| 337 | 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 | } |
| 341 | 338 | |
| 342 | | extra_index = try walkDecls( |
| 339 | extra_index = try self.walkDecls( |
| 343 | 340 | zir, |
| 344 | | gpa, |
| 345 | 341 | &scope, |
| 346 | | decls, |
| 347 | 342 | decls_first_index, |
| 348 | 343 | decls_len, |
| 349 | 344 | &decl_indexes, |
| 350 | 345 | &priv_decl_indexes, |
| 351 | | types, |
| 352 | | ast_nodes, |
| 353 | 346 | extra_index, |
| 354 | 347 | ); |
| 355 | 348 | |
| 356 | 349 | // const body = zir.extra[extra_index..][0..body_len]; |
| 357 | 350 | extra_index += body_len; |
| 358 | 351 | |
| 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( |
| 362 | 355 | zir, |
| 363 | | gpa, |
| 364 | 356 | &scope, |
| 365 | | types, |
| 366 | | decls, |
| 367 | 357 | fields_len, |
| 368 | 358 | &field_type_indexes, |
| 369 | 359 | &field_name_indexes, |
| 370 | | ast_nodes, |
| 371 | 360 | extra_index, |
| 372 | 361 | ); |
| 373 | 362 | |
| 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; |
| 375 | 364 | |
| 376 | | try types.append(.{ |
| 365 | try self.types.append(self.arena, .{ |
| 377 | 366 | .kind = @enumToInt(std.builtin.TypeId.Struct), |
| 378 | 367 | .name = "todo_name", |
| 379 | 368 | .src = self_ast_node_index, |
| ... | ... | @@ -382,7 +371,7 @@ fn walkInstruction( |
| 382 | 371 | .fields = field_type_indexes.items, |
| 383 | 372 | }); |
| 384 | 373 | |
| 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 | 379 | } |
| 391 | 380 | |
| 392 | 381 | fn walkDecls( |
| 382 | self: *Autodoc, |
| 393 | 383 | zir: Zir, |
| 394 | | gpa: std.mem.Allocator, |
| 395 | 384 | scope: *Scope, |
| 396 | | decls: *std.ArrayList(DocData.Decl), |
| 397 | 385 | decls_first_index: usize, |
| 398 | 386 | 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), |
| 403 | 389 | extra_start: usize, |
| 404 | 390 | ) error{OutOfMemory}!usize { |
| 405 | 391 | const bit_bags_count = std.math.divCeil(usize, decls_len, 8) catch unreachable; |
| ... | ... | @@ -478,8 +464,8 @@ fn walkDecls( |
| 478 | 464 | |
| 479 | 465 | // astnode |
| 480 | 466 | 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, .{ |
| 483 | 469 | .file = 0, |
| 484 | 470 | .line = 0, |
| 485 | 471 | .col = 0, |
| ... | ... | @@ -489,16 +475,16 @@ fn walkDecls( |
| 489 | 475 | break :idx idx; |
| 490 | 476 | }; |
| 491 | 477 | |
| 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 | 479 | const type_index = walk_result.type; |
| 494 | 480 | |
| 495 | 481 | if (is_pub) { |
| 496 | | try decl_indexes.append(decls_slot_index); |
| 482 | try decl_indexes.append(self.arena, decls_slot_index); |
| 497 | 483 | } else { |
| 498 | | try priv_decl_indexes.append(decls_slot_index); |
| 484 | try priv_decl_indexes.append(self.arena, decls_slot_index); |
| 499 | 485 | } |
| 500 | 486 | |
| 501 | | decls.items[decls_slot_index] = .{ |
| 487 | self.decls.items[decls_slot_index] = .{ |
| 502 | 488 | .name = name, |
| 503 | 489 | .src = ast_node_index, |
| 504 | 490 | .type = 0, |
| ... | ... | @@ -511,15 +497,12 @@ fn walkDecls( |
| 511 | 497 | } |
| 512 | 498 | |
| 513 | 499 | fn collectFieldInfo( |
| 500 | self: *Autodoc, |
| 514 | 501 | zir: Zir, |
| 515 | | gpa: std.mem.Allocator, |
| 516 | 502 | scope: *Scope, |
| 517 | | types: *std.ArrayList(DocData.Type), |
| 518 | | decls: *std.ArrayList(DocData.Decl), |
| 519 | 503 | 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), |
| 523 | 506 | ei: usize, |
| 524 | 507 | ) !void { |
| 525 | 508 | if (fields_len == 0) return; |
| ... | ... | @@ -559,15 +542,17 @@ fn collectFieldInfo( |
| 559 | 542 | { |
| 560 | 543 | switch (field_type) { |
| 561 | 544 | .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, .{ |
| 564 | 549 | .kind = @enumToInt(std.builtin.TypeId.Void), |
| 565 | 550 | .name = "void", |
| 566 | 551 | }); |
| 567 | 552 | }, |
| 568 | 553 | .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, .{ |
| 571 | 556 | .kind = @enumToInt(std.builtin.TypeId.Int), |
| 572 | 557 | .name = "usize", |
| 573 | 558 | }); |
| ... | ... | @@ -580,19 +565,18 @@ fn collectFieldInfo( |
| 580 | 565 | "TODO: handle ref type: {s}", |
| 581 | 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 | 572 | } else { |
| 585 | 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 | 575 | zir, |
| 588 | | gpa, |
| 589 | 576 | scope, |
| 590 | | types, |
| 591 | | decls, |
| 592 | | ast_nodes, |
| 593 | 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 | 584 | |
| 601 | 585 | // 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 | 588 | const doc_comment: ?[]const u8 = if (doc_comment_index != 0) |
| 605 | 589 | zir.nullTerminatedString(doc_comment_index) |
| 606 | 590 | else |
| 607 | 591 | null; |
| 608 | | try ast_nodes.append(.{ |
| 592 | try self.ast_nodes.append(self.arena, .{ |
| 609 | 593 | .name = field_name, |
| 610 | 594 | .docs = doc_comment, |
| 611 | 595 | }); |