[FIR] Cleanup FIR modules. Part 6 (transformers package)

This commit is contained in:
Dmitriy Novozhilov
2020-06-26 15:52:02 +03:00
parent e817f919c2
commit c6124f6d56
7 changed files with 31 additions and 35 deletions
@@ -57,12 +57,12 @@ class FirCallCompletionResultsWriterTransformer(
Normal, DelegatedPropertyCompletion
}
private fun <T> prepareQualifiedTransform(
qualifiedAccess: T, calleeReference: FirNamedReferenceWithCandidate
): T where T : FirQualifiedAccess, T : FirExpression {
private fun <T : FirQualifiedAccessExpression> prepareQualifiedTransform(
qualifiedAccessExpression: T, calleeReference: FirNamedReferenceWithCandidate
): T {
val subCandidate = calleeReference.candidate
val declaration = subCandidate.symbol.phasedFir
val typeArguments = computeTypeArguments(qualifiedAccess, subCandidate)
val declaration: FirDeclaration = subCandidate.symbol.phasedFir
val typeArguments = computeTypeArguments(qualifiedAccessExpression, subCandidate)
val typeRef = if (declaration is FirTypedDeclaration) {
typeCalculator.tryCalculateReturnType(declaration)
} else {
@@ -72,12 +72,13 @@ class FirCallCompletionResultsWriterTransformer(
}
}
val updatedQualifiedAccess = if (qualifiedAccess is FirFunctionCall) {
qualifiedAccess.transformSingle(integerOperatorsTypeUpdater, null)
val updatedQualifiedAccess = if (qualifiedAccessExpression is FirFunctionCall) {
qualifiedAccessExpression.transformSingle(integerOperatorsTypeUpdater, null)
} else {
qualifiedAccess
qualifiedAccessExpression
}
@Suppress("UNCHECKED_CAST")
val result = updatedQualifiedAccess
.transformCalleeReference(
StoreCalleeReference,
@@ -435,9 +436,8 @@ class FirCallCompletionResultsWriterTransformer(
private inline fun <reified D> transformSyntheticCall(
syntheticCall: D,
data: ExpectedArgumentType?,
): CompositeTransformResult<FirStatement>
where D : FirResolvable, D : FirExpression {
val syntheticCall = syntheticCall.transformChildren(this, data?.getExpectedType(syntheticCall)?.toExpectedType()) as D
): CompositeTransformResult<FirStatement> where D : FirResolvable, D : FirExpression {
syntheticCall.transformChildren(this, data?.getExpectedType(syntheticCall)?.toExpectedType())
val calleeReference = syntheticCall.calleeReference as? FirNamedReferenceWithCandidate ?: return syntheticCall.compose()
val declaration = calleeReference.candidate.symbol.fir as? FirSimpleFunction ?: return syntheticCall.compose()
@@ -472,7 +472,6 @@ class FirCallCompletionResultsWriterTransformer(
resolvedSymbol = this@toResolvedReference.candidateSymbol
}
}
}
sealed class ExpectedArgumentType {
@@ -347,7 +347,7 @@ private class SupertypeComputationSession {
private val scopesForNestedClassesMap = hashMapOf<FirClass<*>, ScopeImmutableList>()
private val supertypeStatusMap = linkedMapOf<FirClassLikeDeclaration<*>, SupertypeComputationStatus>()
val supertypesSupplier = object : SupertypeSupplier() {
val supertypesSupplier: SupertypeSupplier = object : SupertypeSupplier() {
override fun forClass(firClass: FirClass<*>): List<ConeClassLikeType> {
if (firClass.resolvePhase > FirResolvePhase.SUPER_TYPES) return firClass.superConeTypes
return (getSupertypesComputationStatus(firClass) as? SupertypeComputationStatus.Computed)?.supertypeRefs?.mapNotNull {
@@ -22,12 +22,10 @@ import org.jetbrains.kotlin.fir.resolve.toSymbol
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.fir.types.ConeLookupTagBasedType
import org.jetbrains.kotlin.fir.types.ConeNullability
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.lowerBoundIfFlexible
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.visitors.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.utils.addToStdlib.runIf
class FirWhenExhaustivenessTransformer(private val bodyResolveComponents: BodyResolveComponents) : FirTransformer<Nothing?>() {
override fun <E : FirElement> transformElement(element: E, data: Nothing?): CompositeTransformResult<E> {
@@ -50,9 +48,9 @@ class FirWhenExhaustivenessTransformer(private val bodyResolveComponents: BodyRe
?: return null
// TODO: add some report logic about flexible type (see WHEN_ENUM_CAN_BE_NULL_IN_JAVA diagnostic in old frontend)
val type = (typeRef as? FirResolvedTypeRef)?.type?.lowerBoundIfFlexible() ?: return null
val type = typeRef.coneTypeSafe<ConeKotlinType>()?.lowerBoundIfFlexible() ?: return null
val lookupTag = (type as? ConeLookupTagBasedType)?.lookupTag ?: return null
val nullable = typeRef.type.nullability == ConeNullability.NULLABLE
val nullable = type.nullability == ConeNullability.NULLABLE
val isExhaustive = when {
((lookupTag as? ConeClassLikeLookupTag)?.classId == bodyResolveComponents.session.builtinTypes.booleanType.id) -> {
checkBooleanExhaustiveness(whenExpression, nullable)
@@ -70,11 +68,9 @@ class FirWhenExhaustivenessTransformer(private val bodyResolveComponents: BodyRe
}
}
return if (isExhaustive) {
return runIf(isExhaustive) {
whenExpression.replaceIsExhaustive(true)
whenExpression
} else {
null
}
}
@@ -158,8 +154,7 @@ class FirWhenExhaustivenessTransformer(private val bodyResolveComponents: BodyRe
override fun visitOperatorCall(operatorCall: FirOperatorCall, data: SealedExhaustivenessData) {
if (operatorCall.operation == FirOperation.EQ) {
val argument = operatorCall.arguments[1]
when (argument) {
when (val argument = operatorCall.arguments[1]) {
is FirConstExpression<*> -> {
if (argument.value == null) {
data.containsNull = true
@@ -217,4 +212,4 @@ class FirWhenExhaustivenessTransformer(private val bodyResolveComponents: BodyRe
}
}
}
}
}
@@ -113,8 +113,8 @@ class IntegerLiteralTypeApproximationTransformer(
val expectedType: ConeKotlinType? = when {
!leftIsIlt && !rightIsIlt -> return operatorCall.compose()
leftIsIlt && rightIsIlt -> null
leftIsIlt -> rightArgument.typeRef.coneTypeUnsafe<ConeKotlinType>()
rightIsIlt -> leftArgument.typeRef.coneTypeUnsafe<ConeKotlinType>()
leftIsIlt -> rightArgument.typeRef.coneTypeUnsafe()
rightIsIlt -> leftArgument.typeRef.coneTypeUnsafe()
else -> throw IllegalStateException()
}
@@ -151,7 +151,7 @@ fun FirFunctionCall.getOriginalFunction(): FirCallableDeclaration<*>? {
return symbol?.fir as? FirCallableDeclaration<*>
}
class IntegerOperatorsTypeUpdater(val approximator: IntegerLiteralTypeApproximationTransformer) : FirTransformer<Nothing?>() {
class IntegerOperatorsTypeUpdater(private val approximator: IntegerLiteralTypeApproximationTransformer) : FirTransformer<Nothing?>() {
override fun <E : FirElement> transformElement(element: E, data: Nothing?): CompositeTransformResult<E> {
return element.compose()
}
@@ -175,9 +175,8 @@ class IntegerOperatorsTypeUpdater(val approximator: IntegerLiteralTypeApproximat
else -> throw IllegalStateException()
}
else -> {
val argumentType = functionCall.argument.typeRef.coneTypeUnsafe<ConeKotlinType>()
// TODO: handle overflow
when (argumentType) {
when (val argumentType = functionCall.argument.typeRef.coneTypeUnsafe<ConeKotlinType>()) {
is ConeIntegerLiteralType -> {
val argumentValue = argumentType.value
val divisionByZero = argumentValue == 0L
@@ -210,7 +209,13 @@ class IntegerOperatorsTypeUpdater(val approximator: IntegerLiteralTypeApproximat
}
}
}
functionCall.replaceTypeRef(functionCall.resultType.resolvedTypeFromPrototype(ConeIntegerLiteralTypeImpl(resultValue, isUnsigned = receiverType.isUnsigned)))
val newTypeRef = functionCall.resultType.resolvedTypeFromPrototype(
ConeIntegerLiteralTypeImpl(
resultValue,
isUnsigned = receiverType.isUnsigned
)
)
functionCall.replaceTypeRef(newTypeRef)
return functionCall.toOperatorCall().compose()
}
}
@@ -17,7 +17,6 @@ interface ReturnTypeCalculator {
}
class ReturnTypeCalculatorForFullBodyResolve : ReturnTypeCalculator {
override fun tryCalculateReturnType(declaration: FirTypedDeclaration): FirResolvedTypeRef {
val returnTypeRef = declaration.returnTypeRef
if (returnTypeRef is FirResolvedTypeRef) return returnTypeRef
@@ -64,4 +64,3 @@ class FirGlobalClassGenerationProcessor(
}
}
}
@@ -106,7 +106,6 @@ class FirTransformerBasedExtensionStatusProcessor(
}
}
private object ReplaceStatus : FirTransformer<FirDeclarationStatus>() {
override fun <E : FirElement> transformElement(element: E, data: FirDeclarationStatus): CompositeTransformResult<E> {
return element.compose()
@@ -118,4 +117,4 @@ private object ReplaceStatus : FirTransformer<FirDeclarationStatus>() {
): CompositeTransformResult<FirDeclarationStatus> {
return data.compose()
}
}
}