com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node Java Examples

The following examples show how to use com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Infer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getDependencyName(GraphUtils.Node<ListBuffer<Type>> to, GraphUtils.DependencyKind dk) {
    if (dk == DependencyKind.STUCK) return "";
    else {
        StringBuilder buf = new StringBuilder();
        String sep = "";
        for (Type from : data) {
            UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from);
            for (Type bound : uv.getBounds(InferenceBound.values())) {
                if (bound.containsAny(List.from(to.data))) {
                    buf.append(sep);
                    buf.append(bound);
                    sep = ",";
                }
            }
        }
        return buf.toString();
    }
}
 
Example #2
Source File: Infer.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes a path that goes from a given node to the leafs in the graph.
 * Typically this will start from a node containing a variable in
 * {@code varsToSolve}. For any given path, the cost is computed as the total
 * number of type-variables that should be eagerly instantiated across that path.
 */
Pair<List<Node>, Integer> computeTreeToLeafs(Node n) {
    Pair<List<Node>, Integer> cachedPath = treeCache.get(n);
    if (cachedPath == null) {
        //cache miss
        if (n.isLeaf()) {
            //if leaf, stop
            cachedPath = new Pair<List<Node>, Integer>(List.of(n), n.data.length());
        } else {
            //if non-leaf, proceed recursively
            Pair<List<Node>, Integer> path = new Pair<List<Node>, Integer>(List.of(n), n.data.length());
            for (Node n2 : n.getAllDependencies()) {
                if (n2 == n) continue;
                Pair<List<Node>, Integer> subpath = computeTreeToLeafs(n2);
                path = new Pair<List<Node>, Integer>(
                        path.fst.prependList(subpath.fst),
                        path.snd + subpath.snd);
            }
            cachedPath = path;
        }
        //save results in cache
        treeCache.put(n, cachedPath);
    }
    return cachedPath;
}
 
Example #3
Source File: Infer.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes a path that goes from a given node to the leafs in the graph.
 * Typically this will start from a node containing a variable in
 * {@code varsToSolve}. For any given path, the cost is computed as the total
 * number of type-variables that should be eagerly instantiated across that path.
 */
Pair<List<Node>, Integer> computeTreeToLeafs(Node n) {
    Pair<List<Node>, Integer> cachedPath = treeCache.get(n);
    if (cachedPath == null) {
        //cache miss
        if (n.isLeaf()) {
            //if leaf, stop
            cachedPath = new Pair<List<Node>, Integer>(List.of(n), n.data.length());
        } else {
            //if non-leaf, proceed recursively
            Pair<List<Node>, Integer> path = new Pair<List<Node>, Integer>(List.of(n), n.data.length());
            for (Node n2 : n.getAllDependencies()) {
                if (n2 == n) continue;
                Pair<List<Node>, Integer> subpath = computeTreeToLeafs(n2);
                path = new Pair<List<Node>, Integer>(
                        path.fst.prependList(subpath.fst),
                        path.snd + subpath.snd);
            }
            cachedPath = path;
        }
        //save results in cache
        treeCache.put(n, cachedPath);
    }
    return cachedPath;
}
 
Example #4
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Pick the leaf that minimize cost
 */
@Override
public Node pickNode(final InferenceGraph g) {
    treeCache.clear(); //graph changes at every step - cache must be cleared
    Pair<List<Node>, Integer> bestPath = noPath;
    for (Node n : g.nodes) {
        if (!Collections.disjoint(n.data, varsToSolve)) {
            Pair<List<Node>, Integer> path = computeTreeToLeafs(n);
            //discard all paths containing at least a node in the
            //closure computed above
            if (path.snd < bestPath.snd) {
                bestPath = path;
            }
        }
    }
    if (bestPath == noPath) {
        //no path leads there
        throw new NodeNotFoundException(g);
    }
    return bestPath.fst.head;
}
 
Example #5
Source File: Infer.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getDependencyName(GraphUtils.Node<ListBuffer<Type>> to, GraphUtils.DependencyKind dk) {
    if (dk == DependencyKind.STUCK) return "";
    else {
        StringBuilder buf = new StringBuilder();
        String sep = "";
        for (Type from : data) {
            UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from);
            for (Type bound : uv.getBounds(InferenceBound.values())) {
                if (bound.containsAny(List.from(to.data))) {
                    buf.append(sep);
                    buf.append(bound);
                    sep = ",";
                }
            }
        }
        return buf.toString();
    }
}
 
Example #6
Source File: Infer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Pick the leaf that minimize cost
 */
