REIFY Phase 0.0. Add the comptime type-metaprogramming surface as the
on-demand module modules/std/meta.sx (NOT the prelude — declaring its
data types in always-loaded core.sx interns them into every module's
type table and shifts every .ir snapshot):
- EnumVariant / EnumInfo / TypeInfo data types. TypeInfo's variant uses
the backtick raw escape `enum so it reads as the keyword.
- reify / type_info / field_type as bodyless #builtin decls.
Each builtin bails LOUDLY when reached unimplemented (no silent default):
- reify(...) in a :: type-alias position -> decl.zig .call branch
(also the Phase 0.2 construction hook); poisons the alias .unresolved.
- reify / field_type in any other type position ->
generic.zig resolveTypeCallWithBindings.
- type_info(...) in expression position -> call.zig tryLowerReflectionCall.
Unit test src/parser.test.zig (registered in root.zig) locks that the
decls parse. zig build test green (447 unit, 669 examples).
39 lines
1.7 KiB
Plaintext
39 lines
1.7 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.
|
|
EnumInfo :: struct {
|
|
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;
|
|
}
|
|
|
|
// reify(info) — mint a NEW nominal type from a `TypeInfo` (comptime-only).
|
|
// type_info($T) — reflect an existing type into a `TypeInfo`.
|
|
// field_type($T, i) — the i-th field/variant payload type of `$T`.
|
|
reify :: (info: TypeInfo) -> Type #builtin;
|
|
type_info :: ($T: Type) -> TypeInfo #builtin;
|
|
field_type :: ($T: Type, idx: i64) -> Type #builtin;
|