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

package ilog.language.design.kernel;

/**
 * @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 mother class of assignment expressions.
 */
public abstract class Assignment extends Expression
{
  /**
   * The location being set.
   */
  protected Expression _lhs;

  /**
   * The expression whose value is assigned to the location being set.
   */
  protected Expression _rhs;

  public final int numberOfSubexpressions ()
    {
      return 2;
    }

  public final Expression subexpression (int n) throws NoSuchSubexpressionException
    {
      switch (n)
        {
        case 0:
          return _lhs;
        case 1:
          return _rhs;
        }

      throw new NoSuchSubexpressionException(this,n);
    }

  /**
   * Sanitizes the sorts of all expressions in the assignment.
   */
  public final void sanitizeSorts (Enclosure enclosure)
    {
      _lhs.sanitizeSorts(enclosure);
      _rhs.sanitizeSorts(enclosure);
    }

  /**
   * The type of an assignment is that of the rhs.
   */
  public final Type type ()
    {
      return _rhs.type();
    }

  /**
   * Sets the type to the specified type.
   */
  public final void setType (Type type)
    {
      _lhs.setType(type);
      _rhs.setType(type);
    }

  /**
   * The type reference of an assignment is that of the rhs.
   */
  public final Type typeRef ()
    {
      return _rhs.typeRef();
    }

  /**
   * The checked type of an assignment is that of the rhs.
   */
  public final Type checkedType ()
    {
      return _rhs.checkedType();
    }

  /**
   * Sets the checked type of this assignment.
   */
  public final void setCheckedType ()
    {
      if (isSetCheckedType()) return;
      _lhs.setCheckedType();
      _rhs.setCheckedType();
    }

  /**
   * Sets the checked type of this assignment to the given type.
   */
  public final void setCheckedType (Type type)
    {
      _lhs.setCheckedType(type);
      _rhs.setCheckedType(type);
    }

  /**
   * Type-checks this assignment in the context of the specified
   * <a href="./TypeChecker.html"> <tt>TypeChecker</tt></a>.
   */
  public final void typeCheck (TypeChecker typeChecker) throws TypingErrorException
    {
      if (typeCheckLocked()) return;

      _rhs.typeCheck(typeChecker);
      _lhs.typeCheck(_rhs.typeRef(),typeChecker);
    }

  /**
   * Shifts the offsets of all the expressions in this localAssignment.
   */
  public final Expression shiftOffsets (int intShift, int realShift, int objectShift,
                                        int intDepth, int realDepth, int objectDepth)
    {      
      _lhs = _lhs.shiftOffsets(intShift,realShift,objectShift,
                               intDepth,realDepth,objectDepth);

      _rhs = _rhs.shiftOffsets(intShift,realShift,objectShift,
                               intDepth,realDepth,objectDepth);

      return this;
    }

  public final String toString ()
    {
      return _lhs + " = " + _rhs;
    }

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