@Override
public Node pickNode(final InferenceGraph g) {
    treeCache.clear(); //graph changes at every step - cache must be cleared
    Pair<List<Node>, Integer> bestPath = noPath;
    for (Node n : g.nodes) {
        if (!Collections.disjoint(n.data, varsToSolve)) {
            Pair<List<Node>, Integer> path = computeTreeToLeafs(n);
            //discard all paths containing at least a node in the
            //closure computed above
            if (path.snd < bestPath.snd) {
                bestPath = path;
            }
        }
    }
    if (bestPath == noPath) {
        //no path leads there
        throw new NodeNotFoundException(g);
    }
    return bestPath.fst.head;
}
 
Example #7
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Properties dependencyAttributes(Node sink, GraphUtils.DependencyKind dk) {
    Properties p = new Properties();
    p.put("style", ((DependencyKind)dk).dotSyle);
    StringBuilder buf = new StringBuilder();
    String sep = "";
    for (Type from : data) {
        UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from);
        for (Type bound : uv.getBounds(InferenceBound.values())) {
            if (bound.containsAny(List.from(sink.data))) {
                buf.append(sep);
                buf.append(bound);
                sep = ",";
            }
        }
    }
    p.put("label", "\"" + buf.toString() + "\"");
    return p;
}
 
Example #8
Source File: Infer.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Pick the leaf that minimize cost
 */
@Override
public Node pickNode(final InferenceGraph g) {
    treeCache.clear(); //graph changes at every step - cache must be cleared
    Pair<List<Node>, Integer> bestPath = noPath;
    for (Node n : g.nodes) {
        if (!Collections.disjoint(n.data, varsToSolve)) {
            Pair<List<Node>, Integer> path = computeTreeToLeafs(n);
            //discard all paths containing at least a node in the
            //closure computed above
            if (path.snd < bestPath.snd) {
                bestPath = path;
            }
        }
    }
    if (bestPath == noPath) {
        //no path leads there
        throw new NodeNotFoundException(g);
    }
    return bestPath.fst.head;
}
 
Example #9
Source File: Infer.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Pick the leaf that minimize cost
 */
@Override
public Node pickNode(final InferenceGraph g) {
    treeCache.clear(); //graph changes at every step - cache must be cleared
    Pair<List<Node>, Integer> bestPath = noPath;
    for (Node n : g.nodes) {
        if (!Collections.disjoint(n.data, varsToSolve)) {
            Pair<List<Node>, Integer> path = computeTreeToLeafs(n);
            //discard all paths containing at least a node in the
            //closure computed above
            if (path.snd < bestPath.snd) {
                bestPath = path;
            }
        }
    }
    if (bestPath == noPath) {
        //no path leads there
        throw new NodeNotFoundException(g);
    }
    return bestPath.fst.head;
}
 
Example #10
Source File: Infer.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Pick the leaf that minimize cost
 */
@Override
public Node pickNode(final InferenceGraph g) {
    treeCache.clear(); //graph changes at every step - cache must be cleared
    Pair<List<Node>, Integer> bestPath = noPath;
    for (Node n : g.nodes) {
        if (!Collections.disjoint(n.data, varsToSolve)) {
            Pair<List<Node>, Integer> path = computeTreeToLeafs(n);
            //discard all paths containing at least a node in the
            //closure computed above
            if (path.snd < bestPath.snd) {
                bestPath = path;
            }
        }
    }
    if (bestPath == noPath) {
        //no path leads there
        throw new NodeNotFoundException(g);
    }
    return bestPath.fst.head;
}
 
Example #11
Source File: Infer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Properties dependencyAttributes(Node sink, GraphUtils.DependencyKind dk) {
    Properties p = new Properties();
    p.put("style", ((DependencyKind)dk).dotSyle);
    StringBuilder buf = new StringBuilder();
    String sep = "";
    for (Type from : data) {
        UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from);
        for (Type bound : uv.getBounds(InferenceBound.values())) {
            if (bound.containsAny(List.from(sink.data))) {
                buf.append(sep);
                buf.append(bound);
                sep = ",";
            }
        }
    }
    p.put("label", "\"" + buf.toString() + "\"");
    return p;
}
 
Example #12
Source File: Infer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic lookup helper for retrieving a graph node given an inference
 * variable type.
 */
public Node findNode(Type t) {
    for (Node n : nodes) {
        if (n.data.contains(t)) {
            return n;
        }
    }
    return null;
}
 
Example #13
Source File: Infer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Is this node a leaf? This means either the node has no dependencies,
 * or it just has self-dependencies.
 */
