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:
agra
2026-06-19 21:09:18 +03:00
parent 8850fcce70
commit 7b1d8ceb83
5 changed files with 117 additions and 51 deletions

View File

@@ -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