[NI] Introduce basic resolver for many candidates

This commit is contained in:
Mikhail Zarechenskiy
2018-03-15 14:15:27 +03:00
parent 3078e5a6ff
commit 0f60965595
7 changed files with 180 additions and 48 deletions
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
import org.jetbrains.kotlin.resolve.scopes.receivers.CastImplicitClassReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitClassReceiver
@@ -71,7 +72,8 @@ class KotlinToResolvedCallTransformer(
fun <D : CallableDescriptor> transformAndReport(
baseResolvedCall: CallResolutionResult,
context: BasicCallResolutionContext
context: BasicCallResolutionContext,
tracingStrategy: TracingStrategy
): ResolvedCall<D> {
return when (baseResolvedCall) {
is PartialCallResolutionResult -> {
@@ -84,7 +86,9 @@ class KotlinToResolvedCallTransformer(
psiKotlinCall.psiCall
context.trace.record(BindingContext.ONLY_RESOLVED_CALL, psiCall, baseResolvedCall)
context.inferenceSession.addPartiallyResolvedCall(baseResolvedCall)
context.inferenceSession.addPartialCallInfo(
PSIPartialCallInfo(baseResolvedCall, context, tracingStrategy)
)
createStubResolvedCallAndWriteItToTrace(candidate, context.trace, baseResolvedCall.diagnostics)
}
@@ -5,17 +5,110 @@
package org.jetbrains.kotlin.resolve.calls.tower
import org.jetbrains.kotlin.resolve.calls.components.InferenceSession
import org.jetbrains.kotlin.resolve.calls.model.CallResolutionResult
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.calls.components.*
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResults
import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy
abstract class ManyCandidatesResolver : InferenceSession {
private val partiallyResolvedCalls = arrayListOf<CallResolutionResult>()
abstract class ManyCandidatesResolver<D : CallableDescriptor>(
private val psiCallResolver: PSICallResolver,
private val postponedArgumentsAnalyzer: PostponedArgumentsAnalyzer,
private val kotlinConstraintSystemCompleter: KotlinConstraintSystemCompleter,
private val callComponents: KotlinCallComponents,
val builtIns: KotlinBuiltIns
) : InferenceSession {
private val partiallyResolvedCallsInfo = arrayListOf<PSIPartialCallInfo>()
private val errorCallsInfo = arrayListOf<PSIErrorCallInfo<D>>()
override fun shouldFixTypeVariables(): Boolean {
abstract fun prepareForCompletion(commonSystem: NewConstraintSystem, resolvedCallsInfo: List<PSIPartialCallInfo>)
override fun shouldFixTypeVariables(candidate: KotlinResolutionCandidate): Boolean {
return false
}
override fun addPartiallyResolvedCall(call: CallResolutionResult) {
partiallyResolvedCalls.add(call)
override fun addPartialCallInfo(callInfo: PartialCallInfo) {
if (callInfo !is PSIPartialCallInfo) {
throw AssertionError("Call info for $callInfo should be instance of PSIPartialCallInfo")
}
partiallyResolvedCallsInfo.add(callInfo)
}
}
override fun addErrorCallInfo(callInfo: ErrorCallInfo) {
if (callInfo !is PSIErrorCallInfo<*>) {
throw AssertionError("Error call info for $callInfo should be instance of PSIErrorCallInfo")
}
errorCallsInfo.add(callInfo as PSIErrorCallInfo<D>)
}
override fun currentConstraintSystem(): ConstraintStorage {
return partiallyResolvedCallsInfo.lastOrNull()?.callResolutionResult?.constraintSystem ?: ConstraintStorage.Empty
}
fun resolveCandidates(resolutionCallbacks: KotlinResolutionCallbacks): List<ResolutionResultCallInfo<D>> {
val resolvedCallsInfo = partiallyResolvedCallsInfo.toList()
val commonSystem = NewConstraintSystemImpl(callComponents.constraintInjector, builtIns).apply {
addOtherSystem(currentConstraintSystem())
}
prepareForCompletion(commonSystem, resolvedCallsInfo)
val diagnosticHolder = KotlinDiagnosticsHolder.SimpleHolder()
kotlinConstraintSystemCompleter.runCompletion(
commonSystem.asConstraintSystemCompleterContext(),
KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.FULL,
resolvedCallsInfo.map { it.callResolutionResult },
builtIns.unitType
) {
postponedArgumentsAnalyzer.analyze(
commonSystem.asPostponedArgumentsAnalyzerContext(), resolutionCallbacks, it, diagnosticHolder
)
}
val allCandidates = arrayListOf<ResolutionResultCallInfo<D>>()
resolvedCallsInfo.mapTo(allCandidates) {
val resolutionResult = it.asCallResolutionResult(diagnosticHolder, commonSystem)
ResolutionResultCallInfo(
resolutionResult, psiCallResolver.convertToOverloadResolutionResults(it.context, resolutionResult, it.tracingStrategy)
)
}
errorCallsInfo.mapTo(allCandidates) { ResolutionResultCallInfo(it.callResolutionResult, it.result) }
return allCandidates
}
private fun PartialCallInfo.asCallResolutionResult(
diagnosticsHolder: KotlinDiagnosticsHolder.SimpleHolder,
commonSystem: NewConstraintSystem
): CallResolutionResult {
val diagnostics = diagnosticsHolder.getDiagnostics() + callResolutionResult.diagnostics + commonSystem.diagnostics
return CompletedCallResolutionResult(callResolutionResult.resultCallAtom, diagnostics, commonSystem.asReadOnlyStorage())
}
}
data class ResolutionResultCallInfo<D : CallableDescriptor>(
val resolutionResult: CallResolutionResult,
val overloadResolutionResults: OverloadResolutionResults<D>
)
class PSIPartialCallInfo(
override val callResolutionResult: PartialCallResolutionResult,
val context: BasicCallResolutionContext,
val tracingStrategy: TracingStrategy
) : PartialCallInfo
class PSIErrorCallInfo<D : CallableDescriptor>(
override val callResolutionResult: CallResolutionResult,
val result: OverloadResolutionResults<D>
) : ErrorCallInfo
@@ -173,7 +173,7 @@ class PSICallResolver(
}
}
private fun <D : CallableDescriptor> convertToOverloadResolutionResults(
fun <D : CallableDescriptor> convertToOverloadResolutionResults(
context: BasicCallResolutionContext,
result: CallResolutionResult,
tracingStrategy: TracingStrategy
@@ -189,26 +189,12 @@ class PSICallResolver(
val trace = context.trace
result.diagnostics.firstIsInstanceOrNull<NoneCandidatesCallDiagnostic>()?.let {
tracingStrategy.unresolvedReference(trace)
return OverloadResolutionResultsImpl.nameNotFound()
handleErrorResolutionResult<D>(trace, result, tracingStrategy)?.let { errorResult ->
context.inferenceSession.addErrorCallInfo(PSIErrorCallInfo(result, errorResult))
return errorResult
}
result.diagnostics.firstIsInstanceOrNull<ManyCandidatesCallDiagnostic>()?.let {
return transformManyCandidatesAndRecordTrace(it, tracingStrategy, trace)
}
val isInapplicableReceiver =
getResultApplicability(result.diagnostics) == ResolutionCandidateApplicability.INAPPLICABLE_WRONG_RECEIVER
val resolvedCall = if (isInapplicableReceiver) {
val singleCandidate = result.resultCallAtom() ?: error("Should be not null for result: $result")
kotlinToResolvedCallTransformer.onlyTransform<D>(singleCandidate, result.diagnostics).also {
tracingStrategy.unresolvedReferenceWrongReceiver(trace, listOf(it))
}
} else {
kotlinToResolvedCallTransformer.transformAndReport<D>(result, context)
}
val resolvedCall = kotlinToResolvedCallTransformer.transformAndReport<D>(result, context, tracingStrategy)
// NB. Be careful with moving this invocation, as effect system expects resolution results to be written in trace
// (see EffectSystem for details)
@@ -217,6 +203,34 @@ class PSICallResolver(
return SingleOverloadResolutionResult(resolvedCall)
}
private fun <D : CallableDescriptor> handleErrorResolutionResult(
trace: BindingTrace,
result: CallResolutionResult,
tracingStrategy: TracingStrategy
): OverloadResolutionResults<D>? {
val diagnostics = result.diagnostics
diagnostics.firstIsInstanceOrNull<NoneCandidatesCallDiagnostic>()?.let {
tracingStrategy.unresolvedReference(trace)
return OverloadResolutionResultsImpl.nameNotFound()
}
diagnostics.firstIsInstanceOrNull<ManyCandidatesCallDiagnostic>()?.let {
return transformManyCandidatesAndRecordTrace(it, tracingStrategy, trace)
}
if (getResultApplicability(diagnostics) == ResolutionCandidateApplicability.INAPPLICABLE_WRONG_RECEIVER) {
val singleCandidate = result.resultCallAtom() ?: error("Should be not null for result: $result")
val resolvedCall = kotlinToResolvedCallTransformer.onlyTransform<D>(singleCandidate, diagnostics).also {
tracingStrategy.unresolvedReferenceWrongReceiver(trace, listOf(it))
}
return SingleOverloadResolutionResult(resolvedCall)
}
return null
}
private fun <D : CallableDescriptor> transformManyCandidatesAndRecordTrace(
diagnostic: ManyCandidatesCallDiagnostic,
tracingStrategy: TracingStrategy,
@@ -360,7 +374,7 @@ class PSICallResolver(
override fun factoryForVariable(stripExplicitReceiver: Boolean): CandidateFactory<KotlinResolutionCandidate> {
val explicitReceiver = if (stripExplicitReceiver) null else kotlinCall.explicitReceiver
val variableCall = PSIKotlinCallForVariable(kotlinCall, explicitReceiver, kotlinCall.name)
return SimpleCandidateFactory(callComponents, scopeTower, variableCall)
return SimpleCandidateFactory(callComponents, scopeTower, variableCall, context.inferenceSession)
}
override fun factoryForInvoke(variable: KotlinResolutionCandidate, useExplicitReceiver: Boolean):
@@ -380,7 +394,9 @@ class PSICallResolver(
PSIKotlinCallForInvoke(kotlinCall, variable, variableCallArgument, null)
}
return variableCallArgument.receiver to SimpleCandidateFactory(callComponents, scopeTower, callForInvoke)
return variableCallArgument.receiver to SimpleCandidateFactory(
callComponents, scopeTower, callForInvoke, context.inferenceSession
)
}
// todo: create special check that there is no invoke on variable