| 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.Predicate; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import com.google.common.collect.Maps; |
| 23 |
import com.google.common.collect.Sets; |
| 24 |
import com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback; |
| 25 |
import com.google.javascript.jscomp.ReferenceCollectingCallback.Reference; |
| 26 |
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection; |
| 27 |
import com.google.javascript.jscomp.Scope.Var; |
| 28 |
import com.google.javascript.jscomp.VariableVisibilityAnalysis.VariableVisibility; |
| 29 |
import com.google.javascript.rhino.Node; |
| 30 |
import com.google.javascript.rhino.Token; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.Map; |
| 34 |
import java.util.Set; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class SideEffectsAnalysis implements CompilerPass { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
enum LocationAbstractionMode { |
| 59 |
|
| 60 |
DEGENERATE, |
| 61 |
|
| 62 |
VISIBILITY_BASED |
| 63 |
} |
| 64 |
|
| 65 |
private static final Predicate<Node> NOT_FUNCTION_PREDICATE = |
| 66 |
new Predicate<Node>() { |
| 67 |
@Override |
| 68 |
public boolean apply(Node input) { |
| 69 |
return !input.isFunction(); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
private AbstractCompiler compiler; |
| 74 |
|
| 75 |
|
| 76 |
private LocationAbstraction locationAbstraction; |
| 77 |
|
| 78 |
|
| 79 |
private final LocationAbstractionMode locationAbstractionIdentifier; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public SideEffectsAnalysis(AbstractCompiler compiler, |
| 91 |
LocationAbstractionMode locationAbstractionMode) { |
| 92 |
this.compiler = compiler; |
| 93 |
|
| 94 |
this.locationAbstractionIdentifier = locationAbstractionMode; |
| 95 |
} |
| 96 |
|
| 97 |
public SideEffectsAnalysis(AbstractCompiler compiler) { |
| 98 |
this(compiler, LocationAbstractionMode.DEGENERATE); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void process(Node externs, Node root) { |
| 103 |
switch(locationAbstractionIdentifier) { |
| 104 |
case DEGENERATE: |
| 105 |
locationAbstraction = new DegenerateLocationAbstraction(); |
| 106 |
break; |
| 107 |
case VISIBILITY_BASED: |
| 108 |
locationAbstraction = createVisibilityAbstraction(externs, root); |
| 109 |
break; |
| 110 |
default: |
| 111 |
throw new IllegalStateException("Unrecognized location abstraction " + |
| 112 |
"identifier: " + locationAbstractionIdentifier); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
private LocationAbstraction createVisibilityAbstraction(Node externs, |
| 122 |
Node root) { |
| 123 |
VariableVisibilityAnalysis variableVisibility = |
| 124 |
new VariableVisibilityAnalysis(compiler); |
| 125 |
|
| 126 |
variableVisibility.process(externs, root); |
| 127 |
|
| 128 |
VariableUseDeclarationMap variableMap = |
| 129 |
new VariableUseDeclarationMap(compiler); |
| 130 |
|
| 131 |
variableMap.mapUses(root); |
| 132 |
|
| 133 |
return new VisibilityLocationAbstraction(compiler, |
| 134 |
variableVisibility, variableMap); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public boolean safeToMoveBefore(Node source, |
| 155 |
AbstractMotionEnvironment environment, |
| 156 |
Node destination) { |
| 157 |
Preconditions.checkNotNull(locationAbstraction); |
| 158 |
Preconditions.checkArgument(!nodeHasAncestor(destination, source)); |
| 159 |
|
| 160 |
|
| 161 |
if (isPure(source)) { |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
if (nodeHasCall(source)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
LocationSummary sourceLocationSummary = |
| 171 |
locationAbstraction.calculateLocationSummary(source); |
| 172 |
|
| 173 |
EffectLocation sourceModSet = sourceLocationSummary.getModSet(); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if (!sourceModSet.isEmpty() && |
| 178 |
!nodesHaveSameControlFlow(source, destination)) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
EffectLocation sourceRefSet = sourceLocationSummary.getRefSet(); |
| 183 |
|
| 184 |
Set<Node> environmentNodes = environment.calculateEnvironment(); |
| 185 |
|
| 186 |
for (Node environmentNode : environmentNodes) { |
| 187 |
if (nodeHasCall(environmentNode)) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
LocationSummary environmentLocationSummary = |
| 193 |
locationAbstraction.calculateLocationSummary(environmentNodes); |
| 194 |
|
| 195 |
EffectLocation environmentModSet = environmentLocationSummary.getModSet(); |
| 196 |
|
| 197 |
EffectLocation environmentRefSet = environmentLocationSummary.getRefSet(); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
if (!environmentModSet.intersectsLocation(sourceRefSet) |
| 210 |
&& !environmentRefSet.intersectsLocation(sourceModSet) |
| 211 |
&& !environmentModSet.intersectsLocation(sourceModSet)) { |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private boolean isPure(Node node) { |
| 224 |
|
| 225 |
|
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
private static boolean nodesHaveSameControlFlow(Node node1, Node node2) { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
Node node1DeepestControlDependentBlock = |
| 261 |
closestControlDependentAncestor(node1); |
| 262 |
|
| 263 |
Node node2DeepestControlDependentBlock = |
| 264 |
closestControlDependentAncestor(node2); |
| 265 |
|
| 266 |
if (node1DeepestControlDependentBlock == |
| 267 |
node2DeepestControlDependentBlock) { |
| 268 |
|
| 269 |
if (node2DeepestControlDependentBlock != null) { |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
if (node2DeepestControlDependentBlock.isCase()) { |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
Predicate<Node> isEarlyExitPredicate = new Predicate<Node>() { |
| 301 |
@Override |
| 302 |
public boolean apply(Node input) { |
| 303 |
int nodeType = input.getType(); |
| 304 |
|
| 305 |
return nodeType == Token.RETURN |
| 306 |
|| nodeType == Token.BREAK |
| 307 |
|| nodeType == Token.CONTINUE; |
| 308 |
} |
| 309 |
}; |
| 310 |
|
| 311 |
return !NodeUtil.has(node2DeepestControlDependentBlock, |
| 312 |
isEarlyExitPredicate, NOT_FUNCTION_PREDICATE); |
| 313 |
} else { |
| 314 |
return true; |
| 315 |
} |
| 316 |
} else { |
| 317 |
return false; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
private static boolean isControlDependentChild(Node child) { |
| 332 |
Node parent = child.getParent(); |
| 333 |
|
| 334 |
if (parent == null) { |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
ArrayList<Node> siblings = Lists.newArrayList(parent.children()); |
| 339 |
|
| 340 |
int indexOfChildInParent = siblings.indexOf(child); |
| 341 |
|
| 342 |
switch(parent.getType()) { |
| 343 |
case Token.IF: |
| 344 |
case Token.HOOK: |
| 345 |
return (indexOfChildInParent == 1 || indexOfChildInParent == 2); |
| 346 |
case Token.WHILE: |
| 347 |
case Token.DO: |
| 348 |
return true; |
| 349 |
case Token.FOR: |
| 350 |
|
| 351 |
return indexOfChildInParent != 0; |
| 352 |
case Token.SWITCH: |
| 353 |
return indexOfChildInParent > 0; |
| 354 |
case Token.AND: |
| 355 |
return true; |
| 356 |
case Token.OR: |
| 357 |
return true; |
| 358 |
case Token.FUNCTION: |
| 359 |
return true; |
| 360 |
|
| 361 |
default: |
| 362 |
return false; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
private static Node closestControlDependentAncestor(Node node) { |
| 367 |
if (isControlDependentChild(node)) { |
| 368 |
return node; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
for (Node ancestor : node.getAncestors()) { |
| 373 |
if (isControlDependentChild(ancestor)) { |
| 374 |
return ancestor; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
return null; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
private static boolean nodeHasAncestor(Node node, Node possibleAncestor) { |
| 386 |
|
| 387 |
|
| 388 |
for (Node ancestor : node.getAncestors()) { |
| 389 |
if (ancestor == possibleAncestor) { |
| 390 |
return true; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
return false; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
private boolean nodeHasCall(Node node) { |
| 401 |
return NodeUtil.has(node, new Predicate<Node>() { |
| 402 |
@Override |
| 403 |
public boolean apply(Node input) { |
| 404 |
return input.isCall() || input.isNew(); |
| 405 |
}}, |
| 406 |
NOT_FUNCTION_PREDICATE); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public abstract static class AbstractMotionEnvironment { |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public abstract Set<Node> calculateEnvironment(); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
public static class IntraproceduralMotionEnvironment |
| 436 |
extends AbstractMotionEnvironment { |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public IntraproceduralMotionEnvironment( |
| 448 |
ControlFlowGraph<Node> controlFlowGraph, |
| 449 |
Node cfgSource, |
| 450 |
Node cfgDestination) { |
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
@Override |
| 455 |
public Set<Node> calculateEnvironment() { |
| 456 |
|
| 457 |
return null; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
public static class CrossModuleMotionEnvironment |
| 468 |
extends AbstractMotionEnvironment { |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public CrossModuleMotionEnvironment(Node sourceNode, |
| 480 |
JSModule sourceModule, |
| 481 |
Node destinationNode, |
| 482 |
JSModule destinationModule, |
| 483 |
JSModuleGraph moduleGraph) { |
| 484 |
|
| 485 |
} |
| 486 |
|
| 487 |
@Override |
| 488 |
public Set<Node> calculateEnvironment() { |
| 489 |
|
| 490 |
return null; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
public static class RawMotionEnvironment |
| 499 |
extends AbstractMotionEnvironment { |
| 500 |
Set<Node> environment; |
| 501 |
|
| 502 |
public RawMotionEnvironment(Set<Node> environment) { |
| 503 |
this.environment = environment; |
| 504 |
} |
| 505 |
|
| 506 |
@Override |
| 507 |
public Set<Node> calculateEnvironment() { |
| 508 |
return environment; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
private static class LocationSummary { |
| 519 |
|
| 520 |
private EffectLocation modSet; |
| 521 |
private EffectLocation refSet; |
| 522 |
|
| 523 |
public LocationSummary(EffectLocation modSet, EffectLocation refSet) { |
| 524 |
this.modSet = modSet; |
| 525 |
this.refSet = refSet; |
| 526 |
} |
| 527 |
|
| 528 |
public EffectLocation getModSet() { |
| 529 |
return modSet; |
| 530 |
} |
| 531 |
|
| 532 |
public EffectLocation getRefSet() { |
| 533 |
return refSet; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
private static interface EffectLocation { |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
public boolean intersectsLocation(EffectLocation otherLocation); |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
public EffectLocation join(EffectLocation otherLocation); |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public boolean isEmpty(); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
private abstract static class LocationAbstraction { |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
abstract LocationSummary calculateLocationSummary(Node node); |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
abstract EffectLocation getBottomLocation(); |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
public LocationSummary calculateLocationSummary(Set<Node> nodes) { |
| 604 |
EffectLocation modAccumulator = getBottomLocation(); |
| 605 |
EffectLocation refAccumulator = getBottomLocation(); |
| 606 |
|
| 607 |
for (Node node : nodes) { |
| 608 |
LocationSummary nodeLocationSummary = calculateLocationSummary(node); |
| 609 |
|
| 610 |
modAccumulator = modAccumulator.join(nodeLocationSummary.getModSet()); |
| 611 |
refAccumulator = refAccumulator.join(nodeLocationSummary.getRefSet()); |
| 612 |
} |
| 613 |
|
| 614 |
return new LocationSummary(modAccumulator, refAccumulator); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
private static class DegenerateLocationAbstraction |
| 627 |
extends LocationAbstraction { |
| 628 |
|
| 629 |
private static final EffectLocation EVERY_LOCATION = |
| 630 |
new DegenerateEffectLocation(); |
| 631 |
|
| 632 |
private static final EffectLocation NO_LOCATION = |
| 633 |
new DegenerateEffectLocation(); |
| 634 |
|
| 635 |
@Override |
| 636 |
EffectLocation getBottomLocation() { |
| 637 |
return NO_LOCATION; |
| 638 |
} |
| 639 |
|
| 640 |
@Override |
| 641 |
public LocationSummary calculateLocationSummary(Node node) { |
| 642 |
return new LocationSummary(calculateModSet(node), calculateRefSet(node)); |
| 643 |
} |
| 644 |
|
| 645 |
EffectLocation calculateRefSet(Node node) { |
| 646 |
if (NodeUtil.canBeSideEffected(node)) { |
| 647 |
return EVERY_LOCATION; |
| 648 |
} else { |
| 649 |
return NO_LOCATION; |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
EffectLocation calculateModSet(Node node) { |
| 654 |
if (NodeUtil.mayHaveSideEffects(node)) { |
| 655 |
return EVERY_LOCATION; |
| 656 |
} else { |
| 657 |
return NO_LOCATION; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
private static class DegenerateEffectLocation implements EffectLocation { |
| 662 |
@Override |
| 663 |
public EffectLocation join(EffectLocation otherLocation) { |
| 664 |
if (otherLocation == EVERY_LOCATION) { |
| 665 |
return otherLocation; |
| 666 |
} else { |
| 667 |
return this; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
@Override |
| 672 |
public boolean intersectsLocation(EffectLocation otherLocation) { |
| 673 |
return this == EVERY_LOCATION && otherLocation == EVERY_LOCATION; |
| 674 |
} |
| 675 |
|
| 676 |
@Override |
| 677 |
public boolean isEmpty() { |
| 678 |
return this == NO_LOCATION; |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
private static class VisibilityLocationAbstraction |
| 710 |
extends LocationAbstraction { |
| 711 |
|
| 712 |
|
| 713 |
private static final int VISIBILITY_LOCATION_NONE = 0; |
| 714 |
|
| 715 |
|
| 716 |
private static final int UNKNOWN_LOCATION_MASK = 0xFFFFFFFF; |
| 717 |
|
| 718 |
private static final int LOCAL_VARIABLE_LOCATION_MASK = 1 << 1; |
| 719 |
|
| 720 |
private static final int CAPTURED_LOCAL_VARIABLE_LOCATION_MASK = 1 << 2; |
| 721 |
|
| 722 |
private static final int GLOBAL_VARIABLE_LOCATION_MASK = 1 << 3; |
| 723 |
|
| 724 |
private static final int HEAP_LOCATION_MASK = 1 << 4; |
| 725 |
|
| 726 |
AbstractCompiler compiler; |
| 727 |
|
| 728 |
VariableVisibilityAnalysis variableVisibilityAnalysis; |
| 729 |
VariableUseDeclarationMap variableUseMap; |
| 730 |
|
| 731 |
private VisibilityLocationAbstraction(AbstractCompiler compiler, |
| 732 |
VariableVisibilityAnalysis variableVisibilityAnalysis, |
| 733 |
VariableUseDeclarationMap variableUseMap) { |
| 734 |
this.compiler = compiler; |
| 735 |
this.variableVisibilityAnalysis = variableVisibilityAnalysis; |
| 736 |
this.variableUseMap = variableUseMap; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
@Override |
| 743 |
LocationSummary calculateLocationSummary(Node node) { |
| 744 |
int visibilityRefLocations = VISIBILITY_LOCATION_NONE; |
| 745 |
int visibilityModLocations = VISIBILITY_LOCATION_NONE; |
| 746 |
|
| 747 |
for (Node reference : findStorageLocationReferences(node)) { |
| 748 |
int effectMask; |
| 749 |
|
| 750 |
if (reference.isName()) { |
| 751 |
|
| 752 |
effectMask = effectMaskForVariableReference(reference); |
| 753 |
} else { |
| 754 |
|
| 755 |
effectMask = HEAP_LOCATION_MASK; |
| 756 |
} |
| 757 |
|
| 758 |
if (storageNodeIsLValue(reference)) { |
| 759 |
visibilityModLocations |= effectMask; |
| 760 |
} |
| 761 |
|
| 762 |
if (storageNodeIsRValue(reference)) { |
| 763 |
visibilityRefLocations |= effectMask; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
VisibilityBasedEffectLocation modSet = |
| 768 |
new VisibilityBasedEffectLocation(visibilityModLocations); |
| 769 |
|
| 770 |
VisibilityBasedEffectLocation refSet = |
| 771 |
new VisibilityBasedEffectLocation(visibilityRefLocations); |
| 772 |
|
| 773 |
return new LocationSummary(modSet, refSet); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
private Set<Node> findStorageLocationReferences(Node root) { |
| 781 |
final Set<Node> references = Sets.newHashSet(); |
| 782 |
|
| 783 |
NodeTraversal.traverse(compiler, root, new AbstractShallowCallback() { |
| 784 |
@Override |
| 785 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 786 |
if (NodeUtil.isGet(n) |
| 787 |
|| (n.isName() && !parent.isFunction())) { |
| 788 |
references.add(n); |
| 789 |
} |
| 790 |
} |
| 791 |
}); |
| 792 |
|
| 793 |
return references; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
private int effectMaskForVariableReference(Node variableReference) { |
| 800 |
Preconditions.checkArgument(variableReference.isName()); |
| 801 |
|
| 802 |
int effectMask = VISIBILITY_LOCATION_NONE; |
| 803 |
|
| 804 |
Node declaringNameNode = |
| 805 |
variableUseMap.findDeclaringNameNodeForUse(variableReference); |
| 806 |
|
| 807 |
if (declaringNameNode != null) { |
| 808 |
VariableVisibility visibility = |
| 809 |
variableVisibilityAnalysis.getVariableVisibility(declaringNameNode); |
| 810 |
|
| 811 |
switch (visibility) { |
| 812 |
case LOCAL: |
| 813 |
effectMask = LOCAL_VARIABLE_LOCATION_MASK; |
| 814 |
break; |
| 815 |
case CAPTURED_LOCAL: |
| 816 |
effectMask = CAPTURED_LOCAL_VARIABLE_LOCATION_MASK; |
| 817 |
break; |
| 818 |
case PARAMETER: |
| 819 |
|
| 820 |
|
| 821 |
effectMask = HEAP_LOCATION_MASK; |
| 822 |
break; |
| 823 |
case GLOBAL: |
| 824 |
effectMask = GLOBAL_VARIABLE_LOCATION_MASK; |
| 825 |
break; |
| 826 |
default: |
| 827 |
throw new IllegalStateException("Unrecognized variable" + |
| 828 |
" visibility: " + visibility); |
| 829 |
} |
| 830 |
} else { |
| 831 |
|
| 832 |
effectMask = UNKNOWN_LOCATION_MASK; |
| 833 |
} |
| 834 |
|
| 835 |
return effectMask; |
| 836 |
} |
| 837 |
|
| 838 |
@Override |
| 839 |
EffectLocation getBottomLocation() { |
| 840 |
return new VisibilityBasedEffectLocation(VISIBILITY_LOCATION_NONE); |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
private static boolean isStorageNode(Node node) { |
| 849 |
return node.isName() || NodeUtil.isGet(node); |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
private static boolean storageNodeIsRValue(Node node) { |
| 856 |
Preconditions.checkArgument(isStorageNode(node)); |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
Node parent = node.getParent(); |
| 864 |
|
| 865 |
if (storageNodeIsLValue(node)) { |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
boolean nonSimpleAssign = |
| 871 |
NodeUtil.isAssignmentOp(parent) && !parent.isAssign(); |
| 872 |
|
| 873 |
return (nonSimpleAssign |
| 874 |
|| parent.isDec() |
| 875 |
|| parent.isInc()); |
| 876 |
} |
| 877 |
|
| 878 |
return true; |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
private static boolean storageNodeIsLValue(Node node) { |
| 885 |
Preconditions.checkArgument(isStorageNode(node)); |
| 886 |
return NodeUtil.isLValue(node); |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
private static class VisibilityBasedEffectLocation |
| 900 |
implements EffectLocation { |
| 901 |
int visibilityMask = VISIBILITY_LOCATION_NONE; |
| 902 |
|
| 903 |
public VisibilityBasedEffectLocation(int visibilityMask) { |
| 904 |
this.visibilityMask = visibilityMask; |
| 905 |
} |
| 906 |
|
| 907 |
@Override |
| 908 |
public boolean intersectsLocation(EffectLocation otherLocation) { |
| 909 |
Preconditions.checkArgument(otherLocation instanceof |
| 910 |
VisibilityBasedEffectLocation); |
| 911 |
|
| 912 |
int otherMask = |
| 913 |
((VisibilityBasedEffectLocation) otherLocation).visibilityMask; |
| 914 |
|
| 915 |
return (visibilityMask & otherMask) > 0; |
| 916 |
} |
| 917 |
|
| 918 |
@Override |
| 919 |
public boolean isEmpty() { |
| 920 |
return visibilityMask == VISIBILITY_LOCATION_NONE; |
| 921 |
} |
| 922 |
|
| 923 |
@Override |
| 924 |
public EffectLocation join(EffectLocation otherLocation) { |
| 925 |
Preconditions.checkArgument(otherLocation instanceof |
| 926 |
VisibilityBasedEffectLocation); |
| 927 |
|
| 928 |
int otherMask = |
| 929 |
((VisibilityBasedEffectLocation) otherLocation).visibilityMask; |
| 930 |
|
| 931 |
int joinedMask = visibilityMask | otherMask; |
| 932 |
|
| 933 |
return new VisibilityBasedEffectLocation(joinedMask); |
| 934 |
} |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
private static class VariableUseDeclarationMap { |
| 943 |
|
| 944 |
private AbstractCompiler compiler; |
| 945 |
|
| 946 |
|
| 947 |
private Map<Node, Node> referencesByNameNode; |
| 948 |
|
| 949 |
public VariableUseDeclarationMap(AbstractCompiler compiler) { |
| 950 |
this.compiler = compiler; |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
public void mapUses(Node root) { |
| 961 |
referencesByNameNode = Maps.newHashMap(); |
| 962 |
|
| 963 |
ReferenceCollectingCallback callback = |
| 964 |
new ReferenceCollectingCallback(compiler, |
| 965 |
ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR); |
| 966 |
|
| 967 |
NodeTraversal.traverse(compiler, root, callback); |
| 968 |
|
| 969 |
for (Var variable : callback.getAllSymbols()) { |
| 970 |
ReferenceCollection referenceCollection = |
| 971 |
callback.getReferences(variable); |
| 972 |
|
| 973 |
for (Reference reference : referenceCollection.references) { |
| 974 |
Node referenceNameNode = reference.getNode(); |
| 975 |
|
| 976 |
|
| 977 |
referencesByNameNode.put(referenceNameNode, variable.getNameNode()); |
| 978 |
} |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
public Node findDeclaringNameNodeForUse(Node usingNameNode) { |
| 988 |
Preconditions.checkArgument(usingNameNode.isName()); |
| 989 |
|
| 990 |
return referencesByNameNode.get(usingNameNode); |
| 991 |
} |
| 992 |
} |
| 993 |
} |