From 95821313e3817edf888dce689776a5e26eb3d394 Mon Sep 17 00:00:00 2001 From: Roman Artemev Date: Thu, 14 Mar 2019 20:50:42 +0300 Subject: [PATCH] [IR BE] Get rid of usages of descriptor-based IrType utils --- .../js/lower/CallableReferenceLowering.kt | 27 ++++++++++++++---- .../js/lower/MultipleCatchesLowering.kt | 28 +++++++++++++------ .../lower/calls/ReflectionCallsTransformer.kt | 2 +- .../backend/jvm/lower/BridgeLowering.kt | 2 -- .../jvm/lower/JvmCoercionToUnitPatcher.kt | 4 +-- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt index 7e018854fdd..2679ea8302c 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt @@ -9,7 +9,6 @@ import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.common.descriptors.WrappedTypeParameterDescriptor import org.jetbrains.kotlin.backend.common.descriptors.WrappedValueParameterDescriptor import org.jetbrains.kotlin.backend.common.lower.BOUND_VALUE_PARAMETER -import org.jetbrains.kotlin.backend.common.utils.isSubtypeOf import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET @@ -30,6 +29,7 @@ import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrTypeProjection import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl +import org.jetbrains.kotlin.ir.types.isEqualTo import org.jetbrains.kotlin.ir.util.isInlined import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid @@ -417,6 +417,16 @@ class CallableReferenceLowering(val context: JsIrBackendContext) : FileLoweringP result += JsIrBuilder.buildValueParameter(paramName, result.size, type.boxIfInlined()).also { it.parent = closure } } + + if (result.size < arity) { + // That means there are still implicit vararg arguments + val lastParam = result.last() + for (index in result.size until arity) { + val paramName = "${lastParam.name}_$index" + result += JsIrBuilder.buildValueParameter(paramName, result.size, lastParam.type).also { it.parent = closure } + } + } + return result } @@ -535,12 +545,17 @@ class CallableReferenceLowering(val context: JsIrBackendContext) : FileLoweringP val closureParam = unboundParamSymbols[i].owner val value = JsIrBuilder.buildGetValue(unboundParamSymbols[i]) val parameter = callTarget.valueParameters[j] - val argument = if (parameter.varargElementType?.let { closureParam.type.isSubtypeOf(it) } == true) { - // fun foo(x: X, y: vararg Y): Z - // val r: (X, Y) -> Z = ::foo - IrVarargImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, parameter.type, parameter.varargElementType!!, listOf(value)) - } else value + val argument = + if (parameter.varargElementType?.let { closureParam.type.isEqualTo(it) } == true) { + // fun foo(x: X, vararg y: Y): Z + // val r: (X, Y) -> Z = ::foo + val tailValues = unboundParamSymbols.drop(i) + IrVarargImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, parameter.type, parameter.varargElementType!!, tailValues.map { + JsIrBuilder.buildGetValue(it) + }) + } else value irCall.putValueArgument(j++, argument) + if (j == callTarget.valueParameters.size) break } val irClosureReturn = JsIrBuilder.buildReturn(closureFunction.symbol, irCall, context.irBuiltIns.nothingType) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MultipleCatchesLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MultipleCatchesLowering.kt index aa77909ea5e..dcca9af2be1 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MultipleCatchesLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MultipleCatchesLowering.kt @@ -6,8 +6,6 @@ package org.jetbrains.kotlin.ir.backend.js.lower import org.jetbrains.kotlin.backend.common.FileLoweringPass -import org.jetbrains.kotlin.backend.common.utils.commonSupertype -import org.jetbrains.kotlin.backend.common.utils.isSubtypeOf import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder @@ -19,11 +17,12 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrBranchImpl import org.jetbrains.kotlin.ir.expressions.impl.IrCatchImpl import org.jetbrains.kotlin.ir.expressions.impl.IrElseBranchImpl import org.jetbrains.kotlin.ir.expressions.impl.IrTryImpl +import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol import org.jetbrains.kotlin.ir.symbols.IrValueSymbol -import org.jetbrains.kotlin.ir.types.IrDynamicType -import org.jetbrains.kotlin.ir.types.IrType -import org.jetbrains.kotlin.ir.types.classifierOrNull +import org.jetbrains.kotlin.ir.types.* +import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl +import org.jetbrains.kotlin.ir.types.impl.IrStarProjectionImpl import org.jetbrains.kotlin.ir.visitors.IrElementTransformer /** @@ -39,7 +38,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformer * is converted into * * try {} - * catch (ex: Ex = LCA(Ex1, Ex2, Ex3) { + * catch (ex: Ex = LCA(Ex1, Ex2, Ex3)) { * when (ex) { * ex is Ex1 -> catch1((Ex1)ex) * ex is Ex2 -> catch2((Ex2)ex) @@ -125,8 +124,21 @@ class MultipleCatchesLowering(val context: JsIrBackendContext) : FileLoweringPas private fun buildImplicitCast(value: IrExpression, toType: IrType, toTypeSymbol: IrClassifierSymbol) = JsIrBuilder.buildTypeOperator(toType, IrTypeOperator.IMPLICIT_CAST, value, toType, toTypeSymbol) - private fun mergeTypes(types: List) = types.commonSupertype(context.symbolTable).also { - assert(it.isSubtypeOf(context.irBuiltIns.throwableType) || it is IrDynamicType) + private fun mergeTypes(types: List): IrType { + + return types.firstOrNull { it is IrDynamicType } ?: run { + + val superClassifier = + types.map { (it as IrSimpleType).classifier }.commonSuperclass().also { + assert(it.isSubtypeOfClass(context.irBuiltIns.throwableClass)) + } + + val typeArguments = if (superClassifier is IrClassSymbol) { + superClassifier.owner.typeParameters.map { IrStarProjectionImpl } + } else emptyList() + + return IrSimpleTypeImpl(superClassifier, false, typeArguments, emptyList()) + } } }, irFile) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ReflectionCallsTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ReflectionCallsTransformer.kt index 774cfe7d165..d08f7e0e915 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ReflectionCallsTransformer.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ReflectionCallsTransformer.kt @@ -5,12 +5,12 @@ package org.jetbrains.kotlin.ir.backend.js.lower.calls -import org.jetbrains.kotlin.backend.common.utils.isSubtypeOfClass import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext import org.jetbrains.kotlin.ir.util.irCall import org.jetbrains.kotlin.ir.backend.js.utils.Namer import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrExpression +import org.jetbrains.kotlin.ir.types.isSubtypeOfClass import org.jetbrains.kotlin.name.Name diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt index e657d8c30d5..e8f602e5c19 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt @@ -11,13 +11,11 @@ import org.jetbrains.kotlin.backend.common.bridges.findAllReachableDeclarations import org.jetbrains.kotlin.backend.common.bridges.findConcreteSuperDeclaration import org.jetbrains.kotlin.backend.common.bridges.generateBridges import org.jetbrains.kotlin.backend.common.descriptors.WrappedSimpleFunctionDescriptor -import org.jetbrains.kotlin.backend.common.descriptors.WrappedTypeParameterDescriptor import org.jetbrains.kotlin.backend.common.descriptors.WrappedValueParameterDescriptor import org.jetbrains.kotlin.backend.common.ir.* import org.jetbrains.kotlin.backend.common.lower.createIrBuilder import org.jetbrains.kotlin.backend.common.lower.irNot import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase -import org.jetbrains.kotlin.backend.common.utils.isSubtypeOf import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.descriptors.* diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmCoercionToUnitPatcher.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmCoercionToUnitPatcher.kt index 46ad1ed5d03..dbfe007388b 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmCoercionToUnitPatcher.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmCoercionToUnitPatcher.kt @@ -7,12 +7,12 @@ package org.jetbrains.kotlin.backend.jvm.lower import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase -import org.jetbrains.kotlin.backend.common.utils.isSubtypeOf import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensions import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrExpression +import org.jetbrains.kotlin.ir.types.isSubtypeOfClass import org.jetbrains.kotlin.ir.types.toKotlinType import org.jetbrains.kotlin.ir.util.TypeTranslator import org.jetbrains.kotlin.ir.util.coerceToUnitIfNeeded @@ -38,7 +38,7 @@ class JvmCoercionToUnitPatcher(val context: JvmBackendContext) : } override fun IrExpression.coerceToUnit(): IrExpression { - if (type.isSubtypeOf(context.irBuiltIns.unitType) && this is IrCall) { + if (type.isSubtypeOfClass(context.irBuiltIns.unitClass) && this is IrCall) { return coerceToUnitIfNeeded(symbol.owner.returnType.toKotlinType(), context.irBuiltIns) }