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 {
54705470}
54715471
54725472fn 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 {
54735477 const mod = sema.mod;
54745478 const ptr_ty = (try sema.ptrType(.{
54755479 .child = mod.intern_pool.typeOf(val),
......@@ -5479,13 +5483,13 @@ fn anonDeclRef(sema: *Sema, val: InternPool.Index) CompileError!Air.Inst.Ref {
54795483 .address_space = .generic,
54805484 },
54815485 })).toIntern();
5482 return Air.internedToRef(try mod.intern(.{ .ptr = .{
5486 return mod.intern(.{ .ptr = .{
54835487 .ty = ptr_ty,
54845488 .addr = .{ .anon_decl = .{
54855489 .val = val,
54865490 .orig_ty = ptr_ty,
54875491 } },
5488 } }));
5492 } });
54895493}
54905494
54915495fn 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
1703117035 // is_allowzero: bool,
1703217036 Value.makeBool(info.flags.is_allowzero).toIntern(),
1703317037 // sentinel: ?*const anyopaque,
17034 (try sema.optRefValue(block, info.child.toType(), switch (info.sentinel) {
17038 (try sema.optRefValue(switch (info.sentinel) {
1703517039 .none => null,
1703617040 else => info.sentinel.toValue(),
1703717041 })).toIntern(),
......@@ -17066,7 +17070,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1706617070 // child: type,
1706717071 info.elem_type.toIntern(),
1706817072 // sentinel: ?*const anyopaque,
17069 (try sema.optRefValue(block, info.elem_type, info.sentinel)).toIntern(),
17073 (try sema.optRefValue(info.sentinel)).toIntern(),
1707017074 };
1707117075 return Air.internedToRef((try mod.intern(.{ .un = .{
1707217076 .ty = type_info_ty.toIntern(),
......@@ -17593,7 +17597,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1759317597
1759417598 const is_comptime = field_val != .none;
1759517599 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);
1759717601 const struct_field_fields = .{
1759817602 // name: []const u8,
1759917603 name_val,
......@@ -17647,7 +17651,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1764717651 };
1764817652
1764917653 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);
1765117655 const alignment = switch (struct_type.layout) {
1765217656 .Packed => .none,
1765317657 else => try sema.structFieldAlignment(
......@@ -21367,28 +21371,9 @@ fn zirTypeName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
2136721371 const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
2136821372 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
2137321374 var bytes = std.ArrayList(u8).init(sema.arena);
21374 defer bytes.deinit();
2137521375 try ty.print(bytes.writer(), mod);
21376
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);
21376 return addStrLitNoAlias(sema, bytes.items);
2139221377}
2139321378
2139421379fn zirFrameType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -26271,13 +26256,8 @@ fn fieldPtr(
2627126256 switch (inner_ty.zigTypeTag(mod)) {
2627226257 .Array => {
2627326258 if (ip.stringEqlSlice(field_name, "len")) {
26274 var anon_decl = try block.startAnonDecl();
26275 defer anon_decl.deinit();
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 ));
26259 const int_val = try mod.intValue(Type.usize, inner_ty.arrayLen(mod));
26260 return anonDeclRef(sema, int_val.toIntern());
2628126261 } else {
2628226262 return sema.fail(
2628326263 block,
......@@ -26386,20 +26366,14 @@ fn fieldPtr(
2638626366 else => unreachable,
2638726367 }
2638826368
26389 var anon_decl = try block.startAnonDecl();
26390 defer anon_decl.deinit();
2639126369 const error_set_type = if (!child_type.isAnyError(mod))
2639226370 child_type
2639326371 else
2639426372 try mod.singleErrorSetType(field_name);
26395 return sema.analyzeDeclRef(try anon_decl.finish(
26396 error_set_type,
26397 (try mod.intern(.{ .err = .{
26398 .ty = error_set_type.toIntern(),
26399 .name = field_name,
26400 } })).toValue(),
26401 .none, // default alignment
26402 ));
26373 return anonDeclRef(sema, try mod.intern(.{ .err = .{
26374 .ty = error_set_type.toIntern(),
26375 .name = field_name,
26376 } }));
2640326377 },
2640426378 .Union => {
2640526379 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
......@@ -26411,13 +26385,8 @@ fn fieldPtr(
2641126385 if (child_type.unionTagType(mod)) |enum_ty| {
2641226386 if (enum_ty.enumFieldIndex(field_name, mod)) |field_index| {
2641326387 const field_index_u32: u32 = @intCast(field_index);
26414 var anon_decl = try block.startAnonDecl();
26415 defer anon_decl.deinit();
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 ));
26388 const idx_val = try mod.enumValueFieldIndex(enum_ty, field_index_u32);
26389 return anonDeclRef(sema, idx_val.toIntern());
2642126390 }
2642226391 }
2642326392 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
......@@ -26432,13 +26401,8 @@ fn fieldPtr(
2643226401 return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name);
2643326402 };
2643426403 const field_index_u32: u32 = @intCast(field_index);
26435 var anon_decl = try block.startAnonDecl();
26436 defer anon_decl.deinit();
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 ));
26404 const idx_val = try mod.enumValueFieldIndex(child_type, field_index_u32);
26405 return anonDeclRef(sema, idx_val.toIntern());
2644226406 },
2644326407 .Struct, .Opaque => {
2644426408 if (child_type.getNamespaceIndex(mod).unwrap()) |namespace| {
......@@ -31607,31 +31571,13 @@ fn ensureFuncBodyAnalyzed(sema: *Sema, func: InternPool.Index) CompileError!void
3160731571 };
3160831572}
3160931573
31610fn refValue(sema: *Sema, block: *Block, ty: Type, 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 {
31574fn optRefValue(sema: *Sema, opt_val: ?Value) !Value {
3162931575 const mod = sema.mod;
3163031576 const ptr_anyopaque_ty = try mod.singleConstPtrType(Type.anyopaque);
3163131577 return (try mod.intern(.{ .opt = .{
3163231578 .ty = (try mod.optionalType(ptr_anyopaque_ty.toIntern())).toIntern(),
3163331579 .val = if (opt_val) |val| (try mod.getCoerced(
31634 try sema.refValue(block, ty, val),
31580 (try sema.refValue(val.toIntern())).toValue(),
3163531581 ptr_anyopaque_ty,
3163631582 )).toIntern() else .none,
3163731583 } })).toValue();
......@@ -31693,15 +31639,8 @@ fn analyzeRef(
3169331639 switch (mod.intern_pool.indexToKey(val.toIntern())) {
3169431640 .extern_func => |extern_func| return sema.analyzeDeclRef(extern_func.decl),
3169531641 .func => |func| return sema.analyzeDeclRef(func.owner_decl),
31696 else => {},
31642 else => return anonDeclRef(sema, val.toIntern()),
3169731643 }
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 ));
3170531644 }
3170631645
3170731646 try sema.requireRuntimeBlock(block, src, null);
......@@ -36786,7 +36725,7 @@ fn analyzeComptimeAlloc(
3678636725 },
3678736726 });
3678836727
36789 var anon_decl = try block.startAnonDecl();
36728 var anon_decl = try block.startAnonDecl(); // TODO: comptime value mutation without Decl
3679036729 defer anon_decl.deinit();
3679136730
3679236731 const decl_index = try anon_decl.finish(