Commit c379a398
c379a398d18dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 1 file · ~1 modified
Description
Fix inheritance: generate default constructor for child classes
When class B < A and neither has initialize, B now gets a default constructor (sp_B_new) that allocates and returns a pointer. Previously, no constructor was generated, causing link errors.
58/58 tests, 39/39 benchmarks pass.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- c379a398d18d
- Parent
- f996495c1847
src/emit.cmodifiedView file
@@ -96,6 +96,19 @@
9696
if (!init && cls->superclass[0]) {9797
class_info_t *parent = find_class(ctx, cls->superclass);9898
method_info_t *parent_init = parent ? find_method(parent, "initialize") : NULL;99+
if (parent && !parent_init) {100+
/* Parent has no initialize either: generate default constructor */101+
emit_raw(ctx, "static sp_%s *sp_%s_new(void) {\n", cls->name, cls->name);102+
if (ctx->needs_gc) {103+
emit_raw(ctx, " sp_%s *self = (sp_%s *)sp_gc_alloc(sizeof(sp_%s), NULL, NULL);\n",104+
cls->name, cls->name, cls->name);105+
} else {106+
emit_raw(ctx, " sp_%s *self = (sp_%s *)calloc(1, sizeof(sp_%s));\n",107+
cls->name, cls->name, cls->name);108+
}109+
emit_raw(ctx, " return self;\n}\n\n");110+
return;111+
}99112
if (parent && parent_init) {100113
/* Generate: sp_Cat *sp_Cat_new(params...) { alloc; sp_Animal_initialize((sp_Animal*)self, params); return self; } */101114
emit_raw(ctx, "static sp_%s *sp_%s_new(", cls->name, cls->name);