Class ControlFlowChainGenerator

java.lang.Object
org.ek9lang.compiler.phase7.generator.AbstractGenerator
org.ek9lang.compiler.phase7.generator.ControlFlowChainGenerator
All Implemented Interfaces:
Function<ControlFlowChainDetails, List<IRInstr>>

public final class ControlFlowChainGenerator extends AbstractGenerator implements Function<ControlFlowChainDetails, List<IRInstr>>
Unified IR generator for EK9 control flow operators using CONTROL_FLOW_CHAIN.

Supported Operators:

  • Question operator (?) - null/set checking
  • Guarded assignment (:=?) - conditional assignment
  • Null coalescing operator (??) - returns LHS if allocated, else RHS
  • Elvis coalescing operator (:?) - returns LHS if allocated AND set, else RHS
  • Comparison coalescing operators (<?, >?, <=?, >=?) - safe comparison with null/unset handling

Key architectural benefits:
- Single source of truth for operator control flow logic
- Consistent memory management patterns across all operators
- Unified optimization metadata for backend code generation
- Reduced code duplication and maintenance burden

NOTE: If/else, switch, while, do-while, for, and try/catch statements have their own dedicated IR generators and are NOT generated by this class.

  • Constructor Details

  • Method Details

    • apply

      public List<IRInstr> apply(ControlFlowChainDetails details)
      Specified by:
      apply in interface Function<ControlFlowChainDetails, List<IRInstr>>
    • generateQuestionOperator

      public List<IRInstr> generateQuestionOperator(ExprProcessingDetails exprDetails)
      Generate SWITCH_CHAIN_BLOCK for Question operator (?). Converts "operand?" to a two-case chain: null check + _isSet() default. Question operator is self-contained - no RETAIN/SCOPE_REGISTER needed on input.
    • generateQuestionOperatorForVariable

      public List<IRInstr> generateQuestionOperatorForVariable(ISymbol variableSymbol, String resultVariable, DebugInfo debugInfo)
      Generate SWITCH_CHAIN_BLOCK for Question operator applied to a variable. Used by guarded assignment composition.
    • generateQuestionOperatorForTemp

      public List<IRInstr> generateQuestionOperatorForTemp(String variableName, String variableTypeName, boolean isTraitCall, String resultVariable, DebugInfo debugInfo)
      Generate SWITCH_CHAIN_BLOCK for Question operator applied to a temp variable. Used when the variable is a temp holding an expression result, not a named symbol.
      Parameters:
      variableName - The IR variable name (temp variable)
      variableTypeName - The type name for _isSet() method resolution
      isTraitCall - Whether to use INVOKEINTERFACE vs INVOKEVIRTUAL
      resultVariable - The variable to store the Boolean result into
      debugInfo - Debug information
      Returns:
      IR instructions implementing the question operator
    • generateGuardedAssignment

      public List<IRInstr> generateGuardedAssignment(ISymbol lhsSymbol, List<IRInstr> assignmentEvaluation, String assignmentResult, DebugInfo debugInfo)
      Generate SWITCH_CHAIN_BLOCK for guarded assignment (:=?). Composes question operator logic: assign only if LHS is null OR !LHS._isSet().
    • generateGuardExpressionAssignment

      public List<IRInstr> generateGuardExpressionAssignment(String isSetBoolean, ISymbol identifierSymbol, String exprTemp, DebugInfo debugInfo)
      Generate CONTROL_FLOW_CHAIN for guard expression assignment.

      Conditionally assigns exprTemp to identifier if isSetBoolean is true. Uses GUARDED_ASSIGNMENT chain type where the condition is the isSet Boolean directly.

      Parameters:
      isSetBoolean - The Boolean variable holding the isSet result of the expression
      identifierSymbol - The target identifier symbol to assign to
      exprTemp - The temp variable holding the expression result
      debugInfo - Debug information
      Returns:
      IR instructions implementing the conditional assignment
    • generateExternalFieldGuardedAssignment

      public List<IRInstr> generateExternalFieldGuardedAssignment(String isSetBoolean, List<IRInstr> assignmentBody, DebugInfo debugInfo)
      Generate CONTROL_FLOW_CHAIN for guarded assignment on external field.

      Inverts the isSetBoolean (assign when NOT set) and wraps the assignment body in a GUARDED_ASSIGNMENT control flow chain.

      Parameters:
      isSetBoolean - The Boolean variable holding the isSet result of the field's current value
      assignmentBody - The IR instructions to execute if field is unset (evaluate RHS + STORE_FIELD)
      debugInfo - Debug information
      Returns:
      IR instructions implementing the conditional field assignment
    • generateNullCoalescing

      public List<IRInstr> generateNullCoalescing(ExprProcessingDetails lhsDetails, ExprProcessingDetails rhsDetails)
      Generate CONTROL_FLOW_CHAIN for Null Coalescing operator (??). Returns LHS if memory allocated (even if unset), else evaluates and returns RHS.

      IR Structure: 1 case + default
      Case 1: NULL_CHECK - if LHS is null → evaluate and return RHS
      Default: LHS not null → return LHS (even if unset)

    • generateElvisCoalescing

      public List<IRInstr> generateElvisCoalescing(ExprProcessingDetails lhsDetails, ExprProcessingDetails rhsDetails)
      Generate CONTROL_FLOW_CHAIN for Elvis Coalescing operator (:?). Returns LHS if memory allocated AND set, else evaluates and returns RHS.

      IR Structure: 2 cases + default
      Case 1: NULL_CHECK - if LHS is null → evaluate and return RHS
      Case 2: IS_SET check - if LHS is not set → evaluate and return RHS
      Default: LHS is set → return LHS

      Safety: NULL_CHECK always precedes _isSet() call to prevent accessing unallocated memory.

    • generateComparisonCoalescing

      public List<IRInstr> generateComparisonCoalescing(ExprProcessingDetails lhsDetails, ExprProcessingDetails rhsDetails, String comparisonOperator, String chainType)
      Generate CONTROL_FLOW_CHAIN for Comparison Coalescing operators (<?, >?, <=?, >=?). Returns LHS if both operands valid and comparison true, else gracefully handles null/unset.

      IR Structure: 6 cases + default
      Case 1: NULL_CHECK on LHS → if null, return RHS
      Case 2: IS_SET check on LHS → if not set, return RHS
      Case 3: NULL_CHECK on RHS → if null, return LHS
      Case 4: IS_SET check on RHS → if not set, return LHS
      Case 5: COMPARISON (LHS comp RHS) → if true, return LHS
      Default: return RHS

      Parameters:
      comparisonOperator - The comparison operator method name (e.g., "_lt", "_gt", "_lteq", "_gteq")
      chainType - The chain type for IR (e.g., "LESS_THAN_COALESCING_OPERATOR")
    • generateTernary

      public List<IRInstr> generateTernary(ExprProcessingDetails details)
      Generate IR for ternary operator: condition <- thenValue : elseValue.

      Pattern: CONTROL_FLOW_CHAIN with TERNARY_OPERATOR chain type - Condition evaluation in tight scope - True branch evaluates thenValue - False branch (default) evaluates elseValue

      Parameters:
      details - ExprProcessingDetails containing result variable and ternary expression context
      Returns:
      List of IR instructions for ternary operator