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