[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 = 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 = override fun visitBlock(block: FirBlock, data: IrElement): IrElement =
(data as? IrContainerExpression)?.insertImplicitCasts() ?: data (data as? IrContainerExpression)?.insertImplicitCasts() ?: data
@@ -196,13 +196,23 @@ class Fir2IrImplicitCastInserter(
override fun visitReturnExpression(returnExpression: FirReturnExpression, data: IrElement): IrElement { override fun visitReturnExpression(returnExpression: FirReturnExpression, data: IrElement): IrElement {
val irReturn = data as? IrReturn ?: return data val irReturn = data as? IrReturn ?: return data
val expectedType = returnExpression.target.labeledElement.returnTypeRef 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 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) { if (this is IrTypeOperatorCall) {
return this return this
} }
@@ -211,21 +221,21 @@ class Fir2IrImplicitCastInserter(
insertImplicitCasts() insertImplicitCasts()
} }
val valueType = valueTypeRef.fullyExpandedType(session) val expandedValueType = valueType.fullyExpandedType(session)
val expectedType = expectedTypeRef.fullyExpandedType(session) val expandedExpectedType = expectedType.fullyExpandedType(session)
return when { return when {
expectedType.isUnit -> { expandedExpectedType.isUnit -> {
coerceToUnitIfNeeded(this, irBuiltIns) coerceToUnitIfNeeded(this, irBuiltIns)
} }
valueType is ConeDynamicType -> { expandedValueType is ConeDynamicType -> {
if (expectedType !is ConeDynamicType && !expectedType.isNullableAny) { if (expandedExpectedType !is ConeDynamicType && !expandedExpectedType.isNullableAny) {
implicitCast(this, expectedType.toIrType(ConversionTypeOrigin.DEFAULT)) implicitCast(this, expandedExpectedType.toIrType(ConversionTypeOrigin.DEFAULT))
} else { } else {
this this
} }
} }
typeCanBeEnhancedOrFlexibleNullable(valueType) && !expectedType.acceptsNullValues() -> { typeCanBeEnhancedOrFlexibleNullable(expandedValueType) && !expandedExpectedType.acceptsNullValues() -> {
insertImplicitNotNullCastIfNeeded(expression) insertImplicitNotNullCastIfNeeded(expression)
} }
// TODO: coerceIntToAnotherIntegerType // TODO: coerceIntToAnotherIntegerType
@@ -491,7 +491,7 @@ class Fir2IrVisitor(
expectedType: ConeKotlinType, expectedType: ConeKotlinType,
) = ) =
with(implicitCastInserter) { 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) { 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 // here we should pass unsubstituted parameter type to properly infer if the original type accepts null or not
// to properly insert nullability check // to properly insert nullability check
irArgument = irArgument.cast(argument, argumentType, unsubstitutedParameterType) irArgument = irArgument.insertSpecialCast(argument, argumentType, unsubstitutedParameterType)
} }
} }
with(adapterGenerator) { with(adapterGenerator) {
@@ -1290,7 +1290,7 @@ class CallAndReferenceGenerator(
with(visitor.implicitCastInserter) { with(visitor.implicitCastInserter) {
val extensionReceiver = qualifiedAccess.extensionReceiver!! val extensionReceiver = qualifiedAccess.extensionReceiver!!
val substitutor = qualifiedAccess.buildSubstitutorByCalledCallable() val substitutor = qualifiedAccess.buildSubstitutorByCalledCallable()
it.cast( it.insertSpecialCast(
extensionReceiver, extensionReceiver,
extensionReceiver.resolvedType, extensionReceiver.resolvedType,
substitutor.substituteOrSelf(receiverType.coneType), substitutor.substituteOrSelf(receiverType.coneType),
@@ -279,7 +279,7 @@ internal class ClassMemberGenerator(
val irExpression = visitor.convertToIrExpression(initializerExpression, isDelegate = property.delegate != null) val irExpression = visitor.convertToIrExpression(initializerExpression, isDelegate = property.delegate != null)
if (property.delegate == null) { if (property.delegate == null) {
with(visitor.implicitCastInserter) { with(visitor.implicitCastInserter) {
irExpression.cast( irExpression.insertSpecialCast(
initializerExpression, initializerExpression,
initializerExpression.resolvedType, initializerExpression.resolvedType,
property.returnTypeRef.coneType property.returnTypeRef.coneType