| 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.Lists; |
| 21 |
import com.google.common.collect.Sets; |
| 22 |
import com.google.javascript.jscomp.DefinitionsRemover.Definition; |
| 23 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 24 |
import com.google.javascript.rhino.JSDocInfo; |
| 25 |
import com.google.javascript.rhino.Node; |
| 26 |
import com.google.javascript.rhino.Token; |
| 27 |
|
| 28 |
import java.util.Collection; |
| 29 |
import java.util.List; |
| 30 |
import java.util.Set; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class MarkNoSideEffectCalls implements CompilerPass { |
| 41 |
static final DiagnosticType INVALID_NO_SIDE_EFFECT_ANNOTATION = |
| 42 |
DiagnosticType.error( |
| 43 |
"JSC_INVALID_NO_SIDE_EFFECT_ANNOTATION", |
| 44 |
"@nosideeffects may only appear in externs files."); |
| 45 |
|
| 46 |
private final AbstractCompiler compiler; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private final Set<Node> noSideEffectFunctionNames; |
| 51 |
|
| 52 |
MarkNoSideEffectCalls(AbstractCompiler compiler) { |
| 53 |
this.compiler = compiler; |
| 54 |
this.noSideEffectFunctionNames = Sets.newHashSet(); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void process(Node externs, Node root) { |
| 59 |
SimpleDefinitionFinder defFinder = new SimpleDefinitionFinder(compiler); |
| 60 |
defFinder.process(externs, root); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
NodeTraversal.traverse( |
| 65 |
compiler, externs, new GatherNoSideEffectFunctions(true)); |
| 66 |
NodeTraversal.traverse( |
| 67 |
compiler, root, new GatherNoSideEffectFunctions(false)); |
| 68 |
|
| 69 |
NodeTraversal.traverse(compiler, root, |
| 70 |
new SetNoSideEffectCallProperty(defFinder)); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private static boolean definitionTypeContainsFunctionType(Definition def) { |
| 78 |
Node rhs = def.getRValue(); |
| 79 |
if (rhs == null) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
switch (rhs.getType()) { |
| 84 |
case Token.ASSIGN: |
| 85 |
case Token.AND: |
| 86 |
case Token.CALL: |
| 87 |
case Token.GETPROP: |
| 88 |
case Token.GETELEM: |
| 89 |
case Token.FUNCTION: |
| 90 |
case Token.HOOK: |
| 91 |
case Token.NAME: |
| 92 |
case Token.NEW: |
| 93 |
case Token.OR: |
| 94 |
return true; |
| 95 |
default: |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
private static boolean hasNoSideEffectsAnnotation(Node node) { |
| 105 |
JSDocInfo docInfo = node.getJSDocInfo(); |
| 106 |
return docInfo != null && docInfo.isNoSideEffects(); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private class GatherNoSideEffectFunctions extends AbstractPostOrderCallback { |
| 113 |
private final boolean inExterns; |
| 114 |
|
| 115 |
GatherNoSideEffectFunctions(boolean inExterns) { |
| 116 |
this.inExterns = inExterns; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public void visit(NodeTraversal traversal, Node node, Node parent) { |
| 121 |
if (!inExterns && hasNoSideEffectsAnnotation(node)) { |
| 122 |
traversal.report(node, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 123 |
} |
| 124 |
|
| 125 |
if (node.isGetProp()) { |
| 126 |
if (parent.isExprResult() && |
| 127 |
hasNoSideEffectsAnnotation(node)) { |
| 128 |
noSideEffectFunctionNames.add(node); |
| 129 |
} |
| 130 |
} else if (node.isFunction()) { |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
boolean hasAnnotation = hasNoSideEffectsAnnotation(node); |
| 135 |
List<Node> nameNodes = Lists.newArrayList(); |
| 136 |
nameNodes.add(node.getFirstChild()); |
| 137 |
|
| 138 |
if (parent.isName()) { |
| 139 |
Node gramp = parent.getParent(); |
| 140 |
if (gramp.isVar() && |
| 141 |
gramp.hasOneChild() && |
| 142 |
hasNoSideEffectsAnnotation(gramp)) { |
| 143 |
hasAnnotation = true; |
| 144 |
} |
| 145 |
|
| 146 |
nameNodes.add(parent); |
| 147 |
} else if (parent.isAssign()) { |
| 148 |
if (hasNoSideEffectsAnnotation(parent)) { |
| 149 |
hasAnnotation = true; |
| 150 |
} |
| 151 |
|
| 152 |
nameNodes.add(parent.getFirstChild()); |
| 153 |
} |
| 154 |
|
| 155 |
if (hasAnnotation) { |
| 156 |
noSideEffectFunctionNames.addAll(nameNodes); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private class SetNoSideEffectCallProperty extends AbstractPostOrderCallback { |
| 167 |
private final SimpleDefinitionFinder defFinder; |
| 168 |
|
| 169 |
SetNoSideEffectCallProperty(SimpleDefinitionFinder defFinder) { |
| 170 |
this.defFinder = defFinder; |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void visit(NodeTraversal traversal, Node node, Node parent) { |
| 175 |
if (!node.isCall() && !node.isNew()) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
Collection<Definition> definitions = |
| 180 |
defFinder.getDefinitionsReferencedAt(node.getFirstChild()); |
| 181 |
if (definitions == null) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
for (Definition def : definitions) { |
| 186 |
Node lValue = def.getLValue(); |
| 187 |
Preconditions.checkNotNull(lValue); |
| 188 |
if (!noSideEffectFunctionNames.contains(lValue) && |
| 189 |
definitionTypeContainsFunctionType(def)) { |
| 190 |
return; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
node.setSideEffectFlags(Node.NO_SIDE_EFFECTS); |
| 195 |
} |
| 196 |
} |
| 197 |
} |