[IR] Drop excess containingDeclaration parameter from IR checker

This commit is contained in:
Ivan Kylchik
2023-04-27 18:01:55 +02:00
committed by Space Team
parent 1fd8ef801e
commit 82a111250b
4 changed files with 28 additions and 22 deletions
@@ -116,7 +116,11 @@ internal fun createTempClass(name: Name, origin: IrDeclarationOrigin = TEMP_CLAS
internal fun IrFunction.createGetField(): IrExpression {
val backingField = (this as IrSimpleFunction).correspondingPropertySymbol?.owner?.backingField!!
val receiver = dispatchReceiverParameter ?: extensionReceiverParameter
return IrGetFieldImpl(SYNTHETIC_OFFSET, SYNTHETIC_OFFSET, backingField.symbol, backingField.type, receiver?.createGetValue())
return backingField.createGetField(receiver)
}
internal fun IrField.createGetField(receiver: IrValueParameter? = null): IrGetField {
return IrGetFieldImpl(SYNTHETIC_OFFSET, SYNTHETIC_OFFSET, this.symbol, this.type, receiver?.createGetValue())
}
internal fun List<IrStatement>.wrapWithBlockBody(): IrBlockBody {
@@ -20,11 +20,10 @@ import org.jetbrains.kotlin.ir.util.statements
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
class IrCompileTimeChecker(
containingDeclaration: IrElement? = null,
private val mode: EvaluationMode,
private val interpreterConfiguration: IrInterpreterConfiguration,
) : IrElementVisitor<Boolean, Nothing?> {
private val visitedStack = mutableListOf<IrElement>().apply { if (containingDeclaration != null) add(containingDeclaration) }
private val visitedStack = mutableListOf<IrElement>()
private inline fun IrElement.asVisited(crossinline block: () -> Boolean): Boolean {
visitedStack += this
@@ -189,22 +188,24 @@ class IrCompileTimeChecker(
return owner.origin == IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB && owner.isStatic && owner.isFinal &&
(owner.type.isPrimitiveType() || owner.type.isStringClassType())
}
return when {
// TODO fix later; used it here because java boolean resolves very strange,
// its type is flexible (so its not primitive) and there is no initializer at backing field
fqName == "java.lang.Boolean.FALSE" || fqName == "java.lang.Boolean.TRUE" -> true
isJavaStaticWithPrimitiveOrString() -> owner.initializer?.accept(this, data) == true
expression.receiver == null -> property?.isConst == true && owner.initializer?.accept(this, null) == true
owner.origin == IrDeclarationOrigin.PROPERTY_BACKING_FIELD && property?.isConst == true -> {
val receiverComputable = (expression.receiver?.accept(this, null) ?: true)
|| expression.isAccessToNotNullableObject()
val initializerComputable = owner.initializer?.accept(this, null) ?: false
receiverComputable && initializerComputable
}
else -> {
val declarations = owner.parent.getInnerDeclarations()
val getter = declarations.filterIsInstance<IrProperty>().singleOrNull { it == property }?.getter ?: return false
visitedStack.contains(getter)
return owner.asVisited {
when {
// TODO fix later; used it here because java boolean resolves very strange,
// its type is flexible (so its not primitive) and there is no initializer at backing field
fqName == "java.lang.Boolean.FALSE" || fqName == "java.lang.Boolean.TRUE" -> true
isJavaStaticWithPrimitiveOrString() -> owner.initializer?.accept(this, data) == true
expression.receiver == null -> property?.isConst == true && owner.initializer?.accept(this, null) == true
owner.origin == IrDeclarationOrigin.PROPERTY_BACKING_FIELD && property?.isConst == true -> {
val receiverComputable = (expression.receiver?.accept(this, null) ?: true)
|| expression.isAccessToNotNullableObject()
val initializerComputable = owner.initializer?.accept(this, null) ?: false
receiverComputable && initializerComputable
}
else -> {
val declarations = owner.parent.getInnerDeclarations()
val getter = declarations.filterIsInstance<IrProperty>().singleOrNull { it == property }?.getter ?: return@asVisited false
visitedStack.contains(getter)
}
}
}
}
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrStringConcatenationImpl
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
import org.jetbrains.kotlin.ir.interpreter.createGetField
import kotlin.math.max
import kotlin.math.min
@@ -40,7 +41,8 @@ internal class IrConstExpressionTransformer(
val isConst = declaration.correspondingPropertySymbol?.owner?.isConst == true
if (!isConst) return super.visitField(declaration, data)
if (expression.canBeInterpreted(declaration, interpreter.environment.configuration.copy(treatFloatInSpecialWay = false))) {
val getField = declaration.createGetField()
if (getField.canBeInterpreted(interpreter.environment.configuration.copy(treatFloatInSpecialWay = false))) {
initializer.expression = expression.interpret(failAsError = true)
}
@@ -77,11 +77,10 @@ internal abstract class IrConstTransformer(
}
protected fun IrExpression.canBeInterpreted(
containingDeclaration: IrElement? = null,
configuration: IrInterpreterConfiguration = interpreter.environment.configuration
): Boolean {
return try {
this.accept(IrCompileTimeChecker(containingDeclaration, mode, configuration), null) ||
this.accept(IrCompileTimeChecker(mode, configuration), null) ||
this.accept(IrCompileTimeNameChecker(mode), null)
} catch (e: Throwable) {
if (suppressExceptions) {