diff options
Diffstat (limited to 'java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints')
6 files changed, 589 insertions, 0 deletions
diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ConstraintFormula.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ConstraintFormula.java new file mode 100644 index 000000000000..992d875bb535 --- /dev/null +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ConstraintFormula.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.psi.impl.source.resolve.graphInference.constraints; + +import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession; + +import java.util.List; + +/** + * User: anna + */ +public interface ConstraintFormula { + boolean reduce(InferenceSession session, List<ConstraintFormula> constraints, List<ConstraintFormula> delayedConstraints); +} diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ExpressionCompatibilityConstraint.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ExpressionCompatibilityConstraint.java new file mode 100644 index 000000000000..a0e6a767d8f2 --- /dev/null +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ExpressionCompatibilityConstraint.java @@ -0,0 +1,107 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.psi.impl.source.resolve.graphInference.constraints; + +import com.intellij.psi.*; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession; +import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil; +import com.intellij.psi.util.TypeConversionUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * User: anna + */ +public class ExpressionCompatibilityConstraint implements ConstraintFormula { + private PsiExpression myExpression; + private PsiType myT; + + public ExpressionCompatibilityConstraint(@NotNull PsiExpression expression, @NotNull PsiType type) { + myExpression = expression; + myT = type; + } + + @Override + public boolean reduce(InferenceSession session, List<ConstraintFormula> constraints, List<ConstraintFormula> delayedConstraints) { + if (session.isProperType(myT)) { + return TypeConversionUtil.areTypesAssignmentCompatible(myT, myExpression); + } + if (!PsiPolyExpressionUtil.isPolyExpression(myExpression)) { + final PsiType exprType = myExpression.getType(); + if (exprType != null && !exprType.equals(PsiType.NULL)) { + constraints.add(new TypeCompatibilityConstraint(myT, exprType)); + } + return true; + } + if (myExpression instanceof PsiParenthesizedExpression) { + final PsiExpression expression = ((PsiParenthesizedExpression)myExpression).getExpression(); + if (expression != null) { + constraints.add(new ExpressionCompatibilityConstraint(expression, myT)); + return true; + } + } + + if (myExpression instanceof PsiConditionalExpression) { + final PsiExpression thenExpression = ((PsiConditionalExpression)myExpression).getThenExpression(); + if (thenExpression != null) { + constraints.add(new ExpressionCompatibilityConstraint(thenExpression, myT)); + } + + final PsiExpression elseExpression = ((PsiConditionalExpression)myExpression).getElseExpression(); + if (elseExpression != null) { + constraints.add(new ExpressionCompatibilityConstraint(elseExpression, myT)); + } + return true; + } + + if (myExpression instanceof PsiCallExpression) { + //todo + } + + if (myExpression instanceof PsiMethodReferenceExpression) { + //todo + } + + if (myExpression instanceof PsiLambdaExpression) { + constraints.add(new LambdaExpressionCompatibilityConstraint((PsiLambdaExpression)myExpression, myT)); + return true; + } + + + return true; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ExpressionCompatibilityConstraint that = (ExpressionCompatibilityConstraint)o; + + if (!myExpression.equals(that.myExpression)) return false; + if (!myT.equals(that.myT)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = myExpression.hashCode(); + result = 31 * result + myT.hashCode(); + return result; + } +} diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/LambdaExpressionCompatibilityConstraint.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/LambdaExpressionCompatibilityConstraint.java new file mode 100644 index 000000000000..9d9dad46ffc8 --- /dev/null +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/LambdaExpressionCompatibilityConstraint.java @@ -0,0 +1,75 @@ +package com.intellij.psi.impl.source.resolve.graphInference.constraints; + +import com.intellij.psi.*; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceVariable; +import com.intellij.psi.util.PsiUtil; + +import java.util.List; + +/** + * User: anna + */ +public class LambdaExpressionCompatibilityConstraint implements ConstraintFormula { + private final PsiLambdaExpression myExpression; + private final PsiType myT; + + public LambdaExpressionCompatibilityConstraint(PsiLambdaExpression expression, PsiType t) { + myExpression = expression; + myT = t; + } + + @Override + public boolean reduce(InferenceSession session, List<ConstraintFormula> constraints, List<ConstraintFormula> delayedConstraints) { + final InferenceVariable inferenceVariable = session.getInferenceVariable(myT); + if (inferenceVariable != null) { + delayedConstraints.add(this); + return true; + } + if (LambdaHighlightingUtil.checkInterfaceFunctional(myT) != null) { + return false; + } + + if (myExpression.hasFormalParameterTypes()) { + } + final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(myT); + if (interfaceMethod == null) { + return false; + } + final PsiSubstitutor substitutor = LambdaUtil.getSubstitutor(interfaceMethod, PsiUtil.resolveGenericsClassInType(myT)); + final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters(); + for (PsiParameter parameter : parameters) { + if (!session.isProperType(parameter.getType())) { + delayedConstraints.add(this); + return true; + } + } + + final PsiParameter[] lambdaParameters = myExpression.getParameterList().getParameters(); + if (lambdaParameters.length != parameters.length) { + return false; + } + if (myExpression.hasFormalParameterTypes()) { + for (int i = 0; i < lambdaParameters.length; i++) { + constraints.add(new TypeEqualityConstraint(lambdaParameters[i].getType(), substitutor.substitute(parameters[i].getType()))); + } + } + + final PsiType returnType = interfaceMethod.getReturnType(); + if (returnType != null) { + if (returnType.equals(PsiType.VOID)) { + if (!myExpression.isVoidCompatible() && !(myExpression.getBody() instanceof PsiExpression)) { + return false; + } + } else { + if (myExpression.isVoidCompatible()) { //not value-compatible + return false; + } + for (PsiExpression returnExpressions : LambdaUtil.getReturnExpressions(myExpression)) { + constraints.add(new ExpressionCompatibilityConstraint(returnExpressions, returnType)); + } + } + } + return true; + } +} diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/SubtypingConstraint.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/SubtypingConstraint.java new file mode 100644 index 000000000000..64c28d1b462f --- /dev/null +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/SubtypingConstraint.java @@ -0,0 +1,175 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.psi.impl.source.resolve.graphInference.constraints; + +import com.intellij.psi.*; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceBound; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceVariable; +import com.intellij.psi.util.TypeConversionUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * User: anna + */ +public class SubtypingConstraint implements ConstraintFormula { + private PsiType myS; + private PsiType myT; + private boolean myIsRefTypes; + + public SubtypingConstraint(PsiType t, PsiType s, boolean isRefTypes) { + myT = t; + myS = s; + myIsRefTypes = isRefTypes; + } + + @Override + public boolean reduce(InferenceSession session, List<ConstraintFormula> constraints, List<ConstraintFormula> delayedConstraints) { + if (myIsRefTypes) { + if (session.isProperType(myS) && session.isProperType(myT)) { + if (myT == null || myS == null) return myS == myT; + return TypeConversionUtil.isAssignable(myT, myS); + } + InferenceVariable inferenceVariable = session.getInferenceVariable(myS); + if (inferenceVariable != null) { + inferenceVariable.addBound(myT, InferenceBound.UPPER); + return true; + } + if (PsiType.NULL.equals(myS)) return true; + inferenceVariable = session.getInferenceVariable(myT); + if (inferenceVariable != null) { + inferenceVariable.addBound(myS, InferenceBound.LOWER); + return true; + } + if (myT instanceof PsiArrayType) { + if (!(myS instanceof PsiArrayType)) return false; //todo most specific array supertype + final PsiType tComponentType = ((PsiArrayType)myT).getComponentType(); + final PsiType sComponentType = ((PsiArrayType)myS).getComponentType(); + if (!(tComponentType instanceof PsiPrimitiveType) && !(sComponentType instanceof PsiPrimitiveType)) { + constraints.add(new SubtypingConstraint(tComponentType, sComponentType, true)); + return true; + } + return sComponentType instanceof PsiPrimitiveType && sComponentType.equals(tComponentType); + } + if (myT instanceof PsiClassType) { + final PsiClassType.ClassResolveResult TResult = ((PsiClassType)myT).resolveGenerics(); + final PsiClass CClass = TResult.getElement(); + if (CClass != null) { + if (CClass instanceof PsiTypeParameter) { + if (myS instanceof PsiIntersectionType) { + for (PsiType conjunct : ((PsiIntersectionType)myS).getConjuncts()) { + if (myT.equals(conjunct)) return true; + } + } + //todo ((PsiTypeParameter)C).getLowerBound() + return false; + } + + if (!(myS instanceof PsiClassType)) return false; + PsiClassType.ClassResolveResult SResult = ((PsiClassType)myS).resolveGenerics(); + PsiClass SClass = SResult.getElement(); + final PsiSubstitutor tSubstitutor = TResult.getSubstitutor(); + final PsiSubstitutor sSubstitutor = SClass != null ? TypeConversionUtil.getClassSubstitutor(CClass, SClass, SResult.getSubstitutor()) : null; + if (sSubstitutor != null) { + for (PsiTypeParameter parameter : CClass.getTypeParameters()) { + final PsiType tSubstituted = tSubstitutor.substitute(parameter); + final PsiType sSubstituted = sSubstitutor.substituteWithBoundsPromotion(parameter); + constraints.add(new SubtypingConstraint(tSubstituted, sSubstituted, false)); + } + return true; + } + } + return false; + } + + if (myT instanceof PsiIntersectionType) { + for (PsiType conjunct : ((PsiIntersectionType)myT).getConjuncts()) { + constraints.add(new SubtypingConstraint(conjunct, myS, true)); + } + return true; + } + + if (PsiType.NULL.equals(myT)) return false; + } else { + if (myT instanceof PsiWildcardType) { + final PsiType tBound = ((PsiWildcardType)myT).getBound(); + if (tBound == null) { + return true; + } + if (((PsiWildcardType)myT).isExtends()) { + if (tBound.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) { + return true; + } + if (myS instanceof PsiWildcardType) { + final PsiType sBound = ((PsiWildcardType)myS).getBound(); + if (sBound != null && ((PsiWildcardType)myS).isExtends()) { + constraints.add(new SubtypingConstraint(tBound, sBound, true)); + return true; + } + } else { + constraints.add(new SubtypingConstraint(tBound, myS, true)); + return true; + } + return false; + } else { + if (myS instanceof PsiWildcardType) { + final PsiType sBound = ((PsiWildcardType)myS).getBound(); + if (sBound != null && ((PsiWildcardType)myS).isSuper()) { + constraints.add(new SubtypingConstraint(sBound, tBound, true)); + return true; + } + } else { + constraints.add(new SubtypingConstraint(myS, tBound, true)); + return true; + } + } + return false; + } else { + if (myS instanceof PsiWildcardType) { + return false; + } else { + constraints.add(new SubtypingConstraint(myT, myS, true)); + return true; + } + } + } + return true; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + SubtypingConstraint that = (SubtypingConstraint)o; + + if (myIsRefTypes != that.myIsRefTypes) return false; + if (myS != null ? !myS.equals(that.myS) : that.myS != null) return false; + if (myT != null ? !myT.equals(that.myT) : that.myT != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = myS != null ? myS.hashCode() : 0; + result = 31 * result + (myT != null ? myT.hashCode() : 0); + result = 31 * result + (myIsRefTypes ? 1 : 0); + return result; + } +} diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeCompatibilityConstraint.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeCompatibilityConstraint.java new file mode 100644 index 000000000000..353f03b66a80 --- /dev/null +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeCompatibilityConstraint.java @@ -0,0 +1,81 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.psi.impl.source.resolve.graphInference.constraints; + +import com.intellij.psi.PsiClassType; +import com.intellij.psi.PsiPrimitiveType; +import com.intellij.psi.PsiType; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession; +import com.intellij.psi.util.TypeConversionUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * User: anna + */ +public class TypeCompatibilityConstraint implements ConstraintFormula { + private PsiType myT; + private PsiType myS; + + public TypeCompatibilityConstraint(@NotNull PsiType t, @NotNull PsiType s) { + myT = t; + myS = s; + } + + @Override + public boolean reduce(InferenceSession session, List<ConstraintFormula> constraints, List<ConstraintFormula> delayedConstraints) { + if (session.isProperType(myT) && session.isProperType(myS)) { + return TypeConversionUtil.isAssignable(myS, myT); + } + if (myS instanceof PsiPrimitiveType) { + final PsiClassType boxedType = ((PsiPrimitiveType)myS).getBoxedType(session.getManager(), session.getScope()); + if (boxedType != null) { + constraints.add(new TypeCompatibilityConstraint(myT, boxedType)); + return true; + } + } + if (myT instanceof PsiPrimitiveType) { + final PsiClassType boxedType = ((PsiPrimitiveType)myT).getBoxedType(session.getManager(), session.getScope()); + if (boxedType != null) { + constraints.add(new TypeCompatibilityConstraint(boxedType, myS)); + return true; + } + } + constraints.add(new SubtypingConstraint(myT, myS, true)); + return true; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TypeCompatibilityConstraint that = (TypeCompatibilityConstraint)o; + + if (!myS.equals(that.myS)) return false; + if (!myT.equals(that.myT)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = myT.hashCode(); + result = 31 * result + myS.hashCode(); + return result; + } +} diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeEqualityConstraint.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeEqualityConstraint.java new file mode 100644 index 000000000000..a71465653a6f --- /dev/null +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeEqualityConstraint.java @@ -0,0 +1,124 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.psi.impl.source.resolve.graphInference.constraints; + +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.psi.*; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceBound; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceVariable; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * User: anna + */ +public class TypeEqualityConstraint implements ConstraintFormula { + private static final Logger LOG = Logger.getInstance("#" + TypeEqualityConstraint.class.getName()); + private PsiType myT; + private PsiType myS; + + public TypeEqualityConstraint(@NotNull PsiType t, @NotNull PsiType s) { + myT = t; + myS = s; + } + + @Override + public boolean reduce(InferenceSession session, List<ConstraintFormula> constraints, List<ConstraintFormula> delayedConstraints) { + if (session.isProperType(myT) && session.isProperType(myS)) { + return myT.equals(myS); + } + InferenceVariable inferenceVariable = session.getInferenceVariable(myS); + if (inferenceVariable != null) { + inferenceVariable.addBound(myT, InferenceBound.EQ); + return true; + } + inferenceVariable = session.getInferenceVariable(myT); + if (inferenceVariable != null) { + inferenceVariable.addBound(myS, InferenceBound.EQ); + return true; + } + if (myT instanceof PsiClassType && myS instanceof PsiClassType) { + final PsiClassType.ClassResolveResult tResult = ((PsiClassType)myT).resolveGenerics(); + final PsiClassType.ClassResolveResult sResult = ((PsiClassType)myS).resolveGenerics(); + final PsiClass C = tResult.getElement(); + if (C == sResult.getElement() && C != null) { + final PsiSubstitutor tSubstitutor = tResult.getSubstitutor(); + final PsiSubstitutor sSubstitutor = sResult.getSubstitutor(); + for (PsiTypeParameter typeParameter : C.getTypeParameters()) { + final PsiType tSubstituted = tSubstitutor.substitute(typeParameter); + final PsiType sSubstituted = sSubstitutor.substitute(typeParameter); + if (tSubstituted != null && sSubstituted != null) { + constraints.add(new TypeEqualityConstraint(tSubstituted, sSubstituted)); + } + } + return true; + } + } + if (myT instanceof PsiArrayType && myS instanceof PsiArrayType) { + constraints.add(new TypeEqualityConstraint(((PsiArrayType)myT).getComponentType(), ((PsiArrayType)myS).getComponentType())); + return true; + } + if (myT instanceof PsiIntersectionType && myS instanceof PsiIntersectionType) { + final PsiType[] tConjuncts = ((PsiIntersectionType)myT).getConjuncts(); + final PsiType[] sConjuncts = ((PsiIntersectionType)myS).getConjuncts(); + if (sConjuncts.length == tConjuncts.length) { + for (int i = 0; i < sConjuncts.length; i++) { + constraints.add(new TypeEqualityConstraint(tConjuncts[i], sConjuncts[i])); + } + return true; + } + } + + if (myT instanceof PsiWildcardType && myS instanceof PsiWildcardType) { + final PsiType tBound = ((PsiWildcardType)myT).getBound(); + final PsiType sBound = ((PsiWildcardType)myS).getBound(); + + if (tBound == null && sBound == null) return true; + + if (((PsiWildcardType)myT).isExtends() && ((PsiWildcardType)myS).isExtends() || + ((PsiWildcardType)myT).isSuper() && ((PsiWildcardType)myS).isSuper()) { + + LOG.assertTrue(tBound != null); + LOG.assertTrue(sBound != null); + constraints.add(new TypeEqualityConstraint(tBound, sBound)); + return true; + } + } + return false; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TypeEqualityConstraint that = (TypeEqualityConstraint)o; + + if (!myS.equals(that.myS)) return false; + if (!myT.equals(that.myT)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = myT.hashCode(); + result = 31 * result + myS.hashCode(); + return result; + } +} |