use of com.google.javascript.jscomp.newtypes.FunctionType in project closure-compiler by google.
the class NewTypeInference method mayAdjustObjLitType.
/**
* If the object literal is lended, or assigned to a prototype, find a better
* type for it than the object-literal type.
*/
private JSType mayAdjustObjLitType(Node objLit, JSDocInfo jsdoc, TypeEnv env, JSType originalType) {
Node parent = objLit.getParent();
QualifiedName classqname = null;
if (parent.isAssign() && NodeUtil.isPrototypeAssignment(parent.getFirstChild())) {
classqname = QualifiedName.fromNode(parent.getFirstFirstChild());
} else if (jsdoc != null && jsdoc.getLendsName() != null) {
QualifiedName lendsQname = QualifiedName.fromQualifiedString(jsdoc.getLendsName());
if (lendsQname.getRightmostName().equals("prototype")) {
classqname = lendsQname.getAllButRightmost();
}
} else if (parent.isCall() && this.convention.getObjectLiteralCast(parent) != null) {
ObjectLiteralCast cast = this.convention.getObjectLiteralCast(parent);
if (cast.typeName != null) {
classqname = QualifiedName.fromQualifiedString(cast.typeName);
}
}
if (classqname != null) {
JSType classType = envGetTypeOfQname(env, classqname);
if (classType != null) {
FunctionType clazz = classType.getFunTypeIfSingletonObj();
JSType instance = clazz == null ? null : clazz.getInstanceTypeOfCtor();
if (instance != null) {
return instance.getPrototypeObject();
}
}
}
return originalType;
}
use of com.google.javascript.jscomp.newtypes.FunctionType in project closure-compiler by google.
the class NewTypeInference method analyzeInvocationBwd.
private EnvTypePair analyzeInvocationBwd(Node expr, TypeEnv outEnv, JSType requiredType) {
checkArgument(expr.isNew() || expr.isCall() || expr.isTaggedTemplateLit());
Node callee = expr.getFirstChild();
EnvTypePair pair = analyzeExprBwd(callee, outEnv, commonTypes.topFunction());
TypeEnv envAfterCallee = pair.env;
FunctionType funType = pair.type.getFunType();
if (funType == null) {
return analyzeInvocationArgumentsBwd(expr, expr.getFirstChild(), envAfterCallee);
} else if (funType.isLoose()) {
return analyzeLooseCallNodeBwd(expr, envAfterCallee, requiredType);
} else if ((expr.isCall() && funType.isSomeConstructorOrInterface()) || (expr.isNew() && !funType.isSomeConstructorOrInterface())) {
return analyzeInvocationArgumentsBwd(expr, expr.getFirstChild(), envAfterCallee);
} else if (funType.isTopFunction()) {
return analyzeInvocationArgumentsBwd(expr, expr.getFirstChild(), envAfterCallee);
}
if (callee.isName() && !funType.isGeneric() && (expr.isCall() || expr.isTaggedTemplateLit())) {
createDeferredCheckBwd(expr, requiredType);
}
int numArgs = NodeUtil.getInvocationArgsCount(expr);
if (numArgs < funType.getMinArity() || numArgs > funType.getMaxArity()) {
if (expr.isTaggedTemplateLit()) {
return analyzeInvocationArgumentsBwd(expr.getLastChild(), null, envAfterCallee);
} else {
return analyzeInvocationArgumentsBwd(expr, expr.getFirstChild(), envAfterCallee);
}
}
if (funType.isGeneric()) {
Map<String, JSType> typeMap = calcTypeInstantiationBwd(expr, funType, envAfterCallee);
funType = funType.instantiateGenerics(typeMap);
}
TypeEnv tmpEnv = envAfterCallee;
// In bwd direction, analyze arguments in reverse
Node target = expr.isTaggedTemplateLit() ? null : expr.getFirstChild();
Node start = expr.isTaggedTemplateLit() ? expr.getLastChild().getLastChild() : expr.getLastChild();
int i = numArgs;
for (Node arg = start; arg != target; arg = arg.getPrevious()) {
if (expr.isTaggedTemplateLit() && !arg.isTemplateLitSub()) {
// with the formal types of the tag function, i needs to stay unchanged here.
continue;
}
i--;
JSType formalType = funType.getFormalType(i);
// The type of a formal can be BOTTOM as the result of a join.
// Don't use this as a requiredType.
formalType = firstNonBottom(formalType, UNKNOWN);
tmpEnv = analyzeExprBwd(arg, tmpEnv, formalType).env;
// We don't need deferred checks for args in BWD
}
JSType retType = expr.isNew() ? funType.getThisType() : funType.getReturnType();
return new EnvTypePair(tmpEnv, retType);
}
Aggregations