Package com.google.dart.engine.type

Examples of com.google.dart.engine.type.Type


  }

  @Override
  public Void visitIndexExpression(IndexExpression node) {
    Expression target = node.getRealTarget();
    Type staticType = getStaticType(target);
    Type propagatedType = getPropagatedType(target);
    String getterMethodName = TokenType.INDEX.getLexeme();
    String setterMethodName = TokenType.INDEX_EQ.getLexeme();
    boolean isInGetterContext = node.inGetterContext();
    boolean isInSetterContext = node.inSetterContext();
View Full Code Here


    if (target instanceof SuperExpression && !isSuperInValidContext((SuperExpression) target)) {
      return null;
    }
    Element staticElement;
    Element propagatedElement;
    Type staticType = null;
    Type propagatedType = null;
    if (target == null) {
      staticElement = resolveInvokedElement(methodName);
      propagatedElement = null;
    } else if (methodName.getName().equals(FunctionElement.LOAD_LIBRARY_NAME)
        && isDeferredPrefix(target)) {
      LibraryElement importedLibrary = getImportedLibrary(target);
      methodName.setStaticElement(importedLibrary.getLoadLibraryFunction());
      return null;
    } else {
      staticType = getStaticType(target);
      propagatedType = getPropagatedType(target);
      //
      // If this method invocation is of the form 'C.m' where 'C' is a class, then we don't call
      // resolveInvokedElement(..) which walks up the class hierarchy, instead we just look for the
      // member in the type only.
      //
      ClassElementImpl typeReference = getTypeReference(target);
      if (typeReference != null) {
        staticElement = propagatedElement = resolveElement(typeReference, methodName);
      } else {
        staticElement = resolveInvokedElementWithTarget(target, staticType, methodName);
        propagatedElement = resolveInvokedElementWithTarget(target, propagatedType, methodName);
      }
    }
    staticElement = convertSetterToGetter(staticElement);
    propagatedElement = convertSetterToGetter(propagatedElement);
    //
    // Record the results.
    //
    methodName.setStaticElement(staticElement);
    methodName.setPropagatedElement(propagatedElement);
    ArgumentList argumentList = node.getArgumentList();
    if (staticElement != null) {
      ParameterElement[] parameters = computeCorrespondingParameters(argumentList, staticElement);
      if (parameters != null) {
        argumentList.setCorrespondingStaticParameters(parameters);
      }
    }
    if (propagatedElement != null) {
      ParameterElement[] parameters = computeCorrespondingParameters(
          argumentList,
          propagatedElement);
      if (parameters != null) {
        argumentList.setCorrespondingPropagatedParameters(parameters);
      }
    }
    //
    // Then check for error conditions.
    //
    ErrorCode errorCode = checkForInvocationError(target, true, staticElement);
    boolean generatedWithTypePropagation = false;
    if (enableHints && errorCode == null && staticElement == null) {
      // The method lookup may have failed because there were multiple
      // incompatible choices. In this case we don't want to generate a hint.
      if (propagatedElement == null && propagatedType instanceof UnionType) {
        // TODO(collinsn): an improvement here is to make the propagated type of the method call
        // the union of the propagated types of all possible calls.
        if (lookupMethods(target, (UnionType) propagatedType, methodName.getName()).size() > 1) {
          return null;
        }
      }

      errorCode = checkForInvocationError(target, false, propagatedElement);
      if (errorCode == StaticTypeWarningCode.UNDEFINED_METHOD) {
        ClassElement classElementContext = null;
        if (target == null) {
          classElementContext = resolver.getEnclosingClass();
        } else {
          Type type = target.getBestType();
          if (type != null) {
            if (type.getElement() instanceof ClassElement) {
              classElementContext = (ClassElement) type.getElement();
            }
          }
        }
        if (classElementContext != null) {
          subtypeManager.ensureLibraryVisited(definingLibrary);
          HashSet<ClassElement> subtypeElements = subtypeManager.computeAllSubtypes(classElementContext);
          for (ClassElement subtypeElement : subtypeElements) {
            if (subtypeElement.getMethod(methodName.getName()) != null) {
              errorCode = null;
            }
          }
        }
      }
      generatedWithTypePropagation = true;
    }
    if (errorCode == null) {
      return null;
    }
    if (errorCode == StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION) {
      resolver.reportErrorForNode(
          StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION,
          methodName,
          methodName.getName());
    } else if (errorCode == StaticTypeWarningCode.UNDEFINED_FUNCTION) {
      resolver.reportErrorForNode(
          StaticTypeWarningCode.UNDEFINED_FUNCTION,
          methodName,
          methodName.getName());
    } else if (errorCode == StaticTypeWarningCode.UNDEFINED_METHOD) {
      String targetTypeName;
      if (target == null) {
        ClassElement enclosingClass = resolver.getEnclosingClass();
        targetTypeName = enclosingClass.getDisplayName();
        ErrorCode proxyErrorCode = generatedWithTypePropagation ? HintCode.UNDEFINED_METHOD
            : StaticTypeWarningCode.UNDEFINED_METHOD;
        if (doesntHaveProxy(resolver.getEnclosingClass())) {
          resolver.reportErrorForNode(
              proxyErrorCode,
              methodName,
              methodName.getName(),
              targetTypeName);
        }

      } else {
        // ignore Function "call"
        // (if we are about to create a hint using type propagation, then we can use type
        // propagation here as well)
        Type targetType = null;
        if (!generatedWithTypePropagation) {
          targetType = getStaticType(target);
        } else {
          // choose the best type
          targetType = getPropagatedType(target);
          if (targetType == null) {
            targetType = getStaticType(target);
          }
        }
        if (targetType != null && targetType.isDartCoreFunction()
            && methodName.getName().equals(FunctionElement.CALL_METHOD_NAME)) {
          // TODO(brianwilkerson) Can we ever resolve the function being invoked?
          //resolveArgumentsToParameters(node.getArgumentList(), invokedFunction);
          return null;
        }
        targetTypeName = targetType == null ? null : targetType.getDisplayName();
        ErrorCode proxyErrorCode = generatedWithTypePropagation ? HintCode.UNDEFINED_METHOD
            : StaticTypeWarningCode.UNDEFINED_METHOD;
        if (doesntHaveProxy(targetType.getElement())) {
          resolver.reportErrorForNode(
              proxyErrorCode,
              methodName,
              methodName.getName(),
              targetTypeName);
        }
      }
    } else if (errorCode == StaticTypeWarningCode.UNDEFINED_SUPER_METHOD) {
      // Generate the type name.
      // The error code will never be generated via type propagation
      Type targetType = getStaticType(target);
      if (targetType instanceof InterfaceType && !targetType.isObject()) {
        targetType = ((InterfaceType) targetType).getSuperclass();
      }
      String targetTypeName = targetType == null ? null : targetType.getName();
      resolver.reportErrorForNode(
          StaticTypeWarningCode.UNDEFINED_SUPER_METHOD,
          methodName,
          methodName.getName(),
          targetTypeName);
View Full Code Here

  @Override
  public Void visitPostfixExpression(PostfixExpression node) {
    Expression operand = node.getOperand();
    String methodName = getPostfixOperator(node);

    Type staticType = getStaticType(operand);
    MethodElement staticMethod = lookUpMethod(operand, staticType, methodName);
    node.setStaticElement(staticMethod);

    Type propagatedType = getPropagatedType(operand);
    MethodElement propagatedMethod = lookUpMethod(operand, propagatedType, methodName);
    node.setPropagatedElement(propagatedMethod);

    if (shouldReportMissingMember(staticType, staticMethod)) {
      if (doesntHaveProxy(staticType.getElement())) {
        resolver.reportErrorForToken(
            StaticTypeWarningCode.UNDEFINED_OPERATOR,
            node.getOperator(),
            methodName,
            staticType.getDisplayName());
      }
    } else if (enableHints && shouldReportMissingMember(propagatedType, propagatedMethod)
        && !memberFoundInSubclass(propagatedType.getElement(), methodName, true, false)) {
      if (doesntHaveProxy(propagatedType.getElement())) {
        resolver.reportErrorForToken(
            HintCode.UNDEFINED_OPERATOR,
            node.getOperator(),
            methodName,
            propagatedType.getDisplayName());
      }
    }
    return null;
  }
View Full Code Here

    if (operatorType.isUserDefinableOperator() || operatorType == TokenType.PLUS_PLUS
        || operatorType == TokenType.MINUS_MINUS) {
      Expression operand = node.getOperand();
      String methodName = getPrefixOperator(node);

      Type staticType = getStaticType(operand);
      MethodElement staticMethod = lookUpMethod(operand, staticType, methodName);
      node.setStaticElement(staticMethod);

      Type propagatedType = getPropagatedType(operand);
      MethodElement propagatedMethod = lookUpMethod(operand, propagatedType, methodName);
      node.setPropagatedElement(propagatedMethod);

      if (shouldReportMissingMember(staticType, staticMethod)) {
        if (doesntHaveProxy(staticType.getElement())) {
          resolver.reportErrorForToken(
              StaticTypeWarningCode.UNDEFINED_OPERATOR,
              operator,
              methodName,
              staticType.getDisplayName());
        }
      } else if (enableHints && shouldReportMissingMember(propagatedType, propagatedMethod)
          && !memberFoundInSubclass(propagatedType.getElement(), methodName, true, false)) {
        if (doesntHaveProxy(propagatedType.getElement())) {
          resolver.reportErrorForToken(
              HintCode.UNDEFINED_OPERATOR,
              operator,
              methodName,
              propagatedType.getDisplayName());
        }
      }
    }
    return null;
  }
View Full Code Here

      // This is really a function expression invocation.
      //
      // TODO(brianwilkerson) Consider the possibility of re-writing the AST.
      FunctionType getterType = ((PropertyAccessorElement) element).getType();
      if (getterType != null) {
        Type returnType = getterType.getReturnType();
        if (!isExecutableType(returnType)) {
          return StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION;
        }
      }
    } else if (element instanceof ExecutableElement) {
      return null;
    } else if (element instanceof MultiplyDefinedElement) {
      // The error has already been reported
      return null;
    } else if (element == null && target instanceof SuperExpression) {
      // TODO(jwren) We should split the UNDEFINED_METHOD into two error codes, this one, and
      // a code that describes the situation where the method was found, but it was not
      // accessible from the current library.
      return StaticTypeWarningCode.UNDEFINED_SUPER_METHOD;
    } else {
      //
      // This is really a function expression invocation.
      //
      // TODO(brianwilkerson) Consider the possibility of re-writing the AST.
      if (element instanceof PropertyInducingElement) {
        PropertyAccessorElement getter = ((PropertyInducingElement) element).getGetter();
        FunctionType getterType = getter.getType();
        if (getterType != null) {
          Type returnType = getterType.getReturnType();
          if (!isExecutableType(returnType)) {
            return StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION;
          }
        }
      } else if (element instanceof VariableElement) {
        Type variableType = ((VariableElement) element).getType();
        if (!isExecutableType(variableType)) {
          return StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION;
        }
      } else {
        if (target == null) {
          ClassElement enclosingClass = resolver.getEnclosingClass();
          if (enclosingClass == null) {
            return StaticTypeWarningCode.UNDEFINED_FUNCTION;
          } else if (element == null) {
            // Proxy-conditional warning, based on state of resolver.getEnclosingClass()
            return StaticTypeWarningCode.UNDEFINED_METHOD;
          } else {
            return StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION;
          }
        } else {
          Type targetType;
          if (useStaticContext) {
            targetType = getStaticType(target);
          } else {
            // Compute and use the propagated type, if it is null, then it may be the case that
            // static type is some type, in which the static type should be used.
            targetType = target.getBestType();
          }
          if (targetType == null) {
            return StaticTypeWarningCode.UNDEFINED_FUNCTION;
          } else if (!targetType.isDynamic() && !targetType.isBottom()) {
            // Proxy-conditional warning, based on state of targetType.getElement()
            return StaticTypeWarningCode.UNDEFINED_METHOD;
          }
        }
      }
View Full Code Here

      //
      // This is an invocation of the call method defined on the value returned by the getter.
      //
      FunctionType getterType = ((PropertyAccessorElement) element).getType();
      if (getterType != null) {
        Type getterReturnType = getterType.getReturnType();
        if (getterReturnType instanceof InterfaceType) {
          MethodElement callMethod = ((InterfaceType) getterReturnType).lookUpMethod(
              FunctionElement.CALL_METHOD_NAME,
              definingLibrary);
          if (callMethod != null) {
            return resolveArgumentsToFunction(false, argumentList, callMethod);
          }
        } else if (getterReturnType instanceof FunctionType) {
          ParameterElement[] parameters = ((FunctionType) getterReturnType).getParameters();
          return resolveArgumentsToParameters(false, argumentList, parameters);
        }
      }
    } else if (element instanceof ExecutableElement) {
      return resolveArgumentsToFunction(false, argumentList, (ExecutableElement) element);
    } else if (element instanceof VariableElement) {
      VariableElement variable = (VariableElement) element;
      Type type = promoteManager.getStaticType(variable);
      if (type instanceof FunctionType) {
        FunctionType functionType = (FunctionType) type;
        ParameterElement[] parameters = functionType.getParameters();
        return resolveArgumentsToParameters(false, argumentList, parameters);
      } else if (type instanceof InterfaceType) {
View Full Code Here

   *
   * @param expression the expression whose type is to be returned
   * @return the type of the given expression
   */
  private Type getPropagatedType(Expression expression) {
    Type propagatedType = resolveTypeParameter(expression.getPropagatedType());
    if (propagatedType instanceof FunctionType) {
      //
      // All function types are subtypes of 'Function', which is itself a subclass of 'Object'.
      //
      propagatedType = resolver.getTypeProvider().getFunctionType();
View Full Code Here

   */
  private Type getStaticType(Expression expression) {
    if (expression instanceof NullLiteral) {
      return resolver.getTypeProvider().getBottomType();
    }
    Type staticType = resolveTypeParameter(expression.getStaticType());
    if (staticType instanceof FunctionType) {
      //
      // All function types are subtypes of 'Function', which is itself a subclass of 'Object'.
      //
      staticType = resolver.getTypeProvider().getFunctionType();
View Full Code Here

    }
    return memberElement;
  }

  private void resolvePropertyAccess(Expression target, SimpleIdentifier propertyName) {
    Type staticType = getStaticType(target);
    Type propagatedType = getPropagatedType(target);

    Element staticElement = null;
    Element propagatedElement = null;

    //
    // If this property access is of the form 'C.m' where 'C' is a class, then we don't call
    // resolveProperty(..) which walks up the class hierarchy, instead we just look for the
    // member in the type only.
    //
    ClassElementImpl typeReference = getTypeReference(target);
    if (typeReference != null) {
      // TODO(brianwilkerson) Why are we setting the propagated element here? It looks wrong.
      staticElement = propagatedElement = resolveElement(typeReference, propertyName);
    } else {
      staticElement = resolveProperty(target, staticType, propertyName);
      propagatedElement = resolveProperty(target, propagatedType, propertyName);
    }

    // May be part of annotation, record property element only if exists.
    // Error was already reported in validateAnnotationElement().
    if (target.getParent().getParent() instanceof Annotation) {
      if (staticElement != null) {
        propertyName.setStaticElement(staticElement);
      }
      return;
    }

    propertyName.setStaticElement(staticElement);
    propertyName.setPropagatedElement(propagatedElement);

    boolean shouldReportMissingMember_static = shouldReportMissingMember(staticType, staticElement);
    boolean shouldReportMissingMember_propagated = !shouldReportMissingMember_static && enableHints
        && shouldReportMissingMember(propagatedType, propagatedElement) &&
        // If we are about to generate the hint (propagated version of this warning), then check
        // that the member is not in a subtype of the propagated type.
        !memberFoundInSubclass(propagatedType.getElement(), propertyName.getName(), false, true);

    // TODO(collinsn): add support for errors on union types by extending
    // [lookupGetter] and [lookupSetter] in analogy with the earlier [lookupMethod] extensions.
    if (propagatedType instanceof UnionType) {
      shouldReportMissingMember_propagated = false;
    }

    if (shouldReportMissingMember_static || shouldReportMissingMember_propagated) {
      Element staticOrPropagatedEnclosingElt = shouldReportMissingMember_static
          ? staticType.getElement() : propagatedType.getElement();
      boolean isStaticProperty = isStatic(staticOrPropagatedEnclosingElt);
      String displayName = staticOrPropagatedEnclosingElt != null
          ? staticOrPropagatedEnclosingElt.getDisplayName() : propagatedType != null
              ? propagatedType.getDisplayName() : staticType.getDisplayName();

      // Special getter cases.
      if (propertyName.inGetterContext()) {
        if (!isStaticProperty && staticOrPropagatedEnclosingElt instanceof ClassElement) {
          ClassElement classElement = (ClassElement) staticOrPropagatedEnclosingElt;
View Full Code Here

   * @return the type that should be used in place of the argument if it is a type parameter, or the
   *         original argument if it isn't a type parameter
   */
  private Type resolveTypeParameter(Type type) {
    if (type instanceof TypeParameterType) {
      Type bound = ((TypeParameterType) type).getElement().getBound();
      if (bound == null) {
        return resolver.getTypeProvider().getObjectType();
      }
      return bound;
    }
View Full Code Here

TOP

Related Classes of com.google.dart.engine.type.Type

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.