JumpStatement.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
28/**
29 * Common base class for jump statements (e.g. {@code break} and {@code continue}).
30 */
31public abstract class JumpStatement extends Statement implements JoinPredecessor {
32
33    private final String labelName;
34    private final LocalVariableConversion conversion;
35
36    /**
37     * Constructor
38     *
39     * @param lineNumber line number
40     * @param token      token
41     * @param finish     finish
42     * @param labelName  label name for break or null if none
43     */
44    protected JumpStatement(final int lineNumber, final long token, final int finish, final String labelName) {
45        super(lineNumber, token, finish);
46        this.labelName = labelName;
47        this.conversion = null;
48    }
49
50    /**
51     * Copy constructor.
52     * @param jumpStatement the original jump statement.
53     * @param conversion a new local variable conversion.
54     */
55    protected JumpStatement(final JumpStatement jumpStatement, final LocalVariableConversion conversion) {
56        super(jumpStatement);
57        this.labelName = jumpStatement.labelName;
58        this.conversion = conversion;
59    }
60
61    @Override
62    public boolean hasGoto() {
63        return true;
64    }
65
66    /**
67     * Get the label name for this break node
68     * @return label name, or null if none
69     */
70    public String getLabelName() {
71        return labelName;
72    }
73
74    @Override
75    public void toString(final StringBuilder sb, final boolean printType) {
76        sb.append(getStatementName());
77
78        if (labelName != null) {
79            sb.append(' ').append(labelName);
80        }
81    }
82
83    abstract String getStatementName();
84
85    @Override
86    public JumpStatement setLocalVariableConversion(final LexicalContext lc, final LocalVariableConversion conversion) {
87        if(this.conversion == conversion) {
88            return this;
89        }
90        return createNewJumpStatement(conversion);
91    }
92
93    abstract JumpStatement createNewJumpStatement(LocalVariableConversion newConversion);
94
95    @Override
96    public LocalVariableConversion getLocalVariableConversion() {
97        return conversion;
98    }
99}
100