Don't track lookups for JetScope::getDescriptors (and JetScope::getAllDescriptors)

This commit is contained in:
Zalim Bashorov
2015-08-21 19:25:47 +03:00
parent 6a3200764f
commit 3c2762dd1d
9 changed files with 59 additions and 44 deletions
@@ -38,7 +38,7 @@ import org.jetbrains.kotlin.storage.MemoizedFunctionToNotNull
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.toReadOnlyList
import java.util.LinkedHashSet
import java.util.*
public abstract class AbstractLazyMemberScope<D : DeclarationDescriptor, DP : DeclarationProvider>
protected constructor(
@@ -125,33 +125,36 @@ protected constructor(
protected abstract fun getNonDeclaredProperties(name: Name, result: MutableSet<VariableDescriptor>)
protected fun computeDescriptorsFromDeclaredElements(kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean): List<DeclarationDescriptor> {
protected fun computeDescriptorsFromDeclaredElements(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
location: LookupLocation
): List<DeclarationDescriptor> {
val declarations = declarationProvider.getDeclarations(kindFilter, nameFilter)
val result = LinkedHashSet<DeclarationDescriptor>(declarations.size())
for (declaration in declarations) {
if (declaration is JetClassOrObject) {
val name = declaration.getNameAsSafeName()
val name = declaration.nameAsSafeName
if (nameFilter(name)) {
result.addAll(classDescriptors(name))
}
}
else if (declaration is JetFunction) {
val name = declaration.getNameAsSafeName()
val name = declaration.nameAsSafeName
if (nameFilter(name)) {
result.addAll(getFunctions(name))
result.addAll(getFunctions(name, location))
}
}
else if (declaration is JetProperty) {
val name = declaration.getNameAsSafeName()
val name = declaration.nameAsSafeName
if (nameFilter(name)) {
result.addAll(getProperties(name))
result.addAll(getProperties(name, location))
}
}
else if (declaration is JetParameter) {
val name = declaration.getNameAsSafeName()
val name = declaration.nameAsSafeName
if (nameFilter(name)) {
result.addAll(getProperties(name))
result.addAll(getProperties(name, location))
}
}
else if (declaration is JetScript) {
@@ -51,7 +51,7 @@ public open class LazyClassMemberScope(
) : AbstractLazyMemberScope<LazyClassDescriptor, ClassMemberDeclarationProvider>(c, declarationProvider, thisClass, trace) {
private val descriptorsFromDeclaredElements = storageManager.createLazyValue {
computeDescriptorsFromDeclaredElements(DescriptorKindFilter.ALL, JetScope.ALL_NAME_FILTER)
computeDescriptorsFromDeclaredElements(DescriptorKindFilter.ALL, JetScope.ALL_NAME_FILTER, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS)
}
private val extraDescriptors: NotNullLazyValue<Collection<DeclarationDescriptor>> = storageManager.createLazyValue {
computeExtraDescriptors(NoLookupLocation.FOR_ALREADY_TRACKED)
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.resolve.lazy.descriptors
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.JetDeclaration
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
@@ -30,7 +31,7 @@ public class LazyPackageMemberScope(
: AbstractLazyMemberScope<PackageFragmentDescriptor, PackageMemberDeclarationProvider>(resolveSession, declarationProvider, thisPackage, resolveSession.getTrace()) {
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
return computeDescriptorsFromDeclaredElements(kindFilter, nameFilter)
return computeDescriptorsFromDeclaredElements(kindFilter, nameFilter, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS)
}
override fun getPackage(name: Name): PackageViewDescriptor? = null
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptorKindExclude
import org.jetbrains.kotlin.load.java.lazy.KotlinClassLookupResult
import org.jetbrains.kotlin.load.java.lazy.LazyJavaResolverContext
@@ -33,7 +35,6 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.utils.addIfNotNull
public class LazyJavaPackageScope(
@@ -127,6 +128,6 @@ public class LazyJavaPackageScope(
// we don't use implementation from super which caches all descriptors and does not use filters
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
return computeDescriptors(kindFilter, nameFilter)
return computeDescriptors(kindFilter, nameFilter, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS)
}
}
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.load.java.components.ExternalSignatureResolver
import org.jetbrains.kotlin.load.java.components.TypeUsage
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
@@ -40,15 +42,13 @@ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindExclude.NonExtensions
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.storage.NotNullLazyValue
import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.toReadOnlyList
import java.util.ArrayList
import java.util.LinkedHashSet
import java.util.*
public abstract class LazyJavaScope(
protected val c: LazyJavaResolverContext,
@@ -57,7 +57,7 @@ public abstract class LazyJavaScope(
// this lazy value is not used at all in LazyPackageFragmentScopeForJavaPackage because we do not use caching there
// but is placed in the base class to not duplicate code
private val allDescriptors = c.storageManager.createRecursionTolerantLazyValue<Collection<DeclarationDescriptor>>(
{ computeDescriptors(DescriptorKindFilter.ALL, JetScope.ALL_NAME_FILTER) },
{ computeDescriptors(DescriptorKindFilter.ALL, JetScope.ALL_NAME_FILTER, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS) },
// This is to avoid the following recursive case:
// when computing getAllPackageNames() we ask the JavaPsiFacade for all subpackages of foo
// it, in turn, asks JavaElementFinder for subpackages of Kotlin package foo, which calls getAllPackageNames() recursively
@@ -296,15 +296,18 @@ public abstract class LazyJavaScope(
override fun getDescriptors(kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean) = allDescriptors()
protected fun computeDescriptors(kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean): List<DeclarationDescriptor> {
protected fun computeDescriptors(
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean,
location: LookupLocation
): List<DeclarationDescriptor> {
val result = LinkedHashSet<DeclarationDescriptor>()
if (kindFilter.acceptsKinds(DescriptorKindFilter.CLASSIFIERS_MASK)) {
for (name in getClassNames(kindFilter, nameFilter)) {
if (nameFilter(name)) {
// Null signifies that a class found in Java is not present in Kotlin (e.g. package class)
result.addIfNotNull(getClassifier(name))
result.addIfNotNull(getClassifier(name, location))
}
}
}
@@ -312,7 +315,7 @@ public abstract class LazyJavaScope(
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK) && !kindFilter.excludes.contains(NonExtensions)) {
for (name in getFunctionNames(kindFilter, nameFilter)) {
if (nameFilter(name)) {
result.addAll(getFunctions(name))
result.addAll(getFunctions(name, location))
}
}
}
@@ -320,7 +323,7 @@ public abstract class LazyJavaScope(
if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK) && !kindFilter.excludes.contains(NonExtensions)) {
for (name in getPropertyNames(kindFilter, nameFilter)) {
if (nameFilter(name)) {
result.addAll(getProperties(name))
result.addAll(getProperties(name, location))
}
}
}
@@ -31,5 +31,7 @@ public enum class NoLookupLocation : LookupLocation {
FROM_REFLECTION,
WHEN_RESOLVE_DECLARATION,
WHEN_GET_DECLARATION_SCOPE,
FOR_ALREADY_TRACKED
FOR_ALREADY_TRACKED,
// TODO replace with real location (e.g. FROM_IDE) where it possible
WHEN_GET_ALL_DESCRIPTORS
}
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.AbstractClassDescriptor
import org.jetbrains.kotlin.descriptors.impl.EnumEntrySyntheticClassDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.DescriptorFactory
@@ -177,7 +178,7 @@ public class DeserializedClassDescriptor(
private inner class DeserializedClassMemberScope : DeserializedMemberScope(c, classProto.getMemberList()) {
private val classDescriptor: DeserializedClassDescriptor get() = this@DeserializedClassDescriptor
private val allDescriptors = c.storageManager.createLazyValue {
computeDescriptors(DescriptorKindFilter.ALL, JetScope.ALL_NAME_FILTER)
computeDescriptors(DescriptorKindFilter.ALL, JetScope.ALL_NAME_FILTER, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS)
}
override fun getDescriptors(kindFilter: DescriptorKindFilter,
@@ -216,14 +217,14 @@ public class DeserializedClassDescriptor(
})
}
override fun addNonDeclaredDescriptors(result: MutableCollection<DeclarationDescriptor>) {
for (supertype in classDescriptor.getTypeConstructor().getSupertypes()) {
for (descriptor in supertype.getMemberScope().getAllDescriptors()) {
override fun addNonDeclaredDescriptors(result: MutableCollection<DeclarationDescriptor>, location: LookupLocation) {
for (supertype in classDescriptor.getTypeConstructor().supertypes) {
for (descriptor in supertype.memberScope.getAllDescriptors()) {
if (descriptor is FunctionDescriptor) {
result.addAll(getFunctions(descriptor.getName()))
result.addAll(getFunctions(descriptor.name, location))
}
else if (descriptor is PropertyDescriptor) {
result.addAll(getProperties(descriptor.getName()))
result.addAll(getProperties(descriptor.name, location))
}
// Nothing else is inherited
}
@@ -17,19 +17,17 @@
package org.jetbrains.kotlin.serialization.deserialization.descriptors
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.serialization.Flags
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.toReadOnlyList
import java.util.ArrayList
import java.util.LinkedHashMap
import java.util.LinkedHashSet
import java.util.*
public abstract class DeserializedMemberScope protected constructor(
protected val c: DeserializationContext,
@@ -114,8 +112,11 @@ public abstract class DeserializedMemberScope protected constructor(
override fun getContainingDeclaration() = c.containingDeclaration
protected fun computeDescriptors(kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
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 = LinkedHashSet<DeclarationDescriptor>(0)
@@ -124,9 +125,9 @@ public abstract class DeserializedMemberScope protected constructor(
addEnumEntryDescriptors(result, nameFilter)
}
addFunctionsAndProperties(result, kindFilter, nameFilter)
addFunctionsAndProperties(result, kindFilter, nameFilter, location)
addNonDeclaredDescriptors(result)
addNonDeclaredDescriptors(result, location)
if (kindFilter.acceptsKinds(DescriptorKindFilter.CLASSIFIERS_MASK)) {
addClassDescriptors(result, nameFilter)
@@ -138,7 +139,8 @@ public abstract class DeserializedMemberScope protected constructor(
private fun addFunctionsAndProperties(
result: LinkedHashSet<DeclarationDescriptor>,
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean
nameFilter: (Name) -> Boolean,
location: LookupLocation
) {
val acceptsProperties = kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)
val acceptsFunctions = kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)
@@ -148,10 +150,10 @@ public abstract class DeserializedMemberScope protected constructor(
val keys = membersProtos().keySet().filter { nameFilter(it.name) }
if (acceptsProperties) {
addMembers(result, keys, Kind.PROPERTY) { getProperties(it) }
addMembers(result, keys, Kind.PROPERTY) { getProperties(it, location) }
}
if (acceptsFunctions) {
addMembers(result, keys, Kind.FUNCTION) { getFunctions(it) }
addMembers(result, keys, Kind.FUNCTION) { getFunctions(it, location) }
}
}
@@ -169,7 +171,7 @@ public abstract class DeserializedMemberScope protected constructor(
}
}
protected abstract fun addNonDeclaredDescriptors(result: MutableCollection<DeclarationDescriptor>)
protected abstract fun addNonDeclaredDescriptors(result: MutableCollection<DeclarationDescriptor>, location: LookupLocation)
protected abstract fun addEnumEntryDescriptors(result: MutableCollection<DeclarationDescriptor>, nameFilter: (Name) -> Boolean)
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
@@ -43,7 +45,7 @@ public open class DeserializedPackageMemberScope(
internal val classNames by c.storageManager.createLazyValue { classNames().toSet() }
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= computeDescriptors(kindFilter, nameFilter)
= computeDescriptors(kindFilter, nameFilter, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS)
override fun getClassDescriptor(name: Name): ClassDescriptor? =
if (SpecialNames.isSafeIdentifier(name)) c.components.deserializeClass(ClassId(packageFqName, name))
@@ -57,7 +59,7 @@ public open class DeserializedPackageMemberScope(
}
}
override fun addNonDeclaredDescriptors(result: MutableCollection<DeclarationDescriptor>) {
override fun addNonDeclaredDescriptors(result: MutableCollection<DeclarationDescriptor>, location: LookupLocation) {
// Do nothing
}