Support arguments' evaluation of primitive arrays in fir2ir transformer

This commit is contained in:
Ivan Kylchik
2021-09-10 14:52:35 +03:00
committed by TeamCityServer
parent dd45714137
commit cc934d9574
4 changed files with 36 additions and 16 deletions
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.interpreter.checker.EvaluationMode
import org.jetbrains.kotlin.ir.interpreter.checker.IrCompileTimeChecker import org.jetbrains.kotlin.ir.interpreter.checker.IrCompileTimeChecker
import org.jetbrains.kotlin.ir.interpreter.toIrConst import org.jetbrains.kotlin.ir.interpreter.toIrConst
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.isPrimitiveArray
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
fun evaluateConstants(irModuleFragment: IrModuleFragment) { fun evaluateConstants(irModuleFragment: IrModuleFragment) {
@@ -98,8 +99,11 @@ class IrConstTransformer(private val interpreter: IrInterpreter, private val irF
} }
private fun IrExpression.convertToConstIfPossible(type: IrType): IrExpression { private fun IrExpression.convertToConstIfPossible(type: IrType): IrExpression {
if (this !is IrConst<*> || type is IrErrorType) return this return when {
if (type.isArray()) return this.convertToConstIfPossible((type as IrSimpleType).arguments.single().typeOrNull!!) this !is IrConst<*> || type is IrErrorType -> this
return this.value.toIrConst(type, this.startOffset, this.endOffset) type.isArray() -> this.convertToConstIfPossible((type as IrSimpleType).arguments.single().typeOrNull!!)
type.isPrimitiveArray() -> this.convertToConstIfPossible(this.type)
else -> this.value.toIrConst(type, this.startOffset, this.endOffset)
}
} }
} }
@@ -1,5 +1,4 @@
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME // WITH_RUNTIME
// FILE: A.java // FILE: A.java
@@ -1,8 +1,11 @@
FILE fqName:<root> fileName:/C.kt FILE fqName:<root> fileName:/C.kt
CLASS ANNOTATION_CLASS name:Anno modality:FINAL visibility:public superTypes:[kotlin.Annotation] CLASS ANNOTATION_CLASS name:Anno modality:OPEN visibility:public superTypes:[kotlin.Annotation]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Anno $this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Anno
CONSTRUCTOR visibility:public <> (token:kotlin.String) returnType:<root>.Anno [primary] CONSTRUCTOR visibility:public <> (token:kotlin.String) returnType:<root>.Anno [primary]
VALUE_PARAMETER name:token index:0 type:kotlin.String VALUE_PARAMETER name:token index:0 type:kotlin.String
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ANNOTATION_CLASS name:Anno modality:OPEN visibility:public superTypes:[kotlin.Annotation]'
PROPERTY name:token visibility:public modality:FINAL [val] PROPERTY name:token visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:token type:kotlin.String visibility:private [final] FIELD PROPERTY_BACKING_FIELD name:token type:kotlin.String visibility:private [final]
EXPRESSION_BODY EXPRESSION_BODY
@@ -80,10 +83,13 @@ FILE fqName:<root> fileName:/C.kt
overridden: overridden:
public final fun <get-ordinal> (): kotlin.Int declared in kotlin.Enum public final fun <get-ordinal> (): kotlin.Int declared in kotlin.Enum
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<E of kotlin.Enum> $this: VALUE_PARAMETER name:<this> type:kotlin.Enum<E of kotlin.Enum>
CLASS ANNOTATION_CLASS name:Annos modality:FINAL visibility:public superTypes:[kotlin.Annotation] CLASS ANNOTATION_CLASS name:Annos modality:OPEN visibility:public superTypes:[kotlin.Annotation]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Annos $this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Annos
CONSTRUCTOR visibility:public <> (value:kotlin.Array<<root>.Anno>) returnType:<root>.Annos [primary] CONSTRUCTOR visibility:public <> (value:kotlin.Array<<root>.Anno>) returnType:<root>.Annos [primary]
VALUE_PARAMETER name:value index:0 type:kotlin.Array<<root>.Anno> VALUE_PARAMETER name:value index:0 type:kotlin.Array<<root>.Anno>
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ANNOTATION_CLASS name:Annos modality:OPEN visibility:public superTypes:[kotlin.Annotation]'
PROPERTY name:value visibility:public modality:FINAL [val] PROPERTY name:value visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.Array<<root>.Anno> visibility:private [final] FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.Array<<root>.Anno> visibility:private [final]
EXPRESSION_BODY EXPRESSION_BODY
@@ -108,10 +114,13 @@ FILE fqName:<root> fileName:/C.kt
overridden: overridden:
public open fun toString (): kotlin.String [fake_override] declared in kotlin.Annotation public open fun toString (): kotlin.String [fake_override] declared in kotlin.Annotation
$this: VALUE_PARAMETER name:<this> type:kotlin.Any $this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS ANNOTATION_CLASS name:Strings modality:FINAL visibility:public superTypes:[kotlin.Annotation] CLASS ANNOTATION_CLASS name:Strings modality:OPEN visibility:public superTypes:[kotlin.Annotation]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Strings $this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Strings
CONSTRUCTOR visibility:public <> (value:kotlin.Array<kotlin.String>) returnType:<root>.Strings [primary] CONSTRUCTOR visibility:public <> (value:kotlin.Array<kotlin.String>) returnType:<root>.Strings [primary]
VALUE_PARAMETER name:value index:0 type:kotlin.Array<kotlin.String> VALUE_PARAMETER name:value index:0 type:kotlin.Array<kotlin.String>
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ANNOTATION_CLASS name:Strings modality:OPEN visibility:public superTypes:[kotlin.Annotation]'
PROPERTY name:value visibility:public modality:FINAL [val] PROPERTY name:value visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.Array<kotlin.String> visibility:private [final] FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.Array<kotlin.String> visibility:private [final]
EXPRESSION_BODY EXPRESSION_BODY
@@ -136,10 +145,13 @@ FILE fqName:<root> fileName:/C.kt
overridden: overridden:
public open fun toString (): kotlin.String [fake_override] declared in kotlin.Annotation public open fun toString (): kotlin.String [fake_override] declared in kotlin.Annotation
$this: VALUE_PARAMETER name:<this> type:kotlin.Any $this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS ANNOTATION_CLASS name:Ints modality:FINAL visibility:public superTypes:[kotlin.Annotation] CLASS ANNOTATION_CLASS name:Ints modality:OPEN visibility:public superTypes:[kotlin.Annotation]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Ints $this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Ints
CONSTRUCTOR visibility:public <> (value:kotlin.IntArray) returnType:<root>.Ints [primary] CONSTRUCTOR visibility:public <> (value:kotlin.IntArray) returnType:<root>.Ints [primary]
VALUE_PARAMETER name:value index:0 type:kotlin.IntArray VALUE_PARAMETER name:value index:0 type:kotlin.IntArray
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ANNOTATION_CLASS name:Ints modality:OPEN visibility:public superTypes:[kotlin.Annotation]'
PROPERTY name:value visibility:public modality:FINAL [val] PROPERTY name:value visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.IntArray visibility:private [final] FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.IntArray visibility:private [final]
EXPRESSION_BODY EXPRESSION_BODY
@@ -164,10 +176,13 @@ FILE fqName:<root> fileName:/C.kt
overridden: overridden:
public open fun toString (): kotlin.String [fake_override] declared in kotlin.Annotation public open fun toString (): kotlin.String [fake_override] declared in kotlin.Annotation
$this: VALUE_PARAMETER name:<this> type:kotlin.Any $this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS ANNOTATION_CLASS name:Enums modality:FINAL visibility:public superTypes:[kotlin.Annotation] CLASS ANNOTATION_CLASS name:Enums modality:OPEN visibility:public superTypes:[kotlin.Annotation]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Enums $this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Enums
CONSTRUCTOR visibility:public <> (value:kotlin.Array<<root>.E>) returnType:<root>.Enums [primary] CONSTRUCTOR visibility:public <> (value:kotlin.Array<<root>.E>) returnType:<root>.Enums [primary]
VALUE_PARAMETER name:value index:0 type:kotlin.Array<<root>.E> VALUE_PARAMETER name:value index:0 type:kotlin.Array<<root>.E>
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ANNOTATION_CLASS name:Enums modality:OPEN visibility:public superTypes:[kotlin.Annotation]'
PROPERTY name:value visibility:public modality:FINAL [val] PROPERTY name:value visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.Array<<root>.E> visibility:private [final] FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.Array<<root>.E> visibility:private [final]
EXPRESSION_BODY EXPRESSION_BODY
@@ -192,10 +207,13 @@ FILE fqName:<root> fileName:/C.kt
overridden: overridden:
public open fun toString (): kotlin.String [fake_override] declared in kotlin.Annotation public open fun toString (): kotlin.String [fake_override] declared in kotlin.Annotation
$this: VALUE_PARAMETER name:<this> type:kotlin.Any $this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS ANNOTATION_CLASS name:Classes modality:FINAL visibility:public superTypes:[kotlin.Annotation] CLASS ANNOTATION_CLASS name:Classes modality:OPEN visibility:public superTypes:[kotlin.Annotation]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Classes $this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Classes
CONSTRUCTOR visibility:public <> (value:kotlin.Array<kotlin.reflect.KClass<*>>) returnType:<root>.Classes [primary] CONSTRUCTOR visibility:public <> (value:kotlin.Array<kotlin.reflect.KClass<*>>) returnType:<root>.Classes [primary]
VALUE_PARAMETER name:value index:0 type:kotlin.Array<kotlin.reflect.KClass<*>> VALUE_PARAMETER name:value index:0 type:kotlin.Array<kotlin.reflect.KClass<*>>
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ANNOTATION_CLASS name:Classes modality:OPEN visibility:public superTypes:[kotlin.Annotation]'
PROPERTY name:value visibility:public modality:FINAL [val] PROPERTY name:value visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.Array<kotlin.reflect.KClass<*>> visibility:private [final] FIELD PROPERTY_BACKING_FIELD name:value type:kotlin.Array<kotlin.reflect.KClass<*>> visibility:private [final]
EXPRESSION_BODY EXPRESSION_BODY
@@ -228,11 +246,11 @@ FILE fqName:<root> fileName:/C.kt
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:C modality:FINAL visibility:public superTypes:[<root>.A]' INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:C modality:FINAL visibility:public superTypes:[<root>.A]'
FUN FAKE_OVERRIDE name:test visibility:public modality:OPEN <> ($this:<root>.A) returnType:kotlin.Unit [fake_override] FUN FAKE_OVERRIDE name:test visibility:public modality:OPEN <> ($this:<root>.A) returnType:kotlin.Unit [fake_override]
annotations: annotations:
Annos(value = <null>) Annos(value = Anno(token = 'OK'))
Strings(value = <null>) Strings(value = 'OK')
Ints(value = <null>) Ints(value = '42')
Enums(value = <null>) Enums(value = GET_ENUM 'ENUM_ENTRY name:EA' type=IrErrorType(null))
Classes(value = <null>) Classes(value = CLASS_REFERENCE 'CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Double modality:FINAL visibility:public superTypes:[kotlin.Number; kotlin.Comparable<kotlin.Double>; java.io.Serializable]' type=IrErrorType(null))
overridden: overridden:
public open fun test (): kotlin.Unit declared in <root>.A public open fun test (): kotlin.Unit declared in <root>.A
$this: VALUE_PARAMETER name:<this> type:<root>.A $this: VALUE_PARAMETER name:<this> type:<root>.A
@@ -1,6 +1,5 @@
// SKIP_KT_DUMP // SKIP_KT_DUMP
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// FILE: A.java // FILE: A.java
public class A { public class A {