CastExpression.java

package edu.udel.cis.vsl.tass.model.impl.expression;

import edu.udel.cis.vsl.tass.model.IF.ModelFactoryIF;
import edu.udel.cis.vsl.tass.model.IF.SyntaxException;
import edu.udel.cis.vsl.tass.model.IF.type.TypeIF;
import edu.udel.cis.vsl.tass.model.IF.type.TypeIF.TypeKind;

public class CastExpression extends UnaryExpression {

	public CastExpression(ModelFactoryIF factory, TypeIF newType,
			Expression expression) throws SyntaxException {
		super(factory, ExpressionKind.CAST, expression);

		TypeIF oldType = expression.type();
		TypeKind oldTypeKind = oldType.kind();
		TypeKind newTypeKind = newType.kind();

		assert newType != null;
		if (oldTypeKind == TypeKind.POINTER && newTypeKind == TypeKind.POINTER) {
			// OK, casting between any two pointer types is statically OK
		} else if (oldType.kind() == TypeKind.INTEGER
				&& newType.kind() == TypeKind.RATIONAL) {
			// OK, casting from int to a real type is always OK
		} else {
			throw new SyntaxException(expression,
					"TASS does not support casts from " + oldType + " to "
							+ newType);
		}
		type = newType;
	}

	public String toString() {
		return "(" + type + ")" + expression.atomString();

	}

	public String atomString() {
		return toString();
	}

}