[FIR, IR] Refactor: move annotation retention helper functions

Will be needed in subsequent commit to be accessible in
ExpectActualMatchingContext implementations.

^KT-58551
This commit is contained in:
Roman Efremov
2023-06-23 14:41:30 +02:00
committed by Space Team
parent 454756a2b5
commit 59f1a0dd8e
7 changed files with 62 additions and 41 deletions
@@ -6,8 +6,10 @@
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.builtins.UnsignedType
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention
import org.jetbrains.kotlin.ir.*
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.declarations.*
@@ -29,6 +31,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.utils.*
import java.io.StringWriter
@@ -348,6 +351,20 @@ fun IrAnnotationContainer.hasAnnotation(symbol: IrClassSymbol) =
it.symbol.owner.parentAsClass.symbol == symbol
}
fun IrClass.getAnnotationRetention(): KotlinRetention? {
val retentionArgument =
getAnnotation(StandardNames.FqNames.retention)?.getValueArgument(StandardClassIds.Annotations.ParameterNames.retentionValue)
as? IrGetEnumValue ?: return null
val retentionArgumentValue = retentionArgument.symbol.owner
return KotlinRetention.valueOf(retentionArgumentValue.name.asString())
}
// To be generalized to IrMemberAccessExpression as soon as properties get symbols.
fun IrConstructorCall.getValueArgument(name: Name): IrExpression? {
val index = symbol.owner.valueParameters.find { it.name == name }?.index ?: return null
return getValueArgument(index)
}
val IrConstructor.constructedClassType get() = (parent as IrClass).thisReceiver?.type!!