//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// 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:36 2002 by hak
 * @version          modified on Wed Jul 24 12:18:57 2002 by pviry
 * @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 java.util.Iterator;

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

public class Local extends Expression
{
  private Parameter _parameter;
  private int _offset = -1;

  public Local (Parameter parameter)
    {
      _parameter = parameter;
    }

  public Local (Parameter parameter, Type type)
    {
      _parameter = parameter;
      setType(type);
    }

  public final Parameter parameter ()
    {
      return _parameter;
    }

  public final int offset ()
    {
      return _offset;
    }

  public final void setOffset (int offset)
    {
      _offset = offset;
    }

  public final void incOffset ()
    {
      _offset++;
    }

  public final String name ()
    {
      return _parameter.name();
    }

  /**
   * Returns the type reference of this occurrence.
   */
  public final Type typeRef ()
    {
      return _parameter.typeRef();
    }

  /**
   * Returns the current type binding of this occurrence.
   */
  public final Type type ()
    {
      return _parameter.type();
    }

  public final void setType (Type type)
    {
      _parameter.addType(type);
    }

  /**
   * Returns the checked type of this occurrence.
   */
  public final Type checkedType ()
    {
      return _parameter.checkedType();
    }

  /**
   * No-op...
   */
  public final void setCheckedType ()
    {
      if (isSetCheckedType()) return;
    }

  /**
   * No-op...
   */
  public final void setCheckedType (Type type)
    {
    }

  /**
   * This is a no-op since a Local is already sanitized.
   */
  public final Expression sanitizeNames (ParameterStack parameters, ClassTypeHandle handle)
    {
      return this;
    }

  /**
   * Sets this Local's offset and its defining abstraction's depth
   * of frame according to its sort.
   */
  public final void sanitizeSorts (Enclosure enclosure)
    {
      enclosure.setLocalInfo(this);
    }

  /**
   * Ascertains that the type of this is that of the variable it stands for.
   */
  public final void typeCheck (TypeChecker typeChecker) throws TypingErrorException
    {
      if (typeCheckLocked()) return;

      if (_otherTypes != null)
        for (Iterator i=_otherTypes.iterator(); i.hasNext();)
          typeChecker.unify(type(),(Type)i.next(),this);
    }

  public final Expression shiftOffsets (int intShift, int realShift, int objectShift,
                                        int intDepth, int realDepth, int objectDepth)
    {
      switch (boxSort())
        {
        case Type.INT_SORT:
          if (_offset >= intDepth) _offset += intShift;
          break;
        case Type.REAL_SORT:
          if (_offset >= realDepth) _offset += realShift;
          break;
        default:
          if (_offset >= objectDepth) _offset += objectShift;
          break;
        }

      return this;
    }

  public final void compile (Compiler compiler)
    {   
      switch (boxSort())
        {
        case Type.INT_SORT:
          compiler.generate(new PushOffsetInt(_offset));
          return;
        case Type.REAL_SORT:
          compiler.generate(new PushOffsetReal(_offset));
          return;
        default:
          compiler.generate(new PushOffsetObject(_offset));
        }
    }

  public final String toString ()
    {
      String s = name();

      if (s != null) return s;
      if (_offset == -1) return "?";
      return "#"+_offset;
    }

  // Added by PV
  public final String toTypedString ()
    {
        return name() + (checkedType() == null ? "" : (" : " + checkedType()));
    }
}
