com.sun.tools.javac.code.Type.UndetVar Java Examples

The following examples show how to use com.sun.tools.javac.code.Type.UndetVar. 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 lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Check bounds and perform incorporation.
 */
void doIncorporation(InferenceContext inferenceContext, Warner warn) throws InferenceException {
    try {
        boolean progress = true;
        int round = 0;
        while (progress && round < MAX_INCORPORATION_STEPS) {
            progress = false;
            for (Type t : inferenceContext.undetvars) {
                UndetVar uv = (UndetVar)t;
                if (!uv.incorporationActions.isEmpty()) {
                    progress = true;
                    uv.incorporationActions.removeFirst().apply(inferenceContext, warn);
                }
            }
            round++;
        }
    } finally {
        incorporationCache.clear();
    }
}
 
Example #2
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
 * During overload resolution, instantiation is done by doing a partial
 * inference process using eq/lower bound instantiation. During check,
 * we also instantiate any remaining vars by repeatedly using eq/upper
 * instantiation, until all variables are solved.
 */
public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
    while (true) {
        List<Type> solvedVars = solveBasic(steps);
        if (restvars().isEmpty() || partial) {
            //all variables have been instantiated - exit
            break;
        } else if (solvedVars.isEmpty()) {
            //some variables could not be instantiated because of cycles in
            //upper bounds - provide a (possibly recursive) default instantiation
            infer.instantiateAsUninferredVars(restvars(), this);
            break;
        } else {
            //some variables have been instantiated - replace newly instantiated
            //variables in remaining upper bounds and continue
            for (Type t : undetvars) {
                UndetVar uv = (UndetVar)t;
                uv.substBounds(solvedVars, asInstTypes(solvedVars), types);
            }
        }
    }
    infer.doIncorporation(this, warn);
}
 
Example #3
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 #4
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
boolean isEquiv(UndetVar from, Type t, InferenceBound boundKind) {
    UndetVar uv = (UndetVar)asUndetVar(t);
    for (InferenceBound ib : InferenceBound.values()) {
        List<Type> b1 = from.getBounds(ib);
        if (ib == boundKind) {
            b1 = b1.diff(List.of(t));
        }
        List<Type> b2 = uv.getBounds(ib);
        if (ib == boundKind.complement()) {
            b2 = b2.diff(List.of(from.qtype));
        }
        if (!b1.containsAll(b2) || !b2.containsAll(b1)) {
            return false;
        }
    }
    return true;
}
 
Example #5
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
boolean isEquiv(UndetVar from, Type t, InferenceBound boundKind) {
    UndetVar uv = (UndetVar)asUndetVar(t);
    for (InferenceBound ib : InferenceBound.values()) {
        List<Type> b1 = from.getBounds(ib);
        if (ib == boundKind) {
            b1 = b1.diff(List.of(t));
        }
        List<Type> b2 = uv.getBounds(ib);
        if (ib == boundKind.complement()) {
            b2 = b2.diff(List.of(from.qtype));
        }
        if (!b1.containsAll(b2) || !b2.containsAll(b1)) {
            return false;
        }
    }
    return true;
}
 
Example #6
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitUndetVar(UndetVar t, Void _unused) {
    if (min.add(t.qtype)) {
        Set<Type> deps = minMap.getOrDefault(t.qtype, new HashSet<>(Collections.singleton(t.qtype)));
        for (InferenceBound boundKind : InferenceBound.values()) {
            for (Type b : t.getBounds(boundKind)) {
                Type undet = asUndetVar(b);
                if (!undet.hasTag(TypeTag.UNDETVAR)) {
                    visit(undet);
                } else if (isEquiv(t, b, boundKind)) {
                    deps.add(b);
                    equiv.add(b);
                } else {
                    visit(undet);
                }
            }
        }
        minMap.put(t.qtype, deps);
    }
    return null;
}
 
Example #7
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
 * During overload resolution, instantiation is done by doing a partial
 * inference process using eq/lower bound instantiation. During check,
 * we also instantiate any remaining vars by repeatedly using eq/upper
 * instantiation, until all variables are solved.
 */
