package accrue.analysis.pointer;

import polyglot.types.ConstructorInstance;

/**
 * ConstructorContext is used to distinguish instances of code. In 
 * particular, initializers are executed at a certain point 
 * during the invocation of a constructor. Thus, initializer code 
 * is analyzed multiple times, once for each constructor.
 * ConstructorContext distinguishes these difference analyses.
 */
public class ConstructorContext {
    public static ConstructorContext NORMAL = new ConstructorContext(null);
    private final ConstructorInstance ci;

    public ConstructorContext(ConstructorInstance ci) {
        this.ci = ci;
    }

    public ConstructorInstance constructorInstance() {
        return ci;
    }

    @Override
    public int hashCode() {
        return ci == null ? -57 : ci.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o instanceof ConstructorContext) {
            ConstructorContext that = (ConstructorContext) o;
            return this.ci == that.ci
                    || (this.ci != null && this.ci.equals(that.ci));
        }
        return false;
    }

    @Override
    public String toString() {
        if (ci == null) return "NORMAL";
        return "InstanceInit for Constructor " + ci;
    }
}
