BlockStatement.java revision 1173:82ae555768c7
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 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}
127