Remove broken KotlinType.toIrType function
The KotlinType.toIrType function sometimes produces unbound symbols and was only used in IrIntrinsicFunction and SharedVariablesManager. This change removes the broken function and changes SharedVariableManager to use IrType consistently and IrIntrinsicFunction to use KotlinType consistently.
This commit is contained in:
committed by
max-kammerer
parent
048fd9fa25
commit
26aeddf6d9
+10
-22
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.ir.types.*
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
|
||||
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
|
||||
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
|
||||
import org.jetbrains.kotlin.ir.util.defaultType
|
||||
import org.jetbrains.kotlin.ir.util.parentAsClass
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
@@ -85,12 +86,10 @@ class JvmSharedVariablesManager(
|
||||
}
|
||||
}
|
||||
|
||||
private inner class PrimitiveRefProvider(primitiveType: PrimitiveType) : RefProvider() {
|
||||
override val elementType = builtIns.getPrimitiveKotlinType(primitiveType).toIrType()!!
|
||||
|
||||
private inner class PrimitiveRefProvider(override val elementType: IrType) : RefProvider() {
|
||||
override val refClass = buildClass {
|
||||
origin = SHARED_VARIABLE_ORIGIN
|
||||
name = Name.identifier(primitiveType.typeName.asString() + "Ref")
|
||||
name = Name.identifier(elementType.classOrNull!!.owner.name.asString() + "Ref")
|
||||
}.apply {
|
||||
parent = refNamespaceClass
|
||||
refNamespaceClass.addMember(this)
|
||||
@@ -106,8 +105,8 @@ class JvmSharedVariablesManager(
|
||||
override fun getRefType(valueType: IrType) = refClass.defaultType
|
||||
}
|
||||
|
||||
private val primitiveRefProviders = PrimitiveType.values().associate { primitiveType ->
|
||||
primitiveType to PrimitiveRefProvider(primitiveType)
|
||||
private val primitiveRefProviders = irBuiltIns.primitiveIrTypes.associate { primitiveType ->
|
||||
primitiveType.classifierOrFail to PrimitiveRefProvider(primitiveType)
|
||||
}
|
||||
|
||||
private val objectRefProvider = object : RefProvider() {
|
||||
@@ -154,7 +153,11 @@ class JvmSharedVariablesManager(
|
||||
override fun getRefType(valueType: IrType) = refClass.typeWith(listOf(valueType))
|
||||
}
|
||||
|
||||
private fun getProvider(valueType: IrType) = primitiveRefProviders[getPrimitiveType(valueType)] ?: objectRefProvider
|
||||
private fun getProvider(valueType: IrType): RefProvider =
|
||||
if (valueType.isPrimitiveType())
|
||||
primitiveRefProviders.getValue(valueType.classifierOrFail)
|
||||
else
|
||||
objectRefProvider
|
||||
|
||||
private fun getElementFieldSymbol(valueType: IrType): IrFieldSymbol {
|
||||
return getProvider(valueType).elementField.symbol
|
||||
@@ -240,19 +243,4 @@ class JvmSharedVariablesManager(
|
||||
originalSet.type,
|
||||
originalSet.origin
|
||||
)
|
||||
|
||||
private fun getPrimitiveType(type: IrType): PrimitiveType? {
|
||||
val kType = type.toKotlinType()
|
||||
return when {
|
||||
KotlinBuiltIns.isBoolean(kType) -> PrimitiveType.BOOLEAN
|
||||
KotlinBuiltIns.isChar(kType) -> PrimitiveType.CHAR
|
||||
KotlinBuiltIns.isByte(kType) -> PrimitiveType.BYTE
|
||||
KotlinBuiltIns.isShort(kType) -> PrimitiveType.SHORT
|
||||
KotlinBuiltIns.isInt(kType) -> PrimitiveType.INT
|
||||
KotlinBuiltIns.isLong(kType) -> PrimitiveType.LONG
|
||||
KotlinBuiltIns.isFloat(kType) -> PrimitiveType.FLOAT
|
||||
KotlinBuiltIns.isDouble(kType) -> PrimitiveType.DOUBLE
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+22
-43
@@ -12,14 +12,9 @@ import org.jetbrains.kotlin.codegen.AsmUtil
|
||||
import org.jetbrains.kotlin.codegen.Callable
|
||||
import org.jetbrains.kotlin.codegen.StackValue
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.toIrType
|
||||
import org.jetbrains.kotlin.ir.types.toKotlinType
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
|
||||
import org.jetbrains.kotlin.resolve.calls.components.isVararg
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
@@ -27,24 +22,6 @@ import org.jetbrains.org.objectweb.asm.Type
|
||||
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
|
||||
import java.util.*
|
||||
|
||||
private class IrEmptyVarargExpression(
|
||||
override val type: IrType,
|
||||
override val startOffset: Int,
|
||||
override val endOffset: Int
|
||||
) : IrExpression {
|
||||
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
override fun <D> transformChildren(transformer: IrElementTransformer<D>, data: D) {
|
||||
TODO("not implemented")
|
||||
}
|
||||
}
|
||||
|
||||
open class IrIntrinsicFunction(
|
||||
val expression: IrMemberAccessExpression,
|
||||
val signature: JvmMethodSignature,
|
||||
@@ -94,22 +71,23 @@ open class IrIntrinsicFunction(
|
||||
codegen: ExpressionCodegen,
|
||||
data: BlockInfo
|
||||
) {
|
||||
val args = listOfNotNull(expression.dispatchReceiver, expression.extensionReceiver) +
|
||||
expression.descriptor.valueParameters.mapIndexed { i, descriptor ->
|
||||
expression.getValueArgument(i) ?: if (descriptor.isVararg)
|
||||
IrEmptyVarargExpression(descriptor.type.toIrType()!!, UNDEFINED_OFFSET, UNDEFINED_OFFSET)
|
||||
else error("Unknown parameter: $descriptor in $expression")
|
||||
var offset = 0
|
||||
expression.dispatchReceiver?.let { genArg(it, codegen, offset++, data) }
|
||||
expression.extensionReceiver?.let { genArg(it, codegen, offset++, data) }
|
||||
for ((i, descriptor) in expression.descriptor.valueParameters.withIndex()) {
|
||||
val argument = expression.getValueArgument(i)
|
||||
when {
|
||||
argument != null ->
|
||||
genArg(argument, codegen, i + offset, data)
|
||||
descriptor.isVararg -> {
|
||||
val parameterType = codegen.typeMapper.mapType(descriptor.type)
|
||||
StackValue.operation(parameterType) {
|
||||
it.aconst(0)
|
||||
it.newarray(AsmUtil.correctElementType(parameterType))
|
||||
}.put(parameterType, codegen.mv)
|
||||
}
|
||||
|
||||
args.forEachIndexed { i, irExpression ->
|
||||
if (irExpression is IrEmptyVarargExpression) {
|
||||
val parameterType = codegen.typeMapper.mapType(irExpression.type.toKotlinType())
|
||||
StackValue.operation(parameterType) {
|
||||
it.aconst(0)
|
||||
it.newarray(AsmUtil.correctElementType(parameterType))
|
||||
}.put(parameterType, codegen.mv)
|
||||
} else {
|
||||
genArg(irExpression, codegen, i, data)
|
||||
else ->
|
||||
error("Unknown parameter: $descriptor in $expression")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,11 +110,12 @@ open class IrIntrinsicFunction(
|
||||
}
|
||||
}
|
||||
|
||||
fun createWithResult(expression: IrMemberAccessExpression,
|
||||
signature: JvmMethodSignature,
|
||||
context: JvmBackendContext,
|
||||
argsTypes: List<Type> = expression.argTypes(context),
|
||||
invokeInstruction: IrIntrinsicFunction.(InstructionAdapter) -> Type): IrIntrinsicFunction {
|
||||
fun createWithResult(
|
||||
expression: IrMemberAccessExpression, signature: JvmMethodSignature,
|
||||
context: JvmBackendContext,
|
||||
argsTypes: List<Type> = expression.argTypes(context),
|
||||
invokeInstruction: IrIntrinsicFunction.(InstructionAdapter) -> Type
|
||||
): IrIntrinsicFunction {
|
||||
return object : IrIntrinsicFunction(expression, signature, context, argsTypes) {
|
||||
|
||||
override fun genInvokeInstructionWithResult(v: InstructionAdapter) = invokeInstruction(v)
|
||||
|
||||
@@ -17,7 +17,6 @@ import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrSimpleType
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
|
||||
import org.jetbrains.kotlin.ir.types.toIrType
|
||||
import org.jetbrains.kotlin.ir.types.withHasQuestionMark
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
||||
|
||||
@@ -129,28 +129,3 @@ fun IrClassifierSymbol.typeWith(arguments: List<IrType>): IrSimpleType =
|
||||
)
|
||||
|
||||
fun IrClass.typeWith(arguments: List<IrType>) = this.symbol.typeWith(arguments)
|
||||
|
||||
fun KotlinType.toIrType(symbolTable: SymbolTable? = null): IrType? {
|
||||
if (isDynamic()) return IrDynamicTypeImpl(null, listOf(), Variance.INVARIANT)
|
||||
|
||||
val symbol = constructor.declarationDescriptor?.getSymbol(symbolTable) ?: return null
|
||||
|
||||
val arguments = this.arguments.map { projection ->
|
||||
when (projection) {
|
||||
is TypeProjectionImpl -> IrTypeProjectionImpl(projection.type.toIrType(symbolTable)!!, projection.projectionKind)
|
||||
is StarProjectionImpl -> IrStarProjectionImpl
|
||||
else -> error(projection)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
val annotations = listOf()
|
||||
return IrSimpleTypeImpl(null, symbol, isMarkedNullable, arguments, annotations)
|
||||
}
|
||||
|
||||
// TODO: this function creates unbound symbol which is the great source of problems
|
||||
private fun ClassifierDescriptor.getSymbol(symbolTable: SymbolTable?): IrClassifierSymbol = when (this) {
|
||||
is ClassDescriptor -> symbolTable?.referenceClass(this) ?: IrClassSymbolImpl(this)
|
||||
is TypeParameterDescriptor -> symbolTable?.referenceTypeParameter(this) ?: IrTypeParameterSymbolImpl(this)
|
||||
else -> TODO()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user