SplitNode.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 java.util.ArrayList;
29import java.util.Collections;
30import java.util.HashMap;
31import java.util.List;
32import java.util.Map;
33import jdk.nashorn.internal.codegen.CompileUnit;
34import jdk.nashorn.internal.codegen.Label;
35import jdk.nashorn.internal.ir.annotations.Immutable;
36import jdk.nashorn.internal.ir.visitor.NodeVisitor;
37
38/**
39 * Node indicating code is split across classes.
40 */
41@Immutable
42public class SplitNode extends LexicalContextStatement implements Labels, CompileUnitHolder {
43    private static final long serialVersionUID = 1L;
44
45    /** Split node method name. */
46    private final String name;
47
48    /** Compilation unit. */
49    private final CompileUnit compileUnit;
50
51    /** Body of split code. */
52    private final Block body;
53
54    private Map<Label, JoinPredecessor> jumps;
55
56    /**
57     * Constructor
58     *
59     * @param name        name of split node
60     * @param body        body of split code
61     * @param compileUnit compile unit to use for the body
62     */
63    public SplitNode(final String name, final Block body, final CompileUnit compileUnit) {
64        super(body.getFirstStatementLineNumber(), body.getToken(), body.getFinish());
65        this.name        = name;
66        this.body        = body;
67        this.compileUnit = compileUnit;
68    }
69
70    private SplitNode(final SplitNode splitNode, final Block body, final CompileUnit compileUnit, final Map<Label, JoinPredecessor> jumps) {
71        super(splitNode);
72        this.name        = splitNode.name;
73        this.body        = body;
74        this.compileUnit = compileUnit;
75        this.jumps       = jumps;
76    }
77
78    /**
79     * Get the body for this split node - i.e. the actual code it encloses
80     * @return body for split node
81     */
82    public Node getBody() {
83        return body;
84    }
85
86    private SplitNode setBody(final LexicalContext lc, final Block body) {
87        if (this.body == body) {
88            return this;
89        }
90        return Node.replaceInLexicalContext(lc, this, new SplitNode(this, body, compileUnit, jumps));
91    }
92
93    @Override
94    public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) {
95        if (visitor.enterSplitNode(this)) {
96            return visitor.leaveSplitNode(setBody(lc, (Block)body.accept(visitor)));
97        }
98        return this;
99    }
100
101    @Override
102    public void toString(final StringBuilder sb, final boolean printType) {
103        sb.append("<split>(");
104        sb.append(compileUnit.getClass().getSimpleName());
105        sb.append(") ");
106        body.toString(sb, printType);
107    }
108
109    /**
110     * Get the name for this split node
111     * @return name
112     */
113    public String getName() {
114        return name;
115    }
116
117    /**
118     * Get the compile unit for this split node
119     * @return compile unit
120     */
121    @Override
122    public CompileUnit getCompileUnit() {
123        return compileUnit;
124    }
125
126    /**
127     * Set the compile unit for this split node
128     * @param lc lexical context
129     * @param compileUnit compile unit
130     * @return new node if changed, otherwise same node
131     */
132    public SplitNode setCompileUnit(final LexicalContext lc, final CompileUnit compileUnit) {
133        if (this.compileUnit == compileUnit) {
134            return this;
135        }
136        return Node.replaceInLexicalContext(lc, this, new SplitNode(this, body, compileUnit, jumps));
137    }
138
139    /**
140     * Adds a jump that crosses this split node's boundary (it originates within the split node, and goes to a target
141     * outside of it).
142     * @param jumpOrigin the join predecessor that's the origin of the jump
143     * @param targetLabel the label that's the target of the jump.
144     */
145    public void addJump(final JoinPredecessor jumpOrigin, final Label targetLabel) {
146        if (jumps == null) {
147            jumps = new HashMap<>();
148        }
149        jumps.put(targetLabel, jumpOrigin);
150    }
151
152    /**
153     * Returns the jump origin within this split node for a target.
154     * @param targetLabel the target for which a jump origin is sought.
155     * @return the jump origin, or null.
156     */
157    public JoinPredecessor getJumpOrigin(final Label targetLabel) {
158        return jumps == null ? null : jumps.get(targetLabel);
159    }
160
161    @Override
162    public List<Label> getLabels() {
163        return Collections.unmodifiableList(new ArrayList<>(jumps.keySet()));
164    }
165}
166