FIR2IR: insert coerce-to-unit expressions in statement containers

This commit is contained in:
Jinseong Jeon
2020-08-18 08:00:11 +03:00
committed by Mikhail Glukhikh
parent 1b3ab53e16
commit 7e22de1e24
52 changed files with 475 additions and 628 deletions
@@ -42,6 +42,7 @@ import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.coerceToUnitIfNeeded
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.parentClassOrNull
@@ -590,7 +591,7 @@ class Fir2IrVisitor(
} else {
emptyList()
}
)
).insertImplicitCasts()
}
}
@@ -608,16 +609,44 @@ class Fir2IrVisitor(
IrCompositeImpl(
startOffset, endOffset, type, origin,
mapToIrStatements().filterNotNull()
)
).insertImplicitCasts()
} else {
IrBlockImpl(
startOffset, endOffset, type, origin,
mapToIrStatements().filterNotNull()
)
).insertImplicitCasts()
}
}
}
private fun IrBlockBody.insertImplicitCasts(): IrBlockBody {
if (statements.isEmpty()) return this
statements.forEachIndexed { i, irStatement ->
if (irStatement !is IrErrorCallExpression && irStatement is IrExpression) {
statements[i] = irStatement.coerceToUnitIfNeeded(irStatement.type, irBuiltIns)
}
}
return this
}
private fun IrContainerExpression.insertImplicitCasts(): IrContainerExpression {
if (statements.isEmpty()) return this
val lastIndex = statements.lastIndex
statements.forEachIndexed { i, irStatement ->
if (irStatement !is IrErrorCallExpression && irStatement is IrExpression) {
if (i != lastIndex) {
statements[i] = irStatement.coerceToUnitIfNeeded(irStatement.type, irBuiltIns)
} else {
// TODO: for the last statement, need to cast to the return type if mismatched
}
}
}
return this
}
override fun visitErrorExpression(errorExpression: FirErrorExpression, data: Any?): IrElement {
return errorExpression.convertWithOffsets { startOffset, endOffset ->
IrErrorExpressionImpl(