Commit a902895e
a902895ea417Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Implement module singleton accessor codegen + fixes
class << self; attr_accessor :x; end: emitstatic sp_RbVal sg_<Mod>_<field>globals and dispatch Module.field= setters / Module.field getters- Handle
self.field=/self.fieldfrom both class methods and module body (trackg_cbody_class_idin analyze,g_class_body_idin codegen) - Add
emit_box_open/closehelpers for boxing any TyKind to sp_RbVal - Fix user-defined
def self.new: check for it before treating as constructor in both analyze bind_call_params and codegen emit_call_expr - Fix npost_rest (post-splat required params) in analyze + codegen
- Fix Time arithmetic stale-cache: guard
eff_res==TY_FLOATwithrt!=TY_TIME
Fixes #511, #625
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- a902895ea417
- Parent
- 3cfa76fe6ee1
src/analyze.cmodifiedView file
@@ -97,6 +97,9 @@
9797
-1 = no override; set to the receiver's class_id while inferring9898
the block body so that InstanceVariableReadNode uses the right class. */9999
static int g_ie_class_id = -1;100+
/* Class index of the class/module body currently being analyzed (set around101+
ClassNode/ModuleNode body traversal). -1 outside any class body. */102+
static int g_cbody_class_id = -1;100103
101104
/* The value type of `yield` / a `<&block-param>.call` inside method mi: the102105
block-body value type at a (any) call site of mi. Polymorphic, resolved from@@ -327,6 +330,47 @@
327330
nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "class"))328331
return TY_STRING;329332
333+
/* Module.singleton_writer= / Module.singleton_reader */334+
if (recv >= 0 && nt_type(nt, recv) &&335+
(!strcmp(nt_type(nt, recv), "ConstantReadNode") ||336+
!strcmp(nt_type(nt, recv), "ConstantPathNode"))) {337+
const char *cn = nt_str(nt, recv, "name");338+
int ci = cn ? comp_class_index(c, cn) : -1;339+
if (ci >= 0) {340+
ClassInfo *cls = &c->classes[ci];341+
int nlen = (int)strlen(name);342+
/* setter: name ends with '=' */343+
if (nlen > 1 && name[nlen - 1] == '=') {344+
char base[256]; int blen = nlen - 1;345+
if (blen > 0 && blen < (int)sizeof(base)) {346+
memcpy(base, name, (size_t)blen); base[blen] = '\0';347+
if (comp_is_sg_writer(cls, base)) return TY_VOID;348+
}349+
} else {350+
if (comp_is_sg_reader(cls, name)) return TY_POLY;351+
}352+
}353+
}354+
/* self.singleton_writer= / self.singleton_reader: inside a class method355+
or directly in a class/module body (g_cbody_class_id). */356+
if (recv >= 0 && nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "SelfNode")) {357+
Scope *_self = comp_scope_of(c, id);358+
int _sg_cid = (_self && _self->is_cmethod && _self->class_id >= 0)359+
? _self->class_id : g_cbody_class_id;360+
if (_sg_cid >= 0) {361+
ClassInfo *_cls = &c->classes[_sg_cid];362+
int _nlen = (int)strlen(name);363+
if (_nlen > 1 && name[_nlen - 1] == '=') {364+
char _base[256]; int _blen = _nlen - 1;365+
if (_blen > 0 && _blen < (int)sizeof(_base)) {366+
memcpy(_base, name, (size_t)_blen); _base[_blen] = '\0';367+
if (comp_is_sg_writer(_cls, _base)) return TY_VOID;368+
}369+
}370+
else if (comp_is_sg_reader(_cls, name)) return TY_POLY;371+
}372+
}373+
330374
/* SomeClass.name / .to_s / .inspect -> the class-name string */331375
if (recv >= 0 && argc == 0 &&332376
(!strcmp(name, "name") || !strcmp(name, "to_s") || !strcmp(name, "inspect") ||@@ -350,12 +394,20 @@
350394
if (rty && !strcmp(rty, "ConstantPathNode")) {351395
const char *cn = nt_str(nt, recv, "name");352396
int ci = cn ? comp_class_index(c, cn) : -1;353-
if (ci >= 0) return ty_object(ci);397+
if (ci >= 0) {398+
int ucnew = comp_cmethod_in_chain(c, ci, "new", NULL);399+
if (ucnew >= 0) return (TyKind)c->scopes[ucnew].ret;400+
return ty_object(ci);401+
}354402
}355403
if (rty && !strcmp(rty, "ConstantReadNode")) {356404
const char *cn = nt_str(nt, recv, "name");357405
int ci = comp_class_index(c, cn);358-
if (ci >= 0) return ty_object(ci);406+
if (ci >= 0) {407+
int ucnew = comp_cmethod_in_chain(c, ci, "new", NULL);408+
if (ucnew >= 0) return (TyKind)c->scopes[ucnew].ret;409+
return ty_object(ci);410+
}359411
if (cn && !strcmp(cn, "Array") && argc == 2) return ty_array_of(infer_type(c, argv[1]));360412
if (cn && !strcmp(cn, "Array")) {361413
int blk = nt_ref(nt, id, "block");@@ -1949,6 +2001,14 @@
19492001
}19502002
}19512003
}2004+
/* post-splat required parameters (Prism "posts" array) */2005+
int postn = 0;2006+
const int *posts = nt_arr(c->nt, pn, "posts", &postn);2007+
for (int i = 0; i < postn; i++) {2008+
const char *pname = nt_str(c->nt, posts[i], "name");2009+
if (pname) scope_add_param(s, pname, -1);2010+
}2011+
if (postn > 0) s->npost_rest = postn;19522012
int kn = 0;19532013
const int *kws = nt_arr(c->nt, pn, "keywords", &kn);19542014
for (int i = 0; i < kn; i++) {@@ -2028,6 +2088,9 @@
20282088
child = new_idx;20292089
}20302090
2091+
int saved_cbody = g_cbody_class_id;2092+
if (child_class >= 0) g_cbody_class_id = child_class;2093+
20312094
int nr = nt_num_refs(c->nt, id);20322095
for (int i = 0; i < nr; i++) {20332096
int r = nt_ref_at(c->nt, id, i);@@ -2040,6 +2103,7 @@
20402103
for (int j = 0; j < n; j++)20412104
if (ids[j] >= 0) walk_scope(c, ids[j], child, child_class);20422105
}2106+
g_cbody_class_id = saved_cbody;20432107
}20442108
20452109
/* Mark methods following `module_function` in a module body as class-level@@ -2177,8 +2241,41 @@
21772241
}21782242
}21792243
2244+
/* Process attr_accessor/reader/writer call: register ivars + reader/writer names.2245+
If `singleton` is non-zero, registers singleton (class-level) accessors instead. */2246+
static void register_attr_call(Compiler *c, ClassInfo *cls, int s, int singleton) {2247+
const NodeTable *nt = c->nt;2248+
const char *nm = nt_str(nt, s, "name");2249+
if (!nm) return;2250+
int accessor = !strcmp(nm, "attr_accessor") ||2251+
!strcmp(nm, "attribute") || !strcmp(nm, "attributes");2252+
int reader = !strcmp(nm, "attr_reader") || accessor;2253+
int writer = !strcmp(nm, "attr_writer") || accessor;2254+
if (!reader && !writer) return;2255+
int args = nt_ref(nt, s, "arguments");2256+
int an = 0;2257+
const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;2258+
for (int a = 0; a < an; a++) {2259+
const char *aty = nt_type(nt, argv[a]);2260+
if (!aty || strcmp(aty, "SymbolNode")) continue;2261+
const char *base = nt_str(nt, argv[a], "value");2262+
if (!base) continue;2263+
if (singleton) {2264+
if (reader) comp_add_sg_reader(cls, base);2265+
if (writer) comp_add_sg_writer(cls, base);2266+
} else {2267+
char ivname[256];2268+
snprintf(ivname, sizeof ivname, "@%s", base);2269+
comp_ivar_intern(cls, ivname);2270+
if (reader) comp_add_reader(cls, base);2271+
if (writer) comp_add_writer(cls, base);2272+
}2273+
}2274+
}2275+
21802276
/* Collect attr_reader/attr_writer/attr_accessor declarations in class2181-
bodies, registering backing ivars + reader/writer method names. */2277+
bodies, registering backing ivars + reader/writer method names.2278+
Also scans class << self bodies for singleton-level attr_accessors. */21822279
static void register_attrs(Compiler *c) {21832280
const NodeTable *nt = c->nt;21842281
for (int ci = 0; ci < c->nclasses; ci++) {@@ -2189,27 +2286,21 @@
21892286
for (int k = 0; k < n; k++) {21902287
int s = stmts[k];21912288
const char *sty = nt_type(nt, s);2192-
if (!sty || strcmp(sty, "CallNode")) continue;2193-
const char *nm = nt_str(nt, s, "name");2194-
if (!nm) continue;2195-
int accessor = !strcmp(nm, "attr_accessor") ||2196-
!strcmp(nm, "attribute") || !strcmp(nm, "attributes");2197-
int reader = !strcmp(nm, "attr_reader") || accessor;2198-
int writer = !strcmp(nm, "attr_writer") || accessor;2199-
if (!reader && !writer) continue;2200-
int args = nt_ref(nt, s, "arguments");2201-
int an = 0;2202-
const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;2203-
for (int a = 0; a < an; a++) {2204-
const char *aty = nt_type(nt, argv[a]);2205-
if (!aty || strcmp(aty, "SymbolNode")) continue;2206-
const char *base = nt_str(nt, argv[a], "value");2207-
if (!base) continue;2208-
char ivname[256];2209-
snprintf(ivname, sizeof ivname, "@%s", base);2210-
comp_ivar_intern(cls, ivname);2211-
if (reader) comp_add_reader(cls, base);2212-
if (writer) comp_add_writer(cls, base);2289+
if (!sty) continue;2290+
if (!strcmp(sty, "CallNode")) {2291+
register_attr_call(c, cls, s, 0);2292+
} else if (!strcmp(sty, "SingletonClassNode")) {2293+
/* class << self; attr_accessor :x; end */2294+
int sbody = nt_ref(nt, s, "body");2295+
if (sbody < 0) continue;2296+
int sn = 0;2297+
const int *sstmts = nt_arr(nt, sbody, "body", &sn);2298+
for (int j = 0; j < sn; j++) {2299+
int ss = sstmts[j];2300+
const char *ssty = nt_type(nt, ss);2301+
if (ssty && !strcmp(ssty, "CallNode"))2302+
register_attr_call(c, cls, ss, 1);2303+
}22132304
}22142305
}22152306
}@@ -3539,6 +3630,20 @@
35393630
if (pr != TY_UNKNOWN && p->proc_ret != (int)pr) { p->proc_ret = (int)pr; changed = 1; }35403631
}35413632
}3633+
/* Post-splat required params: bind from the end of the positional args. */3634+
if (m->rest_idx >= 0 && m->npost_rest > 0) {3635+
for (int j = 0; j < m->npost_rest; j++) {3636+
int pi = m->rest_idx + 1 + j;3637+
int ai = pos_argc - m->npost_rest + j;3638+
if (pi >= m->nparams || ai < 0 || ai >= pos_argc || !argv) continue;3639+
TyKind at = infer_type(c, argv[ai]);3640+
if (at == TY_NIL) at = TY_POLY;3641+
LocalVar *p = scope_local(m, m->pnames[pi]);3642+
if (!p) continue;3643+
TyKind merged = ty_unify(p->type, at);3644+
if (merged != p->type) { p->type = merged; changed = 1; }3645+
}3646+
}35423647
/* Keyword arguments: match KeywordHashNode elements to named params. */35433648
if (kwh >= 0) {35443649
int en = 0;@@ -3845,8 +3950,13 @@
38453950
if (rty && !strcmp(rty, "ConstantPathNode")) {38463951
const char *cn = nt_str(nt, recv, "name");38473952
int ci = cn ? comp_class_index(c, cn) : -1;3848-
if (ci >= 0 && !strcmp(name, "new"))3849-
changed |= bind_call_params(c, id, comp_method_in_chain(c, ci, "initialize", NULL));3953+
if (ci >= 0 && !strcmp(name, "new")) {3954+
int ucnew = comp_cmethod_in_chain(c, ci, "new", NULL);3955+
if (ucnew >= 0)3956+
changed |= bind_call_params(c, id, ucnew);3957+
else3958+
changed |= bind_call_params(c, id, comp_method_in_chain(c, ci, "initialize", NULL));3959+
}38503960
else if (ci >= 0)38513961
changed |= bind_call_params(c, id, comp_cmethod_in_chain(c, ci, name, NULL));38523962
}@@ -3880,8 +3990,13 @@
38803990
}38813991
continue;38823992
}3883-
if (!strcmp(name, "new"))3884-
changed |= bind_call_params(c, id, comp_method_in_chain(c, ci, "initialize", NULL));3993+
if (!strcmp(name, "new")) {3994+
int ucnew = comp_cmethod_in_chain(c, ci, "new", NULL);3995+
if (ucnew >= 0)3996+
changed |= bind_call_params(c, id, ucnew);3997+
else3998+
changed |= bind_call_params(c, id, comp_method_in_chain(c, ci, "initialize", NULL));3999+
}38854000
else38864001
changed |= bind_call_params(c, id, comp_cmethod_in_chain(c, ci, name, NULL));38874002
continue;src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…