public interface ITypeComputer
expressions
.
Implementations will usually inherit from XbaseTypeComputer
or XbaseWithAnnotationsTypeComputer
and
add new expressions by means of overriding computeTypes(XExpression, ITypeComputationState)
and adding
respective cases.
Implementations are responsible for triggering type inference for all child expressions.
There are three base idioms when implementing type inference for a given expression.
state
.
An example for this are boolean literals.
void computeType(XBooleanLiteral literal, ITypeComputationState state) { LightweightTypeReference bool = getTypeForName(Boolean.TYPE, state); state.acceptActualType(bool); }The announced type is immediately propagated to all expectations. This is done by the inference framework.
void computeType(XStringLiteral literal, ITypeComputationState state) { if (literal.getValue().length() == 1) { for(ITypeExpectation expectation: state.getExpectations() { LightweightTypeReference expectedType = expectation.getExpectedType(); if (expectedType.isType(Character.TYPE) || expectedType.isType(Character.class)) { expectation.acceptActualType(expectedType, ConformanceHint.SUCCESS, ConformanceHint.CHECKED); } else { LightweightTypeReference string = getTypeForName(String.class, state); state.acceptActualType(string); } } } else { LightweightTypeReference string = getTypeForName(String.class, state); state.acceptActualType(string); } }
void computeTypes(XIfExpression expression, ITypeComputationState state) { state.withExpectations(getTypeForName(Boolean.TYPE, state).computeTypes(expression.getIfExpression()); state.computeTypes(expression.getThen()); state.computeTypes(expression.getElse()); }The If expression adjusts the expectation for one of its children and computes its type. Afterwards, it directly reuses the current expectations to compute the types of its branch expressions. The common super type of those is assigned to the If expression by the framework.
Modifier and Type | Method and Description |
---|---|
void |
computeTypes(XExpression expression,
ITypeComputationState state)
Compute the type for the given expression in the context of the given computation state.
|
void computeTypes(XExpression expression, ITypeComputationState state)