| 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.Maps; |
| 21 |
import com.google.javascript.jscomp.CodingConvention.AssertionFunctionSpec; |
| 22 |
import com.google.javascript.jscomp.NodeTraversal.AbstractScopedCallback; |
| 23 |
import com.google.javascript.jscomp.type.ReverseAbstractInterpreter; |
| 24 |
import com.google.javascript.rhino.Node; |
| 25 |
|
| 26 |
import java.util.Map; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class TypeInferencePass implements CompilerPass { |
| 33 |
|
| 34 |
static final DiagnosticType DATAFLOW_ERROR = DiagnosticType.warning( |
| 35 |
"JSC_INTERNAL_ERROR_DATAFLOW", |
| 36 |
"non-monotonic data-flow analysis"); |
| 37 |
|
| 38 |
private final AbstractCompiler compiler; |
| 39 |
private final ReverseAbstractInterpreter reverseInterpreter; |
| 40 |
private Scope topScope; |
| 41 |
private MemoizedScopeCreator scopeCreator; |
| 42 |
private final Map<String, AssertionFunctionSpec> assertionFunctionsMap; |
| 43 |
|
| 44 |
TypeInferencePass(AbstractCompiler compiler, |
| 45 |
ReverseAbstractInterpreter reverseInterpreter, |
| 46 |
Scope topScope, MemoizedScopeCreator scopeCreator) { |
| 47 |
this.compiler = compiler; |
| 48 |
this.reverseInterpreter = reverseInterpreter; |
| 49 |
this.topScope = topScope; |
| 50 |
this.scopeCreator = scopeCreator; |
| 51 |
|
| 52 |
assertionFunctionsMap = Maps.newHashMap(); |
| 53 |
for (AssertionFunctionSpec assertionFucntion : |
| 54 |
compiler.getCodingConvention().getAssertionFunctions()) { |
| 55 |
assertionFunctionsMap.put(assertionFucntion.getFunctionName(), |
| 56 |
assertionFucntion); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@Override |
| 67 |
public void process(Node externsRoot, Node jsRoot) { |
| 68 |
Node externsAndJs = jsRoot.getParent(); |
| 69 |
Preconditions.checkState(externsAndJs != null); |
| 70 |
Preconditions.checkState( |
| 71 |
externsRoot == null || externsAndJs.hasChild(externsRoot)); |
| 72 |
|
| 73 |
inferAllScopes(externsAndJs); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
void inferAllScopes(Node node) { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
(new NodeTraversal( |
| 102 |
compiler, new FirstScopeBuildingCallback(), scopeCreator)) |
| 103 |
.traverseWithScope(node, topScope); |
| 104 |
(new NodeTraversal( |
| 105 |
compiler, new SecondScopeBuildingCallback(), scopeCreator)) |
| 106 |
.traverseWithScope(node, topScope); |
| 107 |
} |
| 108 |
|
| 109 |
void inferScope(Node n, Scope scope) { |
| 110 |
TypeInference typeInference = |
| 111 |
new TypeInference( |
| 112 |
compiler, computeCfg(n), reverseInterpreter, scope, |
| 113 |
assertionFunctionsMap); |
| 114 |
try { |
| 115 |
typeInference.analyze(); |
| 116 |
|
| 117 |
|
| 118 |
compiler.getTypeRegistry().resolveTypesInScope(scope); |
| 119 |
|
| 120 |
} catch (DataFlowAnalysis.MaxIterationsExceededException e) { |
| 121 |
compiler.report(JSError.make(n.getSourceFileName(), n, DATAFLOW_ERROR)); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
private class FirstScopeBuildingCallback extends AbstractScopedCallback { |
| 126 |
@Override |
| 127 |
public void enterScope(NodeTraversal t) { |
| 128 |
t.getScope(); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 133 |
|
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private class SecondScopeBuildingCallback extends AbstractScopedCallback { |
| 138 |
@Override |
| 139 |
public void enterScope(NodeTraversal t) { |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
inferScope(t.getCurrentNode(), t.getScope()); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 148 |
|
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
private ControlFlowGraph<Node> computeCfg(Node n) { |
| 153 |
ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, false); |
| 154 |
cfa.process(null, n); |
| 155 |
return cfa.getCfg(); |
| 156 |
} |
| 157 |
} |