Search in sources :

Example 1 with SuspendFunctionGenerationStrategy

use of org.jetbrains.kotlin.codegen.coroutines.SuspendFunctionGenerationStrategy in project kotlin by JetBrains.

the class InlineCodegen method generateMethodBody.

@NotNull
private static SMAP generateMethodBody(@NotNull MethodVisitor adapter, @NotNull FunctionDescriptor descriptor, @NotNull MethodContext context, @NotNull KtExpression expression, @NotNull JvmMethodSignature jvmMethodSignature, @NotNull ExpressionCodegen codegen, @Nullable LambdaInfo lambdaInfo) {
    boolean isLambda = lambdaInfo != null;
    GenerationState state = codegen.getState();
    // Wrapping for preventing marking actual parent codegen as containing reified markers
    FakeMemberCodegen parentCodegen = new FakeMemberCodegen(codegen.getParentCodegen(), expression, (FieldOwnerContext) context.getParentContext(), isLambda ? codegen.getParentCodegen().getClassName() : state.getTypeMapper().mapImplementationOwner(descriptor).getInternalName());
    FunctionGenerationStrategy strategy;
    if (expression instanceof KtCallableReferenceExpression) {
        KtCallableReferenceExpression callableReferenceExpression = (KtCallableReferenceExpression) expression;
        KtExpression receiverExpression = callableReferenceExpression.getReceiverExpression();
        Type receiverType = receiverExpression != null && codegen.getBindingContext().getType(receiverExpression) != null ? codegen.getState().getTypeMapper().mapType(codegen.getBindingContext().getType(receiverExpression)) : null;
        if (isLambda && lambdaInfo.isPropertyReference()) {
            Type asmType = state.getTypeMapper().mapClass(lambdaInfo.getClassDescriptor());
            PropertyReferenceInfo info = lambdaInfo.getPropertyReferenceInfo();
            strategy = new PropertyReferenceCodegen.PropertyReferenceGenerationStrategy(true, info.getGetFunction(), info.getTarget(), asmType, receiverType, lambdaInfo.expression, state, true);
        } else {
            strategy = new FunctionReferenceGenerationStrategy(state, descriptor, CallUtilKt.getResolvedCallWithAssert(callableReferenceExpression.getCallableReference(), codegen.getBindingContext()), receiverType, null, true);
        }
    } else if (expression instanceof KtFunctionLiteral) {
        strategy = new ClosureGenerationStrategy(state, (KtDeclarationWithBody) expression);
    } else if (descriptor.isSuspend() && expression instanceof KtFunction) {
        strategy = new SuspendFunctionGenerationStrategy(state, CoroutineCodegenUtilKt.<FunctionDescriptor>unwrapInitialDescriptorForSuspendFunction(descriptor), (KtFunction) expression);
    } else {
        strategy = new FunctionGenerationStrategy.FunctionDefault(state, (KtDeclarationWithBody) expression);
    }
    FunctionCodegen.generateMethodBody(adapter, descriptor, context, jvmMethodSignature, strategy, parentCodegen);
    if (isLambda) {
        codegen.propagateChildReifiedTypeParametersUsages(parentCodegen.getReifiedTypeParametersUsages());
    }
    return createSMAPWithDefaultMapping(expression, parentCodegen.getOrCreateSourceMapper().getResultMappings());
}
Also used : SuspendFunctionGenerationStrategy(org.jetbrains.kotlin.codegen.coroutines.SuspendFunctionGenerationStrategy) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) SuspendFunctionGenerationStrategy(org.jetbrains.kotlin.codegen.coroutines.SuspendFunctionGenerationStrategy) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SuspendFunctionGenerationStrategy

use of org.jetbrains.kotlin.codegen.coroutines.SuspendFunctionGenerationStrategy in project kotlin by JetBrains.

the class FunctionCodegen method gen.

public void gen(@NotNull KtNamedFunction function) {
    SimpleFunctionDescriptor functionDescriptor = bindingContext.get(BindingContext.FUNCTION, function);
    if (bindingContext.get(CodegenBinding.SUSPEND_FUNCTION_TO_JVM_VIEW, functionDescriptor) != null) {
        functionDescriptor = (SimpleFunctionDescriptor) bindingContext.get(CodegenBinding.SUSPEND_FUNCTION_TO_JVM_VIEW, functionDescriptor);
    }
    if (functionDescriptor == null) {
        throw ExceptionLogger.logDescriptorNotFound("No descriptor for function " + function.getName(), function);
    }
    if (owner.getContextKind() != OwnerKind.DEFAULT_IMPLS || function.hasBody()) {
        FunctionGenerationStrategy strategy;
        if (functionDescriptor.isSuspend()) {
            strategy = new SuspendFunctionGenerationStrategy(state, CoroutineCodegenUtilKt.<FunctionDescriptor>unwrapInitialDescriptorForSuspendFunction(functionDescriptor), function);
        } else {
            strategy = new FunctionGenerationStrategy.FunctionDefault(state, function);
        }
        generateMethod(JvmDeclarationOriginKt.OtherOrigin(function, functionDescriptor), functionDescriptor, strategy);
    }
    generateDefaultIfNeeded(owner.intoFunction(functionDescriptor, true), functionDescriptor, owner.getContextKind(), DefaultParameterValueLoader.DEFAULT, function);
    generateOverloadsWithDefaultValues(function, functionDescriptor, functionDescriptor);
}
Also used : SuspendFunctionGenerationStrategy(org.jetbrains.kotlin.codegen.coroutines.SuspendFunctionGenerationStrategy) SuspendFunctionGenerationStrategy(org.jetbrains.kotlin.codegen.coroutines.SuspendFunctionGenerationStrategy)

Aggregations

SuspendFunctionGenerationStrategy (org.jetbrains.kotlin.codegen.coroutines.SuspendFunctionGenerationStrategy)2 NotNull (org.jetbrains.annotations.NotNull)1 GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 Type (org.jetbrains.org.objectweb.asm.Type)1