Skip to content

Commit 4dbd4a5

Browse files
committed
Compose: adapt new API for intercepting candidates in call resolvers
Most part is made by @lelandrichardson
1 parent 500acf4 commit 4dbd4a5

File tree

6 files changed

+227
-57
lines changed

6 files changed

+227
-57
lines changed

compiler/frontend/src/org/jetbrains/kotlin/extensions/internal/CandidateInterceptor.kt

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.extensions.internal
77

88
import com.intellij.openapi.project.Project
99
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
10+
import org.jetbrains.kotlin.descriptors.VariableDescriptor
1011
import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor
1112
import org.jetbrains.kotlin.incremental.components.LookupLocation
1213
import org.jetbrains.kotlin.name.Name
@@ -18,7 +19,9 @@ import org.jetbrains.kotlin.resolve.calls.model.CallResolutionResult
1819
import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy
1920
import org.jetbrains.kotlin.resolve.calls.tower.ImplicitScopeTower
2021
import org.jetbrains.kotlin.resolve.calls.tower.NewResolutionOldInference
22+
import org.jetbrains.kotlin.resolve.calls.tower.PSICallResolver
2123
import org.jetbrains.kotlin.resolve.scopes.ResolutionScope
24+
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo
2225

2326
@OptIn(InternalNonStableExtensionPoints::class)
2427
class CandidateInterceptor(project: Project) {
@@ -28,42 +31,66 @@ class CandidateInterceptor(project: Project) {
2831
candidates: Collection<NewResolutionOldInference.MyCandidate>,
2932
context: BasicCallResolutionContext,
3033
candidateResolver: CandidateResolver,
31-
callResolver: CallResolver?,
34+
callResolver: CallResolver,
3235
name: Name,
3336
kind: NewResolutionOldInference.ResolutionKind,
3437
tracing: TracingStrategy
3538
): Collection<NewResolutionOldInference.MyCandidate> = extensions.fold(candidates) { it, extension ->
3639
extension.interceptCandidates(it, context, candidateResolver, callResolver, name, kind, tracing)
3740
}
3841

39-
fun interceptResolvedCandidates(
40-
callResolutionResult: CallResolutionResult,
41-
context: BasicCallResolutionContext,
42-
callResolver: KotlinCallResolver,
42+
fun interceptFunctionCandidates(
43+
candidates: Collection<FunctionDescriptor>,
44+
scopeTower: ImplicitScopeTower,
45+
resolutionContext: BasicCallResolutionContext,
46+
resolutionScope: ResolutionScope,
47+
callResolver: CallResolver,
4348
name: Name,
44-
kind: NewResolutionOldInference.ResolutionKind,
45-
tracing: TracingStrategy
46-
): CallResolutionResult {
47-
var resolutionResult = callResolutionResult
48-
for (extension in extensions) {
49-
resolutionResult = extension.interceptCandidates(
50-
resolutionResult, context, callResolver, name, kind, tracing
51-
)
52-
}
53-
54-
return resolutionResult
49+
location: LookupLocation
50+
): Collection<FunctionDescriptor> = extensions.fold(candidates) { it, extension ->
51+
extension.interceptFunctionCandidates(it, scopeTower, resolutionContext, resolutionScope, callResolver, name, location)
5552
}
5653

57-
fun interceptCandidates(
54+
fun interceptFunctionCandidates(
5855
candidates: Collection<FunctionDescriptor>,
5956
scopeTower: ImplicitScopeTower,
6057
resolutionContext: BasicCallResolutionContext,
6158
resolutionScope: ResolutionScope,
62-
callResolver: CallResolver?,
59+
callResolver: PSICallResolver,
6360
name: Name,
64-
location: LookupLocation
61+
location: LookupLocation,
62+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
63+
extensionReceiver: ReceiverValueWithSmartCastInfo?
6564
): Collection<FunctionDescriptor> = extensions.fold(candidates) { it, extension ->
66-
extension.interceptCandidates(it, scopeTower, resolutionContext, resolutionScope, callResolver, name, location)
65+
extension.interceptFunctionCandidates(
66+
it, scopeTower, resolutionContext, resolutionScope, callResolver, name, location, dispatchReceiver, extensionReceiver
67+
)
68+
}
69+
70+
fun interceptVariableCandidates(
71+
candidates: Collection<VariableDescriptor>,
72+
scopeTower: ImplicitScopeTower,
73+
resolutionContext: BasicCallResolutionContext,
74+
resolutionScope: ResolutionScope,
75+
callResolver: CallResolver,
76+
name: Name,
77+
location: LookupLocation
78+
): Collection<VariableDescriptor> = extensions.fold(candidates) { it, extension ->
79+
extension.interceptVariableCandidates(it, scopeTower, resolutionContext, resolutionScope, callResolver, name, location)
80+
}
81+
82+
fun interceptVariableCandidates(
83+
candidates: Collection<VariableDescriptor>,
84+
scopeTower: ImplicitScopeTower,
85+
resolutionContext: BasicCallResolutionContext,
86+
resolutionScope: ResolutionScope,
87+
callResolver: PSICallResolver,
88+
name: Name,
89+
location: LookupLocation,
90+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
91+
extensionReceiver: ReceiverValueWithSmartCastInfo?
92+
): Collection<VariableDescriptor> = extensions.fold(candidates) { it, extension ->
93+
extension.interceptVariableCandidates(it, scopeTower, resolutionContext, resolutionScope, callResolver, name, location, dispatchReceiver, extensionReceiver)
6794
}
6895

6996
companion object : ProjectExtensionDescriptor<CallResolutionInterceptorExtension>(

compiler/frontend/src/org/jetbrains/kotlin/extensions/internal/NonStableExtensionPoints.kt

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
package org.jetbrains.kotlin.extensions.internal
77

88
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
9+
import org.jetbrains.kotlin.descriptors.VariableDescriptor
910
import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor
1011
import org.jetbrains.kotlin.incremental.components.LookupLocation
1112
import org.jetbrains.kotlin.name.Name
1213
import org.jetbrains.kotlin.psi.KtElement
1314
import org.jetbrains.kotlin.psi.KtLambdaExpression
1415
import org.jetbrains.kotlin.resolve.calls.CallResolver
1516
import org.jetbrains.kotlin.resolve.calls.CandidateResolver
16-
import org.jetbrains.kotlin.resolve.calls.KotlinCallResolver
1717
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
18-
import org.jetbrains.kotlin.resolve.calls.model.CallResolutionResult
1918
import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy
2019
import org.jetbrains.kotlin.resolve.calls.tower.ImplicitScopeTower
2120
import org.jetbrains.kotlin.resolve.calls.tower.NewResolutionOldInference
21+
import org.jetbrains.kotlin.resolve.calls.tower.PSICallResolver
2222
import org.jetbrains.kotlin.resolve.scopes.ResolutionScope
23+
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo
2324
import org.jetbrains.kotlin.types.KotlinType
2425
import org.jetbrains.kotlin.types.expressions.ExpressionTypingContext
2526

@@ -51,26 +52,68 @@ interface TypeResolutionInterceptorExtension {
5152

5253
@InternalNonStableExtensionPoints
5354
interface CallResolutionInterceptorExtension {
55+
@JvmDefault
5456
fun interceptCandidates(
5557
candidates: Collection<NewResolutionOldInference.MyCandidate>,
5658
context: BasicCallResolutionContext,
5759
candidateResolver: CandidateResolver,
58-
callResolver: CallResolver?,
60+
callResolver: CallResolver,
5961
name: Name,
6062
kind: NewResolutionOldInference.ResolutionKind,
6163
tracing: TracingStrategy
6264
): Collection<NewResolutionOldInference.MyCandidate> = candidates
6365

6466
@JvmDefault
65-
fun interceptCandidates(
66-
callResolutionResult: CallResolutionResult,
67-
context: BasicCallResolutionContext,
68-
callResolver: KotlinCallResolver,
67+
fun interceptFunctionCandidates(
68+
candidates: Collection<FunctionDescriptor>,
69+
scopeTower: ImplicitScopeTower,
70+
resolutionContext: BasicCallResolutionContext,
71+
resolutionScope: ResolutionScope,
72+
callResolver: CallResolver,
6973
name: Name,
70-
kind: NewResolutionOldInference.ResolutionKind,
71-
tracing: TracingStrategy
72-
): CallResolutionResult = callResolutionResult
74+
location: LookupLocation
75+
): Collection<FunctionDescriptor> = candidates
7376

77+
@JvmDefault
78+
fun interceptFunctionCandidates(
79+
candidates: Collection<FunctionDescriptor>,
80+
scopeTower: ImplicitScopeTower,
81+
resolutionContext: BasicCallResolutionContext,
82+
resolutionScope: ResolutionScope,
83+
callResolver: PSICallResolver,
84+
name: Name,
85+
location: LookupLocation,
86+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
87+
extensionReceiver: ReceiverValueWithSmartCastInfo?
88+
): Collection<FunctionDescriptor> = candidates
89+
90+
@JvmDefault
91+
fun interceptVariableCandidates(
92+
candidates: Collection<VariableDescriptor>,
93+
scopeTower: ImplicitScopeTower,
94+
resolutionContext: BasicCallResolutionContext,
95+
resolutionScope: ResolutionScope,
96+
callResolver: CallResolver,
97+
name: Name,
98+
location: LookupLocation
99+
): Collection<VariableDescriptor> = candidates
100+
101+
@JvmDefault
102+
fun interceptVariableCandidates(
103+
candidates: Collection<VariableDescriptor>,
104+
scopeTower: ImplicitScopeTower,
105+
resolutionContext: BasicCallResolutionContext,
106+
resolutionScope: ResolutionScope,
107+
callResolver: PSICallResolver,
108+
name: Name,
109+
location: LookupLocation,
110+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
111+
extensionReceiver: ReceiverValueWithSmartCastInfo?
112+
): Collection<VariableDescriptor> = candidates
113+
114+
@Suppress("DeprecatedCallableAddReplaceWith")
115+
@Deprecated("Please use dedicated interceptVariableCandidates and interceptFunctionCandidates instead")
116+
@JvmDefault
74117
fun interceptCandidates(
75118
candidates: Collection<FunctionDescriptor>,
76119
scopeTower: ImplicitScopeTower,
@@ -80,4 +123,4 @@ interface CallResolutionInterceptorExtension {
80123
name: Name,
81124
location: LookupLocation
82125
): Collection<FunctionDescriptor> = candidates
83-
}
126+
}

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,27 @@ class NewResolutionOldInference(
379379
override val isNewInferenceEnabled: Boolean
380380
get() = resolutionContext.languageVersionSettings.supportsFeature(LanguageFeature.NewInference)
381381

382-
override fun interceptCandidates(
382+
383+
override fun interceptFunctionCandidates(
383384
resolutionScope: ResolutionScope,
384385
name: Name,
385386
initialResults: Collection<FunctionDescriptor>,
386-
location: LookupLocation
387+
location: LookupLocation,
388+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
389+
extensionReceiver: ReceiverValueWithSmartCastInfo?
387390
): Collection<FunctionDescriptor> {
388-
return candidateInterceptor.interceptCandidates(initialResults, this, resolutionContext, resolutionScope, callResolver, name, location)
391+
return candidateInterceptor.interceptFunctionCandidates(initialResults, this, resolutionContext, resolutionScope, callResolver, name, location)
392+
}
393+
394+
override fun interceptVariableCandidates(
395+
resolutionScope: ResolutionScope,
396+
name: Name,
397+
initialResults: Collection<VariableDescriptor>,
398+
location: LookupLocation,
399+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
400+
extensionReceiver: ReceiverValueWithSmartCastInfo?
401+
): Collection<VariableDescriptor> {
402+
return candidateInterceptor.interceptVariableCandidates(initialResults, this, resolutionContext, resolutionScope, callResolver, name, location)
389403
}
390404
}
391405

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ class PSICallResolver(
110110
return OverloadResolutionResultsImpl.nameNotFound()
111111
}
112112

113-
result = candidateInterceptor.interceptResolvedCandidates(
114-
result, context, kotlinCallResolver, name, resolutionKind, tracingStrategy
115-
)
116-
117113
val overloadResolutionResults = convertToOverloadResolutionResults<D>(context, result, tracingStrategy)
118114
return overloadResolutionResults.also {
119115
clearCacheForApproximationResults()
@@ -407,13 +403,46 @@ class PSICallResolver(
407403
}
408404
}
409405

410-
override fun interceptCandidates(
406+
override fun interceptFunctionCandidates(
411407
resolutionScope: ResolutionScope,
412408
name: Name,
413409
initialResults: Collection<FunctionDescriptor>,
414-
location: LookupLocation
410+
location: LookupLocation,
411+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
412+
extensionReceiver: ReceiverValueWithSmartCastInfo?
415413
): Collection<FunctionDescriptor> {
416-
return candidateInterceptor.interceptCandidates(initialResults, this, context, resolutionScope, null, name, location)
414+
return candidateInterceptor.interceptFunctionCandidates(
415+
initialResults,
416+
this,
417+
context,
418+
resolutionScope,
419+
this@PSICallResolver,
420+
name,
421+
location,
422+
dispatchReceiver,
423+
extensionReceiver
424+
)
425+
}
426+
427+
override fun interceptVariableCandidates(
428+
resolutionScope: ResolutionScope,
429+
name: Name,
430+
initialResults: Collection<VariableDescriptor>,
431+
location: LookupLocation,
432+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
433+
extensionReceiver: ReceiverValueWithSmartCastInfo?
434+
): Collection<VariableDescriptor> {
435+
return candidateInterceptor.interceptVariableCandidates(
436+
initialResults,
437+
this,
438+
context,
439+
resolutionScope,
440+
this@PSICallResolver,
441+
name,
442+
location,
443+
dispatchReceiver,
444+
extensionReceiver
445+
)
417446
}
418447
}
419448

compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,23 @@ interface ImplicitScopeTower {
4444

4545
val typeApproximator: TypeApproximator
4646

47-
fun interceptCandidates(
47+
fun interceptFunctionCandidates(
4848
resolutionScope: ResolutionScope,
4949
name: Name,
5050
initialResults: Collection<FunctionDescriptor>,
51-
location: LookupLocation
51+
location: LookupLocation,
52+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
53+
extensionReceiver: ReceiverValueWithSmartCastInfo?
5254
): Collection<FunctionDescriptor>
55+
56+
fun interceptVariableCandidates(
57+
resolutionScope: ResolutionScope,
58+
name: Name,
59+
initialResults: Collection<VariableDescriptor>,
60+
location: LookupLocation,
61+
dispatchReceiver: ReceiverValueWithSmartCastInfo?,
62+
extensionReceiver: ReceiverValueWithSmartCastInfo?
63+
): Collection<VariableDescriptor>
5364
}
5465

5566
interface ScopeTowerLevel {

0 commit comments

Comments
 (0)