Commit dbc416e4

dbc416e4e8f3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified

Description

prepend super: shadow-chain mechanism for super inside prepended methods

When prepend M is called on class C, M's instance methods are transplanted into C with the same name; C's original method is renamed to __prep_N_<m>. A prep_chain in ClassInfo maps each active method name to the next shadow so emit_super can route M's super to the shadow (and through the shadow to the parent class chain if the shadow itself has super).

  • compiler.h/c: add prep_from/prep_to/nprep_chain/cprep_chain/prep_shadow_count fields to ClassInfo; add comp_prep_chain_add, comp_prep_chain_target, comp_prep_user_name helpers
  • analyze.c: register_prepends() scans class body for prepend M, renames the active method to a shadow, updates the chain, and transplants M's scopes into the class; propagate_prep_params() in the fixpoint propagates param types from the transplanted scope to the shadow (since the shadow has no AST call site); compute_reachable marks all prep_to shadow names live; SuperNode infer_type follows the prep chain before the parent chain
  • codegen.c: emit_super checks comp_prep_chain_target first (shadow call in same class), then strips _prep_N prefix for parent-chain fallthrough

Fixes prepend_super_basic, _chain, _forward, _multi, _subclass (+5 pass).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
dbc416e4e8f3
Parent
45be195d5cc5

src/analyze.cmodifiedView file

