ASTVariableDeclaration.java

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

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

public abstract class ASTVariableDeclaration extends ASTDeclaration {
	protected ASTIdentifier variableName;

	protected ASTTypeIF variableType;

	// The TYPEDEF value is only used in TreeParser to identify the typedef
	// declaration. It should not be used anywhere else.
	public enum VariableCategory {
		FORMAL, INPUT, LOCAL, OUTPUT, PROCESS, SHARED, TYPEDEF, BOUND
	};

	protected VariableCategory varCategory;

	protected ASTFunctionDeclaration definitionSource;

	protected ASTIdentifier correspondence = null;

	protected ASTExpressionIF assumption;

	protected ASTExpressionIF initialization;

	protected ASTVariableDeclaration(ASTIdentifier id, ASTTypeIF type,
			VariableCategory category) {
		variableName = id;
		variableType = type;
		varCategory = category;
		definitionSource = null;
	}

	protected ASTVariableDeclaration(ASTIdentifier id, ASTTypeIF type,
			VariableCategory category, ASTExpressionIF init) {
		variableName = id;
		variableType = type;
		varCategory = category;
		this.initialization = init;
		definitionSource = null;
	}

	public VariableCategory getVariableCategory() {
		return varCategory;
	}

	public void setVariableCategory(VariableCategory category) {
		varCategory = category;
	}

	public ASTIdentifier getName() {
		return variableName;
	}

	public ASTTypeIF getType() {
		return variableType;
	}

	public ASTDeclarationIF getDefinitionSource() {
		return definitionSource;
	}

	public void setDefinitionSource(ASTDeclarationIF source) {
		definitionSource = (ASTFunctionDeclaration) source;
	}

	public ASTIdentifier getCorrespondence() {
		return correspondence;
	}

	public void setCorrespondence(ASTIdentifier id) {
		this.correspondence = id;
	}

	public boolean hasCorrespondence() {
		return correspondence != null;
	}

	public void setAssumption(ASTExpressionIF assumption) {
		this.assumption = assumption;
	}

	public ASTExpressionIF getAssumption() {
		return this.assumption;
	}

	public ASTExpressionIF getInit() {
		return this.initialization;
	}

	public boolean hasInit() {
		return this.initialization != null;
	}

	public void setInit(ASTExpressionIF expr) throws SyntaxException {
		if ((expr.getType() instanceof ASTScalarType || expr.getType() instanceof ASTPointerType)
				&& !(expr.getType().equals(this.getType()))) {
			throw new edu.udel.cis.vsl.tass.model.IF.SyntaxException(
					"Type inconsistency between declaration type and initialization type: "
							+ this.getType() + " and " + expr.getType());
		}
		this.initialization = expr;
	}
}