diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/AbstractSuspendFunctionsLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/AbstractSuspendFunctionsLowering.kt index 16bb98296c7..da09bbf76a9 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/AbstractSuspendFunctionsLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/AbstractSuspendFunctionsLowering.kt @@ -67,20 +67,15 @@ abstract class AbstractSuspendFunctionsLowering(val co private val builtCoroutines = mutableMapOf() override fun lower(irBody: IrBody, container: IrDeclaration) { - transformToCoroutine(irBody, container) - } - - - private fun transformToCoroutine(irBody: IrBody, container: IrDeclaration) { - if (container !is IrSimpleFunction || !container.isSuspend) return - - transformSuspendFunction2(container, irBody)?.let { - val dc = container.parent as IrDeclarationContainer - dc.addChild(it) + if (container is IrSimpleFunction && container.isSuspend) { + transformSuspendFunction(container, irBody)?.let { + val dc = container.parent as IrDeclarationContainer + dc.addChild(it) + } } } - private fun getSuspendFunctionKind2(function: IrSimpleFunction, body: IrBody): SuspendFunctionKind { + private fun getSuspendFunctionKind(function: IrSimpleFunction, body: IrBody): SuspendFunctionKind { fun IrSimpleFunction.isSuspendLambda() = name.asString() == "invoke" && parentClassOrNull?.let { it.origin === CallableReferenceLowering.Companion.LAMBDA_IMPL } == true @@ -136,10 +131,10 @@ abstract class AbstractSuspendFunctionsLowering(val co } } - private fun transformSuspendFunction2(function: IrSimpleFunction, body: IrBody): IrClass? { + private fun transformSuspendFunction(function: IrSimpleFunction, body: IrBody): IrClass? { assert(function.isSuspend) - return when (val functionKind = getSuspendFunctionKind2(function, body)) { + return when (val functionKind = getSuspendFunctionKind(function, body)) { is SuspendFunctionKind.NO_SUSPEND_CALLS -> { null // No suspend function calls - just an ordinary function. } @@ -150,7 +145,7 @@ abstract class AbstractSuspendFunctionsLowering(val co } is SuspendFunctionKind.NEEDS_STATE_MACHINE -> { - val coroutine = buildCoroutine2(function) // Coroutine implementation. + val coroutine = buildCoroutine(function) // Coroutine implementation. if (coroutine === function.parent) // Suspend lambdas are called through factory method , null else @@ -180,8 +175,8 @@ abstract class AbstractSuspendFunctionsLowering(val co } } - private fun buildCoroutine2(function: IrSimpleFunction): IrClass { - val coroutine = CoroutineBuilder2(function).build() + private fun buildCoroutine(function: IrSimpleFunction): IrClass { + val coroutine = CoroutineBuilder(function).build() val isSuspendLambda = coroutine.coroutineClass === function.parent @@ -201,7 +196,7 @@ abstract class AbstractSuspendFunctionsLowering(val co return coroutine.coroutineClass } - private inner class CoroutineBuilder2(private val function: IrSimpleFunction) { + private inner class CoroutineBuilder(private val function: IrSimpleFunction) { private val startOffset = function.startOffset private val endOffset = function.endOffset private val isSuspendLambda = function.isOperator && function.name.asString() == "invoke" && function.parentClassOrNull @@ -482,6 +477,7 @@ abstract class AbstractSuspendFunctionsLowering(val co coroutineClass.addFakeOverrides(implementedMembers) + // TODO: to meet PIR lower model constructor modification shouldn't be performed here initializeStateMachine(listOf(coroutineConstructor), coroutineClassThis) return BuiltCoroutine( @@ -576,14 +572,4 @@ abstract class AbstractSuspendFunctionsLowering(val co addChild(it) } } -} - -//private val IrDeclaration.isLambda -// get() = origin == IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA - -//class RemoveSuspendLambdas() : DeclarationTransformer { -// -// override fun transformFlat(declaration: IrDeclaration): List? { -// return if (declaration.isLambda && declaration is IrFunction && declaration.isSuspend) emptyList() else null -// } -//} \ No newline at end of file +} \ No newline at end of file diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/JsSuspendFunctionsLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/JsSuspendFunctionsLowering.kt index 973d4226b91..5e6773f58f6 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/JsSuspendFunctionsLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/JsSuspendFunctionsLowering.kt @@ -130,8 +130,6 @@ class JsSuspendFunctionsLowering(ctx: JsIrBackendContext) : AbstractSuspendFunct IrBlockBodyImpl(stateMachineFunction.startOffset, stateMachineFunction.endOffset, listOf(suspendResult, rootLoop)) stateMachineFunction.body = functionBody - // TODO: Investigate parent problems - stateMachineFunction.patchDeclarationParents(stateMachineFunction.parent) // Move return targets to new function functionBody.transformChildrenVoid(object : IrElementTransformerVoid() {