Commit de903890
de903890e3fbMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: add open class methods, Struct.new inheritance, bare attr_reader calls
Fixes bm_open_class (extension methods on Integer/String/Float via open class), bm_struct_inherit (class X < Struct.new(:x, :y, keyword_init: true) with auto-generated constructor and field accessors).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- de903890e3fb
- Parent
- e0d0bec93a59
spinel_codegen.rbmodifiedView file
@@ -137,6 +137,9 @@
137137
# Method reference tracking: var_name -> method_name138138
@method_ref_vars = "".split(",")139139
@method_ref_names = "".split(",")140+
141+
# Open class tracking for built-in types142+
@open_class_names = "".split(",")140143
end141144
142145
def join_sep(arr, sep)@@ -871,6 +874,10 @@
871874
if @current_class_idx >= 0872875
return "obj_" + @cls_names[@current_class_idx]873876
end877+
st = find_var_type("__self_type")878+
if st != ""879+
return st880+
end874881
return "int"875882
end876883
"int"@@ -1448,6 +1455,28 @@
14481455
return mr14491456
end14501457
1458+
# Check open class methods for receiver type1459+
if recv >= 01460+
rt = infer_type(recv)1461+
oc_prefix = ""1462+
if rt == "int"1463+
oc_prefix = "__oc_Integer_"1464+
end1465+
if rt == "string"1466+
oc_prefix = "__oc_String_"1467+
end1468+
if rt == "float"1469+
oc_prefix = "__oc_Float_"1470+
end1471+
if oc_prefix != ""1472+
oc_name = oc_prefix + mname1473+
oc_mi = find_method_idx(oc_name)1474+
if oc_mi >= 01475+
return @meth_return_types[oc_mi]1476+
end1477+
end1478+
end1479+
14511480
"int"14521481
end14531482
@@ -1729,6 +1758,19 @@
17291758
infer_all_returns17301759
end17311760
1761+
def is_builtin_type_name(name)1762+
if name == "Integer"1763+
return 11764+
end1765+
if name == "String"1766+
return 11767+
end1768+
if name == "Float"1769+
return 11770+
end1771+
01772+
end1773+
17321774
def collect_class(nid)17331775
ci = @cls_names.length17341776
cname = ""@@ -1736,24 +1778,133 @@
17361778
if cp >= 017371779
cname = @nd_name[cp]17381780
end1781+
1782+
# Check for open class on built-in type1783+
if is_builtin_type_name(cname) == 11784+
@open_class_names.push(cname)1785+
# Collect methods as top-level functions with special naming1786+
body = @nd_body[nid]1787+
if body >= 01788+
body_stmts = get_stmts(body)1789+
j = 01790+
while j < body_stmts.length1791+
sid = body_stmts[j]1792+
if @nd_type[sid] == "DefNode"1793+
# Add as top-level method with prefix1794+
mname = @nd_name[sid]1795+
# Store with special naming for lookup1796+
mi = @meth_names.length1797+
@meth_names.push("__oc_" + cname + "_" + mname)1798+
params = collect_params_str(sid)1799+
@meth_param_names.push(params)1800+
@meth_param_types.push(collect_ptypes_str(sid, -1))1801+
@meth_return_types.push("int")1802+
@meth_body_ids.push(@nd_body[sid])1803+
@meth_has_yield.push(0)1804+
@meth_has_defaults.push("0")1805+
end1806+
j = j + 11807+
end1808+
end1809+
return1810+
end1811+
17391812
parent = ""17401813
sp = @nd_superclass[nid]1814+
struct_fields = "".split(",")1815+
struct_kw_init = 017411816
if sp >= 01742-
parent = @nd_name[sp]1817+
if @nd_type[sp] == "CallNode"1818+
if @nd_name[sp] == "new"1819+
sr = @nd_receiver[sp]1820+
if sr >= 01821+
if @nd_type[sr] == "ConstantReadNode"1822+
if @nd_name[sr] == "Struct"1823+
# Struct.new(:x, :y, keyword_init: true)1824+
sargs_id = @nd_arguments[sp]1825+
if sargs_id >= 01826+
sarg_ids = get_args(sargs_id)1827+
sk = 01828+
while sk < sarg_ids.length1829+
if @nd_type[sarg_ids[sk]] == "SymbolNode"1830+
fname = @nd_content[sarg_ids[sk]]1831+
if fname == ""1832+
fname = @nd_name[sarg_ids[sk]]1833+
end1834+
struct_fields.push(fname)1835+
end1836+
if @nd_type[sarg_ids[sk]] == "KeywordHashNode"1837+
struct_kw_init = 11838+
end1839+
sk = sk + 11840+
end1841+
end1842+
end1843+
end1844+
end1845+
end1846+
else1847+
parent = @nd_name[sp]1848+
end17431849
end17441850
1851+
ci = @cls_names.length17451852
@cls_names.push(cname)17461853
@cls_parents.push(parent)1747-
@cls_ivar_names.push("")1748-
@cls_ivar_types.push("")1749-
@cls_meth_names.push("")1750-
@cls_meth_params.push("")1751-
@cls_meth_ptypes.push("")1752-
@cls_meth_returns.push("")1753-
@cls_meth_bodies.push("")1754-
@cls_meth_defaults.push("")1755-
@cls_attr_readers.push("")1756-
@cls_attr_writers.push("")1854+
# Initialize struct fields as ivars1855+
ivar_names = ""1856+
ivar_types = ""1857+
attr_readers = ""1858+
attr_writers = ""1859+
sk = 01860+
while sk < struct_fields.length1861+
if sk > 01862+
ivar_names = ivar_names + ";"1863+
ivar_types = ivar_types + ";"1864+
attr_readers = attr_readers + ";"1865+
attr_writers = attr_writers + ";"1866+
end1867+
ivar_names = ivar_names + "@" + struct_fields[sk]1868+
ivar_types = ivar_types + "int"1869+
attr_readers = attr_readers + struct_fields[sk]1870+
attr_writers = attr_writers + struct_fields[sk]1871+
sk = sk + 11872+
end1873+
@cls_ivar_names.push(ivar_names)1874+
@cls_ivar_types.push(ivar_types)1875+
# Auto-generate initialize method for struct-derived classes1876+
if struct_fields.length > 01877+
init_params = ""1878+
init_ptypes = ""1879+
init_defaults = ""1880+
sk = 01881+
while sk < struct_fields.length1882+
if sk > 01883+
init_params = init_params + ","1884+
init_ptypes = init_ptypes + ","1885+
init_defaults = init_defaults + ","1886+
end1887+
init_params = init_params + struct_fields[sk]1888+
init_ptypes = init_ptypes + "int"1889+
init_defaults = init_defaults + "-1"1890+
sk = sk + 11891+
end1892+
@cls_meth_names.push("initialize")1893+
@cls_meth_params.push(init_params)1894+
@cls_meth_ptypes.push(init_ptypes)1895+
@cls_meth_returns.push("void")1896+
@cls_meth_bodies.push("-2")1897+
@cls_meth_defaults.push(init_defaults)1898+
else1899+
@cls_meth_names.push("")1900+
@cls_meth_params.push("")1901+
@cls_meth_ptypes.push("")1902+
@cls_meth_returns.push("")1903+
@cls_meth_bodies.push("")1904+
@cls_meth_defaults.push("")1905+
end1906+
@cls_attr_readers.push(attr_readers)1907+
@cls_attr_writers.push(attr_writers)17571908
@cls_cmeth_names.push("")17581909
@cls_cmeth_params.push("")17591910
@cls_cmeth_ptypes.push("")@@ -3196,6 +3347,17 @@
31963347
i = 031973348
while i < @meth_names.length31983349
push_scope3350+
# Open class self type3351+
mfn = @meth_names[i]3352+
if mfn.start_with?("__oc_Integer_")3353+
declare_var("__self_type", "int")3354+
end3355+
if mfn.start_with?("__oc_String_")3356+
declare_var("__self_type", "string")3357+
end3358+
if mfn.start_with?("__oc_Float_")3359+
declare_var("__self_type", "float")3360+
end31993361
pnames = @meth_param_names[i].split(",")32003362
ptypes = @meth_param_types[i].split(",")32013363
j = 0@@ -4578,8 +4740,37 @@
45784740
end45794741
45804742
def method_params_decl(mi)4743+
mfullname = @meth_names[mi]45814744
pnames = @meth_param_names[mi].split(",")45824745
ptypes = @meth_param_types[mi].split(",")4746+
# Check for open class method4747+
oc_self = ""4748+
if mfullname.start_with?("__oc_Integer_")4749+
oc_self = "mrb_int self"4750+
end4751+
if mfullname.start_with?("__oc_String_")4752+
oc_self = "const char * self"4753+
end4754+
if mfullname.start_with?("__oc_Float_")4755+
oc_self = "mrb_float self"4756+
end4757+
if oc_self != ""4758+
if pnames.length == 04759+
return oc_self4760+
end4761+
result = oc_self4762+
j = 04763+
while j < pnames.length4764+
result = result + ", "4765+
pt = "int"4766+
if j < ptypes.length4767+
pt = ptypes[j]4768+
end4769+
result = result + c_type(pt) + " lv_" + pnames[j]4770+
j = j + 14771+
end4772+
return result4773+
end45834774
if pnames.length == 045844775
return "void"45854776
end@@ -5091,10 +5282,24 @@
50915282
end50925283
50935284
def emit_toplevel_method(mi)5094-
@current_method_name = @meth_names[mi]5285+
mfullname = @meth_names[mi]5286+
@current_method_name = mfullname50955287
@current_method_return = @meth_return_types[mi]50965288
@indent = 150975289
@in_main = 05290+
5291+
# Check if this is an open class method5292+
oc_type = ""5293+
if mfullname.start_with?("__oc_Integer_")5294+
oc_type = "int"5295+
end5296+
if mfullname.start_with?("__oc_String_")5297+
oc_type = "string"5298+
end5299+
if mfullname.start_with?("__oc_Float_")5300+
oc_type = "float"5301+
end5302+
50985303
if @meth_has_yield[mi] == 150995304
@in_yield_method = 151005305
else@@ -5108,9 +5313,26 @@
51085313
if @meth_has_yield[mi] == 151095314
yp = yield_params_suffix(mi)51105315
end5111-
emit_raw("static " + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(@meth_names[mi]) + "(" + method_params_decl(mi) + yp + ") {")51125316
5113-
push_scope5317+
if oc_type != ""5318+
# Open class method: self is primitive type5319+
rt = @meth_return_types[mi]5320+
self_ctype = c_type(oc_type)5321+
pdecl = ""5322+
if pnames.length > 05323+
pdecl = build_params_decl(pnames, ptypes)5324+
pdecl = self_ctype + " self, " + pdecl5325+
else5326+
pdecl = self_ctype + " self"5327+
end5328+
emit_raw("static " + c_type(rt) + " sp_" + sanitize_name(mfullname) + "(" + pdecl + ") {")5329+
push_scope5330+
declare_var("__self_type", oc_type)5331+
else5332+
emit_raw("static " + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(mfullname) + "(" + method_params_decl(mi) + yp + ") {")5333+
push_scope5334+
end5335+
51145336
j = 051155337
while j < pnames.length51165338
pt = "int"@@ -6121,6 +6343,73 @@
61216343
end61226344
return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")"61236345
end6346+
# Check if we're inside an open class method: implicit self.method6347+
st = find_var_type("__self_type")6348+
if st != ""6349+
# Redirect as self.mname - string methods6350+
if st == "string"6351+
@needs_string_helpers = 16352+
if mname == "upcase"6353+
return "sp_str_upcase(self)"6354+
end6355+
if mname == "downcase"6356+
return "sp_str_downcase(self)"6357+
end6358+
if mname == "length"6359+
return "(mrb_int)strlen(self)"6360+
end6361+
if mname == "strip"6362+
return "sp_str_strip(self)"6363+
end6364+
if mname == "chomp"6365+
return "sp_str_chomp(self)"6366+
end6367+
if mname == "to_i"6368+
return "((mrb_int)atoll(self))"6369+
end6370+
if mname == "split"6371+
@needs_str_array = 16372+
return "sp_str_split(self, " + compile_arg0(nid) + ")"6373+
end6374+
if mname == "include?"6375+
return "sp_str_include(self, " + compile_arg0(nid) + ")"6376+
end6377+
if mname == "gsub"6378+
args_id = @nd_arguments[nid]6379+
arg1 = "\"\""6380+
if args_id >= 06381+
a = get_args(args_id)6382+
if a.length >= 26383+
arg1 = compile_expr(a[1])6384+
end6385+
end6386+
return "sp_str_gsub(self, " + compile_arg0(nid) + ", " + arg1 + ")"6387+
end6388+
end6389+
# int methods6390+
if st == "int"6391+
if mname == "to_s"6392+
@needs_string_helpers = 16393+
return "sp_int_to_s(self)"6394+
end6395+
if mname == "to_f"6396+
return "(mrb_float)(self)"6397+
end6398+
if mname == "abs"6399+
return "((self) < 0 ? -(self) : (self))"6400+
end6401+
end6402+
# float methods6403+
if st == "float"6404+
if mname == "to_i"6405+
return "(mrb_int)(self)"6406+
end6407+
if mname == "to_s"6408+
@needs_string_helpers = 16409+
return "sp_float_to_s(self)"6410+
end6411+
end6412+
end61246413
if @current_class_idx >= 061256414
cidx = cls_find_method(@current_class_idx, mname)61266415
if cidx >= 0@@ -6133,6 +6422,15 @@
61336422
return "sp_" + owner + "_" + sanitize_name(mname) + "(self)"61346423
end61356424
end6425+
# Check attr_readers (bare method call like `x` meaning self.x)6426+
readers = @cls_attr_readers[@current_class_idx].split(";")6427+
rk = 06428+
while rk < readers.length6429+
if readers[rk] == mname6430+
return "self->" + sanitize_ivar(mname)6431+
end6432+
rk = rk + 16433+
end61366434
end61376435
return "0"61386436
end@@ -7079,6 +7377,30 @@
70797377
end70807378
end70817379
7380+
# Open class method dispatch on built-in types7381+
oc_prefix = ""7382+
if recv_type == "int"7383+
oc_prefix = "__oc_Integer_"7384+
end7385+
if recv_type == "string"7386+
oc_prefix = "__oc_String_"7387+
end7388+
if recv_type == "float"7389+
oc_prefix = "__oc_Float_"7390+
end7391+
if oc_prefix != ""7392+
oc_name = oc_prefix + mname7393+
oc_mi = find_method_idx(oc_name)7394+
if oc_mi >= 07395+
ca = compile_call_args(nid)7396+
if ca != ""7397+
return "sp_" + sanitize_name(oc_name) + "(" + rc + ", " + ca + ")"7398+
else7399+
return "sp_" + sanitize_name(oc_name) + "(" + rc + ")"7400+
end7401+
end7402+
end7403+
70827404
# Poly method calls70837405
if recv_type == "poly"70847406
return compile_poly_method_call(nid, rc, mname)