BreakableStatement.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.Collections;
29import java.util.List;
30import jdk.nashorn.internal.codegen.Label;
31import jdk.nashorn.internal.ir.annotations.Immutable;
32
33@Immutable
34abstract class BreakableStatement extends LexicalContextStatement implements BreakableNode {
35
36    /** break label. */
37    protected final Label breakLabel;
38
39    final LocalVariableConversion conversion;
40
41    /**
42     * Constructor
43     *
44     * @param lineNumber line number
45     * @param token      token
46     * @param finish     finish
47     * @param breakLabel break label
48     */
49    protected BreakableStatement(final int lineNumber, final long token, final int finish, final Label breakLabel) {
50        super(lineNumber, token, finish);
51        this.breakLabel = breakLabel;
52        this.conversion = null;
53    }
54
55    /**
56     * Copy constructor
57     *
58     * @param breakableNode source node
59     * @param conversion the potentially new local variable conversion
60     */
61    protected BreakableStatement(final BreakableStatement breakableNode, final LocalVariableConversion conversion) {
62        super(breakableNode);
63        this.breakLabel = new Label(breakableNode.getBreakLabel());
64        this.conversion = conversion;
65    }
66
67    /**
68     * Check whether this can be broken out from without using a label,
69     * e.g. everything but Blocks, basically
70     * @return true if breakable without label
71     */
72    @Override
73    public boolean isBreakableWithoutLabel() {
74        return true;
75    }
76
77    /**
78     * Return the break label, i.e. the location to go to on break.
79     * @return the break label
80     */
81    @Override
82    public Label getBreakLabel() {
83        return breakLabel;
84    }
85
86    /**
87     * Return the labels associated with this node. Breakable nodes that
88     * aren't LoopNodes only have a break label - the location immediately
89     * afterwards the node in code
90     * @return list of labels representing locations around this node
91     */
92    @Override
93    public List<Label> getLabels() {
94        return Collections.unmodifiableList(Collections.singletonList(breakLabel));
95    }
96
97    @Override
98    public JoinPredecessor setLocalVariableConversion(final LexicalContext lc, final LocalVariableConversion conversion) {
99        if(this.conversion == conversion) {
100            return this;
101        }
102        return setLocalVariableConversionChanged(lc, conversion);
103    }
104
105    @Override
106    public LocalVariableConversion getLocalVariableConversion() {
107        return conversion;
108    }
109
110    abstract JoinPredecessor setLocalVariableConversionChanged(LexicalContext lc, LocalVariableConversion conversion);
111}
112