Class CollectionElementPromoter
java.lang.Object
org.ek9lang.compiler.phase7.support.CollectionElementPromoter
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionpromoteIfRequired(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).promoteIfRequired(ISymbol elementType, ISymbol targetType, String elementTemp, DebugInfo debugInfo, List<IRInstr> setupInstructions, boolean escaping) AspromoteIfRequired(ISymbol, ISymbol, String, DebugInfo, List), butescapingselects the ARC of any inserted promote:falsefor a scope-local value (collection element, range bound, comparison operand) — RETAIN + SCOPE_REGISTER;truefor a value that ESCAPES the current scope (a coalescing/ternary body-result that becomes the expression value) — RETAIN only, the consumer takes ownership.
-
Constructor Details
-
CollectionElementPromoter
-
-
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 tosetupInstructions.- Parameters:
elementType- static type of the element being addedtargetType- resolved element/key/value type of the collection, ornullwhen the collection is parameterized onAny(no promotion possible/needed)elementTemp- temp variable holding the evaluated elementdebugInfo- debug info for any generated promotion instructionssetupInstructions- 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) AspromoteIfRequired(ISymbol, ISymbol, String, DebugInfo, List), butescapingselects the ARC of any inserted promote:falsefor a scope-local value (collection element, range bound, comparison operand) — RETAIN + SCOPE_REGISTER;truefor 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 andescapingis irrelevant.
-