From 01345ad62bc9a7afd5f44032eb274448e1378474 Mon Sep 17 00:00:00 2001 From: Pavel Kunyavskiy Date: Thu, 25 Nov 2021 11:08:15 +0300 Subject: [PATCH] [K/N] Get rid of work with lagacy descriptor-based types --- .../kotlin/backend/konan/InlineClasses.kt | 4 +- .../kotlin/backend/konan/cgen/CBridgeGen.kt | 12 ++-- .../backend/konan/ir/IrTypeAsKotlinType.kt | 64 ------------------- .../kotlin/backend/konan/ir/NewIrUtils.kt | 4 +- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 2 +- .../kotlin/backend/konan/lower/Autoboxing.kt | 5 +- .../konan/lower/BuiltinOperatorLowering.kt | 14 ++-- .../backend/konan/lower/InteropLowering.kt | 8 +-- .../backend/konan/lower/ReflectionSupport.kt | 3 +- .../backend/konan/lower/TestProcessor.kt | 30 ++++----- .../konan/lower/TypeOperatorLowering.kt | 6 +- 11 files changed, 40 insertions(+), 112 deletions(-) delete mode 100644 kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/IrTypeAsKotlinType.kt diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/InlineClasses.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/InlineClasses.kt index 8beb577db1f..fc3e4df5cc3 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/InlineClasses.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/InlineClasses.kt @@ -7,7 +7,6 @@ package org.jetbrains.kotlin.backend.konan import org.jetbrains.kotlin.backend.common.ir.isTopLevel import org.jetbrains.kotlin.backend.konan.descriptors.findPackage -import org.jetbrains.kotlin.backend.konan.ir.containsNull import org.jetbrains.kotlin.backend.konan.ir.getSuperClassNotAny import org.jetbrains.kotlin.builtins.PrimitiveType import org.jetbrains.kotlin.descriptors.ClassDescriptor @@ -18,6 +17,7 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.classifierOrFail +import org.jetbrains.kotlin.ir.types.isNullable import org.jetbrains.kotlin.ir.types.makeNullable import org.jetbrains.kotlin.ir.util.constructors import org.jetbrains.kotlin.ir.util.defaultType @@ -271,7 +271,7 @@ internal object KotlinTypeInlineClassesSupport : InlineClassesSupport() { - override fun isNullable(type: IrType): Boolean = type.containsNull() + override fun isNullable(type: IrType): Boolean = type.isNullable() override fun makeNullable(type: IrType): IrType = type.makeNullable() diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cgen/CBridgeGen.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cgen/CBridgeGen.kt index 99ba02cd972..d9fd8bda4ed 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cgen/CBridgeGen.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cgen/CBridgeGen.kt @@ -126,7 +126,7 @@ internal fun KotlinStubs.generateCCall(expression: IrCall, builder: IrBuilderWit require(expression.dispatchReceiver == null) { renderCompilerError(expression) } targetPtrParameter = callBuilder.passThroughBridge( expression.extensionReceiver!!, - symbols.interopCPointer.typeWithStarProjections, + symbols.interopCPointer.starProjectedType, CTypes.voidPtr ).name targetFunctionName = "targetPtr" @@ -336,7 +336,7 @@ internal fun KotlinStubs.generateObjCCall( val targetPtrParameter = callBuilder.passThroughBridge( messenger, - symbols.interopCPointer.typeWithStarProjections, + symbols.interopCPointer.starProjectedType, CTypes.voidPtr ).name val targetFunctionName = "targetPtr" @@ -402,7 +402,7 @@ internal fun KotlinStubs.generateObjCCall( internal fun IrBuilderWithScope.getObjCClass(symbols: KonanSymbols, symbol: IrClassSymbol): IrExpression { val classDescriptor = symbol.descriptor require(!classDescriptor.isObjCMetaClass()) - return irCall(symbols.interopGetObjCClass, symbols.nativePtrType, listOf(symbol.typeWithStarProjections)) + return irCall(symbols.interopGetObjCClass, symbols.nativePtrType, listOf(symbol.starProjectedType)) } private fun IrBuilderWithScope.irNullNativePtr(symbols: KonanSymbols) = irCall(symbols.getNativeNullPtr.owner) @@ -708,7 +708,7 @@ private fun KotlinStubs.mapType( type.isFloat() -> TrivialValuePassing(irBuiltIns.floatType, CTypes.float) type.isDouble() -> TrivialValuePassing(irBuiltIns.doubleType, CTypes.double) type.isCPointer(symbols) -> TrivialValuePassing(type, CTypes.voidPtr) - type.isTypeOfNullLiteral() && variadic -> TrivialValuePassing(symbols.interopCPointer.typeWithStarProjections.makeNullable(), CTypes.voidPtr) + type.isTypeOfNullLiteral() && variadic -> TrivialValuePassing(symbols.interopCPointer.starProjectedType.makeNullable(), CTypes.voidPtr) type.isUByte() -> TrivialValuePassing(type, CTypes.unsignedChar) type.isUShort() -> TrivialValuePassing(type, CTypes.unsignedShort) type.isUInt() -> TrivialValuePassing(type, CTypes.unsignedInt) @@ -867,7 +867,7 @@ private class StructValuePassing(private val kotlinClass: IrClass, override val override fun KotlinToCCallBuilder.passValue(expression: IrExpression): CExpression { val cBridgeValue = passThroughBridge( cValuesRefToPointer(expression), - symbols.interopCPointer.typeWithStarProjections, + symbols.interopCPointer.starProjectedType, CTypes.pointer(cType) ).name @@ -1314,7 +1314,7 @@ private object CValuesRefArgumentPassing : KotlinToCArgumentPassing { val bridgeArgument = cValuesRefToPointer(expression) val cBridgeValue = passThroughBridge( bridgeArgument, - symbols.interopCPointer.typeWithStarProjections.makeNullable(), + symbols.interopCPointer.starProjectedType.makeNullable(), CTypes.voidPtr ) return CExpression(cBridgeValue.name, cBridgeValue.type) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/IrTypeAsKotlinType.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/IrTypeAsKotlinType.kt deleted file mode 100644 index a52a178338f..00000000000 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/IrTypeAsKotlinType.kt +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ - -package org.jetbrains.kotlin.backend.konan.ir - -import org.jetbrains.kotlin.ir.declarations.IrClass -import org.jetbrains.kotlin.ir.symbols.IrClassSymbol -import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol -import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol -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.types.KotlinType -import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf - -val IrClassifierSymbol.typeWithoutArguments: IrType - get() = when (this) { - is IrClassSymbol -> { - require(this.descriptor.declaredTypeParameters.isEmpty()) - this.typeWith(arguments = emptyList()) - } - is IrTypeParameterSymbol -> this.defaultType - else -> error(this) - } - -val IrClassifierSymbol.typeWithStarProjections - get() = when (this) { - is IrClassSymbol -> createType( - hasQuestionMark = false, - arguments = this.descriptor.declaredTypeParameters.map { IrStarProjectionImpl } - ) - is IrTypeParameterSymbol -> this.defaultType - else -> error(this) - } - -val IrTypeParameterSymbol.defaultType: IrType get() = IrSimpleTypeImpl( - this, - false, - emptyList(), - emptyList() -) - -fun IrClass.typeWith(arguments: List) = this.symbol.typeWith(arguments) - -fun IrType.containsNull(): Boolean = if (this is IrSimpleType) { - if (this.hasQuestionMark) { - true - } else { - val classifier = this.classifier - when (classifier) { - is IrClassSymbol -> false - is IrTypeParameterSymbol -> classifier.owner.superTypes.any { it.containsNull() } - else -> error(classifier) - } - } -} else { - true -} - -// TODO: get rid of these: -fun IrType.isSubtypeOf(other: KotlinType): Boolean = this.toKotlinType().isSubtypeOf(other) -fun IrType.isSubtypeOf(other: IrType): Boolean = this.isSubtypeOf(other.toKotlinType()) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/NewIrUtils.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/NewIrUtils.kt index 4cfde62b7c5..c137861346e 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/NewIrUtils.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/NewIrUtils.kt @@ -24,9 +24,7 @@ import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl import org.jetbrains.kotlin.ir.symbols.IrClassSymbol -import org.jetbrains.kotlin.ir.types.IdSignatureValues -import org.jetbrains.kotlin.ir.types.classifierOrFail -import org.jetbrains.kotlin.ir.types.isMarkedNullable +import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.library.KotlinLibrary import org.jetbrains.kotlin.name.FqName diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index 6946b3b9ac0..5e6bf36f359 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -1565,7 +1565,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map irKClassUnsupported(context, "KClass for interop types is not supported yet") - else -> irConstantObject(symbols.kClassImplIntrinsicConstructor, emptyList(), listOf(symbol.typeWithStarProjections)) + else -> irConstantObject(symbols.kClassImplIntrinsicConstructor, emptyList(), listOf(symbol.starProjectedType)) } } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/TestProcessor.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/TestProcessor.kt index 25ee82f3590..d3ad800b698 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/TestProcessor.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/TestProcessor.kt @@ -5,18 +5,13 @@ package org.jetbrains.kotlin.backend.konan.lower -import org.jetbrains.kotlin.backend.common.ir.addFakeOverrides -import org.jetbrains.kotlin.backend.common.ir.createDispatchReceiverParameter -import org.jetbrains.kotlin.backend.common.ir.createParameterDeclarations -import org.jetbrains.kotlin.backend.common.ir.simpleFunctions +import org.jetbrains.kotlin.backend.common.ir.* import org.jetbrains.kotlin.backend.common.lower.createIrBuilder import org.jetbrains.kotlin.backend.common.reportWarning import org.jetbrains.kotlin.backend.konan.Context import org.jetbrains.kotlin.backend.konan.descriptors.isAbstract import org.jetbrains.kotlin.backend.konan.descriptors.synthesizedName import org.jetbrains.kotlin.backend.konan.getIncludedLibraryDescriptors -import org.jetbrains.kotlin.backend.konan.ir.typeWithStarProjections -import org.jetbrains.kotlin.backend.konan.ir.typeWithoutArguments import org.jetbrains.kotlin.backend.konan.reportCompilationError import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.DescriptorVisibilities @@ -40,6 +35,8 @@ import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* +import org.jetbrains.kotlin.ir.util.SetDeclarationsParentVisitor +import org.jetbrains.kotlin.ir.util.addChild import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils @@ -123,7 +120,7 @@ internal class TestProcessor (val context: Context) { putValueArgument(0, IrGetEnumValueImpl( it.function.startOffset, it.function.endOffset, - symbols.testFunctionKind.typeWithoutArguments, + symbols.testFunctionKind.typeWithArguments(emptyList()), testKindEntry) ) putValueArgument(1, IrFunctionReferenceImpl( @@ -349,7 +346,7 @@ internal class TestProcessor (val context: Context) { getterName, DescriptorVisibilities.PROTECTED, Modality.FINAL, - objectSymbol.typeWithStarProjections, + objectSymbol.starProjectedType, isInline = false, isExternal = false, isTailrec = false, @@ -368,7 +365,7 @@ internal class TestProcessor (val context: Context) { overriddenSymbols += superFunction.symbol body = context.createIrBuilder(symbol, symbol.owner.startOffset, symbol.owner.endOffset).irBlockBody { - +irReturn(irGetObjectValue(objectSymbol.typeWithoutArguments, objectSymbol) + +irReturn(irGetObjectValue(objectSymbol.typeWithArguments(emptyList()), objectSymbol) ) } } @@ -387,7 +384,7 @@ internal class TestProcessor (val context: Context) { getterName, DescriptorVisibilities.PROTECTED, Modality.FINAL, - classSymbol.typeWithStarProjections, + classSymbol.starProjectedType, isInline = false, isExternal = false, isTailrec = false, @@ -435,7 +432,7 @@ internal class TestProcessor (val context: Context) { IrConstructorSymbolImpl(), Name.special(""), DescriptorVisibilities.PUBLIC, - testSuite.typeWithStarProjections, + testSuite.starProjectedType, isInline = false, isExternal = false, isPrimary = true, @@ -479,8 +476,10 @@ internal class TestProcessor (val context: Context) { */ private fun buildClassSuite(testClass: IrClass, testCompanion: IrClass?, - functions: Collection): IrClass = - IrClassImpl( + functions: Collection, + irFile: IrFile + ): IrClass { + return IrClassImpl( testClass.startOffset, testClass.endOffset, TEST_SUITE_CLASS, IrClassSymbolImpl(), @@ -496,6 +495,7 @@ internal class TestProcessor (val context: Context) { isExpect = false, isFun = false ).apply { + irFile.addChild(this) createParameterDeclarations() val testClassType = testClass.defaultType @@ -530,12 +530,12 @@ internal class TestProcessor (val context: Context) { superTypes += symbols.baseClassSuite.typeWith(listOf(testClassType, testCompanionType)) addFakeOverrides(context.typeSystem) } + } //endregion // region IR generation methods private fun generateClassSuite(irFile: IrFile, testClass: TestClass) = - with(buildClassSuite(testClass.ownerClass, testClass.companion,testClass.functions)) { - irFile.addChild(this) + with(buildClassSuite(testClass.ownerClass, testClass.companion, testClass.functions, irFile)) { val irConstructor = constructors.single() val irBuilder = context.createIrBuilder(irFile.symbol, testClass.ownerClass.startOffset, testClass.ownerClass.endOffset) irBuilder.irCall(irConstructor) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/TypeOperatorLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/TypeOperatorLowering.kt index 01061124a5f..24b5ddf00f2 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/TypeOperatorLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/TypeOperatorLowering.kt @@ -8,8 +8,6 @@ package org.jetbrains.kotlin.backend.konan.lower import org.jetbrains.kotlin.backend.common.CommonBackendContext import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.common.lower.* -import org.jetbrains.kotlin.backend.konan.ir.containsNull -import org.jetbrains.kotlin.backend.konan.ir.isSubtypeOf import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.expressions.IrExpression @@ -64,9 +62,9 @@ internal class TypeOperatorLowering(val context: CommonBackendContext) : FileLow // TODO: consider the case when expression type is wrong e.g. due to generics-related unchecked casts. return when { - expression.argument.type.isSubtypeOf(typeOperand) -> expression.argument + expression.argument.type.isSubtypeOf(typeOperand, context.typeSystem) -> expression.argument - expression.argument.type.containsNull() -> { + expression.argument.type.isNullable() -> { with(builder) { irLetS(expression.argument) { argument -> irIfThenElse(