Rewrote CallableReferenceLowering to use symbols
This commit is contained in:
+2
-6
@@ -52,13 +52,9 @@ abstract internal class IrElementTransformerVoidWithContext : IrElementTransform
|
|||||||
}
|
}
|
||||||
|
|
||||||
override final fun visitField(declaration: IrField): IrStatement {
|
override final fun visitField(declaration: IrField): IrStatement {
|
||||||
val peek = scopeStack.peek()
|
scopeStack.push(ScopeWithIr(Scope(declaration.descriptor), declaration))
|
||||||
val fieldDiffersFromProperty = peek == null || peek.scope.scopeOwner != declaration.descriptor
|
|
||||||
if (fieldDiffersFromProperty)
|
|
||||||
scopeStack.push(ScopeWithIr(Scope(declaration.descriptor), declaration))
|
|
||||||
val result = visitFieldNew(declaration)
|
val result = visitFieldNew(declaration)
|
||||||
if (fieldDiffersFromProperty)
|
scopeStack.pop()
|
||||||
scopeStack.pop()
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-12
@@ -17,7 +17,6 @@
|
|||||||
package org.jetbrains.kotlin.backend.common.lower
|
package org.jetbrains.kotlin.backend.common.lower
|
||||||
|
|
||||||
import org.jetbrains.kotlin.backend.common.BackendContext
|
import org.jetbrains.kotlin.backend.common.BackendContext
|
||||||
import org.jetbrains.kotlin.backend.konan.lower.SuspendFunctionsLowering
|
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||||
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
|
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
|
||||||
@@ -32,6 +31,8 @@ import org.jetbrains.kotlin.ir.declarations.IrProperty
|
|||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFieldImpl
|
import org.jetbrains.kotlin.ir.declarations.impl.IrFieldImpl
|
||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
|
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
|
||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrPropertyImpl
|
import org.jetbrains.kotlin.ir.declarations.impl.IrPropertyImpl
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl
|
||||||
import org.jetbrains.kotlin.ir.util.createParameterDeclarations
|
import org.jetbrains.kotlin.ir.util.createParameterDeclarations
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
@@ -63,11 +64,11 @@ abstract class DescriptorWithIrBuilder<out D: DeclarationDescriptor, out B: IrDe
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun BackendContext.createPropertyGetterBuilder(startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin,
|
fun BackendContext.createPropertyGetterBuilder(startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin,
|
||||||
propertyDescriptor: PropertyDescriptor, type: KotlinType)
|
symbol: IrFieldSymbol, type: KotlinType)
|
||||||
= object: DescriptorWithIrBuilder<PropertyGetterDescriptorImpl, IrFunction>() {
|
= object: DescriptorWithIrBuilder<PropertyGetterDescriptorImpl, IrFunction>() {
|
||||||
|
|
||||||
override fun buildDescriptor() = PropertyGetterDescriptorImpl(
|
override fun buildDescriptor() = PropertyGetterDescriptorImpl(
|
||||||
/* correspondingProperty = */ propertyDescriptor,
|
/* correspondingProperty = */ symbol.descriptor,
|
||||||
/* annotations = */ Annotations.EMPTY,
|
/* annotations = */ Annotations.EMPTY,
|
||||||
/* modality = */ Modality.FINAL,
|
/* modality = */ Modality.FINAL,
|
||||||
/* visibility = */ Visibilities.PRIVATE,
|
/* visibility = */ Visibilities.PRIVATE,
|
||||||
@@ -93,18 +94,18 @@ fun BackendContext.createPropertyGetterBuilder(startOffset: Int, endOffset: Int,
|
|||||||
|
|
||||||
createParameterDeclarations()
|
createParameterDeclarations()
|
||||||
|
|
||||||
body = createIrBuilder(descriptor, startOffset, endOffset).irBlockBody {
|
body = createIrBuilder(this.symbol, startOffset, endOffset).irBlockBody {
|
||||||
+irReturn(irGetField(irThis(), propertyDescriptor))
|
+irReturn(irGetField(irGet(this@apply.dispatchReceiverParameter!!.symbol), symbol))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun BackendContext.createPropertySetterBuilder(startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin,
|
private fun BackendContext.createPropertySetterBuilder(startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin,
|
||||||
propertyDescriptor: PropertyDescriptor, type: KotlinType)
|
symbol: IrFieldSymbol, type: KotlinType)
|
||||||
= object: DescriptorWithIrBuilder<PropertySetterDescriptorImpl, IrFunction>() {
|
= object: DescriptorWithIrBuilder<PropertySetterDescriptorImpl, IrFunction>() {
|
||||||
|
|
||||||
override fun buildDescriptor() = PropertySetterDescriptorImpl(
|
override fun buildDescriptor() = PropertySetterDescriptorImpl(
|
||||||
/* correspondingProperty = */ propertyDescriptor,
|
/* correspondingProperty = */ symbol.descriptor,
|
||||||
/* annotations = */ Annotations.EMPTY,
|
/* annotations = */ Annotations.EMPTY,
|
||||||
/* modality = */ Modality.FINAL,
|
/* modality = */ Modality.FINAL,
|
||||||
/* visibility = */ Visibilities.PRIVATE,
|
/* visibility = */ Visibilities.PRIVATE,
|
||||||
@@ -146,8 +147,8 @@ private fun BackendContext.createPropertySetterBuilder(startOffset: Int, endOffs
|
|||||||
|
|
||||||
createParameterDeclarations()
|
createParameterDeclarations()
|
||||||
|
|
||||||
body = createIrBuilder(descriptor, startOffset, endOffset).irBlockBody {
|
body = createIrBuilder(this.symbol, startOffset, endOffset).irBlockBody {
|
||||||
+irSetField(irThis(), propertyDescriptor, irGet(valueParameterDescriptor))
|
+irSetField(irGet(this@apply.dispatchReceiverParameter!!.symbol), symbol, irGet(this@apply.valueParameters.single().symbol))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -158,6 +159,7 @@ fun BackendContext.createPropertyWithBackingFieldBuilder(startOffset: Int, endOf
|
|||||||
|
|
||||||
private lateinit var getterBuilder: DescriptorWithIrBuilder<PropertyGetterDescriptorImpl, IrFunction>
|
private lateinit var getterBuilder: DescriptorWithIrBuilder<PropertyGetterDescriptorImpl, IrFunction>
|
||||||
private var setterBuilder: DescriptorWithIrBuilder<PropertySetterDescriptorImpl, IrFunction>? = null
|
private var setterBuilder: DescriptorWithIrBuilder<PropertySetterDescriptorImpl, IrFunction>? = null
|
||||||
|
private lateinit var fieldSymbol: IrFieldSymbol
|
||||||
|
|
||||||
override fun buildDescriptor() = PropertyDescriptorImpl.create(
|
override fun buildDescriptor() = PropertyDescriptorImpl.create(
|
||||||
/* containingDeclaration = */ owner,
|
/* containingDeclaration = */ owner,
|
||||||
@@ -176,9 +178,10 @@ fun BackendContext.createPropertyWithBackingFieldBuilder(startOffset: Int, endOf
|
|||||||
/* isDelegated = */ false)
|
/* isDelegated = */ false)
|
||||||
|
|
||||||
override fun doInitialize() {
|
override fun doInitialize() {
|
||||||
getterBuilder = createPropertyGetterBuilder(startOffset, endOffset, origin, descriptor, type).apply { initialize() }
|
fieldSymbol = IrFieldSymbolImpl(descriptor)
|
||||||
|
getterBuilder = createPropertyGetterBuilder(startOffset, endOffset, origin, fieldSymbol, type).apply { initialize() }
|
||||||
if (isMutable)
|
if (isMutable)
|
||||||
setterBuilder = createPropertySetterBuilder(startOffset, endOffset, origin, descriptor, type).apply { initialize() }
|
setterBuilder = createPropertySetterBuilder(startOffset, endOffset, origin, fieldSymbol, type).apply { initialize() }
|
||||||
descriptor.initialize(getterBuilder.descriptor, setterBuilder?.descriptor)
|
descriptor.initialize(getterBuilder.descriptor, setterBuilder?.descriptor)
|
||||||
val receiverType: KotlinType? = null
|
val receiverType: KotlinType? = null
|
||||||
descriptor.setType(type, emptyList(), owner.thisAsReceiverParameter, receiverType)
|
descriptor.setType(type, emptyList(), owner.thisAsReceiverParameter, receiverType)
|
||||||
@@ -189,7 +192,7 @@ fun BackendContext.createPropertyWithBackingFieldBuilder(startOffset: Int, endOf
|
|||||||
startOffset = startOffset,
|
startOffset = startOffset,
|
||||||
endOffset = endOffset,
|
endOffset = endOffset,
|
||||||
origin = origin,
|
origin = origin,
|
||||||
descriptor = descriptor)
|
symbol = fieldSymbol)
|
||||||
return IrPropertyImpl(
|
return IrPropertyImpl(
|
||||||
startOffset = startOffset,
|
startOffset = startOffset,
|
||||||
endOffset = endOffset,
|
endOffset = endOffset,
|
||||||
|
|||||||
+7
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
|
|||||||
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
|
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
|
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.*
|
import org.jetbrains.kotlin.ir.expressions.impl.*
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||||
@@ -147,6 +148,12 @@ fun IrBuilderWithScope.irGetField(receiver: IrExpression, descriptor: PropertyDe
|
|||||||
fun IrBuilderWithScope.irSetField(receiver: IrExpression, descriptor: PropertyDescriptor, value: IrExpression) =
|
fun IrBuilderWithScope.irSetField(receiver: IrExpression, descriptor: PropertyDescriptor, value: IrExpression) =
|
||||||
IrSetFieldImpl(startOffset, endOffset, descriptor, receiver, value)
|
IrSetFieldImpl(startOffset, endOffset, descriptor, receiver, value)
|
||||||
|
|
||||||
|
fun IrBuilderWithScope.irGetField(receiver: IrExpression, symbol: IrFieldSymbol) =
|
||||||
|
IrGetFieldImpl(startOffset, endOffset, symbol, receiver)
|
||||||
|
|
||||||
|
fun IrBuilderWithScope.irSetField(receiver: IrExpression, symbol: IrFieldSymbol, value: IrExpression) =
|
||||||
|
IrSetFieldImpl(startOffset, endOffset, symbol, receiver, value)
|
||||||
|
|
||||||
@Deprecated("Creates unbound symbol")
|
@Deprecated("Creates unbound symbol")
|
||||||
open class IrBuildingTransformer(private val context: BackendContext) : IrElementTransformerVoid() {
|
open class IrBuildingTransformer(private val context: BackendContext) : IrElementTransformerVoid() {
|
||||||
private var currentBuilder: IrBuilderWithScope? = null
|
private var currentBuilder: IrBuilderWithScope? = null
|
||||||
|
|||||||
+43
-39
@@ -38,6 +38,8 @@ import org.jetbrains.kotlin.ir.expressions.IrFunctionReference
|
|||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol
|
||||||
import org.jetbrains.kotlin.ir.util.createParameterDeclarations
|
import org.jetbrains.kotlin.ir.util.createParameterDeclarations
|
||||||
import org.jetbrains.kotlin.ir.util.getArguments
|
import org.jetbrains.kotlin.ir.util.getArguments
|
||||||
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
||||||
@@ -93,7 +95,7 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
return IrCallImpl(
|
return IrCallImpl(
|
||||||
startOffset = expression.startOffset,
|
startOffset = expression.startOffset,
|
||||||
endOffset = expression.endOffset,
|
endOffset = expression.endOffset,
|
||||||
calleeDescriptor = loweredFunctionReference.functionReferenceConstructorDescriptor).apply {
|
symbol = loweredFunctionReference. functionReferenceConstructor.symbol).apply {
|
||||||
expression.getArguments().forEachIndexed { index, argument ->
|
expression.getArguments().forEachIndexed { index, argument ->
|
||||||
putValueArgument(index, argument.second)
|
putValueArgument(index, argument.second)
|
||||||
}
|
}
|
||||||
@@ -104,7 +106,7 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class BuiltFunctionReference(val functionReferenceClass: IrClass,
|
private class BuiltFunctionReference(val functionReferenceClass: IrClass,
|
||||||
val functionReferenceConstructorDescriptor: ClassConstructorDescriptor)
|
val functionReferenceConstructor: IrConstructor)
|
||||||
|
|
||||||
private inner class FunctionReferenceBuilder(val containingDeclaration: DeclarationDescriptor,
|
private inner class FunctionReferenceBuilder(val containingDeclaration: DeclarationDescriptor,
|
||||||
val functionReference: IrFunctionReference) {
|
val functionReference: IrFunctionReference) {
|
||||||
@@ -115,8 +117,9 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
private val unboundFunctionParameters = functionParameters - boundFunctionParameters
|
private val unboundFunctionParameters = functionParameters - boundFunctionParameters
|
||||||
|
|
||||||
private lateinit var functionReferenceClassDescriptor: ClassDescriptorImpl
|
private lateinit var functionReferenceClassDescriptor: ClassDescriptorImpl
|
||||||
private lateinit var argumentToPropertiesMap: Map<ParameterDescriptor, PropertyDescriptor>
|
private lateinit var functionReferenceClass: IrClassImpl
|
||||||
private val functionReferenceMembers = mutableListOf<IrDeclaration>()
|
private lateinit var functionReferenceThis: IrValueParameterSymbol
|
||||||
|
private lateinit var argumentToPropertiesMap: Map<ParameterDescriptor, IrFieldSymbol>
|
||||||
|
|
||||||
private fun KotlinType.replace(types: List<KotlinType>) = this.replace(types.map(::TypeProjectionImpl))
|
private fun KotlinType.replace(types: List<KotlinType>) = this.replace(types.map(::TypeProjectionImpl))
|
||||||
|
|
||||||
@@ -126,7 +129,7 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
val startOffset = functionReference.startOffset
|
val startOffset = functionReference.startOffset
|
||||||
val endOffset = functionReference.endOffset
|
val endOffset = functionReference.endOffset
|
||||||
|
|
||||||
val superTypes = mutableListOf<KotlinType>(
|
val superTypes = mutableListOf(
|
||||||
kFunctionImplClassDescriptor.defaultType.replace(listOf(functionDescriptor.returnType!!))
|
kFunctionImplClassDescriptor.defaultType.replace(listOf(functionDescriptor.returnType!!))
|
||||||
)
|
)
|
||||||
var suspendFunctionClassDescriptor: ClassDescriptor? = null
|
var suspendFunctionClassDescriptor: ClassDescriptor? = null
|
||||||
@@ -143,6 +146,15 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
/* source = */ SourceElement.NO_SOURCE,
|
/* source = */ SourceElement.NO_SOURCE,
|
||||||
/* isExternal = */ false
|
/* isExternal = */ false
|
||||||
)
|
)
|
||||||
|
functionReferenceClass = IrClassImpl(
|
||||||
|
startOffset = startOffset,
|
||||||
|
endOffset = endOffset,
|
||||||
|
origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL,
|
||||||
|
descriptor = functionReferenceClassDescriptor
|
||||||
|
)
|
||||||
|
functionReferenceClass.createParameterDeclarations()
|
||||||
|
functionReferenceThis = functionReferenceClass.thisReceiver!!.symbol
|
||||||
|
|
||||||
val constructorBuilder = createConstructorBuilder()
|
val constructorBuilder = createConstructorBuilder()
|
||||||
|
|
||||||
val typeSubstitutor = TypeSubstitutor.create(
|
val typeSubstitutor = TypeSubstitutor.create(
|
||||||
@@ -164,29 +176,18 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
functionReferenceClassDescriptor.initialize(SimpleMemberScope(contributedDescriptors), setOf(constructorBuilder.descriptor), null)
|
functionReferenceClassDescriptor.initialize(SimpleMemberScope(contributedDescriptors), setOf(constructorBuilder.descriptor), null)
|
||||||
|
|
||||||
constructorBuilder.initialize()
|
constructorBuilder.initialize()
|
||||||
functionReferenceMembers.add(constructorBuilder.ir)
|
functionReferenceClass.declarations.add(constructorBuilder.ir)
|
||||||
|
|
||||||
invokeMethodBuilder.initialize()
|
invokeMethodBuilder.initialize()
|
||||||
functionReferenceMembers.add(invokeMethodBuilder.ir)
|
functionReferenceClass.declarations.add(invokeMethodBuilder.ir)
|
||||||
|
|
||||||
val functionReferenceClass = IrClassImpl(
|
return BuiltFunctionReference(functionReferenceClass, constructorBuilder.ir)
|
||||||
startOffset = startOffset,
|
|
||||||
endOffset = endOffset,
|
|
||||||
origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL,
|
|
||||||
descriptor = functionReferenceClassDescriptor,
|
|
||||||
members = functionReferenceMembers
|
|
||||||
)
|
|
||||||
|
|
||||||
functionReferenceClass.createParameterDeclarations()
|
|
||||||
|
|
||||||
return BuiltFunctionReference(functionReferenceClass, constructorBuilder.descriptor)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createConstructorBuilder()
|
private fun createConstructorBuilder()
|
||||||
= object : DescriptorWithIrBuilder<ClassConstructorDescriptorImpl, IrConstructor>() {
|
= object : DescriptorWithIrBuilder<ClassConstructorDescriptorImpl, IrConstructor>() {
|
||||||
|
|
||||||
private val kFunctionImplConstructorDescriptor = kFunctionImplClassDescriptor.constructors.single()
|
private val kFunctionImplConstructorDescriptor = kFunctionImplClassDescriptor.constructors.single()
|
||||||
private lateinit var constructorParameters: List<ValueParameterDescriptor>
|
|
||||||
|
|
||||||
override fun buildDescriptor(): ClassConstructorDescriptorImpl {
|
override fun buildDescriptor(): ClassConstructorDescriptorImpl {
|
||||||
return ClassConstructorDescriptorImpl.create(
|
return ClassConstructorDescriptorImpl.create(
|
||||||
@@ -198,7 +199,7 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun doInitialize() {
|
override fun doInitialize() {
|
||||||
constructorParameters = boundFunctionParameters.mapIndexed { index, parameter ->
|
val constructorParameters = boundFunctionParameters.mapIndexed { index, parameter ->
|
||||||
parameter.copyAsValueParameter(descriptor, index)
|
parameter.copyAsValueParameter(descriptor, index)
|
||||||
}
|
}
|
||||||
descriptor.initialize(constructorParameters, Visibilities.PUBLIC)
|
descriptor.initialize(constructorParameters, Visibilities.PUBLIC)
|
||||||
@@ -212,34 +213,35 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
|
|
||||||
val startOffset = functionReference.startOffset
|
val startOffset = functionReference.startOffset
|
||||||
val endOffset = functionReference.endOffset
|
val endOffset = functionReference.endOffset
|
||||||
val irBuilder = context.createIrBuilder(descriptor, startOffset, endOffset)
|
|
||||||
return IrConstructorImpl(
|
return IrConstructorImpl(
|
||||||
startOffset = startOffset,
|
startOffset = startOffset,
|
||||||
endOffset = endOffset,
|
endOffset = endOffset,
|
||||||
origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL,
|
origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL,
|
||||||
descriptor = descriptor).apply {
|
descriptor = descriptor).apply {
|
||||||
|
|
||||||
|
val irBuilder = context.createIrBuilder(this.symbol, startOffset, endOffset)
|
||||||
|
|
||||||
createParameterDeclarations()
|
createParameterDeclarations()
|
||||||
|
|
||||||
body = irBuilder.irBlockBody {
|
body = irBuilder.irBlockBody {
|
||||||
+IrDelegatingConstructorCallImpl(startOffset, endOffset, kFunctionImplConstructorDescriptor).apply {
|
+IrDelegatingConstructorCallImpl(startOffset, endOffset, kFunctionImplConstructorDescriptor).apply {
|
||||||
val name = IrConstImpl<String>(startOffset, endOffset, context.builtIns.stringType,
|
val name = IrConstImpl(startOffset, endOffset, context.builtIns.stringType,
|
||||||
IrConstKind.String, functionDescriptor.name.asString())
|
IrConstKind.String, functionDescriptor.name.asString())
|
||||||
putValueArgument(0, name)
|
putValueArgument(0, name)
|
||||||
val fqName = IrConstImpl<String>(startOffset, endOffset, context.builtIns.stringType, IrConstKind.String,
|
val fqName = IrConstImpl(startOffset, endOffset, context.builtIns.stringType, IrConstKind.String,
|
||||||
functionDescriptor.fullName)
|
functionDescriptor.fullName)
|
||||||
putValueArgument(1, fqName)
|
putValueArgument(1, fqName)
|
||||||
val bound = IrConstImpl.boolean(startOffset, endOffset, context.builtIns.booleanType,
|
val bound = IrConstImpl.boolean(startOffset, endOffset, context.builtIns.booleanType,
|
||||||
boundFunctionParameters.isNotEmpty())
|
boundFunctionParameters.isNotEmpty())
|
||||||
putValueArgument(2, bound)
|
putValueArgument(2, bound)
|
||||||
val needReceiver = boundFunctionParameters.singleOrNull() is ReceiverParameterDescriptor
|
val needReceiver = boundFunctionParameters.singleOrNull() is ReceiverParameterDescriptor
|
||||||
val receiver = if (needReceiver) irGet(constructorParameters.single()) else irNull()
|
val receiver = if (needReceiver) irGet(valueParameters.single().symbol) else irNull()
|
||||||
putValueArgument(3, receiver)
|
putValueArgument(3, receiver)
|
||||||
}
|
}
|
||||||
+IrInstanceInitializerCallImpl(startOffset, endOffset, functionReferenceClassDescriptor)
|
+IrInstanceInitializerCallImpl(startOffset, endOffset, functionReferenceClass.symbol)
|
||||||
// Save all arguments to fields.
|
// Save all arguments to fields.
|
||||||
boundFunctionParameters.forEachIndexed { index, parameter ->
|
boundFunctionParameters.forEachIndexed { index, parameter ->
|
||||||
+irSetField(irThis(), argumentToPropertiesMap[parameter]!!, irGet(constructorParameters[index]))
|
+irSetField(irGet(functionReferenceThis), argumentToPropertiesMap[parameter]!!, irGet(valueParameters[index].symbol))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -288,25 +290,27 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
val startOffset = functionReference.startOffset
|
val startOffset = functionReference.startOffset
|
||||||
val endOffset = functionReference.endOffset
|
val endOffset = functionReference.endOffset
|
||||||
val ourDescriptor = this.descriptor
|
val ourDescriptor = this.descriptor
|
||||||
val irBuilder = context.createIrBuilder(this.descriptor, startOffset, endOffset)
|
|
||||||
return IrFunctionImpl(
|
return IrFunctionImpl(
|
||||||
startOffset = startOffset,
|
startOffset = startOffset,
|
||||||
endOffset = endOffset,
|
endOffset = endOffset,
|
||||||
origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL,
|
origin = DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL,
|
||||||
descriptor = this.descriptor).apply {
|
descriptor = this.descriptor).apply {
|
||||||
|
|
||||||
|
val function = this
|
||||||
|
val irBuilder = context.createIrBuilder(function.symbol, startOffset, endOffset)
|
||||||
|
|
||||||
createParameterDeclarations()
|
createParameterDeclarations()
|
||||||
|
|
||||||
body = irBuilder.irBlockBody(startOffset, endOffset) {
|
body = irBuilder.irBlockBody(startOffset, endOffset) {
|
||||||
+irReturn(
|
+irReturn(
|
||||||
irCall(functionReference.descriptor).apply {
|
irCall(functionReference.symbol).apply {
|
||||||
var unboundIndex = 0
|
var unboundIndex = 0
|
||||||
val unboundArgsSet = unboundFunctionParameters.toSet()
|
val unboundArgsSet = unboundFunctionParameters.toSet()
|
||||||
functionParameters.forEach {
|
functionParameters.forEach {
|
||||||
val argument = if (unboundArgsSet.contains(it))
|
val argument = if (unboundArgsSet.contains(it))
|
||||||
irGet(ourDescriptor.valueParameters[unboundIndex++])
|
irGet(valueParameters[unboundIndex++].symbol)
|
||||||
else
|
else
|
||||||
irGet(irThis(), argumentToPropertiesMap[it]!!)
|
irGetField(irGet(functionReferenceThis), argumentToPropertiesMap[it]!!)
|
||||||
when (it) {
|
when (it) {
|
||||||
functionDescriptor.dispatchReceiverParameter -> dispatchReceiver = argument
|
functionDescriptor.dispatchReceiverParameter -> dispatchReceiver = argument
|
||||||
functionDescriptor.extensionReceiverParameter -> extensionReceiver = argument
|
functionDescriptor.extensionReceiverParameter -> extensionReceiver = argument
|
||||||
@@ -322,7 +326,7 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildPropertyWithBackingField(name: Name, type: KotlinType, isMutable: Boolean): PropertyDescriptor {
|
private fun buildPropertyWithBackingField(name: Name, type: KotlinType, isMutable: Boolean): IrFieldSymbol {
|
||||||
val propertyBuilder = context.createPropertyWithBackingFieldBuilder(
|
val propertyBuilder = context.createPropertyWithBackingFieldBuilder(
|
||||||
startOffset = functionReference.startOffset,
|
startOffset = functionReference.startOffset,
|
||||||
endOffset = functionReference.endOffset,
|
endOffset = functionReference.endOffset,
|
||||||
@@ -334,8 +338,8 @@ internal class CallableReferenceLowering(val context: Context): DeclarationConta
|
|||||||
initialize()
|
initialize()
|
||||||
}
|
}
|
||||||
|
|
||||||
functionReferenceMembers.add(propertyBuilder.ir)
|
functionReferenceClass.declarations.add(propertyBuilder.ir)
|
||||||
return propertyBuilder.descriptor
|
return propertyBuilder.ir.backingField!!.symbol
|
||||||
}
|
}
|
||||||
|
|
||||||
private object DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL :
|
private object DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL :
|
||||||
|
|||||||
+9
@@ -17,11 +17,16 @@
|
|||||||
package org.jetbrains.kotlin.ir.builders
|
package org.jetbrains.kotlin.ir.builders
|
||||||
|
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.IrConstructor
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrCall
|
import org.jetbrains.kotlin.ir.expressions.IrCall
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrLoop
|
import org.jetbrains.kotlin.ir.expressions.IrLoop
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
|
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.*
|
import org.jetbrains.kotlin.ir.expressions.impl.*
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.TypeProjectionImpl
|
import org.jetbrains.kotlin.types.TypeProjectionImpl
|
||||||
import org.jetbrains.kotlin.types.TypeSubstitutor
|
import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||||
@@ -75,6 +80,10 @@ fun IrBuilderWithScope.irCall(descriptor: FunctionDescriptor): IrCallImpl {
|
|||||||
return IrCallImpl(this.startOffset, this.endOffset, descriptor)
|
return IrCallImpl(this.startOffset, this.endOffset, descriptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun IrBuilderWithScope.irCall(symbol: IrFunctionSymbol): IrCallImpl {
|
||||||
|
return IrCallImpl(this.startOffset, this.endOffset, symbol)
|
||||||
|
}
|
||||||
|
|
||||||
@Deprecated("Creates unbound symbol")
|
@Deprecated("Creates unbound symbol")
|
||||||
fun IrBuilderWithScope.irCall(
|
fun IrBuilderWithScope.irCall(
|
||||||
callee: FunctionDescriptor,
|
callee: FunctionDescriptor,
|
||||||
|
|||||||
Reference in New Issue
Block a user