Package org.codehaus.groovy.classgen.asm

# Class OptimizingStatementWriter

java.lang.Object
org.codehaus.groovy.classgen.asm.StatementWriter
org.codehaus.groovy.classgen.asm.OptimizingStatementWriter

public class OptimizingStatementWriter extends StatementWriter
A `StatementWriter` that emits optimized fast-path bytecode for statements involving integer (and other primitive) operations. When type guards confirm that the operands have their original (unboxed) types, the fast path is taken; otherwise execution falls back to the standard dynamic-dispatch slow path.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `OptimizingStatementWriter.ClassNodeSkip`
  Marker metadata used to skip optimization analysis for a class node.
  `static class`
  `OptimizingStatementWriter.StatementMeta`
  Stores optimization metadata collected for a statement or expression.

- ## Field Summary

  ### Fields inherited from class org.codehaus.groovy.classgen.asm.StatementWriter

  `controller`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `OptimizingStatementWriter(WriterController controller)`
  Creates an optimizing statement writer backed by the given controller.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `setNodeMeta(TypeChooser chooser, ClassNode classNode)`
  Populates optimization metadata for the supplied class.
  `void`
  `writeBlockStatement(BlockStatement statement)`
  Generates bytecode for a block statement by visiting each contained statement.
  `void`
  `writeDoWhileLoop(DoWhileStatement statement)`
  Generates bytecode for a do-while loop.
  `void`
  `writeExpressionStatement(ExpressionStatement statement)`
  Generates bytecode for an expression statement.
  `protected void`
  `writeForInLoop(ForStatement statement)`
  Generates bytecode for a for-in loop by calling `iterator()` on the collection expression and delegating loop control to `StatementWriter.writeForInLoopControlAndBlock(org.codehaus.groovy.ast.stmt.ForStatement)`.
  `protected void`
  `writeForLoopWithClosureList(ForStatement statement)`
  Generates bytecode for a C-style `for(init; cond; incr)` loop.
  `void`
  `writeIfElse(IfStatement statement)`
  Generates bytecode for an if/else statement.
  `protected void`
  `writeIteratorHasNext(org.objectweb.asm.MethodVisitor mv)`
  Emits the `Iterator.hasNext()` call via the given visitor.
  `protected void`
  `writeIteratorNext(org.objectweb.asm.MethodVisitor mv)`
  Emits the `Iterator.next()` call via the given visitor.
  `void`
  `writeReturn(ReturnStatement statement)`
  Generates bytecode for a return statement.
  `void`
  `writeWhileLoop(WhileStatement statement)`
  Generates bytecode for a while loop.

  ### Methods inherited from class org.codehaus.groovy.classgen.asm.StatementWriter

  `defineLoopIndexVariable, incrementForLoopIndexVariable, storeForLoopVariable, writeAssert, writeBreak, writeContinue, writeForInLoopControlAndBlock, writeForStatement, writeLoopBackEdge, writeStatementLabel, writeSwitch, writeSynchronized, writeThrow, writeTryCatchFinally`

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

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

- ## Constructor Details

  - ### OptimizingStatementWriter

    public OptimizingStatementWriter(WriterController controller)
    Creates an optimizing statement writer backed by the given controller.
    Parameters:
      `controller` - the writer controller for the current compilation

