diff --git a/compiler/backend-common/src/org/jetbrains/kotlin/backend/common/CodegenUtil.kt b/compiler/backend-common/src/org/jetbrains/kotlin/backend/common/CodegenUtil.kt index 16f84691b05..bced5c5c8c2 100644 --- a/compiler/backend-common/src/org/jetbrains/kotlin/backend/common/CodegenUtil.kt +++ b/compiler/backend-common/src/org/jetbrains/kotlin/backend/common/CodegenUtil.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.backend.common +import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import org.jetbrains.kotlin.backend.common.bridges.findInterfaceImplementation import org.jetbrains.kotlin.descriptors.* @@ -154,4 +155,12 @@ object CodegenUtil { } return this[slice, whenExpression] == true } + + @JvmStatic + fun constructFakeFunctionCall(project: Project, referencedFunction: FunctionDescriptor): KtCallExpression { + val fakeFunctionCall = StringBuilder("callableReferenceFakeCall(") + fakeFunctionCall.append(referencedFunction.valueParameters.map { "p${it.index}" }.joinToString(", ")) + fakeFunctionCall.append(")") + return KtPsiFactory(project).createExpression(fakeFunctionCall.toString()) as KtCallExpression + } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionReferenceGenerationStrategy.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionReferenceGenerationStrategy.java index d9c907cc974..00561c91e3e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionReferenceGenerationStrategy.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionReferenceGenerationStrategy.java @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen; import kotlin.collections.CollectionsKt; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.backend.common.CodegenUtil; import org.jetbrains.kotlin.codegen.state.GenerationState; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.psi.*; @@ -74,7 +75,7 @@ public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrat every argument boils down to calling LOAD with the corresponding index */ - KtCallExpression fakeExpression = constructFakeFunctionCall(); + KtCallExpression fakeExpression = CodegenUtil.constructFakeFunctionCall(state.getProject(), referencedFunction); final List fakeArguments = fakeExpression.getValueArguments(); final ReceiverValue dispatchReceiver = computeAndSaveReceiver(signature, codegen, referencedFunction.getDispatchReceiverParameter()); @@ -140,20 +141,6 @@ public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrat v.areturn(returnType); } - @NotNull - private KtCallExpression constructFakeFunctionCall() { - StringBuilder fakeFunctionCall = new StringBuilder("callableReferenceFakeCall("); - for (Iterator iterator = referencedFunction.getValueParameters().iterator(); iterator.hasNext(); ) { - ValueParameterDescriptor descriptor = iterator.next(); - fakeFunctionCall.append("p").append(descriptor.getIndex()); - if (iterator.hasNext()) { - fakeFunctionCall.append(", "); - } - } - fakeFunctionCall.append(")"); - return (KtCallExpression) KtPsiFactoryKt.KtPsiFactory(state.getProject()).createExpression(fakeFunctionCall.toString()); - } - private void computeAndSaveArguments(@NotNull List fakeArguments, @NotNull ExpressionCodegen codegen) { int receivers = (referencedFunction.getDispatchReceiverParameter() != null ? 1 : 0) + (referencedFunction.getExtensionReceiverParameter() != null ? 1 : 0) - diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/reference/CallableReferenceTranslator.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/reference/CallableReferenceTranslator.kt index 9597eee6474..13ee4e4fed4 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/reference/CallableReferenceTranslator.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/reference/CallableReferenceTranslator.kt @@ -16,7 +16,7 @@ package org.jetbrains.kotlin.js.translate.reference -import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.backend.common.CodegenUtil import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.js.backend.ast.* import org.jetbrains.kotlin.js.backend.ast.metadata.SideEffectKind @@ -27,10 +27,8 @@ import org.jetbrains.kotlin.js.translate.context.Namer import org.jetbrains.kotlin.js.translate.context.TranslationContext import org.jetbrains.kotlin.js.translate.general.Translation import org.jetbrains.kotlin.js.translate.utils.BindingUtils -import org.jetbrains.kotlin.psi.KtCallExpression import org.jetbrains.kotlin.psi.KtCallableReferenceExpression import org.jetbrains.kotlin.psi.KtExpression -import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.PropertyImportedFromObject import org.jetbrains.kotlin.resolve.calls.callUtil.getFunctionResolvedCallWithAssert @@ -80,7 +78,7 @@ object CallableReferenceTranslator { receiver: JsExpression? ): JsExpression { val realResolvedCall = expression.callableReference.getFunctionResolvedCallWithAssert(context.bindingContext()) - val fakeExpression = constructFakeFunctionCall(expression.project, descriptor) + val fakeExpression = CodegenUtil.constructFakeFunctionCall(expression.project, descriptor) val fakeCall = CallMaker.makeCall(fakeExpression, null, null, fakeExpression, fakeExpression.valueArguments) val fakeResolvedCall = object : DelegatingResolvedCall(realResolvedCall) { @@ -118,13 +116,6 @@ object CallableReferenceTranslator { return wrapFunctionCallableRef(context, expression.callableReference.getReferencedName(), rawCallableRef) } - private fun constructFakeFunctionCall(project: Project, referencedFunction: FunctionDescriptor): KtCallExpression { - val fakeFunctionCall = StringBuilder("callableReferenceFakeCall(") - fakeFunctionCall.append(referencedFunction.valueParameters.map { "p${it.index}" }.joinToString(", ")) - fakeFunctionCall.append(")") - return KtPsiFactory(project).createExpression(fakeFunctionCall.toString()) as KtCallExpression - } - private fun translateForProperty( descriptor: PropertyDescriptor, context: TranslationContext,