[IR] Make IrReturnTargetSymbol a sealed interface

This commit is contained in:
Sergej Jaskiewicz
2023-05-05 13:49:50 +02:00
committed by Space Team
parent 6c211f3a39
commit 93d1932ccb
7 changed files with 13 additions and 15 deletions
@@ -72,7 +72,6 @@ abstract class AbstractValueUsageTransformer(
is IrSimpleFunctionSymbol -> this.useAs(returnTarget.owner.returnType)
is IrConstructorSymbol -> this.useAs(irBuiltIns.unitType)
is IrReturnableBlockSymbol -> this.useAs(returnTarget.owner.type)
else -> error(returnTarget)
}
protected open fun IrExpression.useAsResult(enclosing: IrExpression): IrExpression =
@@ -149,7 +149,6 @@ class AutoboxingTransformer(context: JsCommonBackendContext) : AbstractValueUsag
is IrSimpleFunctionSymbol -> useReturnableExpressionAsType(returnTarget.owner.returnType)
is IrConstructorSymbol -> useReturnableExpressionAsType(irBuiltIns.unitType)
is IrReturnableBlockSymbol -> useReturnableExpressionAsType(returnTarget.owner.type)
else -> error(returnTarget)
}
override fun IrExpression.useExpressionAsType(actualType: IrType, expectedType: IrType): IrExpression {
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrReturnableBlockSymbol
import org.jetbrains.kotlin.ir.types.isAny
import org.jetbrains.kotlin.ir.util.constructedClassType
@@ -125,17 +126,19 @@ class IrElementToJsStatementTransformer : BaseIrElementToJsNodeTransformer<JsSta
}
override fun visitReturn(expression: IrReturn, context: JsGenerationContext): JsStatement {
val targetSymbol = expression.returnTargetSymbol
val lastStatementTransformer: (() -> JsExpression) -> JsStatement =
if (targetSymbol is IrReturnableBlockSymbol) {
// TODO assert that value is Unit?
{
context.getNameForReturnableBlock(targetSymbol.owner)
.takeIf { !expression.isTheLastReturnStatementIn(targetSymbol) }
?.run { JsBreak(makeRef()) } ?: JsEmpty
when (val targetSymbol = expression.returnTargetSymbol) {
is IrReturnableBlockSymbol -> {
// TODO assert that value is Unit?
{
context.getNameForReturnableBlock(targetSymbol.owner)
.takeIf { !expression.isTheLastReturnStatementIn(targetSymbol) }
?.run { JsBreak(makeRef()) } ?: JsEmpty
}
}
is IrFunctionSymbol -> {
{ JsReturn(it()) }
}
} else {
{ JsReturn(it()) }
}
return expression.value.maybeOptimizeIntoSwitch(context, lastStatementTransformer).withSource(expression, context)
@@ -91,7 +91,7 @@ interface IrValueParameterSymbol : IrValueSymbol, IrBindableSymbol<ParameterDesc
interface IrVariableSymbol : IrValueSymbol, IrBindableSymbol<VariableDescriptor, IrVariable>
interface IrReturnTargetSymbol : IrSymbol {
sealed interface IrReturnTargetSymbol : IrSymbol {
@ObsoleteDescriptorBasedAPI
override val descriptor: FunctionDescriptor
@@ -806,7 +806,6 @@ open class DeepCopyIrTreeWithSymbols(
when (returnTarget) {
is IrFunctionSymbol -> getReferencedFunction(returnTarget)
is IrReturnableBlockSymbol -> getReferencedReturnableBlock(returnTarget)
else -> throw AssertionError("Unexpected return target: ${returnTarget.javaClass} $returnTarget")
}
override fun visitThrow(expression: IrThrow): IrThrow =
@@ -75,7 +75,6 @@ internal abstract class AbstractValueUsageTransformer(
is IrSimpleFunctionSymbol -> this.useAs(returnTarget.owner.returnType)
is IrConstructorSymbol -> this.useAs(irBuiltIns.unitType)
is IrReturnableBlockSymbol -> this.useAs(returnTarget.owner.type)
else -> error(returnTarget)
}
protected open fun IrExpression.useAsResult(enclosing: IrExpression): IrExpression =
@@ -79,7 +79,6 @@ private class AutoboxingTransformer(val context: Context) : AbstractValueUsageTr
is IrSimpleFunctionSymbol -> this.useAs(returnTarget.owner.returnType)
is IrConstructorSymbol -> this.useAs(irBuiltIns.unitType)
is IrReturnableBlockSymbol -> this.useAs(returnTarget.owner.type)
else -> error(returnTarget)
}
override fun IrExpression.useAs(type: IrType): IrExpression {