Search in sources :

Example 6 with FunctionTypeI

use of com.google.javascript.rhino.FunctionTypeI in project closure-compiler by google.

the class DisambiguateProperties method recordInterfaces.

/**
 * Records that this property could be referenced from any interface that
 * this type inherits from.
 *
 * If the property p is defined only on a subtype of constructor, then this
 * method has no effect. But we tried modifying getTypeWithProperty to tell us
 * when the returned type is a subtype, and then skip those calls to
 * recordInterface, and there was no speed-up.
 * And it made the code harder to understand, so we don't do it.
 */
private void recordInterfaces(FunctionTypeI constructor, TypeI relatedType, Property p) {
    Iterable<ObjectTypeI> interfaces = ancestorInterfaces.get(constructor);
    if (interfaces == null) {
        interfaces = constructor.getAncestorInterfaces();
        ancestorInterfaces.put(constructor, interfaces);
    }
    for (ObjectTypeI itype : interfaces) {
        TypeI top = getTypeWithProperty(p.name, itype);
        if (top != null) {
            p.addType(itype, relatedType);
        }
        // If this interface invalidated this property, return now.
        if (p.skipRenaming) {
            return;
        }
    }
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

Example 7 with FunctionTypeI

use of com.google.javascript.rhino.FunctionTypeI in project closure-compiler by google.

the class DevirtualizePrototypeMethods method fixFunctionType.

/**
 * Creates a new type based on the original function type by
 * adding the original this pointer type to the beginning of the
 * argument type list and replacing the this pointer type with bottom.
 */
private void fixFunctionType(Node functionNode) {
    TypeI t = functionNode.getTypeI();
    if (t == null) {
        return;
    }
    FunctionTypeI ft = t.toMaybeFunctionType();
    if (ft != null) {
        functionNode.setTypeI(ft.convertMethodToFunction());
    }
}
Also used : TypeI(com.google.javascript.rhino.TypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 8 with FunctionTypeI

use of com.google.javascript.rhino.FunctionTypeI in project closure-compiler by google.

the class DisambiguateProperties method getTypeAlternatives.

/**
 * Returns the alternatives if this is a type that represents multiple
 * types, and null if not. Union and interface types can correspond to
 * multiple other types.
 */
private Iterable<? extends TypeI> getTypeAlternatives(TypeI type) {
    if (type.isUnionType()) {
        return type.getUnionMembers();
    } else {
        ObjectTypeI objType = type.toMaybeObjectType();
        FunctionTypeI constructor = objType != null ? objType.getConstructor() : null;
        if (constructor != null && constructor.isInterface()) {
            List<TypeI> list = new ArrayList<>();
            for (FunctionTypeI impl : constructor.getDirectSubTypes()) {
                list.add(impl.getInstanceType());
            }
            return list.isEmpty() ? null : list;
        } else {
            return null;
        }
    }
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) ArrayList(java.util.ArrayList) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 9 with FunctionTypeI

use of com.google.javascript.rhino.FunctionTypeI in project closure-compiler by google.

the class DisambiguateProperties method getConstructor.

private FunctionTypeI getConstructor(TypeI type) {
    ObjectTypeI objType = type.toMaybeObjectType();
    if (objType == null) {
        return null;
    }
    FunctionTypeI constructor = null;
    if (objType.isFunctionType()) {
        constructor = objType.toMaybeFunctionType();
    } else if (objType.isPrototypeObject()) {
        constructor = objType.getOwnerFunction();
    } else {
        constructor = objType.getConstructor();
    }
    return constructor;
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 10 with FunctionTypeI

use of com.google.javascript.rhino.FunctionTypeI in project closure-compiler by google.

the class TypeMismatch method registerMismatch.

static void registerMismatch(List<TypeMismatch> mismatches, List<TypeMismatch> implicitInterfaceUses, TypeI found, TypeI required, JSError error) {
    // Don't register a mismatch for differences in null or undefined or if the
    // code didn't downcast.
    found = removeNullUndefinedAndTemplates(found);
    required = removeNullUndefinedAndTemplates(required);
    if (found.isSubtypeOf(required) || required.isSubtypeOf(found)) {
        boolean strictMismatch = !found.isSubtypeWithoutStructuralTyping(required) && !required.isSubtypeWithoutStructuralTyping(found);
        if (strictMismatch && bothAreNotTypeVariables(found, required)) {
            implicitInterfaceUses.add(new TypeMismatch(found, required, Suppliers.ofInstance(error)));
        }
        return;
    }
    if (bothAreNotTypeVariables(found, required)) {
        mismatches.add(new TypeMismatch(found, required, Suppliers.ofInstance(error)));
    }
    if (found.isFunctionType() && required.isFunctionType()) {
        FunctionTypeI fnTypeA = found.toMaybeFunctionType();
        FunctionTypeI fnTypeB = required.toMaybeFunctionType();
        Iterator<TypeI> paramItA = fnTypeA.getParameterTypes().iterator();
        Iterator<TypeI> paramItB = fnTypeB.getParameterTypes().iterator();
        while (paramItA.hasNext() && paramItB.hasNext()) {
            TypeMismatch.registerIfMismatch(mismatches, implicitInterfaceUses, paramItA.next(), paramItB.next(), error);
        }
        TypeMismatch.registerIfMismatch(mismatches, implicitInterfaceUses, fnTypeA.getReturnType(), fnTypeB.getReturnType(), error);
    }
}
Also used : TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Aggregations

FunctionTypeI (com.google.javascript.rhino.FunctionTypeI)15 TypeI (com.google.javascript.rhino.TypeI)10 ObjectTypeI (com.google.javascript.rhino.ObjectTypeI)9 Node (com.google.javascript.rhino.Node)3 JSDocInfo (com.google.javascript.rhino.JSDocInfo)2 Function (com.google.common.base.Function)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 ObjectType (com.google.javascript.rhino.jstype.ObjectType)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 Nullable (javax.annotation.Nullable)1