[IR] Add special preprocessors for ir interpreter
These preprocessors allow us to modify IR at first and only after that try to evaluate. With this we can drop `KCallableNamePropertyLowering`.
This commit is contained in:
-1
@@ -125,7 +125,6 @@ private fun unfoldValueParameters(expression: IrFunctionAccessExpression, enviro
|
||||
callStack.pushState(enumEntry.toState(environment.irBuiltIns))
|
||||
return
|
||||
}
|
||||
// TODO do the same thing but for "KCallable.name" with "this" as receiver
|
||||
|
||||
val hasDefaults = (0 until expression.valueArgumentsCount).any { expression.getValueArgument(it) == null }
|
||||
if (hasDefaults) {
|
||||
|
||||
+2
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.interpreter.checker
|
||||
|
||||
import org.jetbrains.kotlin.ir.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.interpreter.IrInterpreterConfiguration
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
|
||||
|
||||
@@ -13,5 +14,6 @@ interface IrInterpreterChecker : IrElementVisitor<Boolean, IrInterpreterCheckerD
|
||||
}
|
||||
|
||||
class IrInterpreterCheckerData(
|
||||
val irBuiltIns: IrBuiltIns,
|
||||
val interpreterConfiguration: IrInterpreterConfiguration
|
||||
)
|
||||
+40
-15
@@ -5,33 +5,58 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.interpreter.checker
|
||||
|
||||
import org.jetbrains.kotlin.ir.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrProperty
|
||||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrCall
|
||||
import org.jetbrains.kotlin.ir.expressions.IrCallableReference
|
||||
import org.jetbrains.kotlin.ir.expressions.IrGetEnumValue
|
||||
import org.jetbrains.kotlin.ir.expressions.IrStringConcatenation
|
||||
import org.jetbrains.kotlin.ir.util.isSubclassOf
|
||||
|
||||
class IrInterpreterNameChecker(
|
||||
override val mode: EvaluationMode
|
||||
override val mode: EvaluationMode,
|
||||
) : IrInterpreterChecker {
|
||||
private fun IrCall.isIntrinsicConstEvaluationNameProperty(): Boolean {
|
||||
val owner = this.symbol.owner
|
||||
if (owner.extensionReceiverParameter != null || owner.valueParameters.isNotEmpty()) return false
|
||||
val property = (owner as? IrSimpleFunction)?.correspondingPropertySymbol?.owner ?: return false
|
||||
return mode.canEvaluateFunction(owner) && property.name.asString() == "name"
|
||||
}
|
||||
|
||||
override fun visitElement(element: IrElement, data: IrInterpreterCheckerData) = false
|
||||
|
||||
override fun visitCall(expression: IrCall, data: IrInterpreterCheckerData): Boolean {
|
||||
if (!expression.isIntrinsicConstEvaluationNameProperty()) return false
|
||||
return when (val receiver = expression.dispatchReceiver) {
|
||||
is IrCallableReference<*> -> (receiver.dispatchReceiver == null || receiver.dispatchReceiver is IrGetObjectValue) && receiver.extensionReceiver == null
|
||||
is IrGetEnumValue -> true
|
||||
else -> false
|
||||
}
|
||||
val owner = expression.symbol.owner
|
||||
if (!mode.canEvaluateFunction(owner)) return false
|
||||
|
||||
return expression.isKCallableNameCall(data.irBuiltIns) || expression.isEnumName()
|
||||
}
|
||||
|
||||
override fun visitStringConcatenation(expression: IrStringConcatenation, data: IrInterpreterCheckerData): Boolean {
|
||||
val possibleNameCall = expression.arguments.singleOrNull() as? IrCall ?: return false
|
||||
return possibleNameCall.accept(this, data)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun IrCall.isKCallableNameCall(irBuiltIns: IrBuiltIns): Boolean {
|
||||
if (this.dispatchReceiver !is IrCallableReference<*>) return false
|
||||
|
||||
val directMember = this.symbol.owner.let {
|
||||
(it as? IrSimpleFunction)?.correspondingPropertySymbol?.owner ?: it
|
||||
}
|
||||
|
||||
val irClass = directMember.parent as? IrClass ?: return false
|
||||
if (!irClass.isSubclassOf(irBuiltIns.kCallableClass.owner)) return false
|
||||
|
||||
val name = when (directMember) {
|
||||
is IrSimpleFunction -> directMember.name
|
||||
is IrProperty -> directMember.name
|
||||
else -> throw AssertionError("Should be IrSimpleFunction or IrProperty, got $directMember")
|
||||
}
|
||||
return name.asString() == "name"
|
||||
}
|
||||
|
||||
private fun IrCall.isEnumName(): Boolean {
|
||||
val owner = this.symbol.owner
|
||||
if (owner.extensionReceiverParameter != null || owner.valueParameters.isNotEmpty()) return false
|
||||
val property = (owner as? IrSimpleFunction)?.correspondingPropertySymbol?.owner ?: return false
|
||||
return this.dispatchReceiver is IrGetEnumValue && property.name.asString() == "name"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.ir.interpreter.preprocessor
|
||||
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.expressions.IrCall
|
||||
import org.jetbrains.kotlin.ir.expressions.IrCallableReference
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
|
||||
import org.jetbrains.kotlin.ir.interpreter.checker.IrInterpreterNameChecker.Companion.isKCallableNameCall
|
||||
|
||||
// Note: this class still will not allow us to evaluate things like `A()::a.name + `A()::b.name`.
|
||||
// This code will be optimized but not completely turned into "ab" result.
|
||||
class IrInterpreterKCallableNamePreprocessor : IrInterpreterPreprocessor {
|
||||
override fun visitCall(expression: IrCall, data: IrInterpreterPreprocessorData): IrElement {
|
||||
if (!data.mode.canEvaluateFunction(expression.symbol.owner)) return super.visitCall(expression, data)
|
||||
if (!expression.isKCallableNameCall(data.irBuiltIns)) return super.visitCall(expression, data)
|
||||
|
||||
val callableReference = expression.dispatchReceiver as? IrCallableReference<*> ?: return super.visitCall(expression, data)
|
||||
|
||||
// receiver is needed for bound callable reference
|
||||
val receiver = callableReference.dispatchReceiver ?: callableReference.extensionReceiver ?: return expression
|
||||
callableReference.dispatchReceiver = null
|
||||
callableReference.extensionReceiver = null
|
||||
|
||||
return IrCompositeImpl(
|
||||
expression.startOffset, expression.endOffset, expression.type, origin = null, statements = listOf(receiver, expression)
|
||||
)
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.ir.interpreter.preprocessor
|
||||
|
||||
import org.jetbrains.kotlin.ir.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
|
||||
|
||||
interface IrInterpreterPreprocessor: IrElementTransformer<IrInterpreterPreprocessorData> {
|
||||
fun preprocess(file: IrFile, data: IrInterpreterPreprocessorData): IrFile {
|
||||
return file.transform(this, data)
|
||||
}
|
||||
}
|
||||
|
||||
class IrInterpreterPreprocessorData(
|
||||
val mode: EvaluationMode,
|
||||
val irBuiltIns: IrBuiltIns
|
||||
)
|
||||
+19
-8
@@ -16,11 +16,18 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
|
||||
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
|
||||
import org.jetbrains.kotlin.ir.interpreter.IrInterpreterConfiguration
|
||||
import org.jetbrains.kotlin.ir.interpreter.checker.*
|
||||
import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
|
||||
import org.jetbrains.kotlin.ir.interpreter.checker.IrInterpreterCheckerData
|
||||
import org.jetbrains.kotlin.ir.interpreter.checker.IrInterpreterCommonChecker
|
||||
import org.jetbrains.kotlin.ir.interpreter.checker.IrInterpreterNameChecker
|
||||
import org.jetbrains.kotlin.ir.interpreter.preprocessor.IrInterpreterKCallableNamePreprocessor
|
||||
import org.jetbrains.kotlin.ir.interpreter.preprocessor.IrInterpreterPreprocessorData
|
||||
import org.jetbrains.kotlin.ir.interpreter.toConstantValue
|
||||
import org.jetbrains.kotlin.ir.util.dump
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
|
||||
|
||||
private val preprocessors = setOf(IrInterpreterKCallableNamePreprocessor())
|
||||
|
||||
fun IrFile.transformConst(
|
||||
interpreter: IrInterpreter,
|
||||
mode: EvaluationMode,
|
||||
@@ -29,18 +36,22 @@ fun IrFile.transformConst(
|
||||
onError: (IrFile, IrElement, IrErrorExpression) -> Unit = { _, _, _ -> },
|
||||
suppressExceptions: Boolean = false,
|
||||
) {
|
||||
val preprocessedFile = preprocessors.fold(this) { acc, preprocessor ->
|
||||
preprocessor.preprocess(acc, IrInterpreterPreprocessorData(mode, interpreter.irBuiltIns))
|
||||
}
|
||||
|
||||
val irConstExpressionTransformer = IrConstExpressionTransformer(
|
||||
interpreter, this, mode, evaluatedConstTracker, onWarning, onError, suppressExceptions
|
||||
interpreter, preprocessedFile, mode, evaluatedConstTracker, onWarning, onError, suppressExceptions
|
||||
)
|
||||
val irConstDeclarationAnnotationTransformer = IrConstDeclarationAnnotationTransformer(
|
||||
interpreter, this, mode, evaluatedConstTracker, onWarning, onError, suppressExceptions
|
||||
interpreter, preprocessedFile, mode, evaluatedConstTracker, onWarning, onError, suppressExceptions
|
||||
)
|
||||
val irConstTypeAnnotationTransformer = IrConstTypeAnnotationTransformer(
|
||||
interpreter, this, mode, evaluatedConstTracker, onWarning, onError, suppressExceptions
|
||||
interpreter, preprocessedFile, mode, evaluatedConstTracker, onWarning, onError, suppressExceptions
|
||||
)
|
||||
this.transform(irConstExpressionTransformer, null)
|
||||
this.transform(irConstDeclarationAnnotationTransformer, null)
|
||||
this.transform(irConstTypeAnnotationTransformer, null)
|
||||
preprocessedFile.transform(irConstExpressionTransformer, null)
|
||||
preprocessedFile.transform(irConstDeclarationAnnotationTransformer, null)
|
||||
preprocessedFile.transform(irConstTypeAnnotationTransformer, null)
|
||||
}
|
||||
|
||||
// Note: We are using `IrElementTransformer` here instead of `IrElementTransformerVoid` to avoid conflicts with `IrTypeVisitorVoid`
|
||||
@@ -83,7 +94,7 @@ internal abstract class IrConstTransformer(
|
||||
configuration: IrInterpreterConfiguration = interpreter.environment.configuration
|
||||
): Boolean {
|
||||
return try {
|
||||
checkers.any { this.accept(it, IrInterpreterCheckerData(configuration)) }
|
||||
checkers.any { this.accept(it, IrInterpreterCheckerData(interpreter.irBuiltIns, configuration)) }
|
||||
} catch (e: Throwable) {
|
||||
if (suppressExceptions) {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user