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),...@@ -54,7 +54,7 @@ c_object_work_queue: std.fifo.LinearFifo(*CObject, .Dynamic),
54/// These jobs are to tokenize, parse, and astgen files, which may be outdated54/// These jobs are to tokenize, parse, and astgen files, which may be outdated
55/// since the last compilation, as well as scan for `@import` and queue up55/// since the last compilation, as well as scan for `@import` and queue up
56/// additional jobs corresponding to those new files.56/// 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
59/// The ErrorMsg memory is owned by the `CObject`, using Compilation's general purpose allocator.59/// The ErrorMsg memory is owned by the `CObject`, using Compilation's general purpose allocator.
60/// This data is accessed by multiple threads and is protected by `mutex`.60/// This data is accessed by multiple threads and is protected by `mutex`.
...@@ -446,7 +446,7 @@ pub const AllErrors = struct {...@@ -446,7 +446,7 @@ pub const AllErrors = struct {
446 pub fn addZir(446 pub fn addZir(
447 arena: *Allocator,447 arena: *Allocator,
448 errors: *std.ArrayList(Message),448 errors: *std.ArrayList(Message),
449 file: *Module.Scope.File,449 file: *Module.File,
450 ) !void {450 ) !void {
451 assert(file.zir_loaded);451 assert(file.zir_loaded);
452 assert(file.tree_loaded);452 assert(file.tree_loaded);
...@@ -1444,7 +1444,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1444,7 +1444,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1444 .emit_docs = options.emit_docs,1444 .emit_docs = options.emit_docs,
1445 .work_queue = std.fifo.LinearFifo(Job, .Dynamic).init(gpa),1445 .work_queue = std.fifo.LinearFifo(Job, .Dynamic).init(gpa),
1446 .c_object_work_queue = std.fifo.LinearFifo(*CObject, .Dynamic).init(gpa),1446 .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),
1448 .keep_source_files_loaded = options.keep_source_files_loaded,1448 .keep_source_files_loaded = options.keep_source_files_loaded,
1449 .use_clang = use_clang,1449 .use_clang = use_clang,
1450 .clang_argv = options.clang_argv,1450 .clang_argv = options.clang_argv,
...@@ -1770,7 +1770,7 @@ pub fn update(self: *Compilation) !void {...@@ -1770,7 +1770,7 @@ pub fn update(self: *Compilation) !void {
1770 assert(decl.deletion_flag);1770 assert(decl.deletion_flag);
1771 assert(decl.dependants.count() == 0);1771 assert(decl.dependants.count() == 0);
1772 const is_anon = if (decl.zir_decl_index == 0) blk: {1772 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);
1774 } else false;1774 } else false;
17751775
1776 try module.clearDecl(decl, null);1776 try module.clearDecl(decl, null);
...@@ -1889,13 +1889,13 @@ pub fn totalErrorCount(self: *Compilation) usize {...@@ -1889,13 +1889,13 @@ pub fn totalErrorCount(self: *Compilation) usize {
1889 // the previous parse success, including compile errors, but we cannot1889 // the previous parse success, including compile errors, but we cannot
1890 // emit them until the file succeeds parsing.1890 // emit them until the file succeeds parsing.
1891 for (module.failed_decls.keys()) |key| {1891 for (module.failed_decls.keys()) |key| {
1892 if (key.namespace.file_scope.okToReportErrors()) {1892 if (key.getFileScope().okToReportErrors()) {
1893 total += 1;1893 total += 1;
1894 }1894 }
1895 }1895 }
1896 if (module.emit_h) |emit_h| {1896 if (module.emit_h) |emit_h| {
1897 for (emit_h.failed_decls.keys()) |key| {1897 for (emit_h.failed_decls.keys()) |key| {
1898 if (key.namespace.file_scope.okToReportErrors()) {1898 if (key.getFileScope().okToReportErrors()) {
1899 total += 1;1899 total += 1;
1900 }1900 }
1901 }1901 }
...@@ -1968,7 +1968,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {...@@ -1968,7 +1968,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
1968 while (it.next()) |entry| {1968 while (it.next()) |entry| {
1969 // Skip errors for Decls within files that had a parse failure.1969 // Skip errors for Decls within files that had a parse failure.
1970 // We'll try again once parsing succeeds.1970 // We'll try again once parsing succeeds.
1971 if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) {1971 if (entry.key_ptr.*.getFileScope().okToReportErrors()) {
1972 try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);1972 try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);
1973 }1973 }
1974 }1974 }
...@@ -1978,7 +1978,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {...@@ -1978,7 +1978,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
1978 while (it.next()) |entry| {1978 while (it.next()) |entry| {
1979 // Skip errors for Decls within files that had a parse failure.1979 // Skip errors for Decls within files that had a parse failure.
1980 // We'll try again once parsing succeeds.1980 // We'll try again once parsing succeeds.
1981 if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) {1981 if (entry.key_ptr.*.getFileScope().okToReportErrors()) {
1982 try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);1982 try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);
1983 }1983 }
1984 }1984 }
...@@ -2169,12 +2169,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor...@@ -2169,12 +2169,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
2169 defer air.deinit(gpa);2169 defer air.deinit(gpa);
21702170
2171 log.debug("analyze liveness of {s}", .{decl.name});2171 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);
2173 defer liveness.deinit(gpa);2173 defer liveness.deinit(gpa);
21742174
2175 if (builtin.mode == .Debug and self.verbose_air) {2175 if (builtin.mode == .Debug and self.verbose_air) {
2176 std.debug.print("# Begin Function AIR: {s}:\n", .{decl.name});2176 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);
2178 std.debug.print("# End Function AIR: {s}\n\n", .{decl.name});2178 std.debug.print("# End Function AIR: {s}\n\n", .{decl.name});
2179 }2179 }
21802180
...@@ -2465,14 +2465,14 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor...@@ -2465,14 +2465,14 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
2465const AstGenSrc = union(enum) {2465const AstGenSrc = union(enum) {
2466 root,2466 root,
2467 import: struct {2467 import: struct {
2468 importing_file: *Module.Scope.File,2468 importing_file: *Module.File,
2469 import_tok: std.zig.Ast.TokenIndex,2469 import_tok: std.zig.Ast.TokenIndex,
2470 },2470 },
2471};2471};
24722472
2473fn workerAstGenFile(2473fn workerAstGenFile(
2474 comp: *Compilation,2474 comp: *Compilation,
2475 file: *Module.Scope.File,2475 file: *Module.File,
2476 prog_node: *std.Progress.Node,2476 prog_node: *std.Progress.Node,
2477 wg: *WaitGroup,2477 wg: *WaitGroup,
2478 src: AstGenSrc,2478 src: AstGenSrc,
...@@ -2742,7 +2742,7 @@ fn reportRetryableCObjectError(...@@ -2742,7 +2742,7 @@ fn reportRetryableCObjectError(
2742fn reportRetryableAstGenError(2742fn reportRetryableAstGenError(
2743 comp: *Compilation,2743 comp: *Compilation,
2744 src: AstGenSrc,2744 src: AstGenSrc,
2745 file: *Module.Scope.File,2745 file: *Module.File,
2746 err: anyerror,2746 err: anyerror,
2747) error{OutOfMemory}!void {2747) error{OutOfMemory}!void {
2748 const mod = comp.bin_file.options.module.?;2748 const mod = comp.bin_file.options.module.?;
src/Module.zig+346-869
...@@ -59,7 +59,7 @@ export_owners: std.AutoArrayHashMapUnmanaged(*Decl, []*Export) = .{},...@@ -59,7 +59,7 @@ export_owners: std.AutoArrayHashMapUnmanaged(*Decl, []*Export) = .{},
59/// over it and check which source files have been modified on the file system when59/// over it and check which source files have been modified on the file system when
60/// an update is requested, as well as to cache `@import` results.60/// an update is requested, as well as to cache `@import` results.
61/// Keys are fully resolved file paths. This table owns the keys and values.61/// 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
64/// The set of all the generic function instantiations. This is used so that when a generic64/// The set of all the generic function instantiations. This is used so that when a generic
65/// function is called twice with the same comptime parameter arguments, both calls dispatch65/// function is called twice with the same comptime parameter arguments, both calls dispatch
...@@ -85,8 +85,8 @@ failed_decls: std.AutoArrayHashMapUnmanaged(*Decl, *ErrorMsg) = .{},...@@ -85,8 +85,8 @@ failed_decls: std.AutoArrayHashMapUnmanaged(*Decl, *ErrorMsg) = .{},
85/// The value is the AST node index offset from the Decl.85/// The value is the AST node index offset from the Decl.
86compile_log_decls: std.AutoArrayHashMapUnmanaged(*Decl, i32) = .{},86compile_log_decls: std.AutoArrayHashMapUnmanaged(*Decl, i32) = .{},
87/// Using a map here for consistency with the other fields here.87/// 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.88/// The ErrorMsg memory is owned by the `File`, using Module's general purpose allocator.
89failed_files: std.AutoArrayHashMapUnmanaged(*Scope.File, ?*ErrorMsg) = .{},89failed_files: std.AutoArrayHashMapUnmanaged(*File, ?*ErrorMsg) = .{},
90/// Using a map here for consistency with the other fields here.90/// Using a map here for consistency with the other fields here.
91/// The ErrorMsg memory is owned by the `Export`, using Module's general purpose allocator.91/// The ErrorMsg memory is owned by the `Export`, using Module's general purpose allocator.
92failed_exports: std.AutoArrayHashMapUnmanaged(*Export, *ErrorMsg) = .{},92failed_exports: std.AutoArrayHashMapUnmanaged(*Export, *ErrorMsg) = .{},
...@@ -264,7 +264,7 @@ pub const Export = struct {...@@ -264,7 +264,7 @@ pub const Export = struct {
264264
265 pub fn getSrcLoc(exp: Export) SrcLoc {265 pub fn getSrcLoc(exp: Export) SrcLoc {
266 return .{266 return .{
267 .file_scope = exp.src_decl.namespace.file_scope,267 .file_scope = exp.src_decl.getFileScope(),
268 .parent_decl_node = exp.src_decl.src_node,268 .parent_decl_node = exp.src_decl.src_node,
269 .lazy = exp.src,269 .lazy = exp.src,
270 };270 };
...@@ -350,7 +350,7 @@ pub const Decl = struct {...@@ -350,7 +350,7 @@ pub const Decl = struct {
350 /// Reference to externally owned memory.350 /// Reference to externally owned memory.
351 /// In the case of the Decl corresponding to a file, this is351 /// In the case of the Decl corresponding to a file, this is
352 /// the namespace of the struct, since there is no parent.352 /// the namespace of the struct, since there is no parent.
353 namespace: *Scope.Namespace,353 src_namespace: *Namespace,
354354
355 /// The scope which lexically contains this decl. A decl must depend355 /// The scope which lexically contains this decl. A decl must depend
356 /// on its lexical parent, in order to ensure that this pointer is valid.356 /// on its lexical parent, in order to ensure that this pointer is valid.
...@@ -516,7 +516,7 @@ pub const Decl = struct {...@@ -516,7 +516,7 @@ pub const Decl = struct {
516 /// This name is relative to the containing namespace of the decl.516 /// This name is relative to the containing namespace of the decl.
517 /// The memory is owned by the containing File ZIR.517 /// The memory is owned by the containing File ZIR.
518 pub fn getName(decl: Decl) ?[:0]const u8 {518 pub fn getName(decl: Decl) ?[:0]const u8 {
519 const zir = decl.namespace.file_scope.zir;519 const zir = decl.getFileScope().zir;
520 return decl.getNameZir(zir);520 return decl.getNameZir(zir);
521 }521 }
522522
...@@ -528,7 +528,7 @@ pub const Decl = struct {...@@ -528,7 +528,7 @@ pub const Decl = struct {
528 }528 }
529529
530 pub fn contentsHash(decl: Decl) std.zig.SrcHash {530 pub fn contentsHash(decl: Decl) std.zig.SrcHash {
531 const zir = decl.namespace.file_scope.zir;531 const zir = decl.getFileScope().zir;
532 return decl.contentsHashZir(zir);532 return decl.contentsHashZir(zir);
533 }533 }
534534
...@@ -541,21 +541,21 @@ pub const Decl = struct {...@@ -541,21 +541,21 @@ pub const Decl = struct {
541541
542 pub fn zirBlockIndex(decl: *const Decl) Zir.Inst.Index {542 pub fn zirBlockIndex(decl: *const Decl) Zir.Inst.Index {
543 assert(decl.zir_decl_index != 0);543 assert(decl.zir_decl_index != 0);
544 const zir = decl.namespace.file_scope.zir;544 const zir = decl.getFileScope().zir;
545 return zir.extra[decl.zir_decl_index + 6];545 return zir.extra[decl.zir_decl_index + 6];
546 }546 }
547547
548 pub fn zirAlignRef(decl: Decl) Zir.Inst.Ref {548 pub fn zirAlignRef(decl: Decl) Zir.Inst.Ref {
549 if (!decl.has_align) return .none;549 if (!decl.has_align) return .none;
550 assert(decl.zir_decl_index != 0);550 assert(decl.zir_decl_index != 0);
551 const zir = decl.namespace.file_scope.zir;551 const zir = decl.getFileScope().zir;
552 return @intToEnum(Zir.Inst.Ref, zir.extra[decl.zir_decl_index + 7]);552 return @intToEnum(Zir.Inst.Ref, zir.extra[decl.zir_decl_index + 7]);
553 }553 }
554554
555 pub fn zirLinksectionRef(decl: Decl) Zir.Inst.Ref {555 pub fn zirLinksectionRef(decl: Decl) Zir.Inst.Ref {
556 if (!decl.has_linksection_or_addrspace) return .none;556 if (!decl.has_linksection_or_addrspace) return .none;
557 assert(decl.zir_decl_index != 0);557 assert(decl.zir_decl_index != 0);
558 const zir = decl.namespace.file_scope.zir;558 const zir = decl.getFileScope().zir;
559 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align);559 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align);
560 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);560 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
561 }561 }
...@@ -563,16 +563,16 @@ pub const Decl = struct {...@@ -563,16 +563,16 @@ pub const Decl = struct {
563 pub fn zirAddrspaceRef(decl: Decl) Zir.Inst.Ref {563 pub fn zirAddrspaceRef(decl: Decl) Zir.Inst.Ref {
564 if (!decl.has_linksection_or_addrspace) return .none;564 if (!decl.has_linksection_or_addrspace) return .none;
565 assert(decl.zir_decl_index != 0);565 assert(decl.zir_decl_index != 0);
566 const zir = decl.namespace.file_scope.zir;566 const zir = decl.getFileScope().zir;
567 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align) + 1;567 const extra_index = decl.zir_decl_index + 7 + @boolToInt(decl.has_align) + 1;
568 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);568 return @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
569 }569 }
570570
571 /// Returns true if and only if the Decl is the top level struct associated with a File.571 /// Returns true if and only if the Decl is the top level struct associated with a File.
572 pub fn isRoot(decl: *const Decl) bool {572 pub fn isRoot(decl: *const Decl) bool {
573 if (decl.namespace.parent != null)573 if (decl.src_namespace.parent != null)
574 return false;574 return false;
575 return decl == decl.namespace.ty.getOwnerDecl();575 return decl == decl.src_namespace.getDecl();
576 }576 }
577577
578 pub fn relativeToLine(decl: Decl, offset: u32) u32 {578 pub fn relativeToLine(decl: Decl, offset: u32) u32 {
...@@ -608,18 +608,23 @@ pub const Decl = struct {...@@ -608,18 +608,23 @@ pub const Decl = struct {
608 }608 }
609609
610 pub fn srcToken(decl: Decl) Ast.TokenIndex {610 pub fn srcToken(decl: Decl) Ast.TokenIndex {
611 const tree = &decl.namespace.file_scope.tree;611 const tree = &decl.getFileScope().tree;
612 return tree.firstToken(decl.src_node);612 return tree.firstToken(decl.src_node);
613 }613 }
614614
615 pub fn srcByteOffset(decl: Decl) u32 {615 pub fn srcByteOffset(decl: Decl) u32 {
616 const tree = &decl.namespace.file_scope.tree;616 const tree = &decl.getFileScope().tree;
617 return tree.tokens.items(.start)[decl.srcToken()];617 return tree.tokens.items(.start)[decl.srcToken()];
618 }618 }
619619
620 pub fn renderFullyQualifiedName(decl: Decl, writer: anytype) !void {620 pub fn renderFullyQualifiedName(decl: Decl, writer: anytype) !void {
621 const unqualified_name = mem.spanZ(decl.name);621 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);
623 }628 }
624629
625 pub fn getFullyQualifiedName(decl: Decl, gpa: *Allocator) ![:0]u8 {630 pub fn getFullyQualifiedName(decl: Decl, gpa: *Allocator) ![:0]u8 {
...@@ -685,7 +690,7 @@ pub const Decl = struct {...@@ -685,7 +690,7 @@ pub const Decl = struct {
685 /// Gets the namespace that this Decl creates by being a struct, union,690 /// Gets the namespace that this Decl creates by being a struct, union,
686 /// enum, or opaque.691 /// enum, or opaque.
687 /// Only returns it if the Decl is the owner.692 /// Only returns it if the Decl is the owner.
688 pub fn getInnerNamespace(decl: *Decl) ?*Scope.Namespace {693 pub fn getInnerNamespace(decl: *Decl) ?*Namespace {
689 if (!decl.owns_tv) return null;694 if (!decl.owns_tv) return null;
690 const ty = (decl.val.castTag(.ty) orelse return null).data;695 const ty = (decl.val.castTag(.ty) orelse return null).data;
691 switch (ty.tag()) {696 switch (ty.tag()) {
...@@ -730,8 +735,8 @@ pub const Decl = struct {...@@ -730,8 +735,8 @@ pub const Decl = struct {
730 std.debug.print("\n", .{});735 std.debug.print("\n", .{});
731 }736 }
732737
733 pub fn getFileScope(decl: Decl) *Scope.File {738 pub fn getFileScope(decl: Decl) *File {
734 return decl.namespace.file_scope;739 return decl.src_namespace.file_scope;
735 }740 }
736741
737 pub fn getEmitH(decl: *Decl, module: *Module) *EmitH {742 pub fn getEmitH(decl: *Decl, module: *Module) *EmitH {
...@@ -782,7 +787,7 @@ pub const Struct = struct {...@@ -782,7 +787,7 @@ pub const Struct = struct {
782 /// Set of field names in declaration order.787 /// Set of field names in declaration order.
783 fields: std.StringArrayHashMapUnmanaged(Field),788 fields: std.StringArrayHashMapUnmanaged(Field),
784 /// Represents the declarations inside this struct.789 /// Represents the declarations inside this struct.
785 namespace: Scope.Namespace,790 namespace: Namespace,
786 /// Offset from `owner_decl`, points to the struct AST node.791 /// Offset from `owner_decl`, points to the struct AST node.
787 node_offset: i32,792 node_offset: i32,
788 /// Index of the struct_decl ZIR instruction.793 /// Index of the struct_decl ZIR instruction.
...@@ -904,7 +909,7 @@ pub const EnumFull = struct {...@@ -904,7 +909,7 @@ pub const EnumFull = struct {
904 /// If this hash map is empty, it means the enum tags are auto-numbered.909 /// If this hash map is empty, it means the enum tags are auto-numbered.
905 values: ValueMap,910 values: ValueMap,
906 /// Represents the declarations inside this enum.911 /// Represents the declarations inside this enum.
907 namespace: Scope.Namespace,912 namespace: Namespace,
908 /// Offset from `owner_decl`, points to the enum decl AST node.913 /// Offset from `owner_decl`, points to the enum decl AST node.
909 node_offset: i32,914 node_offset: i32,
910915
...@@ -932,7 +937,7 @@ pub const Union = struct {...@@ -932,7 +937,7 @@ pub const Union = struct {
932 /// Set of field names in declaration order.937 /// Set of field names in declaration order.
933 fields: std.StringArrayHashMapUnmanaged(Field),938 fields: std.StringArrayHashMapUnmanaged(Field),
934 /// Represents the declarations inside this union.939 /// Represents the declarations inside this union.
935 namespace: Scope.Namespace,940 namespace: Namespace,
936 /// Offset from `owner_decl`, points to the union decl AST node.941 /// Offset from `owner_decl`, points to the union decl AST node.
937 node_offset: i32,942 node_offset: i32,
938 /// Index of the union_decl ZIR instruction.943 /// Index of the union_decl ZIR instruction.
...@@ -1076,636 +1081,312 @@ pub const Var = struct {...@@ -1076,636 +1081,312 @@ pub const Var = struct {
1076 is_threadlocal: bool,1081 is_threadlocal: bool,
1077};1082};
10781083
1079pub const Scope = struct {1084/// The container that structs, enums, unions, and opaques have.
1080 tag: Tag,1085pub const Namespace = struct {
10811086 parent: ?*Namespace,
1082 pub fn cast(base: *Scope, comptime T: type) ?*T {1087 file_scope: *File,
1083 if (base.tag != T.base_tag)1088 /// Will be a struct, enum, union, or opaque.
1084 return null;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);1098 anon_decls: std.AutoArrayHashMapUnmanaged(*Decl, void) = .{},
1087 }
10881099
1089 /// Get the decl which contains this decl, for the purposes of source reporting1100 /// Key is usingnamespace Decl itself. To find the namespace being included,
1090 pub fn srcDecl(scope: *Scope) ?*Decl {1101 /// the Decl Value has to be resolved as a Type which has a Namespace.
1091 return switch (scope.tag) {1102 /// Value is whether the usingnamespace decl is marked `pub`.
1092 .block => scope.cast(Block).?.src_decl,1103 usingnamespace_set: std.AutoHashMapUnmanaged(*Decl, bool) = .{},
1093 .file => null,
1094 .namespace => scope.cast(Namespace).?.getDecl(),
1095 };
1096 }
10971104
1098 /// Get the scope which contains this decl, for resolving closure_get instructions.1105 pub fn deinit(ns: *Namespace, mod: *Module) void {
1099 pub fn srcScope(scope: *Scope) ?*CaptureScope {1106 ns.destroyDecls(mod);
1100 return switch (scope.tag) {1107 ns.* = undefined;
1101 .block => scope.cast(Block).?.wip_capture_scope,
1102 .file => null,
1103 .namespace => scope.cast(Namespace).?.getDecl().src_scope,
1104 };
1105 }1108 }
11061109
1107 /// Asserts the scope has a parent which is a Namespace and returns it.1110 pub fn destroyDecls(ns: *Namespace, mod: *Module) void {
1108 pub fn namespace(scope: *Scope) *Namespace {1111 const gpa = mod.gpa;
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 }
11151112
1116 /// Asserts the scope has a parent which is a Namespace or File and1113 log.debug("destroyDecls {*}", .{ns});
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 }
11251114
1126 /// When called from inside a Block Scope, chases the src_decl, not the owner_decl.1115 var decls = ns.decls;
1127 pub fn getFileScope(base: *Scope) *Scope.File {1116 ns.decls = .{};
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 }
11371117
1138 pub const Tag = enum {1118 var anon_decls = ns.anon_decls;
1139 /// .zig source code.1119 ns.anon_decls = .{};
1140 file,
1141 /// Namespace owned by structs, enums, unions, and opaques for decls.
1142 namespace,
1143 block,
1144 };
11451120
1146 /// The container that structs, enums, unions, and opaques have.1121 for (decls.values()) |value| {
1147 pub const Namespace = struct {1122 value.destroy(mod);
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;
1173 }1123 }
1124 decls.deinit(gpa);
11741125
1175 pub fn destroyDecls(ns: *Namespace, mod: *Module) void {1126 for (anon_decls.keys()) |key| {
1176 const gpa = mod.gpa;1127 key.destroy(mod);
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);
1195 }1128 }
1129 anon_decls.deinit(gpa);
1130 }
11961131
1197 pub fn deleteAllDecls(1132 pub fn deleteAllDecls(
1198 ns: *Namespace,1133 ns: *Namespace,
1199 mod: *Module,1134 mod: *Module,
1200 outdated_decls: ?*std.AutoArrayHashMap(*Decl, void),1135 outdated_decls: ?*std.AutoArrayHashMap(*Decl, void),
1201 ) !void {1136 ) !void {
1202 const gpa = mod.gpa;1137 const gpa = mod.gpa;
1203
1204 log.debug("deleteAllDecls {*}", .{ns});
12051138
1206 var decls = ns.decls;1139 log.debug("deleteAllDecls {*}", .{ns});
1207 ns.decls = .{};
12081140
1209 var anon_decls = ns.anon_decls;1141 var decls = ns.decls;
1210 ns.anon_decls = .{};1142 ns.decls = .{};
12111143
1212 // TODO rework this code to not panic on OOM.1144 var anon_decls = ns.anon_decls;
1213 // (might want to coordinate with the clearDecl function)1145 ns.anon_decls = .{};
12141146
1215 for (decls.values()) |child_decl| {1147 // TODO rework this code to not panic on OOM.
1216 mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory");1148 // (might want to coordinate with the clearDecl function)
1217 child_decl.destroy(mod);
1218 }
1219 decls.deinit(gpa);
12201149
1221 for (anon_decls.keys()) |child_decl| {1150 for (decls.values()) |child_decl| {
1222 mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory");1151 mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory");
1223 child_decl.destroy(mod);1152 child_decl.destroy(mod);
1224 }
1225 anon_decls.deinit(gpa);
1226 }1153 }
1154 decls.deinit(gpa);
12271155
1228 // This renders e.g. "std.fs.Dir.OpenOptions"1156 for (anon_decls.keys()) |child_decl| {
1229 pub fn renderFullyQualifiedName(1157 mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory");
1230 ns: Namespace,1158 child_decl.destroy(mod);
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 }
1244 }1159 }
12451160 anon_decls.deinit(gpa);
1246 pub fn getDecl(ns: Namespace) *Decl {1161 }
1247 return ns.ty.getOwnerDecl();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);
1248 }1174 }
1249 };1175 if (name.len != 0) {
12501176 try writer.writeAll(".");
1251 pub const File = struct {1177 try writer.writeAll(name);
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);
1302 }1178 }
1179 }
13031180
1304 pub fn unloadTree(file: *File, gpa: *Allocator) void {1181 /// This renders e.g. "std/fs.zig:Dir.OpenOptions"
1305 if (file.tree_loaded) {1182 pub fn renderFullyQualifiedDebugName(
1306 file.tree_loaded = false;1183 ns: Namespace,
1307 file.tree.deinit(gpa);1184 name: []const u8,
1308 }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 = ':';
1309 }1194 }
13101195 if (name.len != 0) {
1311 pub fn unloadSource(file: *File, gpa: *Allocator) void {1196 try writer.writeByte(separator_char);
1312 if (file.source_loaded) {1197 try writer.writeAll(name);
1313 file.source_loaded = false;
1314 gpa.free(file.source);
1315 }
1316 }1198 }
1199 }
13171200
1318 pub fn unloadZir(file: *File, gpa: *Allocator) void {1201 pub fn getDecl(ns: Namespace) *Decl {
1319 if (file.zir_loaded) {1202 return ns.ty.getOwnerDecl();
1320 file.zir_loaded = false;1203 }
1321 file.zir.deinit(gpa);1204};
1322 }
1323 }
13241205
1325 pub fn deinit(file: *File, mod: *Module) void {1206pub const File = struct {
1326 const gpa = mod.gpa;1207 status: enum {
1327 log.debug("deinit File {s}", .{file.sub_file_path});1208 never_loaded,
1328 file.deleted_decls.deinit(gpa);1209 retryable_failure,
1329 file.outdated_decls.deinit(gpa);1210 parse_failure,
1330 if (file.root_decl) |root_decl| {1211 astgen_failure,
1331 root_decl.destroy(mod);1212 success_zir,
1332 }1213 },
1333 gpa.free(file.sub_file_path);1214 source_loaded: bool,
1334 file.unload(gpa);1215 tree_loaded: bool,
1335 if (file.prev_zir) |prev_zir| {1216 zir_loaded: bool,
1336 prev_zir.deinit(gpa);1217 /// Relative to the owning package's root_src_dir.
1337 gpa.destroy(prev_zir);1218 /// Memory is stored in gpa, owned by File.
1338 }1219 sub_file_path: []const u8,
1339 file.* = undefined;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);
1340 }1261 }
1262 }
13411263
1342 pub fn getSource(file: *File, gpa: *Allocator) ![:0]const u8 {1264 pub fn unloadSource(file: *File, gpa: *Allocator) void {
1343 if (file.source_loaded) return file.source;1265 if (file.source_loaded) {
13441266 file.source_loaded = false;
1345 const root_dir_path = file.pkg.root_src_directory.path orelse ".";1267 gpa.free(file.source);
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;
1373 }1268 }
1269 }
13741270
1375 pub fn getTree(file: *File, gpa: *Allocator) !*const Ast {1271 pub fn unloadZir(file: *File, gpa: *Allocator) void {
1376 if (file.tree_loaded) return &file.tree;1272 if (file.zir_loaded) {
13771273 file.zir_loaded = false;
1378 const source = try file.getSource(gpa);1274 file.zir.deinit(gpa);
1379 file.tree = try std.zig.parse(gpa, source);
1380 file.tree_loaded = true;
1381 return &file.tree;
1382 }1275 }
1276 }
13831277
1384 pub fn destroy(file: *File, mod: *Module) void {1278 pub fn deinit(file: *File, mod: *Module) void {
1385 const gpa = mod.gpa;1279 const gpa = mod.gpa;
1386 file.deinit(mod);1280 log.debug("deinit File {s}", .{file.sub_file_path});
1387 gpa.destroy(file);1281 file.deleted_decls.deinit(gpa);
1282 file.outdated_decls.deinit(gpa);
1283 if (file.root_decl) |root_decl| {
1284 root_decl.destroy(mod);
1388 }1285 }
13891286 gpa.free(file.sub_file_path);
1390 pub fn renderFullyQualifiedName(file: File, writer: anytype) !void {1287 file.unload(gpa);
1391 // Convert all the slashes into dots and truncate the extension.1288 if (file.prev_zir) |prev_zir| {
1392 const ext = std.fs.path.extension(file.sub_file_path);1289 prev_zir.deinit(gpa);
1393 const noext = file.sub_file_path[0 .. file.sub_file_path.len - ext.len];1290 gpa.destroy(prev_zir);
1394 for (noext) |byte| switch (byte) {
1395 '/', '\\' => try writer.writeByte('.'),
1396 else => try writer.writeByte(byte),
1397 };
1398 }1291 }
1292 file.* = undefined;
1293 }
13991294
1400 pub fn fullyQualifiedNameZ(file: File, gpa: *Allocator) ![:0]u8 {1295 pub fn getSource(file: *File, gpa: *Allocator) ![:0]const u8 {
1401 var buf = std.ArrayList(u8).init(gpa);1296 if (file.source_loaded) return file.source;
1402 defer buf.deinit();
1403 try file.renderFullyQualifiedName(buf.writer());
1404 return buf.toOwnedSliceSentinel(0);
1405 }
14061297
1407 /// Returns the full path to this file relative to its package.1298 const root_dir_path = file.pkg.root_src_directory.path orelse ".";
1408 pub fn fullPath(file: File, ally: *Allocator) ![]u8 {1299 log.debug("File.getSource, not cached. pkgdir={s} sub_file_path={s}", .{
1409 return file.pkg.root_src_directory.join(ally, &[_][]const u8{file.sub_file_path});1300 root_dir_path, file.sub_file_path,
1410 }1301 });
14111302
1412 pub fn dumpSrc(file: *File, src: LazySrcLoc) void {1303 // Keep track of inode, file size, mtime, hash so we can detect which files
1413 const loc = std.zig.findLineColumn(file.source.bytes, src);1304 // have been modified when an incremental update is requested.
1414 std.debug.print("{s}:{d}:{d}\n", .{ file.sub_file_path, loc.line + 1, loc.column + 1 });1305 var f = try file.pkg.root_src_directory.handle.openFile(file.sub_file_path, .{});
1415 }1306 defer f.close();
14161307
1417 pub fn okToReportErrors(file: File) bool {1308 const stat = try f.stat();
1418 return switch (file.status) {
1419 .parse_failure, .astgen_failure => false,
1420 else => true,
1421 };
1422 }
1423 };
14241309
1425 /// This is the context needed to semantically analyze ZIR instructions and1310 if (stat.size > std.math.maxInt(u32))
1426 /// produce AIR instructions.1311 return error.FileTooBig;
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,
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 @setRuntimeSafety1319 // Here we do not modify stat fields because this function is the one
1458 want_safety: ?bool = null,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 {1328 pub fn getTree(file: *File, gpa: *Allocator) !*const Ast {
1463 /// `noreturn` means `anytype`.1329 if (file.tree_loaded) return &file.tree;
1464 ty: Type,
1465 is_comptime: bool,
1466 };
14671330
1468 /// This `Block` maps a block ZIR instruction to the corresponding1331 const source = try file.getSource(gpa);
1469 /// AIR instruction for break instruction analysis.1332 file.tree = try std.zig.parse(gpa, source);
1470 pub const Label = struct {1333 file.tree_loaded = true;
1471 zir_block: Zir.Inst.Index,1334 return &file.tree;
1472 merges: Merges,1335 }
1473 };
14741336
1475 /// This `Block` indicates that an inline function call is happening1337 pub fn destroy(file: *File, mod: *Module) void {
1476 /// and return instructions should be analyzed as a break instruction1338 const gpa = mod.gpa;
1477 /// to this AIR block instruction.1339 file.deinit(mod);
1478 /// It is shared among all the blocks in an inline or comptime called1340 gpa.destroy(file);
1479 /// function.1341 }
1480 pub const Inlining = struct {
1481 comptime_result: Air.Inst.Ref,
1482 merges: Merges,
1483 };
14841342
1485 pub const Merges = struct {1343 pub fn renderFullyQualifiedName(file: File, writer: anytype) !void {
1486 block_inst: Air.Inst.Index,1344 // Convert all the slashes into dots and truncate the extension.
1487 /// Separate array list from break_inst_list so that it can be passed directly1345 const ext = std.fs.path.extension(file.sub_file_path);
1488 /// to resolvePeerTypes.1346 const noext = file.sub_file_path[0 .. file.sub_file_path.len - ext.len];
1489 results: ArrayListUnmanaged(Air.Inst.Ref),1347 for (noext) |byte| switch (byte) {
1490 /// Keeps track of the break instructions so that the operand can be replaced1348 '/', '\\' => try writer.writeByte('.'),
1491 /// if we need to add type coercion at the end of block analysis.1349 else => try writer.writeByte(byte),
1492 /// Same indexes, capacity, length as `results`.
1493 br_list: ArrayListUnmanaged(Air.Inst.Index),
1494 };1350 };
1351 }
14951352
1496 /// For debugging purposes.1353 pub fn renderFullyQualifiedDebugName(file: File, writer: anytype) !void {
1497 pub fn dump(block: *Block, mod: Module) void {1354 for (file.sub_file_path) |byte| switch (byte) {
1498 Zir.dumpBlock(mod, block);1355 '/', '\\' => try writer.writeByte('/'),
1499 }1356 else => try writer.writeByte(byte),
15001357 };
1501 pub fn makeSubBlock(parent: *Block) Block {1358 }
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 }
16731359
1674 pub const WipAnonDecl = struct {1360 pub fn fullyQualifiedNameZ(file: File, gpa: *Allocator) ![:0]u8 {
1675 block: *Scope.Block,1361 var buf = std.ArrayList(u8).init(gpa);
1676 new_decl_arena: std.heap.ArenaAllocator,1362 defer buf.deinit();
1677 finished: bool,1363 try file.renderFullyQualifiedName(buf.writer());
1364 return buf.toOwnedSliceSentinel(0);
1365 }
16781366
1679 pub fn arena(wad: *WipAnonDecl) *Allocator {1367 /// Returns the full path to this file relative to its package.
1680 return &wad.new_decl_arena.allocator;1368 pub fn fullPath(file: File, ally: *Allocator) ![]u8 {
1681 }1369 return file.pkg.root_src_directory.join(ally, &[_][]const u8{file.sub_file_path});
1370 }
16821371
1683 pub fn deinit(wad: *WipAnonDecl) void {1372 pub fn dumpSrc(file: *File, src: LazySrcLoc) void {
1684 if (!wad.finished) {1373 const loc = std.zig.findLineColumn(file.source.bytes, src);
1685 wad.new_decl_arena.deinit();1374 std.debug.print("{s}:{d}:{d}\n", .{ file.sub_file_path, loc.line + 1, loc.column + 1 });
1686 }1375 }
1687 wad.* = undefined;
1688 }
16891376
1690 pub fn finish(wad: *WipAnonDecl, ty: Type, val: Value) !*Decl {1377 pub fn okToReportErrors(file: File) bool {
1691 const new_decl = try wad.block.sema.mod.createAnonymousDecl(&wad.block.base, .{1378 return switch (file.status) {
1692 .ty = ty,1379 .parse_failure, .astgen_failure => false,
1693 .val = val,1380 else => true,
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 }
1700 };1381 };
1701 };1382 }
1702};1383};
17031384
1704/// This struct holds data necessary to construct API-facing `AllErrors.Message`.1385/// This struct holds data necessary to construct API-facing `AllErrors.Message`.
1705/// Its memory is managed with the general purpose allocator so that they1386/// Its memory is managed with the general purpose allocator so that they
1706/// can be created and destroyed in response to incremental updates.1387/// can be created and destroyed in response to incremental updates.
1707/// In some cases, the Scope.File could have been inferred from where the ErrorMsg1388/// In some cases, the 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.File1389/// is stored. For example, if it is stored in Module.failed_decls, then the File
1709/// would be determined by the Decl Scope. However, the data structure contains the field1390/// would be determined by the Decl Scope. However, the data structure contains the field
1710/// anyway so that `ErrorMsg` can be reused for error notes, which may be in a different1391/// anyway so that `ErrorMsg` can be reused for error notes, which may be in a different
1711/// file than the parent error message. It also simplifies processing of error messages.1392/// file than the parent error message. It also simplifies processing of error messages.
...@@ -1757,7 +1438,7 @@ pub const ErrorMsg = struct {...@@ -1757,7 +1438,7 @@ pub const ErrorMsg = struct {
17571438
1758/// Canonical reference to a position within a source file.1439/// Canonical reference to a position within a source file.
1759pub const SrcLoc = struct {1440pub const SrcLoc = struct {
1760 file_scope: *Scope.File,1441 file_scope: *File,
1761 /// Might be 0 depending on tag of `lazy`.1442 /// Might be 0 depending on tag of `lazy`.
1762 parent_decl_node: Ast.Node.Index,1443 parent_decl_node: Ast.Node.Index,
1763 /// Relative to `parent_decl_node`.1444 /// Relative to `parent_decl_node`.
...@@ -2334,60 +2015,8 @@ pub const LazySrcLoc = union(enum) {...@@ -2334,60 +2015,8 @@ pub const LazySrcLoc = union(enum) {
2334 /// The Decl is determined contextually.2015 /// The Decl is determined contextually.
2335 node_offset_lib_name: i32,2016 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
2389 /// Upgrade to a `SrcLoc` based on the `Decl` provided.2018 /// 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 {
2391 return switch (lazy) {2020 return switch (lazy) {
2392 .unneeded,2021 .unneeded,
2393 .entire_file,2022 .entire_file,
...@@ -2576,7 +2205,7 @@ comptime {...@@ -2576,7 +2205,7 @@ comptime {
2576 }2205 }
2577}2206}
25782207
2579pub fn astGenFile(mod: *Module, file: *Scope.File) !void {2208pub fn astGenFile(mod: *Module, file: *File) !void {
2580 const tracy = trace(@src());2209 const tracy = trace(@src());
2581 defer tracy.end();2210 defer tracy.end();
25822211
...@@ -2960,7 +2589,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File) !void {...@@ -2960,7 +2589,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File) !void {
2960/// * Decl.zir_index2589/// * Decl.zir_index
2961/// * Fn.zir_body_inst2590/// * Fn.zir_body_inst
2962/// * Decl.zir_decl_index2591/// * 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 {
2964 const new_zir = file.zir;2593 const new_zir = file.zir;
29652594
2966 // Maps from old ZIR to new ZIR, struct_decl, enum_decl, etc. Any instruction which2595 // 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 {...@@ -3216,7 +2845,7 @@ pub fn semaPkg(mod: *Module, pkg: *Package) !void {
32162845
3217/// Regardless of the file status, will create a `Decl` so that we2846/// Regardless of the file status, will create a `Decl` so that we
3218/// can track dependencies and re-analyze when the file becomes outdated.2847/// 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 {
3220 const tracy = trace(@src());2849 const tracy = trace(@src());
3221 defer tracy.end();2850 defer tracy.end();
32222851
...@@ -3244,11 +2873,11 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {...@@ -3244,11 +2873,11 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
3244 .file_scope = file,2873 .file_scope = file,
3245 },2874 },
3246 };2875 };
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);
3248 file.root_decl = new_decl;2878 file.root_decl = new_decl;
3249 struct_obj.owner_decl = new_decl;2879 struct_obj.owner_decl = new_decl;
3250 new_decl.src_line = 0;2880 new_decl.src_line = 0;
3251 new_decl.name = try file.fullyQualifiedNameZ(gpa);
3252 new_decl.is_pub = true;2881 new_decl.is_pub = true;
3253 new_decl.is_exported = false;2882 new_decl.is_exported = false;
3254 new_decl.has_align = false;2883 new_decl.has_align = false;
...@@ -3276,7 +2905,6 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {...@@ -3276,7 +2905,6 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
3276 .perm_arena = &new_decl_arena.allocator,2905 .perm_arena = &new_decl_arena.allocator,
3277 .code = file.zir,2906 .code = file.zir,
3278 .owner_decl = new_decl,2907 .owner_decl = new_decl,
3279 .namespace = &struct_obj.namespace,
3280 .func = null,2908 .func = null,
3281 .fn_ret_ty = Type.initTag(.void),2909 .fn_ret_ty = Type.initTag(.void),
3282 .owner_func = null,2910 .owner_func = null,
...@@ -3286,10 +2914,11 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {...@@ -3286,10 +2914,11 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
3286 var wip_captures = try WipCaptureScope.init(gpa, &new_decl_arena.allocator, null);2914 var wip_captures = try WipCaptureScope.init(gpa, &new_decl_arena.allocator, null);
3287 defer wip_captures.deinit();2915 defer wip_captures.deinit();
32882916
3289 var block_scope: Scope.Block = .{2917 var block_scope: Sema.Block = .{
3290 .parent = null,2918 .parent = null,
3291 .sema = &sema,2919 .sema = &sema,
3292 .src_decl = new_decl,2920 .src_decl = new_decl,
2921 .namespace = &struct_obj.namespace,
3293 .wip_capture_scope = wip_captures.scope,2922 .wip_capture_scope = wip_captures.scope,
3294 .instructions = .{},2923 .instructions = .{},
3295 .inlining = null,2924 .inlining = null,
...@@ -3318,12 +2947,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3318,12 +2947,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
3318 const tracy = trace(@src());2947 const tracy = trace(@src());
3319 defer tracy.end();2948 defer tracy.end();
33202949
3321 if (decl.namespace.file_scope.status != .success_zir) {2950 if (decl.getFileScope().status != .success_zir) {
3322 return error.AnalysisFail;2951 return error.AnalysisFail;
3323 }2952 }
33242953
3325 const gpa = mod.gpa;2954 const gpa = mod.gpa;
3326 const zir = decl.namespace.file_scope.zir;2955 const zir = decl.getFileScope().zir;
3327 const zir_datas = zir.instructions.items(.data);2956 const zir_datas = zir.instructions.items(.data);
33282957
3329 decl.analysis = .in_progress;2958 decl.analysis = .in_progress;
...@@ -3342,7 +2971,6 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3342,7 +2971,6 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
3342 .perm_arena = &decl_arena.allocator,2971 .perm_arena = &decl_arena.allocator,
3343 .code = zir,2972 .code = zir,
3344 .owner_decl = decl,2973 .owner_decl = decl,
3345 .namespace = decl.namespace,
3346 .func = null,2974 .func = null,
3347 .fn_ret_ty = Type.initTag(.void),2975 .fn_ret_ty = Type.initTag(.void),
3348 .owner_func = null,2976 .owner_func = null,
...@@ -3366,10 +2994,11 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3366,10 +2994,11 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
3366 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);2994 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);
3367 defer wip_captures.deinit();2995 defer wip_captures.deinit();
33682996
3369 var block_scope: Scope.Block = .{2997 var block_scope: Sema.Block = .{
3370 .parent = null,2998 .parent = null,
3371 .sema = &sema,2999 .sema = &sema,
3372 .src_decl = decl,3000 .src_decl = decl,
3001 .namespace = decl.src_namespace,
3373 .wip_capture_scope = wip_captures.scope,3002 .wip_capture_scope = wip_captures.scope,
3374 .instructions = .{},3003 .instructions = .{},
3375 .inlining = null,3004 .inlining = null,
...@@ -3427,12 +3056,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3427,12 +3056,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
3427 if (decl.is_usingnamespace) {3056 if (decl.is_usingnamespace) {
3428 const ty_ty = Type.initTag(.type);3057 const ty_ty = Type.initTag(.type);
3429 if (!decl_tv.ty.eql(ty_ty)) {3058 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});
3431 }3060 }
3432 var buffer: Value.ToTypeBuffer = undefined;3061 var buffer: Value.ToTypeBuffer = undefined;
3433 const ty = decl_tv.val.toType(&buffer);3062 const ty = decl_tv.val.toType(&buffer);
3434 if (ty.getNamespace() == null) {3063 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});
3436 }3065 }
34373066
3438 decl.ty = ty_ty;3067 decl.ty = ty_ty;
...@@ -3495,11 +3124,11 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3495,11 +3124,11 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
3495 if (decl.is_exported) {3124 if (decl.is_exported) {
3496 const export_src = src; // TODO make this point at `export` token3125 const export_src = src; // TODO make this point at `export` token
3497 if (is_inline) {3126 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", .{});
3499 }3128 }
3500 // The scope needs to have the decl in it.3129 // The scope needs to have the decl in it.
3501 const options: std.builtin.ExportOptions = .{ .name = mem.spanZ(decl.name) };3130 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);
3503 }3132 }
3504 return type_changed or is_inline != prev_is_inline;3133 return type_changed or is_inline != prev_is_inline;
3505 }3134 }
...@@ -3548,18 +3177,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3548,18 +3177,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
3548 try mod.comp.work_queue.writeItem(.{ .emit_h_decl = decl });3177 try mod.comp.work_queue.writeItem(.{ .emit_h_decl = decl });
3549 }3178 }
3550 }3179 }
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
3558 if (decl.is_exported) {3181 if (decl.is_exported) {
3559 const export_src = src; // TODO point to the export token3182 const export_src = src; // TODO point to the export token
3560 // The scope needs to have the decl in it.3183 // The scope needs to have the decl in it.
3561 const options: std.builtin.ExportOptions = .{ .name = mem.spanZ(decl.name) };3184 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);
3563 }3186 }
35643187
3565 return type_changed;3188 return type_changed;
...@@ -3586,7 +3209,7 @@ pub fn declareDeclDependency(mod: *Module, depender: *Decl, dependee: *Decl) !vo...@@ -3586,7 +3209,7 @@ pub fn declareDeclDependency(mod: *Module, depender: *Decl, dependee: *Decl) !vo
3586}3209}
35873210
3588pub const ImportFileResult = struct {3211pub const ImportFileResult = struct {
3589 file: *Scope.File,3212 file: *File,
3590 is_new: bool,3213 is_new: bool,
3591};3214};
35923215
...@@ -3612,7 +3235,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {...@@ -3612,7 +3235,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
3612 const sub_file_path = try gpa.dupe(u8, pkg.root_src_path);3235 const sub_file_path = try gpa.dupe(u8, pkg.root_src_path);
3613 errdefer gpa.free(sub_file_path);3236 errdefer gpa.free(sub_file_path);
36143237
3615 const new_file = try gpa.create(Scope.File);3238 const new_file = try gpa.create(File);
3616 errdefer gpa.destroy(new_file);3239 errdefer gpa.destroy(new_file);
36173240
3618 gop.value_ptr.* = new_file;3241 gop.value_ptr.* = new_file;
...@@ -3639,7 +3262,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {...@@ -3639,7 +3262,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
36393262
3640pub fn importFile(3263pub fn importFile(
3641 mod: *Module,3264 mod: *Module,
3642 cur_file: *Scope.File,3265 cur_file: *File,
3643 import_string: []const u8,3266 import_string: []const u8,
3644) !ImportFileResult {3267) !ImportFileResult {
3645 if (cur_file.pkg.table.get(import_string)) |pkg| {3268 if (cur_file.pkg.table.get(import_string)) |pkg| {
...@@ -3667,7 +3290,7 @@ pub fn importFile(...@@ -3667,7 +3290,7 @@ pub fn importFile(
3667 };3290 };
3668 keep_resolved_path = true; // It's now owned by import_table.3291 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);
3671 errdefer gpa.destroy(new_file);3294 errdefer gpa.destroy(new_file);
36723295
3673 const resolved_root_path = try std.fs.path.resolve(gpa, &[_][]const u8{cur_pkg_dir_path});3296 const resolved_root_path = try std.fs.path.resolve(gpa, &[_][]const u8{cur_pkg_dir_path});
...@@ -3708,7 +3331,7 @@ pub fn importFile(...@@ -3708,7 +3331,7 @@ pub fn importFile(
37083331
3709pub fn scanNamespace(3332pub fn scanNamespace(
3710 mod: *Module,3333 mod: *Module,
3711 namespace: *Scope.Namespace,3334 namespace: *Namespace,
3712 extra_start: usize,3335 extra_start: usize,
3713 decls_len: u32,3336 decls_len: u32,
3714 parent_decl: *Decl,3337 parent_decl: *Decl,
...@@ -3752,7 +3375,7 @@ pub fn scanNamespace(...@@ -3752,7 +3375,7 @@ pub fn scanNamespace(
37523375
3753const ScanDeclIter = struct {3376const ScanDeclIter = struct {
3754 module: *Module,3377 module: *Module,
3755 namespace: *Scope.Namespace,3378 namespace: *Namespace,
3756 parent_decl: *Decl,3379 parent_decl: *Decl,
3757 usingnamespace_index: usize = 0,3380 usingnamespace_index: usize = 0,
3758 comptime_index: usize = 0,3381 comptime_index: usize = 0,
...@@ -3818,13 +3441,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi...@@ -3818,13 +3441,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
3818 // We create a Decl for it regardless of analysis status.3441 // We create a Decl for it regardless of analysis status.
3819 const gop = try namespace.decls.getOrPut(gpa, decl_name);3442 const gop = try namespace.decls.getOrPut(gpa, decl_name);
3820 if (!gop.found_existing) {3443 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);
3822 if (is_usingnamespace) {3445 if (is_usingnamespace) {
3823 namespace.usingnamespace_set.putAssumeCapacity(new_decl, is_pub);3446 namespace.usingnamespace_set.putAssumeCapacity(new_decl, is_pub);
3824 }3447 }
3825 log.debug("scan new {*} ({s}) into {*}", .{ new_decl, decl_name, namespace });3448 log.debug("scan new {*} ({s}) into {*}", .{ new_decl, decl_name, namespace });
3826 new_decl.src_line = line;3449 new_decl.src_line = line;
3827 new_decl.name = decl_name;
3828 gop.value_ptr.* = new_decl;3450 gop.value_ptr.* = new_decl;
3829 // Exported decls, comptime decls, usingnamespace decls, and3451 // Exported decls, comptime decls, usingnamespace decls, and
3830 // test decls if in test mode, get analyzed.3452 // test decls if in test mode, get analyzed.
...@@ -4013,9 +3635,10 @@ pub fn deleteUnusedDecl(mod: *Module, decl: *Decl) void {...@@ -4013,9 +3635,10 @@ pub fn deleteUnusedDecl(mod: *Module, decl: *Decl) void {
4013 },3635 },
4014 }3636 }
40153637
4016 const dependants = decl.dependants.keys();3638 assert(!decl.isRoot());
4017 assert(dependants[0].namespace.anon_decls.swapRemove(decl));3639 assert(decl.src_namespace.anon_decls.swapRemove(decl));
40183640
3641 const dependants = decl.dependants.keys();
4019 for (dependants) |dep| {3642 for (dependants) |dep| {
4020 dep.removeDependency(decl);3643 dep.removeDependency(decl);
4021 }3644 }
...@@ -4026,10 +3649,22 @@ pub fn deleteUnusedDecl(mod: *Module, decl: *Decl) void {...@@ -4026,10 +3649,22 @@ pub fn deleteUnusedDecl(mod: *Module, decl: *Decl) void {
4026 decl.destroy(mod);3649 decl.destroy(mod);
4027}3650}
40283651
4029pub fn deleteAnonDecl(mod: *Module, scope: *Scope, decl: *Decl) void {3652/// Cancel the creation of an anon decl and delete any references to it.
4030 log.debug("deleteAnonDecl {*} ({s})", .{ decl, decl.name });3653/// If other decls depend on this decl, they must be aborted first.
4031 const scope_decl = scope.srcDecl().?;3654pub fn abortAnonDecl(mod: *Module, decl: *Decl) void {
4032 assert(scope_decl.namespace.anon_decls.swapRemove(decl));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
4033 decl.destroy(mod);3668 decl.destroy(mod);
4034}3669}
40353670
...@@ -4087,9 +3722,8 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se...@@ -4087,9 +3722,8 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se
4087 .gpa = gpa,3722 .gpa = gpa,
4088 .arena = arena,3723 .arena = arena,
4089 .perm_arena = &decl_arena.allocator,3724 .perm_arena = &decl_arena.allocator,
4090 .code = decl.namespace.file_scope.zir,3725 .code = decl.getFileScope().zir,
4091 .owner_decl = decl,3726 .owner_decl = decl,
4092 .namespace = decl.namespace,
4093 .func = func,3727 .func = func,
4094 .fn_ret_ty = func.owner_decl.ty.fnReturnType(),3728 .fn_ret_ty = func.owner_decl.ty.fnReturnType(),
4095 .owner_func = func,3729 .owner_func = func,
...@@ -4104,10 +3738,11 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se...@@ -4104,10 +3738,11 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se
4104 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);3738 var wip_captures = try WipCaptureScope.init(gpa, &decl_arena.allocator, decl.src_scope);
4105 defer wip_captures.deinit();3739 defer wip_captures.deinit();
41063740
4107 var inner_block: Scope.Block = .{3741 var inner_block: Sema.Block = .{
4108 .parent = null,3742 .parent = null,
4109 .sema = &sema,3743 .sema = &sema,
4110 .src_decl = decl,3744 .src_decl = decl,
3745 .namespace = decl.src_namespace,
4111 .wip_capture_scope = wip_captures.scope,3746 .wip_capture_scope = wip_captures.scope,
4112 .instructions = .{},3747 .instructions = .{},
4113 .inlining = null,3748 .inlining = null,
...@@ -4226,7 +3861,7 @@ fn markOutdatedDecl(mod: *Module, decl: *Decl) !void {...@@ -4226,7 +3861,7 @@ fn markOutdatedDecl(mod: *Module, decl: *Decl) !void {
4226 decl.analysis = .outdated;3861 decl.analysis = .outdated;
4227}3862}
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 {
4230 // If we have emit-h then we must allocate a bigger structure to store the emit-h state.3865 // If we have emit-h then we must allocate a bigger structure to store the emit-h state.
4231 const new_decl: *Decl = if (mod.emit_h != null) blk: {3866 const new_decl: *Decl = if (mod.emit_h != null) blk: {
4232 const parent_struct = try mod.gpa.create(DeclPlusEmitH);3867 const parent_struct = try mod.gpa.create(DeclPlusEmitH);
...@@ -4238,8 +3873,8 @@ pub fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: Ast....@@ -4238,8 +3873,8 @@ pub fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: Ast.
4238 } else try mod.gpa.create(Decl);3873 } else try mod.gpa.create(Decl);
42393874
4240 new_decl.* = .{3875 new_decl.* = .{
4241 .name = "",3876 .name = name,
4242 .namespace = namespace,3877 .src_namespace = namespace,
4243 .src_node = src_node,3878 .src_node = src_node,
4244 .src_line = undefined,3879 .src_line = undefined,
4245 .has_tv = false,3880 .has_tv = false,
...@@ -4304,119 +3939,49 @@ pub fn getErrorValue(mod: *Module, name: []const u8) !std.StringHashMapUnmanaged...@@ -4304,119 +3939,49 @@ pub fn getErrorValue(mod: *Module, name: []const u8) !std.StringHashMapUnmanaged
4304 };3939 };
4305}3940}
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
4382/// Takes ownership of `name` even if it returns an error.3942/// Takes ownership of `name` even if it returns an error.
4383pub fn createAnonymousDeclNamed(3943pub fn createAnonymousDeclNamed(
4384 mod: *Module,3944 mod: *Module,
4385 scope: *Scope,3945 block: *Sema.Block,
4386 typed_value: TypedValue,3946 typed_value: TypedValue,
4387 name: [:0]u8,3947 name: [:0]u8,
4388) !*Decl {3948) !*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);
4390}3950}
43913951
4392pub fn createAnonymousDecl(mod: *Module, scope: *Scope, typed_value: TypedValue) !*Decl {3952pub fn createAnonymousDecl(mod: *Module, block: *Sema.Block, typed_value: TypedValue) !*Decl {
4393 return mod.createAnonymousDeclFromDecl(scope.srcDecl().?, scope.srcScope(), typed_value);3953 return mod.createAnonymousDeclFromDecl(block.src_decl, block.namespace, block.wip_capture_scope, typed_value);
4394}3954}
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 {
4397 const name_index = mod.getNextAnonNameIndex();3963 const name_index = mod.getNextAnonNameIndex();
4398 const name = try std.fmt.allocPrintZ(mod.gpa, "{s}__anon_{d}", .{3964 const name = try std.fmt.allocPrintZ(mod.gpa, "{s}__anon_{d}", .{
4399 src_decl.name, name_index,3965 src_decl.name, name_index,
4400 });3966 });
4401 return mod.createAnonymousDeclFromDeclNamed(src_decl, src_scope, tv, name);3967 return mod.createAnonymousDeclFromDeclNamed(src_decl, namespace, src_scope, tv, name);
4402}3968}
44033969
4404/// Takes ownership of `name` even if it returns an error.3970/// Takes ownership of `name` even if it returns an error.
4405pub fn createAnonymousDeclFromDeclNamed(3971pub fn createAnonymousDeclFromDeclNamed(
4406 mod: *Module,3972 mod: *Module,
4407 src_decl: *Decl,3973 src_decl: *Decl,
3974 namespace: *Namespace,
4408 src_scope: ?*CaptureScope,3975 src_scope: ?*CaptureScope,
4409 typed_value: TypedValue,3976 typed_value: TypedValue,
4410 name: [:0]u8,3977 name: [:0]u8,
4411) !*Decl {3978) !*Decl {
4412 errdefer mod.gpa.free(name);3979 errdefer mod.gpa.free(name);
44133980
4414 const namespace = src_decl.namespace;
4415 try namespace.anon_decls.ensureUnusedCapacity(mod.gpa, 1);3981 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;
4420 new_decl.src_line = src_decl.src_line;3985 new_decl.src_line = src_decl.src_line;
4421 new_decl.ty = typed_value.ty;3986 new_decl.ty = typed_value.ty;
4422 new_decl.val = typed_value.val;3987 new_decl.val = typed_value.val;
...@@ -4458,19 +4023,6 @@ pub fn makeIntType(arena: *Allocator, signedness: std.builtin.Signedness, bits:...@@ -4458,19 +4023,6 @@ pub fn makeIntType(arena: *Allocator, signedness: std.builtin.Signedness, bits:
4458 return Type.initPayload(&int_payload.base);4023 return Type.initPayload(&int_payload.base);
4459}4024}
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
4474pub fn errNoteNonLazy(4026pub fn errNoteNonLazy(
4475 mod: *Module,4027 mod: *Module,
4476 src_loc: SrcLoc,4028 src_loc: SrcLoc,
...@@ -4488,81 +4040,6 @@ pub fn errNoteNonLazy(...@@ -4488,81 +4040,6 @@ pub fn errNoteNonLazy(
4488 };4040 };
4489}4041}
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
4566pub fn optionalType(arena: *Allocator, child_type: Type) Allocator.Error!Type {4043pub fn optionalType(arena: *Allocator, child_type: Type) Allocator.Error!Type {
4567 switch (child_type.tag()) {4044 switch (child_type.tag()) {
4568 .single_const_pointer => return Type.Tag.optional_single_const_pointer.create(4045 .single_const_pointer => return Type.Tag.optional_single_const_pointer.create(
...@@ -4631,7 +4108,7 @@ pub fn optimizeMode(mod: Module) std.builtin.Mode {...@@ -4631,7 +4108,7 @@ pub fn optimizeMode(mod: Module) std.builtin.Mode {
4631 return mod.comp.bin_file.options.optimize_mode;4108 return mod.comp.bin_file.options.optimize_mode;
4632}4109}
46334110
4634fn lockAndClearFileCompileError(mod: *Module, file: *Scope.File) void {4111fn lockAndClearFileCompileError(mod: *Module, file: *File) void {
4635 switch (file.status) {4112 switch (file.status) {
4636 .success_zir, .retryable_failure => {},4113 .success_zir, .retryable_failure => {},
4637 .never_loaded, .parse_failure, .astgen_failure => {4114 .never_loaded, .parse_failure, .astgen_failure => {
...@@ -4666,10 +4143,10 @@ pub const SwitchProngSrc = union(enum) {...@@ -4666,10 +4143,10 @@ pub const SwitchProngSrc = union(enum) {
4666 range_expand: RangeExpand,4143 range_expand: RangeExpand,
4667 ) LazySrcLoc {4144 ) LazySrcLoc {
4668 @setCold(true);4145 @setCold(true);
4669 const tree = decl.namespace.file_scope.getTree(gpa) catch |err| {4146 const tree = decl.getFileScope().getTree(gpa) catch |err| {
4670 // In this case we emit a warning + a less precise source location.4147 // In this case we emit a warning + a less precise source location.
4671 log.warn("unable to load {s}: {s}", .{4148 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),
4673 });4150 });
4674 return LazySrcLoc{ .node_offset = 0 };4151 return LazySrcLoc{ .node_offset = 0 };
4675 };4152 };
...@@ -4778,10 +4255,10 @@ pub const PeerTypeCandidateSrc = union(enum) {...@@ -4778,10 +4255,10 @@ pub const PeerTypeCandidateSrc = union(enum) {
4778 else => {},4255 else => {},
4779 }4256 }
47804257
4781 const tree = decl.namespace.file_scope.getTree(gpa) catch |err| {4258 const tree = decl.getFileScope().getTree(gpa) catch |err| {
4782 // In this case we emit a warning + a less precise source location.4259 // In this case we emit a warning + a less precise source location.
4783 log.warn("unable to load {s}: {s}", .{4260 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),
4785 });4262 });
4786 return LazySrcLoc{ .node_offset = 0 };4263 return LazySrcLoc{ .node_offset = 0 };
4787 };4264 };
...@@ -4824,7 +4301,7 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void {...@@ -4824,7 +4301,7 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void {
48244301
4825 // Remove from the namespace it resides in, preserving declaration order.4302 // Remove from the namespace it resides in, preserving declaration order.
4826 assert(decl.zir_decl_index != 0);4303 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
4829 try mod.clearDecl(decl, &outdated_decls);4306 try mod.clearDecl(decl, &outdated_decls);
4830 decl.destroy(mod);4307 decl.destroy(mod);
...@@ -4890,7 +4367,7 @@ pub fn populateTestFunctions(mod: *Module) !void {...@@ -4890,7 +4367,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
4890 const gpa = mod.gpa;4367 const gpa = mod.gpa;
4891 const builtin_pkg = mod.main_pkg.table.get("builtin").?;4368 const builtin_pkg = mod.main_pkg.table.get("builtin").?;
4892 const builtin_file = (mod.importPkg(builtin_pkg) catch unreachable).file;4369 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;
4894 const decl = builtin_namespace.decls.get("test_functions").?;4371 const decl = builtin_namespace.decls.get("test_functions").?;
4895 var buf: Type.SlicePtrFieldTypeBuffer = undefined;4372 var buf: Type.SlicePtrFieldTypeBuffer = undefined;
4896 const tmp_test_fn_ty = decl.ty.slicePtrFieldType(&buf).elemType();4373 const tmp_test_fn_ty = decl.ty.slicePtrFieldType(&buf).elemType();
...@@ -4903,7 +4380,7 @@ pub fn populateTestFunctions(mod: *Module) !void {...@@ -4903,7 +4380,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
4903 const arena = &new_decl_arena.allocator;4380 const arena = &new_decl_arena.allocator;
49044381
4905 const test_fn_vals = try arena.alloc(Value, mod.test_functions.count());4382 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, .{
4907 .ty = try Type.Tag.array.create(arena, .{4384 .ty = try Type.Tag.array.create(arena, .{
4908 .len = test_fn_vals.len,4385 .len = test_fn_vals.len,
4909 .elem_type = try tmp_test_fn_ty.copy(arena),4386 .elem_type = try tmp_test_fn_ty.copy(arena),
...@@ -4916,7 +4393,7 @@ pub fn populateTestFunctions(mod: *Module) !void {...@@ -4916,7 +4393,7 @@ pub fn populateTestFunctions(mod: *Module) !void {
4916 var name_decl_arena = std.heap.ArenaAllocator.init(gpa);4393 var name_decl_arena = std.heap.ArenaAllocator.init(gpa);
4917 errdefer name_decl_arena.deinit();4394 errdefer name_decl_arena.deinit();
4918 const bytes = try name_decl_arena.allocator.dupe(u8, test_name_slice);4395 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, .{
4920 .ty = try Type.Tag.array_u8.create(&name_decl_arena.allocator, bytes.len),4397 .ty = try Type.Tag.array_u8.create(&name_decl_arena.allocator, bytes.len),
4921 .val = try Value.Tag.bytes.create(&name_decl_arena.allocator, bytes),4398 .val = try Value.Tag.bytes.create(&name_decl_arena.allocator, bytes),
4922 });4399 });
src/Sema.zig+1352-950
...@@ -21,11 +21,9 @@ air_values: std.ArrayListUnmanaged(Value) = .{},...@@ -21,11 +21,9 @@ air_values: std.ArrayListUnmanaged(Value) = .{},
21/// Maps ZIR to AIR.21/// Maps ZIR to AIR.
22inst_map: InstMap = .{},22inst_map: InstMap = .{},
23/// When analyzing an inline function call, owner_decl is the Decl of the caller23/// 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.
25/// This `Decl` owns the arena memory of this `Sema`.25/// This `Decl` owns the arena memory of this `Sema`.
26owner_decl: *Decl,26owner_decl: *Decl,
27/// How to look up decl names.
28namespace: *Scope.Namespace,
29/// For an inline or comptime function call, this will be the root parent function27/// For an inline or comptime function call, this will be the root parent function
30/// which contains the callsite. Corresponds to `owner_decl`.28/// which contains the callsite. Corresponds to `owner_decl`.
31owner_func: ?*Module.Fn,29owner_func: ?*Module.Fn,
...@@ -62,9 +60,6 @@ comptime_args_fn_inst: Zir.Inst.Index = 0,...@@ -62,9 +60,6 @@ comptime_args_fn_inst: Zir.Inst.Index = 0,
62/// extra hash table lookup in the `monomorphed_funcs` set.60/// extra hash table lookup in the `monomorphed_funcs` set.
63/// Sema will set this to null when it takes ownership.61/// Sema will set this to null when it takes ownership.
64preallocated_new_func: ?*Module.Fn = null,62preallocated_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
69const std = @import("std");64const std = @import("std");
70const mem = std.mem;65const mem = std.mem;
...@@ -80,7 +75,7 @@ const Air = @import("Air.zig");...@@ -80,7 +75,7 @@ const Air = @import("Air.zig");
80const Zir = @import("Zir.zig");75const Zir = @import("Zir.zig");
81const Module = @import("Module.zig");76const Module = @import("Module.zig");
82const trace = @import("tracy.zig").trace;77const trace = @import("tracy.zig").trace;
83const Scope = Module.Scope;78const Namespace = Module.Namespace;
84const CompileError = Module.CompileError;79const CompileError = Module.CompileError;
85const SemaError = Module.SemaError;80const SemaError = Module.SemaError;
86const Decl = Module.Decl;81const Decl = Module.Decl;
...@@ -94,6 +89,286 @@ const crash_report = @import("crash_report.zig");...@@ -94,6 +89,286 @@ const crash_report = @import("crash_report.zig");
9489
95pub const InstMap = std.AutoHashMapUnmanaged(Zir.Inst.Index, Air.Inst.Ref);90pub 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
97pub fn deinit(sema: *Sema) void {372pub fn deinit(sema: *Sema) void {
98 const gpa = sema.gpa;373 const gpa = sema.gpa;
99 sema.air_instructions.deinit(gpa);374 sema.air_instructions.deinit(gpa);
...@@ -101,14 +376,13 @@ pub fn deinit(sema: *Sema) void {...@@ -101,14 +376,13 @@ pub fn deinit(sema: *Sema) void {
101 sema.air_values.deinit(gpa);376 sema.air_values.deinit(gpa);
102 sema.inst_map.deinit(gpa);377 sema.inst_map.deinit(gpa);
103 sema.decl_val_table.deinit(gpa);378 sema.decl_val_table.deinit(gpa);
104 sema.types_pending_resolution.deinit(gpa);
105 sema.* = undefined;379 sema.* = undefined;
106}380}
107381
108/// Returns only the result from the body that is specified.382/// Returns only the result from the body that is specified.
109/// Only appropriate to call when it is determined at comptime that this body383/// Only appropriate to call when it is determined at comptime that this body
110/// has no peers.384/// 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 {
112 const break_inst = try sema.analyzeBody(block, body);386 const break_inst = try sema.analyzeBody(block, body);
113 const operand_ref = sema.code.instructions.items(.data)[break_inst].@"break".operand;387 const operand_ref = sema.code.instructions.items(.data)[break_inst].@"break".operand;
114 return sema.resolveInst(operand_ref);388 return sema.resolveInst(operand_ref);
...@@ -131,7 +405,7 @@ const always_noreturn: CompileError!Zir.Inst.Index = @as(Zir.Inst.Index, undefin...@@ -131,7 +405,7 @@ const always_noreturn: CompileError!Zir.Inst.Index = @as(Zir.Inst.Index, undefin
131/// well as the operand. No block scope needs to be created for this strategy.405/// well as the operand. No block scope needs to be created for this strategy.
132pub fn analyzeBody(406pub fn analyzeBody(
133 sema: *Sema,407 sema: *Sema,
134 block: *Scope.Block,408 block: *Block,
135 body: []const Zir.Inst.Index,409 body: []const Zir.Inst.Index,
136) CompileError!Zir.Inst.Index {410) CompileError!Zir.Inst.Index {
137 // No tracy calls here, to avoid interfering with the tail call mechanism.411 // No tracy calls here, to avoid interfering with the tail call mechanism.
...@@ -148,9 +422,9 @@ pub fn analyzeBody(...@@ -148,9 +422,9 @@ pub fn analyzeBody(
148 wip_captures.deinit();422 wip_captures.deinit();
149 };423 };
150424
151 const map = &block.sema.inst_map;425 const map = &sema.inst_map;
152 const tags = block.sema.code.instructions.items(.tag);426 const tags = sema.code.instructions.items(.tag);
153 const datas = block.sema.code.instructions.items(.data);427 const datas = sema.code.instructions.items(.data);
154428
155 var orig_captures: usize = parent_capture_scope.captures.count();429 var orig_captures: usize = parent_capture_scope.captures.count();
156430
...@@ -673,7 +947,7 @@ pub fn analyzeBody(...@@ -673,7 +947,7 @@ pub fn analyzeBody(
673 return result;947 return result;
674}948}
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 {
677 const extended = sema.code.instructions.items(.data)[inst].extended;951 const extended = sema.code.instructions.items(.data)[inst].extended;
678 switch (extended.opcode) {952 switch (extended.opcode) {
679 // zig fmt: off953 // zig fmt: off
...@@ -725,7 +999,7 @@ pub fn resolveInst(sema: *Sema, zir_ref: Zir.Inst.Ref) Air.Inst.Ref {...@@ -725,7 +999,7 @@ pub fn resolveInst(sema: *Sema, zir_ref: Zir.Inst.Ref) Air.Inst.Ref {
725999
726fn resolveConstBool(1000fn resolveConstBool(
727 sema: *Sema,1001 sema: *Sema,
728 block: *Scope.Block,1002 block: *Block,
729 src: LazySrcLoc,1003 src: LazySrcLoc,
730 zir_ref: Zir.Inst.Ref,1004 zir_ref: Zir.Inst.Ref,
731) !bool {1005) !bool {
...@@ -738,7 +1012,7 @@ fn resolveConstBool(...@@ -738,7 +1012,7 @@ fn resolveConstBool(
7381012
739fn resolveConstString(1013fn resolveConstString(
740 sema: *Sema,1014 sema: *Sema,
741 block: *Scope.Block,1015 block: *Block,
742 src: LazySrcLoc,1016 src: LazySrcLoc,
743 zir_ref: Zir.Inst.Ref,1017 zir_ref: Zir.Inst.Ref,
744) ![]u8 {1018) ![]u8 {
...@@ -749,14 +1023,14 @@ fn resolveConstString(...@@ -749,14 +1023,14 @@ fn resolveConstString(
749 return val.toAllocatedBytes(sema.arena);1023 return val.toAllocatedBytes(sema.arena);
750}1024}
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 {
753 const air_inst = sema.resolveInst(zir_ref);1027 const air_inst = sema.resolveInst(zir_ref);
754 return sema.analyzeAsType(block, src, air_inst);1028 return sema.analyzeAsType(block, src, air_inst);
755}1029}
7561030
757fn analyzeAsType(1031fn analyzeAsType(
758 sema: *Sema,1032 sema: *Sema,
759 block: *Scope.Block,1033 block: *Block,
760 src: LazySrcLoc,1034 src: LazySrcLoc,
761 air_inst: Air.Inst.Ref,1035 air_inst: Air.Inst.Ref,
762) !Type {1036) !Type {
...@@ -773,7 +1047,7 @@ fn analyzeAsType(...@@ -773,7 +1047,7 @@ fn analyzeAsType(
773/// Value Tag `generic_poison` causes `error.GenericPoison` to be returned.1047/// Value Tag `generic_poison` causes `error.GenericPoison` to be returned.
774fn resolveValue(1048fn resolveValue(
775 sema: *Sema,1049 sema: *Sema,
776 block: *Scope.Block,1050 block: *Block,
777 src: LazySrcLoc,1051 src: LazySrcLoc,
778 air_ref: Air.Inst.Ref,1052 air_ref: Air.Inst.Ref,
779) CompileError!Value {1053) CompileError!Value {
...@@ -788,7 +1062,7 @@ fn resolveValue(...@@ -788,7 +1062,7 @@ fn resolveValue(
788/// Value Tag `undef` may be returned.1062/// Value Tag `undef` may be returned.
789fn resolveConstMaybeUndefVal(1063fn resolveConstMaybeUndefVal(
790 sema: *Sema,1064 sema: *Sema,
791 block: *Scope.Block,1065 block: *Block,
792 src: LazySrcLoc,1066 src: LazySrcLoc,
793 inst: Air.Inst.Ref,1067 inst: Air.Inst.Ref,
794) CompileError!Value {1068) CompileError!Value {
...@@ -806,7 +1080,7 @@ fn resolveConstMaybeUndefVal(...@@ -806,7 +1080,7 @@ fn resolveConstMaybeUndefVal(
806/// See `resolveValue` for an alternative.1080/// See `resolveValue` for an alternative.
807fn resolveConstValue(1081fn resolveConstValue(
808 sema: *Sema,1082 sema: *Sema,
809 block: *Scope.Block,1083 block: *Block,
810 src: LazySrcLoc,1084 src: LazySrcLoc,
811 air_ref: Air.Inst.Ref,1085 air_ref: Air.Inst.Ref,
812) CompileError!Value {1086) CompileError!Value {
...@@ -825,7 +1099,7 @@ fn resolveConstValue(...@@ -825,7 +1099,7 @@ fn resolveConstValue(
825/// Value Tag `undef` causes this function to return a compile error.1099/// Value Tag `undef` causes this function to return a compile error.
826fn resolveDefinedValue(1100fn resolveDefinedValue(
827 sema: *Sema,1101 sema: *Sema,
828 block: *Scope.Block,1102 block: *Block,
829 src: LazySrcLoc,1103 src: LazySrcLoc,
830 air_ref: Air.Inst.Ref,1104 air_ref: Air.Inst.Ref,
831) CompileError!?Value {1105) CompileError!?Value {
...@@ -843,7 +1117,7 @@ fn resolveDefinedValue(...@@ -843,7 +1117,7 @@ fn resolveDefinedValue(
843/// Value Tag `generic_poison` causes `error.GenericPoison` to be returned.1117/// Value Tag `generic_poison` causes `error.GenericPoison` to be returned.
844fn resolveMaybeUndefVal(1118fn resolveMaybeUndefVal(
845 sema: *Sema,1119 sema: *Sema,
846 block: *Scope.Block,1120 block: *Block,
847 src: LazySrcLoc,1121 src: LazySrcLoc,
848 inst: Air.Inst.Ref,1122 inst: Air.Inst.Ref,
849) CompileError!?Value {1123) CompileError!?Value {
...@@ -858,7 +1132,7 @@ fn resolveMaybeUndefVal(...@@ -858,7 +1132,7 @@ fn resolveMaybeUndefVal(
858/// Returns all Value tags including `variable` and `undef`.1132/// Returns all Value tags including `variable` and `undef`.
859fn resolveMaybeUndefValAllowVariables(1133fn resolveMaybeUndefValAllowVariables(
860 sema: *Sema,1134 sema: *Sema,
861 block: *Scope.Block,1135 block: *Block,
862 src: LazySrcLoc,1136 src: LazySrcLoc,
863 inst: Air.Inst.Ref,1137 inst: Air.Inst.Ref,
864) CompileError!?Value {1138) CompileError!?Value {
...@@ -885,20 +1159,77 @@ fn resolveMaybeUndefValAllowVariables(...@@ -885,20 +1159,77 @@ fn resolveMaybeUndefValAllowVariables(
885 }1159 }
886}1160}
8871161
888fn failWithNeededComptime(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) CompileError {1162fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
889 return sema.mod.fail(&block.base, src, "unable to resolve comptime value", .{});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", .{});
890}1168}
8911169
892fn failWithUseOfUndef(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) CompileError {1170fn failWithDivideByZero(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
893 return sema.mod.fail(&block.base, src, "use of undefined value here causes undefined behavior", .{});1171 return sema.fail(block, src, "division by zero here causes undefined behavior", .{});
894}1172}
8951173
896fn failWithDivideByZero(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) CompileError {1174fn failWithModRemNegative(sema: *Sema, block: *Block, src: LazySrcLoc, lhs_ty: Type, rhs_ty: Type) CompileError {
897 return sema.mod.fail(&block.base, src, "division by zero here causes undefined behavior", .{});1175 return sema.fail(block, src, "remainder division with '{}' and '{}': signed integers and floats must use @rem or @mod", .{ lhs_ty, rhs_ty });
898}1176}
8991177
900fn failWithModRemNegative(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, lhs_ty: Type, rhs_ty: Type) CompileError {1178/// We don't return a pointer to the new error note because the pointer
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 });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;
902}1233}
9031234
904/// Appropriate to call when the coercion has already been done by result1235/// 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...@@ -907,7 +1238,7 @@ fn failWithModRemNegative(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, lhs
907/// TODO don't ever call this since we're migrating towards ResultLoc.coerced_ty.1238/// TODO don't ever call this since we're migrating towards ResultLoc.coerced_ty.
908fn resolveAlreadyCoercedInt(1239fn resolveAlreadyCoercedInt(
909 sema: *Sema,1240 sema: *Sema,
910 block: *Scope.Block,1241 block: *Block,
911 src: LazySrcLoc,1242 src: LazySrcLoc,
912 zir_ref: Zir.Inst.Ref,1243 zir_ref: Zir.Inst.Ref,
913 comptime Int: type,1244 comptime Int: type,
...@@ -923,15 +1254,15 @@ fn resolveAlreadyCoercedInt(...@@ -923,15 +1254,15 @@ fn resolveAlreadyCoercedInt(
9231254
924fn resolveAlign(1255fn resolveAlign(
925 sema: *Sema,1256 sema: *Sema,
926 block: *Scope.Block,1257 block: *Block,
927 src: LazySrcLoc,1258 src: LazySrcLoc,
928 zir_ref: Zir.Inst.Ref,1259 zir_ref: Zir.Inst.Ref,
929) !u16 {1260) !u16 {
930 const alignment_big = try sema.resolveInt(block, src, zir_ref, Type.initTag(.u16));1261 const alignment_big = try sema.resolveInt(block, src, zir_ref, Type.initTag(.u16));
931 const alignment = @intCast(u16, alignment_big); // We coerce to u16 in the prev line.1262 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", .{});
933 if (!std.math.isPowerOfTwo(alignment)) {1264 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", .{
935 alignment,1266 alignment,
936 });1267 });
937 }1268 }
...@@ -940,7 +1271,7 @@ fn resolveAlign(...@@ -940,7 +1271,7 @@ fn resolveAlign(
9401271
941fn resolveInt(1272fn resolveInt(
942 sema: *Sema,1273 sema: *Sema,
943 block: *Scope.Block,1274 block: *Block,
944 src: LazySrcLoc,1275 src: LazySrcLoc,
945 zir_ref: Zir.Inst.Ref,1276 zir_ref: Zir.Inst.Ref,
946 dest_type: Type,1277 dest_type: Type,
...@@ -956,7 +1287,7 @@ fn resolveInt(...@@ -956,7 +1287,7 @@ fn resolveInt(
956// a function that does not.1287// a function that does not.
957pub fn resolveInstConst(1288pub fn resolveInstConst(
958 sema: *Sema,1289 sema: *Sema,
959 block: *Scope.Block,1290 block: *Block,
960 src: LazySrcLoc,1291 src: LazySrcLoc,
961 zir_ref: Zir.Inst.Ref,1292 zir_ref: Zir.Inst.Ref,
962) CompileError!TypedValue {1293) CompileError!TypedValue {
...@@ -972,7 +1303,7 @@ pub fn resolveInstConst(...@@ -972,7 +1303,7 @@ pub fn resolveInstConst(
972// See `resolveInstConst` for an alternative.1303// See `resolveInstConst` for an alternative.
973pub fn resolveInstValue(1304pub fn resolveInstValue(
974 sema: *Sema,1305 sema: *Sema,
975 block: *Scope.Block,1306 block: *Block,
976 src: LazySrcLoc,1307 src: LazySrcLoc,
977 zir_ref: Zir.Inst.Ref,1308 zir_ref: Zir.Inst.Ref,
978) CompileError!TypedValue {1309) CompileError!TypedValue {
...@@ -984,13 +1315,13 @@ pub fn resolveInstValue(...@@ -984,13 +1315,13 @@ pub fn resolveInstValue(
984 };1315 };
985}1316}
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 {
988 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;1319 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
989 const src = inst_data.src();1320 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", .{});
991}1322}
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 {
994 const tracy = trace(@src());1325 const tracy = trace(@src());
995 defer tracy.end();1326 defer tracy.end();
9961327
...@@ -1052,7 +1383,7 @@ pub fn analyzeStructDecl(...@@ -1052,7 +1383,7 @@ pub fn analyzeStructDecl(
10521383
1053fn zirStructDecl(1384fn zirStructDecl(
1054 sema: *Sema,1385 sema: *Sema,
1055 block: *Scope.Block,1386 block: *Block,
1056 extended: Zir.Inst.Extended.InstData,1387 extended: Zir.Inst.Extended.InstData,
1057 inst: Zir.Inst.Index,1388 inst: Zir.Inst.Index,
1058) CompileError!Air.Inst.Ref {1389) CompileError!Air.Inst.Ref {
...@@ -1069,12 +1400,12 @@ fn zirStructDecl(...@@ -1069,12 +1400,12 @@ fn zirStructDecl(
1069 const struct_ty = try Type.Tag.@"struct".create(&new_decl_arena.allocator, struct_obj);1400 const struct_ty = try Type.Tag.@"struct".create(&new_decl_arena.allocator, struct_obj);
1070 const struct_val = try Value.Tag.ty.create(&new_decl_arena.allocator, struct_ty);1401 const struct_val = try Value.Tag.ty.create(&new_decl_arena.allocator, struct_ty);
1071 const type_name = try sema.createTypeName(block, small.name_strategy);1402 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, .{
1073 .ty = Type.initTag(.type),1404 .ty = Type.initTag(.type),
1074 .val = struct_val,1405 .val = struct_val,
1075 }, type_name);1406 }, type_name);
1076 new_decl.owns_tv = true;1407 new_decl.owns_tv = true;
1077 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);1408 errdefer sema.mod.abortAnonDecl(new_decl);
1078 struct_obj.* = .{1409 struct_obj.* = .{
1079 .owner_decl = new_decl,1410 .owner_decl = new_decl,
1080 .fields = .{},1411 .fields = .{},
...@@ -1084,7 +1415,7 @@ fn zirStructDecl(...@@ -1084,7 +1415,7 @@ fn zirStructDecl(
1084 .status = .none,1415 .status = .none,
1085 .known_has_bits = undefined,1416 .known_has_bits = undefined,
1086 .namespace = .{1417 .namespace = .{
1087 .parent = sema.owner_decl.namespace,1418 .parent = block.namespace,
1088 .ty = struct_ty,1419 .ty = struct_ty,
1089 .file_scope = block.getFileScope(),1420 .file_scope = block.getFileScope(),
1090 },1421 },
...@@ -1093,13 +1424,11 @@ fn zirStructDecl(...@@ -1093,13 +1424,11 @@ fn zirStructDecl(
1093 &struct_obj.namespace, new_decl, new_decl.name,1424 &struct_obj.namespace, new_decl, new_decl.name,
1094 });1425 });
1095 try sema.analyzeStructDecl(new_decl, inst, struct_obj);1426 try sema.analyzeStructDecl(new_decl, inst, struct_obj);
1096 try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1);
1097 try new_decl.finalizeNewArena(&new_decl_arena);1427 try new_decl.finalizeNewArena(&new_decl_arena);
1098 sema.types_pending_resolution.appendAssumeCapacity(struct_ty);
1099 return sema.analyzeDeclVal(block, src, new_decl);1428 return sema.analyzeDeclVal(block, src, new_decl);
1100}1429}
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 {
1103 switch (name_strategy) {1432 switch (name_strategy) {
1104 .anon => {1433 .anon => {
1105 // It would be neat to have "struct:line:column" but this name has1434 // 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...@@ -1127,7 +1456,7 @@ fn createTypeName(sema: *Sema, block: *Scope.Block, name_strategy: Zir.Inst.Name
11271456
1128fn zirEnumDecl(1457fn zirEnumDecl(
1129 sema: *Sema,1458 sema: *Sema,
1130 block: *Scope.Block,1459 block: *Block,
1131 extended: Zir.Inst.Extended.InstData,1460 extended: Zir.Inst.Extended.InstData,
1132) CompileError!Air.Inst.Ref {1461) CompileError!Air.Inst.Ref {
1133 const tracy = trace(@src());1462 const tracy = trace(@src());
...@@ -1180,12 +1509,12 @@ fn zirEnumDecl(...@@ -1180,12 +1509,12 @@ fn zirEnumDecl(
1180 const enum_ty = Type.initPayload(&enum_ty_payload.base);1509 const enum_ty = Type.initPayload(&enum_ty_payload.base);
1181 const enum_val = try Value.Tag.ty.create(&new_decl_arena.allocator, enum_ty);1510 const enum_val = try Value.Tag.ty.create(&new_decl_arena.allocator, enum_ty);
1182 const type_name = try sema.createTypeName(block, small.name_strategy);1511 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, .{
1184 .ty = Type.initTag(.type),1513 .ty = Type.initTag(.type),
1185 .val = enum_val,1514 .val = enum_val,
1186 }, type_name);1515 }, type_name);
1187 new_decl.owns_tv = true;1516 new_decl.owns_tv = true;
1188 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);1517 errdefer mod.abortAnonDecl(new_decl);
11891518
1190 enum_obj.* = .{1519 enum_obj.* = .{
1191 .owner_decl = new_decl,1520 .owner_decl = new_decl,
...@@ -1194,7 +1523,7 @@ fn zirEnumDecl(...@@ -1194,7 +1523,7 @@ fn zirEnumDecl(
1194 .values = .{},1523 .values = .{},
1195 .node_offset = src.node_offset,1524 .node_offset = src.node_offset,
1196 .namespace = .{1525 .namespace = .{
1197 .parent = sema.owner_decl.namespace,1526 .parent = block.namespace,
1198 .ty = enum_ty,1527 .ty = enum_ty,
1199 .file_scope = block.getFileScope(),1528 .file_scope = block.getFileScope(),
1200 },1529 },
...@@ -1227,10 +1556,6 @@ fn zirEnumDecl(...@@ -1227,10 +1556,6 @@ fn zirEnumDecl(
1227 sema.owner_decl = new_decl;1556 sema.owner_decl = new_decl;
1228 defer sema.owner_decl = prev_owner_decl;1557 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
1234 const prev_owner_func = sema.owner_func;1559 const prev_owner_func = sema.owner_func;
1235 sema.owner_func = null;1560 sema.owner_func = null;
1236 defer sema.owner_func = prev_owner_func;1561 defer sema.owner_func = prev_owner_func;
...@@ -1242,10 +1567,11 @@ fn zirEnumDecl(...@@ -1242,10 +1567,11 @@ fn zirEnumDecl(
1242 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, new_decl.src_scope);1567 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, new_decl.src_scope);
1243 defer wip_captures.deinit();1568 defer wip_captures.deinit();
12441569
1245 var enum_block: Scope.Block = .{1570 var enum_block: Block = .{
1246 .parent = null,1571 .parent = null,
1247 .sema = sema,1572 .sema = sema,
1248 .src_decl = new_decl,1573 .src_decl = new_decl,
1574 .namespace = &enum_obj.namespace,
1249 .wip_capture_scope = wip_captures.scope,1575 .wip_capture_scope = wip_captures.scope,
1250 .instructions = .{},1576 .instructions = .{},
1251 .inlining = null,1577 .inlining = null,
...@@ -1303,12 +1629,12 @@ fn zirEnumDecl(...@@ -1303,12 +1629,12 @@ fn zirEnumDecl(
1303 const field_src = enumFieldSrcLoc(block.src_decl, tree.*, src.node_offset, field_i);1629 const field_src = enumFieldSrcLoc(block.src_decl, tree.*, src.node_offset, field_i);
1304 const other_tag_src = enumFieldSrcLoc(block.src_decl, tree.*, src.node_offset, gop.index);1630 const other_tag_src = enumFieldSrcLoc(block.src_decl, tree.*, src.node_offset, gop.index);
1305 const msg = msg: {1631 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", .{});
1307 errdefer msg.destroy(gpa);1633 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", .{});
1309 break :msg msg;1635 break :msg msg;
1310 };1636 };
1311 return mod.failWithOwnedErrorMsg(&block.base, msg);1637 return sema.failWithOwnedErrorMsg(msg);
1312 }1638 }
13131639
1314 if (has_tag_value) {1640 if (has_tag_value) {
...@@ -1331,7 +1657,7 @@ fn zirEnumDecl(...@@ -1331,7 +1657,7 @@ fn zirEnumDecl(
13311657
1332fn zirUnionDecl(1658fn zirUnionDecl(
1333 sema: *Sema,1659 sema: *Sema,
1334 block: *Scope.Block,1660 block: *Block,
1335 extended: Zir.Inst.Extended.InstData,1661 extended: Zir.Inst.Extended.InstData,
1336 inst: Zir.Inst.Index,1662 inst: Zir.Inst.Index,
1337) CompileError!Air.Inst.Ref {1663) CompileError!Air.Inst.Ref {
...@@ -1370,12 +1696,12 @@ fn zirUnionDecl(...@@ -1370,12 +1696,12 @@ fn zirUnionDecl(
1370 const union_ty = Type.initPayload(&union_payload.base);1696 const union_ty = Type.initPayload(&union_payload.base);
1371 const union_val = try Value.Tag.ty.create(&new_decl_arena.allocator, union_ty);1697 const union_val = try Value.Tag.ty.create(&new_decl_arena.allocator, union_ty);
1372 const type_name = try sema.createTypeName(block, small.name_strategy);1698 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, .{
1374 .ty = Type.initTag(.type),1700 .ty = Type.initTag(.type),
1375 .val = union_val,1701 .val = union_val,
1376 }, type_name);1702 }, type_name);
1377 new_decl.owns_tv = true;1703 new_decl.owns_tv = true;
1378 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);1704 errdefer sema.mod.abortAnonDecl(new_decl);
1379 union_obj.* = .{1705 union_obj.* = .{
1380 .owner_decl = new_decl,1706 .owner_decl = new_decl,
1381 .tag_ty = Type.initTag(.@"null"),1707 .tag_ty = Type.initTag(.@"null"),
...@@ -1385,7 +1711,7 @@ fn zirUnionDecl(...@@ -1385,7 +1711,7 @@ fn zirUnionDecl(
1385 .layout = small.layout,1711 .layout = small.layout,
1386 .status = .none,1712 .status = .none,
1387 .namespace = .{1713 .namespace = .{
1388 .parent = sema.owner_decl.namespace,1714 .parent = block.namespace,
1389 .ty = union_ty,1715 .ty = union_ty,
1390 .file_scope = block.getFileScope(),1716 .file_scope = block.getFileScope(),
1391 },1717 },
...@@ -1396,15 +1722,13 @@ fn zirUnionDecl(...@@ -1396,15 +1722,13 @@ fn zirUnionDecl(
13961722
1397 _ = try sema.mod.scanNamespace(&union_obj.namespace, extra_index, decls_len, new_decl);1723 _ = try sema.mod.scanNamespace(&union_obj.namespace, extra_index, decls_len, new_decl);
13981724
1399 try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1);
1400 try new_decl.finalizeNewArena(&new_decl_arena);1725 try new_decl.finalizeNewArena(&new_decl_arena);
1401 sema.types_pending_resolution.appendAssumeCapacity(union_ty);
1402 return sema.analyzeDeclVal(block, src, new_decl);1726 return sema.analyzeDeclVal(block, src, new_decl);
1403}1727}
14041728
1405fn zirOpaqueDecl(1729fn zirOpaqueDecl(
1406 sema: *Sema,1730 sema: *Sema,
1407 block: *Scope.Block,1731 block: *Block,
1408 extended: Zir.Inst.Extended.InstData,1732 extended: Zir.Inst.Extended.InstData,
1409 inst: Zir.Inst.Index,1733 inst: Zir.Inst.Index,
1410) CompileError!Air.Inst.Ref {1734) CompileError!Air.Inst.Ref {
...@@ -1413,12 +1737,12 @@ fn zirOpaqueDecl(...@@ -1413,12 +1737,12 @@ fn zirOpaqueDecl(
14131737
1414 _ = extended;1738 _ = extended;
1415 _ = inst;1739 _ = inst;
1416 return sema.mod.fail(&block.base, sema.src, "TODO implement zirOpaqueDecl", .{});1740 return sema.fail(block, sema.src, "TODO implement zirOpaqueDecl", .{});
1417}1741}
14181742
1419fn zirErrorSetDecl(1743fn zirErrorSetDecl(
1420 sema: *Sema,1744 sema: *Sema,
1421 block: *Scope.Block,1745 block: *Block,
1422 inst: Zir.Inst.Index,1746 inst: Zir.Inst.Index,
1423 name_strategy: Zir.Inst.NameStrategy,1747 name_strategy: Zir.Inst.NameStrategy,
1424) CompileError!Air.Inst.Ref {1748) CompileError!Air.Inst.Ref {
...@@ -1438,12 +1762,12 @@ fn zirErrorSetDecl(...@@ -1438,12 +1762,12 @@ fn zirErrorSetDecl(
1438 const error_set_ty = try Type.Tag.error_set.create(&new_decl_arena.allocator, error_set);1762 const error_set_ty = try Type.Tag.error_set.create(&new_decl_arena.allocator, error_set);
1439 const error_set_val = try Value.Tag.ty.create(&new_decl_arena.allocator, error_set_ty);1763 const error_set_val = try Value.Tag.ty.create(&new_decl_arena.allocator, error_set_ty);
1440 const type_name = try sema.createTypeName(block, name_strategy);1764 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, .{
1442 .ty = Type.initTag(.type),1766 .ty = Type.initTag(.type),
1443 .val = error_set_val,1767 .val = error_set_val,
1444 }, type_name);1768 }, type_name);
1445 new_decl.owns_tv = true;1769 new_decl.owns_tv = true;
1446 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);1770 errdefer sema.mod.abortAnonDecl(new_decl);
1447 const names = try new_decl_arena.allocator.alloc([]const u8, fields.len);1771 const names = try new_decl_arena.allocator.alloc([]const u8, fields.len);
1448 for (fields) |str_index, i| {1772 for (fields) |str_index, i| {
1449 names[i] = try new_decl_arena.allocator.dupe(u8, sema.code.nullTerminatedString(str_index));1773 names[i] = try new_decl_arena.allocator.dupe(u8, sema.code.nullTerminatedString(str_index));
...@@ -1460,7 +1784,7 @@ fn zirErrorSetDecl(...@@ -1460,7 +1784,7 @@ fn zirErrorSetDecl(
14601784
1461fn zirRetPtr(1785fn zirRetPtr(
1462 sema: *Sema,1786 sema: *Sema,
1463 block: *Scope.Block,1787 block: *Block,
1464 extended: Zir.Inst.Extended.InstData,1788 extended: Zir.Inst.Extended.InstData,
1465) CompileError!Air.Inst.Ref {1789) CompileError!Air.Inst.Ref {
1466 const tracy = trace(@src());1790 const tracy = trace(@src());
...@@ -1480,7 +1804,7 @@ fn zirRetPtr(...@@ -1480,7 +1804,7 @@ fn zirRetPtr(
1480 return block.addTy(.alloc, ptr_type);1804 return block.addTy(.alloc, ptr_type);
1481}1805}
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 {
1484 const tracy = trace(@src());1808 const tracy = trace(@src());
1485 defer tracy.end();1809 defer tracy.end();
14861810
...@@ -1491,7 +1815,7 @@ fn zirRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A...@@ -1491,7 +1815,7 @@ fn zirRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
14911815
1492fn zirRetType(1816fn zirRetType(
1493 sema: *Sema,1817 sema: *Sema,
1494 block: *Scope.Block,1818 block: *Block,
1495 extended: Zir.Inst.Extended.InstData,1819 extended: Zir.Inst.Extended.InstData,
1496) CompileError!Air.Inst.Ref {1820) CompileError!Air.Inst.Ref {
1497 const tracy = trace(@src());1821 const tracy = trace(@src());
...@@ -1502,7 +1826,7 @@ fn zirRetType(...@@ -1502,7 +1826,7 @@ fn zirRetType(
1502 return sema.addType(sema.fn_ret_ty);1826 return sema.addType(sema.fn_ret_ty);
1503}1827}
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 {
1506 const tracy = trace(@src());1830 const tracy = trace(@src());
1507 defer tracy.end();1831 defer tracy.end();
15081832
...@@ -1515,18 +1839,18 @@ fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) C...@@ -1515,18 +1839,18 @@ fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) C
15151839
1516fn ensureResultUsed(1840fn ensureResultUsed(
1517 sema: *Sema,1841 sema: *Sema,
1518 block: *Scope.Block,1842 block: *Block,
1519 operand: Air.Inst.Ref,1843 operand: Air.Inst.Ref,
1520 src: LazySrcLoc,1844 src: LazySrcLoc,
1521) CompileError!void {1845) CompileError!void {
1522 const operand_ty = sema.typeOf(operand);1846 const operand_ty = sema.typeOf(operand);
1523 switch (operand_ty.zigTypeTag()) {1847 switch (operand_ty.zigTypeTag()) {
1524 .Void, .NoReturn => return,1848 .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", .{}),
1526 }1850 }
1527}1851}
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 {
1530 const tracy = trace(@src());1854 const tracy = trace(@src());
1531 defer tracy.end();1855 defer tracy.end();
15321856
...@@ -1535,12 +1859,12 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde...@@ -1535,12 +1859,12 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
1535 const src = inst_data.src();1859 const src = inst_data.src();
1536 const operand_ty = sema.typeOf(operand);1860 const operand_ty = sema.typeOf(operand);
1537 switch (operand_ty.zigTypeTag()) {1861 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", .{}),
1539 else => return,1863 else => return,
1540 }1864 }
1541}1865}
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 {
1544 const tracy = trace(@src());1868 const tracy = trace(@src());
1545 defer tracy.end();1869 defer tracy.end();
15461870
...@@ -1561,15 +1885,15 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co...@@ -1561,15 +1885,15 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
1561 }1885 }
1562 if (!elem_ty.isIndexable()) {1886 if (!elem_ty.isIndexable()) {
1563 const msg = msg: {1887 const msg = msg: {
1564 const msg = try sema.mod.errMsg(1888 const msg = try sema.errMsg(
1565 &block.base,1889 block,
1566 src,1890 src,
1567 "type '{}' does not support indexing",1891 "type '{}' does not support indexing",
1568 .{elem_ty},1892 .{elem_ty},
1569 );1893 );
1570 errdefer msg.destroy(sema.gpa);1894 errdefer msg.destroy(sema.gpa);
1571 try sema.mod.errNote(1895 try sema.errNote(
1572 &block.base,1896 block,
1573 src,1897 src,
1574 msg,1898 msg,
1575 "for loop operand must be an array, slice, tuple, or vector",1899 "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...@@ -1577,18 +1901,18 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
1577 );1901 );
1578 break :msg msg;1902 break :msg msg;
1579 };1903 };
1580 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);1904 return sema.failWithOwnedErrorMsg(msg);
1581 }1905 }
1582 const result_ptr = try sema.fieldPtr(block, src, array, "len", src);1906 const result_ptr = try sema.fieldPtr(block, src, array, "len", src);
1583 return sema.analyzeLoad(block, src, result_ptr, src);1907 return sema.analyzeLoad(block, src, result_ptr, src);
1584 }1908 }
15851909
1586 return sema.mod.fail(&block.base, src, "TODO implement Sema.zirIndexablePtrLen", .{});1910 return sema.fail(block, src, "TODO implement Sema.zirIndexablePtrLen", .{});
1587}1911}
15881912
1589fn zirAllocExtended(1913fn zirAllocExtended(
1590 sema: *Sema,1914 sema: *Sema,
1591 block: *Scope.Block,1915 block: *Block,
1592 extended: Zir.Inst.Extended.InstData,1916 extended: Zir.Inst.Extended.InstData,
1593) CompileError!Air.Inst.Ref {1917) CompileError!Air.Inst.Ref {
1594 const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand);1918 const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand);
...@@ -1604,7 +1928,7 @@ fn zirAllocExtended(...@@ -1604,7 +1928,7 @@ fn zirAllocExtended(
1604 extra_index += 1;1928 extra_index += 1;
1605 break :blk try sema.resolveType(block, ty_src, type_ref);1929 break :blk try sema.resolveType(block, ty_src, type_ref);
1606 } else {1930 } 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", .{});
1608 };1932 };
16091933
1610 const alignment: u16 = if (small.has_align) blk: {1934 const alignment: u16 = if (small.has_align) blk: {
...@@ -1615,11 +1939,11 @@ fn zirAllocExtended(...@@ -1615,11 +1939,11 @@ fn zirAllocExtended(
1615 } else 0;1939 } else 0;
16161940
1617 if (small.is_comptime) {1941 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", .{});
1619 }1943 }
16201944
1621 if (!small.is_const) {1945 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", .{});
1623 }1947 }
16241948
1625 const ptr_type = try Type.ptr(sema.arena, .{1949 const ptr_type = try Type.ptr(sema.arena, .{
...@@ -1631,7 +1955,7 @@ fn zirAllocExtended(...@@ -1631,7 +1955,7 @@ fn zirAllocExtended(
1631 return block.addTy(.alloc, ptr_type);1955 return block.addTy(.alloc, ptr_type);
1632}1956}
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 {
1635 const tracy = trace(@src());1959 const tracy = trace(@src());
1636 defer tracy.end();1960 defer tracy.end();
16371961
...@@ -1651,7 +1975,7 @@ fn zirAllocInferredComptime(sema: *Sema, inst: Zir.Inst.Index) CompileError!Air....@@ -1651,7 +1975,7 @@ fn zirAllocInferredComptime(sema: *Sema, inst: Zir.Inst.Index) CompileError!Air.
1651 );1975 );
1652}1976}
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 {
1655 const tracy = trace(@src());1979 const tracy = trace(@src());
1656 defer tracy.end();1980 defer tracy.end();
16571981
...@@ -1667,7 +1991,7 @@ fn zirAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError...@@ -1667,7 +1991,7 @@ fn zirAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
1667 return block.addTy(.alloc, ptr_type);1991 return block.addTy(.alloc, ptr_type);
1668}1992}
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 {
1671 const tracy = trace(@src());1995 const tracy = trace(@src());
1672 defer tracy.end();1996 defer tracy.end();
16731997
...@@ -1689,7 +2013,7 @@ fn zirAllocMut(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -1689,7 +2013,7 @@ fn zirAllocMut(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
16892013
1690fn zirAllocInferred(2014fn zirAllocInferred(
1691 sema: *Sema,2015 sema: *Sema,
1692 block: *Scope.Block,2016 block: *Block,
1693 inst: Zir.Inst.Index,2017 inst: Zir.Inst.Index,
1694 inferred_alloc_ty: Type,2018 inferred_alloc_ty: Type,
1695) CompileError!Air.Inst.Ref {2019) CompileError!Air.Inst.Ref {
...@@ -1720,7 +2044,7 @@ fn zirAllocInferred(...@@ -1720,7 +2044,7 @@ fn zirAllocInferred(
1720 return result;2044 return result;
1721}2045}
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 {
1724 const tracy = trace(@src());2048 const tracy = trace(@src());
1725 defer tracy.end();2049 defer tracy.end();
17262050
...@@ -1779,7 +2103,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde...@@ -1779,7 +2103,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
1779 }2103 }
1780}2104}
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 {
1783 const tracy = trace(@src());2107 const tracy = trace(@src());
1784 defer tracy.end();2108 defer tracy.end();
17852109
...@@ -1811,18 +2135,16 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Ind...@@ -1811,18 +2135,16 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Ind
18112135
1812fn validateUnionInitPtr(2136fn validateUnionInitPtr(
1813 sema: *Sema,2137 sema: *Sema,
1814 block: *Scope.Block,2138 block: *Block,
1815 union_obj: *Module.Union,2139 union_obj: *Module.Union,
1816 init_src: LazySrcLoc,2140 init_src: LazySrcLoc,
1817 instrs: []const Zir.Inst.Index,2141 instrs: []const Zir.Inst.Index,
1818 union_ptr: Air.Inst.Ref,2142 union_ptr: Air.Inst.Ref,
1819) CompileError!void {2143) CompileError!void {
1820 const mod = sema.mod;
1821
1822 if (instrs.len != 1) {2144 if (instrs.len != 1) {
1823 // TODO add note for other field2145 // TODO add note for other field
1824 // TODO add note for union declared here2146 // 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", .{});
1826 }2148 }
18272149
1828 const field_ptr = instrs[0];2150 const field_ptr = instrs[0];
...@@ -1852,13 +2174,12 @@ fn validateUnionInitPtr(...@@ -1852,13 +2174,12 @@ fn validateUnionInitPtr(
18522174
1853fn validateStructInitPtr(2175fn validateStructInitPtr(
1854 sema: *Sema,2176 sema: *Sema,
1855 block: *Scope.Block,2177 block: *Block,
1856 struct_obj: *Module.Struct,2178 struct_obj: *Module.Struct,
1857 init_src: LazySrcLoc,2179 init_src: LazySrcLoc,
1858 instrs: []const Zir.Inst.Index,2180 instrs: []const Zir.Inst.Index,
1859) CompileError!void {2181) CompileError!void {
1860 const gpa = sema.gpa;2182 const gpa = sema.gpa;
1861 const mod = sema.mod;
18622183
1863 // Maps field index to field_ptr index of where it was already initialized.2184 // Maps field index to field_ptr index of where it was already initialized.
1864 const found_fields = try gpa.alloc(Zir.Inst.Index, struct_obj.fields.count());2185 const found_fields = try gpa.alloc(Zir.Inst.Index, struct_obj.fields.count());
...@@ -1877,12 +2198,12 @@ fn validateStructInitPtr(...@@ -1877,12 +2198,12 @@ fn validateStructInitPtr(
1877 const other_field_ptr_data = sema.code.instructions.items(.data)[other_field_ptr].pl_node;2198 const other_field_ptr_data = sema.code.instructions.items(.data)[other_field_ptr].pl_node;
1878 const other_field_src: LazySrcLoc = .{ .node_offset_back2tok = other_field_ptr_data.src_node };2199 const other_field_src: LazySrcLoc = .{ .node_offset_back2tok = other_field_ptr_data.src_node };
1879 const msg = msg: {2200 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", .{});
1881 errdefer msg.destroy(gpa);2202 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", .{});
1883 break :msg msg;2204 break :msg msg;
1884 };2205 };
1885 return mod.failWithOwnedErrorMsg(&block.base, msg);2206 return sema.failWithOwnedErrorMsg(msg);
1886 }2207 }
1887 found_fields[field_index] = field_ptr;2208 found_fields[field_index] = field_ptr;
1888 }2209 }
...@@ -1897,85 +2218,83 @@ fn validateStructInitPtr(...@@ -1897,85 +2218,83 @@ fn validateStructInitPtr(
1897 const template = "missing struct field: {s}";2218 const template = "missing struct field: {s}";
1898 const args = .{field_name};2219 const args = .{field_name};
1899 if (root_msg) |msg| {2220 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);
1901 } else {2222 } else {
1902 root_msg = try mod.errMsg(&block.base, init_src, template, args);2223 root_msg = try sema.errMsg(block, init_src, template, args);
1903 }2224 }
1904 }2225 }
1905 if (root_msg) |msg| {2226 if (root_msg) |msg| {
1906 const fqn = try struct_obj.getFullyQualifiedName(gpa);2227 const fqn = try struct_obj.getFullyQualifiedName(gpa);
1907 defer gpa.free(fqn);2228 defer gpa.free(fqn);
1908 try mod.errNoteNonLazy(2229 try sema.mod.errNoteNonLazy(
1909 struct_obj.srcLoc(),2230 struct_obj.srcLoc(),
1910 msg,2231 msg,
1911 "struct '{s}' declared here",2232 "struct '{s}' declared here",
1912 .{fqn},2233 .{fqn},
1913 );2234 );
1914 return mod.failWithOwnedErrorMsg(&block.base, msg);2235 return sema.failWithOwnedErrorMsg(msg);
1915 }2236 }
1916}2237}
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 {
1919 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;2240 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
1920 const src = inst_data.src();2241 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", .{});
1922}2243}
19232244
1924fn failWithBadFieldAccess(2245fn failWithBadFieldAccess(
1925 sema: *Sema,2246 sema: *Sema,
1926 block: *Scope.Block,2247 block: *Block,
1927 struct_obj: *Module.Struct,2248 struct_obj: *Module.Struct,
1928 field_src: LazySrcLoc,2249 field_src: LazySrcLoc,
1929 field_name: []const u8,2250 field_name: []const u8,
1930) CompileError {2251) CompileError {
1931 const mod = sema.mod;
1932 const gpa = sema.gpa;2252 const gpa = sema.gpa;
19332253
1934 const fqn = try struct_obj.getFullyQualifiedName(gpa);2254 const fqn = try struct_obj.getFullyQualifiedName(gpa);
1935 defer gpa.free(fqn);2255 defer gpa.free(fqn);
19362256
1937 const msg = msg: {2257 const msg = msg: {
1938 const msg = try mod.errMsg(2258 const msg = try sema.errMsg(
1939 &block.base,2259 block,
1940 field_src,2260 field_src,
1941 "no field named '{s}' in struct '{s}'",2261 "no field named '{s}' in struct '{s}'",
1942 .{ field_name, fqn },2262 .{ field_name, fqn },
1943 );2263 );
1944 errdefer msg.destroy(gpa);2264 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", .{});
1946 break :msg msg;2266 break :msg msg;
1947 };2267 };
1948 return mod.failWithOwnedErrorMsg(&block.base, msg);2268 return sema.failWithOwnedErrorMsg(msg);
1949}2269}
19502270
1951fn failWithBadUnionFieldAccess(2271fn failWithBadUnionFieldAccess(
1952 sema: *Sema,2272 sema: *Sema,
1953 block: *Scope.Block,2273 block: *Block,
1954 union_obj: *Module.Union,2274 union_obj: *Module.Union,
1955 field_src: LazySrcLoc,2275 field_src: LazySrcLoc,
1956 field_name: []const u8,2276 field_name: []const u8,
1957) CompileError {2277) CompileError {
1958 const mod = sema.mod;
1959 const gpa = sema.gpa;2278 const gpa = sema.gpa;
19602279
1961 const fqn = try union_obj.getFullyQualifiedName(gpa);2280 const fqn = try union_obj.getFullyQualifiedName(gpa);
1962 defer gpa.free(fqn);2281 defer gpa.free(fqn);
19632282
1964 const msg = msg: {2283 const msg = msg: {
1965 const msg = try mod.errMsg(2284 const msg = try sema.errMsg(
1966 &block.base,2285 block,
1967 field_src,2286 field_src,
1968 "no field named '{s}' in union '{s}'",2287 "no field named '{s}' in union '{s}'",
1969 .{ field_name, fqn },2288 .{ field_name, fqn },
1970 );2289 );
1971 errdefer msg.destroy(gpa);2290 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", .{});
1973 break :msg msg;2292 break :msg msg;
1974 };2293 };
1975 return mod.failWithOwnedErrorMsg(&block.base, msg);2294 return sema.failWithOwnedErrorMsg(msg);
1976}2295}
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 {
1979 const tracy = trace(@src());2298 const tracy = trace(@src());
1980 defer tracy.end();2299 defer tracy.end();
19812300
...@@ -2000,7 +2319,7 @@ fn zirStoreToBlockPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co...@@ -2000,7 +2319,7 @@ fn zirStoreToBlockPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
2000 return sema.storePtr(block, src, bitcasted_ptr, value);2319 return sema.storePtr(block, src, bitcasted_ptr, value);
2001}2320}
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 {
2004 const tracy = trace(@src());2323 const tracy = trace(@src());
2005 defer tracy.end();2324 defer tracy.end();
20062325
...@@ -2048,7 +2367,7 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)...@@ -2048,7 +2367,7 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
2048 unreachable;2367 unreachable;
2049}2368}
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 {
2052 const inst_data = sema.code.instructions.items(.data)[inst].un_node;2371 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2053 const src = inst_data.src();2372 const src = inst_data.src();
2054 const quota = try sema.resolveAlreadyCoercedInt(block, src, inst_data.operand, u32);2373 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)...@@ -2056,7 +2375,7 @@ fn zirSetEvalBranchQuota(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
2056 sema.branch_quota = quota;2375 sema.branch_quota = quota;
2057}2376}
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 {
2060 const tracy = trace(@src());2379 const tracy = trace(@src());
2061 defer tracy.end();2380 defer tracy.end();
20622381
...@@ -2066,7 +2385,7 @@ fn zirStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError...@@ -2066,7 +2385,7 @@ fn zirStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
2066 return sema.storePtr(block, sema.src, ptr, value);2385 return sema.storePtr(block, sema.src, ptr, value);
2067}2386}
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 {
2070 const tracy = trace(@src());2389 const tracy = trace(@src());
2071 defer tracy.end();2390 defer tracy.end();
20722391
...@@ -2078,7 +2397,7 @@ fn zirStoreNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -2078,7 +2397,7 @@ fn zirStoreNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
2078 return sema.storePtr(block, src, ptr, value);2397 return sema.storePtr(block, src, ptr, value);
2079}2398}
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 {
2082 const tracy = trace(@src());2401 const tracy = trace(@src());
2083 defer tracy.end();2402 defer tracy.end();
20842403
...@@ -2097,16 +2416,16 @@ fn zirStr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A...@@ -2097,16 +2416,16 @@ fn zirStr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
2097 const decl_ty = try Type.Tag.array_u8_sentinel_0.create(&new_decl_arena.allocator, bytes.len);2416 const decl_ty = try Type.Tag.array_u8_sentinel_0.create(&new_decl_arena.allocator, bytes.len);
2098 const decl_val = try Value.Tag.bytes.create(&new_decl_arena.allocator, bytes);2417 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, .{
2101 .ty = decl_ty,2420 .ty = decl_ty,
2102 .val = decl_val,2421 .val = decl_val,
2103 });2422 });
2104 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);2423 errdefer sema.mod.abortAnonDecl(new_decl);
2105 try new_decl.finalizeNewArena(&new_decl_arena);2424 try new_decl.finalizeNewArena(&new_decl_arena);
2106 return sema.analyzeDeclRef(new_decl);2425 return sema.analyzeDeclRef(new_decl);
2107}2426}
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 {
2110 _ = block;2429 _ = block;
2111 const tracy = trace(@src());2430 const tracy = trace(@src());
2112 defer tracy.end();2431 defer tracy.end();
...@@ -2115,7 +2434,7 @@ fn zirInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A...@@ -2115,7 +2434,7 @@ fn zirInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
2115 return sema.addIntUnsigned(Type.initTag(.comptime_int), int);2434 return sema.addIntUnsigned(Type.initTag(.comptime_int), int);
2116}2435}
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 {
2119 _ = block;2438 _ = block;
2120 const tracy = trace(@src());2439 const tracy = trace(@src());
2121 defer tracy.end();2440 defer tracy.end();
...@@ -2133,7 +2452,7 @@ fn zirIntBig(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -2133,7 +2452,7 @@ fn zirIntBig(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
2133 );2452 );
2134}2453}
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 {
2137 _ = block;2456 _ = block;
2138 const arena = sema.arena;2457 const arena = sema.arena;
2139 const number = sema.code.instructions.items(.data)[inst].float;2458 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...@@ -2143,7 +2462,7 @@ fn zirFloat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
2143 );2462 );
2144}2463}
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 {
2147 _ = block;2466 _ = block;
2148 const arena = sema.arena;2467 const arena = sema.arena;
2149 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;2468 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...@@ -2155,7 +2474,7 @@ fn zirFloat128(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
2155 );2474 );
2156}2475}
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 {
2159 const tracy = trace(@src());2478 const tracy = trace(@src());
2160 defer tracy.end();2479 defer tracy.end();
21612480
...@@ -2163,12 +2482,12 @@ fn zirCompileError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi...@@ -2163,12 +2482,12 @@ fn zirCompileError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
2163 const src = inst_data.src();2482 const src = inst_data.src();
2164 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };2483 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
2165 const msg = try sema.resolveConstString(block, operand_src, inst_data.operand);2484 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});
2167}2486}
21682487
2169fn zirCompileLog(2488fn zirCompileLog(
2170 sema: *Sema,2489 sema: *Sema,
2171 block: *Scope.Block,2490 block: *Block,
2172 extended: Zir.Inst.Extended.InstData,2491 extended: Zir.Inst.Extended.InstData,
2173) CompileError!Air.Inst.Ref {2492) CompileError!Air.Inst.Ref {
2174 var managed = sema.mod.compile_log_text.toManaged(sema.gpa);2493 var managed = sema.mod.compile_log_text.toManaged(sema.gpa);
...@@ -2200,7 +2519,7 @@ fn zirCompileLog(...@@ -2200,7 +2519,7 @@ fn zirCompileLog(
2200 return Air.Inst.Ref.void_value;2519 return Air.Inst.Ref.void_value;
2201}2520}
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 {
2204 const inst_data = sema.code.instructions.items(.data)[inst].un_node;2523 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2205 const src: LazySrcLoc = inst_data.src();2524 const src: LazySrcLoc = inst_data.src();
2206 const msg_inst = sema.resolveInst(inst_data.operand);2525 const msg_inst = sema.resolveInst(inst_data.operand);
...@@ -2208,7 +2527,7 @@ fn zirPanic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError...@@ -2208,7 +2527,7 @@ fn zirPanic(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
2208 return sema.panicWithMsg(block, src, msg_inst);2527 return sema.panicWithMsg(block, src, msg_inst);
2209}2528}
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 {
2212 const tracy = trace(@src());2531 const tracy = trace(@src());
2213 defer tracy.end();2532 defer tracy.end();
22142533
...@@ -2236,7 +2555,7 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -2236,7 +2555,7 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Compil
2236 .payload = undefined,2555 .payload = undefined,
2237 } },2556 } },
2238 });2557 });
2239 var label: Scope.Block.Label = .{2558 var label: Block.Label = .{
2240 .zir_block = inst,2559 .zir_block = inst,
2241 .merges = .{2560 .merges = .{
2242 .results = .{},2561 .results = .{},
...@@ -2271,7 +2590,7 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -2271,7 +2590,7 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Compil
2271 return sema.analyzeBlockBody(parent_block, src, &child_block, merges);2590 return sema.analyzeBlockBody(parent_block, src, &child_block, merges);
2272}2591}
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 {
2275 const tracy = trace(@src());2594 const tracy = trace(@src());
2276 defer tracy.end();2595 defer tracy.end();
22772596
...@@ -2282,15 +2601,16 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com...@@ -2282,15 +2601,16 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
22822601
2283 // we check this here to avoid undefined symbols2602 // we check this here to avoid undefined symbols
2284 if (!@import("build_options").have_llvm)2603 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
2287 var c_import_buf = std.ArrayList(u8).init(sema.gpa);2606 var c_import_buf = std.ArrayList(u8).init(sema.gpa);
2288 defer c_import_buf.deinit();2607 defer c_import_buf.deinit();
22892608
2290 var child_block: Scope.Block = .{2609 var child_block: Block = .{
2291 .parent = parent_block,2610 .parent = parent_block,
2292 .sema = sema,2611 .sema = sema,
2293 .src_decl = parent_block.src_decl,2612 .src_decl = parent_block.src_decl,
2613 .namespace = parent_block.namespace,
2294 .wip_capture_scope = parent_block.wip_capture_scope,2614 .wip_capture_scope = parent_block.wip_capture_scope,
2295 .instructions = .{},2615 .instructions = .{},
2296 .inlining = parent_block.inlining,2616 .inlining = parent_block.inlining,
...@@ -2302,15 +2622,15 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com...@@ -2302,15 +2622,15 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
2302 _ = try sema.analyzeBody(&child_block, body);2622 _ = try sema.analyzeBody(&child_block, body);
23032623
2304 const c_import_res = sema.mod.comp.cImport(c_import_buf.items) catch |err|2624 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
2307 if (c_import_res.errors.len != 0) {2627 if (c_import_res.errors.len != 0) {
2308 const msg = msg: {2628 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", .{});
2310 errdefer msg.destroy(sema.gpa);2630 errdefer msg.destroy(sema.gpa);
23112631
2312 if (!sema.mod.comp.bin_file.options.link_libc)2632 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
2315 for (c_import_res.errors) |_| {2635 for (c_import_res.errors) |_| {
2316 // TODO integrate with LazySrcLoc2636 // TODO integrate with LazySrcLoc
...@@ -2322,7 +2642,7 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com...@@ -2322,7 +2642,7 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
2322 @import("clang.zig").Stage2ErrorMsg.delete(c_import_res.errors.ptr, c_import_res.errors.len);2642 @import("clang.zig").Stage2ErrorMsg.delete(c_import_res.errors.ptr, c_import_res.errors.len);
2323 break :msg msg;2643 break :msg msg;
2324 };2644 };
2325 return sema.mod.failWithOwnedErrorMsg(&child_block.base, msg);2645 return sema.failWithOwnedErrorMsg(msg);
2326 }2646 }
2327 const c_import_pkg = Package.create(2647 const c_import_pkg = Package.create(
2328 sema.gpa,2648 sema.gpa,
...@@ -2338,10 +2658,10 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com...@@ -2338,10 +2658,10 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
2338 try c_import_pkg.add(sema.gpa, "std", std_pkg);2658 try c_import_pkg.add(sema.gpa, "std", std_pkg);
23392659
2340 const result = sema.mod.importPkg(c_import_pkg) catch |err|2660 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
2343 sema.mod.astGenFile(result.file) catch |err|2663 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
2346 try sema.mod.semaFile(result.file);2666 try sema.mod.semaFile(result.file);
2347 const file_root_decl = result.file.root_decl.?;2667 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...@@ -2349,15 +2669,15 @@ fn zirCImport(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Com
2349 return sema.addConstant(file_root_decl.ty, file_root_decl.val);2669 return sema.addConstant(file_root_decl.ty, file_root_decl.val);
2350}2670}
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 {
2353 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;2673 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
2354 const src = inst_data.src();2674 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", .{});
2356}2676}
23572677
2358fn zirBlock(2678fn zirBlock(
2359 sema: *Sema,2679 sema: *Sema,
2360 parent_block: *Scope.Block,2680 parent_block: *Block,
2361 inst: Zir.Inst.Index,2681 inst: Zir.Inst.Index,
2362) CompileError!Air.Inst.Ref {2682) CompileError!Air.Inst.Ref {
2363 const tracy = trace(@src());2683 const tracy = trace(@src());
...@@ -2378,7 +2698,7 @@ fn zirBlock(...@@ -2378,7 +2698,7 @@ fn zirBlock(
2378 .data = undefined,2698 .data = undefined,
2379 });2699 });
23802700
2381 var label: Scope.Block.Label = .{2701 var label: Block.Label = .{
2382 .zir_block = inst,2702 .zir_block = inst,
2383 .merges = .{2703 .merges = .{
2384 .results = .{},2704 .results = .{},
...@@ -2387,10 +2707,11 @@ fn zirBlock(...@@ -2387,10 +2707,11 @@ fn zirBlock(
2387 },2707 },
2388 };2708 };
23892709
2390 var child_block: Scope.Block = .{2710 var child_block: Block = .{
2391 .parent = parent_block,2711 .parent = parent_block,
2392 .sema = sema,2712 .sema = sema,
2393 .src_decl = parent_block.src_decl,2713 .src_decl = parent_block.src_decl,
2714 .namespace = parent_block.namespace,
2394 .wip_capture_scope = parent_block.wip_capture_scope,2715 .wip_capture_scope = parent_block.wip_capture_scope,
2395 .instructions = .{},2716 .instructions = .{},
2396 .label = &label,2717 .label = &label,
...@@ -2410,11 +2731,11 @@ fn zirBlock(...@@ -2410,11 +2731,11 @@ fn zirBlock(
24102731
2411fn resolveBlockBody(2732fn resolveBlockBody(
2412 sema: *Sema,2733 sema: *Sema,
2413 parent_block: *Scope.Block,2734 parent_block: *Block,
2414 src: LazySrcLoc,2735 src: LazySrcLoc,
2415 child_block: *Scope.Block,2736 child_block: *Block,
2416 body: []const Zir.Inst.Index,2737 body: []const Zir.Inst.Index,
2417 merges: *Scope.Block.Merges,2738 merges: *Block.Merges,
2418) CompileError!Air.Inst.Ref {2739) CompileError!Air.Inst.Ref {
2419 if (child_block.is_comptime) {2740 if (child_block.is_comptime) {
2420 return sema.resolveBody(child_block, body);2741 return sema.resolveBody(child_block, body);
...@@ -2426,10 +2747,10 @@ fn resolveBlockBody(...@@ -2426,10 +2747,10 @@ fn resolveBlockBody(
24262747
2427fn analyzeBlockBody(2748fn analyzeBlockBody(
2428 sema: *Sema,2749 sema: *Sema,
2429 parent_block: *Scope.Block,2750 parent_block: *Block,
2430 src: LazySrcLoc,2751 src: LazySrcLoc,
2431 child_block: *Scope.Block,2752 child_block: *Block,
2432 merges: *Scope.Block.Merges,2753 merges: *Block.Merges,
2433) CompileError!Air.Inst.Ref {2754) CompileError!Air.Inst.Ref {
2434 const tracy = trace(@src());2755 const tracy = trace(@src());
2435 defer tracy.end();2756 defer tracy.end();
...@@ -2527,7 +2848,7 @@ fn analyzeBlockBody(...@@ -2527,7 +2848,7 @@ fn analyzeBlockBody(
2527 return Air.indexToRef(merges.block_inst);2848 return Air.indexToRef(merges.block_inst);
2528}2849}
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 {
2531 const tracy = trace(@src());2852 const tracy = trace(@src());
2532 defer tracy.end();2853 defer tracy.end();
25332854
...@@ -2538,14 +2859,14 @@ fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -2538,14 +2859,14 @@ fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
2538 const options_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };2859 const options_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
2539 const decl_name = sema.code.nullTerminatedString(extra.decl_name);2860 const decl_name = sema.code.nullTerminatedString(extra.decl_name);
2540 if (extra.namespace != .none) {2861 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", .{});
2542 }2863 }
2543 const decl = try sema.lookupIdentifier(block, operand_src, decl_name);2864 const decl = try sema.lookupIdentifier(block, operand_src, decl_name);
2544 const options = try sema.resolveExportOptions(block, options_src, extra.options);2865 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);
2546}2867}
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 {
2549 const tracy = trace(@src());2870 const tracy = trace(@src());
2550 defer tracy.end();2871 defer tracy.end();
25512872
...@@ -2558,45 +2879,122 @@ fn zirExportValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -2558,45 +2879,122 @@ fn zirExportValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
2558 const options = try sema.resolveExportOptions(block, options_src, extra.options);2879 const options = try sema.resolveExportOptions(block, options_src, extra.options);
2559 const decl = switch (operand.val.tag()) {2880 const decl = switch (operand.val.tag()) {
2560 .function => operand.val.castTag(.function).?.data.owner_decl,2881 .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.
2562 };2883 };
2563 try sema.mod.analyzeExport(block, src, options, decl);2884 try sema.analyzeExport(block, src, options, decl);
2564}2885}
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;
2567 const mod = sema.mod;2895 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 {
2568 const inst_data = sema.code.instructions.items(.data)[inst].un_node;2966 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2569 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };2967 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
2570 const src: LazySrcLoc = inst_data.src();2968 const src: LazySrcLoc = inst_data.src();
2571 const alignment = try sema.resolveAlign(block, operand_src, inst_data.operand);2969 const alignment = try sema.resolveAlign(block, operand_src, inst_data.operand);
2572 if (alignment > 256) {2970 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", .{
2574 alignment,2972 alignment,
2575 });2973 });
2576 }2974 }
2577 const func = sema.owner_func orelse2975 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
2580 switch (func.owner_decl.ty.fnCallingConvention()) {2978 switch (func.owner_decl.ty.fnCallingConvention()) {
2581 .Naked => return mod.fail(&block.base, src, "@setAlignStack in naked function", .{}),2979 .Naked => return sema.fail(block, src, "@setAlignStack in naked function", .{}),
2582 .Inline => return mod.fail(&block.base, src, "@setAlignStack in inline function", .{}),2980 .Inline => return sema.fail(block, src, "@setAlignStack in inline function", .{}),
2583 else => {},2981 else => {},
2584 }2982 }
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);
2587 if (gop.found_existing) {2985 if (gop.found_existing) {
2588 const msg = msg: {2986 const msg = msg: {
2589 const msg = try mod.errMsg(&block.base, src, "multiple @setAlignStack in the same function body", .{});2987 const msg = try sema.errMsg(block, src, "multiple @setAlignStack in the same function body", .{});
2590 errdefer msg.destroy(mod.gpa);2988 errdefer msg.destroy(sema.gpa);
2591 try mod.errNote(&block.base, src, msg, "other instance here", .{});2989 try sema.errNote(block, src, msg, "other instance here", .{});
2592 break :msg msg;2990 break :msg msg;
2593 };2991 };
2594 return mod.failWithOwnedErrorMsg(&block.base, msg);2992 return sema.failWithOwnedErrorMsg(msg);
2595 }2993 }
2596 gop.value_ptr.* = .{ .alignment = alignment, .src = src };2994 gop.value_ptr.* = .{ .alignment = alignment, .src = src };
2597}2995}
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 {
2600 const inst_data = sema.code.instructions.items(.data)[inst].un_node;2998 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2601 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };2999 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
2602 const is_cold = try sema.resolveConstBool(block, operand_src, inst_data.operand);3000 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...@@ -2604,19 +3002,19 @@ fn zirSetCold(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
2604 func.is_cold = is_cold;3002 func.is_cold = is_cold;
2605}3003}
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 {
2608 const inst_data = sema.code.instructions.items(.data)[inst].un_node;3006 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2609 const src: LazySrcLoc = inst_data.src();3007 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", .{});
2611}3009}
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 {
2614 const inst_data = sema.code.instructions.items(.data)[inst].un_node;3012 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2615 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };3013 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
2616 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand);3014 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand);
2617}3015}
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 {
2620 if (block.is_comptime) return;3018 if (block.is_comptime) return;
26213019
2622 const inst_data = sema.code.instructions.items(.data)[inst].un_node;3020 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...@@ -2624,7 +3022,7 @@ fn zirFence(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
2624 const order = try sema.resolveAtomicOrder(block, order_src, inst_data.operand);3022 const order = try sema.resolveAtomicOrder(block, order_src, inst_data.operand);
26253023
2626 if (@enumToInt(order) < @enumToInt(std.builtin.AtomicOrder.Acquire)) {3024 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", .{});
2628 }3026 }
26293027
2630 _ = try block.addInst(.{3028 _ = try block.addInst(.{
...@@ -2633,7 +3031,7 @@ fn zirFence(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError...@@ -2633,7 +3031,7 @@ fn zirFence(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
2633 });3031 });
2634}3032}
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 {
2637 const tracy = trace(@src());3035 const tracy = trace(@src());
2638 defer tracy.end();3036 defer tracy.end();
26393037
...@@ -2655,7 +3053,7 @@ fn zirBreak(sema: *Sema, start_block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -2655,7 +3053,7 @@ fn zirBreak(sema: *Sema, start_block: *Scope.Block, inst: Zir.Inst.Index) Compil
2655 }3053 }
2656}3054}
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 {
2659 const tracy = trace(@src());3057 const tracy = trace(@src());
2660 defer tracy.end();3058 defer tracy.end();
26613059
...@@ -2675,7 +3073,7 @@ fn zirDbgStmt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -2675,7 +3073,7 @@ fn zirDbgStmt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
2675 });3073 });
2676}3074}
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 {
2679 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;3077 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
2680 const src = inst_data.src();3078 const src = inst_data.src();
2681 const decl_name = inst_data.get(sema.code);3079 const decl_name = inst_data.get(sema.code);
...@@ -2683,7 +3081,7 @@ fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -2683,7 +3081,7 @@ fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
2683 return sema.analyzeDeclRef(decl);3081 return sema.analyzeDeclRef(decl);
2684}3082}
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 {
2687 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;3085 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
2688 const src = inst_data.src();3086 const src = inst_data.src();
2689 const decl_name = inst_data.get(sema.code);3087 const decl_name = inst_data.get(sema.code);
...@@ -2691,8 +3089,8 @@ fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -2691,8 +3089,8 @@ fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
2691 return sema.analyzeDeclVal(block, src, decl);3089 return sema.analyzeDeclVal(block, src, decl);
2692}3090}
26933091
2694fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []const u8) !*Decl {3092fn lookupIdentifier(sema: *Sema, block: *Block, src: LazySrcLoc, name: []const u8) !*Decl {
2695 var namespace = sema.namespace;3093 var namespace = block.namespace;
2696 while (true) {3094 while (true) {
2697 if (try sema.lookupInNamespace(block, src, namespace, name, false)) |decl| {3095 if (try sema.lookupInNamespace(block, src, namespace, name, false)) |decl| {
2698 return decl;3096 return decl;
...@@ -2706,9 +3104,9 @@ fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []c...@@ -2706,9 +3104,9 @@ fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []c
2706/// only for ones in the specified namespace.3104/// only for ones in the specified namespace.
2707fn lookupInNamespace(3105fn lookupInNamespace(
2708 sema: *Sema,3106 sema: *Sema,
2709 block: *Scope.Block,3107 block: *Block,
2710 src: LazySrcLoc,3108 src: LazySrcLoc,
2711 namespace: *Scope.Namespace,3109 namespace: *Namespace,
2712 ident_name: []const u8,3110 ident_name: []const u8,
2713 observe_usingnamespace: bool,3111 observe_usingnamespace: bool,
2714) CompileError!?*Decl {3112) CompileError!?*Decl {
...@@ -2721,10 +3119,10 @@ fn lookupInNamespace(...@@ -2721,10 +3119,10 @@ fn lookupInNamespace(
2721 }3119 }
27223120
2723 if (observe_usingnamespace and namespace.usingnamespace_set.count() != 0) {3121 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
2726 const gpa = sema.gpa;3124 const gpa = sema.gpa;
2727 var checked_namespaces: std.AutoArrayHashMapUnmanaged(*Scope.Namespace, void) = .{};3125 var checked_namespaces: std.AutoArrayHashMapUnmanaged(*Namespace, void) = .{};
2728 defer checked_namespaces.deinit(gpa);3126 defer checked_namespaces.deinit(gpa);
27293127
2730 // Keep track of name conflicts for error notes.3128 // Keep track of name conflicts for error notes.
...@@ -2739,7 +3137,7 @@ fn lookupInNamespace(...@@ -2739,7 +3137,7 @@ fn lookupInNamespace(
2739 if (check_ns.decls.get(ident_name)) |decl| {3137 if (check_ns.decls.get(ident_name)) |decl| {
2740 // Skip decls which are not marked pub, which are in a different3138 // Skip decls which are not marked pub, which are in a different
2741 // file than the `a.b`/`@hasDecl` syntax.3139 // 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()) {
2743 try candidates.append(gpa, decl);3141 try candidates.append(gpa, decl);
2744 }3142 }
2745 }3143 }
...@@ -2747,7 +3145,7 @@ fn lookupInNamespace(...@@ -2747,7 +3145,7 @@ fn lookupInNamespace(
2747 while (it.next()) |entry| {3145 while (it.next()) |entry| {
2748 const sub_usingnamespace_decl = entry.key_ptr.*;3146 const sub_usingnamespace_decl = entry.key_ptr.*;
2749 const sub_is_pub = entry.value_ptr.*;3147 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()) {
2751 // Skip usingnamespace decls which are not marked pub, which are in3149 // Skip usingnamespace decls which are not marked pub, which are in
2752 // a different file than the `a.b`/`@hasDecl` syntax.3150 // a different file than the `a.b`/`@hasDecl` syntax.
2753 continue;3151 continue;
...@@ -2768,7 +3166,7 @@ fn lookupInNamespace(...@@ -2768,7 +3166,7 @@ fn lookupInNamespace(
2768 },3166 },
2769 else => {3167 else => {
2770 const msg = msg: {3168 const msg = msg: {
2771 const msg = try mod.errMsg(&block.base, src, "ambiguous reference", .{});3169 const msg = try sema.errMsg(block, src, "ambiguous reference", .{});
2772 errdefer msg.destroy(gpa);3170 errdefer msg.destroy(gpa);
2773 for (candidates.items) |candidate| {3171 for (candidates.items) |candidate| {
2774 const src_loc = candidate.srcLoc();3172 const src_loc = candidate.srcLoc();
...@@ -2776,7 +3174,7 @@ fn lookupInNamespace(...@@ -2776,7 +3174,7 @@ fn lookupInNamespace(
2776 }3174 }
2777 break :msg msg;3175 break :msg msg;
2778 };3176 };
2779 return mod.failWithOwnedErrorMsg(&block.base, msg);3177 return sema.failWithOwnedErrorMsg(msg);
2780 },3178 },
2781 }3179 }
2782 } else if (namespace.decls.get(ident_name)) |decl| {3180 } else if (namespace.decls.get(ident_name)) |decl| {
...@@ -2796,7 +3194,7 @@ fn lookupInNamespace(...@@ -2796,7 +3194,7 @@ fn lookupInNamespace(
27963194
2797fn zirCall(3195fn zirCall(
2798 sema: *Sema,3196 sema: *Sema,
2799 block: *Scope.Block,3197 block: *Block,
2800 inst: Zir.Inst.Index,3198 inst: Zir.Inst.Index,
2801) CompileError!Air.Inst.Ref {3199) CompileError!Air.Inst.Ref {
2802 const tracy = trace(@src());3200 const tracy = trace(@src());
...@@ -2898,7 +3296,7 @@ const GenericRemoveAdapter = struct {...@@ -2898,7 +3296,7 @@ const GenericRemoveAdapter = struct {
28983296
2899fn analyzeCall(3297fn analyzeCall(
2900 sema: *Sema,3298 sema: *Sema,
2901 block: *Scope.Block,3299 block: *Block,
2902 func: Air.Inst.Ref,3300 func: Air.Inst.Ref,
2903 func_src: LazySrcLoc,3301 func_src: LazySrcLoc,
2904 call_src: LazySrcLoc,3302 call_src: LazySrcLoc,
...@@ -2910,14 +3308,14 @@ fn analyzeCall(...@@ -2910,14 +3308,14 @@ fn analyzeCall(
29103308
2911 const func_ty = sema.typeOf(func);3309 const func_ty = sema.typeOf(func);
2912 if (func_ty.zigTypeTag() != .Fn)3310 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
2915 const func_ty_info = func_ty.fnInfo();3313 const func_ty_info = func_ty.fnInfo();
2916 const cc = func_ty_info.cc;3314 const cc = func_ty_info.cc;
2917 if (cc == .Naked) {3315 if (cc == .Naked) {
2918 // TODO add error note: declared here3316 // TODO add error note: declared here
2919 return mod.fail(3317 return sema.fail(
2920 &block.base,3318 block,
2921 func_src,3319 func_src,
2922 "unable to call function with naked calling convention",3320 "unable to call function with naked calling convention",
2923 .{},3321 .{},
...@@ -2928,8 +3326,8 @@ fn analyzeCall(...@@ -2928,8 +3326,8 @@ fn analyzeCall(
2928 assert(cc == .C);3326 assert(cc == .C);
2929 if (uncasted_args.len < fn_params_len) {3327 if (uncasted_args.len < fn_params_len) {
2930 // TODO add error note: declared here3328 // TODO add error note: declared here
2931 return mod.fail(3329 return sema.fail(
2932 &block.base,3330 block,
2933 func_src,3331 func_src,
2934 "expected at least {d} argument(s), found {d}",3332 "expected at least {d} argument(s), found {d}",
2935 .{ fn_params_len, uncasted_args.len },3333 .{ fn_params_len, uncasted_args.len },
...@@ -2937,8 +3335,8 @@ fn analyzeCall(...@@ -2937,8 +3335,8 @@ fn analyzeCall(
2937 }3335 }
2938 } else if (fn_params_len != uncasted_args.len) {3336 } else if (fn_params_len != uncasted_args.len) {
2939 // TODO add error note: declared here3337 // TODO add error note: declared here
2940 return mod.fail(3338 return sema.fail(
2941 &block.base,3339 block,
2942 func_src,3340 func_src,
2943 "expected {d} argument(s), found {d}",3341 "expected {d} argument(s), found {d}",
2944 .{ fn_params_len, uncasted_args.len },3342 .{ fn_params_len, uncasted_args.len },
...@@ -2956,7 +3354,7 @@ fn analyzeCall(...@@ -2956,7 +3354,7 @@ fn analyzeCall(
2956 .never_inline,3354 .never_inline,
2957 .no_async,3355 .no_async,
2958 .always_tail,3356 .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 {}", .{
2960 modifier,3358 modifier,
2961 }),3359 }),
2962 }3360 }
...@@ -2971,7 +3369,7 @@ fn analyzeCall(...@@ -2971,7 +3369,7 @@ fn analyzeCall(
2971 const func_val = try sema.resolveConstValue(block, func_src, func);3369 const func_val = try sema.resolveConstValue(block, func_src, func);
2972 const module_fn = switch (func_val.tag()) {3370 const module_fn = switch (func_val.tag()) {
2973 .function => func_val.castTag(.function).?.data,3371 .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", .{
2975 @as([]const u8, if (is_comptime_call) "comptime" else "inline"),3373 @as([]const u8, if (is_comptime_call) "comptime" else "inline"),
2976 }),3374 }),
2977 else => unreachable,3375 else => unreachable,
...@@ -2979,7 +3377,7 @@ fn analyzeCall(...@@ -2979,7 +3377,7 @@ fn analyzeCall(
29793377
2980 // Analyze the ZIR. The same ZIR gets analyzed into a runtime function3378 // Analyze the ZIR. The same ZIR gets analyzed into a runtime function
2981 // or an inlined call depending on what union tag the `label` field is3379 // 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`.
2983 // This block instruction will be used to capture the return value from the3381 // This block instruction will be used to capture the return value from the
2984 // inlined function.3382 // inlined function.
2985 const block_inst = @intCast(Air.Inst.Index, sema.air_instructions.len);3383 const block_inst = @intCast(Air.Inst.Index, sema.air_instructions.len);
...@@ -2989,7 +3387,7 @@ fn analyzeCall(...@@ -2989,7 +3387,7 @@ fn analyzeCall(
2989 });3387 });
2990 // This one is shared among sub-blocks within the same callee, but not3388 // This one is shared among sub-blocks within the same callee, but not
2991 // shared among the entire inline/comptime call stack.3389 // shared among the entire inline/comptime call stack.
2992 var inlining: Scope.Block.Inlining = .{3390 var inlining: Block.Inlining = .{
2993 .comptime_result = undefined,3391 .comptime_result = undefined,
2994 .merges = .{3392 .merges = .{
2995 .results = .{},3393 .results = .{},
...@@ -3000,7 +3398,7 @@ fn analyzeCall(...@@ -3000,7 +3398,7 @@ fn analyzeCall(
3000 // In order to save a bit of stack space, directly modify Sema rather3398 // In order to save a bit of stack space, directly modify Sema rather
3001 // than create a child one.3399 // than create a child one.
3002 const parent_zir = sema.code;3400 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;
3004 defer sema.code = parent_zir;3402 defer sema.code = parent_zir;
30053403
3006 const parent_inst_map = sema.inst_map;3404 const parent_inst_map = sema.inst_map;
...@@ -3010,10 +3408,6 @@ fn analyzeCall(...@@ -3010,10 +3408,6 @@ fn analyzeCall(
3010 sema.inst_map = parent_inst_map;3408 sema.inst_map = parent_inst_map;
3011 }3409 }
30123410
3013 const parent_namespace = sema.namespace;
3014 sema.namespace = module_fn.owner_decl.namespace;
3015 defer sema.namespace = parent_namespace;
3016
3017 const parent_func = sema.func;3411 const parent_func = sema.func;
3018 sema.func = module_fn;3412 sema.func = module_fn;
3019 defer sema.func = parent_func;3413 defer sema.func = parent_func;
...@@ -3021,10 +3415,11 @@ fn analyzeCall(...@@ -3021,10 +3415,11 @@ fn analyzeCall(
3021 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, module_fn.owner_decl.src_scope);3415 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, module_fn.owner_decl.src_scope);
3022 defer wip_captures.deinit();3416 defer wip_captures.deinit();
30233417
3024 var child_block: Scope.Block = .{3418 var child_block: Block = .{
3025 .parent = null,3419 .parent = null,
3026 .sema = sema,3420 .sema = sema,
3027 .src_decl = module_fn.owner_decl,3421 .src_decl = module_fn.owner_decl,
3422 .namespace = module_fn.owner_decl.src_namespace,
3028 .wip_capture_scope = wip_captures.scope,3423 .wip_capture_scope = wip_captures.scope,
3029 .instructions = .{},3424 .instructions = .{},
3030 .label = null,3425 .label = null,
...@@ -3180,12 +3575,6 @@ fn analyzeCall(...@@ -3180,12 +3575,6 @@ fn analyzeCall(
3180 });3575 });
3181 delete_memoized_call_key = false;3576 delete_memoized_call_key = false;
3182 }3577 }
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);
3189 }3578 }
31903579
3191 break :res2 result;3580 break :res2 result;
...@@ -3200,7 +3589,7 @@ fn analyzeCall(...@@ -3200,7 +3589,7 @@ fn analyzeCall(
3200 // Check the Module's generic function map with an adapted context, so that we3589 // Check the Module's generic function map with an adapted context, so that we
3201 // can match against `uncasted_args` rather than doing the work below to create a3590 // can match against `uncasted_args` rather than doing the work below to create a
3202 // generic Scope only to junk it if it matches an existing instantiation.3591 // 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;
3204 const fn_zir = namespace.file_scope.zir;3593 const fn_zir = namespace.file_scope.zir;
3205 const fn_info = fn_zir.getFnInfo(module_fn.zir_body_inst);3594 const fn_info = fn_zir.getFnInfo(module_fn.zir_body_inst);
3206 const zir_tags = fn_zir.instructions.items(.tag);3595 const zir_tags = fn_zir.instructions.items(.tag);
...@@ -3276,12 +3665,12 @@ fn analyzeCall(...@@ -3276,12 +3665,12 @@ fn analyzeCall(
32763665
3277 // Create a Decl for the new function.3666 // Create a Decl for the new function.
3278 const src_decl = namespace.getDecl();3667 const src_decl = namespace.getDecl();
3279 const new_decl = try mod.allocateNewDecl(namespace, module_fn.owner_decl.src_node, src_decl.src_scope);
3280 // TODO better names for generic function instantiations3668 // TODO better names for generic function instantiations
3281 const name_index = mod.getNextAnonNameIndex();3669 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}", .{
3283 module_fn.owner_decl.name, name_index,3671 module_fn.owner_decl.name, name_index,
3284 });3672 });
3673 const new_decl = try mod.allocateNewDecl(decl_name, namespace, module_fn.owner_decl.src_node, src_decl.src_scope);
3285 new_decl.src_line = module_fn.owner_decl.src_line;3674 new_decl.src_line = module_fn.owner_decl.src_line;
3286 new_decl.is_pub = module_fn.owner_decl.is_pub;3675 new_decl.is_pub = module_fn.owner_decl.is_pub;
3287 new_decl.is_exported = module_fn.owner_decl.is_exported;3676 new_decl.is_exported = module_fn.owner_decl.is_exported;
...@@ -3297,6 +3686,11 @@ fn analyzeCall(...@@ -3297,6 +3686,11 @@ fn analyzeCall(
32973686
3298 namespace.anon_decls.putAssumeCapacityNoClobber(new_decl, {});3687 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
3300 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);3694 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);
3301 errdefer new_decl_arena.deinit();3695 errdefer new_decl_arena.deinit();
33023696
...@@ -3311,7 +3705,6 @@ fn analyzeCall(...@@ -3311,7 +3705,6 @@ fn analyzeCall(
3311 .perm_arena = &new_decl_arena.allocator,3705 .perm_arena = &new_decl_arena.allocator,
3312 .code = fn_zir,3706 .code = fn_zir,
3313 .owner_decl = new_decl,3707 .owner_decl = new_decl,
3314 .namespace = namespace,
3315 .func = null,3708 .func = null,
3316 .fn_ret_ty = Type.initTag(.void),3709 .fn_ret_ty = Type.initTag(.void),
3317 .owner_func = null,3710 .owner_func = null,
...@@ -3324,10 +3717,11 @@ fn analyzeCall(...@@ -3324,10 +3717,11 @@ fn analyzeCall(
3324 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, new_decl.src_scope);3717 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, new_decl.src_scope);
3325 defer wip_captures.deinit();3718 defer wip_captures.deinit();
33263719
3327 var child_block: Scope.Block = .{3720 var child_block: Block = .{
3328 .parent = null,3721 .parent = null,
3329 .sema = &child_sema,3722 .sema = &child_sema,
3330 .src_decl = new_decl,3723 .src_decl = new_decl,
3724 .namespace = namespace,
3331 .wip_capture_scope = wip_captures.scope,3725 .wip_capture_scope = wip_captures.scope,
3332 .instructions = .{},3726 .instructions = .{},
3333 .inlining = null,3727 .inlining = null,
...@@ -3430,11 +3824,6 @@ fn analyzeCall(...@@ -3430,11 +3824,6 @@ fn analyzeCall(
3430 });3824 });
3431 assert(!new_decl.ty.fnInfo().is_generic);3825 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
3438 // Queue up a `codegen_func` work item for the new Fn. The `comptime_args` field3827 // Queue up a `codegen_func` work item for the new Fn. The `comptime_args` field
3439 // will be populated, ensuring it will have `analyzeBody` called with the ZIR3828 // will be populated, ensuring it will have `analyzeBody` called with the ZIR
3440 // parameters mapped appropriately.3829 // parameters mapped appropriately.
...@@ -3489,7 +3878,7 @@ fn analyzeCall(...@@ -3489,7 +3878,7 @@ fn analyzeCall(
34893878
3490fn finishGenericCall(3879fn finishGenericCall(
3491 sema: *Sema,3880 sema: *Sema,
3492 block: *Scope.Block,3881 block: *Block,
3493 call_src: LazySrcLoc,3882 call_src: LazySrcLoc,
3494 callee: *Module.Fn,3883 callee: *Module.Fn,
3495 func_src: LazySrcLoc,3884 func_src: LazySrcLoc,
...@@ -3556,7 +3945,7 @@ fn finishGenericCall(...@@ -3556,7 +3945,7 @@ fn finishGenericCall(
3556 return func_inst;3945 return func_inst;
3557}3946}
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 {
3560 _ = block;3949 _ = block;
3561 const tracy = trace(@src());3950 const tracy = trace(@src());
3562 defer tracy.end();3951 defer tracy.end();
...@@ -3567,7 +3956,7 @@ fn zirIntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -3567,7 +3956,7 @@ fn zirIntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
3567 return sema.addType(ty);3956 return sema.addType(ty);
3568}3957}
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 {
3571 const tracy = trace(@src());3960 const tracy = trace(@src());
3572 defer tracy.end();3961 defer tracy.end();
35733962
...@@ -3579,7 +3968,7 @@ fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi...@@ -3579,7 +3968,7 @@ fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
3579 return sema.addType(opt_type);3968 return sema.addType(opt_type);
3580}3969}
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 {
3583 const inst_data = sema.code.instructions.items(.data)[inst].un_node;3972 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
3584 const src = inst_data.src();3973 const src = inst_data.src();
3585 const array_type = try sema.resolveType(block, src, inst_data.operand);3974 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...@@ -3587,7 +3976,7 @@ fn zirElemType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
3587 return sema.addType(elem_type);3976 return sema.addType(elem_type);
3588}3977}
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 {
3591 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;3980 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
3592 const elem_type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };3981 const elem_type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
3593 const len_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };3982 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...@@ -3601,7 +3990,7 @@ fn zirVectorType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
3601 return sema.addType(vector_type);3990 return sema.addType(vector_type);
3602}3991}
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 {
3605 const tracy = trace(@src());3994 const tracy = trace(@src());
3606 defer tracy.end();3995 defer tracy.end();
36073996
...@@ -3613,7 +4002,7 @@ fn zirArrayType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -3613,7 +4002,7 @@ fn zirArrayType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
3613 return sema.addType(array_ty);4002 return sema.addType(array_ty);
3614}4003}
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 {
3617 const tracy = trace(@src());4006 const tracy = trace(@src());
3618 defer tracy.end();4007 defer tracy.end();
36194008
...@@ -3629,7 +4018,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)...@@ -3629,7 +4018,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
3629 return sema.addType(array_ty);4018 return sema.addType(array_ty);
3630}4019}
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 {
3633 const tracy = trace(@src());4022 const tracy = trace(@src());
3634 defer tracy.end();4023 defer tracy.end();
36354024
...@@ -3641,7 +4030,7 @@ fn zirAnyframeType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi...@@ -3641,7 +4030,7 @@ fn zirAnyframeType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
3641 return sema.addType(anyframe_type);4030 return sema.addType(anyframe_type);
3642}4031}
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 {
3645 const tracy = trace(@src());4034 const tracy = trace(@src());
3646 defer tracy.end();4035 defer tracy.end();
36474036
...@@ -3653,7 +4042,7 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com...@@ -3653,7 +4042,7 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com
3653 const payload = try sema.resolveType(block, rhs_src, extra.rhs);4042 const payload = try sema.resolveType(block, rhs_src, extra.rhs);
36544043
3655 if (error_union.zigTypeTag() != .ErrorSet) {4044 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 {}", .{
3657 error_union.elemType(),4046 error_union.elemType(),
3658 });4047 });
3659 }4048 }
...@@ -3661,7 +4050,7 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com...@@ -3661,7 +4050,7 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com
3661 return sema.addType(err_union_ty);4050 return sema.addType(err_union_ty);
3662}4051}
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 {
3665 _ = block;4054 _ = block;
3666 const tracy = trace(@src());4055 const tracy = trace(@src());
3667 defer tracy.end();4056 defer tracy.end();
...@@ -3679,7 +4068,7 @@ fn zirErrorValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile...@@ -3679,7 +4068,7 @@ fn zirErrorValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
3679 );4068 );
3680}4069}
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 {
3683 const tracy = trace(@src());4072 const tracy = trace(@src());
3684 defer tracy.end();4073 defer tracy.end();
36854074
...@@ -3706,7 +4095,7 @@ fn zirErrorToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile...@@ -3706,7 +4095,7 @@ fn zirErrorToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
3706 return block.addTyOp(.bitcast, result_ty, op_coerced);4095 return block.addTyOp(.bitcast, result_ty, op_coerced);
3707}4096}
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 {
3710 const tracy = trace(@src());4099 const tracy = trace(@src());
3711 defer tracy.end();4100 defer tracy.end();
37124101
...@@ -3719,7 +4108,7 @@ fn zirIntToError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile...@@ -3719,7 +4108,7 @@ fn zirIntToError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
3719 if (try sema.resolveDefinedValue(block, operand_src, op)) |value| {4108 if (try sema.resolveDefinedValue(block, operand_src, op)) |value| {
3720 const int = value.toUnsignedInt();4109 const int = value.toUnsignedInt();
3721 if (int > sema.mod.global_error_set.count() or int == 0)4110 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});
3723 const payload = try sema.arena.create(Value.Payload.Error);4112 const payload = try sema.arena.create(Value.Payload.Error);
3724 payload.* = .{4113 payload.* = .{
3725 .base = .{ .tag = .@"error" },4114 .base = .{ .tag = .@"error" },
...@@ -3729,14 +4118,14 @@ fn zirIntToError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile...@@ -3729,14 +4118,14 @@ fn zirIntToError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
3729 }4118 }
3730 try sema.requireRuntimeBlock(block, src);4119 try sema.requireRuntimeBlock(block, src);
3731 if (block.wantSafety()) {4120 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", .{});
3733 // const is_gt_max = @panic("TODO get max errors in compilation");4122 // const is_gt_max = @panic("TODO get max errors in compilation");
3734 // try sema.addSafetyCheck(block, is_gt_max, .invalid_error_code);4123 // try sema.addSafetyCheck(block, is_gt_max, .invalid_error_code);
3735 }4124 }
3736 return block.addTyOp(.bitcast, Type.initTag(.anyerror), op);4125 return block.addTyOp(.bitcast, Type.initTag(.anyerror), op);
3737}4126}
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 {
3740 const tracy = trace(@src());4129 const tracy = trace(@src());
3741 defer tracy.end();4130 defer tracy.end();
37424131
...@@ -3749,19 +4138,19 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com...@@ -3749,19 +4138,19 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com
3749 const rhs = sema.resolveInst(extra.rhs);4138 const rhs = sema.resolveInst(extra.rhs);
3750 if (sema.typeOf(lhs).zigTypeTag() == .Bool and sema.typeOf(rhs).zigTypeTag() == .Bool) {4139 if (sema.typeOf(lhs).zigTypeTag() == .Bool and sema.typeOf(rhs).zigTypeTag() == .Bool) {
3751 const msg = msg: {4140 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'", .{});
3753 errdefer msg.destroy(sema.gpa);4142 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", .{});
3755 break :msg msg;4144 break :msg msg;
3756 };4145 };
3757 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);4146 return sema.failWithOwnedErrorMsg(msg);
3758 }4147 }
3759 const lhs_ty = try sema.analyzeAsType(block, lhs_src, lhs);4148 const lhs_ty = try sema.analyzeAsType(block, lhs_src, lhs);
3760 const rhs_ty = try sema.analyzeAsType(block, rhs_src, rhs);4149 const rhs_ty = try sema.analyzeAsType(block, rhs_src, rhs);
3761 if (lhs_ty.zigTypeTag() != .ErrorSet)4150 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});
3763 if (rhs_ty.zigTypeTag() != .ErrorSet)4152 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
3766 // Anything merged with anyerror is anyerror.4155 // Anything merged with anyerror is anyerror.
3767 if (lhs_ty.tag() == .anyerror or rhs_ty.tag() == .anyerror) {4156 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...@@ -3820,7 +4209,7 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com
3820 return sema.addConstant(Type.initTag(.type), try Value.Tag.ty.create(sema.arena, error_set_ty));4209 return sema.addConstant(Type.initTag(.type), try Value.Tag.ty.create(sema.arena, error_set_ty));
3821}4210}
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 {
3824 _ = block;4213 _ = block;
3825 const tracy = trace(@src());4214 const tracy = trace(@src());
3826 defer tracy.end();4215 defer tracy.end();
...@@ -3833,8 +4222,7 @@ fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -3833,8 +4222,7 @@ fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
3833 );4222 );
3834}4223}
38354224
3836fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {4225fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3837 const mod = sema.mod;
3838 const arena = sema.arena;4226 const arena = sema.arena;
3839 const inst_data = sema.code.instructions.items(.data)[inst].un_node;4227 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
3840 const src = inst_data.src();4228 const src = inst_data.src();
...@@ -3846,17 +4234,17 @@ fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -3846,17 +4234,17 @@ fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
3846 .Enum => operand,4234 .Enum => operand,
3847 .Union => {4235 .Union => {
3848 //if (!operand_ty.unionHasTag()) {4236 //if (!operand_ty.unionHasTag()) {
3849 // return mod.fail(4237 // return sema.fail(
3850 // &block.base,4238 // block,
3851 // operand_src,4239 // operand_src,
3852 // "untagged union '{}' cannot be converted to integer",4240 // "untagged union '{}' cannot be converted to integer",
3853 // .{dest_ty_src},4241 // .{dest_ty_src},
3854 // );4242 // );
3855 //}4243 //}
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", .{});
3857 },4245 },
3858 else => {4246 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 {}", .{
3860 operand_ty,4248 operand_ty,
3861 });4249 });
3862 },4250 },
...@@ -3880,9 +4268,8 @@ fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -3880,9 +4268,8 @@ fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
3880 return block.addTyOp(.bitcast, int_tag_ty, enum_tag);4268 return block.addTyOp(.bitcast, int_tag_ty, enum_tag);
3881}4269}
38824270
3883fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {4271fn zirIntToEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
3884 const mod = sema.mod;4272 const target = sema.mod.getTarget();
3885 const target = mod.getTarget();
3886 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;4273 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
3887 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;4274 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
3888 const src = inst_data.src();4275 const src = inst_data.src();
...@@ -3892,7 +4279,7 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -3892,7 +4279,7 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
3892 const operand = sema.resolveInst(extra.rhs);4279 const operand = sema.resolveInst(extra.rhs);
38934280
3894 if (dest_ty.zigTypeTag() != .Enum) {4281 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});
3896 }4283 }
38974284
3898 if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |int_val| {4285 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...@@ -3904,14 +4291,14 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
3904 }4291 }
3905 if (!dest_ty.enumHasInt(int_val, target)) {4292 if (!dest_ty.enumHasInt(int_val, target)) {
3906 const msg = msg: {4293 const msg = msg: {
3907 const msg = try mod.errMsg(4294 const msg = try sema.errMsg(
3908 &block.base,4295 block,
3909 src,4296 src,
3910 "enum '{}' has no tag with value {}",4297 "enum '{}' has no tag with value {}",
3911 .{ dest_ty, int_val },4298 .{ dest_ty, int_val },
3912 );4299 );
3913 errdefer msg.destroy(sema.gpa);4300 errdefer msg.destroy(sema.gpa);
3914 try mod.errNoteNonLazy(4301 try sema.mod.errNoteNonLazy(
3915 dest_ty.declSrcLoc(),4302 dest_ty.declSrcLoc(),
3916 msg,4303 msg,
3917 "enum declared here",4304 "enum declared here",
...@@ -3919,7 +4306,7 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -3919,7 +4306,7 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
3919 );4306 );
3920 break :msg msg;4307 break :msg msg;
3921 };4308 };
3922 return mod.failWithOwnedErrorMsg(&block.base, msg);4309 return sema.failWithOwnedErrorMsg(msg);
3923 }4310 }
3924 return sema.addConstant(dest_ty, int_val);4311 return sema.addConstant(dest_ty, int_val);
3925 }4312 }
...@@ -3931,7 +4318,7 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -3931,7 +4318,7 @@ fn zirIntToEnum(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
3931/// Pointer in, pointer out.4318/// Pointer in, pointer out.
3932fn zirOptionalPayloadPtr(4319fn zirOptionalPayloadPtr(
3933 sema: *Sema,4320 sema: *Sema,
3934 block: *Scope.Block,4321 block: *Block,
3935 inst: Zir.Inst.Index,4322 inst: Zir.Inst.Index,
3936 safety_check: bool,4323 safety_check: bool,
3937) CompileError!Air.Inst.Ref {4324) CompileError!Air.Inst.Ref {
...@@ -3946,7 +4333,7 @@ fn zirOptionalPayloadPtr(...@@ -3946,7 +4333,7 @@ fn zirOptionalPayloadPtr(
39464333
3947 const opt_type = optional_ptr_ty.elemType();4334 const opt_type = optional_ptr_ty.elemType();
3948 if (opt_type.zigTypeTag() != .Optional) {4335 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});
3950 }4337 }
39514338
3952 const child_type = try opt_type.optionalChildAlloc(sema.arena);4339 const child_type = try opt_type.optionalChildAlloc(sema.arena);
...@@ -3959,7 +4346,7 @@ fn zirOptionalPayloadPtr(...@@ -3959,7 +4346,7 @@ fn zirOptionalPayloadPtr(
3959 if (try sema.resolveDefinedValue(block, src, optional_ptr)) |pointer_val| {4346 if (try sema.resolveDefinedValue(block, src, optional_ptr)) |pointer_val| {
3960 if (try pointer_val.pointerDeref(sema.arena)) |val| {4347 if (try pointer_val.pointerDeref(sema.arena)) |val| {
3961 if (val.isNull()) {4348 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", .{});
3963 }4350 }
3964 // The same Value represents the pointer to the optional and the payload.4351 // The same Value represents the pointer to the optional and the payload.
3965 return sema.addConstant(4352 return sema.addConstant(
...@@ -3980,7 +4367,7 @@ fn zirOptionalPayloadPtr(...@@ -3980,7 +4367,7 @@ fn zirOptionalPayloadPtr(
3980/// Value in, value out.4367/// Value in, value out.
3981fn zirOptionalPayload(4368fn zirOptionalPayload(
3982 sema: *Sema,4369 sema: *Sema,
3983 block: *Scope.Block,4370 block: *Block,
3984 inst: Zir.Inst.Index,4371 inst: Zir.Inst.Index,
3985 safety_check: bool,4372 safety_check: bool,
3986) CompileError!Air.Inst.Ref {4373) CompileError!Air.Inst.Ref {
...@@ -3993,14 +4380,14 @@ fn zirOptionalPayload(...@@ -3993,14 +4380,14 @@ fn zirOptionalPayload(
3993 const operand_ty = sema.typeOf(operand);4380 const operand_ty = sema.typeOf(operand);
3994 const opt_type = operand_ty;4381 const opt_type = operand_ty;
3995 if (opt_type.zigTypeTag() != .Optional) {4382 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});
3997 }4384 }
39984385
3999 const child_type = try opt_type.optionalChildAlloc(sema.arena);4386 const child_type = try opt_type.optionalChildAlloc(sema.arena);
40004387
4001 if (try sema.resolveDefinedValue(block, src, operand)) |val| {4388 if (try sema.resolveDefinedValue(block, src, operand)) |val| {
4002 if (val.isNull()) {4389 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", .{});
4004 }4391 }
4005 const sub_val = val.castTag(.opt_payload).?.data;4392 const sub_val = val.castTag(.opt_payload).?.data;
4006 return sema.addConstant(child_type, sub_val);4393 return sema.addConstant(child_type, sub_val);
...@@ -4017,7 +4404,7 @@ fn zirOptionalPayload(...@@ -4017,7 +4404,7 @@ fn zirOptionalPayload(
4017/// Value in, value out4404/// Value in, value out
4018fn zirErrUnionPayload(4405fn zirErrUnionPayload(
4019 sema: *Sema,4406 sema: *Sema,
4020 block: *Scope.Block,4407 block: *Block,
4021 inst: Zir.Inst.Index,4408 inst: Zir.Inst.Index,
4022 safety_check: bool,4409 safety_check: bool,
4023) CompileError!Air.Inst.Ref {4410) CompileError!Air.Inst.Ref {
...@@ -4030,11 +4417,11 @@ fn zirErrUnionPayload(...@@ -4030,11 +4417,11 @@ fn zirErrUnionPayload(
4030 const operand_src = src;4417 const operand_src = src;
4031 const operand_ty = sema.typeOf(operand);4418 const operand_ty = sema.typeOf(operand);
4032 if (operand_ty.zigTypeTag() != .ErrorUnion)4419 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
4035 if (try sema.resolveDefinedValue(block, src, operand)) |val| {4422 if (try sema.resolveDefinedValue(block, src, operand)) |val| {
4036 if (val.getError()) |name| {4423 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});
4038 }4425 }
4039 const data = val.castTag(.eu_payload).?.data;4426 const data = val.castTag(.eu_payload).?.data;
4040 const result_ty = operand_ty.errorUnionPayload();4427 const result_ty = operand_ty.errorUnionPayload();
...@@ -4052,7 +4439,7 @@ fn zirErrUnionPayload(...@@ -4052,7 +4439,7 @@ fn zirErrUnionPayload(
4052/// Pointer in, pointer out.4439/// Pointer in, pointer out.
4053fn zirErrUnionPayloadPtr(4440fn zirErrUnionPayloadPtr(
4054 sema: *Sema,4441 sema: *Sema,
4055 block: *Scope.Block,4442 block: *Block,
4056 inst: Zir.Inst.Index,4443 inst: Zir.Inst.Index,
4057 safety_check: bool,4444 safety_check: bool,
4058) CompileError!Air.Inst.Ref {4445) CompileError!Air.Inst.Ref {
...@@ -4066,7 +4453,7 @@ fn zirErrUnionPayloadPtr(...@@ -4066,7 +4453,7 @@ fn zirErrUnionPayloadPtr(
4066 assert(operand_ty.zigTypeTag() == .Pointer);4453 assert(operand_ty.zigTypeTag() == .Pointer);
40674454
4068 if (operand_ty.elemType().zigTypeTag() != .ErrorUnion)4455 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
4071 const payload_ty = operand_ty.elemType().errorUnionPayload();4458 const payload_ty = operand_ty.elemType().errorUnionPayload();
4072 const operand_pointer_ty = try Type.ptr(sema.arena, .{4459 const operand_pointer_ty = try Type.ptr(sema.arena, .{
...@@ -4078,7 +4465,7 @@ fn zirErrUnionPayloadPtr(...@@ -4078,7 +4465,7 @@ fn zirErrUnionPayloadPtr(
4078 if (try sema.resolveDefinedValue(block, src, operand)) |pointer_val| {4465 if (try sema.resolveDefinedValue(block, src, operand)) |pointer_val| {
4079 if (try pointer_val.pointerDeref(sema.arena)) |val| {4466 if (try pointer_val.pointerDeref(sema.arena)) |val| {
4080 if (val.getError()) |name| {4467 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});
4082 }4469 }
4083 return sema.addConstant(4470 return sema.addConstant(
4084 operand_pointer_ty,4471 operand_pointer_ty,
...@@ -4096,7 +4483,7 @@ fn zirErrUnionPayloadPtr(...@@ -4096,7 +4483,7 @@ fn zirErrUnionPayloadPtr(
4096}4483}
40974484
4098/// Value in, value out4485/// 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 {
4100 const tracy = trace(@src());4487 const tracy = trace(@src());
4101 defer tracy.end();4488 defer tracy.end();
41024489
...@@ -4105,7 +4492,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi...@@ -4105,7 +4492,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
4105 const operand = sema.resolveInst(inst_data.operand);4492 const operand = sema.resolveInst(inst_data.operand);
4106 const operand_ty = sema.typeOf(operand);4493 const operand_ty = sema.typeOf(operand);
4107 if (operand_ty.zigTypeTag() != .ErrorUnion)4494 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
4110 const result_ty = operand_ty.errorUnionSet();4497 const result_ty = operand_ty.errorUnionSet();
41114498
...@@ -4119,7 +4506,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi...@@ -4119,7 +4506,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
4119}4506}
41204507
4121/// Pointer in, value out4508/// 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 {
4123 const tracy = trace(@src());4510 const tracy = trace(@src());
4124 defer tracy.end();4511 defer tracy.end();
41254512
...@@ -4130,7 +4517,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co...@@ -4130,7 +4517,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
4130 assert(operand_ty.zigTypeTag() == .Pointer);4517 assert(operand_ty.zigTypeTag() == .Pointer);
41314518
4132 if (operand_ty.elemType().zigTypeTag() != .ErrorUnion)4519 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
4135 const result_ty = operand_ty.elemType().errorUnionSet();4522 const result_ty = operand_ty.elemType().errorUnionSet();
41364523
...@@ -4145,7 +4532,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co...@@ -4145,7 +4532,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
4145 return block.addTyOp(.unwrap_errunion_err_ptr, result_ty, operand);4532 return block.addTyOp(.unwrap_errunion_err_ptr, result_ty, operand);
4146}4533}
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 {
4149 const tracy = trace(@src());4536 const tracy = trace(@src());
4150 defer tracy.end();4537 defer tracy.end();
41514538
...@@ -4154,15 +4541,15 @@ fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde...@@ -4154,15 +4541,15 @@ fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
4154 const operand = sema.resolveInst(inst_data.operand);4541 const operand = sema.resolveInst(inst_data.operand);
4155 const operand_ty = sema.typeOf(operand);4542 const operand_ty = sema.typeOf(operand);
4156 if (operand_ty.zigTypeTag() != .ErrorUnion)4543 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});
4158 if (operand_ty.errorUnionPayload().zigTypeTag() != .Void) {4545 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", .{});
4160 }4547 }
4161}4548}
41624549
4163fn zirFunc(4550fn zirFunc(
4164 sema: *Sema,4551 sema: *Sema,
4165 block: *Scope.Block,4552 block: *Block,
4166 inst: Zir.Inst.Index,4553 inst: Zir.Inst.Index,
4167 inferred_error_set: bool,4554 inferred_error_set: bool,
4168) CompileError!Air.Inst.Ref {4555) CompileError!Air.Inst.Ref {
...@@ -4205,7 +4592,7 @@ fn zirFunc(...@@ -4205,7 +4592,7 @@ fn zirFunc(
42054592
4206fn funcCommon(4593fn funcCommon(
4207 sema: *Sema,4594 sema: *Sema,
4208 block: *Scope.Block,4595 block: *Block,
4209 src_node_offset: i32,4596 src_node_offset: i32,
4210 body_inst: Zir.Inst.Index,4597 body_inst: Zir.Inst.Index,
4211 ret_ty_body: []const Zir.Inst.Index,4598 ret_ty_body: []const Zir.Inst.Index,
...@@ -4297,7 +4684,7 @@ fn funcCommon(...@@ -4297,7 +4684,7 @@ fn funcCommon(
4297 }4684 }
42984685
4299 if (align_val.tag() != .null_value) {4686 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", .{});
4301 }4688 }
43024689
4303 is_generic = is_generic or bare_return_type.requiresComptime();4690 is_generic = is_generic or bare_return_type.requiresComptime();
...@@ -4329,15 +4716,15 @@ fn funcCommon(...@@ -4329,15 +4716,15 @@ fn funcCommon(
4329 const lib_name_src: LazySrcLoc = .{ .node_offset_lib_name = src_node_offset };4716 const lib_name_src: LazySrcLoc = .{ .node_offset_lib_name = src_node_offset };
4330 log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});4717 log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});
4331 mod.comp.stage1AddLinkLib(lib_name) catch |err| {4718 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}", .{
4333 lib_name, @errorName(err),4720 lib_name, @errorName(err),
4334 });4721 });
4335 };4722 };
4336 const target = mod.getTarget();4723 const target = mod.getTarget();
4337 if (target_util.is_libc_lib_name(target, lib_name)) {4724 if (target_util.is_libc_lib_name(target, lib_name)) {
4338 if (!mod.comp.bin_file.options.link_libc) {4725 if (!mod.comp.bin_file.options.link_libc) {
4339 return mod.fail(4726 return sema.fail(
4340 &block.base,4727 block,
4341 lib_name_src,4728 lib_name_src,
4342 "dependency on libc must be explicitly specified in the build command",4729 "dependency on libc must be explicitly specified in the build command",
4343 .{},4730 .{},
...@@ -4347,8 +4734,8 @@ fn funcCommon(...@@ -4347,8 +4734,8 @@ fn funcCommon(
4347 }4734 }
4348 if (target_util.is_libcpp_lib_name(target, lib_name)) {4735 if (target_util.is_libcpp_lib_name(target, lib_name)) {
4349 if (!mod.comp.bin_file.options.link_libcpp) {4736 if (!mod.comp.bin_file.options.link_libcpp) {
4350 return mod.fail(4737 return sema.fail(
4351 &block.base,4738 block,
4352 lib_name_src,4739 lib_name_src,
4353 "dependency on libc++ must be explicitly specified in the build command",4740 "dependency on libc++ must be explicitly specified in the build command",
4354 .{},4741 .{},
...@@ -4357,8 +4744,8 @@ fn funcCommon(...@@ -4357,8 +4744,8 @@ fn funcCommon(
4357 break :blk;4744 break :blk;
4358 }4745 }
4359 if (!target.isWasm() and !mod.comp.bin_file.options.pic) {4746 if (!target.isWasm() and !mod.comp.bin_file.options.pic) {
4360 return mod.fail(4747 return sema.fail(
4361 &block.base,4748 block,
4362 lib_name_src,4749 lib_name_src,
4363 "dependency on dynamic library '{s}' requires enabling Position Independent Code. Fixed by `-l{s}` or `-fPIC`.",4750 "dependency on dynamic library '{s}' requires enabling Position Independent Code. Fixed by `-l{s}` or `-fPIC`.",
4364 .{ lib_name, lib_name },4751 .{ lib_name, lib_name },
...@@ -4404,7 +4791,7 @@ fn funcCommon(...@@ -4404,7 +4791,7 @@ fn funcCommon(
44044791
4405fn zirParam(4792fn zirParam(
4406 sema: *Sema,4793 sema: *Sema,
4407 block: *Scope.Block,4794 block: *Block,
4408 inst: Zir.Inst.Index,4795 inst: Zir.Inst.Index,
4409 is_comptime: bool,4796 is_comptime: bool,
4410) CompileError!void {4797) CompileError!void {
...@@ -4474,7 +4861,7 @@ fn zirParam(...@@ -4474,7 +4861,7 @@ fn zirParam(
44744861
4475fn zirParamAnytype(4862fn zirParamAnytype(
4476 sema: *Sema,4863 sema: *Sema,
4477 block: *Scope.Block,4864 block: *Block,
4478 inst: Zir.Inst.Index,4865 inst: Zir.Inst.Index,
4479 is_comptime: bool,4866 is_comptime: bool,
4480) CompileError!void {4867) CompileError!void {
...@@ -4509,7 +4896,7 @@ fn zirParamAnytype(...@@ -4509,7 +4896,7 @@ fn zirParamAnytype(
4509 try sema.inst_map.put(sema.gpa, inst, .generic_poison);4896 try sema.inst_map.put(sema.gpa, inst, .generic_poison);
4510}4897}
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 {
4513 const tracy = trace(@src());4900 const tracy = trace(@src());
4514 defer tracy.end();4901 defer tracy.end();
45154902
...@@ -4517,7 +4904,7 @@ fn zirAs(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -4517,7 +4904,7 @@ fn zirAs(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Ai
4517 return sema.analyzeAs(block, .unneeded, bin_inst.lhs, bin_inst.rhs);4904 return sema.analyzeAs(block, .unneeded, bin_inst.lhs, bin_inst.rhs);
4518}4905}
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 {
4521 const tracy = trace(@src());4908 const tracy = trace(@src());
4522 defer tracy.end();4909 defer tracy.end();
45234910
...@@ -4529,7 +4916,7 @@ fn zirAsNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -4529,7 +4916,7 @@ fn zirAsNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
45294916
4530fn analyzeAs(4917fn analyzeAs(
4531 sema: *Sema,4918 sema: *Sema,
4532 block: *Scope.Block,4919 block: *Block,
4533 src: LazySrcLoc,4920 src: LazySrcLoc,
4534 zir_dest_type: Zir.Inst.Ref,4921 zir_dest_type: Zir.Inst.Ref,
4535 zir_operand: Zir.Inst.Ref,4922 zir_operand: Zir.Inst.Ref,
...@@ -4539,7 +4926,7 @@ fn analyzeAs(...@@ -4539,7 +4926,7 @@ fn analyzeAs(
4539 return sema.coerce(block, dest_type, operand, src);4926 return sema.coerce(block, dest_type, operand, src);
4540}4927}
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 {
4543 const tracy = trace(@src());4930 const tracy = trace(@src());
4544 defer tracy.end();4931 defer tracy.end();
45454932
...@@ -4548,7 +4935,7 @@ fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -4548,7 +4935,7 @@ fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
4548 const ptr_ty = sema.typeOf(ptr);4935 const ptr_ty = sema.typeOf(ptr);
4549 if (ptr_ty.zigTypeTag() != .Pointer) {4936 if (ptr_ty.zigTypeTag() != .Pointer) {
4550 const ptr_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };4937 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});
4552 }4939 }
4553 // TODO handle known-pointer-address4940 // TODO handle known-pointer-address
4554 const src = inst_data.src();4941 const src = inst_data.src();
...@@ -4556,7 +4943,7 @@ fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -4556,7 +4943,7 @@ fn zirPtrToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
4556 return block.addUnOp(.ptrtoint, ptr);4943 return block.addUnOp(.ptrtoint, ptr);
4557}4944}
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 {
4560 const tracy = trace(@src());4947 const tracy = trace(@src());
4561 defer tracy.end();4948 defer tracy.end();
45624949
...@@ -4575,7 +4962,7 @@ fn zirFieldVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -4575,7 +4962,7 @@ fn zirFieldVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
4575 }4962 }
4576}4963}
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 {
4579 const tracy = trace(@src());4966 const tracy = trace(@src());
4580 defer tracy.end();4967 defer tracy.end();
45814968
...@@ -4588,7 +4975,7 @@ fn zirFieldPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -4588,7 +4975,7 @@ fn zirFieldPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
4588 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src);4975 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src);
4589}4976}
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 {
4592 const tracy = trace(@src());4979 const tracy = trace(@src());
4593 defer tracy.end();4980 defer tracy.end();
45944981
...@@ -4601,7 +4988,7 @@ fn zirFieldCallBind(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp...@@ -4601,7 +4988,7 @@ fn zirFieldCallBind(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
4601 return sema.fieldCallBind(block, src, object_ptr, field_name, field_name_src);4988 return sema.fieldCallBind(block, src, object_ptr, field_name, field_name_src);
4602}4989}
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 {
4605 const tracy = trace(@src());4992 const tracy = trace(@src());
4606 defer tracy.end();4993 defer tracy.end();
46074994
...@@ -4614,7 +5001,7 @@ fn zirFieldValNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp...@@ -4614,7 +5001,7 @@ fn zirFieldValNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
4614 return sema.fieldVal(block, src, object, field_name, field_name_src);5001 return sema.fieldVal(block, src, object, field_name, field_name_src);
4615}5002}
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 {
4618 const tracy = trace(@src());5005 const tracy = trace(@src());
4619 defer tracy.end();5006 defer tracy.end();
46205007
...@@ -4627,7 +5014,7 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp...@@ -4627,7 +5014,7 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
4627 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src);5014 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src);
4628}5015}
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 {
4631 const tracy = trace(@src());5018 const tracy = trace(@src());
4632 defer tracy.end();5019 defer tracy.end();
46335020
...@@ -4640,7 +5027,7 @@ fn zirFieldCallBindNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)...@@ -4640,7 +5027,7 @@ fn zirFieldCallBindNamed(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
4640 return sema.fieldCallBind(block, src, object_ptr, field_name, field_name_src);5027 return sema.fieldCallBind(block, src, object_ptr, field_name, field_name_src);
4641}5028}
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 {
4644 const tracy = trace(@src());5031 const tracy = trace(@src());
4645 defer tracy.end();5032 defer tracy.end();
46465033
...@@ -4659,14 +5046,14 @@ fn zirIntCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -4659,14 +5046,14 @@ fn zirIntCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
4659 if (try sema.isComptimeKnown(block, operand_src, operand)) {5046 if (try sema.isComptimeKnown(block, operand_src, operand)) {
4660 return sema.coerce(block, dest_type, operand, operand_src);5047 return sema.coerce(block, dest_type, operand, operand_src);
4661 } else if (dest_is_comptime_int) {5048 } 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'", .{});
4663 }5050 }
46645051
4665 try sema.requireRuntimeBlock(block, operand_src);5052 try sema.requireRuntimeBlock(block, operand_src);
4666 return block.addTyOp(.intcast, dest_type, operand);5053 return block.addTyOp(.intcast, dest_type, operand);
4667}5054}
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 {
4670 const tracy = trace(@src());5057 const tracy = trace(@src());
4671 defer tracy.end();5058 defer tracy.end();
46725059
...@@ -4680,7 +5067,7 @@ fn zirBitcast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -4680,7 +5067,7 @@ fn zirBitcast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
4680 return sema.bitcast(block, dest_type, operand, operand_src);5067 return sema.bitcast(block, dest_type, operand, operand_src);
4681}5068}
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 {
4684 const tracy = trace(@src());5071 const tracy = trace(@src());
4685 defer tracy.end();5072 defer tracy.end();
46865073
...@@ -4696,8 +5083,8 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -4696,8 +5083,8 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
4696 const dest_is_comptime_float = switch (dest_type.zigTypeTag()) {5083 const dest_is_comptime_float = switch (dest_type.zigTypeTag()) {
4697 .ComptimeFloat => true,5084 .ComptimeFloat => true,
4698 .Float => false,5085 .Float => false,
4699 else => return sema.mod.fail(5086 else => return sema.fail(
4700 &block.base,5087 block,
4701 dest_ty_src,5088 dest_ty_src,
4702 "expected float type, found '{}'",5089 "expected float type, found '{}'",
4703 .{dest_type},5090 .{dest_type},
...@@ -4707,8 +5094,8 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -4707,8 +5094,8 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
4707 const operand_ty = sema.typeOf(operand);5094 const operand_ty = sema.typeOf(operand);
4708 switch (operand_ty.zigTypeTag()) {5095 switch (operand_ty.zigTypeTag()) {
4709 .ComptimeFloat, .Float, .ComptimeInt => {},5096 .ComptimeFloat, .Float, .ComptimeInt => {},
4710 else => return sema.mod.fail(5097 else => return sema.fail(
4711 &block.base,5098 block,
4712 operand_src,5099 operand_src,
4713 "expected float type, found '{}'",5100 "expected float type, found '{}'",
4714 .{operand_ty},5101 .{operand_ty},
...@@ -4719,7 +5106,7 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -4719,7 +5106,7 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
4719 return sema.coerce(block, dest_type, operand, operand_src);5106 return sema.coerce(block, dest_type, operand, operand_src);
4720 }5107 }
4721 if (dest_is_comptime_float) {5108 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'", .{});
4723 }5110 }
4724 const target = sema.mod.getTarget();5111 const target = sema.mod.getTarget();
4725 const src_bits = operand_ty.floatBits(target);5112 const src_bits = operand_ty.floatBits(target);
...@@ -4731,7 +5118,7 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -4731,7 +5118,7 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
4731 return block.addTyOp(.fptrunc, dest_type, operand);5118 return block.addTyOp(.fptrunc, dest_type, operand);
4732}5119}
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 {
4735 const tracy = trace(@src());5122 const tracy = trace(@src());
4736 defer tracy.end();5123 defer tracy.end();
47375124
...@@ -4741,7 +5128,7 @@ fn zirElemVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -4741,7 +5128,7 @@ fn zirElemVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
4741 return sema.elemVal(block, sema.src, array, elem_index, sema.src);5128 return sema.elemVal(block, sema.src, array, elem_index, sema.src);
4742}5129}
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 {
4745 const tracy = trace(@src());5132 const tracy = trace(@src());
4746 defer tracy.end();5133 defer tracy.end();
47475134
...@@ -4754,7 +5141,7 @@ fn zirElemValNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -4754,7 +5141,7 @@ fn zirElemValNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
4754 return sema.elemVal(block, src, array, elem_index, elem_index_src);5141 return sema.elemVal(block, src, array, elem_index, elem_index_src);
4755}5142}
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 {
4758 const tracy = trace(@src());5145 const tracy = trace(@src());
4759 defer tracy.end();5146 defer tracy.end();
47605147
...@@ -4764,7 +5151,7 @@ fn zirElemPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -4764,7 +5151,7 @@ fn zirElemPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
4764 return sema.elemPtr(block, sema.src, array_ptr, elem_index, sema.src);5151 return sema.elemPtr(block, sema.src, array_ptr, elem_index, sema.src);
4765}5152}
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 {
4768 const tracy = trace(@src());5155 const tracy = trace(@src());
4769 defer tracy.end();5156 defer tracy.end();
47705157
...@@ -4777,7 +5164,7 @@ fn zirElemPtrNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -4777,7 +5164,7 @@ fn zirElemPtrNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
4777 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src);5164 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src);
4778}5165}
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 {
4781 const tracy = trace(@src());5168 const tracy = trace(@src());
4782 defer tracy.end();5169 defer tracy.end();
47835170
...@@ -4790,7 +5177,7 @@ fn zirSliceStart(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile...@@ -4790,7 +5177,7 @@ fn zirSliceStart(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
4790 return sema.analyzeSlice(block, src, array_ptr, start, .none, .none, .unneeded);5177 return sema.analyzeSlice(block, src, array_ptr, start, .none, .none, .unneeded);
4791}5178}
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 {
4794 const tracy = trace(@src());5181 const tracy = trace(@src());
4795 defer tracy.end();5182 defer tracy.end();
47965183
...@@ -4804,7 +5191,7 @@ fn zirSliceEnd(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -4804,7 +5191,7 @@ fn zirSliceEnd(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
4804 return sema.analyzeSlice(block, src, array_ptr, start, end, .none, .unneeded);5191 return sema.analyzeSlice(block, src, array_ptr, start, end, .none, .unneeded);
4805}5192}
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 {
4808 const tracy = trace(@src());5195 const tracy = trace(@src());
4809 defer tracy.end();5196 defer tracy.end();
48105197
...@@ -4822,7 +5209,7 @@ fn zirSliceSentinel(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp...@@ -4822,7 +5209,7 @@ fn zirSliceSentinel(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
48225209
4823fn zirSwitchCapture(5210fn zirSwitchCapture(
4824 sema: *Sema,5211 sema: *Sema,
4825 block: *Scope.Block,5212 block: *Block,
4826 inst: Zir.Inst.Index,5213 inst: Zir.Inst.Index,
4827 is_multi: bool,5214 is_multi: bool,
4828 is_ref: bool,5215 is_ref: bool,
...@@ -4837,12 +5224,12 @@ fn zirSwitchCapture(...@@ -4837,12 +5224,12 @@ fn zirSwitchCapture(
48375224
4838 _ = is_ref;5225 _ = is_ref;
4839 _ = is_multi;5226 _ = 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", .{});
4841}5228}
48425229
4843fn zirSwitchCaptureElse(5230fn zirSwitchCaptureElse(
4844 sema: *Sema,5231 sema: *Sema,
4845 block: *Scope.Block,5232 block: *Block,
4846 inst: Zir.Inst.Index,5233 inst: Zir.Inst.Index,
4847 is_ref: bool,5234 is_ref: bool,
4848) CompileError!Air.Inst.Ref {5235) CompileError!Air.Inst.Ref {
...@@ -4855,12 +5242,12 @@ fn zirSwitchCaptureElse(...@@ -4855,12 +5242,12 @@ fn zirSwitchCaptureElse(
4855 const src = switch_info.src();5242 const src = switch_info.src();
48565243
4857 _ = is_ref;5244 _ = 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", .{});
4859}5246}
48605247
4861fn zirSwitchBlock(5248fn zirSwitchBlock(
4862 sema: *Sema,5249 sema: *Sema,
4863 block: *Scope.Block,5250 block: *Block,
4864 inst: Zir.Inst.Index,5251 inst: Zir.Inst.Index,
4865 is_ref: bool,5252 is_ref: bool,
4866 special_prong: Zir.SpecialProng,5253 special_prong: Zir.SpecialProng,
...@@ -4893,7 +5280,7 @@ fn zirSwitchBlock(...@@ -4893,7 +5280,7 @@ fn zirSwitchBlock(
48935280
4894fn zirSwitchBlockMulti(5281fn zirSwitchBlockMulti(
4895 sema: *Sema,5282 sema: *Sema,
4896 block: *Scope.Block,5283 block: *Block,
4897 inst: Zir.Inst.Index,5284 inst: Zir.Inst.Index,
4898 is_ref: bool,5285 is_ref: bool,
4899 special_prong: Zir.SpecialProng,5286 special_prong: Zir.SpecialProng,
...@@ -4926,7 +5313,7 @@ fn zirSwitchBlockMulti(...@@ -4926,7 +5313,7 @@ fn zirSwitchBlockMulti(
49265313
4927fn analyzeSwitch(5314fn analyzeSwitch(
4928 sema: *Sema,5315 sema: *Sema,
4929 block: *Scope.Block,5316 block: *Block,
4930 operand: Air.Inst.Ref,5317 operand: Air.Inst.Ref,
4931 extra_end: usize,5318 extra_end: usize,
4932 special_prong: Zir.SpecialProng,5319 special_prong: Zir.SpecialProng,
...@@ -4936,7 +5323,6 @@ fn analyzeSwitch(...@@ -4936,7 +5323,6 @@ fn analyzeSwitch(
4936 src_node_offset: i32,5323 src_node_offset: i32,
4937) CompileError!Air.Inst.Ref {5324) CompileError!Air.Inst.Ref {
4938 const gpa = sema.gpa;5325 const gpa = sema.gpa;
4939 const mod = sema.mod;
49405326
4941 const special: struct { body: []const Zir.Inst.Index, end: usize } = switch (special_prong) {5327 const special: struct { body: []const Zir.Inst.Index, end: usize } = switch (special_prong) {
4942 .none => .{ .body = &.{}, .end = extra_end },5328 .none => .{ .body = &.{}, .end = extra_end },
...@@ -4958,15 +5344,15 @@ fn analyzeSwitch(...@@ -4958,15 +5344,15 @@ fn analyzeSwitch(
4958 // Validate usage of '_' prongs.5344 // Validate usage of '_' prongs.
4959 if (special_prong == .under and !operand_ty.isNonexhaustiveEnum()) {5345 if (special_prong == .under and !operand_ty.isNonexhaustiveEnum()) {
4960 const msg = msg: {5346 const msg = msg: {
4961 const msg = try mod.errMsg(5347 const msg = try sema.errMsg(
4962 &block.base,5348 block,
4963 src,5349 src,
4964 "'_' prong only allowed when switching on non-exhaustive enums",5350 "'_' prong only allowed when switching on non-exhaustive enums",
4965 .{},5351 .{},
4966 );5352 );
4967 errdefer msg.destroy(gpa);5353 errdefer msg.destroy(gpa);
4968 try mod.errNote(5354 try sema.errNote(
4969 &block.base,5355 block,
4970 special_prong_src,5356 special_prong_src,
4971 msg,5357 msg,
4972 "'_' prong here",5358 "'_' prong here",
...@@ -4974,7 +5360,7 @@ fn analyzeSwitch(...@@ -4974,7 +5360,7 @@ fn analyzeSwitch(
4974 );5360 );
4975 break :msg msg;5361 break :msg msg;
4976 };5362 };
4977 return mod.failWithOwnedErrorMsg(&block.base, msg);5363 return sema.failWithOwnedErrorMsg(msg);
4978 }5364 }
49795365
4980 // Validate for duplicate items, missing else prong, and invalid range.5366 // Validate for duplicate items, missing else prong, and invalid range.
...@@ -5037,8 +5423,8 @@ fn analyzeSwitch(...@@ -5037,8 +5423,8 @@ fn analyzeSwitch(
5037 .none => {5423 .none => {
5038 if (!all_tags_handled) {5424 if (!all_tags_handled) {
5039 const msg = msg: {5425 const msg = msg: {
5040 const msg = try mod.errMsg(5426 const msg = try sema.errMsg(
5041 &block.base,5427 block,
5042 src,5428 src,
5043 "switch must handle all possibilities",5429 "switch must handle all possibilities",
5044 .{},5430 .{},
...@@ -5050,15 +5436,15 @@ fn analyzeSwitch(...@@ -5050,15 +5436,15 @@ fn analyzeSwitch(
5050 const field_name = operand_ty.enumFieldName(i);5436 const field_name = operand_ty.enumFieldName(i);
50515437
5052 // TODO have this point to the tag decl instead of here5438 // TODO have this point to the tag decl instead of here
5053 try mod.errNote(5439 try sema.errNote(
5054 &block.base,5440 block,
5055 src,5441 src,
5056 msg,5442 msg,
5057 "unhandled enumeration value: '{s}'",5443 "unhandled enumeration value: '{s}'",
5058 .{field_name},5444 .{field_name},
5059 );5445 );
5060 }5446 }
5061 try mod.errNoteNonLazy(5447 try sema.mod.errNoteNonLazy(
5062 operand_ty.declSrcLoc(),5448 operand_ty.declSrcLoc(),
5063 msg,5449 msg,
5064 "enum '{}' declared here",5450 "enum '{}' declared here",
...@@ -5066,20 +5452,20 @@ fn analyzeSwitch(...@@ -5066,20 +5452,20 @@ fn analyzeSwitch(
5066 );5452 );
5067 break :msg msg;5453 break :msg msg;
5068 };5454 };
5069 return mod.failWithOwnedErrorMsg(&block.base, msg);5455 return sema.failWithOwnedErrorMsg(msg);
5070 }5456 }
5071 },5457 },
5072 .under => {5458 .under => {
5073 if (all_tags_handled) return mod.fail(5459 if (all_tags_handled) return sema.fail(
5074 &block.base,5460 block,
5075 special_prong_src,5461 special_prong_src,
5076 "unreachable '_' prong; all cases already handled",5462 "unreachable '_' prong; all cases already handled",
5077 .{},5463 .{},
5078 );5464 );
5079 },5465 },
5080 .@"else" => {5466 .@"else" => {
5081 if (all_tags_handled) return mod.fail(5467 if (all_tags_handled) return sema.fail(
5082 &block.base,5468 block,
5083 special_prong_src,5469 special_prong_src,
5084 "unreachable else prong; all cases already handled",5470 "unreachable else prong; all cases already handled",
5085 .{},5471 .{},
...@@ -5088,8 +5474,8 @@ fn analyzeSwitch(...@@ -5088,8 +5474,8 @@ fn analyzeSwitch(
5088 }5474 }
5089 },5475 },
50905476
5091 .ErrorSet => return mod.fail(&block.base, src, "TODO validate switch .ErrorSet", .{}),5477 .ErrorSet => return sema.fail(block, src, "TODO validate switch .ErrorSet", .{}),
5092 .Union => return mod.fail(&block.base, src, "TODO validate switch .Union", .{}),5478 .Union => return sema.fail(block, src, "TODO validate switch .Union", .{}),
5093 .Int, .ComptimeInt => {5479 .Int, .ComptimeInt => {
5094 var range_set = RangeSet.init(gpa);5480 var range_set = RangeSet.init(gpa);
5095 defer range_set.deinit();5481 defer range_set.deinit();
...@@ -5164,12 +5550,13 @@ fn analyzeSwitch(...@@ -5164,12 +5550,13 @@ fn analyzeSwitch(
5164 var arena = std.heap.ArenaAllocator.init(gpa);5550 var arena = std.heap.ArenaAllocator.init(gpa);
5165 defer arena.deinit();5551 defer arena.deinit();
51665552
5167 const min_int = try operand_ty.minInt(&arena.allocator, mod.getTarget());5553 const target = sema.mod.getTarget();
5168 const max_int = try operand_ty.maxInt(&arena.allocator, mod.getTarget());5554 const min_int = try operand_ty.minInt(&arena.allocator, target);
5555 const max_int = try operand_ty.maxInt(&arena.allocator, target);
5169 if (try range_set.spans(min_int, max_int, operand_ty)) {5556 if (try range_set.spans(min_int, max_int, operand_ty)) {
5170 if (special_prong == .@"else") {5557 if (special_prong == .@"else") {
5171 return mod.fail(5558 return sema.fail(
5172 &block.base,5559 block,
5173 special_prong_src,5560 special_prong_src,
5174 "unreachable else prong; all cases already handled",5561 "unreachable else prong; all cases already handled",
5175 .{},5562 .{},
...@@ -5179,8 +5566,8 @@ fn analyzeSwitch(...@@ -5179,8 +5566,8 @@ fn analyzeSwitch(
5179 }5566 }
5180 }5567 }
5181 if (special_prong != .@"else") {5568 if (special_prong != .@"else") {
5182 return mod.fail(5569 return sema.fail(
5183 &block.base,5570 block,
5184 src,5571 src,
5185 "switch must handle all possibilities",5572 "switch must handle all possibilities",
5186 .{},5573 .{},
...@@ -5241,8 +5628,8 @@ fn analyzeSwitch(...@@ -5241,8 +5628,8 @@ fn analyzeSwitch(
5241 switch (special_prong) {5628 switch (special_prong) {
5242 .@"else" => {5629 .@"else" => {
5243 if (true_count + false_count == 2) {5630 if (true_count + false_count == 2) {
5244 return mod.fail(5631 return sema.fail(
5245 &block.base,5632 block,
5246 src,5633 src,
5247 "unreachable else prong; all cases already handled",5634 "unreachable else prong; all cases already handled",
5248 .{},5635 .{},
...@@ -5251,8 +5638,8 @@ fn analyzeSwitch(...@@ -5251,8 +5638,8 @@ fn analyzeSwitch(
5251 },5638 },
5252 .under, .none => {5639 .under, .none => {
5253 if (true_count + false_count < 2) {5640 if (true_count + false_count < 2) {
5254 return mod.fail(5641 return sema.fail(
5255 &block.base,5642 block,
5256 src,5643 src,
5257 "switch must handle all possibilities",5644 "switch must handle all possibilities",
5258 .{},5645 .{},
...@@ -5263,8 +5650,8 @@ fn analyzeSwitch(...@@ -5263,8 +5650,8 @@ fn analyzeSwitch(
5263 },5650 },
5264 .EnumLiteral, .Void, .Fn, .Pointer, .Type => {5651 .EnumLiteral, .Void, .Fn, .Pointer, .Type => {
5265 if (special_prong != .@"else") {5652 if (special_prong != .@"else") {
5266 return mod.fail(5653 return sema.fail(
5267 &block.base,5654 block,
5268 src,5655 src,
5269 "else prong required when switching on type '{}'",5656 "else prong required when switching on type '{}'",
5270 .{operand_ty},5657 .{operand_ty},
...@@ -5334,7 +5721,7 @@ fn analyzeSwitch(...@@ -5334,7 +5721,7 @@ fn analyzeSwitch(
5334 .AnyFrame,5721 .AnyFrame,
5335 .ComptimeFloat,5722 .ComptimeFloat,
5336 .Float,5723 .Float,
5337 => return mod.fail(&block.base, operand_src, "invalid switch operand type '{}'", .{5724 => return sema.fail(block, operand_src, "invalid switch operand type '{}'", .{
5338 operand_ty,5725 operand_ty,
5339 }),5726 }),
5340 }5727 }
...@@ -5344,7 +5731,7 @@ fn analyzeSwitch(...@@ -5344,7 +5731,7 @@ fn analyzeSwitch(
5344 .tag = .block,5731 .tag = .block,
5345 .data = undefined,5732 .data = undefined,
5346 });5733 });
5347 var label: Scope.Block.Label = .{5734 var label: Block.Label = .{
5348 .zir_block = switch_inst,5735 .zir_block = switch_inst,
5349 .merges = .{5736 .merges = .{
5350 .results = .{},5737 .results = .{},
...@@ -5353,10 +5740,11 @@ fn analyzeSwitch(...@@ -5353,10 +5740,11 @@ fn analyzeSwitch(
5353 },5740 },
5354 };5741 };
53555742
5356 var child_block: Scope.Block = .{5743 var child_block: Block = .{
5357 .parent = block,5744 .parent = block,
5358 .sema = sema,5745 .sema = sema,
5359 .src_decl = block.src_decl,5746 .src_decl = block.src_decl,
5747 .namespace = block.namespace,
5360 .wip_capture_scope = block.wip_capture_scope,5748 .wip_capture_scope = block.wip_capture_scope,
5361 .instructions = .{},5749 .instructions = .{},
5362 .label = &label,5750 .label = &label,
...@@ -5663,7 +6051,7 @@ fn analyzeSwitch(...@@ -5663,7 +6051,7 @@ fn analyzeSwitch(
56636051
5664fn resolveSwitchItemVal(6052fn resolveSwitchItemVal(
5665 sema: *Sema,6053 sema: *Sema,
5666 block: *Scope.Block,6054 block: *Block,
5667 item_ref: Zir.Inst.Ref,6055 item_ref: Zir.Inst.Ref,
5668 switch_node_offset: i32,6056 switch_node_offset: i32,
5669 switch_prong_src: Module.SwitchProngSrc,6057 switch_prong_src: Module.SwitchProngSrc,
...@@ -5690,7 +6078,7 @@ fn resolveSwitchItemVal(...@@ -5690,7 +6078,7 @@ fn resolveSwitchItemVal(
56906078
5691fn validateSwitchRange(6079fn validateSwitchRange(
5692 sema: *Sema,6080 sema: *Sema,
5693 block: *Scope.Block,6081 block: *Block,
5694 range_set: *RangeSet,6082 range_set: *RangeSet,
5695 first_ref: Zir.Inst.Ref,6083 first_ref: Zir.Inst.Ref,
5696 last_ref: Zir.Inst.Ref,6084 last_ref: Zir.Inst.Ref,
...@@ -5706,7 +6094,7 @@ fn validateSwitchRange(...@@ -5706,7 +6094,7 @@ fn validateSwitchRange(
57066094
5707fn validateSwitchItem(6095fn validateSwitchItem(
5708 sema: *Sema,6096 sema: *Sema,
5709 block: *Scope.Block,6097 block: *Block,
5710 range_set: *RangeSet,6098 range_set: *RangeSet,
5711 item_ref: Zir.Inst.Ref,6099 item_ref: Zir.Inst.Ref,
5712 operand_ty: Type,6100 operand_ty: Type,
...@@ -5720,25 +6108,24 @@ fn validateSwitchItem(...@@ -5720,25 +6108,24 @@ fn validateSwitchItem(
57206108
5721fn validateSwitchItemEnum(6109fn validateSwitchItemEnum(
5722 sema: *Sema,6110 sema: *Sema,
5723 block: *Scope.Block,6111 block: *Block,
5724 seen_fields: []?Module.SwitchProngSrc,6112 seen_fields: []?Module.SwitchProngSrc,
5725 item_ref: Zir.Inst.Ref,6113 item_ref: Zir.Inst.Ref,
5726 src_node_offset: i32,6114 src_node_offset: i32,
5727 switch_prong_src: Module.SwitchProngSrc,6115 switch_prong_src: Module.SwitchProngSrc,
5728) CompileError!void {6116) CompileError!void {
5729 const mod = sema.mod;
5730 const item_tv = try sema.resolveSwitchItemVal(block, item_ref, src_node_offset, switch_prong_src, .none);6117 const item_tv = try sema.resolveSwitchItemVal(block, item_ref, src_node_offset, switch_prong_src, .none);
5731 const field_index = item_tv.ty.enumTagFieldIndex(item_tv.val) orelse {6118 const field_index = item_tv.ty.enumTagFieldIndex(item_tv.val) orelse {
5732 const msg = msg: {6119 const msg = msg: {
5733 const src = switch_prong_src.resolve(sema.gpa, block.src_decl, src_node_offset, .none);6120 const src = switch_prong_src.resolve(sema.gpa, block.src_decl, src_node_offset, .none);
5734 const msg = try mod.errMsg(6121 const msg = try sema.errMsg(
5735 &block.base,6122 block,
5736 src,6123 src,
5737 "enum '{}' has no tag with value '{}'",6124 "enum '{}' has no tag with value '{}'",
5738 .{ item_tv.ty, item_tv.val },6125 .{ item_tv.ty, item_tv.val },
5739 );6126 );
5740 errdefer msg.destroy(sema.gpa);6127 errdefer msg.destroy(sema.gpa);
5741 try mod.errNoteNonLazy(6128 try sema.mod.errNoteNonLazy(
5742 item_tv.ty.declSrcLoc(),6129 item_tv.ty.declSrcLoc(),
5743 msg,6130 msg,
5744 "enum declared here",6131 "enum declared here",
...@@ -5746,7 +6133,7 @@ fn validateSwitchItemEnum(...@@ -5746,7 +6133,7 @@ fn validateSwitchItemEnum(
5746 );6133 );
5747 break :msg msg;6134 break :msg msg;
5748 };6135 };
5749 return mod.failWithOwnedErrorMsg(&block.base, msg);6136 return sema.failWithOwnedErrorMsg(msg);
5750 };6137 };
5751 const maybe_prev_src = seen_fields[field_index];6138 const maybe_prev_src = seen_fields[field_index];
5752 seen_fields[field_index] = switch_prong_src;6139 seen_fields[field_index] = switch_prong_src;
...@@ -5755,26 +6142,25 @@ fn validateSwitchItemEnum(...@@ -5755,26 +6142,25 @@ fn validateSwitchItemEnum(
57556142
5756fn validateSwitchDupe(6143fn validateSwitchDupe(
5757 sema: *Sema,6144 sema: *Sema,
5758 block: *Scope.Block,6145 block: *Block,
5759 maybe_prev_src: ?Module.SwitchProngSrc,6146 maybe_prev_src: ?Module.SwitchProngSrc,
5760 switch_prong_src: Module.SwitchProngSrc,6147 switch_prong_src: Module.SwitchProngSrc,
5761 src_node_offset: i32,6148 src_node_offset: i32,
5762) CompileError!void {6149) CompileError!void {
5763 const prev_prong_src = maybe_prev_src orelse return;6150 const prev_prong_src = maybe_prev_src orelse return;
5764 const mod = sema.mod;
5765 const gpa = sema.gpa;6151 const gpa = sema.gpa;
5766 const src = switch_prong_src.resolve(gpa, block.src_decl, src_node_offset, .none);6152 const src = switch_prong_src.resolve(gpa, block.src_decl, src_node_offset, .none);
5767 const prev_src = prev_prong_src.resolve(gpa, block.src_decl, src_node_offset, .none);6153 const prev_src = prev_prong_src.resolve(gpa, block.src_decl, src_node_offset, .none);
5768 const msg = msg: {6154 const msg = msg: {
5769 const msg = try mod.errMsg(6155 const msg = try sema.errMsg(
5770 &block.base,6156 block,
5771 src,6157 src,
5772 "duplicate switch value",6158 "duplicate switch value",
5773 .{},6159 .{},
5774 );6160 );
5775 errdefer msg.destroy(sema.gpa);6161 errdefer msg.destroy(sema.gpa);
5776 try mod.errNote(6162 try sema.errNote(
5777 &block.base,6163 block,
5778 prev_src,6164 prev_src,
5779 msg,6165 msg,
5780 "previous value here",6166 "previous value here",
...@@ -5782,12 +6168,12 @@ fn validateSwitchDupe(...@@ -5782,12 +6168,12 @@ fn validateSwitchDupe(
5782 );6168 );
5783 break :msg msg;6169 break :msg msg;
5784 };6170 };
5785 return mod.failWithOwnedErrorMsg(&block.base, msg);6171 return sema.failWithOwnedErrorMsg(msg);
5786}6172}
57876173
5788fn validateSwitchItemBool(6174fn validateSwitchItemBool(
5789 sema: *Sema,6175 sema: *Sema,
5790 block: *Scope.Block,6176 block: *Block,
5791 true_count: *u8,6177 true_count: *u8,
5792 false_count: *u8,6178 false_count: *u8,
5793 item_ref: Zir.Inst.Ref,6179 item_ref: Zir.Inst.Ref,
...@@ -5802,7 +6188,7 @@ fn validateSwitchItemBool(...@@ -5802,7 +6188,7 @@ fn validateSwitchItemBool(
5802 }6188 }
5803 if (true_count.* + false_count.* > 2) {6189 if (true_count.* + false_count.* > 2) {
5804 const src = switch_prong_src.resolve(sema.gpa, block.src_decl, src_node_offset, .none);6190 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", .{});
5806 }6192 }
5807}6193}
58086194
...@@ -5810,7 +6196,7 @@ const ValueSrcMap = std.HashMap(Value, Module.SwitchProngSrc, Value.HashContext,...@@ -5810,7 +6196,7 @@ const ValueSrcMap = std.HashMap(Value, Module.SwitchProngSrc, Value.HashContext,
58106196
5811fn validateSwitchItemSparse(6197fn validateSwitchItemSparse(
5812 sema: *Sema,6198 sema: *Sema,
5813 block: *Scope.Block,6199 block: *Block,
5814 seen_values: *ValueSrcMap,6200 seen_values: *ValueSrcMap,
5815 item_ref: Zir.Inst.Ref,6201 item_ref: Zir.Inst.Ref,
5816 src_node_offset: i32,6202 src_node_offset: i32,
...@@ -5823,7 +6209,7 @@ fn validateSwitchItemSparse(...@@ -5823,7 +6209,7 @@ fn validateSwitchItemSparse(
58236209
5824fn validateSwitchNoRange(6210fn validateSwitchNoRange(
5825 sema: *Sema,6211 sema: *Sema,
5826 block: *Scope.Block,6212 block: *Block,
5827 ranges_len: u32,6213 ranges_len: u32,
5828 operand_ty: Type,6214 operand_ty: Type,
5829 src_node_offset: i32,6215 src_node_offset: i32,
...@@ -5835,15 +6221,15 @@ fn validateSwitchNoRange(...@@ -5835,15 +6221,15 @@ fn validateSwitchNoRange(
5835 const range_src: LazySrcLoc = .{ .node_offset_switch_range = src_node_offset };6221 const range_src: LazySrcLoc = .{ .node_offset_switch_range = src_node_offset };
58366222
5837 const msg = msg: {6223 const msg = msg: {
5838 const msg = try sema.mod.errMsg(6224 const msg = try sema.errMsg(
5839 &block.base,6225 block,
5840 operand_src,6226 operand_src,
5841 "ranges not allowed when switching on type '{}'",6227 "ranges not allowed when switching on type '{}'",
5842 .{operand_ty},6228 .{operand_ty},
5843 );6229 );
5844 errdefer msg.destroy(sema.gpa);6230 errdefer msg.destroy(sema.gpa);
5845 try sema.mod.errNote(6231 try sema.errNote(
5846 &block.base,6232 block,
5847 range_src,6233 range_src,
5848 msg,6234 msg,
5849 "range here",6235 "range here",
...@@ -5851,19 +6237,19 @@ fn validateSwitchNoRange(...@@ -5851,19 +6237,19 @@ fn validateSwitchNoRange(
5851 );6237 );
5852 break :msg msg;6238 break :msg msg;
5853 };6239 };
5854 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);6240 return sema.failWithOwnedErrorMsg(msg);
5855}6241}
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 {
5858 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;6244 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5859 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;6245 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
5860 _ = extra;6246 _ = extra;
5861 const src = inst_data.src();6247 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", .{});
5864}6250}
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 {
5867 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;6253 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5868 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;6254 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
5869 const src = inst_data.src();6255 const src = inst_data.src();
...@@ -5871,23 +6257,22 @@ fn zirHasDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -5871,23 +6257,22 @@ fn zirHasDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
5871 const rhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };6257 const rhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
5872 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);6258 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);
5873 const decl_name = try sema.resolveConstString(block, rhs_src, extra.rhs);6259 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(6261 const namespace = container_type.getNamespace() orelse return sema.fail(
5877 &block.base,6262 block,
5878 lhs_src,6263 lhs_src,
5879 "expected struct, enum, union, or opaque, found '{}'",6264 "expected struct, enum, union, or opaque, found '{}'",
5880 .{container_type},6265 .{container_type},
5881 );6266 );
5882 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl| {6267 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()) {
5884 return Air.Inst.Ref.bool_true;6269 return Air.Inst.Ref.bool_true;
5885 }6270 }
5886 }6271 }
5887 return Air.Inst.Ref.bool_false;6272 return Air.Inst.Ref.bool_false;
5888}6273}
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 {
5891 const tracy = trace(@src());6276 const tracy = trace(@src());
5892 defer tracy.end();6277 defer tracy.end();
58936278
...@@ -5898,29 +6283,29 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -5898,29 +6283,29 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
58986283
5899 const result = mod.importFile(block.getFileScope(), operand) catch |err| switch (err) {6284 const result = mod.importFile(block.getFileScope(), operand) catch |err| switch (err) {
5900 error.ImportOutsidePkgPath => {6285 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});
5902 },6287 },
5903 else => {6288 else => {
5904 // TODO: these errors are file system errors; make sure an update() will6289 // TODO: these errors are file system errors; make sure an update() will
5905 // retry this and not cache the file system error, which may be transient.6290 // 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) });
5907 },6292 },
5908 };6293 };
5909 try mod.semaFile(result.file);6294 try mod.semaFile(result.file);
5910 const file_root_decl = result.file.root_decl.?;6295 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);
5912 return sema.addConstant(file_root_decl.ty, file_root_decl.val);6297 return sema.addConstant(file_root_decl.ty, file_root_decl.val);
5913}6298}
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 {
5916 _ = block;6301 _ = block;
5917 _ = inst;6302 _ = inst;
5918 return sema.mod.fail(&block.base, sema.src, "TODO implement zirRetErrValueCode", .{});6303 return sema.fail(block, sema.src, "TODO implement zirRetErrValueCode", .{});
5919}6304}
59206305
5921fn zirShl(6306fn zirShl(
5922 sema: *Sema,6307 sema: *Sema,
5923 block: *Scope.Block,6308 block: *Block,
5924 inst: Zir.Inst.Index,6309 inst: Zir.Inst.Index,
5925 air_tag: Air.Inst.Tag,6310 air_tag: Air.Inst.Tag,
5926) CompileError!Air.Inst.Ref {6311) CompileError!Air.Inst.Ref {
...@@ -5952,8 +6337,8 @@ fn zirShl(...@@ -5952,8 +6337,8 @@ fn zirShl(
5952 }6337 }
5953 const val = try lhs_val.shl(rhs_val, sema.arena);6338 const val = try lhs_val.shl(rhs_val, sema.arena);
5954 switch (air_tag) {6339 switch (air_tag) {
5955 .shl_exact => return sema.mod.fail(&block.base, lhs_src, "TODO implement Sema for comptime shl_exact", .{}),6340 .shl_exact => return sema.fail(block, 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", .{}),6341 .shl_sat => return sema.fail(block, lhs_src, "TODO implement Sema for comptime shl_sat", .{}),
5957 .shl => {},6342 .shl => {},
5958 else => unreachable,6343 else => unreachable,
5959 }6344 }
...@@ -5971,7 +6356,7 @@ fn zirShl(...@@ -5971,7 +6356,7 @@ fn zirShl(
5971 return block.addBinOp(air_tag, lhs, rhs);6356 return block.addBinOp(air_tag, lhs, rhs);
5972}6357}
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 {
5975 const tracy = trace(@src());6360 const tracy = trace(@src());
5976 defer tracy.end();6361 defer tracy.end();
59776362
...@@ -6004,7 +6389,7 @@ fn zirShr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A...@@ -6004,7 +6389,7 @@ fn zirShr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
60046389
6005fn zirBitwise(6390fn zirBitwise(
6006 sema: *Sema,6391 sema: *Sema,
6007 block: *Scope.Block,6392 block: *Block,
6008 inst: Zir.Inst.Index,6393 inst: Zir.Inst.Index,
6009 air_tag: Air.Inst.Tag,6394 air_tag: Air.Inst.Tag,
6010) CompileError!Air.Inst.Ref {6395) CompileError!Air.Inst.Ref {
...@@ -6035,14 +6420,14 @@ fn zirBitwise(...@@ -6035,14 +6420,14 @@ fn zirBitwise(
60356420
6036 if (lhs_ty.zigTypeTag() == .Vector and rhs_ty.zigTypeTag() == .Vector) {6421 if (lhs_ty.zigTypeTag() == .Vector and rhs_ty.zigTypeTag() == .Vector) {
6037 if (lhs_ty.arrayLen() != rhs_ty.arrayLen()) {6422 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}", .{
6039 lhs_ty.arrayLen(),6424 lhs_ty.arrayLen(),
6040 rhs_ty.arrayLen(),6425 rhs_ty.arrayLen(),
6041 });6426 });
6042 }6427 }
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", .{});
6044 } else if (lhs_ty.zigTypeTag() == .Vector or rhs_ty.zigTypeTag() == .Vector) {6429 } 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 '{}'", .{
6046 lhs_ty,6431 lhs_ty,
6047 rhs_ty,6432 rhs_ty,
6048 });6433 });
...@@ -6051,7 +6436,7 @@ fn zirBitwise(...@@ -6051,7 +6436,7 @@ fn zirBitwise(
6051 const is_int = scalar_tag == .Int or scalar_tag == .ComptimeInt;6436 const is_int = scalar_tag == .Int or scalar_tag == .ComptimeInt;
60526437
6053 if (!is_int) {6438 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()) });
6055 }6440 }
60566441
6057 if (try sema.resolveMaybeUndefVal(block, lhs_src, casted_lhs)) |lhs_val| {6442 if (try sema.resolveMaybeUndefVal(block, lhs_src, casted_lhs)) |lhs_val| {
...@@ -6070,15 +6455,15 @@ fn zirBitwise(...@@ -6070,15 +6455,15 @@ fn zirBitwise(
6070 return block.addBinOp(air_tag, casted_lhs, casted_rhs);6455 return block.addBinOp(air_tag, casted_lhs, casted_rhs);
6071}6456}
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 {
6074 const tracy = trace(@src());6459 const tracy = trace(@src());
6075 defer tracy.end();6460 defer tracy.end();
60766461
6077 _ = inst;6462 _ = inst;
6078 return sema.mod.fail(&block.base, sema.src, "TODO implement zirBitNot", .{});6463 return sema.fail(block, sema.src, "TODO implement zirBitNot", .{});
6079}6464}
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 {
6082 const tracy = trace(@src());6467 const tracy = trace(@src());
6083 defer tracy.end();6468 defer tracy.end();
60846469
...@@ -6092,11 +6477,11 @@ fn zirArrayCat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -6092,11 +6477,11 @@ fn zirArrayCat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
6092 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };6477 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
60936478
6094 const lhs_info = getArrayCatInfo(lhs_ty) orelse6479 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});
6096 const rhs_info = getArrayCatInfo(rhs_ty) orelse6481 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});
6098 if (!lhs_info.elem_type.eql(rhs_info.elem_type)) {6483 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 });
6100 }6485 }
61016486
6102 // When there is a sentinel mismatch, no sentinel on the result. The type system6487 // 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...@@ -6142,10 +6527,10 @@ fn zirArrayCat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
6142 else6527 else
6143 sema.analyzeDeclVal(block, .unneeded, try anon_decl.finish(ty, val));6528 sema.analyzeDeclVal(block, .unneeded, try anon_decl.finish(ty, val));
6144 } else {6529 } 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", .{});
6146 }6531 }
6147 } else {6532 } 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", .{});
6149 }6534 }
6150}6535}
61516536
...@@ -6162,7 +6547,7 @@ fn getArrayCatInfo(t: Type) ?Type.ArrayInfo {...@@ -6162,7 +6547,7 @@ fn getArrayCatInfo(t: Type) ?Type.ArrayInfo {
6162 };6547 };
6163}6548}
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 {
6166 const tracy = trace(@src());6551 const tracy = trace(@src());
6167 defer tracy.end();6552 defer tracy.end();
61686553
...@@ -6176,9 +6561,9 @@ fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -6176,9 +6561,9 @@ fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
6176 // In `**` rhs has to be comptime-known, but lhs can be runtime-known6561 // In `**` rhs has to be comptime-known, but lhs can be runtime-known
6177 const tomulby = try sema.resolveInt(block, rhs_src, extra.rhs, Type.initTag(.usize));6562 const tomulby = try sema.resolveInt(block, rhs_src, extra.rhs, Type.initTag(.usize));
6178 const mulinfo = getArrayCatInfo(lhs_ty) orelse6563 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", .{});
6182 if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| {6567 if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| {
6183 var anon_decl = try block.startAnonDecl();6568 var anon_decl = try block.startAnonDecl();
6184 defer anon_decl.deinit();6569 defer anon_decl.deinit();
...@@ -6211,12 +6596,12 @@ fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -6211,12 +6596,12 @@ fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
6211 return sema.analyzeDeclVal(block, .unneeded, try anon_decl.finish(final_ty, val));6596 return sema.analyzeDeclVal(block, .unneeded, try anon_decl.finish(final_ty, val));
6212 }6597 }
6213 }6598 }
6214 return sema.mod.fail(&block.base, lhs_src, "TODO runtime array_mul", .{});6599 return sema.fail(block, lhs_src, "TODO runtime array_mul", .{});
6215}6600}
62166601
6217fn zirNegate(6602fn zirNegate(
6218 sema: *Sema,6603 sema: *Sema,
6219 block: *Scope.Block,6604 block: *Block,
6220 inst: Zir.Inst.Index,6605 inst: Zir.Inst.Index,
6221 tag_override: Zir.Inst.Tag,6606 tag_override: Zir.Inst.Tag,
6222) CompileError!Air.Inst.Ref {6607) CompileError!Air.Inst.Ref {
...@@ -6235,7 +6620,7 @@ fn zirNegate(...@@ -6235,7 +6620,7 @@ fn zirNegate(
62356620
6236fn zirArithmetic(6621fn zirArithmetic(
6237 sema: *Sema,6622 sema: *Sema,
6238 block: *Scope.Block,6623 block: *Block,
6239 inst: Zir.Inst.Index,6624 inst: Zir.Inst.Index,
6240 zir_tag: Zir.Inst.Tag,6625 zir_tag: Zir.Inst.Tag,
6241) CompileError!Air.Inst.Ref {6626) CompileError!Air.Inst.Ref {
...@@ -6255,7 +6640,7 @@ fn zirArithmetic(...@@ -6255,7 +6640,7 @@ fn zirArithmetic(
62556640
6256fn zirOverflowArithmetic(6641fn zirOverflowArithmetic(
6257 sema: *Sema,6642 sema: *Sema,
6258 block: *Scope.Block,6643 block: *Block,
6259 extended: Zir.Inst.Extended.InstData,6644 extended: Zir.Inst.Extended.InstData,
6260) CompileError!Air.Inst.Ref {6645) CompileError!Air.Inst.Ref {
6261 const tracy = trace(@src());6646 const tracy = trace(@src());
...@@ -6264,12 +6649,12 @@ fn zirOverflowArithmetic(...@@ -6264,12 +6649,12 @@ fn zirOverflowArithmetic(
6264 const extra = sema.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;6649 const extra = sema.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
6265 const src: LazySrcLoc = .{ .node_offset = extra.node };6650 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", .{});
6268}6653}
62696654
6270fn analyzeArithmetic(6655fn analyzeArithmetic(
6271 sema: *Sema,6656 sema: *Sema,
6272 block: *Scope.Block,6657 block: *Block,
6273 /// TODO performance investigation: make this comptime?6658 /// TODO performance investigation: make this comptime?
6274 zir_tag: Zir.Inst.Tag,6659 zir_tag: Zir.Inst.Tag,
6275 lhs: Air.Inst.Ref,6660 lhs: Air.Inst.Ref,
...@@ -6284,13 +6669,13 @@ fn analyzeArithmetic(...@@ -6284,13 +6669,13 @@ fn analyzeArithmetic(
6284 const rhs_zig_ty_tag = try rhs_ty.zigTypeTagOrPoison();6669 const rhs_zig_ty_tag = try rhs_ty.zigTypeTagOrPoison();
6285 if (lhs_zig_ty_tag == .Vector and rhs_zig_ty_tag == .Vector) {6670 if (lhs_zig_ty_tag == .Vector and rhs_zig_ty_tag == .Vector) {
6286 if (lhs_ty.arrayLen() != rhs_ty.arrayLen()) {6671 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}", .{
6288 lhs_ty.arrayLen(), rhs_ty.arrayLen(),6673 lhs_ty.arrayLen(), rhs_ty.arrayLen(),
6289 });6674 });
6290 }6675 }
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", .{});
6292 } else if (lhs_zig_ty_tag == .Vector or rhs_zig_ty_tag == .Vector) {6677 } 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 '{}'", .{
6294 lhs_ty, rhs_ty,6679 lhs_ty, rhs_ty,
6295 });6680 });
6296 }6681 }
...@@ -6302,8 +6687,8 @@ fn analyzeArithmetic(...@@ -6302,8 +6687,8 @@ fn analyzeArithmetic(
6302 const air_tag: Air.Inst.Tag = switch (zir_tag) {6687 const air_tag: Air.Inst.Tag = switch (zir_tag) {
6303 .add => .ptr_add,6688 .add => .ptr_add,
6304 .sub => .ptr_sub,6689 .sub => .ptr_sub,
6305 else => return sema.mod.fail(6690 else => return sema.fail(
6306 &block.base,6691 block,
6307 op_src,6692 op_src,
6308 "invalid pointer arithmetic operand: '{s}''",6693 "invalid pointer arithmetic operand: '{s}''",
6309 .{@tagName(zir_tag)},6694 .{@tagName(zir_tag)},
...@@ -6317,7 +6702,7 @@ fn analyzeArithmetic(...@@ -6317,7 +6702,7 @@ fn analyzeArithmetic(
6317 if (try sema.resolveDefinedValue(block, rhs_src, casted_rhs)) |rhs_val| {6702 if (try sema.resolveDefinedValue(block, rhs_src, casted_rhs)) |rhs_val| {
6318 _ = lhs_val;6703 _ = lhs_val;
6319 _ = rhs_val;6704 _ = 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", .{});
6321 } else {6706 } else {
6322 break :runtime_src rhs_src;6707 break :runtime_src rhs_src;
6323 }6708 }
...@@ -6348,7 +6733,7 @@ fn analyzeArithmetic(...@@ -6348,7 +6733,7 @@ fn analyzeArithmetic(
6348 const is_float = scalar_tag == .Float or scalar_tag == .ComptimeFloat;6733 const is_float = scalar_tag == .Float or scalar_tag == .ComptimeFloat;
63496734
6350 if (!is_int and !(is_float and floatOpAllowed(zir_tag))) {6735 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}'", .{
6352 @tagName(lhs_zig_ty_tag), @tagName(rhs_zig_ty_tag),6737 @tagName(lhs_zig_ty_tag), @tagName(rhs_zig_ty_tag),
6353 });6738 });
6354 }6739 }
...@@ -6930,7 +7315,7 @@ fn analyzeArithmetic(...@@ -6930,7 +7315,7 @@ fn analyzeArithmetic(
6930 return block.addBinOp(rs.air_tag, casted_lhs, casted_rhs);7315 return block.addBinOp(rs.air_tag, casted_lhs, casted_rhs);
6931}7316}
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 {
6934 const tracy = trace(@src());7319 const tracy = trace(@src());
6935 defer tracy.end();7320 defer tracy.end();
69367321
...@@ -6943,7 +7328,7 @@ fn zirLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!...@@ -6943,7 +7328,7 @@ fn zirLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!
69437328
6944fn zirAsm(7329fn zirAsm(
6945 sema: *Sema,7330 sema: *Sema,
6946 block: *Scope.Block,7331 block: *Block,
6947 extended: Zir.Inst.Extended.InstData,7332 extended: Zir.Inst.Extended.InstData,
6948 inst: Zir.Inst.Index,7333 inst: Zir.Inst.Index,
6949) CompileError!Air.Inst.Ref {7334) CompileError!Air.Inst.Ref {
...@@ -6958,7 +7343,7 @@ fn zirAsm(...@@ -6958,7 +7343,7 @@ fn zirAsm(
6958 const clobbers_len = @truncate(u5, extended.small >> 10);7343 const clobbers_len = @truncate(u5, extended.small >> 10);
69597344
6960 if (outputs_len > 1) {7345 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", .{});
6962 }7347 }
69637348
6964 var extra_i = extra.end;7349 var extra_i = extra.end;
...@@ -6973,7 +7358,7 @@ fn zirAsm(...@@ -6973,7 +7358,7 @@ fn zirAsm(
6973 output_type_bits >>= 1;7358 output_type_bits >>= 1;
69747359
6975 if (!is_type) {7360 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", .{});
6977 }7362 }
69787363
6979 const constraint = sema.code.nullTerminatedString(output.data.constraint);7364 const constraint = sema.code.nullTerminatedString(output.data.constraint);
...@@ -7022,7 +7407,7 @@ fn zirAsm(...@@ -7022,7 +7407,7 @@ fn zirAsm(
7022/// Only called for equality operators. See also `zirCmp`.7407/// Only called for equality operators. See also `zirCmp`.
7023fn zirCmpEq(7408fn zirCmpEq(
7024 sema: *Sema,7409 sema: *Sema,
7025 block: *Scope.Block,7410 block: *Block,
7026 inst: Zir.Inst.Index,7411 inst: Zir.Inst.Index,
7027 op: std.math.CompareOperator,7412 op: std.math.CompareOperator,
7028 air_tag: Air.Inst.Tag,7413 air_tag: Air.Inst.Tag,
...@@ -7030,7 +7415,6 @@ fn zirCmpEq(...@@ -7030,7 +7415,6 @@ fn zirCmpEq(
7030 const tracy = trace(@src());7415 const tracy = trace(@src());
7031 defer tracy.end();7416 defer tracy.end();
70327417
7033 const mod = sema.mod;
7034 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;7418 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
7035 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;7419 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
7036 const src: LazySrcLoc = inst_data.src();7420 const src: LazySrcLoc = inst_data.src();
...@@ -7059,11 +7443,11 @@ fn zirCmpEq(...@@ -7059,11 +7443,11 @@ fn zirCmpEq(
7059 return sema.analyzeIsNull(block, src, opt_operand, op == .neq);7443 return sema.analyzeIsNull(block, src, opt_operand, op == .neq);
7060 }7444 }
7061 if (((lhs_ty_tag == .Null and rhs_ty.isCPtr()) or (rhs_ty_tag == .Null and lhs_ty.isCPtr()))) {7445 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", .{});
7063 }7447 }
7064 if (lhs_ty_tag == .Null or rhs_ty_tag == .Null) {7448 if (lhs_ty_tag == .Null or rhs_ty_tag == .Null) {
7065 const non_null_type = if (lhs_ty_tag == .Null) rhs_ty else lhs_ty;7449 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});
7067 }7451 }
7068 if (lhs_ty_tag == .EnumLiteral and rhs_ty_tag == .Union) {7452 if (lhs_ty_tag == .EnumLiteral and rhs_ty_tag == .Union) {
7069 return sema.analyzeCmpUnionTag(block, rhs, rhs_src, lhs, lhs_src, op);7453 return sema.analyzeCmpUnionTag(block, rhs, rhs_src, lhs, lhs_src, op);
...@@ -7112,7 +7496,7 @@ fn zirCmpEq(...@@ -7112,7 +7496,7 @@ fn zirCmpEq(
71127496
7113fn analyzeCmpUnionTag(7497fn analyzeCmpUnionTag(
7114 sema: *Sema,7498 sema: *Sema,
7115 block: *Scope.Block,7499 block: *Block,
7116 un: Air.Inst.Ref,7500 un: Air.Inst.Ref,
7117 un_src: LazySrcLoc,7501 un_src: LazySrcLoc,
7118 tag: Air.Inst.Ref,7502 tag: Air.Inst.Ref,
...@@ -7122,7 +7506,7 @@ fn analyzeCmpUnionTag(...@@ -7122,7 +7506,7 @@ fn analyzeCmpUnionTag(
7122 const union_ty = sema.typeOf(un);7506 const union_ty = sema.typeOf(un);
7123 const union_tag_ty = union_ty.unionTagType() orelse {7507 const union_tag_ty = union_ty.unionTagType() orelse {
7124 // TODO note at declaration site that says "union foo is not tagged"7508 // 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", .{});
7126 };7510 };
7127 // Coerce both the union and the tag to the union's tag type, and then execute the7511 // Coerce both the union and the tag to the union's tag type, and then execute the
7128 // enum comparison codepath.7512 // enum comparison codepath.
...@@ -7135,7 +7519,7 @@ fn analyzeCmpUnionTag(...@@ -7135,7 +7519,7 @@ fn analyzeCmpUnionTag(
7135/// Only called for non-equality operators. See also `zirCmpEq`.7519/// Only called for non-equality operators. See also `zirCmpEq`.
7136fn zirCmp(7520fn zirCmp(
7137 sema: *Sema,7521 sema: *Sema,
7138 block: *Scope.Block,7522 block: *Block,
7139 inst: Zir.Inst.Index,7523 inst: Zir.Inst.Index,
7140 op: std.math.CompareOperator,7524 op: std.math.CompareOperator,
7141) CompileError!Air.Inst.Ref {7525) CompileError!Air.Inst.Ref {
...@@ -7154,7 +7538,7 @@ fn zirCmp(...@@ -7154,7 +7538,7 @@ fn zirCmp(
71547538
7155fn analyzeCmp(7539fn analyzeCmp(
7156 sema: *Sema,7540 sema: *Sema,
7157 block: *Scope.Block,7541 block: *Block,
7158 src: LazySrcLoc,7542 src: LazySrcLoc,
7159 lhs: Air.Inst.Ref,7543 lhs: Air.Inst.Ref,
7160 rhs: Air.Inst.Ref,7544 rhs: Air.Inst.Ref,
...@@ -7174,7 +7558,7 @@ fn analyzeCmp(...@@ -7174,7 +7558,7 @@ fn analyzeCmp(
7174 const instructions = &[_]Air.Inst.Ref{ lhs, rhs };7558 const instructions = &[_]Air.Inst.Ref{ lhs, rhs };
7175 const resolved_type = try sema.resolvePeerTypes(block, src, instructions, .{ .override = &[_]LazySrcLoc{ lhs_src, rhs_src } });7559 const resolved_type = try sema.resolvePeerTypes(block, src, instructions, .{ .override = &[_]LazySrcLoc{ lhs_src, rhs_src } });
7176 if (!resolved_type.isSelfComparable(is_equality_cmp)) {7560 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 '{}'", .{
7178 @tagName(op), resolved_type,7562 @tagName(op), resolved_type,
7179 });7563 });
7180 }7564 }
...@@ -7185,7 +7569,7 @@ fn analyzeCmp(...@@ -7185,7 +7569,7 @@ fn analyzeCmp(
71857569
7186fn cmpSelf(7570fn cmpSelf(
7187 sema: *Sema,7571 sema: *Sema,
7188 block: *Scope.Block,7572 block: *Block,
7189 casted_lhs: Air.Inst.Ref,7573 casted_lhs: Air.Inst.Ref,
7190 casted_rhs: Air.Inst.Ref,7574 casted_rhs: Air.Inst.Ref,
7191 op: std.math.CompareOperator,7575 op: std.math.CompareOperator,
...@@ -7243,7 +7627,7 @@ fn cmpSelf(...@@ -7243,7 +7627,7 @@ fn cmpSelf(
7243/// cmp_neq(x, true ) => not(x)7627/// cmp_neq(x, true ) => not(x)
7244fn runtimeBoolCmp(7628fn runtimeBoolCmp(
7245 sema: *Sema,7629 sema: *Sema,
7246 block: *Scope.Block,7630 block: *Block,
7247 op: std.math.CompareOperator,7631 op: std.math.CompareOperator,
7248 lhs: Air.Inst.Ref,7632 lhs: Air.Inst.Ref,
7249 rhs: bool,7633 rhs: bool,
...@@ -7257,7 +7641,7 @@ fn runtimeBoolCmp(...@@ -7257,7 +7641,7 @@ fn runtimeBoolCmp(
7257 }7641 }
7258}7642}
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 {
7261 const inst_data = sema.code.instructions.items(.data)[inst].un_node;7645 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
7262 const src = inst_data.src();7646 const src = inst_data.src();
7263 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };7647 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...@@ -7271,7 +7655,7 @@ fn zirSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
7271 .Null,7655 .Null,
7272 .BoundFn,7656 .BoundFn,
7273 .Opaque,7657 .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}),
7275 .Type,7659 .Type,
7276 .EnumLiteral,7660 .EnumLiteral,
7277 .ComptimeFloat,7661 .ComptimeFloat,
...@@ -7298,7 +7682,7 @@ fn zirSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -7298,7 +7682,7 @@ fn zirSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
7298 return sema.addIntUnsigned(Type.initTag(.comptime_int), abi_size);7682 return sema.addIntUnsigned(Type.initTag(.comptime_int), abi_size);
7299}7683}
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 {
7302 const inst_data = sema.code.instructions.items(.data)[inst].un_node;7686 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
7303 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };7687 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
7304 const operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);7688 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...@@ -7309,17 +7693,17 @@ fn zirBitSizeOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
73097693
7310fn zirThis(7694fn zirThis(
7311 sema: *Sema,7695 sema: *Sema,
7312 block: *Scope.Block,7696 block: *Block,
7313 extended: Zir.Inst.Extended.InstData,7697 extended: Zir.Inst.Extended.InstData,
7314) CompileError!Air.Inst.Ref {7698) CompileError!Air.Inst.Ref {
7315 const this_decl = block.base.namespace().getDecl();7699 const this_decl = block.namespace.getDecl();
7316 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };7700 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
7317 return sema.analyzeDeclVal(block, src, this_decl);7701 return sema.analyzeDeclVal(block, src, this_decl);
7318}7702}
73197703
7320fn zirClosureCapture(7704fn zirClosureCapture(
7321 sema: *Sema,7705 sema: *Sema,
7322 block: *Scope.Block,7706 block: *Block,
7323 inst: Zir.Inst.Index,7707 inst: Zir.Inst.Index,
7324) CompileError!void {7708) CompileError!void {
7325 // TODO: Compile error when closed over values are modified7709 // TODO: Compile error when closed over values are modified
...@@ -7333,7 +7717,7 @@ fn zirClosureCapture(...@@ -7333,7 +7717,7 @@ fn zirClosureCapture(
73337717
7334fn zirClosureGet(7718fn zirClosureGet(
7335 sema: *Sema,7719 sema: *Sema,
7336 block: *Scope.Block,7720 block: *Block,
7337 inst: Zir.Inst.Index,7721 inst: Zir.Inst.Index,
7338) CompileError!Air.Inst.Ref {7722) CompileError!Air.Inst.Ref {
7339 // TODO CLOSURE: Test this with inline functions7723 // TODO CLOSURE: Test this with inline functions
...@@ -7355,23 +7739,23 @@ fn zirClosureGet(...@@ -7355,23 +7739,23 @@ fn zirClosureGet(
73557739
7356fn zirRetAddr(7740fn zirRetAddr(
7357 sema: *Sema,7741 sema: *Sema,
7358 block: *Scope.Block,7742 block: *Block,
7359 extended: Zir.Inst.Extended.InstData,7743 extended: Zir.Inst.Extended.InstData,
7360) CompileError!Air.Inst.Ref {7744) CompileError!Air.Inst.Ref {
7361 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };7745 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", .{});
7363}7747}
73647748
7365fn zirBuiltinSrc(7749fn zirBuiltinSrc(
7366 sema: *Sema,7750 sema: *Sema,
7367 block: *Scope.Block,7751 block: *Block,
7368 extended: Zir.Inst.Extended.InstData,7752 extended: Zir.Inst.Extended.InstData,
7369) CompileError!Air.Inst.Ref {7753) CompileError!Air.Inst.Ref {
7370 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };7754 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", .{});
7372}7756}
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 {
7375 const inst_data = sema.code.instructions.items(.data)[inst].un_node;7759 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
7376 const src = inst_data.src();7760 const src = inst_data.src();
7377 const ty = try sema.resolveType(block, src, inst_data.operand);7761 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...@@ -7570,13 +7954,13 @@ fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
7570 }),7954 }),
7571 );7955 );
7572 },7956 },
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}", .{
7574 @tagName(t),7958 @tagName(t),
7575 }),7959 }),
7576 }7960 }
7577}7961}
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 {
7580 _ = block;7964 _ = block;
7581 const zir_datas = sema.code.instructions.items(.data);7965 const zir_datas = sema.code.instructions.items(.data);
7582 const inst_data = zir_datas[inst].un_node;7966 const inst_data = zir_datas[inst].un_node;
...@@ -7585,7 +7969,7 @@ fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -7585,7 +7969,7 @@ fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
7585 return sema.addType(operand_ty);7969 return sema.addType(operand_ty);
7586}7970}
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 {
7589 _ = block;7973 _ = block;
7590 const inst_data = sema.code.instructions.items(.data)[inst].un_node;7974 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
7591 const operand_ptr = sema.resolveInst(inst_data.operand);7975 const operand_ptr = sema.resolveInst(inst_data.operand);
...@@ -7593,7 +7977,7 @@ fn zirTypeofElem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile...@@ -7593,7 +7977,7 @@ fn zirTypeofElem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
7593 return sema.addType(elem_ty);7977 return sema.addType(elem_ty);
7594}7978}
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 {
7597 const inst_data = sema.code.instructions.items(.data)[inst].un_node;7981 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
7598 const src = inst_data.src();7982 const src = inst_data.src();
7599 const operand = sema.resolveInst(inst_data.operand);7983 const operand = sema.resolveInst(inst_data.operand);
...@@ -7601,14 +7985,14 @@ fn zirTypeofLog2IntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)...@@ -7601,14 +7985,14 @@ fn zirTypeofLog2IntType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
7601 return sema.log2IntType(block, operand_ty, src);7985 return sema.log2IntType(block, operand_ty, src);
7602}7986}
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 {
7605 const inst_data = sema.code.instructions.items(.data)[inst].un_node;7989 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
7606 const src = inst_data.src();7990 const src = inst_data.src();
7607 const operand = try sema.resolveType(block, src, inst_data.operand);7991 const operand = try sema.resolveType(block, src, inst_data.operand);
7608 return sema.log2IntType(block, operand, src);7992 return sema.log2IntType(block, operand, src);
7609}7993}
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 {
7612 switch (operand.zigTypeTag()) {7996 switch (operand.zigTypeTag()) {
7613 .ComptimeInt => return Air.Inst.Ref.comptime_int_type,7997 .ComptimeInt => return Air.Inst.Ref.comptime_int_type,
7614 .Int => {7998 .Int => {
...@@ -7620,8 +8004,8 @@ fn log2IntType(sema: *Sema, block: *Scope.Block, operand: Type, src: LazySrcLoc)...@@ -7620,8 +8004,8 @@ fn log2IntType(sema: *Sema, block: *Scope.Block, operand: Type, src: LazySrcLoc)
7620 const res = try Module.makeIntType(sema.arena, .unsigned, count);8004 const res = try Module.makeIntType(sema.arena, .unsigned, count);
7621 return sema.addType(res);8005 return sema.addType(res);
7622 },8006 },
7623 else => return sema.mod.fail(8007 else => return sema.fail(
7624 &block.base,8008 block,
7625 src,8009 src,
7626 "bit shifting operation expected integer type, found '{}'",8010 "bit shifting operation expected integer type, found '{}'",
7627 .{operand},8011 .{operand},
...@@ -7631,7 +8015,7 @@ fn log2IntType(sema: *Sema, block: *Scope.Block, operand: Type, src: LazySrcLoc)...@@ -7631,7 +8015,7 @@ fn log2IntType(sema: *Sema, block: *Scope.Block, operand: Type, src: LazySrcLoc)
76318015
7632fn zirTypeofPeer(8016fn zirTypeofPeer(
7633 sema: *Sema,8017 sema: *Sema,
7634 block: *Scope.Block,8018 block: *Block,
7635 extended: Zir.Inst.Extended.InstData,8019 extended: Zir.Inst.Extended.InstData,
7636) CompileError!Air.Inst.Ref {8020) CompileError!Air.Inst.Ref {
7637 const tracy = trace(@src());8021 const tracy = trace(@src());
...@@ -7652,7 +8036,7 @@ fn zirTypeofPeer(...@@ -7652,7 +8036,7 @@ fn zirTypeofPeer(
7652 return sema.addType(result_type);8036 return sema.addType(result_type);
7653}8037}
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 {
7656 const tracy = trace(@src());8040 const tracy = trace(@src());
7657 defer tracy.end();8041 defer tracy.end();
76588042
...@@ -7676,7 +8060,7 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -7676,7 +8060,7 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
76768060
7677fn zirBoolBr(8061fn zirBoolBr(
7678 sema: *Sema,8062 sema: *Sema,
7679 parent_block: *Scope.Block,8063 parent_block: *Block,
7680 inst: Zir.Inst.Index,8064 inst: Zir.Inst.Index,
7681 is_bool_or: bool,8065 is_bool_or: bool,
7682) CompileError!Air.Inst.Ref {8066) CompileError!Air.Inst.Ref {
...@@ -7762,7 +8146,7 @@ fn zirBoolBr(...@@ -7762,7 +8146,7 @@ fn zirBoolBr(
77628146
7763fn zirIsNonNull(8147fn zirIsNonNull(
7764 sema: *Sema,8148 sema: *Sema,
7765 block: *Scope.Block,8149 block: *Block,
7766 inst: Zir.Inst.Index,8150 inst: Zir.Inst.Index,
7767) CompileError!Air.Inst.Ref {8151) CompileError!Air.Inst.Ref {
7768 const tracy = trace(@src());8152 const tracy = trace(@src());
...@@ -7776,7 +8160,7 @@ fn zirIsNonNull(...@@ -7776,7 +8160,7 @@ fn zirIsNonNull(
77768160
7777fn zirIsNonNullPtr(8161fn zirIsNonNullPtr(
7778 sema: *Sema,8162 sema: *Sema,
7779 block: *Scope.Block,8163 block: *Block,
7780 inst: Zir.Inst.Index,8164 inst: Zir.Inst.Index,
7781) CompileError!Air.Inst.Ref {8165) CompileError!Air.Inst.Ref {
7782 const tracy = trace(@src());8166 const tracy = trace(@src());
...@@ -7789,7 +8173,7 @@ fn zirIsNonNullPtr(...@@ -7789,7 +8173,7 @@ fn zirIsNonNullPtr(
7789 return sema.analyzeIsNull(block, src, loaded, true);8173 return sema.analyzeIsNull(block, src, loaded, true);
7790}8174}
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 {
7793 const tracy = trace(@src());8177 const tracy = trace(@src());
7794 defer tracy.end();8178 defer tracy.end();
77958179
...@@ -7798,7 +8182,7 @@ fn zirIsNonErr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -7798,7 +8182,7 @@ fn zirIsNonErr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
7798 return sema.analyzeIsNonErr(block, inst_data.src(), operand);8182 return sema.analyzeIsNonErr(block, inst_data.src(), operand);
7799}8183}
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 {
7802 const tracy = trace(@src());8186 const tracy = trace(@src());
7803 defer tracy.end();8187 defer tracy.end();
78048188
...@@ -7811,7 +8195,7 @@ fn zirIsNonErrPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -7811,7 +8195,7 @@ fn zirIsNonErrPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
78118195
7812fn zirCondbr(8196fn zirCondbr(
7813 sema: *Sema,8197 sema: *Sema,
7814 parent_block: *Scope.Block,8198 parent_block: *Block,
7815 inst: Zir.Inst.Index,8199 inst: Zir.Inst.Index,
7816) CompileError!Zir.Inst.Index {8200) CompileError!Zir.Inst.Index {
7817 const tracy = trace(@src());8201 const tracy = trace(@src());
...@@ -7866,7 +8250,7 @@ fn zirCondbr(...@@ -7866,7 +8250,7 @@ fn zirCondbr(
7866 return always_noreturn;8250 return always_noreturn;
7867}8251}
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 {
7870 const tracy = trace(@src());8254 const tracy = trace(@src());
7871 defer tracy.end();8255 defer tracy.end();
78728256
...@@ -7885,7 +8269,7 @@ fn zirUnreachable(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -7885,7 +8269,7 @@ fn zirUnreachable(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
78858269
7886fn zirRetErrValue(8270fn zirRetErrValue(
7887 sema: *Sema,8271 sema: *Sema,
7888 block: *Scope.Block,8272 block: *Block,
7889 inst: Zir.Inst.Index,8273 inst: Zir.Inst.Index,
7890) CompileError!Zir.Inst.Index {8274) CompileError!Zir.Inst.Index {
7891 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;8275 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
...@@ -7909,7 +8293,7 @@ fn zirRetErrValue(...@@ -7909,7 +8293,7 @@ fn zirRetErrValue(
79098293
7910fn zirRetCoerce(8294fn zirRetCoerce(
7911 sema: *Sema,8295 sema: *Sema,
7912 block: *Scope.Block,8296 block: *Block,
7913 inst: Zir.Inst.Index,8297 inst: Zir.Inst.Index,
7914) CompileError!Zir.Inst.Index {8298) CompileError!Zir.Inst.Index {
7915 const tracy = trace(@src());8299 const tracy = trace(@src());
...@@ -7922,7 +8306,7 @@ fn zirRetCoerce(...@@ -7922,7 +8306,7 @@ fn zirRetCoerce(
7922 return sema.analyzeRet(block, operand, src, true);8306 return sema.analyzeRet(block, operand, src, true);
7923}8307}
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 {
7926 const tracy = trace(@src());8310 const tracy = trace(@src());
7927 defer tracy.end();8311 defer tracy.end();
79288312
...@@ -7937,7 +8321,7 @@ fn zirRetNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -7937,7 +8321,7 @@ fn zirRetNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
7937 return sema.analyzeRet(block, operand, src, false);8321 return sema.analyzeRet(block, operand, src, false);
7938}8322}
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 {
7941 const tracy = trace(@src());8325 const tracy = trace(@src());
7942 defer tracy.end();8326 defer tracy.end();
79438327
...@@ -7954,7 +8338,7 @@ fn zirRetLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -7954,7 +8338,7 @@ fn zirRetLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
79548338
7955fn analyzeRet(8339fn analyzeRet(
7956 sema: *Sema,8340 sema: *Sema,
7957 block: *Scope.Block,8341 block: *Block,
7958 uncasted_operand: Air.Inst.Ref,8342 uncasted_operand: Air.Inst.Ref,
7959 src: LazySrcLoc,8343 src: LazySrcLoc,
7960 need_coercion: bool,8344 need_coercion: bool,
...@@ -7987,7 +8371,7 @@ fn floatOpAllowed(tag: Zir.Inst.Tag) bool {...@@ -7987,7 +8371,7 @@ fn floatOpAllowed(tag: Zir.Inst.Tag) bool {
7987 };8371 };
7988}8372}
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 {
7991 const tracy = trace(@src());8375 const tracy = trace(@src());
7992 defer tracy.end();8376 defer tracy.end();
79938377
...@@ -8004,7 +8388,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp...@@ -8004,7 +8388,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Comp
8004 return sema.addType(ty);8388 return sema.addType(ty);
8005}8389}
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 {
8008 const tracy = trace(@src());8392 const tracy = trace(@src());
8009 defer tracy.end();8393 defer tracy.end();
80108394
...@@ -8045,7 +8429,7 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -8045,7 +8429,7 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
8045 } else 0;8429 } else 0;
80468430
8047 if (bit_end != 0 and bit_start >= bit_end * 8)8431 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
8050 const elem_type = try sema.resolveType(block, .unneeded, extra.data.elem_type);8434 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...@@ -8064,7 +8448,7 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
8064 return sema.addType(ty);8448 return sema.addType(ty);
8065}8449}
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 {
8068 const tracy = trace(@src());8452 const tracy = trace(@src());
8069 defer tracy.end();8453 defer tracy.end();
80708454
...@@ -8075,14 +8459,13 @@ fn zirStructInitEmpty(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co...@@ -8075,14 +8459,13 @@ fn zirStructInitEmpty(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
8075 return sema.addConstant(struct_type, Value.initTag(.empty_struct_value));8459 return sema.addConstant(struct_type, Value.initTag(.empty_struct_value));
8076}8460}
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 {
8079 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8463 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8080 const src = inst_data.src();8464 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", .{});
8082}8466}
80838467
8084fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {8468fn zirStructInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index, is_ref: bool) CompileError!Air.Inst.Ref {
8085 const mod = sema.mod;
8086 const gpa = sema.gpa;8469 const gpa = sema.gpa;
8087 const zir_datas = sema.code.instructions.items(.data);8470 const zir_datas = sema.code.instructions.items(.data);
8088 const inst_data = zir_datas[inst].pl_node;8471 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:...@@ -8126,12 +8509,12 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
8126 const other_field_type_data = zir_datas[other_field_type].pl_node;8509 const other_field_type_data = zir_datas[other_field_type].pl_node;
8127 const other_field_src: LazySrcLoc = .{ .node_offset_back2tok = other_field_type_data.src_node };8510 const other_field_src: LazySrcLoc = .{ .node_offset_back2tok = other_field_type_data.src_node };
8128 const msg = msg: {8511 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", .{});
8130 errdefer msg.destroy(gpa);8513 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", .{});
8132 break :msg msg;8515 break :msg msg;
8133 };8516 };
8134 return mod.failWithOwnedErrorMsg(&block.base, msg);8517 return sema.failWithOwnedErrorMsg(msg);
8135 }8518 }
8136 found_fields[field_index] = item.data.field_type;8519 found_fields[field_index] = item.data.field_type;
8137 field_inits[field_index] = sema.resolveInst(item.data.init);8520 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:...@@ -8149,9 +8532,9 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
8149 const template = "missing struct field: {s}";8532 const template = "missing struct field: {s}";
8150 const args = .{field_name};8533 const args = .{field_name};
8151 if (root_msg) |msg| {8534 if (root_msg) |msg| {
8152 try mod.errNote(&block.base, src, msg, template, args);8535 try sema.errNote(block, src, msg, template, args);
8153 } else {8536 } else {
8154 root_msg = try mod.errMsg(&block.base, src, template, args);8537 root_msg = try sema.errMsg(block, src, template, args);
8155 }8538 }
8156 } else {8539 } else {
8157 field_inits[i] = try sema.addConstant(field.ty, field.default_val);8540 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:...@@ -8160,17 +8543,17 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
8160 if (root_msg) |msg| {8543 if (root_msg) |msg| {
8161 const fqn = try struct_obj.getFullyQualifiedName(gpa);8544 const fqn = try struct_obj.getFullyQualifiedName(gpa);
8162 defer gpa.free(fqn);8545 defer gpa.free(fqn);
8163 try mod.errNoteNonLazy(8546 try sema.mod.errNoteNonLazy(
8164 struct_obj.srcLoc(),8547 struct_obj.srcLoc(),
8165 msg,8548 msg,
8166 "struct '{s}' declared here",8549 "struct '{s}' declared here",
8167 .{fqn},8550 .{fqn},
8168 );8551 );
8169 return mod.failWithOwnedErrorMsg(&block.base, msg);8552 return sema.failWithOwnedErrorMsg(msg);
8170 }8553 }
81718554
8172 if (is_ref) {8555 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", .{});
8174 }8557 }
81758558
8176 const is_comptime = for (field_inits) |field_init| {8559 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:...@@ -8187,12 +8570,12 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
8187 return sema.addConstant(resolved_ty, try Value.Tag.@"struct".create(sema.arena, values));8570 return sema.addConstant(resolved_ty, try Value.Tag.@"struct".create(sema.arena, values));
8188 }8571 }
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", .{});
8191 } else if (resolved_ty.cast(Type.Payload.Union)) |union_payload| {8574 } else if (resolved_ty.cast(Type.Payload.Union)) |union_payload| {
8192 const union_obj = union_payload.data;8575 const union_obj = union_payload.data;
81938576
8194 if (extra.data.fields_len != 1) {8577 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", .{});
8196 }8579 }
81978580
8198 const item = sema.code.extraData(Zir.Inst.StructInit.Item, extra.end);8581 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:...@@ -8205,7 +8588,7 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
8205 return sema.failWithBadUnionFieldAccess(block, union_obj, field_src, field_name);8588 return sema.failWithBadUnionFieldAccess(block, union_obj, field_src, field_name);
82068589
8207 if (is_ref) {8590 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", .{});
8209 }8592 }
82108593
8211 const init_inst = sema.resolveInst(item.data.init);8594 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:...@@ -8218,20 +8601,20 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
8218 }),8601 }),
8219 );8602 );
8220 }8603 }
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", .{});
8222 }8605 }
8223 unreachable;8606 unreachable;
8224}8607}
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 {
8227 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8610 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8228 const src = inst_data.src();8611 const src = inst_data.src();
82298612
8230 _ = is_ref;8613 _ = is_ref;
8231 return sema.mod.fail(&block.base, src, "TODO: Sema.zirStructInitAnon", .{});8614 return sema.fail(block, src, "TODO: Sema.zirStructInitAnon", .{});
8232}8615}
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 {
8235 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8618 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8236 const src = inst_data.src();8619 const src = inst_data.src();
82378620
...@@ -8249,10 +8632,13 @@ fn zirArrayInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:...@@ -8249,10 +8632,13 @@ fn zirArrayInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
8249 var anon_decl = try block.startAnonDecl();8632 var anon_decl = try block.startAnonDecl();
8250 defer anon_decl.deinit();8633 defer anon_decl.deinit();
8251 assert(!(resolved_args.len == 0));8634 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 });
8253 const buf = try anon_decl.arena().alloc(Value, resolved_args.len);8639 const buf = try anon_decl.arena().alloc(Value, resolved_args.len);
8254 for (resolved_args) |arg, i| {8640 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());
8256 }8642 }
82578643
8258 const val = try Value.Tag.array.create(anon_decl.arena(), buf);8644 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:...@@ -8280,21 +8666,21 @@ fn zirArrayInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref:
8280 try sema.analyzeLoad(block, .unneeded, alloc, .unneeded);8666 try sema.analyzeLoad(block, .unneeded, alloc, .unneeded);
8281}8667}
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 {
8284 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8670 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8285 const src = inst_data.src();8671 const src = inst_data.src();
82868672
8287 _ = is_ref;8673 _ = is_ref;
8288 return sema.mod.fail(&block.base, src, "TODO: Sema.zirArrayInitAnon", .{});8674 return sema.fail(block, src, "TODO: Sema.zirArrayInitAnon", .{});
8289}8675}
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 {
8292 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8678 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8293 const src = inst_data.src();8679 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", .{});
8295}8681}
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 {
8298 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8684 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8299 const extra = sema.code.extraData(Zir.Inst.FieldType, inst_data.payload_index).data;8685 const extra = sema.code.extraData(Zir.Inst.FieldType, inst_data.payload_index).data;
8300 const src = inst_data.src();8686 const src = inst_data.src();
...@@ -8314,7 +8700,7 @@ fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -8314,7 +8700,7 @@ fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
8314 return sema.failWithBadUnionFieldAccess(block, union_obj, src, field_name);8700 return sema.failWithBadUnionFieldAccess(block, union_obj, src, field_name);
8315 return sema.addType(field.ty);8701 return sema.addType(field.ty);
8316 },8702 },
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 '{}'", .{
8318 resolved_ty,8704 resolved_ty,
8319 }),8705 }),
8320 }8706 }
...@@ -8322,32 +8708,32 @@ fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -8322,32 +8708,32 @@ fn zirFieldType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
83228708
8323fn zirErrorReturnTrace(8709fn zirErrorReturnTrace(
8324 sema: *Sema,8710 sema: *Sema,
8325 block: *Scope.Block,8711 block: *Block,
8326 extended: Zir.Inst.Extended.InstData,8712 extended: Zir.Inst.Extended.InstData,
8327) CompileError!Air.Inst.Ref {8713) CompileError!Air.Inst.Ref {
8328 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };8714 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", .{});
8330}8716}
83318717
8332fn zirFrame(8718fn zirFrame(
8333 sema: *Sema,8719 sema: *Sema,
8334 block: *Scope.Block,8720 block: *Block,
8335 extended: Zir.Inst.Extended.InstData,8721 extended: Zir.Inst.Extended.InstData,
8336) CompileError!Air.Inst.Ref {8722) CompileError!Air.Inst.Ref {
8337 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };8723 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", .{});
8339}8725}
83408726
8341fn zirFrameAddress(8727fn zirFrameAddress(
8342 sema: *Sema,8728 sema: *Sema,
8343 block: *Scope.Block,8729 block: *Block,
8344 extended: Zir.Inst.Extended.InstData,8730 extended: Zir.Inst.Extended.InstData,
8345) CompileError!Air.Inst.Ref {8731) CompileError!Air.Inst.Ref {
8346 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };8732 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", .{});
8348}8734}
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 {
8351 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8737 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8352 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };8738 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
8353 const ty = try sema.resolveType(block, operand_src, inst_data.operand);8739 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...@@ -8356,7 +8742,7 @@ fn zirAlignOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
8356 return sema.addIntUnsigned(Type.comptime_int, abi_align);8742 return sema.addIntUnsigned(Type.comptime_int, abi_align);
8357}8743}
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 {
8360 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8746 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8361 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };8747 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
8362 const operand = sema.resolveInst(inst_data.operand);8748 const operand = sema.resolveInst(inst_data.operand);
...@@ -8368,31 +8754,31 @@ fn zirBoolToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -8368,31 +8754,31 @@ fn zirBoolToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
8368 return block.addUnOp(.bool_to_int, operand);8754 return block.addUnOp(.bool_to_int, operand);
8369}8755}
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 {
8372 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8758 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8373 const src = inst_data.src();8759 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", .{});
8375}8761}
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 {
8378 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8764 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8379 const src = inst_data.src();8765 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", .{});
8381}8767}
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 {
8384 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8770 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8385 const src = inst_data.src();8771 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", .{});
8387}8773}
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 {
8390 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8776 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8391 const src = inst_data.src();8777 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", .{});
8393}8779}
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 {
8396 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8782 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8397 const src = inst_data.src();8783 const src = inst_data.src();
8398 const type_info_ty = try sema.getBuiltinType(block, src, "TypeInfo");8784 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...@@ -8422,55 +8808,55 @@ fn zirReify(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
8422 };8808 };
8423 return sema.addType(ty);8809 return sema.addType(ty);
8424 },8810 },
8425 .Float => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Float", .{}),8811 .Float => return sema.fail(block, src, "TODO: Sema.zirReify for Float", .{}),
8426 .Pointer => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Pointer", .{}),8812 .Pointer => return sema.fail(block, src, "TODO: Sema.zirReify for Pointer", .{}),
8427 .Array => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Array", .{}),8813 .Array => return sema.fail(block, src, "TODO: Sema.zirReify for Array", .{}),
8428 .Struct => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Struct", .{}),8814 .Struct => return sema.fail(block, src, "TODO: Sema.zirReify for Struct", .{}),
8429 .ComptimeFloat => return Air.Inst.Ref.comptime_float_type,8815 .ComptimeFloat => return Air.Inst.Ref.comptime_float_type,
8430 .ComptimeInt => return Air.Inst.Ref.comptime_int_type,8816 .ComptimeInt => return Air.Inst.Ref.comptime_int_type,
8431 .Undefined => return Air.Inst.Ref.undefined_type,8817 .Undefined => return Air.Inst.Ref.undefined_type,
8432 .Null => return Air.Inst.Ref.null_type,8818 .Null => return Air.Inst.Ref.null_type,
8433 .Optional => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Optional", .{}),8819 .Optional => return sema.fail(block, src, "TODO: Sema.zirReify for Optional", .{}),
8434 .ErrorUnion => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for ErrorUnion", .{}),8820 .ErrorUnion => return sema.fail(block, src, "TODO: Sema.zirReify for ErrorUnion", .{}),
8435 .ErrorSet => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for ErrorSet", .{}),8821 .ErrorSet => return sema.fail(block, src, "TODO: Sema.zirReify for ErrorSet", .{}),
8436 .Enum => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Enum", .{}),8822 .Enum => return sema.fail(block, src, "TODO: Sema.zirReify for Enum", .{}),
8437 .Union => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Union", .{}),8823 .Union => return sema.fail(block, src, "TODO: Sema.zirReify for Union", .{}),
8438 .Fn => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Fn", .{}),8824 .Fn => return sema.fail(block, src, "TODO: Sema.zirReify for Fn", .{}),
8439 .BoundFn => @panic("TODO delete BoundFn from the language"),8825 .BoundFn => @panic("TODO delete BoundFn from the language"),
8440 .Opaque => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Opaque", .{}),8826 .Opaque => return sema.fail(block, src, "TODO: Sema.zirReify for Opaque", .{}),
8441 .Frame => return sema.mod.fail(&block.base, src, "TODO: Sema.zirReify for Frame", .{}),8827 .Frame => return sema.fail(block, src, "TODO: Sema.zirReify for Frame", .{}),
8442 .AnyFrame => return Air.Inst.Ref.anyframe_type,8828 .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", .{}),
8444 .EnumLiteral => return Air.Inst.Ref.enum_literal_type,8830 .EnumLiteral => return Air.Inst.Ref.enum_literal_type,
8445 }8831 }
8446}8832}
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 {
8449 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8835 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8450 const src = inst_data.src();8836 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", .{});
8452}8838}
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 {
8455 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8841 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8456 const src = inst_data.src();8842 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", .{});
8458}8844}
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 {
8461 const inst_data = sema.code.instructions.items(.data)[inst].un_node;8847 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8462 const src = inst_data.src();8848 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", .{});
8464}8850}
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 {
8467 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8853 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8468 const src = inst_data.src();8854 const src = inst_data.src();
8469 // TODO don't forget the safety check!8855 // 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", .{});
8471}8857}
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 {
8474 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8860 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8475 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;8861 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
8476 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };8862 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...@@ -8492,7 +8878,7 @@ fn zirIntToFloat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
8492 return block.addTyOp(.int_to_float, dest_ty, operand);8878 return block.addTyOp(.int_to_float, dest_ty, operand);
8493}8879}
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 {
8496 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8882 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8497 const src = inst_data.src();8883 const src = inst_data.src();
84988884
...@@ -8505,15 +8891,15 @@ fn zirIntToPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -8505,15 +8891,15 @@ fn zirIntToPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
8505 const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };8891 const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
8506 const type_res = try sema.resolveType(block, src, extra.lhs);8892 const type_res = try sema.resolveType(block, src, extra.lhs);
8507 if (type_res.zigTypeTag() != .Pointer)8893 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});
8509 const ptr_align = type_res.ptrAlignment(sema.mod.getTarget());8895 const ptr_align = type_res.ptrAlignment(sema.mod.getTarget());
85108896
8511 if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| {8897 if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| {
8512 const addr = val.toUnsignedInt();8898 const addr = val.toUnsignedInt();
8513 if (!type_res.isAllowzeroPtr() and addr == 0)8899 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});
8515 if (addr != 0 and addr % ptr_align != 0)8901 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
8518 const val_payload = try sema.arena.create(Value.Payload.U64);8904 const val_payload = try sema.arena.create(Value.Payload.U64);
8519 val_payload.* = .{8905 val_payload.* = .{
...@@ -8548,13 +8934,13 @@ fn zirIntToPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -8548,13 +8934,13 @@ fn zirIntToPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
8548 return block.addTyOp(.bitcast, type_res, operand_coerced);8934 return block.addTyOp(.bitcast, type_res, operand_coerced);
8549}8935}
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 {
8552 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8938 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8553 const src = inst_data.src();8939 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", .{});
8555}8941}
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 {
8558 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8944 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8559 const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };8945 const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
8560 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };8946 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...@@ -8563,12 +8949,12 @@ fn zirPtrCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
8563 const operand = sema.resolveInst(extra.rhs);8949 const operand = sema.resolveInst(extra.rhs);
8564 const operand_ty = sema.typeOf(operand);8950 const operand_ty = sema.typeOf(operand);
8565 if (operand_ty.zigTypeTag() != .Pointer) {8951 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 '{}'", .{
8567 @tagName(operand_ty.zigTypeTag()), operand_ty,8953 @tagName(operand_ty.zigTypeTag()), operand_ty,
8568 });8954 });
8569 }8955 }
8570 if (dest_ty.zigTypeTag() != .Pointer) {8956 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 '{}'", .{
8572 @tagName(dest_ty.zigTypeTag()), dest_ty,8958 @tagName(dest_ty.zigTypeTag()), dest_ty,
8573 });8959 });
8574 }8960 }
...@@ -8578,7 +8964,7 @@ fn zirPtrCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr...@@ -8578,7 +8964,7 @@ fn zirPtrCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
8578 return block.addTyOp(.bitcast, dest_ty, operand);8964 return block.addTyOp(.bitcast, dest_ty, operand);
8579}8965}
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 {
8582 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;8968 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8583 const src = inst_data.src();8969 const src = inst_data.src();
8584 const dest_ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };8970 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...@@ -8587,7 +8973,6 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
8587 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);8973 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);
8588 const operand = sema.resolveInst(extra.rhs);8974 const operand = sema.resolveInst(extra.rhs);
8589 const operand_ty = sema.typeOf(operand);8975 const operand_ty = sema.typeOf(operand);
8590 const mod = sema.mod;
8591 const dest_is_comptime_int = try sema.checkIntType(block, dest_ty_src, dest_ty);8976 const dest_is_comptime_int = try sema.checkIntType(block, dest_ty_src, dest_ty);
8592 const src_is_comptime_int = try sema.checkIntType(block, operand_src, operand_ty);8977 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...@@ -8595,7 +8980,7 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
8595 return sema.coerce(block, dest_ty, operand, operand_src);8980 return sema.coerce(block, dest_ty, operand, operand_src);
8596 }8981 }
85978982
8598 const target = mod.getTarget();8983 const target = sema.mod.getTarget();
8599 const src_info = operand_ty.intInfo(target);8984 const src_info = operand_ty.intInfo(target);
8600 const dest_info = dest_ty.intInfo(target);8985 const dest_info = dest_ty.intInfo(target);
86018986
...@@ -8605,28 +8990,28 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -8605,28 +8990,28 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
86058990
8606 if (!src_is_comptime_int) {8991 if (!src_is_comptime_int) {
8607 if (src_info.signedness != dest_info.signedness) {8992 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 '{}'", .{
8609 @tagName(dest_info.signedness), operand_ty,8994 @tagName(dest_info.signedness), operand_ty,
8610 });8995 });
8611 }8996 }
8612 if (src_info.bits > 0 and src_info.bits < dest_info.bits) {8997 if (src_info.bits > 0 and src_info.bits < dest_info.bits) {
8613 const msg = msg: {8998 const msg = msg: {
8614 const msg = try mod.errMsg(8999 const msg = try sema.errMsg(
8615 &block.base,9000 block,
8616 src,9001 src,
8617 "destination type '{}' has more bits than source type '{}'",9002 "destination type '{}' has more bits than source type '{}'",
8618 .{ dest_ty, operand_ty },9003 .{ dest_ty, operand_ty },
8619 );9004 );
8620 errdefer msg.destroy(mod.gpa);9005 errdefer msg.destroy(sema.gpa);
8621 try mod.errNote(&block.base, dest_ty_src, msg, "destination type has {d} bits", .{9006 try sema.errNote(block, dest_ty_src, msg, "destination type has {d} bits", .{
8622 dest_info.bits,9007 dest_info.bits,
8623 });9008 });
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", .{
8625 src_info.bits,9010 src_info.bits,
8626 });9011 });
8627 break :msg msg;9012 break :msg msg;
8628 };9013 };
8629 return mod.failWithOwnedErrorMsg(&block.base, msg);9014 return sema.failWithOwnedErrorMsg(msg);
8630 }9015 }
8631 }9016 }
86329017
...@@ -8639,13 +9024,13 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -8639,13 +9024,13 @@ fn zirTruncate(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
8639 return block.addTyOp(.trunc, dest_ty, operand);9024 return block.addTyOp(.trunc, dest_ty, operand);
8640}9025}
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 {
8643 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9028 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8644 const src = inst_data.src();9029 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", .{});
8646}9031}
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 {
8649 const inst_data = sema.code.instructions.items(.data)[inst].un_node;9034 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8650 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };9035 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
8651 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };9036 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...@@ -8653,7 +9038,7 @@ fn zirClz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
8653 const operand_ty = sema.typeOf(operand);9038 const operand_ty = sema.typeOf(operand);
8654 // TODO implement support for vectors9039 // TODO implement support for vectors
8655 if (operand_ty.zigTypeTag() != .Int) {9040 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 '{}'", .{
8657 operand_ty,9042 operand_ty,
8658 });9043 });
8659 }9044 }
...@@ -8672,7 +9057,7 @@ fn zirClz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A...@@ -8672,7 +9057,7 @@ fn zirClz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
8672 return block.addTyOp(.clz, result_ty, operand);9057 return block.addTyOp(.clz, result_ty, operand);
8673}9058}
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 {
8676 const inst_data = sema.code.instructions.items(.data)[inst].un_node;9061 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8677 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };9062 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
8678 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };9063 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...@@ -8680,7 +9065,7 @@ fn zirCtz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
8680 const operand_ty = sema.typeOf(operand);9065 const operand_ty = sema.typeOf(operand);
8681 // TODO implement support for vectors9066 // TODO implement support for vectors
8682 if (operand_ty.zigTypeTag() != .Int) {9067 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 '{}'", .{
8684 operand_ty,9069 operand_ty,
8685 });9070 });
8686 }9071 }
...@@ -8692,85 +9077,85 @@ fn zirCtz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A...@@ -8692,85 +9077,85 @@ fn zirCtz(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A
86929077
8693 const runtime_src = if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |val| {9078 const runtime_src = if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |val| {
8694 if (val.isUndef()) return sema.addConstUndef(result_ty);9079 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", .{});
8696 } else operand_src;9081 } else operand_src;
86979082
8698 try sema.requireRuntimeBlock(block, runtime_src);9083 try sema.requireRuntimeBlock(block, runtime_src);
8699 return block.addTyOp(.ctz, result_ty, operand);9084 return block.addTyOp(.ctz, result_ty, operand);
8700}9085}
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 {
8703 const inst_data = sema.code.instructions.items(.data)[inst].un_node;9088 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8704 const src = inst_data.src();9089 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", .{});
8706}9091}
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 {
8709 const inst_data = sema.code.instructions.items(.data)[inst].un_node;9094 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8710 const src = inst_data.src();9095 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", .{});
8712}9097}
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 {
8715 const inst_data = sema.code.instructions.items(.data)[inst].un_node;9100 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
8716 const src = inst_data.src();9101 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", .{});
8718}9103}
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 {
8721 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9106 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8722 const src = inst_data.src();9107 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", .{});
8724}9109}
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 {
8727 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9112 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8728 const src = inst_data.src();9113 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", .{});
8730}9115}
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 {
8733 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9118 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8734 const src = inst_data.src();9119 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", .{});
8736}9121}
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 {
8739 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9124 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8740 const src = inst_data.src();9125 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", .{});
8742}9127}
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 {
8745 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9130 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8746 const src = inst_data.src();9131 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", .{});
8748}9133}
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 {
8751 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9136 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8752 const src = inst_data.src();9137 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", .{});
8754}9139}
87559140
8756/// Returns `true` if the type was a comptime_int.9141/// 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 {
8758 switch (ty.zigTypeTag()) {9143 switch (ty.zigTypeTag()) {
8759 .ComptimeInt => return true,9144 .ComptimeInt => return true,
8760 .Int => return false,9145 .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}),
8762 }9147 }
8763}9148}
87649149
8765fn checkFloatType(9150fn checkFloatType(
8766 sema: *Sema,9151 sema: *Sema,
8767 block: *Scope.Block,9152 block: *Block,
8768 ty_src: LazySrcLoc,9153 ty_src: LazySrcLoc,
8769 ty: Type,9154 ty: Type,
8770) CompileError!void {9155) CompileError!void {
8771 switch (ty.zigTypeTag()) {9156 switch (ty.zigTypeTag()) {
8772 .ComptimeFloat, .Float => {},9157 .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 '{}'", .{
8774 ty,9159 ty,
8775 }),9160 }),
8776 }9161 }
...@@ -8778,7 +9163,7 @@ fn checkFloatType(...@@ -8778,7 +9163,7 @@ fn checkFloatType(
87789163
8779fn checkAtomicOperandType(9164fn checkAtomicOperandType(
8780 sema: *Sema,9165 sema: *Sema,
8781 block: *Scope.Block,9166 block: *Block,
8782 ty_src: LazySrcLoc,9167 ty_src: LazySrcLoc,
8783 ty: Type,9168 ty: Type,
8784) CompileError!void {9169) CompileError!void {
...@@ -8791,8 +9176,8 @@ fn checkAtomicOperandType(...@@ -8791,8 +9176,8 @@ fn checkAtomicOperandType(
8791 .Float => {9176 .Float => {
8792 const bit_count = ty.floatBits(target);9177 const bit_count = ty.floatBits(target);
8793 if (bit_count > max_atomic_bits) {9178 if (bit_count > max_atomic_bits) {
8794 return sema.mod.fail(9179 return sema.fail(
8795 &block.base,9180 block,
8796 ty_src,9181 ty_src,
8797 "expected {d}-bit float type or smaller; found {d}-bit float type",9182 "expected {d}-bit float type or smaller; found {d}-bit float type",
8798 .{ max_atomic_bits, bit_count },9183 .{ max_atomic_bits, bit_count },
...@@ -8804,8 +9189,8 @@ fn checkAtomicOperandType(...@@ -8804,8 +9189,8 @@ fn checkAtomicOperandType(
8804 else => {9189 else => {
8805 if (ty.isPtrAtRuntime()) return;9190 if (ty.isPtrAtRuntime()) return;
88069191
8807 return sema.mod.fail(9192 return sema.fail(
8808 &block.base,9193 block,
8809 ty_src,9194 ty_src,
8810 "expected bool, integer, float, enum, or pointer type; found {}",9195 "expected bool, integer, float, enum, or pointer type; found {}",
8811 .{ty},9196 .{ty},
...@@ -8814,8 +9199,8 @@ fn checkAtomicOperandType(...@@ -8814,8 +9199,8 @@ fn checkAtomicOperandType(
8814 };9199 };
8815 const bit_count = int_ty.intInfo(target).bits;9200 const bit_count = int_ty.intInfo(target).bits;
8816 if (bit_count > max_atomic_bits) {9201 if (bit_count > max_atomic_bits) {
8817 return sema.mod.fail(9202 return sema.fail(
8818 &block.base,9203 block,
8819 ty_src,9204 ty_src,
8820 "expected {d}-bit integer type or smaller; found {d}-bit integer type",9205 "expected {d}-bit integer type or smaller; found {d}-bit integer type",
8821 .{ max_atomic_bits, bit_count },9206 .{ max_atomic_bits, bit_count },
...@@ -8825,7 +9210,7 @@ fn checkAtomicOperandType(...@@ -8825,7 +9210,7 @@ fn checkAtomicOperandType(
88259210
8826fn resolveExportOptions(9211fn resolveExportOptions(
8827 sema: *Sema,9212 sema: *Sema,
8828 block: *Scope.Block,9213 block: *Block,
8829 src: LazySrcLoc,9214 src: LazySrcLoc,
8830 zir_ref: Zir.Inst.Ref,9215 zir_ref: Zir.Inst.Ref,
8831) CompileError!std.builtin.ExportOptions {9216) CompileError!std.builtin.ExportOptions {
...@@ -8839,7 +9224,7 @@ fn resolveExportOptions(...@@ -8839,7 +9224,7 @@ fn resolveExportOptions(
8839 const linkage_index = struct_obj.fields.getIndex("linkage").?;9224 const linkage_index = struct_obj.fields.getIndex("linkage").?;
8840 const section_index = struct_obj.fields.getIndex("section").?;9225 const section_index = struct_obj.fields.getIndex("section").?;
8841 if (!fields[section_index].isNull()) {9226 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", .{});
8843 }9228 }
8844 return std.builtin.ExportOptions{9229 return std.builtin.ExportOptions{
8845 .name = try fields[name_index].toAllocatedBytes(sema.arena),9230 .name = try fields[name_index].toAllocatedBytes(sema.arena),
...@@ -8850,7 +9235,7 @@ fn resolveExportOptions(...@@ -8850,7 +9235,7 @@ fn resolveExportOptions(
88509235
8851fn resolveAtomicOrder(9236fn resolveAtomicOrder(
8852 sema: *Sema,9237 sema: *Sema,
8853 block: *Scope.Block,9238 block: *Block,
8854 src: LazySrcLoc,9239 src: LazySrcLoc,
8855 zir_ref: Zir.Inst.Ref,9240 zir_ref: Zir.Inst.Ref,
8856) CompileError!std.builtin.AtomicOrder {9241) CompileError!std.builtin.AtomicOrder {
...@@ -8863,7 +9248,7 @@ fn resolveAtomicOrder(...@@ -8863,7 +9248,7 @@ fn resolveAtomicOrder(
88639248
8864fn resolveAtomicRmwOp(9249fn resolveAtomicRmwOp(
8865 sema: *Sema,9250 sema: *Sema,
8866 block: *Scope.Block,9251 block: *Block,
8867 src: LazySrcLoc,9252 src: LazySrcLoc,
8868 zir_ref: Zir.Inst.Ref,9253 zir_ref: Zir.Inst.Ref,
8869) CompileError!std.builtin.AtomicRmwOp {9254) CompileError!std.builtin.AtomicRmwOp {
...@@ -8876,11 +9261,10 @@ fn resolveAtomicRmwOp(...@@ -8876,11 +9261,10 @@ fn resolveAtomicRmwOp(
88769261
8877fn zirCmpxchg(9262fn zirCmpxchg(
8878 sema: *Sema,9263 sema: *Sema,
8879 block: *Scope.Block,9264 block: *Block,
8880 inst: Zir.Inst.Index,9265 inst: Zir.Inst.Index,
8881 air_tag: Air.Inst.Tag,9266 air_tag: Air.Inst.Tag,
8882) CompileError!Air.Inst.Ref {9267) CompileError!Air.Inst.Ref {
8883 const mod = sema.mod;
8884 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9268 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8885 const extra = sema.code.extraData(Zir.Inst.Cmpxchg, inst_data.payload_index).data;9269 const extra = sema.code.extraData(Zir.Inst.Cmpxchg, inst_data.payload_index).data;
8886 const src = inst_data.src();9270 const src = inst_data.src();
...@@ -8896,8 +9280,8 @@ fn zirCmpxchg(...@@ -8896,8 +9280,8 @@ fn zirCmpxchg(
8896 const elem_ty = sema.typeOf(ptr).elemType();9280 const elem_ty = sema.typeOf(ptr).elemType();
8897 try sema.checkAtomicOperandType(block, elem_ty_src, elem_ty);9281 try sema.checkAtomicOperandType(block, elem_ty_src, elem_ty);
8898 if (elem_ty.zigTypeTag() == .Float) {9282 if (elem_ty.zigTypeTag() == .Float) {
8899 return mod.fail(9283 return sema.fail(
8900 &block.base,9284 block,
8901 elem_ty_src,9285 elem_ty_src,
8902 "expected bool, integer, enum, or pointer type; found '{}'",9286 "expected bool, integer, enum, or pointer type; found '{}'",
8903 .{elem_ty},9287 .{elem_ty},
...@@ -8909,16 +9293,16 @@ fn zirCmpxchg(...@@ -8909,16 +9293,16 @@ fn zirCmpxchg(
8909 const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order);9293 const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order);
89109294
8911 if (@enumToInt(success_order) < @enumToInt(std.builtin.AtomicOrder.Monotonic)) {9295 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", .{});
8913 }9297 }
8914 if (@enumToInt(failure_order) < @enumToInt(std.builtin.AtomicOrder.Monotonic)) {9298 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", .{});
8916 }9300 }
8917 if (@enumToInt(failure_order) > @enumToInt(success_order)) {9301 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", .{});
8919 }9303 }
8920 if (failure_order == .Release or failure_order == .AcqRel) {9304 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", .{});
8922 }9306 }
89239307
8924 const result_ty = try Module.optionalType(sema.arena, elem_ty);9308 const result_ty = try Module.optionalType(sema.arena, elem_ty);
...@@ -8965,31 +9349,31 @@ fn zirCmpxchg(...@@ -8965,31 +9349,31 @@ fn zirCmpxchg(
8965 });9349 });
8966}9350}
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 {
8969 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9353 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8970 const src = inst_data.src();9354 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", .{});
8972}9356}
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 {
8975 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9359 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8976 const src = inst_data.src();9360 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", .{});
8978}9362}
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 {
8981 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9365 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8982 const src = inst_data.src();9366 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", .{});
8984}9368}
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 {
8987 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9371 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8988 const src = inst_data.src();9372 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", .{});
8990}9374}
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 {
8993 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9377 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
8994 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;9378 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
8995 // zig fmt: off9379 // zig fmt: off
...@@ -9004,8 +9388,8 @@ fn zirAtomicLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile...@@ -9004,8 +9388,8 @@ fn zirAtomicLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
90049388
9005 switch (order) {9389 switch (order) {
9006 .Release, .AcqRel => {9390 .Release, .AcqRel => {
9007 return sema.mod.fail(9391 return sema.fail(
9008 &block.base,9392 block,
9009 order_src,9393 order_src,
9010 "@atomicLoad atomic ordering must not be Release or AcqRel",9394 "@atomicLoad atomic ordering must not be Release or AcqRel",
9011 .{},9395 .{},
...@@ -9034,8 +9418,7 @@ fn zirAtomicLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile...@@ -9034,8 +9418,7 @@ fn zirAtomicLoad(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
9034 });9418 });
9035}9419}
90369420
9037fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {9421fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9038 const mod = sema.mod;
9039 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9422 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9040 const extra = sema.code.extraData(Zir.Inst.AtomicRmw, inst_data.payload_index).data;9423 const extra = sema.code.extraData(Zir.Inst.AtomicRmw, inst_data.payload_index).data;
9041 const src = inst_data.src();9424 const src = inst_data.src();
...@@ -9053,14 +9436,14 @@ fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -9053,14 +9436,14 @@ fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
90539436
9054 switch (operand_ty.zigTypeTag()) {9437 switch (operand_ty.zigTypeTag()) {
9055 .Enum => if (op != .Xchg) {9438 .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", .{});
9057 },9440 },
9058 .Bool => if (op != .Xchg) {9441 .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", .{});
9060 },9443 },
9061 .Float => switch (op) {9444 .Float => switch (op) {
9062 .Xchg, .Add, .Sub => {},9445 .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", .{}),
9064 },9447 },
9065 else => {},9448 else => {},
9066 }9449 }
...@@ -9068,7 +9451,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -9068,7 +9451,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
9068 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering);9451 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering);
90699452
9070 if (order == .Unordered) {9453 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", .{});
9072 }9455 }
90739456
9074 // special case zero bit types9457 // special case zero bit types
...@@ -9113,7 +9496,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE...@@ -9113,7 +9496,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
9113 });9496 });
9114}9497}
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 {
9117 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9500 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9118 const extra = sema.code.extraData(Zir.Inst.AtomicStore, inst_data.payload_index).data;9501 const extra = sema.code.extraData(Zir.Inst.AtomicStore, inst_data.payload_index).data;
9119 const src = inst_data.src();9502 const src = inst_data.src();
...@@ -9131,8 +9514,8 @@ fn zirAtomicStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -9131,8 +9514,8 @@ fn zirAtomicStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
91319514
9132 const air_tag: Air.Inst.Tag = switch (order) {9515 const air_tag: Air.Inst.Tag = switch (order) {
9133 .Acquire, .AcqRel => {9516 .Acquire, .AcqRel => {
9134 return sema.mod.fail(9517 return sema.fail(
9135 &block.base,9518 block,
9136 order_src,9519 order_src,
9137 "@atomicStore atomic ordering must not be Acquire or AcqRel",9520 "@atomicStore atomic ordering must not be Acquire or AcqRel",
9138 .{},9521 .{},
...@@ -9147,37 +9530,37 @@ fn zirAtomicStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil...@@ -9147,37 +9530,37 @@ fn zirAtomicStore(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
9147 return sema.storePtr2(block, src, ptr, ptr_src, operand, operand_src, air_tag);9530 return sema.storePtr2(block, src, ptr, ptr_src, operand, operand_src, air_tag);
9148}9531}
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 {
9151 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9534 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9152 const src = inst_data.src();9535 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", .{});
9154}9537}
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 {
9157 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9540 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9158 const src = inst_data.src();9541 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", .{});
9160}9543}
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 {
9163 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9546 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9164 const src = inst_data.src();9547 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", .{});
9166}9549}
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 {
9169 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9552 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9170 const src = inst_data.src();9553 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", .{});
9172}9555}
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 {
9175 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9558 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9176 const src = inst_data.src();9559 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", .{});
9178}9561}
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 {
9181 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9564 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9182 const extra = sema.code.extraData(Zir.Inst.Memcpy, inst_data.payload_index).data;9565 const extra = sema.code.extraData(Zir.Inst.Memcpy, inst_data.payload_index).data;
9183 const src = inst_data.src();9566 const src = inst_data.src();
...@@ -9188,16 +9571,16 @@ fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -9188,16 +9571,16 @@ fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
9188 const dest_ptr_ty = sema.typeOf(dest_ptr);9571 const dest_ptr_ty = sema.typeOf(dest_ptr);
91899572
9190 if (dest_ptr_ty.zigTypeTag() != .Pointer) {9573 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});
9192 }9575 }
9193 if (dest_ptr_ty.isConstPtr()) {9576 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});
9195 }9578 }
91969579
9197 const uncasted_src_ptr = sema.resolveInst(extra.source);9580 const uncasted_src_ptr = sema.resolveInst(extra.source);
9198 const uncasted_src_ptr_ty = sema.typeOf(uncasted_src_ptr);9581 const uncasted_src_ptr_ty = sema.typeOf(uncasted_src_ptr);
9199 if (uncasted_src_ptr_ty.zigTypeTag() != .Pointer) {9582 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 '{}'", .{
9201 uncasted_src_ptr_ty,9584 uncasted_src_ptr_ty,
9202 });9585 });
9203 }9586 }
...@@ -9224,7 +9607,7 @@ fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -9224,7 +9607,7 @@ fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
9224 _ = dest_ptr_val;9607 _ = dest_ptr_val;
9225 _ = src_ptr_val;9608 _ = src_ptr_val;
9226 _ = len_val;9609 _ = 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", .{});
9228 } else break :rs len_src;9611 } else break :rs len_src;
9229 } else break :rs src_src;9612 } else break :rs src_src;
9230 } else dest_src;9613 } else dest_src;
...@@ -9242,7 +9625,7 @@ fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -9242,7 +9625,7 @@ fn zirMemcpy(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
9242 });9625 });
9243}9626}
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 {
9246 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9629 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9247 const extra = sema.code.extraData(Zir.Inst.Memset, inst_data.payload_index).data;9630 const extra = sema.code.extraData(Zir.Inst.Memset, inst_data.payload_index).data;
9248 const src = inst_data.src();9631 const src = inst_data.src();
...@@ -9252,10 +9635,10 @@ fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -9252,10 +9635,10 @@ fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
9252 const dest_ptr = sema.resolveInst(extra.dest);9635 const dest_ptr = sema.resolveInst(extra.dest);
9253 const dest_ptr_ty = sema.typeOf(dest_ptr);9636 const dest_ptr_ty = sema.typeOf(dest_ptr);
9254 if (dest_ptr_ty.zigTypeTag() != .Pointer) {9637 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});
9256 }9639 }
9257 if (dest_ptr_ty.isConstPtr()) {9640 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});
9259 }9642 }
9260 const elem_ty = dest_ptr_ty.elemType2();9643 const elem_ty = dest_ptr_ty.elemType2();
9261 const value = try sema.coerce(block, elem_ty, sema.resolveInst(extra.byte), value_src);9644 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...@@ -9270,7 +9653,7 @@ fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
9270 _ = ptr_val;9653 _ = ptr_val;
9271 _ = len_val;9654 _ = len_val;
9272 _ = val;9655 _ = 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", .{});
9274 } else break :rs value_src;9657 } else break :rs value_src;
9275 } else break :rs len_src;9658 } else break :rs len_src;
9276 } else dest_src;9659 } else dest_src;
...@@ -9288,27 +9671,27 @@ fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro...@@ -9288,27 +9671,27 @@ fn zirMemset(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
9288 });9671 });
9289}9672}
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 {
9292 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9675 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9293 const src = inst_data.src();9676 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", .{});
9295}9678}
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 {
9298 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;9681 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9299 const src = inst_data.src();9682 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", .{});
9301}9684}
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 {
9304 const inst_data = sema.code.instructions.items(.data)[inst].un_node;9687 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
9305 const src = inst_data.src();9688 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", .{});
9307}9690}
93089691
9309fn zirAwait(9692fn zirAwait(
9310 sema: *Sema,9693 sema: *Sema,
9311 block: *Scope.Block,9694 block: *Block,
9312 inst: Zir.Inst.Index,9695 inst: Zir.Inst.Index,
9313 is_nosuspend: bool,9696 is_nosuspend: bool,
9314) CompileError!Air.Inst.Ref {9697) CompileError!Air.Inst.Ref {
...@@ -9316,12 +9699,12 @@ fn zirAwait(...@@ -9316,12 +9699,12 @@ fn zirAwait(
9316 const src = inst_data.src();9699 const src = inst_data.src();
93179700
9318 _ = is_nosuspend;9701 _ = is_nosuspend;
9319 return sema.mod.fail(&block.base, src, "TODO: Sema.zirAwait", .{});9702 return sema.fail(block, src, "TODO: Sema.zirAwait", .{});
9320}9703}
93219704
9322fn zirVarExtended(9705fn zirVarExtended(
9323 sema: *Sema,9706 sema: *Sema,
9324 block: *Scope.Block,9707 block: *Block,
9325 extended: Zir.Inst.Extended.InstData,9708 extended: Zir.Inst.Extended.InstData,
9326) CompileError!Air.Inst.Ref {9709) CompileError!Air.Inst.Ref {
9327 const extra = sema.code.extraData(Zir.Inst.ExtendedVar, extended.operand);9710 const extra = sema.code.extraData(Zir.Inst.ExtendedVar, extended.operand);
...@@ -9376,7 +9759,7 @@ fn zirVarExtended(...@@ -9376,7 +9759,7 @@ fn zirVarExtended(
9376 if (lib_name != null) {9759 if (lib_name != null) {
9377 // Look at the sema code for functions which has this logic, it just needs to9760 // Look at the sema code for functions which has this logic, it just needs to
9378 // be extracted and shared by both var and func9761 // 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", .{});
9380 }9763 }
93819764
9382 const new_var = try sema.gpa.create(Module.Var);9765 const new_var = try sema.gpa.create(Module.Var);
...@@ -9396,7 +9779,7 @@ fn zirVarExtended(...@@ -9396,7 +9779,7 @@ fn zirVarExtended(
93969779
9397fn zirFuncExtended(9780fn zirFuncExtended(
9398 sema: *Sema,9781 sema: *Sema,
9399 block: *Scope.Block,9782 block: *Block,
9400 extended: Zir.Inst.Extended.InstData,9783 extended: Zir.Inst.Extended.InstData,
9401 inst: Zir.Inst.Index,9784 inst: Zir.Inst.Index,
9402) CompileError!Air.Inst.Ref {9785) CompileError!Air.Inst.Ref {
...@@ -9463,7 +9846,7 @@ fn zirFuncExtended(...@@ -9463,7 +9846,7 @@ fn zirFuncExtended(
94639846
9464fn zirCUndef(9847fn zirCUndef(
9465 sema: *Sema,9848 sema: *Sema,
9466 block: *Scope.Block,9849 block: *Block,
9467 extended: Zir.Inst.Extended.InstData,9850 extended: Zir.Inst.Extended.InstData,
9468) CompileError!Air.Inst.Ref {9851) CompileError!Air.Inst.Ref {
9469 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;9852 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
...@@ -9476,7 +9859,7 @@ fn zirCUndef(...@@ -9476,7 +9859,7 @@ fn zirCUndef(
94769859
9477fn zirCInclude(9860fn zirCInclude(
9478 sema: *Sema,9861 sema: *Sema,
9479 block: *Scope.Block,9862 block: *Block,
9480 extended: Zir.Inst.Extended.InstData,9863 extended: Zir.Inst.Extended.InstData,
9481) CompileError!Air.Inst.Ref {9864) CompileError!Air.Inst.Ref {
9482 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;9865 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
...@@ -9489,7 +9872,7 @@ fn zirCInclude(...@@ -9489,7 +9872,7 @@ fn zirCInclude(
94899872
9490fn zirCDefine(9873fn zirCDefine(
9491 sema: *Sema,9874 sema: *Sema,
9492 block: *Scope.Block,9875 block: *Block,
9493 extended: Zir.Inst.Extended.InstData,9876 extended: Zir.Inst.Extended.InstData,
9494) CompileError!Air.Inst.Ref {9877) CompileError!Air.Inst.Ref {
9495 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;9878 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
...@@ -9507,41 +9890,41 @@ fn zirCDefine(...@@ -9507,41 +9890,41 @@ fn zirCDefine(
95079890
9508fn zirWasmMemorySize(9891fn zirWasmMemorySize(
9509 sema: *Sema,9892 sema: *Sema,
9510 block: *Scope.Block,9893 block: *Block,
9511 extended: Zir.Inst.Extended.InstData,9894 extended: Zir.Inst.Extended.InstData,
9512) CompileError!Air.Inst.Ref {9895) CompileError!Air.Inst.Ref {
9513 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;9896 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
9514 const src: LazySrcLoc = .{ .node_offset = extra.node };9897 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", .{});
9516}9899}
95179900
9518fn zirWasmMemoryGrow(9901fn zirWasmMemoryGrow(
9519 sema: *Sema,9902 sema: *Sema,
9520 block: *Scope.Block,9903 block: *Block,
9521 extended: Zir.Inst.Extended.InstData,9904 extended: Zir.Inst.Extended.InstData,
9522) CompileError!Air.Inst.Ref {9905) CompileError!Air.Inst.Ref {
9523 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;9906 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
9524 const src: LazySrcLoc = .{ .node_offset = extra.node };9907 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", .{});
9526}9909}
95279910
9528fn zirBuiltinExtern(9911fn zirBuiltinExtern(
9529 sema: *Sema,9912 sema: *Sema,
9530 block: *Scope.Block,9913 block: *Block,
9531 extended: Zir.Inst.Extended.InstData,9914 extended: Zir.Inst.Extended.InstData,
9532) CompileError!Air.Inst.Ref {9915) CompileError!Air.Inst.Ref {
9533 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;9916 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
9534 const src: LazySrcLoc = .{ .node_offset = extra.node };9917 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", .{});
9536}9919}
95379920
9538fn requireFunctionBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {9921fn requireFunctionBlock(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
9539 if (sema.func == null) {9922 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", .{});
9541 }9924 }
9542}9925}
95439926
9544fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {9927fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
9545 if (block.is_comptime) {9928 if (block.is_comptime) {
9546 return sema.failWithNeededComptime(block, src);9929 return sema.failWithNeededComptime(block, src);
9547 }9930 }
...@@ -9551,7 +9934,7 @@ fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void...@@ -9551,7 +9934,7 @@ fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void
9551/// Emit a compile error if type cannot be used for a runtime variable.9934/// Emit a compile error if type cannot be used for a runtime variable.
9552fn validateVarType(9935fn validateVarType(
9553 sema: *Sema,9936 sema: *Sema,
9554 block: *Scope.Block,9937 block: *Block,
9555 src: LazySrcLoc,9938 src: LazySrcLoc,
9556 var_ty: Type,9939 var_ty: Type,
9557 is_extern: bool,9940 is_extern: bool,
...@@ -9595,7 +9978,7 @@ fn validateVarType(...@@ -9595,7 +9978,7 @@ fn validateVarType(
9595 },9978 },
9596 } else unreachable; // TODO should not need else unreachable9979 } else unreachable; // TODO should not need else unreachable
9597 if (!ok) {9980 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});
9599 }9982 }
9600}9983}
96019984
...@@ -9610,17 +9993,18 @@ pub const PanicId = enum {...@@ -9610,17 +9993,18 @@ pub const PanicId = enum {
96109993
9611fn addSafetyCheck(9994fn addSafetyCheck(
9612 sema: *Sema,9995 sema: *Sema,
9613 parent_block: *Scope.Block,9996 parent_block: *Block,
9614 ok: Air.Inst.Ref,9997 ok: Air.Inst.Ref,
9615 panic_id: PanicId,9998 panic_id: PanicId,
9616) !void {9999) !void {
9617 const gpa = sema.gpa;10000 const gpa = sema.gpa;
961810001
9619 var fail_block: Scope.Block = .{10002 var fail_block: Block = .{
9620 .parent = parent_block,10003 .parent = parent_block,
9621 .sema = sema,10004 .sema = sema,
9622 .wip_capture_scope = parent_block.wip_capture_scope,
9623 .src_decl = parent_block.src_decl,10005 .src_decl = parent_block.src_decl,
10006 .namespace = parent_block.namespace,
10007 .wip_capture_scope = parent_block.wip_capture_scope,
9624 .instructions = .{},10008 .instructions = .{},
9625 .inlining = parent_block.inlining,10009 .inlining = parent_block.inlining,
9626 .is_comptime = parent_block.is_comptime,10010 .is_comptime = parent_block.is_comptime,
...@@ -9679,7 +10063,7 @@ fn addSafetyCheck(...@@ -9679,7 +10063,7 @@ fn addSafetyCheck(
967910063
9680fn panicWithMsg(10064fn panicWithMsg(
9681 sema: *Sema,10065 sema: *Sema,
9682 block: *Scope.Block,10066 block: *Block,
9683 src: LazySrcLoc,10067 src: LazySrcLoc,
9684 msg_inst: Air.Inst.Ref,10068 msg_inst: Air.Inst.Ref,
9685) !Zir.Inst.Index {10069) !Zir.Inst.Index {
...@@ -9699,7 +10083,7 @@ fn panicWithMsg(...@@ -9699,7 +10083,7 @@ fn panicWithMsg(
9699 const stack_trace_ty = try sema.resolveTypeFields(block, src, unresolved_stack_trace_ty);10083 const stack_trace_ty = try sema.resolveTypeFields(block, src, unresolved_stack_trace_ty);
9700 const ptr_stack_trace_ty = try Type.ptr(arena, .{10084 const ptr_stack_trace_ty = try Type.ptr(arena, .{
9701 .pointee_type = stack_trace_ty,10085 .pointee_type = stack_trace_ty,
9702 .@"addrspace" = target_util.defaultAddressSpace(sema.mod.getTarget(), .global_constant), // TODO might need a place that is more dynamic10086 .@"addrspace" = target_util.defaultAddressSpace(mod.getTarget(), .global_constant), // TODO might need a place that is more dynamic
9703 });10087 });
9704 const null_stack_trace = try sema.addConstant(10088 const null_stack_trace = try sema.addConstant(
9705 try Module.optionalType(arena, ptr_stack_trace_ty),10089 try Module.optionalType(arena, ptr_stack_trace_ty),
...@@ -9713,7 +10097,7 @@ fn panicWithMsg(...@@ -9713,7 +10097,7 @@ fn panicWithMsg(
971310097
9714fn safetyPanic(10098fn safetyPanic(
9715 sema: *Sema,10099 sema: *Sema,
9716 block: *Scope.Block,10100 block: *Block,
9717 src: LazySrcLoc,10101 src: LazySrcLoc,
9718 panic_id: PanicId,10102 panic_id: PanicId,
9719) CompileError!Zir.Inst.Index {10103) CompileError!Zir.Inst.Index {
...@@ -9741,17 +10125,17 @@ fn safetyPanic(...@@ -9741,17 +10125,17 @@ fn safetyPanic(
9741 return sema.panicWithMsg(block, src, casted_msg_inst);10125 return sema.panicWithMsg(block, src, casted_msg_inst);
9742}10126}
974310127
9744fn emitBackwardBranch(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {10128fn emitBackwardBranch(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
9745 sema.branch_count += 1;10129 sema.branch_count += 1;
9746 if (sema.branch_count > sema.branch_quota) {10130 if (sema.branch_count > sema.branch_quota) {
9747 // TODO show the "called from here" stack10131 // 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});
9749 }10133 }
9750}10134}
975110135
9752fn fieldVal(10136fn fieldVal(
9753 sema: *Sema,10137 sema: *Sema,
9754 block: *Scope.Block,10138 block: *Block,
9755 src: LazySrcLoc,10139 src: LazySrcLoc,
9756 object: Air.Inst.Ref,10140 object: Air.Inst.Ref,
9757 field_name: []const u8,10141 field_name: []const u8,
...@@ -9760,7 +10144,6 @@ fn fieldVal(...@@ -9760,7 +10144,6 @@ fn fieldVal(
9760 // When editing this function, note that there is corresponding logic to be edited10144 // When editing this function, note that there is corresponding logic to be edited
9761 // in `fieldPtr`. This function takes a value and returns a value.10145 // in `fieldPtr`. This function takes a value and returns a value.
976210146
9763 const mod = sema.mod;
9764 const arena = sema.arena;10147 const arena = sema.arena;
9765 const object_src = src; // TODO better source location10148 const object_src = src; // TODO better source location
9766 const object_ty = sema.typeOf(object);10149 const object_ty = sema.typeOf(object);
...@@ -9773,8 +10156,8 @@ fn fieldVal(...@@ -9773,8 +10156,8 @@ fn fieldVal(
9773 try Value.Tag.int_u64.create(arena, object_ty.arrayLen()),10156 try Value.Tag.int_u64.create(arena, object_ty.arrayLen()),
9774 );10157 );
9775 } else {10158 } else {
9776 return mod.fail(10159 return sema.fail(
9777 &block.base,10160 block,
9778 field_name_src,10161 field_name_src,
9779 "no member named '{s}' in '{}'",10162 "no member named '{s}' in '{}'",
9780 .{ field_name, object_ty },10163 .{ field_name, object_ty },
...@@ -9788,8 +10171,8 @@ fn fieldVal(...@@ -9788,8 +10171,8 @@ fn fieldVal(
9788 const result_ty = object_ty.slicePtrFieldType(buf);10171 const result_ty = object_ty.slicePtrFieldType(buf);
9789 if (try sema.resolveMaybeUndefVal(block, object_src, object)) |val| {10172 if (try sema.resolveMaybeUndefVal(block, object_src, object)) |val| {
9790 if (val.isUndef()) return sema.addConstUndef(result_ty);10173 if (val.isUndef()) return sema.addConstUndef(result_ty);
9791 return mod.fail(10174 return sema.fail(
9792 &block.base,10175 block,
9793 field_name_src,10176 field_name_src,
9794 "TODO implement comptime slice ptr",10177 "TODO implement comptime slice ptr",
9795 .{},10178 .{},
...@@ -9809,8 +10192,8 @@ fn fieldVal(...@@ -9809,8 +10192,8 @@ fn fieldVal(
9809 try sema.requireRuntimeBlock(block, src);10192 try sema.requireRuntimeBlock(block, src);
9810 return block.addTyOp(.slice_len, result_ty, object);10193 return block.addTyOp(.slice_len, result_ty, object);
9811 } else {10194 } else {
9812 return mod.fail(10195 return sema.fail(
9813 &block.base,10196 block,
9814 field_name_src,10197 field_name_src,
9815 "no member named '{s}' in '{}'",10198 "no member named '{s}' in '{}'",
9816 .{ field_name, object_ty },10199 .{ field_name, object_ty },
...@@ -9827,8 +10210,8 @@ fn fieldVal(...@@ -9827,8 +10210,8 @@ fn fieldVal(
9827 try Value.Tag.int_u64.create(arena, ptr_child.arrayLen()),10210 try Value.Tag.int_u64.create(arena, ptr_child.arrayLen()),
9828 );10211 );
9829 } else {10212 } else {
9830 return mod.fail(10213 return sema.fail(
9831 &block.base,10214 block,
9832 field_name_src,10215 field_name_src,
9833 "no member named '{s}' in '{}'",10216 "no member named '{s}' in '{}'",
9834 .{ field_name, object_ty },10217 .{ field_name, object_ty },
...@@ -9865,10 +10248,10 @@ fn fieldVal(...@@ -9865,10 +10248,10 @@ fn fieldVal(
9865 break :blk name;10248 break :blk name;
9866 }10249 }
9867 }10250 }
9868 return mod.fail(&block.base, src, "no error named '{s}' in '{}'", .{10251 return sema.fail(block, src, "no error named '{s}' in '{}'", .{
9869 field_name, child_type,10252 field_name, child_type,
9870 });10253 });
9871 } else (try mod.getErrorValue(field_name)).key;10254 } else (try sema.mod.getErrorValue(field_name)).key;
987210255
9873 return sema.addConstant(10256 return sema.addConstant(
9874 try child_type.copy(arena),10257 try child_type.copy(arena),
...@@ -9888,7 +10271,7 @@ fn fieldVal(...@@ -9888,7 +10271,7 @@ fn fieldVal(
9888 .Union => "union",10271 .Union => "union",
9889 else => unreachable,10272 else => unreachable,
9890 };10273 };
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}'", .{
9892 kw_name, child_type, field_name,10275 kw_name, child_type, field_name,
9893 });10276 });
9894 },10277 },
...@@ -9900,14 +10283,14 @@ fn fieldVal(...@@ -9900,14 +10283,14 @@ fn fieldVal(
9900 }10283 }
9901 const field_index = child_type.enumFieldIndex(field_name) orelse {10284 const field_index = child_type.enumFieldIndex(field_name) orelse {
9902 const msg = msg: {10285 const msg = msg: {
9903 const msg = try mod.errMsg(10286 const msg = try sema.errMsg(
9904 &block.base,10287 block,
9905 src,10288 src,
9906 "enum '{}' has no member named '{s}'",10289 "enum '{}' has no member named '{s}'",
9907 .{ child_type, field_name },10290 .{ child_type, field_name },
9908 );10291 );
9909 errdefer msg.destroy(sema.gpa);10292 errdefer msg.destroy(sema.gpa);
9910 try mod.errNoteNonLazy(10293 try sema.mod.errNoteNonLazy(
9911 child_type.declSrcLoc(),10294 child_type.declSrcLoc(),
9912 msg,10295 msg,
9913 "enum declared here",10296 "enum declared here",
...@@ -9915,25 +10298,25 @@ fn fieldVal(...@@ -9915,25 +10298,25 @@ fn fieldVal(
9915 );10298 );
9916 break :msg msg;10299 break :msg msg;
9917 };10300 };
9918 return mod.failWithOwnedErrorMsg(&block.base, msg);10301 return sema.failWithOwnedErrorMsg(msg);
9919 };10302 };
9920 const field_index_u32 = @intCast(u32, field_index);10303 const field_index_u32 = @intCast(u32, field_index);
9921 const enum_val = try Value.Tag.enum_field_index.create(arena, field_index_u32);10304 const enum_val = try Value.Tag.enum_field_index.create(arena, field_index_u32);
9922 return sema.addConstant(try child_type.copy(arena), enum_val);10305 return sema.addConstant(try child_type.copy(arena), enum_val);
9923 },10306 },
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}),
9925 }10308 }
9926 },10309 },
9927 .Struct => return sema.structFieldVal(block, src, object, field_name, field_name_src, object_ty),10310 .Struct => return sema.structFieldVal(block, src, object, field_name, field_name_src, object_ty),
9928 .Union => return sema.unionFieldVal(block, src, object, field_name, field_name_src, object_ty),10311 .Union => return sema.unionFieldVal(block, src, object, field_name, field_name_src, object_ty),
9929 else => {},10312 else => {},
9930 }10313 }
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});
9932}10315}
993310316
9934fn fieldPtr(10317fn fieldPtr(
9935 sema: *Sema,10318 sema: *Sema,
9936 block: *Scope.Block,10319 block: *Block,
9937 src: LazySrcLoc,10320 src: LazySrcLoc,
9938 object_ptr: Air.Inst.Ref,10321 object_ptr: Air.Inst.Ref,
9939 field_name: []const u8,10322 field_name: []const u8,
...@@ -9942,12 +10325,11 @@ fn fieldPtr(...@@ -9942,12 +10325,11 @@ fn fieldPtr(
9942 // When editing this function, note that there is corresponding logic to be edited10325 // When editing this function, note that there is corresponding logic to be edited
9943 // in `fieldVal`. This function takes a pointer and returns a pointer.10326 // in `fieldVal`. This function takes a pointer and returns a pointer.
994410327
9945 const mod = sema.mod;
9946 const object_ptr_src = src; // TODO better source location10328 const object_ptr_src = src; // TODO better source location
9947 const object_ptr_ty = sema.typeOf(object_ptr);10329 const object_ptr_ty = sema.typeOf(object_ptr);
9948 const object_ty = switch (object_ptr_ty.zigTypeTag()) {10330 const object_ty = switch (object_ptr_ty.zigTypeTag()) {
9949 .Pointer => object_ptr_ty.elemType(),10331 .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}),
9951 };10333 };
9952 switch (object_ty.zigTypeTag()) {10334 switch (object_ty.zigTypeTag()) {
9953 .Array => {10335 .Array => {
...@@ -9959,8 +10341,8 @@ fn fieldPtr(...@@ -9959,8 +10341,8 @@ fn fieldPtr(
9959 try Value.Tag.int_u64.create(anon_decl.arena(), object_ty.arrayLen()),10341 try Value.Tag.int_u64.create(anon_decl.arena(), object_ty.arrayLen()),
9960 ));10342 ));
9961 } else {10343 } else {
9962 return mod.fail(10344 return sema.fail(
9963 &block.base,10345 block,
9964 field_name_src,10346 field_name_src,
9965 "no member named '{s}' in '{}'",10347 "no member named '{s}' in '{}'",
9966 .{ field_name, object_ty },10348 .{ field_name, object_ty },
...@@ -9977,22 +10359,22 @@ fn fieldPtr(...@@ -9977,22 +10359,22 @@ fn fieldPtr(
9977 // the runtime value to it, and then return the `alloc`.10359 // the runtime value to it, and then return the `alloc`.
9978 // In both cases the pointer should be const.10360 // In both cases the pointer should be const.
9979 if (mem.eql(u8, field_name, "ptr")) {10361 if (mem.eql(u8, field_name, "ptr")) {
9980 return mod.fail(10362 return sema.fail(
9981 &block.base,10363 block,
9982 field_name_src,10364 field_name_src,
9983 "TODO: implement reference to 'ptr' field of slice '{}'",10365 "TODO: implement reference to 'ptr' field of slice '{}'",
9984 .{object_ty},10366 .{object_ty},
9985 );10367 );
9986 } else if (mem.eql(u8, field_name, "len")) {10368 } else if (mem.eql(u8, field_name, "len")) {
9987 return mod.fail(10369 return sema.fail(
9988 &block.base,10370 block,
9989 field_name_src,10371 field_name_src,
9990 "TODO: implement reference to 'len' field of slice '{}'",10372 "TODO: implement reference to 'len' field of slice '{}'",
9991 .{object_ty},10373 .{object_ty},
9992 );10374 );
9993 } else {10375 } else {
9994 return mod.fail(10376 return sema.fail(
9995 &block.base,10377 block,
9996 field_name_src,10378 field_name_src,
9997 "no member named '{s}' in '{}'",10379 "no member named '{s}' in '{}'",
9998 .{ field_name, object_ty },10380 .{ field_name, object_ty },
...@@ -10011,8 +10393,8 @@ fn fieldPtr(...@@ -10011,8 +10393,8 @@ fn fieldPtr(
10011 try Value.Tag.int_u64.create(anon_decl.arena(), ptr_child.arrayLen()),10393 try Value.Tag.int_u64.create(anon_decl.arena(), ptr_child.arrayLen()),
10012 ));10394 ));
10013 } else {10395 } else {
10014 return mod.fail(10396 return sema.fail(
10015 &block.base,10397 block,
10016 field_name_src,10398 field_name_src,
10017 "no member named '{s}' in '{}'",10399 "no member named '{s}' in '{}'",
10018 .{ field_name, object_ty },10400 .{ field_name, object_ty },
...@@ -10051,10 +10433,10 @@ fn fieldPtr(...@@ -10051,10 +10433,10 @@ fn fieldPtr(
10051 break :blk name;10433 break :blk name;
10052 }10434 }
10053 }10435 }
10054 return mod.fail(&block.base, src, "no error named '{s}' in '{}'", .{10436 return sema.fail(block, src, "no error named '{s}' in '{}'", .{
10055 field_name, child_type,10437 field_name, child_type,
10056 });10438 });
10057 } else (try mod.getErrorValue(field_name)).key;10439 } else (try sema.mod.getErrorValue(field_name)).key;
1005810440
10059 var anon_decl = try block.startAnonDecl();10441 var anon_decl = try block.startAnonDecl();
10060 defer anon_decl.deinit();10442 defer anon_decl.deinit();
...@@ -10076,7 +10458,7 @@ fn fieldPtr(...@@ -10076,7 +10458,7 @@ fn fieldPtr(
10076 .Union => "union",10458 .Union => "union",
10077 else => unreachable,10459 else => unreachable,
10078 };10460 };
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}'", .{
10080 kw_name, child_type, field_name,10462 kw_name, child_type, field_name,
10081 });10463 });
10082 },10464 },
...@@ -10088,14 +10470,14 @@ fn fieldPtr(...@@ -10088,14 +10470,14 @@ fn fieldPtr(
10088 }10470 }
10089 const field_index = child_type.enumFieldIndex(field_name) orelse {10471 const field_index = child_type.enumFieldIndex(field_name) orelse {
10090 const msg = msg: {10472 const msg = msg: {
10091 const msg = try mod.errMsg(10473 const msg = try sema.errMsg(
10092 &block.base,10474 block,
10093 src,10475 src,
10094 "enum '{}' has no member named '{s}'",10476 "enum '{}' has no member named '{s}'",
10095 .{ child_type, field_name },10477 .{ child_type, field_name },
10096 );10478 );
10097 errdefer msg.destroy(sema.gpa);10479 errdefer msg.destroy(sema.gpa);
10098 try mod.errNoteNonLazy(10480 try sema.mod.errNoteNonLazy(
10099 child_type.declSrcLoc(),10481 child_type.declSrcLoc(),
10100 msg,10482 msg,
10101 "enum declared here",10483 "enum declared here",
...@@ -10103,7 +10485,7 @@ fn fieldPtr(...@@ -10103,7 +10485,7 @@ fn fieldPtr(
10103 );10485 );
10104 break :msg msg;10486 break :msg msg;
10105 };10487 };
10106 return mod.failWithOwnedErrorMsg(&block.base, msg);10488 return sema.failWithOwnedErrorMsg(msg);
10107 };10489 };
10108 const field_index_u32 = @intCast(u32, field_index);10490 const field_index_u32 = @intCast(u32, field_index);
10109 var anon_decl = try block.startAnonDecl();10491 var anon_decl = try block.startAnonDecl();
...@@ -10113,19 +10495,19 @@ fn fieldPtr(...@@ -10113,19 +10495,19 @@ fn fieldPtr(
10113 try Value.Tag.enum_field_index.create(anon_decl.arena(), field_index_u32),10495 try Value.Tag.enum_field_index.create(anon_decl.arena(), field_index_u32),
10114 ));10496 ));
10115 },10497 },
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}),
10117 }10499 }
10118 },10500 },
10119 .Struct => return sema.structFieldPtr(block, src, object_ptr, field_name, field_name_src, object_ty),10501 .Struct => return sema.structFieldPtr(block, src, object_ptr, field_name, field_name_src, object_ty),
10120 .Union => return sema.unionFieldPtr(block, src, object_ptr, field_name, field_name_src, object_ty),10502 .Union => return sema.unionFieldPtr(block, src, object_ptr, field_name, field_name_src, object_ty),
10121 else => {},10503 else => {},
10122 }10504 }
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});
10124}10506}
1012510507
10126fn fieldCallBind(10508fn fieldCallBind(
10127 sema: *Sema,10509 sema: *Sema,
10128 block: *Scope.Block,10510 block: *Block,
10129 src: LazySrcLoc,10511 src: LazySrcLoc,
10130 raw_ptr: Air.Inst.Ref,10512 raw_ptr: Air.Inst.Ref,
10131 field_name: []const u8,10513 field_name: []const u8,
...@@ -10134,13 +10516,12 @@ fn fieldCallBind(...@@ -10134,13 +10516,12 @@ fn fieldCallBind(
10134 // When editing this function, note that there is corresponding logic to be edited10516 // When editing this function, note that there is corresponding logic to be edited
10135 // in `fieldVal`. This function takes a pointer and returns a pointer.10517 // in `fieldVal`. This function takes a pointer and returns a pointer.
1013610518
10137 const mod = sema.mod;
10138 const raw_ptr_src = src; // TODO better source location10519 const raw_ptr_src = src; // TODO better source location
10139 const raw_ptr_ty = sema.typeOf(raw_ptr);10520 const raw_ptr_ty = sema.typeOf(raw_ptr);
10140 const inner_ty = if (raw_ptr_ty.zigTypeTag() == .Pointer and raw_ptr_ty.ptrSize() == .One)10521 const inner_ty = if (raw_ptr_ty.zigTypeTag() == .Pointer and raw_ptr_ty.ptrSize() == .One)
10141 raw_ptr_ty.childType()10522 raw_ptr_ty.childType()
10142 else10523 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
10145 // Optionally dereference a second pointer to get the concrete type.10526 // Optionally dereference a second pointer to get the concrete type.
10146 const is_double_ptr = inner_ty.zigTypeTag() == .Pointer and inner_ty.ptrSize() == .One;10527 const is_double_ptr = inner_ty.zigTypeTag() == .Pointer and inner_ty.ptrSize() == .One;
...@@ -10209,7 +10590,7 @@ fn fieldCallBind(...@@ -10209,7 +10590,7 @@ fn fieldCallBind(
10209 };10590 };
10210 return sema.analyzeLoad(block, src, ptr_inst, src);10591 return sema.analyzeLoad(block, src, ptr_inst, src);
10211 },10592 },
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", .{}),
10213 .Type => {10594 .Type => {
10214 const namespace = try sema.analyzeLoad(block, src, object_ptr, src);10595 const namespace = try sema.analyzeLoad(block, src, object_ptr, src);
10215 return sema.fieldVal(block, src, namespace, field_name, field_name_src);10596 return sema.fieldVal(block, src, namespace, field_name, field_name_src);
...@@ -10262,29 +10643,28 @@ fn fieldCallBind(...@@ -10262,29 +10643,28 @@ fn fieldCallBind(
10262 else => {},10643 else => {},
10263 }10644 }
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 });
10266}10647}
1026710648
10268fn namespaceLookup(10649fn namespaceLookup(
10269 sema: *Sema,10650 sema: *Sema,
10270 block: *Scope.Block,10651 block: *Block,
10271 src: LazySrcLoc,10652 src: LazySrcLoc,
10272 namespace: *Scope.Namespace,10653 namespace: *Namespace,
10273 decl_name: []const u8,10654 decl_name: []const u8,
10274) CompileError!?*Decl {10655) CompileError!?*Decl {
10275 const mod = sema.mod;
10276 const gpa = sema.gpa;10656 const gpa = sema.gpa;
10277 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl| {10657 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()) {
10279 const msg = msg: {10659 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'", .{
10281 decl_name,10661 decl_name,
10282 });10662 });
10283 errdefer msg.destroy(gpa);10663 errdefer msg.destroy(gpa);
10284 try mod.errNoteNonLazy(decl.srcLoc(), msg, "declared here", .{});10664 try sema.mod.errNoteNonLazy(decl.srcLoc(), msg, "declared here", .{});
10285 break :msg msg;10665 break :msg msg;
10286 };10666 };
10287 return mod.failWithOwnedErrorMsg(&block.base, msg);10667 return sema.failWithOwnedErrorMsg(msg);
10288 }10668 }
10289 return decl;10669 return decl;
10290 }10670 }
...@@ -10293,9 +10673,9 @@ fn namespaceLookup(...@@ -10293,9 +10673,9 @@ fn namespaceLookup(
1029310673
10294fn namespaceLookupRef(10674fn namespaceLookupRef(
10295 sema: *Sema,10675 sema: *Sema,
10296 block: *Scope.Block,10676 block: *Block,
10297 src: LazySrcLoc,10677 src: LazySrcLoc,
10298 namespace: *Scope.Namespace,10678 namespace: *Namespace,
10299 decl_name: []const u8,10679 decl_name: []const u8,
10300) CompileError!?Air.Inst.Ref {10680) CompileError!?Air.Inst.Ref {
10301 const decl = (try sema.namespaceLookup(block, src, namespace, decl_name)) orelse return null;10681 const decl = (try sema.namespaceLookup(block, src, namespace, decl_name)) orelse return null;
...@@ -10304,7 +10684,7 @@ fn namespaceLookupRef(...@@ -10304,7 +10684,7 @@ fn namespaceLookupRef(
1030410684
10305fn structFieldPtr(10685fn structFieldPtr(
10306 sema: *Sema,10686 sema: *Sema,
10307 block: *Scope.Block,10687 block: *Block,
10308 src: LazySrcLoc,10688 src: LazySrcLoc,
10309 struct_ptr: Air.Inst.Ref,10689 struct_ptr: Air.Inst.Ref,
10310 field_name: []const u8,10690 field_name: []const u8,
...@@ -10344,7 +10724,7 @@ fn structFieldPtr(...@@ -10344,7 +10724,7 @@ fn structFieldPtr(
1034410724
10345fn structFieldVal(10725fn structFieldVal(
10346 sema: *Sema,10726 sema: *Sema,
10347 block: *Scope.Block,10727 block: *Block,
10348 src: LazySrcLoc,10728 src: LazySrcLoc,
10349 struct_byval: Air.Inst.Ref,10729 struct_byval: Air.Inst.Ref,
10350 field_name: []const u8,10730 field_name: []const u8,
...@@ -10382,7 +10762,7 @@ fn structFieldVal(...@@ -10382,7 +10762,7 @@ fn structFieldVal(
1038210762
10383fn unionFieldPtr(10763fn unionFieldPtr(
10384 sema: *Sema,10764 sema: *Sema,
10385 block: *Scope.Block,10765 block: *Block,
10386 src: LazySrcLoc,10766 src: LazySrcLoc,
10387 union_ptr: Air.Inst.Ref,10767 union_ptr: Air.Inst.Ref,
10388 field_name: []const u8,10768 field_name: []const u8,
...@@ -10424,7 +10804,7 @@ fn unionFieldPtr(...@@ -10424,7 +10804,7 @@ fn unionFieldPtr(
1042410804
10425fn unionFieldVal(10805fn unionFieldVal(
10426 sema: *Sema,10806 sema: *Sema,
10427 block: *Scope.Block,10807 block: *Block,
10428 src: LazySrcLoc,10808 src: LazySrcLoc,
10429 union_byval: Air.Inst.Ref,10809 union_byval: Air.Inst.Ref,
10430 field_name: []const u8,10810 field_name: []const u8,
...@@ -10450,12 +10830,12 @@ fn unionFieldVal(...@@ -10450,12 +10830,12 @@ fn unionFieldVal(
10450 }10830 }
1045110831
10452 try sema.requireRuntimeBlock(block, src);10832 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", .{});
10454}10834}
1045510835
10456fn elemPtr(10836fn elemPtr(
10457 sema: *Sema,10837 sema: *Sema,
10458 block: *Scope.Block,10838 block: *Block,
10459 src: LazySrcLoc,10839 src: LazySrcLoc,
10460 array_ptr: Air.Inst.Ref,10840 array_ptr: Air.Inst.Ref,
10461 elem_index: Air.Inst.Ref,10841 elem_index: Air.Inst.Ref,
...@@ -10465,10 +10845,10 @@ fn elemPtr(...@@ -10465,10 +10845,10 @@ fn elemPtr(
10465 const array_ptr_ty = sema.typeOf(array_ptr);10845 const array_ptr_ty = sema.typeOf(array_ptr);
10466 const array_ty = switch (array_ptr_ty.zigTypeTag()) {10846 const array_ty = switch (array_ptr_ty.zigTypeTag()) {
10467 .Pointer => array_ptr_ty.elemType(),10847 .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}),
10469 };10849 };
10470 if (!array_ty.isIndexable()) {10850 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});
10472 }10852 }
10473 if (array_ty.isSinglePointer() and array_ty.elemType().zigTypeTag() == .Array) {10853 if (array_ty.isSinglePointer() and array_ty.elemType().zigTypeTag() == .Array) {
10474 // we have to deref the ptr operand to get the actual array pointer10854 // we have to deref the ptr operand to get the actual array pointer
...@@ -10479,12 +10859,12 @@ fn elemPtr(...@@ -10479,12 +10859,12 @@ fn elemPtr(
10479 return sema.elemPtrArray(block, src, array_ptr, elem_index, elem_index_src);10859 return sema.elemPtrArray(block, src, array_ptr, elem_index, elem_index_src);
10480 }10860 }
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", .{});
10483}10863}
1048410864
10485fn elemVal(10865fn elemVal(
10486 sema: *Sema,10866 sema: *Sema,
10487 block: *Scope.Block,10867 block: *Block,
10488 src: LazySrcLoc,10868 src: LazySrcLoc,
10489 array_maybe_ptr: Air.Inst.Ref,10869 array_maybe_ptr: Air.Inst.Ref,
10490 elem_index: Air.Inst.Ref,10870 elem_index: Air.Inst.Ref,
...@@ -10497,7 +10877,7 @@ fn elemVal(...@@ -10497,7 +10877,7 @@ fn elemVal(
10497 .Slice => {10877 .Slice => {
10498 if (try sema.resolveDefinedValue(block, src, array_maybe_ptr)) |slice_val| {10878 if (try sema.resolveDefinedValue(block, src, array_maybe_ptr)) |slice_val| {
10499 _ = slice_val;10879 _ = 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", .{});
10501 }10881 }
10502 try sema.requireRuntimeBlock(block, src);10882 try sema.requireRuntimeBlock(block, src);
10503 return block.addBinOp(.slice_elem_val, array_maybe_ptr, elem_index);10883 return block.addBinOp(.slice_elem_val, array_maybe_ptr, elem_index);
...@@ -10505,7 +10885,7 @@ fn elemVal(...@@ -10505,7 +10885,7 @@ fn elemVal(
10505 .Many, .C => {10885 .Many, .C => {
10506 if (try sema.resolveDefinedValue(block, src, array_maybe_ptr)) |ptr_val| {10886 if (try sema.resolveDefinedValue(block, src, array_maybe_ptr)) |ptr_val| {
10507 _ = ptr_val;10887 _ = 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", .{});
10509 }10889 }
10510 try sema.requireRuntimeBlock(block, src);10890 try sema.requireRuntimeBlock(block, src);
10511 return block.addBinOp(.ptr_elem_val, array_maybe_ptr, elem_index);10891 return block.addBinOp(.ptr_elem_val, array_maybe_ptr, elem_index);
...@@ -10520,7 +10900,7 @@ fn elemVal(...@@ -10520,7 +10900,7 @@ fn elemVal(
10520 const slice = try sema.analyzeLoad(block, src, array_maybe_ptr, array_ptr_src);10900 const slice = try sema.analyzeLoad(block, src, array_maybe_ptr, array_ptr_src);
10521 if (try sema.resolveDefinedValue(block, src, slice)) |slice_val| {10901 if (try sema.resolveDefinedValue(block, src, slice)) |slice_val| {
10522 _ = slice_val;10902 _ = 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", .{});
10524 }10904 }
10525 try sema.requireRuntimeBlock(block, src);10905 try sema.requireRuntimeBlock(block, src);
10526 return block.addBinOp(.slice_elem_val, slice, elem_index);10906 return block.addBinOp(.slice_elem_val, slice, elem_index);
...@@ -10534,7 +10914,7 @@ fn elemVal(...@@ -10534,7 +10914,7 @@ fn elemVal(
10534 const ptr = try sema.analyzeLoad(block, src, array_maybe_ptr, array_ptr_src);10914 const ptr = try sema.analyzeLoad(block, src, array_maybe_ptr, array_ptr_src);
10535 if (try sema.resolveDefinedValue(block, src, ptr)) |ptr_val| {10915 if (try sema.resolveDefinedValue(block, src, ptr)) |ptr_val| {
10536 _ = ptr_val;10916 _ = 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", .{});
10538 }10918 }
10539 try sema.requireRuntimeBlock(block, src);10919 try sema.requireRuntimeBlock(block, src);
10540 return block.addBinOp(.ptr_elem_val, ptr, elem_index);10920 return block.addBinOp(.ptr_elem_val, ptr, elem_index);
...@@ -10542,8 +10922,8 @@ fn elemVal(...@@ -10542,8 +10922,8 @@ fn elemVal(
10542 try sema.requireRuntimeBlock(block, src);10922 try sema.requireRuntimeBlock(block, src);
10543 return block.addBinOp(.ptr_ptr_elem_val, array_maybe_ptr, elem_index);10923 return block.addBinOp(.ptr_ptr_elem_val, array_maybe_ptr, elem_index);
10544 },10924 },
10545 .One => return sema.mod.fail(10925 .One => return sema.fail(
10546 &block.base,10926 block,
10547 array_ptr_src,10927 array_ptr_src,
10548 "expected pointer, found '{}'",10928 "expected pointer, found '{}'",
10549 .{indexable_ty.elemType()},10929 .{indexable_ty.elemType()},
...@@ -10553,8 +10933,8 @@ fn elemVal(...@@ -10553,8 +10933,8 @@ fn elemVal(
10553 const ptr = try sema.elemPtr(block, src, array_maybe_ptr, elem_index, elem_index_src);10933 const ptr = try sema.elemPtr(block, src, array_maybe_ptr, elem_index, elem_index_src);
10554 return sema.analyzeLoad(block, src, ptr, elem_index_src);10934 return sema.analyzeLoad(block, src, ptr, elem_index_src);
10555 },10935 },
10556 else => return sema.mod.fail(10936 else => return sema.fail(
10557 &block.base,10937 block,
10558 array_ptr_src,10938 array_ptr_src,
10559 "expected pointer, found '{}'",10939 "expected pointer, found '{}'",
10560 .{indexable_ty},10940 .{indexable_ty},
...@@ -10562,8 +10942,8 @@ fn elemVal(...@@ -10562,8 +10942,8 @@ fn elemVal(
10562 }10942 }
10563 },10943 },
10564 },10944 },
10565 else => return sema.mod.fail(10945 else => return sema.fail(
10566 &block.base,10946 block,
10567 array_ptr_src,10947 array_ptr_src,
10568 "expected pointer, found '{}'",10948 "expected pointer, found '{}'",
10569 .{maybe_ptr_ty},10949 .{maybe_ptr_ty},
...@@ -10573,7 +10953,7 @@ fn elemVal(...@@ -10573,7 +10953,7 @@ fn elemVal(
1057310953
10574fn elemPtrArray(10954fn elemPtrArray(
10575 sema: *Sema,10955 sema: *Sema,
10576 block: *Scope.Block,10956 block: *Block,
10577 src: LazySrcLoc,10957 src: LazySrcLoc,
10578 array_ptr: Air.Inst.Ref,10958 array_ptr: Air.Inst.Ref,
10579 elem_index: Air.Inst.Ref,10959 elem_index: Air.Inst.Ref,
...@@ -10613,7 +10993,7 @@ fn elemPtrArray(...@@ -10613,7 +10993,7 @@ fn elemPtrArray(
1061310993
10614fn coerce(10994fn coerce(
10615 sema: *Sema,10995 sema: *Sema,
10616 block: *Scope.Block,10996 block: *Block,
10617 dest_type_unresolved: Type,10997 dest_type_unresolved: Type,
10618 inst: Air.Inst.Ref,10998 inst: Air.Inst.Ref,
10619 inst_src: LazySrcLoc,10999 inst_src: LazySrcLoc,
...@@ -10631,10 +11011,10 @@ fn coerce(...@@ -10631,10 +11011,10 @@ fn coerce(
10631 if (dest_type.eql(inst_ty))11011 if (dest_type.eql(inst_ty))
10632 return inst;11012 return inst;
1063311013
10634 const mod = sema.mod;
10635 const arena = sema.arena;11014 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);
10638 if (in_memory_result == .ok) {11018 if (in_memory_result == .ok) {
10639 return sema.bitcast(block, dest_type, inst, inst_src);11019 return sema.bitcast(block, dest_type, inst, inst_src);
10640 }11020 }
...@@ -10651,8 +11031,6 @@ fn coerce(...@@ -10651,8 +11031,6 @@ fn coerce(
10651 if (try sema.coerceNum(block, dest_type, inst, inst_src)) |some|11031 if (try sema.coerceNum(block, dest_type, inst, inst_src)) |some|
10652 return some;11032 return some;
1065311033
10654 const target = mod.getTarget();
10655
10656 switch (dest_type.zigTypeTag()) {11034 switch (dest_type.zigTypeTag()) {
10657 .Optional => {11035 .Optional => {
10658 // null to ?T11036 // null to ?T
...@@ -10679,7 +11057,7 @@ fn coerce(...@@ -10679,7 +11057,7 @@ fn coerce(
10679 if (inst_ty.ptrAddressSpace() != dest_type.ptrAddressSpace()) break :src_array_ptr;11057 if (inst_ty.ptrAddressSpace() != dest_type.ptrAddressSpace()) break :src_array_ptr;
1068011058
10681 const dst_elem_type = dest_type.elemType();11059 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)) {
10683 .ok => {},11061 .ok => {},
10684 .no_match => break :src_array_ptr,11062 .no_match => break :src_array_ptr,
10685 }11063 }
...@@ -10748,14 +11126,14 @@ fn coerce(...@@ -10748,14 +11126,14 @@ fn coerce(
10748 const resolved_dest_type = try sema.resolveTypeFields(block, inst_src, dest_type);11126 const resolved_dest_type = try sema.resolveTypeFields(block, inst_src, dest_type);
10749 const field_index = resolved_dest_type.enumFieldIndex(bytes) orelse {11127 const field_index = resolved_dest_type.enumFieldIndex(bytes) orelse {
10750 const msg = msg: {11128 const msg = msg: {
10751 const msg = try mod.errMsg(11129 const msg = try sema.errMsg(
10752 &block.base,11130 block,
10753 inst_src,11131 inst_src,
10754 "enum '{}' has no field named '{s}'",11132 "enum '{}' has no field named '{s}'",
10755 .{ resolved_dest_type, bytes },11133 .{ resolved_dest_type, bytes },
10756 );11134 );
10757 errdefer msg.destroy(sema.gpa);11135 errdefer msg.destroy(sema.gpa);
10758 try mod.errNoteNonLazy(11136 try sema.mod.errNoteNonLazy(
10759 resolved_dest_type.declSrcLoc(),11137 resolved_dest_type.declSrcLoc(),
10760 msg,11138 msg,
10761 "enum declared here",11139 "enum declared here",
...@@ -10763,7 +11141,7 @@ fn coerce(...@@ -10763,7 +11141,7 @@ fn coerce(
10763 );11141 );
10764 break :msg msg;11142 break :msg msg;
10765 };11143 };
10766 return mod.failWithOwnedErrorMsg(&block.base, msg);11144 return sema.failWithOwnedErrorMsg(msg);
10767 };11145 };
10768 return sema.addConstant(11146 return sema.addConstant(
10769 resolved_dest_type,11147 resolved_dest_type,
...@@ -10786,7 +11164,7 @@ fn coerce(...@@ -10786,7 +11164,7 @@ fn coerce(
10786 else => {},11164 else => {},
10787 }11165 }
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 });
10790}11168}
1079111169
10792const InMemoryCoercionResult = enum {11170const InMemoryCoercionResult = enum {
...@@ -10887,7 +11265,7 @@ fn coerceInMemoryAllowed(dest_type: Type, src_type: Type, dest_is_mut: bool, tar...@@ -10887,7 +11265,7 @@ fn coerceInMemoryAllowed(dest_type: Type, src_type: Type, dest_is_mut: bool, tar
1088711265
10888fn coerceNum(11266fn coerceNum(
10889 sema: *Sema,11267 sema: *Sema,
10890 block: *Scope.Block,11268 block: *Block,
10891 dest_type: Type,11269 dest_type: Type,
10892 inst: Air.Inst.Ref,11270 inst: Air.Inst.Ref,
10893 inst_src: LazySrcLoc,11271 inst_src: LazySrcLoc,
...@@ -10903,13 +11281,13 @@ fn coerceNum(...@@ -10903,13 +11281,13 @@ fn coerceNum(
10903 .ComptimeInt, .Int => switch (src_zig_tag) {11281 .ComptimeInt, .Int => switch (src_zig_tag) {
10904 .Float, .ComptimeFloat => {11282 .Float, .ComptimeFloat => {
10905 if (val.floatHasFraction()) {11283 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 });
10907 }11285 }
10908 return sema.mod.fail(&block.base, inst_src, "TODO float to int", .{});11286 return sema.fail(block, inst_src, "TODO float to int", .{});
10909 },11287 },
10910 .Int, .ComptimeInt => {11288 .Int, .ComptimeInt => {
10911 if (!val.intFitsInType(dest_type, target)) {11289 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 });
10913 }11291 }
10914 return try sema.addConstant(dest_type, val);11292 return try sema.addConstant(dest_type, val);
10915 },11293 },
...@@ -10923,8 +11301,8 @@ fn coerceNum(...@@ -10923,8 +11301,8 @@ fn coerceNum(
10923 .Float => {11301 .Float => {
10924 const result_val = try val.floatCast(sema.arena, dest_type);11302 const result_val = try val.floatCast(sema.arena, dest_type);
10925 if (!val.eql(result_val, dest_type)) {11303 if (!val.eql(result_val, dest_type)) {
10926 return sema.mod.fail(11304 return sema.fail(
10927 &block.base,11305 block,
10928 inst_src,11306 inst_src,
10929 "type {} cannot represent float value {}",11307 "type {} cannot represent float value {}",
10930 .{ dest_type, val },11308 .{ dest_type, val },
...@@ -10937,8 +11315,8 @@ fn coerceNum(...@@ -10937,8 +11315,8 @@ fn coerceNum(
10937 // TODO implement this compile error11315 // TODO implement this compile error
10938 //const int_again_val = try result_val.floatToInt(sema.arena, inst_ty);11316 //const int_again_val = try result_val.floatToInt(sema.arena, inst_ty);
10939 //if (!int_again_val.eql(val, inst_ty)) {11317 //if (!int_again_val.eql(val, inst_ty)) {
10940 // return sema.mod.fail(11318 // return sema.fail(
10941 // &block.base,11319 // block,
10942 // inst_src,11320 // inst_src,
10943 // "type {} cannot represent integer value {}",11321 // "type {} cannot represent integer value {}",
10944 // .{ dest_type, val },11322 // .{ dest_type, val },
...@@ -10955,13 +11333,13 @@ fn coerceNum(...@@ -10955,13 +11333,13 @@ fn coerceNum(
1095511333
10956fn coerceVarArgParam(11334fn coerceVarArgParam(
10957 sema: *Sema,11335 sema: *Sema,
10958 block: *Scope.Block,11336 block: *Block,
10959 inst: Air.Inst.Ref,11337 inst: Air.Inst.Ref,
10960 inst_src: LazySrcLoc,11338 inst_src: LazySrcLoc,
10961) !Air.Inst.Ref {11339) !Air.Inst.Ref {
10962 const inst_ty = sema.typeOf(inst);11340 const inst_ty = sema.typeOf(inst);
10963 switch (inst_ty.zigTypeTag()) {11341 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", .{}),
10965 else => {},11343 else => {},
10966 }11344 }
10967 // TODO implement more of this function.11345 // TODO implement more of this function.
...@@ -10971,17 +11349,17 @@ fn coerceVarArgParam(...@@ -10971,17 +11349,17 @@ fn coerceVarArgParam(
10971// TODO migrate callsites to use storePtr2 instead.11349// TODO migrate callsites to use storePtr2 instead.
10972fn storePtr(11350fn storePtr(
10973 sema: *Sema,11351 sema: *Sema,
10974 block: *Scope.Block,11352 block: *Block,
10975 src: LazySrcLoc,11353 src: LazySrcLoc,
10976 ptr: Air.Inst.Ref,11354 ptr: Air.Inst.Ref,
10977 uncasted_operand: Air.Inst.Ref,11355 uncasted_operand: Air.Inst.Ref,
10978) !void {11356) CompileError!void {
10979 return sema.storePtr2(block, src, ptr, src, uncasted_operand, src, .store);11357 return sema.storePtr2(block, src, ptr, src, uncasted_operand, src, .store);
10980}11358}
1098111359
10982fn storePtr2(11360fn storePtr2(
10983 sema: *Sema,11361 sema: *Sema,
10984 block: *Scope.Block,11362 block: *Block,
10985 src: LazySrcLoc,11363 src: LazySrcLoc,
10986 ptr: Air.Inst.Ref,11364 ptr: Air.Inst.Ref,
10987 ptr_src: LazySrcLoc,11365 ptr_src: LazySrcLoc,
...@@ -10991,7 +11369,7 @@ fn storePtr2(...@@ -10991,7 +11369,7 @@ fn storePtr2(
10991) !void {11369) !void {
10992 const ptr_ty = sema.typeOf(ptr);11370 const ptr_ty = sema.typeOf(ptr);
10993 if (ptr_ty.isConstPtr())11371 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
10996 const elem_ty = ptr_ty.elemType();11374 const elem_ty = ptr_ty.elemType();
10997 const operand = try sema.coerce(block, elem_ty, uncasted_operand, operand_src);11375 const operand = try sema.coerce(block, elem_ty, uncasted_operand, operand_src);
...@@ -11000,7 +11378,7 @@ fn storePtr2(...@@ -11000,7 +11378,7 @@ fn storePtr2(
1100011378
11001 const runtime_src = if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| rs: {11379 const runtime_src = if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| rs: {
11002 const operand_val = (try sema.resolveMaybeUndefVal(block, operand_src, operand)) orelse11380 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", .{});
11004 if (ptr_val.tag() == .decl_ref_mut) {11382 if (ptr_val.tag() == .decl_ref_mut) {
11005 try sema.storePtrVal(block, src, ptr_val, operand_val, elem_ty);11383 try sema.storePtrVal(block, src, ptr_val, operand_val, elem_ty);
11006 return;11384 return;
...@@ -11018,7 +11396,7 @@ fn storePtr2(...@@ -11018,7 +11396,7 @@ fn storePtr2(
11018/// assert the store must be done at comptime.11396/// assert the store must be done at comptime.
11019fn storePtrVal(11397fn storePtrVal(
11020 sema: *Sema,11398 sema: *Sema,
11021 block: *Scope.Block,11399 block: *Block,
11022 src: LazySrcLoc,11400 src: LazySrcLoc,
11023 ptr_val: Value,11401 ptr_val: Value,
11024 operand_val: Value,11402 operand_val: Value,
...@@ -11028,21 +11406,21 @@ fn storePtrVal(...@@ -11028,21 +11406,21 @@ fn storePtrVal(
11028 if (decl_ref_mut.data.runtime_index < block.runtime_index) {11406 if (decl_ref_mut.data.runtime_index < block.runtime_index) {
11029 if (block.runtime_cond) |cond_src| {11407 if (block.runtime_cond) |cond_src| {
11030 const msg = msg: {11408 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", .{});
11032 errdefer msg.destroy(sema.gpa);11410 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", .{});
11034 break :msg msg;11412 break :msg msg;
11035 };11413 };
11036 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);11414 return sema.failWithOwnedErrorMsg(msg);
11037 }11415 }
11038 if (block.runtime_loop) |loop_src| {11416 if (block.runtime_loop) |loop_src| {
11039 const msg = msg: {11417 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", .{});
11041 errdefer msg.destroy(sema.gpa);11419 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", .{});
11043 break :msg msg;11421 break :msg msg;
11044 };11422 };
11045 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);11423 return sema.failWithOwnedErrorMsg(msg);
11046 }11424 }
11047 unreachable;11425 unreachable;
11048 }11426 }
...@@ -11063,7 +11441,7 @@ fn storePtrVal(...@@ -11063,7 +11441,7 @@ fn storePtrVal(
1106311441
11064fn bitcast(11442fn bitcast(
11065 sema: *Sema,11443 sema: *Sema,
11066 block: *Scope.Block,11444 block: *Block,
11067 dest_type: Type,11445 dest_type: Type,
11068 inst: Air.Inst.Ref,11446 inst: Air.Inst.Ref,
11069 inst_src: LazySrcLoc,11447 inst_src: LazySrcLoc,
...@@ -11079,7 +11457,7 @@ fn bitcast(...@@ -11079,7 +11457,7 @@ fn bitcast(
1107911457
11080fn coerceArrayPtrToSlice(11458fn coerceArrayPtrToSlice(
11081 sema: *Sema,11459 sema: *Sema,
11082 block: *Scope.Block,11460 block: *Block,
11083 dest_type: Type,11461 dest_type: Type,
11084 inst: Air.Inst.Ref,11462 inst: Air.Inst.Ref,
11085 inst_src: LazySrcLoc,11463 inst_src: LazySrcLoc,
...@@ -11094,7 +11472,7 @@ fn coerceArrayPtrToSlice(...@@ -11094,7 +11472,7 @@ fn coerceArrayPtrToSlice(
1109411472
11095fn coerceArrayPtrToMany(11473fn coerceArrayPtrToMany(
11096 sema: *Sema,11474 sema: *Sema,
11097 block: *Scope.Block,11475 block: *Block,
11098 dest_type: Type,11476 dest_type: Type,
11099 inst: Air.Inst.Ref,11477 inst: Air.Inst.Ref,
11100 inst_src: LazySrcLoc,11478 inst_src: LazySrcLoc,
...@@ -11109,7 +11487,7 @@ fn coerceArrayPtrToMany(...@@ -11109,7 +11487,7 @@ fn coerceArrayPtrToMany(
1110911487
11110fn analyzeDeclVal(11488fn analyzeDeclVal(
11111 sema: *Sema,11489 sema: *Sema,
11112 block: *Scope.Block,11490 block: *Block,
11113 src: LazySrcLoc,11491 src: LazySrcLoc,
11114 decl: *Decl,11492 decl: *Decl,
11115) CompileError!Air.Inst.Ref {11493) CompileError!Air.Inst.Ref {
...@@ -11163,7 +11541,7 @@ fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {...@@ -11163,7 +11541,7 @@ fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {
1116311541
11164fn analyzeRef(11542fn analyzeRef(
11165 sema: *Sema,11543 sema: *Sema,
11166 block: *Scope.Block,11544 block: *Block,
11167 src: LazySrcLoc,11545 src: LazySrcLoc,
11168 operand: Air.Inst.Ref,11546 operand: Air.Inst.Ref,
11169) CompileError!Air.Inst.Ref {11547) CompileError!Air.Inst.Ref {
...@@ -11198,7 +11576,7 @@ fn analyzeRef(...@@ -11198,7 +11576,7 @@ fn analyzeRef(
1119811576
11199fn analyzeLoad(11577fn analyzeLoad(
11200 sema: *Sema,11578 sema: *Sema,
11201 block: *Scope.Block,11579 block: *Block,
11202 src: LazySrcLoc,11580 src: LazySrcLoc,
11203 ptr: Air.Inst.Ref,11581 ptr: Air.Inst.Ref,
11204 ptr_src: LazySrcLoc,11582 ptr_src: LazySrcLoc,
...@@ -11206,7 +11584,7 @@ fn analyzeLoad(...@@ -11206,7 +11584,7 @@ fn analyzeLoad(
11206 const ptr_ty = sema.typeOf(ptr);11584 const ptr_ty = sema.typeOf(ptr);
11207 const elem_ty = switch (ptr_ty.zigTypeTag()) {11585 const elem_ty = switch (ptr_ty.zigTypeTag()) {
11208 .Pointer => ptr_ty.elemType(),11586 .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}),
11210 };11588 };
11211 if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| {11589 if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| {
11212 if (try ptr_val.pointerDeref(sema.arena)) |elem_val| {11590 if (try ptr_val.pointerDeref(sema.arena)) |elem_val| {
...@@ -11220,7 +11598,7 @@ fn analyzeLoad(...@@ -11220,7 +11598,7 @@ fn analyzeLoad(
1122011598
11221fn analyzeSliceLen(11599fn analyzeSliceLen(
11222 sema: *Sema,11600 sema: *Sema,
11223 block: *Scope.Block,11601 block: *Block,
11224 src: LazySrcLoc,11602 src: LazySrcLoc,
11225 slice_inst: Air.Inst.Ref,11603 slice_inst: Air.Inst.Ref,
11226) CompileError!Air.Inst.Ref {11604) CompileError!Air.Inst.Ref {
...@@ -11228,7 +11606,7 @@ fn analyzeSliceLen(...@@ -11228,7 +11606,7 @@ fn analyzeSliceLen(
11228 if (slice_val.isUndef()) {11606 if (slice_val.isUndef()) {
11229 return sema.addConstUndef(Type.initTag(.usize));11607 return sema.addConstUndef(Type.initTag(.usize));
11230 }11608 }
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", .{});
11232 }11610 }
11233 try sema.requireRuntimeBlock(block, src);11611 try sema.requireRuntimeBlock(block, src);
11234 return block.addTyOp(.slice_len, Type.initTag(.usize), slice_inst);11612 return block.addTyOp(.slice_len, Type.initTag(.usize), slice_inst);
...@@ -11236,7 +11614,7 @@ fn analyzeSliceLen(...@@ -11236,7 +11614,7 @@ fn analyzeSliceLen(
1123611614
11237fn analyzeIsNull(11615fn analyzeIsNull(
11238 sema: *Sema,11616 sema: *Sema,
11239 block: *Scope.Block,11617 block: *Block,
11240 src: LazySrcLoc,11618 src: LazySrcLoc,
11241 operand: Air.Inst.Ref,11619 operand: Air.Inst.Ref,
11242 invert_logic: bool,11620 invert_logic: bool,
...@@ -11261,7 +11639,7 @@ fn analyzeIsNull(...@@ -11261,7 +11639,7 @@ fn analyzeIsNull(
1126111639
11262fn analyzeIsNonErr(11640fn analyzeIsNonErr(
11263 sema: *Sema,11641 sema: *Sema,
11264 block: *Scope.Block,11642 block: *Block,
11265 src: LazySrcLoc,11643 src: LazySrcLoc,
11266 operand: Air.Inst.Ref,11644 operand: Air.Inst.Ref,
11267) CompileError!Air.Inst.Ref {11645) CompileError!Air.Inst.Ref {
...@@ -11287,7 +11665,7 @@ fn analyzeIsNonErr(...@@ -11287,7 +11665,7 @@ fn analyzeIsNonErr(
1128711665
11288fn analyzeSlice(11666fn analyzeSlice(
11289 sema: *Sema,11667 sema: *Sema,
11290 block: *Scope.Block,11668 block: *Block,
11291 src: LazySrcLoc,11669 src: LazySrcLoc,
11292 array_ptr: Air.Inst.Ref,11670 array_ptr: Air.Inst.Ref,
11293 start: Air.Inst.Ref,11671 start: Air.Inst.Ref,
...@@ -11298,7 +11676,7 @@ fn analyzeSlice(...@@ -11298,7 +11676,7 @@ fn analyzeSlice(
11298 const array_ptr_ty = sema.typeOf(array_ptr);11676 const array_ptr_ty = sema.typeOf(array_ptr);
11299 const ptr_child = switch (array_ptr_ty.zigTypeTag()) {11677 const ptr_child = switch (array_ptr_ty.zigTypeTag()) {
11300 .Pointer => array_ptr_ty.elemType(),11678 .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}),
11302 };11680 };
1130311681
11304 var array_type = ptr_child;11682 var array_type = ptr_child;
...@@ -11311,11 +11689,11 @@ fn analyzeSlice(...@@ -11311,11 +11689,11 @@ fn analyzeSlice(
11311 break :blk ptr_child.elemType().elemType();11689 break :blk ptr_child.elemType().elemType();
11312 }11690 }
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", .{});
11315 }11693 }
11316 break :blk ptr_child.elemType();11694 break :blk ptr_child.elemType();
11317 },11695 },
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}),
11319 };11697 };
1132011698
11321 const slice_sentinel = if (sentinel_opt != .none) blk: {11699 const slice_sentinel = if (sentinel_opt != .none) blk: {
...@@ -11331,7 +11709,7 @@ fn analyzeSlice(...@@ -11331,7 +11709,7 @@ fn analyzeSlice(
11331 const start_u64 = start_val.toUnsignedInt();11709 const start_u64 = start_val.toUnsignedInt();
11332 const end_u64 = end_val.toUnsignedInt();11710 const end_u64 = end_val.toUnsignedInt();
11333 if (start_u64 > end_u64) {11711 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", .{});
11335 }11713 }
1133611714
11337 const len = end_u64 - start_u64;11715 const len = end_u64 - start_u64;
...@@ -11356,13 +11734,13 @@ fn analyzeSlice(...@@ -11356,13 +11734,13 @@ fn analyzeSlice(
11356 });11734 });
11357 _ = return_type;11735 _ = 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", .{});
11360}11738}
1136111739
11362/// Asserts that lhs and rhs types are both numeric.11740/// Asserts that lhs and rhs types are both numeric.
11363fn cmpNumeric(11741fn cmpNumeric(
11364 sema: *Sema,11742 sema: *Sema,
11365 block: *Scope.Block,11743 block: *Block,
11366 src: LazySrcLoc,11744 src: LazySrcLoc,
11367 lhs: Air.Inst.Ref,11745 lhs: Air.Inst.Ref,
11368 rhs: Air.Inst.Ref,11746 rhs: Air.Inst.Ref,
...@@ -11381,13 +11759,13 @@ fn cmpNumeric(...@@ -11381,13 +11759,13 @@ fn cmpNumeric(
1138111759
11382 if (lhs_ty_tag == .Vector and rhs_ty_tag == .Vector) {11760 if (lhs_ty_tag == .Vector and rhs_ty_tag == .Vector) {
11383 if (lhs_ty.arrayLen() != rhs_ty.arrayLen()) {11761 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}", .{
11385 lhs_ty.arrayLen(), rhs_ty.arrayLen(),11763 lhs_ty.arrayLen(), rhs_ty.arrayLen(),
11386 });11764 });
11387 }11765 }
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", .{});
11389 } else if (lhs_ty_tag == .Vector or rhs_ty_tag == .Vector) {11767 } 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 '{}'", .{
11391 lhs_ty, rhs_ty,11769 lhs_ty, rhs_ty,
11392 });11770 });
11393 }11771 }
...@@ -11537,7 +11915,7 @@ fn cmpNumeric(...@@ -11537,7 +11915,7 @@ fn cmpNumeric(
11537 const dest_type = if (dest_float_type) |ft| ft else blk: {11915 const dest_type = if (dest_float_type) |ft| ft else blk: {
11538 const max_bits = std.math.max(lhs_bits, rhs_bits);11916 const max_bits = std.math.max(lhs_bits, rhs_bits);
11539 const casted_bits = std.math.cast(u16, max_bits) catch |err| switch (err) {11917 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}),
11541 };11919 };
11542 const signedness: std.builtin.Signedness = if (dest_int_is_signed) .signed else .unsigned;11920 const signedness: std.builtin.Signedness = if (dest_int_is_signed) .signed else .unsigned;
11543 break :blk try Module.makeIntType(sema.arena, signedness, casted_bits);11921 break :blk try Module.makeIntType(sema.arena, signedness, casted_bits);
...@@ -11550,7 +11928,7 @@ fn cmpNumeric(...@@ -11550,7 +11928,7 @@ fn cmpNumeric(
1155011928
11551fn wrapOptional(11929fn wrapOptional(
11552 sema: *Sema,11930 sema: *Sema,
11553 block: *Scope.Block,11931 block: *Block,
11554 dest_type: Type,11932 dest_type: Type,
11555 inst: Air.Inst.Ref,11933 inst: Air.Inst.Ref,
11556 inst_src: LazySrcLoc,11934 inst_src: LazySrcLoc,
...@@ -11565,7 +11943,7 @@ fn wrapOptional(...@@ -11565,7 +11943,7 @@ fn wrapOptional(
1156511943
11566fn wrapErrorUnion(11944fn wrapErrorUnion(
11567 sema: *Sema,11945 sema: *Sema,
11568 block: *Scope.Block,11946 block: *Block,
11569 dest_type: Type,11947 dest_type: Type,
11570 inst: Air.Inst.Ref,11948 inst: Air.Inst.Ref,
11571 inst_src: LazySrcLoc,11949 inst_src: LazySrcLoc,
...@@ -11584,8 +11962,8 @@ fn wrapErrorUnion(...@@ -11584,8 +11962,8 @@ fn wrapErrorUnion(
11584 const expected_name = val.castTag(.@"error").?.data.name;11962 const expected_name = val.castTag(.@"error").?.data.name;
11585 const n = dest_err_set_ty.castTag(.error_set_single).?.data;11963 const n = dest_err_set_ty.castTag(.error_set_single).?.data;
11586 if (!mem.eql(u8, expected_name, n)) {11964 if (!mem.eql(u8, expected_name, n)) {
11587 return sema.mod.fail(11965 return sema.fail(
11588 &block.base,11966 block,
11589 inst_src,11967 inst_src,
11590 "expected type '{}', found type '{}'",11968 "expected type '{}', found type '{}'",
11591 .{ dest_err_set_ty, inst_ty },11969 .{ dest_err_set_ty, inst_ty },
...@@ -11602,8 +11980,8 @@ fn wrapErrorUnion(...@@ -11602,8 +11980,8 @@ fn wrapErrorUnion(
11602 if (mem.eql(u8, expected_name, name)) break true;11980 if (mem.eql(u8, expected_name, name)) break true;
11603 } else false;11981 } else false;
11604 if (!found) {11982 if (!found) {
11605 return sema.mod.fail(11983 return sema.fail(
11606 &block.base,11984 block,
11607 inst_src,11985 inst_src,
11608 "expected type '{}', found type '{}'",11986 "expected type '{}', found type '{}'",
11609 .{ dest_err_set_ty, inst_ty },11987 .{ dest_err_set_ty, inst_ty },
...@@ -11614,8 +11992,8 @@ fn wrapErrorUnion(...@@ -11614,8 +11992,8 @@ fn wrapErrorUnion(
11614 const expected_name = val.castTag(.@"error").?.data.name;11992 const expected_name = val.castTag(.@"error").?.data.name;
11615 const map = &dest_err_set_ty.castTag(.error_set_inferred).?.data.map;11993 const map = &dest_err_set_ty.castTag(.error_set_inferred).?.data.map;
11616 if (!map.contains(expected_name)) {11994 if (!map.contains(expected_name)) {
11617 return sema.mod.fail(11995 return sema.fail(
11618 &block.base,11996 block,
11619 inst_src,11997 inst_src,
11620 "expected type '{}', found type '{}'",11998 "expected type '{}', found type '{}'",
11621 .{ dest_err_set_ty, inst_ty },11999 .{ dest_err_set_ty, inst_ty },
...@@ -11641,7 +12019,7 @@ fn wrapErrorUnion(...@@ -11641,7 +12019,7 @@ fn wrapErrorUnion(
1164112019
11642fn unionToTag(12020fn unionToTag(
11643 sema: *Sema,12021 sema: *Sema,
11644 block: *Scope.Block,12022 block: *Block,
11645 dest_type: Type,12023 dest_type: Type,
11646 un: Air.Inst.Ref,12024 un: Air.Inst.Ref,
11647 un_src: LazySrcLoc,12025 un_src: LazySrcLoc,
...@@ -11655,7 +12033,7 @@ fn unionToTag(...@@ -11655,7 +12033,7 @@ fn unionToTag(
1165512033
11656fn resolvePeerTypes(12034fn resolvePeerTypes(
11657 sema: *Sema,12035 sema: *Sema,
11658 block: *Scope.Block,12036 block: *Block,
11659 src: LazySrcLoc,12037 src: LazySrcLoc,
11660 instructions: []Air.Inst.Ref,12038 instructions: []Air.Inst.Ref,
11661 candidate_srcs: Module.PeerTypeCandidateSrc,12039 candidate_srcs: Module.PeerTypeCandidateSrc,
...@@ -11750,18 +12128,18 @@ fn resolvePeerTypes(...@@ -11750,18 +12128,18 @@ fn resolvePeerTypes(
11750 );12128 );
1175112129
11752 const msg = msg: {12130 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 });
11754 errdefer msg.destroy(sema.gpa);12132 errdefer msg.destroy(sema.gpa);
1175512133
11756 if (chosen_src) |src_loc|12134 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
11759 if (candidate_src) |src_loc|12137 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
11762 break :msg msg;12140 break :msg msg;
11763 };12141 };
11764 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);12142 return sema.failWithOwnedErrorMsg(msg);
11765 }12143 }
1176612144
11767 return sema.typeOf(chosen);12145 return sema.typeOf(chosen);
...@@ -11769,7 +12147,7 @@ fn resolvePeerTypes(...@@ -11769,7 +12147,7 @@ fn resolvePeerTypes(
1176912147
11770pub fn resolveTypeLayout(12148pub fn resolveTypeLayout(
11771 sema: *Sema,12149 sema: *Sema,
11772 block: *Scope.Block,12150 block: *Block,
11773 src: LazySrcLoc,12151 src: LazySrcLoc,
11774 ty: Type,12152 ty: Type,
11775) CompileError!void {12153) CompileError!void {
...@@ -11780,7 +12158,7 @@ pub fn resolveTypeLayout(...@@ -11780,7 +12158,7 @@ pub fn resolveTypeLayout(
11780 switch (struct_obj.status) {12158 switch (struct_obj.status) {
11781 .none, .have_field_types => {},12159 .none, .have_field_types => {},
11782 .field_types_wip, .layout_wip => {12160 .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});
11784 },12162 },
11785 .have_layout => return,12163 .have_layout => return,
11786 }12164 }
...@@ -11794,78 +12172,23 @@ pub fn resolveTypeLayout(...@@ -11794,78 +12172,23 @@ pub fn resolveTypeLayout(
11794 }12172 }
11795}12173}
1179612174
11797pub fn resolvePendingTypes(sema: *Sema, block: *Scope.Block) !void {12175fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!Type {
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 {
11809 switch (ty.tag()) {12176 switch (ty.tag()) {
11810 .@"struct" => {12177 .@"struct" => {
11811 const struct_obj = ty.castTag(.@"struct").?.data;12178 const struct_obj = ty.castTag(.@"struct").?.data;
11812 if (struct_obj.owner_decl.namespace != sema.owner_decl.namespace) return;
11813 switch (struct_obj.status) {12179 switch (struct_obj.status) {
11814 .none => {},12180 .none => {},
11815 .field_types_wip => {12181 .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});
11817 },12183 },
11818 .have_field_types, .have_layout, .layout_wip => return,12184 .have_field_types, .have_layout, .layout_wip => return ty,
11819 }12185 }
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
11828 struct_obj.status = .field_types_wip;12187 struct_obj.status = .field_types_wip;
11829 try sema.analyzeStructFields(block, struct_obj);12188 try semaStructFields(sema.mod, struct_obj);
11830 struct_obj.status = .have_field_types;12189 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;12191 return ty;
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 }
11869 },12192 },
11870 .type_info => return sema.resolveBuiltinTypeFields(block, src, "TypeInfo"),12193 .type_info => return sema.resolveBuiltinTypeFields(block, src, "TypeInfo"),
11871 .extern_options => return sema.resolveBuiltinTypeFields(block, src, "ExternOptions"),12194 .extern_options => return sema.resolveBuiltinTypeFields(block, src, "ExternOptions"),
...@@ -11881,12 +12204,18 @@ fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type...@@ -11881,12 +12204,18 @@ fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type
11881 .@"union", .union_tagged => {12204 .@"union", .union_tagged => {
11882 const union_obj = ty.cast(Type.Payload.Union).?.data;12205 const union_obj = ty.cast(Type.Payload.Union).?.data;
11883 switch (union_obj.status) {12206 switch (union_obj.status) {
11884 .none => unreachable,12207 .none => {},
11885 .field_types_wip => {12208 .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});
11887 },12210 },
11888 .have_field_types, .have_layout, .layout_wip => return ty,12211 .have_field_types, .have_layout, .layout_wip => return ty,
11889 }12212 }
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;
11890 },12219 },
11891 else => return ty,12220 else => return ty,
11892 }12221 }
...@@ -11894,7 +12223,7 @@ fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type...@@ -11894,7 +12223,7 @@ fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type
1189412223
11895fn resolveBuiltinTypeFields(12224fn resolveBuiltinTypeFields(
11896 sema: *Sema,12225 sema: *Sema,
11897 block: *Scope.Block,12226 block: *Block,
11898 src: LazySrcLoc,12227 src: LazySrcLoc,
11899 name: []const u8,12228 name: []const u8,
11900) CompileError!Type {12229) CompileError!Type {
...@@ -11902,16 +12231,16 @@ fn resolveBuiltinTypeFields(...@@ -11902,16 +12231,16 @@ fn resolveBuiltinTypeFields(
11902 return sema.resolveTypeFields(block, src, resolved_ty);12231 return sema.resolveTypeFields(block, src, resolved_ty);
11903}12232}
1190412233
11905fn analyzeStructFields(12234fn semaStructFields(
11906 sema: *Sema,12235 mod: *Module,
11907 block: *Scope.Block,
11908 struct_obj: *Module.Struct,12236 struct_obj: *Module.Struct,
11909) CompileError!void {12237) CompileError!void {
11910 const tracy = trace(@src());12238 const tracy = trace(@src());
11911 defer tracy.end();12239 defer tracy.end();
1191212240
11913 const gpa = sema.gpa;12241 const gpa = mod.gpa;
11914 const zir = sema.code;12242 const decl = struct_obj.owner_decl;
12243 const zir = struct_obj.namespace.file_scope.zir;
11915 const extended = zir.instructions.items(.data)[struct_obj.zir_index].extended;12244 const extended = zir.instructions.items(.data)[struct_obj.zir_index].extended;
11916 assert(extended.opcode == .struct_decl);12245 assert(extended.opcode == .struct_decl);
11917 const small = @bitCast(Zir.Inst.StructDecl.Small, extended.small);12246 const small = @bitCast(Zir.Inst.StructDecl.Small, extended.small);
...@@ -11950,15 +12279,51 @@ fn analyzeStructFields(...@@ -11950,15 +12279,51 @@ fn analyzeStructFields(
11950 }12279 }
11951 extra_index += body.len;12280 extra_index += body.len;
1195212281
11953 var decl_arena = struct_obj.owner_decl.value_arena.?.promote(gpa);12282 var decl_arena = decl.value_arena.?.promote(gpa);
11954 defer struct_obj.owner_decl.value_arena.?.* = decl_arena.state;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
11958 if (body.len != 0) {12319 if (body.len != 0) {
11959 _ = try sema.analyzeBody(block, body);12320 _ = try sema.analyzeBody(&block_scope, body);
11960 }12321 }
1196112322
12323 try wip_captures.finalize();
12324
12325 try struct_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
12326
11962 const bits_per_field = 4;12327 const bits_per_field = 4;
11963 const fields_per_u32 = 32 / bits_per_field;12328 const fields_per_u32 = 32 / bits_per_field;
11964 const bit_bags_count = std.math.divCeil(usize, fields_len, fields_per_u32) catch unreachable;12329 const bit_bags_count = std.math.divCeil(usize, fields_len, fields_per_u32) catch unreachable;
...@@ -11995,7 +12360,7 @@ fn analyzeStructFields(...@@ -11995,7 +12360,7 @@ fn analyzeStructFields(
11995 // TODO: if we need to report an error here, use a source location12360 // TODO: if we need to report an error here, use a source location
11996 // that points to this type expression rather than the struct.12361 // that points to this type expression rather than the struct.
11997 // But only resolve the source location if we need to emit a compile error.12362 // 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
12000 const gop = struct_obj.fields.getOrPutAssumeCapacity(field_name);12365 const gop = struct_obj.fields.getOrPutAssumeCapacity(field_name);
12001 assert(!gop.found_existing);12366 assert(!gop.found_existing);
...@@ -12013,7 +12378,7 @@ fn analyzeStructFields(...@@ -12013,7 +12378,7 @@ fn analyzeStructFields(
12013 // TODO: if we need to report an error here, use a source location12378 // TODO: if we need to report an error here, use a source location
12014 // that points to this alignment expression rather than the struct.12379 // that points to this alignment expression rather than the struct.
12015 // But only resolve the source location if we need to emit a compile error.12380 // 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;
12017 gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator);12382 gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator);
12018 }12383 }
12019 if (has_default) {12384 if (has_default) {
...@@ -12023,23 +12388,23 @@ fn analyzeStructFields(...@@ -12023,23 +12388,23 @@ fn analyzeStructFields(
12023 // TODO: if we need to report an error here, use a source location12388 // TODO: if we need to report an error here, use a source location
12024 // that points to this default value expression rather than the struct.12389 // that points to this default value expression rather than the struct.
12025 // But only resolve the source location if we need to emit a compile error.12390 // 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)) orelse12391 const default_val = (try sema.resolveMaybeUndefVal(&block_scope, src, default_inst)) orelse
12027 return sema.failWithNeededComptime(block, src);12392 return sema.failWithNeededComptime(&block_scope, src);
12028 gop.value_ptr.default_val = try default_val.copy(&decl_arena.allocator);12393 gop.value_ptr.default_val = try default_val.copy(&decl_arena.allocator);
12029 }12394 }
12030 }12395 }
12031}12396}
1203212397
12033fn analyzeUnionFields(12398fn semaUnionFields(
12034 sema: *Sema,12399 mod: *Module,
12035 block: *Scope.Block,
12036 union_obj: *Module.Union,12400 union_obj: *Module.Union,
12037) CompileError!void {12401) CompileError!void {
12038 const tracy = trace(@src());12402 const tracy = trace(@src());
12039 defer tracy.end();12403 defer tracy.end();
1204012404
12041 const gpa = sema.gpa;12405 const gpa = mod.gpa;
12042 const zir = sema.code;12406 const decl = union_obj.owner_decl;
12407 const zir = union_obj.namespace.file_scope.zir;
12043 const extended = zir.instructions.items(.data)[union_obj.zir_index].extended;12408 const extended = zir.instructions.items(.data)[union_obj.zir_index].extended;
12044 assert(extended.opcode == .union_decl);12409 assert(extended.opcode == .union_decl);
12045 const small = @bitCast(Zir.Inst.UnionDecl.Small, extended.small);12410 const small = @bitCast(Zir.Inst.UnionDecl.Small, extended.small);
...@@ -12087,20 +12452,57 @@ fn analyzeUnionFields(...@@ -12087,20 +12452,57 @@ fn analyzeUnionFields(
12087 var decl_arena = union_obj.owner_decl.value_arena.?.promote(gpa);12452 var decl_arena = union_obj.owner_decl.value_arena.?.promote(gpa);
12088 defer union_obj.owner_decl.value_arena.?.* = decl_arena.state;12453 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
12092 if (body.len != 0) {12489 if (body.len != 0) {
12093 _ = try sema.analyzeBody(block, body);12490 _ = try sema.analyzeBody(&block_scope, body);
12094 }12491 }
12492
12493 try wip_captures.finalize();
12494
12495 try union_obj.fields.ensureTotalCapacity(&decl_arena.allocator, fields_len);
12496
12095 var int_tag_ty: Type = undefined;12497 var int_tag_ty: Type = undefined;
12096 var enum_field_names: ?*Module.EnumNumbered.NameMap = null;12498 var enum_field_names: ?*Module.EnumNumbered.NameMap = null;
12097 var enum_value_map: ?*Module.EnumNumbered.ValueMap = null;12499 var enum_value_map: ?*Module.EnumNumbered.ValueMap = null;
12098 if (tag_type_ref != .none) {12500 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);
12100 if (small.auto_enum_tag) {12502 if (small.auto_enum_tag) {
12101 // The provided type is an integer type and we must construct the enum tag type here.12503 // The provided type is an integer type and we must construct the enum tag type here.
12102 int_tag_ty = provided_ty;12504 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);
12104 enum_field_names = &union_obj.tag_ty.castTag(.enum_numbered).?.data.fields;12506 enum_field_names = &union_obj.tag_ty.castTag(.enum_numbered).?.data.fields;
12105 enum_value_map = &union_obj.tag_ty.castTag(.enum_numbered).?.data.values;12507 enum_value_map = &union_obj.tag_ty.castTag(.enum_numbered).?.data.values;
12106 } else {12508 } else {
...@@ -12111,7 +12513,7 @@ fn analyzeUnionFields(...@@ -12111,7 +12513,7 @@ fn analyzeUnionFields(
12111 // If auto_enum_tag is false, this is an untagged union. However, for semantic analysis12513 // If auto_enum_tag is false, this is an untagged union. However, for semantic analysis
12112 // purposes, we still auto-generate an enum tag type the same way. That the union is12514 // purposes, we still auto-generate an enum tag type the same way. That the union is
12113 // untagged is represented by the Type tag (union vs union_tagged).12515 // 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);
12115 enum_field_names = &union_obj.tag_ty.castTag(.enum_simple).?.data.fields;12517 enum_field_names = &union_obj.tag_ty.castTag(.enum_simple).?.data.fields;
12116 }12518 }
1211712519
...@@ -12160,8 +12562,8 @@ fn analyzeUnionFields(...@@ -12160,8 +12562,8 @@ fn analyzeUnionFields(
1216012562
12161 if (enum_value_map) |map| {12563 if (enum_value_map) |map| {
12162 const tag_src = src; // TODO better source location12564 const tag_src = src; // TODO better source location
12163 const coerced = try sema.coerce(block, int_tag_ty, tag_ref, tag_src);12565 const coerced = try sema.coerce(&block_scope, int_tag_ty, tag_ref, tag_src);
12164 const val = try sema.resolveConstValue(block, tag_src, coerced);12566 const val = try sema.resolveConstValue(&block_scope, tag_src, coerced);
12165 map.putAssumeCapacityContext(val, {}, .{ .ty = int_tag_ty });12567 map.putAssumeCapacityContext(val, {}, .{ .ty = int_tag_ty });
12166 }12568 }
1216712569
...@@ -12177,7 +12579,7 @@ fn analyzeUnionFields(...@@ -12177,7 +12579,7 @@ fn analyzeUnionFields(
12177 // TODO: if we need to report an error here, use a source location12579 // TODO: if we need to report an error here, use a source location
12178 // that points to this type expression rather than the union.12580 // that points to this type expression rather than the union.
12179 // But only resolve the source location if we need to emit a compile error.12581 // 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
12182 const gop = union_obj.fields.getOrPutAssumeCapacity(field_name);12584 const gop = union_obj.fields.getOrPutAssumeCapacity(field_name);
12183 assert(!gop.found_existing);12585 assert(!gop.found_existing);
...@@ -12190,7 +12592,7 @@ fn analyzeUnionFields(...@@ -12190,7 +12592,7 @@ fn analyzeUnionFields(
12190 // TODO: if we need to report an error here, use a source location12592 // TODO: if we need to report an error here, use a source location
12191 // that points to this alignment expression rather than the struct.12593 // that points to this alignment expression rather than the struct.
12192 // But only resolve the source location if we need to emit a compile error.12594 // 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;
12194 gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator);12596 gop.value_ptr.abi_align = try abi_align_val.copy(&decl_arena.allocator);
12195 } else {12597 } else {
12196 gop.value_ptr.abi_align = Value.initTag(.abi_align_default);12598 gop.value_ptr.abi_align = Value.initTag(.abi_align_default);
...@@ -12200,7 +12602,7 @@ fn analyzeUnionFields(...@@ -12200,7 +12602,7 @@ fn analyzeUnionFields(
1220012602
12201fn generateUnionTagTypeNumbered(12603fn generateUnionTagTypeNumbered(
12202 sema: *Sema,12604 sema: *Sema,
12203 block: *Scope.Block,12605 block: *Block,
12204 fields_len: u32,12606 fields_len: u32,
12205 int_ty: Type,12607 int_ty: Type,
12206) !Type {12608) !Type {
...@@ -12218,12 +12620,12 @@ fn generateUnionTagTypeNumbered(...@@ -12218,12 +12620,12 @@ fn generateUnionTagTypeNumbered(
12218 const enum_ty = Type.initPayload(&enum_ty_payload.base);12620 const enum_ty = Type.initPayload(&enum_ty_payload.base);
12219 const enum_val = try Value.Tag.ty.create(&new_decl_arena.allocator, enum_ty);12621 const enum_val = try Value.Tag.ty.create(&new_decl_arena.allocator, enum_ty);
12220 // TODO better type name12622 // TODO better type name
12221 const new_decl = try mod.createAnonymousDecl(&block.base, .{12623 const new_decl = try mod.createAnonymousDecl(block, .{
12222 .ty = Type.initTag(.type),12624 .ty = Type.initTag(.type),
12223 .val = enum_val,12625 .val = enum_val,
12224 });12626 });
12225 new_decl.owns_tv = true;12627 new_decl.owns_tv = true;
12226 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);12628 errdefer mod.abortAnonDecl(new_decl);
1222712629
12228 enum_obj.* = .{12630 enum_obj.* = .{
12229 .owner_decl = new_decl,12631 .owner_decl = new_decl,
...@@ -12239,7 +12641,7 @@ fn generateUnionTagTypeNumbered(...@@ -12239,7 +12641,7 @@ fn generateUnionTagTypeNumbered(
12239 return enum_ty;12641 return enum_ty;
12240}12642}
1224112643
12242fn generateUnionTagTypeSimple(sema: *Sema, block: *Scope.Block, fields_len: u32) !Type {12644fn generateUnionTagTypeSimple(sema: *Sema, block: *Block, fields_len: u32) !Type {
12243 const mod = sema.mod;12645 const mod = sema.mod;
1224412646
12245 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);12647 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);
...@@ -12254,12 +12656,12 @@ fn generateUnionTagTypeSimple(sema: *Sema, block: *Scope.Block, fields_len: u32)...@@ -12254,12 +12656,12 @@ fn generateUnionTagTypeSimple(sema: *Sema, block: *Scope.Block, fields_len: u32)
12254 const enum_ty = Type.initPayload(&enum_ty_payload.base);12656 const enum_ty = Type.initPayload(&enum_ty_payload.base);
12255 const enum_val = try Value.Tag.ty.create(&new_decl_arena.allocator, enum_ty);12657 const enum_val = try Value.Tag.ty.create(&new_decl_arena.allocator, enum_ty);
12256 // TODO better type name12658 // TODO better type name
12257 const new_decl = try mod.createAnonymousDecl(&block.base, .{12659 const new_decl = try mod.createAnonymousDecl(block, .{
12258 .ty = Type.initTag(.type),12660 .ty = Type.initTag(.type),
12259 .val = enum_val,12661 .val = enum_val,
12260 });12662 });
12261 new_decl.owns_tv = true;12663 new_decl.owns_tv = true;
12262 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);12664 errdefer mod.abortAnonDecl(new_decl);
1226312665
12264 enum_obj.* = .{12666 enum_obj.* = .{
12265 .owner_decl = new_decl,12667 .owner_decl = new_decl,
...@@ -12274,7 +12676,7 @@ fn generateUnionTagTypeSimple(sema: *Sema, block: *Scope.Block, fields_len: u32)...@@ -12274,7 +12676,7 @@ fn generateUnionTagTypeSimple(sema: *Sema, block: *Scope.Block, fields_len: u32)
1227412676
12275fn getBuiltin(12677fn getBuiltin(
12276 sema: *Sema,12678 sema: *Sema,
12277 block: *Scope.Block,12679 block: *Block,
12278 src: LazySrcLoc,12680 src: LazySrcLoc,
12279 name: []const u8,12681 name: []const u8,
12280) CompileError!Air.Inst.Ref {12682) CompileError!Air.Inst.Ref {
...@@ -12284,7 +12686,7 @@ fn getBuiltin(...@@ -12284,7 +12686,7 @@ fn getBuiltin(
12284 const opt_builtin_inst = try sema.namespaceLookupRef(12686 const opt_builtin_inst = try sema.namespaceLookupRef(
12285 block,12687 block,
12286 src,12688 src,
12287 std_file.root_decl.?.namespace,12689 std_file.root_decl.?.src_namespace,
12288 "builtin",12690 "builtin",
12289 );12691 );
12290 const builtin_inst = try sema.analyzeLoad(block, src, opt_builtin_inst.?, src);12692 const builtin_inst = try sema.analyzeLoad(block, src, opt_builtin_inst.?, src);
...@@ -12300,7 +12702,7 @@ fn getBuiltin(...@@ -12300,7 +12702,7 @@ fn getBuiltin(
1230012702
12301fn getBuiltinType(12703fn getBuiltinType(
12302 sema: *Sema,12704 sema: *Sema,
12303 block: *Scope.Block,12705 block: *Block,
12304 src: LazySrcLoc,12706 src: LazySrcLoc,
12305 name: []const u8,12707 name: []const u8,
12306) CompileError!Type {12708) CompileError!Type {
...@@ -12314,7 +12716,7 @@ fn getBuiltinType(...@@ -12314,7 +12716,7 @@ fn getBuiltinType(
12314/// that the types are already resolved.12716/// that the types are already resolved.
12315fn typeHasOnePossibleValue(12717fn typeHasOnePossibleValue(
12316 sema: *Sema,12718 sema: *Sema,
12317 block: *Scope.Block,12719 block: *Block,
12318 src: LazySrcLoc,12720 src: LazySrcLoc,
12319 starting_type: Type,12721 starting_type: Type,
12320) CompileError!?Value {12722) CompileError!?Value {
...@@ -12477,8 +12879,8 @@ fn typeHasOnePossibleValue(...@@ -12477,8 +12879,8 @@ fn typeHasOnePossibleValue(
12477 };12879 };
12478}12880}
1247912881
12480fn getAstTree(sema: *Sema, block: *Scope.Block) CompileError!*const std.zig.Ast {12882fn getAstTree(sema: *Sema, block: *Block) CompileError!*const std.zig.Ast {
12481 return block.src_decl.namespace.file_scope.getTree(sema.gpa) catch |err| {12883 return block.namespace.file_scope.getTree(sema.gpa) catch |err| {
12482 log.err("unable to load AST to report compile error: {s}", .{@errorName(err)});12884 log.err("unable to load AST to report compile error: {s}", .{@errorName(err)});
12483 return error.AnalysisFail;12885 return error.AnalysisFail;
12484 };12886 };
...@@ -12667,7 +13069,7 @@ fn getBreakBlock(sema: *Sema, inst_index: Air.Inst.Index) ?Air.Inst.Index {...@@ -12667,7 +13069,7 @@ fn getBreakBlock(sema: *Sema, inst_index: Air.Inst.Index) ?Air.Inst.Index {
1266713069
12668fn isComptimeKnown(13070fn isComptimeKnown(
12669 sema: *Sema,13071 sema: *Sema,
12670 block: *Scope.Block,13072 block: *Block,
12671 src: LazySrcLoc,13073 src: LazySrcLoc,
12672 inst: Air.Inst.Ref,13074 inst: Air.Inst.Ref,
12673) !bool {13075) !bool {
...@@ -12676,7 +13078,7 @@ fn isComptimeKnown(...@@ -12676,7 +13078,7 @@ fn isComptimeKnown(
1267613078
12677fn analyzeComptimeAlloc(13079fn analyzeComptimeAlloc(
12678 sema: *Sema,13080 sema: *Sema,
12679 block: *Scope.Block,13081 block: *Block,
12680 var_type: Type,13082 var_type: Type,
12681) CompileError!Air.Inst.Ref {13083) CompileError!Air.Inst.Ref {
12682 const ptr_type = try Type.ptr(sema.arena, .{13084 const ptr_type = try Type.ptr(sema.arena, .{
...@@ -12719,7 +13121,7 @@ pub const AddressSpaceContext = enum {...@@ -12719,7 +13121,7 @@ pub const AddressSpaceContext = enum {
1271913121
12720pub fn analyzeAddrspace(13122pub fn analyzeAddrspace(
12721 sema: *Sema,13123 sema: *Sema,
12722 block: *Scope.Block,13124 block: *Block,
12723 src: LazySrcLoc,13125 src: LazySrcLoc,
12724 zir_ref: Zir.Inst.Ref,13126 zir_ref: Zir.Inst.Ref,
12725 ctx: AddressSpaceContext,13127 ctx: AddressSpaceContext,
...@@ -12743,8 +13145,8 @@ pub fn analyzeAddrspace(...@@ -12743,8 +13145,8 @@ pub fn analyzeAddrspace(
12743 .pointer => "pointers",13145 .pointer => "pointers",
12744 };13146 };
1274513147
12746 return sema.mod.fail(13148 return sema.fail(
12747 &block.base,13149 block,
12748 src,13150 src,
12749 "{s} with address space '{s}' are not supported on {s}",13151 "{s} with address space '{s}' are not supported on {s}",
12750 .{ entity, @tagName(address_space), arch.genericName() },13152 .{ entity, @tagName(address_space), arch.genericName() },
src/codegen.zig+2-2
...@@ -2584,7 +2584,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -2584,7 +2584,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
25842584
2585 fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue) !void {2585 fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue) !void {
2586 const ty_str = self.air.instructions.items(.data)[inst].ty_str;2586 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;
2588 const name = zir.nullTerminatedString(ty_str.str);2588 const name = zir.nullTerminatedString(ty_str.str);
2589 const name_with_null = name.ptr[0 .. name.len + 1];2589 const name_with_null = name.ptr[0 .. name.len + 1];
2590 const ty = self.air.getRefType(ty_str.ty);2590 const ty = self.air.getRefType(ty_str.ty);
...@@ -3834,7 +3834,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -3834,7 +3834,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
3834 fn airAsm(self: *Self, inst: Air.Inst.Index) !void {3834 fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
3835 const air_datas = self.air.instructions.items(.data);3835 const air_datas = self.air.instructions.items(.data);
3836 const air_extra = self.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload);3836 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;
3838 const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;3838 const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;
3839 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);3839 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
3840 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);3840 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);
src/codegen/c.zig+2-2
...@@ -246,7 +246,7 @@ pub const DeclGen = struct {...@@ -246,7 +246,7 @@ pub const DeclGen = struct {
246 fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } {246 fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } {
247 @setCold(true);247 @setCold(true);
248 const src: LazySrcLoc = .{ .node_offset = 0 };248 const src: LazySrcLoc = .{ .node_offset = 0 };
249 const src_loc = src.toSrcLocWithDecl(dg.decl);249 const src_loc = src.toSrcLoc(dg.decl);
250 dg.error_msg = try Module.ErrorMsg.create(dg.module.gpa, src_loc, format, args);250 dg.error_msg = try Module.ErrorMsg.create(dg.module.gpa, src_loc, format, args);
251 return error.AnalysisFail;251 return error.AnalysisFail;
252 }252 }
...@@ -1696,7 +1696,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -1696,7 +1696,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
1696fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {1696fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
1697 const air_datas = f.air.instructions.items(.data);1697 const air_datas = f.air.instructions.items(.data);
1698 const air_extra = f.air.extraData(Air.Asm, air_datas[inst].ty_pl.payload);1698 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;
1700 const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;1700 const extended = zir.instructions.items(.data)[air_extra.data.zir_index].extended;
1701 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);1701 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
1702 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);1702 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);
src/codegen/llvm.zig+2-2
...@@ -557,7 +557,7 @@ pub const DeclGen = struct {...@@ -557,7 +557,7 @@ pub const DeclGen = struct {
557 fn todo(self: *DeclGen, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {557 fn todo(self: *DeclGen, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {
558 @setCold(true);558 @setCold(true);
559 assert(self.err_msg == null);559 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);
561 self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, "TODO (LLVM): " ++ format, args);561 self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, "TODO (LLVM): " ++ format, args);
562 return error.CodegenFail;562 return error.CodegenFail;
563 }563 }
...@@ -1819,7 +1819,7 @@ pub const FuncGen = struct {...@@ -1819,7 +1819,7 @@ pub const FuncGen = struct {
18191819
1820 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;1820 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
1821 const air_asm = self.air.extraData(Air.Asm, ty_pl.payload);1821 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;
1823 const extended = zir.instructions.items(.data)[air_asm.data.zir_index].extended;1823 const extended = zir.instructions.items(.data)[air_asm.data.zir_index].extended;
1824 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);1824 const zir_extra = zir.extraData(Zir.Inst.Asm, extended.operand);
1825 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);1825 const asm_source = zir.nullTerminatedString(zir_extra.data.asm_source);
src/codegen/spirv.zig+2-2
...@@ -139,7 +139,7 @@ pub const SPIRVModule = struct {...@@ -139,7 +139,7 @@ pub const SPIRVModule = struct {
139 }139 }
140140
141 fn resolveSourceFileName(self: *SPIRVModule, decl: *Decl) !ResultId {141 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;
143 const result = try self.file_names.getOrPut(path);143 const result = try self.file_names.getOrPut(path);
144 if (!result.found_existing) {144 if (!result.found_existing) {
145 result.value_ptr.* = self.allocResultId();145 result.value_ptr.* = self.allocResultId();
...@@ -295,7 +295,7 @@ pub const DeclGen = struct {...@@ -295,7 +295,7 @@ pub const DeclGen = struct {
295 fn fail(self: *DeclGen, comptime format: []const u8, args: anytype) Error {295 fn fail(self: *DeclGen, comptime format: []const u8, args: anytype) Error {
296 @setCold(true);296 @setCold(true);
297 const src: LazySrcLoc = .{ .node_offset = 0 };297 const src: LazySrcLoc = .{ .node_offset = 0 };
298 const src_loc = src.toSrcLocWithDecl(self.decl);298 const src_loc = src.toSrcLoc(self.decl);
299 self.error_msg = try Module.ErrorMsg.create(self.spv.module.gpa, src_loc, format, args);299 self.error_msg = try Module.ErrorMsg.create(self.spv.module.gpa, src_loc, format, args);
300 return error.AnalysisFail;300 return error.AnalysisFail;
301 }301 }
src/codegen/wasm.zig+1-1
...@@ -540,7 +540,7 @@ pub const Context = struct {...@@ -540,7 +540,7 @@ pub const Context = struct {
540 /// Sets `err_msg` on `Context` and returns `error.CodegemFail` which is caught in link/Wasm.zig540 /// Sets `err_msg` on `Context` and returns `error.CodegemFail` which is caught in link/Wasm.zig
541 fn fail(self: *Context, comptime fmt: []const u8, args: anytype) InnerError {541 fn fail(self: *Context, comptime fmt: []const u8, args: anytype) InnerError {
542 const src: LazySrcLoc = .{ .node_offset = 0 };542 const src: LazySrcLoc = .{ .node_offset = 0 };
543 const src_loc = src.toSrcLocWithDecl(self.decl);543 const src_loc = src.toSrcLoc(self.decl);
544 self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, fmt, args);544 self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, fmt, args);
545 return error.CodegenFail;545 return error.CodegenFail;
546 }546 }
src/crash_report.zig+9-11
...@@ -8,6 +8,7 @@ const print_zir = @import("print_zir.zig");...@@ -8,6 +8,7 @@ const print_zir = @import("print_zir.zig");
8const Module = @import("Module.zig");8const Module = @import("Module.zig");
9const Sema = @import("Sema.zig");9const Sema = @import("Sema.zig");
10const Zir = @import("Zir.zig");10const Zir = @import("Zir.zig");
11const Decl = Module.Decl;
1112
12pub const is_enabled = builtin.mode == .Debug;13pub const is_enabled = builtin.mode == .Debug;
1314
...@@ -36,7 +37,7 @@ fn en(val: anytype) En(@TypeOf(val)) {...@@ -36,7 +37,7 @@ fn en(val: anytype) En(@TypeOf(val)) {
36pub const AnalyzeBody = struct {37pub const AnalyzeBody = struct {
37 parent: if (is_enabled) ?*AnalyzeBody else void,38 parent: if (is_enabled) ?*AnalyzeBody else void,
38 sema: En(*Sema),39 sema: En(*Sema),
39 block: En(*Module.Scope.Block),40 block: En(*Sema.Block),
40 body: En([]const Zir.Inst.Index),41 body: En([]const Zir.Inst.Index),
41 body_index: En(usize),42 body_index: En(usize),
4243
...@@ -64,7 +65,7 @@ pub const AnalyzeBody = struct {...@@ -64,7 +65,7 @@ pub const AnalyzeBody = struct {
6465
65threadlocal var zir_state: ?*AnalyzeBody = if (is_enabled) null else @compileError("Cannot use zir_state if crash_report is disabled.");66threadlocal 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 {
68 if (is_enabled) {69 if (is_enabled) {
69 return .{70 return .{
70 .parent = null,71 .parent = null,
...@@ -87,7 +88,7 @@ fn dumpStatusReport() !void {...@@ -87,7 +88,7 @@ fn dumpStatusReport() !void {
87 const allocator = &fba.allocator;88 const allocator = &fba.allocator;
8889
89 const stderr = io.getStdErr().writer();90 const stderr = io.getStdErr().writer();
90 const block: *Scope.Block = anal.block;91 const block: *Sema.Block = anal.block;
9192
92 try stderr.writeAll("Analyzing ");93 try stderr.writeAll("Analyzing ");
93 try writeFullyQualifiedDeclWithFile(block.src_decl, stderr);94 try writeFullyQualifiedDeclWithFile(block.src_decl, stderr);
...@@ -97,7 +98,7 @@ fn dumpStatusReport() !void {...@@ -97,7 +98,7 @@ fn dumpStatusReport() !void {
97 allocator,98 allocator,
98 anal.body,99 anal.body,
99 anal.body_index,100 anal.body_index,
100 block.src_decl.getFileScope(),101 block.namespace.file_scope,
101 block.src_decl.src_node,102 block.src_decl.src_node,
102 6, // indent103 6, // indent
103 stderr,104 stderr,
...@@ -106,7 +107,7 @@ fn dumpStatusReport() !void {...@@ -106,7 +107,7 @@ fn dumpStatusReport() !void {
106 else => |e| return e,107 else => |e| return e,
107 };108 };
108 try stderr.writeAll(" For full context, use the command\n zig ast-check -t ");109 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);
110 try stderr.writeAll("\n\n");111 try stderr.writeAll("\n\n");
111112
112 var parent = anal.parent;113 var parent = anal.parent;
...@@ -118,7 +119,7 @@ fn dumpStatusReport() !void {...@@ -118,7 +119,7 @@ fn dumpStatusReport() !void {
118 print_zir.renderSingleInstruction(119 print_zir.renderSingleInstruction(
119 allocator,120 allocator,
120 curr.body[curr.body_index],121 curr.body[curr.body_index],
121 curr.block.src_decl.getFileScope(),122 curr.block.namespace.file_scope,
122 curr.block.src_decl.src_node,123 curr.block.src_decl.src_node,
123 6, // indent124 6, // indent
124 stderr,125 stderr,
...@@ -134,12 +135,9 @@ fn dumpStatusReport() !void {...@@ -134,12 +135,9 @@ fn dumpStatusReport() !void {
134 try stderr.writeAll("\n");135 try stderr.writeAll("\n");
135}136}
136137
137const Scope = Module.Scope;
138const Decl = Module.Decl;
139
140var crash_heap: [16 * 4096]u8 = undefined;138var crash_heap: [16 * 4096]u8 = undefined;
141139
142fn writeFilePath(file: *Scope.File, stream: anytype) !void {140fn writeFilePath(file: *Module.File, stream: anytype) !void {
143 if (file.pkg.root_src_directory.path) |path| {141 if (file.pkg.root_src_directory.path) |path| {
144 try stream.writeAll(path);142 try stream.writeAll(path);
145 try stream.writeAll(std.fs.path.sep_str);143 try stream.writeAll(std.fs.path.sep_str);
...@@ -150,7 +148,7 @@ fn writeFilePath(file: *Scope.File, stream: anytype) !void {...@@ -150,7 +148,7 @@ fn writeFilePath(file: *Scope.File, stream: anytype) !void {
150fn writeFullyQualifiedDeclWithFile(decl: *Decl, stream: anytype) !void {148fn writeFullyQualifiedDeclWithFile(decl: *Decl, stream: anytype) !void {
151 try writeFilePath(decl.getFileScope(), stream);149 try writeFilePath(decl.getFileScope(), stream);
152 try stream.writeAll(": ");150 try stream.writeAll(": ");
153 try decl.namespace.renderFullyQualifiedName(std.mem.sliceTo(decl.name, 0), stream);151 try decl.renderFullyQualifiedDebugName(stream);
154}152}
155153
156fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn {154fn 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,...@@ -57,7 +57,7 @@ path_arena: std.heap.ArenaAllocator,
57/// of the function to know what file it came from.57/// of the function to know what file it came from.
58/// If we group the decls by file, it makes it really easy to do this (put the symbol in the correct place)58/// If we group the decls by file, it makes it really easy to do this (put the symbol in the correct place)
59fn_decl_table: std.AutoArrayHashMapUnmanaged(59fn_decl_table: std.AutoArrayHashMapUnmanaged(
60 *Module.Scope.File,60 *Module.File,
61 struct { sym_index: u32, functions: std.AutoArrayHashMapUnmanaged(*Module.Decl, FnDeclOutput) = .{} },61 struct { sym_index: u32, functions: std.AutoArrayHashMapUnmanaged(*Module.Decl, FnDeclOutput) = .{} },
62) = .{},62) = .{},
63data_decl_table: std.AutoArrayHashMapUnmanaged(*Module.Decl, []const u8) = .{},63data_decl_table: std.AutoArrayHashMapUnmanaged(*Module.Decl, []const u8) = .{},
...@@ -163,11 +163,11 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Plan9 {...@@ -163,11 +163,11 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Plan9 {
163163
164fn putFn(self: *Plan9, decl: *Module.Decl, out: FnDeclOutput) !void {164fn putFn(self: *Plan9, decl: *Module.Decl, out: FnDeclOutput) !void {
165 const gpa = self.base.allocator;165 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());
167 if (fn_map_res.found_existing) {167 if (fn_map_res.found_existing) {
168 try fn_map_res.value_ptr.functions.put(gpa, decl, out);168 try fn_map_res.value_ptr.functions.put(gpa, decl, out);
169 } else {169 } else {
170 const file = decl.namespace.file_scope;170 const file = decl.getFileScope();
171 const arena = &self.path_arena.allocator;171 const arena = &self.path_arena.allocator;
172 // each file gets a symbol172 // each file gets a symbol
173 fn_map_res.value_ptr.* = .{173 fn_map_res.value_ptr.* = .{
...@@ -548,7 +548,7 @@ pub fn freeDecl(self: *Plan9, decl: *Module.Decl) void {...@@ -548,7 +548,7 @@ pub fn freeDecl(self: *Plan9, decl: *Module.Decl) void {
548 const is_fn = (decl.val.tag() == .function);548 const is_fn = (decl.val.tag() == .function);
549 if (is_fn) {549 if (is_fn) {
550 var symidx_and_submap =550 var symidx_and_submap =
551 self.fn_decl_table.get(decl.namespace.file_scope).?;551 self.fn_decl_table.get(decl.getFileScope()).?;
552 var submap = symidx_and_submap.functions;552 var submap = symidx_and_submap.functions;
553 _ = submap.swapRemove(decl);553 _ = submap.swapRemove(decl);
554 if (submap.count() == 0) {554 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...@@ -3233,7 +3233,7 @@ pub fn cmdFmt(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !voi
3233 const Module = @import("Module.zig");3233 const Module = @import("Module.zig");
3234 const AstGen = @import("AstGen.zig");3234 const AstGen = @import("AstGen.zig");
32353235
3236 var file: Module.Scope.File = .{3236 var file: Module.File = .{
3237 .status = .never_loaded,3237 .status = .never_loaded,
3238 .source_loaded = true,3238 .source_loaded = true,
3239 .zir_loaded = false,3239 .zir_loaded = false,
...@@ -3429,7 +3429,7 @@ fn fmtPathFile(...@@ -3429,7 +3429,7 @@ fn fmtPathFile(
3429 const Module = @import("Module.zig");3429 const Module = @import("Module.zig");
3430 const AstGen = @import("AstGen.zig");3430 const AstGen = @import("AstGen.zig");
34313431
3432 var file: Module.Scope.File = .{3432 var file: Module.File = .{
3433 .status = .never_loaded,3433 .status = .never_loaded,
3434 .source_loaded = true,3434 .source_loaded = true,
3435 .zir_loaded = false,3435 .zir_loaded = false,
...@@ -4019,7 +4019,7 @@ pub fn cmdAstCheck(...@@ -4019,7 +4019,7 @@ pub fn cmdAstCheck(
4019 }4019 }
4020 }4020 }
40214021
4022 var file: Module.Scope.File = .{4022 var file: Module.File = .{
4023 .status = .never_loaded,4023 .status = .never_loaded,
4024 .source_loaded = false,4024 .source_loaded = false,
4025 .tree_loaded = false,4025 .tree_loaded = false,
...@@ -4170,7 +4170,7 @@ pub fn cmdChangelist(...@@ -4170,7 +4170,7 @@ pub fn cmdChangelist(
4170 if (stat.size > max_src_size)4170 if (stat.size > max_src_size)
4171 return error.FileTooBig;4171 return error.FileTooBig;
41724172
4173 var file: Module.Scope.File = .{4173 var file: Module.File = .{
4174 .status = .never_loaded,4174 .status = .never_loaded,
4175 .source_loaded = false,4175 .source_loaded = false,
4176 .tree_loaded = false,4176 .tree_loaded = false,
src/print_zir.zig+4-4
...@@ -11,7 +11,7 @@ const LazySrcLoc = Module.LazySrcLoc;...@@ -11,7 +11,7 @@ const LazySrcLoc = Module.LazySrcLoc;
11/// Write human-readable, debug formatted ZIR code to a file.11/// Write human-readable, debug formatted ZIR code to a file.
12pub fn renderAsTextToFile(12pub fn renderAsTextToFile(
13 gpa: *Allocator,13 gpa: *Allocator,
14 scope_file: *Module.Scope.File,14 scope_file: *Module.File,
15 fs_file: std.fs.File,15 fs_file: std.fs.File,
16) !void {16) !void {
17 var arena = std.heap.ArenaAllocator.init(gpa);17 var arena = std.heap.ArenaAllocator.init(gpa);
...@@ -64,7 +64,7 @@ pub fn renderInstructionContext(...@@ -64,7 +64,7 @@ pub fn renderInstructionContext(
64 gpa: *Allocator,64 gpa: *Allocator,
65 block: []const Zir.Inst.Index,65 block: []const Zir.Inst.Index,
66 block_index: usize,66 block_index: usize,
67 scope_file: *Module.Scope.File,67 scope_file: *Module.File,
68 parent_decl_node: Ast.Node.Index,68 parent_decl_node: Ast.Node.Index,
69 indent: u32,69 indent: u32,
70 stream: anytype,70 stream: anytype,
...@@ -96,7 +96,7 @@ pub fn renderInstructionContext(...@@ -96,7 +96,7 @@ pub fn renderInstructionContext(
96pub fn renderSingleInstruction(96pub fn renderSingleInstruction(
97 gpa: *Allocator,97 gpa: *Allocator,
98 inst: Zir.Inst.Index,98 inst: Zir.Inst.Index,
99 scope_file: *Module.Scope.File,99 scope_file: *Module.File,
100 parent_decl_node: Ast.Node.Index,100 parent_decl_node: Ast.Node.Index,
101 indent: u32,101 indent: u32,
102 stream: anytype,102 stream: anytype,
...@@ -122,7 +122,7 @@ pub fn renderSingleInstruction(...@@ -122,7 +122,7 @@ pub fn renderSingleInstruction(
122const Writer = struct {122const Writer = struct {
123 gpa: *Allocator,123 gpa: *Allocator,
124 arena: *Allocator,124 arena: *Allocator,
125 file: *Module.Scope.File,125 file: *Module.File,
126 code: Zir,126 code: Zir,
127 indent: u32,127 indent: u32,
128 parent_decl_node: Ast.Node.Index,128 parent_decl_node: Ast.Node.Index,
src/type.zig+3-3
...@@ -3067,7 +3067,7 @@ pub const Type = extern union {...@@ -3067,7 +3067,7 @@ pub const Type = extern union {
3067 }3067 }
30683068
3069 /// Returns null if the type has no namespace.3069 /// 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 {
3071 return switch (self.tag()) {3071 return switch (self.tag()) {
3072 .@"struct" => &self.castTag(.@"struct").?.data.namespace,3072 .@"struct" => &self.castTag(.@"struct").?.data.namespace,
3073 .enum_full => &self.castTag(.enum_full).?.data.namespace,3073 .enum_full => &self.castTag(.enum_full).?.data.namespace,
...@@ -3833,12 +3833,12 @@ pub const Type = extern union {...@@ -3833,12 +3833,12 @@ pub const Type = extern union {
3833 /// Most commonly used for files.3833 /// Most commonly used for files.
3834 pub const ContainerScope = struct {3834 pub const ContainerScope = struct {
3835 base: Payload,3835 base: Payload,
3836 data: *Module.Scope.Namespace,3836 data: *Module.Namespace,
3837 };3837 };
38383838
3839 pub const Opaque = struct {3839 pub const Opaque = struct {
3840 base: Payload = .{ .tag = .@"opaque" },3840 base: Payload = .{ .tag = .@"opaque" },
3841 data: Module.Scope.Namespace,3841 data: Module.Namespace,
3842 };3842 };
38433843
3844 pub const Struct = struct {3844 pub const Struct = struct {
src/value.zig+50-2
...@@ -1238,9 +1238,24 @@ pub const Value = extern union {...@@ -1238,9 +1238,24 @@ pub const Value = extern union {
1238 const b_field_index = b.castTag(.enum_field_index).?.data;1238 const b_field_index = b.castTag(.enum_field_index).?.data;
1239 return a_field_index == b_field_index;1239 return a_field_index == b_field_index;
1240 },1240 },
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"),
1241 else => {},1245 else => {},
1242 }1246 }
1243 }1247 }
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
1244 if (ty.zigTypeTag() == .Type) {1259 if (ty.zigTypeTag() == .Type) {
1245 var buf_a: ToTypeBuffer = undefined;1260 var buf_a: ToTypeBuffer = undefined;
1246 var buf_b: ToTypeBuffer = undefined;1261 var buf_b: ToTypeBuffer = undefined;
...@@ -1285,8 +1300,28 @@ pub const Value = extern union {...@@ -1285,8 +1300,28 @@ pub const Value = extern union {
1285 const float = val.toFloat(f128);1300 const float = val.toFloat(f128);
1286 std.hash.autoHash(hasher, @bitCast(u128, float));1301 std.hash.autoHash(hasher, @bitCast(u128, float));
1287 },1302 },
1288 .Pointer => {1303 .Pointer => switch (val.tag()) {
1289 @panic("TODO implement hashing pointer values");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,
1290 },1325 },
1291 .Array, .Vector => {1326 .Array, .Vector => {
1292 @panic("TODO implement hashing array/vector values");1327 @panic("TODO implement hashing array/vector values");
...@@ -1432,6 +1467,19 @@ pub const Value = extern union {...@@ -1432,6 +1467,19 @@ pub const Value = extern union {
1432 return sub_val;1467 return sub_val;
1433 }1468 }
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
1435 pub fn sliceLen(val: Value) u64 {1483 pub fn sliceLen(val: Value) u64 {
1436 return switch (val.tag()) {1484 return switch (val.tag()) {
1437 .empty_array => 0,1485 .empty_array => 0,