@Override public TemplatizedType toMaybeTemplatizedType() { return referencedType.toMaybeTemplatizedType(); }
@Override public TemplatizedType toMaybeTemplatizedType() { return referencedType.toMaybeTemplatizedType(); }
public final boolean isTemplatizedType() { return toMaybeTemplatizedType() != null; }
public final boolean isTemplatizedType() { return toMaybeTemplatizedType() != null; }
private boolean isTemplateOf(JSType type, String typeName) { return type.isTemplatizedType() && type.toMaybeTemplatizedType().getReferenceName().equals(typeName); }
/** Returns an integer that uniquely identifies a JSType. */ private int getIntForType(JSType type) { // Templatized types don't exist at runtime, so collapse to raw type if (type != null && type.isTemplatizedType()) { type = type.toMaybeTemplatizedType().getReferencedType(); } if (intForType.containsKey(type)) { return intForType.get(type).intValue(); } int newInt = intForType.size() + 1; intForType.put(type, newInt); return newInt; }
private boolean isArray(JSType type) { if (type.isArrayType()) { return true; } TemplatizedType templatizedType = type.toMaybeTemplatizedType(); return templatizedType != null && templatizedType.getReferencedType().isArrayType(); }
private boolean isSubtype(JSType rightType, JSType leftType) { // if thisType or thatType is an unresolved templatized type, // then there is no structural interface matching boolean thisUnresolved = rightType.isTemplatizedType() && !rightType.toMaybeTemplatizedType().isResolved(); boolean thatUnresolved = leftType.isTemplatizedType() && !leftType.toMaybeTemplatizedType().isResolved(); if (structuralSubtypesAreCollapsed && !thisUnresolved && !thatUnresolved) { return rightType.isSubtypeOf(leftType); } else { return rightType.isSubtypeWithoutStructuralTyping(leftType); } }
private boolean isSubtype( JSType rightType, JSType leftType, boolean isStructural) { // if thisType or thatType is an unresolved templatized type, // then there is no structural interface matching boolean thisUnresolved = rightType.isTemplatizedType() && !rightType.toMaybeTemplatizedType().isResolved(); boolean thatUnresolved = leftType.isTemplatizedType() && !leftType.toMaybeTemplatizedType().isResolved(); if (isStructural && !thisUnresolved && !thatUnresolved) { return rightType.isSubtype(leftType); } else { return rightType.isSubtypeWithoutStructuralTyping(leftType); } }
/** * Determines if the specified type is exempt from standard invariant templatized typing rules. */ static boolean isIThenableSubtype(JSType type) { if (type.isTemplatizedType()) { TemplatizedType ttype = type.toMaybeTemplatizedType(); return ttype.getTemplateTypeMap().hasTemplateKey(ttype.registry.getThenableValueKey()); } return false; }
/** * Checks whether current type is Object type with non-stringifable key. */ private boolean isObjectTypeWithNonStringifiableKey(JSType type) { if (!type.isTemplatizedType()) { return false; } TemplatizedType templatizedType = type.toMaybeTemplatizedType(); if (templatizedType.getReferencedType().isNativeObjectType() && templatizedType.getTemplateTypes().size() > 1) { return !isReasonableObjectPropertyKey(templatizedType.getTemplateTypes().get(0)); } else { return false; } }
boolean wrapsSameRawType(JSType that) { return that.isTemplatizedType() && this.getReferencedTypeInternal() .isEquivalentTo( that.toMaybeTemplatizedType().getReferencedTypeInternal()); }
/** * Checks whether current type is Object type with non-stringifable key. */ private boolean isObjectTypeWithNonStringifiableKey(JSType type) { if (!type.isTemplatizedType()) { return false; } TemplatizedType templatizedType = type.toMaybeTemplatizedType(); if (templatizedType.getReferencedType().isNativeObjectType() && templatizedType.getTemplateTypes().size() > 1) { return !isStringifiable(templatizedType.getTemplateTypes().get(0)); } else { return false; } }
boolean wrapsSameRawType(JSType that) { return that.isTemplatizedType() && this.getReferencedTypeInternal() .isEquivalentTo( that.toMaybeTemplatizedType().getReferencedTypeInternal()); }
private static boolean areTypesEquivalentIgnoringGenerics(JSType a, JSType b) { boolean equivalent = a.isEquivalentTo(b); if (equivalent) { return true; } if (a.isTemplatizedType()) { return a.toMaybeTemplatizedType().getReferencedType().isEquivalentTo(b); } return false; }
/** * Returns whether the replacement type is a templatized type which contains the current type. * e.g. current type T is being replaced with Foo<T> */ private boolean isRecursive(TemplateType currentType, JSType replacementType) { TemplatizedType replacementTemplatizedType = replacementType.restrictByNotNullOrUndefined().toMaybeTemplatizedType(); if (replacementTemplatizedType == null) { return false; } Iterable<JSType> replacementTemplateTypes = replacementTemplatizedType.getTemplateTypes(); for (JSType replacementTemplateType : replacementTemplateTypes) { if (replacementTemplateType.isTemplateType() && isSameType(currentType, replacementTemplateType.toMaybeTemplateType())) { return true; } } return false; }
/** * Returns whether the replacement type is a templatized type which contains the current type. * e.g. current type T is being replaced with Foo<T> */ private boolean isRecursive(TemplateType currentType, JSType replacementType) { TemplatizedType replacementTemplatizedType = replacementType.restrictByNotNullOrUndefined().toMaybeTemplatizedType(); if (replacementTemplatizedType == null) { return false; } Iterable<JSType> replacementTemplateTypes = replacementTemplatizedType.getTemplateTypes(); for (JSType replacementTemplateType : replacementTemplateTypes) { if (replacementTemplateType.isTemplateType() && isSameType(currentType, replacementTemplateType.toMaybeTemplateType())) { return true; } } return false; }
/** * Computes the greatest subtype of two related templatized types. * @return The greatest subtype. */ JSType getGreatestSubtypeHelper(JSType rawThat) { checkNotNull(rawThat); if (!wrapsSameRawType(rawThat)) { if (!rawThat.isTemplatizedType()) { if (this.isSubtype(rawThat)) { return this; } else if (rawThat.isSubtypeOf(this)) { return filterNoResolvedType(rawThat); } } if (this.isObject() && rawThat.isObject()) { return this.getNativeType(JSTypeNative.NO_OBJECT_TYPE); } return this.getNativeType(JSTypeNative.NO_TYPE); } TemplatizedType that = rawThat.toMaybeTemplatizedType(); checkNotNull(that); if (getTemplateTypeMap().checkEquivalenceHelper( that.getTemplateTypeMap(), EquivalenceMethod.INVARIANT, SubtypingMode.NORMAL)) { return this; } // For types that have the same raw type but different type parameters, // we simply create a type has a "unknown" type parameter. This is // equivalent to the raw type. return getReferencedObjTypeInternal(); }
private boolean containsArray(JSType type) { // Check if type is itself an array if (type.isArrayType()) { return true; } TemplatizedType templatizedType = type.toMaybeTemplatizedType(); if (templatizedType != null && templatizedType.getReferencedType().isArrayType()) { return true; } // Check if this is a union that contains an array if (type.isUnionType()) { JSType arrayType = compiler.getTypeRegistry().getNativeType(JSTypeNative.ARRAY_TYPE); for (JSType alternate : type.toMaybeUnionType().getAlternates()) { if (alternate.isSubtypeOf(arrayType)) { return true; } } } return false; }
Boolean castCastToHelper(JSType thisType, JSType thatType) { if (thatType.isUnknownType() || thatType.isAllType() || thatType.isNoObjectType() // TODO(johnlenz): restrict to objects || thatType.isNoType()) { return true; } else if (thisType.isRecordType() || thatType.isRecordType()) { return true; // TODO(johnlenz): are there any misuses we can catch? } else if (isInterface(thisType) || isInterface(thatType)) { return true; // TODO(johnlenz): are there any misuses we can catch? } else if (thatType.isEnumElementType()) { return thisType.visit(this, thatType.toMaybeEnumElementType().getPrimitiveType()); } else if (thatType.isUnionType()) { return canCastToUnion(thisType, thatType.toMaybeUnionType()); } else if (thatType.isFunctionType()) { return canCastToFunction(thisType, thatType.toMaybeFunctionType()); } else if (thatType.isTemplatizedType()) { // TODO(johnlenz): once the templated type work is finished, // restrict the type parameters. return thisType.visit(this, thatType.toMaybeTemplatizedType().getReferencedTypeInternal()); } return thisType.isSubtypeOf(thatType) || thatType.isSubtypeOf(thisType); }