//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// 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:01 2002 by hak
 * @version          modified on Mon Sep 30 16:43:30 2002 by paulin
 * @version          modified on Wed Jul 24 12:19:25 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 class is the class of sequences of expressions.
 *
 */
public class Sequence extends Expression
{
  /**
   * The expressions in this sequence.
   */
  protected Expression[] _expressions;

  /**
   * Constructs a sequence with the specified list of expressions.
   */
  public Sequence (ArrayList expressions)
    {
      _expressions = new Expression[expressions.size()];

      for (int i=0; i<_expressions.length; i++)
        _expressions[i] = (Expression)expressions.get(i);
    }

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

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

  /**
   * Sanitizes the names of all expressions in the sequence.
   */
  public final Expression sanitizeNames (ParameterStack parameters, ClassTypeHandle handle)
    {
      for (int i=0; i<_expressions.length; i++)
        _expressions[i] = _expressions[i].sanitizeNames(parameters, handle);

      return this;
    }

  /**
   * Sanitizes the sorts of all expressions in the sequence.
   */
  public final void sanitizeSorts (Enclosure enclosure)
    {
      for (int i=0; i<_expressions.length; i++)
        _expressions[i].sanitizeSorts(enclosure);
    }

  /**
   * The type of a sequence of expressions is that of the last one.
   */
  public final Type type ()
    {
      return _expressions[_expressions.length-1].type();
    }

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

  /**
   * The type reference of a sequence of expressions is that of the last one.
   */
  public final Type typeRef ()
    {
      return _expressions[_expressions.length-1].typeRef();
    }

  /**
   * The checked type of a sequence of expressions is that of the last one.
   */
  public final Type checkedType ()
    {
      return _expressions[_expressions.length-1].checkedType();
    }

  /**
   * Sets the checked Type of all expressions of this sequence.
   */
  public final void setCheckedType ()
    {
      if (isSetCheckedType()) return;
      for (int i=0; i<_expressions.length; i++)
        _expressions[i].setCheckedType();
    }

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

  /**
   * Type-checks all expressions in this sequence 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;

      for (int i=0; i<_expressions.length; i++)
        _expressions[i].typeCheck(typeChecker);
    }
    
  /**
   * Shifts the offsets of all the expressions in this sequence.
   */
  public final Expression shiftOffsets (int intShift, int realShift, int objectShift,
                                        int intDepth, int realDepth, int objectDepth)
    {      
      for (int i=0; i<_expressions.length; i++)
        _expressions[i] = _expressions[i].shiftOffsets(intShift,realShift,objectShift,
                                                       intDepth,realDepth,objectDepth);

      return this;
    }

  /**
   * Compiles this sequence of expressions in the context of the specified
   * <a href="./Compiler.html"><tt>Compiler</tt></a>.
   */
  public void compile (Compiler compiler)
    {
      for (int i=0; i<_expressions.length; i++)
        {
          _expressions[i].compile(compiler);
          if (i<_expressions.length-1)
            compiler.generateStackPop(_expressions[i].boxSort());
        }
    }
    
  public final String toString ()
    {
      StringBuffer buf = new StringBuffer("{ ");

      for (int i=0; i<_expressions.length; i++)
        buf.append(_expressions[i]+"; ");

      buf.append("}");

      return buf.toString();
    }

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

      for (int i=0; i<_expressions.length; i++)
        buf.append(_expressions[i].toTypedString()+"; ");

      buf.append("}");

      return typed(buf.toString());
    }
}


