Class CollectionElementPromoter

java.lang.Object
org.ek9lang.compiler.phase7.support.CollectionElementPromoter

public final class CollectionElementPromoter extends Object
THE shared "promote a collection-literal element to the collection's resolved element type" step.

Use this for every collection literal element that may need widening (list elements, dict keys, dict values). Do not re-derive a per-generator variant.

Why this exists: type inference can widen a collection's element type via the #^ promotion operator (e.g. [2.0, 1, 3] infers as List of Float because Integer carries #^ -> Float). When that happens the individual Integer elements must be promoted before they are added, otherwise the raw element type is stored into the collection and the backend emits an add call with the wrong operand type - a JVM VerifyError at load time. This mirrors the argument promotion performed by ParameterPromotionProcessor and reuses the same PromotionCallGenerator.

  • Constructor Details

  • Method Details

    • promoteIfRequired

      public String promoteIfRequired(ISymbol elementType, ISymbol targetType, String elementTemp, DebugInfo debugInfo, List<IRInstr> setupInstructions)
      Insert a #^ promotion for a collection element when its static type is not directly assignable to the collection's resolved element type but is coercible to it (single promotion, per EK9 rules). Any promotion instructions are appended to setupInstructions.
      Parameters:
      elementType - static type of the element being added
      targetType - resolved element/key/value type of the collection, or null when the collection is parameterized on Any (no promotion possible/needed)
      elementTemp - temp variable holding the evaluated element
      debugInfo - debug info for any generated promotion instructions
      setupInstructions - instruction list to append any promotion call to
      Returns:
      the temp variable that should be added to the collection - the promoted temp when a promotion was inserted, otherwise the original elementTemp
    • promoteIfRequired

      public String promoteIfRequired(ISymbol elementType, ISymbol targetType, String elementTemp, DebugInfo debugInfo, List<IRInstr> setupInstructions, boolean escaping)
      As promoteIfRequired(ISymbol, ISymbol, String, DebugInfo, List), but escaping selects the ARC of any inserted promote: false for a scope-local value (collection element, range bound, comparison operand) — RETAIN + SCOPE_REGISTER; true for a value that ESCAPES the current scope (a coalescing/ternary body-result that becomes the expression value) — RETAIN only, the consumer takes ownership. When no promotion is inserted the original temp is returned unchanged and escaping is irrelevant.