Search in sources :

Example 1 with UNKNOWN_TYPE

use of com.google.javascript.rhino.jstype.JSTypeNative.UNKNOWN_TYPE in project closure-compiler by google.

the class TypeInferencePass method inferAllScopes.

/**
 * Execute type inference running over part of the scope tree.
 *
 * @return the top scope, either newly created, or patched by this inference.
 */
TypedScope inferAllScopes(Node inferenceRoot) {
    // order to propagate the type of ns.method in the outer scope.
    try (JSTypeResolver.Closer closer = this.registry.getResolver().openForDefinition()) {
        checkState(inferenceRoot.isRoot());
        checkState(inferenceRoot.getParent() == null);
        checkState(this.topScope == null);
        this.topScope = scopeCreator.createScope(inferenceRoot, null);
        NodeTraversal.builder().setCompiler(compiler).setCallback(new FirstScopeBuildingCallback()).setScopeCreator(scopeCreator).traverseWithScope(inferenceRoot, this.topScope);
        scopeCreator.resolveWeakImportsPreResolution();
    }
    scopeCreator.finishAndFreeze();
    NodeTraversal.builder().setCompiler(compiler).setCallback(new SecondScopeBuildingCallback()).setScopeCreator(scopeCreator).traverseWithScope(inferenceRoot, this.topScope);
    // Normalize TypedVars to have the '?' type instead of null after inference is complete. This
    // currently cannot be done any earlier because it breaks inference of variables assigned in
    // local scopes.
    // TODO(b/149843534): this should be a crash instead.
    final JSType unknownType = this.registry.getNativeType(UNKNOWN_TYPE);
    for (TypedVar var : this.scopeCreator.getAllSymbols()) {
        if (var.getType() == null) {
            var.setType(unknownType);
        }
    }
    if (this.stepCountHistogram != null) {
        try (LogFile histogram = this.compiler.createOrReopenLog(this.getClass(), "step_histogram.log")) {
            histogram.log("step_count token population");
            int[] totals = new int[] { 0, 0 };
            this.stepCountHistogram.keySet().stream().sorted(Comparator.<Integer>naturalOrder().reversed()).forEach((stepCount) -> this.stepCountHistogram.get(stepCount).entrySet().stream().sorted(comparingInt(Multiset.Entry::getCount)).forEach((e) -> {
                totals[0] += stepCount * e.getCount();
                totals[1] += e.getCount();
                histogram.log("%s %s %s", stepCount, e.getElement(), e.getCount());
            }));
            histogram.log("%s TOTAL %s", totals[0], totals[1]);
        }
    }
    return this.topScope;
}
Also used : LogFile(com.google.javascript.jscomp.diagnostic.LogFile) Comparator.comparingInt(java.util.Comparator.comparingInt) LinearFlowState(com.google.javascript.jscomp.DataFlowAnalysis.LinearFlowState) AssertionFunctionLookup(com.google.javascript.jscomp.CodingConvention.AssertionFunctionLookup) AbstractScopedCallback(com.google.javascript.jscomp.NodeTraversal.AbstractScopedCallback) Token(com.google.javascript.rhino.Token) JSType(com.google.javascript.rhino.jstype.JSType) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) ReverseAbstractInterpreter(com.google.javascript.jscomp.type.ReverseAbstractInterpreter) Multiset(com.google.common.collect.Multiset) UNKNOWN_TYPE(com.google.javascript.rhino.jstype.JSTypeNative.UNKNOWN_TYPE) JSTypeResolver(com.google.javascript.rhino.jstype.JSTypeResolver) Preconditions.checkState(com.google.common.base.Preconditions.checkState) LinkedHashMap(java.util.LinkedHashMap) HashMultiset(com.google.common.collect.HashMultiset) JSTypeRegistry(com.google.javascript.rhino.jstype.JSTypeRegistry) LogFile(com.google.javascript.jscomp.diagnostic.LogFile) Comparator(java.util.Comparator) Node(com.google.javascript.rhino.Node) JSType(com.google.javascript.rhino.jstype.JSType) JSTypeResolver(com.google.javascript.rhino.jstype.JSTypeResolver) Multiset(com.google.common.collect.Multiset) HashMultiset(com.google.common.collect.HashMultiset)

Aggregations

Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 HashMultiset (com.google.common.collect.HashMultiset)1 Multiset (com.google.common.collect.Multiset)1 AssertionFunctionLookup (com.google.javascript.jscomp.CodingConvention.AssertionFunctionLookup)1 LinearFlowState (com.google.javascript.jscomp.DataFlowAnalysis.LinearFlowState)1 AbstractScopedCallback (com.google.javascript.jscomp.NodeTraversal.AbstractScopedCallback)1 LogFile (com.google.javascript.jscomp.diagnostic.LogFile)1 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)1 ReverseAbstractInterpreter (com.google.javascript.jscomp.type.ReverseAbstractInterpreter)1 Node (com.google.javascript.rhino.Node)1 Token (com.google.javascript.rhino.Token)1 JSType (com.google.javascript.rhino.jstype.JSType)1 UNKNOWN_TYPE (com.google.javascript.rhino.jstype.JSTypeNative.UNKNOWN_TYPE)1 JSTypeRegistry (com.google.javascript.rhino.jstype.JSTypeRegistry)1 JSTypeResolver (com.google.javascript.rhino.jstype.JSTypeResolver)1 Comparator (java.util.Comparator)1 Comparator.comparingInt (java.util.Comparator.comparingInt)1 LinkedHashMap (java.util.LinkedHashMap)1