FIR: Simplify callable references resolution

Also that fixes some bugs
This commit is contained in:
Denis Zharkov
2020-11-16 13:09:55 +03:00
parent 387fd895a6
commit 14305d1eba
15 changed files with 48 additions and 84 deletions
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.builder.buildExpressionStub
import org.jetbrains.kotlin.fir.expressions.builder.buildResolvedReifiedParameterReference
import org.jetbrains.kotlin.fir.references.*
import org.jetbrains.kotlin.fir.references.builder.buildBackingFieldReference
@@ -400,7 +399,9 @@ class FirCallResolver(
}
}
if (constructorSymbol == null) return null
val candidate = CandidateFactory(transformer.resolutionContext, callInfo).createCandidate(
val candidateFactory = CandidateFactory(transformer.resolutionContext, callInfo)
val candidate = candidateFactory.createCandidate(
callInfo,
constructorSymbol!!,
ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
scope = null
@@ -471,12 +472,6 @@ class FirCallResolver(
expectedType,
outerConstraintSystemBuilder,
lhs,
stubReceiver = if (lhs !is DoubleColonLHS.Type) null else buildExpressionStub {
source = callableReferenceAccess.source
typeRef = buildResolvedTypeRef {
type = lhs.type
}
},
)
}
@@ -547,7 +542,7 @@ class FirCallResolver(
source: FirSourceElement?,
name: Name
): FirErrorReferenceWithCandidate {
val candidate = CandidateFactory(transformer.resolutionContext, callInfo).createErrorCandidate(diagnostic)
val candidate = CandidateFactory(transformer.resolutionContext, callInfo).createErrorCandidate(callInfo, diagnostic)
components.resolutionStageRunner.processCandidate(candidate, transformer.resolutionContext, stopOnFirstError = false)
return FirErrorReferenceWithCandidate(source, name, candidate, diagnostic)
}
@@ -52,16 +52,12 @@ data class CallInfo(
// Four properties for callable references only
val expectedType: ConeKotlinType? = null,
val outerCSBuilder: ConstraintSystemBuilder? = null,
val lhs: DoubleColonLHS? = null,
val stubReceiver: FirExpression? = null
val lhs: DoubleColonLHS? = null
) {
val arguments: List<FirExpression> get() = argumentList.arguments
val argumentCount get() = arguments.size
fun noStubReceiver(): CallInfo =
if (stubReceiver == null) this else copy(stubReceiver = null)
fun replaceWithVariableAccess(): CallInfo =
copy(callKind = CallKind.VariableAccess, typeArguments = emptyList(), argumentList = FirEmptyArgumentList)
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
class CandidateFactory private constructor(
val context: ResolutionContext,
val callInfo: CallInfo,
private val baseSystem: ConstraintStorage
) {
@@ -37,17 +36,10 @@ class CandidateFactory private constructor(
}
}
constructor(context: ResolutionContext, callInfo: CallInfo) :
this(context, callInfo, buildBaseSystem(context, callInfo))
fun replaceCallInfo(callInfo: CallInfo): CandidateFactory {
if (this.callInfo.arguments.size != callInfo.arguments.size) {
throw AssertionError("Incorrect replacement of call info in CandidateFactory")
}
return CandidateFactory(context, callInfo, baseSystem)
}
constructor(context: ResolutionContext, callInfo: CallInfo) : this(context, buildBaseSystem(context, callInfo))
fun createCandidate(
callInfo: CallInfo,
symbol: AbstractFirBasedSymbol<*>,
explicitReceiverKind: ExplicitReceiverKind,
scope: FirScope?,
@@ -65,7 +57,7 @@ class CandidateFactory private constructor(
)
}
fun createErrorCandidate(diagnostic: ConeDiagnostic): Candidate {
fun createErrorCandidate(callInfo: CallInfo, diagnostic: ConeDiagnostic): Candidate {
val symbol: AbstractFirBasedSymbol<*> = when (callInfo.callKind) {
is CallKind.VariableAccess -> createErrorPropertySymbol(diagnostic)
is CallKind.Function,
@@ -245,7 +245,6 @@ internal class FirInvokeResolveTowerExtension(
receiverGroup,
candidateFactoriesAndCollectors.resultCollector,
candidateFactory,
candidateFactoriesAndCollectors.stubReceiverCandidateFactory
)
}
@@ -307,7 +306,6 @@ private class InvokeReceiverResolveTask(
towerDataElementsForName,
collector,
candidateFactory,
stubReceiverCandidateFactory = null
) {
override fun interceptTowerGroup(towerGroup: TowerGroup): TowerGroup =
towerGroup.InvokeResolvePriority(InvokeResolvePriority.INVOKE_RECEIVER)
@@ -324,14 +322,12 @@ private class InvokeFunctionResolveTask(
private val receiverGroup: TowerGroup,
collector: CandidateCollector,
candidateFactory: CandidateFactory,
stubReceiverCandidateFactory: CandidateFactory? = null
) : FirBaseTowerResolveTask(
components,
manager,
towerDataElementsForName,
collector,
candidateFactory,
stubReceiverCandidateFactory
) {
override fun interceptTowerGroup(towerGroup: TowerGroup): TowerGroup =
@@ -9,12 +9,15 @@ import org.jetbrains.kotlin.fir.asReversedFrozen
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier
import org.jetbrains.kotlin.fir.expressions.builder.buildExpressionStub
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
import org.jetbrains.kotlin.fir.resolve.DoubleColonLHS
import org.jetbrains.kotlin.fir.resolve.FirTowerDataContext
import org.jetbrains.kotlin.fir.resolve.calls.*
import org.jetbrains.kotlin.fir.scopes.FirScope
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.impl.FirImplicitBuiltinTypeRef
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
@@ -53,8 +56,7 @@ internal abstract class FirBaseTowerResolveTask(
private val manager: TowerResolveManager,
protected val towerDataElementsForName: TowerDataElementsForName,
private val collector: CandidateCollector,
private val candidateFactory: CandidateFactory,
private val stubReceiverCandidateFactory: CandidateFactory? = null
private val candidateFactory: CandidateFactory
) {
protected val session get() = components.session
@@ -131,7 +133,6 @@ internal abstract class FirBaseTowerResolveTask(
val result = handler.handleLevel(
collector,
candidateFactory,
stubReceiverCandidateFactory,
callInfo,
explicitReceiverKind,
finalGroup,
@@ -149,14 +150,12 @@ internal open class FirTowerResolveTask(
towerDataElementsForName: TowerDataElementsForName,
collector: CandidateCollector,
candidateFactory: CandidateFactory,
stubReceiverCandidateFactory: CandidateFactory? = null
) : FirBaseTowerResolveTask(
components,
manager,
towerDataElementsForName,
collector,
candidateFactory,
stubReceiverCandidateFactory
) {
suspend fun runResolverForQualifierReceiver(
@@ -178,8 +177,17 @@ internal open class FirTowerResolveTask(
if (resolvedQualifier.symbol != null) {
val typeRef = resolvedQualifier.typeRef
if (info.callKind == CallKind.CallableReference && info.stubReceiver != null ) {
runResolverForExpressionReceiver(info, info.stubReceiver, parentGroup = TowerGroup.QualifierValue)
if (info.callKind == CallKind.CallableReference && info.lhs is DoubleColonLHS.Type) {
val stubReceiver = buildExpressionStub {
source = info.explicitReceiver?.source
this.typeRef = buildResolvedTypeRef {
type = info.lhs.type
}
}
val stubReceiverInfo = info.replaceExplicitReceiver(stubReceiver)
runResolverForExpressionReceiver(stubReceiverInfo, stubReceiver, parentGroup = TowerGroup.QualifierValue)
}
// NB: yet built-in Unit is used for "no-value" type
@@ -197,7 +205,7 @@ internal open class FirTowerResolveTask(
val callableScope = qualifierReceiver.callableScope() ?: return
processLevel(
callableScope.toScopeTowerLevel(includeInnerConstructors = false),
info.noStubReceiver(), TowerGroup.Qualifier
info, TowerGroup.Qualifier
)
}
@@ -212,7 +220,7 @@ internal open class FirTowerResolveTask(
val scope = qualifierReceiver.classifierScope() ?: return
val group = if (prioritized) TowerGroup.ClassifierPrioritized else TowerGroup.Classifier
processLevel(
scope.toScopeTowerLevel(includeInnerConstructors = false), info.noStubReceiver(),
scope.toScopeTowerLevel(includeInnerConstructors = false), info,
group
)
}
@@ -52,7 +52,6 @@ class FirTowerResolver(
TowerDataElementsForName(info.name, components.towerDataContext),
candidateFactoriesAndCollectors.resultCollector,
candidateFactoriesAndCollectors.candidateFactory,
candidateFactoriesAndCollectors.stubReceiverCandidateFactory
)
when (val receiver = info.explicitReceiver) {
is FirResolvedQualifier -> {
@@ -103,6 +102,7 @@ class FirTowerResolver(
resultCollector.consumeCandidate(
TowerGroup.Member,
candidateFactory.createCandidate(
info,
it,
ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
scope,
@@ -123,16 +123,10 @@ class FirTowerResolver(
context: ResolutionContext
): CandidateFactoriesAndCollectors {
val candidateFactory = CandidateFactory(context, info)
val stubReceiverCandidateFactory =
if (info.callKind == CallKind.CallableReference && info.stubReceiver != null)
candidateFactory.replaceCallInfo(info.replaceExplicitReceiver(info.stubReceiver))
else
null
return CandidateFactoriesAndCollectors(
candidateFactory,
collector,
stubReceiverCandidateFactory
collector
)
}
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.fir.resolve.calls.tower
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier
import org.jetbrains.kotlin.fir.resolve.calls.*
import org.jetbrains.kotlin.fir.scopes.FirScope
@@ -18,7 +17,6 @@ import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.fir.types.ConeStarProjection
import org.jetbrains.kotlin.fir.types.coneType
import org.jetbrains.kotlin.fir.types.constructClassType
import org.jetbrains.kotlin.fir.types.impl.FirImplicitBuiltinTypeRef
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.types.AbstractTypeChecker
@@ -27,9 +25,6 @@ internal class CandidateFactoriesAndCollectors(
// Common calls
val candidateFactory: CandidateFactory,
val resultCollector: CandidateCollector,
// Callable references
val stubReceiverCandidateFactory: CandidateFactory?,
)
@@ -41,7 +36,6 @@ internal class TowerLevelHandler {
fun handleLevel(
collector: CandidateCollector,
candidateFactory: CandidateFactory,
stubReceiverCandidateFactory: CandidateFactory? = null,
info: CallInfo,
explicitReceiverKind: ExplicitReceiverKind,
group: TowerGroup,
@@ -50,7 +44,7 @@ internal class TowerLevelHandler {
processResult = ProcessorAction.NONE
val processor =
TowerScopeLevelProcessor(
info.explicitReceiver,
info,
explicitReceiverKind,
collector,
candidateFactory,
@@ -69,22 +63,7 @@ internal class TowerLevelHandler {
towerLevel.processFunctions(info.name, processor)
}
CallKind.CallableReference -> {
val stubReceiver = info.stubReceiver
if (stubReceiver != null) {
val stubProcessor = TowerScopeLevelProcessor(
info.explicitReceiver,
explicitReceiverKind,
collector,
stubReceiverCandidateFactory!!, group
)
towerLevel.processFunctionsAndProperties(info.name, stubProcessor)
// NB: we don't perform this for implicit Unit
if (!collector.isSuccess() && info.explicitReceiver?.typeRef !is FirImplicitBuiltinTypeRef) {
towerLevel.processFunctionsAndProperties(info.name, processor)
}
} else {
towerLevel.processFunctionsAndProperties(info.name, processor)
}
towerLevel.processFunctionsAndProperties(info.name, processor)
}
else -> {
throw AssertionError("Unsupported call kind in tower resolver: ${info.callKind}")
@@ -135,7 +114,7 @@ internal class TowerLevelHandler {
}
private class TowerScopeLevelProcessor(
val explicitReceiver: FirExpression?,
val callInfo: CallInfo,
val explicitReceiverKind: ExplicitReceiverKind,
val resultCollector: CandidateCollector,
val candidateFactory: CandidateFactory,
@@ -151,7 +130,7 @@ private class TowerScopeLevelProcessor(
// Check explicit extension receiver for default package members
if (symbol is FirNamedFunctionSymbol && dispatchReceiverValue == null &&
extensionReceiverValue != null &&
explicitReceiver !is FirResolvedQualifier &&
callInfo.explicitReceiver !is FirResolvedQualifier &&
symbol.callableId.packageName.startsWith(defaultPackage)
) {
val extensionReceiverType = extensionReceiverValue.type as? ConeClassLikeType
@@ -175,6 +154,7 @@ private class TowerScopeLevelProcessor(
// ---
resultCollector.consumeCandidate(
group, candidateFactory.createCandidate(
callInfo,
symbol,
explicitReceiverKind,
scope,
@@ -172,12 +172,15 @@ class FirSyntheticCallGenerator(
return FirNamedReferenceWithCandidate(null, name, candidate)
}
private fun generateCandidate(callInfo: CallInfo, function: FirSimpleFunction, context: ResolutionContext): Candidate =
CandidateFactory(context, callInfo).createCandidate(
private fun generateCandidate(callInfo: CallInfo, function: FirSimpleFunction, context: ResolutionContext): Candidate {
val candidateFactory = CandidateFactory(context, callInfo)
return candidateFactory.createCandidate(
callInfo,
symbol = function.symbol,
explicitReceiverKind = ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
scope = null
)
}
private fun generateCallInfo(name: Name, argumentList: FirArgumentList, callKind: CallKind) = CallInfo(
callKind = callKind,