private SymbolScope getScopeInFunction(Symbol sym) { FunctionType type = sym.getFunctionType(); if (type == null) { return null; } Node functionNode = type.getSource(); if (functionNode == null) { return null; } return scopes.get(functionNode); }
@Override public void applySingletonGetterOld(FunctionType functionType, FunctionType getterType, ObjectType objectType) { functionType.defineDeclaredProperty("getInstance", getterType, functionType.getSource()); functionType.defineDeclaredProperty("instance_", objectType, functionType.getSource()); }
private SymbolScope getScopeInFunction(Symbol sym) { FunctionType type = sym.getFunctionType(); if (type == null) { return null; } Node functionNode = type.getSource(); if (functionNode == null) { return null; } return scopes.get(functionNode); }
@Override StringBuilder appendTo(StringBuilder sb, boolean forAnnotations) { if (!constructor.hasReferenceName()) { return super.appendTo(sb, forAnnotations); } else if (forAnnotations) { return sb.append(constructor.getNormalizedReferenceName()); } String name = constructor.getReferenceName(); if (name.isEmpty()) { Node n = constructor.getSource(); return sb.append("<anonymous@") .append(n != null ? n.getSourceFileName() : "unknown") .append(":") .append(n != null ? n.getLineno() : 0) .append(">"); } return sb.append(name); }
private String getSourceName(JSType jsType) { FunctionType constructor = jsType.toMaybeObjectType().getConstructor(); if (constructor == null) { return ""; } return NodeUtil.getSourceName(constructor.getSource()); }
/** See the comment above on shouldAvoidGeneratingExterns. */ private boolean shouldAvoidGeneratingExterns(ObjectType type) { if (type.getConstructor() == null || type.getConstructor().getSource() == null) return false; return shouldAvoidGeneratingExterns( type.getConstructor().getSource().getSourceFileName(), type.getDisplayName()); }
/** Gets the symbol for the given constructor or interface. */ public Symbol getSymbolDeclaredBy(FunctionType fn) { checkState(fn.isConstructor() || fn.isInterface()); ObjectType instanceType = fn.getInstanceType(); return getSymbolForName(fn.getSource(), instanceType.getReferenceName()); }
/** Gets the symbol for the prototype of the given constructor or interface. */ public Symbol getSymbolForInstancesOf(FunctionType fn) { checkState(fn.isConstructor() || fn.isInterface()); ObjectType pType = fn.getPrototype(); return getSymbolForName(fn.getSource(), pType.getReferenceName()); }
/** * Gets the symbol for the given constructor or interface. */ public Symbol getSymbolDeclaredBy(FunctionType fn) { Preconditions.checkState(fn.isConstructor() || fn.isInterface()); ObjectType instanceType = fn.getInstanceType(); return getSymbolForName(fn.getSource(), instanceType.getReferenceName()); }
/** * Gets the symbol for the prototype of the given constructor or interface. */ public Symbol getSymbolForInstancesOf(FunctionType fn) { Preconditions.checkState(fn.isConstructor() || fn.isInterface()); ObjectType pType = fn.getPrototype(); return getSymbolForName(fn.getSource(), pType.getReferenceName()); }
@Override public void applySingletonGetter(NominalTypeBuilder classType, FunctionType getterType) { Node defSite = classType.constructor().getSource(); classType.declareConstructorProperty("getInstance", getterType, defSite); classType.declareConstructorProperty("instance_", classType.instance(), defSite); }
@Override public void applySingletonGetter(NominalTypeBuilder classType, FunctionType getterType) { Node defSite = classType.constructor().getSource(); classType.declareConstructorProperty("getInstance", getterType, defSite); classType.declareConstructorProperty("instance_", classType.instance(), defSite); }
/** * Closure's goog.inherits adds a {@code superClass_} property to the * subclass, and a {@code constructor} property. */ @Override public void applySubclassRelationship( final NominalTypeBuilder parent, final NominalTypeBuilder child, SubclassType type) { super.applySubclassRelationship(parent, child, type); if (type == SubclassType.INHERITS) { final FunctionType childCtor = child.constructor(); child.declareConstructorProperty( "superClass_", parent.prototypeOrInstance(), childCtor.getSource()); // Notice that constructor functions do not need to be covariant on the superclass. // So if G extends F, new G() and new F() can accept completely different argument // types, but G.prototype.constructor needs to be covariant on F.prototype.constructor. // To get around this, we just turn off type-checking on arguments and return types // of G.prototype.constructor. FunctionType qmarkCtor = childCtor.forgetParameterAndReturnTypes(); child.declarePrototypeProperty("constructor", qmarkCtor, childCtor.getSource()); } }
/** * Whether this type is the original constructor of a nominal type. * Does not include structural constructors. */ public final boolean isNominalConstructor() { if (isConstructor() || isInterface()) { FunctionType fn = toMaybeFunctionType(); if (fn == null) { return false; } // Programmer-defined constructors will have a link // back to the original function in the source tree. // Structural constructors will not. if (fn.getSource() != null) { return true; } // Native constructors are always nominal. return fn.isNativeObjectType(); } return false; }
/** * Whether this type is the original constructor of a nominal type. * Does not include structural constructors. */ public final boolean isNominalConstructor() { if (isConstructor() || isInterface()) { FunctionType fn = toMaybeFunctionType(); if (fn == null) { return false; } // Programmer-defined constructors will have a link // back to the original function in the source tree. // Structural constructors will not. if (fn.getSource() != null) { return true; } // Native constructors are always nominal. return fn.isNativeObjectType(); } return false; }
/** Copies all the information from another function type. */ public FunctionBuilder copyFromOtherFunction(FunctionType otherType) { this.name = otherType.getReferenceName(); this.sourceNode = otherType.getSource(); this.parametersNode = otherType.getParametersNode(); this.returnType = otherType.getReturnType(); this.typeOfThis = otherType.getTypeOfThis(); this.templateTypeMap = otherType.getTemplateTypeMap(); this.isConstructor = otherType.isConstructor(); this.isNativeType = otherType.isNativeObjectType(); return this; }
@Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { ScopeCreator creator = compiler.getTypedScopeCreator(); if (creator instanceof TypedScopeCreator) { TypedScopeCreator scopeCreator = (TypedScopeCreator) creator; String newSrc = scriptRoot.getSourceFileName(); for (TypedVar var : scopeCreator.getAllSymbols()) { JSType type = var.getType(); if (type != null) { FunctionType fnType = type.toMaybeFunctionType(); if (fnType != null && newSrc.equals(NodeUtil.getSourceName(fnType.getSource()))) { fnType.setSource(null); } } } scopeCreator.removeScopesForScript(originalRoot.getSourceFileName()); } }
/** * Returns the super class of the given type that has a constructor. */ private static JSType getFinalParentClass(JSType type) { if (type != null) { ObjectType iproto = ObjectType.cast(type).getImplicitPrototype(); while (iproto != null && iproto.getConstructor() == null) { iproto = iproto.getImplicitPrototype(); } if (iproto != null) { Node source = iproto.getConstructor().getSource(); JSDocInfo jsDoc = source != null ? NodeUtil.getBestJSDocInfo(source) : null; if (jsDoc != null && jsDoc.isConstant()) { return iproto; } } } return null; } }
/** Copies all the information from another function type. */ public FunctionBuilder copyFromOtherFunction(FunctionType otherType) { int isNative = otherType.isNativeObjectType() ? IS_NATIVE : 0; int isAbstract = otherType.isAbstract() ? IS_ABSTRACT : 0; int inferredReturnType = otherType.isReturnTypeInferred() ? INFERRED_RETURN_TYPE : 0; this.name = otherType.getReferenceName(); this.sourceNode = otherType.getSource(); this.parametersNode = otherType.getParametersNode(); this.returnType = otherType.getReturnType(); this.typeOfThis = otherType.getTypeOfThis(); this.templateTypeMap = otherType.getTemplateTypeMap(); this.kind = otherType.getKind(); this.properties = isNative | isAbstract | inferredReturnType; return this; }