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    /**
94     * Tells if this is a synthetic block statement or not.
95     *
96     * @return true if this is a synthetic statement
97     */
98    public boolean isSynthetic() {
99        return block.isSynthetic();
100    }
101
102    @Override
103    public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
104        if (visitor.enterBlockStatement(this)) {
105            return visitor.leaveBlockStatement(setBlock((Block)block.accept(visitor)));
106        }
107
108        return this;
109    }
110
111    @Override
112    public void toString(final StringBuilder sb, final boolean printType) {
113        block.toString(sb, printType);
114    }
115
116    /**
117     * Return the block to be executed
118     * @return the block
119     */
120    public Block getBlock() {
121        return block;
122    }
123
124    /**
125     * Reset the block to be executed
126     * @param block the block
127     * @return new or same execute node
128     */
129    public BlockStatement setBlock(final Block block) {
130        if (this.block == block) {
131            return this;
132        }
133        return new BlockStatement(this, block);
134    }
135}
136