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