Package com.google.javascript.jscomp.newtypes

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


  private void createSummary(Scope fn) {
    TypeEnv entryEnv = getEntryTypeEnv();
    TypeEnv exitEnv = getFinalTypeEnv();
    FunctionTypeBuilder builder = new FunctionTypeBuilder();

    DeclaredFunctionType declType = fn.getDeclaredType();
    int reqArity = declType.getRequiredArity();
    int optArity = declType.getOptionalArity();
    if (declType.isGeneric()) {
      builder.addTypeParameters(declType.getTypeParameters());
    }

    // Every trailing undeclared formal whose inferred type is ?
    // or contains undefined can be marked as optional.
    List<String> formals = fn.getFormals();
    for (int i = reqArity - 1; i >= 0; i--) {
      JSType formalType = fn.getDeclaredType().getFormalType(i);
      if (formalType != null) {
        break;
      }
      String formalName = formals.get(i);
      formalType = getTypeAfterFwd(formalName, entryEnv, exitEnv);
      if (formalType.isUnknown() || JSType.UNDEFINED.isSubtypeOf(formalType)) {
        reqArity--;
      } else {
        break;
      }
    }

    // Collect types of formals in the builder
    int formalIndex = 0;
    for (String formal : formals) {
      JSType formalType = fn.getDeclaredTypeOf(formal);
      if (formalType == null) {
        formalType = getTypeAfterFwd(formal, entryEnv, exitEnv);
      }
      if (formalIndex < reqArity) {
        builder.addReqFormal(formalType);
      } else if (formalIndex < optArity) {
        builder.addOptFormal(formalType);
      }
      formalIndex++;
    }
    if (declType.hasRestFormals()) {
      builder.addRestFormals(declType.getFormalType(formalIndex));
    }

    for (String outer : fn.getOuterVars()) {
      println("Free var ", outer, " going in summary");
      builder.addOuterVarPrecondition(
          outer, getTypeAfterFwd(outer, entryEnv, exitEnv));
    }

    builder.addNominalType(declType.getNominalType());
    builder.addReceiverType(declType.getReceiverType());
    JSType declRetType = declType.getReturnType();
    JSType actualRetType = envGetType(exitEnv, RETVAL_ID);

    if (declRetType == null) {
      builder.addRetType(actualRetType);
    } else {
View Full Code Here


            parent.getFirstChild(), parentScope);
        if (calleeDeclType != null) {
          int index = parent.getIndexOfChild(declNode) - 1;
          JSType declTypeFromCallee = calleeDeclType.getFormalType(index);
          if (declTypeFromCallee != null) {
            DeclaredFunctionType t =
                computeFnDeclaredTypeFromCallee(declNode, declTypeFromCallee);
            if (t != null) {
              return t;
            }
          }
        }
      }
      // When any of the above IFs fails, fall through to treat the function as
      // a function without jsdoc.

      ImmutableList<String> typeParameters =
          fnDoc == null ? null : fnDoc.getTemplateTypeNames();

      // TODO(dimvar): warn if multiple jsdocs for a fun

      // Compute the types of formals and the return type
      FunctionTypeBuilder builder =
          typeParser.getFunctionType(fnDoc, declNode, ownerType, parentScope);
      RawNominalType ctorType = null;

      // Look at other annotations, eg, @constructor
      if (fnDoc != null) {
        NominalType parentClass = null;
        if (fnDoc.hasBaseType()) {
          if (!fnDoc.isConstructor()) {
            warnings.add(JSError.make(
                declNode, EXTENDS_NOT_ON_CTOR_OR_INTERF, functionName));
          } else {
            Node docNode = fnDoc.getBaseType().getRoot();
            if (typeParser.hasKnownType(
                docNode, ownerType, parentScope, typeParameters)) {
              parentClass = typeParser.getNominalType(
                      docNode, ownerType, parentScope, typeParameters);
              if (parentClass == null) {
                warnings.add(JSError.make(
                    declNode, EXTENDS_NON_OBJECT, functionName,
                    docNode.toStringTree()));
              } else if (parentClass.isInterface()) {
                warnings.add(JSError.make(
                    declNode, TypeCheck.CONFLICTING_EXTENDED_TYPE,
                    "constructor", functionName));
                parentClass = null;
              }
            }
          }
        }
        ctorType =
            declNode.isFunction() ? nominaltypesByNode.get(declNode) : null;
        ImmutableSet<NominalType> implementedIntfs =
            typeParser.getImplementedInterfaces(
                fnDoc, ownerType, parentScope, typeParameters);

        if (ctorType == null &&
            (fnDoc.isConstructor() || fnDoc.isInterface())) {
          // Anonymous type, don't register it.
          return builder.buildDeclaration();
        } else if (fnDoc.isConstructor()) {
          String className = ctorType.toString();
          if (parentClass == null && !"Object".equals(functionName)) {
            parentClass = getObjectNominalType();
          }
          if (parentClass != null) {
            if (!ctorType.addSuperClass(parentClass)) {
              warnings.add(JSError.make(
                  declNode, INHERITANCE_CYCLE, className));
            } else if (parentClass != getObjectNominalType()) {
              if (ctorType.isStruct() && !parentClass.isStruct()) {
                warnings.add(JSError.make(
                    declNode, TypeCheck.CONFLICTING_SHAPE_TYPE,
                        "struct", className));
              } else if (ctorType.isDict() && !parentClass.isDict()) {
                warnings.add(JSError.make(
                    declNode, TypeCheck.CONFLICTING_SHAPE_TYPE,
                    "dict", className));
              }
            }
          }
          if (ctorType.isDict() && !implementedIntfs.isEmpty()) {
            warnings.add(JSError.make(
                declNode, DICT_IMPLEMENTS_INTERF, className));
          }
          boolean noCycles = ctorType.addInterfaces(implementedIntfs);
          Preconditions.checkState(noCycles);
          builder.addNominalType(ctorType.getAsNominalType());
        } else if (fnDoc.isInterface()) {
          if (!implementedIntfs.isEmpty()) {
            warnings.add(JSError.make(declNode,
                TypeCheck.CONFLICTING_IMPLEMENTED_TYPE, functionName));
          }
          boolean noCycles = ctorType.addInterfaces(
              typeParser.getExtendedInterfaces(
                  fnDoc, ownerType, parentScope, typeParameters));
          if (!noCycles) {
            warnings.add(JSError.make(
                declNode, INHERITANCE_CYCLE, ctorType.toString()));
          }
          builder.addNominalType(ctorType.getAsNominalType());
        } else if (!implementedIntfs.isEmpty()) {
          warnings.add(JSError.make(
              declNode, IMPLEMENTS_WITHOUT_CONSTRUCTOR, functionName));
        }
      }

      if (ownerType != null) {
        builder.addReceiverType(ownerType.getAsNominalType());
      }
      DeclaredFunctionType result = builder.buildDeclaration();
      if (ctorType != null) {
        ctorType.setCtorFunction(result.toFunctionType());
      }
      return result;
    }
