Use ResolvedCall for callable reference in KCallableNameProperty intrinsic

Instead of manually inspecting the DOUBLE_COLON_LHS slice, which is a
bit more error-prone. Note that new tests were passing before this
change
This commit is contained in:
Alexander Udalov
2017-11-22 18:28:21 +01:00
parent f4f5359725
commit 938fd1a57e
5 changed files with 48 additions and 17 deletions
@@ -17,14 +17,14 @@
package org.jetbrains.kotlin.codegen.intrinsics
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.JvmCodegenUtil
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.psi.KtCallableReferenceExpression
import org.jetbrains.kotlin.resolve.BindingContext.DOUBLE_COLON_LHS
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.jvm.AsmTypes.JAVA_STRING_TYPE
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.types.expressions.DoubleColonLHS
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitClassReceiver
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.Type.VOID_TYPE
@@ -33,18 +33,17 @@ class KCallableNameProperty : IntrinsicPropertyGetter() {
val expressionReceiver = resolvedCall!!.dispatchReceiver as? ExpressionReceiver ?: return null
val expression = expressionReceiver.expression as? KtCallableReferenceExpression ?: return null
val receiverExpression = expression.receiverExpression
val lhs = receiverExpression?.let { codegen.bindingContext.get(DOUBLE_COLON_LHS, it) }
val callableReference = expression.callableReference
val descriptor = callableReference.getResolvedCall(codegen.bindingContext)?.resultingDescriptor ?: return null
val referenceResolvedCall = expression.callableReference.getResolvedCall(codegen.bindingContext) ?: return null
val callableReferenceReceiver = JvmCodegenUtil.getBoundCallableReferenceReceiver(referenceResolvedCall)
return StackValue.operation(returnType) { iv ->
// Generate the left-hand side of a bound callable reference expression
if (lhs is DoubleColonLHS.Expression) {
codegen.gen(receiverExpression, VOID_TYPE)
if (callableReferenceReceiver != null && callableReferenceReceiver !is ImplicitClassReceiver) {
val stackValue = codegen.generateReceiverValue(callableReferenceReceiver, false)
StackValue.coercion(stackValue, codegen.asmType(callableReferenceReceiver.type)).put(VOID_TYPE, iv)
}
iv.aconst(descriptor.name.asString())
iv.aconst(referenceResolvedCall.resultingDescriptor.name.asString())
StackValue.coerce(JAVA_STRING_TYPE, returnType, iv)
}
}