IR: Avoid "raw" IrTypes
The ir type checker cannot deal with types with an incorrect number of arguments. On the other hand, the JVM IR backend sometimes produces "raw types" - types with generic parameters but without arguments. This commit removes such raw types as much as possible, by replacing calls to "typeWith()" with calls to "defaultType" for classes without type parametes (defaultType doesn't allocate, unlike typeWith) and with calls to "starProjectedType" for classes with type parameters.
This commit is contained in:
committed by
Alexander Udalov
parent
4d67803f04
commit
2db8471dd7
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.backend.common.ir
|
||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||
import org.jetbrains.kotlin.backend.common.DumpIrTreeWithDescriptorsVisitor
|
||||
import org.jetbrains.kotlin.backend.common.deepCopyWithVariables
|
||||
import org.jetbrains.kotlin.backend.common.lower.VariableRemapper
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
@@ -369,7 +368,7 @@ fun IrClass.createImplicitParameterDeclarationWithWrappedDescriptor() {
|
||||
IrValueParameterSymbolImpl(thisReceiverDescriptor),
|
||||
Name.identifier("<this>"),
|
||||
index = -1,
|
||||
type = this.symbol.typeWith(this.typeParameters.map { it.defaultType }),
|
||||
type = symbol.typeWithParameters(typeParameters),
|
||||
varargElementType = null,
|
||||
isCrossinline = false,
|
||||
isNoinline = false
|
||||
@@ -407,7 +406,7 @@ fun IrClass.createParameterDeclarations() {
|
||||
IrValueParameterSymbolImpl(it),
|
||||
Name.special("<this>"),
|
||||
0,
|
||||
symbol.typeWith(typeParameters.map { it.defaultType }),
|
||||
symbol.typeWithParameters(typeParameters),
|
||||
null,
|
||||
false,
|
||||
false
|
||||
|
||||
+1
-1
@@ -228,7 +228,7 @@ internal class StepHandler(
|
||||
private val symbols = context.ir.symbols
|
||||
|
||||
override val matcher = SimpleCalleeMatcher {
|
||||
singleArgumentExtension(FqName("kotlin.ranges.step"), symbols.progressionClasses.map { it.typeWith() })
|
||||
singleArgumentExtension(FqName("kotlin.ranges.step"), symbols.progressionClasses.map { it.owner.defaultType })
|
||||
parameter(0) { it.type.isInt() || it.type.isLong() }
|
||||
}
|
||||
|
||||
|
||||
@@ -235,7 +235,7 @@ class JvmSymbols(
|
||||
}
|
||||
|
||||
val suspendLambdaClass: IrClassSymbol = createClass(FqName("kotlin.coroutines.jvm.internal.SuspendLambda")) { klass ->
|
||||
klass.superTypes += suspendFunctionInterface.typeWith()
|
||||
klass.superTypes += suspendFunctionInterface.owner.defaultType
|
||||
klass.addConstructor().apply {
|
||||
addValueParameter("arity", irBuiltIns.intType)
|
||||
addValueParameter("completion", continuationClass.typeWith(irBuiltIns.anyNType).makeNullable())
|
||||
@@ -255,7 +255,7 @@ class JvmSymbols(
|
||||
private fun generateCallableReferenceMethods(klass: IrClass) {
|
||||
klass.addFunction("getSignature", irBuiltIns.stringType, Modality.OPEN)
|
||||
klass.addFunction("getName", irBuiltIns.stringType, Modality.OPEN)
|
||||
klass.addFunction("getOwner", irBuiltIns.kDeclarationContainerClass.typeWith(), Modality.OPEN)
|
||||
klass.addFunction("getOwner", irBuiltIns.kDeclarationContainerClass.owner.defaultType, Modality.OPEN)
|
||||
}
|
||||
|
||||
val functionReference: IrClassSymbol = createClass(FqName("kotlin.jvm.internal.FunctionReference")) { klass ->
|
||||
@@ -339,7 +339,7 @@ class JvmSymbols(
|
||||
createClass(FqName("kotlin.jvm.internal.$className")) { klass ->
|
||||
if (impl) {
|
||||
klass.addConstructor().apply {
|
||||
addValueParameter("owner", irBuiltIns.kDeclarationContainerClass.typeWith())
|
||||
addValueParameter("owner", irBuiltIns.kDeclarationContainerClass.owner.defaultType)
|
||||
addValueParameter("name", irBuiltIns.stringType)
|
||||
addValueParameter("string", irBuiltIns.stringType)
|
||||
}
|
||||
@@ -396,29 +396,27 @@ class JvmSymbols(
|
||||
propertyReferenceClasses(PropertyReferenceKey(mutable, parameterCount, impl))
|
||||
|
||||
val reflection: IrClassSymbol = createClass(FqName("kotlin.jvm.internal.Reflection")) { klass ->
|
||||
// We use raw types for java.lang.Class and kotlin.reflect.KClass here for simplicity and because this is what's used
|
||||
// at declaration site at kotlin.jvm.internal.Reflection.
|
||||
val rawJavaLangClass = javaLangClass.typeWith()
|
||||
val rawKClass = irBuiltIns.kClassClass.typeWith()
|
||||
val javaLangClassType = javaLangClass.starProjectedType
|
||||
val kClassType = irBuiltIns.kClassClass.starProjectedType
|
||||
|
||||
klass.addFunction("getOrCreateKotlinPackage", irBuiltIns.kDeclarationContainerClass.typeWith(), isStatic = true).apply {
|
||||
addValueParameter("javaClass", rawJavaLangClass)
|
||||
klass.addFunction("getOrCreateKotlinPackage", irBuiltIns.kDeclarationContainerClass.owner.defaultType, isStatic = true).apply {
|
||||
addValueParameter("javaClass", javaLangClassType)
|
||||
addValueParameter("moduleName", irBuiltIns.stringType)
|
||||
}
|
||||
|
||||
klass.addFunction("getOrCreateKotlinClass", rawKClass, isStatic = true).apply {
|
||||
addValueParameter("javaClass", rawJavaLangClass)
|
||||
klass.addFunction("getOrCreateKotlinClass", kClassType, isStatic = true).apply {
|
||||
addValueParameter("javaClass", javaLangClassType)
|
||||
}
|
||||
|
||||
klass.addFunction("getOrCreateKotlinClasses", irBuiltIns.arrayClass.typeWith(rawKClass), isStatic = true).apply {
|
||||
addValueParameter("javaClasses", irBuiltIns.arrayClass.typeWith(rawJavaLangClass))
|
||||
klass.addFunction("getOrCreateKotlinClasses", irBuiltIns.arrayClass.typeWith(kClassType), isStatic = true).apply {
|
||||
addValueParameter("javaClasses", irBuiltIns.arrayClass.typeWith(javaLangClassType))
|
||||
}
|
||||
|
||||
for (mutable in listOf(false, true)) {
|
||||
for (n in 0..2) {
|
||||
val functionName = (if (mutable) "mutableProperty" else "property") + n
|
||||
klass.addFunction(functionName, irBuiltIns.getKPropertyClass(mutable, n).typeWith(), isStatic = true).apply {
|
||||
addValueParameter("p", getPropertyReferenceClass(mutable, n, impl = false).typeWith())
|
||||
klass.addFunction(functionName, irBuiltIns.getKPropertyClass(mutable, n).starProjectedType, isStatic = true).apply {
|
||||
addValueParameter("p", getPropertyReferenceClass(mutable, n, impl = false).owner.defaultType)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -459,14 +457,15 @@ class JvmSymbols(
|
||||
klass.origin = JvmLoweredDeclarationOrigin.TO_ARRAY
|
||||
|
||||
val arrayType = irBuiltIns.arrayClass.typeWith(irBuiltIns.anyNType)
|
||||
val collectionType = irBuiltIns.collectionClass.starProjectedType
|
||||
klass.addFunction("toArray", arrayType, isStatic = true).apply {
|
||||
origin = JvmLoweredDeclarationOrigin.TO_ARRAY
|
||||
addValueParameter("collection", irBuiltIns.collectionClass.owner.typeWith(), JvmLoweredDeclarationOrigin.TO_ARRAY)
|
||||
addValueParameter("collection", collectionType, JvmLoweredDeclarationOrigin.TO_ARRAY)
|
||||
}
|
||||
klass.addFunction("toArray", arrayType, isStatic = true).apply {
|
||||
origin = JvmLoweredDeclarationOrigin.TO_ARRAY
|
||||
addValueParameter("collection", irBuiltIns.collectionClass.owner.typeWith(), JvmLoweredDeclarationOrigin.TO_ARRAY)
|
||||
addValueParameter("array", arrayType, JvmLoweredDeclarationOrigin.TO_ARRAY)
|
||||
addValueParameter("collection", collectionType, JvmLoweredDeclarationOrigin.TO_ARRAY)
|
||||
addValueParameter("array", arrayType.makeNullable(), JvmLoweredDeclarationOrigin.TO_ARRAY)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -482,8 +481,8 @@ class JvmSymbols(
|
||||
}.apply {
|
||||
parent = kotlinJvmPackage
|
||||
addGetter().apply {
|
||||
addExtensionReceiver(irBuiltIns.kClassClass.typeWith())
|
||||
returnType = javaLangClass.typeWith()
|
||||
addExtensionReceiver(irBuiltIns.kClassClass.starProjectedType)
|
||||
returnType = javaLangClass.starProjectedType
|
||||
}
|
||||
}.symbol
|
||||
|
||||
@@ -525,7 +524,7 @@ class JvmSymbols(
|
||||
addValueParameter("element", irType)
|
||||
}
|
||||
|
||||
klass.addFunction("toArray", irBuiltIns.primitiveArrayForType.getValue(irType).owner.typeWith())
|
||||
klass.addFunction("toArray", irBuiltIns.primitiveArrayForType.getValue(irType).owner.defaultType)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -153,7 +153,7 @@ class JvmSharedVariablesManager(
|
||||
|
||||
override val elementType = refClass.typeParameters[0].defaultType
|
||||
|
||||
override fun getRefType(valueType: IrType) = refClass.typeWith(listOf(valueType))
|
||||
override fun getRefType(valueType: IrType) = refClass.typeWith(valueType)
|
||||
}
|
||||
|
||||
private fun getProvider(valueType: IrType): RefProvider =
|
||||
|
||||
+2
-6
@@ -27,11 +27,7 @@ import org.jetbrains.kotlin.ir.expressions.IrCall
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
|
||||
import org.jetbrains.kotlin.ir.types.getClass
|
||||
import org.jetbrains.kotlin.ir.types.typeWith
|
||||
import org.jetbrains.kotlin.ir.util.fields
|
||||
import org.jetbrains.kotlin.ir.util.functions
|
||||
import org.jetbrains.kotlin.ir.util.getPackageFragment
|
||||
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||
@@ -141,7 +137,7 @@ private class AssertionLowering(private val context: JvmBackendContext) :
|
||||
|
||||
private fun IrBuilderWithScope.getJavaClass(backendContext: JvmBackendContext, irClass: IrClass) =
|
||||
with(CallableReferenceLowering) {
|
||||
javaClassReference(irClass.typeWith(), backendContext)
|
||||
javaClassReference(irClass.defaultType, backendContext)
|
||||
}
|
||||
|
||||
fun IrClass.buildAssertionsDisabledField(backendContext: JvmBackendContext, topLevelClass: IrClass) =
|
||||
|
||||
+7
-5
@@ -14,8 +14,10 @@ import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
|
||||
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
|
||||
import org.jetbrains.kotlin.backend.jvm.ir.*
|
||||
import org.jetbrains.kotlin.backend.jvm.ir.IrInlineReferenceLocator
|
||||
import org.jetbrains.kotlin.backend.jvm.ir.createJvmIrBuilder
|
||||
import org.jetbrains.kotlin.backend.jvm.ir.irArray
|
||||
import org.jetbrains.kotlin.backend.jvm.ir.isLambda
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
@@ -126,7 +128,7 @@ internal class CallableReferenceLowering(private val context: JvmBackendContext)
|
||||
private val superMethod =
|
||||
functionSuperClass.functions.single { it.owner.modality == Modality.ABSTRACT }
|
||||
private val superType =
|
||||
samSuperType ?: (if (isLambda) context.ir.symbols.lambdaClass else context.ir.symbols.functionReference).owner.typeWith()
|
||||
samSuperType ?: (if (isLambda) context.ir.symbols.lambdaClass else context.ir.symbols.functionReference).owner.defaultType
|
||||
|
||||
private val functionReferenceClass = buildClass {
|
||||
setSourceRange(irFunctionReference)
|
||||
@@ -140,7 +142,7 @@ internal class CallableReferenceLowering(private val context: JvmBackendContext)
|
||||
superTypes += superType
|
||||
if (samSuperType == null)
|
||||
superTypes += functionSuperClass.typeWith(parameterTypes)
|
||||
if (irFunctionReference.isSuspend) superTypes += context.ir.symbols.suspendFunctionInterface.typeWith()
|
||||
if (irFunctionReference.isSuspend) superTypes += context.ir.symbols.suspendFunctionInterface.owner.defaultType
|
||||
createImplicitParameterDeclarationWithWrappedDescriptor()
|
||||
copyAttributes(irFunctionReference)
|
||||
}
|
||||
@@ -358,11 +360,11 @@ internal class CallableReferenceLowering(private val context: JvmBackendContext)
|
||||
companion object {
|
||||
private fun IrBuilderWithScope.kClassReference(classType: IrType) =
|
||||
IrClassReferenceImpl(
|
||||
startOffset, endOffset, context.irBuiltIns.kClassClass.typeWith(), context.irBuiltIns.kClassClass, classType
|
||||
startOffset, endOffset, context.irBuiltIns.kClassClass.starProjectedType, context.irBuiltIns.kClassClass, classType
|
||||
)
|
||||
|
||||
private fun IrBuilderWithScope.kClassToJavaClass(kClassReference: IrExpression, context: JvmBackendContext) =
|
||||
irGet(context.ir.symbols.javaLangClass.typeWith(), null, context.ir.symbols.kClassJava.owner.getter!!.symbol).apply {
|
||||
irGet(context.ir.symbols.javaLangClass.starProjectedType, null, context.ir.symbols.kClassJava.owner.getter!!.symbol).apply {
|
||||
extensionReceiver = kClassReference
|
||||
}
|
||||
|
||||
|
||||
@@ -104,6 +104,17 @@ val IrTypeParameter.defaultType: IrType
|
||||
annotations = emptyList()
|
||||
)
|
||||
|
||||
val IrClassSymbol.starProjectedType: IrSimpleType
|
||||
get() = IrSimpleTypeImpl(
|
||||
this,
|
||||
hasQuestionMark = false,
|
||||
arguments = owner.typeParameters.map { IrStarProjectionImpl },
|
||||
annotations = emptyList()
|
||||
)
|
||||
|
||||
fun IrClassifierSymbol.typeWithParameters(parameters: List<IrTypeParameter>): IrSimpleType =
|
||||
typeWith(parameters.map { it.defaultType })
|
||||
|
||||
fun IrClassifierSymbol.typeWith(vararg arguments: IrType): IrSimpleType = typeWith(arguments.toList())
|
||||
|
||||
fun IrClassifierSymbol.typeWith(arguments: List<IrType>): IrSimpleType =
|
||||
|
||||
Reference in New Issue
Block a user