Class ExpressionResultAssigner

java.lang.Object
org.ek9lang.compiler.phase7.generator.ExpressionResultAssigner

public final class ExpressionResultAssigner extends Object
Generates ARC-safe assignment instructions for expression form control flow branches.

Expression forms like result <- switch expr require special ARC memory management when assigning branch results to the return variable. This helper generates the correct instruction sequence following the ternary operator pattern.

The ARC ownership transfer pattern inside branches is:
RELEASE result        // Release old value (decrement ARC count)
STORE result, _temp   // Store new value from branch computation
RETAIN result         // Increment ARC count (ownership transfer)

IMPORTANT: NO SCOPE_REGISTER is emitted inside branches. The receiving scope (outer scope where the return variable is declared) handles scope registration. This separation ensures proper ARC semantics:

  • Branch: RETAIN without SCOPE_REGISTER (ownership transfer to outer scope)
  • Outer scope: SCOPE_REGISTER during return variable setup (ensures cleanup)
See Also:
  • Constructor Details

    • ExpressionResultAssigner

      public ExpressionResultAssigner()
  • Method Details

    • assign

      public List<IRInstr> assign(String returnVariable, String branchResult, DebugInfo debugInfo)
      Generate ARC-safe assignment instructions for a branch result.

      This follows the ternary operator pattern from ControlFlowChainGenerator.generateTernary():

      // Store then result to output variable
      thenBodyEvaluation.add(MemoryInstr.release(resultVariable, debugInfo));
      thenBodyEvaluation.add(MemoryInstr.store(resultVariable, thenResultTemp, debugInfo));
      thenBodyEvaluation.add(MemoryInstr.retain(resultVariable, debugInfo));
      
      Parameters:
      returnVariable - The return variable name (e.g., the "result" in "result <- switch")
      branchResult - The temporary variable holding the branch's computed result
      debugInfo - Debug information for the instructions
      Returns:
      List of IR instructions for the ARC-safe assignment
    • shouldAssign

      public boolean shouldAssign(String returnVariable, String branchResult)
      Check if assignment should be generated for this context.

      Assignment is only generated when:

      • This is an expression form (returnVariable is not null)
      • The branch has a result to assign (branchResult is not null)
      Parameters:
      returnVariable - The return variable name (may be null for statement forms)
      branchResult - The branch result variable (may be null if branch has no value)
      Returns:
      true if assignment instructions should be generated
    • assignIfNeeded

      public List<IRInstr> assignIfNeeded(String returnVariable, String branchResult, DebugInfo debugInfo)
      Conditionally generate assignment instructions. Returns empty list if this is a statement form (no return variable) or if the branch has no result to assign.
      Parameters:
      returnVariable - The return variable name (may be null for statement forms)
      branchResult - The branch result variable (may be null)
      debugInfo - Debug information for the instructions
      Returns:
      List of IR instructions, or empty list if no assignment needed