[K/N][IR] Refactored a bit FunctionReferenceLowering
This commit is contained in:
+91
-132
@@ -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,41 +190,31 @@ 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,
|
parent = this@FunctionReferenceBuilder.parent
|
||||||
Modality.FINAL,
|
createParameterDeclarations()
|
||||||
isCompanion = false,
|
|
||||||
isInner = false,
|
|
||||||
isData = false,
|
|
||||||
isExternal = false,
|
|
||||||
isValue = false,
|
|
||||||
isExpect = false,
|
|
||||||
isFun = false
|
|
||||||
).apply {
|
|
||||||
parent = this@FunctionReferenceBuilder.parent
|
|
||||||
createParameterDeclarations()
|
|
||||||
|
|
||||||
// copy the generated name for IrClass, partially solves KT-47194
|
// copy the generated name for IrClass, partially solves KT-47194
|
||||||
context.generationState.copyLocalClassName(functionReference, this)
|
context.generationState.copyLocalClassName(functionReference, this)
|
||||||
}
|
}
|
||||||
|
|
||||||
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,39 +314,28 @@ 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,
|
valueParameters += boundFunctionParameters.mapIndexed { index, parameter ->
|
||||||
functionReferenceClass.defaultType,
|
parameter.copyTo(this, DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, index,
|
||||||
isInline = false,
|
type = parameter.type.substitute(typeArgumentsMap))
|
||||||
isExternal = false,
|
}
|
||||||
isPrimary = true,
|
|
||||||
isExpect = false
|
|
||||||
).apply {
|
|
||||||
parent = functionReferenceClass
|
|
||||||
functionReferenceClass.declarations += this
|
|
||||||
|
|
||||||
valueParameters += boundFunctionParameters.mapIndexed { index, parameter ->
|
body = context.createIrBuilder(symbol, startOffset, endOffset).irBlockBody {
|
||||||
parameter.copyTo(this, DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, index,
|
val superConstructor = when {
|
||||||
type = parameter.type.substitute(typeArgumentsMap))
|
isKSuspendFunction -> kSuspendFunctionImplConstructorSymbol.owner
|
||||||
|
isLambda -> irBuiltIns.anyClass.owner.constructors.single()
|
||||||
|
else -> kFunctionImplConstructorSymbol.owner
|
||||||
}
|
}
|
||||||
|
+irDelegatingConstructorCall(superConstructor)
|
||||||
body = context.createIrBuilder(symbol, startOffset, endOffset).irBlockBody {
|
+IrInstanceInitializerCallImpl(startOffset, endOffset, functionReferenceClass.symbol, irBuiltIns.unitType)
|
||||||
val superConstructor = when {
|
// Save all arguments to fields.
|
||||||
isKSuspendFunction -> kSuspendFunctionImplConstructorSymbol.owner
|
boundFunctionParameters.forEachIndexed { index, parameter ->
|
||||||
isLambda -> irBuiltIns.anyClass.owner.constructors.single()
|
+irSetField(irGet(functionReferenceThis), argumentToPropertiesMap[parameter]!!, irGet(valueParameters[index]))
|
||||||
else -> kFunctionImplConstructorSymbol.owner
|
|
||||||
}
|
|
||||||
+irDelegatingConstructorCall(superConstructor)
|
|
||||||
+IrInstanceInitializerCallImpl(startOffset, endOffset, functionReferenceClass.symbol, irBuiltIns.unitType)
|
|
||||||
// Save all arguments to fields.
|
|
||||||
boundFunctionParameters.forEachIndexed { index, parameter ->
|
|
||||||
+irSetField(irGet(functionReferenceThis), argumentToPropertiesMap[parameter]!!, irGet(valueParameters[index]))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -425,73 +397,60 @@ 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,
|
val function = this
|
||||||
isInline = false,
|
|
||||||
isExternal = false,
|
|
||||||
isTailrec = false,
|
|
||||||
isSuspend = superFunction.isSuspend,
|
|
||||||
isExpect = false,
|
|
||||||
isFakeOverride = false,
|
|
||||||
isOperator = false,
|
|
||||||
isInfix = false
|
|
||||||
).apply {
|
|
||||||
val function = this
|
|
||||||
parent = functionReferenceClass
|
|
||||||
functionReferenceClass.declarations += function
|
|
||||||
|
|
||||||
this.createDispatchReceiverParameter()
|
function.createDispatchReceiverParameter()
|
||||||
|
|
||||||
extensionReceiverParameter = superFunction.extensionReceiverParameter?.copyTo(function)
|
extensionReceiverParameter = superFunction.extensionReceiverParameter?.copyTo(function)
|
||||||
|
|
||||||
valueParameters += superFunction.valueParameters.mapIndexed { index, parameter ->
|
valueParameters += superFunction.valueParameters.mapIndexed { index, parameter ->
|
||||||
parameter.copyTo(function, DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, index,
|
parameter.copyTo(function, DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL, index,
|
||||||
type = functionParameterTypes[index])
|
type = functionParameterTypes[index])
|
||||||
}
|
}
|
||||||
|
|
||||||
overriddenSymbols += superFunction.symbol
|
overriddenSymbols += superFunction.symbol
|
||||||
|
|
||||||
body = context.createIrBuilder(function.symbol, startOffset, endOffset).irBlockBody(startOffset, endOffset) {
|
body = context.createIrBuilder(function.symbol, startOffset, endOffset).irBlockBody(startOffset, endOffset) {
|
||||||
+irReturn(
|
+irReturn(
|
||||||
irCall(functionReference.symbol).apply {
|
irCall(functionReference.symbol).apply {
|
||||||
var unboundIndex = 0
|
var unboundIndex = 0
|
||||||
val unboundArgsSet = unboundFunctionParameters.toSet()
|
val unboundArgsSet = unboundFunctionParameters.toSet()
|
||||||
for (parameter in functionParameters) {
|
for (parameter in functionParameters) {
|
||||||
val argument =
|
val argument =
|
||||||
if (!unboundArgsSet.contains(parameter))
|
if (!unboundArgsSet.contains(parameter))
|
||||||
// Bound parameter - read from field.
|
// Bound parameter - read from field.
|
||||||
irGetField(
|
irGetField(
|
||||||
irGet(function.dispatchReceiverParameter!!),
|
irGet(function.dispatchReceiverParameter!!),
|
||||||
argumentToPropertiesMap[parameter]!!
|
argumentToPropertiesMap[parameter]!!
|
||||||
)
|
)
|
||||||
else {
|
else {
|
||||||
if (parameter == referencedFunction.extensionReceiverParameter
|
if (parameter == referencedFunction.extensionReceiverParameter
|
||||||
&& extensionReceiverParameter != null)
|
&& extensionReceiverParameter != null)
|
||||||
irGet(extensionReceiverParameter!!)
|
irGet(extensionReceiverParameter!!)
|
||||||
else
|
else
|
||||||
irGet(valueParameters[unboundIndex++])
|
irGet(valueParameters[unboundIndex++])
|
||||||
}
|
}
|
||||||
when (parameter) {
|
when (parameter) {
|
||||||
referencedFunction.dispatchReceiverParameter -> dispatchReceiver = argument
|
referencedFunction.dispatchReceiverParameter -> dispatchReceiver = argument
|
||||||
referencedFunction.extensionReceiverParameter -> extensionReceiver = argument
|
referencedFunction.extensionReceiverParameter -> extensionReceiver = argument
|
||||||
else -> putValueArgument(parameter.index, argument)
|
else -> putValueArgument(parameter.index, argument)
|
||||||
}
|
|
||||||
}
|
|
||||||
assert(unboundIndex == valueParameters.size) { "Not all arguments of <invoke> are used" }
|
|
||||||
|
|
||||||
referencedFunction.typeParameters.forEach { typeParam ->
|
|
||||||
putTypeArgument(typeParam.index, functionReference.getTypeArgument(typeParam.index)!!)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
assert(unboundIndex == valueParameters.size) { "Not all arguments of <invoke> are used" }
|
||||||
}
|
|
||||||
|
referencedFunction.typeParameters.forEach { typeParam ->
|
||||||
|
putTypeArgument(typeParam.index, functionReference.getTypeArgument(typeParam.index)!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user