Package com.google.javascript.jscomp.newtypes

Examples of com.google.javascript.jscomp.newtypes.FunctionType


    for (String local : currentScope.getLocals()) {
      entryEnv = envPutType(entryEnv, local, JSType.UNDEFINED);
    }
    for (String fnName : currentScope.getLocalFunDefs()) {
      JSType summaryType = getSummaryOfLocalFunDef(fnName);
      FunctionType fnType = summaryType.getFunType();
      if (fnType.isConstructor() || fnType.isInterfaceDefinition()) {
        summaryType = fnType.createConstructorObject();
      } else {
        summaryType = summaryType.withProperty(
            new QualifiedName("prototype"), JSType.TOP_OBJECT);
      }
      entryEnv = envPutType(entryEnv, fnName, summaryType);
View Full Code Here


      env = envPutType(env, varName,
          declType == null ? JSType.UNKNOWN : pickInitialType(declType));
    }
    for (String fnName : currentScope.getLocalFunDefs()) {
      JSType summaryType = getSummaryOfLocalFunDef(fnName);
      FunctionType fnType = summaryType.getFunType();
      if (fnType.isConstructor() || fnType.isInterfaceDefinition()) {
        summaryType = fnType.createConstructorObject();
      } else {
        summaryType = summaryType.withProperty(
            new QualifiedName("prototype"), JSType.TOP_OBJECT);
      }
      env = envPutType(env, fnName, summaryType);
View Full Code Here

    }
  }

  private static JSType pickInitialType(JSType declType) {
    Preconditions.checkNotNull(declType);
    FunctionType funType = declType.getFunTypeIfSingletonObj();
    if (funType == null) {
      return declType;
    } else if (funType.isConstructor() || funType.isInterfaceDefinition()) {
      // TODO(dimvar): when declType is a union, consider also creating
      // appropriate ctor objs. (This is going to be rare.)
      return funType.createConstructorObject();
    } else {
      return declType.withProperty(
          new QualifiedName("prototype"), JSType.TOP_OBJECT);
    }
  }
View Full Code Here

          "an object or a union type that includes an object",
          objPair.type);
    }
    ctorPair = analyzeExprFwd(ctor, objPair.env, JSType.topFunction());
    JSType ctorType = ctorPair.type;
    FunctionType ctorFunType = ctorType.getFunType();
    if (!ctorType.isUnknown() &&
        (!ctorType.isSubtypeOf(JSType.topFunction()) ||
            !ctorFunType.isQmarkFunction() && !ctorFunType.isConstructor())) {
      warnInvalidOperand(
          ctor, Token.INSTANCEOF, "a constructor function", ctorType);
    }
    if (ctorFunType == null || !ctorFunType.isConstructor() ||
        (!specializedType.isTruthy() && !specializedType.isFalsy())) {
      ctorPair.type = JSType.BOOLEAN;
      return ctorPair;
    }

    if (ctorFunType.isGeneric()) {
      ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
      for (String typeVar : ctorFunType.getTypeParameters()) {
        builder.put(typeVar, JSType.UNKNOWN);
      }
      ctorFunType = ctorFunType.instantiateGenerics(builder.build());
    }
    // We are in a specialized context *and* we know the constructor type
    JSType instanceType = ctorFunType.getTypeOfThis();
    objPair = analyzeExprFwd(obj, inEnv, JSType.UNKNOWN,
        specializedType.isTruthy() ?
        objPair.type.specialize(instanceType) :
        objPair.type.removeType(instanceType));
    ctorPair = analyzeExprFwd(ctor, objPair.env, JSType.topFunction());
