From dd3f8ecaacde15f96436cafb2d3fe376ef349c44 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Thu, 18 Jul 2019 12:29:33 +0300 Subject: [PATCH] IR BE Common: Provisional IrFunctionExpression lowering Transforms IrFunctionExpression elements to local function references, as it was done before IrFunctionExpression introduction. --- .../ProvisionalFunctionExpressionLowering.kt | 46 +++++++++++++++++++ .../kotlin/ir/backend/js/JsLoweringPhases.kt | 8 ++++ .../jetbrains/kotlin/backend/jvm/JvmLower.kt | 12 ++++- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ProvisionalFunctionExpressionLowering.kt diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ProvisionalFunctionExpressionLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ProvisionalFunctionExpressionLowering.kt new file mode 100644 index 00000000000..e7ab960fccd --- /dev/null +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ProvisionalFunctionExpressionLowering.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.backend.common.lower + +import org.jetbrains.kotlin.backend.common.FileLoweringPass +import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.expressions.IrExpression +import org.jetbrains.kotlin.ir.expressions.IrFunctionExpression +import org.jetbrains.kotlin.ir.expressions.impl.IrBlockImpl +import org.jetbrains.kotlin.ir.expressions.impl.IrFunctionReferenceImpl +import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid +import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid + +class ProvisionalFunctionExpressionLowering : + IrElementTransformerVoid(), + FileLoweringPass { + + override fun lower(irFile: IrFile) { + irFile.transformChildrenVoid(this) + } + + override fun visitFunctionExpression(expression: IrFunctionExpression): IrExpression { + expression.transformChildrenVoid(this) + + val startOffset = expression.startOffset + val endOffset = expression.endOffset + val type = expression.type + val origin = expression.origin + val function = expression.function + + return IrBlockImpl( + startOffset, endOffset, type, origin, + listOf( + function, + IrFunctionReferenceImpl( + startOffset, endOffset, type, + function.symbol, function.descriptor, 0, + origin + ) + ) + ) + } +} \ No newline at end of file diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt index a92ca8cf648..65b85d0a968 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt @@ -98,6 +98,13 @@ private val lateinitLoweringPhase = makeJsModulePhase( description = "Insert checks for lateinit field references" ) +// TODO make all lambda-related stuff work with IrFunctionExpression and drop this phase +private val provisionalFunctionExpressionPhase = makeJsModulePhase( + { ProvisionalFunctionExpressionLowering() }, + name = "FunctionExpression", + description = "Transform IrFunctionExpression to a local function reference" +) + private val arrayConstructorPhase = makeJsModulePhase( ::ArrayConstructorLowering, name = "ArrayConstructor", @@ -389,6 +396,7 @@ val jsPhases = namedIrModulePhase( lower = validateIrBeforeLowering then testGenerationPhase then expectDeclarationsRemovingPhase then + provisionalFunctionExpressionPhase then arrayConstructorPhase then functionInliningPhase then lateinitLoweringPhase then diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt index 1bbd9cda73d..ae90e2b7bcb 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt @@ -36,6 +36,13 @@ private fun makePatchParentsPhase(number: Int) = namedIrFilePhase( nlevels = 0 ) +// TODO make all lambda-related stuff work with IrFunctionExpression and drop this phase +private val provisionalFunctionExpressionPhase = makeIrFilePhase( + { ProvisionalFunctionExpressionLowering() }, + name = "FunctionExpression", + description = "Transform IrFunctionExpression to a local function reference" +) + private val arrayConstructorPhase = makeIrFilePhase( ::ArrayConstructorLowering, name = "ArrayConstructor", @@ -91,8 +98,9 @@ private val innerClassesPhase = makeIrFilePhase( prerequisite = setOf(localDeclarationsPhase) ) -private val jvmFilePhases = inventNamesForLocalClassesPhase then - +private val jvmFilePhases = + provisionalFunctionExpressionPhase then + inventNamesForLocalClassesPhase then kCallableNamePropertyPhase then arrayConstructorPhase then