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 types
142142 @open_class_names = "".split(",")
143+
144+ # Module tracking: module_name -> body node id
145+ @module_names = "".split(",")
146+ @module_body_ids = []
143147 end
144148
145149 def join_sep(arr, sep)
@@ -1728,6 +1732,16 @@
17281732 end
17291733 stmts = get_body_stmts(root)
17301734
1735+ # Pass 0: modules (must come before classes for include)
1736+ i = 0
1737+ while i < stmts.length
1738+ sid = stmts[i]
1739+ if @nd_type[sid] == "ModuleNode"
1740+ collect_module(sid)
1741+ end
1742+ i = i + 1
1743+ end
1744+
17311745 # Pass 1: classes
17321746 i = 0
17331747 while i < stmts.length
@@ -1738,7 +1752,7 @@
17381752 i = i + 1
17391753 end
17401754
1741- # Pass 2: top-level methods, constants, and modules
1755+ # Pass 2: top-level methods, constants (modules already collected in Pass 0)
17421756 i = 0
17431757 while i < stmts.length
17441758 sid = stmts[i]
@@ -1748,9 +1762,6 @@
17481762 if @nd_type[sid] == "ConstantWriteNode"
17491763 collect_constant(sid)
17501764 end
1751- if @nd_type[sid] == "ModuleNode"
1752- collect_module(sid)
1753- end
17541765 i = i + 1
17551766 end
17561767
@@ -1919,6 +1930,7 @@
19191930 return
19201931 end
19211932 body_stmts = get_stmts(body)
1933+ # First pass: collect all class methods and attrs
19221934 j = 0
19231935 while j < body_stmts.length
19241936 sid = body_stmts[j]
@@ -1926,15 +1938,69 @@
19261938 collect_class_method(ci, sid)
19271939 end
19281940 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+ end
1946+ end
19301947 end
19311948 j = j + 1
19321949 end
1950+ # Second pass: handle includes (after all own methods are known)
1951+ j = 0
1952+ while j < body_stmts.length
1953+ 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 >= 0
1958+ inc_ids = get_args(inc_args)
1959+ ik = 0
1960+ while ik < inc_ids.length
1961+ 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+ end
1965+ ik = ik + 1
1966+ end
1967+ end
1968+ end
1969+ end
1970+ j = j + 1
1971+ end
19331972
19341973 # Collect ivars
19351974 collect_ivars(ci)
19361975 end
19371976
1977+ def collect_module_methods_into_class(ci, mod_name)
1978+ # Find the module and add its methods to the class
1979+ mi = 0
1980+ while mi < @module_names.length
1981+ if @module_names[mi] == mod_name
1982+ mbody = @module_body_ids[mi]
1983+ if mbody >= 0
1984+ mstmts = get_stmts(mbody)
1985+ mk = 0
1986+ while mk < mstmts.length
1987+ sid = mstmts[mk]
1988+ if @nd_type[sid] == "DefNode"
1989+ mname = @nd_name[sid]
1990+ # Only add if class doesn't already have this method
1991+ existing = cls_find_method_direct(ci, mname)
1992+ if existing < 0
1993+ collect_class_method(ci, sid)
1994+ end
1995+ end
1996+ mk = mk + 1
1997+ end
1998+ end
1999+ end
2000+ mi = mi + 1
2001+ end
2002+ end
2003+
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 end
25092575 body = @nd_body[nid]
2576+ # Store module info for include
2577+ @module_names.push(mname)
2578+ @module_body_ids.push(body)
25102579 if body < 0
25112580 return
25122581 end
@@ -2525,9 +2594,11 @@
25252594 @const_types.push(ct)
25262595 @const_expr_ids.push(expr_id)
25272596 end
2528- if @nd_type[sid] == "DefNode"
2529- collect_toplevel_method(sid)
2530- end
2597+ # Don't collect module DefNodes as top-level methods -
2598+ # they will be included into classes via collect_module_methods_into_class
2599+ # if @nd_type[sid] == "DefNode"
2600+ # collect_toplevel_method(sid)
2601+ # end
25312602 j = j + 1
25322603 end
25332604 end