Class CompileTimeTypeCalculator
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceOnly exists as Java is missing a()->voidfunctional interface. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static CompileTimeTypeCalculatorprotected static final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void_handleCall(ASTExpression callExpr, ASTExpression funcExpr, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) protected void_handleCall(ASTExpression callExpr, SymTypeExpression resolvedFuncType, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) protected void_handlePassThroughExpression(ASTExpression passThroughExpression, List<ASTExpression> passedThroughSubExpressions, CompileTimeTypeCalculator.Action typeCheckingModeCalculation, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) protected void_handleResolvedType(ASTExpression expr, SymTypeExpression resolvedType, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) protected voidcheckArgumentsForCompileTimeTypeAndSetType(ASTExpression callExpr, List<SymTypeOfFunction> targetFunctions, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Checks the arguments of the found compile-time function type, given an expression that calls said function.protected voidfillAndCheckInvocationTypeBounds(InferenceResult infResult, PartialFunctionInfo funcInfo, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) protected voidfillAndCheckReturnTypeBounds(InferenceResult infResult, PartialFunctionInfo funcInfo) protected voidfillApplicabilityBounds(InferenceResult inferenceResult, PartialFunctionInfo funcInfo) replaceExprsWithTypesIffNoTargetTypeRequired(PartialFunctionInfo, ITraverser, Type4Ast, InferenceContext4Ast)has to be called before this function.protected voidfillInvocationTypeBounds(InferenceResult inferenceResult, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) protected voidfillInvocationTypeConstraints(InferenceResult inferenceResult, PartialFunctionInfo funcInfo, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) protected voidfillReturnTypeBounds(InferenceResult inferenceResult, PartialFunctionInfo funcInfo) protected voidfillTypeParameterBounds(InferenceResult inferenceResult) protected List<SymTypeOfFunction> fixArities(List<SymTypeOfFunction> functions, int parameterCount) Fixes arity to the specified value.protected Optional<List<SymTypeOfFunction>> fixAritiesOrLogError(List<SymTypeOfFunction> functions, int parameterCount) Fixes arity to the specified value.protected Optional<Map<SymTypeOfFunction, InferenceResult>> getApplicableFunctionsOrLogError(List<SymTypeOfFunction> potentiallyApplicableFuncs, PartialFunctionInfo funcInfo) takes potentially applicable functions (s.protected static CompileTimeTypeCalculatorprotected List<SymTypeOfFunction> getFunctionsOfResolvedType(SymTypeExpression resolvedType) protected InferenceResultgetInfResultForMostSpecificFuncOrLogError(Map<SymTypeOfFunction, InferenceResult> func2InferenceResult) protected Optional<SymTypeExpression> getNonFunctionOfResolvedType(SymTypeExpression resolvedType) protected Map<SymTypeVariable, SymTypeInferenceVariable> given a resolved function (with inference variables), returns a map that replaces the type parameters of the declared function with the inference variables.protected Optional<List<SymTypeOfFunction>> getPotentiallyApplicableFunctionsOrLogError(List<SymTypeOfFunction> fixedArityFunctions, PartialFunctionInfo funcInfo) s.protected InferenceResultgetResultIfNoFunctionInfoAvailable(SymTypeExpression resolvedType, InferenceContext inferenceContext) Returns the result for the cases that 1.static voidhandleCall(ASTExpression callExpr, ASTExpression funcExpr, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Sets the compile-time type OR the inference results of a call to a function type in the expression-node.static voidhandleCall(ASTExpression expr, SymTypeExpression funcType, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Sets the compile-time type OR the inference results of a call to a function type in the expression-node.protected voidhandleInferenceResultsOfCallExpression(ASTExpression callExpr, List<InferenceResult> infResults, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Given an expression that calls a function or comparable expressions (e.g., use of operator), this will use the inferred information and the arguments of the call to 1.static voidhandlePassThroughExpression(ASTExpression passThroughExpr, List<ASTExpression> passedThroughSubExpressions, CompileTimeTypeCalculator.Action typeCheckingModeCalculation, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Handles compile time type calculation of "pass-through"-expressions; Pass-through expressions (a subset of JLS 21 15.2 poly expressions) which pass a value through the expression, without modifying the value in any way, while optionally selecting one out of multiple sub-expressions.static voidhandleResolvedType(ASTExpression expr, SymTypeExpression resolvedType, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Takes a resolved type (or similar) and tries to infer an instantiation.protected InferenceResultinferCalledFunction(SymTypeExpression resolvedType, InferenceContext inferenceContext, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) static voidinit()protected voidpassInferenceInformationUpwardsForFunctionCall(ASTExpression callExpr, List<InferenceResult> inferenceResults, InferenceContext4Ast infCtx4Ast) Given an expression that calls a function, if the functions compile-time type has not been calculated, this will store the inference information corresponding to the call in InferenceContext4Ast.protected StringprintBounds(List<Bound> bounds) protected StringprintConstraints(List<? extends Constraint> constraints) protected Stringprotected StringreduceExpressionCompatibilityConstraint(ExpressionCompatibilityConstraint constraint, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) Reduces constraints of the form<Expr --> type>.protected voidreplaceExprsWithTypesIffNoTargetTypeRequired(PartialFunctionInfo funcInfo, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) In the PartialFunctionInfo, given argument-expressions, these are replaced with types, iff a type can be calculated without access to a target type.static voidreset()protected static voidsetDelegate(CompileTimeTypeCalculator newDelegate) protected List<SymTypeExpression> splitResolvedType(SymTypeExpression resolvedType) splits a (non-normalized) resolved type into it's components
-
Field Details
-
LOG_NAME
- See Also:
-
delegate
-
-
Constructor Details
-
CompileTimeTypeCalculator
public CompileTimeTypeCalculator()
-
-
Method Details
-
handleResolvedType
public static void handleResolvedType(ASTExpression expr, SymTypeExpression resolvedType, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Takes a resolved type (or similar) and tries to infer an instantiation. Fills type4Ast and infCtx4Ast with the calculated information.Given, e.g., an FieldAccessExpression or NameExpression, if multiple possible functions / variables are found and the target type is a function type, filter the results and replace the free type variables. E.g.: unfiltered type:
(T -> byte) & inttarget type:short -> shortresult:short -> byte- Parameters:
resolvedType- Free type variables must have been replaced with inference variables before calling this method. It is not required for this to contain references to symbols.
-
handleCall
public static void handleCall(ASTExpression expr, SymTypeExpression funcType, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Sets the compile-time type OR the inference results of a call to a function type in the expression-node.E.g., let the expression be '1 : intStream'; The Operator ':' can be represented with the function type
(a, Stream<a>) -> Stream<a>with 'a' being a free type variable. The arguments of the function will be '1' and 'intStream'. In this case, this will first calculate the compile-time type of the operator to be(int, Stream<int>) -> Stream<int>. Therefore, the type of the expression isStream<int>, which is stored in the type4AstMap. -
handleCall
public static void handleCall(ASTExpression callExpr, ASTExpression funcExpr, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Sets the compile-time type OR the inference results of a call to a function type in the expression-node.This is similar to
handleCall(ASTExpression, SymTypeExpression, List, ITraverser, Type4Ast, InferenceContext4Ast), but it differs in that 1. There exists an expression that the compile-time function type needs to be calculated from. The type is (usually) not known yet. 2. There can be more than one inference result, e.g., from "(b ? f1 : f2)(1)", there will be the inference results from both f1 and f2. The first difference changes which information is available at which point in the CTTI process, which is why this method is implemented separately. -
_handleResolvedType
protected void _handleResolvedType(ASTExpression expr, SymTypeExpression resolvedType, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) -
_handleCall
protected void _handleCall(ASTExpression callExpr, SymTypeExpression resolvedFuncType, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) -
_handleCall
protected void _handleCall(ASTExpression callExpr, ASTExpression funcExpr, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) -
handleInferenceResultsOfCallExpression
protected void handleInferenceResultsOfCallExpression(ASTExpression callExpr, List<InferenceResult> infResults, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Given an expression that calls a function or comparable expressions (e.g., use of operator), this will use the inferred information and the arguments of the call to 1. either store the compile-time type of the call 2. or pass the inference information to nodes further up the ASTThis is the common part of both
handleCall(de.monticore.expressions.expressionsbasis._ast.ASTExpression, de.monticore.types.check.SymTypeExpression, java.util.List<de.monticore.expressions.expressionsbasis._ast.ASTExpression>, de.monticore.visitor.ITraverser, de.monticore.types3.Type4Ast, de.monticore.types3.generics.context.InferenceContext4Ast)methods. -
passInferenceInformationUpwardsForFunctionCall
protected void passInferenceInformationUpwardsForFunctionCall(ASTExpression callExpr, List<InferenceResult> inferenceResults, InferenceContext4Ast infCtx4Ast) Given an expression that calls a function, if the functions compile-time type has not been calculated, this will store the inference information corresponding to the call in InferenceContext4Ast. This corresponds directly tocheckArgumentsForCompileTimeTypeAndSetType(de.monticore.expressions.expressionsbasis._ast.ASTExpression, java.util.List<de.monticore.types.check.SymTypeOfFunction>, java.util.List<de.monticore.expressions.expressionsbasis._ast.ASTExpression>, de.monticore.visitor.ITraverser, de.monticore.types3.Type4Ast, de.monticore.types3.generics.context.InferenceContext4Ast)- Parameters:
inferenceResults- The inference result(s) of the function, not of the call. Is required to be non-empty.
-
checkArgumentsForCompileTimeTypeAndSetType
protected void checkArgumentsForCompileTimeTypeAndSetType(ASTExpression callExpr, List<SymTypeOfFunction> targetFunctions, List<ASTExpression> arguments, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Checks the arguments of the found compile-time function type, given an expression that calls said function. Sets the type in type4Ast for the callExpr. S.a.passInferenceInformationUpwardsForFunctionCall(ASTExpression, List, InferenceContext4Ast)- Parameters:
targetFunctions- compile-time types; 1. no inference variables 2. fixed arity 3. most specific function chosen (for each subExpression that provides a function)
-
inferCalledFunction
protected InferenceResult inferCalledFunction(SymTypeExpression resolvedType, InferenceContext inferenceContext, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) -
replaceExprsWithTypesIffNoTargetTypeRequired
protected void replaceExprsWithTypesIffNoTargetTypeRequired(PartialFunctionInfo funcInfo, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) In the PartialFunctionInfo, given argument-expressions, these are replaced with types, iff a type can be calculated without access to a target type. E.g.,"2+3"is replaced with int, but"new Set<>()"is not replaced, as the type is not fully known without a target type. -
getResultIfNoFunctionInfoAvailable
protected InferenceResult getResultIfNoFunctionInfoAvailable(SymTypeExpression resolvedType, InferenceContext inferenceContext) Returns the result for the cases that 1. a non-function is expected and as such, non will be inferred 2. not enough information is given to assert that a function is expected -
getPotentiallyApplicableFunctionsOrLogError
protected Optional<List<SymTypeOfFunction>> getPotentiallyApplicableFunctionsOrLogError(List<SymTypeOfFunction> fixedArityFunctions, PartialFunctionInfo funcInfo) s.FunctionRelations.internal_canPotentiallyBeCalledWith(SymTypeOfFunction, PartialFunctionInfo)logs error if no functions exist that are callable. -
getApplicableFunctionsOrLogError
protected Optional<Map<SymTypeOfFunction,InferenceResult>> getApplicableFunctionsOrLogError(List<SymTypeOfFunction> potentiallyApplicableFuncs, PartialFunctionInfo funcInfo) takes potentially applicable functions (s. FunctionRelations) and returns applicable functions and their inference results.- Returns:
- On error: either empty or a Map with a value that has an error. On success: all applicable functions and their inference results. Note: this MUST(!) be Map that uses object identity; otherwise, the case is not identified when there are two identical functions (modulo symbols) resolved.
-
fixAritiesOrLogError
protected Optional<List<SymTypeOfFunction>> fixAritiesOrLogError(List<SymTypeOfFunction> functions, int parameterCount) Fixes arity to the specified value. Filters out all functions that cannot have the specified arity, and logs an error if none can have the arity. -
fixArities
Fixes arity to the specified value. Filters out all functions that cannot have the specified arity. -
fillTypeParameterBounds
-
fillApplicabilityBounds
protected void fillApplicabilityBounds(InferenceResult inferenceResult, PartialFunctionInfo funcInfo) replaceExprsWithTypesIffNoTargetTypeRequired(PartialFunctionInfo, ITraverser, Type4Ast, InferenceContext4Ast)has to be called before this function. -
getInfResultForMostSpecificFuncOrLogError
protected InferenceResult getInfResultForMostSpecificFuncOrLogError(Map<SymTypeOfFunction, InferenceResult> func2InferenceResult) -
fillAndCheckReturnTypeBounds
protected void fillAndCheckReturnTypeBounds(InferenceResult infResult, PartialFunctionInfo funcInfo) -
fillReturnTypeBounds
-
fillInvocationTypeConstraints
protected void fillInvocationTypeConstraints(InferenceResult inferenceResult, PartialFunctionInfo funcInfo, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) -
fillAndCheckInvocationTypeBounds
protected void fillAndCheckInvocationTypeBounds(InferenceResult infResult, PartialFunctionInfo funcInfo, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) -
fillInvocationTypeBounds
protected void fillInvocationTypeBounds(InferenceResult inferenceResult, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) -
reduceExpressionCompatibilityConstraint
protected Optional<List<Bound>> reduceExpressionCompatibilityConstraint(ExpressionCompatibilityConstraint constraint, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast inferenceContext4Ast) Reduces constraints of the form<Expr --> type>. WARNING: While<Expr --> type>IS a constraint, it is only ever used in this class; This is used OUTSIDE ofConstraintReduction, as a way to describe aTypeCompatibilityConstraint, without having a type available. S. a. Java Spec 21 18.1.2- Returns:
- empty on error, reduced bounds otherwise
-
handlePassThroughExpression
public static void handlePassThroughExpression(ASTExpression passThroughExpr, List<ASTExpression> passedThroughSubExpressions, CompileTimeTypeCalculator.Action typeCheckingModeCalculation, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) Handles compile time type calculation of "pass-through"-expressions; Pass-through expressions (a subset of JLS 21 15.2 poly expressions) which pass a value through the expression, without modifying the value in any way, while optionally selecting one out of multiple sub-expressions. Examples are BracketExpression, ConditionalExpression, SwitchExpression (currently not part of MC, s. Java).- Parameters:
passThroughExpr- the pass-through expressionpassedThroughSubExpressions- expressions that are passed troughtypeCheckingModeCalculation- calculation to be done during the type-checking mode of the visitor. The target type will have been set iff applicable. This usually needs to check the expression used to select the expression that is passed through.
-
_handlePassThroughExpression
protected void _handlePassThroughExpression(ASTExpression passThroughExpression, List<ASTExpression> passedThroughSubExpressions, CompileTimeTypeCalculator.Action typeCheckingModeCalculation, de.monticore.visitor.ITraverser typeTraverser, Type4Ast type4Ast, InferenceContext4Ast infCtx4Ast) -
getFunctionsOfResolvedType
- Parameters:
resolvedType- must not be normalized- Returns:
- List of normalized functions included in the resolved type
-
getNonFunctionOfResolvedType
- Parameters:
resolvedType- must not be normalized- Returns:
- the non-function(s) included in the resolved type
-
splitResolvedType
splits a (non-normalized) resolved type into it's components- Returns:
- a list of non-normalized types
-
getParamReplaceMap
given a resolved function (with inference variables), returns a map that replaces the type parameters of the declared function with the inference variables. In JLS 21 18.1.3: [P1:=α1,...,Pn:=αn] -
printBounds
-
printConstraints
-
printPertinentToApplicabilityArgInfo
-
printFunctionForLog
-
init
public static void init() -
reset
public static void reset() -
setDelegate
-
getDelegate
-