protected boolean isLeaf() {
    //no deps, or only one self dep
    Set<Node> allDeps = getDependencies(DependencyKind.BOUND, DependencyKind.STUCK);
    if (allDeps.isEmpty()) return true;
    for (Node n : allDeps) {
        if (n != this) {
            return false;
        }
    }
    return true;
}
 
Example #14
Source File: Infer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notify all nodes that something has changed in the graph
 * topology.
 */
private void graphChanged(Node from, Node to) {
    for (DependencyKind dk : removeDependency(from)) {
        if (to != null) {
            addDependency(dk, to);
        }
    }
}
 
Example #15
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Node pickNode(InferenceGraph g) {
    if (g.nodes.isEmpty()) {
        //should not happen
        throw new NodeNotFoundException(g);
    }
    return g.nodes.get(0);
}
 
Example #16
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Solve variables in a given inference context. The amount of variables
 * to be solved, and the way in which the underlying acyclic graph is explored
 * depends on the selected solver strategy.
 */
void solve(GraphStrategy sstrategy) {
    doIncorporation(inferenceContext, warn); //initial propagation of bounds
    InferenceGraph inferenceGraph = new InferenceGraph();
    while (!sstrategy.done()) {
        if (dependenciesFolder != null) {
            //add this graph to the pending queue
            pendingGraphs = pendingGraphs.prepend(inferenceGraph.toDot());
        }
        InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
        List<Type> varsToSolve = List.from(nodeToSolve.data);
        List<Type> saved_undet = inferenceContext.save();
        try {
            //repeat until all variables are solved
            outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
                //for each inference phase
                for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
                    if (inferenceContext.solveBasic(varsToSolve, step.steps).nonEmpty()) {
                        doIncorporation(inferenceContext, warn);
                        continue outer;
                    }
                }
                //no progress
                throw inferenceException.setMessage();
            }
        }
        catch (InferenceException ex) {
            //did we fail because of interdependent ivars?
            inferenceContext.rollback(saved_undet);
            instantiateAsUninferredVars(varsToSolve, inferenceContext);
            doIncorporation(inferenceContext, warn);
        }
        inferenceGraph.deleteNode(nodeToSolve);
    }
}
 
Example #17
Source File: Infer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds dependency with given kind.
 */
protected void addDependency(DependencyKind dk, Node depToAdd) {
    Set<Node> depsByKind = deps.get(dk);
    if (depsByKind == null) {
        depsByKind = new LinkedHashSet<Node>();
        deps.put(dk, depsByKind);
    }
    depsByKind.add(depToAdd);
}
 
Example #18
Source File: Infer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Is this node a leaf? This means either the node has no dependencies,
 * or it just has self-dependencies.
 */
protected boolean isLeaf() {
    //no deps, or only one self dep
    Set<Node> allDeps = getDependencies(DependencyKind.BOUND, DependencyKind.STUCK);
    if (allDeps.isEmpty()) return true;
    for (Node n : allDeps) {
        if (n != this) {
            return false;
        }
    }
    return true;
}
 
Example #19
Source File: Infer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public Node pickNode(InferenceGraph g) {
    if (g.nodes.isEmpty()) {
        //should not happen
        throw new NodeNotFoundException(g);
    };
    return g.nodes.get(0);
}
 
Example #20
Source File: Infer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove a dependency, regardless of its kind.
 */
protected Set<DependencyKind> removeDependency(Node n) {
    Set<DependencyKind> removedKinds = new HashSet<>();
    for (DependencyKind dk : DependencyKind.values()) {
        Set<Node> depsByKind = deps.get(dk);
        if (depsByKind == null) continue;
        if (depsByKind.remove(n)) {
            removedKinds.add(dk);
        }
    }
    return removedKinds;
}
 
Example #21
Source File: Infer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Is this node a leaf? This means either the node has no dependencies,
 * or it just has self-dependencies.
 */
protected boolean isLeaf() {
    //no deps, or only one self dep
    Set<Node> allDeps = getDependencies(DependencyKind.BOUND, DependencyKind.STUCK);
    if (allDeps.isEmpty()) return true;
    for (Node n : allDeps) {
        if (n != this) {
            return false;
        }
    }
    return true;
}
 
Example #22
Source File: Infer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieves all dependencies with given kind(s).
 */
protected Set<Node> getDependencies(DependencyKind... depKinds) {
    Set<Node> buf = new LinkedHashSet<Node>();
    for (DependencyKind dk : depKinds) {
        Set<Node> depsByKind = deps.get(dk);
        if (depsByKind != null) {
            buf.addAll(depsByKind);
        }
    }
    return buf;
}
 
