1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 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 }