Class InferenceContext18
java.lang.Object
org.aspectj.org.eclipse.jdt.internal.compiler.lookup.InferenceContext18
Main class for new type inference as per JLS8 sect 18.
Keeps contextual state and drives the algorithm.
Inference Basics
- 18.1.1 Inference variables:
InferenceVariable - 18.1.2 Constraint Formulas: subclasses of
ConstraintFormula - 18.1.3 Bounds:
TypeBound
Capture bounds are directly captured inBoundSet.captures, throws-bounds inBoundSet.inThrows.
Also:BoundSet: main state during inference.
InferenceContext18 manages instances of the above and coordinates the inference process.
Queries and utilities
TypeBinding.isProperType(boolean): used to exclude "types" that mention inference variables (18.1.1).TypeBinding.mentionsAny(TypeBinding[], int): does the receiver type binding mention any of the given types?TypeBinding.substituteInferenceVariable(InferenceVariable, TypeBinding): replace occurrences of an inference variable with a proper type.TypeBinding.collectInferenceVariables(Set): collect all inference variables mentioned in the receiver type into the given set.TypeVariableBinding.getTypeBounds(InferenceVariable, InferenceSubstitution): Compute the initial type bounds for one inference variable as per JLS8 sect 18.1.3.
Phases of Inference
- 18.2 Reduction:
reduce()with most work happening in implementations ofConstraintFormula.reduce(InferenceContext18):- 18.2.1 Expression Compatibility Constraints:
ConstraintFormula.reduce(InferenceContext18). - 18.2.2 Type Compatibility Constraints ff.
ConstraintFormula.reduce(InferenceContext18).
- 18.2.1 Expression Compatibility Constraints:
- 18.3 Incorporation:
BoundSet.incorporate(InferenceContext18); during inference new constraints are accepted viaBoundSet.reduceOneConstraint(InferenceContext18, ConstraintFormula)(combining 18.2 and 18.3) - 18.4 Resolution:
resolve(InferenceVariable[]).
currentBounds, whereas
the last phase returns the resulting bound set while keeping the previous state in currentBounds.
18.5. Uses of Inference
These are the main entries from the compiler into the inference engine:- 18.5.1 Invocation Applicability Inference
inferInvocationApplicability(MethodBinding, TypeBinding[], boolean). Prepare the initial state for inference of a generic invocation - no target type used at this point. Need to callsolve(boolean)with true afterwards to produce the intermediate result.
Called indirectly fromScope.findMethod(ReferenceBinding, char[], TypeBinding[], InvocationSite, boolean)et al to select applicable methods into overload resolution.- 18.5.2 Invocation Type Inference
inferInvocationType(TypeBinding, InvocationSite, MethodBinding). After a most specific method has been picked, and given a target type determine the final generic instantiation. As long as a target type is still unavailable this phase keeps getting deferred.
Different wrappers exist for the convenience of different callers.- 18.5.3 Functional Interface Parameterization Inference
- Controlled from
FunctionalExpression.resolveType(BlockScope). - 18.5.4 More Specific Method Inference
- Not Yet Implemented
ParameterizedGenericMethodBinding.computeCompatibleMethod(MethodBinding, TypeBinding[], Scope, InvocationSite).
Inference Lifecycle
- Decision whether or not an invocation is a variable-arity invocation is made by first attempting
to solve 18.5.1 in mode
CHECK_LOOSE. Only if that fails, another attempt is made in modeCHECK_VARARG. Which of these two attempts was successful is stored ininferenceKind. This field must be consulted whenever arguments of an invocation should be further processed. See alsogetParameter(TypeBinding[], int, boolean)and its clients.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intApplicability Inference (18.5.1) has been completed.org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSetstatic final intstatic final intstatic final intstatic final intList<org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ConstraintFormula> Signals whether any type compatibility makes use of unchecked conversion.static final booleanstatic final booleanstatic final intintMarks how much work has been done so far? Used to avoid performing any of these tasks more than once.static final intInvocation Type Inference (18.5.2) has been completed (for some target type).static final intboolean -
Constructor Summary
ConstructorsConstructorDescriptionInferenceContext18(Scope scope) InferenceContext18(Scope scope, Expression[] arguments, InvocationSite site, InferenceContext18 outerContext) Construct an inference context for an invocation (method/constructor). -
Method Summary
Modifier and TypeMethodDescriptionvoidaddThrowsContraints(TypeBinding[] parameters, InferenceVariable[] variables, ReferenceBinding[] thrownExceptions) JLS 18.1.3 Bounds: throws α: the inference variable α appears in a throws clauseaddTypeVariableSubstitutions(TypeBinding[] typeArguments, boolean capturesOnly) Add new inference variables for the given type arguments.voidcleanUp()createInitialBoundSet(TypeVariableBinding[] typeParameters) JLS 18.1.3: Create initial bounds from a given set of type parameters declarations.voidcreateInitialConstraintsForParameters(TypeBinding[] parameters, boolean checkVararg, TypeBinding varArgsType, MethodBinding method) JLS 18.5.1: compute bounds from formal and actual parameters.org.aspectj.org.eclipse.jdt.internal.compiler.lookup.InferenceContext18.SuspendedInferenceRecordenterLambda(LambdaExpression lambda) org.aspectj.org.eclipse.jdt.internal.compiler.lookup.InferenceContext18.SuspendedInferenceRecordenterPolyInvocation(InvocationSite invocation, Expression[] innerArguments) voidNot JLS: after overload resolution is done, perform the push of type bounds to outer inference, if any.voidforwardResults(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet result, Invocation invocation, ParameterizedMethodBinding pmb, TypeBinding targetType) From 18.5.3: If B contains an instantiation (§18.1.3) for αi, T, then A'i = T.protected intgetInferenceKind(MethodBinding nonGenericMethod, TypeBinding[] argumentTypes) static TypeBindinggetParameter(TypeBinding[] parameters, int rank, boolean isVarArgs) Retrieve the rank'th parameter, possibly respecting varargs invocation, see 15.12.2.4.getReturnProblemMethodIfNeeded(TypeBinding expectedType, MethodBinding method) Create a problem method signaling failure of invocation type inference, unless the given candidate is tolerable to be compatible with buggy javac.getSolutions(TypeVariableBinding[] typeParameters, InvocationSite site, org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet boundSet) Retrieve the resolved solutions for all given type variables.booleaninferFunctionalInterfaceParameterization(LambdaExpression lambda, BlockScope blockScope, ParameterizedTypeBinding targetTypeWithWildCards) 18.5.3 Functional Interface Parameterization InferencevoidinferInvocationApplicability(MethodBinding method, TypeBinding[] arguments, boolean isDiamond) JLS 18.5.1 Invocation Applicability Inference.org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSetinferInvocationType(TypeBinding expectedType, InvocationSite invocationSite, MethodBinding method) JLS 18.5.2 Invocation Type InferenceinferRecordPatternParameterization(RecordPattern recordPattern, BlockScope scope2, TypeBinding candidateT) voidintegrateInnerInferenceB2(InferenceContext18 innerCtx) booleanbooleanisMoreSpecificThan(MethodBinding m1, MethodBinding m2, boolean isVarArgs, boolean isVarArgs2) 18.5.4 More Specific Method InferencebooleanisResolved(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet boundSet) Have all inference variables been instantiated successfully?static booleanisSameSite(InvocationSite site1, InvocationSite site2) booleanstatic voidstatic ParameterizedTypeBindingIf 'type' is a parameterized type and one of its arguments is a wildcard answer the casted type, else null.voidpushBoundsTo(InferenceContext18 outer) Not per JLS: invent more bubbling up of inner bounds.voidrecordUncheckedConversion(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ConstraintTypeFormula constraint) Record the fact that the given constraint requires unchecked conversion.booleanreduceAndIncorporate(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ConstraintFormula constraint) When inference produces a new constraint, reduce it to a suitable type bound and add the latter to the bound set.booleanfrom 18.5.3: Otherwise, a set of constraint formulas is formed with, for all i (1 ≤ i ≤ n), ‹Pi = Qi›.voidresumeSuspendedInference(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.InferenceContext18.SuspendedInferenceRecord record, InferenceContext18 innerContext) voidsetInexactVarargsInference(boolean isInexactVarargsInference) org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSetsolve()org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSetsolve(boolean inferringApplicability) Try to solve the inference problem defined by constraints and bounds previously registered.org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSetsolve(InferenceVariable[] toResolve, boolean isRecordPatternTypeInference) substitute(TypeBinding type) Substitute any type variables mentioned in 'type' by the corresponding inference variable, if one exists.toString()static voidupdateInnerDiamonds(ParameterizedMethodBinding pmb, Expression[] arguments) booleanFor use by 15.12.2.6 Method Invocation Type
-
Field Details
-
DEBUG
public static final boolean DEBUG- See Also:
-
DEBUG_FINE
public static final boolean DEBUG_FINE- See Also:
-
stepCompleted
public int stepCompletedMarks how much work has been done so far? Used to avoid performing any of these tasks more than once. -
NOT_INFERRED
public static final int NOT_INFERRED- See Also:
-
APPLICABILITY_INFERRED
public static final int APPLICABILITY_INFERREDApplicability Inference (18.5.1) has been completed.- See Also:
-
TYPE_INFERRED
public static final int TYPE_INFERREDInvocation Type Inference (18.5.2) has been completed (for some target type).- See Also:
-
TYPE_INFERRED_FINAL
public static final int TYPE_INFERRED_FINAL- See Also:
-
constraintsWithUncheckedConversion
public List<org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ConstraintFormula> constraintsWithUncheckedConversionSignals whether any type compatibility makes use of unchecked conversion. -
usesUncheckedConversion
public boolean usesUncheckedConversion -
outerContext
-
b2
public org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet b2 -
CHECK_UNKNOWN
public static final int CHECK_UNKNOWN- See Also:
-
CHECK_STRICT
public static final int CHECK_STRICT- See Also:
-
CHECK_LOOSE
public static final int CHECK_LOOSE- See Also:
-
CHECK_VARARG
public static final int CHECK_VARARG- See Also:
-
-
Constructor Details
-
InferenceContext18
public InferenceContext18(Scope scope, Expression[] arguments, InvocationSite site, InferenceContext18 outerContext) Construct an inference context for an invocation (method/constructor). -
InferenceContext18
-
-
Method Details
-
isSameSite
-
createInitialBoundSet
JLS 18.1.3: Create initial bounds from a given set of type parameters declarations.- Returns:
- the set of inference variables created for the given typeParameters
-
substitute
Substitute any type variables mentioned in 'type' by the corresponding inference variable, if one exists. -
createInitialConstraintsForParameters
public void createInitialConstraintsForParameters(TypeBinding[] parameters, boolean checkVararg, TypeBinding varArgsType, MethodBinding method) JLS 18.5.1: compute bounds from formal and actual parameters. -
addTypeVariableSubstitutions
public InferenceVariable[] addTypeVariableSubstitutions(TypeBinding[] typeArguments, boolean capturesOnly) Add new inference variables for the given type arguments. CAVEAT: when passing capturesOnly as true, then the result array may contain nulls! -
addThrowsContraints
public void addThrowsContraints(TypeBinding[] parameters, InferenceVariable[] variables, ReferenceBinding[] thrownExceptions) JLS 18.1.3 Bounds: throws α: the inference variable α appears in a throws clause -
inferInvocationApplicability
public void inferInvocationApplicability(MethodBinding method, TypeBinding[] arguments, boolean isDiamond) JLS 18.5.1 Invocation Applicability Inference. -
inferInvocationType
public org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet inferInvocationType(TypeBinding expectedType, InvocationSite invocationSite, MethodBinding method) throws InferenceFailureException JLS 18.5.2 Invocation Type Inference- Throws:
InferenceFailureException
-
pushBoundsTo
Not per JLS: invent more bubbling up of inner bounds. -
flushBoundOutbox
public void flushBoundOutbox()Not JLS: after overload resolution is done, perform the push of type bounds to outer inference, if any. -
getInferenceKind
-
inferFunctionalInterfaceParameterization
public ReferenceBinding inferFunctionalInterfaceParameterization(LambdaExpression lambda, BlockScope blockScope, ParameterizedTypeBinding targetTypeWithWildCards) 18.5.3 Functional Interface Parameterization Inference -
reduceWithEqualityConstraints
from 18.5.3: Otherwise, a set of constraint formulas is formed with, for all i (1 ≤ i ≤ n), ‹Pi = Qi›. This constraint formula set is reduced to form the bound set B. -
isMoreSpecificThan
public boolean isMoreSpecificThan(MethodBinding m1, MethodBinding m2, boolean isVarArgs, boolean isVarArgs2) 18.5.4 More Specific Method Inference -
solve
public org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet solve(boolean inferringApplicability) throws InferenceFailureException Try to solve the inference problem defined by constraints and bounds previously registered.- Returns:
- a bound set representing the solution, or null if inference failed
- Throws:
InferenceFailureException- a compile error has been detected during inference
-
solve
public org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet solve() throws InferenceFailureException- Throws:
InferenceFailureException
-
solve
public org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet solve(InferenceVariable[] toResolve, boolean isRecordPatternTypeInference) throws InferenceFailureException - Throws:
InferenceFailureException
-
isResolved
public boolean isResolved(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet boundSet) Have all inference variables been instantiated successfully? -
getSolutions
public TypeBinding[] getSolutions(TypeVariableBinding[] typeParameters, InvocationSite site, org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet boundSet) Retrieve the resolved solutions for all given type variables.- Parameters:
boundSet- where instantiations are to be found- Returns:
- array containing the substituted types or
nullelements for any type variable that could not be substituted.
-
reduceAndIncorporate
public boolean reduceAndIncorporate(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ConstraintFormula constraint) throws InferenceFailureException When inference produces a new constraint, reduce it to a suitable type bound and add the latter to the bound set.- Throws:
InferenceFailureException
-
enterPolyInvocation
public org.aspectj.org.eclipse.jdt.internal.compiler.lookup.InferenceContext18.SuspendedInferenceRecord enterPolyInvocation(InvocationSite invocation, Expression[] innerArguments) -
enterLambda
public org.aspectj.org.eclipse.jdt.internal.compiler.lookup.InferenceContext18.SuspendedInferenceRecord enterLambda(LambdaExpression lambda) -
integrateInnerInferenceB2
-
resumeSuspendedInference
public void resumeSuspendedInference(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.InferenceContext18.SuspendedInferenceRecord record, InferenceContext18 innerContext) -
isVarArgs
public boolean isVarArgs() -
getParameter
Retrieve the rank'th parameter, possibly respecting varargs invocation, see 15.12.2.4. Returns null if out of bounds and CHECK_VARARG was not requested. Precondition: isVarArgs implies method.isVarargs() -
getReturnProblemMethodIfNeeded
Create a problem method signaling failure of invocation type inference, unless the given candidate is tolerable to be compatible with buggy javac. -
toString
-
parameterizedWithWildcard
If 'type' is a parameterized type and one of its arguments is a wildcard answer the casted type, else null. A nonnull answer is ensured to also have nonnull arguments. -
getFunctionInterfaceArgumentSolutions
From 18.5.3:- If B contains an instantiation (§18.1.3) for αi, T, then A'i = T.
- Otherwise, A'i = Ai.
-
recordUncheckedConversion
public void recordUncheckedConversion(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ConstraintTypeFormula constraint) Record the fact that the given constraint requires unchecked conversion. -
usesUncheckedConversion
public boolean usesUncheckedConversion()For use by 15.12.2.6 Method Invocation Type -
missingImplementation
-
forwardResults
public void forwardResults(org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BoundSet result, Invocation invocation, ParameterizedMethodBinding pmb, TypeBinding targetType) -
updateInnerDiamonds
-
cleanUp
public void cleanUp() -
inferRecordPatternParameterization
public ReferenceBinding inferRecordPatternParameterization(RecordPattern recordPattern, BlockScope scope2, TypeBinding candidateT) -
isInexactVarargsInference
public boolean isInexactVarargsInference() -
setInexactVarargsInference
public void setInexactVarargsInference(boolean isInexactVarargsInference) -
hasPrematureOverloadResolution
public boolean hasPrematureOverloadResolution()
-