PointerType.java

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

import java.util.Set;

import edu.udel.cis.vsl.tass.model.IF.type.PointerTypeIF;
import edu.udel.cis.vsl.tass.model.IF.type.TypeIF;

public class PointerType extends Type implements PointerTypeIF {

	private static int instanceCounter = 0;

	private TypeIF baseType;

	private int pointerId;

	public PointerType(TypeIF baseType) {
		super(TypeKind.POINTER);
		this.baseType = baseType;
		pointerId = instanceCounter;
		instanceCounter++;
	}

	public PointerType() {
		this(null);
	}

	public void setBaseType(TypeIF baseType) {
		this.baseType = baseType;
	}

	public boolean equals(Object object) {
		if (object instanceof PointerType) {
			PointerType that = (PointerType) object;

			return pointerId == that.pointerId;
		}
		return false;
	}

	public int hashCode() {
		return pointerId + 512;
	}

	public boolean isNumeric() {
		return false;
	}

	public boolean isSubtypeOf(TypeIF type) {
		if (baseType == null)
			return false;
		if (type instanceof PointerType) {
			PointerType that = (PointerType) type;

			return equals(type)
					|| (that.baseType != null && that.baseType.kind() == TypeKind.VOID);
		}
		return false;
	}

	@Override
	public String longName(Set<Type> stack) {
		if (stack.contains(this))
			return shortName();
		if (baseType == null)
			return "pointer(" + pointerId + ")<?>";
		stack.add(this);
		return "pointer(" + pointerId + ")<" + ((Type) baseType).longName(stack) + ">";
	}

	public String shortName() {
		return "pointer(" + pointerId + ")";
	}

	public TypeIF baseType() {
		return baseType;
	}
}