ExpressionStatement.java revision 1068:34ef988d5959
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
26package jdk.nashorn.internal.ir;
27
28import jdk.nashorn.internal.ir.annotations.Immutable;
29import jdk.nashorn.internal.ir.visitor.NodeVisitor;
30
31/**
32 * IR representation for executing bare expressions. Basically, an expression
33 * node means "this code will be executed" and evaluating it results in
34 * statements being added to the IR
35 */
36@Immutable
37public final class ExpressionStatement extends Statement {
38    private static final long serialVersionUID = 1L;
39
40    /** Expression to execute. */
41    private final Expression expression;
42
43    /**
44     * Constructor
45     *
46     * @param lineNumber line number
47     * @param token      token
48     * @param finish     finish
49     * @param expression the expression to execute
50     */
51    public ExpressionStatement(final int lineNumber, final long token, final int finish, final Expression expression) {
52        super(lineNumber, token, finish);
53        this.expression = expression;
54    }
55
56    private ExpressionStatement(final ExpressionStatement expressionStatement, final Expression expression) {
57        super(expressionStatement);
58        this.expression = expression;
59    }
60
61    @Override
62    public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
63        if (visitor.enterExpressionStatement(this)) {
64            return visitor.leaveExpressionStatement(setExpression((Expression)expression.accept(visitor)));
65        }
66
67        return this;
68    }
69
70    @Override
71    public void toString(final StringBuilder sb, final boolean printTypes) {
72        expression.toString(sb, printTypes);
73    }
74
75    /**
76     * Return the expression to be executed
77     * @return the expression
78     */
79    public Expression getExpression() {
80        return expression;
81    }
82
83    /**
84     * Reset the expression to be executed
85     * @param expression the expression
86     * @return new or same execute node
87     */
88    public ExpressionStatement setExpression(final Expression expression) {
89        if (this.expression == expression) {
90            return this;
91        }
92        return new ExpressionStatement(this, expression);
93    }
94}
95