Skip to content

Remove duplicate of IR nodes inside the body #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrBody
Expand Down Expand Up @@ -241,21 +242,17 @@ private fun generateTransformBodyForFunctionLambda(
parameter.defaultValue = originFunctionValueParameter.defaultValue
}


return context.createIrBuilder(function.symbol).irBlockBody {
val suspendLambdaFunc = context.createSuspendLambdaFunctionWithCoroutineScope(
originFunction = originFunction,
function = function,
this
).also { +it }
)

val lambdaType = context.symbols.suspendFunctionN(0).typeWith(suspendLambdaFunc.returnType)

val ex = IrFunctionExpressionImpl(-1, -1, lambdaType, suspendLambdaFunc, IrStatementOrigin.LAMBDA)
+ex

+irReturn(irCall(transformTargetFunctionCall).apply {
putValueArgument(0, ex)
putValueArgument(0, IrFunctionExpressionImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, lambdaType, suspendLambdaFunc, IrStatementOrigin.LAMBDA))
// argument: 1, if is CoroutineScope, and this is CoroutineScope.
val owner = transformTargetFunctionCall.owner

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.types.typeOrNull
Expand Down Expand Up @@ -59,7 +57,7 @@ fun IrPluginContext.createSuspendLambdaWithCoroutineScope(
lambdaType: IrSimpleType,
originFunction: IrFunction,
): IrClass {
return IrFactoryImpl.buildClass {
return irFactory.buildClass {
name = SpecialNames.NO_NAME_PROVIDED
kind = ClassKind.CLASS
/*
Expand Down Expand Up @@ -149,35 +147,15 @@ fun IrPluginContext.createSuspendLambdaFunctionWithCoroutineScope(
function: IrFunction,
blockBodyBuilder: IrBlockBodyBuilder
): IrSimpleFunction {
val func = IrFunctionImpl(
startOffset = -1,
endOffset = -1,
origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA,
name = SpecialNames.NO_NAME_PROVIDED,
visibility = DescriptorVisibilities.LOCAL,
isInline = false,
isExpect = false,
returnType = function.returnType,
modality = Modality.FINAL,
symbol = IrSimpleFunctionSymbolImpl(),
isSuspend = true,
isTailrec = false,
isOperator = false,
isInfix = false,
isExternal = false,
)

// val func1 = IrFactoryImpl.buildFun {
// name = SpecialNames.NO_NAME_PROVIDED
// visibility = DescriptorVisibilities.LOCAL
// isSuspend = true
// returnType = function.returnType
// }

with(func) {
return irFactory.buildFun {
origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
name = SpecialNames.NO_NAME_PROVIDED
visibility = DescriptorVisibilities.LOCAL
returnType = function.returnType
modality = Modality.FINAL
isSuspend = true
}.apply {
parent = function
// origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA

body = createIrBuilder(symbol).run {
// don't use expr body, coroutine codegen can't generate for it.
irBlockBody {
Expand All @@ -198,8 +176,6 @@ fun IrPluginContext.createSuspendLambdaFunctionWithCoroutineScope(
}
}
}

return func
}

fun IrFunction.paramsAndReceiversAsParamsList(): List<IrValueParameter> {
Expand Down