Introduce MemberScope::classifierNames and some implementations

It aims to help with further optimizations
This commit is contained in:
Denis Zharkov
2017-08-15 17:50:37 +07:00
parent 1b255b4ed0
commit cd3edfc5b2
17 changed files with 59 additions and 2 deletions
@@ -58,6 +58,7 @@ class LazyPackageMemberScope(
c.lookupTracker.record(from, thisDescriptor, name)
}
override fun getClassifierNames(): Set<Name>? = declarationProvider.getDeclarationNames()
override fun getFunctionNames() = declarationProvider.getDeclarationNames()
override fun getVariableNames() = declarationProvider.getDeclarationNames()
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.load.java.structure.JavaPackage
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.flatMapClassifierNamesOrNull
import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.util.collectionUtils.getFirstClassifierDiscriminateHeaders
import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes
@@ -76,6 +77,10 @@ class JvmPackageScope(
addAll(javaScope.getVariableNames())
}
override fun getClassifierNames(): Set<Name>? = kotlinScopes.flatMapClassifierNamesOrNull()?.apply {
addAll(javaScope.getClassifierNames())
}
override fun printScopeStructure(p: Printer) {
p.println(this::class.java.simpleName, " {")
p.pushIndent()
@@ -212,9 +212,11 @@ abstract class LazyJavaScope(protected val c: LazyJavaResolverContext) : MemberS
private val functionNamesLazy by c.storageManager.createLazyValue { computeFunctionNames(DescriptorKindFilter.FUNCTIONS, null) }
private val propertyNamesLazy by c.storageManager.createLazyValue { computePropertyNames(DescriptorKindFilter.VARIABLES, null) }
private val classNamesLazy by c.storageManager.createLazyValue { computeClassNames(DescriptorKindFilter.CLASSIFIERS, null) }
override fun getFunctionNames() = functionNamesLazy
override fun getVariableNames() = propertyNamesLazy
override fun getClassifierNames() = classNamesLazy
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
if (name !in getFunctionNames()) return emptyList()
@@ -310,6 +310,12 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
return enumMemberNames.invoke();
}
@NotNull
@Override
public Set<Name> getClassifierNames() {
return Collections.emptySet();
}
@NotNull
@Override
public Set<Name> getVariableNames() {
@@ -57,6 +57,8 @@ open class SubpackagesScope(private val moduleDescriptor: ModuleDescriptor, priv
return result
}
override fun getClassifierNames(): Set<Name> = emptySet()
override fun printScopeStructure(p: Printer) {
p.println(this::class.java.simpleName, " {")
p.pushIndent()
@@ -55,6 +55,8 @@ abstract class AbstractScopeAdapter : MemberScope {
override fun getFunctionNames() = workerScope.getFunctionNames()
override fun getVariableNames() = workerScope.getVariableNames()
override fun getClassifierNames() = workerScope.getClassifierNames()
override fun printScopeStructure(p: Printer) {
p.println(this::class.java.simpleName, " {")
@@ -43,6 +43,7 @@ class ChainedMemberScope(
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 toString() = debugName
@@ -40,6 +40,10 @@ class InnerClassesScopeWrapper(val workerScope: MemberScope) : MemberScopeImpl()
workerScope.printScopeStructure(p)
}
override fun getFunctionNames() = workerScope.getFunctionNames()
override fun getVariableNames() = workerScope.getVariableNames()
override fun getClassifierNames() = workerScope.getClassifierNames()
override fun definitelyDoesNotContainName(name: Name) = workerScope.definitelyDoesNotContainName(name)
override fun toString() = "Classes from $workerScope"
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.addToStdlib.flatMapToNullable
import java.lang.reflect.Modifier
interface MemberScope : ResolutionScope {
@@ -32,6 +33,7 @@ interface MemberScope : ResolutionScope {
*/
fun getFunctionNames(): Set<Name>
fun getVariableNames(): Set<Name>
fun getClassifierNames(): Set<Name>?
/**
* Is supposed to be used in tests and debug only
@@ -45,6 +47,7 @@ interface MemberScope : ResolutionScope {
override fun getFunctionNames() = emptySet<Name>()
override fun getVariableNames() = emptySet<Name>()
override fun getClassifierNames() = emptySet<Name>()
}
companion object {
@@ -52,6 +55,11 @@ interface MemberScope : ResolutionScope {
}
}
fun MemberScope.computeAllNames() = getClassifierNames()?.let { getFunctionNames() + getVariableNames() + it }
fun Collection<MemberScope>.flatMapClassifierNamesOrNull(): MutableSet<Name>? =
flatMapToNullable(hashSetOf(), MemberScope::getClassifierNames)
/**
* The same as getDescriptors(kindFilter, nameFilter) but the result is guaranteed to be filtered by kind and name.
*/
@@ -42,6 +42,8 @@ abstract class MemberScopeImpl : MemberScope {
DescriptorKindFilter.VARIABLES, alwaysTrue()
).filterIsInstance<VariableDescriptor>().mapTo(mutableSetOf()) { it.name }
override fun getClassifierNames(): Set<Name>? = null
// This method should not be implemented here by default: every scope class has its unique structure pattern
abstract override fun printScopeStructure(p: Printer)
}
@@ -81,6 +81,7 @@ class SubstitutingScope(private val workerScope: MemberScope, givenSubstitutor:
override fun getFunctionNames() = workerScope.getFunctionNames()
override fun getVariableNames() = workerScope.getVariableNames()
override fun getClassifierNames() = workerScope.getClassifierNames()
override fun definitelyDoesNotContainName(name: Name) = workerScope.definitelyDoesNotContainName(name)
@@ -211,6 +211,12 @@ public class ErrorUtils {
return emptySet();
}
@NotNull
@Override
public Set<Name> getClassifierNames() {
return emptySet();
}
@NotNull
@Override
public Collection<DeclarationDescriptor> getContributedDescriptors(
@@ -284,6 +290,11 @@ public class ErrorUtils {
throw new IllegalStateException();
}
@Override
public Set<Name> getClassifierNames() {
throw new IllegalStateException();
}
@Override
public boolean definitelyDoesNotContainName(@NotNull Name name) {
return false;
@@ -107,6 +107,7 @@ class MetadataPackageFragment(
) {
override fun hasClass(name: Name): Boolean = hasTopLevelClass(name)
override fun definitelyDoesNotContainName(name: Name) = false
override fun getClassifierNames(): Set<Name>? = null
})
return ChainedMemberScope.create("Metadata scope", scopes)
@@ -76,6 +76,7 @@ abstract class DeserializedMemberScope protected constructor(
override fun getFunctionNames() = functionNamesLazy
override fun getVariableNames() = variableNamesLazy
override fun getClassifierNames(): Set<Name>? = classNames + typeAliasNames
private inline fun <M : MessageLite> Collection<M>.groupByName(
getNameIndex: (M) -> Int
@@ -138,3 +138,11 @@ fun <T, C : MutableCollection<in T>> Iterable<Iterable<T>>.flattenTo(c: C): C {
}
return c
}
inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.flatMapToNullable(destination: C, transform: (T) -> Iterable<R>?): C? {
for (element in this) {
val list = transform(element) ?: return null
destination.addAll(list)
}
return destination
}
@@ -253,6 +253,8 @@ private class ExtensionsScope(
.toSet()
}
override fun getClassifierNames() = null
override fun printScopeStructure(p: Printer) {
p.println("Extensions for ${receiverClass.name} in:")
contextScope.printStructure(p)
@@ -75,8 +75,8 @@ private class GlobalSyntheticPackageViewDescriptor(override val fqName: FqName,
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> = shouldNotBeCalled()
override fun getFunctionNames(): Set<Name> = shouldNotBeCalled()
override fun getVariableNames(): Set<Name> = shouldNotBeCalled()
override fun getClassifierNames(): Set<Name> = shouldNotBeCalled()
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = shouldNotBeCalled()
@@ -130,4 +130,4 @@ private class GlobalSyntheticPackageViewDescriptor(override val fqName: FqName,
override fun acceptVoid(visitor: DeclarationDescriptorVisitor<Void, Void>?) = shouldNotBeCalled()
override val annotations = Annotations.EMPTY
}
}