Search in sources :

Example 6 with Constraint

use of com.intellij.refactoring.typeCook.deductive.builder.Constraint in project intellij-community by JetBrains.

the class ResolverTree method reduceTypeType.

private void reduceTypeType(final Constraint constr) {
    final PsiType left = constr.getLeft();
    final PsiType right = constr.getRight();
    final Set<Constraint> addendumRise = new HashSet<>();
    final Set<Constraint> addendumSink = new HashSet<>();
    final Set<Constraint> addendumWcrd = new HashSet<>();
    int numSons = 0;
    Binding riseBinding = myBindingFactory.rise(left, right, addendumRise);
    if (riseBinding != null)
        numSons++;
    Binding sinkBinding = myBindingFactory.sink(left, right, addendumSink);
    if (sinkBinding != null)
        numSons++;
    Binding wcrdBinding = mySettings.cookToWildcards() ? myBindingFactory.riseWithWildcard(left, right, addendumWcrd) : null;
    if (wcrdBinding != null)
        numSons++;
    Binding omitBinding = null;
    if (mySettings.exhaustive()) {
        final PsiClassType.ClassResolveResult rightResult = Util.resolveType(right);
        final PsiClassType.ClassResolveResult leftResult = Util.resolveType(left);
        final PsiClass rightClass = rightResult.getElement();
        final PsiClass leftClass = leftResult.getElement();
        if (rightClass != null && leftClass != null && rightClass.getManager().areElementsEquivalent(rightClass, leftClass)) {
            if (PsiUtil.typeParametersIterator(rightClass).hasNext()) {
                omitBinding = myBindingFactory.create();
                numSons++;
                for (PsiType type : rightResult.getSubstitutor().getSubstitutionMap().values()) {
                    if (!(type instanceof Bottom)) {
                        numSons--;
                        omitBinding = null;
                        break;
                    }
                }
            }
        }
    }
    if (numSons == 0)
        return;
    if ((riseBinding != null && sinkBinding != null && riseBinding.equals(sinkBinding)) || canBePruned(riseBinding)) {
        numSons--;
        sinkBinding = null;
    }
    if (riseBinding != null && wcrdBinding != null && riseBinding.equals(wcrdBinding)) {
        numSons--;
        wcrdBinding = null;
    }
    myConstraints.remove(constr);
    mySons = new ResolverTree[numSons];
    int n = 0;
    if (riseBinding != null) {
        mySons[n++] = applyRule(riseBinding, addendumRise);
    }
    if (wcrdBinding != null) {
        mySons[n++] = applyRule(wcrdBinding, addendumWcrd);
    }
    if (omitBinding != null) {
        mySons[n++] = applyRule(omitBinding, addendumWcrd);
    }
    if (sinkBinding != null) {
        mySons[n++] = applyRule(sinkBinding, addendumSink);
    }
}
Also used : Constraint(com.intellij.refactoring.typeCook.deductive.builder.Constraint) Constraint(com.intellij.refactoring.typeCook.deductive.builder.Constraint)

Example 7 with Constraint

use of com.intellij.refactoring.typeCook.deductive.builder.Constraint in project intellij-community by JetBrains.

the class ResolverTree method logSolution.

private void logSolution() {
    LOG.debug("Reduced system:");
    for (final Constraint constr : myConstraints) {
        LOG.debug(constr.toString());
    }
    LOG.debug("End of Reduced system.");
    LOG.debug("Reduced binding:");
    LOG.debug(myCurrentBinding.toString());
    LOG.debug("End of Reduced binding.");
}
Also used : Constraint(com.intellij.refactoring.typeCook.deductive.builder.Constraint)

Example 8 with Constraint

use of com.intellij.refactoring.typeCook.deductive.builder.Constraint in project intellij-community by JetBrains.

the class ResolverTree method reduceInterval.

private void reduceInterval(final Constraint left, final Constraint right) {
    final PsiType leftType = left.getLeft();
    final PsiType rightType = right.getRight();
    final PsiTypeVariable var = (PsiTypeVariable) left.getRight();
    if (leftType.equals(rightType)) {
        final Binding binding = myBindingFactory.create(var, leftType);
        myConstraints.remove(left);
        myConstraints.remove(right);
        mySons = new ResolverTree[] { applyRule(binding) };
        return;
    }
    Binding riseBinding = myBindingFactory.rise(leftType, rightType, null);
    Binding sinkBinding = myBindingFactory.sink(leftType, rightType, null);
    int indicator = (riseBinding == null ? 0 : 1) + (sinkBinding == null ? 0 : 1);
    if (indicator == 0) {
        return;
    } else if ((indicator == 2 && riseBinding.equals(sinkBinding)) || canBePruned(riseBinding)) {
        indicator = 1;
        sinkBinding = null;
    }
    PsiType[] riseRange = PsiType.EMPTY_ARRAY;
    PsiType[] sinkRange = PsiType.EMPTY_ARRAY;
    if (riseBinding != null) {
        riseRange = getTypeRange(riseBinding.apply(rightType), riseBinding.apply(leftType));
    }
    if (sinkBinding != null) {
        sinkRange = getTypeRange(sinkBinding.apply(rightType), sinkBinding.apply(leftType));
    }
    if (riseRange.length + sinkRange.length > 0) {
        myConstraints.remove(left);
        myConstraints.remove(right);
    }
    mySons = new ResolverTree[riseRange.length + sinkRange.length];
    for (int i = 0; i < riseRange.length; i++) {
        final PsiType type = riseRange[i];
        mySons[i] = applyRule(riseBinding.compose(myBindingFactory.create(var, type)));
    }
    for (int i = 0; i < sinkRange.length; i++) {
        final PsiType type = sinkRange[i];
        mySons[i + riseRange.length] = applyRule(sinkBinding.compose(myBindingFactory.create(var, type)));
    }
}
Also used : Constraint(com.intellij.refactoring.typeCook.deductive.builder.Constraint)

Aggregations

Constraint (com.intellij.refactoring.typeCook.deductive.builder.Constraint)8 TObjectIntHashMap (gnu.trove.TObjectIntHashMap)3 Pair (com.intellij.openapi.util.Pair)1 Subtype (com.intellij.refactoring.typeCook.deductive.builder.Subtype)1 EmptyIterator (com.intellij.util.containers.EmptyIterator)1 DFSTBuilder (com.intellij.util.graph.DFSTBuilder)1 TIntArrayList (gnu.trove.TIntArrayList)1 TIntProcedure (gnu.trove.TIntProcedure)1