| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.common.annotations.VisibleForTesting; |
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.base.Supplier; |
| 21 |
import com.google.javascript.jscomp.MakeDeclaredNamesUnique.ContextualRenamer; |
| 22 |
import com.google.javascript.rhino.IR; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import com.google.javascript.rhino.Token; |
| 25 |
|
| 26 |
import java.util.Set; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class ExpressionDecomposer { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
enum DecompositionType { |
| 49 |
UNDECOMPOSABLE, |
| 50 |
MOVABLE, |
| 51 |
DECOMPOSABLE |
| 52 |
} |
| 53 |
|
| 54 |
private final AbstractCompiler compiler; |
| 55 |
private final Supplier<String> safeNameIdSupplier; |
| 56 |
private final Set<String> knownConstants; |
| 57 |
|
| 58 |
public ExpressionDecomposer( |
| 59 |
AbstractCompiler compiler, |
| 60 |
Supplier<String> safeNameIdSupplier, |
| 61 |
Set<String> constNames) { |
| 62 |
Preconditions.checkNotNull(compiler); |
| 63 |
Preconditions.checkNotNull(safeNameIdSupplier); |
| 64 |
Preconditions.checkNotNull(constNames); |
| 65 |
this.compiler = compiler; |
| 66 |
this.safeNameIdSupplier = safeNameIdSupplier; |
| 67 |
this.knownConstants = constNames; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
private static final int MAX_INTERATIONS = 100; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
void maybeExposeExpression(Node expression) { |
| 79 |
|
| 80 |
int i = 0; |
| 81 |
while (DecompositionType.DECOMPOSABLE == canExposeExpression(expression)) { |
| 82 |
exposeExpression(expression); |
| 83 |
i++; |
| 84 |
if (i > MAX_INTERATIONS) { |
| 85 |
throw new IllegalStateException( |
| 86 |
"DecomposeExpression depth exceeded on :\n" + |
| 87 |
expression.toStringTree()); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
void exposeExpression(Node expression) { |
| 98 |
Node expressionRoot = findExpressionRoot(expression); |
| 99 |
Preconditions.checkState(expressionRoot != null); |
| 100 |
exposeExpression(expressionRoot, expression); |
| 101 |
compiler.reportCodeChange(); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
void moveExpression(Node expression) { |
| 113 |
String resultName = getResultValueName(); |
| 114 |
Node injectionPoint = findInjectionPoint(expression); |
| 115 |
Preconditions.checkNotNull(injectionPoint); |
| 116 |
Node injectionPointParent = injectionPoint.getParent(); |
| 117 |
Preconditions.checkNotNull(injectionPointParent); |
| 118 |
Preconditions.checkState(NodeUtil.isStatementBlock(injectionPointParent)); |
| 119 |
|
| 120 |
|
| 121 |
Node expressionParent = expression.getParent(); |
| 122 |
expressionParent.replaceChild( |
| 123 |
expression, IR.name(resultName)); |
| 124 |
|
| 125 |
|
| 126 |
Node newExpressionRoot = NodeUtil.newVarNode(resultName, expression); |
| 127 |
injectionPointParent.addChildBefore(newExpressionRoot, injectionPoint); |
| 128 |
compiler.reportCodeChange(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private void exposeExpression(Node expressionRoot, Node subExpression) { |
| 147 |
Node nonconditionalExpr = findNonconditionalParent( |
| 148 |
subExpression, expressionRoot); |
| 149 |
|
| 150 |
boolean hasFollowingSideEffects = NodeUtil.mayHaveSideEffects( |
| 151 |
nonconditionalExpr, compiler); |
| 152 |
|
| 153 |
Node exprInjectionPoint = findInjectionPoint(nonconditionalExpr); |
| 154 |
DecompositionState state = new DecompositionState(); |
| 155 |
state.sideEffects = hasFollowingSideEffects; |
| 156 |
state.extractBeforeStatement = exprInjectionPoint; |
| 157 |
|
| 158 |
|
| 159 |
for (Node grandchild = null, |
| 160 |
child = nonconditionalExpr, |
| 161 |
parent = child.getParent(); |
| 162 |
parent != expressionRoot; |
| 163 |
grandchild = child, |
| 164 |
child = parent, |
| 165 |
parent = child.getParent()) { |
| 166 |
int parentType = parent.getType(); |
| 167 |
Preconditions.checkState( |
| 168 |
!isConditionalOp(parent) || child == parent.getFirstChild()); |
| 169 |
if (parentType == Token.ASSIGN) { |
| 170 |
if (isSafeAssign(parent, state.sideEffects)) { |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
} else { |
| 182 |
|
| 183 |
Node left = parent.getFirstChild(); |
| 184 |
int type = left.getType(); |
| 185 |
if (left != child) { |
| 186 |
Preconditions.checkState(NodeUtil.isGet(left)); |
| 187 |
if (type == Token.GETELEM) { |
| 188 |
decomposeSubExpressions(left.getLastChild(), null, state); |
| 189 |
} |
| 190 |
decomposeSubExpressions(left.getFirstChild(), null, state); |
| 191 |
} |
| 192 |
} |
| 193 |
} else if (parentType == Token.CALL |
| 194 |
&& NodeUtil.isGet(parent.getFirstChild())) { |
| 195 |
Node functionExpression = parent.getFirstChild(); |
| 196 |
decomposeSubExpressions(functionExpression.getNext(), child, state); |
| 197 |
|
| 198 |
if (isExpressionTreeUnsafe(functionExpression, state.sideEffects) |
| 199 |
&& functionExpression.getFirstChild() != grandchild) { |
| 200 |
|
| 201 |
|
| 202 |
Preconditions.checkState(allowObjectCallDecomposing(), |
| 203 |
"Object method calls can not be decomposed."); |
| 204 |
|
| 205 |
|
| 206 |
state.sideEffects = true; |
| 207 |
|
| 208 |
|
| 209 |
Node replacement = rewriteCallExpression(parent, state); |
| 210 |
|
| 211 |
parent = replacement; |
| 212 |
} |
| 213 |
} else if (parentType == Token.OBJECTLIT) { |
| 214 |
decomposeObjectLiteralKeys(parent.getFirstChild(), child, state); |
| 215 |
} else { |
| 216 |
decomposeSubExpressions(parent.getFirstChild(), child, state); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if (nonconditionalExpr == subExpression) { |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
} else { |
| 231 |
Node parent = nonconditionalExpr.getParent(); |
| 232 |
boolean needResult = !parent.isExprResult(); |
| 233 |
extractConditional(nonconditionalExpr, exprInjectionPoint, needResult); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
private static boolean allowObjectCallDecomposing() { |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
private boolean maybeExternMethod(Node node) { |
| 245 |
|
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
private static Node findNonconditionalParent( |
| 254 |
Node subExpression, Node expressionRoot) { |
| 255 |
Node result = subExpression; |
| 256 |
|
| 257 |
for (Node child = subExpression, parent = child.getParent(); |
| 258 |
parent != expressionRoot; |
| 259 |
child = parent, parent = child.getParent()) { |
| 260 |
if (isConditionalOp(parent)) { |
| 261 |
|
| 262 |
|
| 263 |
if (child != parent.getFirstChild()) { |
| 264 |
result = parent; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return result; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
private static class DecompositionState { |
| 278 |
boolean sideEffects; |
| 279 |
Node extractBeforeStatement; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
private void decomposeObjectLiteralKeys( |
| 288 |
Node key, Node stopNode, DecompositionState state) { |
| 289 |
if (key == null || key == stopNode) { |
| 290 |
return; |
| 291 |
} |
| 292 |
decomposeObjectLiteralKeys(key.getNext(), stopNode, state); |
| 293 |
decomposeSubExpressions(key.getFirstChild(), stopNode, state); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private void decomposeSubExpressions( |
| 301 |
Node n, Node stopNode, DecompositionState state) { |
| 302 |
if (n == null || n == stopNode) { |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
Preconditions.checkState(!NodeUtil.isObjectLitKey(n)); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
decomposeSubExpressions( |
| 314 |
n.getNext(), stopNode, state); |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
if (isExpressionTreeUnsafe(n, state.sideEffects)) { |
| 319 |
|
| 320 |
|
| 321 |
state.sideEffects = true; |
| 322 |
state.extractBeforeStatement = extractExpression( |
| 323 |
n, state.extractBeforeStatement); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
private Node extractConditional( |
| 337 |
Node expr, Node injectionPoint, boolean needResult) { |
| 338 |
Node parent = expr.getParent(); |
| 339 |
String tempName = getTempValueName(); |
| 340 |
|
| 341 |
|
| 342 |
Node first = expr.getFirstChild(); |
| 343 |
Node second = first.getNext(); |
| 344 |
Node last = expr.getLastChild(); |
| 345 |
|
| 346 |
|
| 347 |
expr.detachChildren(); |
| 348 |
|
| 349 |
|
| 350 |
Node cond = null; |
| 351 |
Node trueExpr = IR.block().srcref(expr); |
| 352 |
Node falseExpr = IR.block().srcref(expr); |
| 353 |
switch (expr.getType()) { |
| 354 |
case Token.HOOK: |
| 355 |
|
| 356 |
cond = first; |
| 357 |
trueExpr.addChildToFront(NodeUtil.newExpr( |
| 358 |
buildResultExpression(second, needResult, tempName))); |
| 359 |
falseExpr.addChildToFront(NodeUtil.newExpr( |
| 360 |
buildResultExpression(last, needResult, tempName))); |
| 361 |
break; |
| 362 |
case Token.AND: |
| 363 |
|
| 364 |
cond = buildResultExpression(first, needResult, tempName); |
| 365 |
trueExpr.addChildToFront(NodeUtil.newExpr( |
| 366 |
buildResultExpression(last, needResult, tempName))); |
| 367 |
break; |
| 368 |
case Token.OR: |
| 369 |
|
| 370 |
cond = buildResultExpression(first, needResult, tempName); |
| 371 |
falseExpr.addChildToFront(NodeUtil.newExpr( |
| 372 |
buildResultExpression(last, needResult, tempName))); |
| 373 |
break; |
| 374 |
default: |
| 375 |
|
| 376 |
throw new IllegalStateException("Unexpected."); |
| 377 |
} |
| 378 |
|
| 379 |
Node ifNode; |
| 380 |
if (falseExpr.hasChildren()) { |
| 381 |
ifNode = IR.ifNode(cond, trueExpr, falseExpr); |
| 382 |
} else { |
| 383 |
ifNode = IR.ifNode(cond, trueExpr); |
| 384 |
} |
| 385 |
ifNode.copyInformationFrom(expr); |
| 386 |
|
| 387 |
if (needResult) { |
| 388 |
Node tempVarNode = NodeUtil.newVarNode(tempName, null) |
| 389 |
.copyInformationFromForTree(expr); |
| 390 |
Node injectionPointParent = injectionPoint.getParent(); |
| 391 |
injectionPointParent.addChildBefore(tempVarNode, injectionPoint); |
| 392 |
injectionPointParent.addChildAfter(ifNode, tempVarNode); |
| 393 |
|
| 394 |
|
| 395 |
Node replacementValueNode = IR.name(tempName); |
| 396 |
parent.replaceChild(expr, replacementValueNode); |
| 397 |
} else { |
| 398 |
|
| 399 |
|
| 400 |
Preconditions.checkArgument(parent.isExprResult()); |
| 401 |
Node gramps = parent.getParent(); |
| 402 |
gramps.replaceChild(parent, ifNode); |
| 403 |
} |
| 404 |
|
| 405 |
return ifNode; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
private static Node buildResultExpression( |
| 418 |
Node expr, boolean needResult, String tempName) { |
| 419 |
if (needResult) { |
| 420 |
return IR.assign( |
| 421 |
IR.name(tempName), |
| 422 |
expr).srcrefTree(expr); |
| 423 |
} else { |
| 424 |
return expr; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
private boolean isConstantName(Node n, Set<String> knownConstants) { |
| 429 |
|
| 430 |
return n.isName() && (NodeUtil.isConstantName(n) |
| 431 |
|| knownConstants.contains(n.getString())); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
private Node extractExpression(Node expr, Node injectionPoint) { |
| 441 |
Node parent = expr.getParent(); |
| 442 |
|
| 443 |
boolean isLhsOfAssignOp = NodeUtil.isAssignmentOp(parent) |
| 444 |
&& !parent.isAssign() |
| 445 |
&& parent.getFirstChild() == expr; |
| 446 |
|
| 447 |
Node firstExtractedNode = null; |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
if (isLhsOfAssignOp && NodeUtil.isGet(expr)) { |
| 456 |
for (Node n : expr.children()) { |
| 457 |
if (!n.isString() && !isConstantName(n, knownConstants)) { |
| 458 |
Node extractedNode = extractExpression(n, injectionPoint); |
| 459 |
if (firstExtractedNode == null) { |
| 460 |
firstExtractedNode = extractedNode; |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
String tempName = getTempConstantValueName(); |
| 468 |
Node replacementValueNode = IR.name(tempName).srcref(expr); |
| 469 |
|
| 470 |
Node tempNameValue; |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
if (isLhsOfAssignOp) { |
| 475 |
Preconditions.checkState(expr.isName() || NodeUtil.isGet(expr)); |
| 476 |
|
| 477 |
Node opNode = new Node(NodeUtil.getOpFromAssignmentOp(parent)) |
| 478 |
.copyInformationFrom(parent); |
| 479 |
|
| 480 |
Node rightOperand = parent.getLastChild(); |
| 481 |
|
| 482 |
parent.setType(Token.ASSIGN); |
| 483 |
parent.replaceChild(rightOperand, opNode); |
| 484 |
opNode.addChildToFront(replacementValueNode); |
| 485 |
opNode.addChildToBack(rightOperand); |
| 486 |
|
| 487 |
|
| 488 |
tempNameValue = expr.cloneTree(); |
| 489 |
} else { |
| 490 |
|
| 491 |
parent.replaceChild(expr, replacementValueNode); |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
tempNameValue = expr; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
Node tempVarNode = NodeUtil.newVarNode(tempName, tempNameValue); |
| 500 |
|
| 501 |
Node injectionPointParent = injectionPoint.getParent(); |
| 502 |
injectionPointParent.addChildBefore(tempVarNode, injectionPoint); |
| 503 |
|
| 504 |
if (firstExtractedNode == null) { |
| 505 |
firstExtractedNode = tempVarNode; |
| 506 |
} |
| 507 |
return firstExtractedNode; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
private Node rewriteCallExpression(Node call, DecompositionState state) { |
| 521 |
Preconditions.checkArgument(call.isCall()); |
| 522 |
Node first = call.getFirstChild(); |
| 523 |
Preconditions.checkArgument(NodeUtil.isGet(first)); |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
Node getVarNode = extractExpression( |
| 528 |
first, state.extractBeforeStatement); |
| 529 |
state.extractBeforeStatement = getVarNode; |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
Node getExprNode = getVarNode.getFirstChild().getFirstChild(); |
| 534 |
Preconditions.checkArgument(NodeUtil.isGet(getExprNode)); |
| 535 |
Node thisVarNode = extractExpression( |
| 536 |
getExprNode.getFirstChild(), state.extractBeforeStatement); |
| 537 |
state.extractBeforeStatement = thisVarNode; |
| 538 |
|
| 539 |
|
| 540 |
Node thisNameNode = thisVarNode.getFirstChild(); |
| 541 |
Node functionNameNode = getVarNode.getFirstChild(); |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
Node newCall = IR.call( |
| 552 |
IR.getprop( |
| 553 |
functionNameNode.cloneNode(), |
| 554 |
IR.string("call")), |
| 555 |
thisNameNode.cloneNode()).srcref(call); |
| 556 |
|
| 557 |
|
| 558 |
call.removeFirstChild(); |
| 559 |
if (call.hasChildren()) { |
| 560 |
|
| 561 |
newCall.addChildrenToBack(call.removeChildren()); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
Node callParent = call.getParent(); |
| 566 |
callParent.replaceChild(call, newCall); |
| 567 |
|
| 568 |
return newCall; |
| 569 |
} |
| 570 |
|
| 571 |
private String tempNamePrefix = "JSCompiler_temp"; |
| 572 |
private String resultNamePrefix = "JSCompiler_inline_result"; |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
@VisibleForTesting |
| 578 |
public void setTempNamePrefix(String prefix) { |
| 579 |
this.tempNamePrefix = prefix; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
private String getTempValueName(){ |
| 586 |
return tempNamePrefix + ContextualRenamer.UNIQUE_ID_SEPARATOR |
| 587 |
+ safeNameIdSupplier.get(); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
@VisibleForTesting |
| 594 |
public void setResultNamePrefix(String prefix) { |
| 595 |
this.resultNamePrefix = prefix; |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
private String getResultValueName() { |
| 602 |
return resultNamePrefix |
| 603 |
+ ContextualRenamer.UNIQUE_ID_SEPARATOR + safeNameIdSupplier.get(); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
private String getTempConstantValueName(){ |
| 610 |
String name = tempNamePrefix + "_const" |
| 611 |
+ ContextualRenamer.UNIQUE_ID_SEPARATOR |
| 612 |
+ safeNameIdSupplier.get(); |
| 613 |
this.knownConstants.add(name); |
| 614 |
return name; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
static Node findInjectionPoint(Node subExpression) { |
| 622 |
Node expressionRoot = findExpressionRoot(subExpression); |
| 623 |
Preconditions.checkNotNull(expressionRoot); |
| 624 |
|
| 625 |
Node injectionPoint = expressionRoot; |
| 626 |
|
| 627 |
Node parent = injectionPoint.getParent(); |
| 628 |
while (parent.isLabel()) { |
| 629 |
injectionPoint = parent; |
| 630 |
parent = injectionPoint.getParent(); |
| 631 |
} |
| 632 |
|
| 633 |
Preconditions.checkState( |
| 634 |
NodeUtil.isStatementBlock(injectionPoint.getParent())); |
| 635 |
return injectionPoint; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
private static boolean isConditionalOp(Node n) { |
| 642 |
switch(n.getType()) { |
| 643 |
case Token.HOOK: |
| 644 |
case Token.AND: |
| 645 |
case Token.OR: |
| 646 |
return true; |
| 647 |
default: |
| 648 |
return false; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
static Node findExpressionRoot(Node subExpression) { |
| 658 |
Node child = subExpression; |
| 659 |
for (Node parent : child.getAncestors()) { |
| 660 |
int parentType = parent.getType(); |
| 661 |
switch (parentType) { |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
case Token.EXPR_RESULT: |
| 667 |
case Token.IF: |
| 668 |
case Token.SWITCH: |
| 669 |
case Token.RETURN: |
| 670 |
case Token.VAR: |
| 671 |
Preconditions.checkState(child == parent.getFirstChild()); |
| 672 |
return parent; |
| 673 |
|
| 674 |
case Token.SCRIPT: |
| 675 |
case Token.BLOCK: |
| 676 |
case Token.LABEL: |
| 677 |
case Token.CASE: |
| 678 |
case Token.DEFAULT_CASE: |
| 679 |
return null; |
| 680 |
} |
| 681 |
child = parent; |
| 682 |
} |
| 683 |
|
| 684 |
throw new IllegalStateException("Unexpected AST structure."); |
| 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 |
|
| 710 |
|
| 711 |
|
| 712 |
DecompositionType canExposeExpression(Node subExpression) { |
| 713 |
Node expressionRoot = findExpressionRoot(subExpression); |
| 714 |
if (expressionRoot != null) { |
| 715 |
return isSubexpressionMovable(expressionRoot, subExpression); |
| 716 |
} |
| 717 |
return DecompositionType.UNDECOMPOSABLE; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
private DecompositionType isSubexpressionMovable( |
| 747 |
Node expressionRoot, Node subExpression) { |
| 748 |
boolean requiresDecomposition = false; |
| 749 |
boolean seenSideEffects = NodeUtil.mayHaveSideEffects( |
| 750 |
subExpression, compiler); |
| 751 |
|
| 752 |
Node child = subExpression; |
| 753 |
for (Node parent : child.getAncestors()) { |
| 754 |
if (parent == expressionRoot) { |
| 755 |
|
| 756 |
|
| 757 |
return requiresDecomposition |
| 758 |
? DecompositionType.DECOMPOSABLE |
| 759 |
: DecompositionType.MOVABLE; |
| 760 |
} |
| 761 |
|
| 762 |
if (isConditionalOp(parent)) { |
| 763 |
|
| 764 |
|
| 765 |
if (child != parent.getFirstChild()) { |
| 766 |
requiresDecomposition = true; |
| 767 |
} |
| 768 |
} else { |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
if (isSafeAssign(parent, seenSideEffects)) { |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
} else { |
| 788 |
|
| 789 |
for (Node n : parent.children()) { |
| 790 |
if (n == child) { |
| 791 |
|
| 792 |
|
| 793 |
break; |
| 794 |
} |
| 795 |
|
| 796 |
if (isExpressionTreeUnsafe( |
| 797 |
n, seenSideEffects)) { |
| 798 |
seenSideEffects = true; |
| 799 |
requiresDecomposition = true; |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
Node first = parent.getFirstChild(); |
| 819 |
if (requiresDecomposition |
| 820 |
&& parent.isCall() |
| 821 |
&& NodeUtil.isGet(first)) { |
| 822 |
if (maybeExternMethod(first)) { |
| 823 |
return DecompositionType.UNDECOMPOSABLE; |
| 824 |
} else { |
| 825 |
return DecompositionType.DECOMPOSABLE; |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
child = parent; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
throw new IllegalStateException("Unexpected."); |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
private boolean isSafeAssign(Node n, boolean seenSideEffects) { |
| 860 |
if (n.isAssign()) { |
| 861 |
Node lhs = n.getFirstChild(); |
| 862 |
switch (lhs.getType()) { |
| 863 |
case Token.NAME: |
| 864 |
return true; |
| 865 |
case Token.GETPROP: |
| 866 |
return !isExpressionTreeUnsafe(lhs.getFirstChild(), seenSideEffects); |
| 867 |
case Token.GETELEM: |
| 868 |
return !isExpressionTreeUnsafe(lhs.getFirstChild(), seenSideEffects) |
| 869 |
&& !isExpressionTreeUnsafe(lhs.getLastChild(), seenSideEffects); |
| 870 |
} |
| 871 |
} |
| 872 |
return false; |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
private boolean isExpressionTreeUnsafe( |
| 880 |
Node n, boolean followingSideEffectsExist) { |
| 881 |
if (followingSideEffectsExist) { |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
return NodeUtil.canBeSideEffected(n, this.knownConstants); |
| 887 |
} else { |
| 888 |
|
| 889 |
|
| 890 |
return NodeUtil.mayHaveSideEffects(n, compiler); |
| 891 |
} |
| 892 |
} |
| 893 |
} |