Commit 6d53252c
6d53252cbe87Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
refactor: split infer_call_type into 8 focused submethods
Break up the 766-line infer_call_type into a dispatcher (~90 lines) and 8 submethods organized by inference category:
infer_operator_type infer_comparison_type infer_method_name_type infer_constructor_type infer_constant_recv_type infer_math_and_misc_type infer_recv_method_type infer_open_class_type
Verified: 57/57 tests pass, bootstrap gen2==gen3, fib=5702887.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 6d53252cbe87
- Parent
- 50052bc9da65
spinel_codegen.rbmodifiedView file
@@ -1017,10 +1017,97 @@
10171017
"str_int_hash"10181018
end10191019
1020+
10201021
def infer_call_type(nid)10211022
mname = @nd_name[nid]10221023
recv = @nd_receiver[nid]10231024
1025+
# Operators1026+
r = infer_operator_type(nid, mname, recv)1027+
if r != ""1028+
return r1029+
end1030+
1031+
# Comparison operators1032+
r = infer_comparison_type(mname)1033+
if r != ""1034+
return r1035+
end1036+
1037+
# Method name-based type inference1038+
r = infer_method_name_type(nid, mname, recv)1039+
if r != ""1040+
return r1041+
end1042+
1043+
# puts/print1044+
if mname == "puts"1045+
return "void"1046+
end1047+
if mname == "print"1048+
return "void"1049+
end1050+
1051+
# Constructor .new1052+
r = infer_constructor_type(nid, mname, recv)1053+
if r != ""1054+
return r1055+
end1056+
1057+
# Constant receiver (File, ENV, Dir) and StringIO1058+
r = infer_constant_recv_type(nid, mname, recv)1059+
if r != ""1060+
return r1061+
end1062+
1063+
# Math functions, backtick, freeze, to_a1064+
r = infer_math_and_misc_type(nid, mname, recv)1065+
if r != ""1066+
return r1067+
end1068+
1069+
# Method call on poly/int/obj receiver1070+
r = infer_recv_method_type(nid, mname, recv)1071+
if r != ""1072+
return r1073+
end1074+
1075+
# Top-level method1076+
mi = find_method_idx(mname)1077+
if mi >= 01078+
return @meth_return_types[mi]1079+
end1080+
1081+
# Bare method call in class context1082+
if @current_class_idx >= 01083+
mr = cls_method_return(@current_class_idx, mname)1084+
return mr1085+
end1086+
1087+
# proc / Proc.new1088+
if mname == "proc"1089+
return "proc"1090+
end1091+
if mname == "new"1092+
if recv >= 01093+
if @nd_type[recv] == "ConstantReadNode"1094+
if @nd_name[recv] == "Proc"1095+
return "proc"1096+
end1097+
end1098+
end1099+
end1100+
1101+
# Open class method dispatch1102+
r = infer_open_class_type(nid, mname, recv)1103+
if r != ""1104+
return r1105+
end1106+
1107+
"int"1108+
end1109+
1110+
def infer_operator_type(nid, mname, recv)10241111
if mname == "+"10251112
if recv >= 010261113
lt = infer_type(recv)@@ -1116,6 +1203,10 @@
11161203
if mname == "%"11171204
return "int"11181205
end1206+
""1207+
end1208+
1209+
def infer_comparison_type(mname)11191210
if mname == "<"11201211
return "bool"11211212
end@@ -1137,6 +1228,10 @@
11371228
if mname == "!"11381229
return "bool"11391230
end1231+
""1232+
end1233+
1234+
def infer_method_name_type(nid, mname, recv)11401235
if mname == "length"11411236
return "int"11421237
end@@ -1516,12 +1611,10 @@
15161611
end15171612
return "int"15181613
end1519-
if mname == "puts"1520-
return "void"1521-
end1522-
if mname == "print"1523-
return "void"1524-
end1614+
""1615+
end1616+
1617+
def infer_constructor_type(nid, mname, recv)15251618
if mname == "new"15261619
if recv >= 015271620
if @nd_type[recv] == "ConstantReadNode"@@ -1553,6 +1646,10 @@
15531646
end15541647
end15551648
end1649+
""1650+
end1651+
1652+
def infer_constant_recv_type(nid, mname, recv)15561653
# File operations15571654
if recv >= 015581655
if @nd_type[recv] == "ConstantReadNode"@@ -1601,6 +1698,10 @@
16011698
end16021699
end16031700
end1701+
""1702+
end1703+
1704+
def infer_math_and_misc_type(nid, mname, recv)16041705
# backtick16051706
if mname == "`"16061707
return "string"@@ -1641,7 +1742,10 @@
16411742
end16421743
return "int_array"16431744
end1745+
""1746+
end16441747
1748+
def infer_recv_method_type(nid, mname, recv)16451749
# Method call on poly16461750
if recv >= 016471751
rt = infer_type(recv)@@ -1733,33 +1837,10 @@
17331837
end17341838
end17351839
end1736-
1737-
# Top-level method1738-
mi = find_method_idx(mname)1739-
if mi >= 01740-
return @meth_return_types[mi]1741-
end1742-
1743-
# Bare method call in class context1744-
if @current_class_idx >= 01745-
mr = cls_method_return(@current_class_idx, mname)1746-
return mr1747-
end1748-
1749-
# proc / Proc.new1750-
if mname == "proc"1751-
return "proc"1752-
end1753-
if mname == "new"1754-
if recv >= 01755-
if @nd_type[recv] == "ConstantReadNode"1756-
if @nd_name[recv] == "Proc"1757-
return "proc"1758-
end1759-
end1760-
end1761-
end1840+
""1841+
end17621842
1843+
def infer_open_class_type(nid, mname, recv)17631844
# Check open class methods for receiver type17641845
if recv >= 017651846
rt = infer_type(recv)@@ -1781,9 +1862,9 @@
17811862
end17821863
end17831864
end1865+
""1866+
end17841867
1785-
"int"1786-
end17871868
17881869
def is_class_or_ancestor(cname, target)17891870
if cname == target