| ... | ... | @@ -307,7 +307,7 @@ pub const Scope = struct { |
| 307 | 307 | /// Relative to the owning package's root_src_dir. |
| 308 | 308 | /// Reference to external memory, not owned by ZIRModule. |
| 309 | 309 | sub_file_path: []const u8, |
| 310 | | source: union { |
| 310 | source: union(enum) { |
| 311 | 311 | unloaded: void, |
| 312 | 312 | bytes: [:0]const u8, |
| 313 | 313 | }, |
| ... | ... | @@ -320,7 +320,7 @@ pub const Scope = struct { |
| 320 | 320 | unloaded_success, |
| 321 | 321 | unloaded_parse_failure, |
| 322 | 322 | unloaded_sema_failure, |
| 323 | | loaded_parse_failure, |
| 323 | |
| 324 | 324 | loaded_sema_failure, |
| 325 | 325 | loaded_success, |
| 326 | 326 | }, |
| ... | ... | @@ -334,21 +334,22 @@ pub const Scope = struct { |
| 334 | 334 | => {}, |
| 335 | 335 | |
| 336 | 336 | .loaded_success => { |
| 337 | | allocator.free(self.source.bytes); |
| 338 | 337 | self.contents.module.deinit(allocator); |
| 339 | 338 | allocator.destroy(self.contents.module); |
| 340 | 339 | self.status = .unloaded_success; |
| 341 | 340 | }, |
| 342 | 341 | .loaded_sema_failure => { |
| 343 | | allocator.free(self.source.bytes); |
| 344 | 342 | self.contents.module.deinit(allocator); |
| 345 | 343 | allocator.destroy(self.contents.module); |
| 346 | 344 | self.status = .unloaded_sema_failure; |
| 347 | 345 | }, |
| 348 | | .loaded_parse_failure => { |
| 349 | | allocator.free(self.source.bytes); |
| 350 | | self.status = .unloaded_parse_failure; |
| 346 | } |
| 347 | switch (self.source) { |
| 348 | .bytes => |bytes| { |
| 349 | allocator.free(bytes); |
| 350 | self.source = .{ .unloaded = {} }; |
| 351 | 351 | }, |
| 352 | .unloaded => {}, |
| 352 | 353 | } |
| 353 | 354 | } |
| 354 | 355 | |
| ... | ... | @@ -586,7 +587,7 @@ pub fn getAllErrorsAlloc(self: *Module) !AllErrors { |
| 586 | 587 | while (it.next()) |kv| { |
| 587 | 588 | const scope = kv.key; |
| 588 | 589 | const err_msg = kv.value; |
| 589 | | const source = scope.source.bytes; |
| 590 | const source = try self.getSource(scope); |
| 590 | 591 | try AllErrors.add(&arena, &errors, scope.sub_file_path, source, err_msg.*); |
| 591 | 592 | } |
| 592 | 593 | } |
| ... | ... | @@ -595,7 +596,7 @@ pub fn getAllErrorsAlloc(self: *Module) !AllErrors { |
| 595 | 596 | while (it.next()) |kv| { |
| 596 | 597 | const decl = kv.key; |
| 597 | 598 | const err_msg = kv.value; |
| 598 | | const source = decl.scope.source.bytes; |
| 599 | const source = try self.getSource(decl.scope); |
| 599 | 600 | try AllErrors.add(&arena, &errors, decl.scope.sub_file_path, source, err_msg.*); |
| 600 | 601 | } |
| 601 | 602 | } |
| ... | ... | @@ -604,7 +605,7 @@ pub fn getAllErrorsAlloc(self: *Module) !AllErrors { |
| 604 | 605 | while (it.next()) |kv| { |
| 605 | 606 | const decl = kv.key.owner_decl; |
| 606 | 607 | const err_msg = kv.value; |
| 607 | | const source = decl.scope.source.bytes; |
| 608 | const source = try self.getSource(decl.scope); |
| 608 | 609 | try AllErrors.add(&arena, &errors, decl.scope.sub_file_path, source, err_msg.*); |
| 609 | 610 | } |
| 610 | 611 | } |
| ... | ... | @@ -684,20 +685,29 @@ pub fn performAllTheWork(self: *Module) error{OutOfMemory}!void { |
| 684 | 685 | }; |
| 685 | 686 | } |
| 686 | 687 | |
| 687 | | fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module { |
| 688 | | switch (root_scope.status) { |
| 689 | | .never_loaded, .unloaded_success => { |
| 690 | | try self.failed_files.ensureCapacity(self.failed_files.size + 1); |
| 691 | | |
| 692 | | var keep_source = false; |
| 688 | fn getSource(self: *Module, root_scope: *Scope.ZIRModule) ![:0]const u8 { |
| 689 | switch (root_scope.source) { |
| 690 | .unloaded => { |
| 693 | 691 | const source = try self.root_pkg.root_src_dir.readFileAllocOptions( |
| 694 | 692 | self.allocator, |
| 695 | | self.root_pkg.root_src_path, |
| 693 | root_scope.sub_file_path, |
| 696 | 694 | std.math.maxInt(u32), |
| 697 | 695 | 1, |
| 698 | 696 | 0, |
| 699 | 697 | ); |
| 700 | | defer if (!keep_source) self.allocator.free(source); |
| 698 | root_scope.source = .{ .bytes = source }; |
| 699 | return source; |
| 700 | }, |
| 701 | .bytes => |bytes| return bytes, |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module { |
| 706 | switch (root_scope.status) { |
| 707 | .never_loaded, .unloaded_success => { |
| 708 | try self.failed_files.ensureCapacity(self.failed_files.size + 1); |
| 709 | |
| 710 | const source = try self.getSource(root_scope); |
| 701 | 711 | |
| 702 | 712 | var keep_zir_module = false; |
| 703 | 713 | const zir_module = try self.allocator.create(zir.Module); |
| ... | ... | @@ -711,15 +721,11 @@ fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module { |
| 711 | 721 | root_scope, |
| 712 | 722 | try ErrorMsg.create(self.allocator, src_err_msg.byte_offset, "{}", .{src_err_msg.msg}), |
| 713 | 723 | ); |
| 714 | | root_scope.status = .loaded_parse_failure; |
| 715 | | root_scope.source = .{ .bytes = source }; |
| 716 | | keep_source = true; |
| 724 | root_scope.status = .unloaded_parse_failure; |
| 717 | 725 | return error.AnalysisFail; |
| 718 | 726 | } |
| 719 | 727 | |
| 720 | 728 | root_scope.status = .loaded_success; |
| 721 | | root_scope.source = .{ .bytes = source }; |
| 722 | | keep_source = true; |
| 723 | 729 | root_scope.contents = .{ .module = zir_module }; |
| 724 | 730 | keep_zir_module = true; |
| 725 | 731 | |
| ... | ... | @@ -728,10 +734,9 @@ fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module { |
| 728 | 734 | |
| 729 | 735 | .unloaded_parse_failure, |
| 730 | 736 | .unloaded_sema_failure, |
| 731 | | .loaded_parse_failure, |
| 732 | | .loaded_sema_failure, |
| 733 | 737 | => return error.AnalysisFail, |
| 734 | | .loaded_success => return root_scope.contents.module, |
| 738 | |
| 739 | .loaded_success, .loaded_sema_failure => return root_scope.contents.module, |
| 735 | 740 | } |
| 736 | 741 | } |
| 737 | 742 | |
| ... | ... | @@ -760,10 +765,9 @@ fn analyzeRoot(self: *Module, root_scope: *Scope.ZIRModule) !void { |
| 760 | 765 | |
| 761 | 766 | .unloaded_parse_failure, |
| 762 | 767 | .unloaded_sema_failure, |
| 763 | | .loaded_parse_failure, |
| 768 | .unloaded_success, |
| 764 | 769 | .loaded_sema_failure, |
| 765 | 770 | .loaded_success, |
| 766 | | .unloaded_success, |
| 767 | 771 | => { |
| 768 | 772 | const src_module = try self.getSrcModule(root_scope); |
| 769 | 773 | |
| ... | ... | @@ -2008,9 +2012,16 @@ fn coerceArrayPtrToSlice(self: *Module, scope: *Scope, dest_type: Type, inst: *I |
| 2008 | 2012 | |
| 2009 | 2013 | fn fail(self: *Module, scope: *Scope, src: usize, comptime format: []const u8, args: var) InnerError { |
| 2010 | 2014 | @setCold(true); |
| 2011 | | try self.failed_decls.ensureCapacity(self.failed_decls.size + 1); |
| 2012 | | try self.failed_files.ensureCapacity(self.failed_files.size + 1); |
| 2013 | 2015 | const err_msg = try ErrorMsg.create(self.allocator, src, format, args); |
| 2016 | return self.failWithOwnedErrorMsg(scope, src, err_msg); |
| 2017 | } |
| 2018 | |
| 2019 | fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *ErrorMsg) InnerError { |
| 2020 | { |
| 2021 | errdefer err_msg.destroy(self.allocator); |
| 2022 | try self.failed_decls.ensureCapacity(self.failed_decls.size + 1); |
| 2023 | try self.failed_files.ensureCapacity(self.failed_files.size + 1); |
| 2024 | } |
| 2014 | 2025 | switch (scope.tag) { |
| 2015 | 2026 | .decl => { |
| 2016 | 2027 | const decl = scope.cast(Scope.DeclAnalysis).?.decl; |