src/jdk/nashorn/internal/ir/ReturnNode.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

     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  */
    26 package jdk.nashorn.internal.ir;
    28 import static jdk.nashorn.internal.parser.TokenType.RETURN;
    29 import static jdk.nashorn.internal.parser.TokenType.YIELD;
    31 import jdk.nashorn.internal.ir.annotations.Ignore;
    32 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
    33 import jdk.nashorn.internal.runtime.Source;
    35 /**
    36  * IR representation for RETURN or YIELD statements.
    37  *
    38  */
    39 public class ReturnNode extends Node {
    40     /** Optional expression. */
    41     private Node expression;
    43     /** Try chain. */
    44     @Ignore
    45     private final TryNode tryChain;
    47     /**
    48      * Constructor
    49      *
    50      * @param source     the source
    51      * @param token      token
    52      * @param finish     finish
    53      * @param expression expression to return
    54      * @param tryChain   surrounding try chain.
    55      */
    56     public ReturnNode(final Source source, final long token, final int finish, final Node expression, final TryNode tryChain) {
    57         super(source, token, finish);
    59         this.expression = expression;
    60         this.tryChain   = tryChain;
    62         setIsTerminal(true);
    63     }
    65     private ReturnNode(final ReturnNode returnNode, final CopyState cs) {
    66         super(returnNode);
    68         this.expression = cs.existingOrCopy(returnNode.expression);
    69         this.tryChain   = (TryNode)cs.existingOrSame(returnNode.tryChain);
    70     }
    72     @Override
    73     protected Node copy(final CopyState cs) {
    74         return new ReturnNode(this, cs);
    75     }
    77     /**
    78      * Return true if is a RETURN node.
    79      * @return true if is RETURN node.
    80      */
    81     public boolean isReturn() {
    82         return isTokenType(RETURN);
    83     }
    85     /**
    86      * Check if this return node has an expression
    87      * @return true if not a void return
    88      */
    89     public boolean hasExpression() {
    90         return expression != null;
    91     }
    93     /**
    94      * Return true if is a YIELD node.
    95      * @return TRUE if is YIELD node.
    96      */
    97     public boolean isYield() {
    98         return isTokenType(YIELD);
    99     }
   101     @Override
   102     public Node accept(final NodeVisitor visitor) {
   103         if (visitor.enterReturnNode(this) != null) {
   104             if (expression != null) {
   105                 expression = expression.accept(visitor);
   106             }
   108             return visitor.leaveReturnNode(this);
   109         }
   111         return this;
   112     }
   115     @Override
   116     public void toString(final StringBuilder sb) {
   117         sb.append(isYield() ? "yield" : "return ");
   119         if (expression != null) {
   120             expression.toString(sb);
   121         }
   122     }
   124     @Override
   125     public boolean equals(final Object other) {
   126         if (other instanceof ReturnNode) {
   127             final ReturnNode otherReturn = (ReturnNode)other;
   128             if (hasExpression() != otherReturn.hasExpression()) {
   129                 return false;
   130             } else if (hasExpression()) {
   131                 return otherReturn.getExpression().equals(getExpression());
   132             }
   133             return true;
   134         }
   135         return false;
   136     }
   138     @Override
   139     public int hashCode() {
   140         return 0x4711_17 ^ (expression == null ? 0 : expression.hashCode());
   141     }
   143     /**
   144      * Get the expression this node returns
   145      * @return return expression, or null if void return
   146      */
   147     public Node getExpression() {
   148         return expression;
   149     }
   151     /**
   152      * Reset the expression this node returns
   153      * @param expression new expression, or null if void return
   154      */
   155     public void setExpression(final Node expression) {
   156         this.expression = expression;
   157     }
   159     /**
   160      * Get the surrounding try chain for this return node
   161      * @return try chain
   162      */
   163     public TryNode getTryChain() {
   164         return tryChain;
   165     }
   166 }

mercurial