Package: org.codehaus.groovy.classgen.asm

## [Java] Class OptimizingStatementWriter

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

  - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class StatementWriter` | `controller`

  - ### Constructor Summary

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

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public static void` | `setNodeMeta(TypeChooser chooser, ClassNode classNode)` Populates optimization metadata for the supplied class.
    `` | `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. * * Parameters: `block` - the block statement to compile
    `` | `public void` | `writeDoWhileLoop(DoWhileStatement statement)` * Generates bytecode for a do-while loop. * * Parameters: `statement` - the do-while statement to compile
    `` | `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. * * Parameters: `statement` - the expression statement to compile
    `` | `protected void` | `writeForInLoop(ForStatement statement)` * Generates bytecode for a for-in loop by calling `iterator()` on the * collection expression and delegating loop control to * writeForInLoopControlAndBlock. * * Parameters: `statement` - the for-in statement to compile
    `` | `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. * * Parameters: `statement` - the for statement with a ClosureListExpression collection
    `` | `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/ * CompileStack.pop so only path-live names are visible. Path-live * names come from 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). * * Parameters: `statement` - the if statement to compile
    `` | `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. * * Parameters: `mv` - the method visitor to write to
    `` | `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. * * Parameters: `mv` - the method visitor to write to
    `` | `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. * * Parameters: `statement` - the return statement to compile
    `` | `public void` | `writeWhileLoop(WhileStatement statement)` * Generates bytecode for a while loop. * * Parameters: `statement` - the while statement to compile

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class StatementWriter` | `defineLoopIndexVariable, incrementForLoopIndexVariable, storeForLoopVariable, writeAssert, writeBlockStatement, writeBreak, writeContinue, writeDoWhileLoop, writeExpressionStatement, writeForInLoop, writeForInLoopControlAndBlock, writeForLoopWithClosureList, writeForStatement, writeIfElse, writeIteratorHasNext, writeIteratorNext, writeLoopBackEdge, writeReturn, writeStatementLabel, writeSwitch, writeSynchronized, writeThrow, writeTryCatchFinally, writeWhileLoop`

- - ### Constructor Detail

    - #### public OptimizingStatementWriter(WriterController controller)

      Creates an optimizing statement writer backed by the given controller.

      Parameters:
        `controller` - the writer controller for the current compilation

  - ### Method Detail

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

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

      Parameters:
        `block` - the block statement to compile

    - #### public void writeDoWhileLoop(DoWhileStatement statement)

      * Generates bytecode for a do-while loop. * *

      Parameters:
        `statement` - the do-while statement to compile

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

      Parameters:
        `statement` - the expression statement to compile

    - #### protected void writeForInLoop(ForStatement statement)

      * Generates bytecode for a for-in loop by calling `iterator()` on the * collection expression and delegating loop control to * writeForInLoopControlAndBlock. * *

      Parameters:
        `statement` - the for-in statement to compile

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

      Parameters:
        `statement` - the for statement with a ClosureListExpression collection

    - #### 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/ * CompileStack.pop so only path-live names are visible. Path-live * names come from 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). * *

      Parameters:
        `statement` - the if statement to compile

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

      Parameters:
        `mv` - the method visitor to write to

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

      Parameters:
        `mv` - the method visitor to write to

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

      Parameters:
        `statement` - the return statement to compile

    - #### public void writeWhileLoop(WhileStatement statement)

      * Generates bytecode for a while loop. * *

      Parameters:
        `statement` - the while statement to compile