Class CopyGenerator

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

final class CopyGenerator extends AbstractSyntheticGenerator
Generates synthetic IR for the _copy (:=:) operator.

The generated code follows this pattern:

Void _copy(T source):
  [check the source is the same class - an open owner, or an override]
  super._copy(source)   // when the super has a :=:, declared or inherited
  // For each field:
  fieldValue = LOAD source.field
  STORE this.field, fieldValue

  return

Key semantic requirements:

  • Copies ALL fields from source to this
  • This is a mutating operation (modifies this)
  • Returns Void (no return value)
  • Both SET and UNSET field values are copied
  • Super's _copy is called if super is not Any

This is a SHALLOW copy, by design - the copy principle (Steve, 2026-10-07; stated once in docs/language-design/EK9_FORMAL_SPECIFICATION.md, "The Copy Principle"): each target field refers to the SAME object as the source's, as b <- a and passing a parameter do. A built-in collection copies at its own level; a deeper or SELECTIVE copy is user code (a hand-written operator :=: or a domain method), because only the domain knows which fields to duplicate, share or drop. So this generator never recurses and needs no cycle guard (a field-wise variant, fca82f857, was reverted).

The copy is POLYMORPHIC (Q5b, PolymorphicCopy, Contract 20): the owner's own :=:(Owner) and the override of each inherited :=:(Super) copy in full only when the source is an instance of the owner and the source's _copyAccepts(this) agrees - both objects have the same copy owner - and otherwise THROW an EK9 Exception. A closed owner's own copy needs no check (no other class can reach it). This generator also emits _copyAccepts itself (generateCopyAccepts(MethodSymbol, AggregateSymbol)).

  • Constructor Details

  • Method Details

    • generate

      List<IRInstr> generate(MethodSymbol operatorSymbol, AggregateSymbol aggregateSymbol)
      Generate the _copy operator IR for the given aggregate - its own :=:(Owner), or the override of an inherited :=:(Super) signature (PolymorphicCopy, Q5b; told apart by the parameter type).
      Parameters:
      operatorSymbol - The _copy operator method symbol
      aggregateSymbol - The aggregate containing the operator
      Returns:
      List of IR instructions implementing the operator
    • generateCopyAccepts

      List<IRInstr> generateCopyAccepts(MethodSymbol methodSymbol, AggregateSymbol aggregateSymbol)
      Generate the owner's _copyAccepts(target as Any) <- Boolean: is target an instance of this owner (a SUBTYPE test - the owner or any class below it)? Asked of the SOURCE of a copy with the copy's target, so a copy runs in full only between objects whose nearest copy owner is the same class.
        accepted = INSTANCEOF target, Owner [SUBTYPE]
        BRANCH_FALSE accepted, not_accepted
        rtn = Boolean._ofTrue()  ; RETURN rtn
        not_accepted: rtn = Boolean._ofFalse() ; RETURN rtn