authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-02 16:25:15-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-10-02 16:25:15-04:00
logac52e005640e9dc7829356f857a82b0bc3894245
treeac2b5258254af857546337ccf63eabf36f036cdb
parent4916e26be434309209585a7c8a7918ed58c79466
parent1d2c3af90627fd2b5ffdd3a9c5f96fd73dddb2d5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9873 from SpexGuy/fix-struct-namespaces

Stage 2: A bunch of cleaning up

14 files changed, 1794 insertions(+), 1869 deletions(-)

src/Compilation.zig+13-13
......@@ -54,7 +54,7 @@ c_object_work_queue: std.fifo.LinearFifo(*CObject, .Dynamic),
5454/// These jobs are to tokenize, parse, and astgen files, which may be outdated
5555/// since the last compilation, as well as scan for `@import` and queue up
5656/// additional jobs corresponding to those new files.
57astgen_work_queue: std.fifo.LinearFifo(*Module.Scope.File, .Dynamic),
57astgen_work_queue: std.fifo.LinearFifo(*Module.File, .Dynamic),
5858
5959/// The ErrorMsg memory is owned by the `CObject`, using Compilation's general purpose allocator.
6060/// This data is accessed by multiple threads and is protected by `mutex`.
......@@ -446,7 +446,7 @@ pub const AllErrors = struct {
446446 pub fn addZir(
447447 arena: *Allocator,
448448 errors: *std.ArrayList(Message),
449 file: *Module.Scope.File,
449 file: *Module.File,
450450 ) !void {
451451 assert(file.zir_loaded);
452452 assert(file.tree_loaded);
......@@ -1444,7 +1444,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14441444 .emit_docs = options.emit_docs,
14451445 .work_queue = std.fifo.LinearFifo(Job, .Dynamic).init(gpa),
14461446 .c_object_work_queue = std.fifo.LinearFifo(*CObject, .Dynamic).init(gpa),
1447 .astgen_work_queue = std.fifo.LinearFifo(*Module.Scope.File, .Dynamic).init(gpa),
1447 .astgen_work_queue = std.fifo.LinearFifo(*Module.File, .Dynamic).init(gpa),
14481448 .keep_source_files_loaded = options.keep_source_files_loaded,
14491449 .use_clang = use_clang,
14501450 .clang_argv = options.clang_argv,
......@@ -1770,7 +1770,7 @@ pub fn update(self: *Compilation) !void {
17701770 assert(decl.deletion_flag);
17711771 assert(decl.dependants.count() == 0);
17721772 const is_anon = if (decl.zir_decl_index == 0) blk: {
1773 break :blk decl.namespace.anon_decls.swapRemove(decl);
1773 break :blk decl.src_namespace.anon_decls.swapRemove(decl);
17741774 } else false;
17751775
17761776 try module.clearDecl(decl, null);
......@@ -1889,13 +1889,13 @@ pub fn totalErrorCount(self: *Compilation) usize {
18891889 // the previous parse success, including compile errors, but we cannot
18901890 // emit them until the file succeeds parsing.
18911891 for (module.failed_decls.keys()) |key| {
1892 if (key.namespace.file_scope.okToReportErrors()) {
1892 if (key.getFileScope().okToReportErrors()) {
18931893 total += 1;
18941894 }
18951895 }
18961896 if (module.emit_h) |emit_h| {
18971897 for (emit_h.failed_decls.keys()) |key| {
1898 if (key.namespace.file_scope.okToReportErrors()) {
1898 if (key.getFileScope().okToReportErrors()) {
18991899 total += 1;
19001900 }
19011901 }
......@@ -1968,7 +1968,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
19681968 while (it.next()) |entry| {
19691969 // Skip errors for Decls within files that had a parse failure.
19701970 // We'll try again once parsing succeeds.
1971 if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) {
1971 if (entry.key_ptr.*.getFileScope().okToReportErrors()) {
19721972 try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);
19731973 }
19741974 }
......@@ -1978,7 +1978,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
19781978 while (it.next()) |entry| {
19791979 // Skip errors for Decls within files that had a parse failure.
19801980 // We'll try again once parsing succeeds.
1981 if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) {
1981 if (entry.key_ptr.*.getFileScope().okToReportErrors()) {
19821982 try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);
19831983 }
19841984 }
......@@ -2169,12 +2169,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
21692169 defer air.deinit(gpa);
21702170
21712171 log.debug("analyze liveness of {s}", .{decl.name});
2172 var liveness = try Liveness.analyze(gpa, air, decl.namespace.file_scope.zir);
2172 var liveness = try Liveness.analyze(gpa, air, decl.getFileScope().zir);
21732173 defer liveness.deinit(gpa);
21742174
21752175 if (builtin.mode == .Debug and self.verbose_air) {
21762176 std.debug.print("# Begin Function AIR: {s}:\n", .{decl.name});
2177 @import("print_air.zig").dump(gpa, air, decl.namespace.file_scope.zir, liveness);
2177 @import("print_air.zig").dump(gpa, air, decl.getFileScope().zir, liveness);
21782178 std.debug.print("# End Function AIR: {s}\n\n", .{decl.name});
21792179 }
21802180
......@@ -2465,14 +2465,14 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
24652465const AstGenSrc = union(enum) {
24662466 root,
24672467 import: struct {
2468 importing_file: *Module.Scope.File,
2468 importing_file: *Module.File,
24692469 import_tok: std.zig.Ast.TokenIndex,
24702470 },
24712471};
24722472
24732473fn workerAstGenFile(
24742474 comp: *Compilation,
2475 file: *Module.Scope.File,
2475 file: *Module.File,
24762476 prog_node: *std.Progress.Node,
24772477 wg: *WaitGroup,
24782478 src: AstGenSrc,
......@@ -2742,7 +2742,7 @@ fn reportRetryableCObjectError(
27422742fn reportRetryableAstGenError(
27432743 comp: *Compilation,
27442744 src: AstGenSrc,
2745 file: *Module.Scope.File,
2745 file: *Module.File,
27462746 err: anyerror,
27472747) error{OutOfMemory}!void {
27482748 const mod = comp.bin_file.options.module.?;
src/Module.zig+346-869
......@@ -59,7 +59,7 @@ export_owners: std.AutoArrayHashMapUnmanaged(*Decl, []*Export) = .{},
5959/// over it and check which source files have been modified on the file system when
6060/// an update is requested, as well as to cache `@import` results.
6161/// Keys are fully resolved file paths. This table owns the keys and values.
62import_table: std.StringArrayHashMapUnmanaged(*Scope.File) = .{},
62import_table: std.StringArrayHashMapUnmanaged(*File) = .{},
6363
6464/// The set of all the generic function instantiations. This is used so that when a generic
6565/// function is called twice with the same comptime parameter arguments, both calls dispatch
......@@ -85,8 +85,8 @@ failed_decls: std.AutoArrayHashMapUnmanaged(*Decl, *ErrorMsg) = .{},
8585/// The value is the AST node index offset from the Decl.
8686compile_log_decls: std.AutoArrayHashMapUnmanaged(*Decl, i32) = .{},
8787/// Using a map here for consistency with the other fields here.
88/// The ErrorMsg memory is owned by the `Scope.File`, using Module's general purpose allocator.
89failed_files: std.AutoArrayHashMapUnmanaged(*Scope.File, ?*ErrorMsg) = .{},
88/// The ErrorMsg memory is owned by the `File`, using Module's general purpose allocator.
89failed_files: std.AutoArrayHashMapUnmanaged(*File, ?*ErrorMsg) = .{},
9090/// Using a map here for consistency with the other fields here.
9191/// The ErrorMsg memory is owned by the `Export`, using Module's general purpose allocator.
9292failed_exports: std.AutoArrayHashMapUnmanaged(*Export, *ErrorMsg) = .{},
......@@ -264,7 +264,7 @@ pub const Export = struct {
264264
265265 pub fn getSrcLoc(exp: Export) SrcLoc {
266266 return .{
267 .file_scope = exp.src_decl.namespace.file_scope,
267 .file_scope = exp.src_decl.getFileScope(),
268268 .parent_decl_node = exp.src_decl.src_node,
269269 .lazy = exp.src,
270270 };
......@@ -350,7 +350,7 @@ pub const Decl = struct {
350350 /// Reference to externally owned memory.
351351 /// In the case of the Decl corresponding to a file, this is
352352 /// the namespace of the struct, since there is no parent.
353 namespace: *Scope.Namespace,
353 src_namespace: *Namespace,
354354
355355 /// The scope which lexically contains this decl. A decl must depend
356356 /// on its lexical parent, in order to ensure that this pointer is valid.
......@@ -516,7 +516,7 @@ pub const Decl = struct {
516516 /// This name is relative to the containing namespace of the decl.
517517 /// The memory is owned by the containing File ZIR.
518518 pub fn getName(decl: Decl) ?[:0]const u8 {
519 const zir = decl.namespace.file_scope.zir;
519 const zir = decl.getFileScope().zir;
520520 return decl.getNameZir(zir);
521521 }
522522
......@@ -528,7 +528,7 @@ pub const Decl = struct {
528528 }
529529
530530 pub fn contentsHash(decl: Decl) std.zig.SrcHash {
531 const zir = decl.namespace.file_scope.zir;
531 const zir = decl.getFileScope().zir;
532532 return decl.contentsHashZir(zir);
533533 }
534534
......@@ -541,21 +541,21 @@ pub const Decl = struct {
541541
542542 pub fn zirBlockIndex(decl: *const Decl) Zir.Inst.Index {
543543 assert(decl.zir_decl_index != 0);
544 const zir = decl.namespace.file_scope.zir;
544 const zir = decl.getFileScope().zir;
545545 return zir.extra[decl.zir_decl_index + 6];
546546 }
547547
548548 pub fn zirAlignRef(decl: Decl) Zir.Inst.Ref {
549549 if (!decl.has_align) return .none;
550550 assert(decl.zir_decl_index != 0);
551 const zir = decl.namespace.file_scope.zir;
551 const zir = decl.getFileScope().zir;
552552 return @intToEnum(Zir.Inst.Ref, zir.extra[decl.zir_decl_index + 7]);
553553 }
554554
555555 pub fn zirLinksectionRef(decl: Decl) Zir.Inst.Ref {
556556 if (!decl.has_linksection_or_addrspace) return .none;
557557 assert(decl.zir_decl_index != 0);
558 const zir = decl.namespace.file_scope.zir;
558 const zir = decl.getFileScope().zir;
559559 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align);
560560 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
561561 }
......@@ -563,16 +563,16 @@ pub const Decl = struct {
563563 pub fn zirAddrspaceRef(decl: Decl) Zir.Inst.Ref {
564564 if (!decl.has_linksection_or_addrspace) return .none;
565565 assert(decl.zir_decl_index != 0);
566 const zir = decl.namespace.file_scope.zir;
566 const zir = decl.getFileScope().zir;
567567 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align) + 1;
568568 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
569569 }
570570
571571 /// Returns true if and only if the Decl is the top level struct associated with a File.
572572 pub fn isRoot(decl: *const Decl) bool {
573 if (decl.namespace.parent != null)
573 if (decl.src_namespace.parent != null)
574574 return false;
575 return decl == decl.namespace.ty.getOwnerDecl();
575 return decl == decl.src_namespace.getDecl();
576576 }
577577
578578 pub fn relativeToLine(decl: Decl, offset: u32) u32 {
......@@ -608,18 +608,23 @@ pub const Decl = struct {
608608 }
609609
610610 pub fn srcToken(decl: Decl) Ast.TokenIndex {
611 const tree = &decl.namespace.file_scope.tree;
611 const tree = &decl.getFileScope().tree;
612612 return tree.firstToken(decl.src_node);
613613 }
614614
615615 pub fn srcByteOffset(decl: Decl) u32 {
616 const tree = &decl.namespace.file_scope.tree;
616 const tree = &decl.getFileScope().tree;
617617 return tree.tokens.items(.start)[decl.srcToken()];
618618 }
619619
620620 pub fn renderFullyQualifiedName(decl: Decl, writer: anytype) !void {
621621 const unqualified_name = mem.spanZ(decl.name);
622 return decl.namespace.renderFullyQualifiedName(unqualified_name, writer);
622 return decl.src_namespace.renderFullyQualifiedName(unqualified_name, writer);
623 }
624
625 pub fn renderFullyQualifiedDebugName(decl: Decl, writer: anytype) !void {
626 const unqualified_name = mem.spanZ(decl.name);
627 return decl.src_namespace.renderFullyQualifiedDebugName(unqualified_name, writer);
623628 }
624629
625630 pub fn getFullyQualifiedName(decl: Decl, gpa: *Allocator) ![:0]u8 {
......@@ -685,7 +690,7 @@ pub const Decl = struct {
685690 /// Gets the namespace that this Decl creates by being a struct, union,
686691 /// enum, or opaque.
687692 /// Only returns it if the Decl is the owner.
688 pub fn getInnerNamespace(decl: *Decl) ?*Scope.Namespace {
693 pub fn getInnerNamespace(decl: *Decl) ?*Namespace {
689694 if (!decl.owns_tv) return null;
690695 const ty = (decl.val.castTag(.ty) orelse return null).data;
691696 switch (ty.tag()) {
......@@ -730,8 +735,8 @@ pub const Decl = struct {
730735 std.debug.print("\n", .{});
731736 }
732737
733 pub fn getFileScope(decl: Decl) *Scope.File {
734 return decl.namespace.file_scope;
738 pub fn getFileScope(decl: Decl) *File {
739 return decl.src_namespace.file_scope;
735740 }
736741
737742 pub fn getEmitH(decl: *Decl, module: *Module) *EmitH {
......@@ -782,7 +787,7 @@ pub const Struct = struct {
782787 /// Set of field names in declaration order.
783788 fields: std.StringArrayHashMapUnmanaged(Field),
784789 /// Represents the declarations inside this struct.
785 namespace: Scope.Namespace,
790 namespace: Namespace,
786791 /// Offset from `owner_decl`, points to the struct AST node.
787792 node_offset: i32,
788793 /// Index of the struct_decl ZIR instruction.
......@@ -904,7 +909,7 @@ pub const EnumFull = struct {
904909 /// If this hash map is empty, it means the enum tags are auto-numbered.
905910 values: ValueMap,
906911 /// Represents the declarations inside this enum.
907 namespace: Scope.Namespace,
912 namespace: Namespace,
908913 /// Offset from `owner_decl`, points to the enum decl AST node.
909914 node_offset: i32,
910915
......@@ -932,7 +937,7 @@ pub const Union = struct {
932937 /// Set of field names in declaration order.
933938 fields: std.StringArrayHashMapUnmanaged(Field),
934939 /// Represents the declarations inside this union.
935 namespace: Scope.Namespace,
940 namespace: Namespace,
936941 /// Offset from `owner_decl`, points to the union decl AST node.
937942 node_offset: i32,
938943 /// Index of the union_decl ZIR instruction.
......@@ -1076,636 +1081,312 @@ pub const Var = struct {
10761081 is_threadlocal: bool,
10771082};
10781083
1079pub const Scope = struct {
1080 tag: Tag,
1081
1082 pub fn cast(base: *Scope, comptime T: type) ?*T {
1083 if (base.tag != T.base_tag)
1084 return null;
1084/// The container that structs, enums, unions, and opaques have.
1085pub const Namespace = struct {
1086 parent: ?*Namespace,
1087 file_scope: *File,
1088 /// Will be a struct, enum, union, or opaque.
1089 ty: Type,
1090 /// Direct children of the namespace. Used during an update to detect
1091 /// which decls have been added/removed from source.
1092 /// Declaration order is preserved via entry order.
1093 /// Key memory is owned by `decl.name`.
1094 /// TODO save memory with https://github.com/ziglang/zig/issues/8619.
1095 /// Anonymous decls are not stored here; they are kept in `anon_decls` instead.
1096 decls: std.StringArrayHashMapUnmanaged(*Decl) = .{},
10851097
1086 return @fieldParentPtr(T, "base", base);
1087 }
1098 anon_decls: std.AutoArrayHashMapUnmanaged(*Decl, void) = .{},
10881099
1089 /// Get the decl which contains this decl, for the purposes of source reporting
1090 pub fn srcDecl(scope: *Scope) ?*Decl {
1091 return switch (scope.tag) {
1092 .block => scope.cast(Block).?.src_decl,
1093 .file => null,
1094 .namespace => scope.cast(Namespace).?.getDecl(),
1095 };
1096 }
1100 /// Key is usingnamespace Decl itself. To find the namespace being included,
1101 /// the Decl Value has to be resolved as a Type which has a Namespace.
1102 /// Value is whether the usingnamespace decl is marked `pub`.
1103 usingnamespace_set: std.AutoHashMapUnmanaged(*Decl, bool) = .{},
10971104
1098 /// Get the scope which contains this decl, for resolving closure_get instructions.
1099 pub fn srcScope(scope: *Scope) ?*CaptureScope {
1100 return switch (scope.tag) {
1101 .block => scope.cast(Block).?.wip_capture_scope,
1102 .file => null,
1103 .namespace => scope.cast(Namespace).?.getDecl().src_scope,
1104 };
1105 pub fn deinit(ns: *Namespace, mod: *Module) void {
1106 ns.destroyDecls(mod);
1107 ns.* = undefined;
11051108 }
11061109
1107 /// Asserts the scope has a parent which is a Namespace and returns it.
1108 pub fn namespace(scope: *Scope) *Namespace {
1109 switch (scope.tag) {
1110 .block => return scope.cast(Block).?.sema.owner_decl.namespace,
1111 .file => return scope.cast(File).?.root_decl.?.namespace,
1112 .namespace => return scope.cast(Namespace).?,
1113 }
1114 }
1110 pub fn destroyDecls(ns: *Namespace, mod: *Module) void {
1111 const gpa = mod.gpa;
11151112
1116 /// Asserts the scope has a parent which is a Namespace or File and
1117 /// returns the sub_file_path field.
1118 pub fn subFilePath(base: *Scope) []const u8 {
1119 switch (base.tag) {
1120 .namespace => return @fieldParentPtr(Namespace, "base", base).file_scope.sub_file_path,
1121 .file => return @fieldParentPtr(File, "base", base).sub_file_path,
1122 .block => unreachable,
1123 }
1124 }
1113 log.debug("destroyDecls {*}", .{ns});
11251114
1126 /// When called from inside a Block Scope, chases the src_decl, not the owner_decl.
1127 pub fn getFileScope(base: *Scope) *Scope.File {
1128 var cur = base;
1129 while (true) {
1130 cur = switch (cur.tag) {
1131 .namespace => return @fieldParentPtr(Namespace, "base", cur).file_scope,
1132 .file => return @fieldParentPtr(File, "base", cur),
1133 .block => return @fieldParentPtr(Block, "base", cur).src_decl.namespace.file_scope,
1134 };
1135 }
1136 }
1115 var decls = ns.decls;
1116 ns.decls = .{};
11371117
1138 pub const Tag = enum {
1139 /// .zig source code.
1140 file,
1141 /// Namespace owned by structs, enums, unions, and opaques for decls.
1142 namespace,
1143 block,
1144 };
1118 var anon_decls = ns.anon_decls;
1119 ns.anon_decls = .{};
11451120
1146 /// The container that structs, enums, unions, and opaques have.
1147 pub const Namespace = struct {
1148 pub const base_tag: Tag = .namespace;
1149 base: Scope = Scope{ .tag = base_tag },
1150
1151 parent: ?*Namespace,
1152 file_scope: *Scope.File,
1153 /// Will be a struct, enum, union, or opaque.
1154 ty: Type,
1155 /// Direct children of the namespace. Used during an update to detect
1156 /// which decls have been added/removed from source.
1157 /// Declaration order is preserved via entry order.
1158 /// Key memory is owned by `decl.name`.
1159 /// TODO save memory with https://github.com/ziglang/zig/issues/8619.
1160 /// Anonymous decls are not stored here; they are kept in `anon_decls` instead.
1161 decls: std.StringArrayHashMapUnmanaged(*Decl) = .{},
1162
1163 anon_decls: std.AutoArrayHashMapUnmanaged(*Decl, void) = .{},
1164
1165 /// Key is usingnamespace Decl itself. To find the namespace being included,
1166 /// the Decl Value has to be resolved as a Type which has a Namespace.
1167 /// Value is whether the usingnamespace decl is marked `pub`.
1168 usingnamespace_set: std.AutoHashMapUnmanaged(*Decl, bool) = .{},
1169
1170 pub fn deinit(ns: *Namespace, mod: *Module) void {
1171 ns.destroyDecls(mod);
1172 ns.* = undefined;
1121 for (decls.values()) |value| {
1122 value.destroy(mod);
11731123 }
1124 decls.deinit(gpa);
11741125
1175 pub fn destroyDecls(ns: *Namespace, mod: *Module) void {
1176 const gpa = mod.gpa;
1177
1178 log.debug("destroyDecls {*}", .{ns});
1179
1180 var decls = ns.decls;
1181 ns.decls = .{};
1182
1183 var anon_decls = ns.anon_decls;
1184 ns.anon_decls = .{};
1185
1186 for (decls.values()) |value| {
1187 value.destroy(mod);
1188 }
1189 decls.deinit(gpa);
1190
1191 for (anon_decls.keys()) |key| {
1192 key.destroy(mod);
1193 }
1194 anon_decls.deinit(gpa);
1126 for (anon_decls.keys()) |key| {
1127 key.destroy(mod);
11951128 }
1129 anon_decls.deinit(gpa);
1130 }
11961131
1197 pub fn deleteAllDecls(
1198 ns: *Namespace,
1199 mod: *Module,
1200 outdated_decls: ?*std.AutoArrayHashMap(*Decl, void),
1201 ) !void {
1202 const gpa = mod.gpa;
1203
1204 log.debug("deleteAllDecls {*}", .{ns});
1132 pub fn deleteAllDecls(
1133 ns: *Namespace,
1134 mod: *Module,
1135 outdated_decls: ?*std.AutoArrayHashMap(*Decl, void),
1136 ) !void {
1137 const gpa = mod.gpa;
12051138
1206 var decls = ns.decls;
1207 ns.decls = .{};
1139 log.debug("deleteAllDecls {*}", .{ns});
12081140
1209 var anon_decls = ns.anon_decls;
1210 ns.anon_decls = .{};
1141 var decls = ns.decls;
1142 ns.decls = .{};
12111143
1212 // TODO rework this code to not panic on OOM.
1213 // (might want to coordinate with the clearDecl function)
1144 var anon_decls = ns.anon_decls;
1145 ns.anon_decls = .{};
12141146
1215 for (decls.values()) |child_decl| {
1216 mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory");
1217 child_decl.destroy(mod);
1218 }
1219 decls.deinit(gpa);
1147 // TODO rework this code to not panic on OOM.
1148 // (might want to coordinate with the clearDecl function)
12201149
1221 for (anon_decls.keys()) |child_decl| {
1222 mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory");
1223 child_decl.destroy(mod);
1224 }
1225 anon_decls.deinit(gpa);
1150 for (decls.values()) |child_decl| {
1151 mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory");
1152 child_decl.destroy(mod);
12261153 }
1154 decls.deinit(gpa);
12271155
1228 // This renders e.g. "std.fs.Dir.OpenOptions"
1229 pub fn renderFullyQualifiedName(
1230 ns: Namespace,
1231 name: []const u8,
1232 writer: anytype,
1233 ) @TypeOf(writer).Error!void {
1234 if (ns.parent) |parent| {
1235 const decl = ns.getDecl();
1236 try parent.renderFullyQualifiedName(mem.spanZ(decl.name), writer);
1237 } else {
1238 try ns.file_scope.renderFullyQualifiedName(writer);
1239 }
1240 if (name.len != 0) {
1241 try writer.writeAll(".");
1242 try writer.writeAll(name);
1243 }
1156 for (anon_decls.keys()) |child_decl| {
1157 mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory");
1158 child_decl.destroy(mod);
12441159 }
1245
1246 pub fn getDecl(ns: Namespace) *Decl {
1247 return ns.ty.getOwnerDecl();
1160 anon_decls.deinit(gpa);
1161 }
1162
1163 // This renders e.g. "std.fs.Dir.OpenOptions"
1164 pub fn renderFullyQualifiedName(
1165 ns: Namespace,
1166 name: []const u8,
1167 writer: anytype,
1168 ) @TypeOf(writer).Error!void {
1169 if (ns.parent) |parent| {
1170 const decl = ns.getDecl();
1171 try parent.renderFullyQualifiedName(mem.spanZ(decl.name), writer);
1172 } else {
1173 try ns.file_scope.renderFullyQualifiedName(writer);
12481174 }
1249 };
1250
1251 pub const File = struct {
1252 pub const base_tag: Tag = .file;
1253 base: Scope = Scope{ .tag = base_tag },
1254 status: enum {
1255 never_loaded,
1256 retryable_failure,
1257 parse_failure,
1258 astgen_failure,
1259 success_zir,
1260 },
1261 source_loaded: bool,
1262 tree_loaded: bool,
1263 zir_loaded: bool,
1264 /// Relative to the owning package's root_src_dir.
1265 /// Memory is stored in gpa, owned by File.
1266 sub_file_path: []const u8,
1267 /// Whether this is populated depends on `source_loaded`.
1268 source: [:0]const u8,
1269 /// Whether this is populated depends on `status`.
1270 stat_size: u64,
1271 /// Whether this is populated depends on `status`.
1272 stat_inode: std.fs.File.INode,
1273 /// Whether this is populated depends on `status`.
1274 stat_mtime: i128,
1275 /// Whether this is populated or not depends on `tree_loaded`.
1276 tree: Ast,
1277 /// Whether this is populated or not depends on `zir_loaded`.
1278 zir: Zir,
1279 /// Package that this file is a part of, managed externally.
1280 pkg: *Package,
1281 /// The Decl of the struct that represents this File.
1282 root_decl: ?*Decl,
1283
1284 /// Used by change detection algorithm, after astgen, contains the
1285 /// set of decls that existed in the previous ZIR but not in the new one.
1286 deleted_decls: std.ArrayListUnmanaged(*Decl) = .{},
1287 /// Used by change detection algorithm, after astgen, contains the
1288 /// set of decls that existed both in the previous ZIR and in the new one,
1289 /// but their source code has been modified.
1290 outdated_decls: std.ArrayListUnmanaged(*Decl) = .{},
1291
1292 /// The most recent successful ZIR for this file, with no errors.
1293 /// This is only populated when a previously successful ZIR
1294 /// newly introduces compile errors during an update. When ZIR is
1295 /// successful, this field is unloaded.
1296 prev_zir: ?*Zir = null,
1297
1298 pub fn unload(file: *File, gpa: *Allocator) void {
1299 file.unloadTree(gpa);
1300 file.unloadSource(gpa);
1301 file.unloadZir(gpa);
1175 if (name.len != 0) {
1176 try writer.writeAll(".");
1177 try writer.writeAll(name);
13021178 }
1179 }
13031180
1304 pub fn unloadTree(file: *File, gpa: *Allocator) void {
1305 if (file.tree_loaded) {
1306 file.tree_loaded = false;
1307 file.tree.deinit(gpa);
1308 }
1181 /// This renders e.g. "std/fs.zig:Dir.OpenOptions"
1182 pub fn renderFullyQualifiedDebugName(
1183 ns: Namespace,
1184 name: []const u8,
1185 writer: anytype,
1186 ) @TypeOf(writer).Error!void {
1187 var separator_char: u8 = '.';
1188 if (ns.parent) |parent| {
1189 const decl = ns.getDecl();
1190 try parent.renderFullyQualifiedDebugName(mem.spanZ(decl.name), writer);
1191 } else {
1192 try ns.file_scope.renderFullyQualifiedDebugName(writer);
1193 separator_char = ':';
13091194 }
1310
1311 pub fn unloadSource(file: *File, gpa: *Allocator) void {
1312 if (file.source_loaded) {
1313 file.source_loaded = false;
1314 gpa.free(file.source);
1315 }
1195 if (name.len != 0) {
1196 try writer.writeByte(separator_char);
1197 try writer.writeAll(name);
13161198 }
1199 }
13171200
1318 pub fn unloadZir(file: *File, gpa: *Allocator) void {
1319 if (file.zir_loaded) {
1320 file.zir_loaded = false;
1321 file.zir.deinit(gpa);
1322 }
1323 }
1201 pub fn getDecl(ns: Namespace) *Decl {
1202 return ns.ty.getOwnerDecl();
1203 }
1204};
13241205
1325 pub fn deinit(file: *File, mod: *Module) void {
1326 const gpa = mod.gpa;
1327 log.debug("deinit File {s}", .{file.sub_file_path});
1328 file.deleted_decls.deinit(gpa);
1329 file.outdated_decls.deinit(gpa);
1330 if (file.root_decl) |root_decl| {
1331 root_decl.destroy(mod);
1332 }
1333 gpa.free(file.sub_file_path);
1334 file.unload(gpa);
1335 if (file.prev_zir) |prev_zir| {
1336 prev_zir.deinit(gpa);
1337 gpa.destroy(prev_zir);
1338 }
1339 file.* = undefined;
1206pub const File = struct {
1207 status: enum {
1208 never_loaded,
1209 retryable_failure,
1210 parse_failure,
1211 astgen_failure,
1212 success_zir,
1213 },
1214 source_loaded: bool,
1215 tree_loaded: bool,
1216 zir_loaded: bool,
1217 /// Relative to the owning package's root_src_dir.
1218 /// Memory is stored in gpa, owned by File.
1219 sub_file_path: []const u8,
1220 /// Whether this is populated depends on `source_loaded`.
1221 source: [:0]const u8,
1222 /// Whether this is populated depends on `status`.
1223 stat_size: u64,
1224 /// Whether this is populated depends on `status`.
1225 stat_inode: std.fs.File.INode,
1226 /// Whether this is populated depends on `status`.
1227 stat_mtime: i128,
1228 /// Whether this is populated or not depends on `tree_loaded`.
1229 tree: Ast,
1230 /// Whether this is populated or not depends on `zir_loaded`.
1231 zir: Zir,
1232 /// Package that this file is a part of, managed externally.
1233 pkg: *Package,
1234 /// The Decl of the struct that represents this File.
1235 root_decl: ?*Decl,
1236
1237 /// Used by change detection algorithm, after astgen, contains the
1238 /// set of decls that existed in the previous ZIR but not in the new one.
1239 deleted_decls: std.ArrayListUnmanaged(*Decl) = .{},
1240 /// Used by change detection algorithm, after astgen, contains the
1241 /// set of decls that existed both in the previous ZIR and in the new one,
1242 /// but their source code has been modified.
1243 outdated_decls: std.ArrayListUnmanaged(*Decl) = .{},
1244
1245 /// The most recent successful ZIR for this file, with no errors.
1246 /// This is only populated when a previously successful ZIR
1247 /// newly introduces compile errors during an update. When ZIR is
1248 /// successful, this field is unloaded.
1249 prev_zir: ?*Zir = null,
1250
1251 pub fn unload(file: *File, gpa: *Allocator) void {
1252 file.unloadTree(gpa);
1253 file.unloadSource(gpa);
1254 file.unloadZir(gpa);
1255 }
1256
1257 pub fn unloadTree(file: *File, gpa: *Allocator) void {
1258 if (file.tree_loaded) {
1259 file.tree_loaded = false;
1260 file.tree.deinit(gpa);
13401261 }
1262 }
13411263
1342 pub fn getSource(file: *File, gpa: *Allocator) ![:0]const u8 {
1343 if (file.source_loaded) return file.source;
1344
1345 const root_dir_path = file.pkg.root_src_directory.path orelse ".";
1346 log.debug("File.getSource, not cached. pkgdir={s} sub_file_path={s}", .{
1347 root_dir_path, file.sub_file_path,
1348 });
1349
1350 // Keep track of inode, file size, mtime, hash so we can detect which files
1351 // have been modified when an incremental update is requested.
1352 var f = try file.pkg.root_src_directory.handle.openFile(file.sub_file_path, .{});
1353 defer f.close();
1354
1355 const stat = try f.stat();
1356
1357 if (stat.size > std.math.maxInt(u32))
1358 return error.FileTooBig;
1359
1360 const source = try gpa.allocSentinel(u8, @intCast(usize, stat.size), 0);
1361 defer if (!file.source_loaded) gpa.free(source);
1362 const amt = try f.readAll(source);
1363 if (amt != stat.size)
1364 return error.UnexpectedEndOfFile;
1365
1366 // Here we do not modify stat fields because this function is the one
1367 // used for error reporting. We need to keep the stat fields stale so that
1368 // astGenFile can know to regenerate ZIR.
1369
1370 file.source = source;
1371 file.source_loaded = true;
1372 return source;
1264 pub fn unloadSource(file: *File, gpa: *Allocator) void {
1265 if (file.source_loaded) {
1266 file.source_loaded = false;
1267 gpa.free(file.source);
13731268 }
1269 }
13741270
1375 pub fn getTree(file: *File, gpa: *Allocator) !*const Ast {
1376 if (file.tree_loaded) return &file.tree;
1377
1378 const source = try file.getSource(gpa);
1379 file.tree = try std.zig.parse(gpa, source);
1380 file.tree_loaded = true;
1381 return &file.tree;
1271 pub fn unloadZir(file: *File, gpa: *Allocator) void {
1272 if (file.zir_loaded) {
1273 file.zir_loaded = false;
1274 file.zir.deinit(gpa);
13821275 }
1276 }
13831277
1384 pub fn destroy(file: *File, mod: *Module) void {
1385 const gpa = mod.gpa;
1386 file.deinit(mod);
1387 gpa.destroy(file);
1278 pub fn deinit(file: *File, mod: *Module) void {
1279 const gpa = mod.gpa;
1280 log.debug("deinit File {s}", .{file.sub_file_path});
1281 file.deleted_decls.deinit(gpa);
1282 file.outdated_decls.deinit(gpa);
1283 if (file.root_decl) |root_decl| {
1284 root_decl.destroy(mod);
13881285 }
1389
1390 pub fn renderFullyQualifiedName(file: File, writer: anytype) !void {
1391 // Convert all the slashes into dots and truncate the extension.
1392 const ext = std.fs.path.extension(file.sub_file_path);
1393 const noext = file.sub_file_path[0 .. file.sub_file_path.len - ext.len];
1394 for (noext) |byte| switch (byte) {
1395 '/', '\\' => try writer.writeByte('.'),
1396 else => try writer.writeByte(byte),
1397 };
1286 gpa.free(file.sub_file_path);
1287 file.unload(gpa);
1288 if (file.prev_zir) |prev_zir| {
1289 prev_zir.deinit(gpa);
1290 gpa.destroy(prev_zir);
13981291 }
1292 file.* = undefined;
1293 }
13991294
1400 pub fn fullyQualifiedNameZ(file: File, gpa: *Allocator) ![:0]u8 {
1401 var buf = std.ArrayList(u8).init(gpa);
1402 defer buf.deinit();
1403 try file.renderFullyQualifiedName(buf.writer());
1404 return buf.toOwnedSliceSentinel(0);
1405 }
1295 pub fn getSource(file: *File, gpa: *Allocator) ![:0]const u8 {
1296 if (file.source_loaded) return file.source;
14061297
1407 /// Returns the full path to this file relative to its package.
1408 pub fn fullPath(file: File, ally: *Allocator) ![]u8 {
1409 return file.pkg.root_src_directory.join(ally, &[_][]const u8{file.sub_file_path});
1410 }
1298 const root_dir_path = file.pkg.root_src_directory.path orelse ".";
1299 log.debug("File.getSource, not cached. pkgdir={s} sub_file_path={s}", .{
1300 root_dir_path, file.sub_file_path,
1301 });
14111302
1412 pub fn dumpSrc(file: *File, src: LazySrcLoc) void {
1413 const loc = std.zig.findLineColumn(file.source.bytes, src);
1414 std.debug.print("{s}:{d}:{d}\n", .{ file.sub_file_path, loc.line + 1, loc.column + 1 });
1415 }
1303 // Keep track of inode, file size, mtime, hash so we can detect which files
1304 // have been modified when an incremental update is requested.
1305 var f = try file.pkg.root_src_directory.handle.openFile(file.sub_file_path, .{});
1306 defer f.close();
14161307
1417 pub fn okToReportErrors(file: File) bool {
1418 return switch (file.status) {
1419 .parse_failure, .astgen_failure => false,
1420 else => true,
1421 };
1422 }
1423 };
1308 const stat = try f.stat();
14241309
1425 /// This is the context needed to semantically analyze ZIR instructions and
1426 /// produce AIR instructions.
1427 /// This is a temporary structure stored on the stack; references to it are valid only
1428 /// during semantic analysis of the block.
1429 pub const Block = struct {
1430 pub const base_tag: Tag = .block;
1431
1432 base: Scope = Scope{ .tag = base_tag },
1433 parent: ?*Block,
1434 /// Shared among all child blocks.
1435 sema: *Sema,
1436 /// This Decl is the Decl according to the Zig source code corresponding to this Block.
1437 /// This can vary during inline or comptime function calls. See `Sema.owner_decl`
1438 /// for the one that will be the same for all Block instances.
1439 src_decl: *Decl,
1440 instructions: ArrayListUnmanaged(Air.Inst.Index),
1441 // `param` instructions are collected here to be used by the `func` instruction.
1442 params: std.ArrayListUnmanaged(Param) = .{},
1443
1444 wip_capture_scope: *CaptureScope,
1445
1446 label: ?*Label = null,
1447 inlining: ?*Inlining,
1448 /// If runtime_index is not 0 then one of these is guaranteed to be non null.
1449 runtime_cond: ?LazySrcLoc = null,
1450 runtime_loop: ?LazySrcLoc = null,
1451 /// Non zero if a non-inline loop or a runtime conditional have been encountered.
1452 /// Stores to to comptime variables are only allowed when var.runtime_index <= runtime_index.
1453 runtime_index: u32 = 0,
1310 if (stat.size > std.math.maxInt(u32))
1311 return error.FileTooBig;
14541312
1455 is_comptime: bool,
1313 const source = try gpa.allocSentinel(u8, @intCast(usize, stat.size), 0);
1314 defer if (!file.source_loaded) gpa.free(source);
1315 const amt = try f.readAll(source);
1316 if (amt != stat.size)
1317 return error.UnexpectedEndOfFile;
14561318
1457 /// when null, it is determined by build mode, changed by @setRuntimeSafety
1458 want_safety: ?bool = null,
1319 // Here we do not modify stat fields because this function is the one
1320 // used for error reporting. We need to keep the stat fields stale so that
1321 // astGenFile can know to regenerate ZIR.
14591322
1460 c_import_buf: ?*std.ArrayList(u8) = null,
1323 file.source = source;
1324 file.source_loaded = true;
1325 return source;
1326 }
14611327
1462 const Param = struct {
1463 /// `noreturn` means `anytype`.
1464 ty: Type,
1465 is_comptime: bool,
1466 };
1328 pub fn getTree(file: *File, gpa: *Allocator) !*const Ast {
1329 if (file.tree_loaded) return &file.tree;
14671330
1468 /// This `Block` maps a block ZIR instruction to the corresponding
1469 /// AIR instruction for break instruction analysis.
1470 pub const Label = struct {
1471 zir_block: Zir.Inst.Index,
1472 merges: Merges,
1473 };
1331 const source = try file.getSource(gpa);
1332 file.tree = try std.zig.parse(gpa, source);
1333 file.tree_loaded = true;
1334 return &file.tree;
1335 }
14741336
1475 /// This `Block` indicates that an inline function call is happening
1476 /// and return instructions should be analyzed as a break instruction
1477 /// to this AIR block instruction.
1478 /// It is shared among all the blocks in an inline or comptime called
1479 /// function.
1480 pub const Inlining = struct {
1481 comptime_result: Air.Inst.Ref,
1482 merges: Merges,
1483 };
1337 pub fn destroy(file: *File, mod: *Module) void {
1338 const gpa = mod.gpa;
1339 file.deinit(mod);
1340 gpa.destroy(file);
1341 }
14841342
1485 pub const Merges = struct {
1486 block_inst: Air.Inst.Index,
1487 /// Separate array list from break_inst_list so that it can be passed directly
1488 /// to resolvePeerTypes.
1489 results: ArrayListUnmanaged(Air.Inst.Ref),
1490 /// Keeps track of the break instructions so that the operand can be replaced
1491 /// if we need to add type coercion at the end of block analysis.
1492 /// Same indexes, capacity, length as `results`.
1493 br_list: ArrayListUnmanaged(Air.Inst.Index),
1343 pub fn renderFullyQualifiedName(file: File, writer: anytype) !void {
1344 // Convert all the slashes into dots and truncate the extension.
1345 const ext = std.fs.path.extension(file.sub_file_path);
1346 const noext = file.sub_file_path[0 .. file.sub_file_path.len - ext.len];
1347 for (noext) |byte| switch (byte) {
1348 '/', '\\' => try writer.writeByte('.'),
1349 else => try writer.writeByte(byte),
14941350 };
1351 }
14951352
1496 /// For debugging purposes.
1497 pub fn dump(block: *Block, mod: Module) void {
1498 Zir.dumpBlock(mod, block);
1499 }
1500
1501 pub fn makeSubBlock(parent: *Block) Block {
1502 return .{
1503 .parent = parent,
1504 .sema = parent.sema,
1505 .src_decl = parent.src_decl,
1506 .instructions = .{},
1507 .wip_capture_scope = parent.wip_capture_scope,
1508 .label = null,
1509 .inlining = parent.inlining,
1510 .is_comptime = parent.is_comptime,
1511 .runtime_cond = parent.runtime_cond,
1512 .runtime_loop = parent.runtime_loop,
1513 .runtime_index = parent.runtime_index,
1514 .want_safety = parent.want_safety,
1515 .c_import_buf = parent.c_import_buf,
1516 };
1517 }
1518
1519 pub fn wantSafety(block: *const Block) bool {
1520 return block.want_safety orelse switch (block.sema.mod.optimizeMode()) {
1521 .Debug => true,
1522 .ReleaseSafe => true,
1523 .ReleaseFast => false,
1524 .ReleaseSmall => false,
1525 };
1526 }
1527
1528 pub fn getFileScope(block: *Block) *Scope.File {
1529 return block.src_decl.namespace.file_scope;
1530 }
1531
1532 pub fn addTy(
1533 block: *Block,
1534 tag: Air.Inst.Tag,
1535 ty: Type,
1536 ) error{OutOfMemory}!Air.Inst.Ref {
1537 return block.addInst(.{
1538 .tag = tag,
1539 .data = .{ .ty = ty },
1540 });
1541 }
1542
1543 pub fn addTyOp(
1544 block: *Block,
1545 tag: Air.Inst.Tag,
1546 ty: Type,
1547 operand: Air.Inst.Ref,
1548 ) error{OutOfMemory}!Air.Inst.Ref {
1549 return block.addInst(.{
1550 .tag = tag,
1551 .data = .{ .ty_op = .{
1552 .ty = try block.sema.addType(ty),
1553 .operand = operand,
1554 } },
1555 });
1556 }
1557
1558 pub fn addNoOp(block: *Block, tag: Air.Inst.Tag) error{OutOfMemory}!Air.Inst.Ref {
1559 return block.addInst(.{
1560 .tag = tag,
1561 .data = .{ .no_op = {} },
1562 });
1563 }
1564
1565 pub fn addUnOp(
1566 block: *Block,
1567 tag: Air.Inst.Tag,
1568 operand: Air.Inst.Ref,
1569 ) error{OutOfMemory}!Air.Inst.Ref {
1570 return block.addInst(.{
1571 .tag = tag,
1572 .data = .{ .un_op = operand },
1573 });
1574 }
1575
1576 pub fn addBr(
1577 block: *Block,
1578 target_block: Air.Inst.Index,
1579 operand: Air.Inst.Ref,
1580 ) error{OutOfMemory}!Air.Inst.Ref {
1581 return block.addInst(.{
1582 .tag = .br,
1583 .data = .{ .br = .{
1584 .block_inst = target_block,
1585 .operand = operand,
1586 } },
1587 });
1588 }
1589
1590 pub fn addBinOp(
1591 block: *Block,
1592 tag: Air.Inst.Tag,
1593 lhs: Air.Inst.Ref,
1594 rhs: Air.Inst.Ref,
1595 ) error{OutOfMemory}!Air.Inst.Ref {
1596 return block.addInst(.{
1597 .tag = tag,
1598 .data = .{ .bin_op = .{
1599 .lhs = lhs,
1600 .rhs = rhs,
1601 } },
1602 });
1603 }
1604
1605 pub fn addArg(block: *Block, ty: Type, name: u32) error{OutOfMemory}!Air.Inst.Ref {
1606 return block.addInst(.{
1607 .tag = .arg,
1608 .data = .{ .ty_str = .{
1609 .ty = try block.sema.addType(ty),
1610 .str = name,
1611 } },
1612 });
1613 }
1614
1615 pub fn addStructFieldPtr(
1616 block: *Block,
1617 struct_ptr: Air.Inst.Ref,
1618 field_index: u32,
1619 ptr_field_ty: Type,
1620 ) !Air.Inst.Ref {
1621 const ty = try block.sema.addType(ptr_field_ty);
1622 const tag: Air.Inst.Tag = switch (field_index) {
1623 0 => .struct_field_ptr_index_0,
1624 1 => .struct_field_ptr_index_1,
1625 2 => .struct_field_ptr_index_2,
1626 3 => .struct_field_ptr_index_3,
1627 else => {
1628 return block.addInst(.{
1629 .tag = .struct_field_ptr,
1630 .data = .{ .ty_pl = .{
1631 .ty = ty,
1632 .payload = try block.sema.addExtra(Air.StructField{
1633 .struct_operand = struct_ptr,
1634 .field_index = @intCast(u32, field_index),
1635 }),
1636 } },
1637 });
1638 },
1639 };
1640 return block.addInst(.{
1641 .tag = tag,
1642 .data = .{ .ty_op = .{
1643 .ty = ty,
1644 .operand = struct_ptr,
1645 } },
1646 });
1647 }
1648
1649 pub fn addInst(block: *Block, inst: Air.Inst) error{OutOfMemory}!Air.Inst.Ref {
1650 return Air.indexToRef(try block.addInstAsIndex(inst));
1651 }
1652
1653 pub fn addInstAsIndex(block: *Block, inst: Air.Inst) error{OutOfMemory}!Air.Inst.Index {
1654 const sema = block.sema;
1655 const gpa = sema.gpa;
1656
1657 try sema.air_instructions.ensureUnusedCapacity(gpa, 1);
1658 try block.instructions.ensureUnusedCapacity(gpa, 1);
1659
1660 const result_index = @intCast(Air.Inst.Index, sema.air_instructions.len);
1661 sema.air_instructions.appendAssumeCapacity(inst);
1662 block.instructions.appendAssumeCapacity(result_index);
1663 return result_index;
1664 }
1665
1666 pub fn startAnonDecl(block: *Block) !WipAnonDecl {
1667 return WipAnonDecl{
1668 .block = block,
1669 .new_decl_arena = std.heap.ArenaAllocator.init(block.sema.gpa),
1670 .finished = false,
1671 };
1672 }
1353 pub fn renderFullyQualifiedDebugName(file: File, writer: anytype) !void {
1354 for (file.sub_file_path) |byte| switch (byte) {
1355 '/', '\\' => try writer.writeByte('/'),
1356 else => try writer.writeByte(byte),
1357 };
1358 }
16731359
1674 pub const WipAnonDecl = struct {
1675 block: *Scope.Block,
1676 new_decl_arena: std.heap.ArenaAllocator,
1677 finished: bool,
1360 pub fn fullyQualifiedNameZ(file: File, gpa: *Allocator) ![:0]u8 {
1361 var buf = std.ArrayList(u8).init(gpa);
1362 defer buf.deinit();
1363 try file.renderFullyQualifiedName(buf.writer());
1364 return buf.toOwnedSliceSentinel(0);
1365 }
16781366
1679 pub fn arena(wad: *WipAnonDecl) *Allocator {
1680 return &wad.new_decl_arena.allocator;
1681 }
1367 /// Returns the full path to this file relative to its package.
1368 pub fn fullPath(file: File, ally: *Allocator) ![]u8 {
1369 return file.pkg.root_src_directory.join(ally, &[_][]const u8{file.sub_file_path});
1370 }
16821371
1683 pub fn deinit(wad: *WipAnonDecl) void {
1684 if (!wad.finished) {
1685 wad.new_decl_arena.deinit();
1686 }
1687 wad.* = undefined;
1688 }
1372 pub fn dumpSrc(file: *File, src: LazySrcLoc) void {
1373 const loc = std.zig.findLineColumn(file.source.bytes, src);
1374 std.debug.print("{s}:{d}:{d}\n", .{ file.sub_file_path, loc.line + 1, loc.column + 1 });
1375 }
16891376
1690 pub fn finish(wad: *WipAnonDecl, ty: Type, val: Value) !*Decl {
1691 const new_decl = try wad.block.sema.mod.createAnonymousDecl(&wad.block.base, .{
1692 .ty = ty,
1693 .val = val,
1694 });
1695 errdefer wad.block.sema.mod.deleteAnonDecl(&wad.block.base, new_decl);
1696 try new_decl.finalizeNewArena(&wad.new_decl_arena);
1697 wad.finished = true;
1698 return new_decl;
1699 }
1377 pub fn okToReportErrors(file: File) bool {
1378 return switch (file.status) {
1379 .parse_failure, .astgen_failure => false,
1380 else => true,
17001381 };
1701 };
1382 }
17021383};
17031384
17041385/// This struct holds data necessary to construct API-facing `AllErrors.Message`.
17051386/// Its memory is managed with the general purpose allocator so that they
17061387/// can be created and destroyed in response to incremental updates.
1707/// In some cases, the Scope.File could have been inferred from where the ErrorMsg
1708/// is stored. For example, if it is stored in Module.failed_decls, then the Scope.File
1388/// In some cases, the File could have been inferred from where the ErrorMsg
1389/// is stored. For example, if it is stored in Module.failed_decls, then the File
17091390/// would be determined by the Decl Scope. However, the data structure contains the field
17101391/// anyway so that `ErrorMsg` can be reused for error notes, which may be in a different
17111392/// file than the parent error message. It also simplifies processing of error messages.
......@@ -1757,7 +1438,7 @@ pub const ErrorMsg = struct {
17571438
17581439/// Canonical reference to a position within a source file.
17591440pub const SrcLoc = struct {
1760 file_scope: *Scope.File,
1441 file_scope: *File,
17611442 /// Might be 0 depending on tag of `lazy`.
17621443 parent_decl_node: Ast.Node.Index,
17631444 /// Relative to `parent_decl_node`.
......@@ -2334,60 +2015,8 @@ pub const LazySrcLoc = union(enum) {
23342015 /// The Decl is determined contextually.
23352016 node_offset_lib_name: i32,
23362017
2337 /// Upgrade to a `SrcLoc` based on the `Decl` or file in the provided scope.
2338 pub fn toSrcLoc(lazy: LazySrcLoc, scope: *Scope) SrcLoc {
2339 return switch (lazy) {
2340 .unneeded,
2341 .entire_file,
2342 .byte_abs,
2343 .token_abs,
2344 .node_abs,
2345 => .{
2346 .file_scope = scope.getFileScope(),
2347 .parent_decl_node = 0,
2348 .lazy = lazy,
2349 },
2350
2351 .byte_offset,
2352 .token_offset,
2353 .node_offset,
2354 .node_offset_back2tok,
2355 .node_offset_var_decl_ty,
2356 .node_offset_for_cond,
2357 .node_offset_builtin_call_arg0,
2358 .node_offset_builtin_call_arg1,
2359 .node_offset_builtin_call_arg2,
2360 .node_offset_builtin_call_arg3,
2361 .node_offset_builtin_call_arg4,
2362 .node_offset_builtin_call_arg5,
2363 .node_offset_array_access_index,
2364 .node_offset_slice_sentinel,
2365 .node_offset_call_func,
2366 .node_offset_field_name,
2367 .node_offset_deref_ptr,
2368 .node_offset_asm_source,
2369 .node_offset_asm_ret_ty,
2370 .node_offset_if_cond,
2371 .node_offset_bin_op,
2372 .node_offset_bin_lhs,
2373 .node_offset_bin_rhs,
2374 .node_offset_switch_operand,
2375 .node_offset_switch_special_prong,
2376 .node_offset_switch_range,
2377 .node_offset_fn_type_cc,
2378 .node_offset_fn_type_ret_ty,
2379 .node_offset_anyframe_type,
2380 .node_offset_lib_name,
2381 => .{
2382 .file_scope = scope.getFileScope(),
2383 .parent_decl_node = scope.srcDecl().?.src_node,
2384 .lazy = lazy,
2385 },
2386 };
2387 }
2388
23892018 /// Upgrade to a `SrcLoc` based on the `Decl` provided.
2390 pub fn toSrcLocWithDecl(lazy: LazySrcLoc, decl: *Decl) SrcLoc {
2019 pub fn toSrcLoc(lazy: LazySrcLoc, decl: *Decl) SrcLoc {
23912020 return switch (lazy) {
23922021 .unneeded,
23932022 .entire_file,
......@@ -2576,7 +2205,7 @@ comptime {
25762205 }
25772206}
25782207
2579pub fn astGenFile(mod: *Module, file: *Scope.File) !void {
2208pub fn astGenFile(mod: *Module, file: *File) !void {
25802209 const tracy = trace(@src());
25812210 defer tracy.end();
25822211
......@@ -2960,7 +2589,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File) !void {
29602589/// * Decl.zir_index
29612590/// * Fn.zir_body_inst
29622591/// * Decl.zir_decl_index
2963fn updateZirRefs(gpa: *Allocator, file: *Scope.File, old_zir: Zir) !void {
2592fn updateZirRefs(gpa: *Allocator, file: *File, old_zir: Zir) !void {
29642593 const new_zir = file.zir;
29652594
29662595 // Maps from old ZIR to new ZIR, struct_decl, enum_decl, etc. Any instruction which
......@@ -3216,7 +2845,7 @@ pub fn semaPkg(mod: *Module, pkg: *Package) !void {
32162845
32172846/// Regardless of the file status, will create a `Decl` so that we
32182847/// can track dependencies and re-analyze when the file becomes outdated.
3219pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
2848pub fn semaFile(mod: *Module, file: *File) SemaError!void {
32202849 const tracy = trace(@src());
32212850 defer tracy.end();
32222851
......@@ -3244,11 +2873,11 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
32442873 .file_scope = file,
32452874 },
32462875 };
3247 const new_decl = try mod.allocateNewDecl(&struct_obj.namespace, 0, null);
2876 const decl_name = try file.fullyQualifiedNameZ(gpa);
2877 const new_decl = try mod.allocateNewDecl(decl_name, &struct_obj.namespace, 0, null);
32482878 file.root_decl = new_decl;
32492879 struct_obj.owner_decl = new_decl;
32502880 new_decl.src_line = 0;
3251 new_decl.name = try file.fullyQualifiedNameZ(gpa);
32522881 new_decl.is_pub = true;
32532882 new_decl.is_exported = false;
32542883 new_decl.has_align = false;
......@@ -3276,7 +2905,6 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
32762905 .perm_arena = &new_decl_arena.allocator,
32772906 .code = file.zir,
32782907 .owner_decl = new_decl,
3279 .namespace = &struct_obj.namespace,
32802908 .func = null,
32812909 .fn_ret_ty = Type.initTag(.void),
32822910 .owner_func = null,
......@@ -3286,10 +2914,11 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
32862914 var wip_captures = try WipCaptureScope.init(gpa, &new_decl_arena.allocator, null);
32872915 defer wip_captures.deinit();
32882916
3289 var block_scope: Scope.Block = .{
2917 var block_scope: Sema.Block = .{
32902918 .parent = null,
32912919 .sema = &sema,
32922920 .src_decl = new_decl,
2921 .namespace = &struct_obj.namespace,
32932922 .wip_capture_scope = wip_captures.scope,
32942923 .instructions = .{},
32952924 .inlining = null,
......@@ -3318,12 +2947,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
33182947 const tracy = trace(@src());
33192948 defer tracy.end();
33202949
3321 if (decl.namespace.file_scope.status != .success_zir) {
2950 if (decl.getFileScope().status != .success_zir) {
33222951 return error.AnalysisFail;
33232952 }
33242953
33252954 const gpa = mod.gpa;
3326 const zir = decl.namespace.file_scope.zir;
2955 const zir = decl.getFileScope().zir;
33272956 const zir_datas = zir.instructions.items(.data);
33282957
33292958 decl.analysis = .in_progress;
......@@ -3342,7 +2971,6 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
33422971 .perm_arena = &decl_arena.allocator,
33432972 .code = zir,
33442973 .owner_decl = decl,
3345 .namespace = decl.namespace,
33462974 .func = null,
33472975 .fn_ret_ty = Type.initTag(.void),
33482976 .owner_func = null,
......@@ -3366,10 +2994,11 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
33662994 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);
33672995 defer wip_captures.deinit();
33682996
3369 var block_scope: Scope.Block = .{
2997 var block_scope: Sema.Block = .{
33702998 .parent = null,
33712999 .sema = &sema,
33723000 .src_decl = decl,
3001 .namespace = decl.src_namespace,
33733002 .wip_capture_scope = wip_captures.scope,
33743003 .instructions = .{},
33753004 .inlining = null,
......@@ -3427,12 +3056,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
34273056 if (decl.is_usingnamespace) {
34283057 const ty_ty = Type.initTag(.type);
34293058 if (!decl_tv.ty.eql(ty_ty)) {
3430 return mod.fail(&block_scope.base, src, "expected type, found {}", .{decl_tv.ty});
3059 return sema.fail(&block_scope, src, "expected type, found {}", .{decl_tv.ty});
34313060 }
34323061 var buffer: Value.ToTypeBuffer = undefined;
34333062 const ty = decl_tv.val.toType(&buffer);
34343063 if (ty.getNamespace() == null) {
3435 return mod.fail(&block_scope.base, src, "type {} has no namespace", .{ty});
3064 return sema.fail(&block_scope, src, "type {} has no namespace", .{ty});
34363065 }
34373066
34383067 decl.ty = ty_ty;
......@@ -3495,11 +3124,11 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
34953124 if (decl.is_exported) {
34963125 const export_src = src; // TODO make this point at `export` token
34973126 if (is_inline) {
3498 return mod.fail(&block_scope.base, export_src, "export of inline function", .{});
3127 return sema.fail(&block_scope, export_src, "export of inline function", .{});
34993128 }
35003129 // The scope needs to have the decl in it.
35013130 const options: std.builtin.ExportOptions = .{ .name = mem.spanZ(decl.name) };
3502 try mod.analyzeExport(&block_scope, export_src, options, decl);
3131 try sema.analyzeExport(&block_scope, export_src, options, decl);
35033132 }
35043133 return type_changed or is_inline != prev_is_inline;
35053134 }
......@@ -3548,18 +3177,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
35483177 try mod.comp.work_queue.writeItem(.{ .emit_h_decl = decl });
35493178 }
35503179 }
3551 // In case this Decl is a struct or union, we need to resolve the fields
3552 // while we still have the `Sema` in scope, so that the field type expressions
3553 // can use the resolved AIR instructions that they possibly reference.
3554 // We do this after the decl is populated and set to `complete` so that a `Decl`
3555 // may reference itself.
3556 try sema.resolvePendingTypes(&block_scope);
35573180
35583181 if (decl.is_exported) {
35593182 const export_src = src; // TODO point to the export token
35603183 // The scope needs to have the decl in it.
35613184 const options: std.builtin.ExportOptions = .{ .name = mem.spanZ(decl.name) };
3562 try mod.analyzeExport(&block_scope, export_src, options, decl);
3185 try sema.analyzeExport(&block_scope, export_src, options, decl);
35633186 }
35643187
35653188 return type_changed;
......@@ -3586,7 +3209,7 @@ pub fn declareDeclDependency(mod: *Module, depender: *Decl, dependee: *Decl) !vo
35863209}
35873210
35883211pub const ImportFileResult = struct {
3589 file: *Scope.File,
3212 file: *File,
35903213 is_new: bool,
35913214};
35923215
......@@ -3612,7 +3235,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
36123235 const sub_file_path = try gpa.dupe(u8, pkg.root_src_path);
36133236 errdefer gpa.free(sub_file_path);
36143237
3615 const new_file = try gpa.create(Scope.File);
3238 const new_file = try gpa.create(File);
36163239 errdefer gpa.destroy(new_file);
36173240
36183241 gop.value_ptr.* = new_file;
......@@ -3639,7 +3262,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
36393262
36403263pub fn importFile(
36413264 mod: *Module,
3642 cur_file: *Scope.File,
3265 cur_file: *File,
36433266 import_string: []const u8,
36443267) !ImportFileResult {
36453268 if (cur_file.pkg.table.get(import_string)) |pkg| {
......@@ -3667,7 +3290,7 @@ pub fn importFile(
36673290 };
36683291 keep_resolved_path = true; // It's now owned by import_table.
36693292
3670 const new_file = try gpa.create(Scope.File);
3293 const new_file = try gpa.create(File);
36713294 errdefer gpa.destroy(new_file);
36723295
36733296 const resolved_root_path = try std.fs.path.resolve(gpa, &[_][]const u8{cur_pkg_dir_path});
......@@ -3708,7 +3331,7 @@ pub fn importFile(
37083331
37093332pub fn scanNamespace(
37103333 mod: *Module,
3711 namespace: *Scope.Namespace,
3334 namespace: *Namespace,
37123335 extra_start: usize,
37133336 decls_len: u32,
37143337 parent_decl: *Decl,
......@@ -3752,7 +3375,7 @@ pub fn scanNamespace(
37523375
37533376const ScanDeclIter = struct {
37543377 module: *Module,
3755 namespace: *Scope.Namespace,
3378 namespace: *Namespace,
37563379 parent_decl: *Decl,
37573380 usingnamespace_index: usize = 0,
37583381 comptime_index: usize = 0,
......@@ -3818,13 +3441,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
38183441 // We create a Decl for it regardless of analysis status.
38193442 const gop = try namespace.decls.getOrPut(gpa, decl_name);
38203443 if (!gop.found_existing) {
3821 const new_decl = try mod.allocateNewDecl(namespace, decl_node, iter.parent_decl.src_scope);
3444 const new_decl = try mod.allocateNewDecl(decl_name, namespace, decl_node, iter.parent_decl.src_scope);
38223445 if (is_usingnamespace) {
38233446 namespace.usingnamespace_set.putAssumeCapacity(new_decl, is_pub);
38243447 }
38253448 log.debug("scan new {*} ({s}) into {*}", .{ new_decl, decl_name, namespace });
38263449 new_decl.src_line = line;
3827 new_decl.name = decl_name;
38283450 gop.value_ptr.* = new_decl;
38293451 // Exported decls, comptime decls, usingnamespace decls, and
38303452 // test decls if in test mode, get analyzed.
......@@ -4013,9 +3635,10 @@ pub fn deleteUnusedDecl(mod: *Module, decl: *Decl) void {
40133635 },
40143636 }
40153637
4016 const dependants = decl.dependants.keys();
4017 assert(dependants[0].namespace.anon_decls.swapRemove(decl));
3638 assert(!decl.isRoot());
3639 assert(decl.src_namespace.anon_decls.swapRemove(decl));
40183640
3641 const dependants = decl.dependants.keys();
40193642 for (dependants) |dep| {
40203643 dep.removeDependency(decl);
40213644 }
......@@ -4026,10 +3649,22 @@ pub fn deleteUnusedDecl(mod: *Module, decl: *Decl) void {
40263649 decl.destroy(mod);
40273650}
40283651
4029pub fn deleteAnonDecl(mod: *Module, scope: *Scope, decl: *Decl) void {
4030 log.debug("deleteAnonDecl {*} ({s})", .{ decl, decl.name });
4031 const scope_decl = scope.srcDecl().?;
4032 assert(scope_decl.namespace.anon_decls.swapRemove(decl));
3652/// Cancel the creation of an anon decl and delete any references to it.
3653/// If other decls depend on this decl, they must be aborted first.
3654pub fn abortAnonDecl(mod: *Module, decl: *Decl) void {
3655 log.debug("abortAnonDecl {*} ({s})", .{ decl, decl.name });
3656
3657 assert(!decl.isRoot());
3658 assert(decl.src_namespace.anon_decls.swapRemove(decl));
3659
3660 // An aborted decl must not have dependants -- they must have
3661 // been aborted first and removed from this list.
3662 assert(decl.dependants.count() == 0);
3663
3664 for (decl.dependencies.keys()) |dep| {
3665 dep.removeDependant(decl);
3666 }
3667
40333668 decl.destroy(mod);
40343669}
40353670
......@@ -4087,9 +3722,8 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se
40873722 .gpa = gpa,
40883723 .arena = arena,
40893724 .perm_arena = &decl_arena.allocator,
4090 .code = decl.namespace.file_scope.zir,
3725 .code = decl.getFileScope().zir,
40913726 .owner_decl = decl,
4092 .namespace = decl.namespace,
40933727 .func = func,
40943728 .fn_ret_ty = func.owner_decl.ty.fnReturnType(),
40953729 .owner_func = func,
......@@ -4104,10 +3738,11 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se
41043738 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);
41053739 defer wip_captures.deinit();
41063740
4107 var inner_block: Scope.Block = .{
3741 var inner_block: Sema.Block = .{
41083742 .parent = null,
41093743 .sema = &sema,
41103744 .src_decl = decl,
3745 .namespace = decl.src_namespace,
41113746 .wip_capture_scope = wip_captures.scope,
41123747 .instructions = .{},
41133748 .inlining = null,
......@@ -4226,7 +3861,7 @@ fn markOutdatedDecl(mod: *Module, decl: *Decl) !void {
42263861 decl.analysis = .outdated;
42273862}
42283863
4229pub fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: Ast.Node.Index, src_scope: ?*CaptureScope) !*Decl {
3864pub fn allocateNewDecl(mod: *Module, name: [:0]const u8, namespace: *Namespace, src_node: Ast.Node.Index, src_scope: ?*CaptureScope) !*Decl {
42303865 // If we have emit-h then we must allocate a bigger structure to store the emit-h state.
42313866 const new_decl: *Decl = if (mod.emit_h != null) blk: {
42323867 const parent_struct = try mod.gpa.create(DeclPlusEmitH);
......@@ -4238,8 +3873,8 @@ pub fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: Ast.
42383873 } else try mod.gpa.create(Decl);
42393874
42403875 new_decl.* = .{
4241 .name = "",
4242 .namespace = namespace,
3876 .name = name,
3877 .src_namespace = namespace,
42433878 .src_node = src_node,
42443879 .src_line = undefined,
42453880 .has_tv = false,
......@@ -4304,119 +3939,49 @@ pub fn getErrorValue(mod: *Module, name: []const u8) !std.StringHashMapUnmanaged
43043939 };
43053940}
43063941
4307pub fn analyzeExport(
4308 mod: *Module,
4309 block: *Scope.Block,
4310 src: LazySrcLoc,
4311 borrowed_options: std.builtin.ExportOptions,
4312 exported_decl: *Decl,
4313) !void {
4314 try mod.ensureDeclAnalyzed(exported_decl);
4315 switch (exported_decl.ty.zigTypeTag()) {
4316 .Fn => {},
4317 else => return mod.fail(&block.base, src, "unable to export type '{}'", .{exported_decl.ty}),
4318 }
4319
4320 const gpa = mod.gpa;
4321
4322 try mod.decl_exports.ensureUnusedCapacity(gpa, 1);
4323 try mod.export_owners.ensureUnusedCapacity(gpa, 1);
4324
4325 const new_export = try gpa.create(Export);
4326 errdefer gpa.destroy(new_export);
4327
4328 const symbol_name = try gpa.dupe(u8, borrowed_options.name);
4329 errdefer gpa.free(symbol_name);
4330
4331 const section: ?[]const u8 = if (borrowed_options.section) |s| try gpa.dupe(u8, s) else null;
4332 errdefer if (section) |s| gpa.free(s);
4333
4334 const src_decl = block.src_decl;
4335 const owner_decl = block.sema.owner_decl;
4336
4337 log.debug("exporting Decl '{s}' as symbol '{s}' from Decl '{s}'", .{
4338 exported_decl.name, symbol_name, owner_decl.name,
4339 });
4340
4341 new_export.* = .{
4342 .options = .{
4343 .name = symbol_name,
4344 .linkage = borrowed_options.linkage,
4345 .section = section,
4346 },
4347 .src = src,
4348 .link = switch (mod.comp.bin_file.tag) {
4349 .coff => .{ .coff = {} },
4350 .elf => .{ .elf = link.File.Elf.Export{} },
4351 .macho => .{ .macho = link.File.MachO.Export{} },
4352 .plan9 => .{ .plan9 = null },
4353 .c => .{ .c = {} },
4354 .wasm => .{ .wasm = {} },
4355 .spirv => .{ .spirv = {} },
4356 },
4357 .owner_decl = owner_decl,
4358 .src_decl = src_decl,
4359 .exported_decl = exported_decl,
4360 .status = .in_progress,
4361 };
4362
4363 // Add to export_owners table.
4364 const eo_gop = mod.export_owners.getOrPutAssumeCapacity(owner_decl);
4365 if (!eo_gop.found_existing) {
4366 eo_gop.value_ptr.* = &[0]*Export{};
4367 }
4368 eo_gop.value_ptr.* = try gpa.realloc(eo_gop.value_ptr.*, eo_gop.value_ptr.len + 1);
4369 eo_gop.value_ptr.*[eo_gop.value_ptr.len - 1] = new_export;
4370 errdefer eo_gop.value_ptr.* = gpa.shrink(eo_gop.value_ptr.*, eo_gop.value_ptr.len - 1);
4371
4372 // Add to exported_decl table.
4373 const de_gop = mod.decl_exports.getOrPutAssumeCapacity(exported_decl);
4374 if (!de_gop.found_existing) {
4375 de_gop.value_ptr.* = &[0]*Export{};
4376 }
4377 de_gop.value_ptr.* = try gpa.realloc(de_gop.value_ptr.*, de_gop.value_ptr.len + 1);
4378 de_gop.value_ptr.*[de_gop.value_ptr.len - 1] = new_export;
4379 errdefer de_gop.value_ptr.* = gpa.shrink(de_gop.value_ptr.*, de_gop.value_ptr.len - 1);
4380}
4381
43823942/// Takes ownership of `name` even if it returns an error.
43833943pub fn createAnonymousDeclNamed(
43843944 mod: *Module,
4385 scope: *Scope,
3945 block: *Sema.Block,
43863946 typed_value: TypedValue,
43873947 name: [:0]u8,
43883948) !*Decl {
4389 return mod.createAnonymousDeclFromDeclNamed(scope.srcDecl().?, scope.srcScope(), typed_value, name);
3949 return mod.createAnonymousDeclFromDeclNamed(block.src_decl, block.namespace, block.wip_capture_scope, typed_value, name);
43903950}
43913951
4392pub fn createAnonymousDecl(mod: *Module, scope: *Scope, typed_value: TypedValue) !*Decl {
4393 return mod.createAnonymousDeclFromDecl(scope.srcDecl().?, scope.srcScope(), typed_value);
3952pub fn createAnonymousDecl(mod: *Module, block: *Sema.Block, typed_value: TypedValue) !*Decl {
3953 return mod.createAnonymousDeclFromDecl(block.src_decl, block.namespace, block.wip_capture_scope, typed_value);
43943954}
43953955
4396pub fn createAnonymousDeclFromDecl(mod: *Module, src_decl: *Decl, src_scope: ?*CaptureScope, tv: TypedValue) !*Decl {
3956pub fn createAnonymousDeclFromDecl(
3957 mod: *Module,
3958 src_decl: *Decl,
3959 namespace: *Namespace,
3960 src_scope: ?*CaptureScope,
3961 tv: TypedValue,
3962) !*Decl {
43973963 const name_index = mod.getNextAnonNameIndex();
43983964 const name = try std.fmt.allocPrintZ(mod.gpa, "{s}__anon_{d}", .{
43993965 src_decl.name, name_index,
44003966 });
4401 return mod.createAnonymousDeclFromDeclNamed(src_decl, src_scope, tv, name);
3967 return mod.createAnonymousDeclFromDeclNamed(src_decl, namespace, src_scope, tv, name);
44023968}
44033969
44043970/// Takes ownership of `name` even if it returns an error.
44053971pub fn createAnonymousDeclFromDeclNamed(
44063972 mod: *Module,
44073973 src_decl: *Decl,
3974 namespace: *Namespace,
44083975 src_scope: ?*CaptureScope,
44093976 typed_value: TypedValue,
44103977 name: [:0]u8,
44113978) !*Decl {
44123979 errdefer mod.gpa.free(name);
44133980
4414 const namespace = src_decl.namespace;
44153981 try namespace.anon_decls.ensureUnusedCapacity(mod.gpa, 1);
44163982
4417 const new_decl = try mod.allocateNewDecl(namespace, src_decl.src_node, src_scope);
3983 const new_decl = try mod.allocateNewDecl(name, namespace, src_decl.src_node, src_scope);
44183984
4419 new_decl.name = name;
44203985 new_decl.src_line = src_decl.src_line;
44213986 new_decl.ty = typed_value.ty;
44223987 new_decl.val = typed_value.val;
......@@ -4458,19 +4023,6 @@ pub fn makeIntType(arena: *Allocator, signedness: std.builtin.Signedness, bits:
44584023 return Type.initPayload(&int_payload.base);
44594024}
44604025
4461/// We don't return a pointer to the new error note because the pointer
4462/// becomes invalid when you add another one.
4463pub fn errNote(
4464 mod: *Module,
4465 scope: *Scope,
4466 src: LazySrcLoc,
4467 parent: *ErrorMsg,
4468 comptime format: []const u8,
4469 args: anytype,
4470) error{OutOfMemory}!void {
4471 return mod.errNoteNonLazy(src.toSrcLoc(scope), parent, format, args);
4472}
4473
44744026pub fn errNoteNonLazy(
44754027 mod: *Module,
44764028 src_loc: SrcLoc,
......@@ -4488,81 +4040,6 @@ pub fn errNoteNonLazy(
44884040 };
44894041}
44904042
4491pub fn errMsg(
4492 mod: *Module,
4493 scope: *Scope,
4494 src: LazySrcLoc,
4495 comptime format: []const u8,
4496 args: anytype,
4497) error{OutOfMemory}!*ErrorMsg {
4498 return ErrorMsg.create(mod.gpa, src.toSrcLoc(scope), format, args);
4499}
4500
4501pub fn fail(
4502 mod: *Module,
4503 scope: *Scope,
4504 src: LazySrcLoc,
4505 comptime format: []const u8,
4506 args: anytype,
4507) CompileError {
4508 const err_msg = try mod.errMsg(scope, src, format, args);
4509 return mod.failWithOwnedErrorMsg(scope, err_msg);
4510}
4511
4512/// Same as `fail`, except given a token index, and the function sets up the `LazySrcLoc`
4513/// for pointing at it relatively by subtracting from the containing `Decl`.
4514pub fn failTok(
4515 mod: *Module,
4516 scope: *Scope,
4517 token_index: Ast.TokenIndex,
4518 comptime format: []const u8,
4519 args: anytype,
4520) CompileError {
4521 const src = scope.srcDecl().?.tokSrcLoc(token_index);
4522 return mod.fail(scope, src, format, args);
4523}
4524
4525/// Same as `fail`, except given an AST node index, and the function sets up the `LazySrcLoc`
4526/// for pointing at it relatively by subtracting from the containing `Decl`.
4527pub fn failNode(
4528 mod: *Module,
4529 scope: *Scope,
4530 node_index: Ast.Node.Index,
4531 comptime format: []const u8,
4532 args: anytype,
4533) CompileError {
4534 const src = scope.srcDecl().?.nodeSrcLoc(node_index);
4535 return mod.fail(scope, src, format, args);
4536}
4537
4538pub fn failWithOwnedErrorMsg(mod: *Module, scope: *Scope, err_msg: *ErrorMsg) CompileError {
4539 @setCold(true);
4540
4541 {
4542 errdefer err_msg.destroy(mod.gpa);
4543 if (err_msg.src_loc.lazy == .unneeded) {
4544 return error.NeededSourceLocation;
4545 }
4546 try mod.failed_decls.ensureUnusedCapacity(mod.gpa, 1);
4547 try mod.failed_files.ensureUnusedCapacity(mod.gpa, 1);
4548 }
4549 switch (scope.tag) {
4550 .block => {
4551 const block = scope.cast(Scope.Block).?;
4552 if (block.sema.owner_func) |func| {
4553 func.state = .sema_failure;
4554 } else {
4555 block.sema.owner_decl.analysis = .sema_failure;
4556 block.sema.owner_decl.generation = mod.generation;
4557 }
4558 mod.failed_decls.putAssumeCapacityNoClobber(block.sema.owner_decl, err_msg);
4559 },
4560 .file => unreachable,
4561 .namespace => unreachable,
4562 }
4563 return error.AnalysisFail;
4564}
4565
45664043pub fn optionalType(arena: *Allocator, child_type: Type) Allocator.Error!Type {
45674044 switch (child_type.tag()) {
45684045 .single_const_pointer => return Type.Tag.optional_single_const_pointer.create(
......@@ -4631,7 +4108,7 @@ pub fn optimizeMode(mod: Module) std.builtin.Mode {
46314108 return mod.comp.bin_file.options.optimize_mode;
46324109}
46334110
4634fn lockAndClearFileCompileError(mod: *Module, file: *Scope.File) void {
4111fn lockAndClearFileCompileError(mod: *Module, file: *File) void {
46354112 switch (file.status) {
46364113 .success_zir, .retryable_failure => {},
46374114 .never_loaded, .parse_failure, .astgen_failure => {
......@@ -4666,10 +4143,10 @@ pub const SwitchProngSrc = union(enum) {
46664143 range_expand: RangeExpand,
46674144 ) LazySrcLoc {
46684145 @setCold(true);
4669 const tree = decl.namespace.file_scope.getTree(gpa) catch |err| {
4146 const tree = decl.getFileScope().getTree(gpa) catch |err| {
46704147 // In this case we emit a warning + a less precise source location.
46714148 log.warn("unable to load {s}: {s}", .{
4672 decl.namespace.file_scope.sub_file_path, @errorName(err),
4149 decl.getFileScope().sub_file_path, @errorName(err),
46734150 });
46744151 return LazySrcLoc{ .node_offset = 0 };
46754152 };
......@@ -4778,10 +4255,10 @@ pub const PeerTypeCandidateSrc = union(enum) {
47784255 else => {},
47794256 }
47804257
4781 const tree = decl.namespace.file_scope.getTree(gpa) catch |err| {
4258 const tree = decl.getFileScope().getTree(gpa) catch |err| {
47824259 // In this case we emit a warning + a less precise source location.
47834260 log.warn("unable to load {s}: {s}", .{
4784 decl.namespace.file_scope.sub_file_path, @errorName(err),
4261 decl.getFileScope().sub_file_path, @errorName(err),
47854262 });
47864263 return LazySrcLoc{ .node_offset = 0 };
47874264 };
......@@ -4824,7 +4301,7 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void {
48244301
48254302 // Remove from the namespace it resides in, preserving declaration order.
48264303 assert(decl.zir_decl_index != 0);
4827 _ = decl.namespace.decls.orderedRemove(mem.spanZ(decl.name));
4304 _ = decl.src_namespace.decls.orderedRemove(mem.spanZ(decl.name));
48284305
48294306 try mod.clearDecl(decl, &outdated_decls);
48304307 decl.destroy(mod);
......@@ -4890,7 +4367,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
48904367 const gpa = mod.gpa;
48914368 const builtin_pkg = mod.main_pkg.table.get("builtin").?;
48924369 const builtin_file = (mod.importPkg(builtin_pkg) catch unreachable).file;
4893 const builtin_namespace = builtin_file.root_decl.?.namespace;
4370 const builtin_namespace = builtin_file.root_decl.?.src_namespace;
48944371 const decl = builtin_namespace.decls.get("test_functions").?;
48954372 var buf: Type.SlicePtrFieldTypeBuffer = undefined;
48964373 const tmp_test_fn_ty = decl.ty.slicePtrFieldType(&buf).elemType();
......@@ -4903,7 +4380,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
49034380 const arena = &new_decl_arena.allocator;
49044381
49054382 const test_fn_vals = try arena.alloc(Value, mod.test_functions.count());
4906 const array_decl = try mod.createAnonymousDeclFromDecl(decl, null, .{
4383 const array_decl = try mod.createAnonymousDeclFromDecl(decl, decl.src_namespace, null, .{
49074384 .ty = try Type.Tag.array.create(arena, .{
49084385 .len = test_fn_vals.len,
49094386 .elem_type = try tmp_test_fn_ty.copy(arena),
......@@ -4916,7 +4393,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
49164393 var name_decl_arena = std.heap.ArenaAllocator.init(gpa);
49174394 errdefer name_decl_arena.deinit();
49184395 const bytes = try name_decl_arena.allocator.dupe(u8, test_name_slice);
4919 const test_name_decl = try mod.createAnonymousDeclFromDecl(array_decl, null, .{
4396 const test_name_decl = try mod.createAnonymousDeclFromDecl(array_decl, array_decl.src_namespace, null, .{
49204397 .ty = try Type.Tag.array_u8.create(&name_decl_arena.allocator, bytes.len),
49214398 .val = try Value.Tag.bytes.create(&name_decl_arena.allocator, bytes),
49224399 });
src/Sema.zig+1352-950
......@@ -21,11 +21,9 @@ air_values: std.ArrayListUnmanaged(Value) = .{},
2121/// Maps ZIR to AIR.
2222inst_map: InstMap = .{},
2323/// When analyzing an inline function call, owner_decl is the Decl of the caller
24/// and `src_decl` of `Scope.Block` is the `Decl` of the callee.
24/// and `src_decl` of `Block` is the `Decl` of the callee.
2525/// This `Decl` owns the arena memory of this `Sema`.
2626owner_decl: *Decl,
27/// How to look up decl names.
28namespace: *Scope.Namespace,
2927/// For an inline or comptime function call, this will be the root parent function
3028/// which contains the callsite. Corresponds to `owner_decl`.
3129owner_func: ?*Module.Fn,
......@@ -62,9 +60,6 @@ comptime_args_fn_inst: Zir.Inst.Index = 0,
6260/// extra hash table lookup in the `monomorphed_funcs` set.
6361/// Sema will set this to null when it takes ownership.
6462preallocated_new_func: ?*Module.Fn = null,
65/// Collects struct, union, enum, and opaque decls which need to have their
66/// fields resolved before this Sema is deinitialized.
67types_pending_resolution: std.ArrayListUnmanaged(Type) = .{},
6863
6964const std = @import("std");
7065const mem = std.mem;
......@@ -80,7 +75,7 @@ const Air = @import("Air.zig");
8075const Zir = @import("Zir.zig");
8176const Module = @import("Module.zig");
8277const trace = @import("tracy.zig").trace;
83const Scope = Module.Scope;
78const Namespace = Module.Namespace;
8479const CompileError = Module.CompileError;
8580const SemaError = Module.SemaError;
8681const Decl = Module.Decl;
......@@ -94,6 +89,286 @@ const crash_report = @import("crash_report.zig");
9489
9590pub const InstMap = std.AutoHashMapUnmanaged(Zir.Inst.Index, Air.Inst.Ref);
9691
92/// This is the context needed to semantically analyze ZIR instructions and
93/// produce AIR instructions.
94/// This is a temporary structure stored on the stack; references to it are valid only
95/// during semantic analysis of the block.
96pub const Block = struct {
97 parent: ?*Block,
98 /// Shared among all child blocks.
99 sema: *Sema,
100 /// This Decl is the Decl according to the Zig source code corresponding to this Block.
101 /// This can vary during inline or comptime function calls. See `Sema.owner_decl`
102 /// for the one that will be the same for all Block instances.
103 src_decl: *Decl,
104 /// The namespace to use for lookups from this source block
105 /// When analyzing fields, this is different from src_decl.src_namepsace.
106 namespace: *Namespace,
107 /// The AIR instructions generated for this block.
108 instructions: std.ArrayListUnmanaged(Air.Inst.Index),
109 // `param` instructions are collected here to be used by the `func` instruction.
110 params: std.ArrayListUnmanaged(Param) = .{},
111
112 wip_capture_scope: *CaptureScope,
113
114 label: ?*Label = null,
115 inlining: ?*Inlining,
116 /// If runtime_index is not 0 then one of these is guaranteed to be non null.
117 runtime_cond: ?LazySrcLoc = null,
118 runtime_loop: ?LazySrcLoc = null,
119 /// Non zero if a non-inline loop or a runtime conditional have been encountered.
120 /// Stores to to comptime variables are only allowed when var.runtime_index <= runtime_index.
121 runtime_index: u32 = 0,
122
123 is_comptime: bool,
124
125 /// when null, it is determined by build mode, changed by @setRuntimeSafety
126 want_safety: ?bool = null,
127
128 c_import_buf: ?*std.ArrayList(u8) = null,
129
130 const Param = struct {
131 /// `noreturn` means `anytype`.
132 ty: Type,
133 is_comptime: bool,
134 };
135
136 /// This `Block` maps a block ZIR instruction to the corresponding
137 /// AIR instruction for break instruction analysis.
138 pub const Label = struct {
139 zir_block: Zir.Inst.Index,
140 merges: Merges,
141 };
142
143 /// This `Block` indicates that an inline function call is happening
144 /// and return instructions should be analyzed as a break instruction
145 /// to this AIR block instruction.
146 /// It is shared among all the blocks in an inline or comptime called
147 /// function.
148 pub const Inlining = struct {
149 comptime_result: Air.Inst.Ref,
150 merges: Merges,
151 };
152
153 pub const Merges = struct {
154 block_inst: Air.Inst.Index,
155 /// Separate array list from break_inst_list so that it can be passed directly
156 /// to resolvePeerTypes.
157 results: std.ArrayListUnmanaged(Air.Inst.Ref),
158 /// Keeps track of the break instructions so that the operand can be replaced
159 /// if we need to add type coercion at the end of block analysis.
160 /// Same indexes, capacity, length as `results`.
161 br_list: std.ArrayListUnmanaged(Air.Inst.Index),
162 };
163
164 /// For debugging purposes.
165 pub fn dump(block: *Block, mod: Module) void {
166 Zir.dumpBlock(mod, block);
167 }
168
169 pub fn makeSubBlock(parent: *Block) Block {
170 return .{
171 .parent = parent,
172 .sema = parent.sema,
173 .src_decl = parent.src_decl,
174 .namespace = parent.namespace,
175 .instructions = .{},
176 .wip_capture_scope = parent.wip_capture_scope,
177 .label = null,
178 .inlining = parent.inlining,
179 .is_comptime = parent.is_comptime,
180 .runtime_cond = parent.runtime_cond,
181 .runtime_loop = parent.runtime_loop,
182 .runtime_index = parent.runtime_index,
183 .want_safety = parent.want_safety,
184 .c_import_buf = parent.c_import_buf,
185 };
186 }
187
188 pub fn wantSafety(block: *const Block) bool {
189 return block.want_safety orelse switch (block.sema.mod.optimizeMode()) {
190 .Debug => true,
191 .ReleaseSafe => true,
192 .ReleaseFast => false,
193 .ReleaseSmall => false,
194 };
195 }
196
197 pub fn getFileScope(block: *Block) *Module.File {
198 return block.namespace.file_scope;
199 }
200
201 pub fn addTy(
202 block: *Block,
203 tag: Air.Inst.Tag,
204 ty: Type,
205 ) error{OutOfMemory}!Air.Inst.Ref {
206 return block.addInst(.{
207 .tag = tag,
208 .data = .{ .ty = ty },
209 });
210 }
211
212 pub fn addTyOp(
213 block: *Block,
214 tag: Air.Inst.Tag,
215 ty: Type,
216 operand: Air.Inst.Ref,
217 ) error{OutOfMemory}!Air.Inst.Ref {
218 return block.addInst(.{
219 .tag = tag,
220 .data = .{ .ty_op = .{
221 .ty = try block.sema.addType(ty),
222 .operand = operand,
223 } },
224 });
225 }
226
227 pub fn addNoOp(block: *Block, tag: Air.Inst.Tag) error{OutOfMemory}!Air.Inst.Ref {
228 return block.addInst(.{
229 .tag = tag,
230 .data = .{ .no_op = {} },
231 });
232 }
233
234 pub fn addUnOp(
235 block: *Block,
236 tag: Air.Inst.Tag,
237 operand: Air.Inst.Ref,
238 ) error{OutOfMemory}!Air.Inst.Ref {
239 return block.addInst(.{
240 .tag = tag,
241 .data = .{ .un_op = operand },
242 });
243 }
244
245 pub fn addBr(
246 block: *Block,
247 target_block: Air.Inst.Index,
248 operand: Air.Inst.Ref,
249 ) error{OutOfMemory}!Air.Inst.Ref {
250 return block.addInst(.{
251 .tag = .br,
252 .data = .{ .br = .{
253 .block_inst = target_block,
254 .operand = operand,
255 } },
256 });
257 }
258
259 pub fn addBinOp(
260 block: *Block,
261 tag: Air.Inst.Tag,
262 lhs: Air.Inst.Ref,
263 rhs: Air.Inst.Ref,
264 ) error{OutOfMemory}!Air.Inst.Ref {
265 return block.addInst(.{
266 .tag = tag,
267 .data = .{ .bin_op = .{
268 .lhs = lhs,
269 .rhs = rhs,
270 } },
271 });
272 }
273
274 pub fn addArg(block: *Block, ty: Type, name: u32) error{OutOfMemory}!Air.Inst.Ref {
275 return block.addInst(.{
276 .tag = .arg,
277 .data = .{ .ty_str = .{
278 .ty = try block.sema.addType(ty),
279 .str = name,
280 } },
281 });
282 }
283
284 pub fn addStructFieldPtr(
285 block: *Block,
286 struct_ptr: Air.Inst.Ref,
287 field_index: u32,
288 ptr_field_ty: Type,
289 ) !Air.Inst.Ref {
290 const ty = try block.sema.addType(ptr_field_ty);
291 const tag: Air.Inst.Tag = switch (field_index) {
292 0 => .struct_field_ptr_index_0,
293 1 => .struct_field_ptr_index_1,
294 2 => .struct_field_ptr_index_2,
295 3 => .struct_field_ptr_index_3,
296 else => {
297 return block.addInst(.{
298 .tag = .struct_field_ptr,
299 .data = .{ .ty_pl = .{
300 .ty = ty,
301 .payload = try block.sema.addExtra(Air.StructField{
302 .struct_operand = struct_ptr,
303 .field_index = @intCast(u32, field_index),
304 }),
305 } },
306 });
307 },
308 };
309 return block.addInst(.{
310 .tag = tag,
311 .data = .{ .ty_op = .{
312 .ty = ty,
313 .operand = struct_ptr,
314 } },
315 });
316 }
317
318 pub fn addInst(block: *Block, inst: Air.Inst) error{OutOfMemory}!Air.Inst.Ref {
319 return Air.indexToRef(try block.addInstAsIndex(inst));
320 }
321
322 pub fn addInstAsIndex(block: *Block, inst: Air.Inst) error{OutOfMemory}!Air.Inst.Index {
323 const sema = block.sema;
324 const gpa = sema.gpa;
325
326 try sema.air_instructions.ensureUnusedCapacity(gpa, 1);
327 try block.instructions.ensureUnusedCapacity(gpa, 1);
328
329 const result_index = @intCast(Air.Inst.Index, sema.air_instructions.len);
330 sema.air_instructions.appendAssumeCapacity(inst);
331 block.instructions.appendAssumeCapacity(result_index);
332 return result_index;
333 }
334
335 pub fn startAnonDecl(block: *Block) !WipAnonDecl {
336 return WipAnonDecl{
337 .block = block,
338 .new_decl_arena = std.heap.ArenaAllocator.init(block.sema.gpa),
339 .finished = false,
340 };
341 }
342
343 pub const WipAnonDecl = struct {
344 block: *Block,
345 new_decl_arena: std.heap.ArenaAllocator,
346 finished: bool,
347
348 pub fn arena(wad: *WipAnonDecl) *Allocator {
349 return &wad.new_decl_arena.allocator;
350 }
351
352 pub fn deinit(wad: *WipAnonDecl) void {
353 if (!wad.finished) {
354 wad.new_decl_arena.deinit();
355 }
356 wad.* = undefined;
357 }
358
359 pub fn finish(wad: *WipAnonDecl, ty: Type, val: Value) !*Decl {
360 const new_decl = try wad.block.sema.mod.createAnonymousDecl(wad.block, .{
361 .ty = ty,
362 .val = val,
363 });
364 errdefer wad.block.sema.mod.abortAnonDecl(new_decl);
365 try new_decl.finalizeNewArena(&wad.new_decl_arena);
366 wad.finished = true;
367 return new_decl;
368 }
369 };
370};
371
97372pub fn deinit(sema: *Sema) void {
98373 const gpa = sema.gpa;
99374 sema.air_instructions.deinit(gpa);
......@@ -101,14 +376,13 @@ pub fn deinit(sema: *Sema) void {
101376 sema.air_values.deinit(gpa);
102377 sema.inst_map.deinit(gpa);
103378 sema.decl_val_table.deinit(gpa);
104 sema.types_pending_resolution.deinit(gpa);
105379 sema.* = undefined;
106380}
107381
108382/// Returns only the result from the body that is specified.
109383/// Only appropriate to call when it is determined at comptime that this body
110384/// has no peers.
111fn resolveBody(sema: *Sema, block: *Scope.Block, body: []const Zir.Inst.Index) CompileError!Air.Inst.Ref {
385fn resolveBody(sema: *Sema, block: *Block, body: []const Zir.Inst.Index) CompileError!Air.Inst.Ref {
112386 const break_inst = try sema.analyzeBody(block, body);
113387 const operand_ref = sema.code.instructions.items(.data)[break_inst].@"break".operand;
114388 return sema.resolveInst(operand_ref);
......@@ -131,7 +405,7 @@ const always_noreturn: CompileError!Zir.Inst.Index = @as(Zir.Inst.Index, undefin
131405/// well as the operand. No block scope needs to be created for this strategy.
132406pub fn analyzeBody(
133407 sema: *Sema,
134 block: *Scope.Block,
408 block: *Block,
135409 body: []const Zir.Inst.Index,
136410) CompileError!Zir.Inst.Index {
137411 // No tracy calls here, to avoid interfering with the tail call mechanism.
......@@ -148,9 +422,9 @@ pub fn analyzeBody(
148422 wip_captures.deinit();
149423 };
150424
151 const map = &block.sema.inst_map;
152 const tags = block.sema.code.instructions.items(.tag);
153 const datas = block.sema.code.instructions.items(.data);
425 const map = &sema.inst_map;
426 const tags = sema.code.instructions.items(.tag);
427 const datas = sema.code.instructions.items(.data);
154428
155429 var orig_captures: usize = parent_capture_scope.captures.count();
156430
......@@ -673,7 +947,7 @@ pub fn analyzeBody(
673947 return result;
674948}
675949
676fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
950fn zirExtended(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
677951 const extended = sema.code.instructions.items(.data)[inst].extended;
678952 switch (extended.opcode) {
679953 // zig fmt: off
......@@ -725,7 +999,7 @@ pub fn resolveInst(sema: *Sema, zir_ref: Zir.Inst.Ref) Air.Inst.Ref {
725999
7261000fn resolveConstBool(
7271001 sema: *Sema,
728 block: *Scope.Block,
1002 block: *Block,
7291003 src: LazySrcLoc,
7301004 zir_ref: Zir.Inst.Ref,
7311005) !bool {
......@@ -738,7 +1012,7 @@ fn resolveConstBool(
7381012
7391013fn resolveConstString(
7401014 sema: *Sema,
741 block: *Scope.Block,
1015 block: *Block,
7421016 src: LazySrcLoc,
7431017 zir_ref: Zir.Inst.Ref,
7441018) ![]u8 {
......@@ -749,14 +1023,14 @@ fn resolveConstString(
7491023 return val.toAllocatedBytes(sema.arena);
7501024}
7511025
752pub fn resolveType(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) !Type {
1026pub fn resolveType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) !Type {
7531027 const air_inst = sema.resolveInst(zir_ref);
7541028 return sema.analyzeAsType(block, src, air_inst);
7551029}
7561030
7571031fn analyzeAsType(
7581032 sema: *Sema,
759 block: *Scope.Block,
1033 block: *Block,
7601034 src: LazySrcLoc,
7611035 air_inst: Air.Inst.Ref,
7621036) !Type {
......@@ -773,7 +1047,7 @@ fn analyzeAsType(
7731047/// Value Tag `generic_poison` causes `error.GenericPoison` to be returned.
7741048fn resolveValue(
7751049 sema: *Sema,
776 block: *Scope.Block,
1050 block: *Block,
7771051 src: LazySrcLoc,
7781052 air_ref: Air.Inst.Ref,
7791053) CompileError!Value {
......@@ -788,7 +1062,7 @@ fn resolveValue(
7881062/// Value Tag `undef` may be returned.
7891063fn resolveConstMaybeUndefVal(
7901064 sema: *Sema,
791 block: *Scope.Block,
1065 block: *Block,
7921066 src: LazySrcLoc,
7931067 inst: Air.Inst.Ref,
7941068) CompileError!Value {
......@@ -806,7 +1080,7 @@ fn resolveConstMaybeUndefVal(
8061080/// See `resolveValue` for an alternative.
8071081fn resolveConstValue(
8081082 sema: *Sema,
809 block: *Scope.Block,
1083 block: *Block,
8101084 src: LazySrcLoc,
8111085 air_ref: Air.Inst.Ref,
8121086) CompileError!Value {
......@@ -825,7 +1099,7 @@ fn resolveConstValue(
8251099/// Value Tag `undef` causes this function to return a compile error.
8261100fn resolveDefinedValue(
8271101 sema: *Sema,
828 block: *Scope.Block,
1102 block: *Block,
8291103 src: LazySrcLoc,
8301104 air_ref: Air.Inst.Ref,
8311105) CompileError!?Value {
......@@ -843,7 +1117,7 @@ fn resolveDefinedValue(
8431117/// Value Tag `generic_poison` causes `error.GenericPoison` to be returned.
8441118fn resolveMaybeUndefVal(
8451119 sema: *Sema,
846 block: *Scope.Block,
1120 block: *Block,
8471121 src: LazySrcLoc,
8481122 inst: Air.Inst.Ref,
8491123) CompileError!?Value {
......@@ -858,7 +1132,7 @@ fn resolveMaybeUndefVal(
8581132/// Returns all Value tags including `variable` and `undef`.
8591133fn resolveMaybeUndefValAllowVariables(
8601134 sema: *Sema,
861 block: *Scope.Block,
1135 block: *Block,
8621136 src: LazySrcLoc,
8631137 inst: Air.Inst.Ref,
8641138) CompileError!?Value {
......@@ -885,20 +1159,77 @@ fn resolveMaybeUndefValAllowVariables(
8851159 }
8861160}
8871161
888fn failWithNeededComptime(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) CompileError {
889 return sema.mod.fail(&block.base, src, "unable to resolve comptime value", .{});
1162fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
1163 return sema.fail(block, src, "unable to resolve comptime value", .{});
1164}
1165
1166fn failWithUseOfUndef(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
1167 return sema.fail(block, src, "use of undefined value here causes undefined behavior", .{});
8901168}
8911169
892fn failWithUseOfUndef(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) CompileError {
893 return sema.mod.fail(&block.base, src, "use of undefined value here causes undefined behavior", .{});
1170fn failWithDivideByZero(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
1171 return sema.fail(block, src, "division by zero here causes undefined behavior", .{});
8941172}
8951173
896fn failWithDivideByZero(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) CompileError {
897 return sema.mod.fail(&block.base, src, "division by zero here causes undefined behavior", .{});
1174fn failWithModRemNegative(sema: *Sema, block: *Block, src: LazySrcLoc, lhs_ty: Type, rhs_ty: Type) CompileError {
1175 return sema.fail(block, src, "remainder division with '{}' and '{}': signed integers and floats must use @rem or @mod", .{ lhs_ty, rhs_ty });
8981176}
8991177
900fn failWithModRemNegative(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, lhs_ty: Type, rhs_ty: Type) CompileError {
901 return sema.mod.fail(&block.base, src, "remainder division with '{}' and '{}': signed integers and floats must use @rem or @mod", .{ lhs_ty, rhs_ty });
1178/// We don't return a pointer to the new error note because the pointer
1179/// becomes invalid when you add another one.
1180fn errNote(
1181 sema: *Sema,
1182 block: *Block,
1183 src: LazySrcLoc,
1184 parent: *Module.ErrorMsg,
1185 comptime format: []const u8,
1186 args: anytype,
1187) error{OutOfMemory}!void {
1188 return sema.mod.errNoteNonLazy(src.toSrcLoc(block.src_decl), parent, format, args);
1189}
1190
1191fn errMsg(
1192 sema: *Sema,
1193 block: *Block,
1194 src: LazySrcLoc,
1195 comptime format: []const u8,
1196 args: anytype,
1197) error{OutOfMemory}!*Module.ErrorMsg {
1198 return Module.ErrorMsg.create(sema.gpa, src.toSrcLoc(block.src_decl), format, args);
1199}
1200
1201pub fn fail(
1202 sema: *Sema,
1203 block: *Block,
1204 src: LazySrcLoc,
1205 comptime format: []const u8,
1206 args: anytype,
1207) CompileError {
1208 const err_msg = try sema.errMsg(block, src, format, args);
1209 return sema.failWithOwnedErrorMsg(err_msg);
1210}
1211
1212fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
1213 @setCold(true);
1214
1215 const mod = sema.mod;
1216
1217 {
1218 errdefer err_msg.destroy(mod.gpa);
1219 if (err_msg.src_loc.lazy == .unneeded) {
1220 return error.NeededSourceLocation;
1221 }
1222 try mod.failed_decls.ensureUnusedCapacity(mod.gpa, 1);
1223 try mod.failed_files.ensureUnusedCapacity(mod.gpa, 1);
1224 }
1225 if (sema.owner_func) |func| {
1226 func.state = .sema_failure;
1227 } else {
1228 sema.owner_decl.analysis = .sema_failure;
1229 sema.owner_decl.generation = mod.generation;
1230 }
1231 mod.failed_decls.putAssumeCapacityNoClobber(sema.owner_decl, err_msg);
1232 return error.AnalysisFail;
9021233}
9031234
9041235/// Appropriate to call when the coercion has already been done by result
......@@ -907,7 +1238,7 @@ fn failWithModRemNegative(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, lhs
9071238/// TODO don't ever call this since we're migrating towards ResultLoc.coerced_ty.
9081239fn resolveAlreadyCoercedInt(
9091240 sema: *Sema,
910 block: *Scope.Block,
1241 block: *Block,
9111242 src: LazySrcLoc,
9121243 zir_ref: Zir.Inst.Ref,
9131244 comptime Int: type,
......@@ -923,15 +1254,15 @@ fn resolveAlreadyCoercedInt(
9231254
9241255fn resolveAlign(
9251256 sema: *Sema,
926 block: *Scope.Block,
1257 block: *Block,
9271258 src: LazySrcLoc,
9281259 zir_ref: Zir.Inst.Ref,
9291260) !u16 {
9301261 const alignment_big = try sema.resolveInt(block, src, zir_ref, Type.initTag(.u16));
9311262 const alignment = @intCast(u16, alignment_big); // We coerce to u16 in the prev line.
932 if (alignment == 0) return sema.mod.fail(&block.base, src, "alignment must be >= 1", .{});
1263 if (alignment == 0) return sema.fail(block, src, "alignment must be >= 1", .{});
9331264 if (!std.math.isPowerOfTwo(alignment)) {
934 return sema.mod.fail(&block.base, src, "alignment value {d} is not a power of two", .{
1265 return sema.fail(block, src, "alignment value {d} is not a power of two", .{
9351266 alignment,
9361267 });
9371268 }
......@@ -940,7 +1271,7 @@ fn resolveAlign(
9401271
9411272fn resolveInt(
9421273 sema: *Sema,
943 block: *Scope.Block,
1274 block: *Block,
9441275 src: LazySrcLoc,
9451276 zir_ref: Zir.Inst.Ref,
9461277 dest_type: Type,
......@@ -956,7 +1287,7 @@ fn resolveInt(
9561287// a function that does not.
9571288pub fn resolveInstConst(
9581289 sema: *Sema,
959 block: *Scope.Block,
1290 block: *Block,
9601291 src: LazySrcLoc,
9611292 zir_ref: Zir.Inst.Ref,
9621293) CompileError!TypedValue {
......@@ -972,7 +1303,7 @@ pub fn resolveInstConst(
9721303// See `resolveInstConst` for an alternative.
9731304pub fn resolveInstValue(
9741305 sema: *Sema,
975 block: *Scope.Block,
1306 block: *Block,
9761307 src: LazySrcLoc,
9771308 zir_ref: Zir.Inst.Ref,
9781309) CompileError!TypedValue {
......@@ -984,13 +1315,13 @@ pub fn resolveInstValue(
9841315 };
9851316}
9861317
987fn zirBitcastResultPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1318fn zirBitcastResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9881319 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9891320 const src = inst_data.src();
990 return sema.mod.fail(&block.base, src, "TODO implement zir_sema.zirBitcastResultPtr", .{});
1321 return sema.fail(block, src, "TODO implement zir_sema.zirBitcastResultPtr", .{});
9911322}
9921323
993fn zirCoerceResultPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1324fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9941325 const tracy = trace(@src());
9951326 defer tracy.end();
9961327
......@@ -1052,7 +1383,7 @@ pub fn analyzeStructDecl(
10521383
10531384fn zirStructDecl(
10541385 sema: *Sema,
1055 block: *Scope.Block,
1386 block: *Block,
10561387 extended: Zir.Inst.Extended.InstData,
10571388 inst: Zir.Inst.Index,
10581389) CompileError!Air.Inst.Ref {
......@@ -1069,12 +1400,12 @@ fn zirStructDecl(
10691400 const struct_ty = try Type.Tag.@"struct".create(&new_decl_arena.allocator, struct_obj);
10701401 const struct_val = try Value.Tag.ty.create(&new_decl_arena.allocator, struct_ty);
10711402 const type_name = try sema.createTypeName(block, small.name_strategy);
1072 const new_decl = try sema.mod.createAnonymousDeclNamed(&block.base, .{
1403 const new_decl = try sema.mod.createAnonymousDeclNamed(block, .{
10731404 .ty = Type.initTag(.type),
10741405 .val = struct_val,
10751406 }, type_name);
10761407 new_decl.owns_tv = true;
1077 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
1408 errdefer sema.mod.abortAnonDecl(new_decl);
10781409 struct_obj.* = .{
10791410 .owner_decl = new_decl,
10801411 .fields = .{},
......@@ -1084,7 +1415,7 @@ fn zirStructDecl(
10841415 .status = .none,
10851416 .known_has_bits = undefined,
10861417 .namespace = .{
1087 .parent = sema.owner_decl.namespace,
1418 .parent = block.namespace,
10881419 .ty = struct_ty,
10891420 .file_scope = block.getFileScope(),
10901421 },
......@@ -1093,13 +1424,11 @@ fn zirStructDecl(
10931424 &struct_obj.namespace, new_decl, new_decl.name,
10941425 });
10951426 try sema.analyzeStructDecl(new_decl, inst, struct_obj);
1096 try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1);
10971427 try new_decl.finalizeNewArena(&new_decl_arena);
1098 sema.types_pending_resolution.appendAssumeCapacity(struct_ty);
10991428 return sema.analyzeDeclVal(block, src, new_decl);
11001429}
11011430
1102fn createTypeName(sema: *Sema, block: *Scope.Block, name_strategy: Zir.Inst.NameStrategy) ![:0]u8 {
1431fn createTypeName(sema: *Sema, block: *Block, name_strategy: Zir.Inst.NameStrategy) ![:0]u8 {
11031432 switch (name_strategy) {
11041433 .anon => {
11051434 // It would be neat to have "struct:line:column" but this name has
......@@ -1127,7 +1456,7 @@ fn createTypeName(sema: *Sema, block: *Scope.Block, name_strategy: Zir.Inst.Name
11271456
11281457fn zirEnumDecl(
11291458 sema: *Sema,
1130 block: *Scope.Block,
1459 block: *Block,
11311460 extended: Zir.Inst.Extended.InstData,
11321461) CompileError!Air.Inst.Ref {
11331462 const tracy = trace(@src());
......@@ -1180,12 +1509,12 @@ fn zirEnumDecl(
11801509 const enum_ty = Type.initPayload(&enum_ty_payload.base);
11811510 const enum_val = try Value.Tag.ty.create(&new_decl_arena.allocator, enum_ty);
11821511 const type_name = try sema.createTypeName(block, small.name_strategy);
1183 const new_decl = try mod.createAnonymousDeclNamed(&block.base, .{
1512 const new_decl = try mod.createAnonymousDeclNamed(block, .{
11841513 .ty = Type.initTag(.type),
11851514 .val = enum_val,
11861515 }, type_name);
11871516 new_decl.owns_tv = true;
1188 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
1517 errdefer mod.abortAnonDecl(new_decl);
11891518
11901519 enum_obj.* = .{
11911520 .owner_decl = new_decl,
......@@ -1194,7 +1523,7 @@ fn zirEnumDecl(
11941523 .values = .{},
11951524 .node_offset = src.node_offset,
11961525 .namespace = .{
1197 .parent = sema.owner_decl.namespace,
1526 .parent = block.namespace,
11981527 .ty = enum_ty,
11991528 .file_scope = block.getFileScope(),
12001529 },
......@@ -1227,10 +1556,6 @@ fn zirEnumDecl(
12271556 sema.owner_decl = new_decl;
12281557 defer sema.owner_decl = prev_owner_decl;
12291558
1230 const prev_namespace = sema.namespace;
1231 sema.namespace = &enum_obj.namespace;
1232 defer sema.namespace = prev_namespace;
1233
12341559 const prev_owner_func = sema.owner_func;
12351560 sema.owner_func = null;
12361561 defer sema.owner_func = prev_owner_func;
......@@ -1242,10 +1567,11 @@ fn zirEnumDecl(
12421567 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, new_decl.src_scope);
12431568 defer wip_captures.deinit();
12441569
1245 var enum_block: Scope.Block = .{
1570 var enum_block: Block = .{
12461571 .parent = null,
12471572 .sema = sema,
12481573 .src_decl = new_decl,
1574 .namespace = &enum_obj.namespace,
12491575 .wip_capture_scope = wip_captures.scope,
12501576 .instructions = .{},
12511577 .inlining = null,
......@@ -1303,12 +1629,12 @@ fn zirEnumDecl(
13031629 const field_src = enumFieldSrcLoc(block.src_decl, tree.*, src.node_offset, field_i);
13041630 const other_tag_src = enumFieldSrcLoc(block.src_decl, tree.*, src.node_offset, gop.index);
13051631 const msg = msg: {
1306 const msg = try mod.errMsg(&block.base, field_src, "duplicate enum tag", .{});
1632 const msg = try sema.errMsg(block, field_src, "duplicate enum tag", .{});
13071633 errdefer msg.destroy(gpa);
1308 try mod.errNote(&block.base, other_tag_src, msg, "other tag here", .{});
1634 try sema.errNote(block, other_tag_src, msg, "other tag here", .{});
13091635 break :msg msg;
13101636 };
1311 return mod.failWithOwnedErrorMsg(&block.base, msg);
1637 return sema.failWithOwnedErrorMsg(msg);
13121638 }
13131639
13141640 if (has_tag_value) {
......@@ -1331,7 +1657,7 @@ fn zirEnumDecl(
13311657
13321658fn zirUnionDecl(
13331659 sema: *Sema,
1334 block: *Scope.Block,
1660 block: *Block,
13351661 extended: Zir.Inst.Extended.InstData,
13361662 inst: Zir.Inst.Index,
13371663) CompileError!Air.Inst.Ref {
......@@ -1370,12 +1696,12 @@ fn zirUnionDecl(
13701696 const union_ty = Type.initPayload(&union_payload.base);
13711697 const union_val = try Value.Tag.ty.create(&new_decl_arena.allocator, union_ty);
13721698 const type_name = try sema.createTypeName(block, small.name_strategy);
1373 const new_decl = try sema.mod.createAnonymousDeclNamed(&block.base, .{
1699 const new_decl = try sema.mod.createAnonymousDeclNamed(block, .{
13741700 .ty = Type.initTag(.type),
13751701 .val = union_val,
13761702 }, type_name);
13771703 new_decl.owns_tv = true;
1378 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
1704 errdefer sema.mod.abortAnonDecl(new_decl);
13791705 union_obj.* = .{
13801706 .owner_decl = new_decl,
13811707 .tag_ty = Type.initTag(.@"null"),
......@@ -1385,7 +1711,7 @@ fn zirUnionDecl(
13851711 .layout = small.layout,
13861712 .status = .none,
13871713 .namespace = .{
1388 .parent = sema.owner_decl.namespace,
1714 .parent = block.namespace,
13891715 .ty = union_ty,
13901716 .file_scope = block.getFileScope(),
13911717 },
......@@ -1396,15 +1722,13 @@ fn zirUnionDecl(
13961722
13971723 _ = try sema.mod.scanNamespace(&union_obj.namespace, extra_index, decls_len, new_decl);
13981724
1399 try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1);
14001725 try new_decl.finalizeNewArena(&new_decl_arena);
1401 sema.types_pending_resolution.appendAssumeCapacity(union_ty);
14021726 return sema.analyzeDeclVal(block, src, new_decl);
14031727}
14041728
14051729fn zirOpaqueDecl(
14061730 sema: *Sema,
1407 block: *Scope.Block,
1731 block: *Block,
14081732 extended: Zir.Inst.Extended.InstData,
14091733 inst: Zir.Inst.Index,
14101734) CompileError!Air.Inst.Ref {
......@@ -1413,12 +1737,12 @@ fn zirOpaqueDecl(
14131737
14141738 _ = extended;
14151739 _ = inst;
1416 return sema.mod.fail(&block.base, sema.src, "TODO implement zirOpaqueDecl", .{});
1740 return sema.fail(block, sema.src, "TODO implement zirOpaqueDecl", .{});
14171741}
14181742
14191743fn zirErrorSetDecl(
14201744 sema: *Sema,
1421 block: *Scope.Block,
1745 block: *Block,
14221746 inst: Zir.Inst.Index,
14231747 name_strategy: Zir.Inst.NameStrategy,
14241748) CompileError!Air.Inst.Ref {
......@@ -1438,12 +1762,12 @@ fn zirErrorSetDecl(
14381762 const error_set_ty = try Type.Tag.error_set.create(&new_decl_arena.allocator, error_set);
14391763 const error_set_val = try Value.Tag.ty.create(&new_decl_arena.allocator, error_set_ty);
14401764 const type_name = try sema.createTypeName(block, name_strategy);
1441 const new_decl = try sema.mod.createAnonymousDeclNamed(&block.base, .{
1765 const new_decl = try sema.mod.createAnonymousDeclNamed(block, .{
14421766 .ty = Type.initTag(.type),
14431767 .val = error_set_val,
14441768 }, type_name);
14451769 new_decl.owns_tv = true;
1446 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
1770 errdefer sema.mod.abortAnonDecl(new_decl);
14471771 const names = try new_decl_arena.allocator.alloc([]const u8, fields.len);
14481772 for (fields) |str_index, i| {
14491773 names[i] = try new_decl_arena.allocator.dupe(u8, sema.code.nullTerminatedString(str_index));
......@@ -1460,7 +1784,7 @@ fn zirErrorSetDecl(
14601784
14611785fn zirRetPtr(
14621786 sema: *Sema,
1463 block: *Scope.Block,
1787 block: *Block,
14641788 extended: Zir.Inst.Extended.InstData,
14651789) CompileError!Air.Inst.Ref {
14661790 const tracy = trace(@src());
......@@ -1480,7 +1804,7 @@ fn zirRetPtr(
14801804 return block.addTy(.alloc, ptr_type);
14811805}
14821806
1483fn zirRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1807fn zirRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
14841808 const tracy = trace(@src());
14851809 defer tracy.end();
14861810
......@@ -1491,7 +1815,7 @@ fn zirRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
14911815
14921816fn zirRetType(
14931817 sema: *Sema,
1494 block: *Scope.Block,
1818 block: *Block,
14951819 extended: Zir.Inst.Extended.InstData,
14961820) CompileError!Air.Inst.Ref {
14971821 const tracy = trace(@src());
......@@ -1502,7 +1826,7 @@ fn zirRetType(
15021826 return sema.addType(sema.fn_ret_ty);
15031827}
15041828
1505fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
1829fn zirEnsureResultUsed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
15061830 const tracy = trace(@src());
15071831 defer tracy.end();
15081832
......@@ -1515,18 +1839,18 @@ fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) C
15151839
15161840fn ensureResultUsed(
15171841 sema: *Sema,
1518 block: *Scope.Block,
1842 block: *Block,
15191843 operand: Air.Inst.Ref,
15201844 src: LazySrcLoc,
15211845) CompileError!void {
15221846 const operand_ty = sema.typeOf(operand);
15231847 switch (operand_ty.zigTypeTag()) {
15241848 .Void, .NoReturn => return,
1525 else => return sema.mod.fail(&block.base, src, "expression value is ignored", .{}),
1849 else => return sema.fail(block, src, "expression value is ignored", .{}),
15261850 }
15271851}
15281852
1529fn zirEnsureResultNonError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
1853fn zirEnsureResultNonError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
15301854 const tracy = trace(@src());
15311855 defer tracy.end();
15321856
......@@ -1535,12 +1859,12 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
15351859 const src = inst_data.src();
15361860 const operand_ty = sema.typeOf(operand);
15371861 switch (operand_ty.zigTypeTag()) {
1538 .ErrorSet, .ErrorUnion => return sema.mod.fail(&block.base, src, "error is discarded", .{}),
1862 .ErrorSet, .ErrorUnion => return sema.fail(block, src, "error is discarded", .{}),
15391863 else => return,
15401864 }
15411865}
15421866
1543fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1867fn zirIndexablePtrLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
15441868 const tracy = trace(@src());
15451869 defer tracy.end();
15461870
......@@ -1561,15 +1885,15 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
15611885 }
15621886 if (!elem_ty.isIndexable()) {
15631887 const msg = msg: {
1564 const msg = try sema.mod.errMsg(
1565 &block.base,
1888 const msg = try sema.errMsg(
1889 block,
15661890 src,
15671891 "type '{}' does not support indexing",
15681892 .{elem_ty},
15691893 );
15701894 errdefer msg.destroy(sema.gpa);
1571 try sema.mod.errNote(
1572 &block.base,
1895 try sema.errNote(
1896 block,
15731897 src,
15741898 msg,
15751899 "for loop operand must be an array, slice, tuple, or vector",
......@@ -1577,18 +1901,18 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
15771901 );
15781902 break :msg msg;
15791903 };
1580 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);
1904 return sema.failWithOwnedErrorMsg(msg);
15811905 }
15821906 const result_ptr = try sema.fieldPtr(block, src, array, "len", src);
15831907 return sema.analyzeLoad(block, src, result_ptr, src);
15841908 }
15851909
1586 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirIndexablePtrLen", .{});
1910 return sema.fail(block, src, "TODO implement Sema.zirIndexablePtrLen", .{});
15871911}
15881912
15891913fn zirAllocExtended(
15901914 sema: *Sema,
1591 block: *Scope.Block,
1915 block: *Block,
15921916 extended: Zir.Inst.Extended.InstData,
15931917) CompileError!Air.Inst.Ref {
15941918 const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand);
......@@ -1604,7 +1928,7 @@ fn zirAllocExtended(
16041928 extra_index += 1;
16051929 break :blk try sema.resolveType(block, ty_src, type_ref);
16061930 } else {
1607 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirAllocExtended inferred", .{});
1931 return sema.fail(block, src, "TODO implement Sema.zirAllocExtended inferred", .{});
16081932 };
16091933
16101934 const alignment: u16 = if (small.has_align) blk: {
......@@ -1615,11 +1939,11 @@ fn zirAllocExtended(
16151939 } else 0;
16161940
16171941 if (small.is_comptime) {
1618 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirAllocExtended comptime", .{});
1942 return sema.fail(block, src, "TODO implement Sema.zirAllocExtended comptime", .{});
16191943 }
16201944
16211945 if (!small.is_const) {
1622 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirAllocExtended var", .{});
1946 return sema.fail(block, src, "TODO implement Sema.zirAllocExtended var", .{});
16231947 }
16241948
16251949 const ptr_type = try Type.ptr(sema.arena, .{
......@@ -1631,7 +1955,7 @@ fn zirAllocExtended(
16311955 return block.addTy(.alloc, ptr_type);
16321956}
16331957
1634fn zirAllocComptime(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1958fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16351959 const tracy = trace(@src());
16361960 defer tracy.end();
16371961
......@@ -1651,7 +1975,7 @@ fn zirAllocInferredComptime(sema: *Sema, inst: Zir.Inst.Index) CompileError!Air.
16511975 );
16521976}
16531977
1654fn zirAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1978fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16551979 const tracy = trace(@src());
16561980 defer tracy.end();
16571981
......@@ -1667,7 +1991,7 @@ fn zirAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
16671991 return block.addTy(.alloc, ptr_type);
16681992}
16691993
1670fn zirAllocMut(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1994fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16711995 const tracy = trace(@src());
16721996 defer tracy.end();
16731997
......@@ -1689,7 +2013,7 @@ fn zirAllocMut(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
16892013
16902014fn zirAllocInferred(
16912015 sema: *Sema,
1692 block: *Scope.Block,
2016 block: *Block,
16932017 inst: Zir.Inst.Index,
16942018 inferred_alloc_ty: Type,
16952019) CompileError!Air.Inst.Ref {
......@@ -1720,7 +2044,7 @@ fn zirAllocInferred(
17202044 return result;
17212045}
17222046
1723fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2047fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
17242048 const tracy = trace(@src());
17252049 defer tracy.end();
17262050
......@@ -1779,7 +2103,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
17792103 }
17802104}
17812105
1782fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2106fn zirValidateStructInitPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
17832107 const tracy = trace(@src());
17842108 defer tracy.end();
17852109
......@@ -1811,18 +2135,16 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Ind
18112135
18122136fn validateUnionInitPtr(
18132137 sema: *Sema,
1814 block: *Scope.Block,
2138 block: *Block,
18152139 union_obj: *Module.Union,
18162140 init_src: LazySrcLoc,
18172141 instrs: []const Zir.Inst.Index,
18182142 union_ptr: Air.Inst.Ref,
18192143) CompileError!void {
1820 const mod = sema.mod;
1821
18222144 if (instrs.len != 1) {
18232145 // TODO add note for other field
18242146 // TODO add note for union declared here
1825 return mod.fail(&block.base, init_src, "only one union field can be active at once", .{});
2147 return sema.fail(block, init_src, "only one union field can be active at once", .{});
18262148 }
18272149
18282150 const field_ptr = instrs[0];
......@@ -1852,13 +2174,12 @@ fn validateUnionInitPtr(
18522174
18532175fn validateStructInitPtr(
18542176 sema: *Sema,
1855 block: *Scope.Block,
2177 block: *Block,
18562178 struct_obj: *Module.Struct,
18572179 init_src: LazySrcLoc,
18582180 instrs: []const Zir.Inst.Index,
18592181) CompileError!void {
18602182 const gpa = sema.gpa;
1861 const mod = sema.mod;
18622183
18632184 // Maps field index to field_ptr index of where it was already initialized.
18642185 const found_fields = try gpa.alloc(Zir.Inst.Index, struct_obj.fields.count());
......@@ -1877,12 +2198,12 @@ fn validateStructInitPtr(
18772198 const other_field_ptr_data = sema.code.instructions.items(.data)[other_field_ptr].pl_node;
18782199 const other_field_src: LazySrcLoc = .{ .node_offset_back2tok = other_field_ptr_data.src_node };
18792200 const msg = msg: {
1880 const msg = try mod.errMsg(&block.base, field_src, "duplicate field", .{});
2201 const msg = try sema.errMsg(block, field_src, "duplicate field", .{});
18812202 errdefer msg.destroy(gpa);
1882 try mod.errNote(&block.base, other_field_src, msg, "other field here", .{});
2203 try sema.errNote(block, other_field_src, msg, "other field here", .{});
18832204 break :msg msg;
18842205 };
1885 return mod.failWithOwnedErrorMsg(&block.base, msg);
2206 return sema.failWithOwnedErrorMsg(msg);
18862207 }
18872208 found_fields[field_index] = field_ptr;
18882209 }
......@@ -1897,85 +2218,83 @@ fn validateStructInitPtr(
18972218 const template = "missing struct field: {s}";
18982219 const args = .{field_name};
18992220 if (root_msg) |msg| {
1900 try mod.errNote(&block.base, init_src, msg, template, args);
2221 try sema.errNote(block, init_src, msg, template, args);
19012222 } else {
1902 root_msg = try mod.errMsg(&block.base, init_src, template, args);
2223 root_msg = try sema.errMsg(block, init_src, template, args);
19032224 }
19042225 }
19052226 if (root_msg) |msg| {
19062227 const fqn = try struct_obj.getFullyQualifiedName(gpa);
19072228 defer gpa.free(fqn);
1908 try mod.errNoteNonLazy(
2229 try sema.mod.errNoteNonLazy(
19092230 struct_obj.srcLoc(),
19102231 msg,
19112232 "struct '{s}' declared here",
19122233 .{fqn},
19132234 );
1914 return mod.failWithOwnedErrorMsg(&block.base, msg);
2235 return sema.failWithOwnedErrorMsg(msg);
19152236 }
19162237}
19172238
1918fn zirValidateArrayInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2239fn zirValidateArrayInitPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
19192240 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
19202241 const src = inst_data.src();
1921 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirValidateArrayInitPtr", .{});
2242 return sema.fail(block, src, "TODO implement Sema.zirValidateArrayInitPtr", .{});
19222243}
19232244
19242245fn failWithBadFieldAccess(
19252246 sema: *Sema,
1926 block: *Scope.Block,
2247 block: *Block,
19272248 struct_obj: *Module.Struct,
19282249 field_src: LazySrcLoc,
19292250 field_name: []const u8,
19302251) CompileError {
1931 const mod = sema.mod;
19322252 const gpa = sema.gpa;
19332253
19342254 const fqn = try struct_obj.getFullyQualifiedName(gpa);
19352255 defer gpa.free(fqn);
19362256
19372257 const msg = msg: {
1938 const msg = try mod.errMsg(
1939 &block.base,
2258 const msg = try sema.errMsg(
2259 block,
19402260 field_src,
19412261 "no field named '{s}' in struct '{s}'",
19422262 .{ field_name, fqn },
19432263 );
19442264 errdefer msg.destroy(gpa);
1945 try mod.errNoteNonLazy(struct_obj.srcLoc(), msg, "struct declared here", .{});
2265 try sema.mod.errNoteNonLazy(struct_obj.srcLoc(), msg, "struct declared here", .{});
19462266 break :msg msg;
19472267 };
1948 return mod.failWithOwnedErrorMsg(&block.base, msg);
2268 return sema.failWithOwnedErrorMsg(msg);
19492269}
19502270
19512271fn failWithBadUnionFieldAccess(
19522272 sema: *Sema,
1953 block: *Scope.Block,
2273 block: *Block,
19542274 union_obj: *Module.Union,
19552275 field_src: LazySrcLoc,
19562276 field_name: []const u8,
19572277) CompileError {
1958 const mod = sema.mod;
19592278 const gpa = sema.gpa;
19602279
19612280 const fqn = try union_obj.getFullyQualifiedName(gpa);
19622281 defer gpa.free(fqn);
19632282
19642283 const msg = msg: {
1965 const msg = try mod.errMsg(
1966 &block.base,
2284 const msg = try sema.errMsg(
2285 block,
19672286 field_src,
19682287 "no field named '{s}' in union '{s}'",
19692288 .{ field_name, fqn },
19702289 );
19712290 errdefer msg.destroy(gpa);
1972 try mod.errNoteNonLazy(union_obj.srcLoc(), msg, "union declared here", .{});
2291 try sema.mod.errNoteNonLazy(union_obj.srcLoc(), msg, "union declared here", .{});
19732292 break :msg msg;
19742293 };
1975 return mod.failWithOwnedErrorMsg(&block.base, msg);
2294 return sema.failWithOwnedErrorMsg(msg);
19762295}
19772296
1978fn zirStoreToBlockPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2297fn zirStoreToBlockPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
19792298 const tracy = trace(@src());
19802299 defer tracy.end();
19812300
......@@ -2000,7 +2319,7 @@ fn zirStoreToBlockPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
20002319 return sema.storePtr(block, src, bitcasted_ptr, value);
20012320}
20022321
2003fn zirStoreToInferredPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2322fn zirStoreToInferredPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
20042323 const tracy = trace(@src());
20052324 defer tracy.end();
20062325
......@@ -2048,7 +2367,7 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
20482367 unreachable;
20492368}
20502369
2051fn zirSetEvalBranchQuota(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2370fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
20522371 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
20532372 const src = inst_data.src();
20542373 const quota = try sema.resolveAlreadyCoercedInt(block, src, inst_data.operand, u32);
......@@ -2056,7 +2375,7 @@ fn zirSetEvalBranchQuota(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
20562375 sema.branch_quota = quota;
20572376}
20582377
2059fn zirStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2378fn zirStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
20602379 const tracy = trace(@src());
20612380 defer tracy.end();
20622381
......@@ -2066,7 +2385,7 @@ fn zirStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
20662385 return sema.storePtr(block, sema.src, ptr, value);
20672386}
20682387
2069fn zirStoreNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2388fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
20702389 const tracy = trace(@src());
20712390 defer tracy.end();
20722391
......@@ -2078,7 +2397,7 @@ fn zirStoreNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
20782397 return sema.storePtr(block, src, ptr, value);
20792398}
20802399
2081fn zirStr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2400fn zirStr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
20822401 const tracy = trace(@src());
20832402 defer tracy.end();
20842403
......@@ -2097,16 +2416,16 @@ fn zirStr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
20972416 const decl_ty = try Type.Tag.array_u8_sentinel_0.create(&new_decl_arena.allocator, bytes.len);
20982417 const decl_val = try Value.Tag.bytes.create(&new_decl_arena.allocator, bytes);
20992418
2100 const new_decl = try sema.mod.createAnonymousDecl(&block.base, .{
2419 const new_decl = try sema.mod.createAnonymousDecl(block, .{
21012420 .ty = decl_ty,
21022421 .val = decl_val,
21032422 });
2104 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
2423 errdefer sema.mod.abortAnonDecl(new_decl);
21052424 try new_decl.finalizeNewArena(&new_decl_arena);
21062425 return sema.analyzeDeclRef(new_decl);
21072426}
21082427
2109fn zirInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2428fn zirInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
21102429 _ = block;
21112430 const tracy = trace(@src());
21122431 defer tracy.end();
......@@ -2115,7 +2434,7 @@ fn zirInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
21152434 return sema.addIntUnsigned(Type.initTag(.comptime_int), int);
21162435}
21172436
2118fn zirIntBig(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2437fn zirIntBig(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
21192438 _ = block;
21202439 const tracy = trace(@src());
21212440 defer tracy.end();
......@@ -2133,7 +2452,7 @@ fn zirIntBig(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
21332452 );
21342453}
21352454
2136fn zirFloat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2455fn zirFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
21372456 _ = block;
21382457 const arena = sema.arena;
21392458 const number = sema.code.instructions.items(.data)[inst].float;
......@@ -2143,7 +2462,7 @@ fn zirFloat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
21432462 );
21442463}
21452464
2146fn zirFloat128(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2465fn zirFloat128(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
21472466 _ = block;
21482467 const arena = sema.arena;
21492468 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
......@@ -2155,7 +2474,7 @@ fn zirFloat128(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
21552474 );
21562475}
21572476
2158fn zirCompileError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
2477fn zirCompileError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
21592478 const tracy = trace(@src());
21602479 defer tracy.end();
21612480
......@@ -2163,12 +2482,12 @@ fn zirCompileError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
21632482 const src = inst_data.src();
21642483 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
21652484 const msg = try sema.resolveConstString(block, operand_src, inst_data.operand);
2166 return sema.mod.fail(&block.base, src, "{s}", .{msg});
2485 return sema.fail(block, src, "{s}", .{msg});
21672486}
21682487
21692488fn zirCompileLog(
21702489 sema: *Sema,
2171 block: *Scope.Block,
2490 block: *Block,
21722491 extended: Zir.Inst.Extended.InstData,
21732492) CompileError!Air.Inst.Ref {
21742493 var managed = sema.mod.compile_log_text.toManaged(sema.gpa);
......@@ -2200,7 +2519,7 @@ fn zirCompileLog(
22002519 return Air.Inst.Ref.void_value;
22012520}
22022521
2203fn zirPanic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
2522fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
22042523 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
22052524 const src: LazySrcLoc = inst_data.src();
22062525 const msg_inst = sema.resolveInst(inst_data.operand);
......@@ -2208,7 +2527,7 @@ fn zirPanic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
22082527 return sema.panicWithMsg(block, src, msg_inst);
22092528}
22102529
2211fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2530fn zirLoop(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
22122531 const tracy = trace(@src());
22132532 defer tracy.end();
22142533
......@@ -2236,7 +2555,7 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Compil
22362555 .payload = undefined,
22372556 } },
22382557 });
2239 var label: Scope.Block.Label = .{
2558 var label: Block.Label = .{
22402559 .zir_block = inst,
22412560 .merges = .{
22422561 .results = .{},
......@@ -2271,7 +2590,7 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Compil
22712590 return sema.analyzeBlockBody(parent_block, src, &child_block, merges);
22722591}
22732592
2274fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2593fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
22752594 const tracy = trace(@src());
22762595 defer tracy.end();
22772596
......@@ -2282,15 +2601,16 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
22822601
22832602 // we check this here to avoid undefined symbols
22842603 if (!@import("build_options").have_llvm)
2285 return sema.mod.fail(&parent_block.base, src, "cannot do C import on Zig compiler not built with LLVM-extension", .{});
2604 return sema.fail(parent_block, src, "cannot do C import on Zig compiler not built with LLVM-extension", .{});
22862605
22872606 var c_import_buf = std.ArrayList(u8).init(sema.gpa);
22882607 defer c_import_buf.deinit();
22892608
2290 var child_block: Scope.Block = .{
2609 var child_block: Block = .{
22912610 .parent = parent_block,
22922611 .sema = sema,
22932612 .src_decl = parent_block.src_decl,
2613 .namespace = parent_block.namespace,
22942614 .wip_capture_scope = parent_block.wip_capture_scope,
22952615 .instructions = .{},
22962616 .inlining = parent_block.inlining,
......@@ -2302,15 +2622,15 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
23022622 _ = try sema.analyzeBody(&child_block, body);
23032623
23042624 const c_import_res = sema.mod.comp.cImport(c_import_buf.items) catch |err|
2305 return sema.mod.fail(&child_block.base, src, "C import failed: {s}", .{@errorName(err)});
2625 return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
23062626
23072627 if (c_import_res.errors.len != 0) {
23082628 const msg = msg: {
2309 const msg = try sema.mod.errMsg(&child_block.base, src, "C import failed", .{});
2629 const msg = try sema.errMsg(&child_block, src, "C import failed", .{});
23102630 errdefer msg.destroy(sema.gpa);
23112631
23122632 if (!sema.mod.comp.bin_file.options.link_libc)
2313 try sema.mod.errNote(&child_block.base, src, msg, "libc headers not available; compilation does not link against libc", .{});
2633 try sema.errNote(&child_block, src, msg, "libc headers not available; compilation does not link against libc", .{});
23142634
23152635 for (c_import_res.errors) |_| {
23162636 // TODO integrate with LazySrcLoc
......@@ -2322,7 +2642,7 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
23222642 @import("clang.zig").Stage2ErrorMsg.delete(c_import_res.errors.ptr, c_import_res.errors.len);
23232643 break :msg msg;
23242644 };
2325 return sema.mod.failWithOwnedErrorMsg(&child_block.base, msg);
2645 return sema.failWithOwnedErrorMsg(msg);
23262646 }
23272647 const c_import_pkg = Package.create(
23282648 sema.gpa,
......@@ -2338,10 +2658,10 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
23382658 try c_import_pkg.add(sema.gpa, "std", std_pkg);
23392659
23402660 const result = sema.mod.importPkg(c_import_pkg) catch |err|
2341 return sema.mod.fail(&child_block.base, src, "C import failed: {s}", .{@errorName(err)});
2661 return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
23422662
23432663 sema.mod.astGenFile(result.file) catch |err|
2344 return sema.mod.fail(&child_block.base, src, "C import failed: {s}", .{@errorName(err)});
2664 return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
23452665
23462666 try sema.mod.semaFile(result.file);
23472667 const file_root_decl = result.file.root_decl.?;
......@@ -2349,15 +2669,15 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
23492669 return sema.addConstant(file_root_decl.ty, file_root_decl.val);
23502670}
23512671
2352fn zirSuspendBlock(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2672fn zirSuspendBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
23532673 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
23542674 const src = inst_data.src();
2355 return sema.mod.fail(&parent_block.base, src, "TODO: implement Sema.zirSuspendBlock", .{});
2675 return sema.fail(parent_block, src, "TODO: implement Sema.zirSuspendBlock", .{});
23562676}
23572677
23582678fn zirBlock(
23592679 sema: *Sema,
2360 parent_block: *Scope.Block,
2680 parent_block: *Block,
23612681 inst: Zir.Inst.Index,
23622682) CompileError!Air.Inst.Ref {
23632683 const tracy = trace(@src());
......@@ -2378,7 +2698,7 @@ fn zirBlock(
23782698 .data = undefined,
23792699 });
23802700
2381 var label: Scope.Block.Label = .{
2701 var label: Block.Label = .{
23822702 .zir_block = inst,
23832703 .merges = .{
23842704 .results = .{},
......@@ -2387,10 +2707,11 @@ fn zirBlock(
23872707 },
23882708 };
23892709
2390 var child_block: Scope.Block = .{
2710 var child_block: Block = .{
23912711 .parent = parent_block,
23922712 .sema = sema,
23932713 .src_decl = parent_block.src_decl,
2714 .namespace = parent_block.namespace,
23942715 .wip_capture_scope = parent_block.wip_capture_scope,
23952716 .instructions = .{},
23962717 .label = &label,
......@@ -2410,11 +2731,11 @@ fn zirBlock(
24102731
24112732fn resolveBlockBody(
24122733 sema: *Sema,
2413 parent_block: *Scope.Block,
2734 parent_block: *Block,
24142735 src: LazySrcLoc,
2415 child_block: *Scope.Block,
2736 child_block: *Block,
24162737 body: []const Zir.Inst.Index,
2417 merges: *Scope.Block.Merges,
2738 merges: *Block.Merges,
24182739) CompileError!Air.Inst.Ref {
24192740 if (child_block.is_comptime) {
24202741 return sema.resolveBody(child_block, body);
......@@ -2426,10 +2747,10 @@ fn resolveBlockBody(
24262747
24272748fn analyzeBlockBody(
24282749 sema: *Sema,
2429 parent_block: *Scope.Block,
2750 parent_block: *Block,
24302751 src: LazySrcLoc,
2431 child_block: *Scope.Block,
2432 merges: *Scope.Block.Merges,
2752 child_block: *Block,
2753 merges: *Block.Merges,
24332754) CompileError!Air.Inst.Ref {
24342755 const tracy = trace(@src());
24352756 defer tracy.end();
......@@ -2527,7 +2848,7 @@ fn analyzeBlockBody(
25272848 return Air.indexToRef(merges.block_inst);
25282849}
25292850
2530fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2851fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
25312852 const tracy = trace(@src());
25322853 defer tracy.end();
25332854
......@@ -2538,14 +2859,14 @@ fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
25382859 const options_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
25392860 const decl_name = sema.code.nullTerminatedString(extra.decl_name);
25402861 if (extra.namespace != .none) {
2541 return sema.mod.fail(&block.base, src, "TODO: implement exporting with field access", .{});
2862 return sema.fail(block, src, "TODO: implement exporting with field access", .{});
25422863 }
25432864 const decl = try sema.lookupIdentifier(block, operand_src, decl_name);
25442865 const options = try sema.resolveExportOptions(block, options_src, extra.options);
2545 try sema.mod.analyzeExport(block, src, options, decl);
2866 try sema.analyzeExport(block, src, options, decl);
25462867}
25472868
2548fn zirExportValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2869fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
25492870 const tracy = trace(@src());
25502871 defer tracy.end();
25512872
......@@ -2558,45 +2879,122 @@ fn zirExportValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
25582879 const options = try sema.resolveExportOptions(block, options_src, extra.options);
25592880 const decl = switch (operand.val.tag()) {
25602881 .function => operand.val.castTag(.function).?.data.owner_decl,
2561 else => return sema.mod.fail(&block.base, operand_src, "TODO implement exporting arbitrary Value objects", .{}), // TODO put this Value into an anonymous Decl and then export it.
2882 else => return sema.fail(block, operand_src, "TODO implement exporting arbitrary Value objects", .{}), // TODO put this Value into an anonymous Decl and then export it.
25622883 };
2563 try sema.mod.analyzeExport(block, src, options, decl);
2884 try sema.analyzeExport(block, src, options, decl);
25642885}
25652886
2566fn zirSetAlignStack(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2887pub fn analyzeExport(
2888 sema: *Sema,
2889 block: *Block,
2890 src: LazySrcLoc,
2891 borrowed_options: std.builtin.ExportOptions,
2892 exported_decl: *Decl,
2893) !void {
2894 const Export = Module.Export;
25672895 const mod = sema.mod;
2896
2897 try mod.ensureDeclAnalyzed(exported_decl);
2898 switch (exported_decl.ty.zigTypeTag()) {
2899 .Fn => {},
2900 else => return sema.fail(block, src, "unable to export type '{}'", .{exported_decl.ty}),
2901 }
2902
2903 const gpa = mod.gpa;
2904
2905 try mod.decl_exports.ensureUnusedCapacity(gpa, 1);
2906 try mod.export_owners.ensureUnusedCapacity(gpa, 1);
2907
2908 const new_export = try gpa.create(Export);
2909 errdefer gpa.destroy(new_export);
2910
2911 const symbol_name = try gpa.dupe(u8, borrowed_options.name);
2912 errdefer gpa.free(symbol_name);
2913
2914 const section: ?[]const u8 = if (borrowed_options.section) |s| try gpa.dupe(u8, s) else null;
2915 errdefer if (section) |s| gpa.free(s);
2916
2917 const src_decl = block.src_decl;
2918 const owner_decl = sema.owner_decl;
2919
2920 log.debug("exporting Decl '{s}' as symbol '{s}' from Decl '{s}'", .{
2921 exported_decl.name, symbol_name, owner_decl.name,
2922 });
2923
2924 new_export.* = .{
2925 .options = .{
2926 .name = symbol_name,
2927 .linkage = borrowed_options.linkage,
2928 .section = section,
2929 },
2930 .src = src,
2931 .link = switch (mod.comp.bin_file.tag) {
2932 .coff => .{ .coff = {} },
2933 .elf => .{ .elf = .{} },
2934 .macho => .{ .macho = .{} },
2935 .plan9 => .{ .plan9 = null },
2936 .c => .{ .c = {} },
2937 .wasm => .{ .wasm = {} },
2938 .spirv => .{ .spirv = {} },
2939 },
2940 .owner_decl = owner_decl,
2941 .src_decl = src_decl,
2942 .exported_decl = exported_decl,
2943 .status = .in_progress,
2944 };
2945
2946 // Add to export_owners table.
2947 const eo_gop = mod.export_owners.getOrPutAssumeCapacity(owner_decl);
2948 if (!eo_gop.found_existing) {
2949 eo_gop.value_ptr.* = &[0]*Export{};
2950 }
2951 eo_gop.value_ptr.* = try gpa.realloc(eo_gop.value_ptr.*, eo_gop.value_ptr.len + 1);
2952 eo_gop.value_ptr.*[eo_gop.value_ptr.len - 1] = new_export;
2953 errdefer eo_gop.value_ptr.* = gpa.shrink(eo_gop.value_ptr.*, eo_gop.value_ptr.len - 1);
2954
2955 // Add to exported_decl table.
2956 const de_gop = mod.decl_exports.getOrPutAssumeCapacity(exported_decl);
2957 if (!de_gop.found_existing) {
2958 de_gop.value_ptr.* = &[0]*Export{};
2959 }
2960 de_gop.value_ptr.* = try gpa.realloc(de_gop.value_ptr.*, de_gop.value_ptr.len + 1);
2961 de_gop.value_ptr.*[de_gop.value_ptr.len - 1] = new_export;
2962 errdefer de_gop.value_ptr.* = gpa.shrink(de_gop.value_ptr.*, de_gop.value_ptr.len - 1);
2963}
2964
2965fn zirSetAlignStack(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
25682966 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
25692967 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
25702968 const src: LazySrcLoc = inst_data.src();
25712969 const alignment = try sema.resolveAlign(block, operand_src, inst_data.operand);
25722970 if (alignment > 256) {
2573 return mod.fail(&block.base, src, "attempt to @setAlignStack({d}); maximum is 256", .{
2971 return sema.fail(block, src, "attempt to @setAlignStack({d}); maximum is 256", .{
25742972 alignment,
25752973 });
25762974 }
25772975 const func = sema.owner_func orelse
2578 return mod.fail(&block.base, src, "@setAlignStack outside function body", .{});
2976 return sema.fail(block, src, "@setAlignStack outside function body", .{});
25792977
25802978 switch (func.owner_decl.ty.fnCallingConvention()) {
2581 .Naked => return mod.fail(&block.base, src, "@setAlignStack in naked function", .{}),
2582 .Inline => return mod.fail(&block.base, src, "@setAlignStack in inline function", .{}),
2979 .Naked => return sema.fail(block, src, "@setAlignStack in naked function", .{}),
2980 .Inline => return sema.fail(block, src, "@setAlignStack in inline function", .{}),
25832981 else => {},
25842982 }
25852983
2586 const gop = try mod.align_stack_fns.getOrPut(mod.gpa, func);
2984 const gop = try sema.mod.align_stack_fns.getOrPut(sema.mod.gpa, func);
25872985 if (gop.found_existing) {
25882986 const msg = msg: {
2589 const msg = try mod.errMsg(&block.base, src, "multiple @setAlignStack in the same function body", .{});
2590 errdefer msg.destroy(mod.gpa);
2591 try mod.errNote(&block.base, src, msg, "other instance here", .{});
2987 const msg = try sema.errMsg(block, src, "multiple @setAlignStack in the same function body", .{});
2988 errdefer msg.destroy(sema.gpa);
2989 try sema.errNote(block, src, msg, "other instance here", .{});
25922990 break :msg msg;
25932991 };
2594 return mod.failWithOwnedErrorMsg(&block.base, msg);
2992 return sema.failWithOwnedErrorMsg(msg);
25952993 }
25962994 gop.value_ptr.* = .{ .alignment = alignment, .src = src };
25972995}
25982996
2599fn zirSetCold(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
2997fn zirSetCold(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
26002998 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
26012999 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
26023000 const is_cold = try sema.resolveConstBool(block, operand_src, inst_data.operand);
......@@ -2604,19 +3002,19 @@ fn zirSetCold(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
26043002 func.is_cold = is_cold;
26053003}
26063004
2607fn zirSetFloatMode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
3005fn zirSetFloatMode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
26083006 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
26093007 const src: LazySrcLoc = inst_data.src();
2610 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirSetFloatMode", .{});
3008 return sema.fail(block, src, "TODO: implement Sema.zirSetFloatMode", .{});
26113009}
26123010
2613fn zirSetRuntimeSafety(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
3011fn zirSetRuntimeSafety(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
26143012 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
26153013 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
26163014 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand);
26173015}
26183016
2619fn zirFence(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
3017fn zirFence(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
26203018 if (block.is_comptime) return;
26213019
26223020 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
......@@ -2624,7 +3022,7 @@ fn zirFence(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
26243022 const order = try sema.resolveAtomicOrder(block, order_src, inst_data.operand);
26253023
26263024 if (@enumToInt(order) < @enumToInt(std.builtin.AtomicOrder.Acquire)) {
2627 return sema.mod.fail(&block.base, order_src, "atomic ordering must be Acquire or stricter", .{});
3025 return sema.fail(block, order_src, "atomic ordering must be Acquire or stricter", .{});
26283026 }
26293027
26303028 _ = try block.addInst(.{
......@@ -2633,7 +3031,7 @@ fn zirFence(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
26333031 });
26343032}
26353033
2636fn zirBreak(sema: *Sema, start_block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
3034fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
26373035 const tracy = trace(@src());
26383036 defer tracy.end();
26393037
......@@ -2655,7 +3053,7 @@ fn zirBreak(sema: *Sema, start_block: *Scope.Block, inst: Zir.Inst.Index) Compil
26553053 }
26563054}
26573055
2658fn zirDbgStmt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
3056fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
26593057 const tracy = trace(@src());
26603058 defer tracy.end();
26613059
......@@ -2675,7 +3073,7 @@ fn zirDbgStmt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
26753073 });
26763074}
26773075
2678fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3076fn zirDeclRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
26793077 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
26803078 const src = inst_data.src();
26813079 const decl_name = inst_data.get(sema.code);
......@@ -2683,7 +3081,7 @@ fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
26833081 return sema.analyzeDeclRef(decl);
26843082}
26853083
2686fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3084fn zirDeclVal(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
26873085 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
26883086 const src = inst_data.src();
26893087 const decl_name = inst_data.get(sema.code);
......@@ -2691,8 +3089,8 @@ fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
26913089 return sema.analyzeDeclVal(block, src, decl);
26923090}
26933091
2694fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []const u8) !*Decl {
2695 var namespace = sema.namespace;
3092fn lookupIdentifier(sema: *Sema, block: *Block, src: LazySrcLoc, name: []const u8) !*Decl {
3093 var namespace = block.namespace;
26963094 while (true) {
26973095 if (try sema.lookupInNamespace(block, src, namespace, name, false)) |decl| {
26983096 return decl;
......@@ -2706,9 +3104,9 @@ fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []c
27063104/// only for ones in the specified namespace.
27073105fn lookupInNamespace(
27083106 sema: *Sema,
2709 block: *Scope.Block,
3107 block: *Block,
27103108 src: LazySrcLoc,
2711 namespace: *Scope.Namespace,
3109 namespace: *Namespace,
27123110 ident_name: []const u8,
27133111 observe_usingnamespace: bool,
27143112) CompileError!?*Decl {
......@@ -2721,10 +3119,10 @@ fn lookupInNamespace(
27213119 }
27223120
27233121 if (observe_usingnamespace and namespace.usingnamespace_set.count() != 0) {
2724 const src_file = block.src_decl.namespace.file_scope;
3122 const src_file = block.namespace.file_scope;
27253123
27263124 const gpa = sema.gpa;
2727 var checked_namespaces: std.AutoArrayHashMapUnmanaged(*Scope.Namespace, void) = .{};
3125 var checked_namespaces: std.AutoArrayHashMapUnmanaged(*Namespace, void) = .{};
27283126 defer checked_namespaces.deinit(gpa);
27293127
27303128 // Keep track of name conflicts for error notes.
......@@ -2739,7 +3137,7 @@ fn lookupInNamespace(
27393137 if (check_ns.decls.get(ident_name)) |decl| {
27403138 // Skip decls which are not marked pub, which are in a different
27413139 // file than the `a.b`/`@hasDecl` syntax.
2742 if (decl.is_pub or src_file == decl.namespace.file_scope) {
3140 if (decl.is_pub or src_file == decl.getFileScope()) {
27433141 try candidates.append(gpa, decl);
27443142 }
27453143 }
......@@ -2747,7 +3145,7 @@ fn lookupInNamespace(
27473145 while (it.next()) |entry| {
27483146 const sub_usingnamespace_decl = entry.key_ptr.*;
27493147 const sub_is_pub = entry.value_ptr.*;
2750 if (!sub_is_pub and src_file != sub_usingnamespace_decl.namespace.file_scope) {
3148 if (!sub_is_pub and src_file != sub_usingnamespace_decl.getFileScope()) {
27513149 // Skip usingnamespace decls which are not marked pub, which are in
27523150 // a different file than the `a.b`/`@hasDecl` syntax.
27533151 continue;
......@@ -2768,7 +3166,7 @@ fn lookupInNamespace(
27683166 },
27693167 else => {
27703168 const msg = msg: {
2771 const msg = try mod.errMsg(&block.base, src, "ambiguous reference", .{});
3169 const msg = try sema.errMsg(block, src, "ambiguous reference", .{});
27723170 errdefer msg.destroy(gpa);
27733171 for (candidates.items) |candidate| {
27743172 const src_loc = candidate.srcLoc();
......@@ -2776,7 +3174,7 @@ fn lookupInNamespace(
27763174 }
27773175 break :msg msg;
27783176 };
2779 return mod.failWithOwnedErrorMsg(&block.base, msg);
3177 return sema.failWithOwnedErrorMsg(msg);
27803178 },
27813179 }
27823180 } else if (namespace.decls.get(ident_name)) |decl| {
......@@ -2796,7 +3194,7 @@ fn lookupInNamespace(
27963194
27973195fn zirCall(
27983196 sema: *Sema,
2799 block: *Scope.Block,
3197 block: *Block,
28003198 inst: Zir.Inst.Index,
28013199) CompileError!Air.Inst.Ref {
28023200 const tracy = trace(@src());
......@@ -2898,7 +3296,7 @@ const GenericRemoveAdapter = struct {
28983296
28993297fn analyzeCall(
29003298 sema: *Sema,
2901 block: *Scope.Block,
3299 block: *Block,
29023300 func: Air.Inst.Ref,
29033301 func_src: LazySrcLoc,
29043302 call_src: LazySrcLoc,
......@@ -2910,14 +3308,14 @@ fn analyzeCall(
29103308
29113309 const func_ty = sema.typeOf(func);
29123310 if (func_ty.zigTypeTag() != .Fn)
2913 return mod.fail(&block.base, func_src, "type '{}' not a function", .{func_ty});
3311 return sema.fail(block, func_src, "type '{}' not a function", .{func_ty});
29143312
29153313 const func_ty_info = func_ty.fnInfo();
29163314 const cc = func_ty_info.cc;
29173315 if (cc == .Naked) {
29183316 // TODO add error note: declared here
2919 return mod.fail(
2920 &block.base,
3317 return sema.fail(
3318 block,
29213319 func_src,
29223320 "unable to call function with naked calling convention",
29233321 .{},
......@@ -2928,8 +3326,8 @@ fn analyzeCall(
29283326 assert(cc == .C);
29293327 if (uncasted_args.len < fn_params_len) {
29303328 // TODO add error note: declared here
2931 return mod.fail(
2932 &block.base,
3329 return sema.fail(
3330 block,
29333331 func_src,
29343332 "expected at least {d} argument(s), found {d}",
29353333 .{ fn_params_len, uncasted_args.len },
......@@ -2937,8 +3335,8 @@ fn analyzeCall(
29373335 }
29383336 } else if (fn_params_len != uncasted_args.len) {
29393337 // TODO add error note: declared here
2940 return mod.fail(
2941 &block.base,
3338 return sema.fail(
3339 block,
29423340 func_src,
29433341 "expected {d} argument(s), found {d}",
29443342 .{ fn_params_len, uncasted_args.len },
......@@ -2956,7 +3354,7 @@ fn analyzeCall(
29563354 .never_inline,
29573355 .no_async,
29583356 .always_tail,
2959 => return mod.fail(&block.base, call_src, "TODO implement call with modifier {}", .{
3357 => return sema.fail(block, call_src, "TODO implement call with modifier {}", .{
29603358 modifier,
29613359 }),
29623360 }
......@@ -2971,7 +3369,7 @@ fn analyzeCall(
29713369 const func_val = try sema.resolveConstValue(block, func_src, func);
29723370 const module_fn = switch (func_val.tag()) {
29733371 .function => func_val.castTag(.function).?.data,
2974 .extern_fn => return mod.fail(&block.base, call_src, "{s} call of extern function", .{
3372 .extern_fn => return sema.fail(block, call_src, "{s} call of extern function", .{
29753373 @as([]const u8, if (is_comptime_call) "comptime" else "inline"),
29763374 }),
29773375 else => unreachable,
......@@ -2979,7 +3377,7 @@ fn analyzeCall(
29793377
29803378 // Analyze the ZIR. The same ZIR gets analyzed into a runtime function
29813379 // or an inlined call depending on what union tag the `label` field is
2982 // set to in the `Scope.Block`.
3380 // set to in the `Block`.
29833381 // This block instruction will be used to capture the return value from the
29843382 // inlined function.
29853383 const block_inst = @intCast(Air.Inst.Index, sema.air_instructions.len);
......@@ -2989,7 +3387,7 @@ fn analyzeCall(
29893387 });
29903388 // This one is shared among sub-blocks within the same callee, but not
29913389 // shared among the entire inline/comptime call stack.
2992 var inlining: Scope.Block.Inlining = .{
3390 var inlining: Block.Inlining = .{
29933391 .comptime_result = undefined,
29943392 .merges = .{
29953393 .results = .{},
......@@ -3000,7 +3398,7 @@ fn analyzeCall(
30003398 // In order to save a bit of stack space, directly modify Sema rather
30013399 // than create a child one.
30023400 const parent_zir = sema.code;
3003 sema.code = module_fn.owner_decl.namespace.file_scope.zir;
3401 sema.code = module_fn.owner_decl.getFileScope().zir;
30043402 defer sema.code = parent_zir;
30053403
30063404 const parent_inst_map = sema.inst_map;
......@@ -3010,10 +3408,6 @@ fn analyzeCall(
30103408 sema.inst_map = parent_inst_map;
30113409 }
30123410
3013 const parent_namespace = sema.namespace;
3014 sema.namespace = module_fn.owner_decl.namespace;
3015 defer sema.namespace = parent_namespace;
3016
30173411 const parent_func = sema.func;
30183412 sema.func = module_fn;
30193413 defer sema.func = parent_func;
......@@ -3021,10 +3415,11 @@ fn analyzeCall(
30213415 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, module_fn.owner_decl.src_scope);
30223416 defer wip_captures.deinit();
30233417
3024 var child_block: Scope.Block = .{
3418 var child_block: Block = .{
30253419 .parent = null,
30263420 .sema = sema,
30273421 .src_decl = module_fn.owner_decl,
3422 .namespace = module_fn.owner_decl.src_namespace,
30283423 .wip_capture_scope = wip_captures.scope,
30293424 .instructions = .{},
30303425 .label = null,
......@@ -3180,12 +3575,6 @@ fn analyzeCall(
31803575 });
31813576 delete_memoized_call_key = false;
31823577 }
3183
3184 // Much like in `Module.semaDecl`, if the result is a struct or union type,
3185 // we need to resolve the field type expressions right here, right now, while
3186 // the child `Sema` is still available, with the AIR instruction map intact,
3187 // because the field type expressions may reference into it.
3188 try sema.resolvePendingTypes(&child_block);
31893578 }
31903579
31913580 break :res2 result;
......@@ -3200,7 +3589,7 @@ fn analyzeCall(
32003589 // Check the Module's generic function map with an adapted context, so that we
32013590 // can match against `uncasted_args` rather than doing the work below to create a
32023591 // generic Scope only to junk it if it matches an existing instantiation.
3203 const namespace = module_fn.owner_decl.namespace;
3592 const namespace = module_fn.owner_decl.src_namespace;
32043593 const fn_zir = namespace.file_scope.zir;
32053594 const fn_info = fn_zir.getFnInfo(module_fn.zir_body_inst);
32063595 const zir_tags = fn_zir.instructions.items(.tag);
......@@ -3276,12 +3665,12 @@ fn analyzeCall(
32763665
32773666 // Create a Decl for the new function.
32783667 const src_decl = namespace.getDecl();
3279 const new_decl = try mod.allocateNewDecl(namespace, module_fn.owner_decl.src_node, src_decl.src_scope);
32803668 // TODO better names for generic function instantiations
32813669 const name_index = mod.getNextAnonNameIndex();
3282 new_decl.name = try std.fmt.allocPrintZ(gpa, "{s}__anon_{d}", .{
3670 const decl_name = try std.fmt.allocPrintZ(gpa, "{s}__anon_{d}", .{
32833671 module_fn.owner_decl.name, name_index,
32843672 });
3673 const new_decl = try mod.allocateNewDecl(decl_name, namespace, module_fn.owner_decl.src_node, src_decl.src_scope);
32853674 new_decl.src_line = module_fn.owner_decl.src_line;
32863675 new_decl.is_pub = module_fn.owner_decl.is_pub;
32873676 new_decl.is_exported = module_fn.owner_decl.is_exported;
......@@ -3297,6 +3686,11 @@ fn analyzeCall(
32973686
32983687 namespace.anon_decls.putAssumeCapacityNoClobber(new_decl, {});
32993688
3689 // The generic function Decl is guaranteed to be the first dependency
3690 // of each of its instantiations.
3691 assert(new_decl.dependencies.keys().len == 0);
3692 try mod.declareDeclDependency(new_decl, module_fn.owner_decl);
3693
33003694 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);
33013695 errdefer new_decl_arena.deinit();
33023696
......@@ -3311,7 +3705,6 @@ fn analyzeCall(
33113705 .perm_arena = &new_decl_arena.allocator,
33123706 .code = fn_zir,
33133707 .owner_decl = new_decl,
3314 .namespace = namespace,
33153708 .func = null,
33163709 .fn_ret_ty = Type.initTag(.void),
33173710 .owner_func = null,
......@@ -3324,10 +3717,11 @@ fn analyzeCall(
33243717 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, new_decl.src_scope);
33253718 defer wip_captures.deinit();
33263719
3327 var child_block: Scope.Block = .{
3720 var child_block: Block = .{
33283721 .parent = null,
33293722 .sema = &child_sema,
33303723 .src_decl = new_decl,
3724 .namespace = namespace,
33313725 .wip_capture_scope = wip_captures.scope,
33323726 .instructions = .{},
33333727 .inlining = null,
......@@ -3430,11 +3824,6 @@ fn analyzeCall(
34303824 });
34313825 assert(!new_decl.ty.fnInfo().is_generic);
34323826
3433 // The generic function Decl is guaranteed to be the first dependency
3434 // of each of its instantiations.
3435 assert(new_decl.dependencies.keys().len == 0);
3436 try mod.declareDeclDependency(new_decl, module_fn.owner_decl);
3437
34383827 // Queue up a `codegen_func` work item for the new Fn. The `comptime_args` field
34393828 // will be populated, ensuring it will have `analyzeBody` called with the ZIR
34403829 // parameters mapped appropriately.
......@@ -3489,7 +3878,7 @@ fn analyzeCall(
34893878
34903879fn finishGenericCall(
34913880 sema: *Sema,
3492 block: *Scope.Block,
3881 block: *Block,
34933882 call_src: LazySrcLoc,
34943883 callee: *Module.Fn,
34953884 func_src: LazySrcLoc,
......@@ -3556,7 +3945,7 @@ fn finishGenericCall(
35563945 return func_inst;
35573946}
35583947
3559fn zirIntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3948fn zirIntType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
35603949 _ = block;
35613950 const tracy = trace(@src());
35623951 defer tracy.end();
......@@ -3567,7 +3956,7 @@ fn zirIntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
35673956 return sema.addType(ty);
35683957}
35693958
3570fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3959fn zirOptionalType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
35713960 const tracy = trace(@src());
35723961 defer tracy.end();
35733962
......@@ -3579,7 +3968,7 @@ fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
35793968 return sema.addType(opt_type);
35803969}
35813970
3582fn zirElemType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3971fn zirElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
35833972 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
35843973 const src = inst_data.src();
35853974 const array_type = try sema.resolveType(block, src, inst_data.operand);
......@@ -3587,7 +3976,7 @@ fn zirElemType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
35873976 return sema.addType(elem_type);
35883977}
35893978
3590fn zirVectorType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3979fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
35913980 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
35923981 const elem_type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
35933982 const len_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
......@@ -3601,7 +3990,7 @@ fn zirVectorType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
36013990 return sema.addType(vector_type);
36023991}
36033992
3604fn zirArrayType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3993fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
36053994 const tracy = trace(@src());
36063995 defer tracy.end();
36073996
......@@ -3613,7 +4002,7 @@ fn zirArrayType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
36134002 return sema.addType(array_ty);
36144003}
36154004
3616fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4005fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
36174006 const tracy = trace(@src());
36184007 defer tracy.end();
36194008
......@@ -3629,7 +4018,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
36294018 return sema.addType(array_ty);
36304019}
36314020
3632fn zirAnyframeType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4021fn zirAnyframeType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
36334022 const tracy = trace(@src());
36344023 defer tracy.end();
36354024
......@@ -3641,7 +4030,7 @@ fn zirAnyframeType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
36414030 return sema.addType(anyframe_type);
36424031}
36434032
3644fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4033fn zirErrorUnionType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
36454034 const tracy = trace(@src());
36464035 defer tracy.end();
36474036
......@@ -3653,7 +4042,7 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com
36534042 const payload = try sema.resolveType(block, rhs_src, extra.rhs);
36544043
36554044 if (error_union.zigTypeTag() != .ErrorSet) {
3656 return sema.mod.fail(&block.base, lhs_src, "expected error set type, found {}", .{
4045 return sema.fail(block, lhs_src, "expected error set type, found {}", .{
36574046 error_union.elemType(),
36584047 });
36594048 }
......@@ -3661,7 +4050,7 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com
36614050 return sema.addType(err_union_ty);
36624051}
36634052
3664fn zirErrorValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4053fn zirErrorValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
36654054 _ = block;
36664055 const tracy = trace(@src());
36674056 defer tracy.end();
......@@ -3679,7 +4068,7 @@ fn zirErrorValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
36794068 );
36804069}
36814070
3682fn zirErrorToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4071fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
36834072 const tracy = trace(@src());
36844073 defer tracy.end();
36854074
......@@ -3706,7 +4095,7 @@ fn zirErrorToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
37064095 return block.addTyOp(.bitcast, result_ty, op_coerced);
37074096}
37084097
3709fn zirIntToError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4098fn zirIntToError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
37104099 const tracy = trace(@src());
37114100 defer tracy.end();
37124101
......@@ -3719,7 +4108,7 @@ fn zirIntToError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
37194108 if (try sema.resolveDefinedValue(block, operand_src, op)) |value| {
37204109 const int = value.toUnsignedInt();
37214110 if (int > sema.mod.global_error_set.count() or int == 0)
3722 return sema.mod.fail(&block.base, operand_src, "integer value {d} represents no error", .{int});
4111 return sema.fail(block, operand_src, "integer value {d} represents no error", .{int});
37234112 const payload = try sema.arena.create(Value.Payload.Error);
37244113 payload.* = .{
37254114 .base = .{ .tag = .@"error" },
......@@ -3729,14 +4118,14 @@ fn zirIntToError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
37294118 }
37304119 try sema.requireRuntimeBlock(block, src);
37314120 if (block.wantSafety()) {
3732 return sema.mod.fail(&block.base, src, "TODO: get max errors in compilation", .{});
4121 return sema.fail(block, src, "TODO: get max errors in compilation", .{});
37334122 // const is_gt_max = @panic("TODO get max errors in compilation");
37344123 // try sema.addSafetyCheck(block, is_gt_max, .invalid_error_code);
37354124 }
37364125 return block.addTyOp(.bitcast, Type.initTag(.anyerror), op);
37374126}
37384127
3739fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4128fn zirMergeErrorSets(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
37404129 const tracy = trace(@src());
37414130 defer tracy.end();
37424131
......@@ -3749,19 +4138,19 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com
37494138 const rhs = sema.resolveInst(extra.rhs);
37504139 if (sema.typeOf(lhs).zigTypeTag() == .Bool and sema.typeOf(rhs).zigTypeTag() == .Bool) {
37514140 const msg = msg: {
3752 const msg = try sema.mod.errMsg(&block.base, lhs_src, "expected error set type, found 'bool'", .{});
4141 const msg = try sema.errMsg(block, lhs_src, "expected error set type, found 'bool'", .{});
37534142 errdefer msg.destroy(sema.gpa);
3754 try sema.mod.errNote(&block.base, src, msg, "'||' merges error sets; 'or' performs boolean OR", .{});
4143 try sema.errNote(block, src, msg, "'||' merges error sets; 'or' performs boolean OR", .{});
37554144 break :msg msg;
37564145 };
3757 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);
4146 return sema.failWithOwnedErrorMsg(msg);
37584147 }
37594148 const lhs_ty = try sema.analyzeAsType(block, lhs_src, lhs);
37604149 const rhs_ty = try sema.analyzeAsType(block, rhs_src, rhs);
37614150 if (lhs_ty.zigTypeTag() != .ErrorSet)
3762 return sema.mod.fail(&block.base, lhs_src, "expected error set type, found {}", .{lhs_ty});
4151 return sema.fail(block, lhs_src, "expected error set type, found {}", .{lhs_ty});
37634152 if (rhs_ty.zigTypeTag() != .ErrorSet)
3764 return sema.mod.fail(&block.base, rhs_src, "expected error set type, found {}", .{rhs_ty});
4153 return sema.fail(block, rhs_src, "expected error set type, found {}", .{rhs_ty});
37654154
37664155 // Anything merged with anyerror is anyerror.
37674156 if (lhs_ty.tag() == .anyerror or rhs_ty.tag() == .anyerror) {
......@@ -3820,7 +4209,7 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com
38204209 return sema.addConstant(Type.initTag(.type), try Value.Tag.ty.create(sema.arena, error_set_ty));
38214210}
38224211
3823fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4212fn zirEnumLiteral(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
38244213 _ = block;
38254214 const tracy = trace(@src());
38264215 defer tracy.end();
......@@ -3833,8 +4222,7 @@ fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
38334222 );
38344223}
38354224
3836fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3837 const mod = sema.mod;
4225fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
38384226 const arena = sema.arena;
38394227 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
38404228 const src = inst_data.src();
......@@ -3846,17 +4234,17 @@ fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
38464234 .Enum => operand,
38474235 .Union => {
38484236 //if (!operand_ty.unionHasTag()) {
3849 // return mod.fail(
3850 // &block.base,
4237 // return sema.fail(
4238 // block,
38514239 // operand_src,
38524240 // "untagged union '{}' cannot be converted to integer",
38534241 // .{dest_ty_src},
38544242 // );
38554243 //}
3856 return mod.fail(&block.base, operand_src, "TODO zirEnumToInt for tagged unions", .{});
4244 return sema.fail(block, operand_src, "TODO zirEnumToInt for tagged unions", .{});
38574245 },
38584246 else => {
3859 return mod.fail(&block.base, operand_src, "expected enum or tagged union, found {}", .{
4247 return sema.fail(block, operand_src, "expected enum or tagged union, found {}", .{
38604248 operand_ty,
38614249 });
38624250 },
......@@ -3880,9 +4268,8 @@ fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
38804268 return block.addTyOp(.bitcast, int_tag_ty, enum_tag);
38814269}
38824270
3883fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3884 const mod = sema.mod;
3885 const target = mod.getTarget();
4271fn zirIntToEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4272 const target = sema.mod.getTarget();
38864273 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
38874274 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
38884275 const src = inst_data.src();
......@@ -3892,7 +4279,7 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
38924279 const operand = sema.resolveInst(extra.rhs);
38934280
38944281 if (dest_ty.zigTypeTag() != .Enum) {
3895 return mod.fail(&block.base, dest_ty_src, "expected enum, found {}", .{dest_ty});
4282 return sema.fail(block, dest_ty_src, "expected enum, found {}", .{dest_ty});
38964283 }
38974284
38984285 if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |int_val| {
......@@ -3904,14 +4291,14 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
39044291 }
39054292 if (!dest_ty.enumHasInt(int_val, target)) {
39064293 const msg = msg: {
3907 const msg = try mod.errMsg(
3908 &block.base,
4294 const msg = try sema.errMsg(
4295 block,
39094296 src,
39104297 "enum '{}' has no tag with value {}",
39114298 .{ dest_ty, int_val },
39124299 );
39134300 errdefer msg.destroy(sema.gpa);
3914 try mod.errNoteNonLazy(
4301 try sema.mod.errNoteNonLazy(
39154302 dest_ty.declSrcLoc(),
39164303 msg,
39174304 "enum declared here",
......@@ -3919,7 +4306,7 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
39194306 );
39204307 break :msg msg;
39214308 };
3922 return mod.failWithOwnedErrorMsg(&block.base, msg);
4309 return sema.failWithOwnedErrorMsg(msg);
39234310 }
39244311 return sema.addConstant(dest_ty, int_val);
39254312 }
......@@ -3931,7 +4318,7 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
39314318/// Pointer in, pointer out.
39324319fn zirOptionalPayloadPtr(
39334320 sema: *Sema,
3934 block: *Scope.Block,
4321 block: *Block,
39354322 inst: Zir.Inst.Index,
39364323 safety_check: bool,
39374324) CompileError!Air.Inst.Ref {
......@@ -3946,7 +4333,7 @@ fn zirOptionalPayloadPtr(
39464333
39474334 const opt_type = optional_ptr_ty.elemType();
39484335 if (opt_type.zigTypeTag() != .Optional) {
3949 return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type});
4336 return sema.fail(block, src, "expected optional type, found {}", .{opt_type});
39504337 }
39514338
39524339 const child_type = try opt_type.optionalChildAlloc(sema.arena);
......@@ -3959,7 +4346,7 @@ fn zirOptionalPayloadPtr(
39594346 if (try sema.resolveDefinedValue(block, src, optional_ptr)) |pointer_val| {
39604347 if (try pointer_val.pointerDeref(sema.arena)) |val| {
39614348 if (val.isNull()) {
3962 return sema.mod.fail(&block.base, src, "unable to unwrap null", .{});
4349 return sema.fail(block, src, "unable to unwrap null", .{});
39634350 }
39644351 // The same Value represents the pointer to the optional and the payload.
39654352 return sema.addConstant(
......@@ -3980,7 +4367,7 @@ fn zirOptionalPayloadPtr(
39804367/// Value in, value out.
39814368fn zirOptionalPayload(
39824369 sema: *Sema,
3983 block: *Scope.Block,
4370 block: *Block,
39844371 inst: Zir.Inst.Index,
39854372 safety_check: bool,
39864373) CompileError!Air.Inst.Ref {
......@@ -3993,14 +4380,14 @@ fn zirOptionalPayload(
39934380 const operand_ty = sema.typeOf(operand);
39944381 const opt_type = operand_ty;
39954382 if (opt_type.zigTypeTag() != .Optional) {
3996 return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type});
4383 return sema.fail(block, src, "expected optional type, found {}", .{opt_type});
39974384 }
39984385
39994386 const child_type = try opt_type.optionalChildAlloc(sema.arena);
40004387
40014388 if (try sema.resolveDefinedValue(block, src, operand)) |val| {
40024389 if (val.isNull()) {
4003 return sema.mod.fail(&block.base, src, "unable to unwrap null", .{});
4390 return sema.fail(block, src, "unable to unwrap null", .{});
40044391 }
40054392 const sub_val = val.castTag(.opt_payload).?.data;
40064393 return sema.addConstant(child_type, sub_val);
......@@ -4017,7 +4404,7 @@ fn zirOptionalPayload(
40174404/// Value in, value out
40184405fn zirErrUnionPayload(
40194406 sema: *Sema,
4020 block: *Scope.Block,
4407 block: *Block,
40214408 inst: Zir.Inst.Index,
40224409 safety_check: bool,
40234410) CompileError!Air.Inst.Ref {
......@@ -4030,11 +4417,11 @@ fn zirErrUnionPayload(
40304417 const operand_src = src;
40314418 const operand_ty = sema.typeOf(operand);
40324419 if (operand_ty.zigTypeTag() != .ErrorUnion)
4033 return sema.mod.fail(&block.base, operand_src, "expected error union type, found '{}'", .{operand_ty});
4420 return sema.fail(block, operand_src, "expected error union type, found '{}'", .{operand_ty});
40344421
40354422 if (try sema.resolveDefinedValue(block, src, operand)) |val| {
40364423 if (val.getError()) |name| {
4037 return sema.mod.fail(&block.base, src, "caught unexpected error '{s}'", .{name});
4424 return sema.fail(block, src, "caught unexpected error '{s}'", .{name});
40384425 }
40394426 const data = val.castTag(.eu_payload).?.data;
40404427 const result_ty = operand_ty.errorUnionPayload();
......@@ -4052,7 +4439,7 @@ fn zirErrUnionPayload(
40524439/// Pointer in, pointer out.
40534440fn zirErrUnionPayloadPtr(
40544441 sema: *Sema,
4055 block: *Scope.Block,
4442 block: *Block,
40564443 inst: Zir.Inst.Index,
40574444 safety_check: bool,
40584445) CompileError!Air.Inst.Ref {
......@@ -4066,7 +4453,7 @@ fn zirErrUnionPayloadPtr(
40664453 assert(operand_ty.zigTypeTag() == .Pointer);
40674454
40684455 if (operand_ty.elemType().zigTypeTag() != .ErrorUnion)
4069 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand_ty.elemType()});
4456 return sema.fail(block, src, "expected error union type, found {}", .{operand_ty.elemType()});
40704457
40714458 const payload_ty = operand_ty.elemType().errorUnionPayload();
40724459 const operand_pointer_ty = try Type.ptr(sema.arena, .{
......@@ -4078,7 +4465,7 @@ fn zirErrUnionPayloadPtr(
40784465 if (try sema.resolveDefinedValue(block, src, operand)) |pointer_val| {
40794466 if (try pointer_val.pointerDeref(sema.arena)) |val| {
40804467 if (val.getError()) |name| {
4081 return sema.mod.fail(&block.base, src, "caught unexpected error '{s}'", .{name});
4468 return sema.fail(block, src, "caught unexpected error '{s}'", .{name});
40824469 }
40834470 return sema.addConstant(
40844471 operand_pointer_ty,
......@@ -4096,7 +4483,7 @@ fn zirErrUnionPayloadPtr(
40964483}
40974484
40984485/// Value in, value out
4099fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4486fn zirErrUnionCode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
41004487 const tracy = trace(@src());
41014488 defer tracy.end();
41024489
......@@ -4105,7 +4492,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
41054492 const operand = sema.resolveInst(inst_data.operand);
41064493 const operand_ty = sema.typeOf(operand);
41074494 if (operand_ty.zigTypeTag() != .ErrorUnion)
4108 return sema.mod.fail(&block.base, src, "expected error union type, found '{}'", .{operand_ty});
4495 return sema.fail(block, src, "expected error union type, found '{}'", .{operand_ty});
41094496
41104497 const result_ty = operand_ty.errorUnionSet();
41114498
......@@ -4119,7 +4506,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
41194506}
41204507
41214508/// Pointer in, value out
4122fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4509fn zirErrUnionCodePtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
41234510 const tracy = trace(@src());
41244511 defer tracy.end();
41254512
......@@ -4130,7 +4517,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
41304517 assert(operand_ty.zigTypeTag() == .Pointer);
41314518
41324519 if (operand_ty.elemType().zigTypeTag() != .ErrorUnion)
4133 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand_ty.elemType()});
4520 return sema.fail(block, src, "expected error union type, found {}", .{operand_ty.elemType()});
41344521
41354522 const result_ty = operand_ty.elemType().errorUnionSet();
41364523
......@@ -4145,7 +4532,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
41454532 return block.addTyOp(.unwrap_errunion_err_ptr, result_ty, operand);
41464533}
41474534
4148fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
4535fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
41494536 const tracy = trace(@src());
41504537 defer tracy.end();
41514538
......@@ -4154,15 +4541,15 @@ fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
41544541 const operand = sema.resolveInst(inst_data.operand);
41554542 const operand_ty = sema.typeOf(operand);
41564543 if (operand_ty.zigTypeTag() != .ErrorUnion)
4157 return sema.mod.fail(&block.base, src, "expected error union type, found '{}'", .{operand_ty});
4544 return sema.fail(block, src, "expected error union type, found '{}'", .{operand_ty});
41584545 if (operand_ty.errorUnionPayload().zigTypeTag() != .Void) {
4159 return sema.mod.fail(&block.base, src, "expression value is ignored", .{});
4546 return sema.fail(block, src, "expression value is ignored", .{});
41604547 }
41614548}
41624549
41634550fn zirFunc(
41644551 sema: *Sema,
4165 block: *Scope.Block,
4552 block: *Block,
41664553 inst: Zir.Inst.Index,
41674554 inferred_error_set: bool,
41684555) CompileError!Air.Inst.Ref {
......@@ -4205,7 +4592,7 @@ fn zirFunc(
42054592
42064593fn funcCommon(
42074594 sema: *Sema,
4208 block: *Scope.Block,
4595 block: *Block,
42094596 src_node_offset: i32,
42104597 body_inst: Zir.Inst.Index,
42114598 ret_ty_body: []const Zir.Inst.Index,
......@@ -4297,7 +4684,7 @@ fn funcCommon(
42974684 }
42984685
42994686 if (align_val.tag() != .null_value) {
4300 return mod.fail(&block.base, src, "TODO implement support for function prototypes to have alignment specified", .{});
4687 return sema.fail(block, src, "TODO implement support for function prototypes to have alignment specified", .{});
43014688 }
43024689
43034690 is_generic = is_generic or bare_return_type.requiresComptime();
......@@ -4329,15 +4716,15 @@ fn funcCommon(
43294716 const lib_name_src: LazySrcLoc = .{ .node_offset_lib_name = src_node_offset };
43304717 log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});
43314718 mod.comp.stage1AddLinkLib(lib_name) catch |err| {
4332 return mod.fail(&block.base, lib_name_src, "unable to add link lib '{s}': {s}", .{
4719 return sema.fail(block, lib_name_src, "unable to add link lib '{s}': {s}", .{
43334720 lib_name, @errorName(err),
43344721 });
43354722 };
43364723 const target = mod.getTarget();
43374724 if (target_util.is_libc_lib_name(target, lib_name)) {
43384725 if (!mod.comp.bin_file.options.link_libc) {
4339 return mod.fail(
4340 &block.base,
4726 return sema.fail(
4727 block,
43414728 lib_name_src,
43424729 "dependency on libc must be explicitly specified in the build command",
43434730 .{},
......@@ -4347,8 +4734,8 @@ fn funcCommon(
43474734 }
43484735 if (target_util.is_libcpp_lib_name(target, lib_name)) {
43494736 if (!mod.comp.bin_file.options.link_libcpp) {
4350 return mod.fail(
4351 &block.base,
4737 return sema.fail(
4738 block,
43524739 lib_name_src,
43534740 "dependency on libc++ must be explicitly specified in the build command",
43544741 .{},
......@@ -4357,8 +4744,8 @@ fn funcCommon(
43574744 break :blk;
43584745 }
43594746 if (!target.isWasm() and !mod.comp.bin_file.options.pic) {
4360 return mod.fail(
4361 &block.base,
4747 return sema.fail(
4748 block,
43624749 lib_name_src,
43634750 "dependency on dynamic library '{s}' requires enabling Position Independent Code. Fixed by `-l{s}` or `-fPIC`.",
43644751 .{ lib_name, lib_name },
......@@ -4404,7 +4791,7 @@ fn funcCommon(
44044791
44054792fn zirParam(
44064793 sema: *Sema,
4407 block: *Scope.Block,
4794 block: *Block,
44084795 inst: Zir.Inst.Index,
44094796 is_comptime: bool,
44104797) CompileError!void {
......@@ -4474,7 +4861,7 @@ fn zirParam(
44744861
44754862fn zirParamAnytype(
44764863 sema: *Sema,
4477 block: *Scope.Block,
4864 block: *Block,
44784865 inst: Zir.Inst.Index,
44794866 is_comptime: bool,
44804867) CompileError!void {
......@@ -4509,7 +4896,7 @@ fn zirParamAnytype(
45094896 try sema.inst_map.put(sema.gpa, inst, .generic_poison);
45104897}
45114898
4512fn zirAs(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4899fn zirAs(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
45134900 const tracy = trace(@src());
45144901 defer tracy.end();
45154902
......@@ -4517,7 +4904,7 @@ fn zirAs(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Ai
45174904 return sema.analyzeAs(block, .unneeded, bin_inst.lhs, bin_inst.rhs);
45184905}
45194906
4520fn zirAsNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4907fn zirAsNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
45214908 const tracy = trace(@src());
45224909 defer tracy.end();
45234910
......@@ -4529,7 +4916,7 @@ fn zirAsNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
45294916
45304917fn analyzeAs(
45314918 sema: *Sema,
4532 block: *Scope.Block,
4919 block: *Block,
45334920 src: LazySrcLoc,
45344921 zir_dest_type: Zir.Inst.Ref,
45354922 zir_operand: Zir.Inst.Ref,
......@@ -4539,7 +4926,7 @@ fn analyzeAs(
45394926 return sema.coerce(block, dest_type, operand, src);
45404927}
45414928
4542fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4929fn zirPtrToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
45434930 const tracy = trace(@src());
45444931 defer tracy.end();
45454932
......@@ -4548,7 +4935,7 @@ fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
45484935 const ptr_ty = sema.typeOf(ptr);
45494936 if (ptr_ty.zigTypeTag() != .Pointer) {
45504937 const ptr_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
4551 return sema.mod.fail(&block.base, ptr_src, "expected pointer, found '{}'", .{ptr_ty});
4938 return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty});
45524939 }
45534940 // TODO handle known-pointer-address
45544941 const src = inst_data.src();
......@@ -4556,7 +4943,7 @@ fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
45564943 return block.addUnOp(.ptrtoint, ptr);
45574944}
45584945
4559fn zirFieldVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4946fn zirFieldVal(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
45604947 const tracy = trace(@src());
45614948 defer tracy.end();
45624949
......@@ -4575,7 +4962,7 @@ fn zirFieldVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
45754962 }
45764963}
45774964
4578fn zirFieldPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4965fn zirFieldPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
45794966 const tracy = trace(@src());
45804967 defer tracy.end();
45814968
......@@ -4588,7 +4975,7 @@ fn zirFieldPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
45884975 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src);
45894976}
45904977
4591fn zirFieldCallBind(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4978fn zirFieldCallBind(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
45924979 const tracy = trace(@src());
45934980 defer tracy.end();
45944981
......@@ -4601,7 +4988,7 @@ fn zirFieldCallBind(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
46014988 return sema.fieldCallBind(block, src, object_ptr, field_name, field_name_src);
46024989}
46034990
4604fn zirFieldValNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
4991fn zirFieldValNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
46054992 const tracy = trace(@src());
46064993 defer tracy.end();
46074994
......@@ -4614,7 +5001,7 @@ fn zirFieldValNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
46145001 return sema.fieldVal(block, src, object, field_name, field_name_src);
46155002}
46165003
4617fn zirFieldPtrNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5004fn zirFieldPtrNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
46185005 const tracy = trace(@src());
46195006 defer tracy.end();
46205007
......@@ -4627,7 +5014,7 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
46275014 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src);
46285015}
46295016
4630fn zirFieldCallBindNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5017fn zirFieldCallBindNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
46315018 const tracy = trace(@src());
46325019 defer tracy.end();
46335020
......@@ -4640,7 +5027,7 @@ fn zirFieldCallBindNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
46405027 return sema.fieldCallBind(block, src, object_ptr, field_name, field_name_src);
46415028}
46425029
4643fn zirIntCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5030fn zirIntCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
46445031 const tracy = trace(@src());
46455032 defer tracy.end();
46465033
......@@ -4659,14 +5046,14 @@ fn zirIntCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
46595046 if (try sema.isComptimeKnown(block, operand_src, operand)) {
46605047 return sema.coerce(block, dest_type, operand, operand_src);
46615048 } else if (dest_is_comptime_int) {
4662 return sema.mod.fail(&block.base, src, "unable to cast runtime value to 'comptime_int'", .{});
5049 return sema.fail(block, src, "unable to cast runtime value to 'comptime_int'", .{});
46635050 }
46645051
46655052 try sema.requireRuntimeBlock(block, operand_src);
46665053 return block.addTyOp(.intcast, dest_type, operand);
46675054}
46685055
4669fn zirBitcast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5056fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
46705057 const tracy = trace(@src());
46715058 defer tracy.end();
46725059
......@@ -4680,7 +5067,7 @@ fn zirBitcast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
46805067 return sema.bitcast(block, dest_type, operand, operand_src);
46815068}
46825069
4683fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5070fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
46845071 const tracy = trace(@src());
46855072 defer tracy.end();
46865073
......@@ -4696,8 +5083,8 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
46965083 const dest_is_comptime_float = switch (dest_type.zigTypeTag()) {
46975084 .ComptimeFloat => true,
46985085 .Float => false,
4699 else => return sema.mod.fail(
4700 &block.base,
5086 else => return sema.fail(
5087 block,
47015088 dest_ty_src,
47025089 "expected float type, found '{}'",
47035090 .{dest_type},
......@@ -4707,8 +5094,8 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
47075094 const operand_ty = sema.typeOf(operand);
47085095 switch (operand_ty.zigTypeTag()) {
47095096 .ComptimeFloat, .Float, .ComptimeInt => {},
4710 else => return sema.mod.fail(
4711 &block.base,
5097 else => return sema.fail(
5098 block,
47125099 operand_src,
47135100 "expected float type, found '{}'",
47145101 .{operand_ty},
......@@ -4719,7 +5106,7 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
47195106 return sema.coerce(block, dest_type, operand, operand_src);
47205107 }
47215108 if (dest_is_comptime_float) {
4722 return sema.mod.fail(&block.base, src, "unable to cast runtime value to 'comptime_float'", .{});
5109 return sema.fail(block, src, "unable to cast runtime value to 'comptime_float'", .{});
47235110 }
47245111 const target = sema.mod.getTarget();
47255112 const src_bits = operand_ty.floatBits(target);
......@@ -4731,7 +5118,7 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
47315118 return block.addTyOp(.fptrunc, dest_type, operand);
47325119}
47335120
4734fn zirElemVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5121fn zirElemVal(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
47355122 const tracy = trace(@src());
47365123 defer tracy.end();
47375124
......@@ -4741,7 +5128,7 @@ fn zirElemVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
47415128 return sema.elemVal(block, sema.src, array, elem_index, sema.src);
47425129}
47435130
4744fn zirElemValNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5131fn zirElemValNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
47455132 const tracy = trace(@src());
47465133 defer tracy.end();
47475134
......@@ -4754,7 +5141,7 @@ fn zirElemValNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
47545141 return sema.elemVal(block, src, array, elem_index, elem_index_src);
47555142}
47565143
4757fn zirElemPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5144fn zirElemPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
47585145 const tracy = trace(@src());
47595146 defer tracy.end();
47605147
......@@ -4764,7 +5151,7 @@ fn zirElemPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
47645151 return sema.elemPtr(block, sema.src, array_ptr, elem_index, sema.src);
47655152}
47665153
4767fn zirElemPtrNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5154fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
47685155 const tracy = trace(@src());
47695156 defer tracy.end();
47705157
......@@ -4777,7 +5164,7 @@ fn zirElemPtrNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
47775164 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src);
47785165}
47795166
4780fn zirSliceStart(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5167fn zirSliceStart(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
47815168 const tracy = trace(@src());
47825169 defer tracy.end();
47835170
......@@ -4790,7 +5177,7 @@ fn zirSliceStart(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
47905177 return sema.analyzeSlice(block, src, array_ptr, start, .none, .none, .unneeded);
47915178}
47925179
4793fn zirSliceEnd(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5180fn zirSliceEnd(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
47945181 const tracy = trace(@src());
47955182 defer tracy.end();
47965183
......@@ -4804,7 +5191,7 @@ fn zirSliceEnd(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
48045191 return sema.analyzeSlice(block, src, array_ptr, start, end, .none, .unneeded);
48055192}
48065193
4807fn zirSliceSentinel(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
5194fn zirSliceSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
48085195 const tracy = trace(@src());
48095196 defer tracy.end();
48105197
......@@ -4822,7 +5209,7 @@ fn zirSliceSentinel(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
48225209
48235210fn zirSwitchCapture(
48245211 sema: *Sema,
4825 block: *Scope.Block,
5212 block: *Block,
48265213 inst: Zir.Inst.Index,
48275214 is_multi: bool,
48285215 is_ref: bool,
......@@ -4837,12 +5224,12 @@ fn zirSwitchCapture(
48375224
48385225 _ = is_ref;
48395226 _ = is_multi;
4840 return sema.mod.fail(&block.base, src, "TODO implement Sema for zirSwitchCapture", .{});
5227 return sema.fail(block, src, "TODO implement Sema for zirSwitchCapture", .{});
48415228}
48425229
48435230fn zirSwitchCaptureElse(
48445231 sema: *Sema,
4845 block: *Scope.Block,
5232 block: *Block,
48465233 inst: Zir.Inst.Index,
48475234 is_ref: bool,
48485235) CompileError!Air.Inst.Ref {
......@@ -4855,12 +5242,12 @@ fn zirSwitchCaptureElse(
48555242 const src = switch_info.src();
48565243
48575244 _ = is_ref;
4858 return sema.mod.fail(&block.base, src, "TODO implement Sema for zirSwitchCaptureElse", .{});
5245 return sema.fail(block, src, "TODO implement Sema for zirSwitchCaptureElse", .{});
48595246}
48605247
48615248fn zirSwitchBlock(
48625249 sema: *Sema,
4863 block: *Scope.Block,
5250 block: *Block,
48645251 inst: Zir.Inst.Index,
48655252 is_ref: bool,
48665253 special_prong: Zir.SpecialProng,
......@@ -4893,7 +5280,7 @@ fn zirSwitchBlock(
48935280
48945281fn zirSwitchBlockMulti(
48955282 sema: *Sema,
4896 block: *Scope.Block,
5283 block: *Block,
48975284 inst: Zir.Inst.Index,
48985285 is_ref: bool,
48995286 special_prong: Zir.SpecialProng,
......@@ -4926,7 +5313,7 @@ fn zirSwitchBlockMulti(
49265313
49275314fn analyzeSwitch(
49285315 sema: *Sema,
4929 block: *Scope.Block,
5316 block: *Block,
49305317 operand: Air.Inst.Ref,
49315318 extra_end: usize,
49325319 special_prong: Zir.SpecialProng,
......@@ -4936,7 +5323,6 @@ fn analyzeSwitch(
49365323 src_node_offset: i32,
49375324) CompileError!Air.Inst.Ref {
49385325 const gpa = sema.gpa;
4939 const mod = sema.mod;
49405326
49415327 const special: struct { body: []const Zir.Inst.Index, end: usize } = switch (special_prong) {
49425328 .none => .{ .body = &.{}, .end = extra_end },
......@@ -4958,15 +5344,15 @@ fn analyzeSwitch(
49585344 // Validate usage of '_' prongs.
49595345 if (special_prong == .under and !operand_ty.isNonexhaustiveEnum()) {
49605346 const msg = msg: {
4961 const msg = try mod.errMsg(
4962 &block.base,
5347 const msg = try sema.errMsg(
5348 block,
49635349 src,
49645350 "'_' prong only allowed when switching on non-exhaustive enums",
49655351 .{},
49665352 );
49675353 errdefer msg.destroy(gpa);
4968 try mod.errNote(
4969 &block.base,
5354 try sema.errNote(
5355 block,
49705356 special_prong_src,
49715357 msg,
49725358 "'_' prong here",
......@@ -4974,7 +5360,7 @@ fn analyzeSwitch(
49745360 );
49755361 break :msg msg;
49765362 };
4977 return mod.failWithOwnedErrorMsg(&block.base, msg);
5363 return sema.failWithOwnedErrorMsg(msg);
49785364 }
49795365
49805366 // Validate for duplicate items, missing else prong, and invalid range.
......@@ -5037,8 +5423,8 @@ fn analyzeSwitch(
50375423 .none => {
50385424 if (!all_tags_handled) {
50395425 const msg = msg: {
5040 const msg = try mod.errMsg(
5041 &block.base,
5426 const msg = try sema.errMsg(
5427 block,
50425428 src,
50435429 "switch must handle all possibilities",
50445430 .{},
......@@ -5050,15 +5436,15 @@ fn analyzeSwitch(
50505436 const field_name = operand_ty.enumFieldName(i);
50515437
50525438 // TODO have this point to the tag decl instead of here
5053 try mod.errNote(
5054 &block.base,
5439 try sema.errNote(
5440 block,
50555441 src,
50565442 msg,
50575443 "unhandled enumeration value: '{s}'",
50585444 .{field_name},
50595445 );
50605446 }
5061 try mod.errNoteNonLazy(
5447 try sema.mod.errNoteNonLazy(
50625448 operand_ty.declSrcLoc(),
50635449 msg,
50645450 "enum '{}' declared here",
......@@ -5066,20 +5452,20 @@ fn analyzeSwitch(
50665452 );
50675453 break :msg msg;
50685454 };
5069 return mod.failWithOwnedErrorMsg(&block.base, msg);
5455 return sema.failWithOwnedErrorMsg(msg);
50705456 }
50715457 },
50725458 .under => {
5073 if (all_tags_handled) return mod.fail(
5074 &block.base,
5459 if (all_tags_handled) return sema.fail(
5460 block,
50755461 special_prong_src,
50765462 "unreachable '_' prong; all cases already handled",
50775463 .{},
50785464 );
50795465 },
50805466 .@"else" => {
5081 if (all_tags_handled) return mod.fail(
5082 &block.base,
5467 if (all_tags_handled) return sema.fail(
5468 block,
50835469 special_prong_src,
50845470 "unreachable else prong; all cases already handled",
50855471 .{},
......@@ -5088,8 +5474,8 @@ fn analyzeSwitch(
50885474 }
50895475 },
50905476
5091 .ErrorSet => return mod.fail(&block.base, src, "TODO validate switch .ErrorSet", .{}),
5092 .Union => return mod.fail(&block.base, src, "TODO validate switch .Union", .{}),
5477 .ErrorSet => return sema.fail(block, src, "TODO validate switch .ErrorSet", .{}),
5478 .Union => return sema.fail(block, src, "TODO validate switch .Union", .{}),
50935479 .Int, .ComptimeInt => {
50945480 var range_set = RangeSet.init(gpa);
50955481 defer range_set.deinit();
......@@ -5164,12 +5550,13 @@ fn analyzeSwitch(
51645550 var arena = std.heap.ArenaAllocator.init(gpa);
51655551 defer arena.deinit();
51665552
5167 const min_int = try operand_ty.minInt(&arena.allocator, mod.getTarget());
5168 const max_int = try operand_ty.maxInt(&arena.allocator, mod.getTarget());
5553 const target = sema.mod.getTarget();
5554 const min_int = try operand_ty.minInt(&arena.allocator, target);
5555 const max_int = try operand_ty.maxInt(&arena.allocator, target);
51695556 if (try range_set.spans(min_int, max_int, operand_ty)) {
51705557 if (special_prong == .@"else") {
5171 return mod.fail(
5172 &block.base,
5558 return sema.fail(
5559 block,
51735560 special_prong_src,
51745561 "unreachable else prong; all cases already handled",
51755562 .{},
......@@ -5179,8 +5566,8 @@ fn analyzeSwitch(
51795566 }
51805567 }
51815568 if (special_prong != .@"else") {
5182 return mod.fail(
5183 &block.base,
5569 return sema.fail(
5570 block,
51845571 src,
51855572 "switch must handle all possibilities",
51865573 .{},
......@@ -5241,8 +5628,8 @@ fn analyzeSwitch(
52415628 switch (special_prong) {
52425629 .@"else" => {
52435630 if (true_count + false_count == 2) {
5244 return mod.fail(
5245 &block.base,
5631 return sema.fail(
5632 block,
52465633 src,
52475634 "unreachable else prong; all cases already handled",
52485635 .{},
......@@ -5251,8 +5638,8 @@ fn analyzeSwitch(
52515638 },
52525639 .under, .none => {
52535640 if (true_count + false_count < 2) {
5254 return mod.fail(
5255 &block.base,
5641 return sema.fail(
5642 block,
52565643 src,
52575644 "switch must handle all possibilities",
52585645 .{},
......@@ -5263,8 +5650,8 @@ fn analyzeSwitch(
52635650 },
52645651 .EnumLiteral, .Void, .Fn, .Pointer, .Type => {
52655652 if (special_prong != .@"else") {
5266 return mod.fail(
5267 &block.base,
5653 return sema.fail(
5654 block,
52685655 src,
52695656 "else prong required when switching on type '{}'",
52705657 .{operand_ty},
......@@ -5334,7 +5721,7 @@ fn analyzeSwitch(
53345721 .AnyFrame,
53355722 .ComptimeFloat,
53365723 .Float,
5337 => return mod.fail(&block.base, operand_src, "invalid switch operand type '{}'", .{
5724 => return sema.fail(block, operand_src, "invalid switch operand type '{}'", .{
53385725 operand_ty,
53395726 }),
53405727 }
......@@ -5344,7 +5731,7 @@ fn analyzeSwitch(
53445731 .tag = .block,
53455732 .data = undefined,
53465733 });
5347 var label: Scope.Block.Label = .{
5734 var label: Block.Label = .{
53485735 .zir_block = switch_inst,
53495736 .merges = .{
53505737 .results = .{},
......@@ -5353,10 +5740,11 @@ fn analyzeSwitch(
53535740 },
53545741 };
53555742
5356 var child_block: Scope.Block = .{
5743 var child_block: Block = .{
53575744 .parent = block,
53585745 .sema = sema,
53595746 .src_decl = block.src_decl,
5747 .namespace = block.namespace,
53605748 .wip_capture_scope = block.wip_capture_scope,
53615749 .instructions = .{},
53625750 .label = &label,
......@@ -5663,7 +6051,7 @@ fn analyzeSwitch(
56636051
56646052fn resolveSwitchItemVal(
56656053 sema: *Sema,
5666 block: *Scope.Block,
6054 block: *Block,
56676055 item_ref: Zir.Inst.Ref,
56686056 switch_node_offset: i32,
56696057 switch_prong_src: Module.SwitchProngSrc,
......@@ -5690,7 +6078,7 @@ fn resolveSwitchItemVal(
56906078
56916079fn validateSwitchRange(
56926080 sema: *Sema,
5693 block: *Scope.Block,
6081 block: *Block,
56946082 range_set: *RangeSet,
56956083 first_ref: Zir.Inst.Ref,
56966084 last_ref: Zir.Inst.Ref,
......@@ -5706,7 +6094,7 @@ fn validateSwitchRange(
57066094
57076095fn validateSwitchItem(
57086096 sema: *Sema,
5709 block: *Scope.Block,
6097 block: *Block,
57106098 range_set: *RangeSet,
57116099 item_ref: Zir.Inst.Ref,
57126100 operand_ty: Type,
......@@ -5720,25 +6108,24 @@ fn validateSwitchItem(
57206108
57216109fn validateSwitchItemEnum(
57226110 sema: *Sema,
5723 block: *Scope.Block,
6111 block: *Block,
57246112 seen_fields: []?Module.SwitchProngSrc,
57256113 item_ref: Zir.Inst.Ref,
57266114 src_node_offset: i32,
57276115 switch_prong_src: Module.SwitchProngSrc,
57286116) CompileError!void {
5729 const mod = sema.mod;
57306117 const item_tv = try sema.resolveSwitchItemVal(block, item_ref, src_node_offset, switch_prong_src, .none);
57316118 const field_index = item_tv.ty.enumTagFieldIndex(item_tv.val) orelse {
57326119 const msg = msg: {
57336120 const src = switch_prong_src.resolve(sema.gpa, block.src_decl, src_node_offset, .none);
5734 const msg = try mod.errMsg(
5735 &block.base,
6121 const msg = try sema.errMsg(
6122 block,
57366123 src,
57376124 "enum '{}' has no tag with value '{}'",
57386125 .{ item_tv.ty, item_tv.val },
57396126 );
57406127 errdefer msg.destroy(sema.gpa);
5741 try mod.errNoteNonLazy(
6128 try sema.mod.errNoteNonLazy(
57426129 item_tv.ty.declSrcLoc(),
57436130 msg,
57446131 "enum declared here",
......@@ -5746,7 +6133,7 @@ fn validateSwitchItemEnum(
57466133 );
57476134 break :msg msg;
57486135 };
5749 return mod.failWithOwnedErrorMsg(&block.base, msg);
6136 return sema.failWithOwnedErrorMsg(msg);
57506137 };
57516138 const maybe_prev_src = seen_fields[field_index];
57526139 seen_fields[field_index] = switch_prong_src;
......@@ -5755,26 +6142,25 @@ fn validateSwitchItemEnum(
57556142
57566143fn validateSwitchDupe(
57576144 sema: *Sema,
5758 block: *Scope.Block,
6145 block: *Block,
57596146 maybe_prev_src: ?Module.SwitchProngSrc,
57606147 switch_prong_src: Module.SwitchProngSrc,
57616148 src_node_offset: i32,
57626149) CompileError!void {
57636150 const prev_prong_src = maybe_prev_src orelse return;
5764 const mod = sema.mod;
57656151 const gpa = sema.gpa;
57666152 const src = switch_prong_src.resolve(gpa, block.src_decl, src_node_offset, .none);
57676153 const prev_src = prev_prong_src.resolve(gpa, block.src_decl, src_node_offset, .none);
57686154 const msg = msg: {
5769 const msg = try mod.errMsg(
5770 &block.base,
6155 const msg = try sema.errMsg(
6156 block,
57716157 src,
57726158 "duplicate switch value",
57736159 .{},
57746160 );
57756161 errdefer msg.destroy(sema.gpa);
5776 try mod.errNote(
5777 &block.base,
6162 try sema.errNote(
6163 block,
57786164 prev_src,
57796165 msg,
57806166 "previous value here",
......@@ -5782,12 +6168,12 @@ fn validateSwitchDupe(
57826168 );
57836169 break :msg msg;
57846170 };
5785 return mod.failWithOwnedErrorMsg(&block.base, msg);
6171 return sema.failWithOwnedErrorMsg(msg);
57866172}
57876173
57886174fn validateSwitchItemBool(
57896175 sema: *Sema,
5790 block: *Scope.Block,
6176 block: *Block,
57916177 true_count: *u8,
57926178 false_count: *u8,
57936179 item_ref: Zir.Inst.Ref,
......@@ -5802,7 +6188,7 @@ fn validateSwitchItemBool(
58026188 }
58036189 if (true_count.* + false_count.* > 2) {
58046190 const src = switch_prong_src.resolve(sema.gpa, block.src_decl, src_node_offset, .none);
5805 return sema.mod.fail(&block.base, src, "duplicate switch value", .{});
6191 return sema.fail(block, src, "duplicate switch value", .{});
58066192 }
58076193}
58086194
......@@ -5810,7 +6196,7 @@ const ValueSrcMap = std.HashMap(Value, Module.SwitchProngSrc, Value.HashContext,
58106196
58116197fn validateSwitchItemSparse(
58126198 sema: *Sema,
5813 block: *Scope.Block,
6199 block: *Block,
58146200 seen_values: *ValueSrcMap,
58156201 item_ref: Zir.Inst.Ref,
58166202 src_node_offset: i32,
......@@ -5823,7 +6209,7 @@ fn validateSwitchItemSparse(
58236209
58246210fn validateSwitchNoRange(
58256211 sema: *Sema,
5826 block: *Scope.Block,
6212 block: *Block,
58276213 ranges_len: u32,
58286214 operand_ty: Type,
58296215 src_node_offset: i32,
......@@ -5835,15 +6221,15 @@ fn validateSwitchNoRange(
58356221 const range_src: LazySrcLoc = .{ .node_offset_switch_range = src_node_offset };
58366222
58376223 const msg = msg: {
5838 const msg = try sema.mod.errMsg(
5839 &block.base,
6224 const msg = try sema.errMsg(
6225 block,
58406226 operand_src,
58416227 "ranges not allowed when switching on type '{}'",
58426228 .{operand_ty},
58436229 );
58446230 errdefer msg.destroy(sema.gpa);
5845 try sema.mod.errNote(
5846 &block.base,
6231 try sema.errNote(
6232 block,
58476233 range_src,
58486234 msg,
58496235 "range here",
......@@ -5851,19 +6237,19 @@ fn validateSwitchNoRange(
58516237 );
58526238 break :msg msg;
58536239 };
5854 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);
6240 return sema.failWithOwnedErrorMsg(msg);
58556241}
58566242
5857fn zirHasField(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6243fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
58586244 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
58596245 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
58606246 _ = extra;
58616247 const src = inst_data.src();
58626248
5863 return sema.mod.fail(&block.base, src, "TODO implement zirHasField", .{});
6249 return sema.fail(block, src, "TODO implement zirHasField", .{});
58646250}
58656251
5866fn zirHasDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6252fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
58676253 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
58686254 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
58696255 const src = inst_data.src();
......@@ -5871,23 +6257,22 @@ fn zirHasDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
58716257 const rhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
58726258 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);
58736259 const decl_name = try sema.resolveConstString(block, rhs_src, extra.rhs);
5874 const mod = sema.mod;
58756260
5876 const namespace = container_type.getNamespace() orelse return mod.fail(
5877 &block.base,
6261 const namespace = container_type.getNamespace() orelse return sema.fail(
6262 block,
58786263 lhs_src,
58796264 "expected struct, enum, union, or opaque, found '{}'",
58806265 .{container_type},
58816266 );
58826267 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl| {
5883 if (decl.is_pub or decl.namespace.file_scope == block.base.namespace().file_scope) {
6268 if (decl.is_pub or decl.getFileScope() == block.getFileScope()) {
58846269 return Air.Inst.Ref.bool_true;
58856270 }
58866271 }
58876272 return Air.Inst.Ref.bool_false;
58886273}
58896274
5890fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6275fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
58916276 const tracy = trace(@src());
58926277 defer tracy.end();
58936278
......@@ -5898,29 +6283,29 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
58986283
58996284 const result = mod.importFile(block.getFileScope(), operand) catch |err| switch (err) {
59006285 error.ImportOutsidePkgPath => {
5901 return mod.fail(&block.base, src, "import of file outside package path: '{s}'", .{operand});
6286 return sema.fail(block, src, "import of file outside package path: '{s}'", .{operand});
59026287 },
59036288 else => {
59046289 // TODO: these errors are file system errors; make sure an update() will
59056290 // retry this and not cache the file system error, which may be transient.
5906 return mod.fail(&block.base, src, "unable to open '{s}': {s}", .{ operand, @errorName(err) });
6291 return sema.fail(block, src, "unable to open '{s}': {s}", .{ operand, @errorName(err) });
59076292 },
59086293 };
59096294 try mod.semaFile(result.file);
59106295 const file_root_decl = result.file.root_decl.?;
5911 try sema.mod.declareDeclDependency(sema.owner_decl, file_root_decl);
6296 try mod.declareDeclDependency(sema.owner_decl, file_root_decl);
59126297 return sema.addConstant(file_root_decl.ty, file_root_decl.val);
59136298}
59146299
5915fn zirRetErrValueCode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6300fn zirRetErrValueCode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
59166301 _ = block;
59176302 _ = inst;
5918 return sema.mod.fail(&block.base, sema.src, "TODO implement zirRetErrValueCode", .{});
6303 return sema.fail(block, sema.src, "TODO implement zirRetErrValueCode", .{});
59196304}
59206305
59216306fn zirShl(
59226307 sema: *Sema,
5923 block: *Scope.Block,
6308 block: *Block,
59246309 inst: Zir.Inst.Index,
59256310 air_tag: Air.Inst.Tag,
59266311) CompileError!Air.Inst.Ref {
......@@ -5952,8 +6337,8 @@ fn zirShl(
59526337 }
59536338 const val = try lhs_val.shl(rhs_val, sema.arena);
59546339 switch (air_tag) {
5955 .shl_exact => return sema.mod.fail(&block.base, lhs_src, "TODO implement Sema for comptime shl_exact", .{}),
5956 .shl_sat => return sema.mod.fail(&block.base, lhs_src, "TODO implement Sema for comptime shl_sat", .{}),
6340 .shl_exact => return sema.fail(block, lhs_src, "TODO implement Sema for comptime shl_exact", .{}),
6341 .shl_sat => return sema.fail(block, lhs_src, "TODO implement Sema for comptime shl_sat", .{}),
59576342 .shl => {},
59586343 else => unreachable,
59596344 }
......@@ -5971,7 +6356,7 @@ fn zirShl(
59716356 return block.addBinOp(air_tag, lhs, rhs);
59726357}
59736358
5974fn zirShr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6359fn zirShr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
59756360 const tracy = trace(@src());
59766361 defer tracy.end();
59776362
......@@ -6004,7 +6389,7 @@ fn zirShr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
60046389
60056390fn zirBitwise(
60066391 sema: *Sema,
6007 block: *Scope.Block,
6392 block: *Block,
60086393 inst: Zir.Inst.Index,
60096394 air_tag: Air.Inst.Tag,
60106395) CompileError!Air.Inst.Ref {
......@@ -6035,14 +6420,14 @@ fn zirBitwise(
60356420
60366421 if (lhs_ty.zigTypeTag() == .Vector and rhs_ty.zigTypeTag() == .Vector) {
60376422 if (lhs_ty.arrayLen() != rhs_ty.arrayLen()) {
6038 return sema.mod.fail(&block.base, src, "vector length mismatch: {d} and {d}", .{
6423 return sema.fail(block, src, "vector length mismatch: {d} and {d}", .{
60396424 lhs_ty.arrayLen(),
60406425 rhs_ty.arrayLen(),
60416426 });
60426427 }
6043 return sema.mod.fail(&block.base, src, "TODO implement support for vectors in zirBitwise", .{});
6428 return sema.fail(block, src, "TODO implement support for vectors in zirBitwise", .{});
60446429 } else if (lhs_ty.zigTypeTag() == .Vector or rhs_ty.zigTypeTag() == .Vector) {
6045 return sema.mod.fail(&block.base, src, "mixed scalar and vector operands to binary expression: '{}' and '{}'", .{
6430 return sema.fail(block, src, "mixed scalar and vector operands to binary expression: '{}' and '{}'", .{
60466431 lhs_ty,
60476432 rhs_ty,
60486433 });
......@@ -6051,7 +6436,7 @@ fn zirBitwise(
60516436 const is_int = scalar_tag == .Int or scalar_tag == .ComptimeInt;
60526437
60536438 if (!is_int) {
6054 return sema.mod.fail(&block.base, src, "invalid operands to binary bitwise expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) });
6439 return sema.fail(block, src, "invalid operands to binary bitwise expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) });
60556440 }
60566441
60576442 if (try sema.resolveMaybeUndefVal(block, lhs_src, casted_lhs)) |lhs_val| {
......@@ -6070,15 +6455,15 @@ fn zirBitwise(
60706455 return block.addBinOp(air_tag, casted_lhs, casted_rhs);
60716456}
60726457
6073fn zirBitNot(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6458fn zirBitNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
60746459 const tracy = trace(@src());
60756460 defer tracy.end();
60766461
60776462 _ = inst;
6078 return sema.mod.fail(&block.base, sema.src, "TODO implement zirBitNot", .{});
6463 return sema.fail(block, sema.src, "TODO implement zirBitNot", .{});
60796464}
60806465
6081fn zirArrayCat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6466fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
60826467 const tracy = trace(@src());
60836468 defer tracy.end();
60846469
......@@ -6092,11 +6477,11 @@ fn zirArrayCat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
60926477 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
60936478
60946479 const lhs_info = getArrayCatInfo(lhs_ty) orelse
6095 return sema.mod.fail(&block.base, lhs_src, "expected array, found '{}'", .{lhs_ty});
6480 return sema.fail(block, lhs_src, "expected array, found '{}'", .{lhs_ty});
60966481 const rhs_info = getArrayCatInfo(rhs_ty) orelse
6097 return sema.mod.fail(&block.base, rhs_src, "expected array, found '{}'", .{rhs_ty});
6482 return sema.fail(block, rhs_src, "expected array, found '{}'", .{rhs_ty});
60986483 if (!lhs_info.elem_type.eql(rhs_info.elem_type)) {
6099 return sema.mod.fail(&block.base, rhs_src, "expected array of type '{}', found '{}'", .{ lhs_info.elem_type, rhs_ty });
6484 return sema.fail(block, rhs_src, "expected array of type '{}', found '{}'", .{ lhs_info.elem_type, rhs_ty });
61006485 }
61016486
61026487 // When there is a sentinel mismatch, no sentinel on the result. The type system
......@@ -6142,10 +6527,10 @@ fn zirArrayCat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
61426527 else
61436528 sema.analyzeDeclVal(block, .unneeded, try anon_decl.finish(ty, val));
61446529 } else {
6145 return sema.mod.fail(&block.base, lhs_src, "TODO runtime array_cat", .{});
6530 return sema.fail(block, lhs_src, "TODO runtime array_cat", .{});
61466531 }
61476532 } else {
6148 return sema.mod.fail(&block.base, lhs_src, "TODO runtime array_cat", .{});
6533 return sema.fail(block, lhs_src, "TODO runtime array_cat", .{});
61496534 }
61506535}
61516536
......@@ -6162,7 +6547,7 @@ fn getArrayCatInfo(t: Type) ?Type.ArrayInfo {
61626547 };
61636548}
61646549
6165fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6550fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
61666551 const tracy = trace(@src());
61676552 defer tracy.end();
61686553
......@@ -6176,9 +6561,9 @@ fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
61766561 // In `**` rhs has to be comptime-known, but lhs can be runtime-known
61776562 const tomulby = try sema.resolveInt(block, rhs_src, extra.rhs, Type.initTag(.usize));
61786563 const mulinfo = getArrayCatInfo(lhs_ty) orelse
6179 return sema.mod.fail(&block.base, lhs_src, "expected array, found '{}'", .{lhs_ty});
6564 return sema.fail(block, lhs_src, "expected array, found '{}'", .{lhs_ty});
61806565
6181 const final_len = std.math.mul(u64, mulinfo.len, tomulby) catch return sema.mod.fail(&block.base, rhs_src, "operation results in overflow", .{});
6566 const final_len = std.math.mul(u64, mulinfo.len, tomulby) catch return sema.fail(block, rhs_src, "operation results in overflow", .{});
61826567 if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| {
61836568 var anon_decl = try block.startAnonDecl();
61846569 defer anon_decl.deinit();
......@@ -6211,12 +6596,12 @@ fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
62116596 return sema.analyzeDeclVal(block, .unneeded, try anon_decl.finish(final_ty, val));
62126597 }
62136598 }
6214 return sema.mod.fail(&block.base, lhs_src, "TODO runtime array_mul", .{});
6599 return sema.fail(block, lhs_src, "TODO runtime array_mul", .{});
62156600}
62166601
62176602fn zirNegate(
62186603 sema: *Sema,
6219 block: *Scope.Block,
6604 block: *Block,
62206605 inst: Zir.Inst.Index,
62216606 tag_override: Zir.Inst.Tag,
62226607) CompileError!Air.Inst.Ref {
......@@ -6235,7 +6620,7 @@ fn zirNegate(
62356620
62366621fn zirArithmetic(
62376622 sema: *Sema,
6238 block: *Scope.Block,
6623 block: *Block,
62396624 inst: Zir.Inst.Index,
62406625 zir_tag: Zir.Inst.Tag,
62416626) CompileError!Air.Inst.Ref {
......@@ -6255,7 +6640,7 @@ fn zirArithmetic(
62556640
62566641fn zirOverflowArithmetic(
62576642 sema: *Sema,
6258 block: *Scope.Block,
6643 block: *Block,
62596644 extended: Zir.Inst.Extended.InstData,
62606645) CompileError!Air.Inst.Ref {
62616646 const tracy = trace(@src());
......@@ -6264,12 +6649,12 @@ fn zirOverflowArithmetic(
62646649 const extra = sema.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
62656650 const src: LazySrcLoc = .{ .node_offset = extra.node };
62666651
6267 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirOverflowArithmetic", .{});
6652 return sema.fail(block, src, "TODO implement Sema.zirOverflowArithmetic", .{});
62686653}
62696654
62706655fn analyzeArithmetic(
62716656 sema: *Sema,
6272 block: *Scope.Block,
6657 block: *Block,
62736658 /// TODO performance investigation: make this comptime?
62746659 zir_tag: Zir.Inst.Tag,
62756660 lhs: Air.Inst.Ref,
......@@ -6284,13 +6669,13 @@ fn analyzeArithmetic(
62846669 const rhs_zig_ty_tag = try rhs_ty.zigTypeTagOrPoison();
62856670 if (lhs_zig_ty_tag == .Vector and rhs_zig_ty_tag == .Vector) {
62866671 if (lhs_ty.arrayLen() != rhs_ty.arrayLen()) {
6287 return sema.mod.fail(&block.base, src, "vector length mismatch: {d} and {d}", .{
6672 return sema.fail(block, src, "vector length mismatch: {d} and {d}", .{
62886673 lhs_ty.arrayLen(), rhs_ty.arrayLen(),
62896674 });
62906675 }
6291 return sema.mod.fail(&block.base, src, "TODO implement support for vectors in Sema.analyzeArithmetic", .{});
6676 return sema.fail(block, src, "TODO implement support for vectors in Sema.analyzeArithmetic", .{});
62926677 } else if (lhs_zig_ty_tag == .Vector or rhs_zig_ty_tag == .Vector) {
6293 return sema.mod.fail(&block.base, src, "mixed scalar and vector operands to binary expression: '{}' and '{}'", .{
6678 return sema.fail(block, src, "mixed scalar and vector operands to binary expression: '{}' and '{}'", .{
62946679 lhs_ty, rhs_ty,
62956680 });
62966681 }
......@@ -6302,8 +6687,8 @@ fn analyzeArithmetic(
63026687 const air_tag: Air.Inst.Tag = switch (zir_tag) {
63036688 .add => .ptr_add,
63046689 .sub => .ptr_sub,
6305 else => return sema.mod.fail(
6306 &block.base,
6690 else => return sema.fail(
6691 block,
63076692 op_src,
63086693 "invalid pointer arithmetic operand: '{s}''",
63096694 .{@tagName(zir_tag)},
......@@ -6317,7 +6702,7 @@ fn analyzeArithmetic(
63176702 if (try sema.resolveDefinedValue(block, rhs_src, casted_rhs)) |rhs_val| {
63186703 _ = lhs_val;
63196704 _ = rhs_val;
6320 return sema.mod.fail(&block.base, src, "TODO implement Sema for comptime pointer arithmetic", .{});
6705 return sema.fail(block, src, "TODO implement Sema for comptime pointer arithmetic", .{});
63216706 } else {
63226707 break :runtime_src rhs_src;
63236708 }
......@@ -6348,7 +6733,7 @@ fn analyzeArithmetic(
63486733 const is_float = scalar_tag == .Float or scalar_tag == .ComptimeFloat;
63496734
63506735 if (!is_int and !(is_float and floatOpAllowed(zir_tag))) {
6351 return sema.mod.fail(&block.base, src, "invalid operands to binary expression: '{s}' and '{s}'", .{
6736 return sema.fail(block, src, "invalid operands to binary expression: '{s}' and '{s}'", .{
63526737 @tagName(lhs_zig_ty_tag), @tagName(rhs_zig_ty_tag),
63536738 });
63546739 }
......@@ -6930,7 +7315,7 @@ fn analyzeArithmetic(
69307315 return block.addBinOp(rs.air_tag, casted_lhs, casted_rhs);
69317316}
69327317
6933fn zirLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7318fn zirLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
69347319 const tracy = trace(@src());
69357320 defer tracy.end();
69367321
......@@ -6943,7 +7328,7 @@ fn zirLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!
69437328
69447329fn zirAsm(
69457330 sema: *Sema,
6946 block: *Scope.Block,
7331 block: *Block,
69477332 extended: Zir.Inst.Extended.InstData,
69487333 inst: Zir.Inst.Index,
69497334) CompileError!Air.Inst.Ref {
......@@ -6958,7 +7343,7 @@ fn zirAsm(
69587343 const clobbers_len = @truncate(u5, extended.small >> 10);
69597344
69607345 if (outputs_len > 1) {
6961 return sema.mod.fail(&block.base, src, "TODO implement Sema for asm with more than 1 output", .{});
7346 return sema.fail(block, src, "TODO implement Sema for asm with more than 1 output", .{});
69627347 }
69637348
69647349 var extra_i = extra.end;
......@@ -6973,7 +7358,7 @@ fn zirAsm(
69737358 output_type_bits >>= 1;
69747359
69757360 if (!is_type) {
6976 return sema.mod.fail(&block.base, src, "TODO implement Sema for asm with non `->` output", .{});
7361 return sema.fail(block, src, "TODO implement Sema for asm with non `->` output", .{});
69777362 }
69787363
69797364 const constraint = sema.code.nullTerminatedString(output.data.constraint);
......@@ -7022,7 +7407,7 @@ fn zirAsm(
70227407/// Only called for equality operators. See also `zirCmp`.
70237408fn zirCmpEq(
70247409 sema: *Sema,
7025 block: *Scope.Block,
7410 block: *Block,
70267411 inst: Zir.Inst.Index,
70277412 op: std.math.CompareOperator,
70287413 air_tag: Air.Inst.Tag,
......@@ -7030,7 +7415,6 @@ fn zirCmpEq(
70307415 const tracy = trace(@src());
70317416 defer tracy.end();
70327417
7033 const mod = sema.mod;
70347418 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
70357419 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
70367420 const src: LazySrcLoc = inst_data.src();
......@@ -7059,11 +7443,11 @@ fn zirCmpEq(
70597443 return sema.analyzeIsNull(block, src, opt_operand, op == .neq);
70607444 }
70617445 if (((lhs_ty_tag == .Null and rhs_ty.isCPtr()) or (rhs_ty_tag == .Null and lhs_ty.isCPtr()))) {
7062 return mod.fail(&block.base, src, "TODO implement C pointer cmp", .{});
7446 return sema.fail(block, src, "TODO implement C pointer cmp", .{});
70637447 }
70647448 if (lhs_ty_tag == .Null or rhs_ty_tag == .Null) {
70657449 const non_null_type = if (lhs_ty_tag == .Null) rhs_ty else lhs_ty;
7066 return mod.fail(&block.base, src, "comparison of '{}' with null", .{non_null_type});
7450 return sema.fail(block, src, "comparison of '{}' with null", .{non_null_type});
70677451 }
70687452 if (lhs_ty_tag == .EnumLiteral and rhs_ty_tag == .Union) {
70697453 return sema.analyzeCmpUnionTag(block, rhs, rhs_src, lhs, lhs_src, op);
......@@ -7112,7 +7496,7 @@ fn zirCmpEq(
71127496
71137497fn analyzeCmpUnionTag(
71147498 sema: *Sema,
7115 block: *Scope.Block,
7499 block: *Block,
71167500 un: Air.Inst.Ref,
71177501 un_src: LazySrcLoc,
71187502 tag: Air.Inst.Ref,
......@@ -7122,7 +7506,7 @@ fn analyzeCmpUnionTag(
71227506 const union_ty = sema.typeOf(un);
71237507 const union_tag_ty = union_ty.unionTagType() orelse {
71247508 // TODO note at declaration site that says "union foo is not tagged"
7125 return sema.mod.fail(&block.base, un_src, "comparison of union and enum literal is only valid for tagged union types", .{});
7509 return sema.fail(block, un_src, "comparison of union and enum literal is only valid for tagged union types", .{});
71267510 };
71277511 // Coerce both the union and the tag to the union's tag type, and then execute the
71287512 // enum comparison codepath.
......@@ -7135,7 +7519,7 @@ fn analyzeCmpUnionTag(
71357519/// Only called for non-equality operators. See also `zirCmpEq`.
71367520fn zirCmp(
71377521 sema: *Sema,
7138 block: *Scope.Block,
7522 block: *Block,
71397523 inst: Zir.Inst.Index,
71407524 op: std.math.CompareOperator,
71417525) CompileError!Air.Inst.Ref {
......@@ -7154,7 +7538,7 @@ fn zirCmp(
71547538
71557539fn analyzeCmp(
71567540 sema: *Sema,
7157 block: *Scope.Block,
7541 block: *Block,
71587542 src: LazySrcLoc,
71597543 lhs: Air.Inst.Ref,
71607544 rhs: Air.Inst.Ref,
......@@ -7174,7 +7558,7 @@ fn analyzeCmp(
71747558 const instructions = &[_]Air.Inst.Ref{ lhs, rhs };
71757559 const resolved_type = try sema.resolvePeerTypes(block, src, instructions, .{ .override = &[_]LazySrcLoc{ lhs_src, rhs_src } });
71767560 if (!resolved_type.isSelfComparable(is_equality_cmp)) {
7177 return sema.mod.fail(&block.base, src, "{s} operator not allowed for type '{}'", .{
7561 return sema.fail(block, src, "{s} operator not allowed for type '{}'", .{
71787562 @tagName(op), resolved_type,
71797563 });
71807564 }
......@@ -7185,7 +7569,7 @@ fn analyzeCmp(
71857569
71867570fn cmpSelf(
71877571 sema: *Sema,
7188 block: *Scope.Block,
7572 block: *Block,
71897573 casted_lhs: Air.Inst.Ref,
71907574 casted_rhs: Air.Inst.Ref,
71917575 op: std.math.CompareOperator,
......@@ -7243,7 +7627,7 @@ fn cmpSelf(
72437627/// cmp_neq(x, true ) => not(x)
72447628fn runtimeBoolCmp(
72457629 sema: *Sema,
7246 block: *Scope.Block,
7630 block: *Block,
72477631 op: std.math.CompareOperator,
72487632 lhs: Air.Inst.Ref,
72497633 rhs: bool,
......@@ -7257,7 +7641,7 @@ fn runtimeBoolCmp(
72577641 }
72587642}
72597643
7260fn zirSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7644fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
72617645 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
72627646 const src = inst_data.src();
72637647 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
......@@ -7271,7 +7655,7 @@ fn zirSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
72717655 .Null,
72727656 .BoundFn,
72737657 .Opaque,
7274 => return sema.mod.fail(&block.base, src, "no size available for type '{}'", .{operand_ty}),
7658 => return sema.fail(block, src, "no size available for type '{}'", .{operand_ty}),
72757659 .Type,
72767660 .EnumLiteral,
72777661 .ComptimeFloat,
......@@ -7298,7 +7682,7 @@ fn zirSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
72987682 return sema.addIntUnsigned(Type.initTag(.comptime_int), abi_size);
72997683}
73007684
7301fn zirBitSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7685fn zirBitSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
73027686 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
73037687 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
73047688 const operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);
......@@ -7309,17 +7693,17 @@ fn zirBitSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
73097693
73107694fn zirThis(
73117695 sema: *Sema,
7312 block: *Scope.Block,
7696 block: *Block,
73137697 extended: Zir.Inst.Extended.InstData,
73147698) CompileError!Air.Inst.Ref {
7315 const this_decl = block.base.namespace().getDecl();
7699 const this_decl = block.namespace.getDecl();
73167700 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
73177701 return sema.analyzeDeclVal(block, src, this_decl);
73187702}
73197703
73207704fn zirClosureCapture(
73217705 sema: *Sema,
7322 block: *Scope.Block,
7706 block: *Block,
73237707 inst: Zir.Inst.Index,
73247708) CompileError!void {
73257709 // TODO: Compile error when closed over values are modified
......@@ -7333,7 +7717,7 @@ fn zirClosureCapture(
73337717
73347718fn zirClosureGet(
73357719 sema: *Sema,
7336 block: *Scope.Block,
7720 block: *Block,
73377721 inst: Zir.Inst.Index,
73387722) CompileError!Air.Inst.Ref {
73397723 // TODO CLOSURE: Test this with inline functions
......@@ -7355,23 +7739,23 @@ fn zirClosureGet(
73557739
73567740fn zirRetAddr(
73577741 sema: *Sema,
7358 block: *Scope.Block,
7742 block: *Block,
73597743 extended: Zir.Inst.Extended.InstData,
73607744) CompileError!Air.Inst.Ref {
73617745 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
7362 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirRetAddr", .{});
7746 return sema.fail(block, src, "TODO: implement Sema.zirRetAddr", .{});
73637747}
73647748
73657749fn zirBuiltinSrc(
73667750 sema: *Sema,
7367 block: *Scope.Block,
7751 block: *Block,
73687752 extended: Zir.Inst.Extended.InstData,
73697753) CompileError!Air.Inst.Ref {
73707754 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
7371 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirBuiltinSrc", .{});
7755 return sema.fail(block, src, "TODO: implement Sema.zirBuiltinSrc", .{});
73727756}
73737757
7374fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7758fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
73757759 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
73767760 const src = inst_data.src();
73777761 const ty = try sema.resolveType(block, src, inst_data.operand);
......@@ -7570,13 +7954,13 @@ fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
75707954 }),
75717955 );
75727956 },
7573 else => |t| return sema.mod.fail(&block.base, src, "TODO: implement zirTypeInfo for {s}", .{
7957 else => |t| return sema.fail(block, src, "TODO: implement zirTypeInfo for {s}", .{
75747958 @tagName(t),
75757959 }),
75767960 }
75777961}
75787962
7579fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7963fn zirTypeof(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
75807964 _ = block;
75817965 const zir_datas = sema.code.instructions.items(.data);
75827966 const inst_data = zir_datas[inst].un_node;
......@@ -7585,7 +7969,7 @@ fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
75857969 return sema.addType(operand_ty);
75867970}
75877971
7588fn zirTypeofElem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7972fn zirTypeofElem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
75897973 _ = block;
75907974 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
75917975 const operand_ptr = sema.resolveInst(inst_data.operand);
......@@ -7593,7 +7977,7 @@ fn zirTypeofElem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
75937977 return sema.addType(elem_ty);
75947978}
75957979
7596fn zirTypeofLog2IntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7980fn zirTypeofLog2IntType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
75977981 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
75987982 const src = inst_data.src();
75997983 const operand = sema.resolveInst(inst_data.operand);
......@@ -7601,14 +7985,14 @@ fn zirTypeofLog2IntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
76017985 return sema.log2IntType(block, operand_ty, src);
76027986}
76037987
7604fn zirLog2IntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
7988fn zirLog2IntType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
76057989 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
76067990 const src = inst_data.src();
76077991 const operand = try sema.resolveType(block, src, inst_data.operand);
76087992 return sema.log2IntType(block, operand, src);
76097993}
76107994
7611fn log2IntType(sema: *Sema, block: *Scope.Block, operand: Type, src: LazySrcLoc) CompileError!Air.Inst.Ref {
7995fn log2IntType(sema: *Sema, block: *Block, operand: Type, src: LazySrcLoc) CompileError!Air.Inst.Ref {
76127996 switch (operand.zigTypeTag()) {
76137997 .ComptimeInt => return Air.Inst.Ref.comptime_int_type,
76147998 .Int => {
......@@ -7620,8 +8004,8 @@ fn log2IntType(sema: *Sema, block: *Scope.Block, operand: Type, src: LazySrcLoc)
76208004 const res = try Module.makeIntType(sema.arena, .unsigned, count);
76218005 return sema.addType(res);
76228006 },
7623 else => return sema.mod.fail(
7624 &block.base,
8007 else => return sema.fail(
8008 block,
76258009 src,
76268010 "bit shifting operation expected integer type, found '{}'",
76278011 .{operand},
......@@ -7631,7 +8015,7 @@ fn log2IntType(sema: *Sema, block: *Scope.Block, operand: Type, src: LazySrcLoc)
76318015
76328016fn zirTypeofPeer(
76338017 sema: *Sema,
7634 block: *Scope.Block,
8018 block: *Block,
76358019 extended: Zir.Inst.Extended.InstData,
76368020) CompileError!Air.Inst.Ref {
76378021 const tracy = trace(@src());
......@@ -7652,7 +8036,7 @@ fn zirTypeofPeer(
76528036 return sema.addType(result_type);
76538037}
76548038
7655fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8039fn zirBoolNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
76568040 const tracy = trace(@src());
76578041 defer tracy.end();
76588042
......@@ -7676,7 +8060,7 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
76768060
76778061fn zirBoolBr(
76788062 sema: *Sema,
7679 parent_block: *Scope.Block,
8063 parent_block: *Block,
76808064 inst: Zir.Inst.Index,
76818065 is_bool_or: bool,
76828066) CompileError!Air.Inst.Ref {
......@@ -7762,7 +8146,7 @@ fn zirBoolBr(
77628146
77638147fn zirIsNonNull(
77648148 sema: *Sema,
7765 block: *Scope.Block,
8149 block: *Block,
77668150 inst: Zir.Inst.Index,
77678151) CompileError!Air.Inst.Ref {
77688152 const tracy = trace(@src());
......@@ -7776,7 +8160,7 @@ fn zirIsNonNull(
77768160
77778161fn zirIsNonNullPtr(
77788162 sema: *Sema,
7779 block: *Scope.Block,
8163 block: *Block,
77808164 inst: Zir.Inst.Index,
77818165) CompileError!Air.Inst.Ref {
77828166 const tracy = trace(@src());
......@@ -7789,7 +8173,7 @@ fn zirIsNonNullPtr(
77898173 return sema.analyzeIsNull(block, src, loaded, true);
77908174}
77918175
7792fn zirIsNonErr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8176fn zirIsNonErr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
77938177 const tracy = trace(@src());
77948178 defer tracy.end();
77958179
......@@ -7798,7 +8182,7 @@ fn zirIsNonErr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
77988182 return sema.analyzeIsNonErr(block, inst_data.src(), operand);
77998183}
78008184
7801fn zirIsNonErrPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8185fn zirIsNonErrPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
78028186 const tracy = trace(@src());
78038187 defer tracy.end();
78048188
......@@ -7811,7 +8195,7 @@ fn zirIsNonErrPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
78118195
78128196fn zirCondbr(
78138197 sema: *Sema,
7814 parent_block: *Scope.Block,
8198 parent_block: *Block,
78158199 inst: Zir.Inst.Index,
78168200) CompileError!Zir.Inst.Index {
78178201 const tracy = trace(@src());
......@@ -7866,7 +8250,7 @@ fn zirCondbr(
78668250 return always_noreturn;
78678251}
78688252
7869fn zirUnreachable(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
8253fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
78708254 const tracy = trace(@src());
78718255 defer tracy.end();
78728256
......@@ -7885,7 +8269,7 @@ fn zirUnreachable(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
78858269
78868270fn zirRetErrValue(
78878271 sema: *Sema,
7888 block: *Scope.Block,
8272 block: *Block,
78898273 inst: Zir.Inst.Index,
78908274) CompileError!Zir.Inst.Index {
78918275 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
......@@ -7909,7 +8293,7 @@ fn zirRetErrValue(
79098293
79108294fn zirRetCoerce(
79118295 sema: *Sema,
7912 block: *Scope.Block,
8296 block: *Block,
79138297 inst: Zir.Inst.Index,
79148298) CompileError!Zir.Inst.Index {
79158299 const tracy = trace(@src());
......@@ -7922,7 +8306,7 @@ fn zirRetCoerce(
79228306 return sema.analyzeRet(block, operand, src, true);
79238307}
79248308
7925fn zirRetNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
8309fn zirRetNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
79268310 const tracy = trace(@src());
79278311 defer tracy.end();
79288312
......@@ -7937,7 +8321,7 @@ fn zirRetNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
79378321 return sema.analyzeRet(block, operand, src, false);
79388322}
79398323
7940fn zirRetLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
8324fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
79418325 const tracy = trace(@src());
79428326 defer tracy.end();
79438327
......@@ -7954,7 +8338,7 @@ fn zirRetLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
79548338
79558339fn analyzeRet(
79568340 sema: *Sema,
7957 block: *Scope.Block,
8341 block: *Block,
79588342 uncasted_operand: Air.Inst.Ref,
79598343 src: LazySrcLoc,
79608344 need_coercion: bool,
......@@ -7987,7 +8371,7 @@ fn floatOpAllowed(tag: Zir.Inst.Tag) bool {
79878371 };
79888372}
79898373
7990fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8374fn zirPtrTypeSimple(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
79918375 const tracy = trace(@src());
79928376 defer tracy.end();
79938377
......@@ -8004,7 +8388,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
80048388 return sema.addType(ty);
80058389}
80068390
8007fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8391fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
80088392 const tracy = trace(@src());
80098393 defer tracy.end();
80108394
......@@ -8045,7 +8429,7 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
80458429 } else 0;
80468430
80478431 if (bit_end != 0 and bit_start >= bit_end * 8)
8048 return sema.mod.fail(&block.base, src, "bit offset starts after end of host integer", .{});
8432 return sema.fail(block, src, "bit offset starts after end of host integer", .{});
80498433
80508434 const elem_type = try sema.resolveType(block, .unneeded, extra.data.elem_type);
80518435
......@@ -8064,7 +8448,7 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
80648448 return sema.addType(ty);
80658449}
80668450
8067fn zirStructInitEmpty(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8451fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
80688452 const tracy = trace(@src());
80698453 defer tracy.end();
80708454
......@@ -8075,14 +8459,13 @@ fn zirStructInitEmpty(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
80758459 return sema.addConstant(struct_type, Value.initTag(.empty_struct_value));
80768460}
80778461
8078fn zirUnionInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8462fn zirUnionInitPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
80798463 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
80808464 const src = inst_data.src();
8081 return sema.mod.fail(&block.base, src, "TODO: Sema.zirUnionInitPtr", .{});
8465 return sema.fail(block, src, "TODO: Sema.zirUnionInitPtr", .{});
80828466}
80838467
8084fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {
8085 const mod = sema.mod;
8468fn zirStructInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {
80868469 const gpa = sema.gpa;
80878470 const zir_datas = sema.code.instructions.items(.data);
80888471 const inst_data = zir_datas[inst].pl_node;
......@@ -8126,12 +8509,12 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
81268509 const other_field_type_data = zir_datas[other_field_type].pl_node;
81278510 const other_field_src: LazySrcLoc = .{ .node_offset_back2tok = other_field_type_data.src_node };
81288511 const msg = msg: {
8129 const msg = try mod.errMsg(&block.base, field_src, "duplicate field", .{});
8512 const msg = try sema.errMsg(block, field_src, "duplicate field", .{});
81308513 errdefer msg.destroy(gpa);
8131 try mod.errNote(&block.base, other_field_src, msg, "other field here", .{});
8514 try sema.errNote(block, other_field_src, msg, "other field here", .{});
81328515 break :msg msg;
81338516 };
8134 return mod.failWithOwnedErrorMsg(&block.base, msg);
8517 return sema.failWithOwnedErrorMsg(msg);
81358518 }
81368519 found_fields[field_index] = item.data.field_type;
81378520 field_inits[field_index] = sema.resolveInst(item.data.init);
......@@ -8149,9 +8532,9 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
81498532 const template = "missing struct field: {s}";
81508533 const args = .{field_name};
81518534 if (root_msg) |msg| {
8152 try mod.errNote(&block.base, src, msg, template, args);
8535 try sema.errNote(block, src, msg, template, args);
81538536 } else {
8154 root_msg = try mod.errMsg(&block.base, src, template, args);
8537 root_msg = try sema.errMsg(block, src, template, args);
81558538 }
81568539 } else {
81578540 field_inits[i] = try sema.addConstant(field.ty, field.default_val);
......@@ -8160,17 +8543,17 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
81608543 if (root_msg) |msg| {
81618544 const fqn = try struct_obj.getFullyQualifiedName(gpa);
81628545 defer gpa.free(fqn);
8163 try mod.errNoteNonLazy(
8546 try sema.mod.errNoteNonLazy(
81648547 struct_obj.srcLoc(),
81658548 msg,
81668549 "struct '{s}' declared here",
81678550 .{fqn},
81688551 );
8169 return mod.failWithOwnedErrorMsg(&block.base, msg);
8552 return sema.failWithOwnedErrorMsg(msg);
81708553 }
81718554
81728555 if (is_ref) {
8173 return mod.fail(&block.base, src, "TODO: Sema.zirStructInit is_ref=true", .{});
8556 return sema.fail(block, src, "TODO: Sema.zirStructInit is_ref=true", .{});
81748557 }
81758558
81768559 const is_comptime = for (field_inits) |field_init| {
......@@ -8187,12 +8570,12 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
81878570 return sema.addConstant(resolved_ty, try Value.Tag.@"struct".create(sema.arena, values));
81888571 }
81898572
8190 return mod.fail(&block.base, src, "TODO: Sema.zirStructInit for runtime-known struct values", .{});
8573 return sema.fail(block, src, "TODO: Sema.zirStructInit for runtime-known struct values", .{});
81918574 } else if (resolved_ty.cast(Type.Payload.Union)) |union_payload| {
81928575 const union_obj = union_payload.data;
81938576
81948577 if (extra.data.fields_len != 1) {
8195 return sema.mod.fail(&block.base, src, "union initialization expects exactly one field", .{});
8578 return sema.fail(block, src, "union initialization expects exactly one field", .{});
81968579 }
81978580
81988581 const item = sema.code.extraData(Zir.Inst.StructInit.Item, extra.end);
......@@ -8205,7 +8588,7 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
82058588 return sema.failWithBadUnionFieldAccess(block, union_obj, field_src, field_name);
82068589
82078590 if (is_ref) {
8208 return mod.fail(&block.base, src, "TODO: Sema.zirStructInit is_ref=true union", .{});
8591 return sema.fail(block, src, "TODO: Sema.zirStructInit is_ref=true union", .{});
82098592 }
82108593
82118594 const init_inst = sema.resolveInst(item.data.init);
......@@ -8218,20 +8601,20 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
82188601 }),
82198602 );
82208603 }
8221 return mod.fail(&block.base, src, "TODO: Sema.zirStructInit for runtime-known union values", .{});
8604 return sema.fail(block, src, "TODO: Sema.zirStructInit for runtime-known union values", .{});
82228605 }
82238606 unreachable;
82248607}
82258608
8226fn zirStructInitAnon(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {
8609fn zirStructInitAnon(sema: *Sema, block: *Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {
82278610 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
82288611 const src = inst_data.src();
82298612
82308613 _ = is_ref;
8231 return sema.mod.fail(&block.base, src, "TODO: Sema.zirStructInitAnon", .{});
8614 return sema.fail(block, src, "TODO: Sema.zirStructInitAnon", .{});
82328615}
82338616
8234fn zirArrayInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {
8617fn zirArrayInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {
82358618 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
82368619 const src = inst_data.src();
82378620
......@@ -8249,10 +8632,13 @@ fn zirArrayInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
82498632 var anon_decl = try block.startAnonDecl();
82508633 defer anon_decl.deinit();
82518634 assert(!(resolved_args.len == 0));
8252 const final_ty = try Type.Tag.array.create(anon_decl.arena(), .{ .len = resolved_args.len, .elem_type = sema.typeOf(resolved_args[0]) });
8635 const final_ty = try Type.Tag.array.create(anon_decl.arena(), .{
8636 .len = resolved_args.len,
8637 .elem_type = try sema.typeOf(resolved_args[0]).copy(anon_decl.arena()),
8638 });
82538639 const buf = try anon_decl.arena().alloc(Value, resolved_args.len);
82548640 for (resolved_args) |arg, i| {
8255 buf[i] = (try sema.resolveMaybeUndefVal(block, src, arg)).?;
8641 buf[i] = try (try sema.resolveMaybeUndefVal(block, src, arg)).?.copy(anon_decl.arena());
82568642 }
82578643
82588644 const val = try Value.Tag.array.create(anon_decl.arena(), buf);
......@@ -8280,21 +8666,21 @@ fn zirArrayInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
82808666 try sema.analyzeLoad(block, .unneeded, alloc, .unneeded);
82818667}
82828668
8283fn zirArrayInitAnon(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {
8669fn zirArrayInitAnon(sema: *Sema, block: *Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {
82848670 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
82858671 const src = inst_data.src();
82868672
82878673 _ = is_ref;
8288 return sema.mod.fail(&block.base, src, "TODO: Sema.zirArrayInitAnon", .{});
8674 return sema.fail(block, src, "TODO: Sema.zirArrayInitAnon", .{});
82898675}
82908676
8291fn zirFieldTypeRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8677fn zirFieldTypeRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
82928678 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
82938679 const src = inst_data.src();
8294 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFieldTypeRef", .{});
8680 return sema.fail(block, src, "TODO: Sema.zirFieldTypeRef", .{});
82958681}
82968682
8297fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8683fn zirFieldType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
82988684 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
82998685 const extra = sema.code.extraData(Zir.Inst.FieldType, inst_data.payload_index).data;
83008686 const src = inst_data.src();
......@@ -8314,7 +8700,7 @@ fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
83148700 return sema.failWithBadUnionFieldAccess(block, union_obj, src, field_name);
83158701 return sema.addType(field.ty);
83168702 },
8317 else => return sema.mod.fail(&block.base, src, "expected struct or union; found '{}'", .{
8703 else => return sema.fail(block, src, "expected struct or union; found '{}'", .{
83188704 resolved_ty,
83198705 }),
83208706 }
......@@ -8322,32 +8708,32 @@ fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
83228708
83238709fn zirErrorReturnTrace(
83248710 sema: *Sema,
8325 block: *Scope.Block,
8711 block: *Block,
83268712 extended: Zir.Inst.Extended.InstData,
83278713) CompileError!Air.Inst.Ref {
83288714 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
8329 return sema.mod.fail(&block.base, src, "TODO: Sema.zirErrorReturnTrace", .{});
8715 return sema.fail(block, src, "TODO: Sema.zirErrorReturnTrace", .{});
83308716}
83318717
83328718fn zirFrame(
83338719 sema: *Sema,
8334 block: *Scope.Block,
8720 block: *Block,
83358721 extended: Zir.Inst.Extended.InstData,
83368722) CompileError!Air.Inst.Ref {
83378723 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
8338 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrame", .{});
8724 return sema.fail(block, src, "TODO: Sema.zirFrame", .{});
83398725}
83408726
83418727fn zirFrameAddress(
83428728 sema: *Sema,
8343 block: *Scope.Block,
8729 block: *Block,
83448730 extended: Zir.Inst.Extended.InstData,
83458731) CompileError!Air.Inst.Ref {
83468732 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
8347 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrameAddress", .{});
8733 return sema.fail(block, src, "TODO: Sema.zirFrameAddress", .{});
83488734}
83498735
8350fn zirAlignOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8736fn zirAlignOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
83518737 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
83528738 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
83538739 const ty = try sema.resolveType(block, operand_src, inst_data.operand);
......@@ -8356,7 +8742,7 @@ fn zirAlignOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
83568742 return sema.addIntUnsigned(Type.comptime_int, abi_align);
83578743}
83588744
8359fn zirBoolToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8745fn zirBoolToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
83608746 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
83618747 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
83628748 const operand = sema.resolveInst(inst_data.operand);
......@@ -8368,31 +8754,31 @@ fn zirBoolToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
83688754 return block.addUnOp(.bool_to_int, operand);
83698755}
83708756
8371fn zirEmbedFile(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8757fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
83728758 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
83738759 const src = inst_data.src();
8374 return sema.mod.fail(&block.base, src, "TODO: Sema.zirEmbedFile", .{});
8760 return sema.fail(block, src, "TODO: Sema.zirEmbedFile", .{});
83758761}
83768762
8377fn zirErrorName(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8763fn zirErrorName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
83788764 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
83798765 const src = inst_data.src();
8380 return sema.mod.fail(&block.base, src, "TODO: Sema.zirErrorName", .{});
8766 return sema.fail(block, src, "TODO: Sema.zirErrorName", .{});
83818767}
83828768
8383fn zirUnaryMath(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8769fn zirUnaryMath(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
83848770 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
83858771 const src = inst_data.src();
8386 return sema.mod.fail(&block.base, src, "TODO: Sema.zirUnaryMath", .{});
8772 return sema.fail(block, src, "TODO: Sema.zirUnaryMath", .{});
83878773}
83888774
8389fn zirTagName(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8775fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
83908776 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
83918777 const src = inst_data.src();
8392 return sema.mod.fail(&block.base, src, "TODO: Sema.zirTagName", .{});
8778 return sema.fail(block, src, "TODO: Sema.zirTagName", .{});
83938779}
83948780
8395fn zirReify(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8781fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
83968782 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
83978783 const src = inst_data.src();
83988784 const type_info_ty = try sema.getBuiltinType(block, src, "TypeInfo");
......@@ -8422,55 +8808,55 @@ fn zirReify(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
84228808 };
84238809 return sema.addType(ty);
84248810 },
8425 .Float => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Float", .{}),
8426 .Pointer => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Pointer", .{}),
8427 .Array => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Array", .{}),
8428 .Struct => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Struct", .{}),
8811 .Float => return sema.fail(block, src, "TODO: Sema.zirReify for Float", .{}),
8812 .Pointer => return sema.fail(block, src, "TODO: Sema.zirReify for Pointer", .{}),
8813 .Array => return sema.fail(block, src, "TODO: Sema.zirReify for Array", .{}),
8814 .Struct => return sema.fail(block, src, "TODO: Sema.zirReify for Struct", .{}),
84298815 .ComptimeFloat => return Air.Inst.Ref.comptime_float_type,
84308816 .ComptimeInt => return Air.Inst.Ref.comptime_int_type,
84318817 .Undefined => return Air.Inst.Ref.undefined_type,
84328818 .Null => return Air.Inst.Ref.null_type,
8433 .Optional => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Optional", .{}),
8434 .ErrorUnion => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for ErrorUnion", .{}),
8435 .ErrorSet => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for ErrorSet", .{}),
8436 .Enum => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Enum", .{}),
8437 .Union => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Union", .{}),
8438 .Fn => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Fn", .{}),
8819 .Optional => return sema.fail(block, src, "TODO: Sema.zirReify for Optional", .{}),
8820 .ErrorUnion => return sema.fail(block, src, "TODO: Sema.zirReify for ErrorUnion", .{}),
8821 .ErrorSet => return sema.fail(block, src, "TODO: Sema.zirReify for ErrorSet", .{}),
8822 .Enum => return sema.fail(block, src, "TODO: Sema.zirReify for Enum", .{}),
8823 .Union => return sema.fail(block, src, "TODO: Sema.zirReify for Union", .{}),
8824 .Fn => return sema.fail(block, src, "TODO: Sema.zirReify for Fn", .{}),
84398825 .BoundFn => @panic("TODO delete BoundFn from the language"),
8440 .Opaque => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Opaque", .{}),
8441 .Frame => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Frame", .{}),
8826 .Opaque => return sema.fail(block, src, "TODO: Sema.zirReify for Opaque", .{}),
8827 .Frame => return sema.fail(block, src, "TODO: Sema.zirReify for Frame", .{}),
84428828 .AnyFrame => return Air.Inst.Ref.anyframe_type,
8443 .Vector => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Vector", .{}),
8829 .Vector => return sema.fail(block, src, "TODO: Sema.zirReify for Vector", .{}),
84448830 .EnumLiteral => return Air.Inst.Ref.enum_literal_type,
84458831 }
84468832}
84478833
8448fn zirTypeName(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8834fn zirTypeName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
84498835 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
84508836 const src = inst_data.src();
8451 return sema.mod.fail(&block.base, src, "TODO: Sema.zirTypeName", .{});
8837 return sema.fail(block, src, "TODO: Sema.zirTypeName", .{});
84528838}
84538839
8454fn zirFrameType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8840fn zirFrameType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
84558841 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
84568842 const src = inst_data.src();
8457 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrameType", .{});
8843 return sema.fail(block, src, "TODO: Sema.zirFrameType", .{});
84588844}
84598845
8460fn zirFrameSize(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8846fn zirFrameSize(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
84618847 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
84628848 const src = inst_data.src();
8463 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFrameSize", .{});
8849 return sema.fail(block, src, "TODO: Sema.zirFrameSize", .{});
84648850}
84658851
8466fn zirFloatToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8852fn zirFloatToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
84678853 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
84688854 const src = inst_data.src();
84698855 // TODO don't forget the safety check!
8470 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFloatToInt", .{});
8856 return sema.fail(block, src, "TODO: Sema.zirFloatToInt", .{});
84718857}
84728858
8473fn zirIntToFloat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8859fn zirIntToFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
84748860 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
84758861 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
84768862 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
......@@ -8492,7 +8878,7 @@ fn zirIntToFloat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
84928878 return block.addTyOp(.int_to_float, dest_ty, operand);
84938879}
84948880
8495fn zirIntToPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8881fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
84968882 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
84978883 const src = inst_data.src();
84988884
......@@ -8505,15 +8891,15 @@ fn zirIntToPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
85058891 const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
85068892 const type_res = try sema.resolveType(block, src, extra.lhs);
85078893 if (type_res.zigTypeTag() != .Pointer)
8508 return sema.mod.fail(&block.base, type_src, "expected pointer, found '{}'", .{type_res});
8894 return sema.fail(block, type_src, "expected pointer, found '{}'", .{type_res});
85098895 const ptr_align = type_res.ptrAlignment(sema.mod.getTarget());
85108896
85118897 if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| {
85128898 const addr = val.toUnsignedInt();
85138899 if (!type_res.isAllowzeroPtr() and addr == 0)
8514 return sema.mod.fail(&block.base, operand_src, "pointer type '{}' does not allow address zero", .{type_res});
8900 return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{type_res});
85158901 if (addr != 0 and addr % ptr_align != 0)
8516 return sema.mod.fail(&block.base, operand_src, "pointer type '{}' requires aligned address", .{type_res});
8902 return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{type_res});
85178903
85188904 const val_payload = try sema.arena.create(Value.Payload.U64);
85198905 val_payload.* = .{
......@@ -8548,13 +8934,13 @@ fn zirIntToPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
85488934 return block.addTyOp(.bitcast, type_res, operand_coerced);
85498935}
85508936
8551fn zirErrSetCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8937fn zirErrSetCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
85528938 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
85538939 const src = inst_data.src();
8554 return sema.mod.fail(&block.base, src, "TODO: Sema.zirErrSetCast", .{});
8940 return sema.fail(block, src, "TODO: Sema.zirErrSetCast", .{});
85558941}
85568942
8557fn zirPtrCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8943fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
85588944 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
85598945 const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
85608946 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
......@@ -8563,12 +8949,12 @@ fn zirPtrCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
85638949 const operand = sema.resolveInst(extra.rhs);
85648950 const operand_ty = sema.typeOf(operand);
85658951 if (operand_ty.zigTypeTag() != .Pointer) {
8566 return sema.mod.fail(&block.base, operand_src, "expected pointer, found {s} type '{}'", .{
8952 return sema.fail(block, operand_src, "expected pointer, found {s} type '{}'", .{
85678953 @tagName(operand_ty.zigTypeTag()), operand_ty,
85688954 });
85698955 }
85708956 if (dest_ty.zigTypeTag() != .Pointer) {
8571 return sema.mod.fail(&block.base, dest_ty_src, "expected pointer, found {s} type '{}'", .{
8957 return sema.fail(block, dest_ty_src, "expected pointer, found {s} type '{}'", .{
85728958 @tagName(dest_ty.zigTypeTag()), dest_ty,
85738959 });
85748960 }
......@@ -8578,7 +8964,7 @@ fn zirPtrCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
85788964 return block.addTyOp(.bitcast, dest_ty, operand);
85798965}
85808966
8581fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
8967fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
85828968 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
85838969 const src = inst_data.src();
85848970 const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
......@@ -8587,7 +8973,6 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
85878973 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);
85888974 const operand = sema.resolveInst(extra.rhs);
85898975 const operand_ty = sema.typeOf(operand);
8590 const mod = sema.mod;
85918976 const dest_is_comptime_int = try sema.checkIntType(block, dest_ty_src, dest_ty);
85928977 const src_is_comptime_int = try sema.checkIntType(block, operand_src, operand_ty);
85938978
......@@ -8595,7 +8980,7 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
85958980 return sema.coerce(block, dest_ty, operand, operand_src);
85968981 }
85978982
8598 const target = mod.getTarget();
8983 const target = sema.mod.getTarget();
85998984 const src_info = operand_ty.intInfo(target);
86008985 const dest_info = dest_ty.intInfo(target);
86018986
......@@ -8605,28 +8990,28 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
86058990
86068991 if (!src_is_comptime_int) {
86078992 if (src_info.signedness != dest_info.signedness) {
8608 return mod.fail(&block.base, operand_src, "expected {s} integer type, found '{}'", .{
8993 return sema.fail(block, operand_src, "expected {s} integer type, found '{}'", .{
86098994 @tagName(dest_info.signedness), operand_ty,
86108995 });
86118996 }
86128997 if (src_info.bits > 0 and src_info.bits < dest_info.bits) {
86138998 const msg = msg: {
8614 const msg = try mod.errMsg(
8615 &block.base,
8999 const msg = try sema.errMsg(
9000 block,
86169001 src,
86179002 "destination type '{}' has more bits than source type '{}'",
86189003 .{ dest_ty, operand_ty },
86199004 );
8620 errdefer msg.destroy(mod.gpa);
8621 try mod.errNote(&block.base, dest_ty_src, msg, "destination type has {d} bits", .{
9005 errdefer msg.destroy(sema.gpa);
9006 try sema.errNote(block, dest_ty_src, msg, "destination type has {d} bits", .{
86229007 dest_info.bits,
86239008 });
8624 try mod.errNote(&block.base, operand_src, msg, "source type has {d} bits", .{
9009 try sema.errNote(block, operand_src, msg, "source type has {d} bits", .{
86259010 src_info.bits,
86269011 });
86279012 break :msg msg;
86289013 };
8629 return mod.failWithOwnedErrorMsg(&block.base, msg);
9014 return sema.failWithOwnedErrorMsg(msg);
86309015 }
86319016 }
86329017
......@@ -8639,13 +9024,13 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
86399024 return block.addTyOp(.trunc, dest_ty, operand);
86409025}
86419026
8642fn zirAlignCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9027fn zirAlignCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
86439028 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
86449029 const src = inst_data.src();
8645 return sema.mod.fail(&block.base, src, "TODO: Sema.zirAlignCast", .{});
9030 return sema.fail(block, src, "TODO: Sema.zirAlignCast", .{});
86469031}
86479032
8648fn zirClz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9033fn zirClz(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
86499034 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
86509035 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
86519036 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
......@@ -8653,7 +9038,7 @@ fn zirClz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
86539038 const operand_ty = sema.typeOf(operand);
86549039 // TODO implement support for vectors
86559040 if (operand_ty.zigTypeTag() != .Int) {
8656 return sema.mod.fail(&block.base, ty_src, "expected integer type, found '{}'", .{
9041 return sema.fail(block, ty_src, "expected integer type, found '{}'", .{
86579042 operand_ty,
86589043 });
86599044 }
......@@ -8672,7 +9057,7 @@ fn zirClz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
86729057 return block.addTyOp(.clz, result_ty, operand);
86739058}
86749059
8675fn zirCtz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9060fn zirCtz(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
86769061 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
86779062 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
86789063 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
......@@ -8680,7 +9065,7 @@ fn zirCtz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
86809065 const operand_ty = sema.typeOf(operand);
86819066 // TODO implement support for vectors
86829067 if (operand_ty.zigTypeTag() != .Int) {
8683 return sema.mod.fail(&block.base, ty_src, "expected integer type, found '{}'", .{
9068 return sema.fail(block, ty_src, "expected integer type, found '{}'", .{
86849069 operand_ty,
86859070 });
86869071 }
......@@ -8692,85 +9077,85 @@ fn zirCtz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
86929077
86939078 const runtime_src = if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |val| {
86949079 if (val.isUndef()) return sema.addConstUndef(result_ty);
8695 return sema.mod.fail(&block.base, operand_src, "TODO: implement comptime @ctz", .{});
9080 return sema.fail(block, operand_src, "TODO: implement comptime @ctz", .{});
86969081 } else operand_src;
86979082
86989083 try sema.requireRuntimeBlock(block, runtime_src);
86999084 return block.addTyOp(.ctz, result_ty, operand);
87009085}
87019086
8702fn zirPopCount(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9087fn zirPopCount(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
87039088 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
87049089 const src = inst_data.src();
8705 return sema.mod.fail(&block.base, src, "TODO: Sema.zirPopCount", .{});
9090 return sema.fail(block, src, "TODO: Sema.zirPopCount", .{});
87069091}
87079092
8708fn zirByteSwap(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9093fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
87099094 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
87109095 const src = inst_data.src();
8711 return sema.mod.fail(&block.base, src, "TODO: Sema.zirByteSwap", .{});
9096 return sema.fail(block, src, "TODO: Sema.zirByteSwap", .{});
87129097}
87139098
8714fn zirBitReverse(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9099fn zirBitReverse(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
87159100 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
87169101 const src = inst_data.src();
8717 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBitReverse", .{});
9102 return sema.fail(block, src, "TODO: Sema.zirBitReverse", .{});
87189103}
87199104
8720fn zirDivExact(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9105fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
87219106 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
87229107 const src = inst_data.src();
8723 return sema.mod.fail(&block.base, src, "TODO: Sema.zirDivExact", .{});
9108 return sema.fail(block, src, "TODO: Sema.zirDivExact", .{});
87249109}
87259110
8726fn zirDivFloor(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9111fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
87279112 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
87289113 const src = inst_data.src();
8729 return sema.mod.fail(&block.base, src, "TODO: Sema.zirDivFloor", .{});
9114 return sema.fail(block, src, "TODO: Sema.zirDivFloor", .{});
87309115}
87319116
8732fn zirDivTrunc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9117fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
87339118 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
87349119 const src = inst_data.src();
8735 return sema.mod.fail(&block.base, src, "TODO: Sema.zirDivTrunc", .{});
9120 return sema.fail(block, src, "TODO: Sema.zirDivTrunc", .{});
87369121}
87379122
8738fn zirShrExact(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9123fn zirShrExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
87399124 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
87409125 const src = inst_data.src();
8741 return sema.mod.fail(&block.base, src, "TODO: Sema.zirShrExact", .{});
9126 return sema.fail(block, src, "TODO: Sema.zirShrExact", .{});
87429127}
87439128
8744fn zirBitOffsetOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9129fn zirBitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
87459130 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
87469131 const src = inst_data.src();
8747 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBitOffsetOf", .{});
9132 return sema.fail(block, src, "TODO: Sema.zirBitOffsetOf", .{});
87489133}
87499134
8750fn zirOffsetOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9135fn zirOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
87519136 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
87529137 const src = inst_data.src();
8753 return sema.mod.fail(&block.base, src, "TODO: Sema.zirOffsetOf", .{});
9138 return sema.fail(block, src, "TODO: Sema.zirOffsetOf", .{});
87549139}
87559140
87569141/// Returns `true` if the type was a comptime_int.
8757fn checkIntType(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!bool {
9142fn checkIntType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!bool {
87589143 switch (ty.zigTypeTag()) {
87599144 .ComptimeInt => return true,
87609145 .Int => return false,
8761 else => return sema.mod.fail(&block.base, src, "expected integer type, found '{}'", .{ty}),
9146 else => return sema.fail(block, src, "expected integer type, found '{}'", .{ty}),
87629147 }
87639148}
87649149
87659150fn checkFloatType(
87669151 sema: *Sema,
8767 block: *Scope.Block,
9152 block: *Block,
87689153 ty_src: LazySrcLoc,
87699154 ty: Type,
87709155) CompileError!void {
87719156 switch (ty.zigTypeTag()) {
87729157 .ComptimeFloat, .Float => {},
8773 else => return sema.mod.fail(&block.base, ty_src, "expected float type, found '{}'", .{
9158 else => return sema.fail(block, ty_src, "expected float type, found '{}'", .{
87749159 ty,
87759160 }),
87769161 }
......@@ -8778,7 +9163,7 @@ fn checkFloatType(
87789163
87799164fn checkAtomicOperandType(
87809165 sema: *Sema,
8781 block: *Scope.Block,
9166 block: *Block,
87829167 ty_src: LazySrcLoc,
87839168 ty: Type,
87849169) CompileError!void {
......@@ -8791,8 +9176,8 @@ fn checkAtomicOperandType(
87919176 .Float => {
87929177 const bit_count = ty.floatBits(target);
87939178 if (bit_count > max_atomic_bits) {
8794 return sema.mod.fail(
8795 &block.base,
9179 return sema.fail(
9180 block,
87969181 ty_src,
87979182 "expected {d}-bit float type or smaller; found {d}-bit float type",
87989183 .{ max_atomic_bits, bit_count },
......@@ -8804,8 +9189,8 @@ fn checkAtomicOperandType(
88049189 else => {
88059190 if (ty.isPtrAtRuntime()) return;
88069191
8807 return sema.mod.fail(
8808 &block.base,
9192 return sema.fail(
9193 block,
88099194 ty_src,
88109195 "expected bool, integer, float, enum, or pointer type; found {}",
88119196 .{ty},
......@@ -8814,8 +9199,8 @@ fn checkAtomicOperandType(
88149199 };
88159200 const bit_count = int_ty.intInfo(target).bits;
88169201 if (bit_count > max_atomic_bits) {
8817 return sema.mod.fail(
8818 &block.base,
9202 return sema.fail(
9203 block,
88199204 ty_src,
88209205 "expected {d}-bit integer type or smaller; found {d}-bit integer type",
88219206 .{ max_atomic_bits, bit_count },
......@@ -8825,7 +9210,7 @@ fn checkAtomicOperandType(
88259210
88269211fn resolveExportOptions(
88279212 sema: *Sema,
8828 block: *Scope.Block,
9213 block: *Block,
88299214 src: LazySrcLoc,
88309215 zir_ref: Zir.Inst.Ref,
88319216) CompileError!std.builtin.ExportOptions {
......@@ -8839,7 +9224,7 @@ fn resolveExportOptions(
88399224 const linkage_index = struct_obj.fields.getIndex("linkage").?;
88409225 const section_index = struct_obj.fields.getIndex("section").?;
88419226 if (!fields[section_index].isNull()) {
8842 return sema.mod.fail(&block.base, src, "TODO: implement exporting with linksection", .{});
9227 return sema.fail(block, src, "TODO: implement exporting with linksection", .{});
88439228 }
88449229 return std.builtin.ExportOptions{
88459230 .name = try fields[name_index].toAllocatedBytes(sema.arena),
......@@ -8850,7 +9235,7 @@ fn resolveExportOptions(
88509235
88519236fn resolveAtomicOrder(
88529237 sema: *Sema,
8853 block: *Scope.Block,
9238 block: *Block,
88549239 src: LazySrcLoc,
88559240 zir_ref: Zir.Inst.Ref,
88569241) CompileError!std.builtin.AtomicOrder {
......@@ -8863,7 +9248,7 @@ fn resolveAtomicOrder(
88639248
88649249fn resolveAtomicRmwOp(
88659250 sema: *Sema,
8866 block: *Scope.Block,
9251 block: *Block,
88679252 src: LazySrcLoc,
88689253 zir_ref: Zir.Inst.Ref,
88699254) CompileError!std.builtin.AtomicRmwOp {
......@@ -8876,11 +9261,10 @@ fn resolveAtomicRmwOp(
88769261
88779262fn zirCmpxchg(
88789263 sema: *Sema,
8879 block: *Scope.Block,
9264 block: *Block,
88809265 inst: Zir.Inst.Index,
88819266 air_tag: Air.Inst.Tag,
88829267) CompileError!Air.Inst.Ref {
8883 const mod = sema.mod;
88849268 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
88859269 const extra = sema.code.extraData(Zir.Inst.Cmpxchg, inst_data.payload_index).data;
88869270 const src = inst_data.src();
......@@ -8896,8 +9280,8 @@ fn zirCmpxchg(
88969280 const elem_ty = sema.typeOf(ptr).elemType();
88979281 try sema.checkAtomicOperandType(block, elem_ty_src, elem_ty);
88989282 if (elem_ty.zigTypeTag() == .Float) {
8899 return mod.fail(
8900 &block.base,
9283 return sema.fail(
9284 block,
89019285 elem_ty_src,
89029286 "expected bool, integer, enum, or pointer type; found '{}'",
89039287 .{elem_ty},
......@@ -8909,16 +9293,16 @@ fn zirCmpxchg(
89099293 const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order);
89109294
89119295 if (@enumToInt(success_order) < @enumToInt(std.builtin.AtomicOrder.Monotonic)) {
8912 return mod.fail(&block.base, success_order_src, "success atomic ordering must be Monotonic or stricter", .{});
9296 return sema.fail(block, success_order_src, "success atomic ordering must be Monotonic or stricter", .{});
89139297 }
89149298 if (@enumToInt(failure_order) < @enumToInt(std.builtin.AtomicOrder.Monotonic)) {
8915 return mod.fail(&block.base, failure_order_src, "failure atomic ordering must be Monotonic or stricter", .{});
9299 return sema.fail(block, failure_order_src, "failure atomic ordering must be Monotonic or stricter", .{});
89169300 }
89179301 if (@enumToInt(failure_order) > @enumToInt(success_order)) {
8918 return mod.fail(&block.base, failure_order_src, "failure atomic ordering must be no stricter than success", .{});
9302 return sema.fail(block, failure_order_src, "failure atomic ordering must be no stricter than success", .{});
89199303 }
89209304 if (failure_order == .Release or failure_order == .AcqRel) {
8921 return mod.fail(&block.base, failure_order_src, "failure atomic ordering must not be Release or AcqRel", .{});
9305 return sema.fail(block, failure_order_src, "failure atomic ordering must not be Release or AcqRel", .{});
89229306 }
89239307
89249308 const result_ty = try Module.optionalType(sema.arena, elem_ty);
......@@ -8965,31 +9349,31 @@ fn zirCmpxchg(
89659349 });
89669350}
89679351
8968fn zirSplat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9352fn zirSplat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
89699353 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
89709354 const src = inst_data.src();
8971 return sema.mod.fail(&block.base, src, "TODO: Sema.zirSplat", .{});
9355 return sema.fail(block, src, "TODO: Sema.zirSplat", .{});
89729356}
89739357
8974fn zirReduce(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9358fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
89759359 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
89769360 const src = inst_data.src();
8977 return sema.mod.fail(&block.base, src, "TODO: Sema.zirReduce", .{});
9361 return sema.fail(block, src, "TODO: Sema.zirReduce", .{});
89789362}
89799363
8980fn zirShuffle(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9364fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
89819365 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
89829366 const src = inst_data.src();
8983 return sema.mod.fail(&block.base, src, "TODO: Sema.zirShuffle", .{});
9367 return sema.fail(block, src, "TODO: Sema.zirShuffle", .{});
89849368}
89859369
8986fn zirSelect(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9370fn zirSelect(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
89879371 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
89889372 const src = inst_data.src();
8989 return sema.mod.fail(&block.base, src, "TODO: Sema.zirSelect", .{});
9373 return sema.fail(block, src, "TODO: Sema.zirSelect", .{});
89909374}
89919375
8992fn zirAtomicLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9376fn zirAtomicLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
89939377 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
89949378 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
89959379 // zig fmt: off
......@@ -9004,8 +9388,8 @@ fn zirAtomicLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
90049388
90059389 switch (order) {
90069390 .Release, .AcqRel => {
9007 return sema.mod.fail(
9008 &block.base,
9391 return sema.fail(
9392 block,
90099393 order_src,
90109394 "@atomicLoad atomic ordering must not be Release or AcqRel",
90119395 .{},
......@@ -9034,8 +9418,7 @@ fn zirAtomicLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
90349418 });
90359419}
90369420
9037fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9038 const mod = sema.mod;
9421fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
90399422 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
90409423 const extra = sema.code.extraData(Zir.Inst.AtomicRmw, inst_data.payload_index).data;
90419424 const src = inst_data.src();
......@@ -9053,14 +9436,14 @@ fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
90539436
90549437 switch (operand_ty.zigTypeTag()) {
90559438 .Enum => if (op != .Xchg) {
9056 return mod.fail(&block.base, op_src, "@atomicRmw with enum only allowed with .Xchg", .{});
9439 return sema.fail(block, op_src, "@atomicRmw with enum only allowed with .Xchg", .{});
90579440 },
90589441 .Bool => if (op != .Xchg) {
9059 return mod.fail(&block.base, op_src, "@atomicRmw with bool only allowed with .Xchg", .{});
9442 return sema.fail(block, op_src, "@atomicRmw with bool only allowed with .Xchg", .{});
90609443 },
90619444 .Float => switch (op) {
90629445 .Xchg, .Add, .Sub => {},
9063 else => return mod.fail(&block.base, op_src, "@atomicRmw with float only allowed with .Xchg, .Add, and .Sub", .{}),
9446 else => return sema.fail(block, op_src, "@atomicRmw with float only allowed with .Xchg, .Add, and .Sub", .{}),
90649447 },
90659448 else => {},
90669449 }
......@@ -9068,7 +9451,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
90689451 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering);
90699452
90709453 if (order == .Unordered) {
9071 return mod.fail(&block.base, order_src, "@atomicRmw atomic ordering must not be Unordered", .{});
9454 return sema.fail(block, order_src, "@atomicRmw atomic ordering must not be Unordered", .{});
90729455 }
90739456
90749457 // special case zero bit types
......@@ -9113,7 +9496,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
91139496 });
91149497}
91159498
9116fn zirAtomicStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
9499fn zirAtomicStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
91179500 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
91189501 const extra = sema.code.extraData(Zir.Inst.AtomicStore, inst_data.payload_index).data;
91199502 const src = inst_data.src();
......@@ -9131,8 +9514,8 @@ fn zirAtomicStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
91319514
91329515 const air_tag: Air.Inst.Tag = switch (order) {
91339516 .Acquire, .AcqRel => {
9134 return sema.mod.fail(
9135 &block.base,
9517 return sema.fail(
9518 block,
91369519 order_src,
91379520 "@atomicStore atomic ordering must not be Acquire or AcqRel",
91389521 .{},
......@@ -9147,37 +9530,37 @@ fn zirAtomicStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
91479530 return sema.storePtr2(block, src, ptr, ptr_src, operand, operand_src, air_tag);
91489531}
91499532
9150fn zirMulAdd(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9533fn zirMulAdd(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
91519534 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
91529535 const src = inst_data.src();
9153 return sema.mod.fail(&block.base, src, "TODO: Sema.zirMulAdd", .{});
9536 return sema.fail(block, src, "TODO: Sema.zirMulAdd", .{});
91549537}
91559538
9156fn zirBuiltinCall(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9539fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
91579540 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
91589541 const src = inst_data.src();
9159 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBuiltinCall", .{});
9542 return sema.fail(block, src, "TODO: Sema.zirBuiltinCall", .{});
91609543}
91619544
9162fn zirFieldPtrType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9545fn zirFieldPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
91639546 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
91649547 const src = inst_data.src();
9165 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFieldPtrType", .{});
9548 return sema.fail(block, src, "TODO: Sema.zirFieldPtrType", .{});
91669549}
91679550
9168fn zirFieldParentPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9551fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
91699552 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
91709553 const src = inst_data.src();
9171 return sema.mod.fail(&block.base, src, "TODO: Sema.zirFieldParentPtr", .{});
9554 return sema.fail(block, src, "TODO: Sema.zirFieldParentPtr", .{});
91729555}
91739556
9174fn zirMaximum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9557fn zirMaximum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
91759558 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
91769559 const src = inst_data.src();
9177 return sema.mod.fail(&block.base, src, "TODO: Sema.zirMaximum", .{});
9560 return sema.fail(block, src, "TODO: Sema.zirMaximum", .{});
91789561}
91799562
9180fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
9563fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
91819564 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
91829565 const extra = sema.code.extraData(Zir.Inst.Memcpy, inst_data.payload_index).data;
91839566 const src = inst_data.src();
......@@ -9188,16 +9571,16 @@ fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
91889571 const dest_ptr_ty = sema.typeOf(dest_ptr);
91899572
91909573 if (dest_ptr_ty.zigTypeTag() != .Pointer) {
9191 return sema.mod.fail(&block.base, dest_src, "expected pointer, found '{}'", .{dest_ptr_ty});
9574 return sema.fail(block, dest_src, "expected pointer, found '{}'", .{dest_ptr_ty});
91929575 }
91939576 if (dest_ptr_ty.isConstPtr()) {
9194 return sema.mod.fail(&block.base, dest_src, "cannot store through const pointer '{}'", .{dest_ptr_ty});
9577 return sema.fail(block, dest_src, "cannot store through const pointer '{}'", .{dest_ptr_ty});
91959578 }
91969579
91979580 const uncasted_src_ptr = sema.resolveInst(extra.source);
91989581 const uncasted_src_ptr_ty = sema.typeOf(uncasted_src_ptr);
91999582 if (uncasted_src_ptr_ty.zigTypeTag() != .Pointer) {
9200 return sema.mod.fail(&block.base, src_src, "expected pointer, found '{}'", .{
9583 return sema.fail(block, src_src, "expected pointer, found '{}'", .{
92019584 uncasted_src_ptr_ty,
92029585 });
92039586 }
......@@ -9224,7 +9607,7 @@ fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
92249607 _ = dest_ptr_val;
92259608 _ = src_ptr_val;
92269609 _ = len_val;
9227 return sema.mod.fail(&block.base, src, "TODO: Sema.zirMemcpy at comptime", .{});
9610 return sema.fail(block, src, "TODO: Sema.zirMemcpy at comptime", .{});
92289611 } else break :rs len_src;
92299612 } else break :rs src_src;
92309613 } else dest_src;
......@@ -9242,7 +9625,7 @@ fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
92429625 });
92439626}
92449627
9245fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
9628fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
92469629 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
92479630 const extra = sema.code.extraData(Zir.Inst.Memset, inst_data.payload_index).data;
92489631 const src = inst_data.src();
......@@ -9252,10 +9635,10 @@ fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
92529635 const dest_ptr = sema.resolveInst(extra.dest);
92539636 const dest_ptr_ty = sema.typeOf(dest_ptr);
92549637 if (dest_ptr_ty.zigTypeTag() != .Pointer) {
9255 return sema.mod.fail(&block.base, dest_src, "expected pointer, found '{}'", .{dest_ptr_ty});
9638 return sema.fail(block, dest_src, "expected pointer, found '{}'", .{dest_ptr_ty});
92569639 }
92579640 if (dest_ptr_ty.isConstPtr()) {
9258 return sema.mod.fail(&block.base, dest_src, "cannot store through const pointer '{}'", .{dest_ptr_ty});
9641 return sema.fail(block, dest_src, "cannot store through const pointer '{}'", .{dest_ptr_ty});
92599642 }
92609643 const elem_ty = dest_ptr_ty.elemType2();
92619644 const value = try sema.coerce(block, elem_ty, sema.resolveInst(extra.byte), value_src);
......@@ -9270,7 +9653,7 @@ fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
92709653 _ = ptr_val;
92719654 _ = len_val;
92729655 _ = val;
9273 return sema.mod.fail(&block.base, src, "TODO: Sema.zirMemset at comptime", .{});
9656 return sema.fail(block, src, "TODO: Sema.zirMemset at comptime", .{});
92749657 } else break :rs value_src;
92759658 } else break :rs len_src;
92769659 } else dest_src;
......@@ -9288,27 +9671,27 @@ fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
92889671 });
92899672}
92909673
9291fn zirMinimum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9674fn zirMinimum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
92929675 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
92939676 const src = inst_data.src();
9294 return sema.mod.fail(&block.base, src, "TODO: Sema.zirMinimum", .{});
9677 return sema.fail(block, src, "TODO: Sema.zirMinimum", .{});
92959678}
92969679
9297fn zirBuiltinAsyncCall(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9680fn zirBuiltinAsyncCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
92989681 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
92999682 const src = inst_data.src();
9300 return sema.mod.fail(&block.base, src, "TODO: Sema.zirBuiltinAsyncCall", .{});
9683 return sema.fail(block, src, "TODO: Sema.zirBuiltinAsyncCall", .{});
93019684}
93029685
9303fn zirResume(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9686fn zirResume(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
93049687 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
93059688 const src = inst_data.src();
9306 return sema.mod.fail(&block.base, src, "TODO: Sema.zirResume", .{});
9689 return sema.fail(block, src, "TODO: Sema.zirResume", .{});
93079690}
93089691
93099692fn zirAwait(
93109693 sema: *Sema,
9311 block: *Scope.Block,
9694 block: *Block,
93129695 inst: Zir.Inst.Index,
93139696 is_nosuspend: bool,
93149697) CompileError!Air.Inst.Ref {
......@@ -9316,12 +9699,12 @@ fn zirAwait(
93169699 const src = inst_data.src();
93179700
93189701 _ = is_nosuspend;
9319 return sema.mod.fail(&block.base, src, "TODO: Sema.zirAwait", .{});
9702 return sema.fail(block, src, "TODO: Sema.zirAwait", .{});
93209703}
93219704
93229705fn zirVarExtended(
93239706 sema: *Sema,
9324 block: *Scope.Block,
9707 block: *Block,
93259708 extended: Zir.Inst.Extended.InstData,
93269709) CompileError!Air.Inst.Ref {
93279710 const extra = sema.code.extraData(Zir.Inst.ExtendedVar, extended.operand);
......@@ -9376,7 +9759,7 @@ fn zirVarExtended(
93769759 if (lib_name != null) {
93779760 // Look at the sema code for functions which has this logic, it just needs to
93789761 // be extracted and shared by both var and func
9379 return sema.mod.fail(&block.base, src, "TODO: handle var with lib_name in Sema", .{});
9762 return sema.fail(block, src, "TODO: handle var with lib_name in Sema", .{});
93809763 }
93819764
93829765 const new_var = try sema.gpa.create(Module.Var);
......@@ -9396,7 +9779,7 @@ fn zirVarExtended(
93969779
93979780fn zirFuncExtended(
93989781 sema: *Sema,
9399 block: *Scope.Block,
9782 block: *Block,
94009783 extended: Zir.Inst.Extended.InstData,
94019784 inst: Zir.Inst.Index,
94029785) CompileError!Air.Inst.Ref {
......@@ -9463,7 +9846,7 @@ fn zirFuncExtended(
94639846
94649847fn zirCUndef(
94659848 sema: *Sema,
9466 block: *Scope.Block,
9849 block: *Block,
94679850 extended: Zir.Inst.Extended.InstData,
94689851) CompileError!Air.Inst.Ref {
94699852 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
......@@ -9476,7 +9859,7 @@ fn zirCUndef(
94769859
94779860fn zirCInclude(
94789861 sema: *Sema,
9479 block: *Scope.Block,
9862 block: *Block,
94809863 extended: Zir.Inst.Extended.InstData,
94819864) CompileError!Air.Inst.Ref {
94829865 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
......@@ -9489,7 +9872,7 @@ fn zirCInclude(
94899872
94909873fn zirCDefine(
94919874 sema: *Sema,
9492 block: *Scope.Block,
9875 block: *Block,
94939876 extended: Zir.Inst.Extended.InstData,
94949877) CompileError!Air.Inst.Ref {
94959878 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
......@@ -9507,41 +9890,41 @@ fn zirCDefine(
95079890
95089891fn zirWasmMemorySize(
95099892 sema: *Sema,
9510 block: *Scope.Block,
9893 block: *Block,
95119894 extended: Zir.Inst.Extended.InstData,
95129895) CompileError!Air.Inst.Ref {
95139896 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
95149897 const src: LazySrcLoc = .{ .node_offset = extra.node };
9515 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirWasmMemorySize", .{});
9898 return sema.fail(block, src, "TODO: implement Sema.zirWasmMemorySize", .{});
95169899}
95179900
95189901fn zirWasmMemoryGrow(
95199902 sema: *Sema,
9520 block: *Scope.Block,
9903 block: *Block,
95219904 extended: Zir.Inst.Extended.InstData,
95229905) CompileError!Air.Inst.Ref {
95239906 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
95249907 const src: LazySrcLoc = .{ .node_offset = extra.node };
9525 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirWasmMemoryGrow", .{});
9908 return sema.fail(block, src, "TODO: implement Sema.zirWasmMemoryGrow", .{});
95269909}
95279910
95289911fn zirBuiltinExtern(
95299912 sema: *Sema,
9530 block: *Scope.Block,
9913 block: *Block,
95319914 extended: Zir.Inst.Extended.InstData,
95329915) CompileError!Air.Inst.Ref {
95339916 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
95349917 const src: LazySrcLoc = .{ .node_offset = extra.node };
9535 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirBuiltinExtern", .{});
9918 return sema.fail(block, src, "TODO: implement Sema.zirBuiltinExtern", .{});
95369919}
95379920
9538fn requireFunctionBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {
9921fn requireFunctionBlock(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
95399922 if (sema.func == null) {
9540 return sema.mod.fail(&block.base, src, "instruction illegal outside function body", .{});
9923 return sema.fail(block, src, "instruction illegal outside function body", .{});
95419924 }
95429925}
95439926
9544fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {
9927fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
95459928 if (block.is_comptime) {
95469929 return sema.failWithNeededComptime(block, src);
95479930 }
......@@ -9551,7 +9934,7 @@ fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void
95519934/// Emit a compile error if type cannot be used for a runtime variable.
95529935fn validateVarType(
95539936 sema: *Sema,
9554 block: *Scope.Block,
9937 block: *Block,
95559938 src: LazySrcLoc,
95569939 var_ty: Type,
95579940 is_extern: bool,
......@@ -9595,7 +9978,7 @@ fn validateVarType(
95959978 },
95969979 } else unreachable; // TODO should not need else unreachable
95979980 if (!ok) {
9598 return sema.mod.fail(&block.base, src, "variable of type '{}' must be const or comptime", .{var_ty});
9981 return sema.fail(block, src, "variable of type '{}' must be const or comptime", .{var_ty});
95999982 }
96009983}
96019984
......@@ -9610,17 +9993,18 @@ pub const PanicId = enum {
96109993
96119994fn addSafetyCheck(
96129995 sema: *Sema,
9613 parent_block: *Scope.Block,
9996 parent_block: *Block,
96149997 ok: Air.Inst.Ref,
96159998 panic_id: PanicId,
96169999) !void {
961710000 const gpa = sema.gpa;
961810001
9619 var fail_block: Scope.Block = .{
10002 var fail_block: Block = .{
962010003 .parent = parent_block,
962110004 .sema = sema,
9622 .wip_capture_scope = parent_block.wip_capture_scope,
962310005 .src_decl = parent_block.src_decl,
10006 .namespace = parent_block.namespace,
10007 .wip_capture_scope = parent_block.wip_capture_scope,
962410008 .instructions = .{},
962510009 .inlining = parent_block.inlining,
962610010 .is_comptime = parent_block.is_comptime,
......@@ -9679,7 +10063,7 @@ fn addSafetyCheck(
967910063
968010064fn panicWithMsg(
968110065 sema: *Sema,
9682 block: *Scope.Block,
10066 block: *Block,
968310067 src: LazySrcLoc,
968410068 msg_inst: Air.Inst.Ref,
968510069) !Zir.Inst.Index {
......@@ -9699,7 +10083,7 @@ fn panicWithMsg(
969910083 const stack_trace_ty = try sema.resolveTypeFields(block, src, unresolved_stack_trace_ty);
970010084 const ptr_stack_trace_ty = try Type.ptr(arena, .{
970110085 .pointee_type = stack_trace_ty,
9702 .@"addrspace" = target_util.defaultAddressSpace(sema.mod.getTarget(), .global_constant), // TODO might need a place that is more dynamic
10086 .@"addrspace" = target_util.defaultAddressSpace(mod.getTarget(), .global_constant), // TODO might need a place that is more dynamic
970310087 });
970410088 const null_stack_trace = try sema.addConstant(
970510089 try Module.optionalType(arena, ptr_stack_trace_ty),
......@@ -9713,7 +10097,7 @@ fn panicWithMsg(
971310097
971410098fn safetyPanic(
971510099 sema: *Sema,
9716 block: *Scope.Block,
10100 block: *Block,
971710101 src: LazySrcLoc,
971810102 panic_id: PanicId,
971910103) CompileError!Zir.Inst.Index {
......@@ -9741,17 +10125,17 @@ fn safetyPanic(
974110125 return sema.panicWithMsg(block, src, casted_msg_inst);
974210126}
974310127
9744fn emitBackwardBranch(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {
10128fn emitBackwardBranch(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
974510129 sema.branch_count += 1;
974610130 if (sema.branch_count > sema.branch_quota) {
974710131 // TODO show the "called from here" stack
9748 return sema.mod.fail(&block.base, src, "evaluation exceeded {d} backwards branches", .{sema.branch_quota});
10132 return sema.fail(block, src, "evaluation exceeded {d} backwards branches", .{sema.branch_quota});
974910133 }
975010134}
975110135
975210136fn fieldVal(
975310137 sema: *Sema,
9754 block: *Scope.Block,
10138 block: *Block,
975510139 src: LazySrcLoc,
975610140 object: Air.Inst.Ref,
975710141 field_name: []const u8,
......@@ -9760,7 +10144,6 @@ fn fieldVal(
976010144 // When editing this function, note that there is corresponding logic to be edited
976110145 // in `fieldPtr`. This function takes a value and returns a value.
976210146
9763 const mod = sema.mod;
976410147 const arena = sema.arena;
976510148 const object_src = src; // TODO better source location
976610149 const object_ty = sema.typeOf(object);
......@@ -9773,8 +10156,8 @@ fn fieldVal(
977310156 try Value.Tag.int_u64.create(arena, object_ty.arrayLen()),
977410157 );
977510158 } else {
9776 return mod.fail(
9777 &block.base,
10159 return sema.fail(
10160 block,
977810161 field_name_src,
977910162 "no member named '{s}' in '{}'",
978010163 .{ field_name, object_ty },
......@@ -9788,8 +10171,8 @@ fn fieldVal(
978810171 const result_ty = object_ty.slicePtrFieldType(buf);
978910172 if (try sema.resolveMaybeUndefVal(block, object_src, object)) |val| {
979010173 if (val.isUndef()) return sema.addConstUndef(result_ty);
9791 return mod.fail(
9792 &block.base,
10174 return sema.fail(
10175 block,
979310176 field_name_src,
979410177 "TODO implement comptime slice ptr",
979510178 .{},
......@@ -9809,8 +10192,8 @@ fn fieldVal(
980910192 try sema.requireRuntimeBlock(block, src);
981010193 return block.addTyOp(.slice_len, result_ty, object);
981110194 } else {
9812 return mod.fail(
9813 &block.base,
10195 return sema.fail(
10196 block,
981410197 field_name_src,
981510198 "no member named '{s}' in '{}'",
981610199 .{ field_name, object_ty },
......@@ -9827,8 +10210,8 @@ fn fieldVal(
982710210 try Value.Tag.int_u64.create(arena, ptr_child.arrayLen()),
982810211 );
982910212 } else {
9830 return mod.fail(
9831 &block.base,
10213 return sema.fail(
10214 block,
983210215 field_name_src,
983310216 "no member named '{s}' in '{}'",
983410217 .{ field_name, object_ty },
......@@ -9865,10 +10248,10 @@ fn fieldVal(
986510248 break :blk name;
986610249 }
986710250 }
9868 return mod.fail(&block.base, src, "no error named '{s}' in '{}'", .{
10251 return sema.fail(block, src, "no error named '{s}' in '{}'", .{
986910252 field_name, child_type,
987010253 });
9871 } else (try mod.getErrorValue(field_name)).key;
10254 } else (try sema.mod.getErrorValue(field_name)).key;
987210255
987310256 return sema.addConstant(
987410257 try child_type.copy(arena),
......@@ -9888,7 +10271,7 @@ fn fieldVal(
988810271 .Union => "union",
988910272 else => unreachable,
989010273 };
9891 return mod.fail(&block.base, src, "{s} '{}' has no member named '{s}'", .{
10274 return sema.fail(block, src, "{s} '{}' has no member named '{s}'", .{
989210275 kw_name, child_type, field_name,
989310276 });
989410277 },
......@@ -9900,14 +10283,14 @@ fn fieldVal(
990010283 }
990110284 const field_index = child_type.enumFieldIndex(field_name) orelse {
990210285 const msg = msg: {
9903 const msg = try mod.errMsg(
9904 &block.base,
10286 const msg = try sema.errMsg(
10287 block,
990510288 src,
990610289 "enum '{}' has no member named '{s}'",
990710290 .{ child_type, field_name },
990810291 );
990910292 errdefer msg.destroy(sema.gpa);
9910 try mod.errNoteNonLazy(
10293 try sema.mod.errNoteNonLazy(
991110294 child_type.declSrcLoc(),
991210295 msg,
991310296 "enum declared here",
......@@ -9915,25 +10298,25 @@ fn fieldVal(
991510298 );
991610299 break :msg msg;
991710300 };
9918 return mod.failWithOwnedErrorMsg(&block.base, msg);
10301 return sema.failWithOwnedErrorMsg(msg);
991910302 };
992010303 const field_index_u32 = @intCast(u32, field_index);
992110304 const enum_val = try Value.Tag.enum_field_index.create(arena, field_index_u32);
992210305 return sema.addConstant(try child_type.copy(arena), enum_val);
992310306 },
9924 else => return mod.fail(&block.base, src, "type '{}' has no members", .{child_type}),
10307 else => return sema.fail(block, src, "type '{}' has no members", .{child_type}),
992510308 }
992610309 },
992710310 .Struct => return sema.structFieldVal(block, src, object, field_name, field_name_src, object_ty),
992810311 .Union => return sema.unionFieldVal(block, src, object, field_name, field_name_src, object_ty),
992910312 else => {},
993010313 }
9931 return mod.fail(&block.base, src, "type '{}' does not support field access", .{object_ty});
10314 return sema.fail(block, src, "type '{}' does not support field access", .{object_ty});
993210315}
993310316
993410317fn fieldPtr(
993510318 sema: *Sema,
9936 block: *Scope.Block,
10319 block: *Block,
993710320 src: LazySrcLoc,
993810321 object_ptr: Air.Inst.Ref,
993910322 field_name: []const u8,
......@@ -9942,12 +10325,11 @@ fn fieldPtr(
994210325 // When editing this function, note that there is corresponding logic to be edited
994310326 // in `fieldVal`. This function takes a pointer and returns a pointer.
994410327
9945 const mod = sema.mod;
994610328 const object_ptr_src = src; // TODO better source location
994710329 const object_ptr_ty = sema.typeOf(object_ptr);
994810330 const object_ty = switch (object_ptr_ty.zigTypeTag()) {
994910331 .Pointer => object_ptr_ty.elemType(),
9950 else => return mod.fail(&block.base, object_ptr_src, "expected pointer, found '{}'", .{object_ptr_ty}),
10332 else => return sema.fail(block, object_ptr_src, "expected pointer, found '{}'", .{object_ptr_ty}),
995110333 };
995210334 switch (object_ty.zigTypeTag()) {
995310335 .Array => {
......@@ -9959,8 +10341,8 @@ fn fieldPtr(
995910341 try Value.Tag.int_u64.create(anon_decl.arena(), object_ty.arrayLen()),
996010342 ));
996110343 } else {
9962 return mod.fail(
9963 &block.base,
10344 return sema.fail(
10345 block,
996410346 field_name_src,
996510347 "no member named '{s}' in '{}'",
996610348 .{ field_name, object_ty },
......@@ -9977,22 +10359,22 @@ fn fieldPtr(
997710359 // the runtime value to it, and then return the `alloc`.
997810360 // In both cases the pointer should be const.
997910361 if (mem.eql(u8, field_name, "ptr")) {
9980 return mod.fail(
9981 &block.base,
10362 return sema.fail(
10363 block,
998210364 field_name_src,
998310365 "TODO: implement reference to 'ptr' field of slice '{}'",
998410366 .{object_ty},
998510367 );
998610368 } else if (mem.eql(u8, field_name, "len")) {
9987 return mod.fail(
9988 &block.base,
10369 return sema.fail(
10370 block,
998910371 field_name_src,
999010372 "TODO: implement reference to 'len' field of slice '{}'",
999110373 .{object_ty},
999210374 );
999310375 } else {
9994 return mod.fail(
9995 &block.base,
10376 return sema.fail(
10377 block,
999610378 field_name_src,
999710379 "no member named '{s}' in '{}'",
999810380 .{ field_name, object_ty },
......@@ -10011,8 +10393,8 @@ fn fieldPtr(
1001110393 try Value.Tag.int_u64.create(anon_decl.arena(), ptr_child.arrayLen()),
1001210394 ));
1001310395 } else {
10014 return mod.fail(
10015 &block.base,
10396 return sema.fail(
10397 block,
1001610398 field_name_src,
1001710399 "no member named '{s}' in '{}'",
1001810400 .{ field_name, object_ty },
......@@ -10051,10 +10433,10 @@ fn fieldPtr(
1005110433 break :blk name;
1005210434 }
1005310435 }
10054 return mod.fail(&block.base, src, "no error named '{s}' in '{}'", .{
10436 return sema.fail(block, src, "no error named '{s}' in '{}'", .{
1005510437 field_name, child_type,
1005610438 });
10057 } else (try mod.getErrorValue(field_name)).key;
10439 } else (try sema.mod.getErrorValue(field_name)).key;
1005810440
1005910441 var anon_decl = try block.startAnonDecl();
1006010442 defer anon_decl.deinit();
......@@ -10076,7 +10458,7 @@ fn fieldPtr(
1007610458 .Union => "union",
1007710459 else => unreachable,
1007810460 };
10079 return mod.fail(&block.base, src, "{s} '{}' has no member named '{s}'", .{
10461 return sema.fail(block, src, "{s} '{}' has no member named '{s}'", .{
1008010462 kw_name, child_type, field_name,
1008110463 });
1008210464 },
......@@ -10088,14 +10470,14 @@ fn fieldPtr(
1008810470 }
1008910471 const field_index = child_type.enumFieldIndex(field_name) orelse {
1009010472 const msg = msg: {
10091 const msg = try mod.errMsg(
10092 &block.base,
10473 const msg = try sema.errMsg(
10474 block,
1009310475 src,
1009410476 "enum '{}' has no member named '{s}'",
1009510477 .{ child_type, field_name },
1009610478 );
1009710479 errdefer msg.destroy(sema.gpa);
10098 try mod.errNoteNonLazy(
10480 try sema.mod.errNoteNonLazy(
1009910481 child_type.declSrcLoc(),
1010010482 msg,
1010110483 "enum declared here",
......@@ -10103,7 +10485,7 @@ fn fieldPtr(
1010310485 );
1010410486 break :msg msg;
1010510487 };
10106 return mod.failWithOwnedErrorMsg(&block.base, msg);
10488 return sema.failWithOwnedErrorMsg(msg);
1010710489 };
1010810490 const field_index_u32 = @intCast(u32, field_index);
1010910491 var anon_decl = try block.startAnonDecl();
......@@ -10113,19 +10495,19 @@ fn fieldPtr(
1011310495 try Value.Tag.enum_field_index.create(anon_decl.arena(), field_index_u32),
1011410496 ));
1011510497 },
10116 else => return mod.fail(&block.base, src, "type '{}' has no members", .{child_type}),
10498 else => return sema.fail(block, src, "type '{}' has no members", .{child_type}),
1011710499 }
1011810500 },
1011910501 .Struct => return sema.structFieldPtr(block, src, object_ptr, field_name, field_name_src, object_ty),
1012010502 .Union => return sema.unionFieldPtr(block, src, object_ptr, field_name, field_name_src, object_ty),
1012110503 else => {},
1012210504 }
10123 return mod.fail(&block.base, src, "type '{}' does not support field access", .{object_ty});
10505 return sema.fail(block, src, "type '{}' does not support field access", .{object_ty});
1012410506}
1012510507
1012610508fn fieldCallBind(
1012710509 sema: *Sema,
10128 block: *Scope.Block,
10510 block: *Block,
1012910511 src: LazySrcLoc,
1013010512 raw_ptr: Air.Inst.Ref,
1013110513 field_name: []const u8,
......@@ -10134,13 +10516,12 @@ fn fieldCallBind(
1013410516 // When editing this function, note that there is corresponding logic to be edited
1013510517 // in `fieldVal`. This function takes a pointer and returns a pointer.
1013610518
10137 const mod = sema.mod;
1013810519 const raw_ptr_src = src; // TODO better source location
1013910520 const raw_ptr_ty = sema.typeOf(raw_ptr);
1014010521 const inner_ty = if (raw_ptr_ty.zigTypeTag() == .Pointer and raw_ptr_ty.ptrSize() == .One)
1014110522 raw_ptr_ty.childType()
1014210523 else
10143 return mod.fail(&block.base, raw_ptr_src, "expected single pointer, found '{}'", .{raw_ptr_ty});
10524 return sema.fail(block, raw_ptr_src, "expected single pointer, found '{}'", .{raw_ptr_ty});
1014410525
1014510526 // Optionally dereference a second pointer to get the concrete type.
1014610527 const is_double_ptr = inner_ty.zigTypeTag() == .Pointer and inner_ty.ptrSize() == .One;
......@@ -10209,7 +10590,7 @@ fn fieldCallBind(
1020910590 };
1021010591 return sema.analyzeLoad(block, src, ptr_inst, src);
1021110592 },
10212 .Union => return sema.mod.fail(&block.base, src, "TODO implement field calls on unions", .{}),
10593 .Union => return sema.fail(block, src, "TODO implement field calls on unions", .{}),
1021310594 .Type => {
1021410595 const namespace = try sema.analyzeLoad(block, src, object_ptr, src);
1021510596 return sema.fieldVal(block, src, namespace, field_name, field_name_src);
......@@ -10262,29 +10643,28 @@ fn fieldCallBind(
1026210643 else => {},
1026310644 }
1026410645
10265 return mod.fail(&block.base, src, "type '{}' has no field or member function named '{s}'", .{ concrete_ty, field_name });
10646 return sema.fail(block, src, "type '{}' has no field or member function named '{s}'", .{ concrete_ty, field_name });
1026610647}
1026710648
1026810649fn namespaceLookup(
1026910650 sema: *Sema,
10270 block: *Scope.Block,
10651 block: *Block,
1027110652 src: LazySrcLoc,
10272 namespace: *Scope.Namespace,
10653 namespace: *Namespace,
1027310654 decl_name: []const u8,
1027410655) CompileError!?*Decl {
10275 const mod = sema.mod;
1027610656 const gpa = sema.gpa;
1027710657 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl| {
10278 if (!decl.is_pub and decl.namespace.file_scope != block.getFileScope()) {
10658 if (!decl.is_pub and decl.getFileScope() != block.getFileScope()) {
1027910659 const msg = msg: {
10280 const msg = try mod.errMsg(&block.base, src, "'{s}' is not marked 'pub'", .{
10660 const msg = try sema.errMsg(block, src, "'{s}' is not marked 'pub'", .{
1028110661 decl_name,
1028210662 });
1028310663 errdefer msg.destroy(gpa);
10284 try mod.errNoteNonLazy(decl.srcLoc(), msg, "declared here", .{});
10664 try sema.mod.errNoteNonLazy(decl.srcLoc(), msg, "declared here", .{});
1028510665 break :msg msg;
1028610666 };
10287 return mod.failWithOwnedErrorMsg(&block.base, msg);
10667 return sema.failWithOwnedErrorMsg(msg);
1028810668 }
1028910669 return decl;
1029010670 }
......@@ -10293,9 +10673,9 @@ fn namespaceLookup(
1029310673
1029410674fn namespaceLookupRef(
1029510675 sema: *Sema,
10296 block: *Scope.Block,
10676 block: *Block,
1029710677 src: LazySrcLoc,
10298 namespace: *Scope.Namespace,
10678 namespace: *Namespace,
1029910679 decl_name: []const u8,
1030010680) CompileError!?Air.Inst.Ref {
1030110681 const decl = (try sema.namespaceLookup(block, src, namespace, decl_name)) orelse return null;
......@@ -10304,7 +10684,7 @@ fn namespaceLookupRef(
1030410684
1030510685fn structFieldPtr(
1030610686 sema: *Sema,
10307 block: *Scope.Block,
10687 block: *Block,
1030810688 src: LazySrcLoc,
1030910689 struct_ptr: Air.Inst.Ref,
1031010690 field_name: []const u8,
......@@ -10344,7 +10724,7 @@ fn structFieldPtr(
1034410724
1034510725fn structFieldVal(
1034610726 sema: *Sema,
10347 block: *Scope.Block,
10727 block: *Block,
1034810728 src: LazySrcLoc,
1034910729 struct_byval: Air.Inst.Ref,
1035010730 field_name: []const u8,
......@@ -10382,7 +10762,7 @@ fn structFieldVal(
1038210762
1038310763fn unionFieldPtr(
1038410764 sema: *Sema,
10385 block: *Scope.Block,
10765 block: *Block,
1038610766 src: LazySrcLoc,
1038710767 union_ptr: Air.Inst.Ref,
1038810768 field_name: []const u8,
......@@ -10424,7 +10804,7 @@ fn unionFieldPtr(
1042410804
1042510805fn unionFieldVal(
1042610806 sema: *Sema,
10427 block: *Scope.Block,
10807 block: *Block,
1042810808 src: LazySrcLoc,
1042910809 union_byval: Air.Inst.Ref,
1043010810 field_name: []const u8,
......@@ -10450,12 +10830,12 @@ fn unionFieldVal(
1045010830 }
1045110831
1045210832 try sema.requireRuntimeBlock(block, src);
10453 return sema.mod.fail(&block.base, src, "TODO implement runtime union field access", .{});
10833 return sema.fail(block, src, "TODO implement runtime union field access", .{});
1045410834}
1045510835
1045610836fn elemPtr(
1045710837 sema: *Sema,
10458 block: *Scope.Block,
10838 block: *Block,
1045910839 src: LazySrcLoc,
1046010840 array_ptr: Air.Inst.Ref,
1046110841 elem_index: Air.Inst.Ref,
......@@ -10465,10 +10845,10 @@ fn elemPtr(
1046510845 const array_ptr_ty = sema.typeOf(array_ptr);
1046610846 const array_ty = switch (array_ptr_ty.zigTypeTag()) {
1046710847 .Pointer => array_ptr_ty.elemType(),
10468 else => return sema.mod.fail(&block.base, array_ptr_src, "expected pointer, found '{}'", .{array_ptr_ty}),
10848 else => return sema.fail(block, array_ptr_src, "expected pointer, found '{}'", .{array_ptr_ty}),
1046910849 };
1047010850 if (!array_ty.isIndexable()) {
10471 return sema.mod.fail(&block.base, src, "array access of non-array type '{}'", .{array_ty});
10851 return sema.fail(block, src, "array access of non-array type '{}'", .{array_ty});
1047210852 }
1047310853 if (array_ty.isSinglePointer() and array_ty.elemType().zigTypeTag() == .Array) {
1047410854 // we have to deref the ptr operand to get the actual array pointer
......@@ -10479,12 +10859,12 @@ fn elemPtr(
1047910859 return sema.elemPtrArray(block, src, array_ptr, elem_index, elem_index_src);
1048010860 }
1048110861
10482 return sema.mod.fail(&block.base, src, "TODO implement more analyze elemptr", .{});
10862 return sema.fail(block, src, "TODO implement more analyze elemptr", .{});
1048310863}
1048410864
1048510865fn elemVal(
1048610866 sema: *Sema,
10487 block: *Scope.Block,
10867 block: *Block,
1048810868 src: LazySrcLoc,
1048910869 array_maybe_ptr: Air.Inst.Ref,
1049010870 elem_index: Air.Inst.Ref,
......@@ -10497,7 +10877,7 @@ fn elemVal(
1049710877 .Slice => {
1049810878 if (try sema.resolveDefinedValue(block, src, array_maybe_ptr)) |slice_val| {
1049910879 _ = slice_val;
10500 return sema.mod.fail(&block.base, src, "TODO implement Sema for elemVal for comptime known slice", .{});
10880 return sema.fail(block, src, "TODO implement Sema for elemVal for comptime known slice", .{});
1050110881 }
1050210882 try sema.requireRuntimeBlock(block, src);
1050310883 return block.addBinOp(.slice_elem_val, array_maybe_ptr, elem_index);
......@@ -10505,7 +10885,7 @@ fn elemVal(
1050510885 .Many, .C => {
1050610886 if (try sema.resolveDefinedValue(block, src, array_maybe_ptr)) |ptr_val| {
1050710887 _ = ptr_val;
10508 return sema.mod.fail(&block.base, src, "TODO implement Sema for elemVal for comptime known pointer", .{});
10888 return sema.fail(block, src, "TODO implement Sema for elemVal for comptime known pointer", .{});
1050910889 }
1051010890 try sema.requireRuntimeBlock(block, src);
1051110891 return block.addBinOp(.ptr_elem_val, array_maybe_ptr, elem_index);
......@@ -10520,7 +10900,7 @@ fn elemVal(
1052010900 const slice = try sema.analyzeLoad(block, src, array_maybe_ptr, array_ptr_src);
1052110901 if (try sema.resolveDefinedValue(block, src, slice)) |slice_val| {
1052210902 _ = slice_val;
10523 return sema.mod.fail(&block.base, src, "TODO implement Sema for elemVal for comptime known slice", .{});
10903 return sema.fail(block, src, "TODO implement Sema for elemVal for comptime known slice", .{});
1052410904 }
1052510905 try sema.requireRuntimeBlock(block, src);
1052610906 return block.addBinOp(.slice_elem_val, slice, elem_index);
......@@ -10534,7 +10914,7 @@ fn elemVal(
1053410914 const ptr = try sema.analyzeLoad(block, src, array_maybe_ptr, array_ptr_src);
1053510915 if (try sema.resolveDefinedValue(block, src, ptr)) |ptr_val| {
1053610916 _ = ptr_val;
10537 return sema.mod.fail(&block.base, src, "TODO implement Sema for elemVal for comptime known pointer", .{});
10917 return sema.fail(block, src, "TODO implement Sema for elemVal for comptime known pointer", .{});
1053810918 }
1053910919 try sema.requireRuntimeBlock(block, src);
1054010920 return block.addBinOp(.ptr_elem_val, ptr, elem_index);
......@@ -10542,8 +10922,8 @@ fn elemVal(
1054210922 try sema.requireRuntimeBlock(block, src);
1054310923 return block.addBinOp(.ptr_ptr_elem_val, array_maybe_ptr, elem_index);
1054410924 },
10545 .One => return sema.mod.fail(
10546 &block.base,
10925 .One => return sema.fail(
10926 block,
1054710927 array_ptr_src,
1054810928 "expected pointer, found '{}'",
1054910929 .{indexable_ty.elemType()},
......@@ -10553,8 +10933,8 @@ fn elemVal(
1055310933 const ptr = try sema.elemPtr(block, src, array_maybe_ptr, elem_index, elem_index_src);
1055410934 return sema.analyzeLoad(block, src, ptr, elem_index_src);
1055510935 },
10556 else => return sema.mod.fail(
10557 &block.base,
10936 else => return sema.fail(
10937 block,
1055810938 array_ptr_src,
1055910939 "expected pointer, found '{}'",
1056010940 .{indexable_ty},
......@@ -10562,8 +10942,8 @@ fn elemVal(
1056210942 }
1056310943 },
1056410944 },
10565 else => return sema.mod.fail(
10566 &block.base,
10945 else => return sema.fail(
10946 block,
1056710947 array_ptr_src,
1056810948 "expected pointer, found '{}'",
1056910949 .{maybe_ptr_ty},
......@@ -10573,7 +10953,7 @@ fn elemVal(
1057310953
1057410954fn elemPtrArray(
1057510955 sema: *Sema,
10576 block: *Scope.Block,
10956 block: *Block,
1057710957 src: LazySrcLoc,
1057810958 array_ptr: Air.Inst.Ref,
1057910959 elem_index: Air.Inst.Ref,
......@@ -10613,7 +10993,7 @@ fn elemPtrArray(
1061310993
1061410994fn coerce(
1061510995 sema: *Sema,
10616 block: *Scope.Block,
10996 block: *Block,
1061710997 dest_type_unresolved: Type,
1061810998 inst: Air.Inst.Ref,
1061910999 inst_src: LazySrcLoc,
......@@ -10631,10 +11011,10 @@ fn coerce(
1063111011 if (dest_type.eql(inst_ty))
1063211012 return inst;
1063311013
10634 const mod = sema.mod;
1063511014 const arena = sema.arena;
11015 const target = sema.mod.getTarget();
1063611016
10637 const in_memory_result = coerceInMemoryAllowed(dest_type, inst_ty, false, mod.getTarget());
11017 const in_memory_result = coerceInMemoryAllowed(dest_type, inst_ty, false, target);
1063811018 if (in_memory_result == .ok) {
1063911019 return sema.bitcast(block, dest_type, inst, inst_src);
1064011020 }
......@@ -10651,8 +11031,6 @@ fn coerce(
1065111031 if (try sema.coerceNum(block, dest_type, inst, inst_src)) |some|
1065211032 return some;
1065311033
10654 const target = mod.getTarget();
10655
1065611034 switch (dest_type.zigTypeTag()) {
1065711035 .Optional => {
1065811036 // null to ?T
......@@ -10679,7 +11057,7 @@ fn coerce(
1067911057 if (inst_ty.ptrAddressSpace() != dest_type.ptrAddressSpace()) break :src_array_ptr;
1068011058
1068111059 const dst_elem_type = dest_type.elemType();
10682 switch (coerceInMemoryAllowed(dst_elem_type, array_elem_type, dest_is_mut, mod.getTarget())) {
11060 switch (coerceInMemoryAllowed(dst_elem_type, array_elem_type, dest_is_mut, target)) {
1068311061 .ok => {},
1068411062 .no_match => break :src_array_ptr,
1068511063 }
......@@ -10748,14 +11126,14 @@ fn coerce(
1074811126 const resolved_dest_type = try sema.resolveTypeFields(block, inst_src, dest_type);
1074911127 const field_index = resolved_dest_type.enumFieldIndex(bytes) orelse {
1075011128 const msg = msg: {
10751 const msg = try mod.errMsg(
10752 &block.base,
11129 const msg = try sema.errMsg(
11130 block,
1075311131 inst_src,
1075411132 "enum '{}' has no field named '{s}'",
1075511133 .{ resolved_dest_type, bytes },
1075611134 );
1075711135 errdefer msg.destroy(sema.gpa);
10758 try mod.errNoteNonLazy(
11136 try sema.mod.errNoteNonLazy(
1075911137 resolved_dest_type.declSrcLoc(),
1076011138 msg,
1076111139 "enum declared here",
......@@ -10763,7 +11141,7 @@ fn coerce(
1076311141 );
1076411142 break :msg msg;
1076511143 };
10766 return mod.failWithOwnedErrorMsg(&block.base, msg);
11144 return sema.failWithOwnedErrorMsg(msg);
1076711145 };
1076811146 return sema.addConstant(
1076911147 resolved_dest_type,
......@@ -10786,7 +11164,7 @@ fn coerce(
1078611164 else => {},
1078711165 }
1078811166
10789 return mod.fail(&block.base, inst_src, "expected {}, found {}", .{ dest_type, inst_ty });
11167 return sema.fail(block, inst_src, "expected {}, found {}", .{ dest_type, inst_ty });
1079011168}
1079111169
1079211170const InMemoryCoercionResult = enum {
......@@ -10887,7 +11265,7 @@ fn coerceInMemoryAllowed(dest_type: Type, src_type: Type, dest_is_mut: bool, tar
1088711265
1088811266fn coerceNum(
1088911267 sema: *Sema,
10890 block: *Scope.Block,
11268 block: *Block,
1089111269 dest_type: Type,
1089211270 inst: Air.Inst.Ref,
1089311271 inst_src: LazySrcLoc,
......@@ -10903,13 +11281,13 @@ fn coerceNum(
1090311281 .ComptimeInt, .Int => switch (src_zig_tag) {
1090411282 .Float, .ComptimeFloat => {
1090511283 if (val.floatHasFraction()) {
10906 return sema.mod.fail(&block.base, inst_src, "fractional component prevents float value {} from coercion to type '{}'", .{ val, dest_type });
11284 return sema.fail(block, inst_src, "fractional component prevents float value {} from coercion to type '{}'", .{ val, dest_type });
1090711285 }
10908 return sema.mod.fail(&block.base, inst_src, "TODO float to int", .{});
11286 return sema.fail(block, inst_src, "TODO float to int", .{});
1090911287 },
1091011288 .Int, .ComptimeInt => {
1091111289 if (!val.intFitsInType(dest_type, target)) {
10912 return sema.mod.fail(&block.base, inst_src, "type {} cannot represent integer value {}", .{ dest_type, val });
11290 return sema.fail(block, inst_src, "type {} cannot represent integer value {}", .{ dest_type, val });
1091311291 }
1091411292 return try sema.addConstant(dest_type, val);
1091511293 },
......@@ -10923,8 +11301,8 @@ fn coerceNum(
1092311301 .Float => {
1092411302 const result_val = try val.floatCast(sema.arena, dest_type);
1092511303 if (!val.eql(result_val, dest_type)) {
10926 return sema.mod.fail(
10927 &block.base,
11304 return sema.fail(
11305 block,
1092811306 inst_src,
1092911307 "type {} cannot represent float value {}",
1093011308 .{ dest_type, val },
......@@ -10937,8 +11315,8 @@ fn coerceNum(
1093711315 // TODO implement this compile error
1093811316 //const int_again_val = try result_val.floatToInt(sema.arena, inst_ty);
1093911317 //if (!int_again_val.eql(val, inst_ty)) {
10940 // return sema.mod.fail(
10941 // &block.base,
11318 // return sema.fail(
11319 // block,
1094211320 // inst_src,
1094311321 // "type {} cannot represent integer value {}",
1094411322 // .{ dest_type, val },
......@@ -10955,13 +11333,13 @@ fn coerceNum(
1095511333
1095611334fn coerceVarArgParam(
1095711335 sema: *Sema,
10958 block: *Scope.Block,
11336 block: *Block,
1095911337 inst: Air.Inst.Ref,
1096011338 inst_src: LazySrcLoc,
1096111339) !Air.Inst.Ref {
1096211340 const inst_ty = sema.typeOf(inst);
1096311341 switch (inst_ty.zigTypeTag()) {
10964 .ComptimeInt, .ComptimeFloat => return sema.mod.fail(&block.base, inst_src, "integer and float literals in var args function must be casted", .{}),
11342 .ComptimeInt, .ComptimeFloat => return sema.fail(block, inst_src, "integer and float literals in var args function must be casted", .{}),
1096511343 else => {},
1096611344 }
1096711345 // TODO implement more of this function.
......@@ -10971,17 +11349,17 @@ fn coerceVarArgParam(
1097111349// TODO migrate callsites to use storePtr2 instead.
1097211350fn storePtr(
1097311351 sema: *Sema,
10974 block: *Scope.Block,
11352 block: *Block,
1097511353 src: LazySrcLoc,
1097611354 ptr: Air.Inst.Ref,
1097711355 uncasted_operand: Air.Inst.Ref,
10978) !void {
11356) CompileError!void {
1097911357 return sema.storePtr2(block, src, ptr, src, uncasted_operand, src, .store);
1098011358}
1098111359
1098211360fn storePtr2(
1098311361 sema: *Sema,
10984 block: *Scope.Block,
11362 block: *Block,
1098511363 src: LazySrcLoc,
1098611364 ptr: Air.Inst.Ref,
1098711365 ptr_src: LazySrcLoc,
......@@ -10991,7 +11369,7 @@ fn storePtr2(
1099111369) !void {
1099211370 const ptr_ty = sema.typeOf(ptr);
1099311371 if (ptr_ty.isConstPtr())
10994 return sema.mod.fail(&block.base, src, "cannot assign to constant", .{});
11372 return sema.fail(block, src, "cannot assign to constant", .{});
1099511373
1099611374 const elem_ty = ptr_ty.elemType();
1099711375 const operand = try sema.coerce(block, elem_ty, uncasted_operand, operand_src);
......@@ -11000,7 +11378,7 @@ fn storePtr2(
1100011378
1100111379 const runtime_src = if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| rs: {
1100211380 const operand_val = (try sema.resolveMaybeUndefVal(block, operand_src, operand)) orelse
11003 return sema.mod.fail(&block.base, src, "cannot store runtime value in compile time variable", .{});
11381 return sema.fail(block, src, "cannot store runtime value in compile time variable", .{});
1100411382 if (ptr_val.tag() == .decl_ref_mut) {
1100511383 try sema.storePtrVal(block, src, ptr_val, operand_val, elem_ty);
1100611384 return;
......@@ -11018,7 +11396,7 @@ fn storePtr2(
1101811396/// assert the store must be done at comptime.
1101911397fn storePtrVal(
1102011398 sema: *Sema,
11021 block: *Scope.Block,
11399 block: *Block,
1102211400 src: LazySrcLoc,
1102311401 ptr_val: Value,
1102411402 operand_val: Value,
......@@ -11028,21 +11406,21 @@ fn storePtrVal(
1102811406 if (decl_ref_mut.data.runtime_index < block.runtime_index) {
1102911407 if (block.runtime_cond) |cond_src| {
1103011408 const msg = msg: {
11031 const msg = try sema.mod.errMsg(&block.base, src, "store to comptime variable depends on runtime condition", .{});
11409 const msg = try sema.errMsg(block, src, "store to comptime variable depends on runtime condition", .{});
1103211410 errdefer msg.destroy(sema.gpa);
11033 try sema.mod.errNote(&block.base, cond_src, msg, "runtime condition here", .{});
11411 try sema.errNote(block, cond_src, msg, "runtime condition here", .{});
1103411412 break :msg msg;
1103511413 };
11036 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);
11414 return sema.failWithOwnedErrorMsg(msg);
1103711415 }
1103811416 if (block.runtime_loop) |loop_src| {
1103911417 const msg = msg: {
11040 const msg = try sema.mod.errMsg(&block.base, src, "cannot store to comptime variable in non-inline loop", .{});
11418 const msg = try sema.errMsg(block, src, "cannot store to comptime variable in non-inline loop", .{});
1104111419 errdefer msg.destroy(sema.gpa);
11042 try sema.mod.errNote(&block.base, loop_src, msg, "non-inline loop here", .{});
11420 try sema.errNote(block, loop_src, msg, "non-inline loop here", .{});
1104311421 break :msg msg;
1104411422 };
11045 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);
11423 return sema.failWithOwnedErrorMsg(msg);
1104611424 }
1104711425 unreachable;
1104811426 }
......@@ -11063,7 +11441,7 @@ fn storePtrVal(
1106311441
1106411442fn bitcast(
1106511443 sema: *Sema,
11066 block: *Scope.Block,
11444 block: *Block,
1106711445 dest_type: Type,
1106811446 inst: Air.Inst.Ref,
1106911447 inst_src: LazySrcLoc,
......@@ -11079,7 +11457,7 @@ fn bitcast(
1107911457
1108011458fn coerceArrayPtrToSlice(
1108111459 sema: *Sema,
11082 block: *Scope.Block,
11460 block: *Block,
1108311461 dest_type: Type,
1108411462 inst: Air.Inst.Ref,
1108511463 inst_src: LazySrcLoc,
......@@ -11094,7 +11472,7 @@ fn coerceArrayPtrToSlice(
1109411472
1109511473fn coerceArrayPtrToMany(
1109611474 sema: *Sema,
11097 block: *Scope.Block,
11475 block: *Block,
1109811476 dest_type: Type,
1109911477 inst: Air.Inst.Ref,
1110011478 inst_src: LazySrcLoc,
......@@ -11109,7 +11487,7 @@ fn coerceArrayPtrToMany(
1110911487
1111011488fn analyzeDeclVal(
1111111489 sema: *Sema,
11112 block: *Scope.Block,
11490 block: *Block,
1111311491 src: LazySrcLoc,
1111411492 decl: *Decl,
1111511493) CompileError!Air.Inst.Ref {
......@@ -11163,7 +11541,7 @@ fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {
1116311541
1116411542fn analyzeRef(
1116511543 sema: *Sema,
11166 block: *Scope.Block,
11544 block: *Block,
1116711545 src: LazySrcLoc,
1116811546 operand: Air.Inst.Ref,
1116911547) CompileError!Air.Inst.Ref {
......@@ -11198,7 +11576,7 @@ fn analyzeRef(
1119811576
1119911577fn analyzeLoad(
1120011578 sema: *Sema,
11201 block: *Scope.Block,
11579 block: *Block,
1120211580 src: LazySrcLoc,
1120311581 ptr: Air.Inst.Ref,
1120411582 ptr_src: LazySrcLoc,
......@@ -11206,7 +11584,7 @@ fn analyzeLoad(
1120611584 const ptr_ty = sema.typeOf(ptr);
1120711585 const elem_ty = switch (ptr_ty.zigTypeTag()) {
1120811586 .Pointer => ptr_ty.elemType(),
11209 else => return sema.mod.fail(&block.base, ptr_src, "expected pointer, found '{}'", .{ptr_ty}),
11587 else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty}),
1121011588 };
1121111589 if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| {
1121211590 if (try ptr_val.pointerDeref(sema.arena)) |elem_val| {
......@@ -11220,7 +11598,7 @@ fn analyzeLoad(
1122011598
1122111599fn analyzeSliceLen(
1122211600 sema: *Sema,
11223 block: *Scope.Block,
11601 block: *Block,
1122411602 src: LazySrcLoc,
1122511603 slice_inst: Air.Inst.Ref,
1122611604) CompileError!Air.Inst.Ref {
......@@ -11228,7 +11606,7 @@ fn analyzeSliceLen(
1122811606 if (slice_val.isUndef()) {
1122911607 return sema.addConstUndef(Type.initTag(.usize));
1123011608 }
11231 return sema.mod.fail(&block.base, src, "TODO implement Sema analyzeSliceLen on comptime slice", .{});
11609 return sema.fail(block, src, "TODO implement Sema analyzeSliceLen on comptime slice", .{});
1123211610 }
1123311611 try sema.requireRuntimeBlock(block, src);
1123411612 return block.addTyOp(.slice_len, Type.initTag(.usize), slice_inst);
......@@ -11236,7 +11614,7 @@ fn analyzeSliceLen(
1123611614
1123711615fn analyzeIsNull(
1123811616 sema: *Sema,
11239 block: *Scope.Block,
11617 block: *Block,
1124011618 src: LazySrcLoc,
1124111619 operand: Air.Inst.Ref,
1124211620 invert_logic: bool,
......@@ -11261,7 +11639,7 @@ fn analyzeIsNull(
1126111639
1126211640fn analyzeIsNonErr(
1126311641 sema: *Sema,
11264 block: *Scope.Block,
11642 block: *Block,
1126511643 src: LazySrcLoc,
1126611644 operand: Air.Inst.Ref,
1126711645) CompileError!Air.Inst.Ref {
......@@ -11287,7 +11665,7 @@ fn analyzeIsNonErr(
1128711665
1128811666fn analyzeSlice(
1128911667 sema: *Sema,
11290 block: *Scope.Block,
11668 block: *Block,
1129111669 src: LazySrcLoc,
1129211670 array_ptr: Air.Inst.Ref,
1129311671 start: Air.Inst.Ref,
......@@ -11298,7 +11676,7 @@ fn analyzeSlice(
1129811676 const array_ptr_ty = sema.typeOf(array_ptr);
1129911677 const ptr_child = switch (array_ptr_ty.zigTypeTag()) {
1130011678 .Pointer => array_ptr_ty.elemType(),
11301 else => return sema.mod.fail(&block.base, src, "expected pointer, found '{}'", .{array_ptr_ty}),
11679 else => return sema.fail(block, src, "expected pointer, found '{}'", .{array_ptr_ty}),
1130211680 };
1130311681
1130411682 var array_type = ptr_child;
......@@ -11311,11 +11689,11 @@ fn analyzeSlice(
1131111689 break :blk ptr_child.elemType().elemType();
1131211690 }
1131311691
11314 return sema.mod.fail(&block.base, src, "slice of single-item pointer", .{});
11692 return sema.fail(block, src, "slice of single-item pointer", .{});
1131511693 }
1131611694 break :blk ptr_child.elemType();
1131711695 },
11318 else => return sema.mod.fail(&block.base, src, "slice of non-array type '{}'", .{ptr_child}),
11696 else => return sema.fail(block, src, "slice of non-array type '{}'", .{ptr_child}),
1131911697 };
1132011698
1132111699 const slice_sentinel = if (sentinel_opt != .none) blk: {
......@@ -11331,7 +11709,7 @@ fn analyzeSlice(
1133111709 const start_u64 = start_val.toUnsignedInt();
1133211710 const end_u64 = end_val.toUnsignedInt();
1133311711 if (start_u64 > end_u64) {
11334 return sema.mod.fail(&block.base, src, "out of bounds slice", .{});
11712 return sema.fail(block, src, "out of bounds slice", .{});
1133511713 }
1133611714
1133711715 const len = end_u64 - start_u64;
......@@ -11356,13 +11734,13 @@ fn analyzeSlice(
1135611734 });
1135711735 _ = return_type;
1135811736
11359 return sema.mod.fail(&block.base, src, "TODO implement analysis of slice", .{});
11737 return sema.fail(block, src, "TODO implement analysis of slice", .{});
1136011738}
1136111739
1136211740/// Asserts that lhs and rhs types are both numeric.
1136311741fn cmpNumeric(
1136411742 sema: *Sema,
11365 block: *Scope.Block,
11743 block: *Block,
1136611744 src: LazySrcLoc,
1136711745 lhs: Air.Inst.Ref,
1136811746 rhs: Air.Inst.Ref,
......@@ -11381,13 +11759,13 @@ fn cmpNumeric(
1138111759
1138211760 if (lhs_ty_tag == .Vector and rhs_ty_tag == .Vector) {
1138311761 if (lhs_ty.arrayLen() != rhs_ty.arrayLen()) {
11384 return sema.mod.fail(&block.base, src, "vector length mismatch: {d} and {d}", .{
11762 return sema.fail(block, src, "vector length mismatch: {d} and {d}", .{
1138511763 lhs_ty.arrayLen(), rhs_ty.arrayLen(),
1138611764 });
1138711765 }
11388 return sema.mod.fail(&block.base, src, "TODO implement support for vectors in cmpNumeric", .{});
11766 return sema.fail(block, src, "TODO implement support for vectors in cmpNumeric", .{});
1138911767 } else if (lhs_ty_tag == .Vector or rhs_ty_tag == .Vector) {
11390 return sema.mod.fail(&block.base, src, "mixed scalar and vector operands to comparison operator: '{}' and '{}'", .{
11768 return sema.fail(block, src, "mixed scalar and vector operands to comparison operator: '{}' and '{}'", .{
1139111769 lhs_ty, rhs_ty,
1139211770 });
1139311771 }
......@@ -11537,7 +11915,7 @@ fn cmpNumeric(
1153711915 const dest_type = if (dest_float_type) |ft| ft else blk: {
1153811916 const max_bits = std.math.max(lhs_bits, rhs_bits);
1153911917 const casted_bits = std.math.cast(u16, max_bits) catch |err| switch (err) {
11540 error.Overflow => return sema.mod.fail(&block.base, src, "{d} exceeds maximum integer bit count", .{max_bits}),
11918 error.Overflow => return sema.fail(block, src, "{d} exceeds maximum integer bit count", .{max_bits}),
1154111919 };
1154211920 const signedness: std.builtin.Signedness = if (dest_int_is_signed) .signed else .unsigned;
1154311921 break :blk try Module.makeIntType(sema.arena, signedness, casted_bits);
......@@ -11550,7 +11928,7 @@ fn cmpNumeric(
1155011928
1155111929fn wrapOptional(
1155211930 sema: *Sema,
11553 block: *Scope.Block,
11931 block: *Block,
1155411932 dest_type: Type,
1155511933 inst: Air.Inst.Ref,
1155611934 inst_src: LazySrcLoc,
......@@ -11565,7 +11943,7 @@ fn wrapOptional(
1156511943
1156611944fn wrapErrorUnion(
1156711945 sema: *Sema,
11568 block: *Scope.Block,
11946 block: *Block,
1156911947 dest_type: Type,
1157011948 inst: Air.Inst.Ref,
1157111949 inst_src: LazySrcLoc,
......@@ -11584,8 +11962,8 @@ fn wrapErrorUnion(
1158411962 const expected_name = val.castTag(.@"error").?.data.name;
1158511963 const n = dest_err_set_ty.castTag(.error_set_single).?.data;
1158611964 if (!mem.eql(u8, expected_name, n)) {
11587 return sema.mod.fail(
11588 &block.base,
11965 return sema.fail(
11966 block,
1158911967 inst_src,
1159011968 "expected type '{}', found type '{}'",
1159111969 .{ dest_err_set_ty, inst_ty },
......@@ -11602,8 +11980,8 @@ fn wrapErrorUnion(
1160211980 if (mem.eql(u8, expected_name, name)) break true;
1160311981 } else false;
1160411982 if (!found) {
11605 return sema.mod.fail(
11606 &block.base,
11983 return sema.fail(
11984 block,
1160711985 inst_src,
1160811986 "expected type '{}', found type '{}'",
1160911987 .{ dest_err_set_ty, inst_ty },
......@@ -11614,8 +11992,8 @@ fn wrapErrorUnion(
1161411992 const expected_name = val.castTag(.@"error").?.data.name;
1161511993 const map = &dest_err_set_ty.castTag(.error_set_inferred).?.data.map;
1161611994 if (!map.contains(expected_name)) {
11617 return sema.mod.fail(
11618 &block.base,
11995 return sema.fail(
11996 block,
1161911997 inst_src,
1162011998 "expected type '{}', found type '{}'",
1162111999 .{ dest_err_set_ty, inst_ty },
......@@ -11641,7 +12019,7 @@ fn wrapErrorUnion(
1164112019
1164212020fn unionToTag(
1164312021 sema: *Sema,
11644 block: *Scope.Block,
12022 block: *Block,
1164512023 dest_type: Type,
1164612024 un: Air.Inst.Ref,
1164712025 un_src: LazySrcLoc,
......@@ -11655,7 +12033,7 @@ fn unionToTag(
1165512033
1165612034fn resolvePeerTypes(
1165712035 sema: *Sema,
11658 block: *Scope.Block,
12036 block: *Block,
1165912037 src: LazySrcLoc,
1166012038 instructions: []Air.Inst.Ref,
1166112039 candidate_srcs: Module.PeerTypeCandidateSrc,
......@@ -11750,18 +12128,18 @@ fn resolvePeerTypes(
1175012128 );
1175112129
1175212130 const msg = msg: {
11753 const msg = try sema.mod.errMsg(&block.base, src, "incompatible types: '{}' and '{}'", .{ chosen_ty, candidate_ty });
12131 const msg = try sema.errMsg(block, src, "incompatible types: '{}' and '{}'", .{ chosen_ty, candidate_ty });
1175412132 errdefer msg.destroy(sema.gpa);
1175512133
1175612134 if (chosen_src) |src_loc|
11757 try sema.mod.errNote(&block.base, src_loc, msg, "type '{}' here", .{chosen_ty});
12135 try sema.errNote(block, src_loc, msg, "type '{}' here", .{chosen_ty});
1175812136
1175912137 if (candidate_src) |src_loc|
11760 try sema.mod.errNote(&block.base, src_loc, msg, "type '{}' here", .{candidate_ty});
12138 try sema.errNote(block, src_loc, msg, "type '{}' here", .{candidate_ty});
1176112139
1176212140 break :msg msg;
1176312141 };
11764 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);
12142 return sema.failWithOwnedErrorMsg(msg);
1176512143 }
1176612144
1176712145 return sema.typeOf(chosen);
......@@ -11769,7 +12147,7 @@ fn resolvePeerTypes(
1176912147
1177012148pub fn resolveTypeLayout(
1177112149 sema: *Sema,
11772 block: *Scope.Block,
12150 block: *Block,
1177312151 src: LazySrcLoc,
1177412152 ty: Type,
1177512153) CompileError!void {
......@@ -11780,7 +12158,7 @@ pub fn resolveTypeLayout(
1178012158 switch (struct_obj.status) {
1178112159 .none, .have_field_types => {},
1178212160 .field_types_wip, .layout_wip => {
11783 return sema.mod.fail(&block.base, src, "struct {} depends on itself", .{ty});
12161 return sema.fail(block, src, "struct {} depends on itself", .{ty});
1178412162 },
1178512163 .have_layout => return,
1178612164 }
......@@ -11794,78 +12172,23 @@ pub fn resolveTypeLayout(
1179412172 }
1179512173}
1179612174
11797pub fn resolvePendingTypes(sema: *Sema, block: *Scope.Block) !void {
11798 for (sema.types_pending_resolution.items) |ty| {
11799 // If an error happens resolving the fields of a struct, it will be marked
11800 // invalid and a proper compile error set up. But we should still look at the
11801 // other types pending resolution.
11802 const src: LazySrcLoc = .{ .node_offset = 0 };
11803 sema.resolveDeclFields(block, src, ty) catch continue;
11804 }
11805}
11806
11807/// `sema` and `block` are expected to be the same ones used for the `Decl`.
11808pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) !void {
12175fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!Type {
1180912176 switch (ty.tag()) {
1181012177 .@"struct" => {
1181112178 const struct_obj = ty.castTag(.@"struct").?.data;
11812 if (struct_obj.owner_decl.namespace != sema.owner_decl.namespace) return;
1181312179 switch (struct_obj.status) {
1181412180 .none => {},
1181512181 .field_types_wip => {
11816 return sema.mod.fail(&block.base, src, "struct {} depends on itself", .{ty});
12182 return sema.fail(block, src, "struct {} depends on itself", .{ty});
1181712183 },
11818 .have_field_types, .have_layout, .layout_wip => return,
12184 .have_field_types, .have_layout, .layout_wip => return ty,
1181912185 }
11820 const prev_namespace = sema.namespace;
11821 sema.namespace = &struct_obj.namespace;
11822 defer sema.namespace = prev_namespace;
11823
11824 const old_src = block.src_decl;
11825 defer block.src_decl = old_src;
11826 block.src_decl = struct_obj.owner_decl;
1182712186
1182812187 struct_obj.status = .field_types_wip;
11829 try sema.analyzeStructFields(block, struct_obj);
12188 try semaStructFields(sema.mod, struct_obj);
1183012189 struct_obj.status = .have_field_types;
11831 },
11832 .@"union", .union_tagged => {
11833 const union_obj = ty.cast(Type.Payload.Union).?.data;
11834 if (union_obj.owner_decl.namespace != sema.owner_decl.namespace) return;
11835 switch (union_obj.status) {
11836 .none => {},
11837 .field_types_wip => {
11838 return sema.mod.fail(&block.base, src, "union {} depends on itself", .{ty});
11839 },
11840 .have_field_types, .have_layout, .layout_wip => return,
11841 }
11842 const prev_namespace = sema.namespace;
11843 sema.namespace = &union_obj.namespace;
11844 defer sema.namespace = prev_namespace;
1184512190
11846 const old_src = block.src_decl;
11847 defer block.src_decl = old_src;
11848 block.src_decl = union_obj.owner_decl;
11849
11850 union_obj.status = .field_types_wip;
11851 try sema.analyzeUnionFields(block, union_obj);
11852 union_obj.status = .have_field_types;
11853 },
11854 else => return,
11855 }
11856}
11857
11858fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!Type {
11859 switch (ty.tag()) {
11860 .@"struct" => {
11861 const struct_obj = ty.castTag(.@"struct").?.data;
11862 switch (struct_obj.status) {
11863 .none => unreachable,
11864 .field_types_wip => {
11865 return sema.mod.fail(&block.base, src, "struct {} depends on itself", .{ty});
11866 },
11867 .have_field_types, .have_layout, .layout_wip => return ty,
11868 }
12191 return ty;
1186912192 },
1187012193 .type_info => return sema.resolveBuiltinTypeFields(block, src, "TypeInfo"),
1187112194 .extern_options => return sema.resolveBuiltinTypeFields(block, src, "ExternOptions"),
......@@ -11881,12 +12204,18 @@ fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type
1188112204 .@"union", .union_tagged => {
1188212205 const union_obj = ty.cast(Type.Payload.Union).?.data;
1188312206 switch (union_obj.status) {
11884 .none => unreachable,
12207 .none => {},
1188512208 .field_types_wip => {
11886 return sema.mod.fail(&block.base, src, "union {} depends on itself", .{ty});
12209 return sema.fail(block, src, "union {} depends on itself", .{ty});
1188712210 },
1188812211 .have_field_types, .have_layout, .layout_wip => return ty,
1188912212 }
12213
12214 union_obj.status = .field_types_wip;
12215 try semaUnionFields(sema.mod, union_obj);
12216 union_obj.status = .have_field_types;
12217
12218 return ty;
1189012219 },
1189112220 else => return ty,
1189212221 }
......@@ -11894,7 +12223,7 @@ fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type
1189412223
1189512224fn resolveBuiltinTypeFields(
1189612225 sema: *Sema,
11897 block: *Scope.Block,
12226 block: *Block,
1189812227 src: LazySrcLoc,
1189912228 name: []const u8,
1190012229) CompileError!Type {
......@@ -11902,16 +12231,16 @@ fn resolveBuiltinTypeFields(
1190212231 return sema.resolveTypeFields(block, src, resolved_ty);
1190312232}
1190412233
11905fn analyzeStructFields(
11906 sema: *Sema,
11907 block: *Scope.Block,
12234fn semaStructFields(
12235 mod: *Module,
1190812236 struct_obj: *Module.Struct,
1190912237) CompileError!void {
1191012238 const tracy = trace(@src());
1191112239 defer tracy.end();
1191212240
11913 const gpa = sema.gpa;
11914 const zir = sema.code;
12241 const gpa = mod.gpa;
12242 const decl = struct_obj.owner_decl;
12243 const zir = struct_obj.namespace.file_scope.zir;
1191512244 const extended = zir.instructions.items(.data)[struct_obj.zir_index].extended;
1191612245 assert(extended.opcode == .struct_decl);
1191712246 const small = @bitCast(Zir.Inst.StructDecl.Small, extended.small);
......@@ -11950,15 +12279,51 @@ fn analyzeStructFields(
1195012279 }
1195112280 extra_index += body.len;
1195212281
11953 var decl_arena = struct_obj.owner_decl.value_arena.?.promote(gpa);
11954 defer struct_obj.owner_decl.value_arena.?.* = decl_arena.state;
12282 var decl_arena = decl.value_arena.?.promote(gpa);
12283 defer decl.value_arena.?.* = decl_arena.state;
12284
12285 var analysis_arena = std.heap.ArenaAllocator.init(gpa);
12286 defer analysis_arena.deinit();
12287
12288 var sema: Sema = .{
12289 .mod = mod,
12290 .gpa = gpa,
12291 .arena = &analysis_arena.allocator,
12292 .perm_arena = &decl_arena.allocator,
12293 .code = zir,
12294 .owner_decl = decl,
12295 .func = null,
12296 .fn_ret_ty = Type.initTag(.void),
12297 .owner_func = null,
12298 };
12299 defer sema.deinit();
1195512300
11956 try struct_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
12301 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);
12302 defer wip_captures.deinit();
12303
12304 var block_scope: Block = .{
12305 .parent = null,
12306 .sema = &sema,
12307 .src_decl = decl,
12308 .namespace = &struct_obj.namespace,
12309 .wip_capture_scope = wip_captures.scope,
12310 .instructions = .{},
12311 .inlining = null,
12312 .is_comptime = true,
12313 };
12314 defer {
12315 assert(block_scope.instructions.items.len == 0);
12316 block_scope.params.deinit(gpa);
12317 }
1195712318
1195812319 if (body.len != 0) {
11959 _ = try sema.analyzeBody(block, body);
12320 _ = try sema.analyzeBody(&block_scope, body);
1196012321 }
1196112322
12323 try wip_captures.finalize();
12324
12325 try struct_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
12326
1196212327 const bits_per_field = 4;
1196312328 const fields_per_u32 = 32 / bits_per_field;
1196412329 const bit_bags_count = std.math.divCeil(usize, fields_len, fields_per_u32) catch unreachable;
......@@ -11995,7 +12360,7 @@ fn analyzeStructFields(
1199512360 // TODO: if we need to report an error here, use a source location
1199612361 // that points to this type expression rather than the struct.
1199712362 // But only resolve the source location if we need to emit a compile error.
11998 try sema.resolveType(block, src, field_type_ref);
12363 try sema.resolveType(&block_scope, src, field_type_ref);
1199912364
1200012365 const gop = struct_obj.fields.getOrPutAssumeCapacity(field_name);
1200112366 assert(!gop.found_existing);
......@@ -12013,7 +12378,7 @@ fn analyzeStructFields(
1201312378 // TODO: if we need to report an error here, use a source location
1201412379 // that points to this alignment expression rather than the struct.
1201512380 // But only resolve the source location if we need to emit a compile error.
12016 const abi_align_val = (try sema.resolveInstConst(block, src, align_ref)).val;
12381 const abi_align_val = (try sema.resolveInstConst(&block_scope, src, align_ref)).val;
1201712382 gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator);
1201812383 }
1201912384 if (has_default) {
......@@ -12023,23 +12388,23 @@ fn analyzeStructFields(
1202312388 // TODO: if we need to report an error here, use a source location
1202412389 // that points to this default value expression rather than the struct.
1202512390 // But only resolve the source location if we need to emit a compile error.
12026 const default_val = (try sema.resolveMaybeUndefVal(block, src, default_inst)) orelse
12027 return sema.failWithNeededComptime(block, src);
12391 const default_val = (try sema.resolveMaybeUndefVal(&block_scope, src, default_inst)) orelse
12392 return sema.failWithNeededComptime(&block_scope, src);
1202812393 gop.value_ptr.default_val = try default_val.copy(&decl_arena.allocator);
1202912394 }
1203012395 }
1203112396}
1203212397
12033fn analyzeUnionFields(
12034 sema: *Sema,
12035 block: *Scope.Block,
12398fn semaUnionFields(
12399 mod: *Module,
1203612400 union_obj: *Module.Union,
1203712401) CompileError!void {
1203812402 const tracy = trace(@src());
1203912403 defer tracy.end();
1204012404
12041 const gpa = sema.gpa;
12042 const zir = sema.code;
12405 const gpa = mod.gpa;
12406 const decl = union_obj.owner_decl;
12407 const zir = union_obj.namespace.file_scope.zir;
1204312408 const extended = zir.instructions.items(.data)[union_obj.zir_index].extended;
1204412409 assert(extended.opcode == .union_decl);
1204512410 const small = @bitCast(Zir.Inst.UnionDecl.Small, extended.small);
......@@ -12087,20 +12452,57 @@ fn analyzeUnionFields(
1208712452 var decl_arena = union_obj.owner_decl.value_arena.?.promote(gpa);
1208812453 defer union_obj.owner_decl.value_arena.?.* = decl_arena.state;
1208912454
12090 try union_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
12455 var analysis_arena = std.heap.ArenaAllocator.init(gpa);
12456 defer analysis_arena.deinit();
12457
12458 var sema: Sema = .{
12459 .mod = mod,
12460 .gpa = gpa,
12461 .arena = &analysis_arena.allocator,
12462 .perm_arena = &decl_arena.allocator,
12463 .code = zir,
12464 .owner_decl = decl,
12465 .func = null,
12466 .fn_ret_ty = Type.initTag(.void),
12467 .owner_func = null,
12468 };
12469 defer sema.deinit();
12470
12471 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);
12472 defer wip_captures.deinit();
12473
12474 var block_scope: Block = .{
12475 .parent = null,
12476 .sema = &sema,
12477 .src_decl = decl,
12478 .namespace = &union_obj.namespace,
12479 .wip_capture_scope = wip_captures.scope,
12480 .instructions = .{},
12481 .inlining = null,
12482 .is_comptime = true,
12483 };
12484 defer {
12485 assert(block_scope.instructions.items.len == 0);
12486 block_scope.params.deinit(gpa);
12487 }
1209112488
1209212489 if (body.len != 0) {
12093 _ = try sema.analyzeBody(block, body);
12490 _ = try sema.analyzeBody(&block_scope, body);
1209412491 }
12492
12493 try wip_captures.finalize();
12494
12495 try union_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
12496
1209512497 var int_tag_ty: Type = undefined;
1209612498 var enum_field_names: ?*Module.EnumNumbered.NameMap = null;
1209712499 var enum_value_map: ?*Module.EnumNumbered.ValueMap = null;
1209812500 if (tag_type_ref != .none) {
12099 const provided_ty = try sema.resolveType(block, src, tag_type_ref);
12501 const provided_ty = try sema.resolveType(&block_scope, src, tag_type_ref);
1210012502 if (small.auto_enum_tag) {
1210112503 // The provided type is an integer type and we must construct the enum tag type here.
1210212504 int_tag_ty = provided_ty;
12103 union_obj.tag_ty = try sema.generateUnionTagTypeNumbered(block, fields_len, provided_ty);
12505 union_obj.tag_ty = try sema.generateUnionTagTypeNumbered(&block_scope, fields_len, provided_ty);
1210412506 enum_field_names = &union_obj.tag_ty.castTag(.enum_numbered).?.data.fields;
1210512507 enum_value_map = &union_obj.tag_ty.castTag(.enum_numbered).?.data.values;
1210612508 } else {
......@@ -12111,7 +12513,7 @@ fn analyzeUnionFields(
1211112513 // If auto_enum_tag is false, this is an untagged union. However, for semantic analysis
1211212514 // purposes, we still auto-generate an enum tag type the same way. That the union is
1211312515 // untagged is represented by the Type tag (union vs union_tagged).
12114 union_obj.tag_ty = try sema.generateUnionTagTypeSimple(block, fields_len);
12516 union_obj.tag_ty = try sema.generateUnionTagTypeSimple(&block_scope, fields_len);
1211512517 enum_field_names = &union_obj.tag_ty.castTag(.enum_simple).?.data.fields;
1211612518 }
1211712519
......@@ -12160,8 +12562,8 @@ fn analyzeUnionFields(
1216012562
1216112563 if (enum_value_map) |map| {
1216212564 const tag_src = src; // TODO better source location
12163 const coerced = try sema.coerce(block, int_tag_ty, tag_ref, tag_src);
12164 const val = try sema.resolveConstValue(block, tag_src, coerced);
12565 const coerced = try sema.coerce(&block_scope, int_tag_ty, tag_ref, tag_src);
12566 const val = try sema.resolveConstValue(&block_scope, tag_src, coerced);
1216512567 map.putAssumeCapacityContext(val, {}, .{ .ty = int_tag_ty });
1216612568 }
1216712569
......@@ -12177,7 +12579,7 @@ fn analyzeUnionFields(
1217712579 // TODO: if we need to report an error here, use a source location
1217812580 // that points to this type expression rather than the union.
1217912581 // But only resolve the source location if we need to emit a compile error.
12180 try sema.resolveType(block, src, field_type_ref);
12582 try sema.resolveType(&block_scope, src, field_type_ref);
1218112583
1218212584 const gop = union_obj.fields.getOrPutAssumeCapacity(field_name);
1218312585 assert(!gop.found_existing);
......@@ -12190,7 +12592,7 @@ fn analyzeUnionFields(
1219012592 // TODO: if we need to report an error here, use a source location
1219112593 // that points to this alignment expression rather than the struct.
1219212594 // But only resolve the source location if we need to emit a compile error.
12193 const abi_align_val = (try sema.resolveInstConst(block, src, align_ref)).val;
12595 const abi_align_val = (try sema.resolveInstConst(&block_scope, src, align_ref)).val;
1219412596 gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator);
1219512597 } else {
1219612598 gop.value_ptr.abi_align = Value.initTag(.abi_align_default);
......@@ -12200,7 +12602,7 @@ fn analyzeUnionFields(
1220012602
1220112603fn generateUnionTagTypeNumbered(
1220212604 sema: *Sema,
12203 block: *Scope.Block,
12605 block: *Block,
1220412606 fields_len: u32,
1220512607 int_ty: Type,
1220612608) !Type {
......@@ -12218,12 +12620,12 @@ fn generateUnionTagTypeNumbered(
1221812620 const enum_ty = Type.initPayload(&enum_ty_payload.base);
1221912621 const enum_val = try Value.Tag.ty.create(&new_decl_arena.allocator, enum_ty);
1222012622 // TODO better type name
12221 const new_decl = try mod.createAnonymousDecl(&block.base, .{
12623 const new_decl = try mod.createAnonymousDecl(block, .{
1222212624 .ty = Type.initTag(.type),
1222312625 .val = enum_val,
1222412626 });
1222512627 new_decl.owns_tv = true;
12226 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
12628 errdefer mod.abortAnonDecl(new_decl);
1222712629
1222812630 enum_obj.* = .{
1222912631 .owner_decl = new_decl,
......@@ -12239,7 +12641,7 @@ fn generateUnionTagTypeNumbered(
1223912641 return enum_ty;
1224012642}
1224112643
12242fn generateUnionTagTypeSimple(sema: *Sema, block: *Scope.Block, fields_len: u32) !Type {
12644fn generateUnionTagTypeSimple(sema: *Sema, block: *Block, fields_len: u32) !Type {
1224312645 const mod = sema.mod;
1224412646
1224512647 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);
......@@ -12254,12 +12656,12 @@ fn generateUnionTagTypeSimple(sema: *Sema, block: *Scope.Block, fields_len: u32)
1225412656 const enum_ty = Type.initPayload(&enum_ty_payload.base);
1225512657 const enum_val = try Value.Tag.ty.create(&new_decl_arena.allocator, enum_ty);
1225612658 // TODO better type name
12257 const new_decl = try mod.createAnonymousDecl(&block.base, .{
12659 const new_decl = try mod.createAnonymousDecl(block, .{
1225812660 .ty = Type.initTag(.type),
1225912661 .val = enum_val,
1226012662 });
1226112663 new_decl.owns_tv = true;
12262 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
12664 errdefer mod.abortAnonDecl(new_decl);
1226312665
1226412666 enum_obj.* = .{
1226512667 .owner_decl = new_decl,
......@@ -12274,7 +12676,7 @@ fn generateUnionTagTypeSimple(sema: *Sema, block: *Scope.Block, fields_len: u32)
1227412676
1227512677fn getBuiltin(
1227612678 sema: *Sema,
12277 block: *Scope.Block,
12679 block: *Block,
1227812680 src: LazySrcLoc,
1227912681 name: []const u8,
1228012682) CompileError!Air.Inst.Ref {
......@@ -12284,7 +12686,7 @@ fn getBuiltin(
1228412686 const opt_builtin_inst = try sema.namespaceLookupRef(
1228512687 block,
1228612688 src,
12287 std_file.root_decl.?.namespace,
12689 std_file.root_decl.?.src_namespace,
1228812690 "builtin",
1228912691 );
1229012692 const builtin_inst = try sema.analyzeLoad(block, src, opt_builtin_inst.?, src);
......@@ -12300,7 +12702,7 @@ fn getBuiltin(
1230012702
1230112703fn getBuiltinType(
1230212704 sema: *Sema,
12303 block: *Scope.Block,
12705 block: *Block,
1230412706 src: LazySrcLoc,
1230512707 name: []const u8,
1230612708) CompileError!Type {
......@@ -12314,7 +12716,7 @@ fn getBuiltinType(
1231412716/// that the types are already resolved.
1231512717fn typeHasOnePossibleValue(
1231612718 sema: *Sema,
12317 block: *Scope.Block,
12719 block: *Block,
1231812720 src: LazySrcLoc,
1231912721 starting_type: Type,
1232012722) CompileError!?Value {
......@@ -12477,8 +12879,8 @@ fn typeHasOnePossibleValue(
1247712879 };
1247812880}
1247912881
12480fn getAstTree(sema: *Sema, block: *Scope.Block) CompileError!*const std.zig.Ast {
12481 return block.src_decl.namespace.file_scope.getTree(sema.gpa) catch |err| {
12882fn getAstTree(sema: *Sema, block: *Block) CompileError!*const std.zig.Ast {
12883 return block.namespace.file_scope.getTree(sema.gpa) catch |err| {
1248212884 log.err("unable to load AST to report compile error: {s}", .{@errorName(err)});
1248312885 return error.AnalysisFail;
1248412886 };
......@@ -12667,7 +13069,7 @@ fn getBreakBlock(sema: *Sema, inst_index: Air.Inst.Index) ?Air.Inst.Index {
1266713069
1266813070fn isComptimeKnown(
1266913071 sema: *Sema,
12670 block: *Scope.Block,
13072 block: *Block,
1267113073 src: LazySrcLoc,
1267213074 inst: Air.Inst.Ref,
1267313075) !bool {
......@@ -12676,7 +13078,7 @@ fn isComptimeKnown(
1267613078
1267713079fn analyzeComptimeAlloc(
1267813080 sema: *Sema,
12679 block: *Scope.Block,
13081 block: *Block,
1268013082 var_type: Type,
1268113083) CompileError!Air.Inst.Ref {
1268213084 const ptr_type = try Type.ptr(sema.arena, .{
......@@ -12719,7 +13121,7 @@ pub const AddressSpaceContext = enum {
1271913121
1272013122pub fn analyzeAddrspace(
1272113123 sema: *Sema,
12722 block: *Scope.Block,
13124 block: *Block,
1272313125 src: LazySrcLoc,
1272413126 zir_ref: Zir.Inst.Ref,
1272513127 ctx: AddressSpaceContext,
......@@ -12743,8 +13145,8 @@ pub fn analyzeAddrspace(
1274313145 .pointer => "pointers",
1274413146 };
1274513147
12746 return sema.mod.fail(
12747 &block.base,
13148 return sema.fail(
13149 block,
1274813150 src,
1274913151 "{s} with address space '{s}' are not supported on {s}",
1275013152 .{ entity, @tagName(address_space), arch.genericName() },
src/codegen.zig+2-2
......@@ -2584,7 +2584,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
25842584
25852585 fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue) !void {
25862586 const ty_str = self.air.instructions.items(.data)[inst].ty_str;
2587 const zir = &self.mod_fn.owner_decl.namespace.file_scope.zir;
2587 const zir = &self.mod_fn.owner_decl.getFileScope().zir;
25882588 const name = zir.nullTerminatedString(ty_str.str);
25892589 const name_with_null = name.ptr[0 .. name.len + 1];
25902590 const ty = self.air.getRefType(ty_str.ty);
......@@ -3834,7 +3834,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
38343834 fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
38353835 const air_datas = self.air.instructions.items(.data);
38363836 const air_extra = self.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload);
3837 const zir = self.mod_fn.owner_decl.namespace.file_scope.zir;
3837 const zir = self.mod_fn.owner_decl.getFileScope().zir;
38383838 const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;
38393839 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
38403840 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);
src/codegen/c.zig+2-2
......@@ -246,7 +246,7 @@ pub const DeclGen = struct {
246246 fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } {
247247 @setCold(true);
248248 const src: LazySrcLoc = .{ .node_offset = 0 };
249 const src_loc = src.toSrcLocWithDecl(dg.decl);
249 const src_loc = src.toSrcLoc(dg.decl);
250250 dg.error_msg = try Module.ErrorMsg.create(dg.module.gpa, src_loc, format, args);
251251 return error.AnalysisFail;
252252 }
......@@ -1696,7 +1696,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
16961696fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
16971697 const air_datas = f.air.instructions.items(.data);
16981698 const air_extra = f.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload);
1699 const zir = f.object.dg.decl.namespace.file_scope.zir;
1699 const zir = f.object.dg.decl.getFileScope().zir;
17001700 const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;
17011701 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
17021702 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);
src/codegen/llvm.zig+2-2
......@@ -557,7 +557,7 @@ pub const DeclGen = struct {
557557 fn todo(self: *DeclGen, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {
558558 @setCold(true);
559559 assert(self.err_msg == null);
560 const src_loc = @as(LazySrcLoc, .{ .node_offset = 0 }).toSrcLocWithDecl(self.decl);
560 const src_loc = @as(LazySrcLoc, .{ .node_offset = 0 }).toSrcLoc(self.decl);
561561 self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, "TODO (LLVM): " ++ format, args);
562562 return error.CodegenFail;
563563 }
......@@ -1819,7 +1819,7 @@ pub const FuncGen = struct {
18191819
18201820 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
18211821 const air_asm = self.air.extraData(Air.Asm, ty_pl.payload);
1822 const zir = self.dg.decl.namespace.file_scope.zir;
1822 const zir = self.dg.decl.getFileScope().zir;
18231823 const extended = zir.instructions.items(.data)[air_asm.data.zir_index].extended;
18241824 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
18251825 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);
src/codegen/spirv.zig+2-2
......@@ -139,7 +139,7 @@ pub const SPIRVModule = struct {
139139 }
140140
141141 fn resolveSourceFileName(self: *SPIRVModule, decl: *Decl) !ResultId {
142 const path = decl.namespace.file_scope.sub_file_path;
142 const path = decl.getFileScope().sub_file_path;
143143 const result = try self.file_names.getOrPut(path);
144144 if (!result.found_existing) {
145145 result.value_ptr.* = self.allocResultId();
......@@ -295,7 +295,7 @@ pub const DeclGen = struct {
295295 fn fail(self: *DeclGen, comptime format: []const u8, args: anytype) Error {
296296 @setCold(true);
297297 const src: LazySrcLoc = .{ .node_offset = 0 };
298 const src_loc = src.toSrcLocWithDecl(self.decl);
298 const src_loc = src.toSrcLoc(self.decl);
299299 self.error_msg = try Module.ErrorMsg.create(self.spv.module.gpa, src_loc, format, args);
300300 return error.AnalysisFail;
301301 }
src/codegen/wasm.zig+1-1
......@@ -540,7 +540,7 @@ pub const Context = struct {
540540 /// Sets `err_msg` on `Context` and returns `error.CodegemFail` which is caught in link/Wasm.zig
541541 fn fail(self: *Context, comptime fmt: []const u8, args: anytype) InnerError {
542542 const src: LazySrcLoc = .{ .node_offset = 0 };
543 const src_loc = src.toSrcLocWithDecl(self.decl);
543 const src_loc = src.toSrcLoc(self.decl);
544544 self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, fmt, args);
545545 return error.CodegenFail;
546546 }
src/crash_report.zig+9-11
......@@ -8,6 +8,7 @@ const print_zir = @import("print_zir.zig");
88const Module = @import("Module.zig");
99const Sema = @import("Sema.zig");
1010const Zir = @import("Zir.zig");
11const Decl = Module.Decl;
1112
1213pub const is_enabled = builtin.mode == .Debug;
1314
......@@ -36,7 +37,7 @@ fn en(val: anytype) En(@TypeOf(val)) {
3637pub const AnalyzeBody = struct {
3738 parent: if (is_enabled) ?*AnalyzeBody else void,
3839 sema: En(*Sema),
39 block: En(*Module.Scope.Block),
40 block: En(*Sema.Block),
4041 body: En([]const Zir.Inst.Index),
4142 body_index: En(usize),
4243
......@@ -64,7 +65,7 @@ pub const AnalyzeBody = struct {
6465
6566threadlocal var zir_state: ?*AnalyzeBody = if (is_enabled) null else @compileError("Cannot use zir_state if crash_report is disabled.");
6667
67pub fn prepAnalyzeBody(sema: *Sema, block: *Module.Scope.Block, body: []const Zir.Inst.Index) AnalyzeBody {
68pub fn prepAnalyzeBody(sema: *Sema, block: *Sema.Block, body: []const Zir.Inst.Index) AnalyzeBody {
6869 if (is_enabled) {
6970 return .{
7071 .parent = null,
......@@ -87,7 +88,7 @@ fn dumpStatusReport() !void {
8788 const allocator = &fba.allocator;
8889
8990 const stderr = io.getStdErr().writer();
90 const block: *Scope.Block = anal.block;
91 const block: *Sema.Block = anal.block;
9192
9293 try stderr.writeAll("Analyzing ");
9394 try writeFullyQualifiedDeclWithFile(block.src_decl, stderr);
......@@ -97,7 +98,7 @@ fn dumpStatusReport() !void {
9798 allocator,
9899 anal.body,
99100 anal.body_index,
100 block.src_decl.getFileScope(),
101 block.namespace.file_scope,
101102 block.src_decl.src_node,
102103 6, // indent
103104 stderr,
......@@ -106,7 +107,7 @@ fn dumpStatusReport() !void {
106107 else => |e| return e,
107108 };
108109 try stderr.writeAll(" For full context, use the command\n zig ast-check -t ");
109 try writeFilePath(block.src_decl.getFileScope(), stderr);
110 try writeFilePath(block.namespace.file_scope, stderr);
110111 try stderr.writeAll("\n\n");
111112
112113 var parent = anal.parent;
......@@ -118,7 +119,7 @@ fn dumpStatusReport() !void {
118119 print_zir.renderSingleInstruction(
119120 allocator,
120121 curr.body[curr.body_index],
121 curr.block.src_decl.getFileScope(),
122 curr.block.namespace.file_scope,
122123 curr.block.src_decl.src_node,
123124 6, // indent
124125 stderr,
......@@ -134,12 +135,9 @@ fn dumpStatusReport() !void {
134135 try stderr.writeAll("\n");
135136}
136137
137const Scope = Module.Scope;
138const Decl = Module.Decl;
139
140138var crash_heap: [16 * 4096]u8 = undefined;
141139
142fn writeFilePath(file: *Scope.File, stream: anytype) !void {
140fn writeFilePath(file: *Module.File, stream: anytype) !void {
143141 if (file.pkg.root_src_directory.path) |path| {
144142 try stream.writeAll(path);
145143 try stream.writeAll(std.fs.path.sep_str);
......@@ -150,7 +148,7 @@ fn writeFilePath(file: *Scope.File, stream: anytype) !void {
150148fn writeFullyQualifiedDeclWithFile(decl: *Decl, stream: anytype) !void {
151149 try writeFilePath(decl.getFileScope(), stream);
152150 try stream.writeAll(": ");
153 try decl.namespace.renderFullyQualifiedName(std.mem.sliceTo(decl.name, 0), stream);
151 try decl.renderFullyQualifiedDebugName(stream);
154152}
155153
156154fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn {
src/link/Plan9.zig+4-4
......@@ -57,7 +57,7 @@ path_arena: std.heap.ArenaAllocator,
5757/// of the function to know what file it came from.
5858/// If we group the decls by file, it makes it really easy to do this (put the symbol in the correct place)
5959fn_decl_table: std.AutoArrayHashMapUnmanaged(
60 *Module.Scope.File,
60 *Module.File,
6161 struct { sym_index: u32, functions: std.AutoArrayHashMapUnmanaged(*Module.Decl, FnDeclOutput) = .{} },
6262) = .{},
6363data_decl_table: std.AutoArrayHashMapUnmanaged(*Module.Decl, []const u8) = .{},
......@@ -163,11 +163,11 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Plan9 {
163163
164164fn putFn(self: *Plan9, decl: *Module.Decl, out: FnDeclOutput) !void {
165165 const gpa = self.base.allocator;
166 const fn_map_res = try self.fn_decl_table.getOrPut(gpa, decl.namespace.file_scope);
166 const fn_map_res = try self.fn_decl_table.getOrPut(gpa, decl.getFileScope());
167167 if (fn_map_res.found_existing) {
168168 try fn_map_res.value_ptr.functions.put(gpa, decl, out);
169169 } else {
170 const file = decl.namespace.file_scope;
170 const file = decl.getFileScope();
171171 const arena = &self.path_arena.allocator;
172172 // each file gets a symbol
173173 fn_map_res.value_ptr.* = .{
......@@ -548,7 +548,7 @@ pub fn freeDecl(self: *Plan9, decl: *Module.Decl) void {
548548 const is_fn = (decl.val.tag() == .function);
549549 if (is_fn) {
550550 var symidx_and_submap =
551 self.fn_decl_table.get(decl.namespace.file_scope).?;
551 self.fn_decl_table.get(decl.getFileScope()).?;
552552 var submap = symidx_and_submap.functions;
553553 _ = submap.swapRemove(decl);
554554 if (submap.count() == 0) {
src/main.zig+4-4
......@@ -3233,7 +3233,7 @@ pub fn cmdFmt(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !voi
32333233 const Module = @import("Module.zig");
32343234 const AstGen = @import("AstGen.zig");
32353235
3236 var file: Module.Scope.File = .{
3236 var file: Module.File = .{
32373237 .status = .never_loaded,
32383238 .source_loaded = true,
32393239 .zir_loaded = false,
......@@ -3429,7 +3429,7 @@ fn fmtPathFile(
34293429 const Module = @import("Module.zig");
34303430 const AstGen = @import("AstGen.zig");
34313431
3432 var file: Module.Scope.File = .{
3432 var file: Module.File = .{
34333433 .status = .never_loaded,
34343434 .source_loaded = true,
34353435 .zir_loaded = false,
......@@ -4019,7 +4019,7 @@ pub fn cmdAstCheck(
40194019 }
40204020 }
40214021
4022 var file: Module.Scope.File = .{
4022 var file: Module.File = .{
40234023 .status = .never_loaded,
40244024 .source_loaded = false,
40254025 .tree_loaded = false,
......@@ -4170,7 +4170,7 @@ pub fn cmdChangelist(
41704170 if (stat.size > max_src_size)
41714171 return error.FileTooBig;
41724172
4173 var file: Module.Scope.File = .{
4173 var file: Module.File = .{
41744174 .status = .never_loaded,
41754175 .source_loaded = false,
41764176 .tree_loaded = false,
src/print_zir.zig+4-4
......@@ -11,7 +11,7 @@ const LazySrcLoc = Module.LazySrcLoc;
1111/// Write human-readable, debug formatted ZIR code to a file.
1212pub fn renderAsTextToFile(
1313 gpa: *Allocator,
14 scope_file: *Module.Scope.File,
14 scope_file: *Module.File,
1515 fs_file: std.fs.File,
1616) !void {
1717 var arena = std.heap.ArenaAllocator.init(gpa);
......@@ -64,7 +64,7 @@ pub fn renderInstructionContext(
6464 gpa: *Allocator,
6565 block: []const Zir.Inst.Index,
6666 block_index: usize,
67 scope_file: *Module.Scope.File,
67 scope_file: *Module.File,
6868 parent_decl_node: Ast.Node.Index,
6969 indent: u32,
7070 stream: anytype,
......@@ -96,7 +96,7 @@ pub fn renderInstructionContext(
9696pub fn renderSingleInstruction(
9797 gpa: *Allocator,
9898 inst: Zir.Inst.Index,
99 scope_file: *Module.Scope.File,
99 scope_file: *Module.File,
100100 parent_decl_node: Ast.Node.Index,
101101 indent: u32,
102102 stream: anytype,
......@@ -122,7 +122,7 @@ pub fn renderSingleInstruction(
122122const Writer = struct {
123123 gpa: *Allocator,
124124 arena: *Allocator,
125 file: *Module.Scope.File,
125 file: *Module.File,
126126 code: Zir,
127127 indent: u32,
128128 parent_decl_node: Ast.Node.Index,
src/type.zig+3-3
......@@ -3067,7 +3067,7 @@ pub const Type = extern union {
30673067 }
30683068
30693069 /// Returns null if the type has no namespace.
3070 pub fn getNamespace(self: Type) ?*Module.Scope.Namespace {
3070 pub fn getNamespace(self: Type) ?*Module.Namespace {
30713071 return switch (self.tag()) {
30723072 .@"struct" => &self.castTag(.@"struct").?.data.namespace,
30733073 .enum_full => &self.castTag(.enum_full).?.data.namespace,
......@@ -3833,12 +3833,12 @@ pub const Type = extern union {
38333833 /// Most commonly used for files.
38343834 pub const ContainerScope = struct {
38353835 base: Payload,
3836 data: *Module.Scope.Namespace,
3836 data: *Module.Namespace,
38373837 };
38383838
38393839 pub const Opaque = struct {
38403840 base: Payload = .{ .tag = .@"opaque" },
3841 data: Module.Scope.Namespace,
3841 data: Module.Namespace,
38423842 };
38433843
38443844 pub const Struct = struct {
src/value.zig+50-2
......@@ -1238,9 +1238,24 @@ pub const Value = extern union {
12381238 const b_field_index = b.castTag(.enum_field_index).?.data;
12391239 return a_field_index == b_field_index;
12401240 },
1241 .elem_ptr => @panic("TODO: Implement more pointer eql cases"),
1242 .field_ptr => @panic("TODO: Implement more pointer eql cases"),
1243 .eu_payload_ptr => @panic("TODO: Implement more pointer eql cases"),
1244 .opt_payload_ptr => @panic("TODO: Implement more pointer eql cases"),
12411245 else => {},
12421246 }
12431247 }
1248
1249 if (a.pointerDecl()) |a_decl| {
1250 if (b.pointerDecl()) |b_decl| {
1251 return a_decl == b_decl;
1252 } else {
1253 return false;
1254 }
1255 } else if (b.pointerDecl()) |_| {
1256 return false;
1257 }
1258
12441259 if (ty.zigTypeTag() == .Type) {
12451260 var buf_a: ToTypeBuffer = undefined;
12461261 var buf_b: ToTypeBuffer = undefined;
......@@ -1285,8 +1300,28 @@ pub const Value = extern union {
12851300 const float = val.toFloat(f128);
12861301 std.hash.autoHash(hasher, @bitCast(u128, float));
12871302 },
1288 .Pointer => {
1289 @panic("TODO implement hashing pointer values");
1303 .Pointer => switch (val.tag()) {
1304 .decl_ref_mut,
1305 .extern_fn,
1306 .decl_ref,
1307 .function,
1308 .variable,
1309 => std.hash.autoHash(hasher, val.pointerDecl().?),
1310
1311 .elem_ptr => @panic("TODO: Implement more pointer hashing cases"),
1312 .field_ptr => @panic("TODO: Implement more pointer hashing cases"),
1313 .eu_payload_ptr => @panic("TODO: Implement more pointer hashing cases"),
1314 .opt_payload_ptr => @panic("TODO: Implement more pointer hashing cases"),
1315
1316 .zero,
1317 .one,
1318 .int_u64,
1319 .int_i64,
1320 .int_big_positive,
1321 .int_big_negative,
1322 => @panic("TODO: Implement pointer hashing for int pointers"),
1323
1324 else => unreachable,
12901325 },
12911326 .Array, .Vector => {
12921327 @panic("TODO implement hashing array/vector values");
......@@ -1432,6 +1467,19 @@ pub const Value = extern union {
14321467 return sub_val;
14331468 }
14341469
1470 /// Gets the decl referenced by this pointer. If the pointer does not point
1471 /// to a decl, or if it points to some part of a decl (like field_ptr or element_ptr),
1472 /// this function returns null.
1473 pub fn pointerDecl(self: Value) ?*Module.Decl {
1474 return switch (self.tag()) {
1475 .decl_ref_mut => self.castTag(.decl_ref_mut).?.data.decl,
1476 .extern_fn, .decl_ref => self.cast(Payload.Decl).?.data,
1477 .function => self.castTag(.function).?.data.owner_decl,
1478 .variable => self.castTag(.variable).?.data.owner_decl,
1479 else => null,
1480 };
1481 }
1482
14351483 pub fn sliceLen(val: Value) u64 {
14361484 return switch (val.tag()) {
14371485 .empty_array => 0,