use of com.google.javascript.jscomp.type.ReverseAbstractInterpreter in project closure-compiler by google.
the class TypeInferenceTest method parseAndRunTypeInference.
@SuppressWarnings({ "MustBeClosedChecker" })
private void parseAndRunTypeInference(Node root, Node cfgRoot) {
this.closer.close();
TypedScopeCreator scopeCreator = new TypedScopeCreator(compiler);
TypedScope assumedScope;
try (JSTypeResolver.Closer closer = this.registry.getResolver().openForDefinition()) {
// Create the scope with the assumptions.
// Also populate a map allowing us to look up labeled statements later.
labeledStatementMap = new HashMap<>();
NodeTraversal.builder().setCompiler(compiler).setCallback(new AbstractScopedCallback() {
@Override
public void enterScope(NodeTraversal t) {
t.getTypedScope();
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
TypedScope scope = t.getTypedScope();
if (parent != null && parent.isLabel() && !n.isLabelName()) {
// First child of a LABEL is a LABEL_NAME, n is the second child.
Node labelNameNode = checkNotNull(n.getPrevious(), n);
checkState(labelNameNode.isLabelName(), labelNameNode);
String labelName = labelNameNode.getString();
assertWithMessage("Duplicate label name: %s", labelName).that(labeledStatementMap).doesNotContainKey(labelName);
labeledStatementMap.put(labelName, new LabeledStatement(n, scope));
}
}
}).setScopeCreator(scopeCreator).traverse(root);
assumedScope = scopeCreator.createScope(cfgRoot);
for (Map.Entry<String, JSType> entry : assumptions.entrySet()) {
assumedScope.declare(entry.getKey(), null, entry.getValue(), null, false);
}
scopeCreator.resolveWeakImportsPreResolution();
}
scopeCreator.finishAndFreeze();
// Create the control graph.
ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, true);
cfa.process(null, cfgRoot);
ControlFlowGraph<Node> cfg = cfa.getCfg();
// Create a simple reverse abstract interpreter.
ReverseAbstractInterpreter rai = compiler.getReverseAbstractInterpreter();
// Do the type inference by data-flow analysis.
TypeInference dfa = new TypeInference(compiler, cfg, rai, assumedScope, scopeCreator, ASSERTION_FUNCTION_MAP);
dfa.analyze();
// Get the scope of the implicit return.
LinearFlowState<FlowScope> rtnState = cfg.getImplicitReturn().getAnnotation();
if (cfgRoot.isFunction()) {
// Reset the flow scope's syntactic scope to the function block, rather than the function
// node
// itself. This allows pulling out local vars from the function by name to verify their
// types.
returnScope = rtnState.getIn().withSyntacticScope(scopeCreator.createScope(cfgRoot.getLastChild()));
} else {
returnScope = rtnState.getIn();
}
this.closer = this.registry.getResolver().openForDefinition();
}
use of com.google.javascript.jscomp.type.ReverseAbstractInterpreter in project closure-compiler by google.
the class CompilerTestCase method createTypeCheck.
/**
* Returns a newly created TypeCheck.
*/
private static TypeCheck createTypeCheck(Compiler compiler) {
ReverseAbstractInterpreter rai = new SemanticReverseAbstractInterpreter(compiler.getTypeRegistry());
TypeCheck typeChecker = new TypeCheck(compiler, rai, compiler.getTypeRegistry());
compiler.setTypeCheckingHasRun(true);
return typeChecker;
}
Aggregations