Remove unsafe cast function usages from JVM backend modules

This commit is contained in:
Alexander Udalov
2022-11-01 13:56:02 +01:00
committed by Space Team
parent 2cd16f055a
commit 1418423423
34 changed files with 93 additions and 129 deletions
@@ -57,9 +57,7 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.jvm.checkers.JvmSimpleNameBacktickChecker
import org.jetbrains.kotlin.resolve.jvm.diagnostics.*
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.*
import org.jetbrains.org.objectweb.asm.commons.Method
import java.io.File
@@ -247,7 +245,8 @@ class ClassCodegen private constructor(
if (supertypeArgument is IrTypeProjection) {
val typeArgument = supertypeArgument.type
if (typeArgument.isReifiedTypeParameter) {
reifiedTypeParametersUsages.addUsedReifiedParameter(typeArgument.classifierOrFail.cast<IrTypeParameterSymbol>().owner.name.asString())
val typeParameter = typeArgument.classifierOrFail as IrTypeParameterSymbol
reifiedTypeParametersUsages.addUsedReifiedParameter(typeParameter.owner.name.asString())
} else {
processTypeParameters(typeArgument)
}
@@ -580,7 +579,7 @@ class ClassCodegen private constructor(
// The one exception to this rule are anonymous objects defined as members of a class. These are nested inside of the
// class initializer, but can be referred to from anywhere within the scope of the class. That's why we have to ensure
// that all references to classes inside of <clinit> have a non-null `parentFunction`.
parentFunction: IrFunction? = irClass.parent.safeAs<IrFunction>()?.takeIf {
parentFunction: IrFunction? = (irClass.parent as? IrFunction)?.takeIf {
it.origin == JvmLoweredDeclarationOrigin.CLASS_STATIC_INITIALIZER
},
): ClassCodegen =
@@ -55,7 +55,6 @@ import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
import org.jetbrains.kotlin.types.computeExpandedTypeForInlineClass
import org.jetbrains.kotlin.types.model.TypeParameterMarker
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Type
@@ -1502,7 +1501,7 @@ class ExpressionCodegen(
get() = irFunction.isInline || irFunction.origin == JvmLoweredDeclarationOrigin.INLINE_LAMBDA
val IrType.isReifiedTypeParameter: Boolean
get() = this.classifierOrNull?.safeAs<IrTypeParameterSymbol>()?.owner?.isReified == true
get() = (classifierOrNull as? IrTypeParameterSymbol)?.owner?.isReified == true
companion object {
internal fun generateClassInstance(v: InstructionAdapter, classType: IrType, typeMapper: IrTypeMapper, wrapPrimitives: Boolean) {
@@ -21,7 +21,10 @@ import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.descriptors.toIrBasedDescriptor
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.IrClassReference
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.expressions.IrVararg
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.load.java.JavaDescriptorVisibilities
import org.jetbrains.kotlin.name.JvmNames.JVM_SYNTHETIC_ANNOTATION_FQ_NAME
@@ -30,7 +33,6 @@ import org.jetbrains.kotlin.name.JvmNames.SYNCHRONIZED_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.resolve.annotations.JVM_THROWS_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.*
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
import org.jetbrains.org.objectweb.asm.tree.MethodNode
@@ -235,14 +237,10 @@ class FunctionCodegen(private val irFunction: IrFunction, private val classCodeg
if (!classCodegen.irClass.isAnnotationClass) return null
// TODO: any simpler way to get to the value expression?
// Are there other valid IR structures that represent the default value?
return irFunction.safeAs<IrSimpleFunction>()
?.correspondingPropertySymbol?.owner
?.backingField
?.initializer.safeAs<IrExpressionBody>()
?.expression?.safeAs<IrGetValue>()
?.symbol?.owner?.safeAs<IrValueParameter>()
?.defaultValue?.safeAs<IrExpressionBody>()
?.expression
val backingField = (irFunction as? IrSimpleFunction)?.correspondingPropertySymbol?.owner?.backingField
val getValue = backingField?.initializer?.expression as? IrGetValue
val parameter = getValue?.symbol?.owner as? IrValueParameter
return parameter?.defaultValue?.expression
}
private fun IrFunction.createFrameMapWithReceivers(): IrFrameMap {
@@ -29,10 +29,9 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.load.java.JavaDescriptorVisibilities
@@ -40,7 +39,6 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Type
@@ -106,7 +104,7 @@ private fun IrDeclaration.getVisibilityAccessFlagForAnonymous(): Int =
fun IrClass.calculateInnerClassAccessFlags(context: JvmBackendContext): Int {
val isLambda = superTypes.any {
it.safeAs<IrSimpleType>()?.classifier === context.ir.symbols.lambdaClass
it.classOrNull === context.ir.symbols.lambdaClass
}
val visibility = when {
isLambda -> getVisibilityAccessFlagForAnonymous()
@@ -245,7 +243,7 @@ internal fun IrTypeMapper.mapClassSignature(irClass: IrClass, type: Type): JvmCl
val superInterfaces = LinkedHashSet<String>()
for (superType in irClass.superTypes) {
val superClass = superType.safeAs<IrSimpleType>()?.classifier?.safeAs<IrClassSymbol>()?.owner ?: continue
val superClass = superType.classOrNull?.owner ?: continue
if (superClass.isJvmInterface) {
sw.writeInterface()
superInterfaces.add(mapSupertype(superType, sw).internalName)
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.backend.jvm.ir.getStringConstArgument
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.Handle
import org.jetbrains.org.objectweb.asm.Type
@@ -34,8 +33,8 @@ object JvmInvokeDynamic : IntrinsicMethod() {
?: fail("'bootstrapMethodHandle' should be a call")
val bootstrapMethodHandle = evalMethodHandle(bootstrapMethodHandleArg)
val bootstrapMethodArgs = (expression.getValueArgument(2)?.safeAs<IrVararg>()
?: fail("'bootstrapMethodArgs' is expected to be a vararg"))
val bootstrapMethodArgs = expression.getValueArgument(2) as? IrVararg
?: fail("'bootstrapMethodArgs' is expected to be a vararg")
val asmBootstrapMethodArgs = bootstrapMethodArgs.elements
.map { generateBootstrapMethodArg(it, codegen) }
.toTypedArray()