1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.parameterInfo
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.calls.KtFunctionCall
import org.jetbrains.kotlin.analysis.api.calls.calls
import org.jetbrains.kotlin.analysis.api.calls.symbol
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithVisibility
import org.jetbrains.kotlin.psi.*
// Analogous to Call.resolveCandidates() in plugins/kotlin/core/src/org/jetbrains/kotlin/idea/core/Utils.kt
internal fun KtAnalysisSession.resolveCallCandidates(callElement: KtElement): List<CandidateWithMapping> {
// TODO: FE 1.0 plugin collects all candidates (i.e., all overloads), even if arguments do not match. Not just resolved call.
// See Call.resolveCandidates() in core/src/org/jetbrains/kotlin/idea/core/Utils.kt. Note `replaceCollectAllCandidates(true)`.
val (resolvedCall, receiver) = when (callElement) {
is KtCallElement -> {
val parent = callElement.parent
val receiver = if (parent is KtDotQualifiedExpression && parent.selectorExpression == callElement) {
parent.receiverExpression
} else null
Pair(callElement.resolveCall(), receiver)
}
is KtArrayAccessExpression -> Pair(callElement.resolveCall(), callElement.arrayExpression)
else -> return emptyList()
}
val fileSymbol = callElement.containingKtFile.getFileSymbol()
return resolvedCall.calls.filterIsInstance<KtFunctionCall<*>>()
.filter { filterCandidate(it.symbol, callElement, fileSymbol, receiver) }
.map {
CandidateWithMapping(
it.partiallyAppliedSymbol.signature,
it.argumentMapping,
)
}
}
internal fun KtAnalysisSession.filterCandidate(
candidateSymbol: KtSymbol,
callElement: KtElement,
fileSymbol: KtFileSymbol,
receiver: KtExpression?
): Boolean {
if (callElement is KtConstructorDelegationCall) {
// Exclude caller from candidates for `this(...)` delegated constructor calls.
// The parent of KtDelegatedConstructorCall should be the KtConstructor. We don't need to get the symbol for the constructor
// to determine if it's a self-call; we can just compare the candidate's PSI.
val candidatePsi = candidateSymbol.psi
if (candidatePsi != null && candidatePsi == callElement.parent) {
return false
}
}
if (receiver != null && candidateSymbol is KtCallableSymbol) {
// Filter out candidates with wrong receiver
val receiverType = receiver.getKtType() ?: error("Receiver should have a KtType")
val candidateReceiverType = candidateSymbol.receiverType
if (candidateReceiverType != null && receiverType.isNotSubTypeOf(candidateReceiverType)) return false
}
// Filter out candidates not visible from call site
if (candidateSymbol is KtSymbolWithVisibility && !isVisible(candidateSymbol, fileSymbol, receiver, callElement)) return false
return true
}
internal data class CandidateWithMapping(
val candidate: KtFunctionLikeSignature<KtFunctionLikeSymbol>,
val argumentMapping: LinkedHashMap<KtExpression, KtVariableLikeSignature<KtValueParameterSymbol>>,
)
|