[FIR2IR] Reuse declareThisReceiverParameter in DataClassMembersGenerator
This commit is contained in:
@@ -27,7 +27,9 @@ import org.jetbrains.kotlin.fir.symbols.AccessorSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrProperty
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
|
||||
import org.jetbrains.kotlin.ir.descriptors.WrappedReceiverParameterDescriptor
|
||||
import org.jetbrains.kotlin.ir.expressions.IrConst
|
||||
import org.jetbrains.kotlin.ir.expressions.IrConstKind
|
||||
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
|
||||
@@ -36,6 +38,7 @@ import org.jetbrains.kotlin.ir.symbols.*
|
||||
import org.jetbrains.kotlin.ir.types.IrErrorType
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrErrorTypeImpl
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffsetSkippingComments
|
||||
@@ -282,4 +285,27 @@ internal fun FirReference.statementOrigin(): IrStatementOrigin? {
|
||||
}
|
||||
|
||||
fun FirClass<*>.getPrimaryConstructorIfAny(): FirConstructor? =
|
||||
declarations.filterIsInstance<FirConstructor>().firstOrNull()?.takeIf { it.isPrimary }
|
||||
declarations.filterIsInstance<FirConstructor>().firstOrNull()?.takeIf { it.isPrimary }
|
||||
|
||||
internal fun IrDeclarationParent.declareThisReceiverParameter(
|
||||
symbolTable: SymbolTable,
|
||||
thisType: IrType,
|
||||
thisOrigin: IrDeclarationOrigin,
|
||||
startOffset: Int = this.startOffset,
|
||||
endOffset: Int = this.endOffset
|
||||
): IrValueParameter {
|
||||
val receiverDescriptor = WrappedReceiverParameterDescriptor()
|
||||
return symbolTable.declareValueParameter(
|
||||
startOffset, endOffset, thisOrigin, receiverDescriptor, thisType
|
||||
) { symbol ->
|
||||
IrValueParameterImpl(
|
||||
startOffset, endOffset, thisOrigin, symbol,
|
||||
Name.special("<this>"), -1, thisType,
|
||||
varargElementType = null, isCrossinline = false, isNoinline = false
|
||||
).apply {
|
||||
this.parent = this@declareThisReceiverParameter
|
||||
receiverDescriptor.bind(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-23
@@ -65,35 +65,13 @@ class Fir2IrClassifierStorage(
|
||||
}
|
||||
}
|
||||
|
||||
internal fun IrDeclaration.declareThisReceiverParameter(
|
||||
parent: IrDeclarationParent,
|
||||
thisType: IrType,
|
||||
thisOrigin: IrDeclarationOrigin,
|
||||
startOffset: Int = this.startOffset,
|
||||
endOffset: Int = this.endOffset
|
||||
): IrValueParameter {
|
||||
val receiverDescriptor = WrappedReceiverParameterDescriptor()
|
||||
return symbolTable.declareValueParameter(
|
||||
startOffset, endOffset, thisOrigin, receiverDescriptor, thisType
|
||||
) { symbol ->
|
||||
IrValueParameterImpl(
|
||||
startOffset, endOffset, thisOrigin, symbol,
|
||||
Name.special("<this>"), -1, thisType,
|
||||
varargElementType = null, isCrossinline = false, isNoinline = false
|
||||
).apply {
|
||||
this.parent = parent
|
||||
receiverDescriptor.bind(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun IrClass.setThisReceiver() {
|
||||
symbolTable.enterScope(descriptor)
|
||||
val typeArguments = this.typeParameters.map {
|
||||
IrSimpleTypeImpl(it.symbol, false, emptyList(), emptyList())
|
||||
}
|
||||
thisReceiver = declareThisReceiverParameter(
|
||||
parent = this,
|
||||
symbolTable,
|
||||
thisType = IrSimpleTypeImpl(symbol, false, typeArguments, emptyList()),
|
||||
thisOrigin = IrDeclarationOrigin.INSTANCE_RECEIVER
|
||||
)
|
||||
|
||||
+3
-3
@@ -339,7 +339,7 @@ class Fir2IrDeclarationStorage(
|
||||
if (receiverTypeRef != null) {
|
||||
extensionReceiverParameter = receiverTypeRef.convertWithOffsets { startOffset, endOffset ->
|
||||
declareThisReceiverParameter(
|
||||
parent,
|
||||
symbolTable,
|
||||
thisType = receiverTypeRef.toIrType(typeContext),
|
||||
thisOrigin = thisOrigin,
|
||||
startOffset = startOffset,
|
||||
@@ -349,7 +349,7 @@ class Fir2IrDeclarationStorage(
|
||||
}
|
||||
if (function !is FirAnonymousFunction && containingClass != null && !isStatic) {
|
||||
dispatchReceiverParameter = declareThisReceiverParameter(
|
||||
parent,
|
||||
symbolTable,
|
||||
thisType = containingClass.thisReceiver!!.type,
|
||||
thisOrigin = thisOrigin
|
||||
)
|
||||
@@ -358,7 +358,7 @@ class Fir2IrDeclarationStorage(
|
||||
// Set dispatch receiver parameter for inner class's constructor.
|
||||
if (containingClass?.isInner == true) {
|
||||
dispatchReceiverParameter = declareThisReceiverParameter(
|
||||
parent,
|
||||
symbolTable,
|
||||
thisType = containingClass.thisReceiver!!.type,
|
||||
thisOrigin = thisOrigin
|
||||
)
|
||||
|
||||
+7
-20
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.fir.backend.Fir2IrComponents
|
||||
import org.jetbrains.kotlin.fir.backend.declareThisReceiverParameter
|
||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||
import org.jetbrains.kotlin.ir.builders.IrGeneratorContextBase
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
@@ -59,27 +60,13 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) {
|
||||
}
|
||||
|
||||
fun generateDispatchReceiverParameter(irFunction: IrFunction, valueParameterDescriptor: WrappedValueParameterDescriptor) =
|
||||
components.symbolTable.declareValueParameter(
|
||||
UNDEFINED_OFFSET,
|
||||
UNDEFINED_OFFSET,
|
||||
irFunction.declareThisReceiverParameter(
|
||||
components.symbolTable,
|
||||
irClass.defaultType,
|
||||
origin,
|
||||
valueParameterDescriptor,
|
||||
components.irBuiltIns.anyNType
|
||||
) { symbol ->
|
||||
IrValueParameterImpl(
|
||||
UNDEFINED_OFFSET,
|
||||
UNDEFINED_OFFSET,
|
||||
origin,
|
||||
symbol,
|
||||
Name.special("<this>"),
|
||||
-1,
|
||||
irClass.defaultType,
|
||||
null,
|
||||
isCrossinline = false,
|
||||
isNoinline = false
|
||||
)
|
||||
}.apply {
|
||||
parent = irFunction
|
||||
UNDEFINED_OFFSET,
|
||||
UNDEFINED_OFFSET
|
||||
).apply {
|
||||
valueParameterDescriptor.bind(this)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user