Commit d195b324
d195b324e4e1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · ~5 modified
Description
Add M8: classes (value-type structs, instance methods, new, ivars)
Implement user-defined classes as value-type C structs, the way the legacy backend lowers simple objects.
- types: encode object types as TY_OBJECT_BASE + class_id so the flat node-type array still works; ty_is_object/ty_object/ty_object_class.
- compiler: ClassInfo table (name, ivars + types); Scope.class_id tags instance methods; class/ivar/method-in-class lookups; comp_method_index now matches only free functions.
- analyze: walk tags ClassNode bodies, instance methods carry class_id, ivars are registered and their types inferred from assignments (fixpoint pass); @ivar reads typed from the class table; Class.new -> object type; obj.method dispatch via the receiver's class; initialize/instance-method params inferred from call sites.
- codegen: emit a struct per class (typed iv_ fields) + sp_new (zero-init, call initialize by pointer); instance methods take sp *self by pointer (reference semantics); @ivar read/write/ op-write via self->iv_; Class.new and obj.method(&recv, ...) calls (lvalue receiver or temp); puts/p of objects via user to_s.
A Point class (initialize, reader, computed method, to_s, interpolation)
matches CRuby. make test: 13 pass (up from 8).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- d195b324e4e1
- Parent
- 107d0bd9a0ad
src/analyze.cmodifiedView file
@@ -44,6 +44,24 @@
4444
TyKind rt = recv >= 0 ? infer_type(c, recv) : TY_UNKNOWN;4545
TyKind a0 = argc >= 1 ? infer_type(c, argv[0]) : TY_UNKNOWN;4646
47+
/* Class.new(...) -> an instance of that class */48+
if (recv >= 0 && !strcmp(name, "new")) {49+
const char *rty = nt_type(nt, recv);50+
if (rty && !strcmp(rty, "ConstantReadNode")) {51+
int ci = comp_class_index(c, nt_str(nt, recv, "name"));52+
if (ci >= 0) return ty_object(ci);53+
}54+
}55+
56+
/* obj.method(...) -> the method's return type */57+
if (recv >= 0 && ty_is_object(rt)) {58+
int cid = ty_object_class(rt);59+
int mi = comp_method_in_class(c, cid, name);60+
if (mi >= 0) return c->scopes[mi].ret;61+
/* built-in object queries */62+
if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) return TY_STRING;63+
}64+
4765
/* user-defined method call (no receiver) */4866
if (recv < 0) {4967
int mi = comp_method_index(c, name);@@ -161,6 +179,14 @@
161179
LocalVar *lv = nm ? scope_local(s, nm) : NULL;162180
return lv ? lv->type : TY_UNKNOWN;163181
}182+
if (!strcmp(ty, "InstanceVariableReadNode")) {183+
const char *nm = nt_str(nt, id, "name");184+
Scope *s = comp_scope_of(c, id);185+
if (s->class_id < 0) return TY_UNKNOWN;186+
ClassInfo *ci = &c->classes[s->class_id];187+
int iv = nm ? comp_ivar_index(ci, nm) : -1;188+
return iv >= 0 ? ci->ivar_types[iv] : TY_UNKNOWN;189+
}164190
if (!strcmp(ty, "ParenthesesNode")) {165191
int body = nt_ref(nt, id, "body");166192
if (body < 0) return TY_NIL;@@ -227,17 +253,28 @@
227253
lv->is_param = 1;228254
}229255
230-
static void walk_scope(Compiler *c, int id, int scope_idx) {256+
static void walk_scope(Compiler *c, int id, int scope_idx, int class_id) {231257
if (id < 0 || id >= c->nt->count) return;232258
c->nscope[id] = scope_idx;233259
const char *ty = nt_type(c->nt, id);234260
int child = scope_idx;261+
int child_class = class_id;235262
236-
if (ty && !strcmp(ty, "DefNode")) {263+
if (ty && !strcmp(ty, "ClassNode")) {264+
int cp = nt_ref(c->nt, id, "constant_path");265+
const char *cname = cp >= 0 ? nt_str(c->nt, cp, "name") : NULL;266+
if (cname && comp_class_index(c, cname) < 0) {267+
comp_class_new(c, cname, id);268+
child_class = c->nclasses - 1;269+
} else if (cname) {270+
child_class = comp_class_index(c, cname); /* reopened class */271+
}272+
} else if (ty && !strcmp(ty, "DefNode")) {237273
const char *name = nt_str(c->nt, id, "name");238274
Scope *s = comp_scope_new(c, name, id);239275
int new_idx = c->nscopes - 1;240276
s->body = nt_ref(c->nt, id, "body");277+
s->class_id = class_id; /* instance method of the enclosing class */241278
int pn = nt_ref(c->nt, id, "parameters");242279
if (pn >= 0) {243280
int rn = 0;@@ -253,14 +290,14 @@
253290
int nr = nt_num_refs(c->nt, id);254291
for (int i = 0; i < nr; i++) {255292
int r = nt_ref_at(c->nt, id, i);256-
if (r >= 0) walk_scope(c, r, child);293+
if (r >= 0) walk_scope(c, r, child, child_class);257294
}258295
int na = nt_num_arrs(c->nt, id);259296
for (int i = 0; i < na; i++) {260297
int n = 0;261298
const int *ids = nt_arr_at(c->nt, id, i, &n);262299
for (int j = 0; j < n; j++)263-
if (ids[j] >= 0) walk_scope(c, ids[j], child);300+
if (ids[j] >= 0) walk_scope(c, ids[j], child, child_class);264301
}265302
}266303
@@ -276,9 +313,42 @@
276313
const char *nm = nt_str(nt, id, "name");277314
if (nm) scope_local_intern(comp_scope_of(c, id), nm);278315
}316+
if (!strcmp(ty, "InstanceVariableWriteNode") ||317+
!strcmp(ty, "InstanceVariableReadNode") ||318+
!strcmp(ty, "InstanceVariableOperatorWriteNode")) {319+
const char *nm = nt_str(nt, id, "name");320+
Scope *s = comp_scope_of(c, id);321+
if (nm && s->class_id >= 0) comp_ivar_intern(&c->classes[s->class_id], nm);322+
}279323
}280324
}281325
326+
/* @ivar types from their assignments across the class's methods. */327+
static int infer_ivar_types(Compiler *c) {328+
const NodeTable *nt = c->nt;329+
int changed = 0;330+
for (int id = 0; id < nt->count; id++) {331+
const char *ty = nt_type(nt, id);332+
if (!ty) continue;333+
const char *nm = NULL;334+
TyKind vt = TY_UNKNOWN;335+
if (!strcmp(ty, "InstanceVariableWriteNode")) {336+
nm = nt_str(nt, id, "name");337+
vt = infer_type(c, nt_ref(nt, id, "value"));338+
} else {339+
continue;340+
}341+
Scope *s = comp_scope_of(c, id);342+
if (!nm || s->class_id < 0) continue;343+
ClassInfo *ci = &c->classes[s->class_id];344+
int iv = comp_ivar_index(ci, nm);345+
if (iv < 0) continue;346+
TyKind merged = ty_unify(ci->ivar_types[iv], vt);347+
if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; }348+
}349+
return changed;350+
}351+
282352
/* ---- fixpoint passes ---- */283353
284354
static int infer_write_types(Compiler *c) {@@ -314,29 +384,53 @@
314384
return changed;315385
}316386
387+
/* Unify a call's argument types into method scope `mi`'s parameters. */388+
static int bind_call_params(Compiler *c, int call_id, int mi) {389+
if (mi < 0) return 0;390+
const NodeTable *nt = c->nt;391+
Scope *m = &c->scopes[mi];392+
int args = nt_ref(nt, call_id, "arguments");393+
int argc = 0;394+
const int *argv = NULL;395+
if (args >= 0) argv = nt_arr(nt, args, "arguments", &argc);396+
int n = argc < m->nparams ? argc : m->nparams;397+
int changed = 0;398+
for (int k = 0; k < n; k++) {399+
TyKind at = infer_type(c, argv[k]);400+
LocalVar *p = scope_local(m, m->pnames[k]);401+
if (!p) continue;402+
TyKind merged = ty_unify(p->type, at);403+
if (merged != p->type) { p->type = merged; changed = 1; }404+
}405+
return changed;406+
}407+
317408
static int infer_param_types(Compiler *c) {318409
const NodeTable *nt = c->nt;319410
int changed = 0;320411
for (int id = 0; id < nt->count; id++) {321412
const char *ty = nt_type(nt, id);322413
if (!ty || strcmp(ty, "CallNode")) continue;323-
if (nt_ref(nt, id, "receiver") >= 0) continue;324414
const char *name = nt_str(nt, id, "name");325-
int mi = comp_method_index(c, name);326-
if (mi < 0) continue;327-
Scope *m = &c->scopes[mi];328-
int args = nt_ref(nt, id, "arguments");329-
int argc = 0;330-
const int *argv = NULL;331-
if (args >= 0) argv = nt_arr(nt, args, "arguments", &argc);332-
int n = argc < m->nparams ? argc : m->nparams;333-
for (int k = 0; k < n; k++) {334-
TyKind at = infer_type(c, argv[k]);335-
LocalVar *p = scope_local(m, m->pnames[k]);336-
if (!p) continue;337-
TyKind merged = ty_unify(p->type, at);338-
if (merged != p->type) { p->type = merged; changed = 1; }415+
int recv = nt_ref(nt, id, "receiver");416+
417+
if (recv < 0) {418+
changed |= bind_call_params(c, id, comp_method_index(c, name));419+
continue;339420
}421+
/* Class.new -> initialize params */422+
if (!strcmp(name, "new")) {423+
const char *rty = nt_type(nt, recv);424+
if (rty && !strcmp(rty, "ConstantReadNode")) {425+
int ci = comp_class_index(c, nt_str(nt, recv, "name"));426+
if (ci >= 0) changed |= bind_call_params(c, id, comp_method_in_class(c, ci, "initialize"));427+
}428+
continue;429+
}430+
/* obj.method -> instance method params */431+
TyKind rt = infer_type(c, recv);432+
if (ty_is_object(rt))433+
changed |= bind_call_params(c, id, comp_method_in_class(c, ty_object_class(rt), name));340434
}341435
return changed;342436
}@@ -437,7 +531,7 @@
437531
Scope *top = comp_scope_new(c, NULL, -1);438532
top->body = nt_ref(c->nt, c->nt->root_id, "statements");439533
440-
walk_scope(c, c->nt->root_id, 0);534+
walk_scope(c, c->nt->root_id, 0, -1);441535
register_locals(c);442536
443537
/* intern every symbol literal so codegen can emit the id table */@@ -454,6 +548,7 @@
454548
ch |= infer_write_types(c);455549
ch |= infer_param_types(c);456550
ch |= infer_block_params(c);551+
ch |= infer_ivar_types(c);457552
ch |= infer_return_types(c);458553
if (!ch) break;459554
}src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…