Commit 0a3435fc
0a3435fcfc55Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: add module include (mixin) support for class methods
Implements include for mixing module methods into classes. Modules are collected before classes, and include copies module methods that the class doesn't already define.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 0a3435fcfc55
- Parent
- de903890e3fb
spinel_codegen.rbmodifiedView file
@@ -140,6 +140,10 @@
140140
141141
# Open class tracking for built-in types142142
@open_class_names = "".split(",")143+
144+
# Module tracking: module_name -> body node id145+
@module_names = "".split(",")146+
@module_body_ids = []143147
end144148
145149
def join_sep(arr, sep)@@ -1728,6 +1732,16 @@
17281732
end17291733
stmts = get_body_stmts(root)17301734
1735+
# Pass 0: modules (must come before classes for include)1736+
i = 01737+
while i < stmts.length1738+
sid = stmts[i]1739+
if @nd_type[sid] == "ModuleNode"1740+
collect_module(sid)1741+
end1742+
i = i + 11743+
end1744+
17311745
# Pass 1: classes17321746
i = 017331747
while i < stmts.length@@ -1738,7 +1752,7 @@
17381752
i = i + 117391753
end17401754
1741-
# Pass 2: top-level methods, constants, and modules1755+
# Pass 2: top-level methods, constants (modules already collected in Pass 0)17421756
i = 017431757
while i < stmts.length17441758
sid = stmts[i]@@ -1748,9 +1762,6 @@
17481762
if @nd_type[sid] == "ConstantWriteNode"17491763
collect_constant(sid)17501764
end1751-
if @nd_type[sid] == "ModuleNode"1752-
collect_module(sid)1753-
end17541765
i = i + 117551766
end17561767
@@ -1919,6 +1930,7 @@
19191930
return19201931
end19211932
body_stmts = get_stmts(body)1933+
# First pass: collect all class methods and attrs19221934
j = 019231935
while j < body_stmts.length19241936
sid = body_stmts[j]@@ -1926,15 +1938,69 @@
19261938
collect_class_method(ci, sid)19271939
end19281940
if @nd_type[sid] == "CallNode"1929-
collect_attr_call(ci, sid)1941+
cn = @nd_name[sid]1942+
if cn != "include"1943+
if cn != "private"1944+
collect_attr_call(ci, sid)1945+
end1946+
end19301947
end19311948
j = j + 119321949
end1950+
# Second pass: handle includes (after all own methods are known)1951+
j = 01952+
while j < body_stmts.length1953+
sid = body_stmts[j]1954+
if @nd_type[sid] == "CallNode"1955+
if @nd_name[sid] == "include"1956+
inc_args = @nd_arguments[sid]1957+
if inc_args >= 01958+
inc_ids = get_args(inc_args)1959+
ik = 01960+
while ik < inc_ids.length1961+
if @nd_type[inc_ids[ik]] == "ConstantReadNode"1962+
mod_name = @nd_name[inc_ids[ik]]1963+
collect_module_methods_into_class(ci, mod_name)1964+
end1965+
ik = ik + 11966+
end1967+
end1968+
end1969+
end1970+
j = j + 11971+
end19331972
19341973
# Collect ivars19351974
collect_ivars(ci)19361975
end19371976
1977+
def collect_module_methods_into_class(ci, mod_name)1978+
# Find the module and add its methods to the class1979+
mi = 01980+
while mi < @module_names.length1981+
if @module_names[mi] == mod_name1982+
mbody = @module_body_ids[mi]1983+
if mbody >= 01984+
mstmts = get_stmts(mbody)1985+
mk = 01986+
while mk < mstmts.length1987+
sid = mstmts[mk]1988+
if @nd_type[sid] == "DefNode"1989+
mname = @nd_name[sid]1990+
# Only add if class doesn't already have this method1991+
existing = cls_find_method_direct(ci, mname)1992+
if existing < 01993+
collect_class_method(ci, sid)1994+
end1995+
end1996+
mk = mk + 11997+
end1998+
end1999+
end2000+
mi = mi + 12001+
end2002+
end2003+
19382004
def collect_class_method(ci, nid)19392005
mname = @nd_name[nid]19402006
body_id = @nd_body[nid]@@ -2507,6 +2573,9 @@
25072573
mname = @nd_name[cp]25082574
end25092575
body = @nd_body[nid]2576+
# Store module info for include2577+
@module_names.push(mname)2578+
@module_body_ids.push(body)25102579
if body < 025112580
return25122581
end@@ -2525,9 +2594,11 @@
25252594
@const_types.push(ct)25262595
@const_expr_ids.push(expr_id)25272596
end2528-
if @nd_type[sid] == "DefNode"2529-
collect_toplevel_method(sid)2530-
end2597+
# Don't collect module DefNodes as top-level methods -2598+
# they will be included into classes via collect_module_methods_into_class2599+
# if @nd_type[sid] == "DefNode"2600+
# collect_toplevel_method(sid)2601+
# end25312602
j = j + 125322603
end25332604
end