Search in sources :

Example 1 with DiGraphEdge

use of com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge in project closure-compiler by google.

the class NewTypeInference method getOutEnv.

private TypeEnv getOutEnv(DiGraphNode<Node, ControlFlowGraph.Branch> dn) {
    List<DiGraphEdge<Node, ControlFlowGraph.Branch>> outEdges = dn.getOutEdges();
    if (outEdges.isEmpty()) {
        // This occurs when visiting a throw in the backward direction.
        checkArgument(dn.getValue().isThrow());
        return this.typeEnvFromDeclaredTypes;
    }
    if (outEdges.size() == 1) {
        return envs.get(outEdges.get(0));
    }
    Set<TypeEnv> envSet = new LinkedHashSet<>();
    for (DiGraphEdge<Node, ControlFlowGraph.Branch> de : outEdges) {
        TypeEnv env = envs.get(de);
        if (env != null) {
            envSet.add(env);
        }
    }
    checkState(!envSet.isEmpty());
    return TypeEnv.join(envSet);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DiGraphEdge(com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) TypeEnv(com.google.javascript.jscomp.newtypes.TypeEnv)

Example 2 with DiGraphEdge

use of com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge in project closure-compiler by google.

the class ControlFlowAnalysisTest method assertReturnEdge.

/**
 * Assert that there exists a control flow edge of the given type
 * from some node with the first token to the return node.
 */
private static void assertReturnEdge(ControlFlowGraph<Node> cfg, Token startToken) {
    List<DiGraphEdge<Node, Branch>> edges = getAllEdges(cfg);
    for (DiGraphEdge<Node, Branch> edge : edges) {
        Node source = edge.getSource().getValue();
        DiGraphNode<Node, Branch> dest = edge.getDestination();
        if (source.getToken() == startToken && cfg.isImplicitReturn(dest)) {
            return;
        }
    }
    fail("No return edge found");
}
Also used : DiGraphEdge(com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge) Branch(com.google.javascript.jscomp.ControlFlowGraph.Branch) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) Node(com.google.javascript.rhino.Node)

Example 3 with DiGraphEdge

use of com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge in project closure-compiler by google.

the class ControlFlowAnalysisTest method getAllEdges.

/**
 * Gets all the control flow edges from some node with the first token to
 * some node with the second token.
 */
private static List<DiGraphEdge<Node, Branch>> getAllEdges(ControlFlowGraph<Node> cfg, Token startToken, Token endToken) {
    List<DiGraphEdge<Node, Branch>> edges = getAllEdges(cfg);
    Iterator<DiGraphEdge<Node, Branch>> it = edges.iterator();
    while (it.hasNext()) {
        DiGraphEdge<Node, Branch> edge = it.next();
        Node startNode = edge.getSource().getValue();
        Node endNode = edge.getDestination().getValue();
        if (startNode == null || endNode == null || startNode.getToken() != startToken || endNode.getToken() != endToken) {
            it.remove();
        }
    }
    return edges;
}
Also used : DiGraphEdge(com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge) Branch(com.google.javascript.jscomp.ControlFlowGraph.Branch) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) Node(com.google.javascript.rhino.Node)

Example 4 with DiGraphEdge

use of com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge in project closure-compiler by google.

the class ControlFlowAnalysisTest method assertNoReturnEdge.

/**
 * Assert that there exists no control flow edge of the given type
 * from some node with the first token to the return node.
 */
private static void assertNoReturnEdge(ControlFlowGraph<Node> cfg, Token startToken) {
    List<DiGraphEdge<Node, Branch>> edges = getAllEdges(cfg);
    for (DiGraphEdge<Node, Branch> edge : edges) {
        Node source = edge.getSource().getValue();
        DiGraphNode<Node, Branch> dest = edge.getDestination();
        if (source.getToken() == startToken) {
            assertFalse("Token " + startToken + " should not have an out going" + " edge to the implicit return", cfg.isImplicitReturn(dest));
            return;
        }
    }
}
Also used : DiGraphEdge(com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge) Branch(com.google.javascript.jscomp.ControlFlowGraph.Branch) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) Node(com.google.javascript.rhino.Node)

Example 5 with DiGraphEdge

use of com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge in project closure-compiler by google.

the class DotFormatter method traverseNodes.

private void traverseNodes(Node parent) throws IOException {
    // key
    int keyParent = key(parent);
    // edges
    for (Node child = parent.getFirstChild(); child != null; child = child.getNext()) {
        int keyChild = key(child);
        builder.append(INDENT);
        builder.append(formatNodeName(keyParent));
        builder.append(ARROW);
        builder.append(formatNodeName(keyChild));
        builder.append(" [weight=1];\n");
        traverseNodes(child);
    }
    // Flow Edges
    if (cfg != null && cfg.hasNode(parent)) {
        List<DiGraphEdge<Node, Branch>> outEdges = cfg.getOutEdges(parent);
        String[] edgeList = new String[outEdges.size()];
        for (int i = 0; i < edgeList.length; i++) {
            DiGraphEdge<Node, ControlFlowGraph.Branch> edge = outEdges.get(i);
            DiGraphNode<Node, Branch> succ = edge.getDestination();
            String toNode = null;
            if (succ == cfg.getImplicitReturn()) {
                toNode = "RETURN";
            } else {
                int keySucc = key(succ.getValue());
                toNode = formatNodeName(keySucc);
            }
            edgeList[i] = formatNodeName(keyParent) + ARROW + toNode + " [label=\"" + edge.getValue() + "\", " + "fontcolor=\"red\", " + "weight=0.01, color=\"red\"];\n";
        }
        Arrays.sort(edgeList);
        for (String element : edgeList) {
            builder.append(INDENT);
            builder.append(element);
        }
    }
}
Also used : DiGraphEdge(com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge) Branch(com.google.javascript.jscomp.ControlFlowGraph.Branch) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) GraphvizNode(com.google.javascript.jscomp.graph.GraphvizGraph.GraphvizNode) Node(com.google.javascript.rhino.Node)

Aggregations

DiGraphEdge (com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge)9 Node (com.google.javascript.rhino.Node)9 Branch (com.google.javascript.jscomp.ControlFlowGraph.Branch)7 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)7 TypeEnv (com.google.javascript.jscomp.newtypes.TypeEnv)2 LinkedHashSet (java.util.LinkedHashSet)2 GraphvizNode (com.google.javascript.jscomp.graph.GraphvizGraph.GraphvizNode)1 FlowScope (com.google.javascript.jscomp.type.FlowScope)1 JSType (com.google.javascript.rhino.jstype.JSType)1 ObjectType (com.google.javascript.rhino.jstype.ObjectType)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1