ASTAllocateStatement.java

package edu.udel.cis.vsl.tass.front.minimp.ast.statement;

import edu.udel.cis.vsl.tass.front.minimp.ast.expression.ASTExpressionIF;
import edu.udel.cis.vsl.tass.front.minimp.ast.type.ASTIntegerType;
import edu.udel.cis.vsl.tass.front.minimp.ast.type.ASTPointerType;
import edu.udel.cis.vsl.tass.front.minimp.ast.type.ASTTypeIF;
import edu.udel.cis.vsl.tass.model.IF.SyntaxException;

public class ASTAllocateStatement extends ASTStatement {
	private ASTExpressionIF buffer;
	private ASTTypeIF elementType;
	private ASTExpressionIF elementCount;

	public ASTAllocateStatement(ASTExpressionIF buffer, ASTTypeIF elementType,
			ASTExpressionIF elementCount) throws SyntaxException {
		if (buffer == null) {
			throw new NullPointerException("Null buffer in allocate statement.");
		}
		if (elementType == null) {
			throw new NullPointerException(
					"Null element type in allocate statement.");
		}
		if (elementCount == null) {
			throw new NullPointerException(
					"Null number of elements in allocate statement.");
		}

		// Buffer must have pointer type.
		if (!(buffer.getType() instanceof ASTPointerType)) {
			throw new SyntaxException(buffer.getSource(),
					"The buffer in an allocate statement must have pointer type instead of "
							+ buffer.getType());
		}

		// Buffer must be compatible with element type.
		ASTTypeIF baseType = ((ASTPointerType) (buffer.getType()))
				.getBaseType();
		if (!(baseType.equals(elementType))) {
			throw new SyntaxException(buffer.getSource(),
					"The element type and the type the buffer can store is not equal: "
							+ baseType + " and " + elementType);
		}

		// Element count must have integer type.
		if (!(elementCount.getType() instanceof ASTIntegerType)) {
			throw new SyntaxException(buffer.getSource(),
					"The number of elements in an allocate statement must have integer type "
							+ "instead of " + elementCount.getType());
		}

		this.buffer = buffer;
		this.elementType = elementType;
		this.elementCount = elementCount;
	}

	public ASTExpressionIF getBuffer() {
		return this.buffer;
	}

	public ASTTypeIF getElementType() {
		return this.elementType;
	}

	public ASTExpressionIF getElementCount() {
		return this.elementCount;
	}

	public String toString() {
		String result = "allocate(";
		result += this.buffer + ", " + this.elementType + ", "
				+ this.elementCount + ")";
		return result;
	}
}