src/jdk/nashorn/internal/ir/CatchNode.java

Sat, 23 Mar 2013 00:58:39 +0100

author
attila
date
Sat, 23 Mar 2013 00:58:39 +0100
changeset 144
4be452026847
parent 96
e478708faa22
child 211
3a209cbd1d8f
permissions
-rw-r--r--

8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
Reviewed-by: jlaskey, lagergren

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@3 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@3 9 * by Oracle in the LICENSE file that accompanied this code.
jlaskey@3 10 *
jlaskey@3 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 14 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 15 * accompanied this code).
jlaskey@3 16 *
jlaskey@3 17 * You should have received a copy of the GNU General Public License version
jlaskey@3 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 20 *
jlaskey@3 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 22 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 23 * questions.
jlaskey@3 24 */
jlaskey@3 25
jlaskey@3 26 package jdk.nashorn.internal.ir;
jlaskey@3 27
jlaskey@3 28 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
jlaskey@3 29 import jdk.nashorn.internal.runtime.Source;
jlaskey@3 30
jlaskey@3 31 /**
jlaskey@3 32 * IR representation of a catch clause.
jlaskey@3 33 *
jlaskey@3 34 */
jlaskey@3 35 public class CatchNode extends Node {
jlaskey@3 36 /** Exception identifier. */
jlaskey@3 37 private IdentNode exception;
jlaskey@3 38
jlaskey@3 39 /** Exception condition. */
jlaskey@3 40 private Node exceptionCondition;
jlaskey@3 41
jlaskey@3 42 /** Catch body. */
jlaskey@3 43 private Block body;
jlaskey@3 44
jlaskey@3 45 /** Is rethrow - e.g. synthetic catch block for e.g. finallies, the parser case where
jlaskey@3 46 * there has to be at least on catch for syntactic validity */
jlaskey@3 47 private boolean isSyntheticRethrow;
jlaskey@3 48
jlaskey@3 49 /**
jlaskey@3 50 * Constructors
jlaskey@3 51 *
jlaskey@3 52 * @param source the source
jlaskey@3 53 * @param token token
jlaskey@3 54 * @param finish finish
jlaskey@3 55 * @param exception variable name of exception
jlaskey@3 56 * @param exceptionCondition exception condition
jlaskey@3 57 * @param body catch body
jlaskey@3 58 */
jlaskey@3 59 public CatchNode(final Source source, final long token, final int finish, final IdentNode exception, final Node exceptionCondition, final Block body) {
lagergren@57 60 super(source, token, finish);
jlaskey@3 61
jlaskey@3 62 this.exception = exception;
jlaskey@3 63 this.exceptionCondition = exceptionCondition;
jlaskey@3 64 this.body = body;
jlaskey@3 65 }
jlaskey@3 66
jlaskey@3 67 private CatchNode(final CatchNode catchNode, final CopyState cs) {
jlaskey@3 68 super(catchNode);
jlaskey@3 69
lagergren@96 70 this.exception = (IdentNode)cs.existingOrCopy(catchNode.exception);
lagergren@96 71 this.exceptionCondition = cs.existingOrCopy(catchNode.exceptionCondition);
lagergren@96 72 this.body = (Block)cs.existingOrCopy(catchNode.body);
lagergren@96 73 this.isSyntheticRethrow = catchNode.isSyntheticRethrow;
jlaskey@3 74 }
jlaskey@3 75
jlaskey@3 76 @Override
jlaskey@3 77 protected Node copy(final CopyState cs) {
jlaskey@3 78 return new CatchNode(this, cs);
jlaskey@3 79 }
jlaskey@3 80
jlaskey@3 81 /**
jlaskey@3 82 * Assist in IR navigation.
jlaskey@3 83 * @param visitor IR navigating visitor.
jlaskey@3 84 */
jlaskey@3 85 @Override
jlaskey@3 86 public Node accept(final NodeVisitor visitor) {
attila@144 87 if (visitor.enterCatchNode(this) != null) {
jlaskey@3 88 exception = (IdentNode)exception.accept(visitor);
jlaskey@3 89
jlaskey@3 90 if (exceptionCondition != null) {
jlaskey@3 91 exceptionCondition = exceptionCondition.accept(visitor);
jlaskey@3 92 }
jlaskey@3 93
jlaskey@3 94 body = (Block)body.accept(visitor);
attila@144 95 return visitor.leaveCatchNode(this);
jlaskey@3 96 }
jlaskey@3 97
jlaskey@3 98 return this;
jlaskey@3 99 }
jlaskey@3 100
jlaskey@3 101 @Override
jlaskey@3 102 public void toString(final StringBuilder sb) {
jlaskey@3 103 sb.append(" catch (");
jlaskey@3 104 exception.toString(sb);
jlaskey@3 105
jlaskey@3 106 if (exceptionCondition != null) {
jlaskey@3 107 sb.append(" if ");
jlaskey@3 108 exceptionCondition.toString(sb);
jlaskey@3 109 }
jlaskey@3 110 sb.append(')');
jlaskey@3 111 }
jlaskey@3 112
jlaskey@3 113 /**
jlaskey@3 114 * Check if this catch is a synthetic rethrow
jlaskey@3 115 * @return true if this is a synthetic rethrow
jlaskey@3 116 */
jlaskey@3 117 public boolean isSyntheticRethrow() {
jlaskey@3 118 return isSyntheticRethrow;
jlaskey@3 119 }
jlaskey@3 120
jlaskey@3 121 /**
jlaskey@3 122 * Flag this as deliberatly generated catch all that rethrows the
jlaskey@3 123 * caught exception. This is used for example for generating finally
jlaskey@3 124 * expressions
jlaskey@3 125 */
jlaskey@3 126 public void setIsSyntheticRethrow() {
jlaskey@3 127 this.isSyntheticRethrow = true;
jlaskey@3 128 }
jlaskey@3 129
jlaskey@3 130 /**
jlaskey@3 131 * Get the identifier representing the exception thrown
jlaskey@3 132 * @return the exception identifier
jlaskey@3 133 */
jlaskey@3 134 public IdentNode getException() {
jlaskey@3 135 return exception;
jlaskey@3 136 }
jlaskey@3 137
jlaskey@3 138 /**
jlaskey@3 139 * Get the exception condition for this catch block
jlaskey@3 140 * @return the exception condition
jlaskey@3 141 */
jlaskey@3 142 public Node getExceptionCondition() {
jlaskey@3 143 return exceptionCondition;
jlaskey@3 144 }
jlaskey@3 145
jlaskey@3 146 /**
jlaskey@3 147 * Reset the exception condition for this catch block
jlaskey@3 148 * @param exceptionCondition the new exception condition
jlaskey@3 149 */
jlaskey@3 150 public void setExceptionCondition(final Node exceptionCondition) {
jlaskey@3 151 this.exceptionCondition = exceptionCondition;
jlaskey@3 152 }
jlaskey@3 153
jlaskey@3 154 /**
jlaskey@3 155 * Get the body for this catch block
jlaskey@3 156 * @return the catch block body
jlaskey@3 157 */
jlaskey@3 158 public Block getBody() {
jlaskey@3 159 return body;
jlaskey@3 160 }
jlaskey@3 161 }

mercurial