authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-24 17:01:06-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-25 19:57:02-07:00
log405ba2680f180541cb91323fad579e2ca5cf5eb0
tree4033c45de48ae625c5aedfc561c0708ab2108110
parent03b473d21becb85d61ddd4b672323256d7747f19

Sema: replace refValue with simpler implementation

Move some more anon decls to the new mechanism

1 files changed, 25 insertions(+), 86 deletions(-)

src/Sema.zig+25-86
...@@ -5470,6 +5470,10 @@ fn addStrLitNoAlias(sema: *Sema, bytes: []const u8) CompileError!Air.Inst.Ref {...@@ -5470,6 +5470,10 @@ fn addStrLitNoAlias(sema: *Sema, bytes: []const u8) CompileError!Air.Inst.Ref {
5470}5470}
54715471
5472fn anonDeclRef(sema: *Sema, val: InternPool.Index) CompileError!Air.Inst.Ref {5472fn anonDeclRef(sema: *Sema, val: InternPool.Index) CompileError!Air.Inst.Ref {
5473 return Air.internedToRef(try refValue(sema, val));
5474}
5475
5476fn refValue(sema: *Sema, val: InternPool.Index) CompileError!InternPool.Index {
5473 const mod = sema.mod;5477 const mod = sema.mod;
5474 const ptr_ty = (try sema.ptrType(.{5478 const ptr_ty = (try sema.ptrType(.{
5475 .child = mod.intern_pool.typeOf(val),5479 .child = mod.intern_pool.typeOf(val),
...@@ -5479,13 +5483,13 @@ fn anonDeclRef(sema: *Sema, val: InternPool.Index) CompileError!Air.Inst.Ref {...@@ -5479,13 +5483,13 @@ fn anonDeclRef(sema: *Sema, val: InternPool.Index) CompileError!Air.Inst.Ref {
5479 .address_space = .generic,5483 .address_space = .generic,
5480 },5484 },
5481 })).toIntern();5485 })).toIntern();
5482 return Air.internedToRef(try mod.intern(.{ .ptr = .{5486 return mod.intern(.{ .ptr = .{
5483 .ty = ptr_ty,5487 .ty = ptr_ty,
5484 .addr = .{ .anon_decl = .{5488 .addr = .{ .anon_decl = .{
5485 .val = val,5489 .val = val,
5486 .orig_ty = ptr_ty,5490 .orig_ty = ptr_ty,
5487 } },5491 } },
5488 } }));5492 } });
5489}5493}
54905494
5491fn zirInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {5495fn zirInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -17031,7 +17035,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17031,7 +17035,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17031 // is_allowzero: bool,17035 // is_allowzero: bool,
17032 Value.makeBool(info.flags.is_allowzero).toIntern(),17036 Value.makeBool(info.flags.is_allowzero).toIntern(),
17033 // sentinel: ?*const anyopaque,17037 // sentinel: ?*const anyopaque,
17034 (try sema.optRefValue(block, info.child.toType(), switch (info.sentinel) {17038 (try sema.optRefValue(switch (info.sentinel) {
17035 .none => null,17039 .none => null,
17036 else => info.sentinel.toValue(),17040 else => info.sentinel.toValue(),
17037 })).toIntern(),17041 })).toIntern(),
...@@ -17066,7 +17070,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17066,7 +17070,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17066 // child: type,17070 // child: type,
17067 info.elem_type.toIntern(),17071 info.elem_type.toIntern(),
17068 // sentinel: ?*const anyopaque,17072 // sentinel: ?*const anyopaque,
17069 (try sema.optRefValue(block, info.elem_type, info.sentinel)).toIntern(),17073 (try sema.optRefValue(info.sentinel)).toIntern(),
17070 };17074 };
17071 return Air.internedToRef((try mod.intern(.{ .un = .{17075 return Air.internedToRef((try mod.intern(.{ .un = .{
17072 .ty = type_info_ty.toIntern(),17076 .ty = type_info_ty.toIntern(),
...@@ -17593,7 +17597,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17593,7 +17597,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1759317597
17594 const is_comptime = field_val != .none;17598 const is_comptime = field_val != .none;
17595 const opt_default_val = if (is_comptime) field_val.toValue() else null;17599 const opt_default_val = if (is_comptime) field_val.toValue() else null;
17596 const default_val_ptr = try sema.optRefValue(block, field_ty.toType(), opt_default_val);17600 const default_val_ptr = try sema.optRefValue(opt_default_val);
17597 const struct_field_fields = .{17601 const struct_field_fields = .{
17598 // name: []const u8,17602 // name: []const u8,
17599 name_val,17603 name_val,
...@@ -17647,7 +17651,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -17647,7 +17651,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
17647 };17651 };
1764817652
17649 const opt_default_val = if (field_init == .none) null else field_init.toValue();17653 const opt_default_val = if (field_init == .none) null else field_init.toValue();
17650 const default_val_ptr = try sema.optRefValue(block, field_ty, opt_default_val);17654 const default_val_ptr = try sema.optRefValue(opt_default_val);
17651 const alignment = switch (struct_type.layout) {17655 const alignment = switch (struct_type.layout) {
17652 .Packed => .none,17656 .Packed => .none,
17653 else => try sema.structFieldAlignment(17657 else => try sema.structFieldAlignment(
...@@ -21367,28 +21371,9 @@ fn zirTypeName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -21367,28 +21371,9 @@ fn zirTypeName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
21367 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };21371 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
21368 const ty = try sema.resolveType(block, ty_src, inst_data.operand);21372 const ty = try sema.resolveType(block, ty_src, inst_data.operand);
2136921373
21370 var anon_decl = try block.startAnonDecl();
21371 defer anon_decl.deinit();
21372
21373 var bytes = std.ArrayList(u8).init(sema.arena);21374 var bytes = std.ArrayList(u8).init(sema.arena);
21374 defer bytes.deinit();
21375 try ty.print(bytes.writer(), mod);21375 try ty.print(bytes.writer(), mod);
2137621376 return addStrLitNoAlias(sema, bytes.items);
21377 const decl_ty = try mod.arrayType(.{
21378 .len = bytes.items.len,
21379 .sentinel = .zero_u8,
21380 .child = .u8_type,
21381 });
21382 const new_decl = try anon_decl.finish(
21383 decl_ty,
21384 (try mod.intern(.{ .aggregate = .{
21385 .ty = decl_ty.toIntern(),
21386 .storage = .{ .bytes = bytes.items },
21387 } })).toValue(),
21388 .none, // default alignment
21389 );
21390
21391 return sema.analyzeDeclRef(new_decl);
21392}21377}
2139321378
21394fn zirFrameType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {21379fn zirFrameType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -26271,13 +26256,8 @@ fn fieldPtr(...@@ -26271,13 +26256,8 @@ fn fieldPtr(
26271 switch (inner_ty.zigTypeTag(mod)) {26256 switch (inner_ty.zigTypeTag(mod)) {
26272 .Array => {26257 .Array => {
26273 if (ip.stringEqlSlice(field_name, "len")) {26258 if (ip.stringEqlSlice(field_name, "len")) {
26274 var anon_decl = try block.startAnonDecl();26259 const int_val = try mod.intValue(Type.usize, inner_ty.arrayLen(mod));
26275 defer anon_decl.deinit();26260 return anonDeclRef(sema, int_val.toIntern());
26276 return sema.analyzeDeclRef(try anon_decl.finish(
26277 Type.usize,
26278 try mod.intValue(Type.usize, inner_ty.arrayLen(mod)),
26279 .none, // default alignment
26280 ));
26281 } else {26261 } else {
26282 return sema.fail(26262 return sema.fail(
26283 block,26263 block,
...@@ -26386,20 +26366,14 @@ fn fieldPtr(...@@ -26386,20 +26366,14 @@ fn fieldPtr(
26386 else => unreachable,26366 else => unreachable,
26387 }26367 }
2638826368
26389 var anon_decl = try block.startAnonDecl();
26390 defer anon_decl.deinit();
26391 const error_set_type = if (!child_type.isAnyError(mod))26369 const error_set_type = if (!child_type.isAnyError(mod))
26392 child_type26370 child_type
26393 else26371 else
26394 try mod.singleErrorSetType(field_name);26372 try mod.singleErrorSetType(field_name);
26395 return sema.analyzeDeclRef(try anon_decl.finish(26373 return anonDeclRef(sema, try mod.intern(.{ .err = .{
26396 error_set_type,26374 .ty = error_set_type.toIntern(),
26397 (try mod.intern(.{ .err = .{26375 .name = field_name,
26398 .ty = error_set_type.toIntern(),26376 } }));
26399 .name = field_name,
26400 } })).toValue(),
26401 .none, // default alignment
26402 ));
26403 },26377 },
26404 .Union => {26378 .Union => {
26405 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {26379 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
...@@ -26411,13 +26385,8 @@ fn fieldPtr(...@@ -26411,13 +26385,8 @@ fn fieldPtr(
26411 if (child_type.unionTagType(mod)) |enum_ty| {26385 if (child_type.unionTagType(mod)) |enum_ty| {
26412 if (enum_ty.enumFieldIndex(field_name, mod)) |field_index| {26386 if (enum_ty.enumFieldIndex(field_name, mod)) |field_index| {
26413 const field_index_u32: u32 = @intCast(field_index);26387 const field_index_u32: u32 = @intCast(field_index);
26414 var anon_decl = try block.startAnonDecl();26388 const idx_val = try mod.enumValueFieldIndex(enum_ty, field_index_u32);
26415 defer anon_decl.deinit();26389 return anonDeclRef(sema, idx_val.toIntern());
26416 return sema.analyzeDeclRef(try anon_decl.finish(
26417 enum_ty,
26418 try mod.enumValueFieldIndex(enum_ty, field_index_u32),
26419 .none, // default alignment
26420 ));
26421 }26390 }
26422 }26391 }
26423 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);26392 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
...@@ -26432,13 +26401,8 @@ fn fieldPtr(...@@ -26432,13 +26401,8 @@ fn fieldPtr(
26432 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);26401 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
26433 };26402 };
26434 const field_index_u32: u32 = @intCast(field_index);26403 const field_index_u32: u32 = @intCast(field_index);
26435 var anon_decl = try block.startAnonDecl();26404 const idx_val = try mod.enumValueFieldIndex(child_type, field_index_u32);
26436 defer anon_decl.deinit();26405 return anonDeclRef(sema, idx_val.toIntern());
26437 return sema.analyzeDeclRef(try anon_decl.finish(
26438 child_type,
26439 try mod.enumValueFieldIndex(child_type, field_index_u32),
26440 .none, // default alignment
26441 ));
26442 },26406 },
26443 .Struct, .Opaque => {26407 .Struct, .Opaque => {
26444 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {26408 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
...@@ -31607,31 +31571,13 @@ fn ensureFuncBodyAnalyzed(sema: *Sema, func: InternPool.Index) CompileError!void...@@ -31607,31 +31571,13 @@ fn ensureFuncBodyAnalyzed(sema: *Sema, func: InternPool.Index) CompileError!void
31607 };31571 };
31608}31572}
3160931573
31610fn refValue(sema: *Sema, block: *Block, ty: Type, val: Value) !Value {31574fn optRefValue(sema: *Sema, opt_val: ?Value) !Value {
31611 const mod = sema.mod;
31612 var anon_decl = try block.startAnonDecl();
31613 defer anon_decl.deinit();
31614 const decl = try anon_decl.finish(
31615 ty,
31616 val,
31617 .none, // default alignment
31618 );
31619 try sema.maybeQueueFuncBodyAnalysis(decl);
31620 try mod.declareDeclDependency(sema.owner_decl_index, decl);
31621 const result = try mod.intern(.{ .ptr = .{
31622 .ty = (try mod.singleConstPtrType(ty)).toIntern(),
31623 .addr = .{ .decl = decl },
31624 } });
31625 return result.toValue();
31626}
31627
31628fn optRefValue(sema: *Sema, block: *Block, ty: Type, opt_val: ?Value) !Value {
31629 const mod = sema.mod;31575 const mod = sema.mod;
31630 const ptr_anyopaque_ty = try mod.singleConstPtrType(Type.anyopaque);31576 const ptr_anyopaque_ty = try mod.singleConstPtrType(Type.anyopaque);
31631 return (try mod.intern(.{ .opt = .{31577 return (try mod.intern(.{ .opt = .{
31632 .ty = (try mod.optionalType(ptr_anyopaque_ty.toIntern())).toIntern(),31578 .ty = (try mod.optionalType(ptr_anyopaque_ty.toIntern())).toIntern(),
31633 .val = if (opt_val) |val| (try mod.getCoerced(31579 .val = if (opt_val) |val| (try mod.getCoerced(
31634 try sema.refValue(block, ty, val),31580 (try sema.refValue(val.toIntern())).toValue(),
31635 ptr_anyopaque_ty,31581 ptr_anyopaque_ty,
31636 )).toIntern() else .none,31582 )).toIntern() else .none,
31637 } })).toValue();31583 } })).toValue();
...@@ -31693,15 +31639,8 @@ fn analyzeRef(...@@ -31693,15 +31639,8 @@ fn analyzeRef(
31693 switch (mod.intern_pool.indexToKey(val.toIntern())) {31639 switch (mod.intern_pool.indexToKey(val.toIntern())) {
31694 .extern_func => |extern_func| return sema.analyzeDeclRef(extern_func.decl),31640 .extern_func => |extern_func| return sema.analyzeDeclRef(extern_func.decl),
31695 .func => |func| return sema.analyzeDeclRef(func.owner_decl),31641 .func => |func| return sema.analyzeDeclRef(func.owner_decl),
31696 else => {},31642 else => return anonDeclRef(sema, val.toIntern()),
31697 }31643 }
31698 var anon_decl = try block.startAnonDecl();
31699 defer anon_decl.deinit();
31700 return sema.analyzeDeclRef(try anon_decl.finish(
31701 operand_ty,
31702 val,
31703 .none, // default alignment
31704 ));
31705 }31644 }
3170631645
31707 try sema.requireRuntimeBlock(block, src, null);31646 try sema.requireRuntimeBlock(block, src, null);
...@@ -36786,7 +36725,7 @@ fn analyzeComptimeAlloc(...@@ -36786,7 +36725,7 @@ fn analyzeComptimeAlloc(
36786 },36725 },
36787 });36726 });
3678836727
36789 var anon_decl = try block.startAnonDecl();36728 var anon_decl = try block.startAnonDecl(); // TODO: comptime value mutation without Decl
36790 defer anon_decl.deinit();36729 defer anon_decl.deinit();
3679136730
36792 const decl_index = try anon_decl.finish(36731 const decl_index = try anon_decl.finish(