Search in sources :

Example 1 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class InlineCodegen method processAndPutHiddenParameters.

@Override
public void processAndPutHiddenParameters(boolean justProcess) {
    if ((getMethodAsmFlags(functionDescriptor, context.getContextKind(), state) & Opcodes.ACC_STATIC) == 0) {
        invocationParamBuilder.addNextParameter(AsmTypes.OBJECT_TYPE, false);
    }
    for (JvmMethodParameterSignature param : jvmSignature.getValueParameters()) {
        if (param.getKind() == JvmMethodParameterKind.VALUE) {
            break;
        }
        invocationParamBuilder.addNextParameter(param.getAsmType(), false);
    }
    invocationParamBuilder.markValueParametersStart();
    List<ParameterInfo> hiddenParameters = invocationParamBuilder.buildParameters().getParameters();
    delayedHiddenWriting = recordParameterValueInLocalVal(justProcess, hiddenParameters.toArray(new ParameterInfo[hiddenParameters.size()]));
}
Also used : JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature)

Example 2 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class FunctionCodegen method generateParameterAnnotations.

public static void generateParameterAnnotations(@NotNull FunctionDescriptor functionDescriptor, @NotNull MethodVisitor mv, @NotNull JvmMethodSignature jvmSignature, @NotNull List<ValueParameterDescriptor> valueParameters, @NotNull InnerClassConsumer innerClassConsumer, @NotNull GenerationState state) {
    KotlinTypeMapper typeMapper = state.getTypeMapper();
    Iterator<ValueParameterDescriptor> iterator = valueParameters.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, state.getClassBuilderMode());
            continue;
        }
        if (kind == JvmMethodParameterKind.VALUE) {
            ValueParameterDescriptor parameter = iterator.next();
            AnnotationCodegen annotationCodegen = AnnotationCodegen.forParameter(i, mv, innerClassConsumer, 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 = JvmCodegenUtil.getDirectMember(functionDescriptor).getExtensionReceiverParameter();
            if (receiver != null) {
                AnnotationCodegen annotationCodegen = AnnotationCodegen.forParameter(i, mv, innerClassConsumer, typeMapper);
                Annotated targetedAnnotations = new AnnotatedWithOnlyTargetedAnnotations(receiver.getType());
                annotationCodegen.genAnnotations(targetedAnnotations, parameterSignature.getAsmType(), RECEIVER);
                annotationCodegen.genAnnotations(receiver, parameterSignature.getAsmType());
            }
        }
    }
}
Also used : JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature) JvmMethodParameterKind(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind) Annotated(org.jetbrains.kotlin.descriptors.annotations.Annotated) AnnotatedWithOnlyTargetedAnnotations(org.jetbrains.kotlin.codegen.annotation.AnnotatedWithOnlyTargetedAnnotations) KotlinTypeMapper(org.jetbrains.kotlin.codegen.state.KotlinTypeMapper)

Example 3 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class FunctionCodegen method loadExplicitArgumentsOnStack.

private static void loadExplicitArgumentsOnStack(@NotNull Type ownerType, boolean isStatic, @NotNull JvmMethodSignature signature, @NotNull CallGenerator callGenerator) {
    int var = 0;
    if (!isStatic) {
        callGenerator.putValueIfNeeded(ownerType, StackValue.local(var, ownerType));
        var += ownerType.getSize();
    }
    for (JvmMethodParameterSignature parameterSignature : signature.getValueParameters()) {
        if (parameterSignature.getKind() != JvmMethodParameterKind.VALUE) {
            Type type = parameterSignature.getAsmType();
            callGenerator.putValueIfNeeded(type, StackValue.local(var, type));
            var += type.getSize();
        }
    }
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature)

Example 4 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class FunctionCodegen method generateDefaultImplBody.

public static void generateDefaultImplBody(@NotNull MethodContext methodContext, @NotNull FunctionDescriptor functionDescriptor, @NotNull MethodVisitor mv, @NotNull DefaultParameterValueLoader loadStrategy, @Nullable KtNamedFunction function, @NotNull MemberCodegen<?> parentCodegen, @NotNull Method defaultMethod) {
    GenerationState state = parentCodegen.state;
    JvmMethodSignature signature = state.getTypeMapper().mapSignatureWithGeneric(functionDescriptor, methodContext.getContextKind());
    boolean isStatic = isStaticMethod(methodContext.getContextKind(), functionDescriptor);
    FrameMap frameMap = createFrameMap(state, functionDescriptor, signature, isStatic);
    ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, signature.getReturnType(), methodContext, state, parentCodegen);
    CallGenerator generator = codegen.getOrCreateCallGeneratorForDefaultImplBody(functionDescriptor, function);
    InstructionAdapter iv = new InstructionAdapter(mv);
    genDefaultSuperCallCheckIfNeeded(iv, functionDescriptor, defaultMethod);
    List<JvmMethodParameterSignature> mappedParameters = signature.getValueParameters();
    int capturedArgumentsCount = 0;
    while (capturedArgumentsCount < mappedParameters.size() && mappedParameters.get(capturedArgumentsCount).getKind() != JvmMethodParameterKind.VALUE) {
        capturedArgumentsCount++;
    }
    int maskIndex = 0;
    List<ValueParameterDescriptor> valueParameters = functionDescriptor.getValueParameters();
    for (int index = 0; index < valueParameters.size(); index++) {
        if (index % Integer.SIZE == 0) {
            maskIndex = frameMap.enterTemp(Type.INT_TYPE);
        }
        ValueParameterDescriptor parameterDescriptor = valueParameters.get(index);
        Type type = mappedParameters.get(capturedArgumentsCount + index).getAsmType();
        int parameterIndex = frameMap.getIndex(parameterDescriptor);
        if (parameterDescriptor.declaresDefaultValue()) {
            iv.load(maskIndex, Type.INT_TYPE);
            iv.iconst(1 << (index % Integer.SIZE));
            iv.and(Type.INT_TYPE);
            Label loadArg = new Label();
            iv.ifeq(loadArg);
            StackValue.local(parameterIndex, type).store(loadStrategy.genValue(parameterDescriptor, codegen), iv);
            iv.mark(loadArg);
        }
    }
    // load arguments after defaults generation to avoid redundant stack normalization operations
    loadExplicitArgumentsOnStack(OBJECT_TYPE, isStatic, signature, generator);
    for (int index = 0; index < valueParameters.size(); index++) {
        ValueParameterDescriptor parameterDescriptor = valueParameters.get(index);
        Type type = mappedParameters.get(capturedArgumentsCount + index).getAsmType();
        int parameterIndex = frameMap.getIndex(parameterDescriptor);
        generator.putValueIfNeeded(type, StackValue.local(parameterIndex, type));
    }
    CallableMethod method = state.getTypeMapper().mapToCallableMethod(functionDescriptor, false);
    generator.genCall(method, null, false, codegen);
    iv.areturn(signature.getReturnType());
}
Also used : JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) JvmMethodSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)

