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