Refactoring: get rid KotlinCallKind inside ResolutionKind

This commit is contained in:
Mikhail Zarechenskiy
2018-04-19 18:07:05 +03:00
parent 898105a5fa
commit f039ec3625
3 changed files with 41 additions and 27 deletions
@@ -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.components.InferenceSession;
import org.jetbrains.kotlin.resolve.calls.context.*; import org.jetbrains.kotlin.resolve.calls.context.*;
import org.jetbrains.kotlin.resolve.calls.inference.CoroutineInferenceUtilKt; 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.model.MutableDataFlowInfoForArguments;
import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResults; import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResults;
import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResultsImpl; import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResultsImpl;
@@ -190,7 +189,7 @@ public class CallResolver {
@NotNull BasicCallResolutionContext context, @NotNull BasicCallResolutionContext context,
@NotNull Name name, @NotNull Name name,
@NotNull KtReferenceExpression referenceExpression, @NotNull KtReferenceExpression referenceExpression,
@NotNull NewResolutionOldInference.ResolutionKind<D> kind @NotNull NewResolutionOldInference.ResolutionKind kind
) { ) {
TracingStrategy tracing = TracingStrategyImpl.create(referenceExpression, context.call); TracingStrategy tracing = TracingStrategyImpl.create(referenceExpression, context.call);
return computeTasksAndResolveCall(context, name, tracing, kind); return computeTasksAndResolveCall(context, name, tracing, kind);
@@ -201,7 +200,7 @@ public class CallResolver {
@NotNull BasicCallResolutionContext context, @NotNull BasicCallResolutionContext context,
@NotNull Name name, @NotNull Name name,
@NotNull TracingStrategy tracing, @NotNull TracingStrategy tracing,
@NotNull NewResolutionOldInference.ResolutionKind<D> kind @NotNull NewResolutionOldInference.ResolutionKind kind
) { ) {
return callResolvePerfCounter.<OverloadResolutionResults<D>>time(() -> { return callResolvePerfCounter.<OverloadResolutionResults<D>>time(() -> {
ResolutionTask<D> resolutionTask = new ResolutionTask<>(kind, name, null); ResolutionTask<D> resolutionTask = new ResolutionTask<>(kind, name, null);
@@ -227,7 +226,7 @@ public class CallResolver {
) { ) {
return callResolvePerfCounter.<OverloadResolutionResults<D>>time(() -> { return callResolvePerfCounter.<OverloadResolutionResults<D>>time(() -> {
ResolutionTask<D> resolutionTask = new ResolutionTask<>( ResolutionTask<D> resolutionTask = new ResolutionTask<>(
new NewResolutionOldInference.ResolutionKind.GivenCandidates<>(), null, candidates new NewResolutionOldInference.ResolutionKind.GivenCandidates(), null, candidates
); );
return doResolveCallOrGetCachedResults(context, resolutionTask, tracing); return doResolveCallOrGetCachedResults(context, resolutionTask, tracing);
}); });
@@ -553,7 +552,7 @@ public class CallResolver {
Set<ResolutionCandidate<FunctionDescriptor>> candidates = Collections.singleton(candidate); Set<ResolutionCandidate<FunctionDescriptor>> candidates = Collections.singleton(candidate);
ResolutionTask<FunctionDescriptor> resolutionTask = new ResolutionTask<>( ResolutionTask<FunctionDescriptor> resolutionTask = new ResolutionTask<>(
new NewResolutionOldInference.ResolutionKind.GivenCandidates<>(), null, candidates new NewResolutionOldInference.ResolutionKind.GivenCandidates(), null, candidates
); );
return doResolveCallOrGetCachedResults(basicCallResolutionContext, resolutionTask, tracing); return doResolveCallOrGetCachedResults(basicCallResolutionContext, resolutionTask, tracing);
@@ -569,12 +568,13 @@ public class CallResolver {
tracing.bindCall(context.trace, call); tracing.bindCall(context.trace, call);
boolean newInferenceEnabled = languageVersionSettings.supportsFeature(LanguageFeature.NewInference); 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; 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; assert resolutionTask.givenCandidates != null;
return PSICallResolver.runResolutionAndInferenceForGivenCandidates(context, resolutionTask.givenCandidates, tracing); return PSICallResolver.runResolutionAndInferenceForGivenCandidates(context, resolutionTask.givenCandidates, tracing);
} }
@@ -693,10 +693,10 @@ public class CallResolver {
final Collection<ResolutionCandidate<D>> givenCandidates; final Collection<ResolutionCandidate<D>> givenCandidates;
@NotNull @NotNull
final NewResolutionOldInference.ResolutionKind<D> resolutionKind; final NewResolutionOldInference.ResolutionKind resolutionKind;
private ResolutionTask( private ResolutionTask(
@NotNull NewResolutionOldInference.ResolutionKind<D> kind, @NotNull NewResolutionOldInference.ResolutionKind kind,
@Nullable Name name, @Nullable Name name,
@Nullable Collection<ResolutionCandidate<D>> candidates @Nullable Collection<ResolutionCandidate<D>> candidates
) { ) {
@@ -70,7 +70,7 @@ class NewResolutionOldInference(
private val coroutineInferenceSupport: CoroutineInferenceSupport, private val coroutineInferenceSupport: CoroutineInferenceSupport,
private val deprecationResolver: DeprecationResolver private val deprecationResolver: DeprecationResolver
) { ) {
sealed class ResolutionKind<D : CallableDescriptor>(val kotlinCallKind: KotlinCallKind = KotlinCallKind.UNSUPPORTED) { sealed class ResolutionKind {
abstract internal fun createTowerProcessor( abstract internal fun createTowerProcessor(
outer: NewResolutionOldInference, outer: NewResolutionOldInference,
name: Name, name: Name,
@@ -80,7 +80,7 @@ class NewResolutionOldInference(
context: BasicCallResolutionContext context: BasicCallResolutionContext
): ScopeTowerProcessor<MyCandidate> ): ScopeTowerProcessor<MyCandidate>
object Function : ResolutionKind<FunctionDescriptor>(KotlinCallKind.FUNCTION) { object Function : ResolutionKind() {
override fun createTowerProcessor( override fun createTowerProcessor(
outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy,
scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext
@@ -96,7 +96,7 @@ class NewResolutionOldInference(
} }
} }
object Variable : ResolutionKind<VariableDescriptor>(KotlinCallKind.VARIABLE) { object Variable : ResolutionKind() {
override fun createTowerProcessor( override fun createTowerProcessor(
outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy,
scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext
@@ -106,7 +106,7 @@ class NewResolutionOldInference(
} }
} }
object CallableReference : ResolutionKind<CallableDescriptor>() { object CallableReference : ResolutionKind() {
override fun createTowerProcessor( override fun createTowerProcessor(
outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy,
scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext
@@ -120,7 +120,7 @@ class NewResolutionOldInference(
} }
} }
object Invoke : ResolutionKind<FunctionDescriptor>() { object Invoke : ResolutionKind() {
override fun createTowerProcessor( override fun createTowerProcessor(
outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy,
scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext
@@ -142,7 +142,7 @@ class NewResolutionOldInference(
} }
class GivenCandidates<D : CallableDescriptor> : ResolutionKind<D>() { class GivenCandidates : ResolutionKind() {
override fun createTowerProcessor( override fun createTowerProcessor(
outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy,
scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext
@@ -155,7 +155,7 @@ class NewResolutionOldInference(
fun <D : CallableDescriptor> runResolution( fun <D : CallableDescriptor> runResolution(
context: BasicCallResolutionContext, context: BasicCallResolutionContext,
name: Name, name: Name,
kind: ResolutionKind<D>, kind: ResolutionKind,
tracing: TracingStrategy tracing: TracingStrategy
): OverloadResolutionResultsImpl<D> { ): OverloadResolutionResultsImpl<D> {
val explicitReceiver = context.call.explicitReceiver val explicitReceiver = context.call.explicitReceiver
@@ -349,7 +349,7 @@ class NewResolutionOldInference(
private fun reportAdditionalDiagnosticIfNoCandidates( private fun reportAdditionalDiagnosticIfNoCandidates(
context: BasicCallResolutionContext, context: BasicCallResolutionContext,
name: Name, name: Name,
kind: ResolutionKind<*>, kind: ResolutionKind,
scopeTower: ImplicitScopeTower, scopeTower: ImplicitScopeTower,
detailedReceiver: DetailedReceiver? detailedReceiver: DetailedReceiver?
): Boolean { ): Boolean {
@@ -64,16 +64,21 @@ class PSICallResolver(
) { ) {
private val GIVEN_CANDIDATES_NAME = Name.special("<given candidates>") private val GIVEN_CANDIDATES_NAME = Name.special("<given candidates>")
val defaultResolutionKinds = setOf(
NewResolutionOldInference.ResolutionKind.Function,
NewResolutionOldInference.ResolutionKind.Variable
)
fun <D : CallableDescriptor> runResolutionAndInference( fun <D : CallableDescriptor> runResolutionAndInference(
context: BasicCallResolutionContext, context: BasicCallResolutionContext,
name: Name, name: Name,
resolutionKind: NewResolutionOldInference.ResolutionKind<D>, resolutionKind: NewResolutionOldInference.ResolutionKind,
tracingStrategy: TracingStrategy tracingStrategy: TracingStrategy
): OverloadResolutionResults<D> { ): OverloadResolutionResults<D> {
val isBinaryRemOperator = isBinaryRemOperator(context.call) val isBinaryRemOperator = isBinaryRemOperator(context.call)
val refinedName = refineNameForRemOperator(isBinaryRemOperator, name) 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 scopeTower = ASTScopeTower(context)
val resolutionCallbacks = createResolutionCallbacks(context) val resolutionCallbacks = createResolutionCallbacks(context)
@@ -89,7 +94,7 @@ class PSICallResolver(
result = resolveToDeprecatedMod(name, context, resolutionKind, tracingStrategy, scopeTower, resolutionCallbacks, expectedType) 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() return OverloadResolutionResultsImpl.nameNotFound()
} }
@@ -124,17 +129,17 @@ class PSICallResolver(
} }
private fun <D : CallableDescriptor> resolveToDeprecatedMod( private fun resolveToDeprecatedMod(
remOperatorName: Name, remOperatorName: Name,
context: BasicCallResolutionContext, context: BasicCallResolutionContext,
resolutionKind: NewResolutionOldInference.ResolutionKind<D>, resolutionKind: NewResolutionOldInference.ResolutionKind,
tracingStrategy: TracingStrategy, tracingStrategy: TracingStrategy,
scopeTower: ImplicitScopeTower, scopeTower: ImplicitScopeTower,
resolutionCallbacks: KotlinResolutionCallbacksImpl, resolutionCallbacks: KotlinResolutionCallbacksImpl,
expectedType: UnwrappedType? expectedType: UnwrappedType?
): CallResolutionResult { ): CallResolutionResult {
val deprecatedName = OperatorConventions.REM_TO_MOD_OPERATION_NAMES[remOperatorName]!! 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) val refinedProviderForInvokeFactory = FactoryProviderForInvoke(context, scopeTower, callWithDeprecatedName)
return kotlinCallResolver.resolveCall( return kotlinCallResolver.resolveCall(
scopeTower, resolutionCallbacks, callWithDeprecatedName, expectedType, scopeTower, resolutionCallbacks, callWithDeprecatedName, expectedType,
@@ -298,7 +303,7 @@ class PSICallResolver(
private fun reportAdditionalDiagnosticIfNoCandidates( private fun reportAdditionalDiagnosticIfNoCandidates(
context: BasicCallResolutionContext, context: BasicCallResolutionContext,
scopeTower: ImplicitScopeTower, scopeTower: ImplicitScopeTower,
kind: KotlinCallKind, kind: NewResolutionOldInference.ResolutionKind,
kotlinCall: KotlinCall kotlinCall: KotlinCall
): Boolean { ): Boolean {
val reference = context.call.calleeExpression as? KtReferenceExpression ?: return false 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( private fun toKotlinCall(
context: BasicCallResolutionContext, context: BasicCallResolutionContext,
@@ -496,8 +510,8 @@ class PSICallResolver(
astExternalArgument?.setResultDataFlowInfoIfRelevant(resultDataFlowInfo) astExternalArgument?.setResultDataFlowInfoIfRelevant(resultDataFlowInfo)
return PSIKotlinCallImpl( return PSIKotlinCallImpl(
kotlinCallKind, oldCall, tracingStrategy, resolvedExplicitReceiver, name, resolvedTypeArguments, resolvedArgumentsInParenthesis, kotlinCallKind, oldCall, tracingStrategy, resolvedExplicitReceiver, name, resolvedTypeArguments,
astExternalArgument, context.dataFlowInfo, resultDataFlowInfo, context.dataFlowInfoForArguments resolvedArgumentsInParenthesis, astExternalArgument, context.dataFlowInfo, resultDataFlowInfo, context.dataFlowInfoForArguments
) )
} }