From f039ec36257583e4715c5b7baf1f58c51b6b5957 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Thu, 19 Apr 2018 18:07:05 +0300 Subject: [PATCH] Refactoring: get rid `KotlinCallKind` inside `ResolutionKind` --- .../kotlin/resolve/calls/CallResolver.java | 20 ++++++------ .../calls/tower/NewResolutionOldInference.kt | 16 +++++----- .../resolve/calls/tower/PSICallResolver.kt | 32 +++++++++++++------ 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallResolver.java index b22b1231274..3cc2b339143 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallResolver.java @@ -27,7 +27,6 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt; import org.jetbrains.kotlin.resolve.calls.components.InferenceSession; import org.jetbrains.kotlin.resolve.calls.context.*; import org.jetbrains.kotlin.resolve.calls.inference.CoroutineInferenceUtilKt; -import org.jetbrains.kotlin.resolve.calls.model.KotlinCallKind; import org.jetbrains.kotlin.resolve.calls.model.MutableDataFlowInfoForArguments; import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResults; import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResultsImpl; @@ -190,7 +189,7 @@ public class CallResolver { @NotNull BasicCallResolutionContext context, @NotNull Name name, @NotNull KtReferenceExpression referenceExpression, - @NotNull NewResolutionOldInference.ResolutionKind kind + @NotNull NewResolutionOldInference.ResolutionKind kind ) { TracingStrategy tracing = TracingStrategyImpl.create(referenceExpression, context.call); return computeTasksAndResolveCall(context, name, tracing, kind); @@ -201,7 +200,7 @@ public class CallResolver { @NotNull BasicCallResolutionContext context, @NotNull Name name, @NotNull TracingStrategy tracing, - @NotNull NewResolutionOldInference.ResolutionKind kind + @NotNull NewResolutionOldInference.ResolutionKind kind ) { return callResolvePerfCounter.>time(() -> { ResolutionTask resolutionTask = new ResolutionTask<>(kind, name, null); @@ -227,7 +226,7 @@ public class CallResolver { ) { return callResolvePerfCounter.>time(() -> { ResolutionTask resolutionTask = new ResolutionTask<>( - new NewResolutionOldInference.ResolutionKind.GivenCandidates<>(), null, candidates + new NewResolutionOldInference.ResolutionKind.GivenCandidates(), null, candidates ); return doResolveCallOrGetCachedResults(context, resolutionTask, tracing); }); @@ -553,7 +552,7 @@ public class CallResolver { Set> candidates = Collections.singleton(candidate); ResolutionTask resolutionTask = new ResolutionTask<>( - new NewResolutionOldInference.ResolutionKind.GivenCandidates<>(), null, candidates + new NewResolutionOldInference.ResolutionKind.GivenCandidates(), null, candidates ); return doResolveCallOrGetCachedResults(basicCallResolutionContext, resolutionTask, tracing); @@ -569,12 +568,13 @@ public class CallResolver { tracing.bindCall(context.trace, call); boolean newInferenceEnabled = languageVersionSettings.supportsFeature(LanguageFeature.NewInference); - if (newInferenceEnabled && (resolutionTask.resolutionKind.getKotlinCallKind() != KotlinCallKind.UNSUPPORTED)) { + NewResolutionOldInference.ResolutionKind resolutionKind = resolutionTask.resolutionKind; + if (newInferenceEnabled && PSICallResolver.getDefaultResolutionKinds().contains(resolutionKind)) { assert resolutionTask.name != null; - return PSICallResolver.runResolutionAndInference(context, resolutionTask.name, resolutionTask.resolutionKind, tracing); + return PSICallResolver.runResolutionAndInference(context, resolutionTask.name, resolutionKind, tracing); } - if (newInferenceEnabled && resolutionTask.resolutionKind instanceof NewResolutionOldInference.ResolutionKind.GivenCandidates) { + if (newInferenceEnabled && resolutionKind instanceof NewResolutionOldInference.ResolutionKind.GivenCandidates) { assert resolutionTask.givenCandidates != null; return PSICallResolver.runResolutionAndInferenceForGivenCandidates(context, resolutionTask.givenCandidates, tracing); } @@ -693,10 +693,10 @@ public class CallResolver { final Collection> givenCandidates; @NotNull - final NewResolutionOldInference.ResolutionKind resolutionKind; + final NewResolutionOldInference.ResolutionKind resolutionKind; private ResolutionTask( - @NotNull NewResolutionOldInference.ResolutionKind kind, + @NotNull NewResolutionOldInference.ResolutionKind kind, @Nullable Name name, @Nullable Collection> candidates ) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt index 756fd31e056..aed561f8a68 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt @@ -70,7 +70,7 @@ class NewResolutionOldInference( private val coroutineInferenceSupport: CoroutineInferenceSupport, private val deprecationResolver: DeprecationResolver ) { - sealed class ResolutionKind(val kotlinCallKind: KotlinCallKind = KotlinCallKind.UNSUPPORTED) { + sealed class ResolutionKind { abstract internal fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, @@ -80,7 +80,7 @@ class NewResolutionOldInference( context: BasicCallResolutionContext ): ScopeTowerProcessor - object Function : ResolutionKind(KotlinCallKind.FUNCTION) { + object Function : ResolutionKind() { override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext @@ -96,7 +96,7 @@ class NewResolutionOldInference( } } - object Variable : ResolutionKind(KotlinCallKind.VARIABLE) { + object Variable : ResolutionKind() { override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext @@ -106,7 +106,7 @@ class NewResolutionOldInference( } } - object CallableReference : ResolutionKind() { + object CallableReference : ResolutionKind() { override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext @@ -120,7 +120,7 @@ class NewResolutionOldInference( } } - object Invoke : ResolutionKind() { + object Invoke : ResolutionKind() { override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext @@ -142,7 +142,7 @@ class NewResolutionOldInference( } - class GivenCandidates : ResolutionKind() { + class GivenCandidates : ResolutionKind() { override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext @@ -155,7 +155,7 @@ class NewResolutionOldInference( fun runResolution( context: BasicCallResolutionContext, name: Name, - kind: ResolutionKind, + kind: ResolutionKind, tracing: TracingStrategy ): OverloadResolutionResultsImpl { val explicitReceiver = context.call.explicitReceiver @@ -349,7 +349,7 @@ class NewResolutionOldInference( private fun reportAdditionalDiagnosticIfNoCandidates( context: BasicCallResolutionContext, name: Name, - kind: ResolutionKind<*>, + kind: ResolutionKind, scopeTower: ImplicitScopeTower, detailedReceiver: DetailedReceiver? ): Boolean { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt index ce9ec91abd1..13834750dd9 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt @@ -64,16 +64,21 @@ class PSICallResolver( ) { private val GIVEN_CANDIDATES_NAME = Name.special("") + val defaultResolutionKinds = setOf( + NewResolutionOldInference.ResolutionKind.Function, + NewResolutionOldInference.ResolutionKind.Variable + ) + fun runResolutionAndInference( context: BasicCallResolutionContext, name: Name, - resolutionKind: NewResolutionOldInference.ResolutionKind, + resolutionKind: NewResolutionOldInference.ResolutionKind, tracingStrategy: TracingStrategy ): OverloadResolutionResults { val isBinaryRemOperator = isBinaryRemOperator(context.call) val refinedName = refineNameForRemOperator(isBinaryRemOperator, name) - val kotlinCall = toKotlinCall(context, resolutionKind.kotlinCallKind, context.call, refinedName, tracingStrategy) + val kotlinCall = toKotlinCall(context, resolutionKind.toKotlinCallKind(), context.call, refinedName, tracingStrategy) val scopeTower = ASTScopeTower(context) val resolutionCallbacks = createResolutionCallbacks(context) @@ -89,7 +94,7 @@ class PSICallResolver( result = resolveToDeprecatedMod(name, context, resolutionKind, tracingStrategy, scopeTower, resolutionCallbacks, expectedType) } - if (result.isEmpty() && reportAdditionalDiagnosticIfNoCandidates(context, scopeTower, resolutionKind.kotlinCallKind, kotlinCall)) { + if (result.isEmpty() && reportAdditionalDiagnosticIfNoCandidates(context, scopeTower, resolutionKind, kotlinCall)) { return OverloadResolutionResultsImpl.nameNotFound() } @@ -124,17 +129,17 @@ class PSICallResolver( } - private fun resolveToDeprecatedMod( + private fun resolveToDeprecatedMod( remOperatorName: Name, context: BasicCallResolutionContext, - resolutionKind: NewResolutionOldInference.ResolutionKind, + resolutionKind: NewResolutionOldInference.ResolutionKind, tracingStrategy: TracingStrategy, scopeTower: ImplicitScopeTower, resolutionCallbacks: KotlinResolutionCallbacksImpl, expectedType: UnwrappedType? ): CallResolutionResult { val deprecatedName = OperatorConventions.REM_TO_MOD_OPERATION_NAMES[remOperatorName]!! - val callWithDeprecatedName = toKotlinCall(context, resolutionKind.kotlinCallKind, context.call, deprecatedName, tracingStrategy) + val callWithDeprecatedName = toKotlinCall(context, resolutionKind.toKotlinCallKind(), context.call, deprecatedName, tracingStrategy) val refinedProviderForInvokeFactory = FactoryProviderForInvoke(context, scopeTower, callWithDeprecatedName) return kotlinCallResolver.resolveCall( scopeTower, resolutionCallbacks, callWithDeprecatedName, expectedType, @@ -298,7 +303,7 @@ class PSICallResolver( private fun reportAdditionalDiagnosticIfNoCandidates( context: BasicCallResolutionContext, scopeTower: ImplicitScopeTower, - kind: KotlinCallKind, + kind: NewResolutionOldInference.ResolutionKind, kotlinCall: KotlinCall ): Boolean { val reference = context.call.calleeExpression as? KtReferenceExpression ?: return false @@ -446,6 +451,15 @@ class PSICallResolver( } } + private fun NewResolutionOldInference.ResolutionKind.toKotlinCallKind(): KotlinCallKind { + return when (this) { + is NewResolutionOldInference.ResolutionKind.Function -> KotlinCallKind.FUNCTION + is NewResolutionOldInference.ResolutionKind.Variable -> KotlinCallKind.VARIABLE + is NewResolutionOldInference.ResolutionKind.Invoke -> KotlinCallKind.UNSUPPORTED + is NewResolutionOldInference.ResolutionKind.CallableReference -> KotlinCallKind.UNSUPPORTED + is NewResolutionOldInference.ResolutionKind.GivenCandidates -> KotlinCallKind.UNSUPPORTED + } + } private fun toKotlinCall( context: BasicCallResolutionContext, @@ -496,8 +510,8 @@ class PSICallResolver( astExternalArgument?.setResultDataFlowInfoIfRelevant(resultDataFlowInfo) return PSIKotlinCallImpl( - kotlinCallKind, oldCall, tracingStrategy, resolvedExplicitReceiver, name, resolvedTypeArguments, resolvedArgumentsInParenthesis, - astExternalArgument, context.dataFlowInfo, resultDataFlowInfo, context.dataFlowInfoForArguments + kotlinCallKind, oldCall, tracingStrategy, resolvedExplicitReceiver, name, resolvedTypeArguments, + resolvedArgumentsInParenthesis, astExternalArgument, context.dataFlowInfo, resultDataFlowInfo, context.dataFlowInfoForArguments ) }