Skip to content

Commit

Permalink
adapting inference code to new JP-Bridge API
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Oct 6, 2017
1 parent 15a2a84 commit e800744
Show file tree
Hide file tree
Showing 22 changed files with 198 additions and 201 deletions.
@@ -1,10 +1,9 @@
package com.github.javaparser.symbolsolver.resolution.typeinference; package com.github.javaparser.symbolsolver.resolution.typeinference;


import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.IntersectionType;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl; import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.symbolsolver.model.typesystem.Type;
import com.github.javaparser.symbolsolver.resolution.typeinference.bounds.*; import com.github.javaparser.symbolsolver.resolution.typeinference.bounds.*;
import com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSameAsType; import com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSameAsType;
import com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSubtypeOfType; import com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSubtypeOfType;
Expand Down Expand Up @@ -152,20 +151,20 @@ private <T> T forEachPairSubtypeAndSubtype(Processor<SubtypeOfBound, SubtypeOfBo
return currentValue; return currentValue;
} }


private boolean areSameTypeInference(Type a, Type b) { private boolean areSameTypeInference(ResolvedType a, ResolvedType b) {
return isInferenceVariable(a) && isInferenceVariable(b) && a.equals(b); return isInferenceVariable(a) && isInferenceVariable(b) && a.equals(b);
} }


