Move ConstEvaluationLowering into folder for common lowerings

#KT-56023
This commit is contained in:
Ivan Kylchik
2023-02-03 14:49:00 +01:00
committed by Space Team
parent c14dcfce4f
commit b58de2f8da
5 changed files with 60 additions and 56 deletions
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2023 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.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.expressions.IrErrorExpression
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
import org.jetbrains.kotlin.ir.interpreter.IrInterpreterConfiguration
import org.jetbrains.kotlin.ir.interpreter.IrInterpreterEnvironment
import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
import org.jetbrains.kotlin.ir.interpreter.checker.IrConstTransformer
class ConstEvaluationLowering(
val context: CommonBackendContext,
private val suppressErrors: Boolean = false,
private val onWarning: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> },
private val onError: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> },
) : FileLoweringPass {
val configuration = IrInterpreterConfiguration(printOnlyExceptionMessage = true)
val interpreter = IrInterpreter(IrInterpreterEnvironment(context.irBuiltIns, configuration), emptyMap())
override fun lower(irFile: IrFile) {
val transformer = IrConstTransformer(
interpreter, irFile, mode = EvaluationMode.ONLY_INTRINSIC_CONST, onWarning, onError, suppressErrors
)
irFile.transformChildren(transformer, null)
}
}
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.load.java.JavaDescriptorVisibilities
import org.jetbrains.kotlin.name.NameUtils
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmBackendErrors
private var patchParentPhases = 0
@@ -315,6 +316,25 @@ internal val functionInliningPhase = makeIrModulePhase(
)
)
val constEvaluationPhase = makeIrModulePhase<JvmBackendContext>(
{
ConstEvaluationLowering(
it,
it.configuration.getBoolean(JVMConfigurationKeys.IGNORE_CONST_OPTIMIZATION_ERRORS),
{ irFile, element, warning ->
it.ktDiagnosticReporter.at(element, irFile)
.report(JvmBackendErrors.EXCEPTION_IN_CONST_EXPRESSION, warning.description)
},
{ irFile, element, error ->
it.ktDiagnosticReporter.at(element, irFile)
.report(JvmBackendErrors.EXCEPTION_IN_CONST_VAL_INITIALIZER, error.description)
}
)
},
name = "ConstEvaluationLowering",
description = "Evaluate functions that are marked as `IntrinsicConstEvaluation`"
)
private val jvmFilePhases = listOf(
typeAliasAnnotationMethodsPhase,
provisionalFunctionExpressionPhase,
@@ -1,51 +0,0 @@
/*
* Copyright 2010-2021 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.jvm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.phaser.makeIrModulePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.expressions.IrErrorExpression
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
import org.jetbrains.kotlin.ir.interpreter.IrInterpreterConfiguration
import org.jetbrains.kotlin.ir.interpreter.IrInterpreterEnvironment
import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
import org.jetbrains.kotlin.ir.interpreter.checker.IrConstTransformer
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmBackendErrors
val constEvaluationPhase = makeIrModulePhase(
::ConstEvaluationLowering,
name = "ConstEvaluationLowering",
description = "Evaluate functions that are marked as `IntrinsicConstEvaluation`"
)
// TODO make context common
class ConstEvaluationLowering(val context: JvmBackendContext) : FileLoweringPass {
val configuration = IrInterpreterConfiguration(printOnlyExceptionMessage = true)
val interpreter = IrInterpreter(IrInterpreterEnvironment(context.irBuiltIns, configuration), emptyMap())
override fun lower(irFile: IrFile) {
fun onError(element: IrElement, error: IrErrorExpression) {
context.ktDiagnosticReporter.at(element, irFile)
.report(JvmBackendErrors.EXCEPTION_IN_CONST_VAL_INITIALIZER, error.description)
}
fun onWarning(element: IrElement, warning: IrErrorExpression) {
context.ktDiagnosticReporter.at(element, irFile)
.report(JvmBackendErrors.EXCEPTION_IN_CONST_EXPRESSION, warning.description)
}
val suppressErrors = context.configuration.getBoolean(JVMConfigurationKeys.IGNORE_CONST_OPTIMIZATION_ERRORS)
val transformer = IrConstTransformer(
interpreter, irFile, mode = EvaluationMode.ONLY_INTRINSIC_CONST, ::onWarning, ::onError, suppressErrors
)
irFile.transformChildren(transformer, null)
}
}
@@ -25,13 +25,13 @@ class IrConstTransformer(
private val interpreter: IrInterpreter,
private val irFile: IrFile,
private val mode: EvaluationMode,
private val onWarning: (IrElement, IrErrorExpression) -> Unit = { _, _ -> },
private val onError: (IrElement, IrErrorExpression) -> Unit = { _, _ -> },
private val onWarning: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> },
private val onError: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> },
private val suppressExceptions: Boolean = false,
) : IrElementTransformerVoid() {
private fun IrExpression.warningIfError(original: IrExpression): IrExpression {
if (this is IrErrorExpression) {
onWarning(original, this)
onWarning(irFile, original, this)
return original
}
return this
@@ -39,7 +39,7 @@ class IrConstTransformer(
private fun IrExpression.reportIfError(original: IrExpression): IrExpression {
if (this is IrErrorExpression) {
onError(original, this)
onError(irFile, original, this)
return when (mode) {
// need to pass any const value to be able to get some bytecode and then report error
EvaluationMode.ONLY_INTRINSIC_CONST -> IrConstImpl.constNull(startOffset, endOffset, type)
@@ -5,7 +5,7 @@
package org.jetbrains.kotlin.test.runners.codegen
import org.jetbrains.kotlin.backend.jvm.lower.constEvaluationPhase
import org.jetbrains.kotlin.backend.jvm.constEvaluationPhase
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.test.Constructor
import org.jetbrains.kotlin.test.TargetBackend