Optimize scopes handling inside ChainedMemberScope
This commit is contained in:
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
|||||||
import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl
|
import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.utils.Printer
|
import org.jetbrains.kotlin.utils.Printer
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
interface Qualifier : QualifierReceiver {
|
interface Qualifier : QualifierReceiver {
|
||||||
val referenceExpression: KtSimpleNameExpression
|
val referenceExpression: KtSimpleNameExpression
|
||||||
@@ -72,17 +71,13 @@ class ClassQualifier(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override val staticScope: MemberScope
|
override val staticScope: MemberScope
|
||||||
get() {
|
get() =
|
||||||
val scopes = ArrayList<MemberScope>(2)
|
if (descriptor.kind == ClassKind.ENUM_ENTRY) descriptor.staticScope
|
||||||
|
else ChainedMemberScope.create(
|
||||||
scopes.add(descriptor.staticScope)
|
"Static scope for ${descriptor.name} as class or object",
|
||||||
|
descriptor.staticScope,
|
||||||
if (descriptor.kind != ClassKind.ENUM_ENTRY) {
|
descriptor.unsubstitutedInnerClassesScope
|
||||||
scopes.add(descriptor.unsubstitutedInnerClassesScope)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
return ChainedMemberScope("Static scope for ${descriptor.name} as class or object", scopes)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString() = "Class{$descriptor}"
|
override fun toString() = "Class{$descriptor}"
|
||||||
}
|
}
|
||||||
@@ -100,9 +95,10 @@ class TypeAliasQualifier(
|
|||||||
override val staticScope: MemberScope
|
override val staticScope: MemberScope
|
||||||
get() = when {
|
get() = when {
|
||||||
DescriptorUtils.isEnumClass(classDescriptor) ->
|
DescriptorUtils.isEnumClass(classDescriptor) ->
|
||||||
ChainedMemberScope(
|
ChainedMemberScope.create(
|
||||||
"Static scope for typealias ${descriptor.name}",
|
"Static scope for typealias ${descriptor.name}",
|
||||||
listOf(classDescriptor.staticScope, EnumEntriesScope())
|
classDescriptor.staticScope,
|
||||||
|
EnumEntriesScope()
|
||||||
)
|
)
|
||||||
else ->
|
else ->
|
||||||
classDescriptor.staticScope
|
classDescriptor.staticScope
|
||||||
|
|||||||
+1
-1
@@ -198,7 +198,7 @@ abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdir() {
|
|||||||
val list = ArrayList<MemberScope>(packageScopes.size + 1)
|
val list = ArrayList<MemberScope>(packageScopes.size + 1)
|
||||||
list.add(ScopeWithClassifiers(classes))
|
list.add(ScopeWithClassifiers(classes))
|
||||||
list.addAll(packageScopes)
|
list.addAll(packageScopes)
|
||||||
scope = ChainedMemberScope("synthetic package view for test", list)
|
scope = ChainedMemberScope.create("synthetic package view for test", list)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val fqName: FqName
|
override val fqName: FqName
|
||||||
|
|||||||
@@ -24,26 +24,28 @@ import org.jetbrains.kotlin.name.Name
|
|||||||
import org.jetbrains.kotlin.util.collectionUtils.getFirstClassifierDiscriminateHeaders
|
import org.jetbrains.kotlin.util.collectionUtils.getFirstClassifierDiscriminateHeaders
|
||||||
import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes
|
import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes
|
||||||
import org.jetbrains.kotlin.utils.Printer
|
import org.jetbrains.kotlin.utils.Printer
|
||||||
|
import org.jetbrains.kotlin.utils.SmartList
|
||||||
|
|
||||||
class ChainedMemberScope(
|
class ChainedMemberScope private constructor(
|
||||||
internal val debugName: String,
|
private val debugName: String,
|
||||||
private val scopes: List<MemberScope>
|
private val scopes: Array<out MemberScope>
|
||||||
) : MemberScope {
|
) : MemberScope {
|
||||||
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor?
|
|
||||||
= getFirstClassifierDiscriminateHeaders(scopes) { it.getContributedClassifier(name, location) }
|
|
||||||
|
|
||||||
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor>
|
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? =
|
||||||
= getFromAllScopes(scopes) { it.getContributedVariables(name, location) }
|
getFirstClassifierDiscriminateHeaders(scopes) { it.getContributedClassifier(name, location) }
|
||||||
|
|
||||||
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor>
|
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> =
|
||||||
= getFromAllScopes(scopes) { it.getContributedFunctions(name, location) }
|
getFromAllScopes(scopes) { it.getContributedVariables(name, location) }
|
||||||
|
|
||||||
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
|
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> =
|
||||||
= getFromAllScopes(scopes) { it.getContributedDescriptors(kindFilter, nameFilter) }
|
getFromAllScopes(scopes) { it.getContributedFunctions(name, location) }
|
||||||
|
|
||||||
|
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) =
|
||||||
|
getFromAllScopes(scopes) { it.getContributedDescriptors(kindFilter, nameFilter) }
|
||||||
|
|
||||||
override fun getFunctionNames() = scopes.flatMapTo(mutableSetOf()) { it.getFunctionNames() }
|
override fun getFunctionNames() = scopes.flatMapTo(mutableSetOf()) { it.getFunctionNames() }
|
||||||
override fun getVariableNames() = scopes.flatMapTo(mutableSetOf()) { it.getVariableNames() }
|
override fun getVariableNames() = scopes.flatMapTo(mutableSetOf()) { it.getVariableNames() }
|
||||||
override fun getClassifierNames(): Set<Name>? = scopes.flatMapClassifierNamesOrNull()
|
override fun getClassifierNames(): Set<Name>? = scopes.asIterable().flatMapClassifierNamesOrNull()
|
||||||
|
|
||||||
override fun recordLookup(name: Name, location: LookupLocation) {
|
override fun recordLookup(name: Name, location: LookupLocation) {
|
||||||
scopes.forEach { it.recordLookup(name, location) }
|
scopes.forEach { it.recordLookup(name, location) }
|
||||||
@@ -64,12 +66,25 @@ class ChainedMemberScope(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun create(debugName: String, scopes: List<MemberScope>): MemberScope {
|
fun create(debugName: String, vararg scopes: MemberScope): MemberScope = create(debugName, scopes.asIterable())
|
||||||
return when (scopes.size) {
|
|
||||||
0 -> MemberScope.Empty
|
fun create(debugName: String, scopes: Iterable<MemberScope>): MemberScope {
|
||||||
1 -> scopes.single()
|
val flattenedNonEmptyScopes = SmartList<MemberScope>()
|
||||||
else -> ChainedMemberScope(debugName, scopes)
|
for (scope in scopes) {
|
||||||
|
when {
|
||||||
|
scope === MemberScope.Empty -> {}
|
||||||
|
scope is ChainedMemberScope -> flattenedNonEmptyScopes.addAll(scope.scopes)
|
||||||
|
else -> flattenedNonEmptyScopes.add(scope)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return createOrSingle(debugName, flattenedNonEmptyScopes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun createOrSingle(debugName: String, scopes: List<MemberScope>): MemberScope =
|
||||||
|
when (scopes.size) {
|
||||||
|
0 -> MemberScope.Empty
|
||||||
|
1 -> scopes[0]
|
||||||
|
else -> ChainedMemberScope(debugName, scopes.toTypedArray())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,10 @@ import org.jetbrains.kotlin.incremental.components.LookupLocation
|
|||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.resolve.selectMostSpecificInEachOverridableGroup
|
import org.jetbrains.kotlin.resolve.selectMostSpecificInEachOverridableGroup
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
|
import org.jetbrains.kotlin.util.collectionUtils.listOfNonEmptyScopes
|
||||||
import org.jetbrains.kotlin.utils.Printer
|
import org.jetbrains.kotlin.utils.Printer
|
||||||
|
|
||||||
class TypeIntersectionScope private constructor(override val workerScope: ChainedMemberScope) : AbstractScopeAdapter() {
|
class TypeIntersectionScope private constructor(private val debugName: String, override val workerScope: MemberScope) : AbstractScopeAdapter() {
|
||||||
override fun getContributedFunctions(name: Name, location: LookupLocation) =
|
override fun getContributedFunctions(name: Name, location: LookupLocation) =
|
||||||
super.getContributedFunctions(name, location).selectMostSpecificInEachOverridableGroup { this }
|
super.getContributedFunctions(name, location).selectMostSpecificInEachOverridableGroup { this }
|
||||||
|
|
||||||
@@ -39,17 +40,19 @@ class TypeIntersectionScope private constructor(override val workerScope: Chaine
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun printScopeStructure(p: Printer) {
|
override fun printScopeStructure(p: Printer) {
|
||||||
p.print("TypeIntersectionScope for: " + workerScope.debugName)
|
p.print("TypeIntersectionScope for: " + debugName)
|
||||||
super.printScopeStructure(p)
|
super.printScopeStructure(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun create(message: String, types: Collection<KotlinType>): MemberScope {
|
fun create(message: String, types: Collection<KotlinType>): MemberScope {
|
||||||
val chainedScope = ChainedMemberScope(message, types.map { it.memberScope })
|
val nonEmptyScopes = listOfNonEmptyScopes(types.map { it.memberScope })
|
||||||
if (types.size <= 1) return chainedScope
|
val chainedOrSingle = ChainedMemberScope.createOrSingle(message, nonEmptyScopes)
|
||||||
|
|
||||||
return TypeIntersectionScope(chainedScope)
|
if (nonEmptyScopes.size <= 1) return chainedOrSingle
|
||||||
|
|
||||||
|
return TypeIntersectionScope(message, chainedOrSingle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ fun getKDocLinkMemberScope(descriptor: DeclarationDescriptor, contextScope: Lexi
|
|||||||
is PackageViewDescriptor -> descriptor.memberScope
|
is PackageViewDescriptor -> descriptor.memberScope
|
||||||
|
|
||||||
is ClassDescriptor -> {
|
is ClassDescriptor -> {
|
||||||
ChainedMemberScope(
|
ChainedMemberScope.create(
|
||||||
"Member scope for KDoc resolve", listOfNotNull(
|
"Member scope for KDoc resolve", listOfNotNull(
|
||||||
descriptor.unsubstitutedMemberScope,
|
descriptor.unsubstitutedMemberScope,
|
||||||
descriptor.staticScope,
|
descriptor.staticScope,
|
||||||
|
|||||||
+1
-1
@@ -284,7 +284,7 @@ private class EvaluatorModuleDescriptor(
|
|||||||
MemberScope.Empty
|
MemberScope.Empty
|
||||||
} else {
|
} else {
|
||||||
val scopes = fragments.map { it.getMemberScope() } + SubpackagesScope(module, fqName)
|
val scopes = fragments.map { it.getMemberScope() } + SubpackagesScope(module, fqName)
|
||||||
ChainedMemberScope("package view scope for $fqName in ${module.name}", scopes)
|
ChainedMemberScope.create("package view scope for $fqName in ${module.name}", scopes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user