Search in sources :

Example 1 with ReferenceType

use of javax.lang.model.type.ReferenceType in project checker-framework by typetools.

the class CFGTranslationPhaseOne method commonConvert.

/**
 * Assignment conversion and method invocation conversion are almost identical, except that
 * assignment conversion allows narrowing. We factor out the common logic here.
 *
 * @param node a Node producing a value
 * @param varType the type of a variable
 * @param contextAllowsNarrowing whether to allow narrowing (for assignment conversion) or not
 *     (for method invocation conversion)
 * @return a Node with the value converted to the type of the variable, which may be the input
 *     node itself
 */
protected Node commonConvert(Node node, TypeMirror varType, boolean contextAllowsNarrowing) {
    // For assignment conversion, see JLS 5.2
    // For method invocation conversion, see JLS 5.3
    // Check for identical types or "identity conversion"
    TypeMirror nodeType = node.getType();
    boolean isSameType = types.isSameType(nodeType, varType);
    if (isSameType) {
        return node;
    }
    boolean isRightNumeric = TypesUtils.isNumeric(nodeType);
    boolean isRightPrimitive = TypesUtils.isPrimitive(nodeType);
    boolean isRightBoxed = TypesUtils.isBoxedPrimitive(nodeType);
    boolean isRightReference = nodeType instanceof ReferenceType;
    boolean isLeftNumeric = TypesUtils.isNumeric(varType);
    boolean isLeftPrimitive = TypesUtils.isPrimitive(varType);
    // boolean isLeftBoxed = TypesUtils.isBoxedPrimitive(varType);
    boolean isLeftReference = varType instanceof ReferenceType;
    boolean isSubtype = types.isSubtype(nodeType, varType);
    if (isRightNumeric && isLeftNumeric && isSubtype) {
        node = widen(node, varType);
        nodeType = node.getType();
    } else if (isRightReference && isLeftReference && isSubtype) {
    // widening reference conversion is a no-op, but if it
    // applies, then later conversions do not.
    } else if (isRightPrimitive && isLeftReference) {
        if (contextAllowsNarrowing && conversionRequiresNarrowing(varType, node)) {
            node = narrowAndBox(node, varType);
            nodeType = node.getType();
        } else {
            node = box(node);
            nodeType = node.getType();
        }
    } else if (isRightBoxed && isLeftPrimitive) {
        node = unbox(node);
        nodeType = node.getType();
        if (types.isSubtype(nodeType, varType) && !types.isSameType(nodeType, varType)) {
            node = widen(node, varType);
            nodeType = node.getType();
        }
    } else if (isRightPrimitive && isLeftPrimitive) {
        if (contextAllowsNarrowing && conversionRequiresNarrowing(varType, node)) {
            node = narrow(node, varType);
            nodeType = node.getType();
        }
    }
    return node;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ReferenceType(javax.lang.model.type.ReferenceType)

Aggregations

ReferenceType (javax.lang.model.type.ReferenceType)1 TypeMirror (javax.lang.model.type.TypeMirror)1