package accrue.analysis.pointer;

import polyglot.types.ClassType;
import polyglot.types.Type;
import polyglot.util.InternalCompilerError;
import polyglot.util.Position;

/**
 * Represents an allocation site in the code.
 *
 */
public class AllocSiteNode {
    /**
     * Either a ClassType or ArrayType
     */
    public final Type instantiatedType;
    public final ClassType containingClass;
    private final boolean isPreciseType;

    private final int id;
    private final String debugString;
    private final Position pos;

    private static int generator;

    public AllocSiteNode(Type instantiatedClass, ClassType containingClass,
            boolean isPreciseType, String debugString, Position pos) {
        this.instantiatedType = instantiatedClass;
        this.containingClass = containingClass;
        this.isPreciseType = isPreciseType;

        this.id = ++generator;
        this.debugString = debugString;
        this.pos = pos;
        if (debugString == null) {
            throw new InternalCompilerError("Need debug string");
        }
        if ("null".equals(debugString)) {
            throw new InternalCompilerError("Whacky debug string");
        }

    }

    @Override
    final public boolean equals(Object o) {
        return this == o;
    }

    @Override
    final public int hashCode() {
        return id;
    }

    public Type expectedType() {
        return instantiatedType;
    }

    public boolean isPreciseType() {
        return isPreciseType;
    }

    @Override
    public String toString() {
        if (ReferenceVariable.SHOW_POSITION) {
            return debugString + "{" + id + ":" + pos.file() + ":" + pos.line()
                    + "}";
        }
        return debugString + "{" + id + "}";
    }
}
