JSType(JSTypeRegistry registry, TemplateTypeMap templateTypeMap) { this.registry = registry; this.templateTypeMap = templateTypeMap == null ? registry.createTemplateTypeMap(null, null) : templateTypeMap; }
/** Set the template name. */ public FunctionBuilder withTemplateKeys(ImmutableList<TemplateType> templateKeys) { this.templateTypeMap = registry.createTemplateTypeMap(templateKeys, null); return this; }
/** Set the template name. */ public FunctionBuilder withTemplateKeys( ImmutableList<TemplateType> templateKeys) { this.templateTypeMap = registry.createTemplateTypeMap(templateKeys, null); return this; }
JSType(JSTypeRegistry registry, TemplateTypeMap templateTypeMap) { this.registry = registry; this.templateTypeMap = templateTypeMap == null ? registry.createTemplateTypeMap(null, null) : templateTypeMap; }
/** Set the template name. */ public FunctionBuilder withTemplateKeys(TemplateType... templateKeys) { this.templateTypeMap = registry.createTemplateTypeMap(ImmutableList.copyOf(templateKeys), null); return this; }
FunctionBuilder withExtendedTemplate(TemplateType key, JSType value) { this.templateTypeMap = templateTypeMap.extend( registry.createTemplateTypeMap(ImmutableList.of(key), ImmutableList.of(value))); return this; }
/** * Returns a new TemplateTypeMap with the given template types removed. Keys will only be removed * if they are unmapped. */ TemplateTypeMap remove(Set<TemplateType> toRemove) { ImmutableList.Builder<TemplateType> keys = ImmutableList.builder(); keys.addAll(templateKeys.subList(0, templateValues.size())); for (int i = templateValues.size(); i < templateKeys.size(); i++) { TemplateType key = templateKeys.get(i); if (!toRemove.contains(key)) { keys.add(key); } } return registry.createTemplateTypeMap(keys.build(), templateValues); }
/** Returns a new TemplateTypeMap whose values have been extended with the specified list. */ TemplateTypeMap copyFilledWithValues(ImmutableList<JSType> additionalValues) { ImmutableList<JSType> finalValues = resizedToMatchKeys(concatImmutableLists(templateValues, additionalValues)); return registry.createTemplateTypeMap(templateKeys, finalValues); }
/** * Returns a new TemplateTypeMap whose values have been extended with the * specified list. */ TemplateTypeMap addValues(ImmutableList<JSType> newValues) { // Ignore any new template values that will not align with an existing // template key. int numUnfilledKeys = numUnfilledTemplateKeys(); if (numUnfilledKeys < newValues.size()) { newValues = newValues.subList(0, numUnfilledKeys); } return registry.createTemplateTypeMap( templateKeys, concatImmutableLists(templateValues, newValues)); }
/** * Creates an interface function type. * @param name the function's name * @param source the node defining this function. Its type * ({@link Node#getType()}) must be {@link Token#FUNCTION}. * @param templateKeys the templatized types for the interface. */ public FunctionType createInterfaceType(String name, Node source, ImmutableList<TemplateType> templateKeys, boolean struct) { FunctionType fn = FunctionType.forInterface(this, name, source, createTemplateTypeMap(templateKeys, null)); if (struct) { fn.setStruct(); } return fn; }
/** * Extends this TemplateTypeMap with the contents of the specified map. UNKNOWN_TYPE will be used * as the value for any missing values in the specified map. */ TemplateTypeMap extend(TemplateTypeMap other) { ImmutableList<JSType> resizedOtherValues = other.resizedToMatchKeys(other.templateValues); return registry.createTemplateTypeMap( concatImmutableLists(other.templateKeys, templateKeys), concatImmutableLists(resizedOtherValues, templateValues)); }
/** * Extends this TemplateTypeMap with the contents of the specified map. * UNKNOWN_TYPE will be used as the value for any missing values in the * specified map. */ TemplateTypeMap extend(TemplateTypeMap thatMap) { thatMap = thatMap.addUnknownValues(); return registry.createTemplateTypeMap( concatImmutableLists(thatMap.templateKeys, templateKeys), concatImmutableLists(thatMap.templateValues, templateValues)); }
/** * Creates a constructor function type. * @param name the function's name or {@code null} to indicate that the * function is anonymous. * @param source the node defining this function. Its type * ({@link Node#getType()}) must be {@link Token#FUNCTION}. * @param parameters the function's parameters or {@code null} * to indicate that the parameter types are unknown. * @param returnType the function's return type or {@code null} to indicate * that the return type is unknown. * @param templateKeys the templatized types for the class. */ public FunctionType createConstructorType(String name, Node source, Node parameters, JSType returnType, ImmutableList<TemplateType> templateKeys) { Preconditions.checkArgument(source == null || source.isFunction()); return new FunctionType(this, name, source, createArrowType(parameters, returnType), null, createTemplateTypeMap(templateKeys, null), true, false); }
Node createJSCompMakeIteratorCall(Node iterable, Scope scope) { String function = "makeIterator"; Node makeIteratorName = createQName(scope, "$jscomp." + function); // Since createCall (currently) doesn't handle templated functions, fill in the template types // of makeIteratorName manually. if (isAddingTypes() && !makeIteratorName.getJSType().isUnknownType()) { // if makeIteratorName has the unknown type, we must have not injected the required runtime // libraries - hopefully because this is in a test using NonInjectingCompiler. // e.g get `number` from `Iterable<number>` JSType iterableType = iterable .getJSType() .getInstantiatedTypeArgument(getNativeType(JSTypeNative.ITERABLE_TYPE)); JSType makeIteratorType = makeIteratorName.getJSType(); // e.g. replace // function(Iterable<T>): Iterator<T> // with // function(Iterable<number>): Iterator<number> TemplateTypeMap typeMap = registry.createTemplateTypeMap( makeIteratorType.getTemplateTypeMap().getTemplateKeys(), ImmutableList.of(iterableType)); TemplateTypeMapReplacer replacer = new TemplateTypeMapReplacer(registry, typeMap); makeIteratorName.setJSType(makeIteratorType.visit(replacer)); } return createCall(makeIteratorName, iterable); }
Node createJscompArrayFromIteratorCall(Node iterator, Scope scope) { String function = "arrayFromIterator"; Node makeIteratorName = createQName(scope, "$jscomp." + function); // Since createCall (currently) doesn't handle templated functions, fill in the template types // of makeIteratorName manually. if (isAddingTypes() && !makeIteratorName.getJSType().isUnknownType()) { // if makeIteratorName has the unknown type, we must have not injected the required runtime // libraries - hopefully because this is in a test using NonInjectingCompiler. // e.g get `number` from `Iterator<number>` JSType iterableType = iterator .getJSType() .getInstantiatedTypeArgument(getNativeType(JSTypeNative.ITERATOR_TYPE)); JSType makeIteratorType = makeIteratorName.getJSType(); // e.g. replace // function(Iterator<T>): Array<T> // with // function(Iterator<number>): Array<number> TemplateTypeMap typeMap = registry.createTemplateTypeMap( makeIteratorType.getTemplateTypeMap().getTemplateKeys(), ImmutableList.of(iterableType)); TemplateTypeMapReplacer replacer = new TemplateTypeMapReplacer(registry, typeMap); makeIteratorName.setJSType(makeIteratorType.visit(replacer)); } return createCall(makeIteratorName, iterator); }
createArrowType(createOptionalParameters(ALL_TYPE), null), null, createTemplateTypeMap(ImmutableList.of( iObjectIndexTemplateKey, iObjectElementTemplateKey), null), true, true); createArrowType(createParametersWithVarArgs(ALL_TYPE), null), null, createTemplateTypeMap(ImmutableList.of(arrayElementTemplateKey), null) .extend(createTemplateTypeMap( ImmutableList.of(iObjectElementTemplateKey), ImmutableList.<JSType>of(arrayElementTemplateKey))),