ThrowNode.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 jdk.nashorn.internal.ir.annotations.Immutable;
29import jdk.nashorn.internal.ir.visitor.NodeVisitor;
30
31/**
32 * IR representation for THROW statements.
33 */
34@Immutable
35public final class ThrowNode extends Statement implements JoinPredecessor {
36    /** Exception expression. */
37    private final Expression expression;
38
39    private final LocalVariableConversion conversion;
40
41    private final boolean isSyntheticRethrow;
42
43    /**
44     * Constructor
45     *
46     * @param lineNumber line number
47     * @param token      token
48     * @param finish     finish
49     * @param expression expression to throw
50     * @param isSyntheticRethrow true if this throw node is part of a synthetic rethrow.
51     */
52    public ThrowNode(final int lineNumber, final long token, final int finish, final Expression expression, final boolean isSyntheticRethrow) {
53        super(lineNumber, token, finish);
54        this.expression = expression;
55        this.isSyntheticRethrow = isSyntheticRethrow;
56        this.conversion = null;
57    }
58
59    private ThrowNode(final ThrowNode node, final Expression expression, final boolean isSyntheticRethrow,
60            final LocalVariableConversion conversion) {
61        super(node);
62        this.expression = expression;
63        this.isSyntheticRethrow = isSyntheticRethrow;
64        this.conversion = conversion;
65    }
66
67    @Override
68    public boolean isTerminal() {
69        return true;
70    }
71
72    /**
73     * Assist in IR navigation.
74     * @param visitor IR navigating visitor.
75     */
76    @Override
77    public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
78        if (visitor.enterThrowNode(this)) {
79            return visitor.leaveThrowNode(setExpression((Expression)expression.accept(visitor)));
80        }
81
82        return this;
83    }
84
85    @Override
86    public void toString(final StringBuilder sb, final boolean printType) {
87        sb.append("throw ");
88
89        if (expression != null) {
90            expression.toString(sb, printType);
91        }
92        if (conversion != null) {
93            conversion.toString(sb);
94        }
95    }
96
97    /**
98     * Get the expression that is being thrown by this node
99     * @return expression
100     */
101    public Expression getExpression() {
102        return expression;
103    }
104
105    /**
106     * Reset the expression being thrown by this node
107     * @param expression new expression
108     * @return new or same thrownode
109     */
110    public ThrowNode setExpression(final Expression expression) {
111        if (this.expression == expression) {
112            return this;
113        }
114        return new ThrowNode(this, expression, isSyntheticRethrow, conversion);
115    }
116
117    /**
118     * Is this a throw a synthetic rethrow in a synthetic catch-all block
119     * created when inlining finally statements? In that case we never
120     * wrap whatever is thrown into an ECMAException, just rethrow it.
121     * @return true if synthetic throw node
122     */
123    public boolean isSyntheticRethrow() {
124        return isSyntheticRethrow;
125    }
126
127    @Override
128    public JoinPredecessor setLocalVariableConversion(final LexicalContext lc, final LocalVariableConversion conversion) {
129        if(this.conversion == conversion) {
130            return this;
131        }
132        return new ThrowNode(this, expression, isSyntheticRethrow, conversion);
133    }
134
135    @Override
136    public LocalVariableConversion getLocalVariableConversion() {
137        return conversion;
138    }
139
140}
141