[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 { internal fun IrFunction.createGetField(): IrExpression {
val backingField = (this as IrSimpleFunction).correspondingPropertySymbol?.owner?.backingField!! val backingField = (this as IrSimpleFunction).correspondingPropertySymbol?.owner?.backingField!!
val receiver = dispatchReceiverParameter ?: extensionReceiverParameter 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 { internal fun List<IrStatement>.wrapWithBlockBody(): IrBlockBody {
@@ -20,11 +20,10 @@ import org.jetbrains.kotlin.ir.util.statements
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
class IrCompileTimeChecker( class IrCompileTimeChecker(
containingDeclaration: IrElement? = null,
private val mode: EvaluationMode, private val mode: EvaluationMode,
private val interpreterConfiguration: IrInterpreterConfiguration, private val interpreterConfiguration: IrInterpreterConfiguration,
) : IrElementVisitor<Boolean, Nothing?> { ) : 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 { private inline fun IrElement.asVisited(crossinline block: () -> Boolean): Boolean {
visitedStack += this visitedStack += this
@@ -189,22 +188,24 @@ class IrCompileTimeChecker(
return owner.origin == IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB && owner.isStatic && owner.isFinal && return owner.origin == IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB && owner.isStatic && owner.isFinal &&
(owner.type.isPrimitiveType() || owner.type.isStringClassType()) (owner.type.isPrimitiveType() || owner.type.isStringClassType())
} }
return when { return owner.asVisited {
// TODO fix later; used it here because java boolean resolves very strange, when {
// its type is flexible (so its not primitive) and there is no initializer at backing field // TODO fix later; used it here because java boolean resolves very strange,
fqName == "java.lang.Boolean.FALSE" || fqName == "java.lang.Boolean.TRUE" -> true // its type is flexible (so its not primitive) and there is no initializer at backing field
isJavaStaticWithPrimitiveOrString() -> owner.initializer?.accept(this, data) == true fqName == "java.lang.Boolean.FALSE" || fqName == "java.lang.Boolean.TRUE" -> true
expression.receiver == null -> property?.isConst == true && owner.initializer?.accept(this, null) == true isJavaStaticWithPrimitiveOrString() -> owner.initializer?.accept(this, data) == true
owner.origin == IrDeclarationOrigin.PROPERTY_BACKING_FIELD && property?.isConst == true -> { expression.receiver == null -> property?.isConst == true && owner.initializer?.accept(this, null) == true
val receiverComputable = (expression.receiver?.accept(this, null) ?: true) owner.origin == IrDeclarationOrigin.PROPERTY_BACKING_FIELD && property?.isConst == true -> {
|| expression.isAccessToNotNullableObject() val receiverComputable = (expression.receiver?.accept(this, null) ?: true)
val initializerComputable = owner.initializer?.accept(this, null) ?: false || expression.isAccessToNotNullableObject()
receiverComputable && initializerComputable val initializerComputable = owner.initializer?.accept(this, null) ?: false
} receiverComputable && initializerComputable
else -> { }
val declarations = owner.parent.getInnerDeclarations() else -> {
val getter = declarations.filterIsInstance<IrProperty>().singleOrNull { it == property }?.getter ?: return false val declarations = owner.parent.getInnerDeclarations()
visitedStack.contains(getter) 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.expressions.impl.IrStringConcatenationImpl
import org.jetbrains.kotlin.ir.interpreter.IrInterpreter import org.jetbrains.kotlin.ir.interpreter.IrInterpreter
import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
import org.jetbrains.kotlin.ir.interpreter.createGetField
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
@@ -40,7 +41,8 @@ internal class IrConstExpressionTransformer(
val isConst = declaration.correspondingPropertySymbol?.owner?.isConst == true val isConst = declaration.correspondingPropertySymbol?.owner?.isConst == true
if (!isConst) return super.visitField(declaration, data) 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) initializer.expression = expression.interpret(failAsError = true)
} }
@@ -77,11 +77,10 @@ internal abstract class IrConstTransformer(
} }
protected fun IrExpression.canBeInterpreted( protected fun IrExpression.canBeInterpreted(
containingDeclaration: IrElement? = null,
configuration: IrInterpreterConfiguration = interpreter.environment.configuration configuration: IrInterpreterConfiguration = interpreter.environment.configuration
): Boolean { ): Boolean {
return try { return try {
this.accept(IrCompileTimeChecker(containingDeclaration, mode, configuration), null) || this.accept(IrCompileTimeChecker(mode, configuration), null) ||
this.accept(IrCompileTimeNameChecker(mode), null) this.accept(IrCompileTimeNameChecker(mode), null)
} catch (e: Throwable) { } catch (e: Throwable) {
if (suppressExceptions) { if (suppressExceptions) {