comptime VM: Phase 3 — type_kind + type_field_value readers (read side complete)

The last two read-only readers the metatype's type_info(T) needs, each backed by
a TypeTable query both the legacy handler and the VM call (no drift):

  type_kind(t: TypeId) -> i64            (kindCode; stable discriminant, total — never bails)
  type_field_value(t: TypeId, idx) -> i64 (memberValue; enum explicit value or ordinal)

kindCode codes (compiler-owned, stable): 0 other / 1 struct / 2 enum /
3 tagged_union / 4 tuple / 5 union / 6 array / 7 vector / 8 error_set.

With these, the READ side is complete: find_type + type_kind + type_field_count +
type_field_{name,type} + type_nominal_name + type_field_value cover everything
reflectTypeInfo reads — a comptime sx fn can fully reflect a struct/enum/tuple
into data with no #builtin.

Example 0630 reflects Color / WindowFlags(flags) / Point. VM unit test added.

Revised forward direction: the write side will be ONE register_type(info) fn that
branches on the kind in the compiler (subsuming define's per-kind dispatch), not a
per-kind register_struct.

Parity 691/691 (gate OFF and -Dcomptime-flat).
This commit is contained in:
agra
2026-06-18 09:47:23 +03:00
parent d23e208430
commit 27bc301651
10 changed files with 252 additions and 29 deletions

View File

@@ -0,0 +1,47 @@
// Comptime compiler API — type-kind + enum-value reflection readers (Phase 3).
//
// Completes the READ side the metatype needs to re-express `type_info(T)` as sx:
//
// type_kind(t) → a stable kind discriminant, to branch on the shape
// (0 other · 1 struct · 2 enum · 3 tagged_union ·
// 4 tuple · 5 union · 6 array · 7 vector · 8 error_set)
// type_field_value(t, i) → enum variant i's integer value (explicit or ordinal)
//
// Together with find_type / type_field_count / type_field_name / type_field_type
// / type_nominal_name (examples 06280629), a comptime sx function can now fully
// reflect a struct/enum/tuple into data — no `#builtin` needed. All folded at
// `#run`, all serviced natively by the flat-memory VM.
#import "modules/std.sx";
compiler :: #library "compiler";
StringId :: u32;
TypeId :: u32;
intern :: (s: string) -> StringId abi(.zig) extern compiler;
text_of :: (id: StringId) -> string abi(.zig) extern compiler;
find_type :: (name: StringId) -> TypeId abi(.zig) extern compiler;
type_kind :: (t: TypeId) -> i64 abi(.zig) extern compiler;
type_field_name :: (t: TypeId, idx: i64) -> StringId abi(.zig) extern compiler;
type_field_value :: (t: TypeId, idx: i64) -> i64 abi(.zig) extern compiler;
Color :: enum { red; green; blue; }
WindowFlags :: enum flags u32 { vsync :: 64; resizable :: 4; hidden :: 128; }
Point :: struct { x: i64; y: i64; }
color_kind :: #run type_kind(find_type(intern("Color"))); // 2 = enum
point_kind :: #run type_kind(find_type(intern("Point"))); // 1 = struct
// Plain enum → ordinal values.
green_val :: #run type_field_value(find_type(intern("Color")), 1); // 1
// Flags enum → explicit values, read by name + value.
vsync_name :: #run text_of(type_field_name(find_type(intern("WindowFlags")), 0)); // "vsync"
vsync_val :: #run type_field_value(find_type(intern("WindowFlags")), 0); // 64
main :: () {
print("Color kind = {}, Point kind = {}\n", color_kind, point_kind);
print("Color.green = {}\n", green_val);
print("WindowFlags.{} = {}\n", vsync_name, vsync_val);
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,3 @@
Color kind = 2, Point kind = 1
Color.green = 1
WindowFlags.vsync = 64