Commit b9b36327

b9b363271fc1Merged

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

Description

Add M11: inheritance, super, and virtual dispatch

  • compiler: ClassInfo.parent; comp_method_in_chain / comp_reader_in_chain / comp_writer_in_chain walk the superclass chain.
  • analyze: resolve_parents (from ClassNode superclass); inherit_members merges parent ivar/reader/writer NAMES into subclasses so struct layout is [parent ivars..., own ivars...] (cast-compatible); infer_inherited_ivars propagates ivar types parent->child in the fixpoint; SuperNode infers the parent method's return type; implicit-self calls (no receiver inside an instance method) resolve against the current class chain; method/super params bound from call sites across the chain.
  • codegen: every object struct carries a cls_id tag (set in new); emit_dispatch resolves a method over the receiver's subtree -- a static cast call when there's one implementation, else a cls_id switch (statement-expression) for true virtual dispatch. Inherited methods cast self to the defining class; super -> sp_((sp*)self); new calls an inherited initialize; implicit-self reader -> self->iv_x.

A 3-level hierarchy (Animal/Dog/Puppy) with overrides, super (in a method and in initialize), inherited methods/ivars, and a base method calling an overridden method on self (virtual) all match CRuby. make test: 16.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
b9b363271fc1
Parent
d6291adffe13

src/analyze.cmodifiedView file

