From 5355f0f7055e845108c26e3fd2d526459222d826 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 18 Aug 2023 16:59:56 +0300 Subject: [PATCH] [FIR2IR] Get rid of all usages of IrSymbol.owner from AdapterGenerator ^KT-60924 --- .../kotlin/fir/backend/ConversionUtils.kt | 6 ++ .../fir/backend/Fir2IrDeclarationStorage.kt | 4 +- .../kotlin/fir/backend/Fir2IrTypeConverter.kt | 19 +++- .../backend/generators/AdapterGenerator.kt | 42 ++++----- .../callableReferences/kt46069.fir.ir.txt | 94 ------------------- .../expressions/callableReferences/kt46069.kt | 1 + 6 files changed, 45 insertions(+), 121 deletions(-) delete mode 100644 compiler/testData/ir/irText/expressions/callableReferences/kt46069.fir.ir.txt diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt index 2f18517ec97..87e1c6378c2 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt @@ -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() + } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index 841a9dc1451..f72be779ffa 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -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, diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrTypeConverter.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrTypeConverter.kt index 5f6f4d7010a..144b437ccaf 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrTypeConverter.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrTypeConverter.kt @@ -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, diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt index cf3ee9d9178..d7e20ade57b 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt @@ -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) } } diff --git a/compiler/testData/ir/irText/expressions/callableReferences/kt46069.fir.ir.txt b/compiler/testData/ir/irText/expressions/callableReferences/kt46069.fir.ir.txt deleted file mode 100644 index 3de5b49a739..00000000000 --- a/compiler/testData/ir/irText/expressions/callableReferences/kt46069.fir.ir.txt +++ /dev/null @@ -1,94 +0,0 @@ -FILE fqName: fileName:/kt46069.kt - CLASS CLASS name:ObjectAssert modality:FINAL visibility:public superTypes:[kotlin.Any] - $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.ObjectAssert.ObjectAssert> - TYPE_PARAMETER name:ACTUAL index:0 variance: superTypes:[kotlin.Any?] reified:false - CONSTRUCTOR visibility:public <> () returnType:.ObjectAssert.ObjectAssert> [primary] - BLOCK_BODY - DELEGATING_CONSTRUCTOR_CALL 'public constructor () 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:.ObjectAssert.ObjectAssert>, description:kotlin.String?, args:kotlin.Array) returnType:.ObjectAssert.ObjectAssert>? - $this: VALUE_PARAMETER name: type:.ObjectAssert.ObjectAssert> - VALUE_PARAMETER name:description index:0 type:kotlin.String? - VALUE_PARAMETER name:args index:1 type:kotlin.Array varargElementType:kotlin.Any? [vararg] - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun describedAs (description: kotlin.String?, vararg args: kotlin.Any?): .ObjectAssert.ObjectAssert>? declared in .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: 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: 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: type:kotlin.Any - CLASS OBJECT name:Assertions modality:FINAL visibility:public superTypes:[kotlin.Any] - $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Assertions - CONSTRUCTOR visibility:private <> () returnType:.Assertions [primary] - BLOCK_BODY - DELEGATING_CONSTRUCTOR_CALL 'public constructor () 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 ($this:.Assertions, actual:S of .Assertions.assertThat) returnType:.ObjectAssert.Assertions.assertThat> - TYPE_PARAMETER name:S index:0 variance: superTypes:[kotlin.Any?] reified:false - $this: VALUE_PARAMETER name: type:.Assertions - VALUE_PARAMETER name:actual index:0 type:S of .Assertions.assertThat - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun assertThat (actual: S of .Assertions.assertThat): .ObjectAssert.Assertions.assertThat> declared in .Assertions' - CONSTRUCTOR_CALL 'public constructor () declared in .ObjectAssert' type=.ObjectAssert.Assertions.assertThat> origin=null - : S of .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: 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: 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: type:kotlin.Any - FUN name:assertNotNull visibility:public modality:FINAL ($receiver:T of .assertNotNull?, description:kotlin.String?) returnType:kotlin.Unit - TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any] reified:false - $receiver: VALUE_PARAMETER name: type:T of .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:.ObjectAssert.assertNotNull?> [val] - CALL 'public final fun assertThat (actual: S of .Assertions.assertThat): .ObjectAssert.Assertions.assertThat> declared in .Assertions' type=.ObjectAssert.assertNotNull?> origin=null - : T of .assertNotNull? - $this: GET_OBJECT 'CLASS OBJECT name:Assertions modality:FINAL visibility:public superTypes:[kotlin.Any]' type=.Assertions - actual: GET_VAR ': T of .assertNotNull? declared in .assertNotNull' type=T of .assertNotNull? origin=null - TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit - BLOCK type=.ObjectAssert.assertNotNull?>? origin=SAFE_CALL - VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:kotlin.String? [val] - GET_VAR 'description: kotlin.String? declared in .assertNotNull' type=kotlin.String? origin=null - WHEN type=.ObjectAssert.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 .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 (block: kotlin.Function1): R of kotlin.let declared in kotlin' type=.ObjectAssert.assertNotNull?>? origin=null - : kotlin.String - : .ObjectAssert.assertNotNull?>? - $receiver: GET_VAR 'val tmp_0: kotlin.String? declared in .assertNotNull' type=kotlin.String? origin=null - block: BLOCK type=kotlin.Function1.ObjectAssert.assertNotNull?>?> origin=ADAPTED_FUNCTION_REFERENCE - FUN ADAPTER_FOR_CALLABLE_REFERENCE name:describedAs visibility:local modality:FINAL <> ($receiver:.ObjectAssert.assertNotNull?>, p0:kotlin.String?) returnType:.ObjectAssert.assertNotNull?>? - $receiver: VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:receiver type:.ObjectAssert.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?): .ObjectAssert.assertNotNull?>? declared in .assertNotNull' - CALL 'public final fun describedAs (description: kotlin.String?, vararg args: kotlin.Any?): .ObjectAssert.ObjectAssert>? declared in .ObjectAssert' type=.ObjectAssert.ObjectAssert>? origin=null - $this: GET_VAR 'receiver: .ObjectAssert.assertNotNull?> declared in .assertNotNull.describedAs' type=.ObjectAssert.assertNotNull?> origin=ADAPTED_FUNCTION_REFERENCE - description: GET_VAR 'p0: kotlin.String? declared in .assertNotNull.describedAs' type=kotlin.String? origin=null - FUNCTION_REFERENCE 'local final fun describedAs (p0: kotlin.String?): .ObjectAssert.assertNotNull?>? declared in .assertNotNull' type=kotlin.Function1.ObjectAssert.assertNotNull?>?> origin=ADAPTED_FUNCTION_REFERENCE reflectionTarget=public final fun describedAs (description: kotlin.String?, vararg args: kotlin.Any?): .ObjectAssert.ObjectAssert>? declared in .ObjectAssert - $receiver: GET_VAR 'val assert: .ObjectAssert.assertNotNull?> declared in .assertNotNull' type=.ObjectAssert.assertNotNull?> origin=null diff --git a/compiler/testData/ir/irText/expressions/callableReferences/kt46069.kt b/compiler/testData/ir/irText/expressions/callableReferences/kt46069.kt index 8b92b56eb8e..ae539146115 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/kt46069.kt +++ b/compiler/testData/ir/irText/expressions/callableReferences/kt46069.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // SKIP_KT_DUMP class ObjectAssert { fun describedAs(description: String?, vararg args: Any?): ObjectAssert? = null