Class TypeSwitch
java.lang.Object
org.jfuncmachine.compiler.model.SourceElement
org.jfuncmachine.compiler.model.expr.Expression
org.jfuncmachine.compiler.model.expr.TypeSwitch
A switch expression that matches on cases, and then allows additional
comparisons.
This expression relies on a feature that was added as a preview feature
in Java 17, and became a full part of the JVM in Java 21. JFuncMachine
will, by default, generate equivalent if statements for the switch
if generating for Java 20 or lower, although if the usePreviewFeatures
generator option is set, it will only generate the if version for
Java 16 or below.
The idea here is that you may want to do pattern matching against objects
where there may be several patterns for the same class. Each TypeSwitchClass
contains an optional additional comparison expression, which is executed
if a case matches the target class. If the additional comparison is
true, then the case expression is executed. If it is false, then the
switch jumps to the next case that matches, or to the default case.
In order to facilitate the comparisons, a variable named $caseMatchVar is
made available in the additional comparison expression's environment, meaning
it can be fetched with a GetValue expression.
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal TypeSwitchCase[]A case containing a numeric value and an expression to be executed if the switch expression equals the case valuefinal ExpressionThe expression to be executed if none of the cases match the switch valuefinal ExpressionThe expression generating the value to be switched onFields inherited from class org.jfuncmachine.compiler.model.SourceElement
filename, lineNumber -
Constructor Summary
ConstructorsConstructorDescriptionTypeSwitch(Expression expr, TypeSwitchCase[] cases, Expression defaultCase) Create a Switch expressionTypeSwitch(Expression expr, TypeSwitchCase[] cases, Expression defaultCase, String filename, int lineNumber) Create a Switch expression -
Method Summary
Modifier and TypeMethodDescriptionconvertToFullTailCalls(boolean inTailPosition) Converts this expression to one that is compatible with full tail call optimizationvoidfindCaptured(Environment env) Search for local variables that would be captured by a lambda.voidgenerate(ClassGenerator generator, Environment env, boolean inTailPosition) Generate the bytecode for this expressiongetType()Get the type returned by this expressionvoidreset()Resets any labels in expressions so that the method can be regeneratedMethods inherited from class org.jfuncmachine.compiler.model.SourceElement
generateException
-
Field Details
-
expr
The expression generating the value to be switched on -
cases
A case containing a numeric value and an expression to be executed if the switch expression equals the case value -
defaultCase
The expression to be executed if none of the cases match the switch value
-
-
Constructor Details
-
TypeSwitch
Create a Switch expression- Parameters:
expr- The expression generating the value to be switched oncases- A case containing a numeric value and an expression to be executed if the switch expression equals the case valuedefaultCase- The expression to be executed if none of the cases match the switch value
-
TypeSwitch
public TypeSwitch(Expression expr, TypeSwitchCase[] cases, Expression defaultCase, String filename, int lineNumber) Create a Switch expression- Parameters:
expr- The expression generating the value to be switched oncases- A case containing a numeric value and an expression to be executed if the switch expression equals the case valuedefaultCase- The expression to be executed if none of the cases match the switch valuefilename- The name of the source file where this switch is definedlineNumber- The line number in the source file where this switch starts
-
-
Method Details
-
getType
Description copied from class:ExpressionGet the type returned by this expression- Specified by:
getTypein classExpression- Returns:
- The type of this expression
-
reset
public void reset()Description copied from class:ExpressionResets any labels in expressions so that the method can be regenerated- Overrides:
resetin classExpression
-
findCaptured
Description copied from class:ExpressionSearch for local variables that would be captured by a lambda. Each expression has its own implementation of this, because each subexpression must be searched.- Specified by:
findCapturedin classExpression- Parameters:
env- The environment chain to search
-
convertToFullTailCalls
Description copied from class:ExpressionConverts this expression to one that is compatible with full tail call optimization- Specified by:
convertToFullTailCallsin classExpression- Parameters:
inTailPosition- True if this expression is called from the tail position- Returns:
- A version of this expression whose types are compatible with full tail call optimization
-
generate
Description copied from class:ExpressionGenerate the bytecode for this expression- Specified by:
generatein classExpression- Parameters:
generator- The generator for generating instructions and additional declarationsenv- The environment containing the local variables currently visible to this expressioninTailPosition- True if this expression is being generated from the tail position of the method
-