- ## Method Details

  - ### writeBlockStatement

    public void writeBlockStatement(BlockStatement statement)
    Generates bytecode for a block statement by visiting each contained statement. Pushes the block's variable scope, emits the statements, and pops afterward. Named labels on the block create a breakable region so that `break label` within the block jumps to the end of it.
    Overrides:
      `writeBlockStatement` in class `StatementWriter`
    Parameters:
      `statement` - the block statement to compile

  - ### writeDoWhileLoop

    public void writeDoWhileLoop(DoWhileStatement statement)
    Generates bytecode for a do-while loop.
    Overrides:
      `writeDoWhileLoop` in class `StatementWriter`
    Parameters:
      `statement` - the do-while statement to compile

  - ### writeIteratorHasNext

    protected void writeIteratorHasNext(org.objectweb.asm.MethodVisitor mv)
    Emits the `Iterator.hasNext()` call via the given visitor. Overrideable so subclasses can substitute a specialized or inlined variant.
    Overrides:
      `writeIteratorHasNext` in class `StatementWriter`
    Parameters:
      `mv` - the method visitor to write to

  - ### writeIteratorNext

    protected void writeIteratorNext(org.objectweb.asm.MethodVisitor mv)
    Emits the `Iterator.next()` call via the given visitor. Overrideable so subclasses can substitute a specialized or inlined variant.
    Overrides:
      `writeIteratorNext` in class `StatementWriter`
    Parameters:
      `mv` - the method visitor to write to

  - ### writeForInLoop

    protected void writeForInLoop(ForStatement statement)
    Generates bytecode for a for-in loop by calling `iterator()` on the collection expression and delegating loop control to `StatementWriter.writeForInLoopControlAndBlock(org.codehaus.groovy.ast.stmt.ForStatement)`.
    Overrides:
      `writeForInLoop` in class `StatementWriter`
    Parameters:
      `statement` - the for-in statement to compile

  - ### writeForLoopWithClosureList

    protected void writeForLoopWithClosureList(ForStatement statement)
    Generates bytecode for a C-style `for(init; cond; incr)` loop. The collection expression is a `ClosureListExpression` whose middle element is the boolean condition, lower elements are initializers, and upper elements are incrementors.
    Overrides:
      `writeForLoopWithClosureList` in class `StatementWriter`
    Parameters:
      `statement` - the for statement with a `ClosureListExpression` collection

  - ### writeWhileLoop

    public void writeWhileLoop(WhileStatement statement)
    Generates bytecode for a while loop.
    Overrides:
      `writeWhileLoop` in class `StatementWriter`
    Parameters:
      `statement` - the while statement to compile

  - ### writeIfElse

    public void writeIfElse(IfStatement statement)
    Generates bytecode for an if/else statement.

    GROOVY-7463: a labeled `if` registers a breakable frame around the then arm only (same region as before GROOVY-12242). The else arm runs after that frame is popped so labeled-break scoping for the then arm is unchanged. Named break labels remain registered for the method (CompileStack does not un-register them on pop).

    GROOVY-12242 / JEP 394: pattern slots are allocated on the outer frame during the condition (needed for short-circuit `&&` RHS). Each arm then uses `CompileStack.pushState()`/`CompileStack.hidePatternVariablesExcept(java.util.Collection<java.lang.String>, java.util.Collection<java.lang.String>)`/ `CompileStack.pop()` so only path-live names are visible. Path-live names come from `VariableScopeVisitor.InstanceofFlowBindings` metadata attached by `VariableScopeVisitor` — classgen does not re-run the flow analysis. After both arms, non-survivors are permanently hidden on the outer frame (JLS §6.3.2.2-200-C).

    Overrides:
      `writeIfElse` in class `StatementWriter`
    Parameters:
      `statement` - the if statement to compile

  - ### writeReturn

    public void writeReturn(ReturnStatement statement)
    Generates bytecode for a return statement. For void methods emits `RETURN` after applying any finally blocks. For value-returning methods evaluates the expression, casts it to the declared return type, and emits the appropriate typed return instruction.
    Overrides:
      `writeReturn` in class `StatementWriter`
    Parameters:
      `statement` - the return statement to compile

  - ### writeExpressionStatement

    public void writeExpressionStatement(ExpressionStatement statement)
    Generates bytecode for an expression statement. Evaluates the expression and discards any value left on the operand stack. Marks method-call and binary expressions so that unused return values are elided rather than boxed.

    GROOVY-12242: non-declaration statements that contain an `instanceof` type pattern run in a nested CompileStack state (`pushState`/`pop`) so pattern locals defined during evaluation cannot leak past the statement.

    Overrides:
      `writeExpressionStatement` in class `StatementWriter`
    Parameters:
      `statement` - the expression statement to compile

  - ### setNodeMeta

    public static void setNodeMeta(TypeChooser chooser, ClassNode classNode)
    Populates optimization metadata for the supplied class.
    Parameters:
      `chooser` - the type chooser used during analysis
      `classNode` - the class to annotate