Example 5 with JvmMethodParameterSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature in project kotlin by JetBrains.

the class ExpressionCodegen method invokeMethodWithArguments.

public void invokeMethodWithArguments(@NotNull Callable callableMethod, @NotNull ResolvedCall<?> resolvedCall, @NotNull StackValue receiver, @NotNull CallGenerator callGenerator, @NotNull ArgumentGenerator argumentGenerator) {
    boolean isSuspensionPoint = CoroutineCodegenUtilKt.isSuspensionPointInStateMachine(resolvedCall, bindingContext);
    boolean isConstructor = resolvedCall.getResultingDescriptor() instanceof ConstructorDescriptor;
    putReceiverAndInlineMarkerIfNeeded(callableMethod, resolvedCall, receiver, isSuspensionPoint, isConstructor);
    callGenerator.processAndPutHiddenParameters(false);
    List<ResolvedValueArgument> valueArguments = resolvedCall.getValueArgumentsByIndex();
    assert valueArguments != null : "Failed to arrange value arguments by index: " + resolvedCall.getResultingDescriptor();
    DefaultCallArgs defaultArgs = argumentGenerator.generate(valueArguments, new ArrayList<ResolvedValueArgument>(resolvedCall.getValueArguments().values()), resolvedCall.getResultingDescriptor());
    if (tailRecursionCodegen.isTailRecursion(resolvedCall)) {
        tailRecursionCodegen.generateTailRecursion(resolvedCall);
        return;
    }
    boolean defaultMaskWasGenerated = defaultArgs.generateOnStackIfNeeded(callGenerator, isConstructor);
    // Extra constructor marker argument
    if (callableMethod instanceof CallableMethod) {
        List<JvmMethodParameterSignature> callableParameters = ((CallableMethod) callableMethod).getValueParameters();
        for (JvmMethodParameterSignature parameter : callableParameters) {
            if (parameter.getKind() == JvmMethodParameterKind.CONSTRUCTOR_MARKER) {
                callGenerator.putValueIfNeeded(parameter.getAsmType(), StackValue.constant(null, parameter.getAsmType()));
            }
        }
    }
    if (isSuspensionPoint) {
        v.invokestatic(CoroutineCodegenUtilKt.COROUTINE_MARKER_OWNER, CoroutineCodegenUtilKt.BEFORE_SUSPENSION_POINT_MARKER_NAME, "()V", false);
    }
    callGenerator.genCall(callableMethod, resolvedCall, defaultMaskWasGenerated, this);
    if (isSuspensionPoint) {
        v.invokestatic(CoroutineCodegenUtilKt.COROUTINE_MARKER_OWNER, CoroutineCodegenUtilKt.AFTER_SUSPENSION_POINT_MARKER_NAME, "()V", false);
        addInlineMarker(v, false);
    }
    KotlinType returnType = resolvedCall.getResultingDescriptor().getReturnType();
    if (returnType != null && KotlinBuiltIns.isNothing(returnType)) {
        v.aconst(null);
        v.athrow();
    }
}
Also used : JvmMethodParameterSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature) KotlinType(org.jetbrains.kotlin.types.KotlinType) SamConstructorDescriptor(org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor) TypeAliasConstructorDescriptor(org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor)

Aggregations

JvmMethodParameterSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature)11 JvmMethodParameterKind (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind)5 KotlinType (org.jetbrains.kotlin.types.KotlinType)5 NotNull (org.jetbrains.annotations.NotNull)3 TypeAliasConstructorDescriptor (org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor)2 BindingContextUtils.getNotNull (org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)2 Type (org.jetbrains.org.objectweb.asm.Type)2 ArrayList (java.util.ArrayList)1 AnnotatedWithOnlyTargetedAnnotations (org.jetbrains.kotlin.codegen.annotation.AnnotatedWithOnlyTargetedAnnotations)1 GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)1 KotlinTypeMapper (org.jetbrains.kotlin.codegen.state.KotlinTypeMapper)1 Annotated (org.jetbrains.kotlin.descriptors.annotations.Annotated)1 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)1 SamConstructorDescriptor (org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor)1 DefaultValueArgument (org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument)1 ResolvedValueArgument (org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument)1 JvmMethodGenericSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature)1 JvmMethodSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature)1 Type.getObjectType (org.jetbrains.org.objectweb.asm.Type.getObjectType)1 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)1