use of com.google.javascript.jscomp.newtypes.JSType in project closure-compiler by google.
the class NewTypeInference method analyzeInvocationArgumentsFwd.
private TypeEnv analyzeInvocationArgumentsFwd(Node n, Iterable<Node> args, FunctionType funType, List<JSType> argTypesForDeferredCheck, TypeEnv inEnv) {
checkState(NodeUtil.isCallOrNew(n) || n.isTemplateLit());
TypeEnv env = inEnv;
int i = n.isTemplateLit() ? 1 : 0;
for (Node arg : args) {
JSType formalType = funType.getFormalType(i);
checkState(!formalType.isBottom());
EnvTypePair pair = analyzeExprFwd(arg, env, formalType);
if (NodeUtil.isUnannotatedCallback(arg) && formalType.getFunType() != null) {
i++;
// No deferred check needed.
argTypesForDeferredCheck.add(null);
continue;
}
JSType argTypeForDeferredCheck = pair.type;
// Allow passing undefined for an optional argument.
if (funType.isOptionalArg(i) && pair.type.equals(UNDEFINED)) {
// No deferred check needed.
argTypeForDeferredCheck = null;
} else if (!pair.type.isSubtypeOf(formalType)) {
warnAboutInvalidArgument(n, arg, i, formalType, pair.type);
// No deferred check needed.
argTypeForDeferredCheck = null;
} else {
registerImplicitUses(arg, pair.type, formalType);
}
argTypesForDeferredCheck.add(argTypeForDeferredCheck);
env = pair.env;
i++;
}
return env;
}
use of com.google.javascript.jscomp.newtypes.JSType in project closure-compiler by google.
the class NewTypeInference method analyzeLtGtFwd.
private EnvTypePair analyzeLtGtFwd(Node expr, TypeEnv inEnv) {
Node lhs = expr.getFirstChild();
Node rhs = expr.getLastChild();
EnvTypePair lhsPair = analyzeExprFwd(lhs, inEnv);
EnvTypePair rhsPair = analyzeExprFwd(rhs, lhsPair.env);
// The type of either side can be specialized based on the other side
if (lhsPair.type.isScalar() && !rhsPair.type.isScalar()) {
rhsPair = analyzeExprFwd(rhs, lhsPair.env, lhsPair.type);
} else if (rhsPair.type.isScalar()) {
lhsPair = analyzeExprFwd(lhs, inEnv, rhsPair.type);
rhsPair = analyzeExprFwd(rhs, lhsPair.env, rhsPair.type);
}
JSType lhsType = lhsPair.type;
JSType rhsType = rhsPair.type;
if (!lhsType.isSubtypeOf(rhsType) && !rhsType.isSubtypeOf(lhsType) && !(lhsType.isBoolean() && rhsType.isBoolean())) {
warnInvalidOperand(expr, expr.getToken(), "matching types", lhsType + ", " + rhsType);
}
rhsPair.type = BOOLEAN;
return rhsPair;
}
use of com.google.javascript.jscomp.newtypes.JSType in project closure-compiler by google.
the class NewTypeInference method analyzePropAccessBwd.
private EnvTypePair analyzePropAccessBwd(Node receiver, String pname, TypeEnv outEnv, JSType requiredType) {
Node propAccessNode = receiver.getParent();
QualifiedName qname = new QualifiedName(pname);
JSType reqObjType = pickReqObjType(propAccessNode);
if (!NodeUtil.isPropertyTest(compiler, propAccessNode)) {
reqObjType = reqObjType.withProperty(qname, requiredType);
}
EnvTypePair pair = analyzeExprBwd(receiver, outEnv, reqObjType);
JSType receiverType = pair.type;
JSType propAccessType = receiverType.mayHaveProp(qname) ? receiverType.getProp(qname) : requiredType;
pair.type = propAccessType;
return pair;
}
use of com.google.javascript.jscomp.newtypes.JSType in project closure-compiler by google.
the class NewTypeInference method getTypeEnvFromDeclaredTypes.
private TypeEnv getTypeEnvFromDeclaredTypes() {
TypeEnv env = new TypeEnv();
Set<String> varNames = this.currentScope.getOuterVars();
Set<String> locals = this.currentScope.getLocals();
varNames.addAll(locals);
varNames.addAll(this.currentScope.getExterns());
if (this.currentScope.hasThis()) {
varNames.add(THIS_ID);
}
if (this.currentScope.isFunction()) {
Node fn = this.currentScope.getRoot();
if (!this.currentScope.hasThis() && // a function.
NodeUtil.containsType(fn.getLastChild(), Token.SUPER, NodeUtil.MATCH_NOT_FUNCTION)) {
// This function is a static method on some class. To do lookups of the
// class name, we add the root of the qualified name to the environment.
Node funNameNode = NodeUtil.getBestLValue(fn);
Node qnameRoot = NodeUtil.getRootOfQualifiedName(funNameNode);
checkState(qnameRoot.isName());
varNames.add(qnameRoot.getString());
}
if (this.currentScope.getName() != null) {
varNames.add(this.currentScope.getName());
}
varNames.addAll(this.currentScope.getFormals());
// In the rare case when there is a local variable named "arguments",
// this entry will be overwritten in the foreach loop below.
JSType argumentsType;
DeclaredFunctionType dft = this.currentScope.getDeclaredTypeForOwnBody();
if (dft.getOptionalArity() == 0 && dft.hasRestFormals()) {
argumentsType = dft.getRestFormalsType();
} else {
argumentsType = UNKNOWN;
}
env = envPutType(env, "arguments", commonTypes.getArgumentsArrayType(argumentsType));
}
for (String varName : varNames) {
if (!this.currentScope.isLocalFunDef(varName)) {
JSType declType = this.currentScope.getDeclaredTypeOf(varName);
if (declType == null) {
declType = UNKNOWN;
} else if (areTypeVariablesUnknown) {
declType = declType.substituteGenericsWithUnknown();
}
env = envPutType(env, varName, declType);
}
}
for (String fnName : this.currentScope.getLocalFunDefs()) {
env = envPutType(env, fnName, getSummaryOfLocalFunDef(fnName));
}
return env;
}
use of com.google.javascript.jscomp.newtypes.JSType 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;
}
Aggregations