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