Optimize scopes handling inside ChainedMemberScope
This commit is contained in:
+1
-1
@@ -198,7 +198,7 @@ abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdir() {
|
||||
val list = ArrayList<MemberScope>(packageScopes.size + 1)
|
||||
list.add(ScopeWithClassifiers(classes))
|
||||
list.addAll(packageScopes)
|
||||
scope = ChainedMemberScope("synthetic package view for test", list)
|
||||
scope = ChainedMemberScope.create("synthetic package view for test", list)
|
||||
}
|
||||
|
||||
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.getFromAllScopes
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
import org.jetbrains.kotlin.utils.SmartList
|
||||
|
||||
class ChainedMemberScope(
|
||||
internal val debugName: String,
|
||||
private val scopes: List<MemberScope>
|
||||
class ChainedMemberScope private constructor(
|
||||
private val debugName: String,
|
||||
private val scopes: Array<out 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>
|
||||
= getFromAllScopes(scopes) { it.getContributedVariables(name, location) }
|
||||
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? =
|
||||
getFirstClassifierDiscriminateHeaders(scopes) { it.getContributedClassifier(name, location) }
|
||||
|
||||
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor>
|
||||
= getFromAllScopes(scopes) { it.getContributedFunctions(name, location) }
|
||||
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> =
|
||||
getFromAllScopes(scopes) { it.getContributedVariables(name, location) }
|
||||
|
||||
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
|
||||
= getFromAllScopes(scopes) { it.getContributedDescriptors(kindFilter, nameFilter) }
|
||||
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> =
|
||||
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 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) {
|
||||
scopes.forEach { it.recordLookup(name, location) }
|
||||
@@ -64,12 +66,25 @@ class ChainedMemberScope(
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun create(debugName: String, scopes: List<MemberScope>): MemberScope {
|
||||
return when (scopes.size) {
|
||||
0 -> MemberScope.Empty
|
||||
1 -> scopes.single()
|
||||
else -> ChainedMemberScope(debugName, scopes)
|
||||
fun create(debugName: String, vararg scopes: MemberScope): MemberScope = create(debugName, scopes.asIterable())
|
||||
|
||||
fun create(debugName: String, scopes: Iterable<MemberScope>): MemberScope {
|
||||
val flattenedNonEmptyScopes = SmartList<MemberScope>()
|
||||
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.resolve.selectMostSpecificInEachOverridableGroup
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.util.collectionUtils.listOfNonEmptyScopes
|
||||
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) =
|
||||
super.getContributedFunctions(name, location).selectMostSpecificInEachOverridableGroup { this }
|
||||
|
||||
@@ -39,17 +40,19 @@ class TypeIntersectionScope private constructor(override val workerScope: Chaine
|
||||
}
|
||||
|
||||
override fun printScopeStructure(p: Printer) {
|
||||
p.print("TypeIntersectionScope for: " + workerScope.debugName)
|
||||
p.print("TypeIntersectionScope for: " + debugName)
|
||||
super.printScopeStructure(p)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun create(message: String, types: Collection<KotlinType>): MemberScope {
|
||||
val chainedScope = ChainedMemberScope(message, types.map { it.memberScope })
|
||||
if (types.size <= 1) return chainedScope
|
||||
val nonEmptyScopes = listOfNonEmptyScopes(types.map { it.memberScope })
|
||||
val chainedOrSingle = ChainedMemberScope.createOrSingle(message, nonEmptyScopes)
|
||||
|
||||
return TypeIntersectionScope(chainedScope)
|
||||
if (nonEmptyScopes.size <= 1) return chainedOrSingle
|
||||
|
||||
return TypeIntersectionScope(message, chainedOrSingle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user