Files
sx/examples/142-objc-class-method-lowering.sx
agra 659cdc2276 ffi M1.2 A.2c + A.3: eager body lowering + self.field via state struct
Adds Pass 4b 'lowerObjcDefinedClassMethods' to lowerRoot: after
scan, walk objc_defined_class_cache and force-lower each bodied
instance method. The Obj-C runtime invokes these via the IMP
pointers wired up in A.4 — no sx-side call path drives lazy
lowering, so we trigger it here. Mirrors the JNI eager-lower
pattern in Pass 5.

Bug fix: lazyLowerFunction has its OWN inline body-lowering
path (separate from lowerFunction) that re-resolves param types
at line 1025. It was running without current_foreign_class set,
so '*Self' fell through to the type_bridge fallback and got
interned as a 0-field struct named 'Self' — body's
'self.counter' GEP'd into '{}' and LLVM verification rejected.
Fix: set current_foreign_class at the top of lazyLowerFunction
via the same lookupObjcDefinedClassForMethod path lowerFunction
uses. Save+restore via defer.

A.3 ('self.field access via the ivar') falls out for free —
'*Self' resolves to '*__SxFooState' so 'self.counter' is a
plain struct field access. IR snapshot in
142-objc-class-method-lowering.ir shows the round-trip:

    define internal void @SxFoo.bump(ptr, ptr self) {
        %gep = getelementptr inbounds { i32 }, ptr %self, 0, 0
        %v = load i32, ptr %gep
        store i32 (%v + 1), ptr %gep
        ret void
    }

171 examples pass (+1 from 142); zig build test green.

Still gated: Obj-C runtime dispatch (A.7) — sx-side
'f.bump()' calls bail at lower.zig:4407 with the existing
diagnostic. IMP-trampoline emission (the C-ABI shim that bridges
'objc_msgSend' → this body) lands in A.4 alongside class-pair
init.
2026-05-25 22:08:23 +03:00

43 lines
1.3 KiB
Plaintext

// M1.2 A.2c / A.3 — instance-method body lowering on sx-defined
// `#objc_class`.
//
// The Obj-C runtime invokes class methods via the IMP pointers
// wired up in M1.2 A.4. No sx-side call path drives lazy lowering,
// so the eager pass `lowerObjcDefinedClassMethods` walks the
// `objc_defined_class_cache` and force-lowers every bodied method
// after Pass 1 finishes.
//
// `*Self` substitution (A.2b) routes the param's pointee type to
// the hidden state-struct `__<ClassName>State`. `self.counter`
// then resolves as a plain struct field access — A.3's "free if
// types align". The IR snapshot pins the round-trip:
//
// define internal void @SxFoo.bump(ptr __sx_ctx, ptr self) {
// %gep = getelementptr inbounds { i32 }, ptr %self, 0, 0
// %v = load i32, ptr %gep
// %inc = add i32 %v, 1
// store i32 %inc, ptr %gep
// ret void
// }
//
// IMP-trampoline emission (the C-ABI shim that the Obj-C runtime
// calls and that reads the `__sx_state` ivar) lands separately
// in M1.2 A.4 alongside class-pair init. Runtime dispatch
// (M1.2 A.7) stays gated until then.
#import "modules/std.sx";
#import "modules/compiler.sx";
SxFoo :: #objc_class("SxFoo") {
counter: s32;
bump :: (self: *Self) {
self.counter += 1;
}
}
main :: () -> s32 {
print("compiled\n");
0;
}