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