Commit 4046dec6

4046dec61061Merged

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

Description

Support struct as a superclass: class X < Struct.new(:a, :b)

register_structs also handles a ClassNode whose superclass is a Struct.new/Data.define call, registering the members onto the class so its own methods (which read members) and construction work. Refactored into is_struct_call / register_struct_members helpers.

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

Details

Commit
4046dec61061
Parent
fbb50b7e6c75

src/analyze.cmodifiedView file

@@ -631,36 +631,58 @@
631631 class named Const whose positional members are attr_accessors. Register
632632 it as a class with one ivar + reader + writer per member. */
633633 static int is_c_ident(const char *s);
634+
635+/* Is CallNode `val` a `Struct.new(...)` / `Data.define(...)`? */
636+static int is_struct_call(Compiler *c, int val) {
637+ const NodeTable *nt = c->nt;
638+ if (val < 0 || !nt_type(nt, val) || strcmp(nt_type(nt, val), "CallNode")) return 0;
639+ const char *mn = nt_str(nt, val, "name");
640+ int vr = nt_ref(nt, val, "receiver");
641+ const char *rn = vr >= 0 && nt_type(nt, vr) && !strcmp(nt_type(nt, vr), "ConstantReadNode")
642+ ? nt_str(nt, vr, "name") : NULL;
643+ return rn && ((!strcmp(rn, "Struct") && mn && !strcmp(mn, "new")) ||
644+ (!strcmp(rn, "Data") && mn && !strcmp(mn, "define")));
645+}
646+
647+/* Register the symbol members of a Struct.new(...) call onto `cls`. */
648+static void register_struct_members(Compiler *c, ClassInfo *cls, int val) {
649+ const NodeTable *nt = c->nt;
650+ cls->is_struct = 1;
651+ int args = nt_ref(nt, val, "arguments");
652+ int an = 0;
653+ const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
654+ for (int a = 0; a < an; a++) {
655+ if (!nt_type(nt, argv[a]) || strcmp(nt_type(nt, argv[a]), "SymbolNode")) continue;
656+ const char *m = nt_str(nt, argv[a], "value");
657+ if (!m) continue;
658+ char ivn[256]; snprintf(ivn, sizeof ivn, "@%s", m);
659+ comp_ivar_intern(cls, ivn);
660+ comp_add_reader(cls, m);
661+ comp_add_writer(cls, m);
662+ }
663+}
664+
634665 static void register_structs(Compiler *c) {
635666 const NodeTable *nt = c->nt;
636667 for (int id = 0; id < nt->count; id++) {
637668 const char *ty = nt_type(nt, id);
638- if (!ty || strcmp(ty, "ConstantWriteNode")) continue;
639- const char *cname = nt_str(nt, id, "name");
640- int val = nt_ref(nt, id, "value");
641- if (!cname || val < 0 || !is_c_ident(cname)) continue;
642- if (!nt_type(nt, val) || strcmp(nt_type(nt, val), "CallNode")) continue;
643- const char *mn = nt_str(nt, val, "name");
644- int vr = nt_ref(nt, val, "receiver");
645- const char *rn = vr >= 0 && nt_type(nt, vr) && !strcmp(nt_type(nt, vr), "ConstantReadNode")
646- ? nt_str(nt, vr, "name") : NULL;
647- int is_struct = rn && ((!strcmp(rn, "Struct") && mn && !strcmp(mn, "new")) ||
648- (!strcmp(rn, "Data") && mn && !strcmp(mn, "define")));
649- if (!is_struct) continue;
650- if (comp_class_index(c, cname) >= 0) continue;
651- ClassInfo *cls = comp_class_new(c, cname, id);
652- cls->is_struct = 1;
653- int args = nt_ref(nt, val, "arguments");
654- int an = 0;
655- const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
656- for (int a = 0; a < an; a++) {
657- if (!nt_type(nt, argv[a]) || strcmp(nt_type(nt, argv[a]), "SymbolNode")) continue;
658- const char *m = nt_str(nt, argv[a], "value");
659- if (!m) continue;
660- char ivn[256]; snprintf(ivn, sizeof ivn, "@%s", m);
661- comp_ivar_intern(cls, ivn);
662- comp_add_reader(cls, m);
663- comp_add_writer(cls, m);
669+ if (!ty) continue;
670+ /* Const = Struct.new(:a, :b) */
671+ if (!strcmp(ty, "ConstantWriteNode")) {
672+ const char *cname = nt_str(nt, id, "name");
673+ int val = nt_ref(nt, id, "value");
674+ if (!cname || !is_c_ident(cname) || !is_struct_call(c, val)) continue;
675+ if (comp_class_index(c, cname) >= 0) continue;
676+ register_struct_members(c, comp_class_new(c, cname, id), val);
677+ }
678+ /* class X < Struct.new(:a, :b); ... end */
679+ else if (!strcmp(ty, "ClassNode")) {
680+ int sup = nt_ref(nt, id, "superclass");
681+ if (!is_struct_call(c, sup)) continue;
682+ int cp = nt_ref(nt, id, "constant_path");
683+ const char *cname = cp >= 0 ? nt_str(nt, cp, "name") : NULL;
684+ int ci = cname ? comp_class_index(c, cname) : -1;
685+ if (ci >= 0) register_struct_members(c, &c->classes[ci], sup);
664686 }
665687 }
666688 }