Commit 8e64e913
8e64e9133fcaMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
open_class: module include transplant, bool ternary dispatch, sp_class_to_s
- analyze.c: add register_includes() pass — for each class body
include M, transplant M's instance methods into the including class so they are reachable via comp_method_in_chain (mirrors register_prepends without the shadow-chain renaming that prepend requires) - codegen.c: remove TY_BOOL->TrueClass shortcut from generic dispatch; replace with value-aware ternary: true->sp_TrueClass_m / false->sp_FalseClass_m, falling back to TrueClass-only or FalseClass-only when only one side is defined
- codegen.c: emit proper sp_class_to_s switch mapping class indices to names instead of an always-empty stub; required for Object#global_hi/hi on user-defined receivers calling sp_poly_class_name
Fixes open_class_array_builtin, open_class_bool_builtin, open_class_object.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 8e64e9133fca
- Parent
- 082911e3c9a7
src/analyze.cmodifiedView file
@@ -1557,6 +1557,43 @@
15571557
if (recv >= 0 && call_op && !strcmp(call_op, "&.")) return TY_POLY;15581558
}15591559
1560+
/* Builtin class reopening: look up user-defined methods on Array/Numeric/Object1561+
receivers where no builtin method matched. */1562+
if (recv >= 0) {1563+
/* Array reopening: any array-typed receiver */1564+
if (ty_is_array(rt)) {1565+
int oc_ci = comp_class_index(c, "Array");1566+
if (oc_ci >= 0) {1567+
int oc_mi = comp_method_in_chain(c, oc_ci, name, NULL);1568+
if (oc_mi >= 0) return c->scopes[oc_mi].ret;1569+
}1570+
}1571+
/* Numeric reopening: integers and floats */1572+
if (rt == TY_INT || rt == TY_FLOAT) {1573+
int oc_ci = comp_class_index(c, "Numeric");1574+
if (oc_ci >= 0) {1575+
int oc_mi = comp_method_in_chain(c, oc_ci, name, NULL);1576+
if (oc_mi >= 0) return c->scopes[oc_mi].ret;1577+
}1578+
}1579+
/* FalseClass methods (TrueClass already checked earlier for TY_BOOL) */1580+
if (rt == TY_BOOL) {1581+
int oc_ci = comp_class_index(c, "FalseClass");1582+
if (oc_ci >= 0) {1583+
int oc_mi = comp_method_in_chain(c, oc_ci, name, NULL);1584+
if (oc_mi >= 0) return c->scopes[oc_mi].ret;1585+
}1586+
}1587+
/* Object reopening: universal fallback for any receiver type */1588+
{1589+
int oc_ci = comp_class_index(c, "Object");1590+
if (oc_ci >= 0) {1591+
int oc_mi = comp_method_in_chain(c, oc_ci, name, NULL);1592+
if (oc_mi >= 0) return c->scopes[oc_mi].ret;1593+
}1594+
}1595+
}1596+
15601597
return TY_UNKNOWN;15611598
}15621599
@@ -1695,6 +1732,7 @@
16951732
if (!strcmp(cn, "Symbol")) return TY_SYMBOL;16961733
if (!strcmp(cn, "TrueClass") || !strcmp(cn, "FalseClass") || !strcmp(cn, "NilClass")) return TY_BOOL;16971734
if (!strcmp(cn, "Array")) return TY_POLY_ARRAY;1735+
if (!strcmp(cn, "Object")) return TY_POLY; /* dynamic: called on any receiver type */16981736
return ty_object(s->class_id);16991737
}17001738
if (!strcmp(ty, "InstanceVariableReadNode")) {@@ -2411,6 +2449,43 @@
24112449
}24122450
}24132451
2452+
/* For each class, find `include M` declarations and transplant M's instance2453+
methods into the class so they are reachable via comp_method_in_chain. */2454+
static void register_includes(Compiler *c) {2455+
const NodeTable *nt = c->nt;2456+
for (int ci = 0; ci < c->nclasses; ci++) {2457+
int body = nt_ref(nt, c->classes[ci].def_node, "body");2458+
int n = 0;2459+
const int *stmts = body >= 0 ? nt_arr(nt, body, "body", &n) : NULL;2460+
for (int k = 0; k < n; k++) {2461+
int s = stmts[k];2462+
const char *sty = nt_type(nt, s);2463+
if (!sty || strcmp(sty, "CallNode")) continue;2464+
const char *nm = nt_str(nt, s, "name");2465+
if (!nm || strcmp(nm, "include")) continue;2466+
if (nt_ref(nt, s, "receiver") >= 0) continue;2467+
int anode = nt_ref(nt, s, "arguments");2468+
int an = 0;2469+
const int *args = anode >= 0 ? nt_arr(nt, anode, "arguments", &an) : NULL;2470+
for (int j = 0; j < an; j++) {2471+
const char *aty = nt_type(nt, args[j]);2472+
const char *mname = NULL;2473+
if (aty && !strcmp(aty, "ConstantReadNode")) mname = nt_str(nt, args[j], "name");2474+
else if (aty && !strcmp(aty, "ConstantPathNode")) mname = nt_str(nt, args[j], "name");2475+
int mod_id = mname ? comp_class_index(c, mname) : -1;2476+
if (mod_id < 0) continue;2477+
for (int ms = 0; ms < c->nscopes; ms++) {2478+
Scope *sc = &c->scopes[ms];2479+
if (sc->class_id != mod_id || sc->is_cmethod || !sc->name) continue;2480+
/* Skip if class already has a method with this name */2481+
if (comp_method_in_class(c, ci, sc->name) >= 0) continue;2482+
sc->class_id = ci;2483+
}2484+
}2485+
}2486+
}2487+
}2488+
24142489
/* For each class, find `prepend M` declarations and transplant M's instance24152490
methods into the class with shadow-chain renaming so `super` can route24162491
from M's body to the original (now renamed) class body. */@@ -4678,6 +4753,7 @@
46784753
46794754
resolve_parents(c);46804755
inherit_members(c);4756+
register_includes(c);46814757
register_prepends(c);46824758
46834759
/* collect top-level `include <Mod>` calls so bare method calls cansrc/codegen.cmodifiedView file
Loading diff…