package accrue.analysis.interprocanalysis;

import polyglot.types.CodeInstance;

/**
 * This class identifies program points in missing or native code. 
 */
public class NativeNodeIdentifier_c extends NodeIdentifier_c implements
        NativeNodeIdentifier {
    protected final CodeInstance code;
    protected final int idx;

    /**
     * @param context The AnalysisContext the missing code executes in.
     * @param ci The code instance of the missing code.
     * @param idx An index for distinguishing multiple program points within missing code.
     */
    public NativeNodeIdentifier_c(AnalysisContext context, CodeInstance ci,
            int index) {
        super(context, null);
        this.code = ci;
        this.idx = index;
    }

    public NativeNodeIdentifier_c(AnalysisContext context, CodeInstance ci) {
        this(context, ci, ENTRY);
    }

    @Override
    public boolean isNative() {
        return true;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof NativeNodeIdentifier_c) {
            NativeNodeIdentifier_c that = (NativeNodeIdentifier_c) o;
            return this.hashCode() == that.hashCode()
                    && this.idx == that.idx
                    && (this.code == that.code || this.code.equals(that.code))
                    && (this.context == that.context || this.context.equals(that.context));
        }
        return false;
    }

    @Override
    protected int computeHashCode() {
        return context.hashCode() ^ code.hashCode() ^ idx;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "[" + code.toString() + ":" + idx
                + "@" + context + "]";
    }

    @Override
    public CodeInstance getCodeInstance() {
        return code;
    }

    @Override
    public int getIndex() {
        return idx;
    }
}
