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

package ilog.language.design.kernel;

/**
 * @version     Last modified on Fri Oct 18 17:52:03 2002 by hak
 * @version          modified on Wed Jul 24 12:19:29 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 ilog.language.design.types.*;
import ilog.language.design.base.*;

import java.util.ArrayList;

/**
 * This is the class of tuple expressions whose components are identified by position.
 */
public class Tuple extends ProtoExpression
{
  protected Expression[] _components;

  protected Tuple ()
    {
    }

  public final static Tuple EMPTY = new Tuple();

  private Tuple (Expression[] components)
    {
      _components = components;
    }

  public final static Tuple newTuple (ArrayList components)
    {
      if (components == null || components.size() == 0)
        return EMPTY;

      Expression[] expressions = new Expression[components.size()];
      for (int i=expressions.length; i-->0;)
        expressions[i] = (Expression)components.get(i);

      return new Tuple(expressions);
    }

  public final int numberOfSubexpressions ()
    {
      return _components.length;
    }

  public final Expression subexpression (int n) throws NoSuchSubexpressionException
    {
      try
        {
          return _components[n];
        }
      catch (ArrayIndexOutOfBoundsException e)
        {
          throw new NoSuchSubexpressionException(this,n);
        }
    }

  public final int dimension ()
    {
      return _components == null ? 0 : _components.length;
    }

  public final Expression component (int i)
    {
      return _components[i];
    }

  public final Expression sanitizeNames (ParameterStack parameters, ClassTypeHandle handle)
    {
      for (int i=dimension(); i-->0;)
        _components[i] = _components[i].sanitizeNames(parameters, handle);
      return this;
    }

  public final void sanitizeSorts (Enclosure enclosure)
    {
      for (int i=dimension(); i-->0;)
        _components[i].sanitizeSorts(enclosure);
    }

  public final Expression shiftOffsets (int intShift, int realShift, int objectShift,
                                        int intDepth, int realDepth, int objectDepth)
    {
      for (int i=dimension(); i-->0;)
        _components[i] = _components[i].shiftOffsets(intShift,realShift,objectShift,
                                                     intDepth,realDepth,objectDepth);
      return this;
    }

  public final void setCheckedType ()
    {
      if (isSetCheckedType()) return;
      for (int i=dimension(); i-->0;)
        _components[i].setCheckedType();
      setCheckedType(type().copy());
    }

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

      if (dimension() != 0)
        {
          Type[] componentTypes = new Type[dimension()];

          for (int i=dimension(); i-->0;)
            _components[i].typeCheck(componentTypes[i] = new TypeParameter(),typeChecker);

          typeChecker.typeCheck(this,new TupleType(componentTypes));
        }
      else
        typeChecker.typeCheck(this,TupleType.EMPTY);      
    }

  public final void compile (Compiler compiler)
    {
      int intDim = 0, realDim = 0, objectDim = 0;
      TupleType tupleType = (TupleType)checkedType();

      for (int i=dimension(); i-->0;)
        {
          _components[i].compile(compiler);

          switch (tupleType.component(i).boxSort())
            {
            case Type.INT_SORT:
              intDim++;
              continue;
            case Type.REAL_SORT:
              realDim++;
              continue;
            default:
              objectDim++;
            }
        }

      compiler.generate(new PushTuple(intDim,realDim,objectDim));
    }

  public String toString ()
    {
      StringBuffer buf = new StringBuffer("<");

      for (int i=0; i<dimension(); i++)
        buf.append(component(i)+(i==dimension()-1?"":","));

      return buf.append(">").toString();
    }

  // Added by PV
  public String toTypedString ()
    {
      StringBuffer buf = new StringBuffer("<");

      for (int i=0; i<dimension(); i++)
        buf.append(component(i).toTypedString()+(i==dimension()-1?"":","));

      return typed(buf.append(">").toString());
    }
}
