Commit 36a47707

36a47707f320Merged

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

Description

Support module_function + top-level include, fix M::Cls.new seeding

Three related fixes:

  1. M::Cls.new(args) in the analyze seeding fixpoint: the ConstantPathNode receiver case (e.g. M::Sub.new(7)) was not seeding initialize params. Added the missing ConstantPathNode arm in the seed-params loop.

  2. module_function + include Mod: a new register_module_functions pass marks defs following module_function in a module body as is_cmethod=1 (no self parameter). A new collect_toplevel_includes pass records which modules were included at the top level. comp_included_method_index then lets bare calls (no receiver) resolve to those module methods in both the analyze fixpoint and codegen emit_call (last-include-wins, user defs take priority). Declarative calls (include, extend, module_function, etc) in emit_stmt are now no-ops instead of unsupported-call errors.

  3. Empty [] passed as argument to a non-Int array parameter: emit the right typed constructor (sp_StrArray_new() etc.) rather than the default sp_IntArray_new().

Fixes subclass_inherits_module_parent, toplevel_include_module_function, unless_return_path_hash (via previous commit).

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

Details

Commit
36a47707f320
Parent
0238ed488085

src/analyze.cmodifiedView file

@@ -505,6 +505,7 @@
505505 /* user-defined free-function call (no receiver) */
506506 if (recv < 0) {
507507 int mi = comp_method_index(c, name);
508+ if (mi < 0) mi = comp_included_method_index(c, name);
508509 if (mi >= 0) return method_call_ret(c, mi, id);
509510 /* Kernel conversions */
510511 if (!strcmp(name, "Integer") && argc == 1) return TY_INT;
@@ -1154,6 +1155,39 @@
11541155 }
11551156 }
11561157
1158+/* Mark methods following `module_function` in a module body as class-level
1159+ (is_cmethod=1, no self param). This lets them be called as bare functions
1160+ when their module is included at the top level. */
1161+static void register_module_functions(Compiler *c) {
1162+ const NodeTable *nt = c->nt;
1163+ for (int ci = 0; ci < c->nclasses; ci++) {
1164+ int dn = c->classes[ci].def_node;
1165+ const char *dt = dn >= 0 ? nt_type(nt, dn) : NULL;
1166+ if (!dt || strcmp(dt, "ModuleNode")) continue;
1167+ int body = nt_ref(nt, dn, "body");
1168+ if (body < 0) continue;
1169+ int bn = 0;
1170+ const int *stmts = nt_arr(nt, body, "body", &bn);
1171+ int in_module_function = 0;
1172+ for (int k = 0; k < bn; k++) {
1173+ int s = stmts[k];
1174+ const char *sty = nt_type(nt, s);
1175+ if (!sty) continue;
1176+ if (!strcmp(sty, "CallNode") && nt_ref(nt, s, "receiver") < 0) {
1177+ const char *nm = nt_str(nt, s, "name");
1178+ if (nm && !strcmp(nm, "module_function")) { in_module_function = 1; continue; }
1179+ }
1180+ if (!strcmp(sty, "DefNode") && in_module_function) {
1181+ const char *mname = nt_str(nt, s, "name");
1182+ if (!mname) continue;
1183+ for (int mi = 0; mi < c->nscopes; mi++) {
1184+ if (c->scopes[mi].def_node == s) { c->scopes[mi].is_cmethod = 1; break; }
1185+ }
1186+ }
1187+ }
1188+ }
1189+}
1190+
11571191 static void register_locals(Compiler *c) {
11581192 const NodeTable *nt = c->nt;
11591193 for (int id = 0; id < nt->count; id++) {
@@ -1847,12 +1881,22 @@
18471881 Scope *self = comp_scope_of(c, id);
18481882 if (self->class_id >= 0) mi = comp_method_in_chain(c, self->class_id, name, NULL);
18491883 }
1884+ if (mi < 0) mi = comp_included_method_index(c, name);
18501885 changed |= bind_call_params(c, id, mi);
18511886 continue;
18521887 }
18531888 /* Class.new -> initialize params; Class.cmethod -> cmethod params */
18541889 {
18551890 const char *rty = nt_type(nt, recv);
1891+ /* M::Sub.new(...) — resolve by the final path component */
1892+ if (rty && !strcmp(rty, "ConstantPathNode")) {
1893+ const char *cn = nt_str(nt, recv, "name");
1894+ int ci = cn ? comp_class_index(c, cn) : -1;
1895+ if (ci >= 0 && !strcmp(name, "new"))
1896+ changed |= bind_call_params(c, id, comp_method_in_chain(c, ci, "initialize", NULL));
1897+ else if (ci >= 0)
1898+ changed |= bind_call_params(c, id, comp_cmethod_in_chain(c, ci, name, NULL));
1899+ }
18561900 if (rty && !strcmp(rty, "ConstantReadNode")) {
18571901 int ci = comp_class_index(c, nt_str(nt, recv, "name"));
18581902 if (ci >= 0) {
@@ -2442,6 +2486,7 @@
24422486
24432487 walk_scope(c, c->nt->root_id, 0, -1);
24442488 register_structs(c);
2489+ register_module_functions(c);
24452490 register_locals(c);
24462491 register_attrs(c);
24472492 register_aliases(c);
@@ -2463,6 +2508,34 @@
24632508 resolve_parents(c);
24642509 inherit_members(c);
24652510
2511+ /* collect top-level `include <Mod>` calls so bare method calls can
2512+ resolve to module_function methods in those modules. */
2513+ {
2514+ const NodeTable *nt = c->nt;
2515+ int root_stmts = nt_ref(nt, nt->root_id, "statements");
2516+ int sn = 0;
2517+ const int *stmts = root_stmts >= 0 ? nt_arr(nt, root_stmts, "body", &sn) : NULL;
2518+ for (int i = 0; i < sn; i++) {
2519+ if (!nt_type(nt, stmts[i]) || strcmp(nt_type(nt, stmts[i]), "CallNode")) continue;
2520+ if (!nt_str(nt, stmts[i], "name") || strcmp(nt_str(nt, stmts[i], "name"), "include")) continue;
2521+ if (nt_ref(nt, stmts[i], "receiver") >= 0) continue;
2522+ int anode = nt_ref(nt, stmts[i], "arguments");
2523+ int an = 0;
2524+ const int *args = anode >= 0 ? nt_arr(nt, anode, "arguments", &an) : NULL;
2525+ for (int j = 0; j < an; j++) {
2526+ const char *aty = nt_type(nt, args[j]);
2527+ const char *mname = NULL;
2528+ if (aty && !strcmp(aty, "ConstantReadNode")) mname = nt_str(nt, args[j], "name");
2529+ else if (aty && !strcmp(aty, "ConstantPathNode")) mname = nt_str(nt, args[j], "name");
2530+ int ci = mname ? comp_class_index(c, mname) : -1;
2531+ if (ci < 0) continue;
2532+ c->toplevel_includes = realloc(c->toplevel_includes,
2533+ sizeof(int) * (size_t)(c->ntoplevel_includes + 1));
2534+ c->toplevel_includes[c->ntoplevel_includes++] = ci;
2535+ }
2536+ }
2537+ }
2538+
24662539 /* mark block-aware methods (contain yield or block_given?) -- these are
24672540 inlined at every call site so block_given? reflects the actual site */
24682541 for (int id = 0; id < c->nt->count; id++) {