[Native] Drop irCall from IrUtils2

We can reuse the same function from common `ExpressionHelpers`
This commit is contained in:
Ivan Kylchik
2023-08-15 19:13:42 +02:00
committed by Space Team
parent 5c68ea2b64
commit 583a5496d5
14 changed files with 66 additions and 76 deletions
@@ -12,10 +12,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.typeWith
import org.jetbrains.kotlin.ir.util.isImmutable
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.util.primaryConstructor
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.util.*
val IrBuilderWithScope.parent get() = scope.getLocalDeclarationParent()
@@ -294,6 +291,15 @@ fun IrBuilderWithScope.irCall(callee: IrFunction, origin: IrStatementOrigin? = n
origin, superQualifierSymbol
)
fun IrBuilderWithScope.irCallWithSubstitutedType(callee: IrFunction, typeArguments: List<IrType>): IrMemberAccessExpression<*> {
val argsMap = callee.typeParameters.map { it.symbol }.zip(typeArguments).toMap()
return irCall(callee.symbol, callee.returnType.substitute(argsMap), typeArguments)
}
fun IrBuilderWithScope.irCallWithSubstitutedType(callee: IrFunctionSymbol, typeArguments: List<IrType>): IrMemberAccessExpression<*> {
return irCallWithSubstitutedType(callee.owner, typeArguments)
}
fun IrBuilderWithScope.irDelegatingConstructorCall(callee: IrConstructor): IrDelegatingConstructorCall =
IrDelegatingConstructorCallImpl(
startOffset, endOffset, context.irBuiltIns.unitType, callee.symbol,
@@ -1020,7 +1020,7 @@ private class ObjCReferenceValuePassing(
override fun IrBuilderWithScope.bridgedToKotlin(expression: IrExpression, symbols: KonanSymbols): IrExpression =
convertPossiblyRetainedObjCPointer(symbols, retained, expression) {
irCall(symbols.interopInterpretObjCPointerOrNull, listOf(type)).apply {
irCallWithSubstitutedType(symbols.interopInterpretObjCPointerOrNull, listOf(type)).apply {
putValueArgument(0, it)
}
}
@@ -90,7 +90,7 @@ internal class BuiltinOperatorLowering(val context: Context) : FileLoweringPass,
reinterpret(expression, expression.type, toType)
private fun IrBuilderWithScope.reinterpret(expression: IrExpression, fromType: IrType, toType: IrType) =
irCall(symbols.reinterpret.owner, listOf(fromType, toType)).apply {
irCallWithSubstitutedType(symbols.reinterpret.owner, listOf(fromType, toType)).apply {
extensionReceiver = expression
}
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetField
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.*
import org.jetbrains.kotlin.name.Name
@@ -164,23 +165,33 @@ internal class ImportCachesAbiTransformer(val generationState: NativeGenerationS
val irClass = field.parentClassOrNull
val property = field.correspondingPropertySymbol?.owner
// Actual scope for builder is the current function that we don't have access to. So we put a new symbol as scope here,
// but it will not affect the result because we are not creating any declarations here.
fun createIrBuilder() = generationState.context.irBuiltIns.createIrBuilder(
IrSimpleFunctionSymbolImpl(), expression.startOffset, expression.endOffset
)
return when {
generationState.llvmModuleSpecification.containsDeclaration(field) -> expression
irClass?.isInner == true && innerClassesSupport.getOuterThisField(irClass) == field -> {
val accessor = cachesAbiSupport.getOuterThisAccessor(irClass)
dependenciesTracker.add(irClass)
return irCall(expression.startOffset, expression.endOffset, accessor, emptyList()).apply {
putValueArgument(0, expression.receiver)
createIrBuilder().run {
irCall(accessor).apply {
putValueArgument(0, expression.receiver)
}
}
}
property?.isLateinit == true -> {
val accessor = cachesAbiSupport.getLateinitPropertyAccessor(property)
dependenciesTracker.add(property)
return irCall(expression.startOffset, expression.endOffset, accessor, emptyList()).apply {
if (irClass != null)
putValueArgument(0, expression.receiver)
createIrBuilder().run {
irCall(accessor).apply {
if (irClass != null)
putValueArgument(0, expression.receiver)
}
}
}
@@ -10,12 +10,12 @@ import org.jetbrains.kotlin.backend.common.lower.at
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.builders.irCallWithSubstitutedType
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.util.irCall
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
internal class DataClassOperatorsLowering(val context: Context) : FileLoweringPass, IrElementTransformer<IrFunction?> {
@@ -54,7 +54,7 @@ internal class DataClassOperatorsLowering(val context: Context) : FileLoweringPa
// TODO: use more precise type arguments.
val typeArguments = (0 until newCallee.typeParameters.size).map { irBuiltins.anyNType }
irCall(newCallee, typeArguments).apply {
irCallWithSubstitutedType(newCallee, typeArguments).apply {
extensionReceiver = argument
}
}
@@ -250,7 +250,7 @@ internal class PropertyDelegationLowering(val generationState: NativeGenerationS
put("setter", setterCallableReference.convert())
}
})
} else irCall(clazz.constructors.single(), receiverTypes + listOf(returnType)).apply {
} else irCallWithSubstitutedType(clazz.constructors.single(), receiverTypes + listOf(returnType)).apply {
putValueArgument(0, name)
putValueArgument(1, getterCallableReference)
if (setterCallableReference != null)
@@ -154,7 +154,7 @@ internal class EnumUsageLowering(val context: Context) : IrElementTransformer<Ir
} else {
// fallback for enums from old klibs
val valuesFunction = irClass.findStaticMethod(Name.identifier("values"))
data.irCall(context.ir.symbols.createEnumEntries, listOf(irClass.defaultType)).apply {
data.irCallWithSubstitutedType(context.ir.symbols.createEnumEntries, listOf(irClass.defaultType)).apply {
putValueArgument(0, data.irCall(valuesFunction))
}
}
@@ -244,14 +244,14 @@ internal class EnumClassLowering(val context: Context) : FileLoweringPass {
if (body is IrSyntheticBody) {
declaration.body = when (body.kind) {
IrSyntheticBodyKind.ENUM_VALUEOF -> context.createIrBuilder(declaration.symbol).irBlockBody(declaration) {
+irReturn(irCall(symbols.valueOfForEnum, listOf(irClass.defaultType)).apply {
+irReturn(irCallWithSubstitutedType(symbols.valueOfForEnum, listOf(irClass.defaultType)).apply {
putValueArgument(0, irGet(declaration.valueParameters[0]))
putValueArgument(1, irGetField(null, valuesField))
})
}
IrSyntheticBodyKind.ENUM_VALUES -> context.createIrBuilder(declaration.symbol).irBlockBody(declaration) {
+irReturn(irCall(symbols.valuesForEnum, listOf(irClass.defaultType)).apply {
+irReturn(irCallWithSubstitutedType(symbols.valuesForEnum, listOf(irClass.defaultType)).apply {
putValueArgument(0, irGetField(null, valuesField))
})
}
@@ -307,7 +307,7 @@ internal class EnumClassLowering(val context: Context) : FileLoweringPass {
}
val entryClass = entryConstructorCall.symbol.owner.constructedClass
irCall(createUninitializedInstance, listOf(entryClass.defaultType))
irCallWithSubstitutedType(createUninitializedInstance, listOf(entryClass.defaultType))
}
)
val instances = irTemporary(irValuesInitializer)
@@ -338,7 +338,7 @@ internal class EnumClassLowering(val context: Context) : FileLoweringPass {
}
}
)
+irCall(createEnumEntries, listOf(irClass.defaultType)).apply {
+irCallWithSubstitutedType(createEnumEntries, listOf(irClass.defaultType)).apply {
putValueArgument(0, irEntriesArray)
}
})
@@ -283,7 +283,7 @@ private fun InteropCallContext.readObjectiveCReferenceFromMemory(
type: IrType
): IrExpression {
val readMemory = readValueFromMemory(nativePtr, symbols.nativePtrType)
return builder.irCall(symbols.interopInterpretObjCPointerOrNull, listOf(type)).apply {
return builder.irCallWithSubstitutedType(symbols.interopInterpretObjCPointerOrNull, listOf(type)).apply {
putValueArgument(0, readMemory)
}
}
@@ -271,7 +271,7 @@ private class InteropLoweringPart1(val generationState: NativeGenerationState) :
result.body = context.createIrBuilder(result.symbol).irBlockBody(result) {
+irReturn(
irCall(symbols.interopObjCObjectInitBy, listOf(irClass.defaultType)).apply {
irCallWithSubstitutedType(symbols.interopObjCObjectInitBy, listOf(irClass.defaultType)).apply {
extensionReceiver = irGet(result.dispatchReceiverParameter!!)
putValueArgument(0, irCall(constructor).also {
result.valueParameters.forEach { parameter ->
@@ -415,7 +415,7 @@ private class InteropLoweringPart1(val generationState: NativeGenerationState) :
symbols.interopInterpretObjCPointer
}
return irCall(callee, listOf(type)).apply {
return irCallWithSubstitutedType(callee, listOf(type)).apply {
putValueArgument(0, expression)
}
}
@@ -6,14 +6,17 @@
package org.jetbrains.kotlin.backend.konan.lower
import org.jetbrains.kotlin.backend.common.lower.DefaultParameterInjector
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.konan.KonanBackendContext
import org.jetbrains.kotlin.backend.konan.PrimitiveBinaryType
import org.jetbrains.kotlin.backend.konan.computePrimitiveBinaryTypeOrNull
import org.jetbrains.kotlin.backend.konan.getInlinedClassNative
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irCallWithSubstitutedType
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.irCall
internal class NativeDefaultParameterInjector(context: KonanBackendContext) : DefaultParameterInjector<KonanBackendContext>(
context = context,
@@ -24,6 +27,10 @@ internal class NativeDefaultParameterInjector(context: KonanBackendContext) : De
override fun nullConst(startOffset: Int, endOffset: Int, type: IrType): IrExpression {
val symbols = context.ir.symbols
// Actual scope for builder is the current function that we don't have access to. So we put a new symbol as scope here,
// but it will not affect the result because we are not creating any declarations here.
fun createIrBuilder() = context.irBuiltIns.createIrBuilder(IrSimpleFunctionSymbolImpl(), startOffset, endOffset)
val nullConstOfEquivalentType = when (type.computePrimitiveBinaryTypeOrNull()) {
null -> IrConstImpl.constNull(startOffset, endOffset, context.irBuiltIns.nothingNType)
PrimitiveBinaryType.BOOLEAN -> IrConstImpl.boolean(startOffset, endOffset, type, false)
@@ -36,17 +43,14 @@ internal class NativeDefaultParameterInjector(context: KonanBackendContext) : De
PrimitiveBinaryType.LONG -> IrConstImpl.long(startOffset, endOffset, type, 0)
PrimitiveBinaryType.FLOAT -> IrConstImpl.float(startOffset, endOffset, type, 0.0F)
PrimitiveBinaryType.DOUBLE -> IrConstImpl.double(startOffset, endOffset, type, 0.0)
PrimitiveBinaryType.POINTER -> irCall(startOffset, endOffset, symbols.getNativeNullPtr.owner, emptyList())
PrimitiveBinaryType.POINTER -> with(createIrBuilder()) { irCall(symbols.getNativeNullPtr.owner) }
PrimitiveBinaryType.VECTOR128 -> TODO()
}
return irCall(
startOffset,
endOffset,
symbols.reinterpret.owner,
listOf(nullConstOfEquivalentType.type, type)
).apply {
extensionReceiver = nullConstOfEquivalentType
return with(createIrBuilder()) {
irCallWithSubstitutedType(symbols.reinterpret, listOf(nullConstOfEquivalentType.type, type)).apply {
extensionReceiver = nullConstOfEquivalentType
}
}
}
}
@@ -98,8 +98,8 @@ internal class ObjectClassLowering(val generationState: NativeGenerationState) :
fun IrBuilderWithScope.irGetObjCClassCompanion(declaration: IrClass): IrExpression {
require(declaration.isCompanion && (declaration.parent as IrClass).isObjCClass())
return irCall(symbols.interopInterpretObjCPointer, listOf(declaration.defaultType)).apply {
putValueArgument(0, irCall(symbols.interopGetObjCClass, listOf((declaration.parent as IrClass).defaultType)))
return irCallWithSubstitutedType(symbols.interopInterpretObjCPointer, listOf(declaration.defaultType)).apply {
putValueArgument(0, irCallWithSubstitutedType(symbols.interopGetObjCClass, listOf((declaration.parent as IrClass).defaultType)))
}
}
@@ -131,7 +131,8 @@ internal class ObjectClassLowering(val generationState: NativeGenerationState) :
} else {
builder.irBlock {
// we need to make object available for rereading from the same thread while initializing
+irSetField(null, field, irCall(symbols.createUninitializedInstance, listOf(declaration.defaultType)), origin = IrStatementOriginFieldPreInit)
val uninitializedInstanceCall = irCallWithSubstitutedType(symbols.createUninitializedInstance, listOf(declaration.defaultType))
+irSetField(null, field, uninitializedInstanceCall, origin = IrStatementOriginFieldPreInit)
+irCall(symbols.initInstance).apply {
putValueArgument(0, irGetField(null, field))
putValueArgument(1, irCallConstructor(primaryConstructor.symbol, emptyList()))
@@ -14,6 +14,8 @@ import org.jetbrains.kotlin.backend.konan.llvm.ConstantConstructorIntrinsicType
import org.jetbrains.kotlin.backend.konan.llvm.tryGetConstantConstructorIntrinsicType
import org.jetbrains.kotlin.backend.konan.renderCompilerError
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irCallWithSubstitutedType
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrDeclarationBase
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
@@ -22,7 +24,6 @@ import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.util.file
import org.jetbrains.kotlin.ir.util.irCall
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
/**
@@ -58,7 +59,7 @@ internal class PostInlineLowering(val context: Context) : BodyLoweringPass {
expression.transformChildren(this, data)
return data.at(expression).run {
irCall(symbols.kClassImplConstructor, listOf(expression.argument.type)).apply {
irCallWithSubstitutedType(symbols.kClassImplConstructor, listOf(expression.argument.type)).apply {
val typeInfo = irCall(symbols.getObjectTypeInfo).apply {
putValueArgument(0, expression.argument)
}
@@ -1524,7 +1524,7 @@ internal object DevirtualizationAnalysis {
possibleCallees.isEmpty() -> irBlock(expression) {
val throwExpr = irCall(symbols.throwInvalidReceiverTypeException.owner).apply {
putValueArgument(0,
irCall(symbols.kClassImplConstructor.owner, listOf(dispatchReceiver.type)).apply {
irCallWithSubstitutedType(symbols.kClassImplConstructor.owner, listOf(dispatchReceiver.type)).apply {
putValueArgument(0,
irCall(symbols.getObjectTypeInfo.owner).apply {
putValueArgument(0, dispatchReceiver)
@@ -1582,7 +1582,7 @@ internal object DevirtualizationAnalysis {
}
} else {
val receiverType = actualCallee.irFunction!!.parentAsClass
irCall(isSubtype, listOf(receiverType.defaultType)).apply {
irCallWithSubstitutedType(isSubtype, listOf(receiverType.defaultType)).apply {
putValueArgument(0, irGet(typeInfo))
}
}
@@ -1601,7 +1601,7 @@ internal object DevirtualizationAnalysis {
condition = irTrue(),
result = irCall(symbols.throwInvalidReceiverTypeException).apply {
putValueArgument(0,
irCall(symbols.kClassImplConstructor, listOf(dispatchReceiver.type)).apply {
irCallWithSubstitutedType(symbols.kClassImplConstructor, listOf(dispatchReceiver.type)).apply {
putValueArgument(0, irGet(typeInfo))
}
)
@@ -14,22 +14,19 @@ import org.jetbrains.kotlin.ir.IrFileEntry
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.declarations.buildVariable
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.parent
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFieldImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrConstantValue
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrCatchImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrExpressionBodyImpl
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.makeNullable
import org.jetbrains.kotlin.ir.types.typeWith
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.calls.checkers.isRestrictsSuspensionReceiver
@@ -97,36 +94,6 @@ fun IrFunctionAccessExpression.addArguments(args: Map<IrValueParameter, IrExpres
}
}
private fun IrFunction.substitutedReturnType(typeArguments: List<IrType>): IrType {
val unsubstituted = this.returnType
if (typeArguments.isEmpty()) return unsubstituted // Fast path.
if (this is IrConstructor) {
// Workaround for missing type parameters in constructors. TODO: remove.
return this.returnType.classifierOrFail.typeWith(typeArguments)
}
assert(this.typeParameters.size >= typeArguments.size) // TODO: check equality.
// TODO: receiver type must also be considered.
return unsubstituted.substitute(this.typeParameters.map { it.symbol }.zip(typeArguments).toMap())
}
// TODO: this function must be avoided since it takes symbol's owner implicitly.
fun IrBuilderWithScope.irCall(symbol: IrFunctionSymbol, typeArguments: List<IrType> = emptyList()) =
this.irCall(symbol, symbol.owner.substitutedReturnType(typeArguments), typeArguments)
fun IrBuilderWithScope.irCall(irFunction: IrFunction, typeArguments: List<IrType> = emptyList()) =
irCall(irFunction.symbol, typeArguments)
internal fun irCall(startOffset: Int, endOffset: Int, irFunction: IrSimpleFunction, typeArguments: List<IrType>): IrCall =
IrCallImpl.fromSymbolOwner(
startOffset, endOffset, irFunction.substitutedReturnType(typeArguments),
irFunction.symbol, typeArguments.size, irFunction.valueParameters.size
).apply {
typeArguments.forEachIndexed { index, irType ->
this.putTypeArgument(index, irType)
}
}
fun IrBuilderWithScope.irCatch() =
IrCatchImpl(
startOffset, endOffset,