From 26aeddf6d9596b111394099642dc4a270c4bd1f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Thu, 4 Apr 2019 17:26:44 +0200 Subject: [PATCH] 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. --- .../jvm/descriptors/SharedVariablesManager.kt | 32 +++------ .../jvm/intrinsics/IrIntrinsicFunction.kt | 65 +++++++------------ .../kotlin/ir/descriptors/IrBuiltIns.kt | 1 - .../org/jetbrains/kotlin/ir/types/irTypes.kt | 25 ------- 4 files changed, 32 insertions(+), 91 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/SharedVariablesManager.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/SharedVariablesManager.kt index 389fcf60128..6d0728f2aab 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/SharedVariablesManager.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/SharedVariablesManager.kt @@ -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 - } - } } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrIntrinsicFunction.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrIntrinsicFunction.kt index ad8f0917a98..1ba56262fc6 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrIntrinsicFunction.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrIntrinsicFunction.kt @@ -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 accept(visitor: IrElementVisitor, data: D): R { - TODO("not implemented") - } - - override fun acceptChildren(visitor: IrElementVisitor, data: D) { - TODO("not implemented") - } - - override fun transformChildren(transformer: IrElementTransformer, 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 = expression.argTypes(context), - invokeInstruction: IrIntrinsicFunction.(InstructionAdapter) -> Type): IrIntrinsicFunction { + fun createWithResult( + expression: IrMemberAccessExpression, signature: JvmMethodSignature, + context: JvmBackendContext, + argsTypes: List = expression.argTypes(context), + invokeInstruction: IrIntrinsicFunction.(InstructionAdapter) -> Type + ): IrIntrinsicFunction { return object : IrIntrinsicFunction(expression, signature, context, argsTypes) { override fun genInvokeInstructionWithResult(v: InstructionAdapter) = invokeInstruction(v) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltIns.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltIns.kt index d858d8e3f8c..f83e466380a 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltIns.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltIns.kt @@ -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 diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypes.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypes.kt index 2875cc30062..9bf5a692179 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypes.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypes.kt @@ -129,28 +129,3 @@ fun IrClassifierSymbol.typeWith(arguments: List): IrSimpleType = ) fun IrClass.typeWith(arguments: List) = 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() -}