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:...@@ -11,6 +11,8 @@ Merge TODO list:
11 * update astgen.zig11 * update astgen.zig
12 * finish updating Sema.zig12 * finish updating Sema.zig
13 * finish implementing SrcLoc byteOffset function13 * finish implementing SrcLoc byteOffset function
14 * audit Module.zig for use of token_starts - it should only be when
15 resolving LazySrcLoc
1416
1517
16Performance optimizations to look into:18Performance optimizations to look into:
src/Module.zig+156-93
...@@ -389,6 +389,7 @@ pub const Scope = struct {...@@ -389,6 +389,7 @@ pub const Scope = struct {
389 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.arena,389 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.arena,
390 .file => unreachable,390 .file => unreachable,
391 .container => unreachable,391 .container => unreachable,
392 .decl_ref => unreachable,
392 }393 }
393 }394 }
394395
...@@ -406,6 +407,7 @@ pub const Scope = struct {...@@ -406,6 +407,7 @@ pub const Scope = struct {
406 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.decl,407 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.decl,
407 .file => null,408 .file => null,
408 .container => null,409 .container => null,
410 .decl_ref => scope.cast(DeclRef).?.decl,
409 };411 };
410 }412 }
411413
...@@ -419,6 +421,7 @@ pub const Scope = struct {...@@ -419,6 +421,7 @@ pub const Scope = struct {
419 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.decl,421 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.decl,
420 .file => null,422 .file => null,
421 .container => null,423 .container => null,
424 .decl_ref => scope.cast(DeclRef).?.decl,
422 };425 };
423 }426 }
424427
...@@ -433,6 +436,7 @@ pub const Scope = struct {...@@ -433,6 +436,7 @@ pub const Scope = struct {
433 .container => return scope.cast(Container).?,436 .container => return scope.cast(Container).?,
434 .gen_suspend => return scope.cast(GenZir).?.zir_code.decl.container,437 .gen_suspend => return scope.cast(GenZir).?.zir_code.decl.container,
435 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.zir_code.decl.container,438 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir.zir_code.decl.container,
439 .decl_ref => return scope.cast(DeclRef).?.decl.container,
436 }440 }
437 }441 }
438442
...@@ -449,6 +453,7 @@ pub const Scope = struct {...@@ -449,6 +453,7 @@ pub const Scope = struct {
449 .gen_nosuspend => unreachable,453 .gen_nosuspend => unreachable,
450 .file => unreachable,454 .file => unreachable,
451 .container => return scope.cast(Container).?.fullyQualifiedNameHash(name),455 .container => return scope.cast(Container).?.fullyQualifiedNameHash(name),
456 .decl_ref => unreachable,
452 }457 }
453 }458 }
454459
...@@ -463,6 +468,7 @@ pub const Scope = struct {...@@ -463,6 +468,7 @@ pub const Scope = struct {
463 .container => return &scope.cast(Container).?.file_scope.tree,468 .container => return &scope.cast(Container).?.file_scope.tree,
464 .gen_suspend => return &scope.cast(GenZir).?.decl.container.file_scope.tree,469 .gen_suspend => return &scope.cast(GenZir).?.decl.container.file_scope.tree,
465 .gen_nosuspend => return &scope.cast(Nosuspend).?.gen_zir.decl.container.file_scope.tree,470 .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,
466 }472 }
467 }473 }
468474
...@@ -476,6 +482,7 @@ pub const Scope = struct {...@@ -476,6 +482,7 @@ pub const Scope = struct {
476 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir,482 .gen_nosuspend => return scope.cast(Nosuspend).?.gen_zir,
477 .file => unreachable,483 .file => unreachable,
478 .container => unreachable,484 .container => unreachable,
485 .decl_ref => unreachable,
479 };486 };
480 }487 }
481488
...@@ -491,6 +498,7 @@ pub const Scope = struct {...@@ -491,6 +498,7 @@ pub const Scope = struct {
491 .local_ptr => unreachable,498 .local_ptr => unreachable,
492 .gen_suspend => unreachable,499 .gen_suspend => unreachable,
493 .gen_nosuspend => unreachable,500 .gen_nosuspend => unreachable,
501 .decl_ref => unreachable,
494 }502 }
495 }503 }
496504
...@@ -504,6 +512,7 @@ pub const Scope = struct {...@@ -504,6 +512,7 @@ pub const Scope = struct {
504 .block => unreachable,512 .block => unreachable,
505 .gen_suspend => unreachable,513 .gen_suspend => unreachable,
506 .gen_nosuspend => unreachable,514 .gen_nosuspend => unreachable,
515 .decl_ref => unreachable,
507 }516 }
508 }517 }
509518
...@@ -520,6 +529,7 @@ pub const Scope = struct {...@@ -520,6 +529,7 @@ pub const Scope = struct {
520 .block => return @fieldParentPtr(Block, "base", cur).src_decl.container.file_scope,529 .block => return @fieldParentPtr(Block, "base", cur).src_decl.container.file_scope,
521 .gen_suspend => @fieldParentPtr(GenZir, "base", cur).parent,530 .gen_suspend => @fieldParentPtr(GenZir, "base", cur).parent,
522 .gen_nosuspend => @fieldParentPtr(Nosuspend, "base", cur).parent,531 .gen_nosuspend => @fieldParentPtr(Nosuspend, "base", cur).parent,
532 .decl_ref => @fieldParentPtr(DeclRef, "base", cur).decl.container.file_scope,
523 };533 };
524 }534 }
525 }535 }
...@@ -571,6 +581,10 @@ pub const Scope = struct {...@@ -571,6 +581,10 @@ pub const Scope = struct {
571 local_ptr,581 local_ptr,
572 gen_suspend,582 gen_suspend,
573 gen_nosuspend,583 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,
574 };588 };
575589
576 pub const Container = struct {590 pub const Container = struct {
...@@ -1077,6 +1091,12 @@ pub const Scope = struct {...@@ -1077,6 +1091,12 @@ pub const Scope = struct {
1077 gen_zir: *GenZir,1091 gen_zir: *GenZir,
1078 src: LazySrcLoc,1092 src: LazySrcLoc,
1079 };1093 };
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 };
1080};1100};
10811101
1082/// A Work-In-Progress `zir.Code`. This is a shared parent of all1102/// A Work-In-Progress `zir.Code`. This is a shared parent of all
...@@ -1302,6 +1322,35 @@ pub const LazySrcLoc = union(enum) {...@@ -1302,6 +1322,35 @@ pub const LazySrcLoc = union(enum) {
1302 /// to the sentinel expression.1322 /// to the sentinel expression.
1303 /// The Decl is determined contextually.1323 /// The Decl is determined contextually.
1304 node_offset_slice_sentinel: u32,1324 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 }
1305};1354};
13061355
1307pub const InnerError = error{ OutOfMemory, AnalysisFail };1356pub const InnerError = error{ OutOfMemory, AnalysisFail };
...@@ -1534,24 +1583,27 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {...@@ -1534,24 +1583,27 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
15341583
1535 var sema: Sema = .{1584 var sema: Sema = .{
1536 .mod = mod,1585 .mod = mod,
1586 .gpa = mod.gpa,
1587 .arena = &analysis_arena.allocator,
1537 .code = code,1588 .code = code,
1538 .inst_map = try mod.gpa.alloc(*ir.Inst, code.instructions.len),1589 .inst_map = try mod.gpa.alloc(*ir.Inst, code.instructions.len),
1590 .owner_decl = decl,
1591 .func = null,
1592 .param_inst_list = &.{},
1539 };1593 };
1540 defer mod.gpa.free(sema.inst_map);1594 defer mod.gpa.free(sema.inst_map);
15411595
1542 var block_scope: Scope.Block = .{1596 var block_scope: Scope.Block = .{
1543 .parent = null,1597 .parent = null,
1544 .func = null,1598 .sema = &sema,
1545 .owner_decl = decl,
1546 .src_decl = decl,1599 .src_decl = decl,
1547 .instructions = .{},1600 .instructions = .{},
1548 .arena = &analysis_arena.allocator,
1549 .inlining = null,1601 .inlining = null,
1550 .is_comptime = true,1602 .is_comptime = true,
1551 };1603 };
1552 defer block_scope.instructions.deinit(mod.gpa);1604 defer block_scope.instructions.deinit(mod.gpa);
15531605
1554 try sema.root(mod, &block_scope);1606 try sema.root(&block_scope);
15551607
1556 decl.analysis = .complete;1608 decl.analysis = .complete;
1557 decl.generation = mod.generation;1609 decl.generation = mod.generation;
...@@ -1753,19 +1805,21 @@ fn astgenAndSemaFn(...@@ -1753,19 +1805,21 @@ fn astgenAndSemaFn(
1753 const fn_type_code = fn_type_wip_zir_exec.finish();1805 const fn_type_code = fn_type_wip_zir_exec.finish();
1754 var fn_type_sema: Sema = .{1806 var fn_type_sema: Sema = .{
1755 .mod = mod,1807 .mod = mod,
1808 .gpa = mod.gpa,
1809 .arena = &decl_arena.allocator,
1756 .code = fn_type_code,1810 .code = fn_type_code,
1757 .inst_map = try mod.gpa.alloc(*ir.Inst, fn_type_code.instructions.len),1811 .inst_map = try mod.gpa.alloc(*ir.Inst, fn_type_code.instructions.len),
1812 .owner_decl = decl,
1813 .func = null,
1814 .param_inst_list = &.{},
1758 };1815 };
1759 defer mod.gpa.free(fn_type_sema.inst_map);1816 defer mod.gpa.free(fn_type_sema.inst_map);
17601817
1761 var block_scope: Scope.Block = .{1818 var block_scope: Scope.Block = .{
1762 .parent = null,1819 .parent = null,
1763 .sema = &fn_type_sema,1820 .sema = &fn_type_sema,
1764 .func = null,
1765 .owner_decl = decl,
1766 .src_decl = decl,1821 .src_decl = decl,
1767 .instructions = .{},1822 .instructions = .{},
1768 .arena = &decl_arena.allocator,
1769 .inlining = null,1823 .inlining = null,
1770 .is_comptime = false,1824 .is_comptime = false,
1771 };1825 };
...@@ -1959,8 +2013,8 @@ fn astgenAndSemaVarDecl(...@@ -1959,8 +2013,8 @@ fn astgenAndSemaVarDecl(
1959 defer tracy.end();2013 defer tracy.end();
19602014
1961 decl.analysis = .in_progress;2015 decl.analysis = .in_progress;
2016 decl.is_pub = var_decl.visib_token != null;
19622017
1963 const token_starts = tree.tokens.items(.start);
1964 const token_tags = tree.tokens.items(.tag);2018 const token_tags = tree.tokens.items(.tag);
19652019
1966 // We need the memory for the Type to go into the arena for the Decl2020 // We need the memory for the Type to go into the arena for the Decl
...@@ -1968,54 +2022,29 @@ fn astgenAndSemaVarDecl(...@@ -1968,54 +2022,29 @@ fn astgenAndSemaVarDecl(
1968 errdefer decl_arena.deinit();2022 errdefer decl_arena.deinit();
1969 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);2023 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);2025 // Used for simple error reporting.
1972 defer decl_inst_table.deinit();2026 var decl_scope: Scope.DeclRef = .{ .decl = decl };
1973
1974 var branch_quota: u32 = default_eval_branch_quota;
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;
1991 const is_extern = blk: {2028 const is_extern = blk: {
1992 const maybe_extern_token = var_decl.extern_export_token orelse break :blk false;2029 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;2030 break :blk token_tags[maybe_extern_token] == .keyword_extern;
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;
2003 };2031 };
2032
2004 if (var_decl.lib_name) |lib_name| {2033 if (var_decl.lib_name) |lib_name| {
2005 assert(is_extern);2034 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", .{});
2007 }2036 }
2008 const is_mutable = token_tags[var_decl.ast.mut_token] == .keyword_var;2037 const is_mutable = token_tags[var_decl.ast.mut_token] == .keyword_var;
2009 const is_threadlocal = if (var_decl.threadlocal_token) |some| blk: {2038 const is_threadlocal = if (var_decl.threadlocal_token) |some| blk: {
2010 if (!is_mutable) {2039 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", .{});
2012 }2041 }
2013 break :blk true;2042 break :blk true;
2014 } else false;2043 } else false;
2015 assert(var_decl.comptime_token == null);2044 assert(var_decl.comptime_token == null);
2016 if (var_decl.ast.align_node != 0) {2045 if (var_decl.ast.align_node != 0) {
2017 return mod.failNode(2046 return mod.failNode(
2018 &block_scope.base,2047 &decl_scope.base,
2019 var_decl.ast.align_node,2048 var_decl.ast.align_node,
2020 "TODO implement function align expression",2049 "TODO implement function align expression",
2021 .{},2050 .{},
...@@ -2023,7 +2052,7 @@ fn astgenAndSemaVarDecl(...@@ -2023,7 +2052,7 @@ fn astgenAndSemaVarDecl(
2023 }2052 }
2024 if (var_decl.ast.section_node != 0) {2053 if (var_decl.ast.section_node != 0) {
2025 return mod.failNode(2054 return mod.failNode(
2026 &block_scope.base,2055 &decl_scope.base,
2027 var_decl.ast.section_node,2056 var_decl.ast.section_node,
2028 "TODO implement function section expression",2057 "TODO implement function section expression",
2029 .{},2058 .{},
...@@ -2031,25 +2060,36 @@ fn astgenAndSemaVarDecl(...@@ -2031,25 +2060,36 @@ fn astgenAndSemaVarDecl(
2031 }2060 }
20322061
2033 const var_info: struct { ty: Type, val: ?Value } = if (var_decl.ast.init_node != 0) vi: {2062 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
2034 var gen_scope_arena = std.heap.ArenaAllocator.init(mod.gpa);2072 var gen_scope_arena = std.heap.ArenaAllocator.init(mod.gpa);
2035 defer gen_scope_arena.deinit();2073 defer gen_scope_arena.deinit();
2036 var gen_scope: Scope.GenZir = .{2074
2075 var wip_zir_code: WipZirCode = .{
2037 .decl = decl,2076 .decl = decl,
2038 .arena = &gen_scope_arena.allocator,2077 .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 = .{
2040 .force_comptime = true,2083 .force_comptime = true,
2084 .parent = &decl.container.base,
2085 .zir_code = &wip_zir_code,
2041 };2086 };
2042 defer gen_scope.instructions.deinit(mod.gpa);2087 defer gen_scope.instructions.deinit(mod.gpa);
20432088
2044 const init_result_loc: astgen.ResultLoc = if (var_decl.ast.type_node != 0) rl: {2089 const init_result_loc: astgen.ResultLoc = if (var_decl.ast.type_node != 0) .{
2045 const type_node = var_decl.ast.type_node;2090 .ty = try astgen.expr(mod, &gen_scope.base, .{
2046 const src = token_starts[tree.firstToken(type_node)];2091 .ty = @enumToInt(zir.Const.type_type),
2047 const type_type = try astgen.addZIRInstConst(mod, &gen_scope.base, src, .{2092 }, var_decl.ast.type_node),
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 };
2053 } else .none;2093 } else .none;
20542094
2055 const init_inst = try astgen.comptimeExpr(2095 const init_inst = try astgen.comptimeExpr(
...@@ -2058,76 +2098,106 @@ fn astgenAndSemaVarDecl(...@@ -2058,76 +2098,106 @@ fn astgenAndSemaVarDecl(
2058 init_result_loc,2098 init_result_loc,
2059 var_decl.ast.init_node,2099 var_decl.ast.init_node,
2060 );2100 );
2101 const code = wip_zir_code.finish();
2061 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {2102 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 {};
2063 }2104 }
20642105
2065 var var_inst_table = Scope.Block.InstTable.init(mod.gpa);2106 var sema: Sema = .{
2066 defer var_inst_table.deinit();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;2118 var block_scope: Scope.Block = .{
2069 var inner_block: Scope.Block = .{
2070 .parent = null,2119 .parent = null,
2071 .inst_table = &var_inst_table,2120 .sema = &sema,
2072 .func = null,
2073 .owner_decl = decl,
2074 .src_decl = decl,2121 .src_decl = decl,
2075 .instructions = .{},2122 .instructions = .{},
2076 .arena = &gen_scope_arena.allocator,
2077 .inlining = null,2123 .inlining = null,
2078 .is_comptime = true,2124 .is_comptime = true,
2079 .branch_quota = &branch_quota_vi,
2080 };2125 };
2081 defer inner_block.instructions.deinit(mod.gpa);2126 defer block_scope.instructions.deinit(mod.gpa);
2082 try zir_sema.analyzeBody(mod, &inner_block, .{2127
2083 .instructions = gen_scope.instructions.items,2128 try sema.root(&block_scope);
2084 });
20852129
2086 // The result location guarantees the type coercion.2130 // 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);
2088 // The is_comptime in the Scope.Block guarantees the result is comptime-known.2132 // The is_comptime in the Scope.Block guarantees the result is comptime-known.
2089 const val = analyzed_init_inst.value().?;2133 const val = analyzed_init_inst.value().?;
20902134
2091 const ty = try analyzed_init_inst.ty.copy(block_scope.arena);
2092 break :vi .{2135 break :vi .{
2093 .ty = ty,2136 .ty = try analyzed_init_inst.ty.copy(decl_arena),
2094 .val = try val.copy(block_scope.arena),2137 .val = try val.copy(decl_arena),
2095 };2138 };
2096 } else if (!is_extern) {2139 } else if (!is_extern) {
2097 return mod.failTok(2140 return mod.failTok(
2098 &block_scope.base,2141 &decl_scope.base,
2099 var_decl.ast.mut_token,2142 var_decl.ast.mut_token,
2100 "variables must be initialized",2143 "variables must be initialized",
2101 .{},2144 .{},
2102 );2145 );
2103 } else if (var_decl.ast.type_node != 0) vi: {2146 } 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.
2106 var type_scope_arena = std.heap.ArenaAllocator.init(mod.gpa);2147 var type_scope_arena = std.heap.ArenaAllocator.init(mod.gpa);
2107 defer type_scope_arena.deinit();2148 defer type_scope_arena.deinit();
2108 var type_scope: Scope.GenZir = .{2149
2150 var wip_zir_code: WipZirCode = .{
2109 .decl = decl,2151 .decl = decl,
2110 .arena = &type_scope_arena.allocator,2152 .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 = .{
2112 .force_comptime = true,2158 .force_comptime = true,
2159 .parent = &decl.container.base,
2160 .zir_code = &wip_zir_code,
2113 };2161 };
2114 defer type_scope.instructions.deinit(mod.gpa);2162 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();
2117 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {2166 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 {};
2119 }2168 }
21202169
2121 const ty = try zir_sema.analyzeBodyValueAsType(mod, &block_scope, var_type, .{2170 var sema: Sema = .{
2122 .instructions = type_scope.instructions.items,2171 .mod = mod,
2123 });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
2124 break :vi .{2194 break :vi .{
2125 .ty = ty,2195 .ty = try ty.copy(decl_arena),
2126 .val = null,2196 .val = null,
2127 };2197 };
2128 } else {2198 } else {
2129 return mod.failTok(2199 return mod.failTok(
2130 &block_scope.base,2200 &decl_scope.base,
2131 var_decl.ast.mut_token,2201 var_decl.ast.mut_token,
2132 "unable to infer variable type",2202 "unable to infer variable type",
2133 .{},2203 .{},
...@@ -2136,7 +2206,7 @@ fn astgenAndSemaVarDecl(...@@ -2136,7 +2206,7 @@ fn astgenAndSemaVarDecl(
21362206
2137 if (is_mutable and !var_info.ty.isValidVarType(is_extern)) {2207 if (is_mutable and !var_info.ty.isValidVarType(is_extern)) {
2138 return mod.failTok(2208 return mod.failTok(
2139 &block_scope.base,2209 &decl_scope.base,
2140 var_decl.ast.mut_token,2210 var_decl.ast.mut_token,
2141 "variable of type '{}' must be const",2211 "variable of type '{}' must be const",
2142 .{var_info.ty},2212 .{var_info.ty},
...@@ -2179,7 +2249,7 @@ fn astgenAndSemaVarDecl(...@@ -2179,7 +2249,7 @@ fn astgenAndSemaVarDecl(
2179 const name_token = var_decl.ast.mut_token + 1;2249 const name_token = var_decl.ast.mut_token + 1;
2180 const name = tree.tokenSlice(name_token); // TODO identifierTokenString2250 const name = tree.tokenSlice(name_token); // TODO identifierTokenString
2181 // The scope needs to have the decl in it.2251 // 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);
2183 }2253 }
2184 }2254 }
2185 return type_changed;2255 return type_changed;
...@@ -2702,7 +2772,6 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn) !void {...@@ -2702,7 +2772,6 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn) !void {
2702 .sema = &sema,2772 .sema = &sema,
2703 .src_decl = decl,2773 .src_decl = decl,
2704 .instructions = .{},2774 .instructions = .{},
2705 .arena = &arena.allocator,
2706 .inlining = null,2775 .inlining = null,
2707 .is_comptime = false,2776 .is_comptime = false,
2708 };2777 };
...@@ -3101,10 +3170,7 @@ pub fn errNote(...@@ -3101,10 +3170,7 @@ pub fn errNote(
31013170
3102 parent.notes = try mod.gpa.realloc(parent.notes, parent.notes.len + 1);3171 parent.notes = try mod.gpa.realloc(parent.notes, parent.notes.len + 1);
3103 parent.notes[parent.notes.len - 1] = .{3172 parent.notes[parent.notes.len - 1] = .{
3104 .src_loc = .{3173 .src_loc = src.toSrcLoc(scope),
3105 .file_scope = scope.getFileScope(),
3106 .byte_offset = src,
3107 },
3108 .msg = msg,3174 .msg = msg,
3109 };3175 };
3110}3176}
...@@ -3116,10 +3182,7 @@ pub fn errMsg(...@@ -3116,10 +3182,7 @@ pub fn errMsg(
3116 comptime format: []const u8,3182 comptime format: []const u8,
3117 args: anytype,3183 args: anytype,
3118) error{OutOfMemory}!*ErrorMsg {3184) error{OutOfMemory}!*ErrorMsg {
3119 return ErrorMsg.create(mod.gpa, .{3185 return ErrorMsg.create(mod.gpa, src.toSrcLoc(scope), format, args);
3120 .decl = scope.srcDecl().?,
3121 .lazy = src,
3122 }, format, args);
3123}3186}
31243187
3125pub fn fail(3188pub fn fail(
src/Sema.zig+178-157
...@@ -71,27 +71,25 @@ const const_tzir_inst_list = blk: {...@@ -71,27 +71,25 @@ const const_tzir_inst_list = blk: {
7171
72pub fn root(sema: *Sema, root_block: *Scope.Block) !void {72pub fn root(sema: *Sema, root_block: *Scope.Block) !void {
73 const root_body = sema.code.extra[sema.code.root_start..][0..sema.code.root_len];73 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);
75}75}
7676
77pub fn rootAsType(77pub fn rootAsType(
78 sema: *Sema,78 sema: *Sema,
79 root_block: *Scope.Block,79 root_block: *Scope.Block,
80 zir_result_inst: zir.Inst.Index,80 zir_result_inst: zir.Inst.Index,
81 body: zir.Body,
82) !Type {81) !Type {
83 const root_body = sema.code.extra[sema.code.root_start..][0..sema.code.root_len];82 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
86 const result_inst = sema.inst_map[zir_result_inst];85 const result_inst = sema.inst_map[zir_result_inst];
87 // Source location is unneeded because resolveConstValue must have already86 // Source location is unneeded because resolveConstValue must have already
88 // been successfully called when coercing the value to a type, from the87 // been successfully called when coercing the value to a type, from the
89 // result location.88 // result location.
90 const val = try sema.resolveConstValue(root_block, .unneeded, result_inst);89 return sema.resolveType(root_block, .unneeded, result_inst);
91 return val.toType(root_block.arena);
92}90}
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 {
95 const tracy = trace(@src());93 const tracy = trace(@src());
96 defer tracy.end();94 defer tracy.end();
9795
...@@ -154,7 +152,7 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi...@@ -154,7 +152,7 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi
154 .field_val => try sema.zirFieldVal(block, zir_inst),152 .field_val => try sema.zirFieldVal(block, zir_inst),
155 .field_ptr_named => try sema.zirFieldPtrNamed(block, zir_inst),153 .field_ptr_named => try sema.zirFieldPtrNamed(block, zir_inst),
156 .field_val_named => try sema.zirFieldValNamed(block, zir_inst),154 .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),
158 .as => try sema.zirAs(block, zir_inst),156 .as => try sema.zirAs(block, zir_inst),
159 .@"asm" => try sema.zirAsm(block, zir_inst, false),157 .@"asm" => try sema.zirAsm(block, zir_inst, false),
160 .asm_volatile => try sema.zirAsm(block, zir_inst, true),158 .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...@@ -162,8 +160,10 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi
162 .unreachable_unsafe => try sema.zirUnreachable(block, zir_inst, false),160 .unreachable_unsafe => try sema.zirUnreachable(block, zir_inst, false),
163 .ret_tok => try sema.zirRetTok(block, zir_inst),161 .ret_tok => try sema.zirRetTok(block, zir_inst),
164 .ret_node => try sema.zirRetNode(block, zir_inst),162 .ret_node => try sema.zirRetNode(block, zir_inst),
165 .fn_type => try sema.zirFnType(block, zir_inst),163 .fn_type => try sema.zirFnType(block, zir_inst, false),
166 .fn_type_cc => try sema.zirFnTypeCc(block, zir_inst),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),
167 .intcast => try sema.zirIntcast(block, zir_inst),167 .intcast => try sema.zirIntcast(block, zir_inst),
168 .bitcast => try sema.zirBitcast(block, zir_inst),168 .bitcast => try sema.zirBitcast(block, zir_inst),
169 .floatcast => try sema.zirFloatcast(block, zir_inst),169 .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...@@ -230,10 +230,15 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi
230 .import => try sema.zirImport(block, zir_inst),230 .import => try sema.zirImport(block, zir_inst),
231 .bool_and => try sema.zirBoolOp(block, zir_inst, false),231 .bool_and => try sema.zirBoolOp(block, zir_inst, false),
232 .bool_or => try sema.zirBoolOp(block, zir_inst, true),232 .bool_or => try sema.zirBoolOp(block, zir_inst, true),
233 .void_value => try sema.mod.constVoid(block.arena, .unneeded),233 .@"await" => try sema.zirAwait(block, zir_inst),
234 .switchbr => try sema.zirSwitchBr(block, zir_inst, false),234 .nosuspend_await => try sema.zirNosuspendAwait(block, zir_inst),
235 .switchbr_ref => try sema.zirSwitchBr(block, zir_inst, true),235 .suspend_block_one => @panic("TODO"),
236 .switch_range => try sema.zirSwitchRange(block, zir_inst),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),
237 };242 };
238 if (map[zir_inst].ty.isNoReturn()) {243 if (map[zir_inst].ty.isNoReturn()) {
239 break;244 break;
...@@ -241,7 +246,7 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi...@@ -241,7 +246,7 @@ pub fn body(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !voi
241 }246 }
242}247}
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 {
245 var i = zir_ref;250 var i = zir_ref;
246251
247 // First section of indexes correspond to a set number of constant values.252 // First section of indexes correspond to a set number of constant values.
...@@ -277,7 +282,7 @@ fn resolveConstString(...@@ -277,7 +282,7 @@ fn resolveConstString(
277 const wanted_type = Type.initTag(.const_slice_u8);282 const wanted_type = Type.initTag(.const_slice_u8);
278 const coerced_inst = try sema.coerce(block, wanted_type, tzir_inst);283 const coerced_inst = try sema.coerce(block, wanted_type, tzir_inst);
279 const val = try sema.resolveConstValue(block, src, coerced_inst);284 const val = try sema.resolveConstValue(block, src, coerced_inst);
280 return val.toAllocatedBytes(block.arena);285 return val.toAllocatedBytes(sema.arena);
281}286}
282287
283fn resolveType(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, zir_ref: zir.Inst.Ref) !Type {288fn 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...@@ -355,7 +360,7 @@ fn zirConst(sema: *Sema, block: *Scope.Block, const_inst: zir.Inst.Index) InnerE
355 defer tracy.end();360 defer tracy.end();
356 // Move the TypedValue from old memory to new memory. This allows freeing the ZIR instructions361 // Move the TypedValue from old memory to new memory. This allows freeing the ZIR instructions
357 // after analysis.362 // 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);
359 return sema.mod.constInst(scope, const_inst.base.src, typed_value_copy);364 return sema.mod.constInst(scope, const_inst.base.src, typed_value_copy);
360}365}
361366
...@@ -377,14 +382,14 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In...@@ -377,14 +382,14 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In
377 return sema.mod.fail(&block.base, inst.base.src, "TODO implement zirCoerceResultPtr", .{});382 return sema.mod.fail(&block.base, inst.base.src, "TODO implement zirCoerceResultPtr", .{});
378}383}
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 {
381 const tracy = trace(@src());386 const tracy = trace(@src());
382 defer tracy.end();387 defer tracy.end();
383388
384 try sema.requireFunctionBlock(block, inst.base.src);389 try sema.requireFunctionBlock(block, inst.base.src);
385 const fn_ty = block.func.?.owner_decl.typed_value.most_recent.typed_value.ty;390 const fn_ty = block.func.?.owner_decl.typed_value.most_recent.typed_value.ty;
386 const ret_type = fn_ty.fnReturnType();391 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);
388 return block.addNoOp(inst.base.src, ptr_type, .alloc);393 return block.addNoOp(inst.base.src, ptr_type, .alloc);
389}394}
390395
...@@ -403,7 +408,7 @@ fn zirRetType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError...@@ -403,7 +408,7 @@ fn zirRetType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
403 try sema.requireFunctionBlock(block, inst.base.src);408 try sema.requireFunctionBlock(block, inst.base.src);
404 const fn_ty = b.func.?.owner_decl.typed_value.most_recent.typed_value.ty;409 const fn_ty = b.func.?.owner_decl.typed_value.most_recent.typed_value.ty;
405 const ret_type = fn_ty.fnReturnType();410 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);
407}412}
408413
409fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {414fn 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...@@ -414,7 +419,7 @@ fn zirEnsureResultUsed(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) I
414 const operand = sema.resolveInst(block, inst_data.operand);419 const operand = sema.resolveInst(block, inst_data.operand);
415 const src = inst_data.src();420 const src = inst_data.src();
416 switch (operand.ty.zigTypeTag()) {421 switch (operand.ty.zigTypeTag()) {
417 .Void, .NoReturn => return sema.mod.constVoid(block.arena, .unneeded),422 .Void, .NoReturn => return sema.mod.constVoid(sema.arena, .unneeded),
418 else => return sema.mod.fail(&block.base, src, "expression value is ignored", .{}),423 else => return sema.mod.fail(&block.base, src, "expression value is ignored", .{}),
419 }424 }
420}425}
...@@ -428,7 +433,7 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Inde...@@ -428,7 +433,7 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Inde
428 const src = inst_data.src();433 const src = inst_data.src();
429 switch (operand.ty.zigTypeTag()) {434 switch (operand.ty.zigTypeTag()) {
430 .ErrorSet, .ErrorUnion => return sema.mod.fail(&block.base, src, "error is discarded", .{}),435 .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),
432 }437 }
433}438}
434439
...@@ -473,7 +478,7 @@ fn zirAlloc(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*...@@ -473,7 +478,7 @@ fn zirAlloc(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*
473 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };478 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };
474 const var_decl_src = inst_data.src();479 const var_decl_src = inst_data.src();
475 const var_type = try sema.resolveType(block, ty_src, inst_data.operand);480 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);
477 try sema.requireRuntimeBlock(block, var_decl_src);482 try sema.requireRuntimeBlock(block, var_decl_src);
478 return block.addNoOp(var_decl_src, ptr_type, .alloc);483 return block.addNoOp(var_decl_src, ptr_type, .alloc);
479}484}
...@@ -487,7 +492,7 @@ fn zirAllocMut(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro...@@ -487,7 +492,7 @@ fn zirAllocMut(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro
487 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };492 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };
488 const var_type = try sema.resolveType(block, ty_src, inst_data.operand);493 const var_type = try sema.resolveType(block, ty_src, inst_data.operand);
489 try sema.validateVarType(block, ty_src, var_type);494 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);
491 try sema.requireRuntimeBlock(block, var_decl_src);496 try sema.requireRuntimeBlock(block, var_decl_src);
492 return block.addNoOp(var_decl_src, ptr_type, .alloc);497 return block.addNoOp(var_decl_src, ptr_type, .alloc);
493}498}
...@@ -500,7 +505,7 @@ fn zirAllocInferred(...@@ -500,7 +505,7 @@ fn zirAllocInferred(
500) InnerError!*Inst {505) InnerError!*Inst {
501 const tracy = trace(@src());506 const tracy = trace(@src());
502 defer tracy.end();507 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);
504 val_payload.* = .{509 val_payload.* = .{
505 .data = .{},510 .data = .{},
506 };511 };
...@@ -540,13 +545,13 @@ fn zirResolveInferredAlloc(...@@ -540,13 +545,13 @@ fn zirResolveInferredAlloc(
540 if (var_is_mut) {545 if (var_is_mut) {
541 try sema.validateVarType(block, ty_src, final_elem_ty);546 try sema.validateVarType(block, ty_src, final_elem_ty);
542 }547 }
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
545 // Change it to a normal alloc.550 // Change it to a normal alloc.
546 ptr.ty = final_ptr_ty;551 ptr.ty = final_ptr_ty;
547 ptr.tag = .alloc;552 ptr.tag = .alloc;
548553
549 return sema.mod.constVoid(block.arena, .unneeded);554 return sema.mod.constVoid(sema.arena, .unneeded);
550}555}
551556
552fn zirStoreToBlockPtr(557fn zirStoreToBlockPtr(
...@@ -560,7 +565,7 @@ fn zirStoreToBlockPtr(...@@ -560,7 +565,7 @@ fn zirStoreToBlockPtr(
560 const bin_inst = sema.code.instructions.items(.data)[inst].bin;565 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
561 const ptr = sema.resolveInst(bin_inst.lhs);566 const ptr = sema.resolveInst(bin_inst.lhs);
562 const value = sema.resolveInst(bin_inst.rhs);567 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);
564 // TODO detect when this store should be done at compile-time. For example,569 // TODO detect when this store should be done at compile-time. For example,
565 // if expressions should force it when the condition is compile-time known.570 // if expressions should force it when the condition is compile-time known.
566 try sema.requireRuntimeBlock(block, src);571 try sema.requireRuntimeBlock(block, src);
...@@ -582,9 +587,9 @@ fn zirStoreToInferredPtr(...@@ -582,9 +587,9 @@ fn zirStoreToInferredPtr(
582 const inferred_alloc = ptr.castTag(.constant).?.val.castTag(.inferred_alloc).?;587 const inferred_alloc = ptr.castTag(.constant).?.val.castTag(.inferred_alloc).?;
583 // Add the stored instruction to the set we will use to resolve peer types588 // Add the stored instruction to the set we will use to resolve peer types
584 // for the inferred allocation.589 // 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);
586 // Create a runtime bitcast instruction with exactly the type the pointer wants.591 // 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);
588 try sema.requireRuntimeBlock(block, src);593 try sema.requireRuntimeBlock(block, src);
589 const bitcasted_ptr = try block.addUnOp(inst.base.src, ptr_ty, .bitcast, ptr);594 const bitcasted_ptr = try block.addUnOp(inst.base.src, ptr_ty, .bitcast, ptr);
590 return mod.storePtr(scope, inst.base.src, bitcasted_ptr, value);595 return mod.storePtr(scope, inst.base.src, bitcasted_ptr, value);
...@@ -601,7 +606,7 @@ fn zirSetEvalBranchQuota(...@@ -601,7 +606,7 @@ fn zirSetEvalBranchQuota(
601 const quota = try sema.resolveAlreadyCoercedInt(block, src, inst_data.operand, u32);606 const quota = try sema.resolveAlreadyCoercedInt(block, src, inst_data.operand, u32);
602 if (sema.branch_quota < quota)607 if (sema.branch_quota < quota)
603 sema.branch_quota = quota;608 sema.branch_quota = quota;
604 return sema.mod.constVoid(block.arena, .unneeded);609 return sema.mod.constVoid(sema.arena, .unneeded);
605}610}
606611
607fn zirStore(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {612fn 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...@@ -635,7 +640,7 @@ fn zirParamType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr
635 const param_count = fn_ty.fnParamLen();640 const param_count = fn_ty.fnParamLen();
636 if (param_index >= param_count) {641 if (param_index >= param_count) {
637 if (fn_ty.fnIsVarArgs()) {642 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));
639 }644 }
640 return sema.mod.fail(&block.base, inst.base.src, "arg index {d} out of bounds; '{}' has {d} argument(s)", .{645 return sema.mod.fail(&block.base, inst.base.src, "arg index {d} out of bounds; '{}' has {d} argument(s)", .{
641 param_index,646 param_index,
...@@ -646,7 +651,7 @@ fn zirParamType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr...@@ -646,7 +651,7 @@ fn zirParamType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErr
646651
647 // TODO support generic functions652 // TODO support generic functions
648 const param_type = fn_ty.fnParamType(param_index);653 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);
650}655}
651656
652fn zirStr(sema: *Sema, block: *Scope.Block, str_inst: zir.Inst.Index) InnerError!*Inst {657fn 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...@@ -714,7 +719,7 @@ fn zirCompileLog(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr
714 .lazy = inst_data.src(),719 .lazy = inst_data.src(),
715 };720 };
716 }721 }
717 return sema.mod.constVoid(block.arena, .unneeded);722 return sema.mod.constVoid(sema.arena, .unneeded);
718}723}
719724
720fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {725fn 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...@@ -724,7 +729,7 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerE
724 // Reserve space for a Loop instruction so that generated Break instructions can729 // Reserve space for a Loop instruction so that generated Break instructions can
725 // point to it, even if it doesn't end up getting used because the code ends up being730 // point to it, even if it doesn't end up getting used because the code ends up being
726 // comptime evaluated.731 // comptime evaluated.
727 const loop_inst = try parent_block.arena.create(Inst.Loop);732 const loop_inst = try sema.arena.create(Inst.Loop);
728 loop_inst.* = .{733 loop_inst.* = .{
729 .base = .{734 .base = .{
730 .tag = Inst.Loop.base_tag,735 .tag = Inst.Loop.base_tag,
...@@ -741,19 +746,19 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerE...@@ -741,19 +746,19 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) InnerE
741 .owner_decl = parent_block.owner_decl,746 .owner_decl = parent_block.owner_decl,
742 .src_decl = parent_block.src_decl,747 .src_decl = parent_block.src_decl,
743 .instructions = .{},748 .instructions = .{},
744 .arena = parent_block.arena,749 .arena = sema.arena,
745 .inlining = parent_block.inlining,750 .inlining = parent_block.inlining,
746 .is_comptime = parent_block.is_comptime,751 .is_comptime = parent_block.is_comptime,
747 .branch_quota = parent_block.branch_quota,752 .branch_quota = parent_block.branch_quota,
748 };753 };
749 defer child_block.instructions.deinit(mod.gpa);754 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
753 // Loop repetition is implied so the last instruction may or may not be a noreturn instruction.758 // Loop repetition is implied so the last instruction may or may not be a noreturn instruction.
754759
755 try parent_block.instructions.append(mod.gpa, &loop_inst.base);760 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) };
757 return &loop_inst.base;762 return &loop_inst.base;
758}763}
759764
...@@ -765,10 +770,10 @@ fn zirBlockFlat(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index, i...@@ -765,10 +770,10 @@ fn zirBlockFlat(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index, i
765 defer child_block.instructions.deinit(mod.gpa);770 defer child_block.instructions.deinit(mod.gpa);
766 child_block.is_comptime = child_block.is_comptime or is_comptime;771 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
770 // Move the analyzed instructions into the parent block arena.775 // 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);
772 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);777 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);
773778
774 // The result of a flat block is the last instruction.779 // The result of a flat block is the last instruction.
...@@ -789,7 +794,7 @@ fn zirBlock(...@@ -789,7 +794,7 @@ fn zirBlock(
789 // Reserve space for a Block instruction so that generated Break instructions can794 // Reserve space for a Block instruction so that generated Break instructions can
790 // point to it, even if it doesn't end up getting used because the code ends up being795 // point to it, even if it doesn't end up getting used because the code ends up being
791 // comptime evaluated.796 // comptime evaluated.
792 const block_inst = try parent_block.arena.create(Inst.Block);797 const block_inst = try sema.arena.create(Inst.Block);
793 block_inst.* = .{798 block_inst.* = .{
794 .base = .{799 .base = .{
795 .tag = Inst.Block.base_tag,800 .tag = Inst.Block.base_tag,
...@@ -806,7 +811,7 @@ fn zirBlock(...@@ -806,7 +811,7 @@ fn zirBlock(
806 .owner_decl = parent_block.owner_decl,811 .owner_decl = parent_block.owner_decl,
807 .src_decl = parent_block.src_decl,812 .src_decl = parent_block.src_decl,
808 .instructions = .{},813 .instructions = .{},
809 .arena = parent_block.arena,814 .arena = sema.arena,
810 // TODO @as here is working around a stage1 miscompilation bug :(815 // TODO @as here is working around a stage1 miscompilation bug :(
811 .label = @as(?Scope.Block.Label, Scope.Block.Label{816 .label = @as(?Scope.Block.Label, Scope.Block.Label{
812 .zir_block = inst,817 .zir_block = inst,
...@@ -826,7 +831,7 @@ fn zirBlock(...@@ -826,7 +831,7 @@ fn zirBlock(
826 defer merges.results.deinit(mod.gpa);831 defer merges.results.deinit(mod.gpa);
827 defer merges.br_list.deinit(mod.gpa);832 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
831 return analyzeBlockBody(mod, scope, &child_block, merges);836 return analyzeBlockBody(mod, scope, &child_block, merges);
832}837}
...@@ -847,7 +852,7 @@ fn analyzeBlockBody(...@@ -847,7 +852,7 @@ fn analyzeBlockBody(
847 if (merges.results.items.len == 0) {852 if (merges.results.items.len == 0) {
848 // No need for a block instruction. We can put the new instructions853 // No need for a block instruction. We can put the new instructions
849 // directly into the parent block.854 // 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);
851 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);856 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);
852 return copied_instructions[copied_instructions.len - 1];857 return copied_instructions[copied_instructions.len - 1];
853 }858 }
...@@ -858,7 +863,7 @@ fn analyzeBlockBody(...@@ -858,7 +863,7 @@ fn analyzeBlockBody(
858 if (br_block == merges.block_inst) {863 if (br_block == merges.block_inst) {
859 // No need for a block instruction. We can put the new instructions directly864 // No need for a block instruction. We can put the new instructions directly
860 // into the parent block. Here we omit the break instruction.865 // 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]);
862 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);867 try parent_block.instructions.appendSlice(mod.gpa, copied_instructions);
863 return merges.results.items[0];868 return merges.results.items[0];
864 }869 }
...@@ -873,7 +878,7 @@ fn analyzeBlockBody(...@@ -873,7 +878,7 @@ fn analyzeBlockBody(
873 const resolved_ty = try sema.resolvePeerTypes(parent_block, merges.results.items);878 const resolved_ty = try sema.resolvePeerTypes(parent_block, merges.results.items);
874 merges.block_inst.base.ty = resolved_ty;879 merges.block_inst.base.ty = resolved_ty;
875 merges.block_inst.body = .{880 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),
877 };882 };
878 // Now that the block has its type resolved, we need to go back into all the break883 // Now that the block has its type resolved, we need to go back into all the break
879 // instructions, and insert type coercion on the operands.884 // instructions, and insert type coercion on the operands.
...@@ -905,7 +910,7 @@ fn analyzeBlockBody(...@@ -905,7 +910,7 @@ fn analyzeBlockBody(
905 },910 },
906 .block = merges.block_inst,911 .block = merges.block_inst,
907 .body = .{912 .body = .{
908 .instructions = try parent_block.arena.dupe(*Inst, coerce_block.instructions.items),913 .instructions = try sema.arena.dupe(*Inst, coerce_block.instructions.items),
909 },914 },
910 };915 };
911 }916 }
...@@ -936,7 +941,7 @@ fn zirBreakVoidTok(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner...@@ -936,7 +941,7 @@ fn zirBreakVoidTok(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner
936941
937 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;942 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
938 const zir_block = inst_data.operand;943 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);
940 return analyzeBreak(mod, block, inst_data.src(), zir_block, void_inst);945 return analyzeBreak(mod, block, inst_data.src(), zir_block, void_inst);
941}946}
942947
...@@ -984,7 +989,7 @@ fn zirDbgStmtNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE...@@ -984,7 +989,7 @@ fn zirDbgStmtNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE
984 defer tracy.end();989 defer tracy.end();
985990
986 if (b.is_comptime) {991 if (b.is_comptime) {
987 return sema.mod.constVoid(block.arena, .unneeded);992 return sema.mod.constVoid(sema.arena, .unneeded);
988 }993 }
989994
990 const src_node = sema.code.instructions.items(.data)[inst].node;995 const src_node = sema.code.instructions.items(.data)[inst].node;
...@@ -1090,7 +1095,7 @@ fn analyzeCall(...@@ -1090,7 +1095,7 @@ fn analyzeCall(
1090 }1095 }
10911096
1092 // TODO handle function calls of generic functions1097 // 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);
1094 for (zir_args) |zir_arg, i| {1099 for (zir_args) |zir_arg, i| {
1095 // the args are already casted to the result of a param type instruction.1100 // the args are already casted to the result of a param type instruction.
1096 casted_args[i] = sema.resolveInst(block, zir_arg);1101 casted_args[i] = sema.resolveInst(block, zir_arg);
...@@ -1117,7 +1122,7 @@ fn analyzeCall(...@@ -1117,7 +1122,7 @@ fn analyzeCall(
1117 // set to in the `Scope.Block`.1122 // set to in the `Scope.Block`.
1118 // This block instruction will be used to capture the return value from the1123 // This block instruction will be used to capture the return value from the
1119 // inlined function.1124 // inlined function.
1120 const block_inst = try block.arena.create(Inst.Block);1125 const block_inst = try sema.arena.create(Inst.Block);
1121 block_inst.* = .{1126 block_inst.* = .{
1122 .base = .{1127 .base = .{
1123 .tag = Inst.Block.base_tag,1128 .tag = Inst.Block.base_tag,
...@@ -1154,7 +1159,7 @@ fn analyzeCall(...@@ -1154,7 +1159,7 @@ fn analyzeCall(
1154 .owner_decl = scope.ownerDecl().?,1159 .owner_decl = scope.ownerDecl().?,
1155 .src_decl = module_fn.owner_decl,1160 .src_decl = module_fn.owner_decl,
1156 .instructions = .{},1161 .instructions = .{},
1157 .arena = block.arena,1162 .arena = sema.arena,
1158 .label = null,1163 .label = null,
1159 .inlining = &inlining,1164 .inlining = &inlining,
1160 .is_comptime = is_comptime_call,1165 .is_comptime = is_comptime_call,
...@@ -1171,7 +1176,7 @@ fn analyzeCall(...@@ -1171,7 +1176,7 @@ fn analyzeCall(
11711176
1172 // This will have return instructions analyzed as break instructions to1177 // This will have return instructions analyzed as break instructions to
1173 // the block_inst above.1178 // the block_inst above.
1174 try sema.body(&child_block, module_fn.zir);1179 try sema.analyzeBody(&child_block, module_fn.zir);
11751180
1176 return analyzeBlockBody(mod, scope, &child_block, merges);1181 return analyzeBlockBody(mod, scope, &child_block, merges);
1177 }1182 }
...@@ -1191,9 +1196,9 @@ fn zirOptionalType(sema: *Sema, block: *Scope.Block, optional: zir.Inst.Index) I...@@ -1191,9 +1196,9 @@ fn zirOptionalType(sema: *Sema, block: *Scope.Block, optional: zir.Inst.Index) I
11911196
1192 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;1197 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1193 const child_type = try sema.resolveType(block, inst_data.operand);1198 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);
1197}1202}
11981203
1199fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {1204fn 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...@@ -1203,9 +1208,9 @@ fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: zir.Inst.I
1203 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;1208 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1204 const ptr = sema.resolveInst(block, inst_data.operand);1209 const ptr = sema.resolveInst(block, inst_data.operand);
1205 const elem_ty = ptr.ty.elemType();1210 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);
1209}1214}
12101215
1211fn zirArrayType(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index) InnerError!*Inst {1216fn 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...@@ -1215,7 +1220,7 @@ fn zirArrayType(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index) InnerEr
1215 const len = try resolveInstConst(mod, scope, array.positionals.lhs);1220 const len = try resolveInstConst(mod, scope, array.positionals.lhs);
1216 const elem_type = try sema.resolveType(block, array.positionals.rhs);1221 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));
1219}1224}
12201225
1221fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index) InnerError!*Inst {1226fn 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)...@@ -1226,7 +1231,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Scope.Block, array: zir.Inst.Index)
1226 const sentinel = try resolveInstConst(mod, scope, array.positionals.sentinel);1231 const sentinel = try resolveInstConst(mod, scope, array.positionals.sentinel);
1227 const elem_type = try sema.resolveType(block, array.positionals.elem_type);1232 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));
1230}1235}
12311236
1232fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {1237fn 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...@@ -1241,7 +1246,7 @@ fn zirErrorUnionType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inn
1241 return sema.mod.fail(&block.base, inst.base.src, "expected error set type, found {}", .{error_union.elemType()});1246 return sema.mod.fail(&block.base, inst.base.src, "expected error set type, found {}", .{error_union.elemType()});
1242 }1247 }
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));
1245}1250}
12461251
1247fn zirAnyframeType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {1252fn 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...@@ -1252,9 +1257,9 @@ fn zirAnyframeType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner
1252 const src = inst_data.src();1257 const src = inst_data.src();
1253 const operand_src: LazySrcLoc = .{ .node_offset_anyframe_type = inst_data.src_node };1258 const operand_src: LazySrcLoc = .{ .node_offset_anyframe_type = inst_data.src_node };
1254 const return_type = try sema.resolveType(block, operand_src, inst_data.operand);1259 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);
1258}1263}
12591264
1260fn zirErrorSet(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {1265fn 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...@@ -1296,10 +1301,10 @@ fn zirErrorValue(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr
12961301
1297 // Create an anonymous error set type with only this error value, and return the value.1302 // Create an anonymous error set type with only this error value, and return the value.
1298 const entry = try mod.getErrorValue(inst.positionals.name);1303 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);
1300 return sema.mod.constInst(scope, inst.base.src, .{1305 return sema.mod.constInst(scope, inst.base.src, .{
1301 .ty = result_type,1306 .ty = result_type,
1302 .val = try Value.Tag.@"error".create(block.arena, .{1307 .val = try Value.Tag.@"error".create(sema.arena, .{
1303 .name = entry.key,1308 .name = entry.key,
1304 }),1309 }),
1305 });1310 });
...@@ -1388,10 +1393,10 @@ fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, zir_inst: zir.Inst.Index) In...@@ -1388,10 +1393,10 @@ fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, zir_inst: zir.Inst.Index) In
1388 const tracy = trace(@src());1393 const tracy = trace(@src());
1389 defer tracy.end();1394 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);
1392 return sema.mod.constInst(scope, inst.base.src, .{1397 return sema.mod.constInst(scope, inst.base.src, .{
1393 .ty = Type.initTag(.enum_literal),1398 .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),
1395 });1400 });
1396}1401}
13971402
...@@ -1415,11 +1420,11 @@ fn zirOptionalPayloadPtr(...@@ -1415,11 +1420,11 @@ fn zirOptionalPayloadPtr(
1415 return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type});1420 return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type});
1416 }1421 }
14171422
1418 const child_type = try opt_type.optionalChildAlloc(block.arena);1423 const child_type = try opt_type.optionalChildAlloc(sema.arena);
1419 const child_pointer = try sema.mod.simplePtrType(block.arena, child_type, !optional_ptr.ty.isConstPtr(), .One);1424 const child_pointer = try sema.mod.simplePtrType(sema.arena, child_type, !optional_ptr.ty.isConstPtr(), .One);
14201425
1421 if (optional_ptr.value()) |pointer_val| {1426 if (optional_ptr.value()) |pointer_val| {
1422 const val = try pointer_val.pointerDeref(block.arena);1427 const val = try pointer_val.pointerDeref(sema.arena);
1423 if (val.isNull()) {1428 if (val.isNull()) {
1424 return sema.mod.fail(&block.base, src, "unable to unwrap null", .{});1429 return sema.mod.fail(&block.base, src, "unable to unwrap null", .{});
1425 }1430 }
...@@ -1456,7 +1461,7 @@ fn zirOptionalPayload(...@@ -1456,7 +1461,7 @@ fn zirOptionalPayload(
1456 return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type});1461 return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type});
1457 }1462 }
14581463
1459 const child_type = try opt_type.optionalChildAlloc(block.arena);1464 const child_type = try opt_type.optionalChildAlloc(sema.arena);
14601465
1461 if (operand.value()) |val| {1466 if (operand.value()) |val| {
1462 if (val.isNull()) {1467 if (val.isNull()) {
...@@ -1528,10 +1533,10 @@ fn zirErrUnionPayloadPtr(...@@ -1528,10 +1533,10 @@ fn zirErrUnionPayloadPtr(
1528 if (operand.ty.elemType().zigTypeTag() != .ErrorUnion)1533 if (operand.ty.elemType().zigTypeTag() != .ErrorUnion)
1529 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand.ty.elemType()});1534 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
1533 if (operand.value()) |pointer_val| {1538 if (operand.value()) |pointer_val| {
1534 const val = try pointer_val.pointerDeref(block.arena);1539 const val = try pointer_val.pointerDeref(sema.arena);
1535 if (val.getError()) |name| {1540 if (val.getError()) |name| {
1536 return sema.mod.fail(&block.base, src, "caught unexpected error '{s}'", .{name});1541 return sema.mod.fail(&block.base, src, "caught unexpected error '{s}'", .{name});
1537 }1542 }
...@@ -1540,7 +1545,7 @@ fn zirErrUnionPayloadPtr(...@@ -1540,7 +1545,7 @@ fn zirErrUnionPayloadPtr(
1540 return sema.mod.constInst(scope, src, .{1545 return sema.mod.constInst(scope, src, .{
1541 .ty = operand_pointer_ty,1546 .ty = operand_pointer_ty,
1542 .val = try Value.Tag.ref_val.create(1547 .val = try Value.Tag.ref_val.create(
1543 block.arena,1548 sema.arena,
1544 data,1549 data,
1545 ),1550 ),
1546 });1551 });
...@@ -1592,7 +1597,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In...@@ -1592,7 +1597,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In
1592 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand.ty.elemType()});1597 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand.ty.elemType()});
15931598
1594 if (operand.value()) |pointer_val| {1599 if (operand.value()) |pointer_val| {
1595 const val = try pointer_val.pointerDeref(block.arena);1600 const val = try pointer_val.pointerDeref(sema.arena);
1596 assert(val.getError() != null);1601 assert(val.getError() != null);
1597 const data = val.castTag(.error_union).?.data;1602 const data = val.castTag(.error_union).?.data;
1598 return sema.mod.constInst(scope, src, .{1603 return sema.mod.constInst(scope, src, .{
...@@ -1617,40 +1622,46 @@ fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Inde...@@ -1617,40 +1622,46 @@ fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Inde
1617 if (operand.ty.castTag(.error_union).?.data.payload.zigTypeTag() != .Void) {1622 if (operand.ty.castTag(.error_union).?.data.payload.zigTypeTag() != .Void) {
1618 return sema.mod.fail(&block.base, src, "expression value is ignored", .{});1623 return sema.mod.fail(&block.base, src, "expression value is ignored", .{});
1619 }1624 }
1620 return sema.mod.constVoid(block.arena, .unneeded);1625 return sema.mod.constVoid(sema.arena, .unneeded);
1621}1626}
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 {
1624 const tracy = trace(@src());1629 const tracy = trace(@src());
1625 defer tracy.end();1630 defer tracy.end();
16261631
1627 return fnTypeCommon(1632 const inst_data = sema.code.instructions.items(.data)[inst].fn_type;
1628 mod,1633 const extra = sema.code.extraData(zir.Inst.FnType, inst_data.payload_index);
1629 scope,1634 const param_types = sema.code.extra[extra.end..][0..extra.data.param_types_len];
1630 &fntype.base,1635
1631 fntype.positionals.param_types,1636 return sema.fnTypeCommon(
1632 fntype.positionals.return_type,1637 block,
1638 .unneeded,
1639 param_types,
1640 inst_data.return_type,
1633 .Unspecified,1641 .Unspecified,
1634 var_args,1642 var_args,
1635 );1643 );
1636}1644}
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 {
1639 const tracy = trace(@src());1647 const tracy = trace(@src());
1640 defer tracy.end();1648 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);
1643 // TODO once we're capable of importing and analyzing decls from1655 // TODO once we're capable of importing and analyzing decls from
1644 // std.builtin, this needs to change1656 // std.builtin, this needs to change
1645 const cc_str = cc_tv.val.castTag(.enum_literal).?.data;1657 const cc_str = cc_tv.val.castTag(.enum_literal).?.data;
1646 const cc = std.meta.stringToEnum(std.builtin.CallingConvention, cc_str) orelse1658 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});1659 return sema.mod.fail(&block.base, .todo, "Unknown calling convention {s}", .{cc_str});
1648 return fnTypeCommon(1660 return sema.fnTypeCommon(
1649 mod,1661 block,
1650 scope,1662 .unneeded,
1651 &fntype.base,1663 param_types,
1652 fntype.positionals.param_types,1664 inst_data.return_type,
1653 fntype.positionals.return_type,
1654 cc,1665 cc,
1655 var_args,1666 var_args,
1656 );1667 );
...@@ -1659,9 +1670,9 @@ fn zirFnTypeCc(sema: *Sema, block: *Scope.Block, fntype: zir.Inst.Index, var_arg...@@ -1659,9 +1670,9 @@ fn zirFnTypeCc(sema: *Sema, block: *Scope.Block, fntype: zir.Inst.Index, var_arg
1659fn fnTypeCommon(1670fn fnTypeCommon(
1660 sema: *Sema,1671 sema: *Sema,
1661 block: *Scope.Block,1672 block: *Scope.Block,
1662 zir_inst: zir.Inst.Index,1673 src: LazySrcLoc,
1663 zir_param_types: []zir.Inst.Index,1674 zir_param_types: []const zir.Inst.Ref,
1664 zir_return_type: zir.Inst.Index,1675 zir_return_type: zir.Inst.Ref,
1665 cc: std.builtin.CallingConvention,1676 cc: std.builtin.CallingConvention,
1666 var_args: bool,1677 var_args: bool,
1667) InnerError!*Inst {1678) InnerError!*Inst {
...@@ -1670,39 +1681,39 @@ fn fnTypeCommon(...@@ -1670,39 +1681,39 @@ fn fnTypeCommon(
1670 // Hot path for some common function types.1681 // Hot path for some common function types.
1671 if (zir_param_types.len == 0 and !var_args) {1682 if (zir_param_types.len == 0 and !var_args) {
1672 if (return_type.zigTypeTag() == .NoReturn and cc == .Unspecified) {1683 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));
1674 }1685 }
16751686
1676 if (return_type.zigTypeTag() == .Void and cc == .Unspecified) {1687 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));
1678 }1689 }
16791690
1680 if (return_type.zigTypeTag() == .NoReturn and cc == .Naked) {1691 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));
1682 }1693 }
16831694
1684 if (return_type.zigTypeTag() == .Void and cc == .C) {1695 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));
1686 }1697 }
1687 }1698 }
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);
1690 for (zir_param_types) |param_type, i| {1701 for (zir_param_types) |param_type, i| {
1691 const resolved = try sema.resolveType(block, param_type);1702 const resolved = try sema.resolveType(block, param_type);
1692 // TODO skip for comptime params1703 // TODO skip for comptime params
1693 if (!resolved.isValidVarType(false)) {1704 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});
1695 }1706 }
1696 param_types[i] = resolved;1707 param_types[i] = resolved;
1697 }1708 }
16981709
1699 const fn_ty = try Type.Tag.function.create(block.arena, .{1710 const fn_ty = try Type.Tag.function.create(sema.arena, .{
1700 .param_types = param_types,1711 .param_types = param_types,
1701 .return_type = return_type,1712 .return_type = return_type,
1702 .cc = cc,1713 .cc = cc,
1703 .is_var_args = var_args,1714 .is_var_args = var_args,
1704 });1715 });
1705 return sema.mod.constType(block.arena, zir_inst.src, fn_ty);1716 return sema.mod.constType(sema.arena, src, fn_ty);
1706}1717}
17071718
1708fn zirAs(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {1719fn 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...@@ -1981,11 +1992,11 @@ fn zirSwitchRange(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE
19811992
1982 switch (start.ty.zigTypeTag()) {1993 switch (start.ty.zigTypeTag()) {
1983 .Int, .ComptimeInt => {},1994 .Int, .ComptimeInt => {},
1984 else => return sema.mod.constVoid(block.arena, .unneeded),1995 else => return sema.mod.constVoid(sema.arena, .unneeded),
1985 }1996 }
1986 switch (end.ty.zigTypeTag()) {1997 switch (end.ty.zigTypeTag()) {
1987 .Int, .ComptimeInt => {},1998 .Int, .ComptimeInt => {},
1988 else => return sema.mod.constVoid(block.arena, .unneeded),1999 else => return sema.mod.constVoid(sema.arena, .unneeded),
1989 }2000 }
1990 // .switch_range must be inside a comptime scope2001 // .switch_range must be inside a comptime scope
1991 const start_val = start.value().?;2002 const start_val = start.value().?;
...@@ -1993,7 +2004,7 @@ fn zirSwitchRange(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE...@@ -1993,7 +2004,7 @@ fn zirSwitchRange(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE
1993 if (start_val.compare(.gte, end_val)) {2004 if (start_val.compare(.gte, end_val)) {
1994 return sema.mod.fail(&block.base, inst.base.src, "range start value must be smaller than the end value", .{});2005 return sema.mod.fail(&block.base, inst.base.src, "range start value must be smaller than the end value", .{});
1995 }2006 }
1996 return sema.mod.constVoid(block.arena, .unneeded);2007 return sema.mod.constVoid(sema.arena, .unneeded);
1997}2008}
19982009
1999fn zirSwitchBr(2010fn zirSwitchBr(
...@@ -2021,22 +2032,22 @@ fn zirSwitchBr(...@@ -2021,22 +2032,22 @@ fn zirSwitchBr(
2021 const item = try sema.resolveConstValue(parent_block, case_src, casted);2032 const item = try sema.resolveConstValue(parent_block, case_src, casted);
20222033
2023 if (target_val.eql(item)) {2034 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);
2025 return mod.constNoReturn(scope, inst.base.src);2036 return mod.constNoReturn(scope, inst.base.src);
2026 }2037 }
2027 }2038 }
2028 try sema.body(scope.cast(Scope.Block).?, inst.positionals.else_body);2039 try sema.analyzeBody(scope.cast(Scope.Block).?, inst.positionals.else_body);
2029 return mod.constNoReturn(scope, inst.base.src);2040 return mod.constNoReturn(scope, inst.base.src);
2030 }2041 }
20312042
2032 if (inst.positionals.cases.len == 0) {2043 if (inst.positionals.cases.len == 0) {
2033 // no cases just analyze else_branch2044 // 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);
2035 return mod.constNoReturn(scope, inst.base.src);2046 return mod.constNoReturn(scope, inst.base.src);
2036 }2047 }
20372048
2038 try sema.requireRuntimeBlock(parent_block, inst.base.src);2049 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
2041 var case_block: Scope.Block = .{2052 var case_block: Scope.Block = .{
2042 .parent = parent_block,2053 .parent = parent_block,
...@@ -2045,7 +2056,7 @@ fn zirSwitchBr(...@@ -2045,7 +2056,7 @@ fn zirSwitchBr(
2045 .owner_decl = parent_block.owner_decl,2056 .owner_decl = parent_block.owner_decl,
2046 .src_decl = parent_block.src_decl,2057 .src_decl = parent_block.src_decl,
2047 .instructions = .{},2058 .instructions = .{},
2048 .arena = parent_block.arena,2059 .arena = sema.arena,
2049 .inlining = parent_block.inlining,2060 .inlining = parent_block.inlining,
2050 .is_comptime = parent_block.is_comptime,2061 .is_comptime = parent_block.is_comptime,
2051 .branch_quota = parent_block.branch_quota,2062 .branch_quota = parent_block.branch_quota,
...@@ -2060,19 +2071,19 @@ fn zirSwitchBr(...@@ -2060,19 +2071,19 @@ fn zirSwitchBr(
2060 const casted = try sema.coerce(scope, target.ty, resolved);2071 const casted = try sema.coerce(scope, target.ty, resolved);
2061 const item = try sema.resolveConstValue(parent_block, case_src, casted);2072 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
2065 cases[i] = .{2076 cases[i] = .{
2066 .item = item,2077 .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) },
2068 };2079 };
2069 }2080 }
20702081
2071 case_block.instructions.items.len = 0;2082 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
2074 const else_body: ir.Body = .{2085 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),
2076 };2087 };
20772088
2078 return mod.addSwitchBr(parent_block, inst.base.src, target, cases, else_body);2089 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!...@@ -2232,7 +2243,7 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!
2232 return sema.mod.fail(&block.base, src, "unable to open '{s}': {s}", .{ operand, @errorName(err) });2243 return sema.mod.fail(&block.base, src, "unable to open '{s}': {s}", .{ operand, @errorName(err) });
2233 },2244 },
2234 };2245 };
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);
2236}2247}
22372248
2238fn zirShl(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {2249fn 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:...@@ -2412,14 +2423,14 @@ fn analyzeInstComptimeOp(sema: *Sema, block: *Scope.Block, res_type: Type, inst:
2412 const value = switch (inst.base.tag) {2423 const value = switch (inst.base.tag) {
2413 .add => blk: {2424 .add => blk: {
2414 const val = if (is_int)2425 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)
2416 else2427 else
2417 try mod.floatAdd(scope, res_type, inst.base.src, lhs_val, rhs_val);2428 try mod.floatAdd(scope, res_type, inst.base.src, lhs_val, rhs_val);
2418 break :blk val;2429 break :blk val;
2419 },2430 },
2420 .sub => blk: {2431 .sub => blk: {
2421 const val = if (is_int)2432 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)
2423 else2434 else
2424 try mod.floatSub(scope, res_type, inst.base.src, lhs_val, rhs_val);2435 try mod.floatSub(scope, res_type, inst.base.src, lhs_val, rhs_val);
2425 break :blk val;2436 break :blk val;
...@@ -2435,7 +2446,7 @@ fn analyzeInstComptimeOp(sema: *Sema, block: *Scope.Block, res_type: Type, inst:...@@ -2435,7 +2446,7 @@ fn analyzeInstComptimeOp(sema: *Sema, block: *Scope.Block, res_type: Type, inst:
2435 });2446 });
2436}2447}
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 {
2439 const tracy = trace(@src());2450 const tracy = trace(@src());
2440 defer tracy.end();2451 defer tracy.end();
24412452
...@@ -2473,9 +2484,9 @@ fn zirAsm(...@@ -2473,9 +2484,9 @@ fn zirAsm(
2473 };2484 };
2474 } else null;2485 } else null;
24752486
2476 const args = try block.arena.alloc(*Inst, extra.data.args.len);2487 const args = try sema.arena.alloc(*Inst, extra.data.args.len);
2477 const inputs = try block.arena.alloc([]const u8, extra.data.args_len);2488 const inputs = try sema.arena.alloc([]const u8, extra.data.args_len);
2478 const clobbers = try block.arena.alloc([]const u8, extra.data.clobbers_len);2489 const clobbers = try sema.arena.alloc([]const u8, extra.data.clobbers_len);
24792490
2480 for (args) |*arg| {2491 for (args) |*arg| {
2481 const uncasted = sema.resolveInst(block, sema.code.extra[extra_i]);2492 const uncasted = sema.resolveInst(block, sema.code.extra[extra_i]);
...@@ -2492,7 +2503,7 @@ fn zirAsm(...@@ -2492,7 +2503,7 @@ fn zirAsm(
2492 }2503 }
24932504
2494 try sema.requireRuntimeBlock(block, src);2505 try sema.requireRuntimeBlock(block, src);
2495 const inst = try block.arena.create(Inst.Assembly);2506 const inst = try sema.arena.create(Inst.Assembly);
2496 inst.* = .{2507 inst.* = .{
2497 .base = .{2508 .base = .{
2498 .tag = .assembly,2509 .tag = .assembly,
...@@ -2532,7 +2543,7 @@ fn zirCmp(...@@ -2532,7 +2543,7 @@ fn zirCmp(
2532 const rhs_ty_tag = rhs.ty.zigTypeTag();2543 const rhs_ty_tag = rhs.ty.zigTypeTag();
2533 if (is_equality_cmp and lhs_ty_tag == .Null and rhs_ty_tag == .Null) {2544 if (is_equality_cmp and lhs_ty_tag == .Null and rhs_ty_tag == .Null) {
2534 // null == null, null != null2545 // 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);
2536 } else if (is_equality_cmp and2547 } else if (is_equality_cmp and
2537 ((lhs_ty_tag == .Null and rhs_ty_tag == .Optional) or2548 ((lhs_ty_tag == .Null and rhs_ty_tag == .Optional) or
2538 rhs_ty_tag == .Null and lhs_ty_tag == .Optional))2549 rhs_ty_tag == .Null and lhs_ty_tag == .Optional))
...@@ -2559,7 +2570,7 @@ fn zirCmp(...@@ -2559,7 +2570,7 @@ fn zirCmp(
2559 if (rhs.value()) |rval| {2570 if (rhs.value()) |rval| {
2560 if (lhs.value()) |lval| {2571 if (lhs.value()) |lval| {
2561 // 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 faster2572 // 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));
2563 }2574 }
2564 }2575 }
2565 try sema.requireRuntimeBlock(block, inst.base.src);2576 try sema.requireRuntimeBlock(block, inst.base.src);
...@@ -2573,7 +2584,7 @@ fn zirCmp(...@@ -2573,7 +2584,7 @@ fn zirCmp(
2573 if (!is_equality_cmp) {2584 if (!is_equality_cmp) {
2574 return sema.mod.fail(&block.base, inst.base.src, "{s} operator not allowed for types", .{@tagName(op)});2585 return sema.mod.fail(&block.base, inst.base.src, "{s} operator not allowed for types", .{@tagName(op)});
2575 }2586 }
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));
2577 }2588 }
2578 return sema.mod.fail(&block.base, inst.base.src, "TODO implement more cmp analysis", .{});2589 return sema.mod.fail(&block.base, inst.base.src, "TODO implement more cmp analysis", .{});
2579}2590}
...@@ -2584,7 +2595,7 @@ fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!...@@ -2584,7 +2595,7 @@ fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!
25842595
2585 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;2596 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
2586 const operand = sema.resolveInst(block, inst_data.operand);2597 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);
2588}2599}
25892600
2590fn zirTypeofPeer(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {2601fn 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...@@ -2607,7 +2618,7 @@ fn zirTypeofPeer(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerEr
2607 }2618 }
26082619
2609 const result_type = try sema.resolvePeerTypes(block, inst_list, src_list);2620 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);
2611}2622}
26122623
2613fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {2624fn 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...@@ -2621,7 +2632,7 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
2621 const bool_type = Type.initTag(.bool);2632 const bool_type = Type.initTag(.bool);
2622 const operand = try sema.coerce(scope, bool_type, uncasted_operand);2633 const operand = try sema.coerce(scope, bool_type, uncasted_operand);
2623 if (try mod.resolveDefinedValue(scope, operand)) |val| {2634 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());
2625 }2636 }
2626 try sema.requireRuntimeBlock(block, src);2637 try sema.requireRuntimeBlock(block, src);
2627 return block.addUnOp(src, bool_type, .not, operand);2638 return block.addUnOp(src, bool_type, .not, operand);
...@@ -2646,9 +2657,9 @@ fn zirBoolOp(...@@ -2646,9 +2657,9 @@ fn zirBoolOp(
2646 if (lhs.value()) |lhs_val| {2657 if (lhs.value()) |lhs_val| {
2647 if (rhs.value()) |rhs_val| {2658 if (rhs.value()) |rhs_val| {
2648 if (is_bool_or) {2659 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());
2650 } else {2661 } 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());
2652 }2663 }
2653 }2664 }
2654 }2665 }
...@@ -2717,7 +2728,7 @@ fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) Inne...@@ -2717,7 +2728,7 @@ fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) Inne
27172728
2718 if (try mod.resolveDefinedValue(scope, cond)) |cond_val| {2729 if (try mod.resolveDefinedValue(scope, cond)) |cond_val| {
2719 const body = if (cond_val.toBool()) &inst.positionals.then_body else &inst.positionals.else_body;2730 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.*);
2721 return mod.constNoReturn(scope, inst.base.src);2732 return mod.constNoReturn(scope, inst.base.src);
2722 }2733 }
27232734
...@@ -2728,13 +2739,13 @@ fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) Inne...@@ -2728,13 +2739,13 @@ fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) Inne
2728 .owner_decl = parent_block.owner_decl,2739 .owner_decl = parent_block.owner_decl,
2729 .src_decl = parent_block.src_decl,2740 .src_decl = parent_block.src_decl,
2730 .instructions = .{},2741 .instructions = .{},
2731 .arena = parent_block.arena,2742 .arena = sema.arena,
2732 .inlining = parent_block.inlining,2743 .inlining = parent_block.inlining,
2733 .is_comptime = parent_block.is_comptime,2744 .is_comptime = parent_block.is_comptime,
2734 .branch_quota = parent_block.branch_quota,2745 .branch_quota = parent_block.branch_quota,
2735 };2746 };
2736 defer true_block.instructions.deinit(mod.gpa);2747 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
2739 var false_block: Scope.Block = .{2750 var false_block: Scope.Block = .{
2740 .parent = parent_block,2751 .parent = parent_block,
...@@ -2743,16 +2754,16 @@ fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) Inne...@@ -2743,16 +2754,16 @@ fn zirCondbr(sema: *Sema, parent_block: *Scope.Block, inst: zir.Inst.Index) Inne
2743 .owner_decl = parent_block.owner_decl,2754 .owner_decl = parent_block.owner_decl,
2744 .src_decl = parent_block.src_decl,2755 .src_decl = parent_block.src_decl,
2745 .instructions = .{},2756 .instructions = .{},
2746 .arena = parent_block.arena,2757 .arena = sema.arena,
2747 .inlining = parent_block.inlining,2758 .inlining = parent_block.inlining,
2748 .is_comptime = parent_block.is_comptime,2759 .is_comptime = parent_block.is_comptime,
2749 .branch_quota = parent_block.branch_quota,2760 .branch_quota = parent_block.branch_quota,
2750 };2761 };
2751 defer false_block.instructions.deinit(mod.gpa);2762 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) };2765 const then_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, true_block.instructions.items) };
2755 const else_body: ir.Body = .{ .instructions = try block.arena.dupe(*Inst, false_block.instructions.items) };2766 const else_body: ir.Body = .{ .instructions = try sema.arena.dupe(*Inst, false_block.instructions.items) };
2756 return mod.addCondBr(parent_block, inst.base.src, cond, then_body, else_body);2767 return mod.addCondBr(parent_block, inst.base.src, cond, then_body, else_body);
2757}2768}
27582769
...@@ -2797,7 +2808,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inne...@@ -2797,7 +2808,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inne
2797 const inst_data = sema.code.instructions.items(.data)[inst].ptr_type_simple;2808 const inst_data = sema.code.instructions.items(.data)[inst].ptr_type_simple;
2798 const elem_type = try sema.resolveType(block, .unneeded, inst_data.elem_type);2809 const elem_type = try sema.resolveType(block, .unneeded, inst_data.elem_type);
2799 const ty = try sema.mod.ptrType(2810 const ty = try sema.mod.ptrType(
2800 block.arena,2811 sema.arena,
2801 elem_type,2812 elem_type,
2802 null,2813 null,
2803 0,2814 0,
...@@ -2808,7 +2819,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inne...@@ -2808,7 +2819,7 @@ fn zirPtrTypeSimple(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inne
2808 inst_data.is_volatile,2819 inst_data.is_volatile,
2809 inst_data.size,2820 inst_data.size,
2810 );2821 );
2811 return sema.mod.constType(block.arena, .unneeded, ty);2822 return sema.mod.constType(sema.arena, .unneeded, ty);
2812}2823}
28132824
2814fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {2825fn 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...@@ -2861,7 +2872,17 @@ fn zirPtrType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
2861 inst_data.flags.is_volatile,2872 inst_data.flags.is_volatile,
2862 inst_data.size,2873 inst_data.size,
2863 );2874 );
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", .{});
2865}2886}
28662887
2867fn requireFunctionBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {2888fn requireFunctionBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {
...@@ -2877,7 +2898,7 @@ fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void...@@ -2877,7 +2898,7 @@ fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void
2877 }2898 }
2878}2899}
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 {
2881 if (!ty.isValidVarType(false)) {2902 if (!ty.isValidVarType(false)) {
2882 return mod.fail(&block.base, src, "variable of type '{}' must be const or comptime", .{ty});2903 return mod.fail(&block.base, src, "variable of type '{}' must be const or comptime", .{ty});
2883 }2904 }
...@@ -2890,7 +2911,7 @@ pub const PanicId = enum {...@@ -2890,7 +2911,7 @@ pub const PanicId = enum {
2890};2911};
28912912
2892fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id: PanicId) !void {2913fn 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);
2894 block_inst.* = .{2915 block_inst.* = .{
2895 .base = .{2916 .base = .{
2896 .tag = Inst.Block.base_tag,2917 .tag = Inst.Block.base_tag,
...@@ -2898,14 +2919,14 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id:...@@ -2898,14 +2919,14 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id:
2898 .src = ok.src,2919 .src = ok.src,
2899 },2920 },
2900 .body = .{2921 .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.
2902 },2923 },
2903 };2924 };
29042925
2905 const ok_body: ir.Body = .{2926 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.
2907 };2928 };
2908 const br_void = try parent_block.arena.create(Inst.BrVoid);2929 const br_void = try sema.arena.create(Inst.BrVoid);
2909 br_void.* = .{2930 br_void.* = .{
2910 .base = .{2931 .base = .{
2911 .tag = .br_void,2932 .tag = .br_void,
...@@ -2923,7 +2944,7 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id:...@@ -2923,7 +2944,7 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id:
2923 .owner_decl = parent_block.owner_decl,2944 .owner_decl = parent_block.owner_decl,
2924 .src_decl = parent_block.src_decl,2945 .src_decl = parent_block.src_decl,
2925 .instructions = .{},2946 .instructions = .{},
2926 .arena = parent_block.arena,2947 .arena = sema.arena,
2927 .inlining = parent_block.inlining,2948 .inlining = parent_block.inlining,
2928 .is_comptime = parent_block.is_comptime,2949 .is_comptime = parent_block.is_comptime,
2929 .branch_quota = parent_block.branch_quota,2950 .branch_quota = parent_block.branch_quota,
...@@ -2933,9 +2954,9 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id:...@@ -2933,9 +2954,9 @@ fn addSafetyCheck(sema: *Sema, parent_block: *Scope.Block, ok: *Inst, panic_id:
29332954
2934 _ = try mod.safetyPanic(&fail_block, ok.src, panic_id);2955 _ = 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);
2939 condbr.* = .{2960 condbr.* = .{
2940 .base = .{2961 .base = .{
2941 .tag = .condbr,2962 .tag = .condbr,
...@@ -3296,7 +3317,7 @@ fn storePtr(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ptr: *Inst, uncas...@@ -3296,7 +3317,7 @@ fn storePtr(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ptr: *Inst, uncas
3296 const elem_ty = ptr.ty.elemType();3317 const elem_ty = ptr.ty.elemType();
3297 const value = try sema.coerce(scope, elem_ty, uncasted_value);3318 const value = try sema.coerce(scope, elem_ty, uncasted_value);
3298 if (elem_ty.onePossibleValue() != null)3319 if (elem_ty.onePossibleValue() != null)
3299 return sema.mod.constVoid(block.arena, .unneeded);3320 return sema.mod.constVoid(sema.arena, .unneeded);
33003321
3301 // TODO handle comptime pointer writes3322 // TODO handle comptime pointer writes
3302 // TODO handle if the element type requires comptime3323 // TODO handle if the element type requires comptime
...@@ -3438,7 +3459,7 @@ fn analyzeIsNull(...@@ -3438,7 +3459,7 @@ fn analyzeIsNull(
3438 if (operand.value()) |opt_val| {3459 if (operand.value()) |opt_val| {
3439 const is_null = opt_val.isNull();3460 const is_null = opt_val.isNull();
3440 const bool_value = if (invert_logic) !is_null else is_null;3461 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);
3442 }3463 }
3443 try sema.requireRuntimeBlock(block, src);3464 try sema.requireRuntimeBlock(block, src);
3444 const inst_tag: Inst.Tag = if (invert_logic) .is_non_null else .is_null;3465 const inst_tag: Inst.Tag = if (invert_logic) .is_non_null else .is_null;
...@@ -3447,11 +3468,11 @@ fn analyzeIsNull(...@@ -3447,11 +3468,11 @@ fn analyzeIsNull(
34473468
3448fn analyzeIsErr(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, operand: *Inst) InnerError!*Inst {3469fn analyzeIsErr(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, operand: *Inst) InnerError!*Inst {
3449 const ot = operand.ty.zigTypeTag();3470 const ot = operand.ty.zigTypeTag();
3450 if (ot != .ErrorSet and ot != .ErrorUnion) return mod.constBool(block.arena, src, false);3471 if (ot != .ErrorSet and ot != .ErrorUnion) return mod.constBool(sema.arena, src, false);
3451 if (ot == .ErrorSet) return mod.constBool(block.arena, src, true);3472 if (ot == .ErrorSet) return mod.constBool(sema.arena, src, true);
3452 assert(ot == .ErrorUnion);3473 assert(ot == .ErrorUnion);
3453 if (operand.value()) |err_union| {3474 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);
3455 }3476 }
3456 try sema.requireRuntimeBlock(block, src);3477 try sema.requireRuntimeBlock(block, src);
3457 return mod.addUnOp(b, src, Type.initTag(.bool), .is_err, operand);3478 return mod.addUnOp(b, src, Type.initTag(.bool), .is_err, operand);
...@@ -3616,7 +3637,7 @@ fn cmpNumeric(...@@ -3616,7 +3637,7 @@ fn cmpNumeric(
36163637
3617 if (lhs.value()) |lhs_val| {3638 if (lhs.value()) |lhs_val| {
3618 if (rhs.value()) |rhs_val| {3639 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));
3620 }3641 }
3621 }3642 }
36223643
...@@ -3684,8 +3705,8 @@ fn cmpNumeric(...@@ -3684,8 +3705,8 @@ fn cmpNumeric(
3684 const zcmp = lhs_val.orderAgainstZero();3705 const zcmp = lhs_val.orderAgainstZero();
3685 if (lhs_val.floatHasFraction()) {3706 if (lhs_val.floatHasFraction()) {
3686 switch (op) {3707 switch (op) {
3687 .eq => return mod.constBool(block.arena, src, false),3708 .eq => return mod.constBool(sema.arena, src, false),
3688 .neq => return mod.constBool(block.arena, src, true),3709 .neq => return mod.constBool(sema.arena, src, true),
3689 else => {},3710 else => {},
3690 }3711 }
3691 if (zcmp == .lt) {3712 if (zcmp == .lt) {
...@@ -3719,8 +3740,8 @@ fn cmpNumeric(...@@ -3719,8 +3740,8 @@ fn cmpNumeric(
3719 const zcmp = rhs_val.orderAgainstZero();3740 const zcmp = rhs_val.orderAgainstZero();
3720 if (rhs_val.floatHasFraction()) {3741 if (rhs_val.floatHasFraction()) {
3721 switch (op) {3742 switch (op) {
3722 .eq => return mod.constBool(block.arena, src, false),3743 .eq => return mod.constBool(sema.arena, src, false),
3723 .neq => return mod.constBool(block.arena, src, true),3744 .neq => return mod.constBool(sema.arena, src, true),
3724 else => {},3745 else => {},
3725 }3746 }
3726 if (zcmp == .lt) {3747 if (zcmp == .lt) {
src/ir.zig+1-2
...@@ -25,8 +25,7 @@ pub const Inst = struct {...@@ -25,8 +25,7 @@ pub const Inst = struct {
25 /// lifetimes of operands are encoded elsewhere.25 /// lifetimes of operands are encoded elsewhere.
26 deaths: DeathsInt = undefined,26 deaths: DeathsInt = undefined,
27 ty: Type,27 ty: Type,
28 /// Byte offset into the source.28 src: Module.LazySrcLoc,
29 src: usize,
3029
31 pub const DeathsInt = u16;30 pub const DeathsInt = u16;
32 pub const DeathsBitIndex = std.math.Log2Int(DeathsInt);31 pub const DeathsBitIndex = std.math.Log2Int(DeathsInt);
src/zir.zig+37-39
...@@ -12,6 +12,7 @@ const TypedValue = @import("TypedValue.zig");...@@ -12,6 +12,7 @@ const TypedValue = @import("TypedValue.zig");
12const ir = @import("ir.zig");12const ir = @import("ir.zig");
13const Module = @import("Module.zig");13const Module = @import("Module.zig");
14const ast = std.zig.ast;14const ast = std.zig.ast;
15const LazySrcLoc = Module.LazySrcLoc;
1516
16/// The minimum amount of information needed to represent a list of ZIR instructions.17/// The minimum amount of information needed to represent a list of ZIR instructions.
17/// Once this structure is completed, it can be used to generate TZIR, followed by18/// Once this structure is completed, it can be used to generate TZIR, followed by
...@@ -749,39 +750,39 @@ pub const Inst = struct {...@@ -749,39 +750,39 @@ pub const Inst = struct {
749 /// Suspend an async function. The suspend block has any number of statements in it.750 /// Suspend an async function. The suspend block has any number of statements in it.
750 /// Uses the `block` union field.751 /// Uses the `block` union field.
751 suspend_block,752 suspend_block,
752 /// A switch expression.753 // /// A switch expression.
753 /// lhs is target, SwitchBr[rhs]754 // /// lhs is target, SwitchBr[rhs]
754 /// All prongs of target handled.755 // /// All prongs of target handled.
755 switch_br,756 // switch_br,
756 /// Same as switch_br, except has a range field.757 // /// Same as switch_br, except has a range field.
757 switch_br_range,758 // switch_br_range,
758 /// Same as switch_br, except has an else prong.759 // /// Same as switch_br, except has an else prong.
759 switch_br_else,760 // switch_br_else,
760 /// Same as switch_br_else, except has a range field.761 // /// Same as switch_br_else, except has a range field.
761 switch_br_else_range,762 // switch_br_else_range,
762 /// Same as switch_br, except has an underscore prong.763 // /// Same as switch_br, except has an underscore prong.
763 switch_br_underscore,764 // switch_br_underscore,
764 /// Same as switch_br, except has a range field.765 // /// Same as switch_br, except has a range field.
765 switch_br_underscore_range,766 // switch_br_underscore_range,
766 /// Same as `switch_br` but the target is a pointer to the value being switched on.767 // /// Same as `switch_br` but the target is a pointer to the value being switched on.
767 switch_br_ref,768 // switch_br_ref,
768 /// Same as `switch_br_range` but the target is a pointer to the value being switched on.769 // /// Same as `switch_br_range` but the target is a pointer to the value being switched on.
769 switch_br_ref_range,770 // switch_br_ref_range,
770 /// Same as `switch_br_else` but the target is a pointer to the value being switched on.771 // /// Same as `switch_br_else` but the target is a pointer to the value being switched on.
771 switch_br_ref_else,772 // switch_br_ref_else,
772 /// Same as `switch_br_else_range` but the target is a pointer to the773 // /// Same as `switch_br_else_range` but the target is a pointer to the
773 /// value being switched on.774 // /// value being switched on.
774 switch_br_ref_else_range,775 // switch_br_ref_else_range,
775 /// Same as `switch_br_underscore` but the target is a pointer to the value776 // /// Same as `switch_br_underscore` but the target is a pointer to the value
776 /// being switched on.777 // /// being switched on.
777 switch_br_ref_underscore,778 // switch_br_ref_underscore,
778 /// Same as `switch_br_underscore_range` but the target is a pointer to779 // /// Same as `switch_br_underscore_range` but the target is a pointer to
779 /// the value being switched on.780 // /// the value being switched on.
780 switch_br_ref_underscore_range,781 // switch_br_ref_underscore_range,
781 /// A range in a switch case, `lhs...rhs`.782 // /// A range in a switch case, `lhs...rhs`.
782 /// Only checks that `lhs >= rhs` if they are ints, everything else is783 // /// Only checks that `lhs >= rhs` if they are ints, everything else is
783 /// validated by the switch_br instruction.784 // /// validated by the switch_br instruction.
784 switch_range,785 // switch_range,
785786
786 comptime {787 comptime {
787 assert(@sizeOf(Tag) == 1);788 assert(@sizeOf(Tag) == 1);
...@@ -915,7 +916,6 @@ pub const Inst = struct {...@@ -915,7 +916,6 @@ pub const Inst = struct {
915 .resolve_inferred_alloc,916 .resolve_inferred_alloc,
916 .set_eval_branch_quota,917 .set_eval_branch_quota,
917 .compile_log,918 .compile_log,
918 .void_value,
919 .switch_range,919 .switch_range,
920 .@"resume",920 .@"resume",
921 .@"await",921 .@"await",
...@@ -934,8 +934,6 @@ pub const Inst = struct {...@@ -934,8 +934,6 @@ pub const Inst = struct {
934 .container_field_named,934 .container_field_named,
935 .container_field_typed,935 .container_field_typed,
936 .container_field,936 .container_field,
937 .switch_br,
938 .switch_br_ref,
939 .@"suspend",937 .@"suspend",
940 .suspend_block,938 .suspend_block,
941 => true,939 => true,
...@@ -967,7 +965,7 @@ pub const Inst = struct {...@@ -967,7 +965,7 @@ pub const Inst = struct {
967 /// The meaning of this operand depends on the corresponding `Tag`.965 /// The meaning of this operand depends on the corresponding `Tag`.
968 operand: Ref,966 operand: Ref,
969967
970 fn src(self: @This()) LazySrcLoc {968 pub fn src(self: @This()) LazySrcLoc {
971 return .{ .node_offset = self.src_node };969 return .{ .node_offset = self.src_node };
972 }970 }
973 },971 },
...@@ -978,7 +976,7 @@ pub const Inst = struct {...@@ -978,7 +976,7 @@ pub const Inst = struct {
978 /// The meaning of this operand depends on the corresponding `Tag`.976 /// The meaning of this operand depends on the corresponding `Tag`.
979 operand: Ref,977 operand: Ref,
980978
981 fn src(self: @This()) LazySrcLoc {979 pub fn src(self: @This()) LazySrcLoc {
982 return .{ .token_offset = self.src_tok };980 return .{ .token_offset = self.src_tok };
983 }981 }
984 },982 },
...@@ -990,7 +988,7 @@ pub const Inst = struct {...@@ -990,7 +988,7 @@ pub const Inst = struct {
990 /// `Tag` determines what lives there.988 /// `Tag` determines what lives there.
991 payload_index: u32,989 payload_index: u32,
992990
993 fn src(self: @This()) LazySrcLoc {991 pub fn src(self: @This()) LazySrcLoc {
994 return .{ .node_offset = self.src_node };992 return .{ .node_offset = self.src_node };
995 }993 }
996 },994 },