Get rid of DeserializedMemberScope.ProtoKey class
Only 'name' property matters, 'isExtension' is only needed for sorting when computing all descriptors (can be replaced with additional sort)
This commit is contained in:
+36
-33
@@ -16,15 +16,18 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.serialization.deserialization.descriptors
|
package org.jetbrains.kotlin.serialization.deserialization.descriptors
|
||||||
|
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
|
||||||
import org.jetbrains.kotlin.incremental.components.LookupLocation
|
import org.jetbrains.kotlin.incremental.components.LookupLocation
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.protobuf.MessageLite
|
import org.jetbrains.kotlin.protobuf.MessageLite
|
||||||
|
import org.jetbrains.kotlin.resolve.MemberComparator
|
||||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||||
import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl
|
import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl
|
||||||
import org.jetbrains.kotlin.serialization.ProtoBuf
|
import org.jetbrains.kotlin.serialization.ProtoBuf
|
||||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext
|
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext
|
||||||
import org.jetbrains.kotlin.serialization.deserialization.receiverType
|
|
||||||
import org.jetbrains.kotlin.utils.Printer
|
import org.jetbrains.kotlin.utils.Printer
|
||||||
import org.jetbrains.kotlin.utils.toReadOnlyList
|
import org.jetbrains.kotlin.utils.toReadOnlyList
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -36,19 +39,17 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
typeAliasList: Collection<ProtoBuf.TypeAlias>
|
typeAliasList: Collection<ProtoBuf.TypeAlias>
|
||||||
) : MemberScopeImpl() {
|
) : MemberScopeImpl() {
|
||||||
|
|
||||||
private data class ProtoKey(val name: Name, val isExtension: Boolean)
|
|
||||||
|
|
||||||
private val functionProtos =
|
private val functionProtos =
|
||||||
c.storageManager.createLazyValue {
|
c.storageManager.createLazyValue {
|
||||||
groupByKey(functionList, { it.name }) { it.receiverType(c.typeTable) != null }
|
functionList.groupByName { it.name }
|
||||||
}
|
}
|
||||||
private val propertyProtos =
|
private val propertyProtos =
|
||||||
c.storageManager.createLazyValue {
|
c.storageManager.createLazyValue {
|
||||||
groupByKey(propertyList, { it.name }) { it.receiverType(c.typeTable) != null }
|
propertyList.groupByName { it.name }
|
||||||
}
|
}
|
||||||
private val typeAliasProtos =
|
private val typeAliasProtos =
|
||||||
c.storageManager.createLazyValue {
|
c.storageManager.createLazyValue {
|
||||||
typeAliasList.groupBy { c.nameResolver.getName(it.name) }
|
typeAliasList.groupByName { it.name }
|
||||||
}
|
}
|
||||||
protected val typeAliasNames =
|
protected val typeAliasNames =
|
||||||
c.storageManager.createLazyValue {
|
c.storageManager.createLazyValue {
|
||||||
@@ -62,20 +63,12 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
private val typeAliases =
|
private val typeAliases =
|
||||||
c.storageManager.createMemoizedFunction<Name, Collection<TypeAliasDescriptor>> { computeTypeAliases(it) }
|
c.storageManager.createMemoizedFunction<Name, Collection<TypeAliasDescriptor>> { computeTypeAliases(it) }
|
||||||
|
|
||||||
private fun <M : MessageLite> groupByKey(
|
private inline fun <M : MessageLite> Collection<M>.groupByName(
|
||||||
protos: Collection<M>, getNameIndex: (M) -> Int, isExtension: (M) -> Boolean
|
getNameIndex: (M) -> Int
|
||||||
): Map<ProtoKey, List<M>> {
|
) = groupBy { c.nameResolver.getName(getNameIndex(it)) }
|
||||||
val map = LinkedHashMap<ProtoKey, MutableList<M>>()
|
|
||||||
for (proto in protos) {
|
|
||||||
val key = ProtoKey(c.nameResolver.getName(getNameIndex(proto)), isExtension(proto))
|
|
||||||
map.getOrPut(key) { ArrayList(1) }.add(proto)
|
|
||||||
}
|
|
||||||
return map
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun computeFunctions(name: Name): Collection<SimpleFunctionDescriptor> {
|
private fun computeFunctions(name: Name): Collection<SimpleFunctionDescriptor> {
|
||||||
val protos = functionProtos()[ProtoKey(name, isExtension = false)].orEmpty() +
|
val protos = functionProtos()[name].orEmpty()
|
||||||
functionProtos()[ProtoKey(name, isExtension = true)].orEmpty()
|
|
||||||
|
|
||||||
val descriptors = protos.mapTo(linkedSetOf()) {
|
val descriptors = protos.mapTo(linkedSetOf()) {
|
||||||
c.memberDeserializer.loadFunction(it)
|
c.memberDeserializer.loadFunction(it)
|
||||||
@@ -91,8 +84,7 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> = functions(name)
|
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> = functions(name)
|
||||||
|
|
||||||
private fun computeProperties(name: Name): Collection<PropertyDescriptor> {
|
private fun computeProperties(name: Name): Collection<PropertyDescriptor> {
|
||||||
val protos = propertyProtos()[ProtoKey(name, isExtension = false)].orEmpty() +
|
val protos = propertyProtos()[name].orEmpty()
|
||||||
propertyProtos()[ProtoKey(name, isExtension = true)].orEmpty()
|
|
||||||
|
|
||||||
val descriptors = protos.mapTo(linkedSetOf()) {
|
val descriptors = protos.mapTo(linkedSetOf()) {
|
||||||
c.memberDeserializer.loadProperty(it)
|
c.memberDeserializer.loadProperty(it)
|
||||||
@@ -148,32 +140,43 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun addFunctionsAndProperties(
|
private fun addFunctionsAndProperties(
|
||||||
result: LinkedHashSet<DeclarationDescriptor>,
|
result: MutableCollection<DeclarationDescriptor>,
|
||||||
kindFilter: DescriptorKindFilter,
|
kindFilter: DescriptorKindFilter,
|
||||||
nameFilter: (Name) -> Boolean,
|
nameFilter: (Name) -> Boolean,
|
||||||
location: LookupLocation
|
location: LookupLocation
|
||||||
) {
|
) {
|
||||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) {
|
if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) {
|
||||||
val keys = propertyProtos().keys.filter { nameFilter(it.name) }
|
addMembers(
|
||||||
addMembers(result, keys) { getContributedVariables(it, location) }
|
propertyProtos().keys,
|
||||||
|
nameFilter,
|
||||||
|
result
|
||||||
|
) { getContributedVariables(it, location) }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) {
|
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) {
|
||||||
val keys = functionProtos().keys.filter { nameFilter(it.name) }
|
addMembers(
|
||||||
addMembers(result, keys) { getContributedFunctions(it, location) }
|
functionProtos().keys,
|
||||||
|
nameFilter,
|
||||||
|
result
|
||||||
|
) { getContributedFunctions(it, location) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addMembers(
|
private inline fun addMembers(
|
||||||
|
names: Collection<Name>,
|
||||||
|
nameFilter: (Name) -> Boolean,
|
||||||
result: MutableCollection<DeclarationDescriptor>,
|
result: MutableCollection<DeclarationDescriptor>,
|
||||||
keys: Collection<ProtoKey>,
|
descriptorsByName: (Name) -> Collection<DeclarationDescriptor>
|
||||||
getMembers: (Name) -> Collection<CallableDescriptor>
|
|
||||||
) {
|
) {
|
||||||
listOf(false, true).forEach { isExtension ->
|
val subResult = ArrayList<DeclarationDescriptor>()
|
||||||
keys.filter { it.isExtension == isExtension }
|
for (name in names) {
|
||||||
.flatMap { getMembers(it.name) }
|
if (nameFilter(name)) {
|
||||||
.filterTo(result) { (it.extensionReceiverParameter != null) == isExtension }
|
subResult.addAll(descriptorsByName(name))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
subResult.sortWith(MemberComparator.INSTANCE)
|
||||||
|
result.addAll(subResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun addNonDeclaredDescriptors(result: MutableCollection<DeclarationDescriptor>, location: LookupLocation) {
|
protected fun addNonDeclaredDescriptors(result: MutableCollection<DeclarationDescriptor>, location: LookupLocation) {
|
||||||
|
|||||||
Reference in New Issue
Block a user