| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.base.Supplier; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import com.google.javascript.rhino.Token; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.Deque; |
| 28 |
import java.util.HashMap; |
| 29 |
import java.util.HashSet; |
| 30 |
import java.util.Map; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
final class RenameLabels implements CompilerPass { |
| 72 |
private final AbstractCompiler compiler; |
| 73 |
private final Supplier<String> nameSupplier; |
| 74 |
private final boolean removeUnused; |
| 75 |
|
| 76 |
RenameLabels(AbstractCompiler compiler) { |
| 77 |
this(compiler, new DefaultNameSupplier(), true); |
| 78 |
} |
| 79 |
|
| 80 |
RenameLabels( |
| 81 |
AbstractCompiler compiler, |
| 82 |
Supplier<String> supplier, |
| 83 |
boolean removeUnused) { |
| 84 |
this.compiler = compiler; |
| 85 |
this.nameSupplier = supplier; |
| 86 |
this.removeUnused = removeUnused; |
| 87 |
} |
| 88 |
|
| 89 |
static class DefaultNameSupplier implements Supplier<String> { |
| 90 |
|
| 91 |
final NameGenerator nameGenerator = |
| 92 |
new NameGenerator(new HashSet<String>(), "", null); |
| 93 |
|
| 94 |
@Override |
| 95 |
public String get() { |
| 96 |
return nameGenerator.generateNextName(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
class ProcessLabels implements ScopedCallback { |
| 104 |
|
| 105 |
ProcessLabels() { |
| 106 |
|
| 107 |
namespaceStack.push(new LabelNamespace()); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
final Deque<LabelNamespace> namespaceStack = Lists.newLinkedList(); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
final ArrayList<String> names = new ArrayList<String>(); |
| 117 |
|
| 118 |
|
| 119 |
@Override |
| 120 |
public void enterScope(NodeTraversal nodeTraversal) { |
| 121 |
|
| 122 |
namespaceStack.push(new LabelNamespace()); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void exitScope(NodeTraversal nodeTraversal) { |
| 127 |
namespaceStack.pop(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
@Override |
| 137 |
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node node, |
| 138 |
Node parent) { |
| 139 |
if (node.isLabel()) { |
| 140 |
|
| 141 |
LabelNamespace current = namespaceStack.peek(); |
| 142 |
int currentDepth = current.renameMap.size() + 1; |
| 143 |
String name = node.getFirstChild().getString(); |
| 144 |
|
| 145 |
|
| 146 |
LabelInfo li = new LabelInfo(currentDepth); |
| 147 |
Preconditions.checkState(!current.renameMap.containsKey(name)); |
| 148 |
current.renameMap.put(name, li); |
| 149 |
|
| 150 |
|
| 151 |
if (names.size() < currentDepth) { |
| 152 |
names.add(nameSupplier.get()); |
| 153 |
} |
| 154 |
|
| 155 |
String newName = getNameForId(currentDepth); |
| 156 |
compiler.addToDebugLog("label renamed: " + name + " => " + newName); |
| 157 |
} |
| 158 |
|
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
@Override |
| 169 |
public void visit(NodeTraversal nodeTraversal, Node node, Node parent) { |
| 170 |
switch (node.getType()) { |
| 171 |
case Token.LABEL: |
| 172 |
visitLabel(node, parent); |
| 173 |
break; |
| 174 |
|
| 175 |
case Token.BREAK: |
| 176 |
case Token.CONTINUE: |
| 177 |
visitBreakOrContinue(node); |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
private void visitBreakOrContinue(Node node) { |
| 187 |
Node nameNode = node.getFirstChild(); |
| 188 |
if (nameNode != null) { |
| 189 |
|
| 190 |
String name = nameNode.getString(); |
| 191 |
Preconditions.checkState(name.length() != 0); |
| 192 |
LabelInfo li = getLabelInfo(name); |
| 193 |
if (li != null) { |
| 194 |
String newName = getNameForId(li.id); |
| 195 |
|
| 196 |
li.referenced = true; |
| 197 |
if (!name.equals(newName)) { |
| 198 |
|
| 199 |
nameNode.setString(newName); |
| 200 |
compiler.reportCodeChange(); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
private void visitLabel(Node node, Node parent) { |
| 212 |
Node nameNode = node.getFirstChild(); |
| 213 |
Preconditions.checkState(nameNode != null); |
| 214 |
String name = nameNode.getString(); |
| 215 |
LabelInfo li = getLabelInfo(name); |
| 216 |
|
| 217 |
if (li.referenced || !removeUnused) { |
| 218 |
String newName = getNameForId(li.id); |
| 219 |
if (!name.equals(newName)) { |
| 220 |
|
| 221 |
nameNode.setString(newName); |
| 222 |
compiler.reportCodeChange(); |
| 223 |
} |
| 224 |
} else { |
| 225 |
|
| 226 |
Node newChild = node.getLastChild(); |
| 227 |
node.removeChild(newChild); |
| 228 |
parent.replaceChild(node, newChild); |
| 229 |
if (newChild.isBlock()) { |
| 230 |
NodeUtil.tryMergeBlock(newChild); |
| 231 |
} |
| 232 |
compiler.reportCodeChange(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
namespaceStack.peek().renameMap.remove(name); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
String getNameForId(int id) { |
| 245 |
return names.get(id - 1); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
LabelInfo getLabelInfo(String name) { |
| 253 |
return namespaceStack.peek().renameMap.get(name); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
@Override |
| 258 |
public void process(Node externs, Node root) { |
| 259 |
|
| 260 |
NodeTraversal.traverse(compiler, root, new ProcessLabels()); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
private static class LabelInfo { |
| 265 |
boolean referenced = false; |
| 266 |
final int id; |
| 267 |
|
| 268 |
LabelInfo(int id) { |
| 269 |
this.id = id; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
private static class LabelNamespace { |
| 275 |
final Map<String, LabelInfo> renameMap = new HashMap<String, LabelInfo>(); |
| 276 |
} |
| 277 |
|
| 278 |
} |