[IR] Allow to fold IrGetField expressions

#KT-58923
This commit is contained in:
Ivan Kylchik
2023-05-23 11:04:48 +02:00
committed by Space Team
parent b8b8b7fd3a
commit ec4cf40f6f
10 changed files with 19 additions and 84 deletions
@@ -35,14 +35,6 @@ internal class IrConstDeclarationAnnotationTransformer(
override fun visitDeclaration(declaration: IrDeclarationBase, data: Nothing?): IrStatement {
transformAnnotations(declaration)
if (declaration is IrClass && declaration.kind == ClassKind.ANNOTATION_CLASS) {
declaration.primaryConstructor?.valueParameters?.forEach {
val defaultExpression = it.defaultValue?.expression ?: return@forEach
if (defaultExpression.canBeInterpreted()) {
it.defaultValue?.expression = transformAnnotationArgument(defaultExpression, it)
}
}
}
return super.visitDeclaration(declaration, data)
}
}
@@ -8,8 +8,10 @@ package org.jetbrains.kotlin.ir.interpreter.transformer
import org.jetbrains.kotlin.constant.EvaluatedConstTracker
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrStringConcatenationImpl
@@ -51,6 +53,19 @@ internal class IrConstExpressionTransformer(
return super.visitField(declaration, data)
}
override fun visitFunction(declaration: IrFunction, data: Nothing?): IrStatement {
// It is useless to visit default accessor and if we do that we could render excess information for `IrGetField`
if (declaration.origin == IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR) return declaration
return super.visitFunction(declaration, data)
}
override fun visitGetField(expression: IrGetField, data: Nothing?): IrExpression {
if (expression.canBeInterpreted()) {
return expression.interpret(failAsError = false)
}
return super.visitGetField(expression, data)
}
override fun visitStringConcatenation(expression: IrStringConcatenation, data: Nothing?): IrExpression {
fun IrExpression.wrapInStringConcat(): IrExpression = IrStringConcatenationImpl(
this.startOffset, this.endOffset, expression.type, listOf(this@wrapInStringConcat)
@@ -10,7 +10,7 @@ import kotlin.reflect.KClass
@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class IntegerNumberValid(
val message: String = <!EVALUATED("Has illegal integer number value")!>"Has illegal integer number value"<!>,
val message: String = "Has illegal integer number value",
val groups: Array<KClass<*>> = [],
val minimum: Long = Long.<!EVALUATED("-9223372036854775808")!>MIN_VALUE<!>,
@@ -9,7 +9,7 @@ FILE fqName:<root> fileName:/Child.kt
$this: VALUE_PARAMETER name:<this> type:<root>.Child
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun foo (): kotlin.String declared in <root>.Child'
GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:publicStaticField type:kotlin.String visibility:public [final,static]' type=kotlin.String origin=GET_PROPERTY
CONST String type=kotlin.String value="OK"
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in javapackage.PublicParentClass
@@ -1,42 +0,0 @@
FILE fqName:interop fileName:/Definitions.kt
CLASS OBJECT name:Definitions modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:interop.Definitions
CONSTRUCTOR visibility:private <> () returnType:interop.Definitions [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Definitions modality:FINAL visibility:public superTypes:[kotlin.Any]'
PROPERTY name:KT_CONSTANT visibility:public modality:FINAL [const,val]
FIELD PROPERTY_BACKING_FIELD name:KT_CONSTANT type:kotlin.String visibility:public [final]
EXPRESSION_BODY
CONST String type=kotlin.String value="constant"
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-KT_CONSTANT> visibility:public modality:FINAL <> ($this:interop.Definitions) returnType:kotlin.String
correspondingProperty: PROPERTY name:KT_CONSTANT visibility:public modality:FINAL [const,val]
$this: VALUE_PARAMETER name:<this> type:interop.Definitions
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-KT_CONSTANT> (): kotlin.String declared in interop.Definitions'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:KT_CONSTANT type:kotlin.String visibility:public [final]' type=kotlin.String origin=null
receiver: GET_VAR '<this>: interop.Definitions declared in interop.Definitions.<get-KT_CONSTANT>' type=interop.Definitions origin=null
PROPERTY name:ktValue visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:ktValue type:kotlin.String visibility:private [final]
EXPRESSION_BODY
GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:CONSTANT type:kotlin.String visibility:public [final,static]' type=kotlin.String origin=GET_PROPERTY
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-ktValue> visibility:public modality:FINAL <> ($this:interop.Definitions) returnType:kotlin.String
correspondingProperty: PROPERTY name:ktValue visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:interop.Definitions
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-ktValue> (): kotlin.String declared in interop.Definitions'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:ktValue type:kotlin.String visibility:private [final]' type=kotlin.String origin=null
receiver: GET_VAR '<this>: interop.Definitions declared in interop.Definitions.<get-ktValue>' type=interop.Definitions origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
@@ -1,18 +0,0 @@
package interop
object Definitions {
private constructor() /* primary */ {
super/*Any*/()
/* <init>() */
}
const val KT_CONSTANT: String
field = "constant"
get
val ktValue: String
field = #CONSTANT
get
}
+1
View File
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// TARGET_BACKEND: JVM
// FILE: Definitions.kt
// IR_FILE: kt29833.txt
@@ -1,11 +0,0 @@
FILE fqName:<root> fileName:/constFromBuiltins.kt
PROPERTY name:test visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:test type:kotlin.Int visibility:private [final,static]
EXPRESSION_BODY
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:MIN_VALUE type:kotlin.Int visibility:public [final]' type=kotlin.Int origin=null
receiver: GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Companion modality:FINAL visibility:public [companion] superTypes:[kotlin.Any]' type=kotlin.Int.Companion
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test> visibility:public modality:FINAL <> () returnType:kotlin.Int
correspondingProperty: PROPERTY name:test visibility:public modality:FINAL [val]
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-test> (): kotlin.Int declared in <root>'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test type:kotlin.Int visibility:private [final,static]' type=kotlin.Int origin=null
@@ -1,3 +0,0 @@
val test: Int
field = Companion.#MIN_VALUE
get
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// DUMP_EXTERNAL_CLASS: kotlin/Int
// TARGET_BACKEND: JVM_IR