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_name
138138 @method_ref_vars = "".split(",")
139139 @method_ref_names = "".split(",")
140+
141+ # Open class tracking for built-in types
142+ @open_class_names = "".split(",")
140143 end
141144
142145 def join_sep(arr, sep)
@@ -871,6 +874,10 @@
871874 if @current_class_idx >= 0
872875 return "obj_" + @cls_names[@current_class_idx]
873876 end
877+ st = find_var_type("__self_type")
878+ if st != ""
879+ return st
880+ end
874881 return "int"
875882 end
876883 "int"
@@ -1448,6 +1455,28 @@
14481455 return mr
14491456 end
14501457
1458+ # Check open class methods for receiver type
1459+ if recv >= 0
1460+ rt = infer_type(recv)
1461+ oc_prefix = ""
1462+ if rt == "int"
1463+ oc_prefix = "__oc_Integer_"
1464+ end
1465+ if rt == "string"
1466+ oc_prefix = "__oc_String_"
1467+ end
1468+ if rt == "float"
1469+ oc_prefix = "__oc_Float_"
1470+ end
1471+ if oc_prefix != ""
1472+ oc_name = oc_prefix + mname
1473+ oc_mi = find_method_idx(oc_name)
1474+ if oc_mi >= 0
1475+ return @meth_return_types[oc_mi]
1476+ end
1477+ end
1478+ end
1479+
14511480 "int"
14521481 end
14531482
@@ -1729,6 +1758,19 @@
17291758 infer_all_returns
17301759 end
17311760
1761+ def is_builtin_type_name(name)
1762+ if name == "Integer"
1763+ return 1
1764+ end
1765+ if name == "String"
1766+ return 1
1767+ end
1768+ if name == "Float"
1769+ return 1
1770+ end
1771+ 0
1772+ end
1773+
17321774 def collect_class(nid)
17331775 ci = @cls_names.length
17341776 cname = ""
@@ -1736,24 +1778,133 @@
17361778 if cp >= 0
17371779 cname = @nd_name[cp]
17381780 end
1781+
1782+ # Check for open class on built-in type
1783+ if is_builtin_type_name(cname) == 1
1784+ @open_class_names.push(cname)
1785+ # Collect methods as top-level functions with special naming
1786+ body = @nd_body[nid]
1787+ if body >= 0
1788+ body_stmts = get_stmts(body)
1789+ j = 0
1790+ while j < body_stmts.length
1791+ sid = body_stmts[j]
1792+ if @nd_type[sid] == "DefNode"
1793+ # Add as top-level method with prefix
1794+ mname = @nd_name[sid]
1795+ # Store with special naming for lookup
1796+ mi = @meth_names.length
1797+ @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+ end
1806+ j = j + 1
1807+ end
1808+ end
1809+ return
1810+ end
1811+
17391812 parent = ""
17401813 sp = @nd_superclass[nid]
1814+ struct_fields = "".split(",")
1815+ struct_kw_init = 0
17411816 if sp >= 0
1742- parent = @nd_name[sp]
1817+ if @nd_type[sp] == "CallNode"
1818+ if @nd_name[sp] == "new"
1819+ sr = @nd_receiver[sp]
1820+ if sr >= 0
1821+ 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 >= 0
1826+ sarg_ids = get_args(sargs_id)
1827+ sk = 0
1828+ while sk < sarg_ids.length
1829+ 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+ end
1834+ struct_fields.push(fname)
1835+ end
1836+ if @nd_type[sarg_ids[sk]] == "KeywordHashNode"
1837+ struct_kw_init = 1
1838+ end
1839+ sk = sk + 1
1840+ end
1841+ end
1842+ end
1843+ end
1844+ end
1845+ end
1846+ else
1847+ parent = @nd_name[sp]
1848+ end
17431849 end
17441850
1851+ ci = @cls_names.length
17451852 @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 ivars
1855+ ivar_names = ""
1856+ ivar_types = ""
1857+ attr_readers = ""
1858+ attr_writers = ""
1859+ sk = 0
1860+ while sk < struct_fields.length
1861+ if sk > 0
1862+ ivar_names = ivar_names + ";"
1863+ ivar_types = ivar_types + ";"
1864+ attr_readers = attr_readers + ";"
1865+ attr_writers = attr_writers + ";"
1866+ end
1867+ 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 + 1
1872+ end
1873+ @cls_ivar_names.push(ivar_names)
1874+ @cls_ivar_types.push(ivar_types)
1875+ # Auto-generate initialize method for struct-derived classes
1876+ if struct_fields.length > 0
1877+ init_params = ""
1878+ init_ptypes = ""
1879+ init_defaults = ""
1880+ sk = 0
1881+ while sk < struct_fields.length
1882+ if sk > 0
1883+ init_params = init_params + ","
1884+ init_ptypes = init_ptypes + ","
1885+ init_defaults = init_defaults + ","
1886+ end
1887+ init_params = init_params + struct_fields[sk]
1888+ init_ptypes = init_ptypes + "int"
1889+ init_defaults = init_defaults + "-1"
1890+ sk = sk + 1
1891+ end
1892+ @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+ else
1899+ @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+ end
1906+ @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 = 0
31973348 while i < @meth_names.length
31983349 push_scope
3350+ # Open class self type
3351+ mfn = @meth_names[i]
3352+ if mfn.start_with?("__oc_Integer_")
3353+ declare_var("__self_type", "int")
3354+ end
3355+ if mfn.start_with?("__oc_String_")
3356+ declare_var("__self_type", "string")
3357+ end
3358+ if mfn.start_with?("__oc_Float_")
3359+ declare_var("__self_type", "float")
3360+ end
31993361 pnames = @meth_param_names[i].split(",")
32003362 ptypes = @meth_param_types[i].split(",")
32013363 j = 0
@@ -4578,8 +4740,37 @@
45784740 end
45794741
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 method
4747+ oc_self = ""
4748+ if mfullname.start_with?("__oc_Integer_")
4749+ oc_self = "mrb_int self"
4750+ end
4751+ if mfullname.start_with?("__oc_String_")
4752+ oc_self = "const char * self"
4753+ end
4754+ if mfullname.start_with?("__oc_Float_")
4755+ oc_self = "mrb_float self"
4756+ end
4757+ if oc_self != ""
4758+ if pnames.length == 0
4759+ return oc_self
4760+ end
4761+ result = oc_self
4762+ j = 0
4763+ while j < pnames.length
4764+ result = result + ", "
4765+ pt = "int"
4766+ if j < ptypes.length
4767+ pt = ptypes[j]
4768+ end
4769+ result = result + c_type(pt) + " lv_" + pnames[j]
4770+ j = j + 1
4771+ end
4772+ return result
4773+ end
45834774 if pnames.length == 0
45844775 return "void"
45854776 end
@@ -5091,10 +5282,24 @@
50915282 end
50925283
50935284 def emit_toplevel_method(mi)
5094- @current_method_name = @meth_names[mi]
5285+ mfullname = @meth_names[mi]
5286+ @current_method_name = mfullname
50955287 @current_method_return = @meth_return_types[mi]
50965288 @indent = 1
50975289 @in_main = 0
5290+
5291+ # Check if this is an open class method
5292+ oc_type = ""
5293+ if mfullname.start_with?("__oc_Integer_")
5294+ oc_type = "int"
5295+ end
5296+ if mfullname.start_with?("__oc_String_")
5297+ oc_type = "string"
5298+ end
5299+ if mfullname.start_with?("__oc_Float_")
5300+ oc_type = "float"
5301+ end
5302+
50985303 if @meth_has_yield[mi] == 1
50995304 @in_yield_method = 1
51005305 else
@@ -5108,9 +5313,26 @@
51085313 if @meth_has_yield[mi] == 1
51095314 yp = yield_params_suffix(mi)
51105315 end
5111- emit_raw("static " + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(@meth_names[mi]) + "(" + method_params_decl(mi) + yp + ") {")
51125316
5113- push_scope
5317+ if oc_type != ""
5318+ # Open class method: self is primitive type
5319+ rt = @meth_return_types[mi]
5320+ self_ctype = c_type(oc_type)
5321+ pdecl = ""
5322+ if pnames.length > 0
5323+ pdecl = build_params_decl(pnames, ptypes)
5324+ pdecl = self_ctype + " self, " + pdecl
5325+ else
5326+ pdecl = self_ctype + " self"
5327+ end
5328+ emit_raw("static " + c_type(rt) + " sp_" + sanitize_name(mfullname) + "(" + pdecl + ") {")
5329+ push_scope
5330+ declare_var("__self_type", oc_type)
5331+ else
5332+ emit_raw("static " + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(mfullname) + "(" + method_params_decl(mi) + yp + ") {")
5333+ push_scope
5334+ end
5335+
51145336 j = 0
51155337 while j < pnames.length
51165338 pt = "int"
@@ -6121,6 +6343,73 @@
61216343 end
61226344 return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")"
61236345 end
6346+ # Check if we're inside an open class method: implicit self.method
6347+ st = find_var_type("__self_type")
6348+ if st != ""
6349+ # Redirect as self.mname - string methods
6350+ if st == "string"
6351+ @needs_string_helpers = 1
6352+ if mname == "upcase"
6353+ return "sp_str_upcase(self)"
6354+ end
6355+ if mname == "downcase"
6356+ return "sp_str_downcase(self)"
6357+ end
6358+ if mname == "length"
6359+ return "(mrb_int)strlen(self)"
6360+ end
6361+ if mname == "strip"
6362+ return "sp_str_strip(self)"
6363+ end
6364+ if mname == "chomp"
6365+ return "sp_str_chomp(self)"
6366+ end
6367+ if mname == "to_i"
6368+ return "((mrb_int)atoll(self))"
6369+ end
6370+ if mname == "split"
6371+ @needs_str_array = 1
6372+ return "sp_str_split(self, " + compile_arg0(nid) + ")"
6373+ end
6374+ if mname == "include?"
6375+ return "sp_str_include(self, " + compile_arg0(nid) + ")"
6376+ end
6377+ if mname == "gsub"
6378+ args_id = @nd_arguments[nid]
6379+ arg1 = "\"\""
6380+ if args_id >= 0
6381+ a = get_args(args_id)
6382+ if a.length >= 2
6383+ arg1 = compile_expr(a[1])
6384+ end
6385+ end
6386+ return "sp_str_gsub(self, " + compile_arg0(nid) + ", " + arg1 + ")"
6387+ end
6388+ end
6389+ # int methods
6390+ if st == "int"
6391+ if mname == "to_s"
6392+ @needs_string_helpers = 1
6393+ return "sp_int_to_s(self)"
6394+ end
6395+ if mname == "to_f"
6396+ return "(mrb_float)(self)"
6397+ end
6398+ if mname == "abs"
6399+ return "((self) < 0 ? -(self) : (self))"
6400+ end
6401+ end
6402+ # float methods
6403+ if st == "float"
6404+ if mname == "to_i"
6405+ return "(mrb_int)(self)"
6406+ end
6407+ if mname == "to_s"
6408+ @needs_string_helpers = 1
6409+ return "sp_float_to_s(self)"
6410+ end
6411+ end
6412+ end
61246413 if @current_class_idx >= 0
61256414 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 end
61356424 end
6425+ # Check attr_readers (bare method call like `x` meaning self.x)
6426+ readers = @cls_attr_readers[@current_class_idx].split(";")
6427+ rk = 0
6428+ while rk < readers.length
6429+ if readers[rk] == mname
6430+ return "self->" + sanitize_ivar(mname)
6431+ end
6432+ rk = rk + 1
6433+ end
61366434 end
61376435 return "0"
61386436 end
@@ -7079,6 +7377,30 @@
70797377 end
70807378 end
70817379
7380+ # Open class method dispatch on built-in types
7381+ oc_prefix = ""
7382+ if recv_type == "int"
7383+ oc_prefix = "__oc_Integer_"
7384+ end
7385+ if recv_type == "string"
7386+ oc_prefix = "__oc_String_"
7387+ end
7388+ if recv_type == "float"
7389+ oc_prefix = "__oc_Float_"
7390+ end
7391+ if oc_prefix != ""
7392+ oc_name = oc_prefix + mname
7393+ oc_mi = find_method_idx(oc_name)
7394+ if oc_mi >= 0
7395+ ca = compile_call_args(nid)
7396+ if ca != ""
7397+ return "sp_" + sanitize_name(oc_name) + "(" + rc + ", " + ca + ")"
7398+ else
7399+ return "sp_" + sanitize_name(oc_name) + "(" + rc + ")"
7400+ end
7401+ end
7402+ end
7403+
70827404 # Poly method calls
70837405 if recv_type == "poly"
70847406 return compile_poly_method_call(nid, rc, mname)