Commit c1b9d105

c1b9d1054ca9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

Merge pull request #113 from ryanseys/rs-block-forward-proc-var

feat(codegen): forward &expr block-argument call args

Details

Commit
c1b9d1054ca9
Parent
1f9b7c16d448
Merge parent
91432ca12f4e

spinel_codegen.rbmodifiedView file

@@ -698,13 +698,55 @@
698698 end
699699
700700 # Returns 1 if @nd_block[nid] is a literal BlockNode (do/end body),
701- # 0 otherwise. Used by &block-forwarding call sites to decide
702- # whether to emit a proc literal for the trailing block slot.
701+ # 0 otherwise. Pairs with find_block_arg to dispatch correctly at
702+ # &block-forwarding call sites (literal block vs. `&proc_var`).
703703 def has_literal_block(nid)
704704 blk = @nd_block[nid]
705705 (blk >= 0 && @nd_type[blk] == "BlockNode") ? 1 : 0
706706 end
707707
708+ # Returns the inner expression of a BlockArgumentNode whose payload
709+ # is a captured proc local (the `&block` form). Returns -1 for
710+ # absent block-arg, or for shapes the codegen doesn't yet forward
711+ # — `&:sym` (SymbolNode) and `&nil` (NilNode), which would need
712+ # symbol-to-proc / nil-as-no-block lowering. Call sites fall
713+ # through to the no-block path in those cases.
714+ def find_block_arg(nid)
715+ blk = @nd_block[nid]
716+ if blk < 0
717+ return -1
718+ end
719+ if @nd_type[blk] != "BlockArgumentNode"
720+ return -1
721+ end
722+ inner = @nd_expression[blk]
723+ if inner < 0
724+ return -1
725+ end
726+ if @nd_type[inner] != "LocalVariableReadNode"
727+ return -1
728+ end
729+ inner
730+ end
731+
732+ # Resolves the call-site block-forwarding expression: returns the C
733+ # expression for the proc to forward at a `&block`-taking call site
734+ # (a literal block compiles to sp_proc_new(...); a `&proc_var` is
735+ # the captured `sp_Proc *` local), or "" if the call site provides
736+ # no block. Sets @needs_proc when a forwarded proc is emitted.
737+ def block_forward_expr(nid)
738+ if has_literal_block(nid) == 1
739+ @needs_proc = 1
740+ return compile_proc_literal(nid)
741+ end
742+ ba = find_block_arg(nid)
743+ if ba >= 0
744+ @needs_proc = 1
745+ return compile_expr(ba)
746+ end
747+ ""
748+ end
749+
708750 # Flatten a constant reference into an internal name.
709751 # C -> C
710752 # ::C -> C
@@ -13550,20 +13592,25 @@
1355013592 if @meth_has_yield[mi] == 1
1355113593 yargs = ", NULL, NULL"
1355213594 end
13553- # Check if function has a &block param and caller provides a block
13595+ # Check if function has a &block param and caller provides a block.
13596+ # Ruby syntax requires `&block` to be the trailing param — a proc-typed
13597+ # slot in any other position is a positional proc argument, not a block
13598+ # param. Mirrors cls_method_has_block_param.
1355413599 ptypes = @meth_param_types[mi].split(",")
13555- has_block_param = 0
13556- pk = 0
13557- while pk < ptypes.length
13558- if ptypes[pk] == "proc"
13559- has_block_param = 1
13560- end
13561- pk = pk + 1
13562- end
13600+ has_block_param = (ptypes.length > 0 && ptypes.last == "proc") ? 1 : 0
1356313601 if has_block_param == 1
13564- if @nd_block[nid] >= 0
13565- block_proc = compile_proc_literal(nid)
13566- return "sp_" + sanitize_name(mname) + "(" + compile_call_args(nid) + ", " + block_proc + ")"
13602+ # Forward the call site's literal block or `&proc_var` into the
13603+ # callee's &block slot. Use compile_call_args_with_defaults so
13604+ # optional positional params get their defaults filled in, and
13605+ # pass omit_trailing=1 so the &block slot isn't default-padded
13606+ # with "0" — we append the actual proc explicitly below.
13607+ block_proc = block_forward_expr(nid)
13608+ if block_proc != ""
13609+ ca = compile_call_args_with_defaults(nid, mi, 1)
13610+ if ca == ""
13611+ return "sp_" + sanitize_name(mname) + "(" + block_proc + ")"
13612+ end
13613+ return "sp_" + sanitize_name(mname) + "(" + ca + ", " + block_proc + ")"
1356713614 end
1356813615 end
1356913616 return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")"
@@ -13655,9 +13702,8 @@
1365513702 end
1365613703 bp = ""
1365713704 if has_proc == 1
13658- if has_literal_block(nid) == 1
13659- bp = compile_proc_literal(nid)
13660- else
13705+ bp = block_forward_expr(nid)
13706+ if bp == ""
1366113707 # The callee declares &block but the call site provides
1366213708 # none — fill the slot with NULL so the C call has the
1366313709 # right arity.
@@ -16509,13 +16555,11 @@
1650916555 end
1651016556 bp = ""
1651116557 if has_proc == 1
16512- if has_literal_block(nid) == 1
16513- bp = compile_proc_literal(nid)
16514- else
16558+ bp = block_forward_expr(nid)
16559+ if bp == ""
1651516560 # The callee declares &block but the call site provides
1651616561 # none — fill the slot with NULL so the C call has the
16517- # right arity (compile_typed_call_args has already been
16518- # told to skip default-padding for this slot).
16562+ # right arity.
1651916563 bp = "0"
1652016564 end
1652116565 end
@@ -17139,7 +17183,11 @@
1713917183 result
1714017184 end
1714117185
17142- def compile_call_args_with_defaults(nid, mi)
17186+ def compile_call_args_with_defaults(nid, mi, omit_trailing = 0)
17187+ # `omit_trailing` is the number of trailing param slots to leave out
17188+ # entirely — block-forwarding call sites pass 1 so the &block slot
17189+ # isn't default-padded with "0" (the actual proc is appended by the
17190+ # caller after this returns).
1714317191 args_id = @nd_arguments[nid]
1714417192 arg_ids = []
1714517193 if args_id >= 0
@@ -17148,6 +17196,19 @@
1714817196 pnames = @meth_param_names[mi].split(",")
1714917197 ptypes = @meth_param_types[mi].split(",")
1715017198 defaults = @meth_has_defaults[mi].split(",")
17199+ if omit_trailing > 0
17200+ kept = "".split(",")
17201+ pk = 0
17202+ limit = pnames.length - omit_trailing
17203+ if limit < 0
17204+ limit = 0
17205+ end
17206+ while pk < limit
17207+ kept.push(pnames[pk])
17208+ pk = pk + 1
17209+ end
17210+ pnames = kept
17211+ end
1715117212
1715217213 # Check if args contain a KeywordHashNode - extract kw pairs
1715317214 kw_names = "".split(",")