ffi M2.2 (first pass): #property directive on foreign-class fields

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.
This commit is contained in:
agra
2026-05-26 01:45:21 +03:00
parent d6ef691e42
commit 95f13849af
9 changed files with 222 additions and 0 deletions

View File

@@ -1174,10 +1174,51 @@ pub const Parser = struct {
if (self.current.tag == .colon) {
self.advance(); // consume `:`
const field_type = try self.parseTypeExpr();
// M2.2 — optional `#property[(modifier, modifier, ...)]`
// directive after the field type. Synthesizes Obj-C
// getter/setter dispatch at access sites.
var is_property = false;
var property_modifiers = std.ArrayList([]const u8).empty;
if (self.current.tag == .hash_property) {
is_property = true;
self.advance();
if (self.current.tag == .l_paren) {
self.advance(); // consume `(`
while (self.current.tag != .r_paren and self.current.tag != .eof) {
if (property_modifiers.items.len > 0) {
try self.expect(.comma);
if (self.current.tag == .r_paren) break;
}
if (self.current.tag != .identifier) {
return self.fail("expected property modifier name (strong, weak, copy, readonly, ...)");
}
const mod_name = self.tokenSlice(self.current);
self.advance();
// Optional argument: getter("name") / setter("name")
// — parsed but stored as part of the modifier string
// for now (M2.2 first pass; full attribute handling
// arrives with M4 ARC wiring).
if (self.current.tag == .l_paren) {
self.advance();
if (self.current.tag != .string_literal) {
return self.fail("expected string literal argument for property modifier");
}
self.advance();
try self.expect(.r_paren);
}
try property_modifiers.append(self.allocator, mod_name);
}
try self.expect(.r_paren);
}
}
try self.expect(.semicolon);
try members.append(self.allocator, .{ .field = .{
.name = member_name,
.field_type = field_type,
.is_property = is_property,
.property_modifiers = try property_modifiers.toOwnedSlice(self.allocator),
} });
continue;
}