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 end
10191019
1020+
10201021 def infer_call_type(nid)
10211022 mname = @nd_name[nid]
10221023 recv = @nd_receiver[nid]
10231024
1025+ # Operators
1026+ r = infer_operator_type(nid, mname, recv)
1027+ if r != ""
1028+ return r
1029+ end
1030+
1031+ # Comparison operators
1032+ r = infer_comparison_type(mname)
1033+ if r != ""
1034+ return r
1035+ end
1036+
1037+ # Method name-based type inference
1038+ r = infer_method_name_type(nid, mname, recv)
1039+ if r != ""
1040+ return r
1041+ end
1042+
1043+ # puts/print
1044+ if mname == "puts"
1045+ return "void"
1046+ end
1047+ if mname == "print"
1048+ return "void"
1049+ end
1050+
1051+ # Constructor .new
1052+ r = infer_constructor_type(nid, mname, recv)
1053+ if r != ""
1054+ return r
1055+ end
1056+
1057+ # Constant receiver (File, ENV, Dir) and StringIO
1058+ r = infer_constant_recv_type(nid, mname, recv)
1059+ if r != ""
1060+ return r
1061+ end
1062+
1063+ # Math functions, backtick, freeze, to_a
1064+ r = infer_math_and_misc_type(nid, mname, recv)
1065+ if r != ""
1066+ return r
1067+ end
1068+
1069+ # Method call on poly/int/obj receiver
1070+ r = infer_recv_method_type(nid, mname, recv)
1071+ if r != ""
1072+ return r
1073+ end
1074+
1075+ # Top-level method
1076+ mi = find_method_idx(mname)
1077+ if mi >= 0
1078+ return @meth_return_types[mi]
1079+ end
1080+
1081+ # Bare method call in class context
1082+ if @current_class_idx >= 0
1083+ mr = cls_method_return(@current_class_idx, mname)
1084+ return mr
1085+ end
1086+
1087+ # proc / Proc.new
1088+ if mname == "proc"
1089+ return "proc"
1090+ end
1091+ if mname == "new"
1092+ if recv >= 0
1093+ if @nd_type[recv] == "ConstantReadNode"
1094+ if @nd_name[recv] == "Proc"
1095+ return "proc"
1096+ end
1097+ end
1098+ end
1099+ end
1100+
1101+ # Open class method dispatch
1102+ r = infer_open_class_type(nid, mname, recv)
1103+ if r != ""
1104+ return r
1105+ end
1106+
1107+ "int"
1108+ end
1109+
1110+ def infer_operator_type(nid, mname, recv)
10241111 if mname == "+"
10251112 if recv >= 0
10261113 lt = infer_type(recv)
@@ -1116,6 +1203,10 @@
11161203 if mname == "%"
11171204 return "int"
11181205 end
1206+ ""
1207+ end
1208+
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 end
1231+ ""
1232+ end
1233+
1234+ def infer_method_name_type(nid, mname, recv)
11401235 if mname == "length"
11411236 return "int"
11421237 end
@@ -1516,12 +1611,10 @@
15161611 end
15171612 return "int"
15181613 end
1519- if mname == "puts"
1520- return "void"
1521- end
1522- if mname == "print"
1523- return "void"
1524- end
1614+ ""
1615+ end
1616+
1617+ def infer_constructor_type(nid, mname, recv)
15251618 if mname == "new"
15261619 if recv >= 0
15271620 if @nd_type[recv] == "ConstantReadNode"
@@ -1553,6 +1646,10 @@
15531646 end
15541647 end
15551648 end
1649+ ""
1650+ end
1651+
1652+ def infer_constant_recv_type(nid, mname, recv)
15561653 # File operations
15571654 if recv >= 0
15581655 if @nd_type[recv] == "ConstantReadNode"
@@ -1601,6 +1698,10 @@
16011698 end
16021699 end
16031700 end
1701+ ""
1702+ end
1703+
1704+ def infer_math_and_misc_type(nid, mname, recv)
16041705 # backtick
16051706 if mname == "`"
16061707 return "string"
@@ -1641,7 +1742,10 @@
16411742 end
16421743 return "int_array"
16431744 end
1745+ ""
1746+ end
16441747
1748+ def infer_recv_method_type(nid, mname, recv)
16451749 # Method call on poly
16461750 if recv >= 0
16471751 rt = infer_type(recv)
@@ -1733,33 +1837,10 @@
17331837 end
17341838 end
17351839 end
1736-
1737- # Top-level method
1738- mi = find_method_idx(mname)
1739- if mi >= 0
1740- return @meth_return_types[mi]
1741- end
1742-
1743- # Bare method call in class context
1744- if @current_class_idx >= 0
1745- mr = cls_method_return(@current_class_idx, mname)
1746- return mr
1747- end
1748-
1749- # proc / Proc.new
1750- if mname == "proc"
1751- return "proc"
1752- end
1753- if mname == "new"
1754- if recv >= 0
1755- if @nd_type[recv] == "ConstantReadNode"
1756- if @nd_name[recv] == "Proc"
1757- return "proc"
1758- end
1759- end
1760- end
1761- end
1840+ ""
1841+ end
17621842
1843+ def infer_open_class_type(nid, mname, recv)
17631844 # Check open class methods for receiver type
17641845 if recv >= 0
17651846 rt = infer_type(recv)
@@ -1781,9 +1862,9 @@
17811862 end
17821863 end
17831864 end
1865+ ""
1866+ end
17841867
1785- "int"
1786- end
17871868
17881869 def is_class_or_ancestor(cname, target)
17891870 if cname == target