@@ -1377,9 +1377,15 @@
13771377 if (!strcmp(ty, "SuperNode") || !strcmp(ty, "ForwardingSuperNode")) {
13781378 Scope *s = comp_scope_of(c, id);
13791379 if (s->class_id < 0 || !s->name) return TY_UNKNOWN;
1380+ const char *shadow = comp_prep_chain_target(c, s->class_id, s->name);
1381+ if (shadow) {
1382+ int mi = comp_method_in_class(c, s->class_id, shadow);
1383+ return mi >= 0 ? c->scopes[mi].ret : TY_UNKNOWN;
1384+ }
1385+ const char *uname = comp_prep_user_name(s->name);
13801386 int p = c->classes[s->class_id].parent;
13811387 if (p < 0) return TY_UNKNOWN;
1382- int mi = comp_method_in_chain(c, p, s->name, NULL);
1388+ int mi = comp_method_in_chain(c, p, uname, NULL);
13831389 return mi >= 0 ? c->scopes[mi].ret : TY_UNKNOWN;
13841390 }
13851391 if (!strcmp(ty, "AndNode") || !strcmp(ty, "OrNode")) {
@@ -1896,6 +1902,66 @@
18961902 }
18971903 }
18981904
1905+/* For each class, find `prepend M` declarations and transplant M's instance
1906+ methods into the class with shadow-chain renaming so `super` can route
1907+ from M's body to the original (now renamed) class body. */
1908+static void register_prepends(Compiler *c) {
1909+ const NodeTable *nt = c->nt;
1910+ for (int ci = 0; ci < c->nclasses; ci++) {
1911+ int body = nt_ref(nt, c->classes[ci].def_node, "body");
1912+ int n = 0;
1913+ const int *stmts = body >= 0 ? nt_arr(nt, body, "body", &n) : NULL;
1914+ for (int k = 0; k < n; k++) {
1915+ int s = stmts[k];
1916+ const char *sty = nt_type(nt, s);
1917+ if (!sty || strcmp(sty, "CallNode")) continue;
1918+ const char *nm = nt_str(nt, s, "name");
1919+ if (!nm || strcmp(nm, "prepend")) continue;
1920+ if (nt_ref(nt, s, "receiver") >= 0) continue;
1921+ int anode = nt_ref(nt, s, "arguments");
1922+ int an = 0;
1923+ const int *args = anode >= 0 ? nt_arr(nt, anode, "arguments", &an) : NULL;
1924+ for (int j = 0; j < an; j++) {
1925+ const char *aty = nt_type(nt, args[j]);
1926+ const char *mname = NULL;
1927+ if (aty && !strcmp(aty, "ConstantReadNode")) mname = nt_str(nt, args[j], "name");
1928+ else if (aty && !strcmp(aty, "ConstantPathNode")) mname = nt_str(nt, args[j], "name");
1929+ int mod_id = mname ? comp_class_index(c, mname) : -1;
1930+ if (mod_id < 0) continue;
1931+ /* Transplant each instance method of the module into class ci. */
1932+ for (int ms = 0; ms < c->nscopes; ms++) {
1933+ Scope *sc = &c->scopes[ms];
1934+ if (sc->class_id != mod_id || sc->is_cmethod || !sc->name) continue;
1935+ const char *method_name = sc->name;
1936+ int active_mi = comp_method_in_class(c, ci, method_name);
1937+ if (active_mi >= 0) {
1938+ Scope *active = &c->scopes[active_mi];
1939+ char shadow[256];
1940+ snprintf(shadow, sizeof shadow, "__prep_%d_%s",
1941+ c->classes[ci].prep_shadow_count++, method_name);
1942+ /* Rename any existing chain entry for method_name to use shadow. */
1943+ ClassInfo *cif = &c->classes[ci];
1944+ for (int kk = 0; kk < cif->nprep_chain; kk++) {
1945+ if (!strcmp(cif->prep_from[kk], method_name)) {
1946+ free(cif->prep_from[kk]);
1947+ cif->prep_from[kk] = strdup(shadow);
1948+ break;
1949+ }
1950+ }
1951+ /* Rename the currently active scope to the shadow name. */
1952+ free(active->name);
1953+ active->name = strdup(shadow);
1954+ /* Record the new dispatch chain entry: method_name -> shadow. */
1955+ comp_prep_chain_add(&c->classes[ci], method_name, shadow);
1956+ }
1957+ /* Transplant the module scope into class ci. */
1958+ sc->class_id = ci;
1959+ }
1960+ }
1961+ }
1962+ }
1963+}
1964+
18991965 /* Merge inherited ivar/reader/writer NAMES into subclasses so the struct
19001966 layout is [parent ivars..., own ivars...] (cast-compatible). Types are
19011967 propagated later in the fixpoint. Parent-first order. */
@@ -2526,6 +2592,40 @@
25262592 return changed;
25272593 }
25282594
2595+/* Propagate param types from each prep-chain source scope (the transplanted
2596+ module method) to the shadow scope it calls via super. The shadow scope has
2597+ no AST call site, so bind_call_params never runs for it. */
2598+static int propagate_prep_params(Compiler *c) {
2599+ int changed = 0;
2600+ for (int ci = 0; ci < c->nclasses; ci++) {
2601+ ClassInfo *cls = &c->classes[ci];
2602+ for (int k = 0; k < cls->nprep_chain; k++) {
2603+ const char *from_name = cls->prep_from[k];
2604+ const char *to_name = cls->prep_to[k];
2605+ int from_mi = comp_method_in_class(c, ci, from_name);
2606+ int to_mi = -1;
2607+ for (int s = 0; s < c->nscopes; s++) {
2608+ if (c->scopes[s].class_id == ci && !c->scopes[s].is_cmethod &&
2609+ c->scopes[s].name && !strcmp(c->scopes[s].name, to_name)) {
2610+ to_mi = s; break;
2611+ }
2612+ }
2613+ if (from_mi < 0 || to_mi < 0) continue;
2614+ Scope *fs = &c->scopes[from_mi];
2615+ Scope *ts = &c->scopes[to_mi];
2616+ int n = fs->nparams < ts->nparams ? fs->nparams : ts->nparams;
2617+ for (int i = 0; i < n; i++) {
2618+ LocalVar *fp = scope_local(fs, fs->pnames[i]);
2619+ LocalVar *tp = scope_local(ts, ts->pnames[i]);
2620+ if (!fp || !tp || fp->type == TY_UNKNOWN) continue;
2621+ TyKind merged = ty_unify(tp->type, fp->type);
2622+ if (merged != tp->type) { tp->type = merged; changed = 1; }
2623+ }
2624+ }
2625+ }
2626+ return changed;
2627+}
2628+
25292629 /* Optional parameters get a type from their default value too. */
25302630 static int infer_default_param_types(Compiler *c) {
25312631 int changed = 0;
@@ -3248,6 +3348,7 @@
32483348 for (int ci = 0; ci < c->nclasses; ci++) {
32493349 ClassInfo *cls = &c->classes[ci];
32503350 for (int i = 0; i < cls->naliases; i++) { ADD_NAME(cls->alias_new[i]); ADD_NAME(cls->alias_old[i]); }
3351+ for (int i = 0; i < cls->nprep_chain; i++) ADD_NAME(cls->prep_to[i]);
32513352 }
32523353
32533354 /* Names that may be invoked implicitly (no explicit CallNode): keep live. */
@@ -3400,6 +3501,7 @@
34003501
34013502 resolve_parents(c);
34023503 inherit_members(c);
3504+ register_prepends(c);
34033505
34043506 /* collect top-level `include <Mod>` calls so bare method calls can
34053507 resolve to module_function methods in those modules. */
@@ -3511,6 +3613,7 @@
35113613 int ch = 0;
35123614 ch |= infer_write_types(c);
35133615 ch |= infer_param_types(c);
3616+ ch |= propagate_prep_params(c);
35143617 ch |= infer_string_params(c);
35153618 ch |= infer_default_param_types(c);
35163619 ch |= infer_block_params(c);