BlockStatement.java revision 953:221a84ef44c0
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    /** Block to execute. */
36    private final Block block;
37
38    /**
39     * Constructor
40     *
41     * @param lineNumber line number
42     * @param block the block to execute
43     */
44    public BlockStatement(final int lineNumber, final Block block) {
45        super(lineNumber, block.getToken(), block.getFinish());
46        this.block = block;
47    }
48
49    private BlockStatement(final BlockStatement blockStatement, final Block block) {
50        super(blockStatement);
51        this.block = block;
52    }
53
54    /**
55     * Use this method to create a block statement meant to replace a single statement.
56     * @param stmt the statement to replace
57     * @param newStmts the statements for the new block statement
58     * @return a block statement with the new statements. It will have the line number, token, and finish of the
59     * original statement.
60     */
61    public static BlockStatement createReplacement(final Statement stmt, final List<Statement> newStmts) {
62        return createReplacement(stmt, stmt.getFinish(), newStmts);
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 finish the new finish for the block
69     * @param newStmts the statements for the new block statement
70     * @return a block statement with the new statements. It will have the line number, and token of the
71     * original statement.
72     */
73    public static BlockStatement createReplacement(final Statement stmt, final int finish, final List<Statement> newStmts) {
74        return new BlockStatement(stmt.getLineNumber(), new Block(stmt.getToken(), finish, newStmts));
75    }
76
77    @Override
78    public boolean isTerminal() {
79        return block.isTerminal();
80    }
81
82    @Override
83    public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
84        if (visitor.enterBlockStatement(this)) {
85            return visitor.leaveBlockStatement(setBlock((Block)block.accept(visitor)));
86        }
87
88        return this;
89    }
90
91    @Override
92    public void toString(final StringBuilder sb, final boolean printType) {
93        block.toString(sb, printType);
94    }
95
96    /**
97     * Return the block to be executed
98     * @return the block
99     */
100    public Block getBlock() {
101        return block;
102    }
103
104    /**
105     * Reset the block to be executed
106     * @param block the block
107     * @return new or same execute node
108     */
109    public BlockStatement setBlock(final Block block) {
110        if (this.block == block) {
111            return this;
112        }
113        return new BlockStatement(this, block);
114    }
115}
116