Search in sources :

Example 1 with TypeEquivalenceSet

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet in project che by eclipse.

the class InferTypeArgumentsConstraintsSolver method solveConstraints.

public InferTypeArgumentsUpdate solveConstraints(IProgressMonitor pm) {
    //$NON-NLS-1$
    pm.beginTask("", 2);
    fUpdate = new InferTypeArgumentsUpdate();
    ConstraintVariable2[] allConstraintVariables = fTCModel.getAllConstraintVariables();
    if (allConstraintVariables.length == 0)
        return fUpdate;
    fTypeSetEnvironment = new TypeSetEnvironment(fTCModel.getTypeEnvironment());
    ParametricStructureComputer parametricStructureComputer = new ParametricStructureComputer(allConstraintVariables, fTCModel);
    Collection<CollectionElementVariable2> newVars = parametricStructureComputer.createElemConstraintVariables();
    ArrayList<ConstraintVariable2> newAllConstraintVariables = new ArrayList<ConstraintVariable2>();
    newAllConstraintVariables.addAll(Arrays.asList(allConstraintVariables));
    newAllConstraintVariables.addAll(newVars);
    allConstraintVariables = newAllConstraintVariables.toArray(new ConstraintVariable2[newAllConstraintVariables.size()]);
    //loop over all TypeEquivalenceSets and unify the elements from the fElemStructureEnv with the existing TypeEquivalenceSets
    HashSet<TypeEquivalenceSet> allTypeEquivalenceSets = new HashSet<TypeEquivalenceSet>();
    for (int i = 0; i < allConstraintVariables.length; i++) {
        TypeEquivalenceSet typeEquivalenceSet = allConstraintVariables[i].getTypeEquivalenceSet();
        if (typeEquivalenceSet != null)
            allTypeEquivalenceSets.add(typeEquivalenceSet);
    }
    for (Iterator<TypeEquivalenceSet> iter = allTypeEquivalenceSets.iterator(); iter.hasNext(); ) {
        TypeEquivalenceSet typeEquivalenceSet = iter.next();
        ConstraintVariable2[] contributingVariables = typeEquivalenceSet.getContributingVariables();
        for (int i = 0; i < contributingVariables.length; i++) {
            for (int j = i + 1; j < contributingVariables.length; j++) {
                ConstraintVariable2 first = contributingVariables[i];
                ConstraintVariable2 second = contributingVariables[j];
                // recursively
                fTCModel.createElementEqualsConstraints(first, second);
            }
        }
    }
    ITypeConstraint2[] allTypeConstraints = fTCModel.getAllTypeConstraints();
    for (int i = 0; i < allTypeConstraints.length; i++) {
        ITypeConstraint2 typeConstraint = allTypeConstraints[i];
        fTCModel.createElementEqualsConstraints(typeConstraint.getLeft(), typeConstraint.getRight());
    }
    initializeTypeEstimates(allConstraintVariables);
    if (pm.isCanceled())
        throw new OperationCanceledException();
    fWorkList.addAll(Arrays.asList(allConstraintVariables));
    runSolver(new SubProgressMonitor(pm, 1));
    chooseTypes(allConstraintVariables, new SubProgressMonitor(pm, 1));
    findCastsToRemove(fTCModel.getCastVariables());
    return fUpdate;
}
Also used : CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) TypeSetEnvironment(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSetEnvironment) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) TypeEquivalenceSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet) ITypeConstraint2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeConstraint2) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) HashSet(java.util.HashSet)

Example 2 with TypeEquivalenceSet

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet in project che by eclipse.

the class InferTypeArgumentsConstraintsSolver method chooseTypes.

private void chooseTypes(ConstraintVariable2[] allConstraintVariables, SubProgressMonitor pm) {
    //$NON-NLS-1$
    pm.beginTask("", allConstraintVariables.length);
    for (int i = 0; i < allConstraintVariables.length; i++) {
        ConstraintVariable2 cv = allConstraintVariables[i];
        TypeEquivalenceSet set = cv.getTypeEquivalenceSet();
        if (set == null)
            //TODO: should not happen iff all unused constraint variables got pruned
            continue;
        //TODO: should calculate only once per EquivalenceRepresentative; can throw away estimate TypeSet afterwards
        //TODO: is null for Universe TypeSet
        TType type = chooseSingleType((TypeSet) cv.getTypeEstimate());
        setChosenType(cv, type);
        if (cv instanceof CollectionElementVariable2) {
            CollectionElementVariable2 elementCv = (CollectionElementVariable2) cv;
            fUpdate.addDeclaration(elementCv);
        }
        pm.worked(1);
        if (pm.isCanceled())
            throw new OperationCanceledException();
    }
    pm.done();
}
Also used : CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) TypeEquivalenceSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

Example 3 with TypeEquivalenceSet

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet in project che by eclipse.

the class InferTypeArgumentsConstraintsSolver method maintainSimpleConstraint.

