authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-17 00:56:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-17 00:56:08-07:00
log38b3d4b00a693dd91af578d06dfe4ac6071d4536
tree7b6b51b311c464df0673108a88de4dd845528659
parente430f3f7e06cd331c03db98e5feb33047ade96be

stage2: work through some compile errors in Module and Sema


5 files changed, 374 insertions(+), 291 deletions(-)

BRANCH_TODO+2
......@@ -11,6 +11,8 @@ Merge TODO list:
1111 * update astgen.zig
1212 * finish updating Sema.zig
1313 * finish implementing SrcLoc byteOffset function
14 * audit Module.zig for use of token_starts - it should only be when
15 resolving LazySrcLoc
1416
1517
1618Performance optimizations to look into:
src/Module.zig+156-93
......@@ -389,6 +389,7 @@ pub const Scope = struct {
389389 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.arena,
390390 .file => unreachable,
391391 .container => unreachable,
392 .decl_ref => unreachable,
392393 }
393394 }
394395
......@@ -406,6 +407,7 @@ pub const Scope = struct {
406407 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.decl,
407408 .file => null,
408409 .container => null,
410 .decl_ref => scope.cast(DeclRef).?.decl,
409411 };
410412 }
411413
......@@ -419,6 +421,7 @@ pub const Scope = struct {
419421 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.decl,
420422 .file => null,
421423 .container => null,
424 .decl_ref => scope.cast(DeclRef).?.decl,
422425 };
423426 }
424427
......@@ -433,6 +436,7 @@ pub const Scope = struct {
433436 .container => return scope.cast(Container).?,
434437 .gen_suspend => return scope.cast(GenZir).?.zir_code.decl.container,
435438 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.zir_code.decl.container,
439 .decl_ref => return scope.cast(DeclRef).?.decl.container,
436440 }
437441 }
438442
......@@ -449,6 +453,7 @@ pub const Scope = struct {
449453 .gen_nosuspend => unreachable,
450454 .file => unreachable,
451455 .container => return scope.cast(Container).?.fullyQualifiedNameHash(name),
456 .decl_ref => unreachable,
452457 }
453458 }
454459
......@@ -463,6 +468,7 @@ pub const Scope = struct {
463468 .container => return &scope.cast(Container).?.file_scope.tree,
464469 .gen_suspend => return &scope.cast(GenZir).?.decl.container.file_scope.tree,
465470 .gen_nosuspend => return &scope.cast(Nosuspend).?.gen_zir.decl.container.file_scope.tree,
471 .decl_ref => return &scope.cast(DeclRef).?.decl.container.file_scope.tree,
466472 }
467473 }
468474
......@@ -476,6 +482,7 @@ pub const Scope = struct {
476482 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir,
477483 .file => unreachable,
478484 .container => unreachable,
485 .decl_ref => unreachable,
479486 };
480487 }
481488
......@@ -491,6 +498,7 @@ pub const Scope = struct {
491498 .local_ptr => unreachable,
492499 .gen_suspend => unreachable,
493500 .gen_nosuspend => unreachable,
501 .decl_ref => unreachable,
494502 }
495503 }
496504
......@@ -504,6 +512,7 @@ pub const Scope = struct {
504512 .block => unreachable,
505513 .gen_suspend => unreachable,
506514 .gen_nosuspend => unreachable,
515 .decl_ref => unreachable,
507516 }
508517 }
509518
......@@ -520,6 +529,7 @@ pub const Scope = struct {
520529 .block => return @fieldParentPtr(Block, "base", cur).src_decl.container.file_scope,
521530 .gen_suspend => @fieldParentPtr(GenZir, "base", cur).parent,
522531 .gen_nosuspend => @fieldParentPtr(Nosuspend, "base", cur).parent,
532 .decl_ref => @fieldParentPtr(DeclRef, "base", cur).decl.container.file_scope,
523533 };
524534 }
525535 }
......@@ -571,6 +581,10 @@ pub const Scope = struct {
571581 local_ptr,
572582 gen_suspend,
573583 gen_nosuspend,
584 /// Used for simple error reporting. Only contains a reference to a
585 /// `Decl` for use with `srcDecl` and `ownerDecl`.
586 /// Has no parents or children.
587 decl_ref,
574588 };
575589
576590 pub const Container = struct {
......@@ -1077,6 +1091,12 @@ pub const Scope = struct {
10771091 gen_zir: *GenZir,
10781092 src: LazySrcLoc,
10791093 };
1094
1095 pub const DeclRef = struct {
1096 pub const base_tag: Tag = .decl_ref;
1097 base: Scope = Scope{ .tag = base_tag },
1098 decl: *Decl,
1099 };
10801100};
10811101
10821102/// A Work-In-Progress `zir.Code`. This is a shared parent of all
......@@ -1302,6 +1322,35 @@ pub const LazySrcLoc = union(enum) {
13021322 /// to the sentinel expression.
13031323 /// The Decl is determined contextually.
13041324 node_offset_slice_sentinel: u32,
1325
1326 /// Upgrade to a `SrcLoc` based on the `Decl` or file in the provided scope.
1327 pub fn toSrcLoc(lazy: LazySrcLoc, scope: *Scope) SrcLoc {
1328 return switch (lazy) {
1329 .unneeded,
1330 .todo,
1331 .byte_abs,
1332 .token_abs,
1333 => .{
1334 .container = .{ .file_scope = scope.getFileScope() },
1335 .lazy = lazy,
1336 },
1337
1338 .byte_offset,
1339 .token_offset,
1340 .node_offset,
1341 .node_offset_var_decl_ty,
1342 .node_offset_for_cond,
1343 .node_offset_builtin_call_arg0,
1344 .node_offset_builtin_call_arg1,
1345 .node_offset_builtin_call_argn,
1346 .node_offset_array_access_index,
1347 .node_offset_slice_sentinel,
1348 => .{
1349 .container = .{ .decl = scope.srcDecl().? },
1350 .lazy = lazy,
1351 },
1352 };
1353 }
13051354};
13061355
13071356pub const InnerError = error{ OutOfMemory, AnalysisFail };
......@@ -1534,24 +1583,27 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
15341583
15351584 var sema: Sema = .{
15361585 .mod = mod,
1586 .gpa = mod.gpa,
1587 .arena = &analysis_arena.allocator,
15371588 .code = code,
15381589 .inst_map = try mod.gpa.alloc(*ir.Inst, code.instructions.len),
1590 .owner_decl = decl,
1591 .func = null,
1592 .param_inst_list = &.{},
15391593 };
15401594 defer mod.gpa.free(sema.inst_map);
15411595
15421596 var block_scope: Scope.Block = .{
15431597 .parent = null,
1544 .func = null,
1545 .owner_decl = decl,
1598 .sema = &sema,
15461599 .src_decl = decl,
15471600 .instructions = .{},
1548 .arena = &analysis_arena.allocator,
15491601 .inlining = null,
15501602 .is_comptime = true,
15511603 };
15521604 defer block_scope.instructions.deinit(mod.gpa);
15531605
1554 try sema.root(mod, &block_scope);
1606 try sema.root(&block_scope);
15551607
15561608 decl.analysis = .complete;
15571609 decl.generation = mod.generation;
......@@ -1753,19 +1805,21 @@ fn astgenAndSemaFn(
17531805 const fn_type_code = fn_type_wip_zir_exec.finish();
17541806 var fn_type_sema: Sema = .{
17551807 .mod = mod,
1808 .gpa = mod.gpa,
1809 .arena = &decl_arena.allocator,
17561810 .code = fn_type_code,
17571811 .inst_map = try mod.gpa.alloc(*ir.Inst, fn_type_code.instructions.len),
1812 .owner_decl = decl,
1813 .func = null,
1814 .param_inst_list = &.{},
17581815 };
17591816 defer mod.gpa.free(fn_type_sema.inst_map);
17601817
17611818 var block_scope: Scope.Block = .{
17621819 .parent = null,
17631820 .sema = &fn_type_sema,
1764 .func = null,
1765 .owner_decl = decl,
17661821 .src_decl = decl,
17671822 .instructions = .{},
1768 .arena = &decl_arena.allocator,
17691823 .inlining = null,
17701824 .is_comptime = false,
17711825 };
......@@ -1959,8 +2013,8 @@ fn astgenAndSemaVarDecl(
19592013 defer tracy.end();
19602014
19612015 decl.analysis = .in_progress;
2016 decl.is_pub = var_decl.visib_token != null;
19622017
1963 const token_starts = tree.tokens.items(.start);
19642018 const token_tags = tree.tokens.items(.tag);
19652019
19662020 // We need the memory for the Type to go into the arena for the Decl
......@@ -1968,54 +2022,29 @@ fn astgenAndSemaVarDecl(
19682022 errdefer decl_arena.deinit();
19692023 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);
19702024
1971 var decl_inst_table = Scope.Block.InstTable.init(mod.gpa);
1972 defer decl_inst_table.deinit();
1973
1974 var branch_quota: u32 = default_eval_branch_quota;
2025 // Used for simple error reporting.
2026 var decl_scope: Scope.DeclRef = .{ .decl = decl };
19752027
1976 var block_scope: Scope.Block = .{
1977 .parent = null,
1978 .inst_table = &decl_inst_table,
1979 .func = null,
1980 .owner_decl = decl,
1981 .src_decl = decl,
1982 .instructions = .{},
1983 .arena = &decl_arena.allocator,
1984 .inlining = null,
1985 .is_comptime = true,
1986 .branch_quota = &branch_quota,
1987 };
1988 defer block_scope.instructions.deinit(mod.gpa);
1989
1990 decl.is_pub = var_decl.visib_token != null;
19912028 const is_extern = blk: {
19922029 const maybe_extern_token = var_decl.extern_export_token orelse break :blk false;
1993 if (token_tags[maybe_extern_token] != .keyword_extern) break :blk false;
1994 if (var_decl.ast.init_node != 0) {
1995 return mod.failNode(
1996 &block_scope.base,
1997 var_decl.ast.init_node,
1998 "extern variables have no initializers",
1999 .{},
2000 );
2001 }
2002 break :blk true;
2030 break :blk token_tags[maybe_extern_token] == .keyword_extern;
20032031 };
2032
20042033 if (var_decl.lib_name) |lib_name| {
20052034 assert(is_extern);
2006 return mod.failTok(&block_scope.base, lib_name, "TODO implement function library name", .{});
2035 return mod.failTok(&decl_scope.base, lib_name, "TODO implement function library name", .{});
20072036 }
20082037 const is_mutable = token_tags[var_decl.ast.mut_token] == .keyword_var;
20092038 const is_threadlocal = if (var_decl.threadlocal_token) |some| blk: {
20102039 if (!is_mutable) {
2011 return mod.failTok(&block_scope.base, some, "threadlocal variable cannot be constant", .{});
2040 return mod.failTok(&decl_scope.base, some, "threadlocal variable cannot be constant", .{});
20122041 }
20132042 break :blk true;
20142043 } else false;
20152044 assert(var_decl.comptime_token == null);
20162045 if (var_decl.ast.align_node != 0) {
20172046 return mod.failNode(
2018 &block_scope.base,
2047 &decl_scope.base,
20192048 var_decl.ast.align_node,
20202049 "TODO implement function align expression",
20212050 .{},
......@@ -2023,7 +2052,7 @@ fn astgenAndSemaVarDecl(
20232052 }
20242053 if (var_decl.ast.section_node != 0) {
20252054 return mod.failNode(
2026 &block_scope.base,
2055 &decl_scope.base,
20272056 var_decl.ast.section_node,
20282057 "TODO implement function section expression",
20292058 .{},
......@@ -2031,25 +2060,36 @@ fn astgenAndSemaVarDecl(
20312060 }
20322061
20332062 const var_info: struct { ty: Type, val: ?Value } = if (var_decl.ast.init_node != 0) vi: {
2063 if (is_extern) {
2064 return mod.failNode(
2065 &decl_scope.base,
2066 var_decl.ast.init_node,
2067 "extern variables have no initializers",
2068 .{},
2069 );
2070 }
2071
20342072 var gen_scope_arena = std.heap.ArenaAllocator.init(mod.gpa);
20352073 defer gen_scope_arena.deinit();
2036 var gen_scope: Scope.GenZir = .{
2074
2075 var wip_zir_code: WipZirCode = .{
20372076 .decl = decl,
20382077 .arena = &gen_scope_arena.allocator,
2039 .parent = &decl.container.base,
2078 .gpa = mod.gpa,
2079 };
2080 defer wip_zir_code.deinit();
2081
2082 var gen_scope: Scope.GenZir = .{
20402083 .force_comptime = true,
2084 .parent = &decl.container.base,
2085 .zir_code = &wip_zir_code,
20412086 };
20422087 defer gen_scope.instructions.deinit(mod.gpa);
20432088
2044 const init_result_loc: astgen.ResultLoc = if (var_decl.ast.type_node != 0) rl: {
2045 const type_node = var_decl.ast.type_node;
2046 const src = token_starts[tree.firstToken(type_node)];
2047 const type_type = try astgen.addZIRInstConst(mod, &gen_scope.base, src, .{
2048 .ty = Type.initTag(.type),
2049 .val = Value.initTag(.type_type),
2050 });
2051 const var_type = try astgen.expr(mod, &gen_scope.base, .{ .ty = type_type }, type_node);
2052 break :rl .{ .ty = var_type };
2089 const init_result_loc: astgen.ResultLoc = if (var_decl.ast.type_node != 0) .{
2090 .ty = try astgen.expr(mod, &gen_scope.base, .{
2091 .ty = @enumToInt(zir.Const.type_type),
2092 }, var_decl.ast.type_node),
20532093 } else .none;
20542094
20552095 const init_inst = try astgen.comptimeExpr(
......@@ -2058,76 +2098,106 @@ fn astgenAndSemaVarDecl(
20582098 init_result_loc,
20592099 var_decl.ast.init_node,
20602100 );
2101 const code = wip_zir_code.finish();
20612102 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2062 zir.dumpZir(mod.gpa, "var_init", decl.name, gen_scope.instructions.items) catch {};
2103 zir.dumpZir(mod.gpa, "var_init", decl.name, code) catch {};
20632104 }
20642105
2065 var var_inst_table = Scope.Block.InstTable.init(mod.gpa);
2066 defer var_inst_table.deinit();
2106 var sema: Sema = .{
2107 .mod = mod,
2108 .gpa = mod.gpa,
2109 .arena = &gen_scope_arena.allocator,
2110 .code = code,
2111 .inst_map = try mod.gpa.alloc(*ir.Inst, code.instructions.len),
2112 .owner_decl = decl,
2113 .func = null,
2114 .param_inst_list = &.{},
2115 };
2116 defer mod.gpa.free(sema.inst_map);
20672117
2068 var branch_quota_vi: u32 = default_eval_branch_quota;
2069 var inner_block: Scope.Block = .{
2118 var block_scope: Scope.Block = .{
20702119 .parent = null,
2071 .inst_table = &var_inst_table,
2072 .func = null,
2073 .owner_decl = decl,
2120 .sema = &sema,
20742121 .src_decl = decl,
20752122 .instructions = .{},
2076 .arena = &gen_scope_arena.allocator,
20772123 .inlining = null,
20782124 .is_comptime = true,
2079 .branch_quota = &branch_quota_vi,
20802125 };
2081 defer inner_block.instructions.deinit(mod.gpa);
2082 try zir_sema.analyzeBody(mod, &inner_block, .{
2083 .instructions = gen_scope.instructions.items,
2084 });
2126 defer block_scope.instructions.deinit(mod.gpa);
2127
2128 try sema.root(&block_scope);
20852129
20862130 // The result location guarantees the type coercion.
2087 const analyzed_init_inst = var_inst_table.get(init_inst).?;
2131 const analyzed_init_inst = sema.resolveInst(&block_scope, init_inst);
20882132 // The is_comptime in the Scope.Block guarantees the result is comptime-known.
20892133 const val = analyzed_init_inst.value().?;
20902134
2091 const ty = try analyzed_init_inst.ty.copy(block_scope.arena);
20922135 break :vi .{
2093 .ty = ty,
2094 .val = try val.copy(block_scope.arena),
2136 .ty = try analyzed_init_inst.ty.copy(decl_arena),
2137 .val = try val.copy(decl_arena),
20952138 };
20962139 } else if (!is_extern) {
20972140 return mod.failTok(
2098 &block_scope.base,
2141 &decl_scope.base,
20992142 var_decl.ast.mut_token,
21002143 "variables must be initialized",
21012144 .{},
21022145 );
21032146 } else if (var_decl.ast.type_node != 0) vi: {
2104 const type_node = var_decl.ast.type_node;
2105 // Temporary arena for the zir instructions.
21062147 var type_scope_arena = std.heap.ArenaAllocator.init(mod.gpa);
21072148 defer type_scope_arena.deinit();
2108 var type_scope: Scope.GenZir = .{
2149
2150 var wip_zir_code: WipZirCode = .{
21092151 .decl = decl,
21102152 .arena = &type_scope_arena.allocator,
2111 .parent = &decl.container.base,
2153 .gpa = mod.gpa,
2154 };
2155 defer wip_zir_code.deinit();
2156
2157 var type_scope: Scope.GenZir = .{
21122158 .force_comptime = true,
2159 .parent = &decl.container.base,
2160 .zir_code = &wip_zir_code,
21132161 };
21142162 defer type_scope.instructions.deinit(mod.gpa);
21152163
2116 const var_type = try astgen.typeExpr(mod, &type_scope.base, type_node);
2164 const var_type = try astgen.typeExpr(mod, &type_scope.base, var_decl.ast.type_node);
2165 const code = wip_zir_code.finish();
21172166 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2118 zir.dumpZir(mod.gpa, "var_type", decl.name, type_scope.instructions.items) catch {};
2167 zir.dumpZir(mod.gpa, "var_type", decl.name, code) catch {};
21192168 }
21202169
2121 const ty = try zir_sema.analyzeBodyValueAsType(mod, &block_scope, var_type, .{
2122 .instructions = type_scope.instructions.items,
2123 });
2170 var sema: Sema = .{
2171 .mod = mod,
2172 .gpa = mod.gpa,
2173 .arena = &type_scope_arena.allocator,
2174 .code = code,
2175 .inst_map = try mod.gpa.alloc(*ir.Inst, code.instructions.len),
2176 .owner_decl = decl,
2177 .func = null,
2178 .param_inst_list = &.{},
2179 };
2180 defer mod.gpa.free(sema.inst_map);
2181
2182 var block_scope: Scope.Block = .{
2183 .parent = null,
2184 .sema = &sema,
2185 .src_decl = decl,
2186 .instructions = .{},
2187 .inlining = null,
2188 .is_comptime = true,
2189 };
2190 defer block_scope.instructions.deinit(mod.gpa);
2191
2192 const ty = try sema.rootAsType(&block_scope, var_type);
2193
21242194 break :vi .{
2125 .ty = ty,
2195 .ty = try ty.copy(decl_arena),
21262196 .val = null,
21272197 };
21282198 } else {
21292199 return mod.failTok(
2130 &block_scope.base,
2200 &decl_scope.base,
21312201 var_decl.ast.mut_token,
21322202 "unable to infer variable type",
21332203 .{},
......@@ -2136,7 +2206,7 @@ fn astgenAndSemaVarDecl(
21362206
21372207 if (is_mutable and !var_info.ty.isValidVarType(is_extern)) {
21382208 return mod.failTok(
2139 &block_scope.base,
2209 &decl_scope.base,
21402210 var_decl.ast.mut_token,
21412211 "variable of type '{}' must be const",
21422212 .{var_info.ty},
......@@ -2179,7 +2249,7 @@ fn astgenAndSemaVarDecl(
21792249 const name_token = var_decl.ast.mut_token + 1;
21802250 const name = tree.tokenSlice(name_token); // TODO identifierTokenString
21812251 // The scope needs to have the decl in it.
2182 try mod.analyzeExport(&block_scope.base, export_src, name, decl);
2252 try mod.analyzeExport(&decl_scope.base, export_src, name, decl);
21832253 }
21842254 }
21852255 return type_changed;
......@@ -2702,7 +2772,6 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn) !void {
27022772 .sema = &sema,
27032773 .src_decl = decl,
27042774 .instructions = .{},
2705 .arena = &arena.allocator,
27062775 .inlining = null,
27072776 .is_comptime = false,
27082777 };
......@@ -3101,10 +3170,7 @@ pub fn errNote(
31013170
31023171 parent.notes = try mod.gpa.realloc(parent.notes, parent.notes.len + 1);
31033172 parent.notes[parent.notes.len - 1] = .{
3104 .src_loc = .{
3105 .file_scope = scope.getFileScope(),
3106 .byte_offset = src,
3107 },
3173 .src_loc = src.toSrcLoc(scope),
31083174 .msg = msg,
31093175 };
31103176}
......@@ -3116,10 +3182,7 @@ pub fn errMsg(
31163182 comptime format: []const u8,
31173183 args: anytype,
31183184) error{OutOfMemory}!*ErrorMsg {
3119 return ErrorMsg.create(mod.gpa, .{
3120 .decl = scope.srcDecl().?,
3121 .lazy = src,
3122 }, format, args);
3185 return ErrorMsg.create(mod.gpa, src.toSrcLoc(scope), format, args);
31233186}
31243187
31253188pub fn fail(
src/Sema.zig+178-157
......@@ -71,27 +71,25 @@ const const_tzir_inst_list = blk: {
7171
7272pub fn root(sema: *Sema, root_block: *Scope.Block) !void {
7373 const root_body = sema.code.extra[sema.code.root_start..][0..sema.code.root_len];
74 return sema.body(root_block, root_body);
74 return sema.analyzeBody(root_block, root_body);
7575}
7676
7777pub fn rootAsType(
7878 sema: *Sema,
7979 root_block: *Scope.Block,
8080 zir_result_inst: zir.Inst.Index,
81 body: zir.Body,
8281) !Type {
8382 const root_body = sema.code.extra[sema.code.root_start..][0..sema.code.root_len];
84 try sema.body(root_block, root_body);
83 try sema.analyzeBody(root_block, root_body);
8584
8685 const result_inst = sema.inst_map[zir_result_inst];
8786 // Source location is unneeded because resolveConstValue must have already
8887 // been successfully called when coercing the value to a type, from the
8988 // result location.
90 const val = try sema.resolveConstValue(root_block, .unneeded, result_inst);
91 return val.toType(root_block.arena);
89 return sema.resolveType(root_block, .unneeded, result_inst);
9290}
9391
94pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !void {
92pub fn analyzeBody(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !void {
9593 const tracy = trace(@src());
9694 defer tracy.end();
9795
......@@ -154,7 +152,7 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi
154152 .field_val => try sema.zirFieldVal(block, zir_inst),
155153 .field_ptr_named => try sema.zirFieldPtrNamed(block, zir_inst),
156154 .field_val_named => try sema.zirFieldValNamed(block, zir_inst),
157 .deref => try sema.zirDeref(block, zir_inst),
155 .deref_node => try sema.zirDerefNode(block, zir_inst),
158156 .as => try sema.zirAs(block, zir_inst),
159157 .@"asm" => try sema.zirAsm(block, zir_inst, false),
160158 .asm_volatile => try sema.zirAsm(block, zir_inst, true),
......@@ -162,8 +160,10 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi
162160 .unreachable_unsafe => try sema.zirUnreachable(block, zir_inst, false),
163161 .ret_tok => try sema.zirRetTok(block, zir_inst),
164162 .ret_node => try sema.zirRetNode(block, zir_inst),
165 .fn_type => try sema.zirFnType(block, zir_inst),
166 .fn_type_cc => try sema.zirFnTypeCc(block, zir_inst),
163 .fn_type => try sema.zirFnType(block, zir_inst, false),
164 .fn_type_cc => try sema.zirFnTypeCc(block, zir_inst, false),
165 .fn_type_var_args => try sema.zirFnType(block, zir_inst, true),
166 .fn_type_cc_var_args => try sema.zirFnTypeCc(block, zir_inst, true),
167167 .intcast => try sema.zirIntcast(block, zir_inst),
168168 .bitcast => try sema.zirBitcast(block, zir_inst),
169169 .floatcast => try sema.zirFloatcast(block, zir_inst),
......@@ -230,10 +230,15 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi
230230 .import => try sema.zirImport(block, zir_inst),
231231 .bool_and => try sema.zirBoolOp(block, zir_inst, false),
232232 .bool_or => try sema.zirBoolOp(block, zir_inst, true),
233 .void_value => try sema.mod.constVoid(block.arena, .unneeded),
234 .switchbr => try sema.zirSwitchBr(block, zir_inst, false),
235 .switchbr_ref => try sema.zirSwitchBr(block, zir_inst, true),
236 .switch_range => try sema.zirSwitchRange(block, zir_inst),
233 .@"await" => try sema.zirAwait(block, zir_inst),
234 .nosuspend_await => try sema.zirNosuspendAwait(block, zir_inst),
235 .suspend_block_one => @panic("TODO"),
236 .suspend_block => @panic("TODO"),
237 .@"resume" => @panic("TODO"),
238 // TODO
239 //.switchbr => try sema.zirSwitchBr(block, zir_inst, false),
240 //.switchbr_ref => try sema.zirSwitchBr(block, zir_inst, true),
241 //.switch_range => try sema.zirSwitchRange(block, zir_inst),
237242 };
238243 if (map[zir_inst].ty.isNoReturn()) {
239244 break;
......@@ -241,7 +246,7 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi
241246 }
242247}
243248
244fn resolveInst(sema: *Sema, block: *Scope.Block, zir_ref: zir.Inst.Ref) *const ir.Inst {
249pub fn resolveInst(sema: *Sema, block: *Scope.Block, zir_ref: zir.Inst.Ref) *const ir.Inst {
245250 var i = zir_ref;
246251
247252 // First section of indexes correspond to a set number of constant values.
......@@ -277,7 +282,7 @@ fn resolveConstString(
277282 const wanted_type = Type.initTag(.const_slice_u8);
278283 const coerced_inst = try sema.coerce(block, wanted_type, tzir_inst);
279284 const val = try sema.resolveConstValue(block, src, coerced_inst);
280 return val.toAllocatedBytes(block.arena);
285 return val.toAllocatedBytes(sema.arena);
281286}
282287
283288fn resolveType(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, zir_ref: zir.Inst.Ref) !Type {
......@@ -355,7 +360,7 @@ fn zirConst(sema: *Sema, block: *Scope.Block, const_inst: zir.Inst.Index) InnerE
355360 defer tracy.end();
356361 // Move the TypedValue from old memory to new memory. This allows freeing the ZIR instructions
357362 // after analysis.
358 const typed_value_copy = try const_inst.positionals.typed_value.copy(block.arena);
363 const typed_value_copy = try const_inst.positionals.typed_value.copy(sema.arena);
359364 return sema.mod.constInst(scope, const_inst.base.src, typed_value_copy);
360365}
361366
......@@ -377,14 +382,14 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In
377382 return sema.mod.fail(&block.base, inst.base.src, "TODO implement zirCoerceResultPtr", .{});
378383}
379384
380fn zirRetPtr(sema: *Module, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
385fn zirRetPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
381386 const tracy = trace(@src());
382387 defer tracy.end();
383388
384389 try sema.requireFunctionBlock(block, inst.base.src);
385390 const fn_ty = block.func.?.owner_decl.typed_value.most_recent.typed_value.ty;
386391 const ret_type = fn_ty.fnReturnType();
387 const ptr_type = try sema.mod.simplePtrType(block.arena, ret_type, true, .One);
392 const ptr_type = try sema.mod.simplePtrType(sema.arena, ret_type, true, .One);
388393 return block.addNoOp(inst.base.src, ptr_type, .alloc);
389394}
390395
......@@ -403,7 +408,7 @@ fn zirRetType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
403408 try sema.requireFunctionBlock(block, inst.base.src);
404409 const fn_ty = b.func.?.owner_decl.typed_value.most_recent.typed_value.ty;
405410 const ret_type = fn_ty.fnReturnType();
406 return sema.mod.constType(block.arena, inst.base.src, ret_type);
411 return sema.mod.constType(sema.arena, inst.base.src, ret_type);
407412}
408413
409414fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -414,7 +419,7 @@ fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) I
414419 const operand = sema.resolveInst(block, inst_data.operand);
415420 const src = inst_data.src();
416421 switch (operand.ty.zigTypeTag()) {
417 .Void, .NoReturn => return sema.mod.constVoid(block.arena, .unneeded),
422 .Void, .NoReturn => return sema.mod.constVoid(sema.arena, .unneeded),
418423 else => return sema.mod.fail(&block.base, src, "expression value is ignored", .{}),
419424 }
420425}
......@@ -428,7 +433,7 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Inde
428433 const src = inst_data.src();
429434 switch (operand.ty.zigTypeTag()) {
430435 .ErrorSet, .ErrorUnion => return sema.mod.fail(&block.base, src, "error is discarded", .{}),
431 else => return sema.mod.constVoid(block.arena, .unneeded),
436 else => return sema.mod.constVoid(sema.arena, .unneeded),
432437 }
433438}
434439
......@@ -473,7 +478,7 @@ fn zirAlloc(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*
473478 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };
474479 const var_decl_src = inst_data.src();
475480 const var_type = try sema.resolveType(block, ty_src, inst_data.operand);
476 const ptr_type = try sema.mod.simplePtrType(block.arena, var_type, true, .One);
481 const ptr_type = try sema.mod.simplePtrType(sema.arena, var_type, true, .One);
477482 try sema.requireRuntimeBlock(block, var_decl_src);
478483 return block.addNoOp(var_decl_src, ptr_type, .alloc);
479484}
......@@ -487,7 +492,7 @@ fn zirAllocMut(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro
487492 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };
488493 const var_type = try sema.resolveType(block, ty_src, inst_data.operand);
489494 try sema.validateVarType(block, ty_src, var_type);
490 const ptr_type = try sema.mod.simplePtrType(block.arena, var_type, true, .One);
495 const ptr_type = try sema.mod.simplePtrType(sema.arena, var_type, true, .One);
491496 try sema.requireRuntimeBlock(block, var_decl_src);
492497 return block.addNoOp(var_decl_src, ptr_type, .alloc);
493498}
......@@ -500,7 +505,7 @@ fn zirAllocInferred(
500505) InnerError!*Inst {
501506 const tracy = trace(@src());
502507 defer tracy.end();
503 const val_payload = try block.arena.create(Value.Payload.InferredAlloc);
508 const val_payload = try sema.arena.create(Value.Payload.InferredAlloc);
504509 val_payload.* = .{
505510 .data = .{},
506511 };
......@@ -540,13 +545,13 @@ fn zirResolveInferredAlloc(
540545 if (var_is_mut) {
541546 try sema.validateVarType(block, ty_src, final_elem_ty);
542547 }
543 const final_ptr_ty = try sema.mod.simplePtrType(block.arena, final_elem_ty, true, .One);
548 const final_ptr_ty = try sema.mod.simplePtrType(sema.arena, final_elem_ty, true, .One);
544549
545550 // Change it to a normal alloc.
546551 ptr.ty = final_ptr_ty;
547552 ptr.tag = .alloc;
548553
549 return sema.mod.constVoid(block.arena, .unneeded);
554 return sema.mod.constVoid(sema.arena, .unneeded);
550555}
551556
552557fn zirStoreToBlockPtr(
......@@ -560,7 +565,7 @@ fn zirStoreToBlockPtr(
560565 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
561566 const ptr = sema.resolveInst(bin_inst.lhs);
562567 const value = sema.resolveInst(bin_inst.rhs);
563 const ptr_ty = try sema.mod.simplePtrType(block.arena, value.ty, true, .One);
568 const ptr_ty = try sema.mod.simplePtrType(sema.arena, value.ty, true, .One);
564569 // TODO detect when this store should be done at compile-time. For example,
565570 // if expressions should force it when the condition is compile-time known.
566571 try sema.requireRuntimeBlock(block, src);
......@@ -582,9 +587,9 @@ fn zirStoreToInferredPtr(
582587 const inferred_alloc = ptr.castTag(.constant).?.val.castTag(.inferred_alloc).?;
583588 // Add the stored instruction to the set we will use to resolve peer types
584589 // for the inferred allocation.
585 try inferred_alloc.data.stored_inst_list.append(block.arena, value);
590 try inferred_alloc.data.stored_inst_list.append(sema.arena, value);
586591 // Create a runtime bitcast instruction with exactly the type the pointer wants.
587 const ptr_ty = try sema.mod.simplePtrType(block.arena, value.ty, true, .One);
592 const ptr_ty = try sema.mod.simplePtrType(sema.arena, value.ty, true, .One);
588593 try sema.requireRuntimeBlock(block, src);
589594 const bitcasted_ptr = try block.addUnOp(inst.base.src, ptr_ty, .bitcast, ptr);
590595 return mod.storePtr(scope, inst.base.src, bitcasted_ptr, value);
......@@ -601,7 +606,7 @@ fn zirSetEvalBranchQuota(
601606 const quota = try sema.resolveAlreadyCoercedInt(block, src, inst_data.operand, u32);
602607 if (sema.branch_quota < quota)
603608 sema.branch_quota = quota;
604 return sema.mod.constVoid(block.arena, .unneeded);
609 return sema.mod.constVoid(sema.arena, .unneeded);
605610}
606611
607612fn zirStore(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -635,7 +640,7 @@ fn zirParamType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr
635640 const param_count = fn_ty.fnParamLen();
636641 if (param_index >= param_count) {
637642 if (fn_ty.fnIsVarArgs()) {
638 return sema.mod.constType(block.arena, inst.base.src, Type.initTag(.var_args_param));
643 return sema.mod.constType(sema.arena, inst.base.src, Type.initTag(.var_args_param));
639644 }
640645 return sema.mod.fail(&block.base, inst.base.src, "arg index {d} out of bounds; '{}' has {d} argument(s)", .{
641646 param_index,
......@@ -646,7 +651,7 @@ fn zirParamType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr
646651
647652 // TODO support generic functions
648653 const param_type = fn_ty.fnParamType(param_index);
649 return sema.mod.constType(block.arena, inst.base.src, param_type);
654 return sema.mod.constType(sema.arena, inst.base.src, param_type);
650655}
651656
652657fn zirStr(sema: *Sema, block: *Scope.Block, str_inst: zir.Inst.Index) InnerError!*Inst {
......@@ -714,7 +719,7 @@ fn zirCompileLog(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr
714719 .lazy = inst_data.src(),
715720 };
716721 }
717 return sema.mod.constVoid(block.arena, .unneeded);
722 return sema.mod.constVoid(sema.arena, .unneeded);
718723}
719724
720725fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -724,7 +729,7 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerE
724729 // Reserve space for a Loop instruction so that generated Break instructions can
725730 // point to it, even if it doesn't end up getting used because the code ends up being
726731 // comptime evaluated.
727 const loop_inst = try parent_block.arena.create(Inst.Loop);
732 const loop_inst = try sema.arena.create(Inst.Loop);
728733 loop_inst.* = .{
729734 .base = .{
730735 .tag = Inst.Loop.base_tag,
......@@ -741,19 +746,19 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerE
741746 .owner_decl = parent_block.owner_decl,
742747 .src_decl = parent_block.src_decl,
743748 .instructions = .{},
744 .arena = parent_block.arena,
749 .arena = sema.arena,
745750 .inlining = parent_block.inlining,
746751 .is_comptime = parent_block.is_comptime,
747752 .branch_quota = parent_block.branch_quota,
748753 };
749754 defer child_block.instructions.deinit(mod.gpa);
750755
751 try sema.body(&child_block, inst.positionals.body);
756 try sema.analyzeBody(&child_block, inst.positionals.body);
752757
753758 // Loop repetition is implied so the last instruction may or may not be a noreturn instruction.
754759
755760 try parent_block.instructions.append(mod.gpa, &loop_inst.base);
756 loop_inst.body = .{ .instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items) };
761 loop_inst.body = .{ .instructions = try sema.arena.dupe(*Inst, child_block.instructions.items) };
757762 return &loop_inst.base;
758763}
759764
......@@ -765,10 +770,10 @@ fn zirBlockFlat(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index, i
765770 defer child_block.instructions.deinit(mod.gpa);
766771 child_block.is_comptime = child_block.is_comptime or is_comptime;
767772
768 try sema.body(&child_block, inst.positionals.body);
773 try sema.analyzeBody(&child_block, inst.positionals.body);
769774
770775 // Move the analyzed instructions into the parent block arena.
771 const copied_instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items);
776 const copied_instructions = try sema.arena.dupe(*Inst, child_block.instructions.items);
772777 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);
773778
774779 // The result of a flat block is the last instruction.
......@@ -789,7 +794,7 @@ fn zirBlock(
789794 // Reserve space for a Block instruction so that generated Break instructions can
790795 // point to it, even if it doesn't end up getting used because the code ends up being
791796 // comptime evaluated.
792 const block_inst = try parent_block.arena.create(Inst.Block);
797 const block_inst = try sema.arena.create(Inst.Block);
793798 block_inst.* = .{
794799 .base = .{
795800 .tag = Inst.Block.base_tag,
......@@ -806,7 +811,7 @@ fn zirBlock(
806811 .owner_decl = parent_block.owner_decl,
807812 .src_decl = parent_block.src_decl,
808813 .instructions = .{},
809 .arena = parent_block.arena,
814 .arena = sema.arena,
810815 // TODO @as here is working around a stage1 miscompilation bug :(
811816 .label = @as(?Scope.Block.Label, Scope.Block.Label{
812817 .zir_block = inst,
......@@ -826,7 +831,7 @@ fn zirBlock(
826831 defer merges.results.deinit(mod.gpa);
827832 defer merges.br_list.deinit(mod.gpa);
828833
829 try sema.body(&child_block, inst.positionals.body);
834 try sema.analyzeBody(&child_block, inst.positionals.body);
830835
831836 return analyzeBlockBody(mod, scope, &child_block, merges);
832837}
......@@ -847,7 +852,7 @@ fn analyzeBlockBody(
847852 if (merges.results.items.len == 0) {
848853 // No need for a block instruction. We can put the new instructions
849854 // directly into the parent block.
850 const copied_instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items);
855 const copied_instructions = try sema.arena.dupe(*Inst, child_block.instructions.items);
851856 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);
852857 return copied_instructions[copied_instructions.len - 1];
853858 }
......@@ -858,7 +863,7 @@ fn analyzeBlockBody(
858863 if (br_block == merges.block_inst) {
859864 // No need for a block instruction. We can put the new instructions directly
860865 // into the parent block. Here we omit the break instruction.
861 const copied_instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items[0..last_inst_index]);
866 const copied_instructions = try sema.arena.dupe(*Inst, child_block.instructions.items[0..last_inst_index]);
862867 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);
863868 return merges.results.items[0];
864869 }
......@@ -873,7 +878,7 @@ fn analyzeBlockBody(
873878 const resolved_ty = try sema.resolvePeerTypes(parent_block, merges.results.items);
874879 merges.block_inst.base.ty = resolved_ty;
875880 merges.block_inst.body = .{
876 .instructions = try parent_block.arena.dupe(*Inst, child_block.instructions.items),
881 .instructions = try sema.arena.dupe(*Inst, child_block.instructions.items),
877882 };
878883 // Now that the block has its type resolved, we need to go back into all the break
879884 // instructions, and insert type coercion on the operands.
......@@ -905,7 +910,7 @@ fn analyzeBlockBody(
905910 },
906911 .block = merges.block_inst,
907912 .body = .{
908 .instructions = try parent_block.arena.dupe(*Inst, coerce_block.instructions.items),
913 .instructions = try sema.arena.dupe(*Inst, coerce_block.instructions.items),
909914 },
910915 };
911916 }
......@@ -936,7 +941,7 @@ fn zirBreakVoidTok(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner
936941
937942 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
938943 const zir_block = inst_data.operand;
939 const void_inst = try sema.mod.constVoid(block.arena, .unneeded);
944 const void_inst = try sema.mod.constVoid(sema.arena, .unneeded);
940945 return analyzeBreak(mod, block, inst_data.src(), zir_block, void_inst);
941946}
942947
......@@ -984,7 +989,7 @@ fn zirDbgStmtNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE
984989 defer tracy.end();
985990
986991 if (b.is_comptime) {
987 return sema.mod.constVoid(block.arena, .unneeded);
992 return sema.mod.constVoid(sema.arena, .unneeded);
988993 }
989994
990995 const src_node = sema.code.instructions.items(.data)[inst].node;
......@@ -1090,7 +1095,7 @@ fn analyzeCall(
10901095 }
10911096
10921097 // TODO handle function calls of generic functions
1093 const casted_args = try block.arena.alloc(*Inst, zir_args.len);
1098 const casted_args = try sema.arena.alloc(*Inst, zir_args.len);
10941099 for (zir_args) |zir_arg, i| {
10951100 // the args are already casted to the result of a param type instruction.
10961101 casted_args[i] = sema.resolveInst(block, zir_arg);
......@@ -1117,7 +1122,7 @@ fn analyzeCall(
11171122 // set to in the `Scope.Block`.
11181123 // This block instruction will be used to capture the return value from the
11191124 // inlined function.
1120 const block_inst = try block.arena.create(Inst.Block);
1125 const block_inst = try sema.arena.create(Inst.Block);
11211126 block_inst.* = .{
11221127 .base = .{
11231128 .tag = Inst.Block.base_tag,
......@@ -1154,7 +1159,7 @@ fn analyzeCall(
11541159 .owner_decl = scope.ownerDecl().?,
11551160 .src_decl = module_fn.owner_decl,
11561161 .instructions = .{},
1157 .arena = block.arena,
1162 .arena = sema.arena,
11581163 .label = null,
11591164 .inlining = &inlining,
11601165 .is_comptime = is_comptime_call,
......@@ -1171,7 +1176,7 @@ fn analyzeCall(
11711176
11721177 // This will have return instructions analyzed as break instructions to
11731178 // the block_inst above.
1174 try sema.body(&child_block, module_fn.zir);
1179 try sema.analyzeBody(&child_block, module_fn.zir);
11751180
11761181 return analyzeBlockBody(mod, scope, &child_block, merges);
11771182 }
......@@ -1191,9 +1196,9 @@ fn zirOptionalType(sema: *Sema, block: *Scope.Block, optional: zir.Inst.Index) I
11911196
11921197 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
11931198 const child_type = try sema.resolveType(block, inst_data.operand);
1194 const opt_type = try mod.optionalType(block.arena, child_type);
1199 const opt_type = try mod.optionalType(sema.arena, child_type);
11951200
1196 return sema.mod.constType(block.arena, inst_data.src(), opt_type);
1201 return sema.mod.constType(sema.arena, inst_data.src(), opt_type);
11971202}
11981203
11991204fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -1203,9 +1208,9 @@ fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: zir.Inst.I
12031208 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
12041209 const ptr = sema.resolveInst(block, inst_data.operand);
12051210 const elem_ty = ptr.ty.elemType();
1206 const opt_ty = try mod.optionalType(block.arena, elem_ty);
1211 const opt_ty = try mod.optionalType(sema.arena, elem_ty);
12071212
1208 return sema.mod.constType(block.arena, inst_data.src(), opt_ty);
1213 return sema.mod.constType(sema.arena, inst_data.src(), opt_ty);
12091214}
12101215
12111216fn zirArrayType(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index) InnerError!*Inst {
......@@ -1215,7 +1220,7 @@ fn zirArrayType(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index) InnerEr
12151220 const len = try resolveInstConst(mod, scope, array.positionals.lhs);
12161221 const elem_type = try sema.resolveType(block, array.positionals.rhs);
12171222
1218 return sema.mod.constType(block.arena, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), null, elem_type));
1223 return sema.mod.constType(sema.arena, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), null, elem_type));
12191224}
12201225
12211226fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index) InnerError!*Inst {
......@@ -1226,7 +1231,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index)
12261231 const sentinel = try resolveInstConst(mod, scope, array.positionals.sentinel);
12271232 const elem_type = try sema.resolveType(block, array.positionals.elem_type);
12281233
1229 return sema.mod.constType(block.arena, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), sentinel.val, elem_type));
1234 return sema.mod.constType(sema.arena, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), sentinel.val, elem_type));
12301235}
12311236
12321237fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -1241,7 +1246,7 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn
12411246 return sema.mod.fail(&block.base, inst.base.src, "expected error set type, found {}", .{error_union.elemType()});
12421247 }
12431248
1244 return sema.mod.constType(block.arena, inst.base.src, try mod.errorUnionType(scope, error_union, payload));
1249 return sema.mod.constType(sema.arena, inst.base.src, try mod.errorUnionType(scope, error_union, payload));
12451250}
12461251
12471252fn zirAnyframeType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -1252,9 +1257,9 @@ fn zirAnyframeType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner
12521257 const src = inst_data.src();
12531258 const operand_src: LazySrcLoc = .{ .node_offset_anyframe_type = inst_data.src_node };
12541259 const return_type = try sema.resolveType(block, operand_src, inst_data.operand);
1255 const anyframe_type = try sema.mod.anyframeType(block.arena, return_type);
1260 const anyframe_type = try sema.mod.anyframeType(sema.arena, return_type);
12561261
1257 return sema.mod.constType(block.arena, src, anyframe_type);
1262 return sema.mod.constType(sema.arena, src, anyframe_type);
12581263}
12591264
12601265fn zirErrorSet(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -1296,10 +1301,10 @@ fn zirErrorValue(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr
12961301
12971302 // Create an anonymous error set type with only this error value, and return the value.
12981303 const entry = try mod.getErrorValue(inst.positionals.name);
1299 const result_type = try Type.Tag.error_set_single.create(block.arena, entry.key);
1304 const result_type = try Type.Tag.error_set_single.create(sema.arena, entry.key);
13001305 return sema.mod.constInst(scope, inst.base.src, .{
13011306 .ty = result_type,
1302 .val = try Value.Tag.@"error".create(block.arena, .{
1307 .val = try Value.Tag.@"error".create(sema.arena, .{
13031308 .name = entry.key,
13041309 }),
13051310 });
......@@ -1388,10 +1393,10 @@ fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, zir_inst: zir.Inst.Index) In
13881393 const tracy = trace(@src());
13891394 defer tracy.end();
13901395
1391 const duped_name = try block.arena.dupe(u8, inst.positionals.name);
1396 const duped_name = try sema.arena.dupe(u8, inst.positionals.name);
13921397 return sema.mod.constInst(scope, inst.base.src, .{
13931398 .ty = Type.initTag(.enum_literal),
1394 .val = try Value.Tag.enum_literal.create(block.arena, duped_name),
1399 .val = try Value.Tag.enum_literal.create(sema.arena, duped_name),
13951400 });
13961401}
13971402
......@@ -1415,11 +1420,11 @@ fn zirOptionalPayloadPtr(
14151420 return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type});
14161421 }
14171422
1418 const child_type = try opt_type.optionalChildAlloc(block.arena);
1419 const child_pointer = try sema.mod.simplePtrType(block.arena, child_type, !optional_ptr.ty.isConstPtr(), .One);
1423 const child_type = try opt_type.optionalChildAlloc(sema.arena);
1424 const child_pointer = try sema.mod.simplePtrType(sema.arena, child_type, !optional_ptr.ty.isConstPtr(), .One);
14201425
14211426 if (optional_ptr.value()) |pointer_val| {
1422 const val = try pointer_val.pointerDeref(block.arena);
1427 const val = try pointer_val.pointerDeref(sema.arena);
14231428 if (val.isNull()) {
14241429 return sema.mod.fail(&block.base, src, "unable to unwrap null", .{});
14251430 }
......@@ -1456,7 +1461,7 @@ fn zirOptionalPayload(
14561461 return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type});
14571462 }
14581463
1459 const child_type = try opt_type.optionalChildAlloc(block.arena);
1464 const child_type = try opt_type.optionalChildAlloc(sema.arena);
14601465
14611466 if (operand.value()) |val| {
14621467 if (val.isNull()) {
......@@ -1528,10 +1533,10 @@ fn zirErrUnionPayloadPtr(
15281533 if (operand.ty.elemType().zigTypeTag() != .ErrorUnion)
15291534 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand.ty.elemType()});
15301535
1531 const operand_pointer_ty = try sema.mod.simplePtrType(block.arena, operand.ty.elemType().castTag(.error_union).?.data.payload, !operand.ty.isConstPtr(), .One);
1536 const operand_pointer_ty = try sema.mod.simplePtrType(sema.arena, operand.ty.elemType().castTag(.error_union).?.data.payload, !operand.ty.isConstPtr(), .One);
15321537
15331538 if (operand.value()) |pointer_val| {
1534 const val = try pointer_val.pointerDeref(block.arena);
1539 const val = try pointer_val.pointerDeref(sema.arena);
15351540 if (val.getError()) |name| {
15361541 return sema.mod.fail(&block.base, src, "caught unexpected error '{s}'", .{name});
15371542 }
......@@ -1540,7 +1545,7 @@ fn zirErrUnionPayloadPtr(
15401545 return sema.mod.constInst(scope, src, .{
15411546 .ty = operand_pointer_ty,
15421547 .val = try Value.Tag.ref_val.create(
1543 block.arena,
1548 sema.arena,
15441549 data,
15451550 ),
15461551 });
......@@ -1592,7 +1597,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In
15921597 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand.ty.elemType()});
15931598
15941599 if (operand.value()) |pointer_val| {
1595 const val = try pointer_val.pointerDeref(block.arena);
1600 const val = try pointer_val.pointerDeref(sema.arena);
15961601 assert(val.getError() != null);
15971602 const data = val.castTag(.error_union).?.data;
15981603 return sema.mod.constInst(scope, src, .{
......@@ -1617,40 +1622,46 @@ fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Inde
16171622 if (operand.ty.castTag(.error_union).?.data.payload.zigTypeTag() != .Void) {
16181623 return sema.mod.fail(&block.base, src, "expression value is ignored", .{});
16191624 }
1620 return sema.mod.constVoid(block.arena, .unneeded);
1625 return sema.mod.constVoid(sema.arena, .unneeded);
16211626}
16221627
1623fn zirFnType(sema: *Sema, block: *Scope.Block, fntype: zir.Inst.Index, var_args: bool) InnerError!*Inst {
1628fn zirFnType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index, var_args: bool) InnerError!*Inst {
16241629 const tracy = trace(@src());
16251630 defer tracy.end();
16261631
1627 return fnTypeCommon(
1628 mod,
1629 scope,
1630 &fntype.base,
1631 fntype.positionals.param_types,
1632 fntype.positionals.return_type,
1632 const inst_data = sema.code.instructions.items(.data)[inst].fn_type;
1633 const extra = sema.code.extraData(zir.Inst.FnType, inst_data.payload_index);
1634 const param_types = sema.code.extra[extra.end..][0..extra.data.param_types_len];
1635
1636 return sema.fnTypeCommon(
1637 block,
1638 .unneeded,
1639 param_types,
1640 inst_data.return_type,
16331641 .Unspecified,
16341642 var_args,
16351643 );
16361644}
16371645
1638fn zirFnTypeCc(sema: *Sema, block: *Scope.Block, fntype: zir.Inst.Index, var_args: bool) InnerError!*Inst {
1646fn zirFnTypeCc(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index, var_args: bool) InnerError!*Inst {
16391647 const tracy = trace(@src());
16401648 defer tracy.end();
16411649
1642 const cc_tv = try resolveInstConst(mod, scope, fntype.positionals.cc);
1650 const inst_data = sema.code.instructions.items(.data)[inst].fn_type;
1651 const extra = sema.code.extraData(zir.Inst.FnTypeCc, inst_data.payload_index);
1652 const param_types = sema.code.extra[extra.end..][0..extra.data.param_types_len];
1653
1654 const cc_tv = try resolveInstConst(mod, scope, extra.data.cc);
16431655 // TODO once we're capable of importing and analyzing decls from
16441656 // std.builtin, this needs to change
16451657 const cc_str = cc_tv.val.castTag(.enum_literal).?.data;
16461658 const cc = std.meta.stringToEnum(std.builtin.CallingConvention, cc_str) orelse
1647 return sema.mod.fail(&block.base, fntype.positionals.cc.src, "Unknown calling convention {s}", .{cc_str});
1648 return fnTypeCommon(
1649 mod,
1650 scope,
1651 &fntype.base,
1652 fntype.positionals.param_types,
1653 fntype.positionals.return_type,
1659 return sema.mod.fail(&block.base, .todo, "Unknown calling convention {s}", .{cc_str});
1660 return sema.fnTypeCommon(
1661 block,
1662 .unneeded,
1663 param_types,
1664 inst_data.return_type,
16541665 cc,
16551666 var_args,
16561667 );
......@@ -1659,9 +1670,9 @@ fn zirFnTypeCc(sema: *Sema, block: *Scope.Block, fntype: zir.Inst.Index, var_arg
16591670fn fnTypeCommon(
16601671 sema: *Sema,
16611672 block: *Scope.Block,
1662 zir_inst: zir.Inst.Index,
1663 zir_param_types: []zir.Inst.Index,
1664 zir_return_type: zir.Inst.Index,
1673 src: LazySrcLoc,
1674 zir_param_types: []const zir.Inst.Ref,
1675 zir_return_type: zir.Inst.Ref,
16651676 cc: std.builtin.CallingConvention,
16661677 var_args: bool,
16671678) InnerError!*Inst {
......@@ -1670,39 +1681,39 @@ fn fnTypeCommon(
16701681 // Hot path for some common function types.
16711682 if (zir_param_types.len == 0 and !var_args) {
16721683 if (return_type.zigTypeTag() == .NoReturn and cc == .Unspecified) {
1673 return sema.mod.constType(block.arena, zir_inst.src, Type.initTag(.fn_noreturn_no_args));
1684 return sema.mod.constType(sema.arena, src, Type.initTag(.fn_noreturn_no_args));
16741685 }
16751686
16761687 if (return_type.zigTypeTag() == .Void and cc == .Unspecified) {
1677 return sema.mod.constType(block.arena, zir_inst.src, Type.initTag(.fn_void_no_args));
1688 return sema.mod.constType(sema.arena, src, Type.initTag(.fn_void_no_args));
16781689 }
16791690
16801691 if (return_type.zigTypeTag() == .NoReturn and cc == .Naked) {
1681 return sema.mod.constType(block.arena, zir_inst.src, Type.initTag(.fn_naked_noreturn_no_args));
1692 return sema.mod.constType(sema.arena, src, Type.initTag(.fn_naked_noreturn_no_args));
16821693 }
16831694
16841695 if (return_type.zigTypeTag() == .Void and cc == .C) {
1685 return sema.mod.constType(block.arena, zir_inst.src, Type.initTag(.fn_ccc_void_no_args));
1696 return sema.mod.constType(sema.arena, src, Type.initTag(.fn_ccc_void_no_args));
16861697 }
16871698 }
16881699
1689 const param_types = try block.arena.alloc(Type, zir_param_types.len);
1700 const param_types = try sema.arena.alloc(Type, zir_param_types.len);
16901701 for (zir_param_types) |param_type, i| {
16911702 const resolved = try sema.resolveType(block, param_type);
16921703 // TODO skip for comptime params
16931704 if (!resolved.isValidVarType(false)) {
1694 return sema.mod.fail(&block.base, param_type.src, "parameter of type '{}' must be declared comptime", .{resolved});
1705 return sema.mod.fail(&block.base, .todo, "parameter of type '{}' must be declared comptime", .{resolved});
16951706 }
16961707 param_types[i] = resolved;
16971708 }
16981709
1699 const fn_ty = try Type.Tag.function.create(block.arena, .{
1710 const fn_ty = try Type.Tag.function.create(sema.arena, .{
17001711 .param_types = param_types,
17011712 .return_type = return_type,
17021713 .cc = cc,
17031714 .is_var_args = var_args,
17041715 });
1705 return sema.mod.constType(block.arena, zir_inst.src, fn_ty);
1716 return sema.mod.constType(sema.arena, src, fn_ty);
17061717}
17071718
17081719fn zirAs(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -1981,11 +1992,11 @@ fn zirSwitchRange(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE
19811992
19821993 switch (start.ty.zigTypeTag()) {
19831994 .Int, .ComptimeInt => {},
1984 else => return sema.mod.constVoid(block.arena, .unneeded),
1995 else => return sema.mod.constVoid(sema.arena, .unneeded),
19851996 }
19861997 switch (end.ty.zigTypeTag()) {
19871998 .Int, .ComptimeInt => {},
1988 else => return sema.mod.constVoid(block.arena, .unneeded),
1999 else => return sema.mod.constVoid(sema.arena, .unneeded),
19892000 }
19902001 // .switch_range must be inside a comptime scope
19912002 const start_val = start.value().?;
......@@ -1993,7 +2004,7 @@ fn zirSwitchRange(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE
19932004 if (start_val.compare(.gte, end_val)) {
19942005 return sema.mod.fail(&block.base, inst.base.src, "range start value must be smaller than the end value", .{});
19952006 }
1996 return sema.mod.constVoid(block.arena, .unneeded);
2007 return sema.mod.constVoid(sema.arena, .unneeded);
19972008}
19982009
19992010fn zirSwitchBr(
......@@ -2021,22 +2032,22 @@ fn zirSwitchBr(
20212032 const item = try sema.resolveConstValue(parent_block, case_src, casted);
20222033
20232034 if (target_val.eql(item)) {
2024 try sema.body(scope.cast(Scope.Block).?, case.body);
2035 try sema.analyzeBody(scope.cast(Scope.Block).?, case.body);
20252036 return mod.constNoReturn(scope, inst.base.src);
20262037 }
20272038 }
2028 try sema.body(scope.cast(Scope.Block).?, inst.positionals.else_body);
2039 try sema.analyzeBody(scope.cast(Scope.Block).?, inst.positionals.else_body);
20292040 return mod.constNoReturn(scope, inst.base.src);
20302041 }
20312042
20322043 if (inst.positionals.cases.len == 0) {
20332044 // no cases just analyze else_branch
2034 try sema.body(scope.cast(Scope.Block).?, inst.positionals.else_body);
2045 try sema.analyzeBody(scope.cast(Scope.Block).?, inst.positionals.else_body);
20352046 return mod.constNoReturn(scope, inst.base.src);
20362047 }
20372048
20382049 try sema.requireRuntimeBlock(parent_block, inst.base.src);
2039 const cases = try parent_block.arena.alloc(Inst.SwitchBr.Case, inst.positionals.cases.len);
2050 const cases = try sema.arena.alloc(Inst.SwitchBr.Case, inst.positionals.cases.len);
20402051
20412052 var case_block: Scope.Block = .{
20422053 .parent = parent_block,
......@@ -2045,7 +2056,7 @@ fn zirSwitchBr(
20452056 .owner_decl = parent_block.owner_decl,
20462057 .src_decl = parent_block.src_decl,
20472058 .instructions = .{},
2048 .arena = parent_block.arena,
2059 .arena = sema.arena,
20492060 .inlining = parent_block.inlining,
20502061 .is_comptime = parent_block.is_comptime,
20512062 .branch_quota = parent_block.branch_quota,
......@@ -2060,19 +2071,19 @@ fn zirSwitchBr(
20602071 const casted = try sema.coerce(scope, target.ty, resolved);
20612072 const item = try sema.resolveConstValue(parent_block, case_src, casted);
20622073
2063 try sema.body(&case_block, case.body);
2074 try sema.analyzeBody(&case_block, case.body);
20642075
20652076 cases[i] = .{
20662077 .item = item,
2067 .body = .{ .instructions = try parent_block.arena.dupe(*Inst, case_block.instructions.items) },
2078 .body = .{ .instructions = try sema.arena.dupe(*Inst, case_block.instructions.items) },
20682079 };
20692080 }
20702081
20712082 case_block.instructions.items.len = 0;
2072 try sema.body(&case_block, inst.positionals.else_body);
2083 try sema.analyzeBody(&case_block, inst.positionals.else_body);
20732084
20742085 const else_body: ir.Body = .{
2075 .instructions = try parent_block.arena.dupe(*Inst, case_block.instructions.items),
2086 .instructions = try sema.arena.dupe(*Inst, case_block.instructions.items),
20762087 };
20772088
20782089 return mod.addSwitchBr(parent_block, inst.base.src, target, cases, else_body);
......@@ -2232,7 +2243,7 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!
22322243 return sema.mod.fail(&block.base, src, "unable to open '{s}': {s}", .{ operand, @errorName(err) });
22332244 },
22342245 };
2235 return sema.mod.constType(block.arena, src, file_scope.root_container.ty);
2246 return sema.mod.constType(sema.arena, src, file_scope.root_container.ty);
22362247}
22372248
22382249fn zirShl(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -2412,14 +2423,14 @@ fn analyzeInstComptimeOp(sema: *Sema, block: *Scope.Block, res_type: Type, inst:
24122423 const value = switch (inst.base.tag) {
24132424 .add => blk: {
24142425 const val = if (is_int)
2415 try Module.intAdd(block.arena, lhs_val, rhs_val)
2426 try Module.intAdd(sema.arena, lhs_val, rhs_val)
24162427 else
24172428 try mod.floatAdd(scope, res_type, inst.base.src, lhs_val, rhs_val);
24182429 break :blk val;
24192430 },
24202431 .sub => blk: {
24212432 const val = if (is_int)
2422 try Module.intSub(block.arena, lhs_val, rhs_val)
2433 try Module.intSub(sema.arena, lhs_val, rhs_val)
24232434 else
24242435 try mod.floatSub(scope, res_type, inst.base.src, lhs_val, rhs_val);
24252436 break :blk val;
......@@ -2435,7 +2446,7 @@ fn analyzeInstComptimeOp(sema: *Sema, block: *Scope.Block, res_type: Type, inst:
24352446 });
24362447}
24372448
2438fn zirDeref(sema: *Sema, block: *Scope.Block, deref: zir.Inst.Index) InnerError!*Inst {
2449fn zirDerefNode(sema: *Sema, block: *Scope.Block, deref: zir.Inst.Index) InnerError!*Inst {
24392450 const tracy = trace(@src());
24402451 defer tracy.end();
24412452
......@@ -2473,9 +2484,9 @@ fn zirAsm(
24732484 };
24742485 } else null;
24752486
2476 const args = try block.arena.alloc(*Inst, extra.data.args.len);
2477 const inputs = try block.arena.alloc([]const u8, extra.data.args_len);
2478 const clobbers = try block.arena.alloc([]const u8, extra.data.clobbers_len);
2487 const args = try sema.arena.alloc(*Inst, extra.data.args.len);
2488 const inputs = try sema.arena.alloc([]const u8, extra.data.args_len);
2489 const clobbers = try sema.arena.alloc([]const u8, extra.data.clobbers_len);
24792490
24802491 for (args) |*arg| {
24812492 const uncasted = sema.resolveInst(block, sema.code.extra[extra_i]);
......@@ -2492,7 +2503,7 @@ fn zirAsm(
24922503 }
24932504
24942505 try sema.requireRuntimeBlock(block, src);
2495 const inst = try block.arena.create(Inst.Assembly);
2506 const inst = try sema.arena.create(Inst.Assembly);
24962507 inst.* = .{
24972508 .base = .{
24982509 .tag = .assembly,
......@@ -2532,7 +2543,7 @@ fn zirCmp(
25322543 const rhs_ty_tag = rhs.ty.zigTypeTag();
25332544 if (is_equality_cmp and lhs_ty_tag == .Null and rhs_ty_tag == .Null) {
25342545 // null == null, null != null
2535 return mod.constBool(block.arena, inst.base.src, op == .eq);
2546 return mod.constBool(sema.arena, inst.base.src, op == .eq);
25362547 } else if (is_equality_cmp and
25372548 ((lhs_ty_tag == .Null and rhs_ty_tag == .Optional) or
25382549 rhs_ty_tag == .Null and lhs_ty_tag == .Optional))
......@@ -2559,7 +2570,7 @@ fn zirCmp(
25592570 if (rhs.value()) |rval| {
25602571 if (lhs.value()) |lval| {
25612572 // TODO optimisation oppurtunity: evaluate if std.mem.eql is faster with the names, or calling to Module.getErrorValue to get the values and then compare them is faster
2562 return mod.constBool(block.arena, inst.base.src, std.mem.eql(u8, lval.castTag(.@"error").?.data.name, rval.castTag(.@"error").?.data.name) == (op == .eq));
2573 return mod.constBool(sema.arena, inst.base.src, std.mem.eql(u8, lval.castTag(.@"error").?.data.name, rval.castTag(.@"error").?.data.name) == (op == .eq));
25632574 }
25642575 }
25652576 try sema.requireRuntimeBlock(block, inst.base.src);
......@@ -2573,7 +2584,7 @@ fn zirCmp(
25732584 if (!is_equality_cmp) {
25742585 return sema.mod.fail(&block.base, inst.base.src, "{s} operator not allowed for types", .{@tagName(op)});
25752586 }
2576 return mod.constBool(block.arena, inst.base.src, lhs.value().?.eql(rhs.value().?) == (op == .eq));
2587 return mod.constBool(sema.arena, inst.base.src, lhs.value().?.eql(rhs.value().?) == (op == .eq));
25772588 }
25782589 return sema.mod.fail(&block.base, inst.base.src, "TODO implement more cmp analysis", .{});
25792590}
......@@ -2584,7 +2595,7 @@ fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!
25842595
25852596 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
25862597 const operand = sema.resolveInst(block, inst_data.operand);
2587 return sema.mod.constType(block.arena, inst_data.src(), operand.ty);
2598 return sema.mod.constType(sema.arena, inst_data.src(), operand.ty);
25882599}
25892600
25902601fn zirTypeofPeer(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -2607,7 +2618,7 @@ fn zirTypeofPeer(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr
26072618 }
26082619
26092620 const result_type = try sema.resolvePeerTypes(block, inst_list, src_list);
2610 return sema.mod.constType(block.arena, src, result_type);
2621 return sema.mod.constType(sema.arena, src, result_type);
26112622}
26122623
26132624fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -2621,7 +2632,7 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
26212632 const bool_type = Type.initTag(.bool);
26222633 const operand = try sema.coerce(scope, bool_type, uncasted_operand);
26232634 if (try mod.resolveDefinedValue(scope, operand)) |val| {
2624 return mod.constBool(block.arena, src, !val.toBool());
2635 return mod.constBool(sema.arena, src, !val.toBool());
26252636 }
26262637 try sema.requireRuntimeBlock(block, src);
26272638 return block.addUnOp(src, bool_type, .not, operand);
......@@ -2646,9 +2657,9 @@ fn zirBoolOp(
26462657 if (lhs.value()) |lhs_val| {
26472658 if (rhs.value()) |rhs_val| {
26482659 if (is_bool_or) {
2649 return mod.constBool(block.arena, inst.base.src, lhs_val.toBool() or rhs_val.toBool());
2660 return mod.constBool(sema.arena, inst.base.src, lhs_val.toBool() or rhs_val.toBool());
26502661 } else {
2651 return mod.constBool(block.arena, inst.base.src, lhs_val.toBool() and rhs_val.toBool());
2662 return mod.constBool(sema.arena, inst.base.src, lhs_val.toBool() and rhs_val.toBool());
26522663 }
26532664 }
26542665 }
......@@ -2717,7 +2728,7 @@ fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) Inne
27172728
27182729 if (try mod.resolveDefinedValue(scope, cond)) |cond_val| {
27192730 const body = if (cond_val.toBool()) &inst.positionals.then_body else &inst.positionals.else_body;
2720 try sema.body(parent_block, body.*);
2731 try sema.analyzeBody(parent_block, body.*);
27212732 return mod.constNoReturn(scope, inst.base.src);
27222733 }
27232734
......@@ -2728,13 +2739,13 @@ fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) Inne
27282739 .owner_decl = parent_block.owner_decl,
27292740 .src_decl = parent_block.src_decl,
27302741 .instructions = .{},
2731 .arena = parent_block.arena,
2742 .arena = sema.arena,
27322743 .inlining = parent_block.inlining,
27332744 .is_comptime = parent_block.is_comptime,
27342745 .branch_quota = parent_block.branch_quota,
27352746 };
27362747 defer true_block.instructions.deinit(mod.gpa);
2737 try sema.body(&true_block, inst.positionals.then_body);
2748 try sema.analyzeBody(&true_block, inst.positionals.then_body);
27382749
27392750 var false_block: Scope.Block = .{
27402751 .parent = parent_block,
......@@ -2743,16 +2754,16 @@ fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) Inne
27432754 .owner_decl = parent_block.owner_decl,
27442755 .src_decl = parent_block.src_decl,
27452756 .instructions = .{},
2746 .arena = parent_block.arena,
2757 .arena = sema.arena,
27472758 .inlining = parent_block.inlining,
27482759 .is_comptime = parent_block.is_comptime,
27492760 .branch_quota = parent_block.branch_quota,
27502761 };
27512762 defer false_block.instructions.deinit(mod.gpa);
2752 try sema.body(&false_block, inst.positionals.else_body);
2763 try sema.analyzeBody(&false_block, inst.positionals.else_body);
27532764
2754 const then_body: ir.Body = .{ .instructions = try block.arena.dupe(*Inst, true_block.instructions.items) };
2755 const else_body: ir.Body = .{ .instructions = try block.arena.dupe(*Inst, false_block.instructions.items) };
2765 const then_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, true_block.instructions.items) };
2766 const else_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, false_block.instructions.items) };
27562767 return mod.addCondBr(parent_block, inst.base.src, cond, then_body, else_body);
27572768}
27582769
......@@ -2797,7 +2808,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inne
27972808 const inst_data = sema.code.instructions.items(.data)[inst].ptr_type_simple;
27982809 const elem_type = try sema.resolveType(block, .unneeded, inst_data.elem_type);
27992810 const ty = try sema.mod.ptrType(
2800 block.arena,
2811 sema.arena,
28012812 elem_type,
28022813 null,
28032814 0,
......@@ -2808,7 +2819,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inne
28082819 inst_data.is_volatile,
28092820 inst_data.size,
28102821 );
2811 return sema.mod.constType(block.arena, .unneeded, ty);
2822 return sema.mod.constType(sema.arena, .unneeded, ty);
28122823}
28132824
28142825fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
......@@ -2861,7 +2872,17 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
28612872 inst_data.flags.is_volatile,
28622873 inst_data.size,
28632874 );
2864 return sema.mod.constType(block.arena, .unneeded, ty);
2875 return sema.mod.constType(sema.arena, .unneeded, ty);
2876}
2877
2878fn zirAwait(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
2879 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2880 return sema.mod.fail(&block.base, inst_data.src(), "TODO implement Sema await", .{});
2881}
2882
2883fn zirNosuspendAwait(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
2884 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2885 return sema.mod.fail(&block.base, inst_data.src(), "TODO implement Sema nosuspend_await", .{});
28652886}
28662887
28672888fn requireFunctionBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {
......@@ -2877,7 +2898,7 @@ fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void
28772898 }
28782899}
28792900
2880fn validateVarType(sema: *Module, block: *Scope.Block, src: LazySrcLoc, ty: Type) !void {
2901fn validateVarType(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) !void {
28812902 if (!ty.isValidVarType(false)) {
28822903 return mod.fail(&block.base, src, "variable of type '{}' must be const or comptime", .{ty});
28832904 }
......@@ -2890,7 +2911,7 @@ pub const PanicId = enum {
28902911};
28912912
28922913fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id: PanicId) !void {
2893 const block_inst = try parent_block.arena.create(Inst.Block);
2914 const block_inst = try sema.arena.create(Inst.Block);
28942915 block_inst.* = .{
28952916 .base = .{
28962917 .tag = Inst.Block.base_tag,
......@@ -2898,14 +2919,14 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id:
28982919 .src = ok.src,
28992920 },
29002921 .body = .{
2901 .instructions = try parent_block.arena.alloc(*Inst, 1), // Only need space for the condbr.
2922 .instructions = try sema.arena.alloc(*Inst, 1), // Only need space for the condbr.
29022923 },
29032924 };
29042925
29052926 const ok_body: ir.Body = .{
2906 .instructions = try parent_block.arena.alloc(*Inst, 1), // Only need space for the br_void.
2927 .instructions = try sema.arena.alloc(*Inst, 1), // Only need space for the br_void.
29072928 };
2908 const br_void = try parent_block.arena.create(Inst.BrVoid);
2929 const br_void = try sema.arena.create(Inst.BrVoid);
29092930 br_void.* = .{
29102931 .base = .{
29112932 .tag = .br_void,
......@@ -2923,7 +2944,7 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id:
29232944 .owner_decl = parent_block.owner_decl,
29242945 .src_decl = parent_block.src_decl,
29252946 .instructions = .{},
2926 .arena = parent_block.arena,
2947 .arena = sema.arena,
29272948 .inlining = parent_block.inlining,
29282949 .is_comptime = parent_block.is_comptime,
29292950 .branch_quota = parent_block.branch_quota,
......@@ -2933,9 +2954,9 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id:
29332954
29342955 _ = try mod.safetyPanic(&fail_block, ok.src, panic_id);
29352956
2936 const fail_body: ir.Body = .{ .instructions = try parent_block.arena.dupe(*Inst, fail_block.instructions.items) };
2957 const fail_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, fail_block.instructions.items) };
29372958
2938 const condbr = try parent_block.arena.create(Inst.CondBr);
2959 const condbr = try sema.arena.create(Inst.CondBr);
29392960 condbr.* = .{
29402961 .base = .{
29412962 .tag = .condbr,
......@@ -3296,7 +3317,7 @@ fn storePtr(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ptr: *Inst, uncas
32963317 const elem_ty = ptr.ty.elemType();
32973318 const value = try sema.coerce(scope, elem_ty, uncasted_value);
32983319 if (elem_ty.onePossibleValue() != null)
3299 return sema.mod.constVoid(block.arena, .unneeded);
3320 return sema.mod.constVoid(sema.arena, .unneeded);
33003321
33013322 // TODO handle comptime pointer writes
33023323 // TODO handle if the element type requires comptime
......@@ -3438,7 +3459,7 @@ fn analyzeIsNull(
34383459 if (operand.value()) |opt_val| {
34393460 const is_null = opt_val.isNull();
34403461 const bool_value = if (invert_logic) !is_null else is_null;
3441 return mod.constBool(block.arena, src, bool_value);
3462 return mod.constBool(sema.arena, src, bool_value);
34423463 }
34433464 try sema.requireRuntimeBlock(block, src);
34443465 const inst_tag: Inst.Tag = if (invert_logic) .is_non_null else .is_null;
......@@ -3447,11 +3468,11 @@ fn analyzeIsNull(
34473468
34483469fn analyzeIsErr(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, operand: *Inst) InnerError!*Inst {
34493470 const ot = operand.ty.zigTypeTag();
3450 if (ot != .ErrorSet and ot != .ErrorUnion) return mod.constBool(block.arena, src, false);
3451 if (ot == .ErrorSet) return mod.constBool(block.arena, src, true);
3471 if (ot != .ErrorSet and ot != .ErrorUnion) return mod.constBool(sema.arena, src, false);
3472 if (ot == .ErrorSet) return mod.constBool(sema.arena, src, true);
34523473 assert(ot == .ErrorUnion);
34533474 if (operand.value()) |err_union| {
3454 return mod.constBool(block.arena, src, err_union.getError() != null);
3475 return mod.constBool(sema.arena, src, err_union.getError() != null);
34553476 }
34563477 try sema.requireRuntimeBlock(block, src);
34573478 return mod.addUnOp(b, src, Type.initTag(.bool), .is_err, operand);
......@@ -3616,7 +3637,7 @@ fn cmpNumeric(
36163637
36173638 if (lhs.value()) |lhs_val| {
36183639 if (rhs.value()) |rhs_val| {
3619 return mod.constBool(block.arena, src, Value.compare(lhs_val, op, rhs_val));
3640 return mod.constBool(sema.arena, src, Value.compare(lhs_val, op, rhs_val));
36203641 }
36213642 }
36223643
......@@ -3684,8 +3705,8 @@ fn cmpNumeric(
36843705 const zcmp = lhs_val.orderAgainstZero();
36853706 if (lhs_val.floatHasFraction()) {
36863707 switch (op) {
3687 .eq => return mod.constBool(block.arena, src, false),
3688 .neq => return mod.constBool(block.arena, src, true),
3708 .eq => return mod.constBool(sema.arena, src, false),
3709 .neq => return mod.constBool(sema.arena, src, true),
36893710 else => {},
36903711 }
36913712 if (zcmp == .lt) {
......@@ -3719,8 +3740,8 @@ fn cmpNumeric(
37193740 const zcmp = rhs_val.orderAgainstZero();
37203741 if (rhs_val.floatHasFraction()) {
37213742 switch (op) {
3722 .eq => return mod.constBool(block.arena, src, false),
3723 .neq => return mod.constBool(block.arena, src, true),
3743 .eq => return mod.constBool(sema.arena, src, false),
3744 .neq => return mod.constBool(sema.arena, src, true),
37243745 else => {},
37253746 }
37263747 if (zcmp == .lt) {
src/ir.zig+1-2
......@@ -25,8 +25,7 @@ pub const Inst = struct {
2525 /// lifetimes of operands are encoded elsewhere.
2626 deaths: DeathsInt = undefined,
2727 ty: Type,
28 /// Byte offset into the source.
29 src: usize,
28 src: Module.LazySrcLoc,
3029
3130 pub const DeathsInt = u16;
3231 pub const DeathsBitIndex = std.math.Log2Int(DeathsInt);
src/zir.zig+37-39
......@@ -12,6 +12,7 @@ const TypedValue = @import("TypedValue.zig");
1212const ir = @import("ir.zig");
1313const Module = @import("Module.zig");
1414const ast = std.zig.ast;
15const LazySrcLoc = Module.LazySrcLoc;
1516
1617/// The minimum amount of information needed to represent a list of ZIR instructions.
1718/// Once this structure is completed, it can be used to generate TZIR, followed by
......@@ -749,39 +750,39 @@ pub const Inst = struct {
749750 /// Suspend an async function. The suspend block has any number of statements in it.
750751 /// Uses the `block` union field.
751752 suspend_block,
752 /// A switch expression.
753 /// lhs is target, SwitchBr[rhs]
754 /// All prongs of target handled.
755 switch_br,
756 /// Same as switch_br, except has a range field.
757 switch_br_range,
758 /// Same as switch_br, except has an else prong.
759 switch_br_else,
760 /// Same as switch_br_else, except has a range field.
761 switch_br_else_range,
762 /// Same as switch_br, except has an underscore prong.
763 switch_br_underscore,
764 /// Same as switch_br, except has a range field.
765 switch_br_underscore_range,
766 /// Same as `switch_br` but the target is a pointer to the value being switched on.
767 switch_br_ref,
768 /// Same as `switch_br_range` but the target is a pointer to the value being switched on.
769 switch_br_ref_range,
770 /// Same as `switch_br_else` but the target is a pointer to the value being switched on.
771 switch_br_ref_else,
772 /// Same as `switch_br_else_range` but the target is a pointer to the
773 /// value being switched on.
774 switch_br_ref_else_range,
775 /// Same as `switch_br_underscore` but the target is a pointer to the value
776 /// being switched on.
777 switch_br_ref_underscore,
778 /// Same as `switch_br_underscore_range` but the target is a pointer to
779 /// the value being switched on.
780 switch_br_ref_underscore_range,
781 /// A range in a switch case, `lhs...rhs`.
782 /// Only checks that `lhs >= rhs` if they are ints, everything else is
783 /// validated by the switch_br instruction.
784 switch_range,
753 // /// A switch expression.
754 // /// lhs is target, SwitchBr[rhs]
755 // /// All prongs of target handled.
756 // switch_br,
757 // /// Same as switch_br, except has a range field.
758 // switch_br_range,
759 // /// Same as switch_br, except has an else prong.
760 // switch_br_else,
761 // /// Same as switch_br_else, except has a range field.
762 // switch_br_else_range,
763 // /// Same as switch_br, except has an underscore prong.
764 // switch_br_underscore,
765 // /// Same as switch_br, except has a range field.
766 // switch_br_underscore_range,
767 // /// Same as `switch_br` but the target is a pointer to the value being switched on.
768 // switch_br_ref,
769 // /// Same as `switch_br_range` but the target is a pointer to the value being switched on.
770 // switch_br_ref_range,
771 // /// Same as `switch_br_else` but the target is a pointer to the value being switched on.
772 // switch_br_ref_else,
773 // /// Same as `switch_br_else_range` but the target is a pointer to the
774 // /// value being switched on.
775 // switch_br_ref_else_range,
776 // /// Same as `switch_br_underscore` but the target is a pointer to the value
777 // /// being switched on.
778 // switch_br_ref_underscore,
779 // /// Same as `switch_br_underscore_range` but the target is a pointer to
780 // /// the value being switched on.
781 // switch_br_ref_underscore_range,
782 // /// A range in a switch case, `lhs...rhs`.
783 // /// Only checks that `lhs >= rhs` if they are ints, everything else is
784 // /// validated by the switch_br instruction.
785 // switch_range,
785786
786787 comptime {
787788 assert(@sizeOf(Tag) == 1);
......@@ -915,7 +916,6 @@ pub const Inst = struct {
915916 .resolve_inferred_alloc,
916917 .set_eval_branch_quota,
917918 .compile_log,
918 .void_value,
919919 .switch_range,
920920 .@"resume",
921921 .@"await",
......@@ -934,8 +934,6 @@ pub const Inst = struct {
934934 .container_field_named,
935935 .container_field_typed,
936936 .container_field,
937 .switch_br,
938 .switch_br_ref,
939937 .@"suspend",
940938 .suspend_block,
941939 => true,
......@@ -967,7 +965,7 @@ pub const Inst = struct {
967965 /// The meaning of this operand depends on the corresponding `Tag`.
968966 operand: Ref,
969967
970 fn src(self: @This()) LazySrcLoc {
968 pub fn src(self: @This()) LazySrcLoc {
971969 return .{ .node_offset = self.src_node };
972970 }
973971 },
......@@ -978,7 +976,7 @@ pub const Inst = struct {
978976 /// The meaning of this operand depends on the corresponding `Tag`.
979977 operand: Ref,
980978
981 fn src(self: @This()) LazySrcLoc {
979 pub fn src(self: @This()) LazySrcLoc {
982980 return .{ .token_offset = self.src_tok };
983981 }
984982 },
......@@ -990,7 +988,7 @@ pub const Inst = struct {
990988 /// `Tag` determines what lives there.
991989 payload_index: u32,
992990
993 fn src(self: @This()) LazySrcLoc {
991 pub fn src(self: @This()) LazySrcLoc {
994992 return .{ .node_offset = self.src_node };
995993 }
996994 },