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

Thu, 23 May 2013 15:51:08 +0200

author
lagergren
date
Thu, 23 May 2013 15:51:08 +0200
changeset 290
6fc7b51e83d6
parent 253
fb1d7ea3e1b6
child 430
2c007a8bb0e7
permissions
-rw-r--r--

8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
Reviewed-by: attila, jlaskey

     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;
    30 import jdk.nashorn.internal.ir.annotations.Immutable;
    31 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
    33 /**
    34  * IR representation for RETURN or YIELD statements.
    35  */
    36 @Immutable
    37 public class ReturnNode extends Statement {
    38     /** Optional expression. */
    39     private final Node expression;
    41     /**
    42      * Constructor
    43      *
    44      * @param lineNumber line number
    45      * @param token      token
    46      * @param finish     finish
    47      * @param expression expression to return
    48      */
    49     public ReturnNode(final int lineNumber, final long token, final int finish, final Node expression) {
    50         super(lineNumber, token, finish);
    51         this.expression = expression;
    52     }
    54     private ReturnNode(final ReturnNode returnNode, final Node expression) {
    55         super(returnNode);
    56         this.expression = expression;
    57     }
    59     @Override
    60     public boolean isTerminal() {
    61         return true;
    62     }
    64     /**
    65      * Return true if is a RETURN node.
    66      * @return true if is RETURN node.
    67      */
    68     public boolean isReturn() {
    69         return isTokenType(RETURN);
    70     }
    72     /**
    73      * Check if this return node has an expression
    74      * @return true if not a void return
    75      */
    76     public boolean hasExpression() {
    77         return expression != null;
    78     }
    80     /**
    81      * Return true if is a YIELD node.
    82      * @return TRUE if is YIELD node.
    83      */
    84     public boolean isYield() {
    85         return isTokenType(YIELD);
    86     }
    88     @Override
    89     public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
    90         if (visitor.enterReturnNode(this)) {
    91             if (expression != null) {
    92                 return visitor.leaveReturnNode(setExpression(expression.accept(visitor)));
    93             }
    94             return visitor.leaveReturnNode(this);
    95         }
    97         return this;
    98     }
   101     @Override
   102     public void toString(final StringBuilder sb) {
   103         sb.append(isYield() ? "yield" : "return");
   104         if (expression != null) {
   105             sb.append(' ');
   106             expression.toString(sb);
   107         }
   108     }
   110     /**
   111      * Get the expression this node returns
   112      * @return return expression, or null if void return
   113      */
   114     public Node getExpression() {
   115         return expression;
   116     }
   118     /**
   119      * Reset the expression this node returns
   120      * @param expression new expression, or null if void return
   121      * @return new or same return node
   122      */
   123     public ReturnNode setExpression(final Node expression) {
   124         if (this.expression == expression) {
   125             return this;
   126         }
   127         return new ReturnNode(this, expression);
   128     }
   130 }

mercurial