[FIR2IR] Rename Fir2IrImplicitCastInserter.cast method

`cast` name is too generic and doesn't explain the actual
  meaning of this function
This commit is contained in:
Dmitriy Novozhilov
2023-11-08 12:46:06 +02:00
committed by Space Team
parent 8578a0bf6a
commit 10914eb23f
4 changed files with 24 additions and 14 deletions
@@ -188,7 +188,7 @@ class Fir2IrImplicitCastInserter(
}
override fun visitThrowExpression(throwExpression: FirThrowExpression, data: IrElement): IrElement =
(data as IrThrow).cast(throwExpression, throwExpression.exception.resolvedType, throwExpression.resolvedType)
(data as IrThrow).insertSpecialCast(throwExpression, throwExpression.exception.resolvedType, throwExpression.resolvedType)
override fun visitBlock(block: FirBlock, data: IrElement): IrElement =
(data as? IrContainerExpression)?.insertImplicitCasts() ?: data
@@ -196,13 +196,23 @@ class Fir2IrImplicitCastInserter(
override fun visitReturnExpression(returnExpression: FirReturnExpression, data: IrElement): IrElement {
val irReturn = data as? IrReturn ?: return data
val expectedType = returnExpression.target.labeledElement.returnTypeRef
irReturn.value = irReturn.value.cast(returnExpression.result, returnExpression.result.resolvedType, expectedType.coneType)
irReturn.value = irReturn.value.insertSpecialCast(returnExpression.result, returnExpression.result.resolvedType, expectedType.coneType)
return data
}
// ==================================================================================
internal fun IrExpression.cast(expression: FirExpression, valueTypeRef: ConeKotlinType, expectedTypeRef: ConeKotlinType): IrExpression {
/**
* This functions processes the following casts:
* - coercion to Unit
* - nullability casts based on nullability annotations
* - casts for dynamic types
*/
internal fun IrExpression.insertSpecialCast(
expression: FirExpression,
valueType: ConeKotlinType,
expectedType: ConeKotlinType,
): IrExpression {
if (this is IrTypeOperatorCall) {
return this
}
@@ -211,21 +221,21 @@ class Fir2IrImplicitCastInserter(
insertImplicitCasts()
}
val valueType = valueTypeRef.fullyExpandedType(session)
val expectedType = expectedTypeRef.fullyExpandedType(session)
val expandedValueType = valueType.fullyExpandedType(session)
val expandedExpectedType = expectedType.fullyExpandedType(session)
return when {
expectedType.isUnit -> {
expandedExpectedType.isUnit -> {
coerceToUnitIfNeeded(this, irBuiltIns)
}
valueType is ConeDynamicType -> {
if (expectedType !is ConeDynamicType && !expectedType.isNullableAny) {
implicitCast(this, expectedType.toIrType(ConversionTypeOrigin.DEFAULT))
expandedValueType is ConeDynamicType -> {
if (expandedExpectedType !is ConeDynamicType && !expandedExpectedType.isNullableAny) {
implicitCast(this, expandedExpectedType.toIrType(ConversionTypeOrigin.DEFAULT))
} else {
this
}
}
typeCanBeEnhancedOrFlexibleNullable(valueType) && !expectedType.acceptsNullValues() -> {
typeCanBeEnhancedOrFlexibleNullable(expandedValueType) && !expandedExpectedType.acceptsNullValues() -> {
insertImplicitNotNullCastIfNeeded(expression)
}
// TODO: coerceIntToAnotherIntegerType
@@ -491,7 +491,7 @@ class Fir2IrVisitor(
expectedType: ConeKotlinType,
) =
with(implicitCastInserter) {
this@insertImplicitCast.cast(baseExpression, valueType, expectedType)
this@insertImplicitCast.insertSpecialCast(baseExpression, valueType, expectedType)
}
override fun visitProperty(property: FirProperty, data: Any?): IrElement = whileAnalysing(session, property) {
@@ -1040,7 +1040,7 @@ class CallAndReferenceGenerator(
}
// here we should pass unsubstituted parameter type to properly infer if the original type accepts null or not
// to properly insert nullability check
irArgument = irArgument.cast(argument, argumentType, unsubstitutedParameterType)
irArgument = irArgument.insertSpecialCast(argument, argumentType, unsubstitutedParameterType)
}
}
with(adapterGenerator) {
@@ -1290,7 +1290,7 @@ class CallAndReferenceGenerator(
with(visitor.implicitCastInserter) {
val extensionReceiver = qualifiedAccess.extensionReceiver!!
val substitutor = qualifiedAccess.buildSubstitutorByCalledCallable()
it.cast(
it.insertSpecialCast(
extensionReceiver,
extensionReceiver.resolvedType,
substitutor.substituteOrSelf(receiverType.coneType),
@@ -279,7 +279,7 @@ internal class ClassMemberGenerator(
val irExpression = visitor.convertToIrExpression(initializerExpression, isDelegate = property.delegate != null)
if (property.delegate == null) {
with(visitor.implicitCastInserter) {
irExpression.cast(
irExpression.insertSpecialCast(
initializerExpression,
initializerExpression.resolvedType,
property.returnTypeRef.coneType