BlockStatement.java revision 1068:34ef988d5959
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
26package jdk.nashorn.internal.ir;
27
28import java.util.List;
29import jdk.nashorn.internal.ir.visitor.NodeVisitor;
30
31/**
32 * Represents a block used as a statement.
33 */
34public 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 lineNumber line number
44     * @param block the block to execute
45     */
46    public BlockStatement(final int lineNumber, final Block block) {
47        super(lineNumber, block.getToken(), block.getFinish());
48        this.block = block;
49    }
50
51    private BlockStatement(final BlockStatement blockStatement, final Block block) {
52        super(blockStatement);
53        this.block = block;
54    }
55
56    /**
57     * Use this method to create a block statement meant to replace a single statement.
58     * @param stmt the statement to replace
59     * @param newStmts the statements for the new block statement
60     * @return a block statement with the new statements. It will have the line number, token, and finish of the
61     * original statement.
62     */
63    public static BlockStatement createReplacement(final Statement stmt, final List<Statement> newStmts) {
64        return createReplacement(stmt, stmt.getFinish(), newStmts);
65    }
66
67    /**
68     * Use this method to create a block statement meant to replace a single statement.
69     * @param stmt the statement to replace
70     * @param finish the new finish for the block
71     * @param newStmts the statements for the new block statement
72     * @return a block statement with the new statements. It will have the line number, and token of the
73     * original statement.
74     */
75    public static BlockStatement createReplacement(final Statement stmt, final int finish, final List<Statement> newStmts) {
76        return new BlockStatement(stmt.getLineNumber(), new Block(stmt.getToken(), finish, newStmts));
77    }
78
79    @Override
80    public boolean isTerminal() {
81        return block.isTerminal();
82    }
83
84    @Override
85    public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
86        if (visitor.enterBlockStatement(this)) {
87            return visitor.leaveBlockStatement(setBlock((Block)block.accept(visitor)));
88        }
89
90        return this;
91    }
92
93    @Override
94    public void toString(final StringBuilder sb, final boolean printType) {
95        block.toString(sb, printType);
96    }
97
98    /**
99     * Return the block to be executed
100     * @return the block
101     */
102    public Block getBlock() {
103        return block;
104    }
105
106    /**
107     * Reset the block to be executed
108     * @param block the block
109     * @return new or same execute node
110     */
111    public BlockStatement setBlock(final Block block) {
112        if (this.block == block) {
113            return this;
114        }
115        return new BlockStatement(this, block);
116    }
117}
118