ffi M2.3: #extends method-resolution chaining + Obj-C parent resolution
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.
This commit is contained in:
@@ -70,6 +70,12 @@ pub const Module = struct {
|
||||
name: []const u8,
|
||||
decl: *const ast.ForeignClassDecl,
|
||||
methods: []const ObjcDefinedMethodEntry = &.{},
|
||||
/// Pre-resolved Obj-C runtime name of the parent class, so
|
||||
/// emit_llvm can pass it to `objc_getClass(parent)` /
|
||||
/// `objc_allocateClassPair(super, ...)` without walking the
|
||||
/// sx-side foreign_class_map (which lives in lower.zig).
|
||||
/// Defaults to "NSObject" when no `#extends` member is present.
|
||||
parent_objc_name: []const u8 = "NSObject",
|
||||
};
|
||||
|
||||
pub const ObjcDefinedMethodEntry = struct {
|
||||
@@ -155,6 +161,16 @@ pub const Module = struct {
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the resolved Obj-C runtime parent name on a cache entry.
|
||||
pub fn setObjcDefinedClassParent(self: *Module, name: []const u8, parent_objc_name: []const u8) void {
|
||||
for (self.objc_defined_class_cache.items) |*entry| {
|
||||
if (std.mem.eql(u8, entry.name, name)) {
|
||||
entry.parent_objc_name = parent_objc_name;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn addFunction(self: *Module, func: Function) FuncId {
|
||||
const id = FuncId.fromIndex(@intCast(self.functions.items.len));
|
||||
self.functions.append(self.alloc, func) catch unreachable;
|
||||
|
||||
Reference in New Issue
Block a user