FIR2IR: simplify ConversionTypeOrigin/Context

This commit is contained in:
Mikhail Glukhikh
2023-06-26 12:48:58 +03:00
committed by Space Team
parent 2c0c9e5b5c
commit 0e6a7bbe82
12 changed files with 77 additions and 90 deletions
@@ -61,7 +61,6 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffsetSkippingComments
import org.jetbrains.kotlin.types.ConstantValueKind import org.jetbrains.kotlin.types.ConstantValueKind
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.utils.addToStdlib.runIf
fun AbstractKtSourceElement?.startOffsetSkippingComments(): Int? { fun AbstractKtSourceElement?.startOffsetSkippingComments(): Int? {
return when (this) { return when (this) {
@@ -110,37 +109,26 @@ internal fun <T : IrElement> FirStatement.convertWithOffsets(
internal fun createErrorType(): IrErrorType = IrErrorTypeImpl(null, emptyList(), Variance.INVARIANT) internal fun createErrorType(): IrErrorType = IrErrorTypeImpl(null, emptyList(), Variance.INVARIANT)
internal enum class ConversionTypeOrigin { enum class ConversionTypeOrigin(val forSetter: Boolean) {
DEFAULT, DEFAULT(forSetter = false),
SETTER SETTER(forSetter = true);
}
class ConversionTypeContext private constructor(internal val origin: ConversionTypeOrigin) {
companion object {
internal val DEFAULT = ConversionTypeContext(
origin = ConversionTypeOrigin.DEFAULT
)
internal val IN_SETTER = ConversionTypeContext(
origin = ConversionTypeOrigin.SETTER
)
}
} }
context(Fir2IrComponents) context(Fir2IrComponents)
fun FirClassifierSymbol<*>.toSymbol( fun FirClassifierSymbol<*>.toSymbol(
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT,
handleAnnotations: ((List<FirAnnotation>) -> Unit)? = null handleAnnotations: ((List<FirAnnotation>) -> Unit)? = null
): IrClassifierSymbol { ): IrClassifierSymbol {
return when (this) { return when (this) {
is FirTypeParameterSymbol -> { is FirTypeParameterSymbol -> {
classifierStorage.getIrTypeParameterSymbol(this, typeContext) classifierStorage.getIrTypeParameterSymbol(this, typeOrigin)
} }
is FirTypeAliasSymbol -> { is FirTypeAliasSymbol -> {
handleAnnotations?.invoke(fir.expandedTypeRef.annotations) handleAnnotations?.invoke(fir.expandedTypeRef.annotations)
val coneClassLikeType = fir.expandedTypeRef.coneType as ConeClassLikeType val coneClassLikeType = fir.expandedTypeRef.coneType as ConeClassLikeType
coneClassLikeType.lookupTag.toSymbol(session) coneClassLikeType.lookupTag.toSymbol(session)
?.toSymbol(typeContext, handleAnnotations) ?.toSymbol(typeOrigin, handleAnnotations)
?: classifierStorage.getIrClassSymbolForNotFoundClass(coneClassLikeType.lookupTag) ?: classifierStorage.getIrClassSymbolForNotFoundClass(coneClassLikeType.lookupTag)
} }
@@ -63,7 +63,7 @@ class Fir2IrBuiltIns(
} }
private val extensionFunctionTypeAnnotationSymbol by lazy { private val extensionFunctionTypeAnnotationSymbol by lazy {
extensionFunctionTypeAnnotationFirSymbol?.toSymbol(ConversionTypeContext.DEFAULT) as? IrClassSymbol extensionFunctionTypeAnnotationFirSymbol?.toSymbol(ConversionTypeOrigin.DEFAULT) as? IrClassSymbol
} }
internal fun extensionFunctionTypeAnnotationConstructorCall(): IrConstructorCall? = internal fun extensionFunctionTypeAnnotationConstructorCall(): IrConstructorCall? =
@@ -66,8 +66,8 @@ class Fir2IrClassifierStorage(
commonMemberStorage.localClassCache commonMemberStorage.localClassCache
) )
private fun FirTypeRef.toIrType(typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT): IrType = private fun FirTypeRef.toIrType(typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT): IrType =
with(typeConverter) { toIrType(typeContext) } with(typeConverter) { toIrType(typeOrigin) }
fun preCacheBuiltinClasses() { fun preCacheBuiltinClasses() {
for ((classId, irBuiltinSymbol) in typeConverter.classIdToSymbolMap) { for ((classId, irBuiltinSymbol) in typeConverter.classIdToSymbolMap) {
@@ -93,7 +93,7 @@ class Fir2IrClassifierStorage(
private fun IrClass.setThisReceiver(typeParameters: List<FirTypeParameterRef>) { private fun IrClass.setThisReceiver(typeParameters: List<FirTypeParameterRef>) {
symbolTable.enterScope(this) symbolTable.enterScope(this)
val typeArguments = typeParameters.map { val typeArguments = typeParameters.map {
IrSimpleTypeImpl(getIrTypeParameterSymbol(it.symbol, ConversionTypeContext.DEFAULT), false, emptyList(), emptyList()) IrSimpleTypeImpl(getIrTypeParameterSymbol(it.symbol, ConversionTypeOrigin.DEFAULT), false, emptyList(), emptyList())
} }
thisReceiver = declareThisReceiverParameter( thisReceiver = declareThisReceiverParameter(
thisType = IrSimpleTypeImpl(symbol, false, typeArguments, emptyList()), thisType = IrSimpleTypeImpl(symbol, false, typeArguments, emptyList()),
@@ -108,7 +108,7 @@ class Fir2IrClassifierStorage(
getCachedIrTypeParameter(original) getCachedIrTypeParameter(original)
?: createIrTypeParameterWithoutBounds(original, index, irOwnerSymbol) ?: createIrTypeParameterWithoutBounds(original, index, irOwnerSymbol)
if (owner is FirProperty && owner.isVar) { if (owner is FirProperty && owner.isVar) {
val context = ConversionTypeContext.IN_SETTER val context = ConversionTypeOrigin.SETTER
getCachedIrTypeParameter(original, context) getCachedIrTypeParameter(original, context)
?: createIrTypeParameterWithoutBounds(original, index, irOwnerSymbol, context) ?: createIrTypeParameterWithoutBounds(original, index, irOwnerSymbol, context)
} }
@@ -117,14 +117,14 @@ class Fir2IrClassifierStorage(
internal fun IrTypeParametersContainer.setTypeParameters( internal fun IrTypeParametersContainer.setTypeParameters(
owner: FirTypeParameterRefsOwner, owner: FirTypeParameterRefsOwner,
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT
) { ) {
typeParameters = owner.typeParameters.mapIndexedNotNull { index, typeParameter -> typeParameters = owner.typeParameters.mapIndexedNotNull { index, typeParameter ->
if (typeParameter !is FirTypeParameter) return@mapIndexedNotNull null if (typeParameter !is FirTypeParameter) return@mapIndexedNotNull null
getIrTypeParameter(typeParameter, index, symbol, typeContext).apply { getIrTypeParameter(typeParameter, index, symbol, typeOrigin).apply {
parent = this@setTypeParameters parent = this@setTypeParameters
if (superTypes.isEmpty()) { if (superTypes.isEmpty()) {
superTypes = typeParameter.bounds.map { it.toIrType(typeContext) } superTypes = typeParameter.bounds.map { it.toIrType(typeOrigin) }
} }
} }
} }
@@ -419,7 +419,7 @@ class Fir2IrClassifierStorage(
typeParameter: FirTypeParameter, typeParameter: FirTypeParameter,
index: Int, index: Int,
ownerSymbol: IrSymbol, ownerSymbol: IrSymbol,
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT,
): IrTypeParameter { ): IrTypeParameter {
require(index >= 0) require(index >= 0)
val origin = typeParameter.computeIrOrigin() val origin = typeParameter.computeIrOrigin()
@@ -476,7 +476,7 @@ class Fir2IrClassifierStorage(
} }
// Cache the type parameter BEFORE processing its bounds/supertypes, to properly handle recursive type bounds. // Cache the type parameter BEFORE processing its bounds/supertypes, to properly handle recursive type bounds.
if (typeContext.origin == ConversionTypeOrigin.SETTER) { if (typeOrigin.forSetter) {
typeParameterCacheForSetter[typeParameter] = irTypeParameter typeParameterCacheForSetter[typeParameter] = irTypeParameter
} else { } else {
typeParameterCache[typeParameter] = irTypeParameter typeParameterCache[typeParameter] = irTypeParameter
@@ -487,9 +487,9 @@ class Fir2IrClassifierStorage(
internal fun getCachedIrTypeParameter( internal fun getCachedIrTypeParameter(
typeParameter: FirTypeParameter, typeParameter: FirTypeParameter,
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT
): IrTypeParameter? { ): IrTypeParameter? {
return if (typeContext.origin == ConversionTypeOrigin.SETTER) return if (typeOrigin.forSetter)
typeParameterCacheForSetter[typeParameter] typeParameterCacheForSetter[typeParameter]
else else
typeParameterCache[typeParameter] typeParameterCache[typeParameter]
@@ -499,11 +499,11 @@ class Fir2IrClassifierStorage(
typeParameter: FirTypeParameter, typeParameter: FirTypeParameter,
index: Int, index: Int,
ownerSymbol: IrSymbol, ownerSymbol: IrSymbol,
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT
): IrTypeParameter { ): IrTypeParameter {
getCachedIrTypeParameter(typeParameter, typeContext)?.let { return it } getCachedIrTypeParameter(typeParameter, typeOrigin)?.let { return it }
return typeParameter.run { return typeParameter.run {
val irTypeParameter = createIrTypeParameterWithoutBounds(typeParameter, index, ownerSymbol, typeContext) val irTypeParameter = createIrTypeParameterWithoutBounds(typeParameter, index, ownerSymbol, typeOrigin)
irTypeParameter.superTypes = bounds.map { it.toIrType() } irTypeParameter.superTypes = bounds.map { it.toIrType() }
irTypeParameter irTypeParameter
} }
@@ -664,10 +664,10 @@ class Fir2IrClassifierStorage(
fun getIrTypeParameterSymbol( fun getIrTypeParameterSymbol(
firTypeParameterSymbol: FirTypeParameterSymbol, firTypeParameterSymbol: FirTypeParameterSymbol,
typeContext: ConversionTypeContext typeOrigin: ConversionTypeOrigin
): IrTypeParameterSymbol { ): IrTypeParameterSymbol {
val firTypeParameter = firTypeParameterSymbol.fir val firTypeParameter = firTypeParameterSymbol.fir
return getCachedIrTypeParameter(firTypeParameter, typeContext)?.symbol return getCachedIrTypeParameter(firTypeParameter, typeOrigin)?.symbol
// We can try to use default cache because setter can use parent type parameters // We can try to use default cache because setter can use parent type parameters
?: typeParameterCache[firTypeParameter]?.symbol ?: typeParameterCache[firTypeParameter]?.symbol
?: error("Cannot find cached type parameter by FIR symbol: ${firTypeParameterSymbol.name} of the owner: ${firTypeParameter.containingDeclarationSymbol}") ?: error("Cannot find cached type parameter by FIR symbol: ${firTypeParameterSymbol.name} of the owner: ${firTypeParameter.containingDeclarationSymbol}")
@@ -135,8 +135,8 @@ class Fir2IrConversionScope {
fun parent(): IrDeclarationParent? = parentStack.lastOrNull() fun parent(): IrDeclarationParent? = parentStack.lastOrNull()
fun defaultConversionTypeContext(): ConversionTypeContext = fun defaultConversionTypeOrigin(): ConversionTypeOrigin =
if ((parent() as? IrFunction)?.isSetter == true) ConversionTypeContext.IN_SETTER else ConversionTypeContext.DEFAULT if ((parent() as? IrFunction)?.isSetter == true) ConversionTypeOrigin.SETTER else ConversionTypeOrigin.DEFAULT
fun dispatchReceiverParameter(irClass: IrClass): IrValueParameter? { fun dispatchReceiverParameter(irClass: IrClass): IrValueParameter? {
for (function in functionStack.asReversed()) { for (function in functionStack.asReversed()) {
@@ -53,7 +53,6 @@ import org.jetbrains.kotlin.ir.expressions.IrSyntheticBodyKind
import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl
import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.impl.* import org.jetbrains.kotlin.ir.symbols.impl.*
import org.jetbrains.kotlin.ir.types.IrErrorType
import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
@@ -240,11 +239,11 @@ class Fir2IrDeclarationStorage(
symbolTable.leaveScope(declaration) symbolTable.leaveScope(declaration)
} }
private fun FirTypeRef.toIrType(typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT): IrType = private fun FirTypeRef.toIrType(typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT): IrType =
with(typeConverter) { toIrType(typeContext) } with(typeConverter) { toIrType(typeOrigin) }
private fun ConeKotlinType.toIrType(typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT): IrType = private fun ConeKotlinType.toIrType(typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT): IrType =
with(typeConverter) { toIrType(typeContext) } with(typeConverter) { toIrType(typeOrigin) }
private fun getIrExternalOrBuiltInsPackageFragment(fqName: FqName, firOrigin: FirDeclarationOrigin): IrExternalPackageFragment { private fun getIrExternalOrBuiltInsPackageFragment(fqName: FqName, firOrigin: FirDeclarationOrigin): IrExternalPackageFragment {
val isBuiltIn = fqName in BUILT_INS_PACKAGE_FQ_NAMES val isBuiltIn = fqName in BUILT_INS_PACKAGE_FQ_NAMES
@@ -365,10 +364,10 @@ class Fir2IrDeclarationStorage(
setTypeParameters(function) setTypeParameters(function)
} }
} }
val typeContext = if (forSetter) ConversionTypeContext.IN_SETTER else ConversionTypeContext.DEFAULT val typeOrigin = if (forSetter) ConversionTypeOrigin.SETTER else ConversionTypeOrigin.DEFAULT
if (function is FirDefaultPropertySetter) { if (function is FirDefaultPropertySetter) {
val valueParameter = function.valueParameters.first() val valueParameter = function.valueParameters.first()
val type = valueParameter.returnTypeRef.toIrType(ConversionTypeContext.IN_SETTER) val type = valueParameter.returnTypeRef.toIrType(ConversionTypeOrigin.SETTER)
declareDefaultSetterParameter(type, valueParameter) declareDefaultSetterParameter(type, valueParameter)
} else if (function != null) { } else if (function != null) {
val contextReceivers = function.contextReceiversForFunctionOrContainingProperty() val contextReceivers = function.contextReceiversForFunctionOrContainingProperty()
@@ -381,7 +380,7 @@ class Fir2IrDeclarationStorage(
createIrParameter( createIrParameter(
valueParameter, index + contextReceiverParametersCount, valueParameter, index + contextReceiverParametersCount,
useStubForDefaultValueStub = function !is FirConstructor || containingClass?.name != Name.identifier("Enum"), useStubForDefaultValueStub = function !is FirConstructor || containingClass?.name != Name.identifier("Enum"),
typeContext, typeOrigin,
skipDefaultParameter = isFakeOverride || origin == IrDeclarationOrigin.DELEGATED_MEMBER skipDefaultParameter = isFakeOverride || origin == IrDeclarationOrigin.DELEGATED_MEMBER
).apply { ).apply {
this.parent = parent this.parent = parent
@@ -402,7 +401,7 @@ class Fir2IrDeclarationStorage(
Name.identifier("\$this\$$suffix") Name.identifier("\$this\$$suffix")
} ?: SpecialNames.THIS } ?: SpecialNames.THIS
declareThisReceiverParameter( declareThisReceiverParameter(
thisType = receiver.typeRef.toIrType(typeContext), thisType = receiver.typeRef.toIrType(typeOrigin),
thisOrigin = thisOrigin, thisOrigin = thisOrigin,
startOffset = startOffset, startOffset = startOffset,
endOffset = endOffset, endOffset = endOffset,
@@ -776,7 +775,7 @@ class Fir2IrDeclarationStorage(
} }
with(classifierStorage) { with(classifierStorage) {
setTypeParameters( setTypeParameters(
property, if (isSetter) ConversionTypeContext.IN_SETTER else ConversionTypeContext.DEFAULT property, if (isSetter) ConversionTypeOrigin.SETTER else ConversionTypeOrigin.DEFAULT
) )
} }
// NB: we should enter accessor' scope before declaring its parameters // NB: we should enter accessor' scope before declaring its parameters
@@ -784,7 +783,7 @@ class Fir2IrDeclarationStorage(
enterScope(this) enterScope(this)
if (propertyAccessor == null && isSetter) { if (propertyAccessor == null && isSetter) {
declareDefaultSetterParameter( declareDefaultSetterParameter(
property.returnTypeRef.toIrType(ConversionTypeContext.IN_SETTER), property.returnTypeRef.toIrType(ConversionTypeOrigin.SETTER),
firValueParameter = null firValueParameter = null
) )
} }
@@ -1207,11 +1206,11 @@ class Fir2IrDeclarationStorage(
valueParameter: FirValueParameter, valueParameter: FirValueParameter,
index: Int = UNDEFINED_PARAMETER_INDEX, index: Int = UNDEFINED_PARAMETER_INDEX,
useStubForDefaultValueStub: Boolean = true, useStubForDefaultValueStub: Boolean = true,
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT,
skipDefaultParameter: Boolean = false, skipDefaultParameter: Boolean = false,
): IrValueParameter = convertCatching(valueParameter) { ): IrValueParameter = convertCatching(valueParameter) {
val origin = valueParameter.computeIrOrigin() val origin = valueParameter.computeIrOrigin()
val type = valueParameter.returnTypeRef.toIrType(typeContext) val type = valueParameter.returnTypeRef.toIrType(typeOrigin)
val irParameter = valueParameter.convertWithOffsets { startOffset, endOffset -> val irParameter = valueParameter.convertWithOffsets { startOffset, endOffset ->
irFactory.createValueParameter( irFactory.createValueParameter(
startOffset = startOffset, startOffset = startOffset,
@@ -1224,7 +1223,7 @@ class Fir2IrDeclarationStorage(
index = index, index = index,
varargElementType = varargElementType =
if (!valueParameter.isVararg) null if (!valueParameter.isVararg) null
else valueParameter.returnTypeRef.coneType.arrayElementType()?.toIrType(typeContext), else valueParameter.returnTypeRef.coneType.arrayElementType()?.toIrType(typeOrigin),
isCrossinline = valueParameter.isCrossinline, isCrossinline = valueParameter.isCrossinline,
isNoinline = valueParameter.isNoinline, isNoinline = valueParameter.isNoinline,
isHidden = false, isHidden = false,
@@ -30,12 +30,12 @@ class Fir2IrImplicitCastInserter(
private val components: Fir2IrComponents private val components: Fir2IrComponents
) : Fir2IrComponents by components, FirDefaultVisitor<IrElement, IrElement>() { ) : Fir2IrComponents by components, FirDefaultVisitor<IrElement, IrElement>() {
private fun FirTypeRef.toIrType(conversionTypeContext: ConversionTypeContext): IrType = with(typeConverter) { private fun FirTypeRef.toIrType(typeOrigin: ConversionTypeOrigin): IrType = with(typeConverter) {
toIrType(conversionTypeContext) toIrType(typeOrigin)
} }
private fun ConeKotlinType.toIrType(conversionTypeContext: ConversionTypeContext): IrType = with(typeConverter) { private fun ConeKotlinType.toIrType(typeOrigin: ConversionTypeOrigin): IrType = with(typeConverter) {
toIrType(conversionTypeContext) toIrType(typeOrigin)
} }
override fun visitElement(element: FirElement, data: IrElement): IrElement { override fun visitElement(element: FirElement, data: IrElement): IrElement {
@@ -216,7 +216,7 @@ class Fir2IrImplicitCastInserter(
} }
valueType is ConeDynamicType -> { valueType is ConeDynamicType -> {
if (expectedType !is ConeDynamicType && !expectedType.isNullableAny) { if (expectedType !is ConeDynamicType && !expectedType.isNullableAny) {
implicitCast(this, expectedType.toIrType(ConversionTypeContext.DEFAULT)) implicitCast(this, expectedType.toIrType(ConversionTypeOrigin.DEFAULT))
} else { } else {
this this
} }
@@ -282,7 +282,7 @@ class Fir2IrImplicitCastInserter(
original: IrExpression, original: IrExpression,
originalTypeRef: FirTypeRef, originalTypeRef: FirTypeRef,
calleeReference: FirReference?, calleeReference: FirReference?,
conversionTypeContext: ConversionTypeContext, typeOrigin: ConversionTypeOrigin,
): IrExpression { ): IrExpression {
val referencedDeclaration = calleeReference?.toResolvedCallableSymbol()?.unwrapCallRepresentative()?.fir val referencedDeclaration = calleeReference?.toResolvedCallableSymbol()?.unwrapCallRepresentative()?.fir
@@ -295,23 +295,23 @@ class Fir2IrImplicitCastInserter(
val castType = originalTypeRef.coneTypeSafe<ConeIntersectionType>() val castType = originalTypeRef.coneTypeSafe<ConeIntersectionType>()
castType?.intersectedTypes?.forEach { componentType -> castType?.intersectedTypes?.forEach { componentType ->
if (AbstractTypeChecker.isSubtypeOf(session.typeContext, componentType, starProjectedDispatchReceiver)) { if (AbstractTypeChecker.isSubtypeOf(session.typeContext, componentType, starProjectedDispatchReceiver)) {
return implicitCastOrExpression(original, componentType, conversionTypeContext) return implicitCastOrExpression(original, componentType, typeOrigin)
} }
} }
return implicitCastOrExpression(original, originalTypeRef, conversionTypeContext) return implicitCastOrExpression(original, originalTypeRef, typeOrigin)
} }
private fun implicitCastOrExpression( private fun implicitCastOrExpression(
original: IrExpression, castType: ConeKotlinType, conversionTypeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT original: IrExpression, castType: ConeKotlinType, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT
): IrExpression { ): IrExpression {
return implicitCastOrExpression(original, castType.toIrType(conversionTypeContext)) return implicitCastOrExpression(original, castType.toIrType(typeOrigin))
} }
private fun implicitCastOrExpression( private fun implicitCastOrExpression(
original: IrExpression, castType: FirTypeRef, conversionTypeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT original: IrExpression, castType: FirTypeRef, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT
): IrExpression { ): IrExpression {
return implicitCastOrExpression(original, castType.toIrType(conversionTypeContext)) return implicitCastOrExpression(original, castType.toIrType(typeOrigin))
} }
companion object { companion object {
@@ -89,11 +89,11 @@ class Fir2IrTypeConverter(
override val intersectionTypesInContravariantPositions: Boolean get() = true override val intersectionTypesInContravariantPositions: Boolean get() = true
} }
fun FirTypeRef.toIrType(typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT): IrType { fun FirTypeRef.toIrType(typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT): IrType {
capturedTypeCache.clear() capturedTypeCache.clear()
return when (this) { return when (this) {
!is FirResolvedTypeRef -> createErrorType() !is FirResolvedTypeRef -> createErrorType()
!is FirImplicitBuiltinTypeRef -> type.toIrType(typeContext, annotations) !is FirImplicitBuiltinTypeRef -> type.toIrType(typeOrigin, annotations)
is FirImplicitNothingTypeRef -> irBuiltIns.nothingType is FirImplicitNothingTypeRef -> irBuiltIns.nothingType
is FirImplicitUnitTypeRef -> irBuiltIns.unitType is FirImplicitUnitTypeRef -> irBuiltIns.unitType
is FirImplicitBooleanTypeRef -> irBuiltIns.booleanType is FirImplicitBooleanTypeRef -> irBuiltIns.booleanType
@@ -102,12 +102,12 @@ class Fir2IrTypeConverter(
is FirImplicitIntTypeRef -> irBuiltIns.intType is FirImplicitIntTypeRef -> irBuiltIns.intType
is FirImplicitNullableAnyTypeRef -> irBuiltIns.anyNType is FirImplicitNullableAnyTypeRef -> irBuiltIns.anyNType
is FirImplicitNullableNothingTypeRef -> irBuiltIns.nothingNType is FirImplicitNullableNothingTypeRef -> irBuiltIns.nothingNType
else -> type.toIrType(typeContext, annotations) else -> type.toIrType(typeOrigin, annotations)
} }
} }
fun ConeKotlinType.toIrType( fun ConeKotlinType.toIrType(
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT,
annotations: List<FirAnnotation> = emptyList(), annotations: List<FirAnnotation> = emptyList(),
hasFlexibleNullability: Boolean = false, hasFlexibleNullability: Boolean = false,
hasFlexibleMutability: Boolean = false, hasFlexibleMutability: Boolean = false,
@@ -121,7 +121,7 @@ class Fir2IrTypeConverter(
val irSymbol = val irSymbol =
getBuiltInClassSymbol(classId) getBuiltInClassSymbol(classId)
?: lookupTag.toSymbol(session)?.toSymbol(typeContext) { ?: lookupTag.toSymbol(session)?.toSymbol(typeOrigin) {
typeAnnotations += with(annotationGenerator) { it.toIrAnnotations() } typeAnnotations += with(annotationGenerator) { it.toIrAnnotations() }
} }
?: (lookupTag as? ConeClassLikeLookupTag)?.let(classifierStorage::getIrClassSymbolForNotFoundClass) ?: (lookupTag as? ConeClassLikeLookupTag)?.let(classifierStorage::getIrClassSymbolForNotFoundClass)
@@ -169,7 +169,7 @@ class Fir2IrTypeConverter(
IrSimpleTypeImpl( IrSimpleTypeImpl(
irSymbol, irSymbol,
hasQuestionMark = approximatedType.isMarkedNullable, hasQuestionMark = approximatedType.isMarkedNullable,
arguments = approximatedType.typeArguments.map { it.toIrTypeArgument(typeContext) }, arguments = approximatedType.typeArguments.map { it.toIrTypeArgument(typeOrigin) },
annotations = typeAnnotations annotations = typeAnnotations
) )
} }
@@ -177,7 +177,7 @@ class Fir2IrTypeConverter(
// Upper bound has star projections here, so we take lower one // Upper bound has star projections here, so we take lower one
// (some reflection tests rely on this) // (some reflection tests rely on this)
lowerBound.toIrType( lowerBound.toIrType(
typeContext, typeOrigin,
annotations, annotations,
hasFlexibleNullability = lowerBound.nullability != upperBound.nullability, hasFlexibleNullability = lowerBound.nullability != upperBound.nullability,
hasFlexibleMutability = isMutabilityFlexible(), hasFlexibleMutability = isMutabilityFlexible(),
@@ -198,14 +198,14 @@ class Fir2IrTypeConverter(
(intermediate.withNullability(upper.isNullable) as ConeKotlinType) (intermediate.withNullability(upper.isNullable) as ConeKotlinType)
.withAttributes(lower.attributes) .withAttributes(lower.attributes)
.toIrType( .toIrType(
typeContext, typeOrigin,
annotations, annotations,
hasFlexibleNullability = lower.nullability != upper.nullability, hasFlexibleNullability = lower.nullability != upper.nullability,
hasFlexibleMutability = isMutabilityFlexible() hasFlexibleMutability = isMutabilityFlexible()
) )
} else { } else {
upperBound.toIrType( upperBound.toIrType(
typeContext, typeOrigin,
annotations, annotations,
hasFlexibleNullability = lowerBound.nullability != upperBound.nullability, hasFlexibleNullability = lowerBound.nullability != upperBound.nullability,
hasFlexibleMutability = isMutabilityFlexible() hasFlexibleMutability = isMutabilityFlexible()
@@ -220,7 +220,7 @@ class Fir2IrTypeConverter(
val approximation = supertypes.find { val approximation = supertypes.find {
it == (constructor.projection as? ConeKotlinTypeProjection)?.type it == (constructor.projection as? ConeKotlinTypeProjection)?.type
} ?: supertypes.first() } ?: supertypes.first()
val irType = approximation.toIrType(typeContext) val irType = approximation.toIrType(typeOrigin)
capturedTypeCache[this] = irType capturedTypeCache[this] = irType
irType irType
} else { } else {
@@ -231,11 +231,11 @@ class Fir2IrTypeConverter(
} }
} }
is ConeDefinitelyNotNullType -> { is ConeDefinitelyNotNullType -> {
original.toIrType(typeContext).makeNotNull() original.toIrType(typeOrigin).makeNotNull()
} }
is ConeIntersectionType -> { is ConeIntersectionType -> {
// TODO: add intersectionTypeApproximation // TODO: add intersectionTypeApproximation
intersectedTypes.first().toIrType(typeContext) intersectedTypes.first().toIrType(typeOrigin)
} }
is ConeStubType -> createErrorType() is ConeStubType -> createErrorType()
is ConeIntegerLiteralType -> createErrorType() is ConeIntegerLiteralType -> createErrorType()
@@ -250,9 +250,9 @@ class Fir2IrTypeConverter(
CommonFlexibleTypeBoundsChecker.getBaseBoundFqNameByMutability(upperFqName) CommonFlexibleTypeBoundsChecker.getBaseBoundFqNameByMutability(upperFqName)
} }
private fun ConeTypeProjection.toIrTypeArgument(typeContext: ConversionTypeContext): IrTypeArgument { private fun ConeTypeProjection.toIrTypeArgument(typeOrigin: ConversionTypeOrigin): IrTypeArgument {
fun toIrTypeArgument(type: ConeKotlinType, variance: Variance): IrTypeProjection { fun toIrTypeArgument(type: ConeKotlinType, variance: Variance): IrTypeProjection {
val irType = type.toIrType(typeContext) val irType = type.toIrType(typeOrigin)
return makeTypeProjection(irType, variance) return makeTypeProjection(irType, variance)
} }
@@ -267,7 +267,7 @@ class Fir2IrTypeConverter(
// We can return * early here to avoid recursive type conversions. // We can return * early here to avoid recursive type conversions.
IrStarProjectionImpl IrStarProjectionImpl
} else { } else {
val irType = toIrType(typeContext) val irType = toIrType(typeOrigin)
makeTypeProjection(irType, Variance.INVARIANT) makeTypeProjection(irType, Variance.INVARIANT)
} }
} }
@@ -331,8 +331,8 @@ class Fir2IrTypeConverter(
fun FirTypeRef.toIrType( fun FirTypeRef.toIrType(
typeConverter: Fir2IrTypeConverter, typeConverter: Fir2IrTypeConverter,
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT
): IrType = ): IrType =
with(typeConverter) { with(typeConverter) {
toIrType(typeContext) toIrType(typeOrigin)
} }
@@ -816,7 +816,7 @@ class Fir2IrVisitor(
implicitCastInserter.implicitCastFromDispatchReceiver( implicitCastInserter.implicitCastFromDispatchReceiver(
this, expression.typeRef, calleeReference, this, expression.typeRef, calleeReference,
conversionScope.defaultConversionTypeContext() conversionScope.defaultConversionTypeOrigin()
) )
} }
} }
@@ -1411,7 +1411,7 @@ class Fir2IrVisitor(
} }
val irClassReferenceSymbol = when (argument) { val irClassReferenceSymbol = when (argument) {
is FirResolvedReifiedParameterReference -> { is FirResolvedReifiedParameterReference -> {
classifierStorage.getIrTypeParameterSymbol(argument.symbol, ConversionTypeContext.DEFAULT) classifierStorage.getIrTypeParameterSymbol(argument.symbol, ConversionTypeOrigin.DEFAULT)
} }
is FirResolvedQualifier -> { is FirResolvedQualifier -> {
when (val symbol = argument.symbol) { when (val symbol = argument.symbol) {
@@ -80,8 +80,8 @@ internal class AdapterGenerator(
} }
} }
private fun ConeKotlinType.toIrType(typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT): IrType = private fun ConeKotlinType.toIrType(typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT): IrType =
with(typeConverter) { toIrType(typeContext) } with(typeConverter) { toIrType(typeOrigin) }
internal fun needToGenerateAdaptedCallableReference( internal fun needToGenerateAdaptedCallableReference(
callableReferenceAccess: FirCallableReferenceAccess, callableReferenceAccess: FirCallableReferenceAccess,
@@ -440,7 +440,7 @@ internal class AdapterGenerator(
} }
val samFirType = substitutedParameterType.removeExternalProjections() ?: substitutedParameterType val samFirType = substitutedParameterType.removeExternalProjections() ?: substitutedParameterType
val samType = samFirType.toIrType(ConversionTypeContext.DEFAULT) val samType = samFirType.toIrType(ConversionTypeOrigin.DEFAULT)
// Make sure the converted IrType owner indeed has a single abstract method, since FunctionReferenceLowering relies on it. // Make sure the converted IrType owner indeed has a single abstract method, since FunctionReferenceLowering relies on it.
if (!samType.isSamType) return this if (!samType.isSamType) return this
return IrTypeOperatorCallImpl( return IrTypeOperatorCallImpl(
@@ -54,10 +54,10 @@ class CallAndReferenceGenerator(
private val adapterGenerator = AdapterGenerator(components, conversionScope) private val adapterGenerator = AdapterGenerator(components, conversionScope)
private fun FirTypeRef.toIrType(): IrType = private fun FirTypeRef.toIrType(): IrType =
with(typeConverter) { toIrType(conversionScope.defaultConversionTypeContext()) } with(typeConverter) { toIrType(conversionScope.defaultConversionTypeOrigin()) }
private fun ConeKotlinType.toIrType(): IrType = private fun ConeKotlinType.toIrType(): IrType =
with(typeConverter) { toIrType(conversionScope.defaultConversionTypeContext()) } with(typeConverter) { toIrType(conversionScope.defaultConversionTypeOrigin()) }
fun convertToIrCallableReference( fun convertToIrCallableReference(
callableReferenceAccess: FirCallableReferenceAccess, callableReferenceAccess: FirCallableReferenceAccess,
@@ -197,7 +197,7 @@ class Fir2IrLazyProperty(
correspondingPropertySymbol = this@Fir2IrLazyProperty.symbol correspondingPropertySymbol = this@Fir2IrLazyProperty.symbol
with(classifierStorage) { with(classifierStorage) {
setTypeParameters( setTypeParameters(
this@Fir2IrLazyProperty.fir, ConversionTypeContext.DEFAULT this@Fir2IrLazyProperty.fir, ConversionTypeOrigin.DEFAULT
) )
} }
} }
@@ -229,7 +229,7 @@ class Fir2IrLazyProperty(
correspondingPropertySymbol = this@Fir2IrLazyProperty.symbol correspondingPropertySymbol = this@Fir2IrLazyProperty.symbol
with(classifierStorage) { with(classifierStorage) {
setTypeParameters( setTypeParameters(
this@Fir2IrLazyProperty.fir, ConversionTypeContext.IN_SETTER this@Fir2IrLazyProperty.fir, ConversionTypeOrigin.SETTER
) )
} }
} }
@@ -119,5 +119,5 @@ class Fir2IrLazyPropertyAccessor(
override val containerSource: DeserializedContainerSource? override val containerSource: DeserializedContainerSource?
get() = firParentProperty.containerSource get() = firParentProperty.containerSource
private val conversionTypeContext = if (isSetter) ConversionTypeContext.IN_SETTER else ConversionTypeContext.DEFAULT private val conversionTypeContext = if (isSetter) ConversionTypeOrigin.SETTER else ConversionTypeOrigin.DEFAULT
} }