View Full Code Here

    JSType calleeType = calleePair.type;
    if (!calleeType.isSubtypeOf(JSType.topFunction())) {
      warnings.add(JSError.make(
          expr, TypeCheck.NOT_CALLABLE, calleeType.toString()));
    }
    FunctionType funType = calleeType.getFunType();
    if (funType == null
        || funType.isTopFunction() || funType.isQmarkFunction()) {
      return analyzeCallNodeArgumentsFwd(expr, inEnv);
    } else if (funType.isLoose()) {
      return analyzeLooseCallNodeFwd(expr, inEnv, requiredType);
    } else if (expr.isCall() && funType.isConstructor()) {
      // TODO(dimvar): handle constructors with @return; they can be called
      // without new, eg, Number in es3.js
      warnings.add(JSError.make(
          expr, TypeCheck.CONSTRUCTOR_NOT_CALLABLE, funType.toString()));
      return analyzeCallNodeArgumentsFwd(expr, inEnv);
    } else if (expr.isNew() && !funType.isConstructor()) {
      warnings.add(JSError.make(
          expr, NOT_A_CONSTRUCTOR, funType.toString()));
      return analyzeCallNodeArgumentsFwd(expr, inEnv);
    }
    int maxArity = funType.getMaxArity();
    int minArity = funType.getMinArity();
    int numArgs = expr.getChildCount() - 1;
    if (numArgs < minArity || numArgs > maxArity) {
      warnings.add(JSError.make(
          expr, TypeCheck.WRONG_ARGUMENT_COUNT, "",
          Integer.toString(numArgs), Integer.toString(minArity),
          " and at most " + maxArity));
      return analyzeCallNodeArgumentsFwd(expr, inEnv);
    }
    FunctionType origFunType = funType; // save for later
    if (funType.isGeneric()) {
      Map<String, JSType> typeMap =
          calcTypeInstantiationFwd(expr, funType, inEnv);
      funType = funType.instantiateGenerics(typeMap);
      println("Instantiated function type: " + funType);
    }
    // argTypes collects types of actuals for deferred checks.
    List<JSType> argTypes = new ArrayList<>();
    TypeEnv tmpEnv = inEnv;
    Node arg = expr.getChildAtIndex(1);
    for (int i = 0; i < numArgs; i++) {
      JSType formalType = funType.getFormalType(i);
      if (formalType.isBottom()) {
        warnings.add(JSError.make(expr, CALL_FUNCTION_WITH_BOTTOM_FORMAL,
                Integer.toString(i)));
        formalType = JSType.UNKNOWN;
      }
      EnvTypePair pair = analyzeExprFwd(arg, tmpEnv, formalType);
      JSType argTypeForDeferredCheck = pair.type;
      // Allow passing undefined for an optional argument.
      if (i >= minArity && pair.type.equals(JSType.UNDEFINED)) {
        argTypeForDeferredCheck = null; // No deferred check needed.
      } else if (!pair.type.isSubtypeOf(formalType)) {
        warnings.add(JSError.make(arg, INVALID_ARGUMENT_TYPE,
                Integer.toString(i + 1), "",
                formalType.toString(), pair.type.toString()));
        argTypeForDeferredCheck = null; // No deferred check needed.
      }
      argTypes.add(argTypeForDeferredCheck);
      tmpEnv = pair.env;
      arg = arg.getNext();
    }
    JSType retType = funType.getReturnType();
    if (callee.isName()) {
      String calleeName = callee.getQualifiedName();
      if (currentScope.isKnownFunction(calleeName)
          && !currentScope.isExternalFunction(calleeName)) {
        // Local function definitions will be type-checked more
        // exactly using their summaries, and don't need deferred checks
        if (currentScope.isLocalFunDef(calleeName)) {
          collectTypesForFreeVarsFwd(callee, tmpEnv);
        } else if (!origFunType.isGeneric()) {
          JSType expectedRetType = requiredType;
          println("Updating deferred check with ret: ", expectedRetType,
              " and args: ", argTypes);
          DeferredCheck dc;
          if (expr.isCall()) {
View Full Code Here

      // So, we don't associate pname with a getter/setter.
      // We add a property with a name that's weird enough to hopefully avoid
      // an accidental clash.
      if (prop.isGetterDef() || prop.isSetterDef()) {
        EnvTypePair pair = analyzeExprFwd(prop.getFirstChild(), env);
        FunctionType funType = pair.type.getFunType();
        Preconditions.checkNotNull(funType);
        String specialPropName;
        JSType propType;
        if (prop.isGetterDef()) {
          specialPropName = GETTER_PREFIX + pname;
          propType = funType.getReturnType();
        } else {
          specialPropName = SETTER_PREFIX + pname;
          propType = pair.type;
        }
        result = result.withProperty(
View Full Code Here

  private void collectTypesForFreeVarsFwd(Node callee, TypeEnv env) {
    Scope calleeScope = currentScope.getScope(callee.getQualifiedName());
    for (String freeVar : calleeScope.getOuterVars()) {
      if (calleeScope.getDeclaredTypeOf(freeVar) == null) {
        FunctionType summary = summaries.get(calleeScope).getFunType();
        JSType outerType = envGetType(env, freeVar);
        JSType innerType = summary.getOuterVarPrecondition(freeVar);
        if (outerType != null && JSType.meet(outerType, innerType).isBottom()) {
          warnings.add(JSError.make(callee, CROSS_SCOPE_GOTCHA,
                  freeVar, outerType.toString(), innerType.toString()));
        }
      }
View Full Code Here

  private EnvTypePair analyzeCallNewBwd(
      Node expr, TypeEnv outEnv, JSType requiredType) {
    Node callee = expr.getFirstChild();
    JSType calleeTypeGeneral =
        analyzeExprBwd(callee, outEnv, JSType.topFunction()).type;
    FunctionType funType = calleeTypeGeneral.getFunType();
    if (funType == null) {
      return analyzeCallNodeArgumentsBwd(expr, outEnv);
    } else if (funType.isLoose()) {
      return analyzeLooseCallNodeBwd(expr, outEnv, requiredType);
    } else if (expr.isCall() && funType.isConstructor() ||
        expr.isNew() && !funType.isConstructor()) {
      return analyzeCallNodeArgumentsBwd(expr, outEnv);
    } else if (funType.isTopFunction()) {
      return analyzeCallNodeArgumentsBwd(expr, outEnv);
    }
    if (callee.isName() && !funType.isGeneric() && expr.isCall()) {
      createDeferredCheckBwd(expr, requiredType);
    }
    int numArgs = expr.getChildCount() - 1;
    if (numArgs < funType.getMinArity() || numArgs > funType.getMaxArity()) {
      return analyzeCallNodeArgumentsBwd(expr, outEnv);
    }
    if (funType.isGeneric()) {
      Map<String, JSType> typeMap =
          calcTypeInstantiationBwd(expr, funType, outEnv);
      funType = funType.instantiateGenerics(typeMap);
    }
    TypeEnv tmpEnv = outEnv;
    // In bwd direction, analyze arguments in reverse
    for (int i = expr.getChildCount() - 2; i >= 0; 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.
      if (formalType.isBottom()) {
        formalType = JSType.UNKNOWN;
      }
      Node arg = expr.getChildAtIndex(i + 1);
      tmpEnv = analyzeExprBwd(arg, tmpEnv, formalType).env;
      // We don't need deferred checks for args in BWD
    }
    if (callee.isName() && currentScope.isLocalFunDef(callee.getString())) {
      tmpEnv = collectTypesForFreeVarsBwd(callee, tmpEnv);
    }
    return new EnvTypePair(tmpEnv, funType.getReturnType());
  }
View Full Code Here

      mayWarnAboutDictPropAccess(obj, lvalueType);
    }
    QualifiedName setterPname =
        new QualifiedName(SETTER_PREFIX + pnameAsString);
    if (lvalueType.hasProp(setterPname)) {
      FunctionType funType = lvalueType.getProp(setterPname).getFunType();
      Preconditions.checkNotNull(funType);
      JSType formalType = funType.getFormalType(0);
      Preconditions.checkState(!formalType.isBottom());
      return new LValueResultFwd(lvalueEnv, formalType, formalType, null);
    }
    return new LValueResultFwd(
        lvalueEnv,
View Full Code Here

        case Token.NEW:
          JSType ratorType = simpleInferExprType(n.getFirstChild());
          if (ratorType == null) {
            return null;
          }
          FunctionType funType = ratorType.getFunType();
          if (funType == null) {
            return null;
          }
          if (funType.isGeneric()) {
            ImmutableList.Builder<JSType> argTypes = ImmutableList.builder();
            for (Node argNode = n.getFirstChild().getNext();
                 argNode != null;
                 argNode = argNode.getNext()) {
              JSType t = simpleInferExprType(argNode);
              if (t == null) {
                return null;
              }
              argTypes.add(t);
            }
            funType = funType
                .instantiateGenericsFromArgumentTypes(argTypes.build());
            if (funType == null) {
              return null;
            }
          }
          return funType.getReturnType();
        default:
          switch (NodeUtil.getKnownValueType(n)) {
            case NULL:
              return JSType.NULL;
            case VOID:
View Full Code Here

TOP

Related Classes of com.google.javascript.jscomp.newtypes.FunctionType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.