KT-41346 Move related code to OptimizedImplementation

This does not change any semantics, only moves the code around
This commit is contained in:
Roman Golyshev
2020-09-17 18:01:34 +03:00
parent c8878f862d
commit 5596bf33d8
@@ -62,8 +62,117 @@ abstract class DeserializedMemberScope protected constructor(
classNames: () -> Collection<Name> classNames: () -> Collection<Name>
) : MemberScopeImpl() { ) : MemberScopeImpl() {
private val impl: Implementation = OptimizedImplementation() private val impl: Implementation = OptimizedImplementation(functionList, propertyList, typeAliasList)
internal val classNames by c.storageManager.createLazyValue { classNames().toSet() }
private val classifierNamesLazy by c.storageManager.createNullableLazyValue {
val nonDeclaredNames = getNonDeclaredClassifierNames() ?: return@createNullableLazyValue null
this.classNames + impl.typeAliasNames + nonDeclaredNames
}
override fun getFunctionNames() = impl.functionNames
override fun getVariableNames() = impl.variableNames
override fun getClassifierNames(): Set<Name>? = classifierNamesLazy
override fun definitelyDoesNotContainName(name: Name): Boolean {
return name !in impl.functionNames && name !in impl.variableNames && name !in classNames && name !in helper.typeAliasNames
}
/**
* Can be overridden to filter specific declared functions. Not called on non-declared functions.
*/
protected open fun isDeclaredFunctionAvailable(function: SimpleFunctionDescriptor): Boolean = true
protected open fun computeNonDeclaredFunctions(name: Name, functions: MutableCollection<SimpleFunctionDescriptor>) {
}
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
return impl.getContributedFunctions(name, location)
}
protected open fun computeNonDeclaredProperties(name: Name, descriptors: MutableCollection<PropertyDescriptor>) {
}
private fun getTypeAliasByName(name: Name): TypeAliasDescriptor? {
return impl.getTypeAliasByName(name)
}
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
return impl.getContributedVariables(name, location)
}
protected fun computeDescriptors(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
location: LookupLocation
): Collection<DeclarationDescriptor> {
//NOTE: descriptors should be in the same order they were serialized in
// see MemberComparator
val result = ArrayList<DeclarationDescriptor>(0)
if (kindFilter.acceptsKinds(DescriptorKindFilter.SINGLETON_CLASSIFIERS_MASK)) {
addEnumEntryDescriptors(result, nameFilter)
}
impl.addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location)
if (kindFilter.acceptsKinds(DescriptorKindFilter.CLASSIFIERS_MASK)) {
for (className in classNames) {
if (nameFilter(className)) {
result.addIfNotNull(deserializeClass(className))
}
}
}
if (kindFilter.acceptsKinds(DescriptorKindFilter.TYPE_ALIASES_MASK)) {
for (typeAliasName in impl.typeAliasNames) {
if (nameFilter(typeAliasName)) {
result.addIfNotNull(impl.getTypeAliasByName(typeAliasName))
}
}
}
return result.compact()
}
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? =
when {
hasClass(name) -> deserializeClass(name)
name in impl.typeAliasNames -> getTypeAliasByName(name)
else -> null
}
private fun deserializeClass(name: Name): ClassDescriptor? =
c.components.deserializeClass(createClassId(name))
protected open fun hasClass(name: Name): Boolean =
name in classNames
protected abstract fun createClassId(name: Name): ClassId
protected abstract fun getNonDeclaredFunctionNames(): Set<Name>
protected abstract fun getNonDeclaredVariableNames(): Set<Name>
protected abstract fun getNonDeclaredClassifierNames(): Set<Name>?
protected abstract fun addEnumEntryDescriptors(result: MutableCollection<DeclarationDescriptor>, nameFilter: (Name) -> Boolean)
override fun printScopeStructure(p: Printer) {
p.println(this::class.java.simpleName, " {")
p.pushIndent()
p.println("containingDeclaration = " + c.containingDeclaration)
p.popIndent()
p.println("}")
}
private inner class OptimizedImplementation(
functionList: Collection<ProtoBuf.Function>,
propertyList: Collection<ProtoBuf.Property>,
typeAliasList: Collection<ProtoBuf.TypeAlias>
) : Implementation {
private val functionProtosBytes = functionList.groupByName { it.name }.packToByteArray() private val functionProtosBytes = functionList.groupByName { it.name }.packToByteArray()
private val propertyProtosBytes = propertyList.groupByName { it.name }.packToByteArray() private val propertyProtosBytes = propertyList.groupByName { it.name }.packToByteArray()
@@ -88,22 +197,16 @@ abstract class DeserializedMemberScope protected constructor(
private val typeAliasByName = private val typeAliasByName =
c.storageManager.createMemoizedFunctionWithNullableValues<Name, TypeAliasDescriptor> { createTypeAlias(it) } c.storageManager.createMemoizedFunctionWithNullableValues<Name, TypeAliasDescriptor> { createTypeAlias(it) }
internal val classNames by c.storageManager.createLazyValue { classNames().toSet() } override val functionNames by c.storageManager.createLazyValue {
functionProtosBytes.keys + getNonDeclaredFunctionNames()
private val classifierNamesLazy by c.storageManager.createNullableLazyValue {
val nonDeclaredNames = getNonDeclaredClassifierNames() ?: return@createNullableLazyValue null
this.classNames + impl.typeAliasNames + nonDeclaredNames
} }
override fun getFunctionNames() = impl.functionNames override val variableNames by c.storageManager.createLazyValue {
override fun getVariableNames() = impl.variableNames propertyProtosBytes.keys + getNonDeclaredVariableNames()
override fun getClassifierNames(): Set<Name>? = classifierNamesLazy
override fun definitelyDoesNotContainName(name: Name): Boolean {
return name !in impl.functionNames && name !in impl.variableNames && name !in classNames && name !in impl.typeAliasNames
} }
override val typeAliasNames: Set<Name> get() = typeAliasBytes.keys
private inline fun <M : MessageLite> Collection<M>.groupByName( private inline fun <M : MessageLite> Collection<M>.groupByName(
getNameIndex: (M) -> Int getNameIndex: (M) -> Int
) = groupBy { c.nameResolver.getName(getNameIndex(it)) } ) = groupBy { c.nameResolver.getName(getNameIndex(it)) }
@@ -146,18 +249,6 @@ abstract class DeserializedMemberScope protected constructor(
return descriptors.compact() return descriptors.compact()
} }
/**
* Can be overridden to filter specific declared functions. Not called on non-declared functions.
*/
protected open fun isDeclaredFunctionAvailable(function: SimpleFunctionDescriptor): Boolean = true
protected open fun computeNonDeclaredFunctions(name: Name, functions: MutableCollection<SimpleFunctionDescriptor>) {
}
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
return impl.getContributedFunctions(name, location)
}
private fun computeProperties(name: Name) = private fun computeProperties(name: Name) =
computeDescriptors( computeDescriptors(
name, name,
@@ -167,9 +258,6 @@ abstract class DeserializedMemberScope protected constructor(
{ computeNonDeclaredProperties(name, it) } { computeNonDeclaredProperties(name, it) }
) )
protected open fun computeNonDeclaredProperties(name: Name, descriptors: MutableCollection<PropertyDescriptor>) {
}
private fun createTypeAlias(name: Name): TypeAliasDescriptor? { private fun createTypeAlias(name: Name): TypeAliasDescriptor? {
val byteArray = typeAliasBytes[name] ?: return null val byteArray = typeAliasBytes[name] ?: return null
val proto = val proto =
@@ -179,55 +267,41 @@ abstract class DeserializedMemberScope protected constructor(
return c.memberDeserializer.loadTypeAlias(proto) return c.memberDeserializer.loadTypeAlias(proto)
} }
private fun getTypeAliasByName(name: Name): TypeAliasDescriptor? { override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
return impl.getTypeAliasByName(name) if (name !in functionNames) return emptyList()
return functions(name)
}
override fun getTypeAliasByName(name: Name): TypeAliasDescriptor? {
return typeAliasByName(name)
} }
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> { override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
return impl.getContributedVariables(name, location) if (name !in variableNames) return emptyList()
return properties(name)
} }
protected fun computeDescriptors( override fun addFunctionsAndPropertiesTo(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
location: LookupLocation
): Collection<DeclarationDescriptor> {
//NOTE: descriptors should be in the same order they were serialized in
// see MemberComparator
val result = ArrayList<DeclarationDescriptor>(0)
if (kindFilter.acceptsKinds(DescriptorKindFilter.SINGLETON_CLASSIFIERS_MASK)) {
addEnumEntryDescriptors(result, nameFilter)
}
addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location)
if (kindFilter.acceptsKinds(DescriptorKindFilter.CLASSIFIERS_MASK)) {
for (className in classNames) {
if (nameFilter(className)) {
result.addIfNotNull(deserializeClass(className))
}
}
}
if (kindFilter.acceptsKinds(DescriptorKindFilter.TYPE_ALIASES_MASK)) {
for (typeAliasName in impl.typeAliasNames) {
if (nameFilter(typeAliasName)) {
result.addIfNotNull(typeAliasByName(typeAliasName))
}
}
}
return result.compact()
}
private fun addFunctionsAndPropertiesTo(
result: MutableCollection<DeclarationDescriptor>, result: MutableCollection<DeclarationDescriptor>,
kindFilter: DescriptorKindFilter, kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean, nameFilter: (Name) -> Boolean,
location: LookupLocation location: LookupLocation
) { ) {
impl.addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location) if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) {
addMembers(
variableNames,
nameFilter,
result
) { getContributedVariables(it, location) }
}
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) {
addMembers(
functionNames,
nameFilter,
result
) { getContributedFunctions(it, location) }
}
} }
private inline fun addMembers( private inline fun addMembers(
@@ -246,84 +320,5 @@ abstract class DeserializedMemberScope protected constructor(
subResult.sortWith(MemberComparator.NameAndTypeMemberComparator.INSTANCE) subResult.sortWith(MemberComparator.NameAndTypeMemberComparator.INSTANCE)
result.addAll(subResult) result.addAll(subResult)
} }
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? =
when {
hasClass(name) -> deserializeClass(name)
name in impl.typeAliasNames -> getTypeAliasByName(name)
else -> null
}
private fun deserializeClass(name: Name): ClassDescriptor? =
c.components.deserializeClass(createClassId(name))
protected open fun hasClass(name: Name): Boolean =
name in classNames
protected abstract fun createClassId(name: Name): ClassId
protected abstract fun getNonDeclaredFunctionNames(): Set<Name>
protected abstract fun getNonDeclaredVariableNames(): Set<Name>
protected abstract fun getNonDeclaredClassifierNames(): Set<Name>?
protected abstract fun addEnumEntryDescriptors(result: MutableCollection<DeclarationDescriptor>, nameFilter: (Name) -> Boolean)
override fun printScopeStructure(p: Printer) {
p.println(this::class.java.simpleName, " {")
p.pushIndent()
p.println("containingDeclaration = " + c.containingDeclaration)
p.popIndent()
p.println("}")
}
inner class OptimizedImplementation : Implementation {
override val functionNames by c.storageManager.createLazyValue {
functionProtosBytes.keys + getNonDeclaredFunctionNames()
}
override val variableNames by c.storageManager.createLazyValue {
propertyProtosBytes.keys + getNonDeclaredVariableNames()
}
override val typeAliasNames: Set<Name> get() = typeAliasBytes.keys
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
if (name !in getFunctionNames()) return emptyList()
return functions(name)
}
override fun getTypeAliasByName(name: Name): TypeAliasDescriptor? {
return typeAliasByName(name)
}
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
if (name !in getVariableNames()) return emptyList()
return properties(name)
}
override fun addFunctionsAndPropertiesTo(
result: MutableCollection<DeclarationDescriptor>,
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
location: LookupLocation
) {
if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) {
addMembers(
getVariableNames(),
nameFilter,
result
) { getContributedVariables(it, location) }
}
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) {
addMembers(
getFunctionNames(),
nameFilter,
result
) { getContributedFunctions(it, location) }
}
}
} }
} }