//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// PLEASE DO NOT EDIT WITHOUT THE EXPLICIT CONSENT OF THE AUTHOR! \\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

package ilog.language.design.kernel;

/**
 * @version     Last modified on Fri Oct 18 17:51:00 2002 by hak
 * @author      <a href="mailto:hak@ilog.fr">Hassan A&iuml;t-Kaci</a>
 * @copyright   &copy; 2000-2002 <a href="http://www.ilog.fr/">ILOG, S.A.</a>
 */

import ilog.language.design.types.*;
import ilog.language.design.base.*;

/**
 * This class is the type of expressions used to exit non-locally from -
 * <i>i.e.</i>, other than "falling off" its end of - an <i>exitable</i>
 * <a href="Abstraction.html"><tt>Abstraction</tt></a>.  The operational
 * semantics is simple: exit the current (<i>latest pushed</i>) exitable
 * closure; in other words exactly like a regular <tt>RETURN</tt> instruction
 * except that the state restored is that of the latest saved
 * <i>exitable</i> state. As for type-checking, the "exited-with"
 * value's type must be the same as with (<i>n.b.</i>, if subtyping,
 * compatible) the return type of the enclosing <i>exitable</i>
 * abstraction.
 */
public class ExitWithValue extends ProtoExpression
{
  private Expression _value;
  private boolean _typeAsValue = true;

  public ExitWithValue (Expression value)
    {
      _value = value;
    }

  public ExitWithValue (Expression value, boolean typeAsValue)
    {
      _value = value;
      _typeAsValue = typeAsValue;
    }

  public final Expression value ()
    {
      return _value;
    }

  public final int numberOfSubexpressions ()
    {
      return 1;
    }

  public final Expression subexpression (int n) throws NoSuchSubexpressionException
    {
      if (n == 0)
        return _value;

      throw new NoSuchSubexpressionException(this,n);
    }

  public final void setCheckedType ()
    {
      if (isSetCheckedType()) return;
      setCheckedType(type().copy());
      _value.setCheckedType();
    }

  public final Expression sanitizeNames (ParameterStack parameters, ClassTypeHandle handle)
    {
      _value = _value.sanitizeNames(parameters, handle);
      return this;
    }

  public final void sanitizeSorts (Enclosure enclosure)
    {
      _value.sanitizeSorts(enclosure);
    }

  public final Expression shiftOffsets (int intShift, int realShift, int objectShift,
                                        int intDepth, int realDepth, int objectDepth)
    {
      _value.shiftOffsets(intShift,realShift,objectShift,
                          intDepth,realDepth,objectDepth);
      return this;
    }

  public final void typeCheck (TypeChecker typeChecker) throws TypingErrorException
    {
      if (typeCheckLocked()) return;

      _value.typeCheck(typeChecker);
      typeChecker.prove(new CheckExitableGoal(_value.type(),this));
      if (_typeAsValue)
        typeChecker.unify(_type,_value.type(),this);
    }

  public final void compile (Compiler compiler)
    {
      _value.compile(compiler);

      switch (_value.checkedType().boxSort())
        {
        case Type.INT_SORT:
          compiler.generate(Instruction.NL_RETURN_I);
          return;
          
        case Type.REAL_SORT:
          compiler.generate(Instruction.NL_RETURN_R);
          return;
          
        default:
          compiler.generate(Instruction.NL_RETURN_O);
          return;
        }
    }

  public final String toString ()
    {
      return "return "+_value;
    }

  // Added by PV
  public final String toTypedString ()
    {
        return typed("return "+_value.toTypedString());
    }
}
