From 9377d274a8c28debee6ac4095128b8c002adc9d0 Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Wed, 17 Jun 2020 15:23:40 +0300 Subject: [PATCH] Drop ExecutionResult class implementations and make it regular class There was 2 implementations that I managed to combine --- .../backend/common/interpreter/Label.kt | 88 +++++++------------ 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/Label.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/Label.kt index 8ae9600ee86..a93b03929e9 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/Label.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/interpreter/Label.kt @@ -26,10 +26,33 @@ enum class ReturnLabel { NEXT, RETURN, BREAK_LOOP, BREAK_WHEN, CONTINUE, EXCEPTION } -interface ExecutionResult { - val returnLabel: ReturnLabel +open class ExecutionResult(val returnLabel: ReturnLabel, private val owner: IrElement? = null) { + suspend fun getNextLabel(irElement: IrElement, interpret: suspend IrElement.() -> ExecutionResult): ExecutionResult { + return when (returnLabel) { + ReturnLabel.RETURN -> when (irElement) { + is IrCall, is IrReturnableBlock, is IrFunctionImpl, is IrLazyFunction -> if (owner == irElement) Next else this + else -> this + } + ReturnLabel.BREAK_WHEN -> when (irElement) { + is IrWhen -> Next + else -> this + } + ReturnLabel.BREAK_LOOP -> when (irElement) { + is IrWhileLoop -> if (owner == irElement) Next else this + else -> this + } + ReturnLabel.CONTINUE -> when (irElement) { + is IrWhileLoop -> if (owner == irElement) irElement.interpret() else this + else -> this + } + ReturnLabel.EXCEPTION -> Exception + ReturnLabel.NEXT -> Next + } + } - suspend fun getNextLabel(irElement: IrElement, interpret: suspend IrElement.() -> ExecutionResult): ExecutionResult + fun addOwnerInfo(owner: IrElement): ExecutionResult { + return ExecutionResult(returnLabel, owner) + } } inline fun ExecutionResult.check(toCheckLabel: ReturnLabel = ReturnLabel.NEXT, returnBlock: (ExecutionResult) -> Unit): ExecutionResult { @@ -55,56 +78,9 @@ internal fun ExecutionResult.implicitCastIfNeeded(expectedType: IrType, actualTy return this } -open class ExecutionResultWithoutInfoAboutOwner(override val returnLabel: ReturnLabel) : ExecutionResult { - override suspend fun getNextLabel(irElement: IrElement, interpret: suspend IrElement.() -> ExecutionResult): ExecutionResult { - return when (returnLabel) { - ReturnLabel.RETURN -> this - ReturnLabel.BREAK_WHEN -> when (irElement) { - is IrWhen -> Next - else -> this - } - ReturnLabel.BREAK_LOOP -> this - ReturnLabel.CONTINUE -> this - ReturnLabel.EXCEPTION -> this - ReturnLabel.NEXT -> this - } - } - - fun addOwnerInfo(owner: IrElement): ExecutionResultWithInfoAboutOwner { - return ExecutionResultWithInfoAboutOwner(returnLabel, owner) - } -} - -class ExecutionResultWithInfoAboutOwner( - override val returnLabel: ReturnLabel, private val owner: IrElement -) : ExecutionResultWithoutInfoAboutOwner(returnLabel) { - override suspend fun getNextLabel(irElement: IrElement, interpret: suspend IrElement.() -> ExecutionResult): ExecutionResult { - return when (returnLabel) { - ReturnLabel.RETURN -> when (irElement) { - is IrCall, is IrReturnableBlock, is IrFunctionImpl, is IrLazyFunction -> if (owner == irElement) Next else this - else -> this - } - ReturnLabel.BREAK_WHEN -> when (irElement) { - is IrWhen -> Next - else -> this - } - ReturnLabel.BREAK_LOOP -> when (irElement) { - is IrWhileLoop -> if (owner == irElement) Next else this - else -> this - } - ReturnLabel.CONTINUE -> when (irElement) { - is IrWhileLoop -> if (owner == irElement) irElement.interpret() else this - else -> this - } - ReturnLabel.EXCEPTION -> Exception - ReturnLabel.NEXT -> Next - } - } -} - -object Next : ExecutionResultWithoutInfoAboutOwner(ReturnLabel.NEXT) -object Return : ExecutionResultWithoutInfoAboutOwner(ReturnLabel.RETURN) -object BreakLoop : ExecutionResultWithoutInfoAboutOwner(ReturnLabel.BREAK_LOOP) -object BreakWhen : ExecutionResultWithoutInfoAboutOwner(ReturnLabel.BREAK_WHEN) -object Continue : ExecutionResultWithoutInfoAboutOwner(ReturnLabel.CONTINUE) -object Exception : ExecutionResultWithoutInfoAboutOwner(ReturnLabel.EXCEPTION) \ No newline at end of file +object Next : ExecutionResult(ReturnLabel.NEXT) +object Return : ExecutionResult(ReturnLabel.RETURN) +object BreakLoop : ExecutionResult(ReturnLabel.BREAK_LOOP) +object BreakWhen : ExecutionResult(ReturnLabel.BREAK_WHEN) +object Continue : ExecutionResult(ReturnLabel.CONTINUE) +object Exception : ExecutionResult(ReturnLabel.EXCEPTION) \ No newline at end of file