@@ -760,41 +760,43 @@ internal class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : Jvm
760760 " Expected ${replacement.render()} , got ${declarations?.map { it.render() }} "
761761 }
762762 }
763- return makeNewLambda(originalFunction, ref, makeBody = { wrapper ->
764- variablesToAdd[replacement]?.let {
765- variablesToAdd[wrapper] = it
766- variablesToAdd.remove(replacement)
767- }
768- if (replacement in possibleExtraBoxUsageGenerated) {
769- possibleExtraBoxUsageGenerated.add(wrapper)
770- possibleExtraBoxUsageGenerated.remove(replacement)
771- }
772- with (context.createJvmIrBuilder(wrapper.symbol)) {
773- irExprBody(irBlock {
774- val newArguments: List <IrValueDeclaration > = wrapper.parameters.flatMap { parameter ->
775- if (! parameter.type.needsMfvcFlattening()) {
776- listOf (parameter)
777- } else {
778- // Old parameter value will be only used to set parameter of the lowered function,
779- // thus it is useless to show it in debugger
780- parameter.origin = JvmLoweredDeclarationOrigin .TEMPORARY_MULTI_FIELD_VALUE_CLASS_PARAMETER
781- val rootNode = replacements.getRootMfvcNode(parameter.type.erasedUpperBound)
782- rootNode.createInstanceFromBox(this , irGet(parameter), AccessType .ChooseEffective , ::variablesSaver)
783- .makeFlattenedGetterExpressions(this , irCurrentClass, ::registerPossibleExtraBoxUsage)
784- .mapIndexed { index, expression ->
785- savableStandaloneVariableWithSetter(
786- expression = expression,
787- name = " ${parameter.name.asString()} -${rootNode.leaves[index].fullFieldName} " ,
788- origin = JvmLoweredDeclarationOrigin .MULTI_FIELD_VALUE_CLASS_REPRESENTATION_VARIABLE ,
789- saveVariable = ::variablesSaver,
790- )
791- }
792- }
763+ return makeNewLambdaBlock(originalFunction, ref, makeBody = { wrapper -> makeLambdaBody(wrapper, replacement) })
764+ }
765+
766+ private fun makeLambdaBody (wrapper : IrSimpleFunction , replacement : IrFunction ): IrBody {
767+ variablesToAdd[replacement]?.let {
768+ variablesToAdd[wrapper] = it
769+ variablesToAdd.remove(replacement)
770+ }
771+ if (replacement in possibleExtraBoxUsageGenerated) {
772+ possibleExtraBoxUsageGenerated.add(wrapper)
773+ possibleExtraBoxUsageGenerated.remove(replacement)
774+ }
775+ return with (context.createJvmIrBuilder(wrapper.symbol)) {
776+ irExprBody(irBlock {
777+ val newArguments: List <IrValueDeclaration > = wrapper.parameters.flatMap { parameter ->
778+ if (! parameter.type.needsMfvcFlattening()) {
779+ listOf (parameter)
780+ } else {
781+ // Old parameter value will be only used to set parameter of the lowered function,
782+ // thus it is useless to show it in debugger
783+ parameter.origin = JvmLoweredDeclarationOrigin .TEMPORARY_MULTI_FIELD_VALUE_CLASS_PARAMETER
784+ val rootNode = replacements.getRootMfvcNode(parameter.type.erasedUpperBound)
785+ rootNode.createInstanceFromBox(this , irGet(parameter), AccessType .ChooseEffective , ::variablesSaver)
786+ .makeFlattenedGetterExpressions(this , irCurrentClass, ::registerPossibleExtraBoxUsage)
787+ .mapIndexed { index, expression ->
788+ savableStandaloneVariableWithSetter(
789+ expression = expression,
790+ name = " ${parameter.name.asString()} -${rootNode.leaves[index].fullFieldName} " ,
791+ origin = JvmLoweredDeclarationOrigin .MULTI_FIELD_VALUE_CLASS_REPRESENTATION_VARIABLE ,
792+ saveVariable = ::variablesSaver,
793+ )
794+ }
793795 }
794- + replacement.inline(wrapper.parent, newArguments)
795- } )
796- }
797- })
796+ }
797+ + replacement.inline(wrapper.parent, newArguments )
798+ })
799+ }
798800 }
799801
800802 override fun visitFunctionReference (expression : IrFunctionReference ): IrExpression {
@@ -814,12 +816,54 @@ internal class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : Jvm
814816 }.unwrapBlock()
815817 }
816818
819+ override fun visitRichFunctionReference (expression : IrRichFunctionReference ): IrExpression {
820+ val originalFunction = expression.invokeFunction
821+ val replacement = originalFunction.getReplacement()
822+ if (replacement == null ) {
823+ expression.invokeFunction = visitFunctionNew(originalFunction) as IrSimpleFunction
824+ return expression
825+ }
826+ val declarations = transformFunctionFlat(originalFunction)
827+ require(declarations == listOf (replacement)) {
828+ " Expected ${replacement.render()} , got ${declarations?.map { it.render() }} "
829+ }
830+ val wrapper = makeNewLamdaFunction(originalFunction, makeBody = { wrapper -> makeLambdaBody(wrapper, replacement) })
831+ expression.boundValues.transformInPlace(this , null )
832+ expression.invokeFunction = wrapper
833+ return expression
834+ }
835+
817836 private fun IrFunction.getReplacement (): IrFunction ? = replacements.getReplacementFunction(this )
818837 ? : (this as ? IrConstructor )?.let { replacements.getReplacementForRegularClassConstructor(it) }
819838
820- private fun makeNewLambda (
839+ private fun makeNewLambdaBlock (
821840 originalFunction : IrFunction , expression : IrFunctionReference , makeBody : (wrapper: IrSimpleFunction ) -> IrBody
822841 ): IrContainerExpression {
842+ val wrapper = makeNewLamdaFunction(originalFunction, makeBody)
843+
844+ val newReference = IrFunctionReferenceImpl (
845+ startOffset = UNDEFINED_OFFSET ,
846+ endOffset = UNDEFINED_OFFSET ,
847+ type = expression.type,
848+ symbol = wrapper.symbol,
849+ typeArgumentsCount = expression.typeArguments.size,
850+ reflectionTarget = expression.reflectionTarget,
851+ origin = expression.origin,
852+ ).apply {
853+ copyTypeArgumentsFrom(expression)
854+ arguments.assignFrom(expression.arguments) { it?.transform(this @JvmMultiFieldValueClassLowering, null ) }
855+ copyAttributes(expression)
856+ }
857+ return context.createJvmIrBuilder(getCurrentScopeSymbol(), expression).irBlock(origin = IrStatementOrigin .LAMBDA ) {
858+ + wrapper
859+ + newReference
860+ }
861+ }
862+
863+ private fun makeNewLamdaFunction (
864+ originalFunction : IrFunction ,
865+ makeBody : (IrSimpleFunction ) -> IrBody ,
866+ ): IrSimpleFunction {
823867 val currentDeclarationParent = currentDeclarationParent!!
824868 val wrapper = context.irFactory.buildFun {
825869 updateFrom(originalFunction)
@@ -847,24 +891,7 @@ internal class JvmMultiFieldValueClassLowering(context: JvmBackendContext) : Jvm
847891 postActionAfterTransformingClassDeclaration(this )
848892 }
849893 }
850-
851- val newReference = IrFunctionReferenceImpl (
852- startOffset = UNDEFINED_OFFSET ,
853- endOffset = UNDEFINED_OFFSET ,
854- type = expression.type,
855- symbol = wrapper.symbol,
856- typeArgumentsCount = expression.typeArguments.size,
857- reflectionTarget = expression.reflectionTarget,
858- origin = expression.origin,
859- ).apply {
860- copyTypeArgumentsFrom(expression)
861- arguments.assignFrom(expression.arguments) { it?.transform(this @JvmMultiFieldValueClassLowering, null ) }
862- copyAttributes(expression)
863- }
864- return context.createJvmIrBuilder(getCurrentScopeSymbol(), expression).irBlock(origin = IrStatementOrigin .LAMBDA ) {
865- + wrapper
866- + newReference
867- }
894+ return wrapper
868895 }
869896
870897 override fun visitFunctionAccess (expression : IrFunctionAccessExpression ): IrExpression {
0 commit comments