use of com.google.javascript.jscomp.newtypes.TypeEnv in project closure-compiler by google.
the class NewTypeInference method analyzeArrayLitBwd.
private EnvTypePair analyzeArrayLitBwd(Node expr, TypeEnv outEnv) {
TypeEnv env = outEnv;
JSType elementType = BOTTOM;
for (Node elm = expr.getLastChild(); elm != null; elm = elm.getPrevious()) {
EnvTypePair pair = analyzeExprBwd(elm, env);
env = pair.env;
elementType = JSType.join(elementType, pair.type);
}
elementType = firstNonBottom(elementType, UNKNOWN);
return new EnvTypePair(env, commonTypes.getArrayInstance(elementType));
}
use of com.google.javascript.jscomp.newtypes.TypeEnv in project closure-compiler by google.
the class NewTypeInference method analyzeInvocationFwd.
private EnvTypePair analyzeInvocationFwd(Node expr, TypeEnv inEnv, JSType requiredType, JSType specializedType) {
if (isPropertyTestCall(expr)) {
return analyzePropertyTestCallFwd(expr, inEnv, specializedType);
}
if (expr.isCall() && this.convention.getObjectLiteralCast(expr) != null) {
return analyzeObjLitCastFwd(this.convention.getObjectLiteralCast(expr), expr, inEnv);
}
Node callee = expr.getFirstChild();
if (isFunctionBind(callee, inEnv, true)) {
return analyzeFunctionBindFwd(expr, inEnv);
}
AssertionFunctionSpec assertionFunctionSpec = assertionFunctionsMap.get(callee.getQualifiedName());
if (assertionFunctionSpec != null) {
return analyzeAssertionCall(expr, inEnv, assertionFunctionSpec);
}
EnvTypePair calleePair = analyzeExprFwd(callee, inEnv, commonTypes.topFunction());
TypeEnv envAfterCallee = calleePair.env;
calleePair = mayWarnAboutNullableReferenceAndTighten(callee, calleePair.type, null, envAfterCallee);
JSType calleeType = calleePair.type;
if (calleeType.isBottom() || !calleeType.isSubtypeOf(commonTypes.topFunction())) {
warnings.add(JSError.make(expr, NOT_CALLABLE, calleeType.toString()));
}
FunctionType funType = calleeType.getFunTypeIfSingletonObj();
if (funType == null || funType.isTopFunction() || funType.isQmarkFunction()) {
return analyzeInvocationArgsFwdWhenError(expr, envAfterCallee);
} else if (funType.isLoose()) {
return analyzeLooseCallNodeFwd(expr, envAfterCallee, requiredType);
} else if (!isConstructorCall(expr) && funType.isSomeConstructorOrInterface() && (funType.getReturnType().isUnknown() || funType.getReturnType().isUndefined())) {
warnings.add(JSError.make(expr, CONSTRUCTOR_NOT_CALLABLE, funType.toString()));
return analyzeInvocationArgsFwdWhenError(expr, envAfterCallee);
} else if (expr.isNew()) {
if (!funType.isSomeConstructorOrInterface() || funType.isInterfaceDefinition()) {
// or as an arbitrarily nested property), don't warn.
if (callee.isQualifiedName()) {
String qnameRoot = QualifiedName.fromNode(callee).getLeftmostName();
if (!this.currentScope.isFormalParamInAnyAncestorScope(qnameRoot)) {
warnings.add(JSError.make(expr, NOT_A_CONSTRUCTOR, funType.toString()));
}
}
return analyzeInvocationArgsFwdWhenError(expr, envAfterCallee);
} else if (funType.isConstructorOfAbstractClass()) {
warnings.add(JSError.make(expr, CANNOT_INSTANTIATE_ABSTRACT_CLASS, funType.toString()));
return analyzeInvocationArgsFwdWhenError(expr, envAfterCallee);
}
} else if (expr.isTaggedTemplateLit()) {
funType = checkTaggedFunctionFirstParam(expr.getLastChild(), expr.getFirstChild(), funType);
}
if (!isInvocationArgCountCorrectAndWarn(funType, expr, callee)) {
return analyzeInvocationArgsFwdWhenError(expr, envAfterCallee);
}
// save for later
FunctionType originalFunType = funType;
if (funType.isGeneric()) {
Node receiver = callee.isGetProp() ? callee.getFirstChild() : null;
Node firstArg = expr.getSecondChild();
ImmutableMap<String, JSType> typeMap = calcTypeInstantiationFwd(expr, receiver, firstArg, funType, envAfterCallee);
funType = instantiateCalleeMaybeWithTTL(funType, typeMap);
callee.setTypeI(this.commonTypes.fromFunctionType(funType));
println("Instantiated function type: ", funType);
}
// argTypes collects types of actuals for deferred checks.
List<JSType> argTypes = new ArrayList<>();
Node invocationNode = expr.isTaggedTemplateLit() ? expr.getLastChild() : expr;
Iterable<Node> argIterable = NodeUtil.getInvocationArgsAsIterable(expr);
TypeEnv tmpEnv = analyzeInvocationArgumentsFwd(invocationNode, argIterable, funType, argTypes, envAfterCallee);
if (callee.isName()) {
String calleeName = callee.getString();
if (this.currentScope.isKnownFunction(calleeName) && !this.currentScope.isExternalFunction(calleeName)) {
// exactly using their summaries, and don't need deferred checks
if (this.currentScope.isLocalFunDef(calleeName)) {
tmpEnv = collectTypesForEscapedVarsFwd(callee, tmpEnv);
} else if (!originalFunType.isGeneric()) {
JSType expectedRetType = requiredType;
println("Updating deferred check with ret: ", expectedRetType, " and args: ", argTypes);
DeferredCheck dc;
if (funType.isSomeConstructorOrInterface()) {
dc = new DeferredCheck(expr, null, this.currentScope, this.currentScope.getScope(calleeName));
deferredChecks.put(expr, dc);
} else {
dc = deferredChecks.get(expr);
if (dc != null) {
dc.updateReturn(expectedRetType);
} else {
// The backward analysis of a function is skipped when all
// variables, including outer vars, are declared.
// So, we check that dc is null iff bwd was skipped.
Preconditions.checkState(!this.currentScope.hasUndeclaredFormalsOrOuters(), "No deferred check created in backward direction for %s", expr);
}
}
if (dc != null) {
dc.updateArgTypes(argTypes);
}
}
}
}
JSType retType = expr.isNew() ? funType.getThisType() : funType.getReturnType();
if (retType.isSubtypeOf(requiredType)) {
retType = retType.specialize(specializedType);
}
return new EnvTypePair(tmpEnv, retType);
}
use of com.google.javascript.jscomp.newtypes.TypeEnv in project closure-compiler by google.
the class NewTypeInference method analyzePropLValFwd.
private LValueResultFwd analyzePropLValFwd(Node obj, QualifiedName pname, LValueResultFwd recvLvalue, JSType requiredType, boolean insideQualifiedName) {
checkArgument(pname.isIdentifier());
TypeEnv inEnv = recvLvalue.env;
JSType recvType = recvLvalue.type;
if (!recvType.isUnion() && !recvType.isSingletonObj()) {
// The lvalue is a subtype of TOP_OBJECT, but does not contain an object
// yet, eg, it is ?, truthy, or bottom.
recvType = TOP_OBJECT.withLoose();
}
Node propAccessNode = obj.getParent();
if (propAccessNode.isGetProp() && propAccessNode.getParent().isAssign() && mayWarnAboutPropCreation(pname, propAccessNode, recvType)) {
return new LValueResultFwd(inEnv, requiredType, null, null);
}
if (!insideQualifiedName && mayWarnAboutConstProp(propAccessNode, recvType, pname)) {
return new LValueResultFwd(inEnv, requiredType, null, null);
}
if (!recvType.hasProp(pname)) {
// name, or for assignment ops that won't create a new property.
if (insideQualifiedName || !propAccessNode.getParent().isAssign()) {
mayWarnAboutInexistentProp(propAccessNode, recvType, pname);
if (!recvType.isLoose()) {
return new LValueResultFwd(inEnv, requiredType, null, null);
}
}
if (recvType.isLoose()) {
// For loose objects, create the inner property if it doesn't exist.
recvType = recvType.withProperty(pname, UNKNOWN);
inEnv = updateLvalueTypeInEnv(inEnv, obj, recvLvalue.ptr, recvType);
}
}
if (propAccessNode.isGetElem()) {
mayWarnAboutStructPropAccess(obj, recvType);
} else if (propAccessNode.isGetProp()) {
mayWarnAboutDictPropAccess(obj, recvType);
}
QualifiedName setterPname = new QualifiedName(commonTypes.createSetterPropName(pname.getLeftmostName()));
if (recvType.hasProp(setterPname)) {
FunctionType funType = recvType.getProp(setterPname).getFunType();
checkNotNull(funType, "recvType=%s, setterPname=%s", recvType, setterPname);
JSType formalType = funType.getFormalType(0);
checkState(!formalType.isBottom());
return new LValueResultFwd(inEnv, formalType, formalType, null);
}
QualifiedName ptr = recvLvalue.ptr == null ? null : QualifiedName.join(recvLvalue.ptr, pname);
return recvType.mayHaveProp(pname) ? new LValueResultFwd(inEnv, recvType.getProp(pname), recvType.getDeclaredProp(pname), ptr) : new LValueResultFwd(inEnv, UNKNOWN, null, ptr);
}
use of com.google.javascript.jscomp.newtypes.TypeEnv in project closure-compiler by google.
the class NewTypeInference method getOutEnv.
private TypeEnv getOutEnv(DiGraphNode<Node, ControlFlowGraph.Branch> dn) {
List<DiGraphEdge<Node, ControlFlowGraph.Branch>> outEdges = dn.getOutEdges();
if (outEdges.isEmpty()) {
// This occurs when visiting a throw in the backward direction.
checkArgument(dn.getValue().isThrow());
return this.typeEnvFromDeclaredTypes;
}
if (outEdges.size() == 1) {
return envs.get(outEdges.get(0));
}
Set<TypeEnv> envSet = new LinkedHashSet<>();
for (DiGraphEdge<Node, ControlFlowGraph.Branch> de : outEdges) {
TypeEnv env = envs.get(de);
if (env != null) {
envSet.add(env);
}
}
checkState(!envSet.isEmpty());
return TypeEnv.join(envSet);
}
use of com.google.javascript.jscomp.newtypes.TypeEnv in project closure-compiler by google.
the class NewTypeInference method analyzeVarDeclFwd.
/**
* This method processes a single variable declaration in a VAR statement, in the forward
* phase of the analysis.
*/
private TypeEnv analyzeVarDeclFwd(Node nameNode, TypeEnv inEnv) {
String varName = nameNode.getString();
JSType declType = this.currentScope.getDeclaredTypeOf(varName);
if (this.currentScope.isLocalFunDef(varName)) {
return inEnv;
}
Node rhs = nameNode.getFirstChild();
if (NodeUtil.isNamespaceDecl(nameNode) || (GlobalTypeInfoCollector.isCtorDefinedByCall(nameNode) && !isFunctionBind(rhs.getFirstChild(), inEnv, true)) || nameNode.getParent().getBooleanProp(Node.ANALYZED_DURING_GTI)) {
Preconditions.checkNotNull(declType, "Can't skip var declaration with undeclared type at: %s", nameNode);
if (!rhs.isQualifiedName()) {
analyzeExprFwdIgnoreResult(rhs, inEnv);
}
maybeSetTypeI(nameNode, declType);
maybeSetTypeI(rhs, declType);
return envPutType(inEnv, varName, declType);
}
TypeEnv outEnv = inEnv;
JSType rhsType = null;
if (rhs != null) {
EnvTypePair pair = analyzeExprFwd(rhs, inEnv, firstNonNull(declType, UNKNOWN));
outEnv = pair.env;
rhsType = pair.type;
if (declType != null) {
if (rhsType.isSubtypeOf(declType)) {
registerImplicitUses(rhs, rhsType, declType);
} else {
registerMismatchAndWarn(JSError.make(rhs, MISTYPED_ASSIGN_RHS, errorMsgWithTypeDiff(declType, rhsType)), rhsType, declType);
}
}
}
JSType varType = getInitialTypeOfVar(nameNode, declType, rhsType);
maybeSetTypeI(nameNode, varType);
return envPutType(outEnv, varName, varType);
}
Aggregations