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

Fri, 17 Oct 2014 14:24:26 +0200

author
attila
date
Fri, 17 Oct 2014 14:24:26 +0200
changeset 1063
8c51767d534d
parent 963
e2497b11a021
child 1205
4112748288bb
child 1226
8b3f832bea55
permissions
-rw-r--r--

8059843: Make AST serializable
Reviewed-by: hannesw, 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 java.util.ArrayList;
jlaskey@3 29 import java.util.Collections;
jlaskey@3 30 import java.util.List;
lagergren@211 31 import jdk.nashorn.internal.ir.annotations.Immutable;
jlaskey@3 32 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
jlaskey@3 33
jlaskey@3 34 /**
jlaskey@3 35 * IR representation of a TRY statement.
jlaskey@3 36 */
lagergren@211 37 @Immutable
attila@963 38 public final class TryNode extends Statement implements JoinPredecessor {
attila@1063 39 private static final long serialVersionUID = 1L;
attila@1063 40
jlaskey@3 41 /** Try statements. */
lagergren@211 42 private final Block body;
jlaskey@3 43
jlaskey@3 44 /** List of catch clauses. */
lagergren@211 45 private final List<Block> catchBlocks;
jlaskey@3 46
jlaskey@3 47 /** Finally clause. */
lagergren@211 48 private final Block finallyBody;
jlaskey@3 49
jlaskey@3 50 /** Exception symbol. */
jlaskey@3 51 private Symbol exception;
jlaskey@3 52
lagergren@57 53 /** Catchall exception for finally expansion, where applicable */
lagergren@57 54 private Symbol finallyCatchAll;
lagergren@57 55
attila@963 56 private final LocalVariableConversion conversion;
attila@963 57
jlaskey@3 58 /**
jlaskey@3 59 * Constructor
jlaskey@3 60 *
lagergren@253 61 * @param lineNumber lineNumber
lagergren@211 62 * @param token token
lagergren@211 63 * @param finish finish
lagergren@211 64 * @param body try node body
lagergren@211 65 * @param catchBlocks list of catch blocks in order
lagergren@211 66 * @param finallyBody body of finally block or null if none
jlaskey@3 67 */
lagergren@253 68 public TryNode(final int lineNumber, final long token, final int finish, final Block body, final List<Block> catchBlocks, final Block finallyBody) {
lagergren@253 69 super(lineNumber, token, finish);
lagergren@253 70 this.body = body;
lagergren@211 71 this.catchBlocks = catchBlocks;
lagergren@211 72 this.finallyBody = finallyBody;
attila@963 73 this.conversion = null;
jlaskey@3 74 }
jlaskey@3 75
attila@963 76 private TryNode(final TryNode tryNode, final Block body, final List<Block> catchBlocks, final Block finallyBody, final LocalVariableConversion conversion) {
jlaskey@3 77 super(tryNode);
lagergren@253 78 this.body = body;
lagergren@211 79 this.catchBlocks = catchBlocks;
lagergren@211 80 this.finallyBody = finallyBody;
attila@963 81 this.conversion = conversion;
attila@963 82 this.exception = tryNode.exception;
jlaskey@3 83 }
jlaskey@3 84
jlaskey@3 85 @Override
lagergren@211 86 public Node ensureUniqueLabels(final LexicalContext lc) {
lagergren@211 87 //try nodes are never in lex context
attila@963 88 return new TryNode(this, body, catchBlocks, finallyBody, conversion);
lagergren@211 89 }
lagergren@211 90
lagergren@211 91 @Override
lagergren@211 92 public boolean isTerminal() {
lagergren@211 93 if (body.isTerminal()) {
lagergren@211 94 for (final Block catchBlock : getCatchBlocks()) {
lagergren@211 95 if (!catchBlock.isTerminal()) {
lagergren@211 96 return false;
lagergren@211 97 }
lagergren@211 98 }
lagergren@211 99 return true;
lagergren@211 100 }
lagergren@211 101 return false;
jlaskey@3 102 }
jlaskey@3 103
jlaskey@3 104 /**
jlaskey@3 105 * Assist in IR navigation.
jlaskey@3 106 * @param visitor IR navigating visitor.
jlaskey@3 107 */
jlaskey@3 108 @Override
lagergren@290 109 public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
lagergren@211 110 if (visitor.enterTryNode(this)) {
lagergren@211 111 // Need to do finallybody first for termination analysis. TODO still necessary?
lagergren@211 112 final Block newFinallyBody = finallyBody == null ? null : (Block)finallyBody.accept(visitor);
lagergren@211 113 final Block newBody = (Block)body.accept(visitor);
lagergren@211 114 return visitor.leaveTryNode(
lagergren@211 115 setBody(newBody).
lagergren@211 116 setFinallyBody(newFinallyBody).
attila@963 117 setCatchBlocks(Node.accept(visitor, catchBlocks)).
lagergren@211 118 setFinallyCatchAll(finallyCatchAll));
jlaskey@3 119 }
jlaskey@3 120
jlaskey@3 121 return this;
jlaskey@3 122 }
jlaskey@3 123
jlaskey@3 124 @Override
attila@963 125 public void toString(final StringBuilder sb, final boolean printType) {
lagergren@211 126 sb.append("try ");
jlaskey@3 127 }
jlaskey@3 128
jlaskey@3 129 /**
jlaskey@3 130 * Get the body for this try block
jlaskey@3 131 * @return body
jlaskey@3 132 */
jlaskey@3 133 public Block getBody() {
jlaskey@3 134 return body;
jlaskey@3 135 }
jlaskey@3 136
jlaskey@3 137 /**
jlaskey@3 138 * Reset the body of this try block
jlaskey@3 139 * @param body new body
lagergren@211 140 * @return new TryNode or same if unchanged
jlaskey@3 141 */
lagergren@211 142 public TryNode setBody(final Block body) {
lagergren@211 143 if (this.body == body) {
lagergren@211 144 return this;
lagergren@211 145 }
attila@963 146 return new TryNode(this, body, catchBlocks, finallyBody, conversion);
jlaskey@3 147 }
jlaskey@3 148
jlaskey@3 149 /**
jlaskey@3 150 * Get the catches for this try block
jlaskey@3 151 * @return a list of catch nodes
jlaskey@3 152 */
jlaskey@3 153 public List<CatchNode> getCatches() {
jlaskey@3 154 final List<CatchNode> catches = new ArrayList<>(catchBlocks.size());
jlaskey@3 155 for (final Block catchBlock : catchBlocks) {
attila@963 156 catches.add(getCatchNodeFromBlock(catchBlock));
jlaskey@3 157 }
lagergren@211 158 return Collections.unmodifiableList(catches);
attila@144 159 }
attila@144 160
attila@963 161 private static CatchNode getCatchNodeFromBlock(final Block catchBlock) {
attila@963 162 return (CatchNode)catchBlock.getStatements().get(0);
attila@963 163 }
attila@963 164
attila@144 165 /**
jlaskey@3 166 * Get the catch blocks for this try block
jlaskey@3 167 * @return a list of blocks
jlaskey@3 168 */
jlaskey@3 169 public List<Block> getCatchBlocks() {
jlaskey@3 170 return Collections.unmodifiableList(catchBlocks);
jlaskey@3 171 }
jlaskey@3 172
jlaskey@3 173 /**
jlaskey@3 174 * Set the catch blocks of this try
jlaskey@3 175 * @param catchBlocks list of catch blocks
lagergren@211 176 * @return new TryNode or same if unchanged
jlaskey@3 177 */
lagergren@211 178 public TryNode setCatchBlocks(final List<Block> catchBlocks) {
lagergren@211 179 if (this.catchBlocks == catchBlocks) {
lagergren@211 180 return this;
lagergren@211 181 }
attila@963 182 return new TryNode(this, body, catchBlocks, finallyBody, conversion);
jlaskey@3 183 }
jlaskey@3 184
jlaskey@3 185 /**
jlaskey@3 186 * Get the exception symbol for this try block
jlaskey@3 187 * @return a symbol for the compiler to store the exception in
jlaskey@3 188 */
jlaskey@3 189 public Symbol getException() {
jlaskey@3 190 return exception;
jlaskey@3 191 }
jlaskey@3 192 /**
jlaskey@3 193 * Set the exception symbol for this try block
jlaskey@3 194 * @param exception a symbol for the compiler to store the exception in
lagergren@211 195 * @return new TryNode or same if unchanged
jlaskey@3 196 */
lagergren@211 197 public TryNode setException(final Symbol exception) {
jlaskey@3 198 this.exception = exception;
lagergren@211 199 return this;
jlaskey@3 200 }
jlaskey@3 201
jlaskey@3 202 /**
lagergren@57 203 * Get the catch all symbol for this try block
lagergren@57 204 * @return catch all symbol
lagergren@57 205 */
lagergren@57 206 public Symbol getFinallyCatchAll() {
lagergren@57 207 return this.finallyCatchAll;
lagergren@57 208 }
lagergren@57 209
lagergren@57 210 /**
lagergren@57 211 * If a finally block exists, the synthetic catchall needs another symbol to
lagergren@57 212 * store its throwable
lagergren@57 213 * @param finallyCatchAll a symbol for the finally catch all exception
lagergren@211 214 * @return new TryNode or same if unchanged
lagergren@211 215 *
lagergren@211 216 * TODO can this still be stateful?
lagergren@57 217 */
lagergren@211 218 public TryNode setFinallyCatchAll(final Symbol finallyCatchAll) {
lagergren@57 219 this.finallyCatchAll = finallyCatchAll;
lagergren@211 220 return this;
lagergren@57 221 }
lagergren@57 222
lagergren@57 223 /**
jlaskey@3 224 * Get the body of the finally clause for this try
jlaskey@3 225 * @return finally body, or null if no finally
jlaskey@3 226 */
jlaskey@3 227 public Block getFinallyBody() {
jlaskey@3 228 return finallyBody;
jlaskey@3 229 }
jlaskey@3 230
jlaskey@3 231 /**
jlaskey@3 232 * Set the finally body of this try
jlaskey@3 233 * @param finallyBody new finally body
lagergren@211 234 * @return new TryNode or same if unchanged
jlaskey@3 235 */
lagergren@211 236 public TryNode setFinallyBody(final Block finallyBody) {
lagergren@211 237 if (this.finallyBody == finallyBody) {
lagergren@211 238 return this;
lagergren@211 239 }
attila@963 240 return new TryNode(this, body, catchBlocks, finallyBody, conversion);
attila@963 241 }
attila@963 242
attila@963 243 @Override
attila@963 244 public JoinPredecessor setLocalVariableConversion(final LexicalContext lc, final LocalVariableConversion conversion) {
attila@963 245 if(this.conversion == conversion) {
attila@963 246 return this;
attila@963 247 }
attila@963 248 return new TryNode(this, body, catchBlocks, finallyBody, conversion);
attila@963 249 }
attila@963 250
attila@963 251 @Override
attila@963 252 public LocalVariableConversion getLocalVariableConversion() {
attila@963 253 return conversion;
jlaskey@3 254 }
jlaskey@3 255 }

mercurial