| 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.Predicate; |
| 21 |
import com.google.common.collect.Collections2; |
| 22 |
import com.google.common.collect.ImmutableList; |
| 23 |
import com.google.common.collect.Maps; |
| 24 |
import com.google.javascript.jscomp.DefinitionsRemover.Definition; |
| 25 |
import com.google.javascript.jscomp.NameReferenceGraph.Name; |
| 26 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 27 |
import com.google.javascript.jscomp.graph.DiGraph; |
| 28 |
import com.google.javascript.jscomp.graph.LinkedDirectedGraph; |
| 29 |
import com.google.javascript.rhino.Node; |
| 30 |
import com.google.javascript.rhino.Token; |
| 31 |
|
| 32 |
import java.util.Collection; |
| 33 |
import java.util.LinkedList; |
| 34 |
import java.util.Map; |
| 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 |
public class CallGraph implements CompilerPass { |
| 62 |
private AbstractCompiler compiler; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private Map<Node, Callsite> callsitesByNode; |
| 68 |
|
| 69 |
|
| 70 |
private Map<Node, Function> functionsByNode; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private boolean computeBackwardGraph; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private boolean computeForwardGraph; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private boolean useNameReferenceGraph = false; |
| 89 |
|
| 90 |
|
| 91 |
private boolean alreadyRun = false; |
| 92 |
|
| 93 |
|
| 94 |
@VisibleForTesting |
| 95 |
public static final String MAIN_FUNCTION_NAME = "{main}"; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private Function mainFunction; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public CallGraph(AbstractCompiler compiler, boolean computeForwardGraph, |
| 120 |
boolean computeBackwardGraph) { |
| 121 |
Preconditions.checkArgument(computeForwardGraph || computeBackwardGraph); |
| 122 |
|
| 123 |
this.compiler = compiler; |
| 124 |
|
| 125 |
this.computeForwardGraph = computeForwardGraph; |
| 126 |
this.computeBackwardGraph = computeBackwardGraph; |
| 127 |
|
| 128 |
callsitesByNode = Maps.newLinkedHashMap(); |
| 129 |
functionsByNode = Maps.newLinkedHashMap(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public CallGraph(AbstractCompiler compiler) { |
| 136 |
this(compiler, true, true); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
@Override |
| 144 |
public void process(Node externsRoot, Node jsRoot) { |
| 145 |
Preconditions.checkState(alreadyRun == false); |
| 146 |
|
| 147 |
DefinitionProvider definitionProvider = |
| 148 |
constructDefinitionProvider(externsRoot, jsRoot); |
| 149 |
|
| 150 |
createFunctionsAndCallsites(jsRoot, definitionProvider); |
| 151 |
|
| 152 |
fillInFunctionInformation(definitionProvider); |
| 153 |
|
| 154 |
alreadyRun = true; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public Function getFunctionForAstNode(Node functionNode) { |
| 162 |
Preconditions.checkArgument(functionNode.isFunction()); |
| 163 |
|
| 164 |
return functionsByNode.get(functionNode); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public Function getMainFunction() { |
| 171 |
return mainFunction; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public Collection<Function> getAllFunctions() { |
| 179 |
return functionsByNode.values(); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
@VisibleForTesting |
| 188 |
public Function getUniqueFunctionWithName(final String desiredName) { |
| 189 |
Collection<Function> functions = |
| 190 |
Collections2.<Function>filter(getAllFunctions(), |
| 191 |
new Predicate<Function>() { |
| 192 |
@Override |
| 193 |
public boolean apply(Function function) { |
| 194 |
|
| 195 |
String functionName = function.getName(); |
| 196 |
|
| 197 |
|
| 198 |
if (functionName != null && desiredName != null) { |
| 199 |
return desiredName.equals(functionName); |
| 200 |
} else { |
| 201 |
return desiredName == functionName; |
| 202 |
} |
| 203 |
} |
| 204 |
}); |
| 205 |
|
| 206 |
if (functions.size() == 1) { |
| 207 |
return functions.iterator().next(); |
| 208 |
} else { |
| 209 |
throw new IllegalStateException("Found " + functions.size() |
| 210 |
+ " functions with name " + desiredName); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public Callsite getCallsiteForAstNode(Node callsiteNode) { |
| 219 |
Preconditions.checkArgument(callsiteNode.isCall() || |
| 220 |
callsiteNode.isNew()); |
| 221 |
|
| 222 |
return callsitesByNode.get(callsiteNode); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public Collection<Callsite> getAllCallsites() { |
| 229 |
return callsitesByNode.values(); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private void createFunctionsAndCallsites(Node jsRoot, |
| 237 |
final DefinitionProvider provider) { |
| 238 |
|
| 239 |
mainFunction = createFunction(jsRoot); |
| 240 |
|
| 241 |
NodeTraversal.traverse(compiler, jsRoot, new AbstractPostOrderCallback() { |
| 242 |
@Override |
| 243 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 244 |
int nodeType = n.getType(); |
| 245 |
|
| 246 |
if (nodeType == Token.CALL || nodeType == Token.NEW) { |
| 247 |
Callsite callsite = createCallsite(n); |
| 248 |
|
| 249 |
Node containingFunctionNode = t.getScopeRoot(); |
| 250 |
|
| 251 |
Function containingFunction = |
| 252 |
functionsByNode.get(containingFunctionNode); |
| 253 |
|
| 254 |
if (containingFunction == null) { |
| 255 |
containingFunction = createFunction(containingFunctionNode); |
| 256 |
} |
| 257 |
callsite.containingFunction = containingFunction; |
| 258 |
containingFunction.addCallsiteInFunction(callsite); |
| 259 |
|
| 260 |
connectCallsiteToTargets(callsite, provider); |
| 261 |
|
| 262 |
} else if (n.isFunction()) { |
| 263 |
if (!functionsByNode.containsKey(n)) { |
| 264 |
createFunction(n); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
}); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
private Function createFunction(Node functionNode) { |
| 278 |
Function function = new Function(functionNode); |
| 279 |
functionsByNode.put(functionNode, function); |
| 280 |
|
| 281 |
return function; |
| 282 |
} |
| 283 |
|
| 284 |
private Callsite createCallsite(Node callsiteNode) { |
| 285 |
Callsite callsite = new Callsite(callsiteNode); |
| 286 |
callsitesByNode.put(callsiteNode, callsite); |
| 287 |
|
| 288 |
return callsite; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
private void connectCallsiteToTargets(Callsite callsite, |
| 307 |
DefinitionProvider definitionProvider) { |
| 308 |
Collection<Definition> definitions = |
| 309 |
lookupDefinitionsForTargetsOfCall(callsite.getAstNode(), |
| 310 |
definitionProvider); |
| 311 |
|
| 312 |
if (definitions == null) { |
| 313 |
callsite.hasUnknownTarget = true; |
| 314 |
} else { |
| 315 |
for (Definition definition : definitions) { |
| 316 |
if (definition.isExtern()) { |
| 317 |
callsite.hasExternTarget = true; |
| 318 |
} else { |
| 319 |
Node target = definition.getRValue(); |
| 320 |
|
| 321 |
if (target != null && target.isFunction()) { |
| 322 |
Function targetFunction = functionsByNode.get(target); |
| 323 |
|
| 324 |
if (targetFunction == null) { |
| 325 |
targetFunction = createFunction(target); |
| 326 |
} |
| 327 |
|
| 328 |
if (computeForwardGraph) { |
| 329 |
callsite.addPossibleTarget(targetFunction); |
| 330 |
} |
| 331 |
|
| 332 |
if (computeBackwardGraph) { |
| 333 |
targetFunction.addCallsitePossiblyTargetingFunction(callsite); |
| 334 |
} |
| 335 |
} else { |
| 336 |
callsite.hasUnknownTarget = true; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
private void fillInFunctionInformation(DefinitionProvider provider) { |
| 353 |
if (useNameReferenceGraph) { |
| 354 |
NameReferenceGraph referenceGraph = (NameReferenceGraph) provider; |
| 355 |
|
| 356 |
for (Function function : getAllFunctions()) { |
| 357 |
if (!function.isMain()) { |
| 358 |
String functionName = function.getName(); |
| 359 |
|
| 360 |
if (functionName != null) { |
| 361 |
Name symbol = referenceGraph.getSymbol(functionName); |
| 362 |
updateFunctionForName(function, symbol); |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} else { |
| 367 |
SimpleDefinitionFinder finder = (SimpleDefinitionFinder) provider; |
| 368 |
|
| 369 |
for (DefinitionSite definitionSite : finder.getDefinitionSites()) { |
| 370 |
Definition definition = definitionSite.definition; |
| 371 |
|
| 372 |
Function function = lookupFunctionForDefinition(definition); |
| 373 |
|
| 374 |
if (function != null) { |
| 375 |
for (UseSite useSite : finder.getUseSites(definition)) { |
| 376 |
updateFunctionForUse(function, useSite.node); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
private void updateFunctionForName(Function function, Name name) { |
| 391 |
if (name.isAliased()) { |
| 392 |
function.isAliased = true; |
| 393 |
} |
| 394 |
|
| 395 |
if (name.exposedToCallOrApply()) { |
| 396 |
function.isExposedToCallOrApply = true; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
private void updateFunctionForUse(Function function, Node useNode) { |
| 408 |
Node useParent = useNode.getParent(); |
| 409 |
int parentType = useParent.getType(); |
| 410 |
|
| 411 |
if ((parentType == Token.CALL || parentType == Token.NEW) |
| 412 |
&& useParent.getFirstChild() == useNode) { |
| 413 |
|
| 414 |
} else if (NodeUtil.isGet(useParent)) { |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
if (useParent.isGetProp()) { |
| 419 |
Node gramps = useParent.getParent(); |
| 420 |
if (NodeUtil.isFunctionObjectApply(gramps) || |
| 421 |
NodeUtil.isFunctionObjectCall(gramps)) { |
| 422 |
function.isExposedToCallOrApply = true; |
| 423 |
} |
| 424 |
} |
| 425 |
} else { |
| 426 |
function.isAliased = true; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
private Function lookupFunctionForDefinition(Definition definition) { |
| 435 |
if (definition != null && !definition.isExtern()) { |
| 436 |
Node rValue = definition.getRValue(); |
| 437 |
|
| 438 |
if (rValue != null && rValue.isFunction()) { |
| 439 |
Function function = functionsByNode.get(rValue); |
| 440 |
Preconditions.checkNotNull(function); |
| 441 |
|
| 442 |
return function; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
return null; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public DiGraph<Function, Callsite> getForwardDirectedGraph() { |
| 457 |
return constructDirectedGraph(true); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
public DiGraph<Function, Callsite> getBackwardDirectedGraph() { |
| 468 |
return constructDirectedGraph(false); |
| 469 |
} |
| 470 |
|
| 471 |
private static void digraphConnect(DiGraph<Function, Callsite> digraph, |
| 472 |
Function caller, |
| 473 |
Callsite callsite, |
| 474 |
Function callee, |
| 475 |
boolean forward) { |
| 476 |
|
| 477 |
Function source; |
| 478 |
Function destination; |
| 479 |
|
| 480 |
if (forward) { |
| 481 |
source = caller; |
| 482 |
destination = callee; |
| 483 |
} else { |
| 484 |
source = callee; |
| 485 |
destination = caller; |
| 486 |
} |
| 487 |
|
| 488 |
digraph.connect(source, callsite, destination); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
private DiGraph<Function, Callsite> constructDirectedGraph(boolean forward) { |
| 503 |
DiGraph<Function, Callsite>digraph = |
| 504 |
LinkedDirectedGraph.createWithoutAnnotations(); |
| 505 |
|
| 506 |
|
| 507 |
for (Function function : getAllFunctions()) { |
| 508 |
digraph.createNode(function); |
| 509 |
} |
| 510 |
|
| 511 |
if (computeForwardGraph) { |
| 512 |
|
| 513 |
for (Function caller : getAllFunctions()) { |
| 514 |
for (Callsite callsite : caller.getCallsitesInFunction()) { |
| 515 |
for (Function callee : callsite.getPossibleTargets()) { |
| 516 |
digraphConnect(digraph, caller, callsite, callee, |
| 517 |
forward); |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
} else { |
| 522 |
|
| 523 |
for (Function callee : getAllFunctions()) { |
| 524 |
for (Callsite callsite : |
| 525 |
callee.getCallsitesPossiblyTargetingFunction()) { |
| 526 |
|
| 527 |
Function caller = callsite.getContainingFunction(); |
| 528 |
digraphConnect(digraph, caller, callsite, callee, |
| 529 |
forward); |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
return digraph; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
private DefinitionProvider constructDefinitionProvider(Node externsRoot, |
| 551 |
Node jsRoot) { |
| 552 |
if (useNameReferenceGraph) { |
| 553 |
|
| 554 |
NameReferenceGraphConstruction graphConstruction |
| 555 |
= new NameReferenceGraphConstruction(compiler); |
| 556 |
|
| 557 |
graphConstruction.process(externsRoot, jsRoot); |
| 558 |
|
| 559 |
return graphConstruction.getNameReferenceGraph(); |
| 560 |
} else { |
| 561 |
SimpleDefinitionFinder defFinder = new SimpleDefinitionFinder(compiler); |
| 562 |
defFinder.process(externsRoot, jsRoot); |
| 563 |
return defFinder; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
private Collection<Definition> lookupDefinitionsForTargetsOfCall( |
| 576 |
Node callsite, DefinitionProvider definitionProvider) { |
| 577 |
Preconditions.checkArgument(callsite.isCall() |
| 578 |
|| callsite.isNew()); |
| 579 |
|
| 580 |
Node targetExpression = callsite.getFirstChild(); |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
if (!useNameReferenceGraph |
| 585 |
|| (targetExpression.isGetProp() |
| 586 |
|| targetExpression.isName())) { |
| 587 |
|
| 588 |
Collection<Definition> definitions = |
| 589 |
definitionProvider.getDefinitionsReferencedAt(targetExpression); |
| 590 |
|
| 591 |
if (definitions != null && !definitions.isEmpty()) { |
| 592 |
return definitions; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
return null; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
public class Function { |
| 605 |
|
| 606 |
private Node astNode; |
| 607 |
|
| 608 |
private boolean isAliased = false; |
| 609 |
|
| 610 |
private boolean isExposedToCallOrApply = false; |
| 611 |
|
| 612 |
private Collection<Callsite> callsitesInFunction; |
| 613 |
|
| 614 |
private Collection<Callsite> callsitesPossiblyTargetingFunction; |
| 615 |
|
| 616 |
private Function(Node functionAstNode) { |
| 617 |
astNode = functionAstNode; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
public boolean isMain() { |
| 624 |
return (this == CallGraph.this.mainFunction); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
public Node getAstNode() { |
| 633 |
return astNode; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
public Node getBodyNode() { |
| 641 |
if (isMain()) { |
| 642 |
return astNode; |
| 643 |
} else { |
| 644 |
return NodeUtil.getFunctionBody(astNode); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
public String getName() { |
| 653 |
if (isMain()) { |
| 654 |
return MAIN_FUNCTION_NAME; |
| 655 |
} else { |
| 656 |
return NodeUtil.getFunctionName(astNode); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
public Collection<Callsite> getCallsitesInFunction() { |
| 664 |
if (callsitesInFunction != null) { |
| 665 |
return callsitesInFunction; |
| 666 |
} else { |
| 667 |
return ImmutableList.of(); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
private void addCallsiteInFunction(Callsite callsite) { |
| 672 |
if (callsitesInFunction == null) { |
| 673 |
callsitesInFunction = new LinkedList<Callsite>(); |
| 674 |
} |
| 675 |
callsitesInFunction.add(callsite); |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
public Collection<Callsite> getCallsitesPossiblyTargetingFunction() { |
| 690 |
if (computeBackwardGraph) { |
| 691 |
if (callsitesPossiblyTargetingFunction != null) { |
| 692 |
return callsitesPossiblyTargetingFunction; |
| 693 |
} else { |
| 694 |
return ImmutableList.of(); |
| 695 |
} |
| 696 |
} else { |
| 697 |
throw new UnsupportedOperationException("Cannot call " + |
| 698 |
"getCallsitesPossiblyTargetingFunction() on a Function " |
| 699 |
+ "from a non-backward CallGraph"); |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
private void addCallsitePossiblyTargetingFunction(Callsite callsite) { |
| 704 |
Preconditions.checkState(computeBackwardGraph); |
| 705 |
if (callsitesPossiblyTargetingFunction == null) { |
| 706 |
callsitesPossiblyTargetingFunction = |
| 707 |
new LinkedList<Callsite>(); |
| 708 |
} |
| 709 |
callsitesPossiblyTargetingFunction.add(callsite); |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
public boolean isAliased() { |
| 716 |
return isAliased; |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
public boolean isExposedToCallOrApply() { |
| 723 |
return isExposedToCallOrApply; |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
public class Callsite { |
| 733 |
private Node astNode; |
| 734 |
|
| 735 |
private boolean hasUnknownTarget = false; |
| 736 |
private boolean hasExternTarget = false; |
| 737 |
|
| 738 |
private Function containingFunction = null; |
| 739 |
|
| 740 |
private Collection<Function> possibleTargets; |
| 741 |
|
| 742 |
private Callsite(Node callsiteAstNode) { |
| 743 |
astNode = callsiteAstNode; |
| 744 |
} |
| 745 |
|
| 746 |
public Node getAstNode() { |
| 747 |
return astNode; |
| 748 |
} |
| 749 |
|
| 750 |
public Function getContainingFunction() { |
| 751 |
return containingFunction; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
public Collection<Function> getPossibleTargets() { |
| 769 |
if (computeForwardGraph) { |
| 770 |
if (possibleTargets != null) { |
| 771 |
return possibleTargets; |
| 772 |
} else { |
| 773 |
return ImmutableList.of(); |
| 774 |
} |
| 775 |
} else { |
| 776 |
throw new UnsupportedOperationException("Cannot call " + |
| 777 |
"getPossibleTargets() on a Callsite from a non-forward " + |
| 778 |
"CallGraph"); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
private void addPossibleTarget(Function target) { |
| 783 |
Preconditions.checkState(computeForwardGraph); |
| 784 |
|
| 785 |
if (possibleTargets == null) { |
| 786 |
possibleTargets = new LinkedList<Function>(); |
| 787 |
} |
| 788 |
possibleTargets.add(target); |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
public boolean hasUnknownTarget() { |
| 799 |
return hasUnknownTarget; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
public boolean hasExternTarget() { |
| 807 |
return hasExternTarget; |
| 808 |
} |
| 809 |
} |
| 810 |
} |