Get rid of binding context in FirBodyResolveTransformer

This commit is contained in:
Mikhail Glukhikh
2019-03-25 11:44:24 +03:00
parent 8664fc3b28
commit 73ed52a511
2 changed files with 23 additions and 26 deletions
@@ -20,10 +20,7 @@ import org.jetbrains.kotlin.fir.scopes.processClassifiersByNameWithAction
import org.jetbrains.kotlin.fir.service import org.jetbrains.kotlin.fir.service
import org.jetbrains.kotlin.fir.symbols.* import org.jetbrains.kotlin.fir.symbols.*
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinErrorType import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.coneTypeUnsafe
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.utils.addToStdlib.cast import org.jetbrains.kotlin.utils.addToStdlib.cast
@@ -207,7 +204,7 @@ fun createSimpleConsumer(
return if (explicitReceiver != null) { return if (explicitReceiver != null) {
ExplicitReceiverTowerDataConsumer(session, name, token, object : ReceiverValueWithPossibleTypes { ExplicitReceiverTowerDataConsumer(session, name, token, object : ReceiverValueWithPossibleTypes {
override val type: ConeKotlinType override val type: ConeKotlinType
get() = explicitReceiverType?.coneTypeUnsafe() get() = explicitReceiverType?.coneTypeSafe()
?: ConeKotlinErrorType("No type calculated for: ${explicitReceiver.renderWithType()}") // TODO: assert here ?: ConeKotlinErrorType("No type calculated for: ${explicitReceiver.renderWithType()}") // TODO: assert here
}, callKind) }, callKind)
} else { } else {
@@ -91,7 +91,7 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
val resolved = super.transformTypeOperatorCall(typeOperatorCall, data).single val resolved = super.transformTypeOperatorCall(typeOperatorCall, data).single
when ((resolved as FirTypeOperatorCall).operation) { when ((resolved as FirTypeOperatorCall).operation) {
FirOperation.IS, FirOperation.NOT_IS -> { FirOperation.IS, FirOperation.NOT_IS -> {
bindingContext[resolved] = FirResolvedTypeRefImpl( resolved.resultType = FirResolvedTypeRefImpl(
session, session,
null, null,
StandardClassIds.Boolean(symbolProvider).constructType(emptyArray(), isNullable = false), StandardClassIds.Boolean(symbolProvider).constructType(emptyArray(), isNullable = false),
@@ -100,10 +100,10 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
) )
} }
FirOperation.AS -> { FirOperation.AS -> {
bindingContext[resolved] = resolved.conversionTypeRef resolved.resultType = resolved.conversionTypeRef
} }
FirOperation.SAFE_AS -> { FirOperation.SAFE_AS -> {
bindingContext[resolved] = resolved.resultType =
resolved.conversionTypeRef.withReplacedConeType( resolved.conversionTypeRef.withReplacedConeType(
session, session,
resolved.conversionTypeRef.coneTypeUnsafe().withNullability(ConeNullability.NULLABLE) resolved.conversionTypeRef.coneTypeUnsafe().withNullability(ConeNullability.NULLABLE)
@@ -134,7 +134,7 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
val jump = ReturnTypeCalculatorWithJump(session) val jump = ReturnTypeCalculatorWithJump(session)
private fun <T> storeTypeFromCallee(access: T) where T : FirQualifiedAccess, T : FirExpression { private fun <T> storeTypeFromCallee(access: T) where T : FirQualifiedAccess, T : FirExpression {
bindingContext[access] = access.resultType =
when (val newCallee = access.calleeReference) { when (val newCallee = access.calleeReference) {
is FirErrorNamedReference -> is FirErrorNamedReference ->
FirErrorTypeRefImpl(session, access.psi, newCallee.errorReason) FirErrorTypeRefImpl(session, access.psi, newCallee.errorReason)
@@ -155,7 +155,7 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
val labelName = callee.labelName val labelName = callee.labelName
val types = if (labelName == null) labels.values() else labels[Name.identifier(labelName)] val types = if (labelName == null) labels.values() else labels[Name.identifier(labelName)]
val type = types.lastOrNull() ?: ConeKotlinErrorType("Unresolved this@$labelName") val type = types.lastOrNull() ?: ConeKotlinErrorType("Unresolved this@$labelName")
bindingContext[qualifiedAccessExpression] = FirResolvedTypeRefImpl(session, null, type, false, emptyList()) qualifiedAccessExpression.resultType = FirResolvedTypeRefImpl(session, null, type, false, emptyList())
} }
is FirSuperReference -> { is FirSuperReference -> {
qualifiedAccessExpression.resultType = qualifiedAccessExpression.resultType =
@@ -275,7 +275,7 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
is FirExpression -> statement is FirExpression -> statement
else -> null else -> null
} }
resultExpression?.resultType?.let { bindingContext[block] = it } (resultExpression?.resultType as? FirResolvedTypeRef)?.let { block.resultType = it }
return block.compose() return block.compose()
} }
@@ -288,7 +288,7 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
val type = commonSuperType(whenExpression.branches.mapNotNull { val type = commonSuperType(whenExpression.branches.mapNotNull {
it.result.resultType it.result.resultType
}) })
if (type != null) bindingContext[whenExpression] = type if (type != null) whenExpression.resultType = type
return whenExpression.compose() return whenExpression.compose()
} }
@@ -313,22 +313,19 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
val type = ConeClassTypeImpl(symbol.toLookupTag(), emptyArray(), isNullable = constExpression.kind == IrConstKind.Null) val type = ConeClassTypeImpl(symbol.toLookupTag(), emptyArray(), isNullable = constExpression.kind == IrConstKind.Null)
bindingContext[constExpression] = FirResolvedTypeRefImpl(session, null, type, false, emptyList()) constExpression.resultType = FirResolvedTypeRefImpl(session, null, type, false, emptyList())
} else { } else {
bindingContext[constExpression] = expectedType constExpression.resultType = expectedType
} }
return super.transformConstExpression(constExpression, data) return super.transformConstExpression(constExpression, data)
} }
@Deprecated("It is temp", level = DeprecationLevel.WARNING, replaceWith = ReplaceWith("TODO(\"что-то нормальное\")")) private var FirExpression.resultType: FirTypeRef
val bindingContext = mutableMapOf<FirExpression, FirTypeRef>() get() = typeRef
var FirExpression.resultType: FirTypeRef?
get() = bindingContext[this]
set(type) { set(type) {
if (type != null) bindingContext[this] = type replaceTypeRef(type)
} }
@@ -368,11 +365,14 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
initializer != null -> { initializer != null -> {
variable.transformReturnTypeRef( variable.transformReturnTypeRef(
this, this,
initializer.resultType ?: FirErrorTypeRefImpl( when (val resultType = initializer.resultType) {
session, is FirImplicitTypeRef -> FirErrorTypeRefImpl(
null, session,
"No result type for initializer" null,
) "No result type for initializer"
)
else -> resultType
}
) )
} }
variable.delegate != null -> { variable.delegate != null -> {
@@ -400,7 +400,7 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
} }
override fun transformExpression(expression: FirExpression, data: Any?): CompositeTransformResult<FirStatement> { override fun transformExpression(expression: FirExpression, data: Any?): CompositeTransformResult<FirStatement> {
if (expression.resultType == null) { if (expression.resultType is FirImplicitTypeRef) {
val type = FirErrorTypeRefImpl(session, expression.psi, "Type calculating for ${expression.render()} is not supported") val type = FirErrorTypeRefImpl(session, expression.psi, "Type calculating for ${expression.render()} is not supported")
expression.resultType = type expression.resultType = type
} }