summaryrefslogtreecommitdiff
path: root/plugins/kotlin/common/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt
blob: 9c5baba72db0c91a20fa6cdc0648583754e1a12d (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 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.

@file:JvmName("ResolutionUtils")

package org.jetbrains.kotlin.idea.caches.resolve

import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.caches.resolve.KotlinCacheService
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.idea.FrontendInternals
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingTraceContext
import org.jetbrains.kotlin.resolve.ImportPath
import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.lazy.NoDescriptorForDeclarationException
import org.jetbrains.kotlin.utils.addToStdlib.safeAs

fun KtElement.getResolutionFacade(): ResolutionFacade =
    KotlinCacheService.getInstance(project).getResolutionFacade(this)

/**
 * For local declarations is equivalent to unsafeResolveToDescriptor(bodyResolveMode)
 *
 * But for non-local declarations it ignores bodyResolveMode and uses LazyDeclarationResolver directly
 */
@Deprecated(
    message = "This function has unclear semantics. Please use either unsafeResolveToDescriptor or resolveToDescriptorIfAny instead",
    replaceWith = ReplaceWith("unsafeResolveToDescriptor")
)
fun KtDeclaration.resolveToDescriptor(bodyResolveMode: BodyResolveMode = BodyResolveMode.FULL): DeclarationDescriptor =
    getResolutionFacade().resolveToDescriptor(this, bodyResolveMode)

/**
 * This function throws exception when resolveToDescriptorIfAny returns null, otherwise works equivalently.
 *
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtDeclaration.unsafeResolveToDescriptor(
    bodyResolveMode: BodyResolveMode = BodyResolveMode.FULL
): DeclarationDescriptor =
    unsafeResolveToDescriptor(getResolutionFacade(), bodyResolveMode)

/**
 * This function first uses declaration resolvers to resolve this declaration and/or additional declarations (e.g. its parent),
 * and then takes the relevant descriptor from binding context.
 * The exact set of declarations to resolve depends on bodyResolveMode
 *
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtDeclaration.resolveToDescriptorIfAny(
    bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL
): DeclarationDescriptor? =
    resolveToDescriptorIfAny(getResolutionFacade(), bodyResolveMode)

/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtClassOrObject.resolveToDescriptorIfAny(bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL) =
    resolveToDescriptorIfAny(getResolutionFacade(), bodyResolveMode)

/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtNamedFunction.resolveToDescriptorIfAny(bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL) =
    resolveToDescriptorIfAny(getResolutionFacade(), bodyResolveMode)

/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtProperty.resolveToDescriptorIfAny(bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL) =
    resolveToDescriptorIfAny(getResolutionFacade(), bodyResolveMode)

/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtParameter.resolveToParameterDescriptorIfAny(bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL) =
    resolveToParameterDescriptorIfAny(getResolutionFacade(), bodyResolveMode)

/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtElement.resolveToCall(bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL) =
    resolveToCall(getResolutionFacade(), bodyResolveMode)

fun KtFile.resolveImportReference(fqName: FqName): Collection<DeclarationDescriptor> {
    val facade = getResolutionFacade()
    return facade.resolveImportReference(facade.moduleDescriptor, fqName)
}

fun KtAnnotationEntry.resolveToDescriptorIfAny(
    bodyResolveMode: BodyResolveMode = BodyResolveMode.PARTIAL_NO_ADDITIONAL
): AnnotationDescriptor? =
    resolveToDescriptorIfAny(getResolutionFacade(), bodyResolveMode)

// This and next functions are used for 'normal' element analysis
// This analysis *should* provide all information extractable from this KtElement except:
// - for declarations, it does not analyze their bodies
// - for classes, it does not analyze their content
// - for member / top-level properties, it does not analyze initializers / accessors
// This information includes related descriptors, resolved calls (but not inside body, see above!)
// and many other binding context slices.
// Normally, the function is used on local declarations or statements / expressions
// Any usage on non-local declaration is a bit suspicious,
// consider replacing it with resolveToDescriptorIfAny and
// remember that body / content is not analyzed;
// if it's necessary, use analyzeWithContent()
//
// If you need diagnostics in result context, use BodyResolveMode.PARTIAL_WITH_DIAGNOSTICS.
// BodyResolveMode.FULL analyzes all statements on the level of KtElement and above.
// BodyResolveMode.PARTIAL analyzes only statements necessary for this KtElement precise analysis.
//
// See also: ResolveSessionForBodies, ResolveElementCache
/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
@JvmOverloads
fun KtElement.analyze(
    bodyResolveMode: BodyResolveMode = BodyResolveMode.FULL
): BindingContext =
    analyze(getResolutionFacade(), bodyResolveMode)

@JvmOverloads
fun KtElement.safeAnalyze(
    bodyResolveMode: BodyResolveMode = BodyResolveMode.FULL
): BindingContext = safeAnalyze(getResolutionFacade(), bodyResolveMode)

/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtElement.analyzeAndGetResult(): AnalysisResult {
    return analyzeAndGetResult(getResolutionFacade())
}

/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtElement.analyzeWithContentAndGetResult(): AnalysisResult =
    analyzeWithContentAndGetResult(getResolutionFacade())

fun KtElement.findModuleDescriptor(): ModuleDescriptor = getResolutionFacade().moduleDescriptor

// This function is used on declarations to make analysis not only declaration itself but also it content:
// body for declaration with body, initializer & accessors for properties
/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
fun KtDeclaration.analyzeWithContent(): BindingContext =
    analyzeWithContent(getResolutionFacade())

// This function is used to make full analysis of declaration container.
// All its declarations, including their content (see above), are analyzed.
/**
 * **Please, use overload with providing resolutionFacade for stable results of subsequent calls**
 */
inline fun <reified T> T.analyzeWithContent(): BindingContext where T : KtDeclarationContainer, T : KtElement =
    analyzeWithContent(getResolutionFacade())

/**
 * This function is expected to produce the same result as compiler for the whole file content (including diagnostics,
 * trace slices, descriptors, etc.).
 *
 * It's not recommended to call this function without real need.
 *
 * @ref [KotlinCacheService]
 * @ref [org.jetbrains.kotlin.idea.caches.resolve.PerFileAnalysisCache]
 */
fun KtFile.analyzeWithAllCompilerChecks(vararg extraFiles: KtFile): AnalysisResult =
    this.analyzeWithAllCompilerChecks(null, *extraFiles)

fun KtFile.analyzeWithAllCompilerChecks(callback: ((Diagnostic) -> Unit)?, vararg extraFiles: KtFile): AnalysisResult {
    return if (extraFiles.isEmpty()) {
        KotlinCacheService.getInstance(project).getResolutionFacade(this)
            .analyzeWithAllCompilerChecks(this, callback)
    } else {
        KotlinCacheService.getInstance(project).getResolutionFacade(listOf(this) + extraFiles.toList())
            .analyzeWithAllCompilerChecks(this, callback)
    }
}

/**
 * This function is expected to produce the same result as compiler for the given element and its children (including diagnostics,
 * trace slices, descriptors, etc.). For some expression element it actually performs analyze for some parent (usually declaration).
 *
 * It's not recommended to call this function without real need.
 *
 * NB: for statements / expressions, usually should be replaced with analyze(),
 * for declarations, analyzeWithContent() will do what you want.
 *
 * @ref [KotlinCacheService]
 * @ref [org.jetbrains.kotlin.idea.caches.resolve.PerFileAnalysisCache]
 */
@Deprecated(
    "Use either KtFile.analyzeWithAllCompilerChecks() or KtElement.analyzeAndGetResult()",
    ReplaceWith("analyzeAndGetResult()")
)
fun KtElement.analyzeWithAllCompilerChecks(): AnalysisResult = getResolutionFacade().analyzeWithAllCompilerChecks(this)

// this method don't check visibility and collect all descriptors with given fqName
@OptIn(FrontendInternals::class)
fun ResolutionFacade.resolveImportReference(
    moduleDescriptor: ModuleDescriptor,
    fqName: FqName
): Collection<DeclarationDescriptor> {
    val importDirective = KtPsiFactory(project).createImportDirective(ImportPath(fqName, false))
    val qualifiedExpressionResolver = this.getFrontendService(moduleDescriptor, QualifiedExpressionResolver::class.java)
    return qualifiedExpressionResolver.processImportReference(
        importDirective,
        moduleDescriptor,
        BindingTraceContext(),
        excludedImportNames = emptyList(),
        packageFragmentForVisibilityCheck = null
    )?.getContributedDescriptors() ?: emptyList()
}

@Suppress("DEPRECATION")
@Deprecated(
    "This method is going to be removed in 1.3.0 release",
    ReplaceWith("analyzeWithAllCompilerChecks().bindingContext"),
    DeprecationLevel.ERROR
)
fun KtElement.analyzeFully(): BindingContext = analyzeWithAllCompilerChecks().bindingContext