Commit 0a798c07

0a798c07cf74Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Inline non-escaping &block params (block.call / &-forwarding)

A method whose block parameter never escapes -- every use is a block.call(...) or a &block/& forward -- is inlined at its call sites like a yielding method, with no first-class proc. block.call routes through the yield expander (shared emit_block_invoke), block arg types flow from the call args, and BlockArgumentNode forwards the active block instead of resetting it. The virtual &block slot is skipped in local declaration. Anonymous & is forward-only and always inlinable. Escaping procs still error, pending sp_Proc support.

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

Details

Commit
0a798c07cf74
Parent
630fd313cb3c

src/analyze.cmodifiedView file

@@ -487,6 +487,13 @@
487487 int dv = nt_ref(c->nt, opts[i], "value");
488488 if (pname) scope_add_param(s, pname, dv);
489489 }
490+ /* `&block` parameter: tracked for yield-style inlining, no typed slot.
491+ Anonymous `&` has no name; record "" so forwarding still works. */
492+ int bp = nt_ref(c->nt, pn, "block");
493+ if (bp >= 0 && nt_type(c->nt, bp) && !strcmp(nt_type(c->nt, bp), "BlockParameterNode")) {
494+ const char *bn = nt_str(c->nt, bp, "name");
495+ s->blk_param = strdup(bn ? bn : "");
496+ }
490497 }
491498 child = new_idx;
492499 }
@@ -1034,6 +1041,24 @@
10341041 return -1;
10351042 }
10361043
1044+/* Arguments node of the first `<&block-param>.call(...)` in scope `si`, or
1045+ -1. Lets block-param inference treat block.call like a yield. */
1046+static int first_block_call_args(Compiler *c, int si) {
1047+ Scope *m = &c->scopes[si];
1048+ if (!m->blk_param || !m->blk_param[0]) return -1;
1049+ for (int id = 0; id < c->nt->count; id++) {
1050+ const char *ty = nt_type(c->nt, id);
1051+ if (!ty || strcmp(ty, "CallNode") || c->nscope[id] != si) continue;
1052+ const char *nm = nt_str(c->nt, id, "name");
1053+ if (!nm || strcmp(nm, "call")) continue;
1054+ int recv = nt_ref(c->nt, id, "receiver");
1055+ if (recv < 0 || !nt_type(c->nt, recv) || strcmp(nt_type(c->nt, recv), "LocalVariableReadNode")) continue;
1056+ const char *rn = nt_str(c->nt, recv, "name");
1057+ if (rn && !strcmp(rn, m->blk_param)) return nt_ref(c->nt, id, "arguments");
1058+ }
1059+ return -1;
1060+}
1061+
10371062 /* Bind block parameter types for supported iteration methods. */
10381063 static int infer_block_params(Compiler *c) {
10391064 const NodeTable *nt = c->nt;
@@ -1063,7 +1088,7 @@
10631088 }
10641089 if (mi >= 0 && c->scopes[mi].yields) {
10651090 int yn = first_yield(c, mi);
1066- int ya = yn >= 0 ? nt_ref(nt, yn, "arguments") : -1;
1091+ int ya = yn >= 0 ? nt_ref(nt, yn, "arguments") : first_block_call_args(c, mi);
10671092 int yc = 0;
10681093 const int *yargs = ya >= 0 ? nt_arr(nt, ya, "arguments", &yc) : NULL;
10691094 Scope *bs = comp_scope_of(c, block);
@@ -1240,6 +1265,40 @@
12401265 }
12411266 }
12421267
1268+ /* `&block` + block.call: a method whose block parameter never escapes
1269+ (every read is a `.call` receiver or a `&block` forward) is inlined at
1270+ its call sites exactly like a yielding method. The block-param slot is
1271+ then virtual -- the literal block flows in like an implicit yield. */
1272+ for (int mi = 0; mi < c->nscopes; mi++) {
1273+ Scope *m = &c->scopes[mi];
1274+ if (!m->blk_param) continue;
1275+ /* Anonymous `&`: nameless, so it can only be forwarded -- always safe
1276+ to inline (there is no escaping read to worry about). */
1277+ if (!m->blk_param[0]) { m->yields = 1; continue; }
1278+ int escapes = 0, uses = 0;
1279+ for (int id = 0; id < c->nt->count && !escapes; id++) {
1280+ const char *ty = nt_type(c->nt, id);
1281+ if (!ty || strcmp(ty, "LocalVariableReadNode")) continue;
1282+ if (comp_scope_of(c, id) != m) continue;
1283+ const char *nm = nt_str(c->nt, id, "name");
1284+ if (!nm || strcmp(nm, m->blk_param)) continue;
1285+ uses++;
1286+ /* approved: receiver of a `.call`, or expression of a `&block` arg */
1287+ int ok = 0;
1288+ for (int p = 0; p < c->nt->count; p++) {
1289+ const char *pty = nt_type(c->nt, p);
1290+ if (!pty) continue;
1291+ if (!strcmp(pty, "CallNode") && nt_ref(c->nt, p, "receiver") == id) {
1292+ const char *cn = nt_str(c->nt, p, "name");
1293+ if (cn && !strcmp(cn, "call")) { ok = 1; break; }
1294+ }
1295+ if (!strcmp(pty, "BlockArgumentNode") && nt_ref(c->nt, p, "expression") == id) { ok = 1; break; }
1296+ }
1297+ if (!ok) escapes = 1;
1298+ }
1299+ if (!escapes && uses > 0) m->yields = 1;
1300+ }
1301+
12431302 /* intern every symbol literal so codegen can emit the id table */
12441303 for (int id = 0; id < c->nt->count; id++) {
12451304 const char *ty = nt_type(c->nt, id);