WithNode.java revision 1060:ca67ae7c46cb
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 {@code with} statements.
33 */
34@Immutable
35public final class WithNode extends LexicalContextStatement {
36   /** This expression. */
37    private final Expression expression;
38
39    /** Statements. */
40    private final Block body;
41
42    /**
43     * Constructor
44     *
45     * @param lineNumber Line number of the header
46     * @param token      First token
47     * @param finish     Character index of the last token
48     * @param expression With expression
49     * @param body       Body of with node
50     */
51    public WithNode(final int lineNumber, final long token, final int finish, final Expression expression, final Block body) {
52        super(lineNumber, token, finish);
53        this.expression = expression;
54        this.body       = body;
55    }
56
57    private WithNode(final WithNode node, final Expression expression, final Block body) {
58        super(node);
59        this.expression = expression;
60        this.body       = body;
61    }
62
63    /**
64     * Assist in IR navigation.
65     *
66     * @param visitor IR navigating visitor.
67     */
68    @Override
69    public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) {
70        if (visitor.enterWithNode(this)) {
71             return visitor.leaveWithNode(
72                setExpression(lc, (Expression)expression.accept(visitor)).
73                setBody(lc, (Block)body.accept(visitor)));
74        }
75        return this;
76    }
77
78    @Override
79    public boolean isTerminal() {
80        return body.isTerminal();
81    }
82
83    @Override
84    public void toString(final StringBuilder sb, final boolean printType) {
85        sb.append("with (");
86        expression.toString(sb, printType);
87        sb.append(')');
88    }
89
90    /**
91     * Get the body of this WithNode
92     * @return the body
93     */
94    public Block getBody() {
95        return body;
96    }
97
98    /**
99     * Reset the body of this with node
100     * @param lc lexical context
101     * @param body new body
102     * @return new or same withnode
103     */
104    public WithNode setBody(final LexicalContext lc, final Block body) {
105        if (this.body == body) {
106            return this;
107        }
108        return Node.replaceInLexicalContext(lc, this, new WithNode(this, expression, body));
109    }
110
111    /**
112     * Get the expression of this WithNode
113     * @return the expression
114     */
115    public Expression getExpression() {
116        return expression;
117    }
118
119    /**
120     * Reset the expression of this with node
121     * @param lc lexical context
122     * @param expression new expression
123     * @return new or same withnode
124     */
125    public WithNode setExpression(final LexicalContext lc, final Expression expression) {
126        if (this.expression == expression) {
127            return this;
128        }
129        return Node.replaceInLexicalContext(lc, this, new WithNode(this, expression, body));
130    }
131}
132
133