/**
  *
  *                      OOAS Compiler
  *
  *       Copyright 2015, AIT Austrian Institute of Technology.
  * This code is based on the C# Version of the OOAS Compiler, which is
  * copyright 2015 by the Institute of Software Technology, Graz University
  * of Technology with portions copyright by the AIT Austrian Institute of
  * Technology. All rights reserved.
  *
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
  *
  * If you modify the file please update the list of contributors below to in-
  * clude your name. Please also stick to the coding convention of using TABs
  * to do the basic (block-level) indentation and spaces for anything after
  * that. (Enable the display of special chars and it should be pretty obvious
  * what this means.) Also, remove all trailing whitespace.
  *
  * Contributors:
  *               Willibald Krenn (AIT)
  *               Stephan Zimmerer (AIT)
  *               Markus Demetz (AIT)
  *               Christoph Czurda (AIT)
  *
  */


package org.momut.ooas.ast.expressions;

import org.momut.ooas.ast.IAstVisitor;
import org.momut.ooas.ast.types.Type;
import org.momut.ooas.utils.exceptions.ArgumentException;


///////////////////////////////////////////////
///  === Unary Operators ===
///
public class UnaryOperator extends Expression
{
	private Expression m_child;

	public Expression child() { return m_child; }

	public UnaryOperator(ExpressionKind aKind, Expression child, int line, int pos)
	{
		super (aKind, line, pos);
		m_child = child;
	}

	public UnaryOperator(UnaryOperator toCopy)
	{
		super (toCopy);
		m_child = toCopy.m_child;
	}

	@Override
	public /*override*/ Expression Clone()
	{
		return new UnaryOperator(this);
	}

	public void SetChild(Expression child)
	{
		m_child = child;
	}

	@Override
	public /*override*/ void Accept(IAstVisitor visitor)
	{
		visitor.visit(this);
	}

	public static Expression CoerceUp(Expression anExpr, Type target)
	{
		if (target == null)
			throw new ArgumentException();

		if (anExpr.kind() == ExpressionKind.Value
				&& anExpr.type().kind() == target.kind()
				&& Type.TypeEqual(Type.CoverType(anExpr.type(), target),target))
		{
			// if we are widening the range of a type assigned to a value, then just set the
			// new type and skip constructing a new node.
			anExpr.SetType(target);
			return anExpr;
		}
		else
		{
			final UnaryOperator result = new UnaryOperator(ExpressionKind.Cast, anExpr, anExpr.line(), anExpr.pos());
			result.SetType(target);
			return result;
		}
	}

	public static Expression TryCoerceUp(Expression anExpr, Type target)
	{
		if (!Type.TypeEqual(anExpr.type(), target))
		{
			return CoerceUp(anExpr, target);
		}
		else
			return anExpr;
	}

}

