Package icyllis.arc3d.compiler

# Class ConstantFolder

java.lang.Object
icyllis.arc3d.compiler.ConstantFolder

public class ConstantFolder extends Object
Performs constant folding on AST expressions. This simplifies expressions containing compile-time constants, such as replacing `Literal(2) + Literal(2)` with `Literal(4)`.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ConstantFolder()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Expression`
  `fold(Context context, int pos, Operator op, Expression base)`
  Simplifies the unary expression `OP base`.
  `static Expression`
  `fold(Context context, int pos, Expression left, Operator op, Expression right, Type resultType)`
  Simplifies the binary expression `left OP right`.
  `static OptionalLong`
  `getConstantInt(Expression value)`
  Returns the value if it is an int literal or const int variable with a known value.
  `static Expression`
  `getConstantValueForVariable(Expression value)`
  If the expression is a const variable with a known compile-time constant value, returns that value.
  `static Expression`
  `getConstantValueOrNullForVariable(Expression value)`
  If the expression is a const variable with a known compile-time constant value, returns that value.
  `static Expression`
  `makeConstantValueForVariable(int pos, Expression value)`
  If the expression is a const variable with a known compile-time constant value, returns a clone of that value.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ConstantFolder

    public ConstantFolder()

- ## Method Details

  - ### getConstantInt

    public static OptionalLong getConstantInt(Expression value)
    Returns the value if it is an int literal or const int variable with a known value.

  - ### getConstantValueForVariable

    public static Expression getConstantValueForVariable(Expression value)
    If the expression is a const variable with a known compile-time constant value, returns that value. If not, returns the original expression as-is.

  - ### makeConstantValueForVariable

    public static Expression makeConstantValueForVariable(int pos, Expression value)
    If the expression is a const variable with a known compile-time constant value, returns a clone of that value. If not, returns the original expression as-is.

  - ### getConstantValueOrNullForVariable

    @Nullable public static Expression getConstantValueOrNullForVariable(Expression value)
    If the expression is a const variable with a known compile-time constant value, returns that value. If not, returns null.

  - ### fold

    @Nullable public static Expression fold(@Nonnull Context context, int pos, Expression left, Operator op, Expression right, Type resultType)
    Simplifies the binary expression `left OP right`. Returns null if it can't be simplified.

  - ### fold

    @Nullable public static Expression fold(@Nonnull Context context, int pos, @Nonnull Operator op, Expression base)
    Simplifies the unary expression `OP base`. Returns null if it can't be simplified.