[FIR] ConversionUtils: cleanup code

^KT-54311
This commit is contained in:
Dmitrii Gridin
2022-10-05 14:20:08 +02:00
committed by Space Team
parent a0b814a1b6
commit 5f9076fb7a
@@ -82,7 +82,7 @@ internal fun <T : IrElement> FirQualifiedAccess.convertWithOffsets(
): T { ): T {
val psi = calleeReference.psi val psi = calleeReference.psi
if (psi is PsiCompiledElement) return f(UNDEFINED_OFFSET, UNDEFINED_OFFSET) if (psi is PsiCompiledElement) return f(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
val startOffset = psi?.startOffsetSkippingComments ?: calleeReference.source ?.startOffset ?: UNDEFINED_OFFSET val startOffset = psi?.startOffsetSkippingComments ?: calleeReference.source?.startOffset ?: UNDEFINED_OFFSET
val endOffset = source?.endOffset ?: UNDEFINED_OFFSET val endOffset = source?.endOffset ?: UNDEFINED_OFFSET
return f(startOffset, endOffset) return f(startOffset, endOffset)
} }
@@ -137,6 +137,7 @@ fun FirClassifierSymbol<*>.toSymbol(
is FirTypeParameterSymbol -> { is FirTypeParameterSymbol -> {
classifierStorage.getIrTypeParameterSymbol(this, typeContext) classifierStorage.getIrTypeParameterSymbol(this, typeContext)
} }
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
@@ -144,9 +145,11 @@ fun FirClassifierSymbol<*>.toSymbol(
?.toSymbol(typeContext, handleAnnotations) ?.toSymbol(typeContext, handleAnnotations)
?: classifierStorage.getIrClassSymbolForNotFoundClass(coneClassLikeType.lookupTag) ?: classifierStorage.getIrClassSymbolForNotFoundClass(coneClassLikeType.lookupTag)
} }
is FirClassSymbol -> { is FirClassSymbol -> {
classifierStorage.getIrClassSymbol(this) classifierStorage.getIrClassSymbol(this)
} }
else -> error("Unknown symbol: $this") else -> error("Unknown symbol: $this")
} }
} }
@@ -166,6 +169,7 @@ private fun FirBasedSymbol<*>.toSymbolForCall(
isDelegate, isDelegate,
isReference isReference
) )
is FirClassifierSymbol<*> -> toSymbol() is FirClassifierSymbol<*> -> toSymbol()
else -> error("Unknown symbol: $this") else -> error("Unknown symbol: $this")
} }
@@ -188,6 +192,7 @@ fun FirReference.toSymbolForCall(
isDelegate, isDelegate,
isReference isReference
) )
is FirErrorNamedReference -> is FirErrorNamedReference ->
candidateSymbol?.toSymbolForCall( candidateSymbol?.toSymbolForCall(
dispatchReceiver, dispatchReceiver,
@@ -196,6 +201,7 @@ fun FirReference.toSymbolForCall(
isDelegate, isDelegate,
isReference isReference
) )
is FirThisReference -> { is FirThisReference -> {
when (val boundSymbol = boundSymbol) { when (val boundSymbol = boundSymbol) {
is FirClassSymbol<*> -> classifierStorage.getIrClassSymbol(boundSymbol).owner.thisReceiver?.symbol is FirClassSymbol<*> -> classifierStorage.getIrClassSymbol(boundSymbol).owner.thisReceiver?.symbol
@@ -204,9 +210,11 @@ fun FirReference.toSymbolForCall(
val property = declarationStorage.getIrPropertySymbol(boundSymbol).owner as? IrProperty val property = declarationStorage.getIrPropertySymbol(boundSymbol).owner as? IrProperty
property?.let { conversionScope.parentAccessorOfPropertyFromStack(it) }?.symbol property?.let { conversionScope.parentAccessorOfPropertyFromStack(it) }?.symbol
} }
else -> null else -> null
} }
} }
else -> null else -> null
} }
} }
@@ -230,6 +238,7 @@ private fun FirCallableSymbol<*>.toSymbolForCall(
null null
} }
} }
else -> { else -> {
val coneType = dispatchReceiver.typeRef.coneType val coneType = dispatchReceiver.typeRef.coneType
dispatchReceiver.typeRef.coneType.findClassRepresentation(coneType, declarationStorage.session) dispatchReceiver.typeRef.coneType.findClassRepresentation(coneType, declarationStorage.session)
@@ -256,6 +265,7 @@ private fun FirCallableSymbol<*>.toSymbolForCall(
} ?: declarationStorage.getIrPropertySymbol(this) } ?: declarationStorage.getIrPropertySymbol(this)
} }
} }
is FirFunctionSymbol<*> -> declarationStorage.getIrFunctionSymbol(this, dispatchReceiverLookupTag) is FirFunctionSymbol<*> -> declarationStorage.getIrFunctionSymbol(this, dispatchReceiverLookupTag)
is FirPropertySymbol -> declarationStorage.getIrPropertySymbol(this, dispatchReceiverLookupTag) is FirPropertySymbol -> declarationStorage.getIrPropertySymbol(this, dispatchReceiverLookupTag)
is FirFieldSymbol -> declarationStorage.getIrFieldSymbol(this) is FirFieldSymbol -> declarationStorage.getIrFieldSymbol(this)
@@ -271,6 +281,7 @@ fun FirConstExpression<*>.getIrConstKind(): IrConstKind<*> = when (kind) {
val type = typeRef.coneTypeUnsafe<ConeIntegerLiteralType>() val type = typeRef.coneTypeUnsafe<ConeIntegerLiteralType>()
type.getApproximatedType().toConstKind()!!.toIrConstKind() type.getApproximatedType().toConstKind()!!.toIrConstKind()
} }
else -> kind.toIrConstKind() else -> kind.toIrConstKind()
} }
@@ -513,31 +524,37 @@ private val nameToOperationConventionOrigin = mutableMapOf(
OperatorNameConventions.CONTAINS to IrStatementOrigin.IN, OperatorNameConventions.CONTAINS to IrStatementOrigin.IN,
) )
internal fun FirReference.statementOrigin(): IrStatementOrigin? { internal fun FirReference.statementOrigin(): IrStatementOrigin? = when (this) {
return when (this) { is FirPropertyFromParameterResolvedNamedReference -> IrStatementOrigin.INITIALIZE_PROPERTY_FROM_PARAMETER
is FirPropertyFromParameterResolvedNamedReference -> IrStatementOrigin.INITIALIZE_PROPERTY_FROM_PARAMETER is FirResolvedNamedReference -> when (val symbol = resolvedSymbol) {
is FirResolvedNamedReference -> when (val symbol = resolvedSymbol) { is FirSyntheticPropertySymbol -> IrStatementOrigin.GET_PROPERTY
is FirSyntheticPropertySymbol -> IrStatementOrigin.GET_PROPERTY is FirNamedFunctionSymbol -> when {
is FirNamedFunctionSymbol -> when { symbol.callableId.isInvoke() ->
symbol.callableId.isInvoke() -> IrStatementOrigin.INVOKE
IrStatementOrigin.INVOKE
source?.elementType == KtNodeTypes.FOR && symbol.callableId.isIteratorNext() -> source?.elementType == KtNodeTypes.FOR && symbol.callableId.isIteratorNext() ->
IrStatementOrigin.FOR_LOOP_NEXT IrStatementOrigin.FOR_LOOP_NEXT
source?.elementType == KtNodeTypes.FOR && symbol.callableId.isIteratorHasNext() ->
IrStatementOrigin.FOR_LOOP_HAS_NEXT source?.elementType == KtNodeTypes.FOR && symbol.callableId.isIteratorHasNext() ->
source?.elementType == KtNodeTypes.FOR && symbol.callableId.isIterator() -> IrStatementOrigin.FOR_LOOP_HAS_NEXT
IrStatementOrigin.FOR_LOOP_ITERATOR
source?.elementType == KtNodeTypes.OPERATION_REFERENCE -> source?.elementType == KtNodeTypes.FOR && symbol.callableId.isIterator() ->
nameToOperationConventionOrigin[symbol.callableId.callableName] IrStatementOrigin.FOR_LOOP_ITERATOR
source?.kind is KtFakeSourceElementKind.DesugaredComponentFunctionCall ->
IrStatementOrigin.COMPONENT_N.withIndex(name.asString().removePrefix(DATA_CLASS_COMPONENT_PREFIX).toInt()) source?.elementType == KtNodeTypes.OPERATION_REFERENCE ->
else -> nameToOperationConventionOrigin[symbol.callableId.callableName]
null
} source?.kind is KtFakeSourceElementKind.DesugaredComponentFunctionCall ->
else -> null IrStatementOrigin.COMPONENT_N.withIndex(name.asString().removePrefix(DATA_CLASS_COMPONENT_PREFIX).toInt())
else ->
null
} }
else -> null else -> null
} }
else -> null
} }
context(Fir2IrComponents) context(Fir2IrComponents)
@@ -631,7 +648,7 @@ fun Fir2IrComponents.computeValueClassRepresentation(klass: FirRegularClass): Va
require((klass.valueClassRepresentation != null) == klass.isInline) require((klass.valueClassRepresentation != null) == klass.isInline)
return klass.valueClassRepresentation?.mapUnderlyingType { return klass.valueClassRepresentation?.mapUnderlyingType {
with(typeConverter) { with(typeConverter) {
it.toIrType() as? IrSimpleType?: error("Value class underlying type is not a simple type: ${klass.render()}") it.toIrType() as? IrSimpleType ?: error("Value class underlying type is not a simple type: ${klass.render()}")
} }
} }
} }