Commit 1e909c64

1e909c640e1aMerged

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

Description

Forward-declare all class typedefs before struct bodies

A class that embeds a pointer to a class defined later in the file failed to compile ("unknown type name sp_X"). All struct typedefs are now forward-declared before any struct body is emitted, so out-of-order class references resolve.

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

Details

Commit
1e909c640e1a
Parent
59aed21e61f0

src/codegen.cmodifiedView file

@@ -5205,7 +5205,8 @@
52055205
52065206 /* Emit the struct + the constructor (sp_<Class>_new) for one class. */
52075207 static void emit_class_struct(Compiler *c, ClassInfo *ci, Buf *b) {
5208- buf_printf(b, "typedef struct sp_%s_s sp_%s;\n", ci->name, ci->name);
5208+ /* the typedef is forward-declared for every class first (see codegen_program)
5209+ so a class can embed a pointer to a class defined later in the file */
52095210 buf_printf(b, "struct sp_%s_s {\n", ci->name);
52105211 buf_puts(b, " mrb_int cls_id;\n"); /* runtime class tag for virtual dispatch */
52115212 for (int i = 0; i < ci->nivars; i++) {
@@ -5382,7 +5383,10 @@
53825383 }
53835384 buf_puts(&b, "static const char *sp_class_to_s(sp_Class c){(void)c;return \"\";}\n\n\n");
53845385
5385- /* class structs + GC scan functions */
5386+ /* class structs + GC scan functions. Forward-declare every typedef first so
5387+ a class struct may embed a pointer to a class defined later. */
5388+ for (int i = 0; i < c->nclasses; i++)
5389+ buf_printf(&b, "typedef struct sp_%s_s sp_%s;\n", c->classes[i].name, c->classes[i].name);
53865390 for (int i = 0; i < c->nclasses; i++) emit_class_struct(c, &c->classes[i], &b);
53875391 for (int i = 0; i < c->nclasses; i++) emit_class_scan(c, &c->classes[i], &b);
53885392 if (c->nclasses > 0) buf_puts(&b, "\n");