/** * 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; } }
/** * Creates a templatized instance of the specified type. Only ObjectTypes * can currently be templatized; extend the logic in this function when * more types can be templatized. * @param baseType the type to be templatized. * @param templatizedTypes a list of the template JSTypes. Will be matched by * list order to the template keys on the base type. */ public TemplatizedType createTemplatizedType( ObjectType baseType, ImmutableList<JSType> templatizedTypes) { // Only ObjectTypes can currently be templatized; extend this logic when // more types can be templatized. return new TemplatizedType(this, baseType, templatizedTypes); }
/** * 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 Void emitTemplatizedType(TemplatizedType type, boolean inImplementsExtendsPosition) { ObjectType referencedType = type.getReferencedType(); String templateTypeName = getAbsoluteName(type); if (typeRegistry.getNativeType(ARRAY_TYPE).equals(referencedType) && type.getTemplateTypes().size() == 1) { emit("Array<"); visitTypeAsPrimary(type.getTemplateTypes().get(0)); emit(inImplementsExtendsPosition ? ">" : "[]"); return null; final String displayName = type.getDisplayName(); if (PlatformSymbols.NOT_TEMPLATIZED_IN_TYPESCRIPT.contains(displayName)) { emit(PlatformSymbols.CLOSURE_TO_TYPESCRIPT.getOrDefault(displayName, displayName)); templateTypeName = maybeGlobalName == null ? templateTypeName : maybeGlobalName; if (type.getTemplateTypes().isEmpty()) { return null; Iterator<JSType> it = type.getTemplateTypes().iterator(); if (typeRegistry.getNativeType(OBJECT_TYPE).equals(referencedType)) { emit("{");
public final ObjectType getRawType() { TemplatizedType t = toMaybeTemplatizedType(); return t == null ? this : t.getReferencedType(); }
@Override public Set<String> caseTemplatizedType(TemplatizedType type) { // Visit the type arguments. // SomeType.<T1, T2> for (JSType templateType : type.getTemplateTypes()) { visitOnce(templateType); } return externProperties; }
TemplatizedType templatizedCurrent = current.toMaybeTemplatizedType(); if (templatizedCurrent.wrapsSameRawType(templatizedAlternate)) { if (alternate.getTemplateTypeMap().checkEquivalenceHelper( current.getTemplateTypeMap(), } else { ObjectType rawType = templatizedCurrent.getReferencedObjTypeInternal();
@Override public Boolean caseTemplatizedType( TemplatizedType thisType, JSType thatType) { // TODO(johnlenz): once the templated type work is finished, // restrict the type parameters. return thisType.getReferencedTypeInternal().visit(this, thatType); }
/** * @return The referenced ObjectType wrapped by this TemplatizedType. */ public ObjectType getReferencedType() { return getReferencedObjTypeInternal(); }
TemplatizedType( JSTypeRegistry registry, ObjectType objectType, ImmutableList<JSType> templateTypes) { super(registry, objectType, objectType.getTemplateTypeMap().addValues( templateTypes)); // Cache which template keys were filled, and what JSTypes they were filled // with. ImmutableList<TemplateType> filledTemplateKeys = objectType.getTemplateTypeMap().getUnfilledTemplateKeys(); ImmutableList.Builder<JSType> builder = ImmutableList.builder(); for (TemplateType filledTemplateKey : filledTemplateKeys) { builder.add(getTemplateTypeMap().getResolvedTemplateType(filledTemplateKey)); } this.templateTypes = builder.build(); replacer = new TemplateTypeMapReplacer(registry, getTemplateTypeMap()); }
return thatType.toMaybeUnionType().meet(thisType); } else if (thisType.isTemplatizedType()) { return thisType.toMaybeTemplatizedType().getGreatestSubtypeHelper( thatType); } else if (thatType.isTemplatizedType()) { return thatType.toMaybeTemplatizedType().getGreatestSubtypeHelper( thisType); } else if (thisType.isSubtypeOf(thatType)) {
/** 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 JSType evalTemplateTypeOf(Node ttlAst, NameResolver nameResolver) { ImmutableList<Node> params = getCallParams(ttlAst); JSType type = evalInternal(params.get(0), nameResolver); if (!type.isTemplatizedType()) { reportWarning(ttlAst, TEMPTYPE_INVALID, "templateTypeOf", type.toString()); return getUnknownType(); } int index = (int) params.get(1).getDouble(); ImmutableList<JSType> templateTypes = ((TemplatizedType) type).getTemplateTypes(); if (index > templateTypes.size()) { reportWarning(ttlAst, INDEX_OUTOFBOUNDS, Integer.toString(index), Integer.toString(templateTypes.size())); return getUnknownType(); } return templateTypes.get(index); }
TemplatizedType templatizedCurrent = current.toMaybeTemplatizedType(); if (templatizedCurrent.wrapsSameRawType(templatizedAlternate)) { if (alternate.getTemplateTypeMap().checkEquivalenceHelper( current.getTemplateTypeMap(), .getReferencedObjTypeInternal(); return addAlternate(merged);
@Override public Boolean caseTemplatizedType( TemplatizedType thisType, JSType thatType) { // TODO(johnlenz): once the templated type work is finished, // restrict the type parameters. return thisType.getReferencedTypeInternal().visit(this, thatType); }
/** * @return The referenced ObjectType wrapped by this TemplatizedType. */ public ObjectType getReferencedType() { return getReferencedObjTypeInternal(); } }
TemplatizedType( JSTypeRegistry registry, ObjectType objectType, ImmutableList<JSType> templateTypes) { super( registry, objectType, objectType.getTemplateTypeMap().copyFilledWithValues(templateTypes)); ImmutableList.Builder<JSType> builder = ImmutableList.builder(); boolean maybeIsSpecializedOnlyWithUnknown = true; for (TemplateType newlyFilledTemplateKey : objectType.getTemplateTypeMap().getUnfilledTemplateKeys()) { JSType resolvedType = getTemplateTypeMap().getResolvedTemplateType(newlyFilledTemplateKey); builder.add(resolvedType); maybeIsSpecializedOnlyWithUnknown = maybeIsSpecializedOnlyWithUnknown && resolvedType.isUnknownType(); } this.templateTypes = builder.build(); this.isSpecializedOnlyWithUnknown = maybeIsSpecializedOnlyWithUnknown; this.replacer = new TemplateTypeMapReplacer(registry, getTemplateTypeMap()); }
return thatType.toMaybeUnionType().meet(thisType); } else if (thisType.isTemplatizedType()) { return thisType.toMaybeTemplatizedType().getGreatestSubtypeHelper( thatType); } else if (thatType.isTemplatizedType()) { return thatType.toMaybeTemplatizedType().getGreatestSubtypeHelper( thisType); } else if (thisType.isSubtype(thatType)) {
/** * Computes the greatest subtype of two related templatized types. * @return The greatest subtype. */ JSType getGreatestSubtypeHelper(JSType rawThat) { Preconditions.checkNotNull(rawThat); if (!wrapsSameRawType(rawThat)) { if (!rawThat.isTemplatizedType()) { if (this.isSubtype(rawThat)) { return this; } else if (rawThat.isSubtype(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(); Preconditions.checkNotNull(that); if (getTemplateTypeMap().checkEquivalenceHelper( that.getTemplateTypeMap(), EquivalenceMethod.INVARIANT)) { 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(); }
/** * 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; } }