Commit fb028522
fb0285220b43Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · +1 added · ~2 modified
Description
Add class inheritance and super
Design: cast-to-parent approach (no vtables):
- Child structs embed parent fields at the same offsets
- Inherited method calls cast child pointer to parent type
- Method resolution walks superclass chain (find_method_inherited)
- super(args) calls parent's method with cast
Implementation:
- Superclass extraction from PM_CONSTANT_READ_NODE
- Ivar inheritance: parent ivars prepended to child struct
- Type propagation: parent ivar/param types flow to children
- emit_initialize_func() for super() calls
- Constructor generation for classes inheriting initialize
Test: examples/bm_inherit.rb — Animal/Dog/Cat hierarchy with inherited methods, overrides, super(), and polymorphic access.
All regressions pass.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- fb0285220b43
- Parent
- 246c0e942b4b
examples/bm_inherit.rbaddedView file
1+
# Test inheritance and super2+
3+
class Animal4+
def initialize(name)5+
@name = name6+
end7+
8+
def name9+
@name10+
end11+
12+
def speak13+
"..."14+
end15+
16+
def describe17+
name18+
end19+
end20+
21+
class Dog < Animal22+
def initialize(name, breed)23+
super(name)24+
@breed = breed25+
end26+
27+
def breed28+
@breed29+
end30+
31+
def speak32+
"Woof!"33+
end34+
end35+
36+
class Cat < Animal37+
def speak38+
"Meow!"39+
end40+
end41+
42+
# Basic inheritance43+
dog = Dog.new("Rex", "Labrador")44+
puts dog.name # Rex (inherited)45+
puts dog.breed # Labrador46+
puts dog.speak # Woof! (overridden)47+
puts dog.describe # Rex (inherited, calls name)48+
49+
cat = Cat.new("Whiskers")50+
puts cat.name # Whiskers (inherited)51+
puts cat.speak # Meow! (overridden)52+
puts cat.describe # Whiskers (inherited)src/codegen.cmodifiedView file
Loading diff…
src/codegen.hmodifiedView file
Loading diff…