/** * Extends the TemplateTypeMap of the function's this type, based on the specified type. * * @param type */ public final void extendTemplateTypeMapBasedOn(ObjectType type) { typeOfThis.extendTemplateTypeMap(type.getTemplateTypeMap()); }
/** * Extends the TemplateTypeMap of the function's this type, based on the * specified type. * @param type */ public void extendTemplateTypeMapBasedOn(ObjectType type) { typeOfThis.extendTemplateTypeMap(type.getTemplateTypeMap()); }
private List<String> getTemplateTypeNames(ObjectType objType) { if (objType.getTemplateTypeMap() == null) { return Collections.emptyList(); } return objType .getTemplateTypeMap() .getTemplateKeys() .stream() .map(jsType -> jsType != null ? jsType.getDisplayName() : "") .collect(toCollection(ArrayList::new)); }
public final void setExtendedInterfaces(List<ObjectType> extendedInterfaces) { if (isInterface()) { this.extendedInterfaces = ImmutableList.copyOf(extendedInterfaces); for (ObjectType extendedInterface : this.extendedInterfaces) { typeOfThis.extendTemplateTypeMap(extendedInterface.getTemplateTypeMap()); } } else { throw new UnsupportedOperationException(); } }
public void setExtendedInterfaces(List<ObjectType> extendedInterfaces) throws UnsupportedOperationException { if (isInterface()) { this.extendedInterfaces = ImmutableList.copyOf(extendedInterfaces); for (ObjectType extendedInterface : this.extendedInterfaces) { typeOfThis.extendTemplateTypeMap( extendedInterface.getTemplateTypeMap()); } } else { throw new UnsupportedOperationException(); } }
public final void setImplementedInterfaces(List<ObjectType> implementedInterfaces) { if (isConstructor()) { // Records this type for each implemented interface. for (ObjectType type : implementedInterfaces) { registry.registerTypeImplementingInterface(this, type); typeOfThis.extendTemplateTypeMap(type.getTemplateTypeMap()); } this.implementedInterfaces = ImmutableList.copyOf(implementedInterfaces); } else { throw new UnsupportedOperationException("An interface cannot implement other inferfaces"); } }
public void setImplementedInterfaces(List<ObjectType> implementedInterfaces) { if (isConstructor()) { // Records this type for each implemented interface. for (ObjectType type : implementedInterfaces) { registry.registerTypeImplementingInterface(this, type); typeOfThis.extendTemplateTypeMap(type.getTemplateTypeMap()); } this.implementedInterfaces = ImmutableList.copyOf(implementedInterfaces); } else { throw new UnsupportedOperationException( "An interface cannot implement other inferfaces"); } }
/** * 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 map from TemplateType to corresponding JSType * value. Any unfilled TemplateTypes on the baseType that are *not* * contained in this map will have UNKNOWN_TYPE used as their value. */ public TemplatizedType createTemplatizedType( ObjectType baseType, Map<TemplateType, JSType> templatizedTypes) { ImmutableList.Builder<JSType> builder = ImmutableList.builder(); TemplateTypeMap baseTemplateTypeMap = baseType.getTemplateTypeMap(); for (TemplateType key : baseTemplateTypeMap.getUnfilledTemplateKeys()) { JSType templatizedType = templatizedTypes.containsKey(key) ? templatizedTypes.get(key) : getNativeType(UNKNOWN_TYPE); builder.add(templatizedType); } return createTemplatizedType(baseType, builder.build()); }
/** Returns a list of template types present on the constructor but not on the instance. */ public final ImmutableList<TemplateType> getConstructorOnlyTemplateParameters() { TemplateTypeMap ctorMap = getTemplateTypeMap(); TemplateTypeMap instanceMap = getInstanceType().getTemplateTypeMap(); if (ctorMap == instanceMap) { return ImmutableList.of(); } ImmutableList.Builder<TemplateType> ctorKeys = ImmutableList.builder(); Set<TemplateType> instanceKeys = ImmutableSet.copyOf(instanceMap.getUnfilledTemplateKeys()); for (TemplateType ctorKey : ctorMap.getUnfilledTemplateKeys()) { if (!instanceKeys.contains(ctorKey)) { ctorKeys.add(ctorKey); } } return ctorKeys.build(); }
/** * 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 map from TemplateType to corresponding JSType * value. Any unfilled TemplateTypes on the baseType that are *not* * contained in this map will have UNKNOWN_TYPE used as their value. */ public TemplatizedType createTemplatizedType( ObjectType baseType, Map<TemplateType, JSType> templatizedTypes) { ImmutableList.Builder<JSType> builder = ImmutableList.builder(); TemplateTypeMap baseTemplateTypeMap = baseType.getTemplateTypeMap(); for (TemplateType key : baseTemplateTypeMap.getUnfilledTemplateKeys()) { JSType templatizedType = templatizedTypes.containsKey(key) ? templatizedTypes.get(key) : getNativeType(UNKNOWN_TYPE); builder.add(templatizedType); } return createTemplatizedType(baseType, builder.build()); }
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()); }
public ObjectType instantiateGenericsWithUnknown(ObjectType obj) { if (obj.isTemplatizedType()) { ImmutableList.Builder<JSType> unknowns = ImmutableList.builder(); for (TemplateType unused : obj.getTemplateTypeMap().getTemplateKeys()) { unknowns.add(getNativeType(UNKNOWN_TYPE)); } return createTemplatizedType(obj.toMaybeTemplatizedType().getRawType(), unknowns.build()); } return obj; }
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 return an immutable list of template types of the given builtin. */ public ImmutableList<TemplateType> maybeGetTemplateTypesOfBuiltin(String fnName) { JSType type = getType(null, fnName); ObjectType objType = type == null ? null : type.toObjectType(); if (objType != null && objType.isNativeObjectType()) { ImmutableList<TemplateType> templateKeys = objType.getTemplateTypeMap().getUnfilledTemplateKeys(); return templateKeys; } return null; }
/** * Returns the type specified in a JSDoc annotation near a GETPROP, NAME, member function, or * object literal key. * * <p>Extracts type information from the {@code @type} tag. */ private JSType getDeclaredTypeInAnnotation(Node node, JSDocInfo info) { checkArgument(info.hasType()); ImmutableList<TemplateType> ownerTypeKeys = ImmutableList.of(); Node ownerNode = NodeUtil.getBestLValueOwner(node); String ownerName = NodeUtil.getBestLValueName(ownerNode); ObjectType ownerType = null; if (ownerName != null) { TypedVar ownerVar = currentScope.getVar(ownerName); if (ownerVar != null) { ownerType = getPrototypeOwnerType(ObjectType.cast(ownerVar.getType())); if (ownerType != null) { ownerTypeKeys = ownerType.getTemplateTypeMap().getTemplateKeys(); } } } StaticTypedScope templateScope = !ownerTypeKeys.isEmpty() ? typeRegistry.createScopeWithTemplates(currentScope, ownerTypeKeys) : currentScope; return info.getType().evaluate(templateScope, typeRegistry); }
public Void emitObjectType( ObjectType type, boolean extendingInstanceClass, boolean inExtendsImplementsPosition) { if (!type.getTemplateTypeMap().isEmpty() && !typeRegistry.getNativeType(OBJECT_TYPE).equals(type)) { return emitTemplatizedType(
/** * Check for structural equivalence with {@code that}. * (e.g. two @record types with the same prototype properties) */ final boolean checkStructuralEquivalenceHelper( ObjectType otherObject, EquivalenceMethod eqMethod, EqCache eqCache) { if (this.isTemplatizedType() && this.toMaybeTemplatizedType().wrapsSameRawType(otherObject)) { return this.getTemplateTypeMap().checkEquivalenceHelper( otherObject.getTemplateTypeMap(), eqMethod, eqCache, SubtypingMode.NORMAL); } MatchStatus result = eqCache.checkCache(this, otherObject); if (result != null) { return result.subtypeValue(); } Set<String> keySet = getPropertyNames(); Set<String> otherKeySet = otherObject.getPropertyNames(); if (!otherKeySet.equals(keySet)) { eqCache.updateCache(this, otherObject, MatchStatus.NOT_MATCH); return false; } for (String key : keySet) { if (!otherObject.getPropertyType(key).checkEquivalenceHelper( getPropertyType(key), eqMethod, eqCache)) { eqCache.updateCache(this, otherObject, MatchStatus.NOT_MATCH); return false; } } eqCache.updateCache(this, otherObject, MatchStatus.MATCH); return true; }
/** * Check for structural equivalence with {@code that}. * (e.g. two @record types with the same prototype properties) */ boolean checkStructuralEquivalenceHelper( ObjectType otherObject, EquivalenceMethod eqMethod, EqCache eqCache) { if (this.isTemplatizedType() && this.toMaybeTemplatizedType().wrapsSameRawType(otherObject)) { return this.getTemplateTypeMap().checkEquivalenceHelper( otherObject.getTemplateTypeMap(), eqMethod, eqCache); } MatchStatus result = eqCache.checkCache(this, otherObject); if (result != null) { return result.subtypeValue(); } Set<String> keySet = getPropertyNames(); Set<String> otherKeySet = otherObject.getPropertyNames(); if (!otherKeySet.equals(keySet)) { eqCache.updateCache(this, otherObject, MatchStatus.NOT_MATCH); return false; } for (String key : keySet) { if (!otherObject.getPropertyType(key).checkEquivalenceHelper( getPropertyType(key), eqMethod, eqCache)) { eqCache.updateCache(this, otherObject, MatchStatus.NOT_MATCH); return false; } } eqCache.updateCache(this, otherObject, MatchStatus.MATCH); return true; }
ObjectType dereferenced = objType.dereference(); if (dereferenced != null && dereferenced .getTemplateTypeMap() .hasTemplateKey(typeRegistry.getObjectIndexKey())) { expectCanAssignTo(t, indexNode, indexType, dereferenced .getTemplateTypeMap().getResolvedTemplateType(typeRegistry.getObjectIndexKey()), "restricted index type"); } else if (dereferenced != null && dereferenced.isArrayType()) {
ObjectType dereferenced = objType.dereference(); if (dereferenced != null && dereferenced .getTemplateTypeMap() .hasTemplateKey(typeRegistry.getObjectIndexKey())) { expectCanAssignTo(t, indexNode, indexType, dereferenced .getTemplateTypeMap().getResolvedTemplateType(typeRegistry.getObjectIndexKey()), "restricted index type"); } else if (dereferenced != null && dereferenced.isArrayType()) {