[NI] Refactoring: KotlinCallContext to stateless component (1)

This commit is contained in:
Stanislav Erokhin
2017-07-10 11:02:08 +03:00
parent 9bbfac11b4
commit e88c1b4f0a
12 changed files with 64 additions and 45 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.resolve.calls
import org.jetbrains.kotlin.resolve.calls.components.KotlinCallCompleter
import org.jetbrains.kotlin.resolve.calls.components.KotlinResolutionCallbacks
import org.jetbrains.kotlin.resolve.calls.components.NewOverloadingConflictResolver
import org.jetbrains.kotlin.resolve.calls.context.CheckArgumentTypesMode
import org.jetbrains.kotlin.resolve.calls.model.*
@@ -42,7 +43,7 @@ class KotlinCallResolver(
kotlinCall.checkCallInvariants()
val candidateFactory = SimpleCandidateFactory(callContext, kotlinCall)
val candidateFactory = SimpleCandidateFactory(callContext, scopeTower, kotlinCall)
val processor = when(kotlinCall.callKind) {
KotlinCallKind.VARIABLE -> {
createVariableAndObjectProcessor(scopeTower, kotlinCall.name, candidateFactory, kotlinCall.explicitReceiver?.receiver)
@@ -55,7 +56,7 @@ class KotlinCallResolver(
val candidates = towerResolver.runResolve(scopeTower, processor, useOrder = kotlinCall.callKind != KotlinCallKind.UNSUPPORTED)
return choseMostSpecific(callContext, expectedType, candidates)
return choseMostSpecific(callContext.resolutionCallbacks, expectedType, candidates)
}
fun resolveGivenCandidates(
@@ -70,6 +71,7 @@ class KotlinCallResolver(
val resolutionCandidates = givenCandidates.map {
SimpleKotlinResolutionCandidate(callContext,
it.scopeTower,
kotlinCall,
if (it.dispatchReceiver == null) ExplicitReceiverKind.NO_EXPLICIT_RECEIVER else ExplicitReceiverKind.DISPATCH_RECEIVER,
it.dispatchReceiver?.let { ReceiverExpressionKotlinCallArgument(it, isSafeCall) },
@@ -82,30 +84,31 @@ class KotlinCallResolver(
val candidates = towerResolver.runWithEmptyTowerData(KnownResultProcessor(resolutionCandidates),
TowerResolver.SuccessfulResultCollector { it.status },
useOrder = true)
return choseMostSpecific(callContext, expectedType, candidates)
return choseMostSpecific(callContext.resolutionCallbacks, expectedType, candidates)
}
private fun choseMostSpecific(
callContext: KotlinCallContext,
resolutionCallbacks: KotlinResolutionCallbacks,
expectedType: UnwrappedType?,
candidates: Collection<KotlinResolutionCandidate>
): Collection<ResolvedKotlinCall> {
val isDebuggerContext = (candidates.firstOrNull() ?: return emptyList()).lastCall.scopeTower.isDebuggerContext
val maximallySpecificCandidates = overloadingConflictResolver.chooseMaximallySpecificCandidates(
candidates,
CheckArgumentTypesMode.CHECK_VALUE_ARGUMENTS,
discriminateGenerics = true, // todo
isDebuggerContext = callContext.scopeTower.isDebuggerContext)
isDebuggerContext = isDebuggerContext)
val singleResult = maximallySpecificCandidates.singleOrNull()?.let {
kotlinCallCompleter.completeCallIfNecessary(it, expectedType, callContext.resolutionCallbacks)
kotlinCallCompleter.completeCallIfNecessary(it, expectedType, resolutionCallbacks)
}
if (singleResult != null) {
return listOf(singleResult)
}
return maximallySpecificCandidates.map {
kotlinCallCompleter.transformWhenAmbiguity(it, callContext.resolutionCallbacks)
kotlinCallCompleter.transformWhenAmbiguity(it, resolutionCallbacks)
}
}
}
@@ -153,12 +153,13 @@ private fun ConstraintSystemOperation.addReceiverConstraint(
class CallableReferencesCandidateFactory(
val argument: CallableReferenceKotlinCallArgument,
val outerCallContext: KotlinCallContext,
val scopeTower: ImplicitScopeTower,
val compatibilityChecker: ((ConstraintSystemOperation) -> Unit) -> Unit,
val expectedType: UnwrappedType?
) : CandidateFactory<CallableReferenceCandidate> {
fun createCallableProcessor(explicitReceiver: DetailedReceiver?) =
createCallableReferenceProcessor(outerCallContext.scopeTower, argument.rhsName, this, explicitReceiver)
createCallableReferenceProcessor(scopeTower, argument.rhsName, this, explicitReceiver)
override fun createCandidate(
towerCandidate: CandidateWithBoundDispatchReceiver,
@@ -189,7 +190,7 @@ class CallableReferencesCandidateFactory(
if (it.hasContradiction) return@compatibilityChecker
val (_, visibilityError) = it.checkCallableReference(argument, dispatchCallableReceiver, extensionCallableReceiver, candidateDescriptor,
reflectionCandidateType, expectedType, outerCallContext.scopeTower.lexicalScope.ownerDescriptor)
reflectionCandidateType, expectedType, scopeTower.lexicalScope.ownerDescriptor)
diagnostics.addIfNotNull(visibilityError)
@@ -273,7 +274,7 @@ class CallableReferencesCandidateFactory(
val mutable = descriptor.isVar && run {
val setter = descriptor.setter
setter == null || Visibilities.isVisible(dispatchReceiver?.asReceiverValueForVisibilityChecks, setter,
outerCallContext.scopeTower.lexicalScope.ownerDescriptor)
scopeTower.lexicalScope.ownerDescriptor)
}
return outerCallContext.reflectionTypes.getKPropertyType(Annotations.EMPTY, argumentsAndReceivers, descriptorReturnType, mutable) to 0
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.resolve.calls.results.FlatSignature
import org.jetbrains.kotlin.resolve.calls.results.OverloadingConflictResolver
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
import org.jetbrains.kotlin.resolve.calls.tower.ImplicitScopeTower
import org.jetbrains.kotlin.resolve.calls.tower.TowerResolver
import org.jetbrains.kotlin.types.UnwrappedType
@@ -34,7 +35,7 @@ import org.jetbrains.kotlin.types.UnwrappedType
class CallableReferenceOverloadConflictResolver(
builtIns: KotlinBuiltIns,
specificityComparator: TypeSpecificityComparator,
externalPredicates: KotlinResolutionExternalPredicates,
statelessCallbacks: KotlinResolutionStatelessCallbacks,
constraintInjector: ConstraintInjector,
typeResolver: ResultTypeResolver
) : OverloadingConflictResolver<CallableReferenceCandidate>(
@@ -44,7 +45,7 @@ class CallableReferenceOverloadConflictResolver(
{ SimpleConstraintSystemImpl(constraintInjector, typeResolver) },
Companion::createFlatSignature,
{ null },
{ externalPredicates.isDescriptorFromSource(it) }
{ statelessCallbacks.isDescriptorFromSource(it) }
) {
companion object {
private fun createFlatSignature(candidate: CallableReferenceCandidate) =
@@ -54,6 +55,7 @@ class CallableReferenceOverloadConflictResolver(
fun processCallableReferenceArgument(
callContext: KotlinCallContext,
scopeTower: ImplicitScopeTower,
csBuilder: ConstraintSystemBuilder,
postponedArgument: PostponedCallableReferenceArgument
): KotlinCallDiagnostic? {
@@ -64,7 +66,7 @@ fun processCallableReferenceArgument(
if (subLHSCall != null) {
csBuilder.addInnerCall(subLHSCall.resolvedCall)
}
val candidates = callContext.callableReferenceResolver.runRLSResolution(callContext, argument, expectedType) { checkCallableReference ->
val candidates = callContext.callableReferenceResolver.runRLSResolution(callContext, scopeTower, argument, expectedType) { checkCallableReference ->
csBuilder.runTransaction { checkCallableReference(this); false }
}
val chosenCandidate = when (candidates.size) {
@@ -74,7 +76,7 @@ fun processCallableReferenceArgument(
}
val (toFreshSubstitutor, diagnostic) = with(chosenCandidate) {
csBuilder.checkCallableReference(argument, dispatchReceiver, extensionReceiver, candidate,
reflectionCandidateType, expectedType, callContext.scopeTower.lexicalScope.ownerDescriptor)
reflectionCandidateType, expectedType, scopeTower.lexicalScope.ownerDescriptor)
}
postponedArgument.analyzedAndThereIsResult = true
@@ -92,16 +94,17 @@ class CallableReferenceResolver(
fun runRLSResolution(
outerCallContext: KotlinCallContext,
scopeTower: ImplicitScopeTower,
callableReference: CallableReferenceKotlinCallArgument,
expectedType: UnwrappedType?, // this type can have not fixed type variable inside
compatibilityChecker: ((ConstraintSystemOperation) -> Unit) -> Unit // you can run anything throw this operation and all this operation will be roll backed
): Set<CallableReferenceCandidate> {
val factory = CallableReferencesCandidateFactory(callableReference, outerCallContext, compatibilityChecker, expectedType)
val factory = CallableReferencesCandidateFactory(callableReference, outerCallContext, scopeTower, compatibilityChecker, expectedType)
val processor = createCallableReferenceProcessor(factory)
val candidates = towerResolver.runResolve(outerCallContext.scopeTower, processor, useOrder = true)
val candidates = towerResolver.runResolve(scopeTower, processor, useOrder = true)
return callableReferenceOverloadConflictResolver.chooseMaximallySpecificCandidates(candidates, CheckArgumentTypesMode.CHECK_VALUE_ARGUMENTS,
discriminateGenerics = false,
isDebuggerContext = outerCallContext.scopeTower.isDebuggerContext)
isDebuggerContext = scopeTower.isDebuggerContext)
}
}
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.types.UnwrappedType
// stateless component
interface KotlinResolutionExternalPredicates {
interface KotlinResolutionStatelessCallbacks {
fun isDescriptorFromSource(descriptor: CallableDescriptor): Boolean
fun isInfixCall(kotlinCall: KotlinCall): Boolean
fun isOperatorCall(kotlinCall: KotlinCall): Boolean
@@ -93,7 +93,7 @@ class KotlinCallCompleter(
private fun resolveCallableReferenceArguments(c: Context, topLevelCall: SimpleKotlinResolutionCandidate) {
for (callableReferenceArgument in c.postponedArguments) {
if (callableReferenceArgument !is PostponedCallableReferenceArgument) continue
processCallableReferenceArgument(topLevelCall.callContext, c.getBuilder(), callableReferenceArgument)
processCallableReferenceArgument(topLevelCall.callContext, topLevelCall.scopeTower, c.getBuilder(), callableReferenceArgument)
}
}
@@ -31,7 +31,7 @@ import java.util.*
class NewOverloadingConflictResolver(
builtIns: KotlinBuiltIns,
specificityComparator: TypeSpecificityComparator,
externalPredicates: KotlinResolutionExternalPredicates,
statelessCallbacks: KotlinResolutionStatelessCallbacks,
constraintInjector: ConstraintInjector,
typeResolver: ResultTypeResolver
) : OverloadingConflictResolver<KotlinResolutionCandidate>(
@@ -45,7 +45,7 @@ class NewOverloadingConflictResolver(
{ SimpleConstraintSystemImpl(constraintInjector, typeResolver) },
Companion::createFlatSignature,
{ (it as? VariableAsFunctionKotlinResolutionCandidate)?.resolvedVariable },
{ externalPredicates.isDescriptorFromSource(it) }
{ statelessCallbacks.isDescriptorFromSource(it) }
) {
companion object {
@@ -37,7 +37,7 @@ import org.jetbrains.kotlin.utils.addIfNotNull
internal object CheckInstantiationOfAbstractClass : ResolutionPart {
override fun SimpleKotlinResolutionCandidate.process(): List<KotlinCallDiagnostic> {
if (candidateDescriptor is ConstructorDescriptor && !callContext.externalPredicates.isSuperOrDelegatingConstructorCall(kotlinCall)) {
if (candidateDescriptor is ConstructorDescriptor && !callContext.statelessCallbacks.isSuperOrDelegatingConstructorCall(kotlinCall)) {
if (candidateDescriptor.constructedClass.modality == Modality.ABSTRACT) {
return listOf(InstantiationOfAbstractClass)
}
@@ -62,7 +62,7 @@ internal object CheckVisibility : ResolutionPart {
return listOf(VisibilityError(invisibleMember))
}
private val SimpleKotlinResolutionCandidate.containingDescriptor: DeclarationDescriptor get() = callContext.scopeTower.lexicalScope.ownerDescriptor
private val SimpleKotlinResolutionCandidate.containingDescriptor: DeclarationDescriptor get() = scopeTower.lexicalScope.ownerDescriptor
}
internal object MapTypeArguments : ResolutionPart {
@@ -253,7 +253,7 @@ internal object CheckArguments : ResolutionPart {
internal object CheckInfixResolutionPart : ResolutionPart {
override fun SimpleKotlinResolutionCandidate.process(): List<KotlinCallDiagnostic> {
if (callContext.externalPredicates.isInfixCall(kotlinCall) &&
if (callContext.statelessCallbacks.isInfixCall(kotlinCall) &&
(candidateDescriptor !is FunctionDescriptor || !candidateDescriptor.isInfix)) {
return listOf(InfixCallNoInfixModifier)
}
@@ -264,7 +264,7 @@ internal object CheckInfixResolutionPart : ResolutionPart {
internal object CheckOperatorResolutionPart : ResolutionPart {
override fun SimpleKotlinResolutionCandidate.process(): List<KotlinCallDiagnostic> {
if (callContext.externalPredicates.isOperatorCall(kotlinCall) &&
if (callContext.statelessCallbacks.isOperatorCall(kotlinCall) &&
(candidateDescriptor !is FunctionDescriptor || !candidateDescriptor.isOperator)) {
return listOf(InvokeConventionCallNoOperatorModifier)
}
@@ -33,7 +33,7 @@ import org.jetbrains.kotlin.types.isDynamic
class KotlinCallContext(
val scopeTower: ImplicitScopeTower,
val resolutionCallbacks: KotlinResolutionCallbacks,
val externalPredicates: KotlinResolutionExternalPredicates,
val statelessCallbacks: KotlinResolutionStatelessCallbacks,
val argumentsToParametersMapper: ArgumentsToParametersMapper,
val typeArgumentsToParametersMapper: TypeArgumentsToParametersMapper,
val resultTypeResolver: ResultTypeResolver,
@@ -42,7 +42,11 @@ class KotlinCallContext(
val reflectionTypes: ReflectionTypes
)
class SimpleCandidateFactory(val callContext: KotlinCallContext, val kotlinCall: KotlinCall): CandidateFactory<SimpleKotlinResolutionCandidate> {
class SimpleCandidateFactory(
val callContext: KotlinCallContext,
val scopeTower: ImplicitScopeTower,
val kotlinCall: KotlinCall
): CandidateFactory<SimpleKotlinResolutionCandidate> {
// todo: try something else, because current method is ugly and unstable
private fun createReceiverArgument(
@@ -73,11 +77,11 @@ class SimpleCandidateFactory(val callContext: KotlinCallContext, val kotlinCall:
val extensionArgumentReceiver = createReceiverArgument(kotlinCall.getExplicitExtensionReceiver(explicitReceiverKind), extensionReceiver)
if (ErrorUtils.isError(towerCandidate.descriptor)) {
return ErrorKotlinResolutionCandidate(callContext, kotlinCall, explicitReceiverKind, dispatchArgumentReceiver, extensionArgumentReceiver, towerCandidate.descriptor)
return ErrorKotlinResolutionCandidate(callContext, scopeTower, kotlinCall, explicitReceiverKind, dispatchArgumentReceiver, extensionArgumentReceiver, towerCandidate.descriptor)
}
val candidateDiagnostics = towerCandidate.diagnostics.toMutableList()
if (callContext.externalPredicates.isHiddenInResolution(towerCandidate.descriptor, kotlinCall)) {
if (callContext.statelessCallbacks.isHiddenInResolution(towerCandidate.descriptor, kotlinCall)) {
candidateDiagnostics.add(HiddenDescriptor)
}
@@ -91,7 +95,7 @@ class SimpleCandidateFactory(val callContext: KotlinCallContext, val kotlinCall:
}
}
return SimpleKotlinResolutionCandidate(callContext, kotlinCall, explicitReceiverKind, dispatchArgumentReceiver, extensionArgumentReceiver,
return SimpleKotlinResolutionCandidate(callContext, scopeTower, kotlinCall, explicitReceiverKind, dispatchArgumentReceiver, extensionArgumentReceiver,
towerCandidate.descriptor, null, candidateDiagnostics)
}
}
@@ -126,6 +130,7 @@ enum class KotlinCallKind(vararg resolutionPart: ResolutionPart) {
}
class GivenCandidate(
val scopeTower: ImplicitScopeTower,
val descriptor: FunctionDescriptor,
val dispatchReceiver: ReceiverValueWithSmartCastInfo?,
val knownTypeParametersResultingSubstitutor: TypeSubstitutor?
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImp
import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.calls.tower.Candidate
import org.jetbrains.kotlin.resolve.calls.tower.ImplicitScopeTower
import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateStatus
import org.jetbrains.kotlin.resolve.calls.tower.isSuccess
import org.jetbrains.kotlin.types.TypeSubstitutor
@@ -105,6 +106,7 @@ sealed class AbstractSimpleKotlinResolutionCandidate(
open class SimpleKotlinResolutionCandidate(
val callContext: KotlinCallContext,
val scopeTower: ImplicitScopeTower,
override val kotlinCall: KotlinCall,
val explicitReceiverKind: ExplicitReceiverKind,
val dispatchReceiverArgument: SimpleKotlinCallArgument?,
@@ -133,12 +135,14 @@ open class SimpleKotlinResolutionCandidate(
class ErrorKotlinResolutionCandidate(
callContext: KotlinCallContext,
scopeTower: ImplicitScopeTower,
kotlinCall: KotlinCall,
explicitReceiverKind: ExplicitReceiverKind,
dispatchReceiverArgument: SimpleKotlinCallArgument?,
extensionReceiver: SimpleKotlinCallArgument?,
candidateDescriptor: CallableDescriptor
) : SimpleKotlinResolutionCandidate(callContext, kotlinCall, explicitReceiverKind, dispatchReceiverArgument, extensionReceiver, candidateDescriptor, null, listOf()) {
) : SimpleKotlinResolutionCandidate(callContext, scopeTower, kotlinCall, explicitReceiverKind, dispatchReceiverArgument,
extensionReceiver, candidateDescriptor, null, listOf()) {
override val resolutionSequence: List<ResolutionPart> get() = emptyList()
init {