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
end699699
700700
# Returns 1 if @nd_block[nid] is a literal BlockNode (do/end body),701-
# 0 otherwise. Used by &block-forwarding call sites to decide702-
# whether to emit a proc literal for the trailing block slot.701+
# 0 otherwise. Pairs with find_block_arg to dispatch correctly at702+
# &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 : 0706706
end707707
708+
# Returns the inner expression of a BlockArgumentNode whose payload709+
# is a captured proc local (the `&block` form). Returns -1 for710+
# absent block-arg, or for shapes the codegen doesn't yet forward711+
# — `&:sym` (SymbolNode) and `&nil` (NilNode), which would need712+
# symbol-to-proc / nil-as-no-block lowering. Call sites fall713+
# through to the no-block path in those cases.714+
def find_block_arg(nid)715+
blk = @nd_block[nid]716+
if blk < 0717+
return -1718+
end719+
if @nd_type[blk] != "BlockArgumentNode"720+
return -1721+
end722+
inner = @nd_expression[blk]723+
if inner < 0724+
return -1725+
end726+
if @nd_type[inner] != "LocalVariableReadNode"727+
return -1728+
end729+
inner730+
end731+
732+
# Resolves the call-site block-forwarding expression: returns the C733+
# expression for the proc to forward at a `&block`-taking call site734+
# (a literal block compiles to sp_proc_new(...); a `&proc_var` is735+
# the captured `sp_Proc *` local), or "" if the call site provides736+
# no block. Sets @needs_proc when a forwarded proc is emitted.737+
def block_forward_expr(nid)738+
if has_literal_block(nid) == 1739+
@needs_proc = 1740+
return compile_proc_literal(nid)741+
end742+
ba = find_block_arg(nid)743+
if ba >= 0744+
@needs_proc = 1745+
return compile_expr(ba)746+
end747+
""748+
end749+
708750
# Flatten a constant reference into an internal name.709751
# C -> C710752
# ::C -> C@@ -13550,20 +13592,25 @@
1355013592
if @meth_has_yield[mi] == 11355113593
yargs = ", NULL, NULL"1355213594
end13553-
# Check if function has a &block param and caller provides a block13595+
# Check if function has a &block param and caller provides a block.13596+
# Ruby syntax requires `&block` to be the trailing param — a proc-typed13597+
# slot in any other position is a positional proc argument, not a block13598+
# param. Mirrors cls_method_has_block_param.1355413599
ptypes = @meth_param_types[mi].split(",")13555-
has_block_param = 013556-
pk = 013557-
while pk < ptypes.length13558-
if ptypes[pk] == "proc"13559-
has_block_param = 113560-
end13561-
pk = pk + 113562-
end13600+
has_block_param = (ptypes.length > 0 && ptypes.last == "proc") ? 1 : 01356313601
if has_block_param == 113564-
if @nd_block[nid] >= 013565-
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 the13603+
# callee's &block slot. Use compile_call_args_with_defaults so13604+
# optional positional params get their defaults filled in, and13605+
# pass omit_trailing=1 so the &block slot isn't default-padded13606+
# 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+
end13613+
return "sp_" + sanitize_name(mname) + "(" + ca + ", " + block_proc + ")"1356713614
end1356813615
end1356913616
return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")"@@ -13655,9 +13702,8 @@
1365513702
end1365613703
bp = ""1365713704
if has_proc == 113658-
if has_literal_block(nid) == 113659-
bp = compile_proc_literal(nid)13660-
else13705+
bp = block_forward_expr(nid)13706+
if bp == ""1366113707
# The callee declares &block but the call site provides1366213708
# none — fill the slot with NULL so the C call has the1366313709
# right arity.@@ -16509,13 +16555,11 @@
1650916555
end1651016556
bp = ""1651116557
if has_proc == 116512-
if has_literal_block(nid) == 116513-
bp = compile_proc_literal(nid)16514-
else16558+
bp = block_forward_expr(nid)16559+
if bp == ""1651516560
# The callee declares &block but the call site provides1651616561
# none — fill the slot with NULL so the C call has the16517-
# right arity (compile_typed_call_args has already been16518-
# told to skip default-padding for this slot).16562+
# right arity.1651916563
bp = "0"1652016564
end1652116565
end@@ -17139,7 +17183,11 @@
1713917183
result1714017184
end1714117185
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 out17188+
# entirely — block-forwarding call sites pass 1 so the &block slot17189+
# isn't default-padded with "0" (the actual proc is appended by the17190+
# 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 > 017200+
kept = "".split(",")17201+
pk = 017202+
limit = pnames.length - omit_trailing17203+
if limit < 017204+
limit = 017205+
end17206+
while pk < limit17207+
kept.push(pnames[pk])17208+
pk = pk + 117209+
end17210+
pnames = kept17211+
end1715117212
1715217213
# Check if args contain a KeywordHashNode - extract kw pairs1715317214
kw_names = "".split(",")spinel_parse.cmodifiedView file
Loading diff…
test/block_forward_block_arg.rbaddedView file
Loading diff…