public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
    while (true) {
        List<Type> solvedVars = solveBasic(steps);
        if (restvars().isEmpty() || partial) {
            //all variables have been instantiated - exit
            break;
        } else if (solvedVars.isEmpty()) {
            //some variables could not be instantiated because of cycles in
            //upper bounds - provide a (possibly recursive) default instantiation
            infer.instantiateAsUninferredVars(restvars(), this);
            break;
        } else {
            //some variables have been instantiated - replace newly instantiated
            //variables in remaining upper bounds and continue
            for (Type t : undetvars) {
                UndetVar uv = (UndetVar)t;
                uv.substBounds(solvedVars, asInstTypes(solvedVars), types);
            }
        }
    }
    infer.doIncorporation(this, warn);
}
 
Example #8
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Void visitUndetVar(UndetVar t, Void _unused) {
    if (min.add(t.qtype)) {
        Set<Type> deps = Maps.getOrDefault(minMap,t.qtype, new HashSet<>(Collections.singleton(t.qtype)));
        for (InferenceBound boundKind : InferenceBound.values()) {
            for (Type b : t.getBounds(boundKind)) {
                Type undet = asUndetVar(b);
                if (!undet.hasTag(TypeTag.UNDETVAR)) {
                    visit(undet);
                } else if (isEquiv(t, b, boundKind)) {
                    deps.add(b);
                    equiv.add(b);
                } else {
                    visit(undet);
                }
            }
        }
        minMap.put(t.qtype, deps);
    }
    return null;
}
 
Example #9
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** Restore the state of this inference context to the previous known checkpoint.
*  Consider that the number of saved undetermined variables can be different to the current
*  amount. This is because new captured variables could have been added.
*/
public void rollback(List<Type> saved_undet) {
    Assert.check(saved_undet != null);
    //restore bounds (note: we need to preserve the old instances)
    ListBuffer<Type> newUndetVars = new ListBuffer<>();
    ListBuffer<Type> newInferenceVars = new ListBuffer<>();
    while (saved_undet.nonEmpty() && undetvars.nonEmpty()) {
        UndetVar uv = (UndetVar)undetvars.head;
        UndetVar uv_saved = (UndetVar)saved_undet.head;
        if (uv.qtype == uv_saved.qtype) {
            uv_saved.dupTo(uv, types);
            undetvars = undetvars.tail;
            saved_undet = saved_undet.tail;
            newUndetVars.add(uv);
            newInferenceVars.add(uv.qtype);
        } else {
            undetvars = undetvars.tail;
        }
    }
    undetvars = newUndetVars.toList();
    inferencevars = newInferenceVars.toList();
}
 
Example #10
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Restore the state of this inference context to the previous known checkpoint.
*  Consider that the number of saved undetermined variables can be different to the current
*  amount. This is because new captured variables could have been added.
*/
public void rollback(List<Type> saved_undet) {
    Assert.check(saved_undet != null);
    //restore bounds (note: we need to preserve the old instances)
    ListBuffer<Type> newUndetVars = new ListBuffer<>();
    ListBuffer<Type> newInferenceVars = new ListBuffer<>();
    while (saved_undet.nonEmpty() && undetvars.nonEmpty()) {
        UndetVar uv = (UndetVar)undetvars.head;
        UndetVar uv_saved = (UndetVar)saved_undet.head;
        if (uv.qtype == uv_saved.qtype) {
            uv_saved.dupTo(uv, types);
            undetvars = undetvars.tail;
            saved_undet = saved_undet.tail;
            newUndetVars.add(uv);
            newInferenceVars.add(uv.qtype);
        } else {
            undetvars = undetvars.tail;
        }
    }
    undetvars = newUndetVars.toList();
    inferencevars = newInferenceVars.toList();
}
 
Example #11
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
List<Type> instTypes() {
    ListBuffer<Type> buf = new ListBuffer<>();
    for (Type t : undetvars) {
        UndetVar uv = (UndetVar)t;
        buf.append(uv.getInst() != null ? uv.getInst() : uv.qtype);
    }
    return buf.toList();
}
 
