Esempio n. 1
0
  @NotNull
  public Collection<JetType> getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor receiverParameter = descriptor.getReceiverParameter();

    List<TypeProjection> typeArguments = new ArrayList<TypeProjection>(2);

    ClassDescriptor classDescriptor;
    if (receiverParameter != null) {
      classDescriptor = extensionFunctionImpl;
      typeArguments.add(new TypeProjectionImpl(receiverParameter.getType()));
    } else {
      classDescriptor = functionImpl;
    }

    //noinspection ConstantConditions
    typeArguments.add(new TypeProjectionImpl(descriptor.getReturnType()));

    JetType functionImplType =
        new JetTypeImpl(
            classDescriptor.getDefaultType().getAnnotations(),
            classDescriptor.getTypeConstructor(),
            false,
            typeArguments,
            classDescriptor.getMemberScope(typeArguments));

    JetType functionType =
        KotlinBuiltIns.getInstance()
            .getFunctionType(
                Annotations.EMPTY,
                receiverParameter == null ? null : receiverParameter.getType(),
                DescriptorUtils.getValueParametersTypes(descriptor.getValueParameters()),
                descriptor.getReturnType());

    return Arrays.asList(functionImplType, functionType);
  }
Esempio n. 2
0
  /* FUNCTIONS */
  private void renderFunction(
      @NotNull FunctionDescriptor function, @NotNull StringBuilder builder) {
    if (!startFromName) {
      renderAnnotations(function, builder);
      renderVisibility(function.getVisibility(), builder);
      renderModalityForCallable(function, builder);
      renderOverride(function, builder);
      renderMemberKind(function, builder);

      builder.append(renderKeyword("fun")).append(" ");
      renderTypeParameters(function.getTypeParameters(), builder, true);

      ReceiverParameterDescriptor receiver = function.getReceiverParameter();
      if (receiver != null) {
        builder.append(escape(renderType(receiver.getType()))).append(".");
      }
    }

    renderName(function, builder);
    renderValueParameters(function, builder);
    JetType returnType = function.getReturnType();
    if (unitReturnType || !KotlinBuiltIns.getInstance().isUnit(returnType)) {
      builder.append(": ").append(returnType == null ? "[NULL]" : escape(renderType(returnType)));
    }
    renderWhereSuffix(function.getTypeParameters(), builder);
  }
Esempio n. 3
0
  private void generateParameterAnnotations(
      @NotNull FunctionDescriptor functionDescriptor,
      @NotNull MethodVisitor mv,
      @NotNull JvmMethodSignature jvmSignature) {
    Iterator<ValueParameterDescriptor> iterator =
        functionDescriptor.getValueParameters().iterator();
    List<JvmMethodParameterSignature> kotlinParameterTypes = jvmSignature.getValueParameters();

    for (int i = 0; i < kotlinParameterTypes.size(); i++) {
      JvmMethodParameterSignature parameterSignature = kotlinParameterTypes.get(i);
      JvmMethodParameterKind kind = parameterSignature.getKind();
      if (kind.isSkippedInGenericSignature()) {
        markEnumOrInnerConstructorParameterAsSynthetic(mv, i);
        continue;
      }

      if (kind == JvmMethodParameterKind.VALUE) {
        ValueParameterDescriptor parameter = iterator.next();
        if (parameter.getIndex() != i) {
          v.getSerializationBindings().put(INDEX_FOR_VALUE_PARAMETER, parameter, i);
        }
        AnnotationCodegen annotationCodegen = AnnotationCodegen.forParameter(i, mv, typeMapper);

        if (functionDescriptor instanceof PropertySetterDescriptor) {
          PropertyDescriptor propertyDescriptor =
              ((PropertySetterDescriptor) functionDescriptor).getCorrespondingProperty();
          Annotated targetedAnnotations =
              new AnnotatedWithOnlyTargetedAnnotations(propertyDescriptor);
          annotationCodegen.genAnnotations(
              targetedAnnotations, parameterSignature.getAsmType(), SETTER_PARAMETER);
        }

        if (functionDescriptor instanceof ConstructorDescriptor) {
          annotationCodegen.genAnnotations(
              parameter, parameterSignature.getAsmType(), CONSTRUCTOR_PARAMETER);
        } else {
          annotationCodegen.genAnnotations(parameter, parameterSignature.getAsmType());
        }
      } else if (kind == JvmMethodParameterKind.RECEIVER) {
        ReceiverParameterDescriptor receiver =
            ((functionDescriptor instanceof PropertyAccessorDescriptor)
                    ? ((PropertyAccessorDescriptor) functionDescriptor).getCorrespondingProperty()
                    : functionDescriptor)
                .getExtensionReceiverParameter();
        if (receiver != null) {
          AnnotationCodegen annotationCodegen = AnnotationCodegen.forParameter(i, mv, typeMapper);
          Annotated targetedAnnotations =
              new AnnotatedWithOnlyTargetedAnnotations(receiver.getType());
          annotationCodegen.genAnnotations(
              targetedAnnotations, parameterSignature.getAsmType(), RECEIVER);
        }
      }
    }
  }
