src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/BlockStatement.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.ir;
  27 
  28 import java.util.List;
  29 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
  30 
  31 /**
  32  * Represents a block used as a statement.
  33  */
  34 public class BlockStatement extends Statement {
  35     private static final long serialVersionUID = 1L;
  36 
  37     /** Block to execute. */
  38     private final Block block;


  39 
  40     /**
  41      * Constructor
  42      *
  43      * @param block the block to execute
  44      */
  45     public BlockStatement(final Block block) {
  46         this(block.getFirstStatementLineNumber(), block);
  47     }
  48 
  49     /**
  50      * Constructor
  51      *
  52      * @param lineNumber line number
  53      * @param block the block to execute
  54      */
  55     public BlockStatement(final int lineNumber, final Block block) {
  56         super(lineNumber, block.getToken(), block.getFinish());
  57         this.block = block;

  58     }
  59 
  60     private BlockStatement(final BlockStatement blockStatement, final Block block) {
  61         super(blockStatement);
  62         this.block = block;

  63     }
  64 
  65     /**
  66      * Use this method to create a block statement meant to replace a single statement.
  67      * @param stmt the statement to replace
  68      * @param newStmts the statements for the new block statement
  69      * @return a block statement with the new statements. It will have the line number, token, and finish of the
  70      * original statement.
  71      */
  72     public static BlockStatement createReplacement(final Statement stmt, final List<Statement> newStmts) {
  73         return createReplacement(stmt, stmt.getFinish(), newStmts);
  74     }
  75 
  76     /**
  77      * Use this method to create a block statement meant to replace a single statement.
  78      * @param stmt the statement to replace
  79      * @param finish the new finish for the block
  80      * @param newStmts the statements for the new block statement
  81      * @return a block statement with the new statements. It will have the line number, and token of the
  82      * original statement.
  83      */
  84     public static BlockStatement createReplacement(final Statement stmt, final int finish, final List<Statement> newStmts) {
  85         return new BlockStatement(stmt.getLineNumber(), new Block(stmt.getToken(), finish, newStmts));
  86     }
  87 
  88     @Override
  89     public boolean isTerminal() {
  90         return block.isTerminal();
  91     }
  92 




  93     @Override
  94     public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
  95         if (visitor.enterBlockStatement(this)) {
  96             return visitor.leaveBlockStatement(setBlock((Block)block.accept(visitor)));
  97         }
  98 
  99         return this;
 100     }
 101 
 102     @Override
 103     public void toString(final StringBuilder sb, final boolean printType) {
 104         block.toString(sb, printType);
 105     }
 106 
 107     /**
 108      * Return the block to be executed
 109      * @return the block
 110      */
 111     public Block getBlock() {
 112         return block;
 113     }
 114 
 115     /**
 116      * Reset the block to be executed
 117      * @param block the block
 118      * @return new or same execute node
 119      */
 120     public BlockStatement setBlock(final Block block) {
 121         if (this.block == block) {
 122             return this;
 123         }
 124         return new BlockStatement(this, block);
 125     }
 126 }


  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.ir;
  27 
  28 import java.util.List;
  29 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
  30 
  31 /**
  32  * Represents a block used as a statement.
  33  */
  34 public class BlockStatement extends Statement {
  35     private static final long serialVersionUID = 1L;
  36 
  37     /** Block to execute. */
  38     private final Block block;
  39     /** is this a synthetic block? */
  40     private boolean synthetic;
  41 
  42     /**
  43      * Constructor
  44      *
  45      * @param block the block to execute
  46      */
  47     public BlockStatement(final Block block) {
  48         this(block.getFirstStatementLineNumber(), block, true);
  49     }
  50 
  51     /**
  52      * Constructor
  53      *
  54      * @param lineNumber line number
  55      * @param block the block to execute
  56      */
  57     public BlockStatement(final int lineNumber, final Block block, final boolean synthetic) {
  58         super(lineNumber, block.getToken(), block.getFinish());
  59         this.block = block;
  60         this.synthetic = synthetic;
  61     }
  62 
  63     private BlockStatement(final BlockStatement blockStatement, final Block block, final boolean synthetic) {
  64         super(blockStatement);
  65         this.block = block;
  66         this.synthetic = synthetic;
  67     }
  68 
  69     /**
  70      * Use this method to create a block statement meant to replace a single statement.
  71      * @param stmt the statement to replace
  72      * @param newStmts the statements for the new block statement
  73      * @return a block statement with the new statements. It will have the line number, token, and finish of the
  74      * original statement.
  75      */
  76     public static BlockStatement createReplacement(final Statement stmt, final List<Statement> newStmts) {
  77         return createReplacement(stmt, stmt.getFinish(), newStmts);
  78     }
  79 
  80     /**
  81      * Use this method to create a block statement meant to replace a single statement.
  82      * @param stmt the statement to replace
  83      * @param finish the new finish for the block
  84      * @param newStmts the statements for the new block statement
  85      * @return a block statement with the new statements. It will have the line number, and token of the
  86      * original statement.
  87      */
  88     public static BlockStatement createReplacement(final Statement stmt, final int finish, final List<Statement> newStmts) {
  89         return new BlockStatement(stmt.getLineNumber(), new Block(stmt.getToken(), finish, newStmts), true);
  90     }
  91 
  92     @Override
  93     public boolean isTerminal() {
  94         return block.isTerminal();
  95     }
  96 
  97     public boolean isSynthetic() {
  98         return synthetic;
  99     }
 100 
 101     @Override
 102     public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
 103         if (visitor.enterBlockStatement(this)) {
 104             return visitor.leaveBlockStatement(setBlock((Block)block.accept(visitor)));
 105         }
 106 
 107         return this;
 108     }
 109 
 110     @Override
 111     public void toString(final StringBuilder sb, final boolean printType) {
 112         block.toString(sb, printType);
 113     }
 114 
 115     /**
 116      * Return the block to be executed
 117      * @return the block
 118      */
 119     public Block getBlock() {
 120         return block;
 121     }
 122 
 123     /**
 124      * Reset the block to be executed
 125      * @param block the block
 126      * @return new or same execute node
 127      */
 128     public BlockStatement setBlock(final Block block) {
 129         if (this.block == block) {
 130             return this;
 131         }
 132         return new BlockStatement(this, block, synthetic);
 133     }
 134 }