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 jdk.nashorn.internal.ir.annotations.Immutable;
29 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
30 import jdk.nashorn.internal.parser.TokenType;
31 
32 /**
33  * IR representation for executing bare expressions. Basically, an expression
34  * node means "this code will be executed" and evaluating it results in
35  * statements being added to the IR
36  */
37 @Immutable
38 public final class ExpressionStatement extends Statement {
39     private static final long serialVersionUID = 1L;
40 
41     /** Expression to execute. */
42     private final Expression expression;
43     private final TokenType destructuringDecl;
44 
45     /**
46      * Constructor
47      *
48      * @param lineNumber line number
49      * @param token      token
50      * @param finish     finish
51      * @param expression the expression to execute
52      * @param destructuringDecl does this statement represent a destructuring declaration?
53      */
ExpressionStatement(final int lineNumber, final long token, final int finish, final Expression expression, final TokenType destructuringDecl)54     public ExpressionStatement(final int lineNumber, final long token, final int finish,
55             final Expression expression, final TokenType destructuringDecl) {
56         super(lineNumber, token, finish);
57         this.expression = expression;
58         this.destructuringDecl = destructuringDecl;
59     }
60 
61     /**
62      * Constructor
63      *
64      * @param lineNumber line number
65      * @param token      token
66      * @param finish     finish
67      * @param expression the expression to execute
68      */
ExpressionStatement(final int lineNumber, final long token, final int finish, final Expression expression)69     public ExpressionStatement(final int lineNumber, final long token, final int finish, final Expression expression) {
70         this(lineNumber, token, finish, expression, null);
71     }
72 
ExpressionStatement(final ExpressionStatement expressionStatement, final Expression expression)73     private ExpressionStatement(final ExpressionStatement expressionStatement, final Expression expression) {
74         super(expressionStatement);
75         this.expression = expression;
76         this.destructuringDecl = null;
77     }
78 
79     @Override
accept(final NodeVisitor<? extends LexicalContext> visitor)80     public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
81         if (visitor.enterExpressionStatement(this)) {
82             return visitor.leaveExpressionStatement(setExpression((Expression)expression.accept(visitor)));
83         }
84 
85         return this;
86     }
87 
88     @Override
toString(final StringBuilder sb, final boolean printTypes)89     public void toString(final StringBuilder sb, final boolean printTypes) {
90         expression.toString(sb, printTypes);
91     }
92 
93     /**
94      * Return the expression to be executed
95      * @return the expression
96      */
getExpression()97     public Expression getExpression() {
98         return expression;
99     }
100 
101     /**
102      * Return declaration type if this expression statement is a destructuring declaration
103      *
104      * @return declaration type (LET, VAR, CONST) if destructuring declaration, null otherwise.
105      */
destructuringDeclarationType()106     public TokenType destructuringDeclarationType() {
107         return destructuringDecl;
108     }
109 
110     /**
111      * Reset the expression to be executed
112      * @param expression the expression
113      * @return new or same execute node
114      */
setExpression(final Expression expression)115     public ExpressionStatement setExpression(final Expression expression) {
116         if (this.expression == expression) {
117             return this;
118         }
119         return new ExpressionStatement(this, expression);
120     }
121 }
122