| 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.base.Preconditions; |
| 19 |
import com.google.common.collect.Sets; |
| 20 |
import com.google.javascript.jscomp.DefinitionsRemover.Definition; |
| 21 |
import com.google.javascript.rhino.Node; |
| 22 |
import com.google.javascript.rhino.Token; |
| 23 |
|
| 24 |
import java.util.Set; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class SimpleFunctionAliasAnalysis { |
| 33 |
private Set<Node> aliasedFunctions; |
| 34 |
|
| 35 |
private Set<Node> functionsExposedToCallOrApply; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public boolean isAliased(Node functionNode) { |
| 44 |
Preconditions.checkNotNull(aliasedFunctions); |
| 45 |
Preconditions.checkArgument(functionNode.isFunction()); |
| 46 |
|
| 47 |
return aliasedFunctions.contains(functionNode); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public boolean isExposedToCallOrApply(Node functionNode) { |
| 57 |
Preconditions.checkNotNull(functionsExposedToCallOrApply); |
| 58 |
Preconditions.checkArgument(functionNode.isFunction()); |
| 59 |
|
| 60 |
return functionsExposedToCallOrApply.contains(functionNode); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public void analyze(SimpleDefinitionFinder finder) { |
| 68 |
Preconditions.checkState(aliasedFunctions == null); |
| 69 |
|
| 70 |
aliasedFunctions = Sets.newHashSet(); |
| 71 |
functionsExposedToCallOrApply = Sets.newHashSet(); |
| 72 |
|
| 73 |
for (DefinitionSite definitionSite : finder.getDefinitionSites()) { |
| 74 |
Definition definition = definitionSite.definition; |
| 75 |
|
| 76 |
if (!definition.isExtern()) { |
| 77 |
Node rValue = definition.getRValue(); |
| 78 |
|
| 79 |
if (rValue != null && rValue.isFunction()) { |
| 80 |
|
| 81 |
|
| 82 |
for (UseSite useSite : finder.getUseSites(definition)) { |
| 83 |
updateFunctionForUse(rValue, useSite.node); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
private void updateFunctionForUse(Node function, Node useNode) { |
| 98 |
Node useParent = useNode.getParent(); |
| 99 |
int parentType = useParent.getType(); |
| 100 |
|
| 101 |
if ((parentType == Token.CALL || parentType == Token.NEW) |
| 102 |
&& useParent.getFirstChild() == useNode) { |
| 103 |
|
| 104 |
} else if (NodeUtil.isGet(useParent)) { |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if (useParent.isGetProp()) { |
| 109 |
Node gramps = useParent.getParent(); |
| 110 |
if (NodeUtil.isFunctionObjectApply(gramps) || |
| 111 |
NodeUtil.isFunctionObjectCall(gramps)) { |
| 112 |
functionsExposedToCallOrApply.add(function); |
| 113 |
} |
| 114 |
} |
| 115 |
} else { |
| 116 |
aliasedFunctions.add(function); |
| 117 |
} |
| 118 |
} |
| 119 |
} |