@@ -54,12 +54,12 @@
5454 }
5555 }
5656
57- /* obj.method(...) -> the method's return type */
57+ /* obj.method(...) -> the method's return type (walks the superclass chain) */
5858 if (recv >= 0 && ty_is_object(rt)) {
5959 int cid = ty_object_class(rt);
6060 ClassInfo *cls = &c->classes[cid];
6161 /* attr reader */
62- if (comp_is_reader(cls, name)) {
62+ if (comp_reader_in_chain(c, cid, name, NULL)) {
6363 char ivn[256];
6464 snprintf(ivn, sizeof ivn, "@%s", name);
6565 int iv = comp_ivar_index(cls, ivn);
@@ -71,16 +71,31 @@
7171 char base[256];
7272 if (ln - 1 < sizeof base) {
7373 memcpy(base, name, ln - 1); base[ln - 1] = '\0';
74- if (comp_is_writer(cls, base) && argc >= 1) return infer_type(c, argv[0]);
74+ if (comp_writer_in_chain(c, cid, base, NULL) && argc >= 1) return infer_type(c, argv[0]);
7575 }
7676 }
77- int mi = comp_method_in_class(c, cid, name);
77+ int mi = comp_method_in_chain(c, cid, name, NULL);
7878 if (mi >= 0) return c->scopes[mi].ret;
7979 if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) return TY_STRING;
8080 }
8181
82- /* user-defined method call (no receiver) */
82+ /* implicit-self call inside an instance method */
8383 if (recv < 0) {
84+ Scope *self = comp_scope_of(c, id);
85+ if (self->class_id >= 0) {
86+ if (comp_reader_in_chain(c, self->class_id, name, NULL)) {
87+ char ivn[256];
88+ snprintf(ivn, sizeof ivn, "@%s", name);
89+ int iv = comp_ivar_index(&c->classes[self->class_id], ivn);
90+ if (iv >= 0) return c->classes[self->class_id].ivar_types[iv];
91+ }
92+ int mi = comp_method_in_chain(c, self->class_id, name, NULL);
93+ if (mi >= 0) return c->scopes[mi].ret;
94+ }
95+ }
96+
97+ /* user-defined free-function call (no receiver) */
98+ if (recv < 0) {
8499 int mi = comp_method_index(c, name);
85100 if (mi >= 0) return c->scopes[mi].ret;
86101 }
@@ -276,6 +291,14 @@
276291 }
277292 return ty_hash_of(kt, vt);
278293 }
294+ if (!strcmp(ty, "SuperNode") || !strcmp(ty, "ForwardingSuperNode")) {
295+ Scope *s = comp_scope_of(c, id);
296+ if (s->class_id < 0 || !s->name) return TY_UNKNOWN;
297+ int p = c->classes[s->class_id].parent;
298+ if (p < 0) return TY_UNKNOWN;
299+ int mi = comp_method_in_chain(c, p, s->name, NULL);
300+ return mi >= 0 ? c->scopes[mi].ret : TY_UNKNOWN;
301+ }
279302 if (!strcmp(ty, "CallNode")) return infer_call(c, id);
280303
281304 return TY_UNKNOWN;
@@ -404,6 +427,65 @@
404427 }
405428 }
406429
430+/* Resolve each class's superclass index from its ClassNode. */
431+static void resolve_parents(Compiler *c) {
432+ const NodeTable *nt = c->nt;
433+ for (int i = 0; i < c->nclasses; i++) {
434+ int sc = nt_ref(nt, c->classes[i].def_node, "superclass");
435+ if (sc < 0) continue;
436+ const char *sty = nt_type(nt, sc);
437+ if (sty && !strcmp(sty, "ConstantReadNode")) {
438+ int p = comp_class_index(c, nt_str(nt, sc, "name"));
439+ if (p >= 0 && p != i) c->classes[i].parent = p;
440+ }
441+ }
442+}
443+
444+/* Merge inherited ivar/reader/writer NAMES into subclasses so the struct
445+ layout is [parent ivars..., own ivars...] (cast-compatible). Types are
446+ propagated later in the fixpoint. Parent-first order. */
447+static void inherit_members(Compiler *c) {
448+ for (int i = 0; i < c->nclasses; i++) {
449+ ClassInfo *ci = &c->classes[i];
450+ int p = ci->parent;
451+ if (p < 0 || p >= i) continue; /* parent defined earlier; already merged */
452+ ClassInfo *pc = &c->classes[p];
453+
454+ char **old = ci->ivars; TyKind *oldt = ci->ivar_types; int oldn = ci->nivars;
455+ ci->ivars = NULL; ci->ivar_types = NULL; ci->nivars = ci->civars = 0;
456+ for (int k = 0; k < pc->nivars; k++) {
457+ int idx = comp_ivar_intern(ci, pc->ivars[k]);
458+ ci->ivar_types[idx] = pc->ivar_types[k];
459+ }
460+ for (int k = 0; k < oldn; k++) {
461+ int idx = comp_ivar_intern(ci, old[k]);
462+ ci->ivar_types[idx] = ty_unify(ci->ivar_types[idx], oldt[k]);
463+ free(old[k]);
464+ }
465+ free(old); free(oldt);
466+
467+ for (int k = 0; k < pc->nreaders; k++) comp_add_reader(ci, pc->readers[k]);
468+ for (int k = 0; k < pc->nwriters; k++) comp_add_writer(ci, pc->writers[k]);
469+ }
470+}
471+
472+/* Propagate inherited @ivar types parent -> child. */
473+static int infer_inherited_ivars(Compiler *c) {
474+ int changed = 0;
475+ for (int i = 0; i < c->nclasses; i++) {
476+ ClassInfo *ci = &c->classes[i];
477+ if (ci->parent < 0) continue;
478+ ClassInfo *pc = &c->classes[ci->parent];
479+ for (int k = 0; k < pc->nivars; k++) {
480+ int idx = comp_ivar_index(ci, pc->ivars[k]);
481+ if (idx < 0) continue;
482+ TyKind merged = ty_unify(ci->ivar_types[idx], pc->ivar_types[k]);
483+ if (merged != ci->ivar_types[idx]) { ci->ivar_types[idx] = merged; changed = 1; }
484+ }
485+ }
486+ return changed;
487+}
488+
407489 /* @ivar types from their assignments across the class's methods. */
408490 static int infer_ivar_types(Compiler *c) {
409491 const NodeTable *nt = c->nt;
@@ -530,12 +612,26 @@
530612 int changed = 0;
531613 for (int id = 0; id < nt->count; id++) {
532614 const char *ty = nt_type(nt, id);
533- if (!ty || strcmp(ty, "CallNode")) continue;
615+ if (!ty) continue;
616+ if (!strcmp(ty, "SuperNode")) {
617+ Scope *s = comp_scope_of(c, id);
618+ if (s->class_id < 0 || !s->name) continue;
619+ int p = c->classes[s->class_id].parent;
620+ if (p < 0) continue;
621+ changed |= bind_call_params(c, id, comp_method_in_chain(c, p, s->name, NULL));
622+ continue;
623+ }
624+ if (strcmp(ty, "CallNode")) continue;
534625 const char *name = nt_str(nt, id, "name");
535626 int recv = nt_ref(nt, id, "receiver");
536627
537628 if (recv < 0) {
538- changed |= bind_call_params(c, id, comp_method_index(c, name));
629+ int mi = comp_method_index(c, name);
630+ if (mi < 0) {
631+ Scope *self = comp_scope_of(c, id);
632+ if (self->class_id >= 0) mi = comp_method_in_chain(c, self->class_id, name, NULL);
633+ }
634+ changed |= bind_call_params(c, id, mi);
539635 continue;
540636 }
541637 /* Class.new -> initialize params */
@@ -654,6 +750,8 @@
654750 walk_scope(c, c->nt->root_id, 0, -1);
655751 register_locals(c);
656752 register_attrs(c);
753+ resolve_parents(c);
754+ inherit_members(c);
657755
658756 /* intern every symbol literal so codegen can emit the id table */
659757 for (int id = 0; id < c->nt->count; id++) {
@@ -670,6 +768,7 @@
670768 ch |= infer_param_types(c);
671769 ch |= infer_block_params(c);
672770 ch |= infer_ivar_types(c);
771+ ch |= infer_inherited_ivars(c);
673772 ch |= infer_return_types(c);
674773 if (!ch) break;
675774 }