public JSType instantiateGenericType( ObjectType genericType, ImmutableList<JSType> typeArgs) { return createTemplatizedType(genericType, typeArgs); }
private JSType createTemplatizedType(ObjectType baseType, JSType[] params) { return typeRegistry.createTemplatizedType(baseType, params); }
/** * 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, JSType... templatizedTypes) { return createTemplatizedType(baseType, ImmutableList.copyOf(templatizedTypes)); }
/** * 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, JSType... templatizedTypes) { return createTemplatizedType( baseType, ImmutableList.copyOf(templatizedTypes)); }
/** * Returns an `Iterable` type templated on {@code elementType}. * * <p>Example: `number' => `Iterable<number>`. */ static final JSType createIterableTypeOf(JSType elementType, JSTypeRegistry typeRegistry) { return typeRegistry.createTemplatizedType( typeRegistry.getNativeObjectType(JSTypeNative.ITERABLE_TYPE), elementType); }
/** * 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()); }
/** * 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()); }
private JSType inferArrayPatternTargetType() { JSType patternType = patternTypeSupplier.get(); // e.g. get `number` from `!Iterable<number>` JSType templateTypeOfIterable = patternType.getInstantiatedTypeArgument(registry.getNativeType(JSTypeNative.ITERABLE_TYPE)); if (isRest) { // return `!Array<number>` return registry.createTemplatizedType( registry.getNativeObjectType(JSTypeNative.ARRAY_TYPE), templateTypeOfIterable); } else { return templateTypeOfIterable; } } }
/** * Wraps the given type in an IThenable. * * <p>If the given type is already IThenable it is first unwrapped. For example: * * <p>{@code number} becomes {@code IThenable<number>} * * <p>{@code IThenable<number>} becomes {@code IThenable<number>} * * <p>{@code Promise<number>} becomes {@code IThenable<number>} * * <p>{@code IThenable<number>|string} becomes {@code IThenable<number|string>} * * <p>{@code IThenable<number>|IThenable<string>} becomes {@code IThenable<number|string>} */ static final JSType wrapInIThenable(JSTypeRegistry registry, JSType maybeThenable) { // Unwrap for simplicity first in the event it is a thenable. JSType unwrapped = getResolvedType(registry, maybeThenable); return registry.createTemplatizedType( registry.getNativeObjectType(JSTypeNative.I_THENABLE_TYPE), unwrapped); }
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; }
@Override public JSType caseTemplatizedType(TemplatizedType type) { boolean changed = false; ObjectType beforeBaseType = type.getReferencedType(); ObjectType afterBaseType = ObjectType.cast(beforeBaseType.visit(this)); if (beforeBaseType != afterBaseType) { changed = true; } ImmutableList.Builder<JSType> builder = ImmutableList.builder(); for (JSType beforeTemplateType : type.getTemplateTypes()) { JSType afterTemplateType = beforeTemplateType.visit(this); if (beforeTemplateType != afterTemplateType) { changed = true; } builder.add(afterTemplateType); } if (changed) { type = registry.createTemplatizedType(afterBaseType, builder.build()); } return type; }
@Override public JSType caseTemplatizedType(TemplatizedType type) { boolean changed = false; ObjectType beforeBaseType = type.getReferencedType(); ObjectType afterBaseType = ObjectType.cast(beforeBaseType.visit(this)); if (beforeBaseType != afterBaseType) { changed = true; } ImmutableList.Builder<JSType> builder = ImmutableList.builder(); for (JSType beforeTemplateType : type.getTemplateTypes()) { JSType afterTemplateType = beforeTemplateType.visit(this); if (beforeTemplateType != afterTemplateType) { changed = true; } builder.add(afterTemplateType); } if (changed) { type = registry.createTemplatizedType(afterBaseType, builder.build()); } return type; }
ObjectType iThenableType = registry.getNativeObjectType(JSTypeNative.I_THENABLE_TYPE); JSType iThenableOfUnknownType = registry.createTemplatizedType(iThenableType, unknownType); asyncTemplateUnion, registry.createTemplatizedType(iThenableType, asyncTemplateUnion));
Node createArraylit(Node... elements) { Node result = IR.arraylit(elements); if (isAddingTypes()) { result.setJSType( registry.createTemplatizedType( registry.getNativeObjectType(JSTypeNative.ARRAY_TYPE), // TODO(nickreid): Use a reasonable template type. Remeber to consider SPREAD. getNativeType(JSTypeNative.UNKNOWN_TYPE))); } return result; }
TypeValidator(AbstractCompiler compiler) { this.compiler = compiler; this.typeRegistry = compiler.getTypeRegistry(); this.allBitwisableValueTypes = typeRegistry.createUnionType(STRING_TYPE, NUMBER_TYPE, BOOLEAN_TYPE, NULL_TYPE, VOID_TYPE); this.nullOrUndefined = typeRegistry.getNativeType(JSTypeNative.NULL_VOID); this.promiseOfUnknownType = typeRegistry.createTemplatizedType( typeRegistry.getNativeObjectType(JSTypeNative.PROMISE_TYPE), typeRegistry.getNativeType(JSTypeNative.UNKNOWN_TYPE)); this.iterableOrAsyncIterable = typeRegistry.createUnionType( typeRegistry.getNativeObjectType(JSTypeNative.ITERATOR_TYPE), typeRegistry.getNativeObjectType(JSTypeNative.ASYNC_ITERATOR_TYPE)); }
type = instanceType; } else { type = registry.createTemplatizedType(instanceType, inferredTypes);
typeRegistry.createTemplatizedType( generatorType, typeRegistry.getNativeType(UNKNOWN_TYPE)); return; returnType = typeRegistry.createTemplatizedType(promiseType, inferredReturnType); } else { returnType = inferredReturnType;
type = instanceType; } else { type = registry.createTemplatizedType(instanceType, inferredTypes);
result = registry.createTemplatizedType(result.toMaybeObjectType(), typeArgs); setReferencedType(result);
SingleGeneratorFunctionTranspiler(Node genFunc, int genaratorNestingLevel) { this.generatorNestingLevel = genaratorNestingLevel; this.originalGeneratorBody = genFunc.getLastChild(); ObjectType contextType = null; if (shouldAddTypes) { // Find the yield type of the generator. // e.g. given @return {!Generator<number>}, we want this.yieldType to be number. yieldType = unknownType; if (genFunc.getJSType() != null && genFunc.getJSType().isFunctionType()) { FunctionType fnType = genFunc.getJSType().toMaybeFunctionType(); this.originalGenReturnType = fnType.getReturnType(); yieldType = JsIterables.getElementType(originalGenReturnType, registry); } JSType globalContextType = registry.getGlobalType("$jscomp.generator.Context"); if (globalContextType == null) { // We don't have the es6/generator polyfill, which can happen in tests using a // NonInjectingCompiler or if someone sets --inject_libraries=false. Don't crash, just // back off on giving some type information. contextType = registry.getNativeObjectType(JSTypeNative.OBJECT_TYPE); } else { contextType = registry.createTemplatizedType(globalContextType.toMaybeObjectType(), yieldType); } } this.context = new TranspilationContext(contextType); }