Class AbstractSyntheticGenerator

java.lang.Object
org.ek9lang.compiler.phase7.synthesis.AbstractSyntheticGenerator
Direct Known Subclasses:
CompareGenerator, CopyGenerator, DerivedComparisonGenerator, EqualsGenerator, FieldSetStatusGenerator, HashCodeGenerator, IsSetGenerator, NotEqualsGenerator, ToStringGenerator

public abstract class AbstractSyntheticGenerator extends Object
Base class for synthetic operator generators.

Provides common patterns and utilities used by all synthetic generators:

  • IsSet guard generation for this and parameters
  • Unset return block generation
  • Field iteration utilities
  • Memory management (RETAIN/SCOPE_REGISTER) patterns

All generated IR follows the EK9 tri-state semantics where operations return unset if any operand is unset.

  • Field Details

  • Constructor Details

    • AbstractSyntheticGenerator

      protected AbstractSyntheticGenerator(IRGenerationContext stackContext)
      Create a new synthetic generator with the given context.
      Parameters:
      stackContext - The IR generation context
  • Method Details

    • getTypeName

      protected String getTypeName(ISymbol symbol)
      Get the fully qualified name for a type.
      Parameters:
      symbol - The symbol to get the type name for
      Returns:
      The fully qualified type name
    • getBooleanTypeName

      protected String getBooleanTypeName()
      Get the Boolean type name from the EK9 type system.
    • getIntegerTypeName

      protected String getIntegerTypeName()
      Get the Integer type name from the EK9 type system.
    • getStringTypeName

      protected String getStringTypeName()
      Get the String type name from the EK9 type system.
    • getVoidTypeName

      protected String getVoidTypeName()
      Get the Void type name from the EK9 type system.
    • generateTempName

      protected String generateTempName()
      Generate a temporary variable name.
    • generateLabelName

      protected String generateLabelName(String prefix)
      Generate a label name with the given prefix.
    • createDebugInfo

      protected DebugInfo createDebugInfo(ISymbol symbol)
      Create debug info from a symbol's source token.
    • generateThisIsSetGuard

      protected List<IRInstr> generateThisIsSetGuard(String aggregateTypeName, DebugInfo debugInfo, String unsetLabel, String scopeId)
      Generate isSet guard check for 'this' with branch to unset return.

      Pattern:

        _temp = CALL this._isSet() -> Boolean
        RETAIN _temp
        SCOPE_REGISTER _temp, scope_id
        _temp_val = UNBOX _temp -> boolean
        BRANCH_IF_FALSE _temp_val -> unset_label
      
      Parameters:
      aggregateTypeName - The fully qualified type name of the aggregate
      debugInfo - Debug information for the instructions
      unsetLabel - Label to branch to if unset
      scopeId - Current scope ID for memory management
      Returns:
      List of IR instructions for the guard
    • generateIsSetGuard

      protected List<IRInstr> generateIsSetGuard(String variableName, String typeName, DebugInfo debugInfo, String unsetLabel, String scopeId)
      Generate isSet guard check for a variable with branch to unset return.

      The check generates:

        1. _temp = CALL variable._isSet() -> Boolean
        2. RETAIN/SCOPE_REGISTER _temp
        3. _tempBool = CALL _temp._true() -> boolean (primitive)
        4. RETAIN/SCOPE_REGISTER _tempBool
        5. BRANCH_FALSE _tempBool, unsetLabel
      
      Parameters:
      variableName - The variable to check
      typeName - The fully qualified type name of the variable
      debugInfo - Debug information for the instructions
      unsetLabel - Label to branch to if unset
      scopeId - Current scope ID for memory management
      Returns:
      List of IR instructions for the guard
    • generateUnsetReturnBlock

      protected List<IRInstr> generateUnsetReturnBlock(String returnTypeName, String returnVarName, DebugInfo debugInfo, String scopeId)
      Generate the return_unset basic block that returns an unset value.

      Pattern for Boolean return:

        return_unset:
          _result = CALL Boolean._new() -> Boolean
          RETAIN _result
          STORE rtn = _result
          SCOPE_EXIT scope_id
          RETURN rtn
      
      Parameters:
      returnTypeName - The type of value to return (e.g., Boolean, Integer)
      returnVarName - The name of the return variable (typically "rtn")
      debugInfo - Debug information for the instructions
      scopeId - Current scope ID for cleanup
      Returns:
      List of IR instructions for the unset return block
    • generateBooleanReturnBlock

      protected List<IRInstr> generateBooleanReturnBlock(boolean value, String returnVarName, DebugInfo debugInfo, String scopeId)
      Generate a return block that returns a specific boolean value (true or false).
      Parameters:
      value - The boolean value to return (true or false)
      returnVarName - The name of the return variable
      debugInfo - Debug information
      scopeId - Current scope ID
      Returns:
      List of IR instructions
    • getSyntheticFields

      protected List<ISymbol> getSyntheticFields(AggregateSymbol aggregateSymbol)
      Get all fields from an aggregate that should be included in synthetic operations.

      This returns only the direct properties of the aggregate, not inherited ones. Inherited fields are handled by calling super's synthetic operators.

      Parameters:
      aggregateSymbol - The aggregate to get fields from
      Returns:
      List of field symbols
    • superHasOperator

      protected boolean superHasOperator(AggregateSymbol aggregateSymbol, String operatorName)
      Check if the super aggregate has a specific operator defined.
      Parameters:
      aggregateSymbol - The aggregate whose super we're checking
      operatorName - The operator name (e.g., "==", "<=>")
      Returns:
      true if super has the operator, false otherwise
    • isAnyType

      protected boolean isAnyType(IAggregateSymbol type)
      Check if the given type is the Any type.
    • getSuperTypeName

      protected String getSuperTypeName(AggregateSymbol aggregateSymbol)
      Get the fully qualified name of the super aggregate.
      Parameters:
      aggregateSymbol - The aggregate to get super from
      Returns:
      The super type name, or empty string if no super (other than Any)
    • generateFieldLoad

      protected List<IRInstr> generateFieldLoad(String targetVar, String objectVar, String fieldName, DebugInfo debugInfo, String scopeId)
      Generate field load instruction with memory management.

      Pattern:

        _temp = LOAD this.fieldName -> FieldType
        RETAIN _temp
        SCOPE_REGISTER _temp, scope_id
      
      Parameters:
      targetVar - The variable to store the loaded value
      objectVar - The object to load from (typically "this" or "other")
      fieldName - The field name to load
      debugInfo - Debug information
      scopeId - Current scope ID
      Returns:
      List of IR instructions
    • generateConstructorCall

      protected List<IRInstr> generateConstructorCall(String resultVar, String typeName, DebugInfo debugInfo, String scopeId)
      Generate a constructor call to create a new instance.

      Pattern:

        _result = CALL (Type)Type.<init>() [pure=true, complexity=1, effects=RETURN_MUTATION]
        RETAIN _result
        SCOPE_REGISTER _result, scope_id
      
      Parameters:
      resultVar - Variable to store result
      typeName - Fully qualified type name to construct
      debugInfo - Debug information
      scopeId - Current scope ID
      Returns:
      List of IR instructions
    • generateMethodCall

      protected List<IRInstr> generateMethodCall(String resultVar, String targetVar, String targetType, String methodName, List<String> arguments, List<String> parameterTypes, String returnType, DebugInfo debugInfo, String scopeId)
      Generate a method call on a variable with result storage and memory management.
      Parameters:
      resultVar - Variable to store result
      targetVar - Variable to call method on (null for static calls)
      targetType - Fully qualified type name of the target
      methodName - Method to call
      arguments - Method argument variable names
      parameterTypes - Parameter types (must match arguments)
      returnType - Return type of method
      debugInfo - Debug information
      scopeId - Current scope ID
      Returns:
      List of IR instructions