FIR: Extract some parts from FirTowerResolver and cleanup

This commit is contained in:
Denis Zharkov
2020-03-26 09:50:51 +03:00
parent 7843f7d3f0
commit a5c3e4bfd4
@@ -164,19 +164,18 @@ class FirTowerResolver(
info: CallInfo, info: CallInfo,
manager: TowerResolveManager manager: TowerResolveManager
) { ) {
processExtensionsThatHideMembers(info, manager, explicitReceiverValue = null) manager.processExtensionsThatHideMembers(info, explicitReceiverValue = null)
val nonEmptyLocalScopes = mutableListOf<FirLocalScope>() val nonEmptyLocalScopes = mutableListOf<FirLocalScope>()
processLocalScopes(manager, info, nonEmptyLocalScopes) manager.processLocalScopesWithNoReceiver(info, nonEmptyLocalScopes)
val emptyTopLevelScopes = mutableSetOf<FirScope>() val emptyTopLevelScopes = mutableSetOf<FirScope>()
processImplicitReceivers(manager, info, nonEmptyLocalScopes, emptyTopLevelScopes) manager.processImplicitReceiversWithNoExplicit(info, nonEmptyLocalScopes, emptyTopLevelScopes)
processTopLevelScopes(info, emptyTopLevelScopes, manager) manager.processTopLevelScopesNoReceiver(info, emptyTopLevelScopes)
} }
private suspend fun processExtensionsThatHideMembers( private suspend fun TowerResolveManager.processExtensionsThatHideMembers(
info: CallInfo, info: CallInfo,
manager: TowerResolveManager,
explicitReceiverValue: ReceiverValue? explicitReceiverValue: ReceiverValue?
) { ) {
val shouldProcessExtensionsBeforeMembers = val shouldProcessExtensionsBeforeMembers =
@@ -186,11 +185,11 @@ class FirTowerResolver(
for ((index, topLevelScope) in topLevelScopes.withIndex()) { for ((index, topLevelScope) in topLevelScopes.withIndex()) {
if (explicitReceiverValue != null) { if (explicitReceiverValue != null) {
manager.processHideMembersLevel(explicitReceiverValue, topLevelScope, info, index, depth = null) processHideMembersLevel(explicitReceiverValue, topLevelScope, info, index, depth = null)
} else { } else {
for ((implicitReceiverValue, usableAsValue, depth) in implicitReceivers) { for ((implicitReceiverValue, usableAsValue, depth) in implicitReceivers) {
if (!usableAsValue) continue if (!usableAsValue) continue
manager.processHideMembersLevel(implicitReceiverValue, topLevelScope, info, index, depth) processHideMembersLevel(implicitReceiverValue, topLevelScope, info, index, depth)
} }
} }
} }
@@ -209,13 +208,12 @@ class FirTowerResolver(
info, TowerGroup.TopPrioritized(index).let { if (depth != null) it.Implicit(depth) else it } info, TowerGroup.TopPrioritized(index).let { if (depth != null) it.Implicit(depth) else it }
) )
private suspend fun processLocalScopes( private suspend fun TowerResolveManager.processLocalScopesWithNoReceiver(
manager: TowerResolveManager,
info: CallInfo, info: CallInfo,
nonEmptyLocalScopes: MutableList<FirLocalScope> nonEmptyLocalScopes: MutableList<FirLocalScope>
) { ) {
for ((index, localScope) in localScopes.withIndex()) { for ((index, localScope) in localScopes.withIndex()) {
val result = manager.processLevel( val result = processLevel(
localScope.toScopeTowerLevel(), info, TowerGroup.Local(index) localScope.toScopeTowerLevel(), info, TowerGroup.Local(index)
) )
if (result != ProcessorAction.NONE) { if (result != ProcessorAction.NONE) {
@@ -224,8 +222,7 @@ class FirTowerResolver(
} }
} }
private suspend fun processImplicitReceivers( private suspend fun TowerResolveManager.processImplicitReceiversWithNoExplicit(
manager: TowerResolveManager,
info: CallInfo, info: CallInfo,
nonEmptyLocalScopes: MutableList<FirLocalScope>, nonEmptyLocalScopes: MutableList<FirLocalScope>,
emptyTopLevelScopes: MutableSet<FirScope> emptyTopLevelScopes: MutableSet<FirScope>
@@ -238,7 +235,7 @@ class FirTowerResolver(
if (usableAsValue) { if (usableAsValue) {
processCandidatesWithGivenImplicitReceiverAsValue( processCandidatesWithGivenImplicitReceiverAsValue(
implicitReceiverValue, implicitReceiverValue,
manager, this,
info, info,
parentGroup, parentGroup,
implicitReceiverValuesWithEmptyScopes, implicitReceiverValuesWithEmptyScopes,
@@ -250,7 +247,7 @@ class FirTowerResolver(
if (implicitReceiverValue is ImplicitDispatchReceiverValue) { if (implicitReceiverValue is ImplicitDispatchReceiverValue) {
val scope = implicitReceiverValue.scope(session, components.scopeSession) val scope = implicitReceiverValue.scope(session, components.scopeSession)
if (scope != null) { if (scope != null) {
manager.processLevel( processLevel(
FirStaticScope(scope).toScopeTowerLevel(), FirStaticScope(scope).toScopeTowerLevel(),
info, parentGroup.Static(depth) info, parentGroup.Static(depth)
) )
@@ -304,16 +301,15 @@ class FirTowerResolver(
} }
} }
private suspend fun processTopLevelScopes( private suspend fun TowerResolveManager.processTopLevelScopesNoReceiver(
info: CallInfo, info: CallInfo,
emptyTopLevelScopes: Collection<FirScope>, emptyTopLevelScopes: Collection<FirScope>
manager: TowerResolveManager
) { ) {
for ((index, topLevelScope) in topLevelScopes.withIndex()) { for ((index, topLevelScope) in topLevelScopes.withIndex()) {
// NB: this check does not work for variables // NB: this check does not work for variables
// because we do not search for objects if we have extension receiver // because we do not search for objects if we have extension receiver
if (info.callKind != CallKind.VariableAccess && topLevelScope in emptyTopLevelScopes) continue if (info.callKind != CallKind.VariableAccess && topLevelScope in emptyTopLevelScopes) continue
manager.processLevel( processLevel(
topLevelScope.toScopeTowerLevel(), info, TowerGroup.Top(index) topLevelScope.toScopeTowerLevel(), info, TowerGroup.Top(index)
) )
} }
@@ -326,11 +322,8 @@ class FirTowerResolver(
) { ) {
val explicitReceiverValue = ExpressionReceiverValue(receiver) val explicitReceiverValue = ExpressionReceiverValue(receiver)
processExtensionsThatHideMembers(info, manager, explicitReceiverValue) manager.processExtensionsThatHideMembers(info, explicitReceiverValue)
manager.processMembersForExplicitReceiver(explicitReceiverValue, info)
manager.processLevel(
explicitReceiverValue.toMemberScopeTowerLevel(), info, TowerGroup.Member, ExplicitReceiverKind.DISPATCH_RECEIVER
)
val shouldProcessExplicitReceiverScopeOnly = val shouldProcessExplicitReceiverScopeOnly =
info.callKind == CallKind.Function && info.explicitReceiver?.typeRef?.coneTypeSafe<ConeIntegerLiteralType>() != null info.callKind == CallKind.Function && info.explicitReceiver?.typeRef?.coneTypeSafe<ConeIntegerLiteralType>() != null
@@ -352,49 +345,75 @@ class FirTowerResolver(
localScope.toScopeTowerLevel(), info, TowerGroup.Local(index) localScope.toScopeTowerLevel(), info, TowerGroup.Local(index)
) )
} }
for ((implicitReceiverValue, usableAsValue, depth) in implicitReceivers) { for ((implicitReceiverValue, usableAsValue, depth) in implicitReceivers) {
if (!usableAsValue) continue if (!usableAsValue) continue
// NB: companions are processed via implicitReceiverValues! processCombinationOfReceivers(manager, implicitReceiverValue, explicitReceiverValue, info, depth, nonEmptyLocalScopes)
val parentGroup = TowerGroup.Implicit(depth)
manager.processLevel(
implicitReceiverValue.toMemberScopeTowerLevel(extensionReceiver = explicitReceiverValue),
info, parentGroup.Member, ExplicitReceiverKind.EXTENSION_RECEIVER
)
manager.processLevelForPropertyWithInvoke(
implicitReceiverValue.toMemberScopeTowerLevel(), info, parentGroup.Member
)
for ((localIndex, localScope) in nonEmptyLocalScopes.withIndex()) {
manager.processLevelForPropertyWithInvoke(
localScope.toScopeTowerLevel(extensionReceiver = implicitReceiverValue),
info, parentGroup.Local(localIndex)
)
}
for ((implicitDispatchReceiverValue, usable, dispatchDepth) in implicitReceivers) {
if (!usable) continue
manager.processLevelForPropertyWithInvoke(
implicitDispatchReceiverValue.toMemberScopeTowerLevel(extensionReceiver = implicitReceiverValue),
info, parentGroup.Implicit(dispatchDepth)
)
}
for ((topIndex, topLevelScope) in topLevelScopes.withIndex()) {
manager.processLevelForPropertyWithInvoke(
topLevelScope.toScopeTowerLevel(extensionReceiver = implicitReceiverValue),
info, parentGroup.Top(topIndex)
)
}
} }
for ((index, topLevelScope) in topLevelScopes.withIndex()) {
for ((index, topLevelScope) in topLevelScopes.withIndex()) {
manager.processLevel( manager.processLevel(
topLevelScope.toScopeTowerLevel(extensionReceiver = explicitReceiverValue), topLevelScope.toScopeTowerLevel(extensionReceiver = explicitReceiverValue),
info, TowerGroup.Top(index), ExplicitReceiverKind.EXTENSION_RECEIVER info, TowerGroup.Top(index), ExplicitReceiverKind.EXTENSION_RECEIVER
) )
manager.processLevelForPropertyWithInvoke( manager.processLevelForPropertyWithInvoke(
topLevelScope.toScopeTowerLevel(), info, TowerGroup.Top(index) topLevelScope.toScopeTowerLevel(), info, TowerGroup.Top(index)
) )
} }
}
private suspend fun TowerResolveManager.processMembersForExplicitReceiver(
explicitReceiverValue: ExpressionReceiverValue,
info: CallInfo
) {
processLevel(
explicitReceiverValue.toMemberScopeTowerLevel(), info, TowerGroup.Member, ExplicitReceiverKind.DISPATCH_RECEIVER
)
}
private suspend fun processCombinationOfReceivers(
manager: TowerResolveManager,
implicitReceiverValue: ImplicitReceiverValue<*>,
explicitReceiverValue: ExpressionReceiverValue,
info: CallInfo,
depth: Int,
nonEmptyLocalScopes: MutableList<FirLocalScope>
) {
// NB: companions are processed via implicitReceiverValues!
val parentGroup = TowerGroup.Implicit(depth)
// Member extensions
manager.processLevel(
implicitReceiverValue.toMemberScopeTowerLevel(extensionReceiver = explicitReceiverValue),
info, parentGroup.Member, ExplicitReceiverKind.EXTENSION_RECEIVER
)
// properties for invoke on implicit receiver
manager.processLevelForPropertyWithInvoke(
implicitReceiverValue.toMemberScopeTowerLevel(), info, parentGroup.Member
)
for ((localIndex, localScope) in nonEmptyLocalScopes.withIndex()) {
manager.processLevelForPropertyWithInvoke(
localScope.toScopeTowerLevel(extensionReceiver = implicitReceiverValue),
info, parentGroup.Local(localIndex)
)
}
for ((implicitDispatchReceiverValue, usable, dispatchDepth) in implicitReceivers) {
if (!usable) continue
manager.processLevelForPropertyWithInvoke(
implicitDispatchReceiverValue.toMemberScopeTowerLevel(extensionReceiver = implicitReceiverValue),
info, parentGroup.Implicit(dispatchDepth)
)
}
for ((topIndex, topLevelScope) in topLevelScopes.withIndex()) {
manager.processLevelForPropertyWithInvoke(
topLevelScope.toScopeTowerLevel(extensionReceiver = implicitReceiverValue),
info, parentGroup.Top(topIndex)
)
}
} }
private suspend fun runResolverForSuperReceiver( private suspend fun runResolverForSuperReceiver(
@@ -426,6 +445,7 @@ class FirTowerResolver(
ExplicitReceiverKind.DISPATCH_RECEIVER, ExplicitReceiverKind.DISPATCH_RECEIVER,
InvokeResolveMode.IMPLICIT_CALL_ON_GIVEN_RECEIVER InvokeResolveMode.IMPLICIT_CALL_ON_GIVEN_RECEIVER
) )
for ((index, localScope) in localScopes.withIndex()) { for ((index, localScope) in localScopes.withIndex()) {
manager.processLevel( manager.processLevel(
localScope.toScopeTowerLevel(extensionReceiver = invokeReceiverValue), localScope.toScopeTowerLevel(extensionReceiver = invokeReceiverValue),
@@ -434,6 +454,7 @@ class FirTowerResolver(
InvokeResolveMode.IMPLICIT_CALL_ON_GIVEN_RECEIVER InvokeResolveMode.IMPLICIT_CALL_ON_GIVEN_RECEIVER
) )
} }
for ((implicitReceiverValue, usable, depth) in implicitReceivers) { for ((implicitReceiverValue, usable, depth) in implicitReceivers) {
if (!usable) continue if (!usable) continue
// NB: companions are processed via implicitReceiverValues! // NB: companions are processed via implicitReceiverValues!
@@ -445,6 +466,7 @@ class FirTowerResolver(
InvokeResolveMode.IMPLICIT_CALL_ON_GIVEN_RECEIVER InvokeResolveMode.IMPLICIT_CALL_ON_GIVEN_RECEIVER
) )
} }
for ((index, topLevelScope) in topLevelScopes.withIndex()) { for ((index, topLevelScope) in topLevelScopes.withIndex()) {
manager.processLevel( manager.processLevel(
topLevelScope.toScopeTowerLevel(extensionReceiver = invokeReceiverValue), topLevelScope.toScopeTowerLevel(extensionReceiver = invokeReceiverValue),