Class ControlFlowChainGenerator
- All Implemented Interfaces:
Function<ControlFlowChainDetails, List<IRInstr>>
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.
-
Field Summary
Fields inherited from class AbstractGenerator
debugInfoCreator, instructionBuilder, stackContext, typeNameOrException -
Constructor Summary
ConstructorsConstructorDescriptionControlFlowChainGenerator(IRGenerationContext stackContext, VariableMemoryManagement variableMemoryManagement, Function<ExprProcessingDetails, List<IRInstr>> rawExprProcessor) -
Method Summary
Modifier and TypeMethodDescriptionapply(ControlFlowChainDetails details) generateComparisonCoalescing(ExprProcessingDetails lhsDetails, ExprProcessingDetails rhsDetails, String comparisonOperator, String chainType) Generate CONTROL_FLOW_CHAIN for Comparison Coalescing operators (<?, >?, <=?, >=?).generateElvisCoalescing(ExprProcessingDetails lhsDetails, ExprProcessingDetails rhsDetails) Generate CONTROL_FLOW_CHAIN for Elvis Coalescing operator (:?).generateGuardedAssignment(ISymbol lhsSymbol, List<IRInstr> assignmentEvaluation, String assignmentResult, DebugInfo debugInfo) Generate SWITCH_CHAIN_BLOCK for guarded assignment (:=?).generateNullCoalescing(ExprProcessingDetails lhsDetails, ExprProcessingDetails rhsDetails) Generate CONTROL_FLOW_CHAIN for Null Coalescing operator (??).generateQuestionOperator(ExprProcessingDetails exprDetails) Generate SWITCH_CHAIN_BLOCK for Question operator (?).generateQuestionOperatorForVariable(ISymbol variableSymbol, String resultVariable, DebugInfo debugInfo) Generate SWITCH_CHAIN_BLOCK for Question operator applied to a variable.Methods inherited from class AbstractGenerator
createTempVariable, createTempVariableFromContext, extractReturnType, getRecordedSymbolOrException, processBlockStatements, validateNoPreFlowStatement, validateStatementFormOnly
-
Constructor Details
-
ControlFlowChainGenerator
public ControlFlowChainGenerator(IRGenerationContext stackContext, VariableMemoryManagement variableMemoryManagement, Function<ExprProcessingDetails, List<IRInstr>> rawExprProcessor)
-
-
Method Details
-
apply
- Specified by:
applyin interfaceFunction<ControlFlowChainDetails, List<IRInstr>>
-
generateQuestionOperator
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
-
generateGuardedAssignment
-
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")
-