Example #12
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
List<Type> instTypes() {
    ListBuffer<Type> buf = new ListBuffer<>();
    for (Type t : undetvars) {
        UndetVar uv = (UndetVar)t;
        buf.append(uv.getInst() != null ? uv.getInst() : uv.qtype);
    }
    return buf.toList();
}
 
Example #13
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Save the state of this inference context
 */
public List<Type> save() {
    ListBuffer<Type> buf = new ListBuffer<>();
    for (Type t : undetvars) {
        buf.add(((UndetVar)t).dup(infer.types));
    }
    return buf.toList();
}
 
Example #14
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitTypeVar(TypeVar t, Void aVoid) {
    Type undet = asUndetVar(t);
    if (undet.hasTag(TypeTag.UNDETVAR)) {
        visitUndetVar((UndetVar)undet, null);
    }
    return null;
}
 
Example #15
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
List<Type> solveBasic(List<Type> varsToSolve, EnumSet<InferenceStep> steps) {
    ListBuffer<Type> solvedVars = new ListBuffer<>();
    for (Type t : varsToSolve.intersect(restvars())) {
        UndetVar uv = (UndetVar)asUndetVar(t);
        for (InferenceStep step : steps) {
            if (step.accepts(uv, this)) {
                uv.setInst(step.solve(uv, this));
                solvedVars.add(uv.qtype);
                break;
            }
        }
    }
    return solvedVars.toList();
}
 
Example #16
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Save the state of this inference context
 */
public List<Type> save() {
    ListBuffer<Type> buf = new ListBuffer<>();
    for (Type t : undetvars) {
        buf.add(((UndetVar)t).dup(infer.types));
    }
    return buf.toList();
}
 
Example #17
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
CheckBounds(UndetVar uv, Type t, BiFunction<InferenceContext, Type, Type> typeFunc,
            BiPredicate<InferenceContext, Type> typeFilter, InferenceBound from) {
    super(uv, t);
    this.from = from;
    this.typeFunc = typeFunc;
    this.optFilter = typeFilter;
}
 
Example #18
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Void visitTypeVar(TypeVar t, Void aVoid) {
    Type undet = asUndetVar(t);
    if (undet.hasTag(TypeTag.UNDETVAR)) {
        visitUndetVar((UndetVar)undet, null);
    }
    return null;
}
 
Example #19
Source File: TypeEqualityInInferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void checkEqualityBound(UndetVar uv, Type boundType) {
    com.sun.tools.javac.util.List<Type> equalBounds = uv.getBounds(InferenceBound.EQ);
    Assert.check(!equalBounds.isEmpty() && equalBounds.length() == 1,
            "undetVar must have only one equality bound");
    Type bound = equalBounds.head;
    Assert.check(bound == boundType, "equal bound must be of type " + boundType);
}
 
Example #20
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Incorporation error: mismatch between inferred type and given bound.
 */
void reportInstError(UndetVar uv, InferenceBound ib) {
    reportInferenceError(
            String.format("inferred.do.not.conform.to.%s.bounds", StringUtils.toLowerCase(ib.name())),
            uv.getInst(),
            uv.getBounds(ib));
}
 
Example #21
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Incorporation error: mismatch between two (or more) bounds of same kind.
 */
void reportBoundError(UndetVar uv, InferenceBound ib) {
    reportInferenceError(
            String.format("incompatible.%s.bounds", StringUtils.toLowerCase(ib.name())),
            uv.qtype,
            uv.getBounds(ib));
}
 
Example #22
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Incorporation error: mismatch between two (or more) bounds of different kinds.
 */
void reportBoundError(UndetVar uv, InferenceBound ib1, InferenceBound ib2) {
    reportInferenceError(
            String.format("incompatible.%s.%s.bounds",
                    StringUtils.toLowerCase(ib1.name()),
                    StringUtils.toLowerCase(ib2.name())),
            uv.qtype,
            uv.getBounds(ib1),
            uv.getBounds(ib2));
}
 
