Commit af4cd1a4

af4cd1a4b612Merged

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

Description

include transplant: copy param locals + skip source emission

When a module is included into a class, register_includes now calls scope_local_intern for each parameter in the destination scope copy, so infer_param_types can update the copy's param types during the fixpoint (previously the copy had pnames but no LocalVar entries).

Add is_transplanted_source flag to Scope: set when a module method is copied into an including class. Codegen skips emitting transplanted source scopes (they are not directly callable since modules cannot be instantiated; only the copies are dispatched to).

Also handle TrueClass/FalseClass in the .class ternary dispatch: exclude them from the is_builtin_reopen TY_POLY override so that bool-typed self generates the correct SP_BOOL ternary rather than a sp_poly_class_name call on an int.

Fixes module_reopen_after_include_namespaced and open_class_bool_builtin.

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

Details

Commit
af4cd1a4b612
Parent
8e64e9133fca

src/analyze.cmodifiedView file

@@ -2449,43 +2449,96 @@
24492449 }
24502450 }
24512451
2452-/* For each class, find `include M` declarations and transplant M's instance
2453- methods into the class so they are reachable via comp_method_in_chain. */
2454-static void register_includes(Compiler *c) {
2452+/* Process include calls in a single class body, creating scope copies for each
2453+ included module method. We copy (not mutate) so multiple classes can include
2454+ the same module independently. */
2455+static void process_include_body(Compiler *c, int ci, int body_node) {
24552456 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;
2457+ int n = 0;
2458+ const int *stmts = body_node >= 0 ? nt_arr(nt, body_node, "body", &n) : NULL;
2459+ for (int k = 0; k < n; k++) {
2460+ int s = stmts[k];
2461+ const char *sty = nt_type(nt, s);
2462+ if (!sty || strcmp(sty, "CallNode")) continue;
2463+ const char *nm = nt_str(nt, s, "name");
2464+ if (!nm || strcmp(nm, "include")) continue;
2465+ if (nt_ref(nt, s, "receiver") >= 0) continue;
2466+ int anode = nt_ref(nt, s, "arguments");
2467+ int an = 0;
2468+ const int *args = anode >= 0 ? nt_arr(nt, anode, "arguments", &an) : NULL;
2469+ for (int j = 0; j < an; j++) {
2470+ const char *aty = nt_type(nt, args[j]);
2471+ const char *mname = NULL;
2472+ if (aty && !strcmp(aty, "ConstantReadNode")) mname = nt_str(nt, args[j], "name");
2473+ else if (aty && !strcmp(aty, "ConstantPathNode")) mname = nt_str(nt, args[j], "name");
2474+ int mod_id = mname ? comp_class_index(c, mname) : -1;
2475+ if (mod_id < 0) continue;
2476+ /* snapshot count before adding new scopes to avoid re-scanning them */
2477+ int snap = c->nscopes;
2478+ for (int ms = 0; ms < snap; ms++) {
2479+ Scope *src = &c->scopes[ms];
2480+ if (src->class_id != mod_id || src->is_cmethod || !src->name) continue;
2481+ if (comp_method_in_class(c, ci, src->name) >= 0) continue;
2482+ /* Create a new scope sharing the same AST nodes but owned by ci. */
2483+ Scope *dst = comp_scope_new(c, src->name, src->def_node);
2484+ /* comp_scope_new may realloc c->scopes; re-derive src pointer. */
2485+ src = &c->scopes[ms];
2486+ dst->body = src->body;
2487+ dst->class_id = ci;
2488+ dst->is_cmethod = 0;
2489+ dst->reachable = src->reachable;
2490+ dst->yields = src->yields;
2491+ dst->nrequired = src->nrequired;
2492+ dst->rest_idx = src->rest_idx;
2493+ if (src->blk_param) dst->blk_param = strdup(src->blk_param);
2494+ /* Copy parameter names and defaults. */
2495+ dst->nparams = src->nparams;
2496+ if (src->nparams > 0) {
2497+ dst->pnames = malloc(sizeof(char *) * (size_t)src->nparams);
2498+ dst->pdefault = malloc(sizeof(int) * (size_t)src->nparams);
2499+ for (int p = 0; p < src->nparams; p++) {
2500+ dst->pnames[p] = src->pnames[p] ? strdup(src->pnames[p]) : NULL;
2501+ dst->pdefault[p] = src->pdefault ? src->pdefault[p] : -1;
2502+ }
2503+ /* Register param locals so infer_param_types can update types. */
2504+ for (int p = 0; p < src->nparams; p++) {
2505+ if (dst->pnames[p]) {
2506+ LocalVar *lv = scope_local_intern(dst, dst->pnames[p]);
2507+ lv->is_param = 1;
2508+ }
2509+ }
2510+ src->is_transplanted_source = 1;
24832511 }
24842512 }
24852513 }
24862514 }
24872515 }
24882516
2517+/* For each class, find `include M` declarations in ALL class bodies
2518+ (including reopenings) and transplant M's instance methods into the
2519+ class so they are reachable via comp_method_in_chain. */
2520+static void register_includes(Compiler *c) {
2521+ const NodeTable *nt = c->nt;
2522+ /* First pass: process def_node bodies (first class definition). */
2523+ for (int ci = 0; ci < c->nclasses; ci++) {
2524+ int body = nt_ref(nt, c->classes[ci].def_node, "body");
2525+ process_include_body(c, ci, body);
2526+ }
2527+ /* Second pass: scan all ClassNode/ModuleNode in the AST for reopenings. */
2528+ for (int id = 0; id < nt->count; id++) {
2529+ const char *ty = nt_type(nt, id);
2530+ if (!ty || (strcmp(ty, "ClassNode") && strcmp(ty, "ModuleNode"))) continue;
2531+ int cp = nt_ref(nt, id, "constant_path");
2532+ const char *cname = cp >= 0 ? nt_str(nt, cp, "name") : NULL;
2533+ if (!cname) continue;
2534+ int ci = comp_class_index(c, cname);
2535+ if (ci < 0) continue;
2536+ if (id == c->classes[ci].def_node) continue; /* already processed above */
2537+ int body = nt_ref(nt, id, "body");
2538+ process_include_body(c, ci, body);
2539+ }
2540+}
2541+
24892542 /* For each class, find `prepend M` declarations and transplant M's instance
24902543 methods into the class with shadow-chain renaming so `super` can route
24912544 from M's body to the original (now renamed) class body. */
@@ -5033,6 +5086,33 @@
50335086 (e.g. a never-called method with an uninferrable param). */
50345087 compute_reachable(c);
50355088
5089+ /* Post-fixpoint: scope copies created by register_includes have pnames but no
5090+ locals (since nscope still maps body nodes to the original scope). Find each
5091+ such copy and transplant the finalized locals + return type from the source. */
5092+ for (int ci = 0; ci < c->nscopes; ci++) {
5093+ Scope *s = &c->scopes[ci];
5094+ if (!s->name || s->nparams == 0 || s->nlocals > 0) continue;
5095+ /* Has params but no locals: a copy whose params were never registered. */
5096+ for (int si = 0; si < c->nscopes; si++) {
5097+ if (si == ci) continue;
5098+ Scope *src = &c->scopes[si];
5099+ if (!src->name || strcmp(src->name, s->name)) continue;
5100+ if (src->body != s->body || src->nlocals == 0) continue;
5101+ if (src->nparams != s->nparams) continue;
5102+ if (!scope_local(src, s->pnames[0])) continue;
5103+ s->nlocals = src->nlocals;
5104+ s->clocals = src->nlocals;
5105+ s->locals = malloc(sizeof(LocalVar) * (size_t)src->nlocals);
5106+ for (int l = 0; l < src->nlocals; l++) {
5107+ s->locals[l] = src->locals[l];
5108+ s->locals[l].name = src->locals[l].name ? strdup(src->locals[l].name) : NULL;
5109+ }
5110+ s->ret = src->ret;
5111+ s->ret_proc_ret = src->ret_proc_ret;
5112+ break;
5113+ }
5114+ }
5115+
50365116 /* finalize: gc-root needs + full node type cache */
50375117 for (int s = 0; s < c->nscopes; s++)
50385118 for (int i = 0; i < c->scopes[s].nlocals; i++)