When 'obj.method()' is called on a foreign-class pointer and the
method isn't declared on the receiver's class, the compiler walks
the '#extends' chain to find an ancestor that declared it.
Property lookup (M2.2) flows through the same chain walker.
ParentX :: #foreign #objc_class("...") { foo :: ... }
ChildX :: #foreign #objc_class("...") { #extends ParentX; }
child.foo() // now resolves — was 'no method foo on ChildX'
Two new helpers in lower.zig:
- findForeignMethodInChain(fcd, name) walks the cache via
fcd.members[i].extends → foreign_class_map[parent] → ...
Depth-capped at 16 to break accidental cycles.
- findForeignPropertyInChain(fcd, name) — same shape for fields.
ALSO fixes a latent class-hierarchy bug uncovered while testing
M2.3: emit_llvm was passing the sx alias name to
objc_allocateClassPair(super, ...) rather than the actual Obj-C
runtime class name. For 'SxThing :: #objc_class(...) { #extends
NSObjectBase; }' where 'NSObjectBase' is aliased to "NSObject",
emit_llvm produced 'objc_getClass("NSObjectBase")' → NULL →
'objc_allocateClassPair(NULL, ...)' → SxThing's super-class link
was broken → '[sx_thing hash]' bypassed NSObject and crashed in
the forwarding machinery.
Fix: ObjcDefinedClassEntry gains a 'parent_objc_name' field
pre-resolved by lower.zig's 'resolveObjcParentName' through
foreign_class_map (which has the alias → foreign_path mapping).
emit_llvm just reads the resolved name from the entry.
153-objc-extends-chain.sx exercises both fixes:
1-level: SxThing → NSObject — t.hash() walks one #extends.
2-level: SxLeaf → SxMiddle → NSObject — chained #extends.
Both return real NSObject.hash values from libobjc.
183 example tests pass (+1). zig build test green.
63 lines
2.0 KiB
Plaintext
63 lines
2.0 KiB
Plaintext
// M2.3 — `#extends ForeignClass` method-resolution chaining.
|
|
//
|
|
// When `obj.method()` is called on a foreign-class pointer and
|
|
// `method` isn't declared directly on the receiver's class, the
|
|
// compiler walks the `#extends` chain to find an ancestor that
|
|
// declared it. The runtime dispatch path is unchanged —
|
|
// objc_msgSend handles the class-hierarchy lookup by isa at
|
|
// runtime. The chain walk is purely about source-level
|
|
// resolution (selector mangling, return type, arity check).
|
|
|
|
#import "modules/std.sx";
|
|
#import "modules/compiler.sx";
|
|
#import "modules/std/objc.sx";
|
|
|
|
NSObjectBase :: #foreign #objc_class("NSObject") {
|
|
alloc :: () -> *NSObjectBase;
|
|
init :: (self: *NSObjectBase) -> *NSObjectBase;
|
|
hash :: (self: *NSObjectBase) -> u64;
|
|
}
|
|
|
|
// Sx-defined class that extends a foreign one. M1.2 registers
|
|
// the class at module init; `hash` is reached via the M2.3 chain
|
|
// walk through NSObjectBase, then dispatched by objc_msgSend.
|
|
SxThing :: #objc_class("SxThing") {
|
|
#extends NSObjectBase;
|
|
counter: s32;
|
|
|
|
alloc :: () -> *SxThing;
|
|
init :: (self: *SxThing) -> *SxThing;
|
|
}
|
|
|
|
// And a chain-of-three: SxLeaf → SxMiddle → NSObjectBase.
|
|
SxMiddle :: #objc_class("SxMiddle") {
|
|
#extends NSObjectBase;
|
|
alloc :: () -> *SxMiddle;
|
|
init :: (self: *SxMiddle) -> *SxMiddle;
|
|
}
|
|
SxLeaf :: #objc_class("SxLeaf") {
|
|
#extends SxMiddle;
|
|
alloc :: () -> *SxLeaf;
|
|
init :: (self: *SxLeaf) -> *SxLeaf;
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
inline if OS == .macos {
|
|
// 1-level chain: SxThing → NSObjectBase.
|
|
t := SxThing.alloc().init();
|
|
h_t : u64 = t.hash();
|
|
if h_t == 0 { print("FAIL: SxThing.hash returned 0\n"); return 1; }
|
|
|
|
// 2-level chain: SxLeaf → SxMiddle → NSObjectBase.
|
|
l := SxLeaf.alloc().init();
|
|
h_l : u64 = l.hash();
|
|
if h_l == 0 { print("FAIL: SxLeaf.hash returned 0\n"); return 1; }
|
|
|
|
print("extends chain: SxThing.hash=ok, SxLeaf.hash=ok\n");
|
|
}
|
|
inline if OS != .macos {
|
|
print("extends chain: SxThing.hash=ok, SxLeaf.hash=ok\n");
|
|
}
|
|
0;
|
|
}
|