Esempio n. 4
0
 static List<KotlinType> compiledValueParameters(CallableDescriptor callableDescriptor) {
   ReceiverParameterDescriptor receiverParameter =
       callableDescriptor.getExtensionReceiverParameter();
   List<KotlinType> parameters = new ArrayList<KotlinType>();
   if (receiverParameter != null) {
     parameters.add(receiverParameter.getType());
   }
   for (ValueParameterDescriptor valueParameterDescriptor :
       callableDescriptor.getValueParameters()) {
     parameters.add(valueParameterDescriptor.getType());
   }
   return parameters;
 }
Esempio n. 5
0
  @NotNull
  public Collection<KotlinType> getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();

    //noinspection ConstantConditions
    KotlinType functionType =
        DescriptorUtilsKt.getBuiltIns(descriptor)
            .getFunctionType(
                Annotations.Companion.getEMPTY(),
                receiverParameter == null ? null : receiverParameter.getType(),
                ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()),
                descriptor.getReturnType());

    return Arrays.asList(lambda.getDefaultType(), functionType);
  }
Esempio n. 6
0
  private void generateBridge(@NotNull Method bridge, @NotNull Method delegate) {
    if (bridge.equals(delegate)) return;

    MethodVisitor mv =
        v.newMethod(
            OtherOrigin(element, funDescriptor),
            ACC_PUBLIC | ACC_BRIDGE,
            bridge.getName(),
            bridge.getDescriptor(),
            null,
            ArrayUtil.EMPTY_STRING_ARRAY);

    if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return;

    mv.visitCode();

    InstructionAdapter iv = new InstructionAdapter(mv);
    ImplementationBodyCodegen.markLineNumberForSyntheticFunction(
        DescriptorUtils.getParentOfType(funDescriptor, ClassDescriptor.class), iv);

    iv.load(0, asmType);

    ReceiverParameterDescriptor receiver = funDescriptor.getExtensionReceiverParameter();
    int count = 1;
    if (receiver != null) {
      StackValue.local(count, bridge.getArgumentTypes()[count - 1])
          .put(typeMapper.mapType(receiver.getType()), iv);
      count++;
    }

    List<ValueParameterDescriptor> params = funDescriptor.getValueParameters();
    for (ValueParameterDescriptor param : params) {
      StackValue.local(count, bridge.getArgumentTypes()[count - 1])
          .put(typeMapper.mapType(param.getType()), iv);
      count++;
    }

    iv.invokevirtual(
        asmType.getInternalName(), delegate.getName(), delegate.getDescriptor(), false);
    StackValue.onStack(delegate.getReturnType()).put(bridge.getReturnType(), iv);

    iv.areturn(bridge.getReturnType());

    FunctionCodegen.endVisit(mv, "bridge", element);
  }
