Use original descriptors to create symbols for callable references

This commit is contained in:
Dmitry Petrov
2017-05-04 12:43:31 +03:00
parent c2601c947f
commit fdb4de355c
8 changed files with 74 additions and 15 deletions
@@ -37,7 +37,7 @@ class LocalFunctionGenerator(statementGenerator: StatementGenerator) : Statement
irBlock.statements.add( irBlock.statements.add(
IrFunctionReferenceImpl( IrFunctionReferenceImpl(
ktLambda.startOffset, ktLambda.endOffset, lambdaExpressionType, ktLambda.startOffset, ktLambda.endOffset, lambdaExpressionType,
irLambdaFunction.symbol, irLambdaFunction.symbol, irLambdaFunction.symbol.descriptor,
null, IrStatementOrigin.LAMBDA null, IrStatementOrigin.LAMBDA
) )
) )
@@ -59,7 +59,7 @@ class LocalFunctionGenerator(statementGenerator: StatementGenerator) : Statement
irBlock.statements.add( irBlock.statements.add(
IrFunctionReferenceImpl( IrFunctionReferenceImpl(
ktFun.startOffset, ktFun.endOffset, funExpressionType, ktFun.startOffset, ktFun.endOffset, funExpressionType,
irFun.symbol, irFun.symbol, irFun.symbol.descriptor,
null, IrStatementOrigin.ANONYMOUS_FUNCTION null, IrStatementOrigin.ANONYMOUS_FUNCTION
) )
) )
@@ -42,7 +42,7 @@ class ReflectionReferencesGenerator(statementGenerator: StatementGenerator) : St
} }
else { else {
val typeConstructorDeclaration = lhs.type.constructor.declarationDescriptor val typeConstructorDeclaration = lhs.type.constructor.declarationDescriptor
val typeClass = typeConstructorDeclaration as? ClassifierDescriptor ?: val typeClass = typeConstructorDeclaration ?:
throw AssertionError("Unexpected type constructor for ${lhs.type}: $typeConstructorDeclaration") throw AssertionError("Unexpected type constructor for ${lhs.type}: $typeConstructorDeclaration")
IrClassReferenceImpl(ktClassLiteral.startOffset, ktClassLiteral.endOffset, resultType, IrClassReferenceImpl(ktClassLiteral.startOffset, ktClassLiteral.endOffset, resultType,
context.symbolTable.referenceClassifier(typeClass)) context.symbolTable.referenceClassifier(typeClass))
@@ -83,7 +83,8 @@ class ReflectionReferencesGenerator(statementGenerator: StatementGenerator) : St
is FunctionDescriptor -> is FunctionDescriptor ->
generateFunctionReference( generateFunctionReference(
startOffset, endOffset, type, startOffset, endOffset, type,
context.symbolTable.referenceFunction(callableDescriptor), context.symbolTable.referenceFunction(callableDescriptor.original),
callableDescriptor,
typeArguments, typeArguments,
origin origin
) )
@@ -128,8 +129,8 @@ class ReflectionReferencesGenerator(statementGenerator: StatementGenerator) : St
val setterDescriptor = propertyDescriptor.setter val setterDescriptor = propertyDescriptor.setter
val fieldSymbol = if (getterDescriptor == null) context.symbolTable.referenceField(propertyDescriptor) else null val fieldSymbol = if (getterDescriptor == null) context.symbolTable.referenceField(propertyDescriptor) else null
val getterSymbol = getterDescriptor?.let { context.symbolTable.referenceFunction(it) } val getterSymbol = getterDescriptor?.let { context.symbolTable.referenceFunction(it.original) }
val setterSymbol = setterDescriptor?.let { context.symbolTable.referenceFunction(it) } val setterSymbol = setterDescriptor?.let { context.symbolTable.referenceFunction(it.original) }
return IrPropertyReferenceImpl( return IrPropertyReferenceImpl(
startOffset, endOffset, type, startOffset, endOffset, type,
@@ -145,12 +146,13 @@ class ReflectionReferencesGenerator(statementGenerator: StatementGenerator) : St
endOffset: Int, endOffset: Int,
type: KotlinType, type: KotlinType,
symbol: IrFunctionSymbol, symbol: IrFunctionSymbol,
descriptor: FunctionDescriptor,
typeArguments: Map<TypeParameterDescriptor, KotlinType>?, typeArguments: Map<TypeParameterDescriptor, KotlinType>?,
origin: IrStatementOrigin? origin: IrStatementOrigin?
): IrFunctionReference = ): IrFunctionReference =
IrFunctionReferenceImpl( IrFunctionReferenceImpl(
startOffset, endOffset, type, startOffset, endOffset, type,
symbol, symbol, descriptor,
typeArguments, typeArguments,
origin origin
) )
@@ -30,6 +30,7 @@ class IrFunctionReferenceImpl(
endOffset: Int, endOffset: Int,
type: KotlinType, type: KotlinType,
override val symbol: IrFunctionSymbol, override val symbol: IrFunctionSymbol,
override val descriptor: FunctionDescriptor,
typeArguments: Map<TypeParameterDescriptor, KotlinType>?, typeArguments: Map<TypeParameterDescriptor, KotlinType>?,
origin: IrStatementOrigin? = null origin: IrStatementOrigin? = null
) : IrFunctionReference, ) : IrFunctionReference,
@@ -48,9 +49,7 @@ class IrFunctionReferenceImpl(
descriptor: FunctionDescriptor, descriptor: FunctionDescriptor,
typeArguments: Map<TypeParameterDescriptor, KotlinType>?, typeArguments: Map<TypeParameterDescriptor, KotlinType>?,
origin: IrStatementOrigin? = null origin: IrStatementOrigin? = null
) : this(startOffset, endOffset, type, createFunctionSymbol(descriptor), typeArguments, origin) ) : this(startOffset, endOffset, type, createFunctionSymbol(descriptor.original), descriptor, typeArguments, origin)
override val descriptor: FunctionDescriptor get() = symbol.descriptor
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R = override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitFunctionReference(this, data) visitor.visitFunctionReference(this, data)
@@ -409,7 +409,7 @@ open class DeepCopyIrTree : IrElementTransformerVoid() {
return IrFunctionReferenceImpl( return IrFunctionReferenceImpl(
expression.startOffset, expression.endOffset, expression.startOffset, expression.endOffset,
expression.type, expression.type,
newCallee, newCallee.original,
expression.transformTypeArguments(newCallee), expression.transformTypeArguments(newCallee),
mapStatementOrigin(expression.origin) mapStatementOrigin(expression.origin)
).transformValueArguments(expression) ).transformValueArguments(expression)
@@ -418,8 +418,8 @@ open class DeepCopyIrTree : IrElementTransformerVoid() {
override fun visitPropertyReference(expression: IrPropertyReference): IrExpression { override fun visitPropertyReference(expression: IrPropertyReference): IrExpression {
val newProperty = mapPropertyReference(expression.descriptor) val newProperty = mapPropertyReference(expression.descriptor)
val newFieldSymbol = if (newProperty.getter == null) IrFieldSymbolImpl(newProperty) else null val newFieldSymbol = if (newProperty.getter == null) IrFieldSymbolImpl(newProperty) else null
val newGetterSymbol = newProperty.getter?.let { IrSimpleFunctionSymbolImpl(it) } val newGetterSymbol = newProperty.getter?.let { IrSimpleFunctionSymbolImpl(it.original) }
val newSetterSymbol = newProperty.setter?.let { IrSimpleFunctionSymbolImpl(it) } val newSetterSymbol = newProperty.setter?.let { IrSimpleFunctionSymbolImpl(it.original) }
return IrPropertyReferenceImpl( return IrPropertyReferenceImpl(
expression.startOffset, expression.endOffset, expression.type, expression.startOffset, expression.endOffset, expression.type,
newProperty, newFieldSymbol, newGetterSymbol, newSetterSymbol, newProperty, newFieldSymbol, newGetterSymbol, newSetterSymbol,
@@ -432,8 +432,8 @@ open class DeepCopyIrTree : IrElementTransformerVoid() {
val newLocalDelegatedProperty = mapLocalPropertyReference(expression.descriptor) val newLocalDelegatedProperty = mapLocalPropertyReference(expression.descriptor)
val newDelegateDescriptor = mapVariableReference(expression.delegate.descriptor) val newDelegateDescriptor = mapVariableReference(expression.delegate.descriptor)
val newDelegateSymbol = IrVariableSymbolImpl(newDelegateDescriptor) val newDelegateSymbol = IrVariableSymbolImpl(newDelegateDescriptor)
val newGetterSymbol = newLocalDelegatedProperty.getter!!.let { IrSimpleFunctionSymbolImpl(it) } val newGetterSymbol = newLocalDelegatedProperty.getter!!.let { IrSimpleFunctionSymbolImpl(it.original) }
val newSetterSymbol = newLocalDelegatedProperty.setter?.let { IrSimpleFunctionSymbolImpl(it) } val newSetterSymbol = newLocalDelegatedProperty.setter?.let { IrSimpleFunctionSymbolImpl(it.original) }
return IrLocalDelegatedPropertyReferenceImpl( return IrLocalDelegatedPropertyReferenceImpl(
expression.startOffset, expression.endOffset, expression.type, expression.startOffset, expression.endOffset, expression.type,
newLocalDelegatedProperty, newLocalDelegatedProperty,
@@ -378,6 +378,7 @@ class DeepCopyIrTreeWithSymbols(private val symbolsRemapper: DeepCopySymbolsRema
expression.startOffset, expression.endOffset, expression.startOffset, expression.endOffset,
expression.type, expression.type,
symbolsRemapper.getReferencedFunction(expression.symbol), symbolsRemapper.getReferencedFunction(expression.symbol),
expression.descriptor, // TODO substitute referenced descriptor
expression.getTypeArgumentsMap(), expression.getTypeArgumentsMap(),
mapStatementOrigin(expression.origin) mapStatementOrigin(expression.origin)
).transformValueArguments(expression) ).transformValueArguments(expression)
@@ -0,0 +1,7 @@
class A<T> {
fun foo() {}
val bar = 42
}
val test1 = A<String>::foo
val test2 = A<String>::bar
@@ -0,0 +1,44 @@
FILE /callableRefToGenericMember.kt
CLASS CLASS A
$this: VALUE_PARAMETER this@A: A<T>
TYPE_PARAMETER <T>
CONSTRUCTOR public constructor A<T>()
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
INSTANCE_INITIALIZER_CALL classDescriptor='A'
FUN public final fun foo(): kotlin.Unit
$this: VALUE_PARAMETER this@A: A<T>
BLOCK_BODY
PROPERTY public final val bar: kotlin.Int = 42
FIELD PROPERTY_BACKING_FIELD public final val bar: kotlin.Int = 42
EXPRESSION_BODY
CONST Int type=kotlin.Int value='42'
FUN DEFAULT_PROPERTY_ACCESSOR public final fun <get-bar>(): kotlin.Int
$this: VALUE_PARAMETER this@A: A<T>
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-bar>(): Int'
GET_FIELD 'bar: Int' type=kotlin.Int origin=null
receiver: GET_VAR 'this@A: A<T>' type=A<T> origin=null
FUN FAKE_OVERRIDE public open override fun equals(other: kotlin.Any?): kotlin.Boolean
$this: VALUE_PARAMETER this@Any: Any
VALUE_PARAMETER value-parameter other: kotlin.Any?
FUN FAKE_OVERRIDE public open override fun hashCode(): kotlin.Int
$this: VALUE_PARAMETER this@Any: Any
FUN FAKE_OVERRIDE public open override fun toString(): kotlin.String
$this: VALUE_PARAMETER this@Any: Any
PROPERTY public val test1: kotlin.reflect.KFunction1<A<kotlin.String>, kotlin.Unit>
FIELD PROPERTY_BACKING_FIELD public val test1: kotlin.reflect.KFunction1<A<kotlin.String>, kotlin.Unit>
EXPRESSION_BODY
FUNCTION_REFERENCE 'foo(): Unit' type=kotlin.reflect.KFunction1<A<kotlin.String>, kotlin.Unit> origin=null
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test1>(): kotlin.reflect.KFunction1<A<kotlin.String>, kotlin.Unit>
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-test1>(): KFunction1<A<String>, Unit>'
GET_FIELD 'test1: KFunction1<A<String>, Unit>' type=kotlin.reflect.KFunction1<A<kotlin.String>, kotlin.Unit> origin=null
PROPERTY public val test2: kotlin.reflect.KProperty1<A<kotlin.String>, kotlin.Int>
FIELD PROPERTY_BACKING_FIELD public val test2: kotlin.reflect.KProperty1<A<kotlin.String>, kotlin.Int>
EXPRESSION_BODY
PROPERTY_REFERENCE 'bar: Int' field=null getter='<get-bar>(): Int' setter=null type=kotlin.reflect.KProperty1<A<kotlin.String>, kotlin.Int> origin=null
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test2>(): kotlin.reflect.KProperty1<A<kotlin.String>, kotlin.Int>
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-test2>(): KProperty1<A<String>, Int>'
GET_FIELD 'test2: KProperty1<A<String>, Int>' type=kotlin.reflect.KProperty1<A<kotlin.String>, kotlin.Int> origin=null
@@ -545,6 +545,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("callableRefToGenericMember.kt")
public void testCallableRefToGenericMember() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/callableRefToGenericMember.kt");
doTest(fileName);
}
@TestMetadata("calls.kt") @TestMetadata("calls.kt")
public void testCalls() throws Exception { public void testCalls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/calls.kt"); String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/calls.kt");