package accrue.analysis.visit;

import java.util.Map;

import polyglot.ast.FieldDecl;
import polyglot.ast.Node;
import polyglot.types.FieldInstance;
import polyglot.util.InternalCompilerError;
import polyglot.visit.NodeVisitor;

public class RegisterFieldDecls extends NodeVisitor {
    protected Map<FieldInstance, Node> decls;

    public RegisterFieldDecls(Map<FieldInstance, Node> decls) {
        this.decls = decls;
    }

    @Override
    public Node leave(Node old, Node n, NodeVisitor v) {
        Node o = null;
        if (n instanceof FieldDecl) {
            FieldDecl decl = (FieldDecl) n;
            o = decls.put(decl.fieldInstance(), decl);
            if (o != null) {
                throw new InternalCompilerError("Decl collision! " + o + "("
                        + o.hashCode() + ")" + " is already an entry for "
                        + decl.fieldInstance() + ": " + decl.hashCode());
            }
        }

        return n;
    }
}