private void maintainSimpleConstraint(ITypeConstraint2 stc) {
    ConstraintVariable2 left = stc.getLeft();
    ConstraintVariable2 right = stc.getRight();
    TypeEquivalenceSet leftSet = left.getTypeEquivalenceSet();
    TypeEquivalenceSet rightSet = right.getTypeEquivalenceSet();
    TypeSet leftEstimate = (TypeSet) leftSet.getTypeEstimate();
    TypeSet rightEstimate = (TypeSet) rightSet.getTypeEstimate();
    if (leftEstimate.isUniverse() && rightEstimate.isUniverse())
        // nothing to do
        return;
    if (leftEstimate.equals(rightEstimate))
        // nothing to do
        return;
    TypeSet lhsSuperTypes = leftEstimate.superTypes();
    TypeSet rhsSubTypes = rightEstimate.subTypes();
    if (!rhsSubTypes.containsAll(leftEstimate)) {
        TypeSet xsection = leftEstimate.intersectedWith(rhsSubTypes);
        //			if (xsection.isEmpty()) // too bad, but this can happen
        //				throw new IllegalStateException("Type estimate set is now empty for LHS in " + left + " <= " + right + "; estimates were " + leftEstimate + " <= " + rightEstimate); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        leftSet.setTypeEstimate(xsection);
        fWorkList.addAll(Arrays.asList(leftSet.getContributingVariables()));
    }
    if (!lhsSuperTypes.containsAll(rightEstimate)) {
        TypeSet xsection = rightEstimate.intersectedWith(lhsSuperTypes);
        //			if (xsection.isEmpty())
        //				throw new IllegalStateException("Type estimate set is now empty for RHS in " + left + " <= " + right + "; estimates were " + leftEstimate + " <= " + rightEstimate); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        rightSet.setTypeEstimate(xsection);
        fWorkList.addAll(Arrays.asList(rightSet.getContributingVariables()));
    }
}
Also used : EnumeratedTypeSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.EnumeratedTypeSet) SingletonTypeSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.SingletonTypeSet) TypeSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet) TypeEquivalenceSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

Example 4 with TypeEquivalenceSet

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet in project che by eclipse.

the class InferTypeArgumentsTCModel method createEqualsConstraint.

public void createEqualsConstraint(ConstraintVariable2 leftElement, ConstraintVariable2 rightElement) {
    if (leftElement == null || rightElement == null)
        return;
    TypeEquivalenceSet leftSet = leftElement.getTypeEquivalenceSet();
    TypeEquivalenceSet rightSet = rightElement.getTypeEquivalenceSet();
    if (leftSet == null) {
        if (rightSet == null) {
            TypeEquivalenceSet set = new TypeEquivalenceSet(leftElement, rightElement);
            leftElement.setTypeEquivalenceSet(set);
            rightElement.setTypeEquivalenceSet(set);
        } else {
            rightSet.add(leftElement);
            leftElement.setTypeEquivalenceSet(rightSet);
        }
    } else {
        if (rightSet == null) {
            leftSet.add(rightElement);
            rightElement.setTypeEquivalenceSet(leftSet);
        } else if (leftSet == rightSet) {
            return;
        } else {
            ConstraintVariable2[] cvs = rightSet.getContributingVariables();
            leftSet.addAll(cvs);
            for (int i = 0; i < cvs.length; i++) cvs[i].setTypeEquivalenceSet(leftSet);
        }
    }
}
Also used : TypeEquivalenceSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet)

Example 5 with TypeEquivalenceSet

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet in project che by eclipse.

the class InferTypeArgumentsConstraintsSolver method initializeTypeEstimates.

private void initializeTypeEstimates(ConstraintVariable2[] allConstraintVariables) {
    for (int i = 0; i < allConstraintVariables.length; i++) {
        ConstraintVariable2 cv = allConstraintVariables[i];
        //TODO: not necessary for types that are not used in a TypeConstraint but only as type in CollectionElementVariable
        //TODO: handle nested element variables; see ParametricStructureComputer.createAndInitVars()
        TypeEquivalenceSet set = cv.getTypeEquivalenceSet();
        if (set == null) {
            set = new TypeEquivalenceSet(cv);
            set.setTypeEstimate(createInitialEstimate(cv));
            cv.setTypeEquivalenceSet(set);
        } else {
            TypeSet typeEstimate = (TypeSet) cv.getTypeEstimate();
            if (typeEstimate == null) {
                ConstraintVariable2[] cvs = set.getContributingVariables();
                typeEstimate = fTypeSetEnvironment.getUniverseTypeSet();
                for (//TODO: optimize: just try to find an immutable CV; if not found, use Universe
                int j = 0; //TODO: optimize: just try to find an immutable CV; if not found, use Universe
                j < cvs.length; //TODO: optimize: just try to find an immutable CV; if not found, use Universe
                j++) typeEstimate = typeEstimate.intersectedWith(createInitialEstimate(cvs[j]));
                set.setTypeEstimate(typeEstimate);
            }
        }
    }
}
Also used : EnumeratedTypeSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.EnumeratedTypeSet) SingletonTypeSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.SingletonTypeSet) TypeSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet) TypeEquivalenceSet(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

Aggregations

TypeEquivalenceSet (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet)7 ConstraintVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)5 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 TType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType)2 EnumeratedTypeSet (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.EnumeratedTypeSet)2 SingletonTypeSet (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.SingletonTypeSet)2 TypeSet (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet)2 CollectionElementVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2)2 ITypeConstraint2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeConstraint2)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 TypeSetEnvironment (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSetEnvironment)1 SubTypeConstraint2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.SubTypeConstraint2)1