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:
agra
2026-05-26 01:56:25 +03:00
parent 239e7df27c
commit ea32f8a27a
6 changed files with 178 additions and 28 deletions

View File

@@ -0,0 +1,62 @@
// 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;
}

View File

@@ -589,14 +589,11 @@ pub const LLVMEmitter = struct {
const fcd = entry_kv.decl;
const class_name = fcd.name;
// Parent class — find `.extends` member; default NSObject.
var parent_name: []const u8 = "NSObject";
for (fcd.members) |m| {
switch (m) {
.extends => |p| parent_name = p,
else => {},
}
}
// Parent class — pre-resolved Obj-C runtime name from
// lower.zig (M2.3 resolveObjcParentName). Stored on the
// cache entry so emit_llvm doesn't re-walk
// foreign_class_map here.
const parent_name = entry_kv.parent_objc_name;
const parent_str_global = self.emitPrivateCString(parent_name, "OBJC_CLASS_NAME_");
const class_str_global = self.emitPrivateCString(class_name, "OBJC_CLASS_NAME_");

View File

@@ -4446,24 +4446,22 @@ pub const Lowering = struct {
method_args: []const Ref,
span: ast.Span,
) Ref {
var found_method: ?ast.ForeignMethodDecl = null;
for (fcd.members) |m| {
switch (m) {
.method => |md| {
if (std.mem.eql(u8, md.name, method_name)) {
found_method = md;
break;
}
},
else => {},
}
}
const method = found_method orelse {
// M2.3 — walk the `#extends` chain when the method isn't
// declared directly on this fcd. The dispatch target stays
// the original receiver — objc_msgSend's runtime walks the
// class hierarchy by isa, so we just need to find ANY
// ancestor that declared the method (for the selector
// mangling + signature info). The receiver-class fcd is
// still used for `*Self` substitution at the dispatch site
// — the inherited method's *Self should resolve to the
// child receiver, not the parent.
const found = self.findForeignMethodInChain(fcd, method_name) orelse {
if (self.diagnostics) |d| {
d.addFmt(.err, span, "no method '{s}' on foreign class '{s}'", .{ method_name, fcd.name });
d.addFmt(.err, span, "no method '{s}' on foreign class '{s}' (or any `#extends` ancestor)", .{ method_name, fcd.name });
}
return Ref.none;
};
const method = found.method;
// Obj-C instance dispatch (Phase 3 step 3.0 + M1.2 A.7).
// `inst.method(args)` on an `#objc_class` / `#objc_protocol`
@@ -9653,6 +9651,13 @@ pub const Lowering = struct {
if (!fcd.is_foreign and fcd.runtime == .objc_class) {
if (self.module.lookupObjcDefinedClass(fcd.name) == null) {
self.module.appendObjcDefinedClass(fcd.name, fcd);
// M2.3 — resolve the `#extends` alias to the actual
// Obj-C runtime class name. `#extends NSObjectBase`
// where NSObjectBase is aliased to "NSObject" must
// pass "NSObject" to objc_allocateClassPair, otherwise
// the runtime's class-hierarchy link is broken and
// inherited-method dispatch fails.
self.module.setObjcDefinedClassParent(fcd.name, self.resolveObjcParentName(fcd));
// M1.2 A.4b.i: per-class ivar handle global. The class-pair
// init constructor (emit_llvm) populates it via
// class_getInstanceVariable after the class is registered;
@@ -9667,6 +9672,30 @@ pub const Lowering = struct {
}
}
/// Resolve the `#extends ParentAlias` declaration on a sx-defined
/// `#objc_class` to the actual Obj-C runtime class name. Falls
/// back to "NSObject" when no `#extends` is declared.
/// Aliases that resolve to foreign Obj-C classes use the
/// foreign_path; aliases for OTHER sx-defined classes use the
/// alias name directly (which equals the Obj-C class name for
/// sx-defined classes).
fn resolveObjcParentName(self: *Lowering, fcd: *const ast.ForeignClassDecl) []const u8 {
for (fcd.members) |m| switch (m) {
.extends => |alias| {
if (self.foreign_class_map.get(alias)) |parent_fcd| {
if (parent_fcd.is_foreign) return parent_fcd.foreign_path;
// Sx-defined parent — its alias IS its Obj-C name.
return parent_fcd.name;
}
// Unknown alias — pass through as-is and let the
// runtime diagnose if it's genuinely wrong.
return alias;
},
else => {},
};
return "NSObject";
}
/// Declare a per-class global `__<ClassName>_state_ivar : *void = null`.
/// emit_llvm's `emitObjcDefinedClassInit` constructor fills it in via
/// `class_getInstanceVariable(cls, "__sx_state")` once per module load.
@@ -11589,8 +11618,9 @@ pub const Lowering = struct {
}
/// If `obj_expr` is typed as a pointer to a foreign Obj-C class
/// and that class declares a `#property` field with the given
/// name, return the `ForeignFieldDecl`. M2.2.
/// and that class (or any of its `#extends` ancestors) declares a
/// `#property` field with the given name, return the
/// `ForeignFieldDecl`. M2.2 + M2.3.
fn lookupObjcPropertyOnPointer(self: *Lowering, obj_expr: *const ast.Node, field_name: []const u8) ?ast.ForeignFieldDecl {
const obj_ty = self.inferExprType(obj_expr);
if (obj_ty.isBuiltin()) return null;
@@ -11601,10 +11631,53 @@ pub const Lowering = struct {
const struct_name = self.module.types.getString(pointee_info.@"struct".name);
const fcd = self.foreign_class_map.get(struct_name) orelse return null;
if (fcd.runtime != .objc_class and fcd.runtime != .objc_protocol) return null;
for (fcd.members) |m| switch (m) {
.field => |f| if (f.is_property and std.mem.eql(u8, f.name, field_name)) return f,
else => {},
};
return self.findForeignPropertyInChain(fcd, field_name);
}
/// Walk the `#extends` chain looking for a method by name. M2.3.
/// Returns the owning fcd + the method decl, or null if no ancestor
/// declares it. Depth-capped at 16 to break accidental cycles
/// (real Obj-C class chains rarely exceed 6 levels).
fn findForeignMethodInChain(self: *Lowering, fcd: *const ast.ForeignClassDecl, method_name: []const u8) ?struct { fcd: *const ast.ForeignClassDecl, method: ast.ForeignMethodDecl } {
var current: *const ast.ForeignClassDecl = fcd;
var depth: u32 = 0;
while (depth < 16) : (depth += 1) {
for (current.members) |m| switch (m) {
.method => |md| if (std.mem.eql(u8, md.name, method_name)) return .{ .fcd = current, .method = md },
else => {},
};
// Not on this level — follow `#extends ParentName`.
const parent = blk: {
for (current.members) |m| switch (m) {
.extends => |p| break :blk p,
else => {},
};
break :blk null;
} orelse return null;
current = self.foreign_class_map.get(parent) orelse return null;
}
return null;
}
/// Walk the `#extends` chain looking for a `#property` field by
/// name. M2.3 companion to findForeignMethodInChain.
fn findForeignPropertyInChain(self: *Lowering, fcd: *const ast.ForeignClassDecl, field_name: []const u8) ?ast.ForeignFieldDecl {
var current: *const ast.ForeignClassDecl = fcd;
var depth: u32 = 0;
while (depth < 16) : (depth += 1) {
for (current.members) |m| switch (m) {
.field => |f| if (f.is_property and std.mem.eql(u8, f.name, field_name)) return f,
else => {},
};
const parent = blk: {
for (current.members) |m| switch (m) {
.extends => |p| break :blk p,
else => {},
};
break :blk null;
} orelse return null;
current = self.foreign_class_map.get(parent) orelse return null;
}
return null;
}

View File

@@ -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;

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
extends chain: SxThing.hash=ok, SxLeaf.hash=ok