From 80b7194799cb024958241514eaa56dcf63d9e0d6 Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Thu, 17 Sep 2020 19:07:40 +0300 Subject: [PATCH] IR: remove KotlinType usage from Ir.kt --- .../jetbrains/kotlin/backend/common/ir/Ir.kt | 41 +++++++++++-------- .../lower/DefaultArgumentStubGenerator.kt | 6 ++- .../compiler/backend/ir/GeneratorHelpers.kt | 6 +-- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt index dc23c5c5e1b..c83e1029eec 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt @@ -20,13 +20,13 @@ import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol 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.classOrNull import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.calls.components.isVararg -import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly @@ -226,33 +226,40 @@ open class BuiltinSymbolsBase(protected val irBuiltIns: IrBuiltIns, protected va val mutableListIterator = symbolTable.referenceClass(builtIns.mutableListIterator) val comparable = symbolTable.referenceClass(builtIns.comparable) - private val binaryOperatorCache = mutableMapOf, IrSimpleFunctionSymbol>() + private val binaryOperatorCache = mutableMapOf, IrSimpleFunctionSymbol>() - fun getBinaryOperator(name: Name, lhsType: KotlinType, rhsType: KotlinType): IrSimpleFunctionSymbol { + fun getBinaryOperator(name: Name, lhsType: IrType, rhsType: IrType): IrSimpleFunctionSymbol { + require(lhsType is IrSimpleType) { "Expected IrSimpleType in getBinaryOperator, got $lhsType" } + val classifier = lhsType.classifier + require(classifier is IrClassSymbol && classifier.isBound) { + "Expected a bound IrClassSymbol for lhsType in getBinaryOperator, got $classifier" + } val key = Triple(name, lhsType, rhsType) return binaryOperatorCache.getOrPut(key) { - symbolTable.referenceSimpleFunction( - lhsType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) - .first { it.valueParameters.size == 1 && it.valueParameters[0].type == rhsType } - ) + classifier.functions.single { + val function = it.owner + function.name == name && function.valueParameters.size == 1 && function.valueParameters[0].type == rhsType + } } } - private val unaryOperatorCache = mutableMapOf, IrSimpleFunctionSymbol>() + private val unaryOperatorCache = mutableMapOf, IrSimpleFunctionSymbol>() - fun getUnaryOperator(name: Name, receiverType: KotlinType): IrSimpleFunctionSymbol { - val key = name to receiverType + fun getUnaryOperator(name: Name, receiverType: IrType): IrSimpleFunctionSymbol { + require(receiverType is IrSimpleType) { "Expected IrSimpleType in getBinaryOperator, got $receiverType" } + val classifier = receiverType.classifier + require(classifier is IrClassSymbol && classifier.isBound) { + "Expected a bound IrClassSymbol for receiverType in getBinaryOperator, got $classifier" + } + val key = Pair(name, receiverType) return unaryOperatorCache.getOrPut(key) { - symbolTable.referenceSimpleFunction( - receiverType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) - .first { it.valueParameters.isEmpty() } - ) + classifier.functions.single { + val function = it.owner + function.name == name && function.valueParameters.isEmpty() + } } } - val intAnd = getBinaryOperator(OperatorNameConventions.AND, builtIns.intType, builtIns.intType) - val intPlusInt = getBinaryOperator(OperatorNameConventions.PLUS, builtIns.intType, builtIns.intType) - open fun functionN(n: Int): IrClassSymbol = irBuiltIns.function(n) open fun suspendFunctionN(n: Int): IrClassSymbol = irBuiltIns.suspendFunction(n) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt index a0d88d354f1..3a62e99a198 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.util.OperatorNameConventions // TODO: fix expect/actual default parameters @@ -99,6 +100,9 @@ open class DefaultArgumentStubGenerator( generateSuperCallHandlerCheckIfNeeded(irFunction, newIrFunction) + val intAnd = this@DefaultArgumentStubGenerator.context.ir.symbols.getBinaryOperator( + OperatorNameConventions.AND, context.irBuiltIns.intType, context.irBuiltIns.intType + ) var sourceParameterIndex = -1 for (valueParameter in irFunction.valueParameters) { if (!valueParameter.isMovedReceiver()) { @@ -109,7 +113,7 @@ open class DefaultArgumentStubGenerator( val mask = irGet(newIrFunction.valueParameters[irFunction.valueParameters.size + valueParameter.index / 32]) val bit = irInt(1 shl (sourceParameterIndex % 32)) val defaultFlag = - irCallOp(this@DefaultArgumentStubGenerator.context.ir.symbols.intAnd, context.irBuiltIns.intType, mask, bit) + irCallOp(intAnd, context.irBuiltIns.intType, mask, bit) val expression = valueParameter.defaultValue!!.expression .prepareToBeUsedIn(newIrFunction) diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/GeneratorHelpers.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/GeneratorHelpers.kt index c0b3a73fc25..35ee1319393 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/GeneratorHelpers.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/GeneratorHelpers.kt @@ -109,11 +109,7 @@ interface IrBuilderExtension { } fun IrBuilderWithScope.irBinOp(name: Name, lhs: IrExpression, rhs: IrExpression): IrExpression { - val symbol = compilerContext.symbols.getBinaryOperator( - name, - lhs.type.toKotlinType(), - rhs.type.toKotlinType() - ) + val symbol = compilerContext.symbols.getBinaryOperator(name, lhs.type, rhs.type) return irInvoke(lhs, symbol, rhs) }