Second slice of the re-architecture — the compiler now has ZERO type- construction code beyond declare/define. - instantiateTypeFunction: a type-fn body returning a computed Type (a call to a non-generic, bodied, Type-returning fn) is comptime-evaluated with the type bindings active, then renamed to the mangled instantiation name for identity (renameNominalType). Replaces the old reify-call pattern-matching. - DELETED: reifyType (lower/nominal.zig), findReturnReifyCall (lower/generic.zig), and the stale inline-position reify gate in resolveTypeCallWithBindings. - evalComptimeType (was evalComptimeTypeNamed): pure eval, no rename; the type-fn caller renames explicitly. renameReifiedType → renameNominalType. - The TYPE NAME now travels in the data: EnumInfo gains `name`, and define() names the slot from it (the compiler derives no name from a binding LHS). examples/0614/0615 carry `name = "..."`; RecvResult/TryResult set it too. - field_type stays a reflection #builtin (reads a type); only construction moved out. All reify mentions stripped from compiler source. examples 0614/0615/0617 run on the floor. Full suite green (673).
82 lines
3.6 KiB
Plaintext
82 lines
3.6 KiB
Plaintext
// Comptime type metaprogramming (REIFY) — `type_info` / `reify` / `field_type`
|
|
// plus the data model they reflect INTO and construct FROM. Mirrors the Zig
|
|
// `@typeInfo` / `@Type` split: reflect a type → data, construct a NEW nominal
|
|
// type from data.
|
|
//
|
|
// This is a SEPARATE on-demand module rather than part of the prelude: its data
|
|
// types would otherwise intern into every module's type table and shift every
|
|
// `.ir` snapshot. Import it explicitly: #import "modules/std/meta.sx";
|
|
//
|
|
// `reify` / `type_info` / `field_type` are comptime-only builtins — a `reify`
|
|
// reached at runtime is a hard error (the type must be minted at compile time).
|
|
|
|
// One variant of a reify'd enum: a name plus an optional payload type.
|
|
// `payload = void` means a tagless variant (e.g. `closed`).
|
|
EnumVariant :: struct {
|
|
name: string;
|
|
payload: Type;
|
|
}
|
|
|
|
// The shape of an enum/tagged-union being reflected or constructed. `name` is
|
|
// the type's name — it travels WITH the shape (so `define` can name the slot and
|
|
// `type_info` round-trips it); the compiler derives nothing from a binding LHS.
|
|
EnumInfo :: struct {
|
|
name: string;
|
|
variants: []EnumVariant;
|
|
}
|
|
|
|
// The reflected/constructed type shape. A tagged union over the kinds of type
|
|
// `reify` can mint. Phase 0 ships only `` .`enum ``; struct/tuple land later.
|
|
// The variant uses the backtick raw-identifier escape so it reads as the
|
|
// keyword `enum` (`` reify(.`enum(...)) ``) rather than a mangled `enum_`.
|
|
TypeInfo :: enum {
|
|
`enum: EnumInfo;
|
|
}
|
|
|
|
// The compiler's ONLY type-construction primitives (comptime-only #builtins):
|
|
// declare() — mint a NEW empty (undefined) nominal type, returned
|
|
// as a `Type` handle. Using it before `define` is a
|
|
// loud error. References to it (`*Self`) are fine.
|
|
// define(handle, info) — fill a declared handle's body from a `TypeInfo`.
|
|
// `reify` and every other constructor below are PLAIN sx built over these — the
|
|
// compiler has no `reify` knowledge.
|
|
declare :: () -> Type #builtin;
|
|
define :: (handle: Type, info: TypeInfo) #builtin;
|
|
type_info :: ($T: Type) -> TypeInfo #builtin;
|
|
field_type :: ($T: Type, idx: i64) -> Type #builtin;
|
|
|
|
// reify(info) — the one-shot, non-recursive sugar: declare + define + return.
|
|
// (Recursive / mutually-recursive types use the explicit declare/define split
|
|
// so the handle can be referenced inside its own definition.)
|
|
reify :: (info: TypeInfo) -> Type {
|
|
h := declare();
|
|
define(h, info);
|
|
return h;
|
|
}
|
|
|
|
// --- Reify'd shapes built in sx library code (no new compiler machinery) ---
|
|
//
|
|
// The channel result types, expressed as type-fns over `reify`. They are the
|
|
// canonical demonstration that `reify` carries a full enum through codegen:
|
|
// `RecvResult(i64)` constructs and matches like any hand-written enum, and is
|
|
// one nominal type across sites (the type-fn mangled-name identity path). The
|
|
// channel library (N3) consumes these once it lands.
|
|
|
|
// A blocking recv: a value, or the channel was closed (drained).
|
|
RecvResult :: ($T: Type) -> Type {
|
|
return reify(.enum(.{ name = "RecvResult", variants = .[
|
|
EnumVariant.{ name = "value", payload = T },
|
|
EnumVariant.{ name = "closed", payload = void },
|
|
] }));
|
|
}
|
|
|
|
// A non-blocking try-recv: a value, currently empty, or closed — three states
|
|
// a bool can't express.
|
|
TryResult :: ($T: Type) -> Type {
|
|
return reify(.enum(.{ name = "TryResult", variants = .[
|
|
EnumVariant.{ name = "value", payload = T },
|
|
EnumVariant.{ name = "empty", payload = void },
|
|
EnumVariant.{ name = "closed", payload = void },
|
|
] }));
|
|
}
|