Support cast type operations in inline

This commit is contained in:
Konstantin Anisimov
2017-03-17 16:54:28 +07:00
committed by KonstantinAnisimov
parent ba8621389e
commit 864bd9a212
3 changed files with 35 additions and 3 deletions
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.ir.util.getArguments
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.makeNullable
//-----------------------------------------------------------------------------//
@@ -177,24 +178,39 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoid(
//---------------------------------------------------------------------//
fun getTypeOperatorReturnType(operator: IrTypeOperator, type: KotlinType) : KotlinType {
return when (operator) {
IrTypeOperator.CAST,
IrTypeOperator.IMPLICIT_CAST,
IrTypeOperator.IMPLICIT_NOTNULL,
IrTypeOperator.IMPLICIT_COERCION_TO_UNIT,
IrTypeOperator.IMPLICIT_INTEGER_COERCION -> type
IrTypeOperator.SAFE_CAST -> type.makeNullable()
IrTypeOperator.INSTANCEOF,
IrTypeOperator.NOT_INSTANCEOF -> context.builtIns.booleanType
}
}
//---------------------------------------------------------------------//
override fun visitTypeOperator(expression: IrTypeOperatorCall): IrExpression {
val newExpression = super.visitTypeOperator(expression) as IrTypeOperatorCall
if (typeArgsMap == null) return newExpression
if (newExpression.operator == IrTypeOperator.IMPLICIT_COERCION_TO_UNIT) { // Nothing to do for IMPLICIT_COERCION_TO_UNIT
return newExpression
}
if (typeArgsMap == null) return newExpression
val operandTypeDescriptor = newExpression.typeOperand.constructor.declarationDescriptor
if (operandTypeDescriptor !is TypeParameterDescriptor) return newExpression // It is not TypeParameter - do nothing
val typeNew = typeArgsMap[operandTypeDescriptor]!!
val startOffset = newExpression.startOffset
val endOffset = newExpression.endOffset
val type = newExpression.type // TODO
val operator = newExpression.operator
val argument = newExpression.argument
val type = getTypeOperatorReturnType(operator, typeNew)
return IrTypeOperatorCallImpl(startOffset, endOffset, type, operator, typeNew, argument)
}
+5
View File
@@ -1504,6 +1504,11 @@ task inline22(type: RunKonanTest) {
source = "codegen/inline/inline22.kt"
}
task inline23(type: RunKonanTest) {
goldValue = "33\n"
source = "codegen/inline/inline23.kt"
}
kotlinNativeInterop {
sysstat {
pkg 'sysstat'
@@ -0,0 +1,11 @@
inline fun <reified T> foo(i2: Any): T {
return i2 as T
}
fun bar(i1: Int): Int {
return foo<Int>(i1)
}
fun main(args: Array<String>) {
println(bar(33))
}