From c6124f6d564e286ebb9e6c278a9c9d1eb805e4b8 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 26 Jun 2020 15:52:02 +0300 Subject: [PATCH] [FIR] Cleanup FIR modules. Part 6 (`transformers` package) --- ...rCallCompletionResultsWriterTransformer.kt | 23 +++++++++---------- .../transformers/FirSupertypesResolution.kt | 2 +- .../FirWhenExhaustivenessTransformer.kt | 19 ++++++--------- ...egerLiteralTypeApproximationTransformer.kt | 17 +++++++++----- .../transformers/ReturnTypeCalculator.kt | 1 - .../FirGlobalClassGenerationProcessor.kt | 1 - .../FirGlobalExtensionStatusProcessor.kt | 3 +-- 7 files changed, 31 insertions(+), 35 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt index ad8963da5c7..e9ff8c5b6a8 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt @@ -57,12 +57,12 @@ class FirCallCompletionResultsWriterTransformer( Normal, DelegatedPropertyCompletion } - private fun prepareQualifiedTransform( - qualifiedAccess: T, calleeReference: FirNamedReferenceWithCandidate - ): T where T : FirQualifiedAccess, T : FirExpression { + private fun 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 transformSyntheticCall( syntheticCall: D, data: ExpectedArgumentType?, - ): CompositeTransformResult - where D : FirResolvable, D : FirExpression { - val syntheticCall = syntheticCall.transformChildren(this, data?.getExpectedType(syntheticCall)?.toExpectedType()) as D + ): CompositeTransformResult 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 { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt index 6856b47288f..0d664319e07 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt @@ -347,7 +347,7 @@ private class SupertypeComputationSession { private val scopesForNestedClassesMap = hashMapOf, ScopeImmutableList>() private val supertypeStatusMap = linkedMapOf, SupertypeComputationStatus>() - val supertypesSupplier = object : SupertypeSupplier() { + val supertypesSupplier: SupertypeSupplier = object : SupertypeSupplier() { override fun forClass(firClass: FirClass<*>): List { if (firClass.resolvePhase > FirResolvePhase.SUPER_TYPES) return firClass.superConeTypes return (getSupertypesComputationStatus(firClass) as? SupertypeComputationStatus.Computed)?.supertypeRefs?.mapNotNull { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirWhenExhaustivenessTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirWhenExhaustivenessTransformer.kt index 50989b731d6..b4892ac80cd 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirWhenExhaustivenessTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirWhenExhaustivenessTransformer.kt @@ -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() { override fun transformElement(element: E, data: Nothing?): CompositeTransformResult { @@ -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()?.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 } } } -} \ No newline at end of file +} diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralTypeApproximationTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralTypeApproximationTransformer.kt index 8a5d7b59662..2b43b5137a2 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralTypeApproximationTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/IntegerLiteralTypeApproximationTransformer.kt @@ -113,8 +113,8 @@ class IntegerLiteralTypeApproximationTransformer( val expectedType: ConeKotlinType? = when { !leftIsIlt && !rightIsIlt -> return operatorCall.compose() leftIsIlt && rightIsIlt -> null - leftIsIlt -> rightArgument.typeRef.coneTypeUnsafe() - rightIsIlt -> leftArgument.typeRef.coneTypeUnsafe() + 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() { +class IntegerOperatorsTypeUpdater(private val approximator: IntegerLiteralTypeApproximationTransformer) : FirTransformer() { override fun transformElement(element: E, data: Nothing?): CompositeTransformResult { return element.compose() } @@ -175,9 +175,8 @@ class IntegerOperatorsTypeUpdater(val approximator: IntegerLiteralTypeApproximat else -> throw IllegalStateException() } else -> { - val argumentType = functionCall.argument.typeRef.coneTypeUnsafe() // TODO: handle overflow - when (argumentType) { + when (val argumentType = functionCall.argument.typeRef.coneTypeUnsafe()) { 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() } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/ReturnTypeCalculator.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/ReturnTypeCalculator.kt index 01a3c0d0306..c64f181ee01 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/ReturnTypeCalculator.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/ReturnTypeCalculator.kt @@ -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 diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirGlobalClassGenerationProcessor.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirGlobalClassGenerationProcessor.kt index d9437f007b1..0d68e6e606b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirGlobalClassGenerationProcessor.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirGlobalClassGenerationProcessor.kt @@ -64,4 +64,3 @@ class FirGlobalClassGenerationProcessor( } } } - diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirGlobalExtensionStatusProcessor.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirGlobalExtensionStatusProcessor.kt index fa055d71bde..45fb485117e 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirGlobalExtensionStatusProcessor.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirGlobalExtensionStatusProcessor.kt @@ -106,7 +106,6 @@ class FirTransformerBasedExtensionStatusProcessor( } } - private object ReplaceStatus : FirTransformer() { override fun transformElement(element: E, data: FirDeclarationStatus): CompositeTransformResult { return element.compose() @@ -118,4 +117,4 @@ private object ReplaceStatus : FirTransformer() { ): CompositeTransformResult { return data.compose() } -} \ No newline at end of file +}