package org.simantics.scl.compiler.internal.elaboration.constraints2; import java.util.ArrayList; import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; import org.simantics.scl.compiler.elaboration.modules.TypeClass; import org.simantics.scl.compiler.types.TCon; import org.simantics.scl.compiler.types.TPred; import org.simantics.scl.compiler.types.TVar; import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; import org.simantics.scl.compiler.types.util.TypeUnparsingContext; import gnu.trove.map.hash.THashMap; import gnu.trove.procedure.TObjectProcedure; class ConstraintStore { private final ConstraintSolver solver; private final TypeClass typeClass; private final THashMap> constraintsByHead = new THashMap>(); private final ArrayList constraintsWithoutHead = new ArrayList(); public ConstraintStore(ConstraintSolver solver, TCon typeClassCon) { this.solver = solver; this.typeClass = solver.environment.getTypeClass(typeClassCon); if(this.typeClass == null) throw new InternalCompilerError("Didn't find type class " + typeClassCon + "."); } private ArrayList getConstraintListFor(TPred pred) { Type head = head(pred); if(head instanceof TCon || head instanceof TVar) { ArrayList result = constraintsByHead.get(head); if(result == null) { result = new ArrayList(2); constraintsByHead.put(head, result); } return result; } else return constraintsWithoutHead; } public ConstraintHandle addConstraint(TPred pred, long demandLocation) { // Try to find an existing ConstraintHandle for the predicate ArrayList handles = getConstraintListFor(pred); for(ConstraintHandle handle : handles) if(equals(pred.parameters, handle.constraint.parameters)) return handle; // Create a new ConstraintHandle ConstraintHandle handle = new ConstraintHandle(pred, demandLocation); handles.add(handle); addSuperDemands(handle); return handle; } private void addSuperDemands(ConstraintHandle handle) { for(int i=0;i>() { @Override public boolean execute(ArrayList constraintHandles) { for(ConstraintHandle constraintHandle : constraintHandles) System.out.println(constraintHandle.toString(tuc)); return true; } }); for(ConstraintHandle constraintHandle : constraintsWithoutHead) System.out.println(constraintHandle.toString(tuc)); } }