Example #23
Source File: Infer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notify all nodes that something has changed in the graph
 * topology.
 */
private void graphChanged(Node from, Node to) {
    for (DependencyKind dk : removeDependency(from)) {
        if (to != null) {
            addDependency(dk, to);
        }
    }
}
 
Example #24
Source File: Infer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove a dependency, regardless of its kind.
 */
protected Set<DependencyKind> removeDependency(Node n) {
    Set<DependencyKind> removedKinds = new HashSet<>();
    for (DependencyKind dk : DependencyKind.values()) {
        Set<Node> depsByKind = deps.get(dk);
        if (depsByKind == null) continue;
        if (depsByKind.remove(n)) {
            removedKinds.add(dk);
        }
    }
    return removedKinds;
}
 
Example #25
Source File: Infer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieves all dependencies with given kind(s).
 */
protected Set<Node> getDependencies(DependencyKind... depKinds) {
    Set<Node> buf = new LinkedHashSet<Node>();
    for (DependencyKind dk : depKinds) {
        Set<Node> depsByKind = deps.get(dk);
        if (depsByKind != null) {
            buf.addAll(depsByKind);
        }
    }
    return buf;
}
 
Example #26
Source File: Infer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove a dependency, regardless of its kind.
 */
protected Set<DependencyKind> removeDependency(Node n) {
    Set<DependencyKind> removedKinds = new HashSet<>();
    for (DependencyKind dk : DependencyKind.values()) {
        Set<Node> depsByKind = deps.get(dk);
        if (depsByKind == null) continue;
        if (depsByKind.remove(n)) {
            removedKinds.add(dk);
        }
    }
    return removedKinds;
}
 
Example #27
Source File: Infer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Solve variables in a given inference context. The amount of variables
 * to be solved, and the way in which the underlying acyclic graph is explored
 * depends on the selected solver strategy.
 */
void solve(GraphStrategy sstrategy) {
    checkWithinBounds(inferenceContext, warn); //initial propagation of bounds
    InferenceGraph inferenceGraph = new InferenceGraph(stuckDeps);
    while (!sstrategy.done()) {
        InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
        List<Type> varsToSolve = List.from(nodeToSolve.data);
        List<Type> saved_undet = inferenceContext.save();
        try {
            //repeat until all variables are solved
            outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
                //for each inference phase
                for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
                    if (inferenceContext.solveBasic(varsToSolve, step.steps)) {
                        checkWithinBounds(inferenceContext, warn);
                        continue outer;
                    }
                }
                //no progress
                throw inferenceException.setMessage();
            }
        }
        catch (InferenceException ex) {
            //did we fail because of interdependent ivars?
            inferenceContext.rollback(saved_undet);
            instantiateAsUninferredVars(varsToSolve, inferenceContext);
            checkWithinBounds(inferenceContext, warn);
        }
        inferenceGraph.deleteNode(nodeToSolve);
    }
}
 
Example #28
Source File: Infer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Collection<? extends Node> getDependenciesByKind(GraphUtils.DependencyKind dk) {
    if (dk == DependencyKind.BOUND) {
        return deps;
    } else {
        throw new IllegalStateException();
    }
}
 
Example #29
Source File: Infer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Solve variables in a given inference context. The amount of variables
 * to be solved, and the way in which the underlying acyclic graph is explored
 * depends on the selected solver strategy.
 */
void solve(GraphStrategy sstrategy) {
    checkWithinBounds(inferenceContext, warn); //initial propagation of bounds
    InferenceGraph inferenceGraph = new InferenceGraph(stuckDeps);
    while (!sstrategy.done()) {
        InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
        List<Type> varsToSolve = List.from(nodeToSolve.data);
        List<Type> saved_undet = inferenceContext.save();
        try {
            //repeat until all variables are solved
            outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
                //for each inference phase
                for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
                    if (inferenceContext.solveBasic(varsToSolve, step.steps)) {
                        checkWithinBounds(inferenceContext, warn);
                        continue outer;
                    }
                }
                //no progress
                throw inferenceException.setMessage();
            }
        }
        catch (InferenceException ex) {
            //did we fail because of interdependent ivars?
            inferenceContext.rollback(saved_undet);
            instantiateAsUninferredVars(varsToSolve, inferenceContext);
            checkWithinBounds(inferenceContext, warn);
        }
        inferenceGraph.deleteNode(nodeToSolve);
    }
}
 
Example #30
Source File: Infer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public Node pickNode(InferenceGraph g) {
    if (g.nodes.isEmpty()) {
        //should not happen
        throw new NodeNotFoundException(g);
    };
    return g.nodes.get(0);
}