/** * Returns the JSType as specified by the typeName. * Returns null if shouldCreate is false. */ static JSType createType(boolean shouldCreate, JSTypeRegistry registry, JSTypeNative typeName) { if (!shouldCreate) { return null; } return registry.getNativeType(typeName); }
/** * Utility method for less verbose code. */ JSType getNativeType(JSTypeNative typeId) { return registry.getNativeType(typeId); }
@Override public final boolean isInstanceType() { // The universal constructor is its own instance, bizarrely. It overrides // getConstructor() appropriately when it's declared. return this == registry.getNativeType(U2U_CONSTRUCTOR_TYPE); }
Builder addTypesInvalidForPropertyRenaming() { types.addAll( ImmutableList.of( registry.getNativeType(JSTypeNative.FUNCTION_FUNCTION_TYPE), registry.getNativeType(JSTypeNative.FUNCTION_INSTANCE_TYPE), registry.getNativeType(JSTypeNative.FUNCTION_PROTOTYPE), registry.getNativeType(JSTypeNative.OBJECT_TYPE), registry.getNativeType(JSTypeNative.OBJECT_PROTOTYPE), registry.getNativeType(JSTypeNative.OBJECT_FUNCTION_TYPE), registry.getNativeType(JSTypeNative.TOP_LEVEL_PROTOTYPE))); return this; }
Builder disallowGlobalThis() { // Disambiguate does not invalidate global this because it // sets skipping explicitly for extern properties only on // the extern types. types.add(registry.getNativeType(JSTypeNative.GLOBAL_THIS)); return this; }
private JSType getNativeType(JSTypeNative nativeType) { checkNotNull(registry, "registry is null"); return checkNotNull( registry.getNativeType(nativeType), "native type not found: %s", nativeType); }
private JSType getNativeStringType() { if (nativeStringType == null) { nativeStringType = compiler.getTypeRegistry().getNativeType(STRING_TYPE); } return nativeStringType; }
/** * Creates a type representing nullable values of the given type. * @return the union of the type and the Null type */ public JSType createNullableType(JSType type) { return createUnionType(type, getNativeType(JSTypeNative.NULL_TYPE)); }
private JSType getJSTypeOrUnknown(Node n) { JSType jsType = n.getJSType(); if (jsType == null) { return getNativeType(UNKNOWN_TYPE); } else { return jsType; } }
JSType getUnresolvedOriginalTemplateType(TemplateType key) { int index = getTemplateTypeIndex(key); return (index == -1) ? registry.getNativeType(JSTypeNative.UNKNOWN_TYPE) : templateValues.get(index); }
ImmutableList<JSType> getAlternates() { JSType specialCaseType = reduceAlternatesWithoutUnion(); if (specialCaseType != null) { return ImmutableList.of(specialCaseType); } JSType wildcard = getNativeWildcardType(); if (wildcard != null && containsVoidType) { return ImmutableList.of(wildcard, registry.getNativeType(VOID_TYPE)); } // This copy should be pretty cheap since in the common case alternates only contains 2-3 items return ImmutableList.copyOf(alternates); }
/** This method gets the JSType from the Node argument and verifies that it is present. */ private JSType getJSType(Node n) { JSType type = n.getJSType(); if (type == null) { return compiler.getTypeRegistry().getNativeType(JSTypeNative.UNKNOWN_TYPE); } else { return type; } }
/** * Creates a type representing optional values of the given type. * @return the union of the type and the void type */ public JSType createOptionalType(JSType type) { if (type instanceof UnknownType || type.isAllType()) { return type; } else { return createUnionType(type, getNativeType(JSTypeNative.VOID_TYPE)); } }
private JSType getType(Node node) { if (node == null || node.getJSType() == null) { return registry.getNativeType(JSTypeNative.UNKNOWN_TYPE); } return node.getJSType(); }
/** * @return {@code true} if returnType is void, unknown, or a union * containing void or unknown */ private boolean isVoidOrUnknown(JSType returnType) { final JSType voidType = compiler.getTypeRegistry().getNativeType(JSTypeNative.VOID_TYPE); return voidType.isSubtypeOf(returnType); } }
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; }
public Es6RewriteClass(AbstractCompiler compiler) { this.compiler = compiler; this.registry = compiler.getTypeRegistry(); this.astFactory = compiler.createAstFactory(); // Finds the type for `ObjectPropertyDescriptor`. Fallback to the unknown type if it's not // present, which may happen if typechecking hasn't run or this is a unit test w/o externs. JSType actualObjectPropertyDescriptorType = registry.getGlobalType("ObjectPropertyDescriptor"); this.objectPropertyDescriptorType = actualObjectPropertyDescriptorType != null ? actualObjectPropertyDescriptorType : registry.getNativeType(JSTypeNative.UNKNOWN_TYPE); }
public AbstractTypeRestrictionRule(AbstractCompiler compiler, Requirement requirement) throws InvalidRequirementSpec { super(compiler, requirement); nativeObjectType = compiler.getTypeRegistry().getNativeType(JSTypeNative.OBJECT_TYPE); List<String> whitelistedTypeNames = requirement.getValueList(); whitelistedTypes = union(whitelistedTypeNames); ImmutableList.Builder<Node> builder = ImmutableList.builder(); for (AssertionFunctionSpec fn : compiler.getCodingConvention().getAssertionFunctions()) { builder.add(NodeUtil.newQName(compiler, fn.getFunctionName())); } assertionsFunctionNames = builder.build(); }
/** * Returns a call to $jscomp.arrayFromIterable with {@code iterable} as its argument. */ static Node arrayFromIterable(AbstractCompiler compiler, Node iterable) { JSTypeRegistry registry = compiler.getTypeRegistry(); JSType arrayType = registry.getNativeType(JSTypeNative.ARRAY_TYPE); Node call = callEs6RuntimeFunction(compiler, iterable, "arrayFromIterable").setJSType(arrayType); call.getFirstChild().setJSType(registry.createFunctionTypeWithVarArgs(arrayType)); return call; }
private void addExtern(String export) { Node objectPrototype = NodeUtil.newQName(compiler, "Object.prototype"); JSType objCtor = compiler.getTypeRegistry().getNativeType(JSTypeNative.OBJECT_FUNCTION_TYPE); objectPrototype.getFirstChild().setJSType(objCtor); Node propstmt = IR.exprResult(IR.getprop(objectPrototype, IR.string(export))); propstmt.useSourceInfoFromForTree(getSynthesizedExternsRoot()); propstmt.setOriginalName(export); getSynthesizedExternsRoot().addChildToBack(propstmt); compiler.reportChangeToEnclosingScope(propstmt); }