private List<Pair<ReferenceType, ReferenceType>> findPairsOfCommonAncestors(ReferenceType r1, ReferenceType r2) { private List<Pair<ResolvedReferenceType, ResolvedReferenceType>> findPairsOfCommonAncestors(ResolvedReferenceType r1, ResolvedReferenceType r2) {
List<ReferenceType> set1 = new LinkedList<>(); List<ResolvedReferenceType> set1 = new LinkedList<>();
set1.add(r1); set1.add(r1);
set1.addAll(r1.getAllAncestors()); set1.addAll(r1.getAllAncestors());
List<ReferenceType> set2 = new LinkedList<>(); List<ResolvedReferenceType> set2 = new LinkedList<>();
set2.add(r2); set2.add(r2);
set2.addAll(r2.getAllAncestors()); set2.addAll(r2.getAllAncestors());
List<Pair<ReferenceType, ReferenceType>> pairs = new LinkedList<>(); List<Pair<ResolvedReferenceType, ResolvedReferenceType>> pairs = new LinkedList<>();
for (ReferenceType rtFrom1 : set1) { for (ResolvedReferenceType rtFrom1 : set1) {
for (ReferenceType rtFrom2 : set2) { for (ResolvedReferenceType rtFrom2 : set2) {
if (rtFrom1.getTypeDeclaration().equals(rtFrom2.getTypeDeclaration())) { if (rtFrom1.getTypeDeclaration().equals(rtFrom2.getTypeDeclaration())) {
pairs.add(new Pair<>(rtFrom1, rtFrom2)); pairs.add(new Pair<>(rtFrom1, rtFrom2));
} }
Expand Down Expand Up @@ -277,33 +276,33 @@ public BoundSet deriveImpliedBounds(TypeSolver typeSolver) {
newConstraintsSet = forEachPairSameAs((a, b, currentConstraintSet) -> { newConstraintsSet = forEachPairSameAs((a, b, currentConstraintSet) -> {
if (isInferenceVariable(a.getS()) && isProperType(a.getT())) { if (isInferenceVariable(a.getS()) && isProperType(a.getT())) {
InferenceVariable alpha = (InferenceVariable)a.getS(); InferenceVariable alpha = (InferenceVariable)a.getS();
Type U = a.getT(); ResolvedType U = a.getT();
Type S = b.getS(); ResolvedType S = b.getS();
Type T = b.getT(); ResolvedType T = b.getT();
Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U); Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T))); currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
} }
if (isInferenceVariable(a.getT()) && isProperType(a.getS())) { if (isInferenceVariable(a.getT()) && isProperType(a.getS())) {
InferenceVariable alpha = (InferenceVariable)a.getT(); InferenceVariable alpha = (InferenceVariable)a.getT();
Type U = a.getS(); ResolvedType U = a.getS();
Type S = b.getS(); ResolvedType S = b.getS();
Type T = b.getT(); ResolvedType T = b.getT();
Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U); Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T))); currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
} }
if (isInferenceVariable(b.getS()) && isProperType(b.getT())) { if (isInferenceVariable(b.getS()) && isProperType(b.getT())) {
InferenceVariable alpha = (InferenceVariable)b.getS(); InferenceVariable alpha = (InferenceVariable)b.getS();
Type U = b.getT(); ResolvedType U = b.getT();
Type S = a.getS(); ResolvedType S = a.getS();
Type T = a.getT(); ResolvedType T = a.getT();
Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U); Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T))); currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
} }
if (isInferenceVariable(b.getT()) && isProperType(b.getS())) { if (isInferenceVariable(b.getT()) && isProperType(b.getS())) {
InferenceVariable alpha = (InferenceVariable)b.getT(); InferenceVariable alpha = (InferenceVariable)b.getT();
Type U = b.getS(); ResolvedType U = b.getS();
Type S = a.getS(); ResolvedType S = a.getS();
Type T = a.getT(); ResolvedType T = a.getT();
Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U); Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T))); currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
} }
Expand All @@ -315,17 +314,17 @@ public BoundSet deriveImpliedBounds(TypeSolver typeSolver) {
newConstraintsSet = forEachPairSameAndSubtype((a, b, currentConstraintSet) -> { newConstraintsSet = forEachPairSameAndSubtype((a, b, currentConstraintSet) -> {
if (isInferenceVariable(a.getS()) && isProperType(a.getT())) { if (isInferenceVariable(a.getS()) && isProperType(a.getT())) {
InferenceVariable alpha = (InferenceVariable)a.getS(); InferenceVariable alpha = (InferenceVariable)a.getS();
Type U = a.getT(); ResolvedType U = a.getT();
Type S = b.getS(); ResolvedType S = b.getS();
Type T = b.getT(); ResolvedType T = b.getT();
Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U); Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, sub.apply(S), sub.apply(T))); currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, sub.apply(S), sub.apply(T)));
} }
if (isInferenceVariable(a.getT()) && isProperType(a.getS())) { if (isInferenceVariable(a.getT()) && isProperType(a.getS())) {
InferenceVariable alpha = (InferenceVariable)a.getT(); InferenceVariable alpha = (InferenceVariable)a.getT();
Type U = a.getS(); ResolvedType U = a.getS();
Type S = b.getS(); ResolvedType S = b.getS();
Type T = b.getT(); ResolvedType T = b.getT();
Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U); Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, sub.apply(S), sub.apply(T))); currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, sub.apply(S), sub.apply(T)));
} }
Expand All @@ -340,13 +339,13 @@ public BoundSet deriveImpliedBounds(TypeSolver typeSolver) {
newConstraintsSet = forEachPairSubtypeAndSubtype((a, b, currentConstraintSet) -> { newConstraintsSet = forEachPairSubtypeAndSubtype((a, b, currentConstraintSet) -> {
if (isInferenceVariable(a.getS()) && isInferenceVariable(b.getS())) { if (isInferenceVariable(a.getS()) && isInferenceVariable(b.getS())) {
if (a.getT().isReferenceType() && b.getT().isReferenceType()) { if (a.getT().isReferenceType() && b.getT().isReferenceType()) {
ReferenceType S = a.getT().asReferenceType(); ResolvedReferenceType S = a.getT().asReferenceType();
ReferenceType T = b.getT().asReferenceType(); ResolvedReferenceType T = b.getT().asReferenceType();
List<Pair<ReferenceType, ReferenceType>> pairs = findPairsOfCommonAncestors(S, T); List<Pair<ResolvedReferenceType, ResolvedReferenceType>> pairs = findPairsOfCommonAncestors(S, T);
for (Pair<ReferenceType, ReferenceType> pair : pairs) { for (Pair<ResolvedReferenceType, ResolvedReferenceType> pair : pairs) {
for (int i=0;i<Math.min(pair.a.typeParametersValues().size(), pair.b.typeParametersValues().size()); i++) { for (int i=0;i<Math.min(pair.a.typeParametersValues().size(), pair.b.typeParametersValues().size()); i++) {
Type si = pair.a.typeParametersValues().get(i); ResolvedType si = pair.a.typeParametersValues().get(i);
Type ti = pair.b.typeParametersValues().get(i); ResolvedType ti = pair.b.typeParametersValues().get(i);
if (!si.isWildcard() && !ti.isWildcard()) { if (!si.isWildcard() && !ti.isWildcard()) {
currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(si, ti)); currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(si, ti));
} }
Expand Down Expand Up @@ -660,12 +659,12 @@ public Optional<InstantiationSet> performResolution(List<InferenceVariable> vari
if (!hasSomeCaptureForAlphas) { if (!hasSomeCaptureForAlphas) {
BoundSet newBounds = BoundSet.empty(); BoundSet newBounds = BoundSet.empty();
for (InferenceVariable alphaI : alphas) { for (InferenceVariable alphaI : alphas) {
Set<Type> properLowerBounds = bounds.stream() Set<ResolvedType> properLowerBounds = bounds.stream()
.filter(b -> b.isProperLowerBoundFor(alphaI).isPresent()) .filter(b -> b.isProperLowerBoundFor(alphaI).isPresent())
.map(b -> b.isProperLowerBoundFor(alphaI).get().getProperType()) .map(b -> b.isProperLowerBoundFor(alphaI).get().getProperType())
.collect(Collectors.toSet()); .collect(Collectors.toSet());


Type Ti = null; ResolvedType Ti = null;


// - If αi has one or more proper lower bounds, L1, ..., Lk, then Ti = lub(L1, ..., Lk) (§4.10.4). // - If αi has one or more proper lower bounds, L1, ..., Lk, then Ti = lub(L1, ..., Lk) (§4.10.4).


Expand All @@ -684,7 +683,7 @@ public Optional<InstantiationSet> performResolution(List<InferenceVariable> vari
// - Otherwise, where αi has proper upper bounds U1, ..., Uk, Ti = glb(U1, ..., Uk) (§5.1.10). // - Otherwise, where αi has proper upper bounds U1, ..., Uk, Ti = glb(U1, ..., Uk) (§5.1.10).


if (Ti == null) { if (Ti == null) {
Set<Type> properUpperBounds = bounds.stream() Set<ResolvedType> properUpperBounds = bounds.stream()
.filter(b -> b.isProperUpperBoundFor(alphaI).isPresent()) .filter(b -> b.isProperUpperBoundFor(alphaI).isPresent())
.map(b -> b.isProperUpperBoundFor(alphaI).get().getProperType()) .map(b -> b.isProperUpperBoundFor(alphaI).get().getProperType())
.collect(Collectors.toSet()); .collect(Collectors.toSet());
Expand Down
Expand Up @@ -4,7 +4,6 @@
import com.github.javaparser.ast.body.ConstructorDeclaration; import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.body.InitializerDeclaration; import com.github.javaparser.ast.body.InitializerDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.VariableDeclarationExpr; import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.*; import com.github.javaparser.ast.stmt.*;
import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.GenericVisitor;
Expand Down
Expand Up @@ -6,9 +6,9 @@
import com.github.javaparser.ast.stmt.ExpressionStmt; import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.ReturnStmt; import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.type.UnknownType; import com.github.javaparser.ast.type.UnknownType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.Type;


import java.util.List; import java.util.List;


Expand Down Expand Up @@ -131,7 +131,7 @@ public static List<Expression> getResultExpressions(BlockStmt blockStmt) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


public static boolean isCompatibleInAssignmentContext(Expression expression, Type type, TypeSolver typeSolver) { public static boolean isCompatibleInAssignmentContext(Expression expression, ResolvedType type, TypeSolver typeSolver) {
return type.isAssignableBy(JavaParserFacade.get(typeSolver).getType(expression, false)); return type.isAssignableBy(JavaParserFacade.get(typeSolver).getType(expression, false));
} }
} }
@@ -1,6 +1,6 @@
package com.github.javaparser.symbolsolver.resolution.typeinference; package com.github.javaparser.symbolsolver.resolution.typeinference;


import com.github.javaparser.symbolsolver.model.typesystem.Type; import com.github.javaparser.resolution.types.ResolvedType;


import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
Expand All @@ -13,7 +13,7 @@ public class InferenceVariableSubstitution {
private final static InferenceVariableSubstitution EMPTY = new InferenceVariableSubstitution(); private final static InferenceVariableSubstitution EMPTY = new InferenceVariableSubstitution();


private List<InferenceVariable> inferenceVariables; private List<InferenceVariable> inferenceVariables;
private List<Type> types; private List<ResolvedType> types;


public static InferenceVariableSubstitution empty() { public static InferenceVariableSubstitution empty() {
return EMPTY; return EMPTY;
Expand All @@ -24,7 +24,7 @@ private InferenceVariableSubstitution() {
this.types = new LinkedList<>(); this.types = new LinkedList<>();
} }


public InferenceVariableSubstitution withPair(InferenceVariable inferenceVariable, Type type) { public InferenceVariableSubstitution withPair(InferenceVariable inferenceVariable, ResolvedType type) {
InferenceVariableSubstitution newInstance = new InferenceVariableSubstitution(); InferenceVariableSubstitution newInstance = new InferenceVariableSubstitution();
newInstance.inferenceVariables.addAll(this.inferenceVariables); newInstance.inferenceVariables.addAll(this.inferenceVariables);
newInstance.types.addAll(this.types); newInstance.types.addAll(this.types);
Expand Down
@@ -1,15 +1,16 @@
package com.github.javaparser.symbolsolver.resolution.typeinference; package com.github.javaparser.symbolsolver.resolution.typeinference;


import com.github.javaparser.symbolsolver.model.typesystem.Type;
import com.github.javaparser.resolution.types.ResolvedType;


/** /**
* @author Federico Tomassetti * @author Federico Tomassetti
*/ */
public class Instantiation { public class Instantiation {
private InferenceVariable inferenceVariable; private InferenceVariable inferenceVariable;
private Type properType; private ResolvedType properType;


public Instantiation(InferenceVariable inferenceVariable, Type properType) { public Instantiation(InferenceVariable inferenceVariable, ResolvedType properType) {
this.inferenceVariable = inferenceVariable; this.inferenceVariable = inferenceVariable;
this.properType = properType; this.properType = properType;
} }
Expand All @@ -18,7 +19,7 @@ public InferenceVariable getInferenceVariable() {
return inferenceVariable; return inferenceVariable;
} }


public Type getProperType() { public ResolvedType getProperType() {
return properType; return properType;
} }


Expand Down
@@ -1,6 +1,6 @@
package com.github.javaparser.symbolsolver.resolution.typeinference; package com.github.javaparser.symbolsolver.resolution.typeinference;


import com.github.javaparser.symbolsolver.model.typesystem.Type; import com.github.javaparser.resolution.types.ResolvedType;


import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -59,7 +59,7 @@ public String toString() {
'}'; '}';
} }


public Type apply(Type type) { public ResolvedType apply(ResolvedType type) {
for (Instantiation instantiation : instantiations) { for (Instantiation instantiation : instantiations) {
type = type.replaceTypeVariables(instantiation.getInferenceVariable().getTypeParameterDeclaration(), instantiation.getProperType()); type = type.replaceTypeVariables(instantiation.getInferenceVariable().getTypeParameterDeclaration(), instantiation.getProperType());
} }
Expand Down
@@ -1,15 +1,15 @@
package com.github.javaparser.symbolsolver.resolution.typeinference; package com.github.javaparser.symbolsolver.resolution.typeinference;


import com.github.javaparser.symbolsolver.model.typesystem.Type; import com.github.javaparser.resolution.types.ResolvedType;


/** /**
* @author Federico Tomassetti * @author Federico Tomassetti
*/ */
public class ProperLowerBound { public class ProperLowerBound {
private InferenceVariable inferenceVariable; private InferenceVariable inferenceVariable;
private Type properType; private ResolvedType properType;


public ProperLowerBound(InferenceVariable inferenceVariable, Type properType) { public ProperLowerBound(InferenceVariable inferenceVariable, ResolvedType properType) {
this.inferenceVariable = inferenceVariable; this.inferenceVariable = inferenceVariable;
this.properType = properType; this.properType = properType;
} }
Expand Down Expand Up @@ -44,7 +44,7 @@ public InferenceVariable getInferenceVariable() {
return inferenceVariable; return inferenceVariable;
} }


public Type getProperType() { public ResolvedType getProperType() {
return properType; return properType;
} }
} }
@@ -1,15 +1,15 @@
package com.github.javaparser.symbolsolver.resolution.typeinference; package com.github.javaparser.symbolsolver.resolution.typeinference;


import com.github.javaparser.symbolsolver.model.typesystem.Type; import com.github.javaparser.resolution.types.ResolvedType;


/** /**
* @author Federico Tomassetti * @author Federico Tomassetti
*/ */
public class ProperUpperBound { public class ProperUpperBound {
private InferenceVariable inferenceVariable; private InferenceVariable inferenceVariable;
private Type properType; private ResolvedType properType;


public ProperUpperBound(InferenceVariable inferenceVariable, Type properType) { public ProperUpperBound(InferenceVariable inferenceVariable, ResolvedType properType) {
this.inferenceVariable = inferenceVariable; this.inferenceVariable = inferenceVariable;
this.properType = properType; this.properType = properType;
} }
Expand Down Expand Up @@ -44,7 +44,7 @@ public InferenceVariable getInferenceVariable() {
return inferenceVariable; return inferenceVariable;
} }


public Type getProperType() { public ResolvedType getProperType() {
return properType; return properType;
} }
} }
@@ -1,7 +1,7 @@
package com.github.javaparser.symbolsolver.resolution.typeinference; package com.github.javaparser.symbolsolver.resolution.typeinference;


import com.github.javaparser.symbolsolver.model.declarations.TypeParameterDeclaration; import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration;
import com.github.javaparser.symbolsolver.model.typesystem.Type; import com.github.javaparser.resolution.types.ResolvedType;


import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
Expand All @@ -11,16 +11,16 @@
*/ */
public class Substitution { public class Substitution {


private List<TypeParameterDeclaration> typeParameterDeclarations; private List<ResolvedTypeParameterDeclaration> typeParameterDeclarations;
private List<Type> types; private List<ResolvedType> types;


private final static Substitution EMPTY = new Substitution(); private final static Substitution EMPTY = new Substitution();


public static Substitution empty() { public static Substitution empty() {
return EMPTY; return EMPTY;
} }


public Substitution withPair(TypeParameterDeclaration typeParameterDeclaration, Type type) { public Substitution withPair(ResolvedTypeParameterDeclaration typeParameterDeclaration, ResolvedType type) {
Substitution newInstance = new Substitution(); Substitution newInstance = new Substitution();
newInstance.typeParameterDeclarations.addAll(this.typeParameterDeclarations); newInstance.typeParameterDeclarations.addAll(this.typeParameterDeclarations);
newInstance.types.addAll(this.types); newInstance.types.addAll(this.types);
Expand All @@ -35,8 +35,8 @@ private Substitution() {
this.types = new LinkedList<>(); this.types = new LinkedList<>();
} }


public Type apply(Type originalType) { public ResolvedType apply(ResolvedType originalType) {
Type result = originalType; ResolvedType result = originalType;
for (int i=0;i<typeParameterDeclarations.size();i++) { for (int i=0;i<typeParameterDeclarations.size();i++) {
result = result.replaceTypeVariables(typeParameterDeclarations.get(i), types.get(i)); result = result.replaceTypeVariables(typeParameterDeclarations.get(i), types.get(i));
} }
Expand Down

0 comments on commit e800744

Please sign in to comment.