[K/N][IR] Refactored a bit FunctionReferenceLowering

This commit is contained in:
Igor Chevdar
2022-10-13 13:51:10 +03:00
committed by Space Team
parent d16bbb1145
commit 7a7f1d559d
@@ -13,22 +13,15 @@ import org.jetbrains.kotlin.backend.common.push
import org.jetbrains.kotlin.backend.konan.Context import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.backend.konan.descriptors.synthesizedName import org.jetbrains.kotlin.backend.konan.descriptors.synthesizedName
import org.jetbrains.kotlin.backend.konan.llvm.computeFullName import org.jetbrains.kotlin.backend.konan.llvm.computeFullName
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.addFunction import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl
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.types.*
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
@@ -36,7 +29,6 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
internal class FunctionReferenceLowering(val context: Context) : FileLoweringPass { internal class FunctionReferenceLowering(val context: Context) : FileLoweringPass {
private object DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL : IrDeclarationOriginImpl("FUNCTION_REFERENCE_IMPL") private object DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL : IrDeclarationOriginImpl("FUNCTION_REFERENCE_IMPL")
companion object { companion object {
@@ -157,7 +149,6 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
private val VOLATILE_LAMBDA_FQ_NAME = FqName.fromSegments(listOf("kotlin", "native", "internal", "VolatileLambda")) private val VOLATILE_LAMBDA_FQ_NAME = FqName.fromSegments(listOf("kotlin", "native", "internal", "VolatileLambda"))
class FunctionReferenceBuilder( class FunctionReferenceBuilder(
val irFile: IrFile, val irFile: IrFile,
val parent: IrDeclarationParent, val parent: IrDeclarationParent,
@@ -170,6 +161,8 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
private val irBuiltIns = context.irBuiltIns private val irBuiltIns = context.irBuiltIns
private val symbols = context.ir.symbols private val symbols = context.ir.symbols
private val irFactory = context.irFactory
private val startOffset = functionReference.startOffset private val startOffset = functionReference.startOffset
private val endOffset = functionReference.endOffset private val endOffset = functionReference.endOffset
private val referencedFunction = functionReference.symbol.owner private val referencedFunction = functionReference.symbol.owner
@@ -197,23 +190,13 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
private val functionReferenceTarget = adaptedReferenceOriginalTarget ?: referencedFunction private val functionReferenceTarget = adaptedReferenceOriginalTarget ?: referencedFunction
private val functionReferenceClass: IrClass = private val functionReferenceClass = irFactory.buildClass {
IrClassImpl( startOffset = this@FunctionReferenceBuilder.startOffset
startOffset, endOffset, endOffset = this@FunctionReferenceBuilder.endOffset
DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL
IrClassSymbolImpl(), name = "${functionReferenceTarget.name}\$FUNCTION_REFERENCE\$${context.functionReferenceCount++}".synthesizedName
"${functionReferenceTarget.name}\$FUNCTION_REFERENCE\$${context.functionReferenceCount++}".synthesizedName, visibility = DescriptorVisibilities.PRIVATE
ClassKind.CLASS, }.apply {
DescriptorVisibilities.PRIVATE,
Modality.FINAL,
isCompanion = false,
isInner = false,
isData = false,
isExternal = false,
isValue = false,
isExpect = false,
isFun = false
).apply {
parent = this@FunctionReferenceBuilder.parent parent = this@FunctionReferenceBuilder.parent
createParameterDeclarations() createParameterDeclarations()
@@ -223,15 +206,15 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
private val functionReferenceThis = functionReferenceClass.thisReceiver!! private val functionReferenceThis = functionReferenceClass.thisReceiver!!
private val argumentToPropertiesMap = boundFunctionParameters.associate { private val argumentToPropertiesMap = boundFunctionParameters.associateWith {
it to createField( functionReferenceClass.addField {
startOffset, endOffset, startOffset = this@FunctionReferenceBuilder.startOffset
DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, endOffset = this@FunctionReferenceBuilder.endOffset
it.type, origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL
it.name, name = it.name
isMutable = false, type = it.type
owner = functionReferenceClass isFinal = true
) }
} }
private fun IrClass.getInvokeFunction() = simpleFunctions().single { it.name.asString() == "invoke" } private fun IrClass.getInvokeFunction() = simpleFunctions().single { it.name.asString() == "invoke" }
@@ -331,22 +314,12 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
return functionReferenceClass return functionReferenceClass
} }
private fun buildConstructor(): IrConstructor { private fun buildConstructor() = functionReferenceClass.addConstructor {
return IrConstructorImpl( startOffset = this@FunctionReferenceBuilder.startOffset
startOffset, endOffset, endOffset = this@FunctionReferenceBuilder.endOffset
DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL
IrConstructorSymbolImpl(), isPrimary = true
Name.special("<init>"), }.apply {
DescriptorVisibilities.PUBLIC,
functionReferenceClass.defaultType,
isInline = false,
isExternal = false,
isPrimary = true,
isExpect = false
).apply {
parent = functionReferenceClass
functionReferenceClass.declarations += this
valueParameters += boundFunctionParameters.mapIndexed { index, parameter -> valueParameters += boundFunctionParameters.mapIndexed { index, parameter ->
parameter.copyTo(this, DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, index, parameter.copyTo(this, DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, index,
type = parameter.type.substitute(typeArgumentsMap)) type = parameter.type.substitute(typeArgumentsMap))
@@ -366,7 +339,6 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
} }
} }
} }
}
fun build(): BuiltFunctionReference { fun build(): BuiltFunctionReference {
val clazz = buildClass() val clazz = buildClass()
@@ -425,29 +397,17 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
return false return false
} }
private fun buildInvokeMethod(superFunction: IrSimpleFunction): IrSimpleFunction { private fun buildInvokeMethod(superFunction: IrSimpleFunction) = functionReferenceClass.addFunction {
return IrFunctionImpl( startOffset = this@FunctionReferenceBuilder.startOffset
startOffset, endOffset, endOffset = this@FunctionReferenceBuilder.endOffset
DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL
IrSimpleFunctionSymbolImpl(), name = superFunction.name
superFunction.name, returnType = functionReturnType
DescriptorVisibilities.PRIVATE, isSuspend = superFunction.isSuspend
Modality.FINAL, }.apply {
functionReturnType,
isInline = false,
isExternal = false,
isTailrec = false,
isSuspend = superFunction.isSuspend,
isExpect = false,
isFakeOverride = false,
isOperator = false,
isInfix = false
).apply {
val function = this val function = this
parent = functionReferenceClass
functionReferenceClass.declarations += function
this.createDispatchReceiverParameter() function.createDispatchReceiverParameter()
extensionReceiverParameter = superFunction.extensionReceiverParameter?.copyTo(function) extensionReceiverParameter = superFunction.extensionReceiverParameter?.copyTo(function)
@@ -495,4 +455,3 @@ internal class FunctionReferenceLowering(val context: Context) : FileLoweringPas
} }
} }
} }
}