[FIR2IR] Get rid of all usages of IrSymbol.owner from AdapterGenerator

^KT-60924
This commit is contained in:
Dmitriy Novozhilov
2023-08-18 16:59:56 +03:00
committed by Space Team
parent 39195146a9
commit 5355f0f705
6 changed files with 45 additions and 121 deletions
@@ -849,3 +849,9 @@ internal fun FirVariable.irTypeForPotentiallyComponentCall(predefinedType: IrTyp
}
return typeRef.toIrType(typeConverter)
}
internal val FirValueParameter.varargElementType: ConeKotlinType?
get() {
if (!isVararg) return null
return returnTypeRef.coneType.arrayElementType()
}
@@ -1450,9 +1450,7 @@ class Fir2IrDeclarationStorage(
isAssignable = false,
symbol = IrValueParameterSymbolImpl(),
index = index,
varargElementType =
if (!valueParameter.isVararg) null
else valueParameter.returnTypeRef.coneType.arrayElementType()?.toIrType(typeOrigin),
varargElementType = valueParameter.varargElementType?.toIrType(typeOrigin),
isCrossinline = valueParameter.isCrossinline,
isNoinline = valueParameter.isNoinline,
isHidden = false,
@@ -332,10 +332,25 @@ class Fir2IrTypeConverter(
fun FirTypeRef.toIrType(
typeConverter: Fir2IrTypeConverter,
typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT
): IrType =
with(typeConverter) {
): IrType {
return with(typeConverter) {
toIrType(typeOrigin)
}
}
context(Fir2IrComponents)
fun FirTypeRef.toIrType(typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT): IrType {
return with(typeConverter) {
toIrType(typeOrigin)
}
}
context(Fir2IrComponents)
fun ConeKotlinType.toIrType(typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT): IrType {
return with(typeConverter) {
toIrType(typeOrigin)
}
}
fun ConeKotlinType.toIrType(
typeConverter: Fir2IrTypeConverter,
@@ -36,7 +36,6 @@ import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbolInternals
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.*
@@ -44,6 +43,7 @@ import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.types.AbstractTypeChecker
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.model.TypeVariance
@@ -138,7 +138,6 @@ internal class AdapterGenerator(
}
}
@OptIn(IrSymbolInternals::class)
internal fun generateAdaptedCallableReference(
callableReferenceAccess: FirCallableReferenceAccess,
explicitReceiverExpression: IrExpression?,
@@ -146,14 +145,13 @@ internal class AdapterGenerator(
type: IrSimpleType
): IrExpression {
val firAdaptee = callableReferenceAccess.toResolvedCallableReference()?.resolvedSymbol?.fir as? FirFunction
val adaptee = adapteeSymbol.owner
val expectedReturnType = type.arguments.last().typeOrNull
return callableReferenceAccess.convertWithOffsets { startOffset, endOffset ->
val boundDispatchReceiver = callableReferenceAccess.findBoundReceiver(explicitReceiverExpression, isDispatch = true)
val boundExtensionReceiver = callableReferenceAccess.findBoundReceiver(explicitReceiverExpression, isDispatch = false)
val irAdapterFunction = createAdapterFunctionForCallableReference(
callableReferenceAccess, startOffset, endOffset, firAdaptee!!, adaptee, type, boundDispatchReceiver, boundExtensionReceiver
callableReferenceAccess, startOffset, endOffset, firAdaptee!!, type, boundDispatchReceiver, boundExtensionReceiver
)
val irCall = createAdapteeCallForCallableReference(
callableReferenceAccess, firAdaptee, adapteeSymbol, irAdapterFunction, type, boundDispatchReceiver, boundExtensionReceiver
@@ -176,7 +174,7 @@ internal class AdapterGenerator(
irAdapterFunction.valueParameters.size, null, IrStatementOrigin.ADAPTED_FUNCTION_REFERENCE
).apply {
extensionReceiver = boundDispatchReceiver ?: boundExtensionReceiver
reflectionTarget = adaptee.symbol
reflectionTarget = adapteeSymbol
}
IrBlockImpl(startOffset, endOffset, type, IrStatementOrigin.ADAPTED_FUNCTION_REFERENCE).apply {
statements.add(irAdapterFunction)
@@ -203,7 +201,6 @@ internal class AdapterGenerator(
startOffset: Int,
endOffset: Int,
firAdaptee: FirFunction,
adaptee: IrFunction,
type: IrSimpleType,
boundDispatchReceiver: IrExpression?,
boundExtensionReceiver: IrExpression?
@@ -211,11 +208,15 @@ internal class AdapterGenerator(
val returnType = type.arguments.last().typeOrNull!!
val parameterTypes = type.arguments.dropLast(1).map { it.typeOrNull!! }
val firMemberAdaptee = firAdaptee as FirMemberDeclaration
val name = when (firAdaptee) {
is FirConstructor -> SpecialNames.INIT
else -> firAdaptee.symbol.name
}
return irFactory.createSimpleFunction(
startOffset = startOffset,
endOffset = endOffset,
origin = IrDeclarationOrigin.ADAPTER_FOR_CALLABLE_REFERENCE,
name = adaptee.name,
name = name,
visibility = DescriptorVisibilities.LOCAL,
isInline = firMemberAdaptee.isInline,
isExpect = false,
@@ -288,7 +289,6 @@ internal class AdapterGenerator(
private fun IrValueDeclaration.toIrGetValue(startOffset: Int, endOffset: Int): IrGetValue =
IrGetValueImpl(startOffset, endOffset, this.type, this.symbol)
@OptIn(IrSymbolInternals::class)
private fun createAdapteeCallForCallableReference(
callableReferenceAccess: FirCallableReferenceAccess,
firAdaptee: FirFunction,
@@ -297,11 +297,8 @@ internal class AdapterGenerator(
adaptedType: IrSimpleType,
boundDispatchReceiver: IrExpression?,
boundExtensionReceiver: IrExpression?
): IrExpression {
val adapteeFunction = adapteeSymbol.owner
val startOffset = adapteeFunction.startOffset
val endOffset = adapteeFunction.endOffset
val type = adapteeFunction.returnType
): IrExpression = callableReferenceAccess.convertWithOffsets { startOffset, endOffset ->
val type = firAdaptee.returnTypeRef.toIrType(typeConverter)
val irCall = when (adapteeSymbol) {
is IrConstructorSymbol ->
IrConstructorCallImpl.fromSymbolOwner(startOffset, endOffset, type, adapteeSymbol)
@@ -312,7 +309,7 @@ internal class AdapterGenerator(
type,
adapteeSymbol,
callableReferenceAccess.typeArguments.size,
adapteeFunction.valueParameters.size,
firAdaptee.valueParameters.size + firAdaptee.contextReceivers.size,
origin = null,
superQualifierSymbol = null
)
@@ -337,7 +334,7 @@ internal class AdapterGenerator(
val adaptedReceiverValue = IrGetValueImpl(
startOffset, endOffset, adaptedReceiverParameter.type, adaptedReceiverParameter.symbol
)
if (adapteeFunction.extensionReceiverParameter != null) {
if (firAdaptee.receiverParameter != null) {
irCall.extensionReceiver = adaptedReceiverValue
} else {
irCall.dispatchReceiver = adaptedReceiverValue
@@ -353,16 +350,17 @@ internal class AdapterGenerator(
return adapterFunction.valueParameters[parameterIndex + parameterShift].toIrGetValue(startOffset, endOffset)
}
adapteeFunction.valueParameters.zip(firAdaptee.valueParameters).forEachIndexed { index, (valueParameter, firParameter) ->
when (val mappedArgument = mappedArguments?.get(firParameter)) {
firAdaptee.valueParameters.forEachIndexed { index, valueParameter ->
val varargElementType = valueParameter.varargElementType?.toIrType()
val parameterType = valueParameter.returnTypeRef.toIrType(typeConverter)
when (val mappedArgument = mappedArguments?.get(valueParameter)) {
is ResolvedCallArgument.VarargArgument -> {
val valueArgument = if (mappedArgument.arguments.isEmpty()) {
null
} else {
val parameterType = valueParameter.type
val reifiedVarargElementType: IrType
val reifiedVarargType: IrType
if (valueParameter.varargElementType?.isTypeParameter() == true &&
if (varargElementType?.isTypeParameter() == true &&
parameterType is IrSimpleType &&
!parameterType.isPrimitiveArray()
) {
@@ -375,7 +373,7 @@ internal class AdapterGenerator(
parameterType.abbreviation
)
} else {
reifiedVarargElementType = valueParameter.varargElementType!!
reifiedVarargElementType = varargElementType!!
reifiedVarargType = parameterType
}
@@ -402,7 +400,7 @@ internal class AdapterGenerator(
irCall.putValueArgument(
index, IrVarargImpl(
startOffset, endOffset,
valueParameter.type, valueParameter.varargElementType!!,
parameterType, varargElementType!!,
listOf(IrSpreadElementImpl(startOffset, endOffset, irValueArgument))
)
)
@@ -416,7 +414,7 @@ internal class AdapterGenerator(
}
with(callGenerator) {
return irCall.applyTypeArguments(callableReferenceAccess)
irCall.applyTypeArguments(callableReferenceAccess)
}
}
@@ -1,94 +0,0 @@
FILE fqName:<root> fileName:/kt46069.kt
CLASS CLASS name:ObjectAssert modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.ObjectAssert<ACTUAL of <root>.ObjectAssert>
TYPE_PARAMETER name:ACTUAL index:0 variance: superTypes:[kotlin.Any?] reified:false
CONSTRUCTOR visibility:public <> () returnType:<root>.ObjectAssert<ACTUAL of <root>.ObjectAssert> [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:ObjectAssert modality:FINAL visibility:public superTypes:[kotlin.Any]'
FUN name:describedAs visibility:public modality:FINAL <> ($this:<root>.ObjectAssert<ACTUAL of <root>.ObjectAssert>, description:kotlin.String?, args:kotlin.Array<out kotlin.Any?>) returnType:<root>.ObjectAssert<ACTUAL of <root>.ObjectAssert>?
$this: VALUE_PARAMETER name:<this> type:<root>.ObjectAssert<ACTUAL of <root>.ObjectAssert>
VALUE_PARAMETER name:description index:0 type:kotlin.String?
VALUE_PARAMETER name:args index:1 type:kotlin.Array<out kotlin.Any?> varargElementType:kotlin.Any? [vararg]
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun describedAs (description: kotlin.String?, vararg args: kotlin.Any?): <root>.ObjectAssert<ACTUAL of <root>.ObjectAssert>? declared in <root>.ObjectAssert'
CONST Null type=kotlin.Nothing? value=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS OBJECT name:Assertions modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Assertions
CONSTRUCTOR visibility:private <> () returnType:<root>.Assertions [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Assertions modality:FINAL visibility:public superTypes:[kotlin.Any]'
FUN name:assertThat visibility:public modality:FINAL <S> ($this:<root>.Assertions, actual:S of <root>.Assertions.assertThat) returnType:<root>.ObjectAssert<S of <root>.Assertions.assertThat>
TYPE_PARAMETER name:S index:0 variance: superTypes:[kotlin.Any?] reified:false
$this: VALUE_PARAMETER name:<this> type:<root>.Assertions
VALUE_PARAMETER name:actual index:0 type:S of <root>.Assertions.assertThat
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun assertThat <S> (actual: S of <root>.Assertions.assertThat): <root>.ObjectAssert<S of <root>.Assertions.assertThat> declared in <root>.Assertions'
CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.ObjectAssert' type=<root>.ObjectAssert<S of <root>.Assertions.assertThat> origin=null
<class: ACTUAL>: S of <root>.Assertions.assertThat
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:assertNotNull visibility:public modality:FINAL <T> ($receiver:T of <root>.assertNotNull?, description:kotlin.String?) returnType:kotlin.Unit
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any] reified:false
$receiver: VALUE_PARAMETER name:<this> type:T of <root>.assertNotNull?
VALUE_PARAMETER name:description index:0 type:kotlin.String?
EXPRESSION_BODY
CONST Null type=kotlin.Nothing? value=null
BLOCK_BODY
VAR name:assert type:<root>.ObjectAssert<T of <root>.assertNotNull?> [val]
CALL 'public final fun assertThat <S> (actual: S of <root>.Assertions.assertThat): <root>.ObjectAssert<S of <root>.Assertions.assertThat> declared in <root>.Assertions' type=<root>.ObjectAssert<T of <root>.assertNotNull?> origin=null
<S>: T of <root>.assertNotNull?
$this: GET_OBJECT 'CLASS OBJECT name:Assertions modality:FINAL visibility:public superTypes:[kotlin.Any]' type=<root>.Assertions
actual: GET_VAR '<this>: T of <root>.assertNotNull? declared in <root>.assertNotNull' type=T of <root>.assertNotNull? origin=null
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
BLOCK type=<root>.ObjectAssert<T of <root>.assertNotNull?>? origin=SAFE_CALL
VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:kotlin.String? [val]
GET_VAR 'description: kotlin.String? declared in <root>.assertNotNull' type=kotlin.String? origin=null
WHEN type=<root>.ObjectAssert<T of <root>.assertNotNull?>? origin=null
BRANCH
if: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ
arg0: GET_VAR 'val tmp_0: kotlin.String? declared in <root>.assertNotNull' type=kotlin.String? origin=null
arg1: CONST Null type=kotlin.Nothing? value=null
then: CONST Null type=kotlin.Nothing? value=null
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: CALL 'public final fun let <T, R> (block: kotlin.Function1<T of kotlin.let, R of kotlin.let>): R of kotlin.let declared in kotlin' type=<root>.ObjectAssert<T of <root>.assertNotNull?>? origin=null
<T>: kotlin.String
<R>: <root>.ObjectAssert<T of <root>.assertNotNull?>?
$receiver: GET_VAR 'val tmp_0: kotlin.String? declared in <root>.assertNotNull' type=kotlin.String? origin=null
block: BLOCK type=kotlin.Function1<kotlin.String?, <root>.ObjectAssert<T of <root>.assertNotNull?>?> origin=ADAPTED_FUNCTION_REFERENCE
FUN ADAPTER_FOR_CALLABLE_REFERENCE name:describedAs visibility:local modality:FINAL <> ($receiver:<root>.ObjectAssert<T of <root>.assertNotNull?>, p0:kotlin.String?) returnType:<root>.ObjectAssert<T of <root>.assertNotNull?>?
$receiver: VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:receiver type:<root>.ObjectAssert<T of <root>.assertNotNull?>
VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:kotlin.String?
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun describedAs (p0: kotlin.String?): <root>.ObjectAssert<T of <root>.assertNotNull?>? declared in <root>.assertNotNull'
CALL 'public final fun describedAs (description: kotlin.String?, vararg args: kotlin.Any?): <root>.ObjectAssert<ACTUAL of <root>.ObjectAssert>? declared in <root>.ObjectAssert' type=<root>.ObjectAssert<ACTUAL of <root>.ObjectAssert>? origin=null
$this: GET_VAR 'receiver: <root>.ObjectAssert<T of <root>.assertNotNull?> declared in <root>.assertNotNull.describedAs' type=<root>.ObjectAssert<T of <root>.assertNotNull?> origin=ADAPTED_FUNCTION_REFERENCE
description: GET_VAR 'p0: kotlin.String? declared in <root>.assertNotNull.describedAs' type=kotlin.String? origin=null
FUNCTION_REFERENCE 'local final fun describedAs (p0: kotlin.String?): <root>.ObjectAssert<T of <root>.assertNotNull?>? declared in <root>.assertNotNull' type=kotlin.Function1<kotlin.String?, <root>.ObjectAssert<T of <root>.assertNotNull?>?> origin=ADAPTED_FUNCTION_REFERENCE reflectionTarget=public final fun describedAs (description: kotlin.String?, vararg args: kotlin.Any?): <root>.ObjectAssert<ACTUAL of <root>.ObjectAssert>? declared in <root>.ObjectAssert
$receiver: GET_VAR 'val assert: <root>.ObjectAssert<T of <root>.assertNotNull?> declared in <root>.assertNotNull' type=<root>.ObjectAssert<T of <root>.assertNotNull?> origin=null
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// SKIP_KT_DUMP
class ObjectAssert<ACTUAL> {
fun describedAs(description: String?, vararg args: Any?): ObjectAssert<ACTUAL>? = null