| 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.collect.ArrayListMultimap; |
| 21 |
import com.google.common.collect.ImmutableMap; |
| 22 |
import com.google.common.collect.Maps; |
| 23 |
import com.google.common.collect.Sets; |
| 24 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 25 |
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; |
| 26 |
import com.google.javascript.jscomp.Scope.Var; |
| 27 |
import com.google.javascript.rhino.Node; |
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.Comparator; |
| 30 |
import java.util.HashMap; |
| 31 |
import java.util.HashSet; |
| 32 |
import java.util.Iterator; |
| 33 |
import java.util.List; |
| 34 |
import java.util.Map; |
| 35 |
import java.util.Set; |
| 36 |
import java.util.SortedSet; |
| 37 |
import java.util.TreeSet; |
| 38 |
|
| 39 |
import javax.annotation.Nullable; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
final class RenameVars implements CompilerPass { |
| 47 |
private final AbstractCompiler compiler; |
| 48 |
|
| 49 |
|
| 50 |
private final ArrayList<Node> globalNameNodes = new ArrayList<Node>(); |
| 51 |
|
| 52 |
|
| 53 |
private final ArrayList<Node> localNameNodes = new ArrayList<Node>(); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private final Map<Node, String> pseudoNameMap; |
| 60 |
|
| 61 |
|
| 62 |
private final Set<String> externNames = new HashSet<String>(); |
| 63 |
|
| 64 |
|
| 65 |
private final Set<String> reservedNames; |
| 66 |
|
| 67 |
|
| 68 |
private final Map<String, String> renameMap = new HashMap<String, String>(); |
| 69 |
|
| 70 |
|
| 71 |
private final VariableMap prevUsedRenameMap; |
| 72 |
|
| 73 |
|
| 74 |
private final String prefix; |
| 75 |
|
| 76 |
|
| 77 |
private int assignmentCount = 0; |
| 78 |
|
| 79 |
|
| 80 |
private StringBuilder assignmentLog; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private Set<Var> localBleedingFunctions = Sets.newHashSet(); |
| 85 |
private ArrayListMultimap<Scope, Var> localBleedingFunctionsPerScope = |
| 86 |
ArrayListMultimap.create(); |
| 87 |
|
| 88 |
class Assignment { |
| 89 |
final String oldName; |
| 90 |
final int orderOfOccurrence; |
| 91 |
String newName; |
| 92 |
int count; |
| 93 |
|
| 94 |
Assignment(String name) { |
| 95 |
this.oldName = name; |
| 96 |
this.newName = null; |
| 97 |
this.count = 0; |
| 98 |
|
| 99 |
|
| 100 |
this.orderOfOccurrence = assignmentCount++; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
void setNewName(String newName) { |
| 107 |
Preconditions.checkState(this.newName == null); |
| 108 |
this.newName = newName; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
private final Map<String, Assignment> assignments = |
| 114 |
new HashMap<String, Assignment>(); |
| 115 |
|
| 116 |
|
| 117 |
private final boolean localRenamingOnly; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private boolean preserveFunctionExpressionNames; |
| 126 |
|
| 127 |
private final boolean shouldShadow; |
| 128 |
|
| 129 |
|
| 130 |
private final char[] reservedCharacters; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static final String LOCAL_VAR_PREFIX = "L "; |
| 135 |
|
| 136 |
RenameVars(AbstractCompiler compiler, String prefix, |
| 137 |
boolean localRenamingOnly, boolean preserveFunctionExpressionNames, |
| 138 |
boolean generatePseudoNames, boolean shouldShadow, |
| 139 |
VariableMap prevUsedRenameMap, |
| 140 |
@Nullable char[] reservedCharacters, |
| 141 |
@Nullable Set<String> reservedNames) { |
| 142 |
this.compiler = compiler; |
| 143 |
this.prefix = prefix == null ? "" : prefix; |
| 144 |
this.localRenamingOnly = localRenamingOnly; |
| 145 |
this.preserveFunctionExpressionNames = preserveFunctionExpressionNames; |
| 146 |
if (generatePseudoNames) { |
| 147 |
this.pseudoNameMap = Maps.newHashMap(); |
| 148 |
} else { |
| 149 |
this.pseudoNameMap = null; |
| 150 |
} |
| 151 |
this.prevUsedRenameMap = prevUsedRenameMap; |
| 152 |
this.reservedCharacters = reservedCharacters; |
| 153 |
this.shouldShadow = shouldShadow; |
| 154 |
if (reservedNames == null) { |
| 155 |
this.reservedNames = Sets.newHashSet(); |
| 156 |
} else { |
| 157 |
this.reservedNames = Sets.newHashSet(reservedNames); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
class ProcessVars extends AbstractPostOrderCallback |
| 185 |
implements ScopedCallback { |
| 186 |
private final boolean isExternsPass_; |
| 187 |
|
| 188 |
ProcessVars(boolean isExterns) { |
| 189 |
isExternsPass_ = isExterns; |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public void enterScope(NodeTraversal t) { |
| 194 |
if (t.inGlobalScope()) return; |
| 195 |
|
| 196 |
Iterator<Var> it = t.getScope().getVars(); |
| 197 |
while (it.hasNext()) { |
| 198 |
Var current = it.next(); |
| 199 |
if (current.isBleedingFunction()) { |
| 200 |
localBleedingFunctions.add(current); |
| 201 |
localBleedingFunctionsPerScope.put( |
| 202 |
t.getScope().getParent(), current); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public void exitScope(NodeTraversal t) {} |
| 209 |
|
| 210 |
@Override |
| 211 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 212 |
if (!n.isName()) { |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
String name = n.getString(); |
| 217 |
|
| 218 |
|
| 219 |
if (name.length() == 0) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
Scope.Var var = t.getScope().getVar(name); |
| 228 |
boolean local = (var != null) && var.isLocal() && |
| 229 |
(!var.scope.getParent().isGlobal() || |
| 230 |
!var.isBleedingFunction()); |
| 231 |
|
| 232 |
|
| 233 |
if (!local && localRenamingOnly) { |
| 234 |
reservedNames.add(name); |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
if (preserveFunctionExpressionNames && var != null |
| 240 |
&& NodeUtil.isFunctionExpression(var.getParentNode())) { |
| 241 |
reservedNames.add(name); |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
if (!okToRenameVar(name, local)) { |
| 247 |
if (local) { |
| 248 |
|
| 249 |
String newName = MakeDeclaredNamesUnique.ContextualRenameInverter |
| 250 |
.getOrginalName(name); |
| 251 |
if (!newName.equals(name)) { |
| 252 |
n.setString(newName); |
| 253 |
} |
| 254 |
} |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
if (isExternsPass_) { |
| 259 |
|
| 260 |
if (!local) { |
| 261 |
externNames.add(name); |
| 262 |
} |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
if (pseudoNameMap != null) { |
| 267 |
recordPseudoName(n); |
| 268 |
} |
| 269 |
|
| 270 |
if (local) { |
| 271 |
|
| 272 |
String tempName = LOCAL_VAR_PREFIX + getLocalVarIndex(var); |
| 273 |
incCount(tempName); |
| 274 |
localNameNodes.add(n); |
| 275 |
n.setString(tempName); |
| 276 |
} else if (var != null) { |
| 277 |
|
| 278 |
incCount(name); |
| 279 |
globalNameNodes.add(n); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
void incCount(String name) { |
| 285 |
Assignment s = assignments.get(name); |
| 286 |
if (s == null) { |
| 287 |
s = new Assignment(name); |
| 288 |
assignments.put(name, s); |
| 289 |
} |
| 290 |
s.count++; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
private static final Comparator<Assignment> FREQUENCY_COMPARATOR = |
| 299 |
new Comparator<Assignment>() { |
| 300 |
@Override |
| 301 |
public int compare(Assignment a1, Assignment a2) { |
| 302 |
if (a1.count != a2.count) { |
| 303 |
return a2.count - a1.count; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
return ORDER_OF_OCCURRENCE_COMPARATOR.compare(a1, a2); |
| 308 |
} |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
private static final Comparator<Assignment> ORDER_OF_OCCURRENCE_COMPARATOR = |
| 316 |
new Comparator<Assignment>() { |
| 317 |
@Override |
| 318 |
public int compare(Assignment a1, Assignment a2) { |
| 319 |
return a1.orderOfOccurrence - a2.orderOfOccurrence; |
| 320 |
} |
| 321 |
}; |
| 322 |
|
| 323 |
@Override |
| 324 |
public void process(Node externs, Node root) { |
| 325 |
assignmentLog = new StringBuilder(); |
| 326 |
|
| 327 |
|
| 328 |
NodeTraversal.traverse(compiler, externs, new ProcessVars(true)); |
| 329 |
NodeTraversal.traverse(compiler, root, new ProcessVars(false)); |
| 330 |
|
| 331 |
|
| 332 |
reservedNames.addAll(externNames); |
| 333 |
|
| 334 |
|
| 335 |
SortedSet<Assignment> varsByFrequency = |
| 336 |
new TreeSet<Assignment>(FREQUENCY_COMPARATOR); |
| 337 |
varsByFrequency.addAll(assignments.values()); |
| 338 |
|
| 339 |
if (shouldShadow) { |
| 340 |
new ShadowVariables( |
| 341 |
compiler, assignments, varsByFrequency, pseudoNameMap).process( |
| 342 |
externs, root); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
if (prevUsedRenameMap != null) { |
| 347 |
reusePreviouslyUsedVariableMap(); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
assignNames(varsByFrequency); |
| 352 |
|
| 353 |
boolean changed = false; |
| 354 |
|
| 355 |
|
| 356 |
for (Node n : globalNameNodes) { |
| 357 |
String newName = getNewGlobalName(n); |
| 358 |
|
| 359 |
if (newName != null) { |
| 360 |
n.setString(newName); |
| 361 |
changed = true; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
for (Node n : localNameNodes) { |
| 367 |
String newName = getNewLocalName(n); |
| 368 |
if (newName != null) { |
| 369 |
n.setString(newName); |
| 370 |
changed = true; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
if (changed) { |
| 375 |
compiler.reportCodeChange(); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
compiler.addToDebugLog("JS var assignments:\n" + assignmentLog); |
| 380 |
assignmentLog = null; |
| 381 |
} |
| 382 |
|
| 383 |
private String getNewGlobalName(Node n) { |
| 384 |
String oldName = n.getString(); |
| 385 |
Assignment a = assignments.get(oldName); |
| 386 |
if (a.newName != null && !a.newName.equals(oldName)) { |
| 387 |
if (pseudoNameMap != null) { |
| 388 |
return pseudoNameMap.get(n); |
| 389 |
} |
| 390 |
return a.newName; |
| 391 |
} else { |
| 392 |
return null; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
private String getNewLocalName(Node n) { |
| 397 |
String oldTempName = n.getString(); |
| 398 |
Assignment a = assignments.get(oldTempName); |
| 399 |
if (!a.newName.equals(oldTempName)) { |
| 400 |
if (pseudoNameMap != null) { |
| 401 |
return pseudoNameMap.get(n); |
| 402 |
} |
| 403 |
return a.newName; |
| 404 |
} |
| 405 |
return null; |
| 406 |
} |
| 407 |
|
| 408 |
private void recordPseudoName(Node n) { |
| 409 |
|
| 410 |
|
| 411 |
pseudoNameMap.put(n, '$' + n.getString() + "$$" ); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
private void reusePreviouslyUsedVariableMap() { |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
Preconditions.checkState( |
| 424 |
prevUsedRenameMap.getNewNameToOriginalNameMap() instanceof Map); |
| 425 |
for (Assignment a : assignments.values()) { |
| 426 |
String prevNewName = prevUsedRenameMap.lookupNewName(a.oldName); |
| 427 |
if (prevNewName == null || reservedNames.contains(prevNewName)) { |
| 428 |
continue; |
| 429 |
} |
| 430 |
|
| 431 |
if (a.oldName.startsWith(LOCAL_VAR_PREFIX) |
| 432 |
|| (!externNames.contains(a.oldName) |
| 433 |
&& prevNewName.startsWith(prefix))) { |
| 434 |
reservedNames.add(prevNewName); |
| 435 |
finalizeNameAssignment(a, prevNewName); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
private void assignNames(SortedSet<Assignment> varsToRename) { |
| 444 |
NameGenerator globalNameGenerator = |
| 445 |
new NameGenerator(reservedNames, prefix, reservedCharacters); |
| 446 |
|
| 447 |
|
| 448 |
NameGenerator localNameGenerator = |
| 449 |
prefix.isEmpty() ? globalNameGenerator : new NameGenerator( |
| 450 |
reservedNames, "", reservedCharacters); |
| 451 |
|
| 452 |
|
| 453 |
List<Assignment> pendingAssignments = new ArrayList<Assignment>(); |
| 454 |
List<String> generatedNamesForAssignments = new ArrayList<String>(); |
| 455 |
|
| 456 |
for (Assignment a : varsToRename) { |
| 457 |
if (a.newName != null) { |
| 458 |
continue; |
| 459 |
} |
| 460 |
|
| 461 |
if (externNames.contains(a.oldName)) { |
| 462 |
continue; |
| 463 |
} |
| 464 |
|
| 465 |
String newName; |
| 466 |
if (a.oldName.startsWith(LOCAL_VAR_PREFIX)) { |
| 467 |
|
| 468 |
newName = localNameGenerator.generateNextName(); |
| 469 |
finalizeNameAssignment(a, newName); |
| 470 |
} else { |
| 471 |
|
| 472 |
|
| 473 |
newName = globalNameGenerator.generateNextName(); |
| 474 |
pendingAssignments.add(a); |
| 475 |
generatedNamesForAssignments.add(newName); |
| 476 |
} |
| 477 |
reservedNames.add(newName); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
int numPendingAssignments = generatedNamesForAssignments.size(); |
| 495 |
for (int i = 0; i < numPendingAssignments;) { |
| 496 |
SortedSet<Assignment> varsByOrderOfOccurrence = |
| 497 |
new TreeSet<Assignment>(ORDER_OF_OCCURRENCE_COMPARATOR); |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
int len = generatedNamesForAssignments.get(i).length(); |
| 502 |
for (int j = i; j < numPendingAssignments |
| 503 |
&& generatedNamesForAssignments.get(j).length() == len; j++) { |
| 504 |
varsByOrderOfOccurrence.add(pendingAssignments.get(j)); |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
for (Assignment a : varsByOrderOfOccurrence) { |
| 509 |
finalizeNameAssignment(a, generatedNamesForAssignments.get(i)); |
| 510 |
++i; |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
private void finalizeNameAssignment(Assignment a, String newName) { |
| 519 |
a.setNewName(newName); |
| 520 |
|
| 521 |
|
| 522 |
renameMap.put(a.oldName, newName); |
| 523 |
|
| 524 |
|
| 525 |
assignmentLog.append(a.oldName).append(" => ").append(newName).append('\n'); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
VariableMap getVariableMap() { |
| 532 |
return new VariableMap(ImmutableMap.copyOf(renameMap)); |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
private boolean okToRenameVar(String name, boolean isLocal) { |
| 539 |
return !compiler.getCodingConvention().isExported(name, isLocal); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
private int getLocalVarIndex(Var v) { |
| 548 |
int num = v.index; |
| 549 |
Scope s = v.scope.getParent(); |
| 550 |
if (s == null) { |
| 551 |
throw new IllegalArgumentException("Var is not local"); |
| 552 |
} |
| 553 |
|
| 554 |
boolean isBleedingIntoScope = s.getParent() != null && |
| 555 |
localBleedingFunctions.contains(v); |
| 556 |
|
| 557 |
while (s.getParent() != null) { |
| 558 |
if (isBleedingIntoScope) { |
| 559 |
num += localBleedingFunctionsPerScope.get(s).indexOf(v) + 1; |
| 560 |
isBleedingIntoScope = false; |
| 561 |
} else { |
| 562 |
num += localBleedingFunctionsPerScope.get(s).size(); |
| 563 |
} |
| 564 |
|
| 565 |
num += s.getVarCount(); |
| 566 |
s = s.getParent(); |
| 567 |
} |
| 568 |
return num; |
| 569 |
} |
| 570 |
} |