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 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.