FIR IDE: separate KtExpressionTypeProvider into components
This commit is contained in:
+11
-6
@@ -36,7 +36,6 @@ import org.jetbrains.kotlin.psi.*
|
|||||||
*/
|
*/
|
||||||
abstract class KtAnalysisSession(final override val token: ValidityToken) : ValidityTokenOwner {
|
abstract class KtAnalysisSession(final override val token: ValidityToken) : ValidityTokenOwner {
|
||||||
protected abstract val smartCastProvider: KtSmartCastProvider
|
protected abstract val smartCastProvider: KtSmartCastProvider
|
||||||
protected abstract val typeProvider: KtTypeProvider
|
|
||||||
protected abstract val diagnosticProvider: KtDiagnosticProvider
|
protected abstract val diagnosticProvider: KtDiagnosticProvider
|
||||||
protected abstract val scopeProvider: KtScopeProvider
|
protected abstract val scopeProvider: KtScopeProvider
|
||||||
protected abstract val containingDeclarationProvider: KtSymbolContainingDeclarationProvider
|
protected abstract val containingDeclarationProvider: KtSymbolContainingDeclarationProvider
|
||||||
@@ -45,7 +44,11 @@ abstract class KtAnalysisSession(final override val token: ValidityToken) : Vali
|
|||||||
protected abstract val completionCandidateChecker: KtCompletionCandidateChecker
|
protected abstract val completionCandidateChecker: KtCompletionCandidateChecker
|
||||||
protected abstract val symbolDeclarationOverridesProvider: KtSymbolDeclarationOverridesProvider
|
protected abstract val symbolDeclarationOverridesProvider: KtSymbolDeclarationOverridesProvider
|
||||||
@Suppress("LeakingThis")
|
@Suppress("LeakingThis")
|
||||||
|
|
||||||
protected open val typeRenderer: KtTypeRenderer = KtDefaultTypeRenderer(this, token)
|
protected open val typeRenderer: KtTypeRenderer = KtDefaultTypeRenderer(this, token)
|
||||||
|
protected abstract val expressionTypeProvider: KtExpressionTypeProvider
|
||||||
|
protected abstract val typeProvider: KtTypeProvider
|
||||||
|
protected abstract val subtypingComponent: KtSubtypingComponent
|
||||||
protected abstract val expressionHandlingComponent: KtExpressionHandlingComponent
|
protected abstract val expressionHandlingComponent: KtExpressionHandlingComponent
|
||||||
|
|
||||||
/// TODO: get rid of
|
/// TODO: get rid of
|
||||||
@@ -59,18 +62,20 @@ abstract class KtAnalysisSession(final override val token: ValidityToken) : Vali
|
|||||||
|
|
||||||
fun KtExpression.getImplicitReceiverSmartCasts(): Collection<ImplicitReceiverSmartCast> = smartCastProvider.getImplicitReceiverSmartCasts(this)
|
fun KtExpression.getImplicitReceiverSmartCasts(): Collection<ImplicitReceiverSmartCast> = smartCastProvider.getImplicitReceiverSmartCasts(this)
|
||||||
|
|
||||||
fun KtExpression.getKtType(): KtType = typeProvider.getKtExpressionType(this)
|
fun KtExpression.getKtType(): KtType = expressionTypeProvider.getKtExpressionType(this)
|
||||||
|
|
||||||
fun KtDeclaration.getReturnKtType(): KtType = typeProvider.getReturnTypeForKtDeclaration(this)
|
fun KtDeclaration.getReturnKtType(): KtType = expressionTypeProvider.getReturnTypeForKtDeclaration(this)
|
||||||
|
|
||||||
infix fun KtType.isEqualTo(other: KtType): Boolean = typeProvider.isEqualTo(this, other)
|
infix fun KtType.isEqualTo(other: KtType): Boolean = subtypingComponent.isEqualTo(this, other)
|
||||||
|
|
||||||
infix fun KtType.isSubTypeOf(superType: KtType): Boolean = typeProvider.isSubTypeOf(this, superType)
|
infix fun KtType.isSubTypeOf(superType: KtType): Boolean = subtypingComponent.isSubTypeOf(this, superType)
|
||||||
|
|
||||||
fun PsiElement.getExpectedType(): KtType? = typeProvider.getExpectedType(this)
|
fun PsiElement.getExpectedType(): KtType? = expressionTypeProvider.getExpectedType(this)
|
||||||
|
|
||||||
fun KtType.isBuiltInFunctionalType(): Boolean = typeProvider.isBuiltinFunctionalType(this)
|
fun KtType.isBuiltInFunctionalType(): Boolean = typeProvider.isBuiltinFunctionalType(this)
|
||||||
|
|
||||||
|
val builtinTypes: KtBuiltinTypes get() = typeProvider.builtinTypes
|
||||||
|
|
||||||
fun KtElement.getDiagnostics(): Collection<Diagnostic> = diagnosticProvider.getDiagnosticsForElement(this)
|
fun KtElement.getDiagnostics(): Collection<Diagnostic> = diagnosticProvider.getDiagnosticsForElement(this)
|
||||||
|
|
||||||
fun KtFile.collectDiagnosticsForFile(): Collection<Diagnostic> = diagnosticProvider.collectDiagnosticsForFile(this)
|
fun KtFile.collectDiagnosticsForFile(): Collection<Diagnostic> = diagnosticProvider.collectDiagnosticsForFile(this)
|
||||||
|
|||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.frontend.api.components
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.ValidityTokenOwner
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
|
||||||
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
|
|
||||||
|
abstract class KtExpressionTypeProvider : KtAnalysisSessionComponent() {
|
||||||
|
abstract fun getReturnTypeForKtDeclaration(declaration: KtDeclaration): KtType
|
||||||
|
abstract fun getKtExpressionType(expression: KtExpression): KtType
|
||||||
|
|
||||||
|
abstract fun getExpectedType(expression: PsiElement): KtType?
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.frontend.api.components
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
|
||||||
|
|
||||||
|
abstract class KtSubtypingComponent : KtAnalysisSessionComponent() {
|
||||||
|
abstract fun isEqualTo(first: KtType, second: KtType): Boolean
|
||||||
|
abstract fun isSubTypeOf(subType: KtType, superType: KtType): Boolean
|
||||||
|
}
|
||||||
+2
-12
@@ -5,27 +5,17 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.frontend.api.components
|
package org.jetbrains.kotlin.idea.frontend.api.components
|
||||||
|
|
||||||
import com.intellij.psi.PsiElement
|
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.ValidityTokenOwner
|
import org.jetbrains.kotlin.idea.frontend.api.ValidityTokenOwner
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
|
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
|
||||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
|
||||||
import org.jetbrains.kotlin.psi.KtExpression
|
|
||||||
|
|
||||||
abstract class KtTypeProvider : KtAnalysisSessionComponent() {
|
abstract class KtTypeProvider : KtAnalysisSessionComponent() {
|
||||||
abstract fun getReturnTypeForKtDeclaration(declaration: KtDeclaration): KtType
|
//TODO get rid of it
|
||||||
abstract fun getKtExpressionType(expression: KtExpression): KtType
|
|
||||||
|
|
||||||
abstract fun isEqualTo(first: KtType, second: KtType): Boolean
|
|
||||||
abstract fun isSubTypeOf(subType: KtType, superType: KtType): Boolean
|
|
||||||
|
|
||||||
//TODO get rid of
|
|
||||||
abstract fun isBuiltinFunctionalType(type: KtType): Boolean
|
abstract fun isBuiltinFunctionalType(type: KtType): Boolean
|
||||||
|
|
||||||
abstract fun getExpectedType(expression: PsiElement): KtType?
|
|
||||||
|
|
||||||
abstract val builtinTypes: KtBuiltinTypes
|
abstract val builtinTypes: KtBuiltinTypes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Suppress("PropertyName")
|
@Suppress("PropertyName")
|
||||||
abstract class KtBuiltinTypes : ValidityTokenOwner {
|
abstract class KtBuiltinTypes : ValidityTokenOwner {
|
||||||
abstract val INT: KtType
|
abstract val INT: KtType
|
||||||
|
|||||||
+3
-1
@@ -34,7 +34,7 @@ private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override val smartCastProvider: KtSmartCastProvider = KtFirSmartcastProvider(this, token)
|
override val smartCastProvider: KtSmartCastProvider = KtFirSmartcastProvider(this, token)
|
||||||
override val typeProvider: KtTypeProvider = KtFirTypeProvider(this, token)
|
override val expressionTypeProvider: KtExpressionTypeProvider = KtFirExpressionTypeProvider(this, token)
|
||||||
override val diagnosticProvider: KtDiagnosticProvider = KtFirDiagnosticProvider(this, token)
|
override val diagnosticProvider: KtDiagnosticProvider = KtFirDiagnosticProvider(this, token)
|
||||||
override val containingDeclarationProvider = KtFirSymbolContainingDeclarationProvider(this, token)
|
override val containingDeclarationProvider = KtFirSymbolContainingDeclarationProvider(this, token)
|
||||||
override val callResolver: KtCallResolver = KtFirCallResolver(this, token)
|
override val callResolver: KtCallResolver = KtFirCallResolver(this, token)
|
||||||
@@ -46,6 +46,8 @@ private constructor(
|
|||||||
KtFirSymbolDeclarationOverridesProvider(this, token)
|
KtFirSymbolDeclarationOverridesProvider(this, token)
|
||||||
|
|
||||||
override val expressionHandlingComponent: KtExpressionHandlingComponent = KtFirExpressionHandlingComponent(this, token)
|
override val expressionHandlingComponent: KtExpressionHandlingComponent = KtFirExpressionHandlingComponent(this, token)
|
||||||
|
override val typeProvider: KtTypeProvider = KtFirTypeProvider(this, token)
|
||||||
|
override val subtypingComponent: KtSubtypingComponent = KtFirSubtypingComponent(this, token)
|
||||||
|
|
||||||
override fun createContextDependentCopy(): KtAnalysisSession {
|
override fun createContextDependentCopy(): KtAnalysisSession {
|
||||||
check(!isContextSession) { "Cannot create context-dependent copy of KtAnalysis session from a context dependent one" }
|
check(!isContextSession) { "Cannot create context-dependent copy of KtAnalysis session from a context dependent one" }
|
||||||
|
|||||||
+7
@@ -6,6 +6,7 @@
|
|||||||
package org.jetbrains.kotlin.idea.frontend.api.fir.components
|
package org.jetbrains.kotlin.idea.frontend.api.fir.components
|
||||||
|
|
||||||
import org.jetbrains.kotlin.fir.types.ConeKotlinType
|
import org.jetbrains.kotlin.fir.types.ConeKotlinType
|
||||||
|
import org.jetbrains.kotlin.fir.types.ConeTypeCheckerContext
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession
|
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession
|
||||||
|
|
||||||
internal interface KtFirAnalysisSessionComponent {
|
internal interface KtFirAnalysisSessionComponent {
|
||||||
@@ -14,4 +15,10 @@ internal interface KtFirAnalysisSessionComponent {
|
|||||||
val firSymbolBuilder get() = analysisSession.firSymbolBuilder
|
val firSymbolBuilder get() = analysisSession.firSymbolBuilder
|
||||||
val firResolveState get() = analysisSession.firResolveState
|
val firResolveState get() = analysisSession.firResolveState
|
||||||
fun ConeKotlinType.asKtType() = analysisSession.firSymbolBuilder.buildKtType(this)
|
fun ConeKotlinType.asKtType() = analysisSession.firSymbolBuilder.buildKtType(this)
|
||||||
|
|
||||||
|
fun createTypeCheckerContext() = ConeTypeCheckerContext(
|
||||||
|
isErrorTypeEqualsToAnything = true,
|
||||||
|
isStubTypeEqualsToAnything = true,
|
||||||
|
analysisSession.firResolveState.rootModuleSession //TODO use correct session here
|
||||||
|
)
|
||||||
}
|
}
|
||||||
+107
@@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.frontend.api.fir.components
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
|
||||||
|
import org.jetbrains.kotlin.fir.expressions.FirExpression
|
||||||
|
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
|
||||||
|
import org.jetbrains.kotlin.fir.expressions.argumentMapping
|
||||||
|
import org.jetbrains.kotlin.fir.psi
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.inference.isBuiltinFunctionalType
|
||||||
|
import org.jetbrains.kotlin.fir.types.ConeTypeCheckerContext
|
||||||
|
import org.jetbrains.kotlin.fir.types.coneType
|
||||||
|
import org.jetbrains.kotlin.idea.fir.low.level.api.api.getOrBuildFirOfType
|
||||||
|
import org.jetbrains.kotlin.idea.fir.low.level.api.api.getOrBuildFirSafe
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.ValidityToken
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.assertIsValid
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.components.KtBuiltinTypes
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.components.KtExpressionTypeProvider
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.fir.types.KtFirType
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.withValidityAssertion
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||||
|
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||||
|
|
||||||
|
internal class KtFirExpressionTypeProvider(
|
||||||
|
override val analysisSession: KtFirAnalysisSession,
|
||||||
|
override val token: ValidityToken,
|
||||||
|
) : KtExpressionTypeProvider(), KtFirAnalysisSessionComponent {
|
||||||
|
override fun getReturnTypeForKtDeclaration(declaration: KtDeclaration): KtType = withValidityAssertion {
|
||||||
|
val firDeclaration = declaration.getOrBuildFirOfType<FirCallableDeclaration<*>>(firResolveState)
|
||||||
|
firDeclaration.returnTypeRef.coneType.asKtType()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getKtExpressionType(expression: KtExpression): KtType = withValidityAssertion {
|
||||||
|
expression.getOrBuildFirOfType<FirExpression>(firResolveState).typeRef.coneType.asKtType()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getExpectedType(expression: PsiElement): KtType? =
|
||||||
|
getExpectedTypeByReturnExpression(expression)
|
||||||
|
?: getExpressionTypeByIfOrBooleanCondition(expression)
|
||||||
|
?: getExpectedTypeOfFunctionParameter(expression)
|
||||||
|
|
||||||
|
private fun getExpectedTypeOfFunctionParameter(expression: PsiElement): KtType? {
|
||||||
|
val (ktCallExpression, ktArgument) = expression.getFunctionCallAsWithThisAsParameter() ?: return null
|
||||||
|
val firCall = ktCallExpression.getOrBuildFirSafe<FirFunctionCall>(firResolveState) ?: return null
|
||||||
|
val arguments = firCall.argumentMapping ?: return null
|
||||||
|
val firParameterForExpression = arguments.entries.firstOrNull { (arg, _) -> arg.psi == ktArgument }?.value ?: return null
|
||||||
|
return firParameterForExpression.returnTypeRef.coneType.asKtType()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun PsiElement.getFunctionCallAsWithThisAsParameter(): KtCallWithArgument? {
|
||||||
|
val valueArgument = unwrapQualified<KtValueArgument> { valueArg, expr -> valueArg.getArgumentExpression() == expr } ?: return null
|
||||||
|
val argumentsList = valueArgument.parent as? KtValueArgumentList ?: return null
|
||||||
|
val callExpression = argumentsList.parent as? KtCallExpression ?: return null
|
||||||
|
val argumentExpression = valueArgument.getArgumentExpression() ?: return null
|
||||||
|
return KtCallWithArgument(callExpression, argumentExpression)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getExpectedTypeByReturnExpression(expression: PsiElement): KtType? {
|
||||||
|
val returnParent = expression.getReturnExpressionWithThisType() ?: return null
|
||||||
|
val targetSymbol = with(analysisSession) { returnParent.getReturnTargetSymbol() } ?: return null
|
||||||
|
return targetSymbol.type
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun PsiElement.getReturnExpressionWithThisType(): KtReturnExpression? =
|
||||||
|
unwrapQualified { returnExpr, target -> returnExpr.returnedExpression == target }
|
||||||
|
|
||||||
|
private fun getExpressionTypeByIfOrBooleanCondition(expression: PsiElement): KtType? = when {
|
||||||
|
expression.isWhileLoopCondition() || expression.isIfCondition() -> with(analysisSession) { builtinTypes.BOOLEAN }
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun PsiElement.isWhileLoopCondition() =
|
||||||
|
unwrapQualified<KtWhileExpressionBase> { whileExpr, cond -> whileExpr.condition == cond } != null
|
||||||
|
|
||||||
|
private fun PsiElement.isIfCondition() =
|
||||||
|
unwrapQualified<KtIfExpression> { ifExpr, cond -> ifExpr.condition == cond } != null
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class KtCallWithArgument(val call: KtCallExpression, val argument: KtExpression)
|
||||||
|
|
||||||
|
private inline fun <reified R : Any> PsiElement.unwrapQualified(check: (R, PsiElement) -> Boolean): R? {
|
||||||
|
val parent = nonContainerParent
|
||||||
|
return when {
|
||||||
|
parent is R && check(parent, this) -> parent
|
||||||
|
parent is KtQualifiedExpression && parent.selectorExpression == this -> {
|
||||||
|
val grandParent = parent.nonContainerParent
|
||||||
|
when {
|
||||||
|
grandParent is R && check(grandParent, parent) -> grandParent
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val PsiElement.nonContainerParent: PsiElement?
|
||||||
|
get() = when (val parent = parent) {
|
||||||
|
is KtContainerNode -> parent.parent
|
||||||
|
else -> parent
|
||||||
|
}
|
||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.frontend.api.fir.components
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.ValidityToken
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.assertIsValid
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.components.KtSubtypingComponent
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.fir.types.KtFirType
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
|
||||||
|
import org.jetbrains.kotlin.idea.frontend.api.withValidityAssertion
|
||||||
|
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||||
|
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||||
|
|
||||||
|
internal class KtFirSubtypingComponent(
|
||||||
|
override val analysisSession: KtFirAnalysisSession,
|
||||||
|
override val token: ValidityToken,
|
||||||
|
) : KtSubtypingComponent(), KtFirAnalysisSessionComponent {
|
||||||
|
override fun isEqualTo(first: KtType, second: KtType): Boolean = withValidityAssertion {
|
||||||
|
second.assertIsValid()
|
||||||
|
check(first is KtFirType)
|
||||||
|
check(second is KtFirType)
|
||||||
|
return AbstractTypeChecker.equalTypes(
|
||||||
|
createTypeCheckerContext() as AbstractTypeCheckerContext,
|
||||||
|
first.coneType,
|
||||||
|
second.coneType
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isSubTypeOf(subType: KtType, superType: KtType): Boolean = withValidityAssertion {
|
||||||
|
superType.assertIsValid()
|
||||||
|
check(subType is KtFirType)
|
||||||
|
check(superType is KtFirType)
|
||||||
|
return AbstractTypeChecker.isSubtypeOf(
|
||||||
|
createTypeCheckerContext() as AbstractTypeCheckerContext,
|
||||||
|
subType.coneType,
|
||||||
|
superType.coneType
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
-120
@@ -5,110 +5,19 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.frontend.api.fir.components
|
package org.jetbrains.kotlin.idea.frontend.api.fir.components
|
||||||
|
|
||||||
import com.intellij.psi.PsiElement
|
|
||||||
import com.jetbrains.rd.util.firstOrNull
|
|
||||||
import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
|
|
||||||
import org.jetbrains.kotlin.fir.expressions.FirCall
|
|
||||||
import org.jetbrains.kotlin.fir.expressions.FirExpression
|
|
||||||
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
|
|
||||||
import org.jetbrains.kotlin.fir.expressions.argumentMapping
|
|
||||||
import org.jetbrains.kotlin.fir.psi
|
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.isBuiltinFunctionalType
|
import org.jetbrains.kotlin.fir.resolve.inference.isBuiltinFunctionalType
|
||||||
import org.jetbrains.kotlin.fir.types.ConeTypeCheckerContext
|
|
||||||
import org.jetbrains.kotlin.fir.types.coneType
|
|
||||||
import org.jetbrains.kotlin.idea.fir.low.level.api.api.getOrBuildFir
|
|
||||||
import org.jetbrains.kotlin.idea.fir.low.level.api.api.getOrBuildFirOfType
|
|
||||||
import org.jetbrains.kotlin.idea.fir.low.level.api.api.getOrBuildFirSafe
|
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.ValidityToken
|
import org.jetbrains.kotlin.idea.frontend.api.ValidityToken
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.assertIsValid
|
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.components.KtBuiltinTypes
|
import org.jetbrains.kotlin.idea.frontend.api.components.KtBuiltinTypes
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.components.KtTypeProvider
|
import org.jetbrains.kotlin.idea.frontend.api.components.KtTypeProvider
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession
|
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.fir.types.KtFirType
|
import org.jetbrains.kotlin.idea.frontend.api.fir.types.KtFirType
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
|
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
|
||||||
import org.jetbrains.kotlin.idea.frontend.api.withValidityAssertion
|
import org.jetbrains.kotlin.idea.frontend.api.withValidityAssertion
|
||||||
import org.jetbrains.kotlin.psi.*
|
|
||||||
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
|
||||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
|
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
internal class KtFirTypeProvider(
|
internal class KtFirTypeProvider(
|
||||||
override val analysisSession: KtFirAnalysisSession,
|
override val analysisSession: KtFirAnalysisSession,
|
||||||
override val token: ValidityToken,
|
override val token: ValidityToken,
|
||||||
) : KtTypeProvider(), KtFirAnalysisSessionComponent {
|
) : KtTypeProvider(), KtFirAnalysisSessionComponent {
|
||||||
override fun getReturnTypeForKtDeclaration(declaration: KtDeclaration): KtType = withValidityAssertion {
|
|
||||||
val firDeclaration = declaration.getOrBuildFirOfType<FirCallableDeclaration<*>>(firResolveState)
|
|
||||||
firDeclaration.returnTypeRef.coneType.asKtType()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getKtExpressionType(expression: KtExpression): KtType = withValidityAssertion {
|
|
||||||
expression.getOrBuildFirOfType<FirExpression>(firResolveState).typeRef.coneType.asKtType()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getExpectedType(expression: PsiElement): KtType? =
|
|
||||||
getExpectedTypeByReturnExpression(expression)
|
|
||||||
?: getExpressionTypeByIfOrBooleanCondition(expression)
|
|
||||||
?: getExpectedTypeOfFunctionParameter(expression)
|
|
||||||
|
|
||||||
private fun getExpectedTypeOfFunctionParameter(expression: PsiElement): KtType? {
|
|
||||||
val (ktCallExpression, ktArgument) = expression.getFunctionCallAsWithThisAsParameter() ?: return null
|
|
||||||
val firCall = ktCallExpression.getOrBuildFirSafe<FirFunctionCall>(firResolveState) ?: return null
|
|
||||||
val arguments = firCall.argumentMapping ?: return null
|
|
||||||
val firParameterForExpression = arguments.entries.firstOrNull { (arg, _) -> arg.psi == ktArgument }?.value ?: return null
|
|
||||||
return firParameterForExpression.returnTypeRef.coneType.asKtType()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun PsiElement.getFunctionCallAsWithThisAsParameter(): KtCallWithArgument? {
|
|
||||||
val valueArgument = unwrapQualified<KtValueArgument> { valueArg, expr -> valueArg.getArgumentExpression() == expr } ?: return null
|
|
||||||
val argumentsList = valueArgument.parent as? KtValueArgumentList ?: return null
|
|
||||||
val callExpression = argumentsList.parent as? KtCallExpression ?: return null
|
|
||||||
val argumentExpression = valueArgument.getArgumentExpression() ?: return null
|
|
||||||
return KtCallWithArgument(callExpression, argumentExpression)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getExpectedTypeByReturnExpression(expression: PsiElement): KtType? {
|
|
||||||
val returnParent = expression.getReturnExpressionWithThisType() ?: return null
|
|
||||||
val targetSymbol = with(analysisSession) { returnParent.getReturnTargetSymbol() } ?: return null
|
|
||||||
return targetSymbol.type
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun PsiElement.getReturnExpressionWithThisType(): KtReturnExpression? =
|
|
||||||
unwrapQualified { returnExpr, target -> returnExpr.returnedExpression == target }
|
|
||||||
|
|
||||||
private fun getExpressionTypeByIfOrBooleanCondition(expression: PsiElement): KtType? = when {
|
|
||||||
expression.isWhileLoopCondition() || expression.isIfCondition() -> builtinTypes.BOOLEAN
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun PsiElement.isWhileLoopCondition() =
|
|
||||||
unwrapQualified<KtWhileExpressionBase> { whileExpr, cond -> whileExpr.condition == cond } != null
|
|
||||||
|
|
||||||
private fun PsiElement.isIfCondition() =
|
|
||||||
unwrapQualified<KtIfExpression> { ifExpr, cond -> ifExpr.condition == cond } != null
|
|
||||||
|
|
||||||
override fun isEqualTo(first: KtType, second: KtType): Boolean = withValidityAssertion {
|
|
||||||
second.assertIsValid()
|
|
||||||
check(first is KtFirType)
|
|
||||||
check(second is KtFirType)
|
|
||||||
return AbstractTypeChecker.equalTypes(
|
|
||||||
this.createTypeCheckerContext() as AbstractTypeCheckerContext,
|
|
||||||
first.coneType,
|
|
||||||
second.coneType
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isSubTypeOf(subType: KtType, superType: KtType): Boolean = withValidityAssertion {
|
|
||||||
superType.assertIsValid()
|
|
||||||
check(subType is KtFirType)
|
|
||||||
check(superType is KtFirType)
|
|
||||||
return AbstractTypeChecker.isSubtypeOf(
|
|
||||||
this.createTypeCheckerContext() as AbstractTypeCheckerContext,
|
|
||||||
subType.coneType,
|
|
||||||
superType.coneType
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isBuiltinFunctionalType(type: KtType): Boolean = withValidityAssertion {
|
override fun isBuiltinFunctionalType(type: KtType): Boolean = withValidityAssertion {
|
||||||
check(type is KtFirType)
|
check(type is KtFirType)
|
||||||
type.coneType.isBuiltinFunctionalType(analysisSession.firResolveState.rootModuleSession) //TODO use correct session here
|
type.coneType.isBuiltinFunctionalType(analysisSession.firResolveState.rootModuleSession) //TODO use correct session here
|
||||||
@@ -116,33 +25,4 @@ internal class KtFirTypeProvider(
|
|||||||
|
|
||||||
override val builtinTypes: KtBuiltinTypes =
|
override val builtinTypes: KtBuiltinTypes =
|
||||||
KtFirBuiltInTypes(analysisSession.firResolveState.rootModuleSession.builtinTypes, analysisSession.firSymbolBuilder, token)
|
KtFirBuiltInTypes(analysisSession.firResolveState.rootModuleSession.builtinTypes, analysisSession.firSymbolBuilder, token)
|
||||||
|
|
||||||
private fun createTypeCheckerContext() = ConeTypeCheckerContext(
|
|
||||||
isErrorTypeEqualsToAnything = true,
|
|
||||||
isStubTypeEqualsToAnything = true,
|
|
||||||
analysisSession.firResolveState.rootModuleSession //TODO use correct session here
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class KtCallWithArgument(val call: KtCallExpression, val argument: KtExpression)
|
|
||||||
|
|
||||||
private inline fun <reified R : Any> PsiElement.unwrapQualified(check: (R, PsiElement) -> Boolean): R? {
|
|
||||||
val parent = nonContainerParent
|
|
||||||
return when {
|
|
||||||
parent is R && check(parent, this) -> parent
|
|
||||||
parent is KtQualifiedExpression && parent.selectorExpression == this -> {
|
|
||||||
val grandParent = parent.nonContainerParent
|
|
||||||
when {
|
|
||||||
grandParent is R && check(grandParent, parent) -> grandParent
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private val PsiElement.nonContainerParent: PsiElement?
|
|
||||||
get() = when (val parent = parent) {
|
|
||||||
is KtContainerNode -> parent.parent
|
|
||||||
else -> parent
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user