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