コード例 #1
0
  private List<ValueParameterDescriptor> createValueParameterDescriptors(
      ExpressionTypingContext context,
      JetFunctionLiteral functionLiteral,
      FunctionDescriptorImpl functionDescriptor,
      boolean functionTypeExpected) {
    List<ValueParameterDescriptor> valueParameterDescriptors = Lists.newArrayList();
    List<JetParameter> declaredValueParameters = functionLiteral.getValueParameters();

    List<ValueParameterDescriptor> expectedValueParameters =
        (functionTypeExpected)
            ? JetStandardClasses.getValueParameters(functionDescriptor, context.expectedType)
            : null;

    boolean hasDeclaredValueParameters = functionLiteral.getValueParameterList() != null;
    if (functionTypeExpected
        && !hasDeclaredValueParameters
        && expectedValueParameters.size() == 1) {
      ValueParameterDescriptor valueParameterDescriptor = expectedValueParameters.get(0);
      ValueParameterDescriptor it =
          new ValueParameterDescriptorImpl(
              functionDescriptor,
              0,
              Collections.<AnnotationDescriptor>emptyList(),
              "it",
              false,
              valueParameterDescriptor.getOutType(),
              valueParameterDescriptor.hasDefaultValue(),
              valueParameterDescriptor.getVarargElementType());
      valueParameterDescriptors.add(it);
      context.trace.record(AUTO_CREATED_IT, it);
    } else {
      for (int i = 0; i < declaredValueParameters.size(); i++) {
        JetParameter declaredParameter = declaredValueParameters.get(i);
        JetTypeReference typeReference = declaredParameter.getTypeReference();

        JetType type;
        if (typeReference != null) {
          type = context.getTypeResolver().resolveType(context.scope, typeReference);
        } else {
          if (expectedValueParameters != null && i < expectedValueParameters.size()) {
            type = expectedValueParameters.get(i).getOutType();
          } else {
            context.trace.report(CANNOT_INFER_PARAMETER_TYPE.on(declaredParameter));
            type = ErrorUtils.createErrorType("Cannot be inferred");
          }
        }
        ValueParameterDescriptor valueParameterDescriptor =
            context
                .getDescriptorResolver()
                .resolveValueParameterDescriptor(functionDescriptor, declaredParameter, i, type);
        valueParameterDescriptors.add(valueParameterDescriptor);
      }
    }
    return valueParameterDescriptors;
  }
コード例 #2
0
  @Override
  public PsiJetParameterStub createStub(@NotNull JetParameter psi, StubElement parentStub) {
    JetTypeReference typeReference = psi.getTypeReference();
    JetExpression defaultValue = psi.getDefaultValue();

    return new PsiJetParameterStubImpl(
        parentStub,
        psi.getFqName(),
        psi.getName(),
        psi.isMutable(),
        psi.isVarArg(),
        typeReference != null ? typeReference.getText() : null,
        defaultValue != null ? defaultValue.getText() : null);
  }
コード例 #3
0
  @NotNull
  private static ValueParameterDescriptor createValueParameterDescriptor(
      @NotNull ExpressionTypingContext context,
      @NotNull FunctionDescriptorImpl functionDescriptor,
      @NotNull List<JetParameter> declaredValueParameters,
      @Nullable List<ValueParameterDescriptor> expectedValueParameters,
      int index) {
    JetParameter declaredParameter = declaredValueParameters.get(index);
    JetTypeReference typeReference = declaredParameter.getTypeReference();

    JetType expectedType;
    if (expectedValueParameters != null && index < expectedValueParameters.size()) {
      expectedType = expectedValueParameters.get(index).getType();
    } else {
      expectedType = null;
    }
    JetType type;
    if (typeReference != null) {
      type =
          context
              .expressionTypingServices
              .getTypeResolver()
              .resolveType(context.scope, typeReference, context.trace, true);
      if (expectedType != null) {
        if (!JetTypeChecker.INSTANCE.isSubtypeOf(expectedType, type)) {
          context.trace.report(
              EXPECTED_PARAMETER_TYPE_MISMATCH.on(declaredParameter, expectedType));
        }
      }
    } else {
      if (expectedType == null
          || expectedType == DONT_CARE
          || expectedType == CANT_INFER_TYPE_PARAMETER) {
        context.trace.report(CANNOT_INFER_PARAMETER_TYPE.on(declaredParameter));
      }
      if (expectedType != null) {
        type = expectedType;
      } else {
        type = CANT_INFER_LAMBDA_PARAM_TYPE;
      }
    }
    return context
        .expressionTypingServices
        .getDescriptorResolver()
        .resolveValueParameterDescriptorWithAnnotationArguments(
            context.scope, functionDescriptor, declaredParameter, index, type, context.trace);
  }
コード例 #4
0
  private void computeValueParameters(
      @NotNull List<ValueParameterDescriptor> parameterDescriptors) {
    if (parameterDescriptors.size() != altFunDeclaration.getValueParameters().size()) {
      throw new AlternativeSignatureMismatchException(
          "Method signature has %d value parameters, but alternative signature has %d",
          parameterDescriptors.size(), altFunDeclaration.getValueParameters().size());
    }

    List<ValueParameterDescriptor> altParamDescriptors = new ArrayList<ValueParameterDescriptor>();
    for (int i = 0, size = parameterDescriptors.size(); i < size; i++) {
      ValueParameterDescriptor originalParameterDescriptor = parameterDescriptors.get(i);
      JetParameter annotationValueParameter = altFunDeclaration.getValueParameters().get(i);

      //noinspection ConstantConditions
      JetTypeElement alternativeTypeElement =
          annotationValueParameter.getTypeReference().getTypeElement();
      assert alternativeTypeElement != null;

      JetType alternativeType;
      JetType alternativeVarargElementType;

      JetType originalParamVarargElementType = originalParameterDescriptor.getVarargElementType();
      if (originalParamVarargElementType == null) {
        if (annotationValueParameter.isVarArg()) {
          throw new AlternativeSignatureMismatchException(
              "Parameter in method signature is not vararg, but in alternative signature it is vararg");
        }

        alternativeType =
            TypeTransformingVisitor.computeType(
                alternativeTypeElement,
                originalParameterDescriptor.getType(),
                originalToAltTypeParameters,
                MEMBER_SIGNATURE_CONTRAVARIANT);
        alternativeVarargElementType = null;
      } else {
        if (!annotationValueParameter.isVarArg()) {
          throw new AlternativeSignatureMismatchException(
              "Parameter in method signature is vararg, but in alternative signature it is not");
        }

        alternativeVarargElementType =
            TypeTransformingVisitor.computeType(
                alternativeTypeElement, originalParamVarargElementType,
                originalToAltTypeParameters, MEMBER_SIGNATURE_CONTRAVARIANT);
        alternativeType = KotlinBuiltIns.getInstance().getArrayType(alternativeVarargElementType);
      }

      Name altName = annotationValueParameter.getNameAsName();

      altParamDescriptors.add(
          new ValueParameterDescriptorImpl(
              originalParameterDescriptor.getContainingDeclaration(),
              null,
              originalParameterDescriptor.getIndex(),
              originalParameterDescriptor.getAnnotations(),
              altName != null ? altName : originalParameterDescriptor.getName(),
              alternativeType,
              originalParameterDescriptor.declaresDefaultValue(),
              alternativeVarargElementType));
    }

    altValueParameters = altParamDescriptors;
  }