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

package ilog.language.design.kernel;

/**
 * @version     Last modified on Tue Jul 23 23:02:20 2002 by paulin
 * @version          modified on Wed Jul 03 17:05:59 2002 by hak
 * @version          modified on Wed Jun 12 19:44:28 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.*;

/**
 * This class is the class of local assignment expressions.
 */
public class LocalAssignment extends Assignment
{
  public LocalAssignment (Expression lhs, Expression rhs)
    {
      _lhs = lhs;
      _rhs = rhs; 
    }

  /**
   * Sanitizes the names of all expressions in this assignment.
   */
  public final Expression sanitizeNames (ParameterStack parameters, ClassTypeHandle handle)
    {
      _lhs = _lhs.sanitizeNames(parameters, handle);
      _rhs = _rhs.sanitizeNames(parameters, handle);

      if (!(_lhs instanceof Local))
        throw locate(new AssignmentErrorException("unassignable location: "+_lhs));

      return this;
    }

  /**
   * Compiles this local assignment in the context of the specified
   * <a href="./Compiler.html"><tt>Compiler</tt></a>.
   */
  public final void compile (Compiler compiler)
    {
      _rhs.compile(compiler);
      
      switch (_lhs.boxSort())
        {
        case Type.INT_SORT:
          if (_rhs.checkedType().isBoxedType())
            compiler.generateUnwrapper(Type.INT_SORT);
          compiler.generate(new SetOffsetInt(((Local)_lhs).offset()));
          return;
        case Type.REAL_SORT:
          if (_rhs.checkedType().isBoxedType())
            compiler.generateUnwrapper(Type.REAL_SORT);
          compiler.generate(new SetOffsetReal(((Local)_lhs).offset()));
          return;
        default:
          if (!_rhs.checkedType().isBoxedType())
            compiler.generateWrapper(_rhs.sort());
          compiler.generate(new SetOffsetObject(((Local)_lhs).offset()));
          return;
        }
    }
}