Esempio n. 7
0
  @NotNull
  public Collection<JetType> getSupertypesForCallableReference(
      @NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor receiverParameter = descriptor.getReceiverParameter();
    ReceiverParameterDescriptor expectedThisObject = descriptor.getExpectedThisObject();

    List<TypeProjection> typeArguments = new ArrayList<TypeProjection>(2);

    ClassDescriptor classDescriptor;
    JetType receiverType;
    if (receiverParameter != null) {
      classDescriptor = kExtensionFunctionImpl;
      receiverType = receiverParameter.getType();
      typeArguments.add(new TypeProjectionImpl(receiverType));
    } else if (expectedThisObject != null) {
      classDescriptor = kMemberFunctionImpl;
      receiverType = expectedThisObject.getType();
      typeArguments.add(new TypeProjectionImpl(receiverType));
    } else {
      classDescriptor = kFunctionImpl;
      receiverType = null;
    }

    //noinspection ConstantConditions
    typeArguments.add(new TypeProjectionImpl(descriptor.getReturnType()));

    JetType kFunctionImplType =
        new JetTypeImpl(
            classDescriptor.getDefaultType().getAnnotations(),
            classDescriptor.getTypeConstructor(),
            false,
            typeArguments,
            classDescriptor.getMemberScope(typeArguments));

    JetType kFunctionType =
        reflectionTypes.getKFunctionType(
            Annotations.EMPTY,
            receiverType,
            DescriptorUtils.getValueParametersTypes(descriptor.getValueParameters()),
            descriptor.getReturnType(),
            receiverParameter != null);

    return Arrays.asList(kFunctionImplType, kFunctionType);
  }
Esempio n. 8
0
 @Nullable
 private static Type getThisTypeForFunction(
     @NotNull FunctionDescriptor functionDescriptor,
     @NotNull MethodContext context,
     @NotNull JetTypeMapper typeMapper) {
   ReceiverParameterDescriptor dispatchReceiver =
       functionDescriptor.getDispatchReceiverParameter();
   if (functionDescriptor instanceof ConstructorDescriptor) {
     return typeMapper.mapType(functionDescriptor);
   } else if (dispatchReceiver != null) {
     return typeMapper.mapType(dispatchReceiver.getType());
   } else if (isFunctionLiteral(functionDescriptor)
       || isLocalFunction(functionDescriptor)
       || isFunctionExpression(functionDescriptor)) {
     return typeMapper.mapType(context.getThisDescriptor());
   } else {
     return null;
   }
 }
Esempio n. 9
0
  @NotNull
  public Collection<KotlinType> getSupertypesForFunctionReference(
      @NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter();
    ReceiverParameterDescriptor dispatchReceiver = descriptor.getDispatchReceiverParameter();

    KotlinType receiverType =
        extensionReceiver != null
            ? extensionReceiver.getType()
            : dispatchReceiver != null ? dispatchReceiver.getType() : null;

    //noinspection ConstantConditions
    KotlinType functionType =
        DescriptorUtilsKt.getBuiltIns(descriptor)
            .getFunctionType(
                Annotations.Companion.getEMPTY(),
                receiverType,
                ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()),
                descriptor.getReturnType());

    return Arrays.asList(functionReference.getDefaultType(), functionType);
  }
Esempio n. 10
0
  private void renderProperty(
      @NotNull PropertyDescriptor property, @NotNull StringBuilder builder) {
    if (!startFromName) {
      renderAnnotations(property, builder);
      renderVisibility(property.getVisibility(), builder);
      renderModalityForCallable(property, builder);
      renderOverride(property, builder);
      renderMemberKind(property, builder);

      renderValVarPrefix(property, builder);
    }

    renderTypeParameters(property.getTypeParameters(), builder, true);

    ReceiverParameterDescriptor receiver = property.getReceiverParameter();
    if (receiver != null) {
      builder.append(escape(renderType(receiver.getType()))).append(".");
    }
    renderName(property, builder);
    builder.append(": ").append(escape(renderType(property.getType())));

    renderWhereSuffix(property.getTypeParameters(), builder);
  }
Esempio n. 11
0
  public static boolean containsErrorType(@NotNull FunctionDescriptor function) {
    if (containsErrorType(function.getReturnType())) {
      return true;
    }
    ReceiverParameterDescriptor receiverParameter = function.getReceiverParameter();
    if (receiverParameter != null && containsErrorType(receiverParameter.getType())) {
      return true;
    }
    for (ValueParameterDescriptor parameter : function.getValueParameters()) {
      if (containsErrorType(parameter.getType())) {
        return true;
      }
    }
    for (TypeParameterDescriptor parameter : function.getTypeParameters()) {
      for (JetType upperBound : parameter.getUpperBounds()) {
        if (containsErrorType(upperBound)) {
          return true;
        }
      }
    }

    return false;
  }
Esempio n. 12
0
 @Nullable
 public static KotlinType getReceiverParameterType(
     @Nullable ReceiverParameterDescriptor receiverParameterDescriptor) {
   return receiverParameterDescriptor == null ? null : receiverParameterDescriptor.getType();
 }