IR: IrTypeOperatorCall.classifierSymbol can be computed from typeOperand

This commit is contained in:
Dmitry Petrov
2019-04-22 17:13:47 +03:00
parent 18c3778250
commit 7a44b0f951
23 changed files with 227 additions and 145 deletions
@@ -27,7 +27,7 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrExpressionWithCopy
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.psiUtil.endOffset
@@ -140,6 +140,19 @@ private fun StatementGenerator.generateThisOrSuperReceiver(receiver: ReceiverVal
return generateThisReceiver(ktReceiver.startOffsetSkippingComments, ktReceiver.endOffset, type, classDescriptor)
}
fun IrExpression.implicitCastTo(expectedType: IrType?): IrExpression {
if (expectedType == null) return this
return IrTypeOperatorCallImpl(
startOffset, endOffset,
expectedType,
IrTypeOperator.IMPLICIT_CAST,
expectedType
).also {
it.argument = this
}
}
fun StatementGenerator.generateBackingFieldReceiver(
startOffset: Int,
endOffset: Int,
@@ -167,8 +180,7 @@ fun StatementGenerator.generateCallReceiver(
assert(dispatchReceiver == null) {
"Call for member imported from object $calleeDescriptor has non-null dispatch receiver $dispatchReceiver"
}
dispatchReceiverValue =
generateReceiverForCalleeImportedFromObject(startOffset, endOffset, calleeDescriptor)
dispatchReceiverValue = generateReceiverForCalleeImportedFromObject(startOffset, endOffset, calleeDescriptor)
extensionReceiverValue = generateReceiverOrNull(ktDefaultElement, extensionReceiver)
}
is TypeAliasConstructorDescriptor -> {
@@ -291,15 +303,7 @@ fun StatementGenerator.castArgumentToFunctionalInterfaceForSamType(
val kotlinFunctionType = samConversion.getFunctionTypeForSAMClass(samClassDescriptor)
val irFunctionType = context.typeTranslator.translateType(kotlinFunctionType)
return IrTypeOperatorCallImpl(
irExpression.startOffset, irExpression.endOffset,
irFunctionType,
IrTypeOperator.IMPLICIT_CAST,
irFunctionType
).apply {
argument = irExpression
typeOperandClassifier = irFunctionType.classifierOrFail
}
return irExpression.implicitCastTo(irFunctionType)
}
fun Generator.getSuperQualifier(resolvedCall: ResolvedCall<*>): ClassDescriptor? {
@@ -429,7 +433,6 @@ fun StatementGenerator.generateSamConversionForValueArgumentsIfRequired(call: Ca
val originalArgument = call.irValueArgumentsByIndex[i] ?: continue
val targetType = underlyingParameterType.toIrType()
val targetClassifier = targetType.classifierOrFail
call.irValueArgumentsByIndex[i] =
IrTypeOperatorCallImpl(
@@ -437,7 +440,6 @@ fun StatementGenerator.generateSamConversionForValueArgumentsIfRequired(call: Ca
targetType,
IrTypeOperator.SAM_CONVERSION,
targetType,
targetClassifier,
castArgumentToFunctionalInterfaceForSamType(originalArgument, underlyingParameterType)
)
}
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.psi2ir.intermediate.*
import org.jetbrains.kotlin.psi2ir.unwrappedGetMethod
import org.jetbrains.kotlin.psi2ir.unwrappedSetMethod
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.PropertyImportedFromObject
import org.jetbrains.kotlin.resolve.calls.callUtil.isSafeCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic
@@ -301,6 +302,12 @@ class AssignmentGenerator(statementGenerator: StatementGenerator) : StatementGen
}
}
private fun PropertyDescriptor.unwrapPropertyDescriptor() =
when (this) {
is PropertyImportedFromObject -> callableFromObject
else -> this
}
private fun createPropertyLValue(
ktExpression: KtExpression,
descriptor: PropertyDescriptor,
@@ -311,8 +318,9 @@ class AssignmentGenerator(statementGenerator: StatementGenerator) : StatementGen
): PropertyLValueBase {
val superQualifierSymbol = superQualifier?.let { context.symbolTable.referenceClass(it) }
val getterDescriptor = descriptor.unwrappedGetMethod
val setterDescriptor = descriptor.unwrappedSetMethod
val unwrappedPropertyDescriptor = descriptor.unwrapPropertyDescriptor()
val getterDescriptor = unwrappedPropertyDescriptor.unwrappedGetMethod
val setterDescriptor = unwrappedPropertyDescriptor.unwrappedSetMethod
val getterSymbol = getterDescriptor?.let { context.symbolTable.referenceFunction(it.original) }
val setterSymbol = setterDescriptor?.let { context.symbolTable.referenceFunction(it.original) }
@@ -341,7 +349,7 @@ class AssignmentGenerator(statementGenerator: StatementGenerator) : StatementGen
context,
scope,
ktExpression.startOffsetSkippingComments, ktExpression.endOffset, origin,
context.symbolTable.referenceField(descriptor),
context.symbolTable.referenceField(unwrappedPropertyDescriptor.original),
propertyIrType,
propertyReceiver,
superQualifierSymbol
@@ -207,12 +207,11 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
private fun generateIsPatternCondition(irSubject: IrVariable, ktCondition: KtWhenConditionIsPattern): IrExpression {
val typeOperand = getOrFail(BindingContext.TYPE, ktCondition.typeReference)
val irTypeOperand = typeOperand.toIrType()
val typeSymbol = irTypeOperand.classifierOrNull ?: throw AssertionError("Not a classifier type: $typeOperand")
val irInstanceOf = IrTypeOperatorCallImpl(
ktCondition.startOffsetSkippingComments, ktCondition.endOffset,
context.irBuiltIns.booleanType,
IrTypeOperator.INSTANCEOF,
irTypeOperand, typeSymbol,
irTypeOperand,
irSubject.defaultLoad()
)
return if (ktCondition.isNegated)
@@ -75,7 +75,6 @@ class CallGenerator(statementGenerator: StatementGenerator) : StatementGenerator
targetType,
IrTypeOperator.SAM_CONVERSION,
targetType,
targetType.classifierOrFail,
statementGenerator.castArgumentToFunctionalInterfaceForSamType(
call.irValueArgumentsByIndex[0]!!,
targetKotlinType
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
import org.jetbrains.kotlin.ir.types.makeNotNull
import org.jetbrains.kotlin.ir.util.referenceClassifier
import org.jetbrains.kotlin.ir.util.referenceFunction
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
@@ -104,7 +103,6 @@ class OperatorExpressionGenerator(statementGenerator: StatementGenerator) : Stat
return IrTypeOperatorCallImpl(
expression.startOffsetSkippingComments, expression.endOffset, resultType.toIrType(), irOperator, rhsType.toIrType(),
context.symbolTable.referenceClassifier(rhsType.constructor.declarationDescriptor!!),
expression.left.genExpr()
)
}
@@ -117,7 +115,6 @@ class OperatorExpressionGenerator(statementGenerator: StatementGenerator) : Stat
return IrTypeOperatorCallImpl(
expression.startOffsetSkippingComments, expression.endOffset, context.irBuiltIns.booleanType, irOperator,
againstType.toIrType(),
context.symbolTable.referenceClassifier(againstType.constructor.declarationDescriptor!!),
expression.leftHandSide.genExpr()
)
}
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.psi2ir.transformations
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.*
@@ -32,119 +31,151 @@ import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.util.coerceToUnitIfNeeded
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.psi2ir.generators.GeneratorExtensions
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isDynamic
import org.jetbrains.kotlin.types.isNullabilityFlexible
import org.jetbrains.kotlin.types.typeUtil.isUnit
fun insertImplicitCasts(element: IrElement, context: GeneratorContext) {
element.transformChildren(
InsertImplicitCasts(context.builtIns, context.irBuiltIns, context.typeTranslator, context.extensions.samConversion),
InsertImplicitCasts(context.irBuiltIns, context.typeTranslator),
null
)
}
open class InsertImplicitCasts(
private val builtIns: KotlinBuiltIns,
private val irBuiltIns: IrBuiltIns,
private val typeTranslator: TypeTranslator,
private val samConversion: GeneratorExtensions.SamConversion
private val typeTranslator: TypeTranslator
) : IrElementTransformerVoid() {
// override fun visitCallableReference(expression: IrCallableReference): IrExpression =
// expression.transformPostfix {
// transformReceiverArguments()
// }
private fun getDeclarationSideTypeParameters(declaration: IrFunction): List<IrTypeParameterSymbol> {
return run {
declaration.typeParameters + if (declaration is IrConstructor) declaration.parentAsClass.typeParameters else emptyList()
}.map { it.symbol }
private fun getDeclarationTypeParameters(declaration: IrFunction): List<IrTypeParameterSymbol> {
return (declaration.typeParameters + if (declaration is IrConstructor) declaration.parentAsClass.typeParameters else emptyList())
.map { it.symbol }
}
private fun getTypeSideTypeParameters(declaration: IrFunction): List<IrTypeParameterSymbol> {
return declaration.dispatchReceiverParameter?.run {
extractTypeParameters(declaration.parentAsClass).map { it.symbol }
} ?: emptyList()
private fun getDispatchReceiverTypeParameters(declaration: IrDeclaration): List<IrTypeParameterSymbol> {
val classWithTypeParameters =
if (declaration.isNonStaticMemberDeclaration())
declaration.parentAsClass
else
return emptyList()
return extractTypeParameters(classWithTypeParameters).map { it.symbol }
}
private fun getTypeArguments(expression: IrMemberAccessExpression, declarationTypeParameters: List<IrTypeParameterSymbol>): List<IrTypeArgument> {
private fun IrDeclaration.isNonStaticMemberDeclaration() =
this is IrFunction && dispatchReceiverParameter != null ||
this is IrField && !isStatic
val expressionTypeArguments =
declarationTypeParameters.map { p -> makeTypeProjection(expression.getTypeArgument(p.owner.index)!!, p.owner.variance) }
val receiverTypeArguments = expression.dispatchReceiver?.type?.let { (it as? IrSimpleType)?.arguments } ?: emptyList()
return expressionTypeArguments + receiverTypeArguments
}
private fun IrType.substitute(typeParameters: List<IrTypeParameterSymbol>, typeArguments: List<IrTypeArgument>): IrType {
return IrTypeSubstitutor(typeParameters.distinct(), typeArguments, irBuiltIns).substitute(this)
}
override fun visitFunctionReference(expression: IrFunctionReference): IrExpression {
return expression.transformPostfix {
val declaration = symbol.owner
val dTypeParameters = getDeclarationSideTypeParameters(declaration)
val cTypeParameters = getTypeSideTypeParameters(declaration)
val typeArguments = getTypeArguments(expression, dTypeParameters)
dispatchReceiver = dispatchReceiver?.cast(declaration.dispatchReceiverParameter?.type?.substitute(dTypeParameters + cTypeParameters, typeArguments))
extensionReceiver = extensionReceiver?.cast(declaration.extensionReceiverParameter?.type?.substitute(dTypeParameters + cTypeParameters, typeArguments))
private fun getTypeArguments(
expression: IrExpression,
declarationTypeParameters: List<IrTypeParameterSymbol>
): List<IrTypeArgument> =
when (expression) {
is IrMemberAccessExpression -> {
val expressionTypeArguments = declarationTypeParameters.map { p ->
makeTypeProjection(expression.getTypeArgument(p.owner.index)!!, p.owner.variance)
}
val receiverTypeArguments = expression.dispatchReceiver.getTypeArgumentsForReceiver()
expressionTypeArguments + receiverTypeArguments
}
is IrFieldAccessExpression -> {
expression.receiver.getTypeArgumentsForReceiver()
}
else -> {
throw AssertionError("Unexpected expression: ${expression.render()}")
}
}
private fun IrExpression?.getTypeArgumentsForReceiver(): List<IrTypeArgument> {
if (this == null) return emptyList()
val expressionType = type as? IrSimpleType ?: return emptyList()
return expressionType.arguments
}
override fun visitPropertyReference(expression: IrPropertyReference): IrExpression {
return expression.transformPostfix {
val dispatchReceiver = expression.run { getter?.owner?.dispatchReceiverParameter ?: setter?.owner?.dispatchReceiverParameter }
val extensionReceiver = expression.run { getter?.owner?.extensionReceiverParameter ?: setter?.owner?.extensionReceiverParameter }
private fun IrType.substitute(typeParameters: List<IrTypeParameterSymbol>, typeArguments: List<IrTypeArgument>): IrType =
IrTypeSubstitutor(typeParameters.distinct(), typeArguments, irBuiltIns).substitute(this)
override fun visitFunctionReference(expression: IrFunctionReference): IrExpression =
expression.transformPostfix {
val declaration = symbol.owner
val declarationTypeParameters = getDeclarationTypeParameters(declaration)
val dispatchReceiverTypeParameters = getDispatchReceiverTypeParameters(declaration)
val typeParameters = declarationTypeParameters + dispatchReceiverTypeParameters
val typeArguments = getTypeArguments(expression, declarationTypeParameters)
dispatchReceiver = dispatchReceiver?.cast(
declaration.dispatchReceiverParameter?.type?.substitute(typeParameters, typeArguments)
)
extensionReceiver = extensionReceiver?.cast(
declaration.extensionReceiverParameter?.type?.substitute(typeParameters, typeArguments)
)
}
override fun visitPropertyReference(expression: IrPropertyReference): IrExpression =
expression.transformPostfix {
val dispatchReceiver = expression.run {
getter?.owner?.dispatchReceiverParameter
?: setter?.owner?.dispatchReceiverParameter
}
val extensionReceiver = expression.run {
getter?.owner?.extensionReceiverParameter
?: setter?.owner?.extensionReceiverParameter
}
this.dispatchReceiver = this.dispatchReceiver?.cast(dispatchReceiver?.type)
this.extensionReceiver = this.extensionReceiver?.cast(extensionReceiver?.type)
}
}
override fun visitLocalDelegatedPropertyReference(expression: IrLocalDelegatedPropertyReference): IrExpression {
return expression.transformPostfix {
override fun visitLocalDelegatedPropertyReference(expression: IrLocalDelegatedPropertyReference): IrExpression =
expression.transformPostfix {
val declaration = expression.getter.owner
val dTypeParameters = getDeclarationSideTypeParameters(declaration)
val cTypeParameters = getTypeSideTypeParameters(declaration)
val typeArguments = getTypeArguments(expression, dTypeParameters)
val declarationTypeParameters = getDeclarationTypeParameters(declaration)
val receiverTypeParameters = getDispatchReceiverTypeParameters(declaration)
val typeParameters = declarationTypeParameters + receiverTypeParameters
val typeArguments = getTypeArguments(expression, declarationTypeParameters)
dispatchReceiver = dispatchReceiver?.cast(
declaration.dispatchReceiverParameter?.type?.substitute(dTypeParameters + cTypeParameters, typeArguments)
declaration.dispatchReceiverParameter?.run {
type.substitute(typeParameters, typeArguments)
}
)
extensionReceiver = extensionReceiver?.cast(
declaration.extensionReceiverParameter?.type?.substitute(dTypeParameters + cTypeParameters, typeArguments)
declaration.extensionReceiverParameter?.run {
type.substitute(typeParameters, typeArguments)
}
)
}
}
private fun IrMemberAccessExpression.transformReceiverArguments() {
val declaration = (this as IrFunctionAccessExpression).symbol.owner
val dTypeParameters = getDeclarationSideTypeParameters(declaration)
val cTypeParameters = getTypeSideTypeParameters(declaration)
val typeArguments = getTypeArguments(this, dTypeParameters)
val declarationTypeParameters = getDeclarationTypeParameters(declaration)
val receiverTypeParameters = getDispatchReceiverTypeParameters(declaration)
val typeParameters = declarationTypeParameters + receiverTypeParameters
val typeArguments = getTypeArguments(this, declarationTypeParameters)
dispatchReceiver = dispatchReceiver?.cast(
declaration.dispatchReceiverParameter?.type?.substitute(dTypeParameters + cTypeParameters, typeArguments)
declaration.dispatchReceiverParameter?.run {
type.substitute(typeParameters, typeArguments)
}
)
extensionReceiver = extensionReceiver?.cast(
declaration.extensionReceiverParameter?.type?.substitute(dTypeParameters + cTypeParameters, typeArguments)
declaration.extensionReceiverParameter?.run {
type.substitute(typeParameters, typeArguments)
}
)
}
override fun visitMemberAccess(expression: IrMemberAccessExpression): IrExpression =
with (expression as IrFunctionAccessExpression) {
with(expression as IrFunctionAccessExpression) {
val declaration = symbol.owner
val dTypeParameters = getDeclarationSideTypeParameters(declaration)
val cTypeParameters = getTypeSideTypeParameters(declaration)
val typeArguments = getTypeArguments(expression, dTypeParameters)
val declarationTypeParameters = getDeclarationTypeParameters(declaration)
val receiverTypeParameters = getDispatchReceiverTypeParameters(declaration)
val typeArguments = getTypeArguments(expression, declarationTypeParameters)
val typeParameters = declarationTypeParameters + receiverTypeParameters
transformPostfix {
transformReceiverArguments()
for (index in declaration.valueParameters.indices) {
val argument = getValueArgument(index) ?: continue
val parameterType = declaration.valueParameters[index].type.substitute(dTypeParameters + cTypeParameters, typeArguments)
val parameterType = declaration.valueParameters[index].type.substitute(typeParameters, typeArguments)
putValueArgument(index, argument.cast(parameterType))
}
}
@@ -189,9 +220,29 @@ open class InsertImplicitCasts(
value = value.cast(expression.symbol.owner.type)
}
override fun visitGetField(expression: IrGetField): IrExpression =
expression.transformPostfix {
val declaration = expression.symbol.owner
val receiverTypeParameters = getDispatchReceiverTypeParameters(declaration)
val typeArguments = getTypeArguments(expression, receiverTypeParameters)
receiver = receiver?.cast(
declaration.parentAsClass.thisReceiver?.run {
type.substitute(receiverTypeParameters, typeArguments)
}
)
}
override fun visitSetField(expression: IrSetField): IrExpression =
expression.transformPostfix {
value = value.cast(expression.symbol.owner.type)
val declaration = expression.symbol.owner
val receiverTypeParameters = getDispatchReceiverTypeParameters(declaration)
val typeArguments = getTypeArguments(expression, receiverTypeParameters)
receiver = receiver?.cast(
declaration.parentAsClass.thisReceiver?.run {
type.substitute(receiverTypeParameters, typeArguments)
}
)
value = value.cast(expression.symbol.owner.type.substitute(receiverTypeParameters, typeArguments))
}
override fun visitVariable(declaration: IrVariable): IrVariable =
@@ -248,13 +299,15 @@ open class InsertImplicitCasts(
finallyExpression = finallyExpression?.coerceToUnit()
}
override fun visitTypeOperator(expression: IrTypeOperatorCall): IrExpression =
when (expression.operator) {
override fun visitTypeOperator(expression: IrTypeOperatorCall): IrExpression {
expression.transformChildren()
return when (expression.operator) {
IrTypeOperator.IMPLICIT_CAST ->
expression.argument.cast(expression.typeOperand)
else ->
super.visitTypeOperator(expression)
}
}
override fun visitVararg(expression: IrVararg): IrExpression =
expression.transformPostfix {
@@ -333,7 +386,7 @@ open class InsertImplicitCasts(
endOffset,
targetType,
typeOperator,
targetType, targetType.classifierOrFail,
targetType,
this
)
}
@@ -342,19 +395,6 @@ open class InsertImplicitCasts(
return coerceToUnitIfNeeded(type, irBuiltIns)
}
protected fun getKotlinType(irExpression: IrExpression) =
irExpression.type.originalKotlinType!!
private fun KotlinType.isBuiltInIntegerType(): Boolean =
KotlinBuiltIns.isByte(this) ||
KotlinBuiltIns.isShort(this) ||
KotlinBuiltIns.isInt(this) ||
KotlinBuiltIns.isLong(this) ||
KotlinBuiltIns.isUByte(this) ||
KotlinBuiltIns.isUShort(this) ||
KotlinBuiltIns.isUInt(this) ||
KotlinBuiltIns.isULong(this)
private fun IrType.isBuiltInIntegerType(): Boolean =
isByte() || isShort() || isInt() || isLong() ||
isUByte() || isUShort() || isUInt() || isULong()