Commit 74c18902
74c1890208a9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: fix all Phase 1-4 tests (9/9 passing)
- Fix constructor inheritance for classes without own initialize
- Fix ivar type inference from constructor call sites
- Fix method return type inference for CaseNode and bare method calls
- Fix block parameter type inference for hash.each
- Fix (range).to_a with parenthesized ranges
- Add reject/reduce/inject as expression context
- Add default argument support for top-level method calls
- Fix incremental return type saving for intra-class method calls
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 74c1890208a9
- Parent
- 9de90f639f53
spinel_codegen.rbmodifiedView file
@@ -772,6 +772,23 @@
772772
end773773
return "void"774774
end775+
if t == "CaseNode"776+
# Infer from first when branch777+
conds = parse_id_list(@nd_conditions[nid])778+
if conds.length > 0779+
wid = conds[0]780+
if @nd_type[wid] == "WhenNode"781+
wbody = @nd_body[wid]782+
if wbody >= 0783+
ws = get_stmts(wbody)784+
if ws.length > 0785+
return infer_type(ws[ws.length - 1])786+
end787+
end788+
end789+
end790+
return "int"791+
end775792
if t == "AndNode"776793
return "bool"777794
end@@ -997,6 +1014,12 @@
9971014
end9981015
return "int_array"9991016
end1017+
if mname == "reject"1018+
if recv >= 01019+
return infer_type(recv)1020+
end1021+
return "int_array"1022+
end10001023
if mname == "[]"10011024
if recv >= 010021025
rt = infer_type(recv)@@ -1040,6 +1063,18 @@
10401063
end10411064
return "string"10421065
end1066+
if mname == "to_a"1067+
if recv >= 01068+
rt = infer_type(recv)1069+
if rt == "range"1070+
return "int_array"1071+
end1072+
if rt == "int_array"1073+
return "int_array"1074+
end1075+
end1076+
return "int_array"1077+
end10431078
10441079
# Method call on object10451080
if recv >= 0@@ -1738,7 +1773,209 @@
17381773
end17391774
17401775
# ---- Return type inference ----1776+
def infer_constructor_types1777+
# Scan AST for ClassName.new(args) calls and infer param types1778+
scan_new_calls(@root_id)1779+
end1780+
1781+
def scan_new_calls(nid)1782+
if nid < 01783+
return1784+
end1785+
if @nd_type[nid] == "CallNode"1786+
# Also infer top-level method param types from call sites1787+
mname = @nd_name[nid]1788+
if @nd_receiver[nid] < 01789+
mi = find_method_idx(mname)1790+
if mi >= 01791+
args_id = @nd_arguments[nid]1792+
if args_id >= 01793+
arg_ids = get_args(args_id)1794+
ptypes = @meth_param_types[mi].split(",")1795+
k = 01796+
while k < arg_ids.length1797+
at = infer_type(arg_ids[k])1798+
if k < ptypes.length1799+
if ptypes[k] == "int"1800+
if at != "int"1801+
ptypes[k] = at1802+
end1803+
end1804+
end1805+
k = k + 11806+
end1807+
@meth_param_types[mi] = join_sep(ptypes, ",")1808+
end1809+
end1810+
end1811+
if @nd_name[nid] == "new"1812+
recv = @nd_receiver[nid]1813+
if recv >= 01814+
if @nd_type[recv] == "ConstantReadNode"1815+
cname = @nd_name[recv]1816+
ci = find_class_idx(cname)1817+
if ci >= 01818+
init_ci = find_init_class(ci)1819+
if init_ci >= 01820+
init_idx = cls_find_method_direct(init_ci, "initialize")1821+
if init_idx >= 01822+
args_id = @nd_arguments[nid]1823+
if args_id >= 01824+
arg_ids = get_args(args_id)1825+
all_ptypes = @cls_meth_ptypes[init_ci].split("|")1826+
if init_idx < all_ptypes.length1827+
ptypes = all_ptypes[init_idx].split(",")1828+
k = 01829+
while k < arg_ids.length1830+
at = infer_type(arg_ids[k])1831+
if k < ptypes.length1832+
if ptypes[k] == "int"1833+
if at != "int"1834+
ptypes[k] = at1835+
end1836+
end1837+
end1838+
k = k + 11839+
end1840+
all_ptypes[init_idx] = join_sep(ptypes, ",")1841+
@cls_meth_ptypes[init_ci] = join_sep(all_ptypes, "|")1842+
end1843+
end1844+
end1845+
end1846+
end1847+
end1848+
end1849+
end1850+
end1851+
# Recurse into children1852+
if @nd_body[nid] >= 01853+
scan_new_calls(@nd_body[nid])1854+
end1855+
stmts = parse_id_list(@nd_stmts[nid])1856+
k = 01857+
while k < stmts.length1858+
scan_new_calls(stmts[k])1859+
k = k + 11860+
end1861+
if @nd_receiver[nid] >= 01862+
scan_new_calls(@nd_receiver[nid])1863+
end1864+
if @nd_arguments[nid] >= 01865+
scan_new_calls(@nd_arguments[nid])1866+
end1867+
args = parse_id_list(@nd_args[nid])1868+
k = 01869+
while k < args.length1870+
scan_new_calls(args[k])1871+
k = k + 11872+
end1873+
if @nd_expression[nid] >= 01874+
scan_new_calls(@nd_expression[nid])1875+
end1876+
if @nd_predicate[nid] >= 01877+
scan_new_calls(@nd_predicate[nid])1878+
end1879+
if @nd_subsequent[nid] >= 01880+
scan_new_calls(@nd_subsequent[nid])1881+
end1882+
if @nd_else_clause[nid] >= 01883+
scan_new_calls(@nd_else_clause[nid])1884+
end1885+
if @nd_left[nid] >= 01886+
scan_new_calls(@nd_left[nid])1887+
end1888+
if @nd_right[nid] >= 01889+
scan_new_calls(@nd_right[nid])1890+
end1891+
if @nd_block[nid] >= 01892+
scan_new_calls(@nd_block[nid])1893+
end1894+
elems = parse_id_list(@nd_elements[nid])1895+
k = 01896+
while k < elems.length1897+
scan_new_calls(elems[k])1898+
k = k + 11899+
end1900+
conds = parse_id_list(@nd_conditions[nid])1901+
k = 01902+
while k < conds.length1903+
scan_new_calls(conds[k])1904+
k = k + 11905+
end1906+
end1907+
1908+
def update_ivar_types_from_params1909+
# For each class, if initialize assigns @ivar = param, update ivar type from param type1910+
i = 01911+
while i < @cls_names.length1912+
init_idx = cls_find_method_direct(i, "initialize")1913+
if init_idx >= 01914+
all_params = @cls_meth_params[i].split("|")1915+
all_ptypes = @cls_meth_ptypes[i].split("|")1916+
pnames = []1917+
ptypes = []1918+
if init_idx < all_params.length1919+
pnames = all_params[init_idx].split(",")1920+
end1921+
if init_idx < all_ptypes.length1922+
ptypes = all_ptypes[init_idx].split(",")1923+
end1924+
bodies = @cls_meth_bodies[i].split(";")1925+
bid = -11926+
if init_idx < bodies.length1927+
bid = bodies[init_idx].to_i1928+
end1929+
if bid >= 01930+
stmts = get_stmts(bid)1931+
k = 01932+
while k < stmts.length1933+
sid = stmts[k]1934+
if @nd_type[sid] == "InstanceVariableWriteNode"1935+
expr = @nd_expression[sid]1936+
if expr >= 01937+
if @nd_type[expr] == "LocalVariableReadNode"1938+
pname = @nd_name[expr]1939+
# Find param index1940+
pi = 01941+
while pi < pnames.length1942+
if pnames[pi] == pname1943+
if pi < ptypes.length1944+
# Update ivar type1945+
iname = @nd_name[sid]1946+
ivar_names = @cls_ivar_names[i].split(";")1947+
ivar_types = @cls_ivar_types[i].split(";")1948+
ij = 01949+
while ij < ivar_names.length1950+
if ivar_names[ij] == iname1951+
if ivar_types[ij] == "int"1952+
ivar_types[ij] = ptypes[pi]1953+
end1954+
end1955+
ij = ij + 11956+
end1957+
@cls_ivar_types[i] = join_sep(ivar_types, ";")1958+
end1959+
end1960+
pi = pi + 11961+
end1962+
end1963+
end1964+
end1965+
k = k + 11966+
end1967+
end1968+
end1969+
i = i + 11970+
end1971+
end1972+
17411973
def infer_all_returns1974+
# Pre-pass: scan for .new calls to infer constructor param types1975+
infer_constructor_types1976+
# Update ivar types from constructor params1977+
update_ivar_types_from_params1978+
17421979
# Top-level methods17431980
i = 017441981
while i < @meth_names.length@@ -1828,10 +2065,11 @@
18282065
if j < returns.length18292066
returns[j] = rt18302067
end2068+
# Save incrementally so later methods can see updated return types2069+
@cls_meth_returns[i] = join_sep(returns, ";")18312070
pop_scope18322071
j = j + 118332072
end1834-
@cls_meth_returns[i] = join_sep(returns, ";")18352073
18362074
# Class methods18372075
cmnames = @cls_cmeth_names[i].split(";")@@ -1883,6 +2121,37 @@
18832121
end18842122
end18852123
end2124+
# Also check super calls2125+
if @nd_type[sid] == "SuperNode"2126+
super_args = @nd_arguments[sid]2127+
if super_args >= 02128+
sa_ids = get_args(super_args)2129+
sk = 02130+
while sk < sa_ids.length2131+
if @nd_type[sa_ids[sk]] == "LocalVariableReadNode"2132+
if @nd_name[sa_ids[sk]] == pname2133+
# This param is passed to parent's initialize at position sk2134+
if @cls_parents[ci] != ""2135+
parent_ci = find_class_idx(@cls_parents[ci])2136+
if parent_ci >= 02137+
parent_init = cls_find_method_direct(parent_ci, "initialize")2138+
if parent_init >= 02139+
parent_ptypes = @cls_meth_ptypes[parent_ci].split("|")2140+
if parent_init < parent_ptypes.length2141+
ppt = parent_ptypes[parent_init].split(",")2142+
if sk < ppt.length2143+
return ppt[sk]2144+
end2145+
end2146+
end2147+
end2148+
end2149+
end2150+
end2151+
sk = sk + 12152+
end2153+
end2154+
end18862155
k = k + 118872156
end18882157
end@@ -2057,7 +2326,8 @@
20572326
end20582327
if mname == "to_a"20592328
if @nd_receiver[nid] >= 02060-
if @nd_type[@nd_receiver[nid]] == "RangeNode"2329+
rt = infer_type(@nd_receiver[nid])2330+
if rt == "range"20612331
@needs_int_array = 120622332
@needs_gc = 120632333
end@@ -3058,9 +3328,36 @@
30583328
lnames = []30593329
ltypes = []30603330
scan_locals(bid, lnames, ltypes, params)3331+
# Declare all locals first so block param inference can see them30613332
j = 030623333
while j < lnames.length30633334
declare_var(lnames[j], ltypes[j])3335+
j = j + 13336+
end3337+
# Second pass: re-scan with types now in scope for better block param inference3338+
lnames2 = []3339+
ltypes2 = []3340+
scan_locals(bid, lnames2, ltypes2, params)3341+
# Update types that may have improved3342+
j = 03343+
while j < lnames2.length3344+
k = 03345+
while k < lnames.length3346+
if lnames[k] == lnames2[j]3347+
if ltypes[k] == "int"3348+
if ltypes2[j] != "int"3349+
ltypes[k] = ltypes2[j]3350+
set_var_type(lnames[k], ltypes2[j])3351+
end3352+
end3353+
end3354+
k = k + 13355+
end3356+
j = j + 13357+
end3358+
# Emit declarations3359+
j = 03360+
while j < lnames.length30643361
emit(" " + c_type(ltypes[j]) + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")30653362
j = j + 130663363
end@@ -3121,6 +3418,19 @@
31213418
recv_type = ""31223419
if @nd_receiver[nid] >= 031233420
recv_type = infer_type(@nd_receiver[nid])3421+
# If type is int and receiver is local var, check names array3422+
if recv_type == "int"3423+
if @nd_type[@nd_receiver[nid]] == "LocalVariableReadNode"3424+
rname = @nd_name[@nd_receiver[nid]]3425+
ri = 03426+
while ri < names.length3427+
if names[ri] == rname3428+
recv_type = types[ri]3429+
end3430+
ri = ri + 13431+
end3432+
end3433+
end31243434
end31253435
mname = @nd_name[nid]31263436
if mname == "each"@@ -3591,7 +3901,7 @@
35913901
if recv < 035923902
mi = find_method_idx(mname)35933903
if mi >= 03594-
return "sp_" + sanitize_name(mname) + "(" + compile_call_args(nid) + ")"3904+
return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + ")"35953905
end35963906
if @current_class_idx >= 035973907
cidx = cls_find_method(@current_class_idx, mname)@@ -3868,6 +4178,25 @@
38684178
end38694179
end38704180
4181+
# reject as expression4182+
if mname == "reject"4183+
if @nd_block[nid] >= 04184+
return compile_reject_expr(nid)4185+
end4186+
end4187+
4188+
# reduce/inject as expression4189+
if mname == "reduce"4190+
if @nd_block[nid] >= 04191+
return compile_reduce_expr(nid)4192+
end4193+
end4194+
if mname == "inject"4195+
if @nd_block[nid] >= 04196+
return compile_reduce_expr(nid)4197+
end4198+
end4199+
38714200
# Math38724201
if @nd_type[recv] == "ConstantReadNode"38734202
if @nd_name[recv] == "Math"@@ -3877,12 +4206,27 @@
38774206
end38784207
end38794208
3880-
# to_a on range4209+
# to_a on range (including parenthesized range)38814210
if mname == "to_a"4211+
range_nid = -138824212
if @nd_type[recv] == "RangeNode"4213+
range_nid = recv4214+
end4215+
if @nd_type[recv] == "ParenthesesNode"4216+
pb = @nd_body[recv]4217+
if pb >= 04218+
pstmts = get_stmts(pb)4219+
if pstmts.length > 04220+
if @nd_type[pstmts[0]] == "RangeNode"4221+
range_nid = pstmts[0]4222+
end4223+
end4224+
end4225+
end4226+
if range_nid >= 038834227
@needs_int_array = 138844228
@needs_gc = 13885-
return "sp_IntArray_from_range(" + compile_expr(@nd_left[recv]) + ", " + compile_expr(@nd_right[recv]) + ")"4229+
return "sp_IntArray_from_range(" + compile_expr(@nd_left[range_nid]) + ", " + compile_expr(@nd_right[range_nid]) + ")"38864230
end38874231
end38884232
@@ -3984,6 +4328,40 @@
39844328
"(" + lc + " " + op + " " + rc + ")"39854329
end39864330
4331+
def compile_call_args_with_defaults(nid, mi)4332+
args_id = @nd_arguments[nid]4333+
arg_ids = []4334+
if args_id >= 04335+
arg_ids = get_args(args_id)4336+
end4337+
pnames = @meth_param_names[mi].split(",")4338+
defaults = @meth_has_defaults[mi].split(",")4339+
result = ""4340+
k = 04341+
while k < pnames.length4342+
if k > 04343+
result = result + ", "4344+
end4345+
if k < arg_ids.length4346+
result = result + compile_expr(arg_ids[k])4347+
else4348+
# Use default value4349+
if k < defaults.length4350+
def_id = defaults[k].to_i4351+
if def_id >= 04352+
result = result + compile_expr(def_id)4353+
else4354+
result = result + "0"4355+
end4356+
else4357+
result = result + "0"4358+
end4359+
end4360+
k = k + 14361+
end4362+
result4363+
end4364+
39874365
def compile_call_args(nid)39884366
args_id = @nd_arguments[nid]39894367
if args_id < 0@@ -4873,6 +5251,16 @@
48735251
@in_loop = old48745252
end48755253
5254+
def compile_reduce_expr(nid)5255+
# Emit the reduce loop as side effects, return accumulator5256+
compile_reduce_block(nid)5257+
bp1 = get_block_param(nid, 0)5258+
if bp1 == ""5259+
bp1 = "_acc"5260+
end5261+
"lv_" + bp15262+
end5263+
48765264
def compile_reduce_block(nid)48775265
rc = compile_expr(@nd_receiver[nid])48785266
init_val = compile_arg0(nid)@@ -4908,6 +5296,40 @@
49085296
end49095297
end49105298
5299+
def compile_reject_expr(nid)5300+
rc = compile_expr(@nd_receiver[nid])5301+
bp1 = get_block_param(nid, 0)5302+
if bp1 == ""5303+
bp1 = "_x"5304+
end5305+
rt = infer_type(@nd_receiver[nid])5306+
if rt == "int_array"5307+
@needs_int_array = 15308+
tmp_arr = new_temp5309+
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")5310+
tmp_i = new_temp5311+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")5312+
emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")5313+
@indent = @indent + 15314+
blk = @nd_block[nid]5315+
if blk >= 05316+
body = @nd_body[blk]5317+
if body >= 05318+
stmts = get_stmts(body)5319+
if stmts.length > 05320+
last = stmts[stmts.length - 1]5321+
cond = compile_expr(last)5322+
emit(" if (!(" + cond + ")) sp_IntArray_push(" + tmp_arr + ", lv_" + bp1 + ");")5323+
end5324+
end5325+
end5326+
@indent = @indent - 15327+
emit(" }")5328+
return tmp_arr5329+
end5330+
"0"5331+
end5332+
49115333
def compile_reject_block(nid)49125334
rc = compile_expr(@nd_receiver[nid])49135335
bp1 = get_block_param(nid, 0)