Example #23
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<Type> filterVars(Filter<UndetVar> fu) {
    ListBuffer<Type> res = new ListBuffer<>();
    for (Type t : undetvars) {
        UndetVar uv = (UndetVar)t;
        if (fu.accepts(uv)) {
            res.append(uv.qtype);
        }
    }
    return res.toList();
}
 
Example #24
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
Type solve(UndetVar uv, InferenceContext inferenceContext) {
    Infer infer = inferenceContext.infer;
    List<Type> lobounds = filterBounds(uv, inferenceContext);
    //note: lobounds should have at least one element
    Type owntype = lobounds.tail.tail == null  ? lobounds.head : infer.types.lub(lobounds);
    if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
        throw infer.inferenceException
            .setMessage("no.unique.minimal.instance.exists",
                        uv.qtype, lobounds);
    } else {
        return owntype;
    }
}
 
Example #25
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
    if (!t.isThrows()) {
        //not a throws undet var
        return false;
    }
    Types types = inferenceContext.types;
    Symtab syms = inferenceContext.infer.syms;
    return StreamSupport.stream(t.getBounds(InferenceBound.UPPER))
            .filter(b -> !inferenceContext.free(b))
            .allMatch(u -> types.isSubtype(syms.runtimeExceptionType, u));
}
 
Example #26
Source File: Printer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public String visitUndetVar(UndetVar t, Locale locale) {
    if (t.inst != null) {
        return visit(t.inst, locale);
    } else {
        return visit(t.qtype, locale) + "?";
    }
}
 
Example #27
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
Type solve(UndetVar uv, InferenceContext inferenceContext) {
    Infer infer = inferenceContext.infer;
    List<Type> hibounds = filterBounds(uv, inferenceContext);
    //note: hibounds should have at least one element
    Type owntype = hibounds.tail.tail == null  ? hibounds.head : infer.types.glb(hibounds);
    if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
        throw infer.inferenceException
            .setMessage("no.unique.maximal.instance.exists",
                        uv.qtype, hibounds);
    } else {
        return owntype;
    }
}
 
Example #28
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Debugging: dot representation of this graph
 */
String toDot() {
    StringBuilder buf = new StringBuilder();
    for (Type t : inferenceContext.undetvars) {
        UndetVar uv = (UndetVar)t;
        buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n",
                uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER),
                uv.getBounds(InferenceBound.EQ)));
    }
    return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString());
}
 
Example #29
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create the graph nodes. First a simple node is created for every inference
 * variables to be solved. Then Tarjan is used to found all connected components
 * in the graph. For each component containing more than one node, a super node is
 * created, effectively replacing the original cyclic nodes.
 */
void initNodes() {
    //add nodes
    nodes = new ArrayList<>();
    for (Type t : inferenceContext.restvars()) {
        nodes.add(new Node(t));
    }
    //add dependencies
    for (Node n_i : nodes) {
        Type i = n_i.data.first();
        for (Node n_j : nodes) {
            Type j = n_j.data.first();
            // don't compare a variable to itself
            if (i != j) {
                UndetVar uv_i = (UndetVar)inferenceContext.asUndetVar(i);
                if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) {
                    //update i's bound dependencies
                    n_i.addDependency(n_j);
                }
            }
        }
    }
    //merge cyclic nodes
    ArrayList<Node> acyclicNodes = new ArrayList<>();
    for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) {
        if (conSubGraph.length() > 1) {
            Node root = conSubGraph.head;
            root.mergeWith(conSubGraph.tail);
            for (Node n : conSubGraph) {
                notifyUpdate(n, root);
            }
        }
        acyclicNodes.add(conSubGraph.head);
    }
    nodes = acyclicNodes;
}
 
Example #30
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
Type solve(UndetVar uv, InferenceContext inferenceContext) {
    Infer infer = inferenceContext.infer;
    Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ?
            UPPER.solve(uv, inferenceContext) :
            infer.syms.objectType;
    Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ?
            LOWER.solve(uv, inferenceContext) :
            infer.syms.botType;
    CapturedType prevCaptured = (CapturedType)uv.qtype;
    return new CapturedType(prevCaptured.tsym.name, prevCaptured.tsym.owner,
                            upper, lower, prevCaptured.wildcard);
}