From 864bd9a212c7e20858ebffa3e86a85bd101655e5 Mon Sep 17 00:00:00 2001 From: Konstantin Anisimov Date: Fri, 17 Mar 2017 16:54:28 +0700 Subject: [PATCH] Support cast type operations in inline --- .../backend/konan/lower/FunctionInlining.kt | 22 ++++++++++++++++--- backend.native/tests/build.gradle | 5 +++++ .../tests/codegen/inline/inline23.kt | 11 ++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 backend.native/tests/codegen/inline/inline23.kt diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt index 9775885b7ce..4d1e02071de 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/FunctionInlining.kt @@ -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) } diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 8064b5a8234..24c46406992 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -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' diff --git a/backend.native/tests/codegen/inline/inline23.kt b/backend.native/tests/codegen/inline/inline23.kt new file mode 100644 index 00000000000..bf5a0385059 --- /dev/null +++ b/backend.native/tests/codegen/inline/inline23.kt @@ -0,0 +1,11 @@ +inline fun foo(i2: Any): T { + return i2 as T +} + +fun bar(i1: Int): Int { + return foo(i1) +} + +fun main(args: Array) { + println(bar(33)) +}