Files
sx/examples/0627-comptime-weld-struct-reflected-layout.sx
agra 40d075ca98 compiler-API: welded structs by reflection + memory-order validation
Replace the explored byte-layout-override engine (offset-ordered LLVM structs /
weld plans / byte-blobs — all unnecessary) with a much simpler design: a welded
`struct abi(.zig) extern compiler { … }` is a bodied header declaring its fields
in the bound compiler type's MEMORY order. The compiler reflects the real Zig
type (field names via @typeInfo, offsets via @offsetOf, size via @sizeOf —
nothing hand-maintained) and validates the header matches, with loud diagnostics.

On pass it is an ordinary struct whose natural layout already equals the Zig
layout — no reorder, no padding, no index/remap tables, no special LLVM path — so
@ptrCast'ing it to the compiler's own type and dereferencing is byte-identical.
When types.zig shifts, the header stops matching and the developer gets a specific
message to fix it.

- compiler_lib.zig: weldStruct reflects field names and bakes bound_types fields
  in ascending-offset (memory) order; deleted computeWeldPlan/WeldPlan/WeldElement.
- nominal.zig validateWeldedStruct: precise diagnostics — field-not-found,
  wrong-field-order (+ expected memory order), type-layout (size) mismatch,
  total-size mismatch.
- Examples: 0627 (StructInfo in memory order, byte-identical, usable),
  1186 (source-order StructInfo -> wrong-field-order diagnostic); 1183 refreshed.
- Design doc + checkpoint updated.
2026-06-17 15:45:23 +03:00

31 lines
1.1 KiB
Plaintext

// Comptime compiler API — a welded struct mirrors the compiler's real Zig type
// byte-for-byte by declaring its fields in the compiler type's MEMORY order.
//
// `StructInfo` is the real `types.TypeInfo.StructInfo`, which Zig reorders from
// source order to (fields, name, nominal_id, is_protocol). The sx header declares
// the fields in that memory order; the compiler reflects the bound Zig type
// (@offsetOf/@sizeOf) and validates the header matches — so the struct is laid
// out identically and a pointer to it can be cast to the compiler's own type and
// dereferenced. Nothing is maintained by hand: a types.zig change re-reflects.
#import "modules/std.sx";
compiler :: #library "compiler";
Field :: struct abi(.zig) extern compiler { name: u32; ty: u32; }
StructInfo :: struct abi(.zig) extern compiler {
fields: []Field;
name: u32;
nominal_id: u32;
is_protocol: bool;
}
main :: () {
si : StructInfo = ---;
si.name = 42;
si.nominal_id = 7;
si.is_protocol = true;
print("name={} nominal={} proto={}\n", si.name, si.nominal_id, si.is_protocol);
}