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:
-
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.
-
module_function+include Mod: a new register_module_functions pass marks defs followingmodule_functionin 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. -
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
/* user-defined free-function call (no receiver) */ if (recv < 0) { int mi = comp_method_index(c, name); if (mi < 0) mi = comp_included_method_index(c, name); if (mi >= 0) return method_call_ret(c, mi, id); /* Kernel conversions */ if (!strcmp(name, "Integer") && argc == 1) return TY_INT; }}/* Mark methods following `module_function` in a module body as class-level (is_cmethod=1, no self param). This lets them be called as bare functions when their module is included at the top level. */static void register_module_functions(Compiler *c) { const NodeTable *nt = c->nt; for (int ci = 0; ci < c->nclasses; ci++) { int dn = c->classes[ci].def_node; const char *dt = dn >= 0 ? nt_type(nt, dn) : NULL; if (!dt || strcmp(dt, "ModuleNode")) continue; int body = nt_ref(nt, dn, "body"); if (body < 0) continue; int bn = 0; const int *stmts = nt_arr(nt, body, "body", &bn); int in_module_function = 0; for (int k = 0; k < bn; k++) { int s = stmts[k]; const char *sty = nt_type(nt, s); if (!sty) continue; if (!strcmp(sty, "CallNode") && nt_ref(nt, s, "receiver") < 0) { const char *nm = nt_str(nt, s, "name"); if (nm && !strcmp(nm, "module_function")) { in_module_function = 1; continue; } } if (!strcmp(sty, "DefNode") && in_module_function) { const char *mname = nt_str(nt, s, "name"); if (!mname) continue; for (int mi = 0; mi < c->nscopes; mi++) { if (c->scopes[mi].def_node == s) { c->scopes[mi].is_cmethod = 1; break; } } } } }}static void register_locals(Compiler *c) { const NodeTable *nt = c->nt; for (int id = 0; id < nt->count; id++) { Scope *self = comp_scope_of(c, id); if (self->class_id >= 0) mi = comp_method_in_chain(c, self->class_id, name, NULL); } if (mi < 0) mi = comp_included_method_index(c, name); changed |= bind_call_params(c, id, mi); continue; } /* Class.new -> initialize params; Class.cmethod -> cmethod params */ { const char *rty = nt_type(nt, recv); /* M::Sub.new(...) — resolve by the final path component */ if (rty && !strcmp(rty, "ConstantPathNode")) { const char *cn = nt_str(nt, recv, "name"); int ci = cn ? comp_class_index(c, cn) : -1; if (ci >= 0 && !strcmp(name, "new")) changed |= bind_call_params(c, id, comp_method_in_chain(c, ci, "initialize", NULL)); else if (ci >= 0) changed |= bind_call_params(c, id, comp_cmethod_in_chain(c, ci, name, NULL)); } if (rty && !strcmp(rty, "ConstantReadNode")) { int ci = comp_class_index(c, nt_str(nt, recv, "name")); if (ci >= 0) { walk_scope(c, c->nt->root_id, 0, -1); register_structs(c); register_module_functions(c); register_locals(c); register_attrs(c); register_aliases(c); resolve_parents(c); inherit_members(c); /* collect top-level `include <Mod>` calls so bare method calls can resolve to module_function methods in those modules. */ { const NodeTable *nt = c->nt; int root_stmts = nt_ref(nt, nt->root_id, "statements"); int sn = 0; const int *stmts = root_stmts >= 0 ? nt_arr(nt, root_stmts, "body", &sn) : NULL; for (int i = 0; i < sn; i++) { if (!nt_type(nt, stmts[i]) || strcmp(nt_type(nt, stmts[i]), "CallNode")) continue; if (!nt_str(nt, stmts[i], "name") || strcmp(nt_str(nt, stmts[i], "name"), "include")) continue; if (nt_ref(nt, stmts[i], "receiver") >= 0) continue; int anode = nt_ref(nt, stmts[i], "arguments"); int an = 0; const int *args = anode >= 0 ? nt_arr(nt, anode, "arguments", &an) : NULL; for (int j = 0; j < an; j++) { const char *aty = nt_type(nt, args[j]); const char *mname = NULL; if (aty && !strcmp(aty, "ConstantReadNode")) mname = nt_str(nt, args[j], "name"); else if (aty && !strcmp(aty, "ConstantPathNode")) mname = nt_str(nt, args[j], "name"); int ci = mname ? comp_class_index(c, mname) : -1; if (ci < 0) continue; c->toplevel_includes = realloc(c->toplevel_includes, sizeof(int) * (size_t)(c->ntoplevel_includes + 1)); c->toplevel_includes[c->ntoplevel_includes++] = ci; } } } /* mark block-aware methods (contain yield or block_given?) -- these are inlined at every call site so block_given? reflects the actual site */ for (int id = 0; id < c->nt->count; id++) {src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…