Commit 0c3c5d91

0c3c5d918fc5Merged

Yusuke Endoh <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

chore(codegen): introduce is_array_type helper and use it at converged sites

After the per-method poly_array fixes (Array#shuffle, Array#+, Array#concat, Array#each_with_object, Array#flat_map), eight call sites independently spelled out the same int_array | str_array | float_array | sym_array | poly_array predicate. Factor it into is_array_type and use it at those sites. *_ptr_array stays out of the helper for now — the existing dispatchers don't route ptr_array correctly even on master and adding it would be a separate concern.

No behavior change.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
0c3c5d918fc5
Parent
2951d3d713b2

spinel_codegen.rbmodifiedView file

@@ -1676,7 +1676,7 @@
16761676 if lt == "poly"
16771677 return "poly"
16781678 end
1679- if lt == "int_array" || lt == "str_array" || lt == "float_array" || lt == "sym_array" || lt == "poly_array"
1679+ if is_array_type(lt) == 1
16801680 return lt
16811681 end
16821682 if lt == "float"
@@ -2380,7 +2380,7 @@
23802380 if bbs.length > 0
23812381 bret = infer_type(bbs.last)
23822382 # If block returns an array type, use it as result type
2383- if bret == "int_array" || bret == "str_array" || bret == "float_array" || bret == "sym_array" || bret == "poly_array"
2383+ if is_array_type(bret) == 1
23842384 return bret
23852385 end
23862386 end
@@ -3539,6 +3539,20 @@
35393539 "IntArray"
35403540 end
35413541
3542+ # The canonical "is this an array type?" check. Use this when you need
3543+ # to dispatch a method that's defined for every typed array — `+`,
3544+ # `concat`, `shuffle`, `each_with_object`, `flat_map`, etc. Covers the
3545+ # 5 typed arrays (int/str/float/sym/poly). *_ptr_array is intentionally
3546+ # excluded for now: sp_PtrArray lacks `_dup`/`_shuffle` and several
3547+ # other helpers, and the existing dispatchers don't route ptr_array
3548+ # through this path correctly even on master.
3549+ def is_array_type(t)
3550+ if t == "int_array" || t == "str_array" || t == "float_array" || t == "sym_array" || t == "poly_array"
3551+ return 1
3552+ end
3553+ 0
3554+ end
3555+
35423556 # ---- Collection pass ----
35433557 def collect_all
35443558 root = @root_id
@@ -13814,7 +13828,7 @@
1381413828 @needs_string_helpers = 1
1381513829 return "sp_poly_add(" + compile_expr(recv) + ", " + box_expr_to_poly(@nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid])[0] : -1) + ")"
1381613830 end
13817- if lt == "int_array" || lt == "str_array" || lt == "float_array" || lt == "sym_array" || lt == "poly_array"
13831+ if is_array_type(lt) == 1
1381813832 rc = compile_expr_gc_rooted(recv)
1381913833 arg = compile_arg0(nid)
1382013834 pfx = array_c_prefix(lt)
@@ -14903,7 +14917,7 @@
1490314917 # the same method name (e.g. (poly).to_s, (int).to_s) fall through
1490414918 # to their own scalar dispatchers.
1490514919 if mname == "inspect" || mname == "to_s"
14906- if recv_type == "int_array" || recv_type == "float_array" || recv_type == "str_array" || recv_type == "sym_array" || recv_type == "poly_array"
14920+ if is_array_type(recv_type) == 1
1490714921 r = compile_inspect_for(recv_type, rc)
1490814922 if r != ""
1490914923 return r
@@ -15089,14 +15103,12 @@
1508915103 pfx = array_c_prefix(recv_type)
1509015104 return "sp_" + pfx + "_get(" + rc + ", rand() % sp_" + pfx + "_length(" + rc + "))"
1509115105 end
15092- if mname == "shuffle" &&
15093- (recv_type == "int_array" || recv_type == "str_array" || recv_type == "float_array" || recv_type == "sym_array" || recv_type == "poly_array")
15106+ if mname == "shuffle" && is_array_type(recv_type) == 1
1509415107 @needs_rand = 1
1509515108 pfx = array_c_prefix(recv_type)
1509615109 return "sp_" + pfx + "_shuffle(" + rc + ")"
1509715110 end
15098- if mname == "shuffle!" &&
15099- (recv_type == "int_array" || recv_type == "str_array" || recv_type == "float_array" || recv_type == "sym_array" || recv_type == "poly_array")
15111+ if mname == "shuffle!" && is_array_type(recv_type) == 1
1510015112 @needs_rand = 1
1510115113 pfx = array_c_prefix(recv_type)
1510215114 emit(" sp_" + pfx + "_shuffle_bang(" + rc + ");")
@@ -18670,7 +18682,7 @@
1867018682 if mname == "concat"
1867118683 if recv >= 0
1867218684 rt = infer_type(recv)
18673- if rt == "int_array" || rt == "str_array" || rt == "float_array" || rt == "sym_array" || rt == "poly_array"
18685+ if is_array_type(rt) == 1
1867418686 rc = compile_expr_gc_rooted(recv)
1867518687 arg = compile_arg0(nid)
1867618688 pfx = array_c_prefix(rt)
@@ -20965,7 +20977,7 @@
2096520977 result = new_temp
2096620978 emit(" " + obj_ct + " " + result + " = " + obj_arg + ";")
2096720979 tmp_i = new_temp
20968- if rt == "int_array" || rt == "str_array" || rt == "float_array" || rt == "sym_array" || rt == "poly_array"
20980+ if is_array_type(rt) == 1
2096920981 pfx = array_c_prefix(rt)
2097020982 emit(" {")
2097120983 @indent = @indent + 1
@@ -21726,7 +21738,7 @@
2172621738 end
2172721739 end
2172821740 # Fall back to receiver type if block doesn't return an array
21729- if block_ret != "int_array" && block_ret != "str_array" && block_ret != "float_array" && block_ret != "sym_array" && block_ret != "poly_array"
21741+ if is_array_type(block_ret) == 0
2173021742 block_ret = rt
2173121743 end
2173221744 @needs_gc = 1