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);
        }
      }
    }
  }
示例#2
0
 private static Collection<DeclarationDescriptor> getAllVariables(LexicalScope scope) {
   Collection<DeclarationDescriptor> result = ContainerUtil.newArrayList();
   result.addAll(
       ScopeUtilsKt.collectDescriptorsFiltered(
           scope, DescriptorKindFilter.VARIABLES, MemberScope.Companion.getALL_NAME_FILTER()));
   for (ReceiverParameterDescriptor implicitReceiver :
       ScopeUtilsKt.getImplicitReceiversHierarchy(scope)) {
     result.addAll(DescriptorUtils.getAllDescriptors(implicitReceiver.getType().getMemberScope()));
   }
   return result;
 }
  private static void checkIfOperatorModifierPresent(
      KtExpression expression, FunctionDescriptor descriptor, DiagnosticSink sink) {
    if (ErrorUtils.isError(descriptor)) return;
    ReceiverParameterDescriptor extensionReceiverParameter =
        descriptor.getExtensionReceiverParameter();
    if ((extensionReceiverParameter != null)
        && (DynamicTypesKt.isDynamic(extensionReceiverParameter.getType()))) return;

    if (!descriptor.isOperator()) {
      OperatorValidator.Companion.report(expression, descriptor, sink);
    }
  }
示例#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;
 }
示例#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);
  }
示例#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);
  }
示例#7
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;
   }
 }
示例#8
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);
  }
示例#9
0
 @Nullable
 public static KotlinType getReceiverParameterType(
     @Nullable ReceiverParameterDescriptor receiverParameterDescriptor) {
   return receiverParameterDescriptor == null ? null : receiverParameterDescriptor.getType();
 }