IR: remove KotlinType usage from Ir.kt

This commit is contained in:
Georgy Bronnikov
2020-09-17 19:07:40 +03:00
parent 7afad9a91d
commit 80b7194799
3 changed files with 30 additions and 23 deletions
@@ -20,13 +20,13 @@ import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol 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.IrType
import org.jetbrains.kotlin.ir.types.classOrNull import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.calls.components.isVararg 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.OperatorNameConventions
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly 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 mutableListIterator = symbolTable.referenceClass(builtIns.mutableListIterator)
val comparable = symbolTable.referenceClass(builtIns.comparable) val comparable = symbolTable.referenceClass(builtIns.comparable)
private val binaryOperatorCache = mutableMapOf<Triple<Name, KotlinType, KotlinType>, IrSimpleFunctionSymbol>() private val binaryOperatorCache = mutableMapOf<Triple<Name, IrType, IrType>, 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) val key = Triple(name, lhsType, rhsType)
return binaryOperatorCache.getOrPut(key) { return binaryOperatorCache.getOrPut(key) {
symbolTable.referenceSimpleFunction( classifier.functions.single {
lhsType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) val function = it.owner
.first { it.valueParameters.size == 1 && it.valueParameters[0].type == rhsType } function.name == name && function.valueParameters.size == 1 && function.valueParameters[0].type == rhsType
) }
} }
} }
private val unaryOperatorCache = mutableMapOf<Pair<Name, KotlinType>, IrSimpleFunctionSymbol>() private val unaryOperatorCache = mutableMapOf<Pair<Name, IrType>, IrSimpleFunctionSymbol>()
fun getUnaryOperator(name: Name, receiverType: KotlinType): IrSimpleFunctionSymbol { fun getUnaryOperator(name: Name, receiverType: IrType): IrSimpleFunctionSymbol {
val key = name to receiverType 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) { return unaryOperatorCache.getOrPut(key) {
symbolTable.referenceSimpleFunction( classifier.functions.single {
receiverType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) val function = it.owner
.first { it.valueParameters.isEmpty() } 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 functionN(n: Int): IrClassSymbol = irBuiltIns.function(n)
open fun suspendFunctionN(n: Int): IrClassSymbol = irBuiltIns.suspendFunction(n) open fun suspendFunctionN(n: Int): IrClassSymbol = irBuiltIns.suspendFunction(n)
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.util.OperatorNameConventions
// TODO: fix expect/actual default parameters // TODO: fix expect/actual default parameters
@@ -99,6 +100,9 @@ open class DefaultArgumentStubGenerator(
generateSuperCallHandlerCheckIfNeeded(irFunction, newIrFunction) generateSuperCallHandlerCheckIfNeeded(irFunction, newIrFunction)
val intAnd = this@DefaultArgumentStubGenerator.context.ir.symbols.getBinaryOperator(
OperatorNameConventions.AND, context.irBuiltIns.intType, context.irBuiltIns.intType
)
var sourceParameterIndex = -1 var sourceParameterIndex = -1
for (valueParameter in irFunction.valueParameters) { for (valueParameter in irFunction.valueParameters) {
if (!valueParameter.isMovedReceiver()) { if (!valueParameter.isMovedReceiver()) {
@@ -109,7 +113,7 @@ open class DefaultArgumentStubGenerator(
val mask = irGet(newIrFunction.valueParameters[irFunction.valueParameters.size + valueParameter.index / 32]) val mask = irGet(newIrFunction.valueParameters[irFunction.valueParameters.size + valueParameter.index / 32])
val bit = irInt(1 shl (sourceParameterIndex % 32)) val bit = irInt(1 shl (sourceParameterIndex % 32))
val defaultFlag = 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 val expression = valueParameter.defaultValue!!.expression
.prepareToBeUsedIn(newIrFunction) .prepareToBeUsedIn(newIrFunction)
@@ -109,11 +109,7 @@ interface IrBuilderExtension {
} }
fun IrBuilderWithScope.irBinOp(name: Name, lhs: IrExpression, rhs: IrExpression): IrExpression { fun IrBuilderWithScope.irBinOp(name: Name, lhs: IrExpression, rhs: IrExpression): IrExpression {
val symbol = compilerContext.symbols.getBinaryOperator( val symbol = compilerContext.symbols.getBinaryOperator(name, lhs.type, rhs.type)
name,
lhs.type.toKotlinType(),
rhs.type.toKotlinType()
)
return irInvoke(lhs, symbol, rhs) return irInvoke(lhs, symbol, rhs)
} }