Callable references now can have receivers and arguments

(for bound references implementation).
This commit is contained in:
Dmitry Petrov
2016-09-02 17:31:43 +03:00
committed by Dmitry Petrov
parent fe397eddbe
commit a3dadd9e3f
16 changed files with 45 additions and 68 deletions
@@ -126,6 +126,3 @@ fun IrBuilderWithScope.irString(value: String) =
fun IrBuilderWithScope.irConcat() =
IrStringConcatenationImpl(startOffset, endOffset, context.builtIns.stringType)
fun IrBuilderWithScope.irCallableReference(type: KotlinType, descriptor: CallableDescriptor) =
IrCallableReferenceImpl(startOffset, endOffset, type, descriptor)
@@ -19,18 +19,12 @@ package org.jetbrains.kotlin.psi2ir.generators
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.declarations.IrFunctionBase
import org.jetbrains.kotlin.ir.descriptors.IrPropertyDelegateDescriptor
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi2ir.builders.irBlockBody
import org.jetbrains.kotlin.psi2ir.builders.irCallableReference
import org.jetbrains.kotlin.psi2ir.builders.irGet
import org.jetbrains.kotlin.psi2ir.builders.irReturn
import org.jetbrains.kotlin.psi2ir.intermediate.VariableLValue
import org.jetbrains.kotlin.psi2ir.intermediate.setExplicitReceiverValue
import org.jetbrains.kotlin.resolve.BindingContext
import java.lang.AssertionError
import java.util.*
class BodyGenerator(val scopeOwner: DeclarationDescriptor, override val context: GeneratorContext): GeneratorWithScope {
@@ -86,7 +86,8 @@ class DelegatedPropertyGenerator(override val context: GeneratorContext) : Gener
BackingFieldLValue(ktDelegate.startOffset, ktDelegate.endOffset, delegateDescriptor, null)
private fun createCallableReference(ktElement: KtElement, type: KotlinType, referencedDescriptor: CallableDescriptor): IrCallableReference =
IrCallableReferenceImpl(ktElement.startOffset, ktElement.endOffset, type, referencedDescriptor)
IrCallableReferenceImpl(ktElement.startOffset, ktElement.endOffset, type,
referencedDescriptor, IrOperator.PROPERTY_REFERENCE_FOR_DELEGATE)
fun generateLocalDelegatedProperty(
ktProperty: KtProperty,
@@ -40,7 +40,8 @@ class LocalFunctionGenerator(statementGenerator: StatementGenerator) : Statement
irFun.body = BodyGenerator(lambdaDescriptor, statementGenerator.context).generateLambdaBody(ktFun)
irBlock.addStatement(irFun)
irBlock.addStatement(IrCallableReferenceImpl(ktLambda.startOffset, ktLambda.endOffset, lambdaExpressionType, lambdaDescriptor))
irBlock.addStatement(IrCallableReferenceImpl(ktLambda.startOffset, ktLambda.endOffset, lambdaExpressionType,
lambdaDescriptor, IrOperator.LAMBDA))
return irBlock
}
@@ -57,7 +58,8 @@ class LocalFunctionGenerator(statementGenerator: StatementGenerator) : Statement
val irFun = generateFunctionDeclaration(ktFun)
irBlock.addStatement(irFun)
irBlock.addStatement(IrCallableReferenceImpl(ktFun.startOffset, ktFun.endOffset, funExpressionType, irFun.descriptor))
irBlock.addStatement(IrCallableReferenceImpl(ktFun.startOffset, ktFun.endOffset, funExpressionType,
irFun.descriptor, IrOperator.ANONYMOUS_FUNCTION))
irBlock
}
@@ -17,37 +17,19 @@
package org.jetbrains.kotlin.ir.expressions
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.types.KotlinType
interface IrCallableReference : IrDeclarationReference {
override val descriptor: CallableDescriptor
// TODO closure
}
interface IrCallableReference : IrGeneralCall
class IrCallableReferenceImpl(
startOffset: Int,
endOffset: Int,
type: KotlinType,
descriptor: CallableDescriptor
) : IrDeclarationReferenceBase<CallableDescriptor>(startOffset, endOffset, type, descriptor), IrCallableReference {
override val descriptor: CallableDescriptor,
override val operator: IrOperator? = null
) : IrGeneralCallBase(startOffset, endOffset, type, descriptor.valueParameters.size, operator), IrCallableReference {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
return visitor.visitCallableReference(this, data)
}
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
// TODO
}
override fun getChild(slot: Int): IrElement? {
// TODO
return null
}
override fun replaceChild(slot: Int, newChild: IrElement) {
// TODO
}
}
@@ -91,7 +91,8 @@ interface IrOperator {
object SUPER_CONSTRUCTOR_CALL : IrOperatorImpl("SUPER_CONSTRUCTOR_CALL")
object INITIALIZE_PROPERTY_FROM_PARAMETER : IrOperatorImpl("INITIALIZE_PROPERTY_FROM_PARAMETER")
object ANONYMOUS_INITIALIZER : IrOperatorImpl("ANONYMOUS_INITIALIZER")
object PROPERTY_REFERENCE_FOR_DELEGATE : IrOperatorImpl("PROPERTY_REFERENCE_FOR_DELEGATE")
data class COMPONENT_N private constructor(val index: Int) : IrOperatorImpl("COMPONENT_$index") {
companion object {
@@ -171,10 +171,10 @@ class RenderIrElementVisitor : IrElementVisitor<String, Nothing?> {
"THROW type=${expression.type.render()}"
override fun visitCallableReference(expression: IrCallableReference, data: Nothing?): String =
"CALLABLE_REFERENCE ${expression.descriptor.render()} type=${expression.type.render()}"
"CALLABLE_REFERENCE ${expression.descriptor.name} type=${expression.type.render()} operator=${expression.operator}"
override fun visitClassReference(expression: IrClassReference, data: Nothing?): String =
"CLASS_REFERENCE ${expression.descriptor.render()} type=${expression.type.render()}"
"CLASS_REFERENCE ${expression.descriptor.name} type=${expression.type.render()}"
override fun visitGetClass(expression: IrGetClass, data: Nothing?): String =
"GET_CLASS type=${expression.type.render()}"
@@ -8,14 +8,14 @@ FILE /delegatedProperties.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from=<anonymous>
CONST Int type=kotlin.Int value='42'
CALLABLE_REFERENCE local final fun <anonymous>(): kotlin.Int type=() -> kotlin.Int
CALLABLE_REFERENCE <anonymous> type=() -> kotlin.Int operator=LAMBDA
PROPERTY_GETTER public fun <get-test1>(): kotlin.Int
BLOCK_BODY
RETURN type=kotlin.Nothing from=<get-test1>
CALL .getValue type=kotlin.Int operator=null
$receiver: GET_BACKING_FIELD test1$delegate type=kotlin.Lazy<kotlin.Int> operator=null
thisRef: CONST Null type=kotlin.Nothing? value='null'
property: CALLABLE_REFERENCE public val test1: kotlin.Int type=kotlin.reflect.KProperty0<kotlin.Int>
property: CALLABLE_REFERENCE test1 type=kotlin.reflect.KProperty0<kotlin.Int> operator=PROPERTY_REFERENCE_FOR_DELEGATE
CLASS CLASS C
CONSTRUCTOR public constructor C(/*0*/ map: kotlin.collections.MutableMap<kotlin.String, kotlin.Any>)
BLOCK_BODY
@@ -34,14 +34,14 @@ FILE /delegatedProperties.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from=<anonymous>
CONST Int type=kotlin.Int value='42'
CALLABLE_REFERENCE local final fun <anonymous>(): kotlin.Int type=() -> kotlin.Int
CALLABLE_REFERENCE <anonymous> type=() -> kotlin.Int operator=LAMBDA
PROPERTY_GETTER public final fun <get-test2>(): kotlin.Int
BLOCK_BODY
RETURN type=kotlin.Nothing from=<get-test2>
CALL .getValue type=kotlin.Int operator=null
$receiver: GET_BACKING_FIELD test2$delegate type=kotlin.Lazy<kotlin.Int> operator=null
thisRef: THIS public final class C type=C
property: CALLABLE_REFERENCE public final val test2: kotlin.Int type=kotlin.reflect.KProperty1<C, kotlin.Int>
property: CALLABLE_REFERENCE test2 type=kotlin.reflect.KProperty1<C, kotlin.Int> operator=PROPERTY_REFERENCE_FOR_DELEGATE
PROPERTY public final var test3: kotlin.Any
delegate: PROPERTY val `test3$delegate`: kotlin.collections.MutableMap<kotlin.String, kotlin.Any>
EXPRESSION_BODY
@@ -53,14 +53,14 @@ FILE /delegatedProperties.kt
CALL .getValue type=kotlin.Any operator=null
$receiver: GET_BACKING_FIELD test3$delegate type=kotlin.collections.MutableMap<kotlin.String, kotlin.Any> operator=null
thisRef: THIS public final class C type=C
property: CALLABLE_REFERENCE public final var test3: kotlin.Any type=kotlin.reflect.KMutableProperty1<C, kotlin.Any>
property: CALLABLE_REFERENCE test3 type=kotlin.reflect.KMutableProperty1<C, kotlin.Any> operator=PROPERTY_REFERENCE_FOR_DELEGATE
PROPERTY_SETTER public final fun <set-test3>(/*0*/ <set-?>: kotlin.Any): kotlin.Unit
BLOCK_BODY
RETURN type=kotlin.Nothing from=<set-test3>
CALL .setValue type=kotlin.Unit operator=null
$receiver: GET_BACKING_FIELD test3$delegate type=kotlin.collections.MutableMap<kotlin.String, kotlin.Any> operator=null
thisRef: THIS public final class C type=C
property: CALLABLE_REFERENCE public final var test3: kotlin.Any type=kotlin.reflect.KMutableProperty1<C, kotlin.Any>
property: CALLABLE_REFERENCE test3 type=kotlin.reflect.KMutableProperty1<C, kotlin.Any> operator=PROPERTY_REFERENCE_FOR_DELEGATE
value: GET_VAR <set-?> type=kotlin.Any operator=null
PROPERTY public var test4: kotlin.Any
delegate: PROPERTY val `test4$delegate`: java.util.HashMap<kotlin.String, kotlin.Any>
@@ -72,12 +72,12 @@ FILE /delegatedProperties.kt
CALL .getValue type=kotlin.Any operator=null
$receiver: GET_BACKING_FIELD test4$delegate type=java.util.HashMap<kotlin.String, kotlin.Any> operator=null
thisRef: CONST Null type=kotlin.Nothing? value='null'
property: CALLABLE_REFERENCE public var test4: kotlin.Any type=kotlin.reflect.KMutableProperty0<kotlin.Any>
property: CALLABLE_REFERENCE test4 type=kotlin.reflect.KMutableProperty0<kotlin.Any> operator=PROPERTY_REFERENCE_FOR_DELEGATE
PROPERTY_SETTER public fun <set-test4>(/*0*/ <set-?>: kotlin.Any): kotlin.Unit
BLOCK_BODY
RETURN type=kotlin.Nothing from=<set-test4>
CALL .setValue type=kotlin.Unit operator=null
$receiver: GET_BACKING_FIELD test4$delegate type=java.util.HashMap<kotlin.String, kotlin.Any> operator=null
thisRef: CONST Null type=kotlin.Nothing? value='null'
property: CALLABLE_REFERENCE public var test4: kotlin.Any type=kotlin.reflect.KMutableProperty0<kotlin.Any>
property: CALLABLE_REFERENCE test4 type=kotlin.reflect.KMutableProperty0<kotlin.Any> operator=PROPERTY_REFERENCE_FOR_DELEGATE
value: GET_VAR <set-?> type=kotlin.Any operator=null
@@ -9,14 +9,14 @@ FILE /localDelegatedProperties.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from=<anonymous>
CONST Int type=kotlin.Int value='42'
CALLABLE_REFERENCE local final fun <anonymous>(): kotlin.Int type=() -> kotlin.Int
CALLABLE_REFERENCE <anonymous> type=() -> kotlin.Int operator=LAMBDA
LOCAL_PROPERTY_ACCESSOR <get-x>
BLOCK_BODY
RETURN type=kotlin.Nothing from=<get-x>
CALL .getValue type=kotlin.Int operator=null
$receiver: GET_VAR x$delegate type=kotlin.Lazy<kotlin.Int> operator=null
thisRef: CONST Null type=kotlin.Nothing? value='null'
property: CALLABLE_REFERENCE val x: kotlin.Int type=kotlin.reflect.KProperty0<kotlin.Int>
property: CALLABLE_REFERENCE x type=kotlin.reflect.KProperty0<kotlin.Int> operator=PROPERTY_REFERENCE_FOR_DELEGATE
CALL .println type=kotlin.Unit operator=null
message: CALL .<get-x> type=kotlin.Int operator=GET_LOCAL_PROPERTY
FUN public fun test2(): kotlin.Unit
@@ -30,7 +30,7 @@ FILE /localDelegatedProperties.kt
CALL .getValue type=kotlin.Int operator=null
$receiver: GET_VAR x$delegate type=java.util.HashMap<kotlin.String, kotlin.Int> operator=null
thisRef: CONST Null type=kotlin.Nothing? value='null'
property: CALLABLE_REFERENCE var x: kotlin.Int type=kotlin.reflect.KMutableProperty0<kotlin.Int>
property: CALLABLE_REFERENCE x type=kotlin.reflect.KMutableProperty0<kotlin.Int> operator=PROPERTY_REFERENCE_FOR_DELEGATE
LOCAL_PROPERTY_ACCESSOR <set-x>
BLOCK_BODY
RETURN type=kotlin.Nothing from=<set-x>
@@ -38,7 +38,7 @@ FILE /localDelegatedProperties.kt
CALL .setValue type=kotlin.Unit operator=null
$receiver: GET_VAR x$delegate type=java.util.HashMap<kotlin.String, kotlin.Int> operator=null
thisRef: CONST Null type=kotlin.Nothing? value='null'
property: CALLABLE_REFERENCE var x: kotlin.Int type=kotlin.reflect.KMutableProperty0<kotlin.Int>
property: CALLABLE_REFERENCE x type=kotlin.reflect.KMutableProperty0<kotlin.Int> operator=PROPERTY_REFERENCE_FOR_DELEGATE
value: GET_VAR value type=kotlin.Int operator=null
CALL .<set-x> type=kotlin.Int operator=EQ
value: CONST Int type=kotlin.Int value='0'
@@ -12,23 +12,23 @@ FILE /reflectionLiterals.kt
CONST Int type=kotlin.Int value='42'
PROPERTY public val test1: kotlin.reflect.KClass<A>
EXPRESSION_BODY
CLASS_REFERENCE public final class A type=kotlin.reflect.KClass<A>
CLASS_REFERENCE A type=kotlin.reflect.KClass<A>
PROPERTY public val test2: kotlin.reflect.KClass<kotlin.Int>
EXPRESSION_BODY
GET_CLASS type=kotlin.reflect.KClass<kotlin.Int>
CALL .<get-qux> type=kotlin.Int operator=GET_PROPERTY
PROPERTY public val test3: kotlin.reflect.KFunction1<A, kotlin.Unit>
EXPRESSION_BODY
CALLABLE_REFERENCE public final fun foo(): kotlin.Unit type=kotlin.reflect.KFunction1<A, kotlin.Unit>
CALLABLE_REFERENCE foo type=kotlin.reflect.KFunction1<A, kotlin.Unit> operator=null
PROPERTY public val test4: kotlin.reflect.KFunction0<A>
EXPRESSION_BODY
CALLABLE_REFERENCE public constructor A() type=kotlin.reflect.KFunction0<A>
CALLABLE_REFERENCE <init> type=kotlin.reflect.KFunction0<A> operator=null
PROPERTY public val test5: kotlin.reflect.KFunction0<kotlin.Unit>
EXPRESSION_BODY
CALLABLE_REFERENCE public final fun foo(): kotlin.Unit type=kotlin.reflect.KFunction0<kotlin.Unit>
CALLABLE_REFERENCE foo type=kotlin.reflect.KFunction0<kotlin.Unit> operator=null
PROPERTY public val test6: kotlin.reflect.KFunction0<kotlin.Unit>
EXPRESSION_BODY
CALLABLE_REFERENCE public fun bar(): kotlin.Unit type=kotlin.reflect.KFunction0<kotlin.Unit>
CALLABLE_REFERENCE bar type=kotlin.reflect.KFunction0<kotlin.Unit> operator=null
PROPERTY public val test7: kotlin.reflect.KProperty0<kotlin.Int>
EXPRESSION_BODY
CALLABLE_REFERENCE public val qux: kotlin.Int = 42 type=kotlin.reflect.KProperty0<kotlin.Int>
CALLABLE_REFERENCE qux type=kotlin.reflect.KProperty0<kotlin.Int> operator=null
@@ -7,7 +7,7 @@ FILE /variableAsFunctionCall.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from=<anonymous>
$RECEIVER of: k type=kotlin.String
CALLABLE_REFERENCE local final fun <anonymous>(): kotlin.String type=() -> kotlin.String
CALLABLE_REFERENCE <anonymous> type=() -> kotlin.String operator=LAMBDA
FUN public fun test1(/*0*/ f: () -> kotlin.Unit): kotlin.Unit
BLOCK_BODY
RETURN type=kotlin.Nothing from=test1
+1 -1
View File
@@ -5,4 +5,4 @@ FILE /anonymousFunction.kt
FUN local final fun <no name provided>(): kotlin.Unit
BLOCK_BODY
CALL .println type=kotlin.Unit operator=null
CALLABLE_REFERENCE local final fun <no name provided>(): kotlin.Unit type=() -> kotlin.Unit
CALLABLE_REFERENCE <no name provided> type=() -> kotlin.Unit operator=ANONYMOUS_FUNCTION
+1 -1
View File
@@ -10,4 +10,4 @@ FILE /extensionLambda.kt
RETURN type=kotlin.Nothing from=<anonymous>
CALL .<get-length> type=kotlin.Int operator=GET_PROPERTY
$this: $RECEIVER of: <anonymous> type=kotlin.String
CALLABLE_REFERENCE local final fun kotlin.String.<anonymous>(): kotlin.Int type=kotlin.String.() -> kotlin.Int
CALLABLE_REFERENCE <anonymous> type=kotlin.String.() -> kotlin.Int operator=LAMBDA
+1 -1
View File
@@ -6,4 +6,4 @@ FILE /justLambda.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from=<anonymous>
CONST Int type=kotlin.Int value='42'
CALLABLE_REFERENCE local final fun <anonymous>(): kotlin.Int type=() -> kotlin.Int
CALLABLE_REFERENCE <anonymous> type=() -> kotlin.Int operator=LAMBDA
@@ -43,6 +43,6 @@ FILE /multipleImplicitReceivers.kt
$receiver: CALL .<get-foo> type=B operator=GET_PROPERTY
$this: $RECEIVER of: <anonymous> type=IFoo
$receiver: $RECEIVER of: <anonymous> type=A
CALLABLE_REFERENCE local final fun IInvoke.<anonymous>(): kotlin.Int type=IInvoke.() -> kotlin.Int
CALLABLE_REFERENCE local final fun IFoo.<anonymous>(): kotlin.Int type=IFoo.() -> kotlin.Int
CALLABLE_REFERENCE local final fun A.<anonymous>(): kotlin.Int type=A.() -> kotlin.Int
CALLABLE_REFERENCE <anonymous> type=IInvoke.() -> kotlin.Int operator=LAMBDA
CALLABLE_REFERENCE <anonymous> type=IFoo.() -> kotlin.Int operator=LAMBDA
CALLABLE_REFERENCE <anonymous> type=A.() -> kotlin.Int operator=LAMBDA
+5 -5
View File
@@ -6,7 +6,7 @@ FILE /nonLocalReturn.kt
FUN local final fun <anonymous>(): kotlin.Nothing
BLOCK_BODY
RETURN type=kotlin.Nothing from=test0
CALLABLE_REFERENCE local final fun <anonymous>(): kotlin.Nothing type=() -> kotlin.Nothing
CALLABLE_REFERENCE <anonymous> type=() -> kotlin.Nothing operator=LAMBDA
FUN public fun test1(): kotlin.Unit
BLOCK_BODY
CALL .run type=kotlin.Unit operator=null
@@ -14,7 +14,7 @@ FILE /nonLocalReturn.kt
FUN local final fun <anonymous>(): kotlin.Unit
BLOCK_BODY
RETURN type=kotlin.Nothing from=<anonymous>
CALLABLE_REFERENCE local final fun <anonymous>(): kotlin.Unit type=() -> kotlin.Unit
CALLABLE_REFERENCE <anonymous> type=() -> kotlin.Unit operator=LAMBDA
FUN public fun test2(): kotlin.Unit
BLOCK_BODY
CALL .run type=kotlin.Unit operator=null
@@ -22,7 +22,7 @@ FILE /nonLocalReturn.kt
FUN local final fun <anonymous>(): kotlin.Unit
BLOCK_BODY
RETURN type=kotlin.Nothing from=<anonymous>
CALLABLE_REFERENCE local final fun <anonymous>(): kotlin.Unit type=() -> kotlin.Unit
CALLABLE_REFERENCE <anonymous> type=() -> kotlin.Unit operator=LAMBDA
FUN public fun testLrmFoo1(/*0*/ ints: kotlin.collections.List<kotlin.Int>): kotlin.Unit
BLOCK_BODY
CALL .forEach type=kotlin.Unit operator=null
@@ -38,7 +38,7 @@ FILE /nonLocalReturn.kt
RETURN type=kotlin.Nothing from=<anonymous>
CALL .print type=kotlin.Unit operator=null
message: GET_VAR it type=kotlin.Int operator=null
CALLABLE_REFERENCE local final fun <anonymous>(/*0*/ it: kotlin.Int): kotlin.Unit type=(kotlin.Int) -> kotlin.Unit
CALLABLE_REFERENCE <anonymous> type=(kotlin.Int) -> kotlin.Unit operator=LAMBDA
FUN public fun testLrmFoo2(/*0*/ ints: kotlin.collections.List<kotlin.Int>): kotlin.Unit
BLOCK_BODY
CALL .forEach type=kotlin.Unit operator=null
@@ -54,4 +54,4 @@ FILE /nonLocalReturn.kt
RETURN type=kotlin.Nothing from=<anonymous>
CALL .print type=kotlin.Unit operator=null
message: GET_VAR it type=kotlin.Int operator=null
CALLABLE_REFERENCE local final fun <anonymous>(/*0*/ it: kotlin.Int): kotlin.Unit type=(kotlin.Int) -> kotlin.Unit
CALLABLE_REFERENCE <anonymous> type=(kotlin.Int) -> kotlin.Unit operator=LAMBDA