Class ToStringGenerator

java.lang.Object
org.ek9lang.compiler.phase7.synthesis.AbstractSyntheticGenerator
org.ek9lang.compiler.phase7.synthesis.ToStringGenerator

final class ToStringGenerator extends AbstractSyntheticGenerator
Generates synthetic IR for the _string ($) operator.

The generated code follows this pattern:

String _string():
  // Guard: if no field (own or super) is set, return UNSET
  if this is empty -> return unset String

  result = "ClassName("

  // Super FIRST (base-to-derived), via super.$() - honours a user-defined super $:
  if super has $:
    result = result + super.$()        // nested: an unset super propagates (copy-left)

  // Then each own field, unconditionally (no isSet guard):
  for each field:
    [result = result + ", " if not the first element]
    result = result + "fieldName="
    result = result + field.$()        // unset field -> result becomes unset (copy-left)

  result = result + ")"
  return result

Key semantic requirements:

  • Renders inherited fields by calling super.$() (NOT by reading super's fields): a user-defined super $ may render more than just fields and must be honoured, so the super string is treated as opaque and nested - Cat(Animal(name=Felix), breed=Siamese)
  • Copy-left String join: String._add is set only when BOTH operands are set, so any unset field (or unset super) propagates and the WHOLE $ becomes unset - the string twin of the AND-model ? operator. There is no field=? placeholder.
  • Format: "ClassName(field1=value1, field2=value2)" (simple class name, not fully qualified)
  • Constructor Details

  • Method Details

    • generate

      List<IRInstr> generate(MethodSymbol operatorSymbol, AggregateSymbol aggregateSymbol)
      Generate the _string operator IR for the given aggregate.
      Parameters:
      operatorSymbol - The _string operator symbol
      aggregateSymbol - The aggregate containing the operator
      Returns:
      List of IR instructions implementing the operator