Commit b6427d2e
b6427d2e9977Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Class variables (@@x)
Each @@x is registered in its owning class with a fixpoint-inferred type and
lowered to a file-scope static cvar_<Class>_<name>. Reads and writes (in both
statement and value position) route to that static; the write yields the
assigned value. Operator-writes (@@x += 1) and multiple-assignment targets are
not yet covered.
make test: 179 pass / 5 fail / 660 error (was 178 / 5 / 661); cvar_method_first_assign green, no regressions.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- b6427d2e9977
- Parent
- 52299d2c09e2
src/analyze.cmodifiedView file
@@ -682,6 +682,15 @@
682682
int iv = nm ? comp_ivar_index(ci, nm) : -1;683683
return iv >= 0 ? ci->ivar_types[iv] : TY_UNKNOWN;684684
}685+
if (!strcmp(ty, "ClassVariableReadNode")) {686+
const char *nm = nt_str(nt, id, "name");687+
Scope *s = comp_scope_of(c, id);688+
if (s->class_id < 0) return TY_UNKNOWN;689+
int idx = nm ? comp_cvar_index(&c->classes[s->class_id], nm) : -1;690+
return idx >= 0 ? c->classes[s->class_id].cvar_types[idx] : TY_UNKNOWN;691+
}692+
if (!strcmp(ty, "ClassVariableWriteNode"))693+
return infer_type(c, nt_ref(nt, id, "value"));685694
if (!strcmp(ty, "ParenthesesNode")) {686695
int body = nt_ref(nt, id, "body");687696
if (body < 0) return TY_NIL;@@ -1140,6 +1149,27 @@
11401149
}11411150
11421151
/* @ivar types from their assignments across the class's methods. */1152+
/* Register each class variable (@@x) in its owning class and infer its type1153+
from the write sites' RHS. */1154+
static int infer_cvar_types(Compiler *c) {1155+
const NodeTable *nt = c->nt;1156+
int changed = 0;1157+
for (int id = 0; id < nt->count; id++) {1158+
const char *ty = nt_type(nt, id);1159+
if (!ty || strcmp(ty, "ClassVariableWriteNode")) continue;1160+
const char *nm = nt_str(nt, id, "name");1161+
Scope *s = comp_scope_of(c, id);1162+
if (!nm || s->class_id < 0) continue;1163+
ClassInfo *ci = &c->classes[s->class_id];1164+
int idx = comp_cvar_intern(ci, nm);1165+
TyKind vt = infer_type(c, nt_ref(nt, id, "value"));1166+
if (vt == TY_NIL) continue;1167+
TyKind merged = ty_unify(ci->cvar_types[idx], vt);1168+
if (merged != ci->cvar_types[idx]) { ci->cvar_types[idx] = merged; changed = 1; }1169+
}1170+
return changed;1171+
}1172+
11431173
static int infer_ivar_types(Compiler *c) {11441174
const NodeTable *nt = c->nt;11451175
int changed = 0;@@ -2004,6 +2034,7 @@
20042034
ch |= infer_block_params(c);20052035
ch |= infer_for_index(c);20062036
ch |= infer_ivar_types(c);2037+
ch |= infer_cvar_types(c);20072038
ch |= infer_inherited_ivars(c);20082039
ch |= infer_global_const_types(c);20092040
ch |= infer_return_types(c);src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…