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

package ilog.language.design.kernel;

/**
 * @version     Last modified on Wed Jul 03 17:00:57 2002 by hak
 * @version          modified on Wed Jun 12 19:44:07 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 global assignment expressions.
 */
public class GlobalAssignment extends Assignment
{
  public GlobalAssignment (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 Global))
        throw locate(new AssignmentErrorException("unassignable location: "+_lhs));

      return this;
    }

  /**
   * Compiles this global assignment in the context of the specified
   * <a href="./Compiler.html"><tt>Compiler</tt></a>.
   */
  public final void compile (Compiler compiler)
    {
      Global lhs = (Global)_lhs;
      DefinedEntry entry = lhs.symbol().registerCodeEntry(lhs.checkedType().standardize());
      entry.setInlinable(false);
      _rhs.compile(compiler);
      if (_rhs.checkedType().isBoxedType() && !_lhs.checkedType().isBoxedType())
        compiler.generateUnwrapper(_rhs.boxSort());
      if (!_rhs.checkedType().isBoxedType() && _lhs.checkedType().isBoxedType())
        compiler.generateWrapper(_rhs.boxSort());
      compiler.generate(new SetGlobal(entry));
    }
}
