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:
@@ -1,4 +1,4 @@
|
||||
error: comptime type construction failed: comptime define(): enum has no variants
|
||||
error: comptime type construction failed: comptime register_type: a type with no members is never valid
|
||||
--> examples/1179-diagnostics-comptime-type-construction-bail.sx:14:10
|
||||
|
|
||||
14 | Empty :: define(declare("Empty"), .enum(.{ variants = .[] }));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
error: comptime type construction failed: comptime define(): duplicate variant name 'value'
|
||||
error: comptime type construction failed: comptime register_type: duplicate member name 'value'
|
||||
--> examples/1180-diagnostics-metatype-duplicate-variant.sx:8:8
|
||||
|
|
||||
8 | Bad :: define(declare("Bad"), .enum(.{ variants = .[
|
||||
|
||||
@@ -6,10 +6,12 @@
|
||||
// types would otherwise intern into every module's type table and shift every
|
||||
// `.ir` snapshot. Import it explicitly: #import "modules/std/meta.sx";
|
||||
//
|
||||
// `declare` is ordinary sx (over the `abi(.compiler)` primitive `declare_type`);
|
||||
// `define` / `type_info` / `field_type` are comptime-only compiler builtins —
|
||||
// reaching one at runtime is a hard error (the type must be minted / reflected
|
||||
// at compile time).
|
||||
// `declare` / `define` are ordinary sx (over the `abi(.compiler)` primitives
|
||||
// `declare_type` / `register_type`); `type_info` / `field_type` are comptime-only
|
||||
// compiler builtins — reaching one at runtime is a hard error (the type must be
|
||||
// minted / reflected at compile time). `define` builds its member list via
|
||||
// `List`, so meta.sx pulls in the prelude.
|
||||
#import "modules/std.sx";
|
||||
|
||||
// One variant of a constructed enum: a name plus an optional payload type.
|
||||
// `payload = void` means a tagless variant (e.g. `closed`).
|
||||
@@ -54,18 +56,31 @@ TypeInfo :: enum {
|
||||
`tuple: TupleInfo;
|
||||
}
|
||||
|
||||
// ── The low-level compiler-API type-construction primitive ───────────────────
|
||||
// ── The low-level compiler-API type-construction primitives ──────────────────
|
||||
//
|
||||
// `declare_type` is an `abi(.compiler)` function — the compiler's primitive for
|
||||
// minting a forward nominal slot, serviced by `comptime_vm.callCompilerFn`. It
|
||||
// runs at LOWERING time (when a `-> Type` builder's result is first referenced),
|
||||
// where the compiler still resolves references to the new type. The DSL's
|
||||
// `declare` below is ordinary sx written over it. (Declared here, not imported
|
||||
// These `abi(.compiler)` functions are the compiler's type-construction
|
||||
// primitives — serviced by `comptime_vm.callCompilerFn`. They run at LOWERING
|
||||
// time (when a `-> Type` builder's result is first referenced), where the
|
||||
// compiler still resolves references to the new types. The DSL's `declare` /
|
||||
// `define` below are ordinary sx written over them. (Declared here, not imported
|
||||
// from `modules/compiler.sx`, so `meta.sx` stays self-contained and doesn't
|
||||
// intern `compiler.sx`'s `List(string)` types into every importer's table.)
|
||||
declare_type :: (name: string) -> Type abi(.compiler);
|
||||
//
|
||||
// `register_type`'s `Member` is the shared `{ name, ty }` shape it decodes (an
|
||||
// `EnumVariant`/`StructField` has the same two-field `{name, type}` layout). The
|
||||
// `kind` codes match the read-side `type_kind`:
|
||||
// 1 struct · 2 enum (payloadless) · 3 tagged_union · 4 tuple.
|
||||
Member :: struct { name: string; ty: Type; }
|
||||
|
||||
// ── The metatype DSL: `declare` (sx), `define`/`type_info`/`field_type` (builtins)
|
||||
declare_type :: (name: string) -> Type abi(.compiler);
|
||||
register_type :: (handle: Type, kind: i64, members: []Member) -> Type abi(.compiler);
|
||||
|
||||
KIND_STRUCT :: 1;
|
||||
KIND_ENUM :: 2; // an ACTUAL payloadless enum
|
||||
KIND_TAGGED_UNION :: 3; // a payload-carrying enum
|
||||
KIND_TUPLE :: 4;
|
||||
|
||||
// ── The metatype DSL: `declare` / `define` (sx); `type_info`/`field_type` (builtins)
|
||||
//
|
||||
// declare(name) — mint a NEW empty (undefined) nominal type NAMED
|
||||
// `name`, returned as a `Type` handle. The compiler
|
||||
@@ -81,11 +96,9 @@ declare_type :: (name: string) -> Type abi(.compiler);
|
||||
// List :: define(declare("List"), .enum(.{ variants = .[
|
||||
// EnumVariant.{ name = "cons", payload = *List },
|
||||
// EnumVariant.{ name = "nil", payload = void } ] }));
|
||||
// Still a compiler builtin: decoding the `TypeInfo`
|
||||
// tagged-union VALUE needs comptime-VM tagged-union
|
||||
// matching (`enum_tag` on a tagged union), which the VM
|
||||
// doesn't model yet — so `define` can't be plain sx until
|
||||
// that lands (it would `match` the union).
|
||||
// Plain sx: it matches the `TypeInfo` union and calls
|
||||
// `register_type` with the matching kind code, decoding
|
||||
// the variant/field/element list into `[]Member`.
|
||||
// type_info($T) — reflect a type INTO a `TypeInfo` value (the inverse of
|
||||
// `define`). Still a compiler builtin: building the
|
||||
// tagged-union value byte-compatibly is a compiler job.
|
||||
@@ -94,7 +107,6 @@ declare_type :: (name: string) -> Type abi(.compiler);
|
||||
// composes inside `type_eq` / `type_name` / any type-arg
|
||||
// slot — a property `type_field_type` (a runtime value)
|
||||
// can't provide.
|
||||
define :: (handle: Type, info: TypeInfo) -> Type #builtin;
|
||||
type_info :: ($T: Type) -> TypeInfo #builtin;
|
||||
field_type :: ($T: Type, idx: i64) -> Type #builtin;
|
||||
|
||||
@@ -105,6 +117,42 @@ declare :: (name: string) -> Type {
|
||||
return declare_type(name);
|
||||
}
|
||||
|
||||
// `define(handle, info)` is plain sx over `register_type`: match the `TypeInfo`
|
||||
// union, collect the members into a `[]Member`, and register with the kind code.
|
||||
// An all-void enum variant set is an ACTUAL enum (kind 2); any payload variant
|
||||
// makes it a tagged_union (kind 3) — `register_type` rejects a payload variant
|
||||
// under kind 2, so the kind is chosen from whether every payload is `void`.
|
||||
define :: (handle: Type, info: TypeInfo) -> Type {
|
||||
if info == {
|
||||
case .`enum: (e) {
|
||||
mems : List(Member) = .{};
|
||||
all_void := true;
|
||||
for e.variants (v) {
|
||||
mems.append(Member.{ name = v.name, ty = v.payload });
|
||||
if v.payload != void { all_void = false; }
|
||||
}
|
||||
kind := KIND_TAGGED_UNION;
|
||||
if all_void { kind = KIND_ENUM; }
|
||||
return register_type(handle, kind, mems.items[0..mems.len]);
|
||||
}
|
||||
case .`struct: (s) {
|
||||
mems : List(Member) = .{};
|
||||
for s.fields (f) {
|
||||
mems.append(Member.{ name = f.name, ty = f.type });
|
||||
}
|
||||
return register_type(handle, KIND_STRUCT, mems.items[0..mems.len]);
|
||||
}
|
||||
case .`tuple: (t) {
|
||||
mems : List(Member) = .{};
|
||||
for t.elements (ety) {
|
||||
mems.append(Member.{ name = "", ty = ety });
|
||||
}
|
||||
return register_type(handle, KIND_TUPLE, mems.items[0..mems.len]);
|
||||
}
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
// --- Type constructors built in sx library code (no compiler machinery) ---
|
||||
//
|
||||
// The channel result types, expressed as type-fns over declare/define. They
|
||||
|
||||
@@ -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