P5.7 Step D: re-express metatype define() as sx over register_type
define(handle, info) is now an ordinary sx fn in modules/std/meta.sx: it matches the TypeInfo union and calls the abi(.compiler) register_type primitive with the matching kind code, decoding the variant/field/element list into []Member. An all-void enum variant set registers as kind 2 (actual enum); any payload variant as kind 3 (tagged_union). To support matching the TypeInfo VALUE in the comptime VM, added tagged-union value support: kindOf now treats tagged_union as a by-address aggregate, enum_tag reads the tag word at offset 0, and a new enum_payload arm reads the active payload at tag_size (both bail loudly on backing_type unions, whose layout differs). register_type's duplicate-name diagnostics now include the offending name. Dropped the define interception in tryLowerReflectionCall; the .enum(...) arg infers TypeInfo from the sx fn's param type via the ordinary call path. Regenerated 1179/1180 diagnostic snapshots (same span/line; the message now names register_type instead of define()). define/type_info builtins still exist pending dead-code removal.
This commit is contained in:
@@ -902,9 +902,44 @@ pub const Vm = struct {
|
||||
if (oty.isBuiltin()) return .{ .value = v }; // already an integer tag
|
||||
const table = try self.requireTable();
|
||||
if (table.get(oty) == .@"enum") return .{ .value = v }; // payloadless: word IS the tag
|
||||
self.detail = "comptime VM: enum_tag on a tagged union not yet ported";
|
||||
if (table.get(oty) == .tagged_union) {
|
||||
// `{ tag@0, payload@tag_size }` — read the tag word from the
|
||||
// value's address. A `backing_type` union lays the tag out
|
||||
// differently (it's a field of the backing struct), so bail
|
||||
// rather than read the wrong bytes.
|
||||
const tu = table.get(oty).tagged_union;
|
||||
if (tu.backing_type != null) {
|
||||
self.detail = "comptime VM: enum_tag on a backing_type tagged union not yet ported (layout differs)";
|
||||
return error.Unsupported;
|
||||
}
|
||||
return .{ .value = try self.readField(table, v, tu.tag_type) };
|
||||
}
|
||||
self.detail = "comptime VM: enum_tag on an unexpected operand type";
|
||||
return error.Unsupported;
|
||||
},
|
||||
// Extract a tagged union's active payload — the bytes at `tag_size`,
|
||||
// read as the variant's payload type. Mirrors the `enum_init` write
|
||||
// layout (`{ tag@0, [N x i8] payload@tag_size }`). The match-arm
|
||||
// capture binding (`case .v: (x)`) uses this.
|
||||
.enum_payload => |fa| {
|
||||
const oty = (try self.refTy(ref_types, fa.base));
|
||||
const base = frame.get(fa.base.index());
|
||||
const table = try self.requireTable();
|
||||
if (oty.isBuiltin() or table.get(oty) != .tagged_union) {
|
||||
self.detail = "comptime VM: enum_payload on a non-tagged-union operand";
|
||||
return error.Unsupported;
|
||||
}
|
||||
const tu = table.get(oty).tagged_union;
|
||||
if (tu.backing_type != null) {
|
||||
self.detail = "comptime VM: enum_payload on a backing_type tagged union not yet ported (layout differs)";
|
||||
return error.Unsupported;
|
||||
}
|
||||
if (fa.field_index >= tu.fields.len)
|
||||
return self.failMsg("comptime VM: enum_payload variant index out of range");
|
||||
const payload_ty = tu.fields[fa.field_index].ty;
|
||||
const tag_size: Addr = @intCast(table.typeSizeBytes(tu.tag_type));
|
||||
return .{ .value = try self.readField(table, base + tag_size, payload_ty) };
|
||||
},
|
||||
|
||||
// `is_comptime()` — always true on the comptime VM (folds to false in
|
||||
// compiled code). Mirrors the legacy interp's `.is_comptime => true`.
|
||||
@@ -1786,7 +1821,7 @@ pub const Vm = struct {
|
||||
const names = self.gpa.alloc(types.StringId, members.items.len) catch return self.failMsg("comptime register_type: out of memory");
|
||||
for (members.items, 0..) |m, i| {
|
||||
if (m.ty != .void) return self.failMsg("comptime register_type: payload variant — use kind 3 (tagged_union)");
|
||||
for (names[0..i]) |prev| if (prev == m.name) return self.failMsg("comptime register_type: duplicate enum variant");
|
||||
for (names[0..i]) |prev| if (prev == m.name) return self.failFmt("comptime register_type: duplicate variant name '{s}'", .{tbl.getString(m.name)});
|
||||
names[i] = m.name;
|
||||
}
|
||||
tbl.replaceKeyedInfo(handle, .{ .@"enum" = .{ .name = ident.name, .variants = names, .nominal_id = ident.nominal_id } });
|
||||
@@ -1794,7 +1829,7 @@ pub const Vm = struct {
|
||||
1, 3 => { // struct / tagged_union — `{ name, ty }` fields (dup names rejected)
|
||||
const flds = self.gpa.alloc(types.TypeInfo.StructInfo.Field, members.items.len) catch return self.failMsg("comptime register_type: out of memory");
|
||||
for (members.items, 0..) |m, i| {
|
||||
for (flds[0..i]) |prev| if (prev.name == m.name) return self.failMsg("comptime register_type: duplicate member name");
|
||||
for (flds[0..i]) |prev| if (prev.name == m.name) return self.failFmt("comptime register_type: duplicate member name '{s}'", .{tbl.getString(m.name)});
|
||||
flds[i] = .{ .name = m.name, .ty = m.ty };
|
||||
}
|
||||
const full: types.TypeInfo = if (kind == 1)
|
||||
@@ -2212,7 +2247,9 @@ pub const Vm = struct {
|
||||
.pointer, .many_pointer, .function => .word,
|
||||
.@"enum" => .word, // payloadless enum: i64 (or its backing) — a word
|
||||
.error_set => .word, // the error channel is a u32 tag id — a word
|
||||
.@"struct", .array, .tuple, .slice => .aggregate,
|
||||
// A tagged union is a `{ tag@0, [N x i8] payload@tag_size }` value held
|
||||
// by-address (like a struct) — same as the `enum_init` write path.
|
||||
.@"struct", .array, .tuple, .slice, .tagged_union => .aggregate,
|
||||
// `?T`: a pointer child is null-as-0 (word); else `{T, i1}` by-address.
|
||||
.optional => |o| if (optChildIsPtr(table, o.child)) .word else .aggregate,
|
||||
else => .unsupported,
|
||||
|
||||
@@ -1677,33 +1677,14 @@ pub fn tryLowerReflectionCall(self: *Lowering, name: []const u8, c: *const ast.C
|
||||
// classification covers all 7; it runs before dispatch.
|
||||
if (self.reflectionTypeArgGuard(name, c)) |sentinel| return sentinel;
|
||||
|
||||
// `declare(name)` is now an ordinary sx function (`modules/std/meta.sx`)
|
||||
// written over the `abi(.compiler)` primitive `declare_type` — no longer
|
||||
// intercepted here. (`preregisterForwardTypes` still scans for the literal
|
||||
// `declare("Name")` spelling so a `*Name` self-reference forward-registers
|
||||
// before the body lowers; the sx `declare` calls `declare_type`, which
|
||||
// returns that same forward slot.)
|
||||
if (std.mem.eql(u8, name, "define")) {
|
||||
// Comptime type-construction primitive: complete a declare()'d slot
|
||||
// from a TypeInfo value. `define(handle, info)`.
|
||||
if (c.args.len != 2) {
|
||||
if (self.diagnostics) |d| d.addFmt(.err, c.callee.span, "define(handle, info) takes exactly two arguments", .{});
|
||||
return Ref.none;
|
||||
}
|
||||
const handle_ref = self.lowerExpr(c.args[0]);
|
||||
// Lower the info arg with `TypeInfo` as the target type so its `.enum(…)`
|
||||
// enum-literal infers correctly (we intercept the call, bypassing the
|
||||
// normal param-type-threading the regular call path does).
|
||||
const saved_tt = self.target_type;
|
||||
if (self.module.types.findByName(self.module.types.internString("TypeInfo"))) |ti|
|
||||
self.target_type = ti;
|
||||
const info_ref = self.lowerExpr(c.args[1]);
|
||||
self.target_type = saved_tt;
|
||||
const args_owned = self.alloc.dupe(Ref, &.{ handle_ref, info_ref }) catch return Ref.none;
|
||||
// define returns the (now-completed) handle as a `Type` value, so the
|
||||
// one-shot constructor form chains: `T :: define(declare(), info)`.
|
||||
return self.builder.callBuiltin(.define, args_owned, .type_value);
|
||||
}
|
||||
// `declare(name)` and `define(handle, info)` are now ordinary sx functions
|
||||
// (`modules/std/meta.sx`) written over the `abi(.compiler)` primitives
|
||||
// (`declare_type` / `register_type`) — no longer intercepted here.
|
||||
// (`preregisterForwardTypes` still scans for the literal `declare("Name")`
|
||||
// spelling so a `*Name` self-reference forward-registers before the body
|
||||
// lowers; the sx `declare` calls `declare_type`, which returns that slot.
|
||||
// The `.enum(…)` arg to `define` now infers `TypeInfo` from the sx fn's
|
||||
// declared param type via the ordinary call path's target-type threading.)
|
||||
if (std.mem.eql(u8, name, "type_info")) {
|
||||
// Comptime reflection-into-data: reflect a type INTO a `TypeInfo`
|
||||
// value (the inverse of `define`'s decode). Resolve `$T` at lower
|
||||
|
||||
Reference in New Issue
Block a user