Commit 41db42fe

41db42fe3079Merged

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

Description

Support class << self; def X; end; end inside class bodies

  • Extract collect_def_params helper from inline DefNode handling
  • In walk_scope: detect SingletonClassNode and register its DefNode children as class methods (is_cmethod=1) on the enclosing class, preventing double-registration as instance methods
  • In codegen class-body loop: skip SingletonClassNode statements (like DefNode) since methods are emitted separately via emit_class_methods

Fixes class_singleton_def. Module singleton attr_accessor support is deferred (needs poly-const backing for Mod.accessor read/write).

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

Details

Commit
41db42fe3079
Parent
5aa545581f8b

src/analyze.cmodifiedView file

@@ -1909,6 +1909,58 @@
19091909 lv->is_param = 1;
19101910 }
19111911
1912+/* Collect parameters from a DefNode into scope s. */
1913+static void collect_def_params(Compiler *c, int def_id, Scope *s) {
1914+ int pn = nt_ref(c->nt, def_id, "parameters");
1915+ if (pn < 0) return;
1916+ int rn = 0;
1917+ const int *reqs = nt_arr(c->nt, pn, "requireds", &rn);
1918+ for (int i = 0; i < rn; i++) {
1919+ const char *pname = nt_str(c->nt, reqs[i], "name");
1920+ if (pname) scope_add_param(s, pname, -1);
1921+ }
1922+ int on = 0;
1923+ const int *opts = nt_arr(c->nt, pn, "optionals", &on);
1924+ for (int i = 0; i < on; i++) {
1925+ const char *pname = nt_str(c->nt, opts[i], "name");
1926+ int dv = nt_ref(c->nt, opts[i], "value");
1927+ if (pname) scope_add_param(s, pname, dv);
1928+ }
1929+ int rp = nt_ref(c->nt, pn, "rest");
1930+ if (rp >= 0) {
1931+ const char *rpty = nt_type(c->nt, rp);
1932+ if (rpty && !strcmp(rpty, "RestParameterNode")) {
1933+ const char *rname = nt_str(c->nt, rp, "name");
1934+ if (rname) {
1935+ if (s->nparams % 8 == 0) {
1936+ s->pnames = realloc(s->pnames, sizeof(char *) * (size_t)(s->nparams + 8));
1937+ s->pdefault = realloc(s->pdefault, sizeof(int) * (size_t)(s->nparams + 8));
1938+ }
1939+ s->pdefault[s->nparams] = -1;
1940+ s->pnames[s->nparams++] = strdup(rname);
1941+ LocalVar *lv = scope_local_intern(s, rname);
1942+ lv->is_param = 1;
1943+ lv->type = TY_POLY_ARRAY;
1944+ s->rest_idx = s->nparams - 1;
1945+ }
1946+ }
1947+ }
1948+ int kn = 0;
1949+ const int *kws = nt_arr(c->nt, pn, "keywords", &kn);
1950+ for (int i = 0; i < kn; i++) {
1951+ const char *pty = nt_type(c->nt, kws[i]);
1952+ if (!pty) continue;
1953+ const char *pname = nt_str(c->nt, kws[i], "name");
1954+ int dv = !strcmp(pty, "OptionalKeywordParameterNode") ? nt_ref(c->nt, kws[i], "value") : -1;
1955+ if (pname) scope_add_param(s, pname, dv);
1956+ }
1957+ int bp = nt_ref(c->nt, pn, "block");
1958+ if (bp >= 0 && nt_type(c->nt, bp) && !strcmp(nt_type(c->nt, bp), "BlockParameterNode")) {
1959+ const char *bn = nt_str(c->nt, bp, "name");
1960+ s->blk_param = strdup(bn ? bn : "");
1961+ }
1962+}
1963+
19121964 static void walk_scope(Compiler *c, int id, int scope_idx, int class_id) {
19131965 if (id < 0 || id >= c->nt->count) return;
19141966 c->nscope[id] = scope_idx;
@@ -1916,6 +1968,39 @@
19161968 int child = scope_idx;
19171969 int child_class = class_id;
19181970
1971+ /* `class << self; def X; ...; end; end` — treat body defs as class methods. */
1972+ if (ty && !strcmp(ty, "SingletonClassNode")) {
1973+ int sbody = nt_ref(c->nt, id, "body");
1974+ if (sbody >= 0) {
1975+ int n = 0;
1976+ const int *stmts = nt_arr(c->nt, sbody, "body", &n);
1977+ for (int k = 0; k < n; k++) {
1978+ int s = stmts[k];
1979+ const char *sty = nt_type(c->nt, s);
1980+ if (!sty) continue;
1981+ if (!strcmp(sty, "DefNode")) {
1982+ const char *name = nt_str(c->nt, s, "name");
1983+ if (!name) continue;
1984+ Scope *sc = comp_scope_new(c, name, s);
1985+ int new_idx = c->nscopes - 1;
1986+ sc->body = nt_ref(c->nt, s, "body");
1987+ sc->class_id = class_id;
1988+ sc->is_cmethod = 1;
1989+ collect_def_params(c, s, sc);
1990+ /* Assign scope to the def node and its body */
1991+ c->nscope[s] = new_idx;
1992+ if (sc->body >= 0) walk_scope(c, sc->body, new_idx, class_id);
1993+ }
1994+ else {
1995+ walk_scope(c, s, scope_idx, class_id);
1996+ }
1997+ }
1998+ c->nscope[id] = scope_idx;
1999+ c->nscope[sbody] = scope_idx;
2000+ }
2001+ return;
2002+ }
2003+
19192004 if (ty && (!strcmp(ty, "ClassNode") || !strcmp(ty, "ModuleNode"))) {
19202005 int cp = nt_ref(c->nt, id, "constant_path");
19212006 const char *cname = cp >= 0 ? nt_str(c->nt, cp, "name") : NULL;
@@ -1935,60 +2020,7 @@
19352020 s->class_id = class_id; /* instance method of the enclosing class */
19362021 /* `def self.foo` / `def Klass.foo`: a class (singleton) method. */
19372022 if (nt_ref(c->nt, id, "receiver") >= 0) s->is_cmethod = 1;
1938- int pn = nt_ref(c->nt, id, "parameters");
1939- if (pn >= 0) {
1940- int rn = 0;
1941- const int *reqs = nt_arr(c->nt, pn, "requireds", &rn);
1942- for (int i = 0; i < rn; i++) {
1943- const char *pname = nt_str(c->nt, reqs[i], "name");
1944- if (pname) scope_add_param(s, pname, -1);
1945- }
1946- int on = 0;
1947- const int *opts = nt_arr(c->nt, pn, "optionals", &on);
1948- for (int i = 0; i < on; i++) {
1949- const char *pname = nt_str(c->nt, opts[i], "name");
1950- int dv = nt_ref(c->nt, opts[i], "value");
1951- if (pname) scope_add_param(s, pname, dv);
1952- }
1953- /* `*rest` parameter */
1954- int rp = nt_ref(c->nt, pn, "rest");
1955- if (rp >= 0) {
1956- const char *rpty = nt_type(c->nt, rp);
1957- if (rpty && !strcmp(rpty, "RestParameterNode")) {
1958- const char *rname = nt_str(c->nt, rp, "name");
1959- if (rname) {
1960- /* Add param slot without bumping nrequired */
1961- if (s->nparams % 8 == 0) {
1962- s->pnames = realloc(s->pnames, sizeof(char *) * (size_t)(s->nparams + 8));
1963- s->pdefault = realloc(s->pdefault, sizeof(int) * (size_t)(s->nparams + 8));
1964- }
1965- s->pdefault[s->nparams] = -1;
1966- s->pnames[s->nparams++] = strdup(rname);
1967- LocalVar *lv = scope_local_intern(s, rname);
1968- lv->is_param = 1;
1969- lv->type = TY_POLY_ARRAY;
1970- s->rest_idx = s->nparams - 1;
1971- }
1972- }
1973- }
1974- /* keyword parameters: `tag:` (required) or `tag: default` (optional) */
1975- int kn = 0;
1976- const int *kws = nt_arr(c->nt, pn, "keywords", &kn);
1977- for (int i = 0; i < kn; i++) {
1978- const char *pty = nt_type(c->nt, kws[i]);
1979- if (!pty) continue;
1980- const char *pname = nt_str(c->nt, kws[i], "name");
1981- int dv = !strcmp(pty, "OptionalKeywordParameterNode") ? nt_ref(c->nt, kws[i], "value") : -1;
1982- if (pname) scope_add_param(s, pname, dv);
1983- }
1984- /* `&block` parameter: tracked for yield-style inlining, no typed slot.
1985- Anonymous `&` has no name; record "" so forwarding still works. */
1986- int bp = nt_ref(c->nt, pn, "block");
1987- if (bp >= 0 && nt_type(c->nt, bp) && !strcmp(nt_type(c->nt, bp), "BlockParameterNode")) {
1988- const char *bn = nt_str(c->nt, bp, "name");
1989- s->blk_param = strdup(bn ? bn : "");
1990- }
1991- }
2023+ collect_def_params(c, id, s);
19922024 child = new_idx;
19932025 }
19942026