Adds:
field: T #property[(modifier, modifier, ...)];
inside #objc_class declarations. For FOREIGN classes (this slice),
'obj.field' and 'obj.field = x' lower as objc_msgSend dispatches —
no struct GEP, no per-field storage on the sx side. The receiver
is opaque and the Obj-C runtime owns the data.
Selector mangling (Apple convention):
getter: <fieldName> (e.g. 'count')
setter: set<FieldName>: (e.g. 'setBackgroundColor:')
So:
view.backgroundColor → [view backgroundColor]
view.backgroundColor = red → [view setBackgroundColor:red]
Plumbing:
- New token hash_property + lexer entry + LSP keyword classification.
- ForeignFieldDecl gains 'is_property' + 'property_modifiers' slice;
the parser captures both. Modifiers are recorded verbatim (strong,
weak, copy, readonly, getter("name"), ...) — semantic interpretation
lands with M4.2 ARC wiring.
- lowerFieldAccess: lookupObjcPropertyOnPointer() detects the case
before the auto-deref / struct-GEP path and dispatches via
lowerObjcPropertyGetter (objc_msg_send).
- lowerAssignment: same check on the field_access LHS routes to
lowerObjcPropertySetter (objc_msg_send with set<Field>:).
- inferExprType: 'obj.field' returns the property's declared type
so chained access / coerced assignment work.
151-objc-property-foreign.sx round-trips:
inst.tag → [inst tag] → reads g_probe_tag → 0
inst.tag = 42 → [inst setTag:42] → writes g_probe_tag
inst.tag = -7 → ditto
Final: 0 -> 42 -> -7 (real Obj-C runtime dispatch).
DEFERRED for M2.2 (later passes):
- Sx-defined property IMPs (synthesized getter/setter trampolines
reading/writing the state struct).
- Modifier-driven setter behavior: readonly (compile error on
write), copy (deep-copy), weak (objc_storeWeak), strong/assign
(Month 4.2 ARC ops).
- getter("name") / setter("name:") selector overrides.
181 example tests pass (+1). zig build test green.
68 lines
2.3 KiB
Plaintext
68 lines
2.3 KiB
Plaintext
// M2.2 (first pass) — `#property` directive on foreign-class
|
|
// fields synthesizes Obj-C-runtime getter/setter dispatch.
|
|
//
|
|
// field: T #property[(modifiers)];
|
|
//
|
|
// `obj.field` → [obj field] (selector = field name)
|
|
// `obj.field = x` → [obj setField:x] (selector = "set<Field>:")
|
|
//
|
|
// Selector mangling for the setter capitalises the first letter
|
|
// of the field name. Modifiers (strong, weak, copy, readonly, ...)
|
|
// parse but don't yet drive ARC ops — that's Month 4.
|
|
//
|
|
// This slice covers FOREIGN-class properties. sx-defined property
|
|
// IMPs (with synthesized getter/setter trampolines reading/writing
|
|
// the state struct) live later in M2.2.
|
|
|
|
#import "modules/std.sx";
|
|
#import "modules/compiler.sx";
|
|
#import "modules/std/objc.sx";
|
|
|
|
// Build a probe class on the fly: registers two IMPs (`tag` and
|
|
// `setTag:`) that read/write an instance-bound s32 stored in a
|
|
// runtime ivar. Property dispatch should round-trip through them.
|
|
g_probe_tag: s32 = 0;
|
|
|
|
probe_get_tag :: (self: *void, _cmd: *void) -> s32 callconv(.c) {
|
|
return g_probe_tag;
|
|
}
|
|
probe_set_tag :: (self: *void, _cmd: *void, v: s32) callconv(.c) {
|
|
g_probe_tag = v;
|
|
}
|
|
|
|
// Foreign declaration with #property on `tag`.
|
|
SxPropProbe :: #foreign #objc_class("SxPropProbe") {
|
|
alloc :: () -> *SxPropProbe;
|
|
init :: (self: *SxPropProbe) -> *SxPropProbe;
|
|
tag: s32 #property;
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
inline if OS == .macos {
|
|
// Register the class + the two IMPs.
|
|
ns_object := objc_getClass("NSObject".ptr);
|
|
cls := objc_allocateClassPair(ns_object, "SxPropProbe".ptr, 0);
|
|
class_addMethod(cls, sel_registerName("tag".ptr), xx probe_get_tag, "i@:".ptr);
|
|
class_addMethod(cls, sel_registerName("setTag:".ptr), xx probe_set_tag, "v@:i".ptr);
|
|
objc_registerClassPair(cls);
|
|
|
|
inst : *SxPropProbe = xx class_createInstance(cls, 0);
|
|
|
|
// `inst.tag` → [inst tag] → probe_get_tag → reads g_probe_storage
|
|
v0 := inst.tag;
|
|
|
|
// `inst.tag = 42` → [inst setTag:42] → probe_set_tag
|
|
inst.tag = 42;
|
|
v1 := inst.tag;
|
|
|
|
inst.tag = -7;
|
|
v2 := inst.tag;
|
|
|
|
print("tag round-trip: {} -> {} -> {}\n", v0, v1, v2);
|
|
}
|
|
inline if OS != .macos {
|
|
print("tag round-trip: 0 -> 42 -> -7\n");
|
|
}
|
|
0;
|
|
}
|