/
KtFe10ScopeResolution.kt
134 lines (113 loc) · 5.67 KB
/
KtFe10ScopeResolution.kt
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
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.api.descriptors.scopes
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtConstructorSymbol
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtSymbol
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeOwner
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtPossiblyNamedSymbol
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.*
internal abstract class KtFe10ScopeResolution : KtScope, KtLifetimeOwner {
abstract val analysisContext: Fe10AnalysisContext
abstract val scope: ResolutionScope
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> = withValidityAssertion {
return scope
.getContributedDescriptors(kindFilter = DescriptorKindFilter.ALL, nameFilter)
.asSequence()
.filter { nameFilter(it.name) }
.mapNotNull { it.toKtSymbol(analysisContext) as? KtCallableSymbol }
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> = withValidityAssertion {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getCallableSymbols { it in namesSet }
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
return scope
.getContributedDescriptors(kindFilter = DescriptorKindFilter.CLASSIFIERS, nameFilter)
.asSequence()
.filter { nameFilter(it.name) }
.mapNotNull { it.toKtSymbol(analysisContext) as? KtClassifierSymbol }
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> = withValidityAssertion {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getClassifierSymbols { it in namesSet }
}
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
emptySequence()
}
override val token: KtLifetimeToken
get() = analysisContext.token
}
internal class KtFe10ScopeLexical(
override val scope: LexicalScope,
override val analysisContext: Fe10AnalysisContext
) : KtFe10ScopeResolution(), KtLifetimeOwner {
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
return emptySet()
}
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
return emptySet()
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion {
return scope
.getContributedDescriptors(kindFilter = DescriptorKindFilter.FUNCTIONS)
.asSequence()
.filterIsInstance<ConstructorDescriptor>()
.map { it.toKtConstructorSymbol(analysisContext) }
}
}
internal open class KtFe10ScopeMember(
override val scope: MemberScope,
private val constructors: Collection<ConstructorDescriptor>,
override val analysisContext: Fe10AnalysisContext
) : KtFe10ScopeResolution() {
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
return scope.getFunctionNames() + scope.getVariableNames()
}
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
return scope.getClassifierNames() ?: emptySet()
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = sequence {
constructors.forEach { yield(it.toKtConstructorSymbol(analysisContext)) }
}
}
internal open class KtFe10ScopeNonStaticMember(
scope: MemberScope,
constructors: Collection<ConstructorDescriptor>,
analysisContext: Fe10AnalysisContext
) : KtFe10ScopeMember(scope, constructors, analysisContext) {
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> =
super.getClassifierSymbols(nameFilter).filter { it is KtNamedClassOrObjectSymbol && it.isInner }
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> = withValidityAssertion {
super.getCallableSymbols(nameFilter).filter { symbol ->
when (symbol) {
is KtFunctionSymbol -> !symbol.isStatic
is KtPropertySymbol -> !symbol.isStatic
else -> true
}
}
}
}
internal class KtFe10ScopeImporting(
override val scope: ImportingScope,
override val analysisContext: Fe10AnalysisContext
) : KtFe10ScopeResolution() {
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
return getCallableSymbols().mapNotNullTo(mutableSetOf()) { (it as? KtPossiblyNamedSymbol)?.name }
}
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
return getClassifierSymbols().mapNotNullTo(mutableSetOf()) { (it as? KtPossiblyNamedSymbol)?.name }
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion { emptySequence() }
}