Commit 11c37624
11c37624f910Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Attr-writer dispatch on a poly (class-union) receiver
A setter call obj.x = v where obj is typed as a class union previously emitted no arms and silently dropped the store. Switch on the boxed value's cls_id and assign into each candidate class's ivar, converting the rhs to that slot's type (box when the slot is poly, unbox when the rhs widened to poly).
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 11c37624f910
- Parent
- 8feecda0ae69
src/codegen.cmodifiedView file
@@ -323,6 +323,7 @@
323323
/* Emit a hash key, unboxing a poly value to the typed-hash's key type. */324324
static void emit_hash_key(Compiler *c, int key, TyKind kt, Buf *b);325325
static void emit_boxed_text(Compiler *c, TyKind t, const char *expr, Buf *b);326+
static void emit_unbox_text(Compiler *c, TyKind t, const char *expr, Buf *b);326327
static void emit_proc_literal(Compiler *c, int create, Buf *b);327328
static int proc_slot_is_direct(TyKind t);328329
static int proc_slot_is_ptr(TyKind t);@@ -5281,6 +5282,45 @@
52815282
}52825283
}52835284
}5285+
/* poly receiver: switch on cls_id and store into each candidate5286+
class's ivar, converting the rhs to that ivar's slot type. */5287+
else if (rt == TY_POLY) {5288+
char base[256];5289+
if (ln - 1 < sizeof base) {5290+
memcpy(base, nm, ln - 1); base[ln - 1] = '\0';5291+
int args = nt_ref(nt, id, "arguments");5292+
int an = 0; const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;5293+
int ncand = 0;5294+
for (int k = 0; k < c->nclasses; k++)5295+
if (comp_is_writer(&c->classes[k], base)) ncand++;5296+
if (an >= 1 && ncand > 0) {5297+
TyKind at = comp_ntype(c, argv[0]);5298+
int tv = ++g_tmp, tval = ++g_tmp;5299+
emit_indent(b, indent);5300+
buf_printf(b, "{ sp_RbVal _t%d = ", tv); emit_expr(c, recv, b); buf_puts(b, "; ");5301+
emit_ctype(c, at, b); buf_printf(b, " _t%d = ", tval); emit_expr(c, argv[0], b); buf_puts(b, ";");5302+
buf_printf(b, " switch (_t%d.cls_id) {", tv);5303+
char src[32]; snprintf(src, sizeof src, "_t%d", tval);5304+
for (int k = 0; k < c->nclasses; k++) {5305+
if (!comp_is_writer(&c->classes[k], base)) continue;5306+
char ivn[256]; snprintf(ivn, sizeof ivn, "@%s", base);5307+
int iv = comp_ivar_index(&c->classes[k], ivn);5308+
TyKind ivt = iv >= 0 ? c->classes[k].ivar_types[iv] : at;5309+
/* skip a class whose slot can't hold this concrete rhs (the5310+
runtime object isn't that class anyway): a raw assignment5311+
between mismatched C types would not compile */5312+
if (at != ivt && at != TY_POLY && ivt != TY_POLY) continue;5313+
buf_printf(b, " case %d: ((sp_%s *)_t%d.v.p)->iv_%s = ", k, c->classes[k].name, tv, base);5314+
if (ivt == TY_POLY && at != TY_POLY) emit_boxed_text(c, at, src, b);5315+
else if (at == TY_POLY && ivt != TY_POLY) emit_unbox_text(c, ivt, src, b);5316+
else buf_puts(b, src);5317+
buf_puts(b, "; break;");5318+
}5319+
buf_puts(b, " } }\n");5320+
return;5321+
}5322+
}5323+
}52845324
}52855325
}52865326
if (emit_array_mutate_stmt(c, id, b, indent)) return;@@ -5640,6 +5680,23 @@
56405680
else buf_printf(b, "sp_box_int(%s)", expr); /* fallback */56415681
}56425682
5683+
/* Emit `expr` (a poly value) unboxed to its concrete C representation. */5684+
static void emit_unbox_text(Compiler *c, TyKind t, const char *expr, Buf *b) {5685+
if (t == TY_POLY) { buf_puts(b, expr); return; }5686+
switch (t) {5687+
case TY_INT: buf_printf(b, "(%s).v.i", expr); return;5688+
case TY_FLOAT: buf_printf(b, "(%s).v.f", expr); return;5689+
case TY_STRING: buf_printf(b, "(%s).v.s", expr); return;5690+
case TY_BOOL: buf_printf(b, "(%s).v.b", expr); return;5691+
case TY_SYMBOL: buf_printf(b, "(sp_sym)(%s).v.i", expr); return;5692+
default: break;5693+
}5694+
if (ty_is_object(t)) { buf_printf(b, "(sp_%s *)(%s).v.p", c->classes[ty_object_class(t)].name, expr); return; }5695+
const char *cn = c_type_name(t);5696+
if (cn) buf_printf(b, "(%s)(%s).v.p", cn, expr);5697+
else buf_printf(b, "(%s).v.i", expr);5698+
}5699+
56435700
static void emit_boxed(Compiler *c, int node, Buf *b) {56445701
TyKind t = comp_ntype(c, node);56455702
if (t == TY_POLY) { emit_expr(c, node, b); return; }