@Nullable
 public static List<ValueParameterDescriptor> getSubstitutedValueParameters(
     FunctionDescriptor substitutedDescriptor,
     @NotNull FunctionDescriptor functionDescriptor,
     @NotNull TypeSubstitutor substitutor) {
   List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
   List<ValueParameterDescriptor> unsubstitutedValueParameters =
       functionDescriptor.getValueParameters();
   for (int i = 0, unsubstitutedValueParametersSize = unsubstitutedValueParameters.size();
       i < unsubstitutedValueParametersSize;
       i++) {
     ValueParameterDescriptor unsubstitutedValueParameter = unsubstitutedValueParameters.get(i);
     // TODO : Lazy?
     JetType substitutedType =
         substitutor.substitute(unsubstitutedValueParameter.getType(), Variance.IN_VARIANCE);
     JetType varargElementType = unsubstitutedValueParameter.getVarargElementType();
     JetType substituteVarargElementType =
         varargElementType == null
             ? null
             : substitutor.substitute(varargElementType, Variance.IN_VARIANCE);
     if (substitutedType == null) return null;
     result.add(
         new ValueParameterDescriptorImpl(
             substitutedDescriptor,
             unsubstitutedValueParameter,
             unsubstitutedValueParameter.getAnnotations(),
             unsubstitutedValueParameter.isVar(),
             substitutedType,
             substituteVarargElementType));
   }
   return result;
 }
  @NotNull
  public static SimpleFunctionDescriptor createSamAdapterFunction(
      @NotNull SimpleFunctionDescriptor original) {
    SimpleFunctionDescriptorImpl result =
        new SimpleFunctionDescriptorImpl(
            original.getContainingDeclaration(),
            original.getAnnotations(),
            original.getName(),
            CallableMemberDescriptor.Kind.SYNTHESIZED);

    TypeParameters typeParameters =
        recreateAndInitializeTypeParameters(original.getTypeParameters(), result);

    JetType returnTypeUnsubstituted = original.getReturnType();
    assert returnTypeUnsubstituted != null : original;
    JetType returnType =
        typeParameters.substitutor.substitute(returnTypeUnsubstituted, Variance.OUT_VARIANCE);
    assert returnType != null
        : "couldn't substitute type: "
            + returnType
            + ", substitutor = "
            + typeParameters.substitutor;

    List<ValueParameterDescriptor> valueParameters = Lists.newArrayList();
    for (ValueParameterDescriptor originalParam : original.getValueParameters()) {
      JetType originalType = originalParam.getType();
      JetType newTypeUnsubstituted =
          isSamType(originalType) ? getFunctionTypeForSamType(originalType) : originalType;
      JetType newType =
          typeParameters.substitutor.substitute(newTypeUnsubstituted, Variance.IN_VARIANCE);
      assert newType != null
          : "couldn't substitute type: "
              + newTypeUnsubstituted
              + ", substitutor = "
              + typeParameters.substitutor;

      ValueParameterDescriptor newParam =
          new ValueParameterDescriptorImpl(
              result,
              originalParam.getIndex(),
              originalParam.getAnnotations(),
              originalParam.getName(),
              newType,
              false,
              null);
      valueParameters.add(newParam);
    }

    result.initialize(
        null,
        original.getExpectedThisObject(),
        typeParameters.descriptors,
        valueParameters,
        returnType,
        original.getModality(),
        original.getVisibility(),
        false);

    return result;
  }
Exemplo n.º 3
0
  private JavaDescriptorResolver.ValueParameterDescriptors
      modifyValueParametersAccordingToSuperMethods(
          @NotNull
              JavaDescriptorResolver.ValueParameterDescriptors
                  parameters // descriptors built by parameters resolver
          ) {
    // we are not processing receiver type specifically:
    // if this function comes from Kotlin, then we don't need to do it, if it doesn't, then it can't
    // have receiver

    List<ValueParameterDescriptor> resultParameters = Lists.newArrayList();

    for (ValueParameterDescriptor originalParam : parameters.getDescriptors()) {
      final int index = originalParam.getIndex();
      List<TypeAndVariance> typesFromSuperMethods =
          ContainerUtil.map(
              superFunctions,
              new Function<FunctionDescriptor, TypeAndVariance>() {
                @Override
                public TypeAndVariance fun(FunctionDescriptor superFunction) {
                  return new TypeAndVariance(
                      superFunction.getValueParameters().get(index).getType(), INVARIANT);
                }
              });

      VarargCheckResult varargCheckResult = checkVarargInSuperFunctions(originalParam);

      JetType altType =
          modifyTypeAccordingToSuperMethods(
              varargCheckResult.parameterType,
              typesFromSuperMethods,
              MEMBER_SIGNATURE_CONTRAVARIANT);

      resultParameters.add(
          new ValueParameterDescriptorImpl(
              originalParam.getContainingDeclaration(),
              index,
              originalParam.getAnnotations(),
              originalParam.getName(),
              altType,
              originalParam.declaresDefaultValue(),
              varargCheckResult.isVararg
                  ? KotlinBuiltIns.getInstance().getArrayElementType(altType)
                  : null));
    }

    JetType originalReceiverType = parameters.getReceiverType();
    if (originalReceiverType != null) {
      JetType substituted =
          SignaturesUtil.createSubstitutorForFunctionTypeParameters(autoTypeParameterToModified)
              .substitute(originalReceiverType, INVARIANT);
      assert substituted != null;
      return new JavaDescriptorResolver.ValueParameterDescriptors(substituted, resultParameters);
    } else {
      return new JavaDescriptorResolver.ValueParameterDescriptors(null, resultParameters);
    }
  }
  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;
  }