Commit cf73c878

cf73c8788f51Merged

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

Description

analyze: implement extend M as class-method transplant

Add register_extends: scans each class body for extend M calls and transplants M's instance methods as class methods (is_cmethod=1) into the extending class, mirroring the include transplant approach.

Param locals are registered immediately so infer_param_types can type them during the fixpoint. Source scopes are marked is_transplanted_source so codegen skips emitting them directly.

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

Details

Commit
cf73c8788f51
Parent
6363ab430bfe

src/analyze.cmodifiedView file

@@ -2553,6 +2553,69 @@
25532553 }
25542554 }
25552555
2556+/* For each class, find `extend M` declarations and transplant M's instance
2557+ methods as class methods (is_cmethod=1) so they are callable as C.m. */
2558+static void register_extends(Compiler *c) {
2559+ const NodeTable *nt = c->nt;
2560+ for (int ci = 0; ci < c->nclasses; ci++) {
2561+ int body = nt_ref(nt, c->classes[ci].def_node, "body");
2562+ int n = 0;
2563+ const int *stmts = body >= 0 ? nt_arr(nt, body, "body", &n) : NULL;
2564+ for (int k = 0; k < n; k++) {
2565+ int s = stmts[k];
2566+ const char *sty = nt_type(nt, s);
2567+ if (!sty || strcmp(sty, "CallNode")) continue;
2568+ const char *nm = nt_str(nt, s, "name");
2569+ if (!nm || strcmp(nm, "extend")) continue;
2570+ if (nt_ref(nt, s, "receiver") >= 0) continue;
2571+ int anode = nt_ref(nt, s, "arguments");
2572+ int an = 0;
2573+ const int *args = anode >= 0 ? nt_arr(nt, anode, "arguments", &an) : NULL;
2574+ for (int j = 0; j < an; j++) {
2575+ const char *aty = nt_type(nt, args[j]);
2576+ const char *mname = NULL;
2577+ if (aty && !strcmp(aty, "ConstantReadNode")) mname = nt_str(nt, args[j], "name");
2578+ else if (aty && !strcmp(aty, "ConstantPathNode")) mname = nt_str(nt, args[j], "name");
2579+ int mod_id = mname ? comp_class_index(c, mname) : -1;
2580+ if (mod_id < 0) continue;
2581+ int snap = c->nscopes;
2582+ for (int ms = 0; ms < snap; ms++) {
2583+ Scope *src = &c->scopes[ms];
2584+ /* Only transplant instance methods; self.* on the module stay on it. */
2585+ if (src->class_id != mod_id || src->is_cmethod || !src->name) continue;
2586+ if (comp_cmethod_in_class(c, ci, src->name) >= 0) continue;
2587+ Scope *dst = comp_scope_new(c, src->name, src->def_node);
2588+ src = &c->scopes[ms];
2589+ dst->body = src->body;
2590+ dst->class_id = ci;
2591+ dst->is_cmethod = 1; /* transplanted as a class method */
2592+ dst->reachable = src->reachable;
2593+ dst->yields = src->yields;
2594+ dst->nrequired = src->nrequired;
2595+ dst->rest_idx = src->rest_idx;
2596+ if (src->blk_param) dst->blk_param = strdup(src->blk_param);
2597+ dst->nparams = src->nparams;
2598+ if (src->nparams > 0) {
2599+ dst->pnames = malloc(sizeof(char *) * (size_t)src->nparams);
2600+ dst->pdefault = malloc(sizeof(int) * (size_t)src->nparams);
2601+ for (int p = 0; p < src->nparams; p++) {
2602+ dst->pnames[p] = src->pnames[p] ? strdup(src->pnames[p]) : NULL;
2603+ dst->pdefault[p] = src->pdefault ? src->pdefault[p] : -1;
2604+ }
2605+ for (int p = 0; p < src->nparams; p++) {
2606+ if (dst->pnames[p]) {
2607+ LocalVar *lv = scope_local_intern(dst, dst->pnames[p]);
2608+ lv->is_param = 1;
2609+ }
2610+ }
2611+ }
2612+ src->is_transplanted_source = 1;
2613+ }
2614+ }
2615+ }
2616+ }
2617+}
2618+
25562619 /* For each class, find `prepend M` declarations and transplant M's instance
25572620 methods into the class with shadow-chain renaming so `super` can route
25582621 from M's body to the original (now renamed) class body. */
@@ -4821,6 +4884,7 @@
48214884 resolve_parents(c);
48224885 inherit_members(c);
48234886 register_includes(c);
4887+ register_extends(c);
48244888 register_prepends(c);
48254889
48264890 /* collect top-level `include <Mod>` calls so bare method calls can