nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/JumpStatement.java
author attila
Wed, 28 Jan 2015 17:58:08 +0100
changeset 28690 78317797ab62
parent 27204 06ec78f29a56
permissions -rw-r--r--
8067139: Finally blocks inlined incorrectly Reviewed-by: hannesw, lagergren
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     1
/*
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     4
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    10
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    15
 * accompanied this code).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    16
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    20
 *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    23
 * questions.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    24
 */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    25
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    26
package jdk.nashorn.internal.ir;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    27
26889
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    28
import jdk.nashorn.internal.codegen.Label;
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    29
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    30
/**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    31
 * Common base class for jump statements (e.g. {@code break} and {@code continue}).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    32
 */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    33
public abstract class JumpStatement extends Statement implements JoinPredecessor {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26889
diff changeset
    34
    private static final long serialVersionUID = 1L;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    35
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    36
    private final String labelName;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    37
    private final LocalVariableConversion conversion;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    38
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    39
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    40
     * Constructor
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    41
     *
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    42
     * @param lineNumber line number
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    43
     * @param token      token
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    44
     * @param finish     finish
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    45
     * @param labelName  label name for break or null if none
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    46
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    47
    protected JumpStatement(final int lineNumber, final long token, final int finish, final String labelName) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    48
        super(lineNumber, token, finish);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    49
        this.labelName = labelName;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    50
        this.conversion = null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    51
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    52
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    53
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    54
     * Copy constructor.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    55
     * @param jumpStatement the original jump statement.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    56
     * @param conversion a new local variable conversion.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    57
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    58
    protected JumpStatement(final JumpStatement jumpStatement, final LocalVariableConversion conversion) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    59
        super(jumpStatement);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    60
        this.labelName = jumpStatement.labelName;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    61
        this.conversion = conversion;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    62
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    63
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    64
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    65
    public boolean hasGoto() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    66
        return true;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    67
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    68
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    69
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    70
     * Get the label name for this break node
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    71
     * @return label name, or null if none
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    72
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    73
    public String getLabelName() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    74
        return labelName;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    75
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    76
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    77
    @Override
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
    78
    public void toString(final StringBuilder sb, final boolean printType) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    79
        sb.append(getStatementName());
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    80
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    81
        if (labelName != null) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    82
            sb.append(' ').append(labelName);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    83
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    84
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    85
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    86
    abstract String getStatementName();
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
    87
26889
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    88
    /**
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    89
     * Finds the target for this jump statement in a lexical context.
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    90
     * @param lc the lexical context
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    91
     * @return the target, or null if not found
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    92
     */
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    93
    public abstract BreakableNode getTarget(final LexicalContext lc);
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    94
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    95
    /**
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    96
     * Returns the label corresponding to this kind of jump statement (either a break or continue label) in the target.
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    97
     * @param target the target. Note that it need not be the target of this jump statement, as the method can retrieve
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    98
     * a label on any passed target as long as the target has a label of the requisite kind. Of course, it is advisable
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
    99
     * to invoke the method on a jump statement that targets the breakable.
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
   100
     * @return the label of the target corresponding to the kind of jump statement.
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
   101
     * @throws ClassCastException if invoked on the kind of breakable node that this jump statement is not prepared to
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
   102
     * handle.
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
   103
     */
28690
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   104
    abstract Label getTargetLabel(final BreakableNode target);
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   105
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   106
    /**
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   107
     * Returns the label this jump statement targets.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   108
     * @param lc the lexical context
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   109
     * @return the label this jump statement targets.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   110
     */
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   111
    public Label getTargetLabel(final LexicalContext lc) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   112
        return getTargetLabel(getTarget(lc));
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   113
    }
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   114
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   115
    /**
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   116
     * Returns the limit node for popping scopes when this jump statement is effected.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   117
     * @param lc the current lexical context
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   118
     * @return the limit node for popping scopes when this jump statement is effected.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   119
     */
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   120
    public LexicalContextNode getPopScopeLimit(final LexicalContext lc) {
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   121
        // In most cases (break and continue) this is equal to the target.
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   122
        return getTarget(lc);
78317797ab62 8067139: Finally blocks inlined incorrectly
attila
parents: 27204
diff changeset
   123
    }
26889
dba314d7a634 8059371: Code duplication in handling of break and continue
attila
parents: 25865
diff changeset
   124
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   125
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   126
    public JumpStatement setLocalVariableConversion(final LexicalContext lc, final LocalVariableConversion conversion) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   127
        if(this.conversion == conversion) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   128
            return this;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   129
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   130
        return createNewJumpStatement(conversion);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   131
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   132
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   133
    abstract JumpStatement createNewJumpStatement(LocalVariableConversion newConversion);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   134
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   135
    @Override
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   136
    public LocalVariableConversion getLocalVariableConversion() {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   137
        return conversion;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   138
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents:
diff changeset
   139
}