Commit 83536b81

83536b81e8feMerged

Ryan Seys <[email protected]> · 6 months ago · 8 files · +6 added · ~2 modified

Description

feat(parse,codegen): support Call{Operator,And,Or}WriteNode (3 nodes)

Bundle of three Call* compound-write forms. Together with the Index{Operator,And,Or,Target}WriteNode set and the existing IndexOperatorWriteNode, this closes out the compound-write family across all variable kinds (locals, ivars, globals, cvars, indexed slots, and now method-style accessors).

Critical for correctness: obj.bar += val evaluates the receiver exactly ONCE -- the source is shorthand for tmp = obj; tmp.bar = tmp.bar + val, NOT obj.bar = obj.bar + val. The single-evaluation matters when the receiver expression has side effects (the test exercises get_cfg(c).name &&= "second" and verifies the helper fires exactly once via a global counter). compile_call_assign_typed emits a temp-receiver block:

{ sp_<Cls> *_t = recv; _t->iv_<bname> = _t->iv_<bname> <op> (val); }

so the read and write share one temp.

Implementation restriction: only typed instance receivers backed by attr_accessor (or struct field) are supported. The codegen checks rt[0,4] == "obj_" to detect a typed instance, then cls_has_attr_writer(ci, bname) to confirm the attr exists. For non-attr receivers (untyped, polymorphic, or method-without- backing-ivar), we exit(1) with a precise message rather than emit silently-wrong C -- matching Phase 0's loud-failure spirit even though the parser succeeded.

CallOperatorWriteNode (obj.attr += val): Full operator dispatch: +, -, *, /, %, <<, >>, &, |, ^. String-valued attrs with += route to sp_str_concat instead of raw + (mirroring the LocalVariableOperatorWriteNode arm).

CallAndWriteNode (obj.attr &&= val): Emits if (_t->iv_<bname>) _t->iv_<bname> = (val);.

CallOrWriteNode (obj.attr ||= val): Emits if (!_t->iv_<bname>) _t->iv_<bname> = (val);. Test fixture documents the Spinel-specific gap: nil-typed string slots get sentineled to "" by the constructor's default arg path, and "" is C-truthy, so the "fire on nil" branch doesn't exercise cleanly. The don't-fire-on-truthy branch agrees in C and Ruby, which the test covers.

CallTargetNode (the 4th compound form, obj.x, p.y = 1, 2 for multi-assign LHS) is intentionally NOT in this commit -- it landed upstream via mame's #216 (CallTargetNode in multi-write targets), so it is already present.

Test exercises (3 fixtures):

  • call_op_write: Counter#n with all 10 numeric operators; Greeting#msg += " world" exercising the string-concat path.
  • call_or_write: don't-fire-on-truthy + receiver-once via side-effecting helper.
  • call_and_write: fire-on-truthy + receiver-once verification.

Out of scope (each documented inline): non-attr_accessor receivers (method-style getters with custom logic); polymorphic receivers (untyped or union-type); the nil-vs-empty-string conversion gap that limits the ||= "fire on nil" branch for string slots.

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

Details

Commit
83536b81e8fe
Parent
40403f434501

spinel_codegen.rbmodifiedView file

@@ -25495,6 +25495,18 @@
2549525495 compile_index_or_assign(nid)
2549625496 return
2549725497 end
25498+ if t == "CallOperatorWriteNode"
25499+ compile_call_assign_typed(nid, "Operator")
25500+ return
25501+ end
25502+ if t == "CallAndWriteNode"
25503+ compile_call_assign_typed(nid, "And")
25504+ return
25505+ end
25506+ if t == "CallOrWriteNode"
25507+ compile_call_assign_typed(nid, "Or")
25508+ return
25509+ end
2549825510 if t == "IfNode"
2549925511 compile_if_stmt(nid)
2550025512 return
@@ -29162,6 +29174,67 @@
2916229174 end
2916329175 end
2916429176
29177+ # `obj.attr <op>= val` family helper -- builds the
29178+ # temp-receiver-once C block for typed-instance receivers backed by
29179+ # an attr_accessor (or struct field). For non-attr receivers, we
29180+ # exit with a precise error rather than fall through to an
29181+ # incorrect emission. The caller passes a fragment that takes the
29182+ # temp variable name and returns the C body to execute.
29183+ #
29184+ # Spec note: even for the typed-attr-accessor case the receiver is
29185+ # evaluated exactly ONCE -- the source `obj.bar += val` is
29186+ # `tmp = obj; tmp.bar = tmp.bar + val`, NOT `obj.bar = obj.bar + val`.
29187+ # The temp pattern matters when the receiver expression has side
29188+ # effects (e.g. `next_holder().attr += 1`).
29189+ def compile_call_assign_typed(nid, kind)
29190+ recv = @nd_receiver[nid]
29191+ rt = infer_type(recv)
29192+ bname = @nd_name[nid] # parser emits read_name as "name"
29193+ if rt.length <= 4 || rt[0, 4] != "obj_"
29194+ $stderr.puts "Spinel: Call" + kind + "WriteNode requires a typed instance receiver with attr_accessor; got receiver type \"" + rt + "\""
29195+ exit(1)
29196+ end
29197+ cname = rt[4, rt.length - 4]
29198+ ci = find_class_idx(cname)
29199+ if ci < 0 || cls_has_attr_writer(ci, bname) == 0
29200+ $stderr.puts "Spinel: Call" + kind + "WriteNode for `." + bname + "` requires attr_accessor (or struct field) on class " + cname
29201+ exit(1)
29202+ end
29203+ rc = compile_expr_gc_rooted(recv)
29204+ val = compile_expr(@nd_expression[nid])
29205+ tt = new_temp
29206+ field = "iv_" + bname
29207+ if kind == "Operator"
29208+ op = @nd_binop[nid]
29209+ ivar_t = cls_ivar_type(ci, "@" + bname)
29210+ if op == "+" && ivar_t == "string" && infer_type(@nd_expression[nid]) == "string"
29211+ # String-valued attr with `+=` becomes sp_str_concat, mirroring
29212+ # the LocalVariableOperatorWriteNode arm at line 21786.
29213+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29214+ tt + "->" + field + " = sp_str_concat(" + tt + "->" + field + ", (" + val + ")); }")
29215+ return
29216+ end
29217+ if op == "%"
29218+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29219+ tt + "->" + field + " = sp_imod(" + tt + "->" + field + ", (" + val + ")); }")
29220+ else
29221+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29222+ tt + "->" + field + " = " + tt + "->" + field + " " + op + " (" + val + "); }")
29223+ end
29224+ return
29225+ end
29226+ if kind == "And"
29227+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29228+ "if (" + tt + "->" + field + ") " + tt + "->" + field + " = (" + val + "); }")
29229+ return
29230+ end
29231+ if kind == "Or"
29232+ emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " +
29233+ "if (!" + tt + "->" + field + ") " + tt + "->" + field + " = (" + val + "); }")
29234+ return
29235+ end
29236+ end
29237+
2916529238 # `a[i] &&= val` -- read once, conditionally write once. Mirrors
2916629239 # compile_index_op_assign's per-receiver-type dispatch but routes
2916729240 # the new value through a C `if (cur)` guard instead of an