KT-41346 Refactor computeNonDeclared*

Change the signature of the `computeNonDeclared*` methods so they expect
mutable lists - it better suits their contracts. Also, add a little
documentations about the contract

Also, change `Collection` to `List` for incoming protos to `emphasize`
that their order might be used
This commit is contained in:
Roman Golyshev
2020-09-23 14:07:06 +03:00
parent b1097c49d3
commit bf371ff98a
2 changed files with 25 additions and 13 deletions

View File

@@ -250,7 +250,7 @@ class DeserializedClassDescriptor(
return c.components.platformDependentDeclarationFilter.isFunctionAvailable(this@DeserializedClassDescriptor, function)
}
override fun computeNonDeclaredFunctions(name: Name, functions: MutableCollection<SimpleFunctionDescriptor>) {
override fun computeNonDeclaredFunctions(name: Name, functions: MutableList<SimpleFunctionDescriptor>) {
val fromSupertypes = ArrayList<SimpleFunctionDescriptor>()
for (supertype in refinedSupertypes()) {
fromSupertypes.addAll(supertype.memberScope.getContributedFunctions(name, NoLookupLocation.FOR_ALREADY_TRACKED))
@@ -260,7 +260,7 @@ class DeserializedClassDescriptor(
generateFakeOverrides(name, fromSupertypes, functions)
}
override fun computeNonDeclaredProperties(name: Name, descriptors: MutableCollection<PropertyDescriptor>) {
override fun computeNonDeclaredProperties(name: Name, descriptors: MutableList<PropertyDescriptor>) {
val fromSupertypes = ArrayList<PropertyDescriptor>()
for (supertype in refinedSupertypes()) {
fromSupertypes.addAll(supertype.memberScope.getContributedVariables(name, NoLookupLocation.FOR_ALREADY_TRACKED))
@@ -271,7 +271,7 @@ class DeserializedClassDescriptor(
private fun <D : CallableMemberDescriptor> generateFakeOverrides(
name: Name,
fromSupertypes: Collection<D>,
result: MutableCollection<D>
result: MutableList<D>
) {
val fromCurrent = ArrayList<CallableMemberDescriptor>(result)
c.components.kotlinTypeChecker.overridingUtil.generateOverridesInFunctionGroup(

View File

@@ -39,9 +39,9 @@ import kotlin.collections.ArrayList
abstract class DeserializedMemberScope protected constructor(
protected val c: DeserializationContext,
functionList: Collection<ProtoBuf.Function>,
propertyList: Collection<ProtoBuf.Property>,
typeAliasList: Collection<ProtoBuf.TypeAlias>,
functionList: List<ProtoBuf.Function>,
propertyList: List<ProtoBuf.Property>,
typeAliasList: List<ProtoBuf.TypeAlias>,
classNames: () -> Collection<Name>
) : MemberScopeImpl() {
@@ -68,14 +68,26 @@ abstract class DeserializedMemberScope protected constructor(
*/
protected open fun isDeclaredFunctionAvailable(function: SimpleFunctionDescriptor): Boolean = true
protected open fun computeNonDeclaredFunctions(name: Name, functions: MutableCollection<SimpleFunctionDescriptor>) {
/**
* This function has the next contract:
*
* * It can only add to the end of the [functions] list and shall not modify it otherwise (e.g. remove from it).
* * Before the call, [functions] should already contain all declared functions with the [name] name.
*/
protected open fun computeNonDeclaredFunctions(name: Name, functions: MutableList<SimpleFunctionDescriptor>) {
}
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
return impl.getContributedFunctions(name, location)
}
protected open fun computeNonDeclaredProperties(name: Name, descriptors: MutableCollection<PropertyDescriptor>) {
/**
* This function has the next contract:
*
* * It can only add to the end of the [descriptors] list and shall not modify it otherwise (e.g. remove from it).
* * Before the call, [descriptors] should already contain all declared properties with the [name] name.
*/
protected open fun computeNonDeclaredProperties(name: Name, descriptors: MutableList<PropertyDescriptor>) {
}
private fun getTypeAliasByName(name: Name): TypeAliasDescriptor? {
@@ -169,9 +181,9 @@ abstract class DeserializedMemberScope protected constructor(
}
private inner class OptimizedImplementation(
functionList: Collection<ProtoBuf.Function>,
propertyList: Collection<ProtoBuf.Property>,
typeAliasList: Collection<ProtoBuf.TypeAlias>
functionList: List<ProtoBuf.Function>,
propertyList: List<ProtoBuf.Property>,
typeAliasList: List<ProtoBuf.TypeAlias>
) : Implementation {
private val functionProtosBytes = functionList.groupByName { it.name }.packToByteArray()
@@ -225,7 +237,7 @@ abstract class DeserializedMemberScope protected constructor(
bytesByName: Map<Name, ByteArray>,
parser: Parser<M>,
factory: (M) -> D?,
computeNonDeclared: (MutableCollection<D>) -> Unit
computeNonDeclared: (MutableList<D>) -> Unit
): Collection<D> =
computeDescriptors(
bytesByName[name]?.let {
@@ -241,7 +253,7 @@ abstract class DeserializedMemberScope protected constructor(
private inline fun <M : MessageLite, D : DeclarationDescriptor> computeDescriptors(
protos: Collection<M>,
factory: (M) -> D?,
computeNonDeclared: (MutableCollection<D>) -> Unit
computeNonDeclared: (MutableList<D>) -> Unit
): Collection<D> {
val descriptors = protos.mapNotNullTo(ArrayList(protos.size), factory)