Class WrapperGivenAsAny

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

public final class WrapperGivenAsAny extends Object
THE step for an operand declared Any that may be a WRAPPER over a base value - a parameterised generic's wrapper (D60: DictEntry <=> Any) or a constrained type (D74: Age <=> Any). Both are closed, so "is this wrapper" is an EXACT run-time type test. A base's own operator taking Any tests its argument for the BASE type (instanceof Integer), which fails on the wrapper: every such <=>/== answered unset and the runtime sort (which asks _cmp(Any)) kept the values in input order. So the base is given the wrapper's DELEGATE when the argument is exactly the wrapper, and the argument as it is otherwise.

Callers: Scenario2WrapperHelper.castWhenThisWrapper (wrapper equality), Scenario2AggregateWrapperGenerator and ConstrainedTypeDfnGenerator. Use it; never write a third copy.

ARC (Contract 2): the chosen value is an ALIAS held in one variable that is never retained or registered itself - the parameter is borrowed from the caller, and the cast and the delegate are each retained and registered in the given scope on the path that makes them, so nothing is released here and the scope exit releases what was made.

  • Method Details

    • castWhenExactly

      public static String castWhenExactly(List<IRInstr> instructions, Supplier<String> tempNames, String operandName, String wrapperType, String notThisWrapperLabel, String scopeId, DebugInfo debugInfo)
      The test of an operand for being exactly wrapperType, and its cast when it is.
        isWrapper = INSTANCEOF operand, wrapperType [EXACT]
        BRANCH_FALSE isWrapper, notThisWrapperLabel
        casted = CAST operand, wrapperType
        RETAIN casted
        SCOPE_REGISTER casted, scopeId
      
      Returns:
      the variable holding the operand cast to the wrapper (valid only on the fall-through path)
    • delegateWhenExactly

      public static String delegateWhenExactly(List<IRInstr> instructions, Supplier<String> tempNames, String parameterName, String wrapperType, String delegateType, String passAsIsLabel, String scopeId, DebugInfo debugInfo)
      The value to hand a base operator for a parameter declared Any: the wrapper's delegate when the argument is exactly wrapperType, the argument as it is otherwise.
        REFERENCE chosen, Any
        STORE chosen, parameter
        (castWhenExactly ... passAsIsLabel)
        delegate = LOAD_FIELD casted, delegate, wrapperType, delegateType
        RETAIN delegate
        SCOPE_REGISTER delegate, scopeId
        STORE chosen, delegate
        LABEL passAsIsLabel
      
      Returns:
      the variable holding the chosen value