authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-03 20:10:44+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-04 16:20:29+00:00
logd3ca10d5d8bc92280a14f9e40dc41d6accc1b4c2
tree20306667045b1136e12399d5fe13612925f0677c
parent3a4bb47fedbb890dc149622e31c75101b14c3b16
signaturelock-open Commit is signed but in an unrecognized format.

Zcu: remove `*_loaded` fields on `File`

Instead, `source`, `tree`, and `zir` should all be optional. This is precisely what we're actually trying to model here; and `File` isn't optimized for memory consumption or serializability anyway, so it's fine to use a couple of extra bytes on actual optionals here.

11 files changed, 191 insertions(+), 240 deletions(-)

src/Builtin.zig+9-13
......@@ -264,14 +264,12 @@ pub fn append(opts: @This(), buffer: *std.ArrayList(u8)) Allocator.Error!void {
264264}
265265
266266pub fn populateFile(comp: *Compilation, mod: *Module, file: *File) !void {
267 assert(file.source_loaded == true);
268
269267 if (mod.root.statFile(mod.root_src_path)) |stat| {
270 if (stat.size != file.source.len) {
268 if (stat.size != file.source.?.len) {
271269 std.log.warn(
272270 "the cached file '{}{s}' had the wrong size. Expected {d}, found {d}. " ++
273271 "Overwriting with correct file contents now",
274 .{ mod.root, mod.root_src_path, file.source.len, stat.size },
272 .{ mod.root, mod.root_src_path, file.source.?.len, stat.size },
275273 );
276274
277275 try writeFile(file, mod);
......@@ -296,15 +294,13 @@ pub fn populateFile(comp: *Compilation, mod: *Module, file: *File) !void {
296294
297295 log.debug("parsing and generating '{s}'", .{mod.root_src_path});
298296
299 file.tree = try std.zig.Ast.parse(comp.gpa, file.source, .zig);
300 assert(file.tree.errors.len == 0); // builtin.zig must parse
301 file.tree_loaded = true;
297 file.tree = try std.zig.Ast.parse(comp.gpa, file.source.?, .zig);
298 assert(file.tree.?.errors.len == 0); // builtin.zig must parse
302299
303 file.zir = try AstGen.generate(comp.gpa, file.tree);
304 assert(!file.zir.hasCompileErrors()); // builtin.zig must not have astgen errors
305 file.zir_loaded = true;
300 file.zir = try AstGen.generate(comp.gpa, file.tree.?);
301 assert(!file.zir.?.hasCompileErrors()); // builtin.zig must not have astgen errors
306302 file.status = .success_zir;
307 // Note that whilst we set `zir_loaded` here, we populated `path_digest`
303 // Note that whilst we set `zir` here, we populated `path_digest`
308304 // all the way back in `Package.Module.create`.
309305}
310306
......@@ -312,7 +308,7 @@ fn writeFile(file: *File, mod: *Module) !void {
312308 var buf: [std.fs.max_path_bytes]u8 = undefined;
313309 var af = try mod.root.atomicFile(mod.root_src_path, .{ .make_path = true }, &buf);
314310 defer af.deinit();
315 try af.file.writeAll(file.source);
311 try af.file.writeAll(file.source.?);
316312 af.finish() catch |err| switch (err) {
317313 error.AccessDenied => switch (builtin.os.tag) {
318314 .windows => {
......@@ -326,7 +322,7 @@ fn writeFile(file: *File, mod: *Module) !void {
326322 };
327323
328324 file.stat = .{
329 .size = file.source.len,
325 .size = file.source.?.len,
330326 .inode = 0, // dummy value
331327 .mtime = 0, // dummy value
332328 };
src/Compilation.zig+7-13
......@@ -3211,7 +3211,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
32113211 } else {
32123212 // Must be ZIR or Zoir errors. Note that this may include AST errors.
32133213 _ = try file.getTree(gpa); // Tree must be loaded.
3214 if (file.zir_loaded) {
3214 if (file.zir != null) {
32153215 try addZirErrorMessages(&bundle, file);
32163216 } else if (file.zoir != null) {
32173217 try addZoirErrorMessages(&bundle, file);
......@@ -3623,22 +3623,17 @@ pub fn addModuleErrorMsg(
36233623}
36243624
36253625pub fn addZirErrorMessages(eb: *ErrorBundle.Wip, file: *Zcu.File) !void {
3626 assert(file.zir_loaded);
3627 assert(file.tree_loaded);
3628 assert(file.source_loaded);
36293626 const gpa = eb.gpa;
36303627 const src_path = try file.fullPath(gpa);
36313628 defer gpa.free(src_path);
3632 return eb.addZirErrorMessages(file.zir, file.tree, file.source, src_path);
3629 return eb.addZirErrorMessages(file.zir.?, file.tree.?, file.source.?, src_path);
36333630}
36343631
36353632pub fn addZoirErrorMessages(eb: *ErrorBundle.Wip, file: *Zcu.File) !void {
3636 assert(file.source_loaded);
3637 assert(file.tree_loaded);
36383633 const gpa = eb.gpa;
36393634 const src_path = try file.fullPath(gpa);
36403635 defer gpa.free(src_path);
3641 return eb.addZoirErrorMessages(file.zoir.?, file.tree, file.source, src_path);
3636 return eb.addZoirErrorMessages(file.zoir.?, file.tree.?, file.source.?, src_path);
36423637}
36433638
36443639pub fn performAllTheWork(
......@@ -4312,18 +4307,17 @@ fn workerAstGenFile(
43124307 // Pre-emptively look for `@import` paths and queue them up.
43134308 // If we experience an error preemptively fetching the
43144309 // file, just ignore it and let it happen again later during Sema.
4315 assert(file.zir_loaded);
4316 const imports_index = file.zir.extra[@intFromEnum(Zir.ExtraIndex.imports)];
4310 const imports_index = file.zir.?.extra[@intFromEnum(Zir.ExtraIndex.imports)];
43174311 if (imports_index != 0) {
4318 const extra = file.zir.extraData(Zir.Inst.Imports, imports_index);
4312 const extra = file.zir.?.extraData(Zir.Inst.Imports, imports_index);
43194313 var import_i: u32 = 0;
43204314 var extra_index = extra.end;
43214315
43224316 while (import_i < extra.data.imports_len) : (import_i += 1) {
4323 const item = file.zir.extraData(Zir.Inst.Imports.Item, extra_index);
4317 const item = file.zir.?.extraData(Zir.Inst.Imports.Item, extra_index);
43244318 extra_index = item.end;
43254319
4326 const import_path = file.zir.nullTerminatedString(item.data.name);
4320 const import_path = file.zir.?.nullTerminatedString(item.data.name);
43274321 // `@import("builtin")` is handled specially.
43284322 if (mem.eql(u8, import_path, "builtin")) continue;
43294323
src/Package/Module.zig+4-6
......@@ -482,13 +482,11 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
482482 };
483483 new_file.* = .{
484484 .sub_file_path = "builtin.zig",
485 .source = generated_builtin_source,
486 .source_loaded = true,
487 .tree_loaded = false,
488 .zir_loaded = false,
489485 .stat = undefined,
490 .tree = undefined,
491 .zir = undefined,
486 .source = generated_builtin_source,
487 .tree = null,
488 .zir = null,
489 .zoir = null,
492490 .status = .never_loaded,
493491 .prev_status = .never_loaded,
494492 .mod = new,
src/Sema.zig+6-7
......@@ -7649,9 +7649,8 @@ fn analyzeCall(
76497649 const nav = ip.getNav(info.owner_nav);
76507650 const resolved_func_inst = info.zir_body_inst.resolveFull(ip) orelse return error.AnalysisFail;
76517651 const file = zcu.fileByIndex(resolved_func_inst.file);
7652 assert(file.zir_loaded);
7653 const zir_info = file.zir.getFnInfo(resolved_func_inst.inst);
7654 break :b .{ nav, file.zir, info.zir_body_inst, resolved_func_inst.inst, zir_info };
7652 const zir_info = file.zir.?.getFnInfo(resolved_func_inst.inst);
7653 break :b .{ nav, file.zir.?, info.zir_body_inst, resolved_func_inst.inst, zir_info };
76557654 } else .{ undefined, undefined, undefined, undefined, undefined };
76567655
76577656 // This is the `inst_map` used when evaluating generic parameters and return types.
......@@ -35328,7 +35327,7 @@ fn backingIntType(
3532835327 break :blk accumulator;
3532935328 };
3533035329
35331 const zir = zcu.namespacePtr(struct_type.namespace).fileScope(zcu).zir;
35330 const zir = zcu.namespacePtr(struct_type.namespace).fileScope(zcu).zir.?;
3533235331 const zir_index = struct_type.zir_index.resolve(ip) orelse return error.AnalysisFail;
3533335332 const extended = zir.instructions.items(.data)[@intFromEnum(zir_index)].extended;
3533435333 assert(extended.opcode == .struct_decl);
......@@ -35948,7 +35947,7 @@ fn structFields(
3594835947 const gpa = zcu.gpa;
3594935948 const ip = &zcu.intern_pool;
3595035949 const namespace_index = struct_type.namespace;
35951 const zir = zcu.namespacePtr(namespace_index).fileScope(zcu).zir;
35950 const zir = zcu.namespacePtr(namespace_index).fileScope(zcu).zir.?;
3595235951 const zir_index = struct_type.zir_index.resolve(ip) orelse return error.AnalysisFail;
3595335952
3595435953 const fields_len, _, var extra_index = structZirInfo(zir, zir_index);
......@@ -36149,7 +36148,7 @@ fn structFieldInits(
3614936148 assert(!struct_type.haveFieldInits(ip));
3615036149
3615136150 const namespace_index = struct_type.namespace;
36152 const zir = zcu.namespacePtr(namespace_index).fileScope(zcu).zir;
36151 const zir = zcu.namespacePtr(namespace_index).fileScope(zcu).zir.?;
3615336152 const zir_index = struct_type.zir_index.resolve(ip) orelse return error.AnalysisFail;
3615436153 const fields_len, _, var extra_index = structZirInfo(zir, zir_index);
3615536154
......@@ -36268,7 +36267,7 @@ fn unionFields(
3626836267 const zcu = pt.zcu;
3626936268 const gpa = zcu.gpa;
3627036269 const ip = &zcu.intern_pool;
36271 const zir = zcu.namespacePtr(union_type.namespace).fileScope(zcu).zir;
36270 const zir = zcu.namespacePtr(union_type.namespace).fileScope(zcu).zir.?;
3627236271 const zir_index = union_type.zir_index.resolve(ip) orelse return error.AnalysisFail;
3627336272 const extended = zir.instructions.items(.data)[@intFromEnum(zir_index)].extended;
3627436273 assert(extended.opcode == .union_decl);
src/Type.zig+3-4
......@@ -3587,8 +3587,7 @@ pub fn typeDeclSrcLine(ty: Type, zcu: *Zcu) ?u32 {
35873587 };
35883588 const info = tracked.resolveFull(&zcu.intern_pool) orelse return null;
35893589 const file = zcu.fileByIndex(info.file);
3590 assert(file.zir_loaded);
3591 const zir = file.zir;
3590 const zir = file.zir.?;
35923591 const inst = zir.instructions.get(@intFromEnum(info.inst));
35933592 return switch (inst.tag) {
35943593 .struct_init, .struct_init_ref => zir.extraData(Zir.Inst.StructInit, inst.data.pl_node.payload_index).data.abs_line,
......@@ -3905,7 +3904,7 @@ fn resolveStructInner(
39053904 var comptime_err_ret_trace = std.ArrayList(Zcu.LazySrcLoc).init(gpa);
39063905 defer comptime_err_ret_trace.deinit();
39073906
3908 const zir = zcu.namespacePtr(struct_obj.namespace).fileScope(zcu).zir;
3907 const zir = zcu.namespacePtr(struct_obj.namespace).fileScope(zcu).zir.?;
39093908 var sema: Sema = .{
39103909 .pt = pt,
39113910 .gpa = gpa,
......@@ -3959,7 +3958,7 @@ fn resolveUnionInner(
39593958 var comptime_err_ret_trace = std.ArrayList(Zcu.LazySrcLoc).init(gpa);
39603959 defer comptime_err_ret_trace.deinit();
39613960
3962 const zir = zcu.namespacePtr(union_obj.namespace).fileScope(zcu).zir;
3961 const zir = zcu.namespacePtr(union_obj.namespace).fileScope(zcu).zir.?;
39633962 var sema: Sema = .{
39643963 .pt = pt,
39653964 .gpa = gpa,
src/Zcu.zig+39-46
......@@ -660,22 +660,17 @@ pub const Namespace = struct {
660660pub const File = struct {
661661 status: Status,
662662 prev_status: Status,
663 source_loaded: bool,
664 tree_loaded: bool,
665 zir_loaded: bool,
666663 /// Relative to the owning package's root source directory.
667664 /// Memory is stored in gpa, owned by File.
668665 sub_file_path: []const u8,
669 /// Whether this is populated depends on `source_loaded`.
670 source: [:0]const u8,
671666 /// Whether this is populated depends on `status`.
672667 stat: Cache.File.Stat,
673 /// Whether this is populated or not depends on `tree_loaded`.
674 tree: Ast,
675 /// Whether this is populated or not depends on `zir_loaded`.
676 zir: Zir,
677 /// Cached Zoir, generated lazily.
678 zoir: ?Zoir = null,
668
669 source: ?[:0]const u8,
670 tree: ?Ast,
671 zir: ?Zir,
672 zoir: ?Zoir,
673
679674 /// Module that this file is a part of, managed externally.
680675 mod: *Package.Module,
681676 /// Whether this file is a part of multiple packages. This is an error condition which will be reported after AstGen.
......@@ -727,23 +722,23 @@ pub const File = struct {
727722 }
728723
729724 pub fn unloadTree(file: *File, gpa: Allocator) void {
730 if (file.tree_loaded) {
731 file.tree_loaded = false;
732 file.tree.deinit(gpa);
725 if (file.tree) |*tree| {
726 tree.deinit(gpa);
727 file.tree = null;
733728 }
734729 }
735730
736731 pub fn unloadSource(file: *File, gpa: Allocator) void {
737 if (file.source_loaded) {
738 file.source_loaded = false;
739 gpa.free(file.source);
732 if (file.source) |source| {
733 gpa.free(source);
734 file.source = null;
740735 }
741736 }
742737
743738 pub fn unloadZir(file: *File, gpa: Allocator) void {
744 if (file.zir_loaded) {
745 file.zir_loaded = false;
746 file.zir.deinit(gpa);
739 if (file.zir) |*zir| {
740 zir.deinit(gpa);
741 file.zir = null;
747742 }
748743 }
749744
......@@ -753,8 +748,8 @@ pub const File = struct {
753748 };
754749
755750 pub fn getSource(file: *File, gpa: Allocator) !Source {
756 if (file.source_loaded) return Source{
757 .bytes = file.source,
751 if (file.source) |source| return .{
752 .bytes = source,
758753 .stat = file.stat,
759754 };
760755
......@@ -769,7 +764,8 @@ pub const File = struct {
769764 return error.FileTooBig;
770765
771766 const source = try gpa.allocSentinel(u8, @as(usize, @intCast(stat.size)), 0);
772 defer if (!file.source_loaded) gpa.free(source);
767 defer gpa.free(source);
768
773769 const amt = try f.readAll(source);
774770 if (amt != stat.size)
775771 return error.UnexpectedEndOfFile;
......@@ -778,9 +774,9 @@ pub const File = struct {
778774 // used for error reporting. We need to keep the stat fields stale so that
779775 // astGenFile can know to regenerate ZIR.
780776
777 errdefer comptime unreachable; // don't error after populating `source`
781778 file.source = source;
782 file.source_loaded = true;
783 return Source{
779 return .{
784780 .bytes = source,
785781 .stat = .{
786782 .size = stat.size,
......@@ -791,20 +787,20 @@ pub const File = struct {
791787 }
792788
793789 pub fn getTree(file: *File, gpa: Allocator) !*const Ast {
794 if (file.tree_loaded) return &file.tree;
790 if (file.tree) |*tree| return tree;
795791
796792 const source = try file.getSource(gpa);
797 file.tree = try Ast.parse(gpa, source.bytes, file.getMode());
798 file.tree_loaded = true;
799 return &file.tree;
793 file.tree = try .parse(gpa, source.bytes, file.getMode());
794 return &file.tree.?;
800795 }
801796
802797 pub fn getZoir(file: *File, zcu: *Zcu) !*const Zoir {
803798 if (file.zoir) |*zoir| return zoir;
804799
805 assert(file.tree_loaded);
806 assert(file.tree.mode == .zon);
807 file.zoir = try ZonGen.generate(zcu.gpa, file.tree, .{});
800 const tree = file.tree.?;
801 assert(tree.mode == .zon);
802
803 file.zoir = try ZonGen.generate(zcu.gpa, tree, .{});
808804 if (file.zoir.?.hasCompileErrors()) {
809805 try zcu.failed_files.putNoClobber(zcu.gpa, file, null);
810806 return error.AnalysisFail;
......@@ -900,18 +896,18 @@ pub const File = struct {
900896
901897 // We can only mark children as failed if the ZIR is loaded, which may not
902898 // be the case if there were other astgen failures in this file
903 if (!file.zir_loaded) return;
899 if (file.zir == null) return;
904900
905 const imports_index = file.zir.extra[@intFromEnum(Zir.ExtraIndex.imports)];
901 const imports_index = file.zir.?.extra[@intFromEnum(Zir.ExtraIndex.imports)];
906902 if (imports_index == 0) return;
907 const extra = file.zir.extraData(Zir.Inst.Imports, imports_index);
903 const extra = file.zir.?.extraData(Zir.Inst.Imports, imports_index);
908904
909905 var extra_index = extra.end;
910906 for (0..extra.data.imports_len) |_| {
911 const item = file.zir.extraData(Zir.Inst.Imports.Item, extra_index);
907 const item = file.zir.?.extraData(Zir.Inst.Imports.Item, extra_index);
912908 extra_index = item.end;
913909
914 const import_path = file.zir.nullTerminatedString(item.data.name);
910 const import_path = file.zir.?.nullTerminatedString(item.data.name);
915911 if (mem.eql(u8, import_path, "builtin")) continue;
916912
917913 const res = pt.importFile(file, import_path) catch continue;
......@@ -1012,7 +1008,7 @@ pub const SrcLoc = struct {
10121008 lazy: LazySrcLoc.Offset,
10131009
10141010 pub fn baseSrcToken(src_loc: SrcLoc) Ast.TokenIndex {
1015 const tree = src_loc.file_scope.tree;
1011 const tree = src_loc.file_scope.tree.?;
10161012 return tree.firstToken(src_loc.base_node);
10171013 }
10181014
......@@ -1057,7 +1053,6 @@ pub const SrcLoc = struct {
10571053 const node_off = traced_off.x;
10581054 const tree = try src_loc.file_scope.getTree(gpa);
10591055 const node = src_loc.relativeToNodeIndex(node_off);
1060 assert(src_loc.file_scope.tree_loaded);
10611056 return tree.nodeToSpan(node);
10621057 },
10631058 .node_offset_main_token => |node_off| {
......@@ -1069,7 +1064,6 @@ pub const SrcLoc = struct {
10691064 .node_offset_bin_op => |node_off| {
10701065 const tree = try src_loc.file_scope.getTree(gpa);
10711066 const node = src_loc.relativeToNodeIndex(node_off);
1072 assert(src_loc.file_scope.tree_loaded);
10731067 return tree.nodeToSpan(node);
10741068 },
10751069 .node_offset_initializer => |node_off| {
......@@ -2408,9 +2402,8 @@ pub const LazySrcLoc = struct {
24082402 if (zir_inst == .main_struct_inst) return .{ file, 0 };
24092403
24102404 // Otherwise, make sure ZIR is loaded.
2411 assert(file.zir_loaded);
2405 const zir = file.zir.?;
24122406
2413 const zir = file.zir;
24142407 const inst = zir.instructions.get(@intFromEnum(zir_inst));
24152408 const base_node: Ast.Node.Index = switch (inst.tag) {
24162409 .declaration => inst.data.declaration.src_node,
......@@ -3671,7 +3664,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
36713664 const inst_info = nav.analysis.?.zir_index.resolveFull(ip) orelse continue;
36723665 const file = zcu.fileByIndex(inst_info.file);
36733666 // If the file failed AstGen, the TrackedInst refers to the old ZIR.
3674 const zir = if (file.status == .success_zir) file.zir else file.prev_zir.?.*;
3667 const zir = if (file.status == .success_zir) file.zir.? else file.prev_zir.?.*;
36753668 const decl = zir.getDeclaration(inst_info.inst);
36763669
36773670 if (!comp.config.is_test or file.mod != zcu.main_mod) continue;
......@@ -3703,7 +3696,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
37033696 const inst_info = ip.getNav(nav).analysis.?.zir_index.resolveFull(ip) orelse continue;
37043697 const file = zcu.fileByIndex(inst_info.file);
37053698 // If the file failed AstGen, the TrackedInst refers to the old ZIR.
3706 const zir = if (file.status == .success_zir) file.zir else file.prev_zir.?.*;
3699 const zir = if (file.status == .success_zir) file.zir.? else file.prev_zir.?.*;
37073700 const decl = zir.getDeclaration(inst_info.inst);
37083701 if (decl.linkage == .@"export") {
37093702 const unit: AnalUnit = .wrap(.{ .nav_val = nav });
......@@ -3721,7 +3714,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
37213714 const inst_info = ip.getNav(nav).analysis.?.zir_index.resolveFull(ip) orelse continue;
37223715 const file = zcu.fileByIndex(inst_info.file);
37233716 // If the file failed AstGen, the TrackedInst refers to the old ZIR.
3724 const zir = if (file.status == .success_zir) file.zir else file.prev_zir.?.*;
3717 const zir = if (file.status == .success_zir) file.zir.? else file.prev_zir.?.*;
37253718 const decl = zir.getDeclaration(inst_info.inst);
37263719 if (decl.linkage == .@"export") {
37273720 const unit: AnalUnit = .wrap(.{ .nav_val = nav });
......@@ -3858,7 +3851,7 @@ pub fn navSrcLine(zcu: *Zcu, nav_index: InternPool.Nav.Index) u32 {
38583851 const ip = &zcu.intern_pool;
38593852 const inst_info = ip.getNav(nav_index).srcInst(ip).resolveFull(ip).?;
38603853 const zir = zcu.fileByIndex(inst_info.file).zir;
3861 return zir.getDeclaration(inst_info.inst).src_line;
3854 return zir.?.getDeclaration(inst_info.inst).src_line;
38623855}
38633856
38643857pub fn navValue(zcu: *const Zcu, nav_index: InternPool.Nav.Index) Value {
src/Zcu/PerThread.zig+56-63
......@@ -209,7 +209,6 @@ pub fn astGenFile(
209209 },
210210 else => |e| return e,
211211 };
212 file.zir_loaded = true;
213212 file.stat = .{
214213 .size = header.stat_size,
215214 .inode = header.stat_inode,
......@@ -219,12 +218,12 @@ pub fn astGenFile(
219218 file.status = .success_zir;
220219 log.debug("AstGen cached success: {s}", .{file.sub_file_path});
221220
222 if (file.zir.hasCompileErrors()) {
221 if (file.zir.?.hasCompileErrors()) {
223222 comp.mutex.lock();
224223 defer comp.mutex.unlock();
225224 try zcu.failed_files.putNoClobber(gpa, file, null);
226225 }
227 if (file.zir.loweringFailed()) {
226 if (file.zir.?.loweringFailed()) {
228227 file.status = .astgen_failure;
229228 return error.AnalysisFail;
230229 }
......@@ -261,13 +260,12 @@ pub fn astGenFile(
261260 // single-threaded context, so we need to keep both versions around
262261 // until that point in the pipeline. Previous ZIR data is freed after
263262 // that.
264 if (file.zir_loaded and !file.zir.loweringFailed()) {
263 if (file.zir != null and !file.zir.?.loweringFailed()) {
265264 assert(file.prev_zir == null);
266265 const prev_zir_ptr = try gpa.create(Zir);
267266 file.prev_zir = prev_zir_ptr;
268 prev_zir_ptr.* = file.zir;
269 file.zir = undefined;
270 file.zir_loaded = false;
267 prev_zir_ptr.* = file.zir.?;
268 file.zir = null;
271269 }
272270 file.unload(gpa);
273271
......@@ -275,7 +273,7 @@ pub fn astGenFile(
275273 return error.FileTooBig;
276274
277275 const source = try gpa.allocSentinel(u8, @as(usize, @intCast(stat.size)), 0);
278 defer if (!file.source_loaded) gpa.free(source);
276 defer if (file.source == null) gpa.free(source);
279277 const amt = try source_file.readAll(source);
280278 if (amt != stat.size)
281279 return error.UnexpectedEndOfFile;
......@@ -286,42 +284,39 @@ pub fn astGenFile(
286284 .mtime = stat.mtime,
287285 };
288286 file.source = source;
289 file.source_loaded = true;
290287
291288 file.tree = try Ast.parse(gpa, source, .zig);
292 file.tree_loaded = true;
293289
294290 // Any potential AST errors are converted to ZIR errors here.
295 file.zir = try AstGen.generate(gpa, file.tree);
296 file.zir_loaded = true;
291 file.zir = try AstGen.generate(gpa, file.tree.?);
297292 file.prev_status = file.status;
298293 file.status = .success_zir;
299294 log.debug("AstGen fresh success: {s}", .{file.sub_file_path});
300295
301296 const safety_buffer = if (Zcu.data_has_safety_tag)
302 try gpa.alloc([8]u8, file.zir.instructions.len)
297 try gpa.alloc([8]u8, file.zir.?.instructions.len)
303298 else
304299 undefined;
305300 defer if (Zcu.data_has_safety_tag) gpa.free(safety_buffer);
306301 const data_ptr = if (Zcu.data_has_safety_tag)
307 if (file.zir.instructions.len == 0)
302 if (file.zir.?.instructions.len == 0)
308303 @as([*]const u8, undefined)
309304 else
310305 @as([*]const u8, @ptrCast(safety_buffer.ptr))
311306 else
312 @as([*]const u8, @ptrCast(file.zir.instructions.items(.data).ptr));
307 @as([*]const u8, @ptrCast(file.zir.?.instructions.items(.data).ptr));
313308 if (Zcu.data_has_safety_tag) {
314309 // The `Data` union has a safety tag but in the file format we store it without.
315 for (file.zir.instructions.items(.data), 0..) |*data, i| {
310 for (file.zir.?.instructions.items(.data), 0..) |*data, i| {
316311 const as_struct: *const Zcu.HackDataLayout = @ptrCast(data);
317312 safety_buffer[i] = as_struct.data;
318313 }
319314 }
320315
321316 const header: Zir.Header = .{
322 .instructions_len = @as(u32, @intCast(file.zir.instructions.len)),
323 .string_bytes_len = @as(u32, @intCast(file.zir.string_bytes.len)),
324 .extra_len = @as(u32, @intCast(file.zir.extra.len)),
317 .instructions_len = @as(u32, @intCast(file.zir.?.instructions.len)),
318 .string_bytes_len = @as(u32, @intCast(file.zir.?.string_bytes.len)),
319 .extra_len = @as(u32, @intCast(file.zir.?.extra.len)),
325320
326321 .stat_size = stat.size,
327322 .stat_inode = stat.inode,
......@@ -333,20 +328,20 @@ pub fn astGenFile(
333328 .len = @sizeOf(Zir.Header),
334329 },
335330 .{
336 .base = @as([*]const u8, @ptrCast(file.zir.instructions.items(.tag).ptr)),
337 .len = file.zir.instructions.len,
331 .base = @as([*]const u8, @ptrCast(file.zir.?.instructions.items(.tag).ptr)),
332 .len = file.zir.?.instructions.len,
338333 },
339334 .{
340335 .base = data_ptr,
341 .len = file.zir.instructions.len * 8,
336 .len = file.zir.?.instructions.len * 8,
342337 },
343338 .{
344 .base = file.zir.string_bytes.ptr,
345 .len = file.zir.string_bytes.len,
339 .base = file.zir.?.string_bytes.ptr,
340 .len = file.zir.?.string_bytes.len,
346341 },
347342 .{
348 .base = @as([*]const u8, @ptrCast(file.zir.extra.ptr)),
349 .len = file.zir.extra.len * 4,
343 .base = @as([*]const u8, @ptrCast(file.zir.?.extra.ptr)),
344 .len = file.zir.?.extra.len * 4,
350345 },
351346 };
352347 cache_file.writevAll(&iovecs) catch |err| {
......@@ -355,12 +350,12 @@ pub fn astGenFile(
355350 });
356351 };
357352
358 if (file.zir.hasCompileErrors()) {
353 if (file.zir.?.hasCompileErrors()) {
359354 comp.mutex.lock();
360355 defer comp.mutex.unlock();
361356 try zcu.failed_files.putNoClobber(gpa, file, null);
362357 }
363 if (file.zir.loweringFailed()) {
358 if (file.zir.?.loweringFailed()) {
364359 file.status = .astgen_failure;
365360 return error.AnalysisFail;
366361 }
......@@ -392,7 +387,7 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
392387 try zcu.markDependeeOutdated(.not_marked_po, .{ .file = file_index });
393388 }
394389 const old_zir = file.prev_zir orelse continue;
395 const new_zir = file.zir;
390 const new_zir = file.zir.?;
396391 const gop = try updated_files.getOrPut(gpa, file_index);
397392 assert(!gop.found_existing);
398393 gop.value_ptr.* = .{
......@@ -400,7 +395,7 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
400395 .inst_map = .{},
401396 };
402397 if (!new_zir.loweringFailed()) {
403 try Zcu.mapOldZirToNew(gpa, old_zir.*, file.zir, &gop.value_ptr.inst_map);
398 try Zcu.mapOldZirToNew(gpa, old_zir.*, new_zir, &gop.value_ptr.inst_map);
404399 }
405400 }
406401
......@@ -426,7 +421,7 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
426421 // Either way, invalidate associated `src_hash` deps.
427422 log.debug("tracking failed for %{d}{s}", .{
428423 old_inst,
429 if (file.zir.loweringFailed()) " due to AstGen failure" else "",
424 if (file.zir.?.loweringFailed()) " due to AstGen failure" else "",
430425 });
431426 tracked_inst.inst = .lost;
432427 try zcu.markDependeeOutdated(.not_marked_po, .{ .src_hash = tracked_inst_index });
......@@ -435,7 +430,7 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
435430 tracked_inst.inst = InternPool.TrackedInst.MaybeLost.ZirIndex.wrap(new_inst);
436431
437432 const old_zir = file.prev_zir.?.*;
438 const new_zir = file.zir;
433 const new_zir = file.zir.?;
439434 const old_tag = old_zir.instructions.items(.tag)[@intFromEnum(old_inst)];
440435 const old_data = old_zir.instructions.items(.data)[@intFromEnum(old_inst)];
441436
......@@ -532,7 +527,7 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
532527
533528 for (updated_files.keys(), updated_files.values()) |file_index, updated_file| {
534529 const file = updated_file.file;
535 if (file.zir.loweringFailed()) {
530 if (file.zir.?.loweringFailed()) {
536531 // Keep `prev_zir` around: it's the last usable ZIR.
537532 // Don't update the namespace, as we have no new data to update *to*.
538533 } else {
......@@ -805,7 +800,7 @@ fn analyzeComptimeUnit(pt: Zcu.PerThread, cu_id: InternPool.ComptimeUnit.Id) Zcu
805800 // unnecessary, and we can move the below `removeDependenciesForDepender` call up with its friends
806801 // in `ensureComptimeUnitUpToDate`.
807802 if (file.status != .success_zir) return error.AnalysisFail;
808 const zir = file.zir;
803 const zir = file.zir.?;
809804
810805 // We are about to re-analyze this unit; drop its depenndencies.
811806 zcu.intern_pool.removeDependenciesForDepender(gpa, anal_unit);
......@@ -1002,7 +997,7 @@ fn analyzeNavVal(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileErr
1002997 // unnecessary, and we can move the below `removeDependenciesForDepender` call up with its friends
1003998 // in `ensureComptimeUnitUpToDate`.
1004999 if (file.status != .success_zir) return error.AnalysisFail;
1005 const zir = file.zir;
1000 const zir = file.zir.?;
10061001
10071002 // We are about to re-analyze this unit; drop its depenndencies.
10081003 zcu.intern_pool.removeDependenciesForDepender(gpa, anal_unit);
......@@ -1380,7 +1375,7 @@ fn analyzeNavType(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileEr
13801375 // unnecessary, and we can move the below `removeDependenciesForDepender` call up with its friends
13811376 // in `ensureComptimeUnitUpToDate`.
13821377 if (file.status != .success_zir) return error.AnalysisFail;
1383 const zir = file.zir;
1378 const zir = file.zir.?;
13841379
13851380 // We are about to re-analyze this unit; drop its depenndencies.
13861381 zcu.intern_pool.removeDependenciesForDepender(gpa, anal_unit);
......@@ -1758,7 +1753,7 @@ fn createFileRootStruct(
17581753 const gpa = zcu.gpa;
17591754 const ip = &zcu.intern_pool;
17601755 const file = zcu.fileByIndex(file_index);
1761 const extended = file.zir.instructions.items(.data)[@intFromEnum(Zir.Inst.Index.main_struct_inst)].extended;
1756 const extended = file.zir.?.instructions.items(.data)[@intFromEnum(Zir.Inst.Index.main_struct_inst)].extended;
17621757 assert(extended.opcode == .struct_decl);
17631758 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
17641759 assert(!small.has_captures_len);
......@@ -1766,16 +1761,16 @@ fn createFileRootStruct(
17661761 assert(small.layout == .auto);
17671762 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).@"struct".fields.len;
17681763 const fields_len = if (small.has_fields_len) blk: {
1769 const fields_len = file.zir.extra[extra_index];
1764 const fields_len = file.zir.?.extra[extra_index];
17701765 extra_index += 1;
17711766 break :blk fields_len;
17721767 } else 0;
17731768 const decls_len = if (small.has_decls_len) blk: {
1774 const decls_len = file.zir.extra[extra_index];
1769 const decls_len = file.zir.?.extra[extra_index];
17751770 extra_index += 1;
17761771 break :blk decls_len;
17771772 } else 0;
1778 const decls = file.zir.bodySlice(extra_index, decls_len);
1773 const decls = file.zir.?.bodySlice(extra_index, decls_len);
17791774 extra_index += decls_len;
17801775
17811776 const tracked_inst = try ip.trackZir(gpa, pt.tid, .{
......@@ -1844,17 +1839,17 @@ fn updateFileNamespace(pt: Zcu.PerThread, file_index: Zcu.File.Index) Allocator.
18441839
18451840 const namespace_index = Type.fromInterned(file_root_type).getNamespaceIndex(zcu);
18461841 const decls = decls: {
1847 const extended = file.zir.instructions.items(.data)[@intFromEnum(Zir.Inst.Index.main_struct_inst)].extended;
1842 const extended = file.zir.?.instructions.items(.data)[@intFromEnum(Zir.Inst.Index.main_struct_inst)].extended;
18481843 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
18491844
18501845 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).@"struct".fields.len;
18511846 extra_index += @intFromBool(small.has_fields_len);
18521847 const decls_len = if (small.has_decls_len) blk: {
1853 const decls_len = file.zir.extra[extra_index];
1848 const decls_len = file.zir.?.extra[extra_index];
18541849 extra_index += 1;
18551850 break :blk decls_len;
18561851 } else 0;
1857 break :decls file.zir.bodySlice(extra_index, decls_len);
1852 break :decls file.zir.?.bodySlice(extra_index, decls_len);
18581853 };
18591854 try pt.scanNamespace(namespace_index, decls);
18601855 zcu.namespacePtr(namespace_index).generation = zcu.generation;
......@@ -1873,7 +1868,7 @@ fn semaFile(pt: Zcu.PerThread, file_index: Zcu.File.Index) Zcu.SemaError!void {
18731868 if (file.status != .success_zir) {
18741869 return error.AnalysisFail;
18751870 }
1876 assert(file.zir_loaded);
1871 assert(file.zir != null);
18771872
18781873 const new_namespace_index = try pt.createNamespace(.{
18791874 .parent = .none,
......@@ -1983,13 +1978,11 @@ pub fn importPkg(pt: Zcu.PerThread, mod: *Module) !Zcu.ImportFileResult {
19831978 gop.value_ptr.* = new_file_index;
19841979 new_file.* = .{
19851980 .sub_file_path = sub_file_path,
1986 .source = undefined,
1987 .source_loaded = false,
1988 .tree_loaded = false,
1989 .zir_loaded = false,
19901981 .stat = undefined,
1991 .tree = undefined,
1992 .zir = undefined,
1982 .source = null,
1983 .tree = null,
1984 .zir = null,
1985 .zoir = null,
19931986 .status = .never_loaded,
19941987 .prev_status = .never_loaded,
19951988 .mod = mod,
......@@ -2096,13 +2089,11 @@ pub fn importFile(
20962089 gop.value_ptr.* = new_file_index;
20972090 new_file.* = .{
20982091 .sub_file_path = sub_file_path,
2099 .source = undefined,
2100 .source_loaded = false,
2101 .tree_loaded = false,
2102 .zir_loaded = false,
21032092 .stat = undefined,
2104 .tree = undefined,
2105 .zir = undefined,
2093 .source = null,
2094 .tree = null,
2095 .zir = null,
2096 .zoir = null,
21062097 .status = .never_loaded,
21072098 .prev_status = .never_loaded,
21082099 .mod = mod,
......@@ -2441,7 +2432,7 @@ const ScanDeclIter = struct {
24412432 const namespace = zcu.namespacePtr(namespace_index);
24422433 const gpa = zcu.gpa;
24432434 const file = namespace.fileScope(zcu);
2444 const zir = file.zir;
2435 const zir = file.zir.?;
24452436 const ip = &zcu.intern_pool;
24462437
24472438 const decl = zir.getDeclaration(decl_inst);
......@@ -2591,7 +2582,7 @@ fn analyzeFnBodyInner(pt: Zcu.PerThread, func_index: InternPool.Index) Zcu.SemaE
25912582 const func = zcu.funcInfo(func_index);
25922583 const inst_info = func.zir_body_inst.resolveFull(ip) orelse return error.AnalysisFail;
25932584 const file = zcu.fileByIndex(inst_info.file);
2594 const zir = file.zir;
2585 const zir = file.zir.?;
25952586
25962587 try zcu.analysis_in_progress.put(gpa, anal_unit, {});
25972588 errdefer _ = zcu.analysis_in_progress.swapRemove(anal_unit);
......@@ -2843,7 +2834,9 @@ pub fn getErrorValueFromSlice(pt: Zcu.PerThread, name: []const u8) Allocator.Err
28432834/// Removes any entry from `Zcu.failed_files` associated with `file`. Acquires `Compilation.mutex` as needed.
28442835/// `file.zir` must be unchanged from the last update, as it is used to determine if there is such an entry.
28452836fn lockAndClearFileCompileError(pt: Zcu.PerThread, file: *Zcu.File) void {
2846 if (!file.zir_loaded or !file.zir.hasCompileErrors()) return;
2837 const zir = file.zir orelse return;
2838 if (zir.hasCompileErrors()) return;
2839
28472840 pt.zcu.comp.mutex.lock();
28482841 defer pt.zcu.comp.mutex.unlock();
28492842 if (pt.zcu.failed_files.fetchSwapRemove(file)) |kv| {
......@@ -3779,7 +3772,7 @@ fn recreateStructType(
37793772 const inst_info = key.zir_index.resolveFull(ip).?;
37803773 const file = zcu.fileByIndex(inst_info.file);
37813774 assert(file.status == .success_zir); // otherwise inst tracking failed
3782 const zir = file.zir;
3775 const zir = file.zir.?;
37833776
37843777 assert(zir.instructions.items(.tag)[@intFromEnum(inst_info.inst)] == .extended);
37853778 const extended = zir.instructions.items(.data)[@intFromEnum(inst_info.inst)].extended;
......@@ -3852,7 +3845,7 @@ fn recreateUnionType(
38523845 const inst_info = key.zir_index.resolveFull(ip).?;
38533846 const file = zcu.fileByIndex(inst_info.file);
38543847 assert(file.status == .success_zir); // otherwise inst tracking failed
3855 const zir = file.zir;
3848 const zir = file.zir.?;
38563849
38573850 assert(zir.instructions.items(.tag)[@intFromEnum(inst_info.inst)] == .extended);
38583851 const extended = zir.instructions.items(.data)[@intFromEnum(inst_info.inst)].extended;
......@@ -3939,7 +3932,7 @@ fn recreateEnumType(
39393932 const inst_info = key.zir_index.resolveFull(ip).?;
39403933 const file = zcu.fileByIndex(inst_info.file);
39413934 assert(file.status == .success_zir); // otherwise inst tracking failed
3942 const zir = file.zir;
3935 const zir = file.zir.?;
39433936
39443937 assert(zir.instructions.items(.tag)[@intFromEnum(inst_info.inst)] == .extended);
39453938 const extended = zir.instructions.items(.data)[@intFromEnum(inst_info.inst)].extended;
......@@ -4083,7 +4076,7 @@ pub fn ensureNamespaceUpToDate(pt: Zcu.PerThread, namespace_index: Zcu.Namespace
40834076 const inst_info = key.zir_index.resolveFull(ip) orelse return error.AnalysisFail;
40844077 const file = zcu.fileByIndex(inst_info.file);
40854078 if (file.status != .success_zir) return error.AnalysisFail;
4086 const zir = file.zir;
4079 const zir = file.zir.?;
40874080
40884081 assert(zir.instructions.items(.tag)[@intFromEnum(inst_info.inst)] == .extended);
40894082 const extended = zir.instructions.items(.data)[@intFromEnum(inst_info.inst)].extended;
src/link.zig+1-2
......@@ -750,8 +750,7 @@ pub const File = struct {
750750 {
751751 const ti = ti_id.resolveFull(&pt.zcu.intern_pool).?;
752752 const file = pt.zcu.fileByIndex(ti.file);
753 assert(file.zir_loaded);
754 const inst = file.zir.instructions.get(@intFromEnum(ti.inst));
753 const inst = file.zir.?.instructions.get(@intFromEnum(ti.inst));
755754 assert(inst.tag == .declaration);
756755 }
757756
src/link/Dwarf.zig+7-10
......@@ -2358,8 +2358,7 @@ fn initWipNavInner(
23582358 const nav = ip.getNav(nav_index);
23592359 const inst_info = nav.srcInst(ip).resolveFull(ip).?;
23602360 const file = zcu.fileByIndex(inst_info.file);
2361 assert(file.zir_loaded);
2362 const decl = file.zir.getDeclaration(inst_info.inst);
2361 const decl = file.zir.?.getDeclaration(inst_info.inst);
23632362 log.debug("initWipNav({s}:{d}:{d} %{d} = {})", .{
23642363 file.sub_file_path,
23652364 decl.src_line + 1,
......@@ -2373,7 +2372,7 @@ fn initWipNavInner(
23732372 switch (nav_key) {
23742373 // Ignore @extern
23752374 .@"extern" => |@"extern"| if (decl.linkage != .@"extern" or
2376 !@"extern".name.eqlSlice(file.zir.nullTerminatedString(decl.name), ip)) return null,
2375 !@"extern".name.eqlSlice(file.zir.?.nullTerminatedString(decl.name), ip)) return null,
23772376 else => {},
23782377 }
23792378
......@@ -2696,8 +2695,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
26962695 const nav = ip.getNav(nav_index);
26972696 const inst_info = nav.srcInst(ip).resolveFull(ip).?;
26982697 const file = zcu.fileByIndex(inst_info.file);
2699 assert(file.zir_loaded);
2700 const decl = file.zir.getDeclaration(inst_info.inst);
2698 const decl = file.zir.?.getDeclaration(inst_info.inst);
27012699 log.debug("updateComptimeNav({s}:{d}:{d} %{d} = {})", .{
27022700 file.sub_file_path,
27032701 decl.src_line + 1,
......@@ -4097,7 +4095,7 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
40974095 // if a newly-tracked instruction can be a type's owner `zir_index`.
40984096 comptime assert(Zir.inst_tracking_version == 0);
40994097
4100 const decl_inst = file.zir.instructions.get(@intFromEnum(inst_info.inst));
4098 const decl_inst = file.zir.?.instructions.get(@intFromEnum(inst_info.inst));
41014099 const name_strat: Zir.Inst.NameStrategy = switch (decl_inst.tag) {
41024100 .struct_init, .struct_init_ref, .struct_init_anon => .anon,
41034101 .extended => switch (decl_inst.data.extended.opcode) {
......@@ -4301,14 +4299,13 @@ pub fn updateLineNumber(dwarf: *Dwarf, zcu: *Zcu, zir_index: InternPool.TrackedI
43014299 const inst_info = zir_index.resolveFull(ip).?;
43024300 assert(inst_info.inst != .main_struct_inst);
43034301 const file = zcu.fileByIndex(inst_info.file);
4304 assert(file.zir_loaded);
4305 const decl = file.zir.getDeclaration(inst_info.inst);
4302 const decl = file.zir.?.getDeclaration(inst_info.inst);
43064303 log.debug("updateLineNumber({s}:{d}:{d} %{d} = {s})", .{
43074304 file.sub_file_path,
43084305 decl.src_line + 1,
43094306 decl.src_column + 1,
43104307 @intFromEnum(inst_info.inst),
4311 file.zir.nullTerminatedString(decl.name),
4308 file.zir.?.nullTerminatedString(decl.name),
43124309 });
43134310
43144311 var line_buf: [4]u8 = undefined;
......@@ -4661,7 +4658,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
46614658 .target_unit = StringSection.unit,
46624659 .target_entry = (try dwarf.debug_line_str.addString(
46634660 dwarf,
4664 if (file.mod.builtin_file == file) file.source else "",
4661 if (file.mod.builtin_file == file) file.source.? else "",
46654662 )).toOptional(),
46664663 });
46674664 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
src/main.zig+49-63
......@@ -3636,7 +3636,7 @@ fn buildOutputType(
36363636
36373637 if (show_builtin) {
36383638 const builtin_mod = comp.root_mod.getBuiltinDependency();
3639 const source = builtin_mod.builtin_file.?.source;
3639 const source = builtin_mod.builtin_file.?.source.?;
36403640 return std.io.getStdOut().writeAll(source);
36413641 }
36423642 switch (listen) {
......@@ -6135,14 +6135,12 @@ fn cmdAstCheck(
61356135 var file: Zcu.File = .{
61366136 .status = .never_loaded,
61376137 .prev_status = .never_loaded,
6138 .source_loaded = false,
6139 .tree_loaded = false,
6140 .zir_loaded = false,
61416138 .sub_file_path = undefined,
6142 .source = undefined,
61436139 .stat = undefined,
6144 .tree = undefined,
6145 .zir = undefined,
6140 .source = null,
6141 .tree = null,
6142 .zir = null,
6143 .zoir = null,
61466144 .mod = undefined,
61476145 };
61486146 if (zig_source_file) |file_name| {
......@@ -6163,7 +6161,6 @@ fn cmdAstCheck(
61636161
61646162 file.sub_file_path = file_name;
61656163 file.source = source;
6166 file.source_loaded = true;
61676164 file.stat = .{
61686165 .size = stat.size,
61696166 .inode = stat.inode,
......@@ -6176,7 +6173,6 @@ fn cmdAstCheck(
61766173 };
61776174 file.sub_file_path = "<stdin>";
61786175 file.source = source;
6179 file.source_loaded = true;
61806176 file.stat.size = source.len;
61816177 }
61826178
......@@ -6196,17 +6192,15 @@ fn cmdAstCheck(
61966192 .fully_qualified_name = "root",
61976193 });
61986194
6199 file.tree = try Ast.parse(gpa, file.source, mode);
6200 file.tree_loaded = true;
6201 defer file.tree.deinit(gpa);
6195 file.tree = try Ast.parse(gpa, file.source.?, mode);
6196 defer file.tree.?.deinit(gpa);
62026197
62036198 switch (mode) {
62046199 .zig => {
6205 file.zir = try AstGen.generate(gpa, file.tree);
6206 file.zir_loaded = true;
6207 defer file.zir.deinit(gpa);
6200 file.zir = try AstGen.generate(gpa, file.tree.?);
6201 defer file.zir.?.deinit(gpa);
62086202
6209 if (file.zir.hasCompileErrors()) {
6203 if (file.zir.?.hasCompileErrors()) {
62106204 var wip_errors: std.zig.ErrorBundle.Wip = undefined;
62116205 try wip_errors.init(gpa);
62126206 defer wip_errors.deinit();
......@@ -6215,13 +6209,13 @@ fn cmdAstCheck(
62156209 defer error_bundle.deinit(gpa);
62166210 error_bundle.renderToStdErr(color.renderOptions());
62176211
6218 if (file.zir.loweringFailed()) {
6212 if (file.zir.?.loweringFailed()) {
62196213 process.exit(1);
62206214 }
62216215 }
62226216
62236217 if (!want_output_text) {
6224 if (file.zir.hasCompileErrors()) {
6218 if (file.zir.?.hasCompileErrors()) {
62256219 process.exit(1);
62266220 } else {
62276221 return cleanExit();
......@@ -6233,18 +6227,18 @@ fn cmdAstCheck(
62336227
62346228 {
62356229 const token_bytes = @sizeOf(Ast.TokenList) +
6236 file.tree.tokens.len * (@sizeOf(std.zig.Token.Tag) + @sizeOf(Ast.ByteOffset));
6237 const tree_bytes = @sizeOf(Ast) + file.tree.nodes.len *
6230 file.tree.?.tokens.len * (@sizeOf(std.zig.Token.Tag) + @sizeOf(Ast.ByteOffset));
6231 const tree_bytes = @sizeOf(Ast) + file.tree.?.nodes.len *
62386232 (@sizeOf(Ast.Node.Tag) +
62396233 @sizeOf(Ast.Node.Data) +
62406234 @sizeOf(Ast.TokenIndex));
6241 const instruction_bytes = file.zir.instructions.len *
6235 const instruction_bytes = file.zir.?.instructions.len *
62426236 // Here we don't use @sizeOf(Zir.Inst.Data) because it would include
62436237 // the debug safety tag but we want to measure release size.
62446238 (@sizeOf(Zir.Inst.Tag) + 8);
6245 const extra_bytes = file.zir.extra.len * @sizeOf(u32);
6239 const extra_bytes = file.zir.?.extra.len * @sizeOf(u32);
62466240 const total_bytes = @sizeOf(Zir) + instruction_bytes + extra_bytes +
6247 file.zir.string_bytes.len * @sizeOf(u8);
6241 file.zir.?.string_bytes.len * @sizeOf(u8);
62486242 const stdout = io.getStdOut();
62496243 const fmtIntSizeBin = std.fmt.fmtIntSizeBin;
62506244 // zig fmt: off
......@@ -6258,27 +6252,27 @@ fn cmdAstCheck(
62586252 \\# Extra Data Items: {d} ({})
62596253 \\
62606254 , .{
6261 fmtIntSizeBin(file.source.len),
6262 file.tree.tokens.len, fmtIntSizeBin(token_bytes),
6263 file.tree.nodes.len, fmtIntSizeBin(tree_bytes),
6255 fmtIntSizeBin(file.source.?.len),
6256 file.tree.?.tokens.len, fmtIntSizeBin(token_bytes),
6257 file.tree.?.nodes.len, fmtIntSizeBin(tree_bytes),
62646258 fmtIntSizeBin(total_bytes),
6265 file.zir.instructions.len, fmtIntSizeBin(instruction_bytes),
6266 fmtIntSizeBin(file.zir.string_bytes.len),
6267 file.zir.extra.len, fmtIntSizeBin(extra_bytes),
6259 file.zir.?.instructions.len, fmtIntSizeBin(instruction_bytes),
6260 fmtIntSizeBin(file.zir.?.string_bytes.len),
6261 file.zir.?.extra.len, fmtIntSizeBin(extra_bytes),
62686262 });
62696263 // zig fmt: on
62706264 }
62716265
62726266 try @import("print_zir.zig").renderAsTextToFile(gpa, &file, io.getStdOut());
62736267
6274 if (file.zir.hasCompileErrors()) {
6268 if (file.zir.?.hasCompileErrors()) {
62756269 process.exit(1);
62766270 } else {
62776271 return cleanExit();
62786272 }
62796273 },
62806274 .zon => {
6281 const zoir = try ZonGen.generate(gpa, file.tree, .{});
6275 const zoir = try ZonGen.generate(gpa, file.tree.?, .{});
62826276 defer zoir.deinit(gpa);
62836277
62846278 if (zoir.hasCompileErrors()) {
......@@ -6289,7 +6283,7 @@ fn cmdAstCheck(
62896283 {
62906284 const src_path = try file.fullPath(gpa);
62916285 defer gpa.free(src_path);
6292 try wip_errors.addZoirErrorMessages(zoir, file.tree, file.source, src_path);
6286 try wip_errors.addZoirErrorMessages(zoir, file.tree.?, file.source.?, src_path);
62936287 }
62946288
62956289 var error_bundle = try wip_errors.toOwnedBundle("");
......@@ -6519,26 +6513,24 @@ fn cmdDumpZir(
65196513 var file: Zcu.File = .{
65206514 .status = .never_loaded,
65216515 .prev_status = .never_loaded,
6522 .source_loaded = false,
6523 .tree_loaded = false,
6524 .zir_loaded = true,
65256516 .sub_file_path = undefined,
6526 .source = undefined,
65276517 .stat = undefined,
6528 .tree = undefined,
6518 .source = null,
6519 .tree = null,
65296520 .zir = try Zcu.loadZirCache(gpa, f),
6521 .zoir = null,
65306522 .mod = undefined,
65316523 };
6532 defer file.zir.deinit(gpa);
6524 defer file.zir.?.deinit(gpa);
65336525
65346526 {
6535 const instruction_bytes = file.zir.instructions.len *
6527 const instruction_bytes = file.zir.?.instructions.len *
65366528 // Here we don't use @sizeOf(Zir.Inst.Data) because it would include
65376529 // the debug safety tag but we want to measure release size.
65386530 (@sizeOf(Zir.Inst.Tag) + 8);
6539 const extra_bytes = file.zir.extra.len * @sizeOf(u32);
6531 const extra_bytes = file.zir.?.extra.len * @sizeOf(u32);
65406532 const total_bytes = @sizeOf(Zir) + instruction_bytes + extra_bytes +
6541 file.zir.string_bytes.len * @sizeOf(u8);
6533 file.zir.?.string_bytes.len * @sizeOf(u8);
65426534 const stdout = io.getStdOut();
65436535 const fmtIntSizeBin = std.fmt.fmtIntSizeBin;
65446536 // zig fmt: off
......@@ -6550,9 +6542,9 @@ fn cmdDumpZir(
65506542 \\
65516543 , .{
65526544 fmtIntSizeBin(total_bytes),
6553 file.zir.instructions.len, fmtIntSizeBin(instruction_bytes),
6554 fmtIntSizeBin(file.zir.string_bytes.len),
6555 file.zir.extra.len, fmtIntSizeBin(extra_bytes),
6545 file.zir.?.instructions.len, fmtIntSizeBin(instruction_bytes),
6546 fmtIntSizeBin(file.zir.?.string_bytes.len),
6547 file.zir.?.extra.len, fmtIntSizeBin(extra_bytes),
65566548 });
65576549 // zig fmt: on
65586550 }
......@@ -6587,18 +6579,16 @@ fn cmdChangelist(
65876579 var file: Zcu.File = .{
65886580 .status = .never_loaded,
65896581 .prev_status = .never_loaded,
6590 .source_loaded = false,
6591 .tree_loaded = false,
6592 .zir_loaded = false,
65936582 .sub_file_path = old_source_file,
6594 .source = undefined,
65956583 .stat = .{
65966584 .size = stat.size,
65976585 .inode = stat.inode,
65986586 .mtime = stat.mtime,
65996587 },
6600 .tree = undefined,
6601 .zir = undefined,
6588 .source = null,
6589 .tree = null,
6590 .zir = null,
6591 .zoir = null,
66026592 .mod = undefined,
66036593 };
66046594
......@@ -6613,17 +6603,14 @@ fn cmdChangelist(
66136603 if (amt != stat.size)
66146604 return error.UnexpectedEndOfFile;
66156605 file.source = source;
6616 file.source_loaded = true;
66176606
6618 file.tree = try Ast.parse(gpa, file.source, .zig);
6619 file.tree_loaded = true;
6620 defer file.tree.deinit(gpa);
6607 file.tree = try Ast.parse(gpa, file.source.?, .zig);
6608 defer file.tree.?.deinit(gpa);
66216609
6622 file.zir = try AstGen.generate(gpa, file.tree);
6623 file.zir_loaded = true;
6624 defer file.zir.deinit(gpa);
6610 file.zir = try AstGen.generate(gpa, file.tree.?);
6611 defer file.zir.?.deinit(gpa);
66256612
6626 if (file.zir.loweringFailed()) {
6613 if (file.zir.?.loweringFailed()) {
66276614 var wip_errors: std.zig.ErrorBundle.Wip = undefined;
66286615 try wip_errors.init(gpa);
66296616 defer wip_errors.deinit();
......@@ -6652,13 +6639,12 @@ fn cmdChangelist(
66526639 var new_tree = try Ast.parse(gpa, new_source, .zig);
66536640 defer new_tree.deinit(gpa);
66546641
6655 var old_zir = file.zir;
6642 var old_zir = file.zir.?;
66566643 defer old_zir.deinit(gpa);
6657 file.zir_loaded = false;
6644 file.zir = null;
66586645 file.zir = try AstGen.generate(gpa, new_tree);
6659 file.zir_loaded = true;
66606646
6661 if (file.zir.loweringFailed()) {
6647 if (file.zir.?.loweringFailed()) {
66626648 var wip_errors: std.zig.ErrorBundle.Wip = undefined;
66636649 try wip_errors.init(gpa);
66646650 defer wip_errors.deinit();
......@@ -6672,7 +6658,7 @@ fn cmdChangelist(
66726658 var inst_map: std.AutoHashMapUnmanaged(Zir.Inst.Index, Zir.Inst.Index) = .empty;
66736659 defer inst_map.deinit(gpa);
66746660
6675 try Zcu.mapOldZirToNew(gpa, old_zir, file.zir, &inst_map);
6661 try Zcu.mapOldZirToNew(gpa, old_zir, file.zir.?, &inst_map);
66766662
66776663 var bw = io.bufferedWriter(io.getStdOut().writer());
66786664 const stdout = bw.writer();
src/print_zir.zig+10-13
......@@ -22,7 +22,7 @@ pub fn renderAsTextToFile(
2222 .gpa = gpa,
2323 .arena = arena.allocator(),
2424 .file = scope_file,
25 .code = scope_file.zir,
25 .code = scope_file.zir.?,
2626 .indent = 0,
2727 .parent_decl_node = 0,
2828 .recurse_decls = true,
......@@ -36,18 +36,18 @@ pub fn renderAsTextToFile(
3636 try stream.print("%{d} ", .{@intFromEnum(main_struct_inst)});
3737 try writer.writeInstToStream(stream, main_struct_inst);
3838 try stream.writeAll("\n");
39 const imports_index = scope_file.zir.extra[@intFromEnum(Zir.ExtraIndex.imports)];
39 const imports_index = scope_file.zir.?.extra[@intFromEnum(Zir.ExtraIndex.imports)];
4040 if (imports_index != 0) {
4141 try stream.writeAll("Imports:\n");
4242
43 const extra = scope_file.zir.extraData(Zir.Inst.Imports, imports_index);
43 const extra = scope_file.zir.?.extraData(Zir.Inst.Imports, imports_index);
4444 var extra_index = extra.end;
4545
4646 for (0..extra.data.imports_len) |_| {
47 const item = scope_file.zir.extraData(Zir.Inst.Imports.Item, extra_index);
47 const item = scope_file.zir.?.extraData(Zir.Inst.Imports.Item, extra_index);
4848 extra_index = item.end;
4949
50 const import_path = scope_file.zir.nullTerminatedString(item.data.name);
50 const import_path = scope_file.zir.?.nullTerminatedString(item.data.name);
5151 try stream.print(" @import(\"{}\") ", .{
5252 std.zig.fmtEscapes(import_path),
5353 });
......@@ -75,7 +75,7 @@ pub fn renderInstructionContext(
7575 .gpa = gpa,
7676 .arena = arena.allocator(),
7777 .file = scope_file,
78 .code = scope_file.zir,
78 .code = scope_file.zir.?,
7979 .indent = if (indent < 2) 2 else indent,
8080 .parent_decl_node = parent_decl_node,
8181 .recurse_decls = false,
......@@ -107,7 +107,7 @@ pub fn renderSingleInstruction(
107107 .gpa = gpa,
108108 .arena = arena.allocator(),
109109 .file = scope_file,
110 .code = scope_file.zir,
110 .code = scope_file.zir.?,
111111 .indent = indent,
112112 .parent_decl_node = parent_decl_node,
113113 .recurse_decls = false,
......@@ -2759,8 +2759,7 @@ const Writer = struct {
27592759 }
27602760
27612761 fn writeSrcNode(self: *Writer, stream: anytype, src_node: i32) !void {
2762 if (!self.file.tree_loaded) return;
2763 const tree = self.file.tree;
2762 const tree = self.file.tree orelse return;
27642763 const abs_node = self.relativeToNodeIndex(src_node);
27652764 const src_span = tree.nodeToSpan(abs_node);
27662765 const start = self.line_col_cursor.find(tree.source, src_span.start);
......@@ -2772,8 +2771,7 @@ const Writer = struct {
27722771 }
27732772
27742773 fn writeSrcTok(self: *Writer, stream: anytype, src_tok: u32) !void {
2775 if (!self.file.tree_loaded) return;
2776 const tree = self.file.tree;
2774 const tree = self.file.tree orelse return;
27772775 const abs_tok = tree.firstToken(self.parent_decl_node) + src_tok;
27782776 const span_start = tree.tokens.items(.start)[abs_tok];
27792777 const span_end = span_start + @as(u32, @intCast(tree.tokenSlice(abs_tok).len));
......@@ -2786,8 +2784,7 @@ const Writer = struct {
27862784 }
27872785
27882786 fn writeSrcTokAbs(self: *Writer, stream: anytype, src_tok: u32) !void {
2789 if (!self.file.tree_loaded) return;
2790 const tree = self.file.tree;
2787 const tree = self.file.tree orelse return;
27912788 const span_start = tree.tokens.items(.start)[src_tok];
27922789 const span_end = span_start + @as(u32, @intCast(tree.tokenSlice(src_tok).len));
27932790 const start = self.line_col_cursor.find(tree.source, span_start);