View Full Code Here

      FunctionType funType = declaredTypeAsJSType.getFunType();
      if (funType == null) {
        return null;
      }
      DeclaredFunctionType declType = funType.toDeclaredFunctionType();
      if (declType == null) {
        return null;
      }
      int numFormals = declNode.getChildAtIndex(1).getChildCount();
      int reqArity = declType.getRequiredArity();
      int optArity = declType.getOptionalArity();
      boolean hasRestFormals = declType.hasRestFormals();
      if (reqArity == optArity && !hasRestFormals) {
        return numFormals == reqArity ? declType : null;
      }
      if (numFormals == optArity && !hasRestFormals
          || numFormals == (optArity + 1) && hasRestFormals) {
View Full Code Here

    private void updateFnScope(Scope fnScope, RawNominalType ownerType) {
      Node fn = fnScope.getRoot();
      Preconditions.checkState(fn.isFunction());
      JSDocInfo fnDoc = NodeUtil.getFunctionJSDocInfo(fn);
      String functionName = getFunInternalName(fn);
      DeclaredFunctionType declFunType = computeFnDeclaredType(
        fnDoc, functionName, fn, ownerType, currentScope);
      fnScope.setDeclaredType(declFunType);
    }
View Full Code Here

     * but also for @lends.
     */
    private void mayAddPropToPrototype(
        RawNominalType rawType, String pname, Node defSite, Node initializer) {
      Scope methodScope;
      DeclaredFunctionType methodType;
      JSType propDeclType;

      // Find the declared type of the property.
      if (initializer != null && initializer.isFunction()) {
        if (initializer.getLastChild().hasChildren() && rawType.isInterface()) {
          warnings.add(JSError.make(
              initializer.getLastChild(), TypeCheck.INTERFACE_METHOD_NOT_EMPTY));
        }

        // TODO(dimvar): we must do this for any function "defined" as the rhs
        // of an assignment to a property, not just when the property is a
        // prototype property.
        methodScope = visitFunctionLate(initializer, rawType);
        methodType = methodScope.getDeclaredType();
        propDeclType = JSType.fromFunctionType(methodType.toFunctionType());
      } else {
        JSDocInfo jsdoc = NodeUtil.getBestJSDocInfo(defSite);
        if (jsdoc != null && jsdoc.containsFunctionDeclaration()) {
          // We're parsing a function declaration without a function initializer
          methodScope = null;
          methodType = computeFnDeclaredType(
              jsdoc, pname, defSite, rawType, currentScope);
          propDeclType = JSType.fromFunctionType(methodType.toFunctionType());
        } else if (jsdoc != null && jsdoc.hasType()) {
          // We are parsing a non-function prototype property
          methodScope = null;
          methodType = null;
          propDeclType =
View Full Code Here

        Preconditions.checkState(!methodTypes.isEmpty());
        PropertyDef localPropDef =
            propertyDefs.get(rawNominalType, pname);
        // To find the declared type of a method, we must meet declared types
        // from all inherited methods.
        DeclaredFunctionType superMethodType =
            DeclaredFunctionType.meet(methodTypes);
        DeclaredFunctionType updatedMethodType =
            localPropDef.methodType.withTypeInfoFromSuper(superMethodType);
        localPropDef.updateMethodType(updatedMethodType);
        propTypesToProcess.put(pname,
            JSType.fromFunctionType(updatedMethodType.toFunctionType()));
      }
      // Check inherited types of all props
    add_interface_props:
      for (String pname : propTypesToProcess.keySet()) {
        Collection<JSType> defs = propTypesToProcess.get(pname);
View Full Code Here

TOP

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

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.