Make ModuleDescriptor#getPackage() return not null lazy object with lazy scope
Refactor: no need to create package view in order to obtain its subpackages LazyPackageViewDescriptorImpl to replace PackageViewDescriptorImpl This allows to avoid computations when package views are requested but their contents not necessarily queried For example: DescriptorResolver.resolvePackageHeader()
This commit is contained in:
@@ -31,13 +31,13 @@ import kotlin.properties.Delegates
|
||||
val KOTLIN_REFLECT_FQ_NAME = FqName("kotlin.reflect")
|
||||
|
||||
public class ReflectionTypes(private val module: ModuleDescriptor) {
|
||||
private val kotlinReflectScope: JetScope? by Delegates.lazy {
|
||||
module.getPackage(KOTLIN_REFLECT_FQ_NAME)?.memberScope
|
||||
private val kotlinReflectScope: JetScope by Delegates.lazy {
|
||||
module.getPackage(KOTLIN_REFLECT_FQ_NAME).memberScope
|
||||
}
|
||||
|
||||
fun find(className: String): ClassDescriptor {
|
||||
val name = Name.identifier(className)
|
||||
return kotlinReflectScope?.getClassifier(name) as? ClassDescriptor
|
||||
return kotlinReflectScope.getClassifier(name) as? ClassDescriptor
|
||||
?: ErrorUtils.createErrorClass(KOTLIN_REFLECT_FQ_NAME.child(name).asString())
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ public class BuiltInFictitiousFunctionClassFactory(
|
||||
val kindWithArity = parseClassName(className, packageFqName) ?: return null
|
||||
val (kind, arity) = kindWithArity // KT-5100
|
||||
|
||||
val containingPackageFragment = module.getPackage(packageFqName)!!.fragments.single()
|
||||
val containingPackageFragment = module.getPackage(packageFqName).fragments.single()
|
||||
|
||||
return FunctionClassDescriptor(storageManager, containingPackageFragment, kind, arity)
|
||||
}
|
||||
|
||||
+1
-1
@@ -158,7 +158,7 @@ public class FunctionClassDescriptor(
|
||||
if (functionKind.hasExtensionReceiver) functionArity++
|
||||
|
||||
val module = containingDeclaration.getContainingDeclaration()
|
||||
val kotlinPackageFragment = module.getPackage(BUILT_INS_PACKAGE_FQ_NAME)!!.fragments.single()
|
||||
val kotlinPackageFragment = module.getPackage(BUILT_INS_PACKAGE_FQ_NAME).fragments.single()
|
||||
|
||||
// If this is a KMemberFunction{n} or KExtensionFunction{n}, it extends Function{n} with the annotation kotlin.extension,
|
||||
// so that the value of this type is callable as an extension function, with the receiver before the dot
|
||||
|
||||
@@ -24,9 +24,6 @@ import org.jetbrains.kotlin.resolve.ImportPath
|
||||
import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||
|
||||
public trait ModuleDescriptor : DeclarationDescriptor, ModuleParameters {
|
||||
public fun getPackage(fqName: FqName): PackageViewDescriptor?
|
||||
public fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection<FqName>
|
||||
|
||||
override fun getContainingDeclaration(): DeclarationDescriptor? = null
|
||||
|
||||
public val builtIns: KotlinBuiltIns
|
||||
@@ -40,6 +37,10 @@ public trait ModuleDescriptor : DeclarationDescriptor, ModuleParameters {
|
||||
override fun <R, D> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R {
|
||||
return visitor.visitModuleDeclaration(this, data)
|
||||
}
|
||||
|
||||
public fun getPackage(fqName: FqName): PackageViewDescriptor
|
||||
|
||||
public fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection<FqName>
|
||||
}
|
||||
|
||||
trait ModuleParameters {
|
||||
@@ -56,4 +57,4 @@ public fun ModuleParameters(defaultImports: List<ImportPath>, platformToKotlinCl
|
||||
object : ModuleParameters {
|
||||
override val defaultImports: List<ImportPath> = defaultImports
|
||||
override val platformToKotlinClassMap: PlatformToKotlinClassMap = platformToKotlinClassMap
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,4 +29,6 @@ public interface PackageViewDescriptor : DeclarationDescriptor {
|
||||
public val module: ModuleDescriptor
|
||||
|
||||
public val fragments: List<PackageFragmentDescriptor>
|
||||
|
||||
public fun isEmpty(): Boolean = fragments.isEmpty()
|
||||
}
|
||||
|
||||
+30
-16
@@ -16,36 +16,46 @@
|
||||
|
||||
package org.jetbrains.kotlin.descriptors.impl
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorVisitor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.resolve.scopes.ChainedScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.LazyScopeAdapter
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.storage.get
|
||||
import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||
|
||||
public class PackageViewDescriptorImpl(
|
||||
override val module: ModuleDescriptor,
|
||||
public class LazyPackageViewDescriptorImpl(
|
||||
override val module: ModuleDescriptorImpl,
|
||||
override val fqName: FqName,
|
||||
override val fragments: List<PackageFragmentDescriptor>
|
||||
storageManager: StorageManager
|
||||
) : DeclarationDescriptorImpl(Annotations.EMPTY, fqName.shortNameOrSpecial()), PackageViewDescriptor {
|
||||
override val memberScope: JetScope = run {
|
||||
assert(fragments.isNotEmpty()) { "$fqName in module" }
|
||||
|
||||
val scopes = fragments.map { it.getMemberScope() } + SubpackagesScope(this)
|
||||
ChainedScope(this, "package view scope for $fqName in ${module.getName()}", *scopes.toTypedArray())
|
||||
override val fragments: List<PackageFragmentDescriptor> by storageManager.createLazyValue {
|
||||
module.packageFragmentProvider.getPackageFragments(fqName)
|
||||
}
|
||||
|
||||
override fun getContainingDeclaration(): PackageViewDescriptor? = if (fqName.isRoot()) null else module.getPackage(fqName.parent())
|
||||
override val memberScope: JetScope = LazyScopeAdapter(storageManager.createLazyValue {
|
||||
if (fragments.isEmpty()) {
|
||||
JetScope.Empty
|
||||
}
|
||||
else {
|
||||
val scopes = fragments.map { it.getMemberScope() } + SubpackagesScope(module, fqName)
|
||||
ChainedScope(this, "package view scope for $fqName in ${module.getName()}", *scopes.toTypedArray())
|
||||
}
|
||||
})
|
||||
|
||||
override fun substitute(substitutor: TypeSubstitutor): DeclarationDescriptor? = this
|
||||
|
||||
override fun <R, D> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R = visitor.visitPackageViewDescriptor(this, data)
|
||||
override fun getContainingDeclaration(): PackageViewDescriptor? {
|
||||
return if (fqName.isRoot()) null else module.getPackage(fqName.parent())
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
val that = other as PackageViewDescriptorImpl
|
||||
return fqName == that.fqName && module == that.module
|
||||
val that = other as? PackageViewDescriptor ?: return false
|
||||
return this.fqName == that.fqName && this.module == that.module
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
@@ -53,4 +63,8 @@ public class PackageViewDescriptorImpl(
|
||||
result = 31 * result + fqName.hashCode()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun substitute(substitutor: TypeSubstitutor): DeclarationDescriptor? = this
|
||||
|
||||
override fun <R, D> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R = visitor.visitPackageViewDescriptor(this, data)
|
||||
}
|
||||
+10
-10
@@ -43,6 +43,12 @@ public class ModuleDescriptorImpl(
|
||||
private var dependencies: ModuleDependencies? = null
|
||||
private var packageFragmentProviderForModuleContent: PackageFragmentProvider? = null
|
||||
|
||||
override fun getPackage(fqName: FqName): PackageViewDescriptor = LazyPackageViewDescriptorImpl(this, fqName, storageManager)
|
||||
|
||||
override fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection<FqName> {
|
||||
return packageFragmentProvider.getSubPackagesOf(fqName, nameFilter)
|
||||
}
|
||||
|
||||
private val packageFragmentProviderForWholeModuleWithDependencies by Delegates.lazy {
|
||||
val moduleDependencies = dependencies.sure { "Dependencies of module $id were not set before querying module content" }
|
||||
val dependenciesDescriptors = moduleDependencies.descriptors
|
||||
@@ -82,17 +88,11 @@ public class ModuleDescriptorImpl(
|
||||
*/
|
||||
public fun initialize(providerForModuleContent: PackageFragmentProvider) {
|
||||
assert(!isInitialized) { "Attempt to initialize module $id twice" }
|
||||
packageFragmentProviderForModuleContent = providerForModuleContent
|
||||
this.packageFragmentProviderForModuleContent = providerForModuleContent
|
||||
}
|
||||
|
||||
override fun getPackage(fqName: FqName): PackageViewDescriptor? {
|
||||
val fragments = packageFragmentProviderForWholeModuleWithDependencies.getPackageFragments(fqName)
|
||||
return if (!fragments.isEmpty()) PackageViewDescriptorImpl(this, fqName, fragments) else null
|
||||
}
|
||||
|
||||
override fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection<FqName> {
|
||||
return packageFragmentProviderForWholeModuleWithDependencies.getSubPackagesOf(fqName, nameFilter)
|
||||
}
|
||||
val packageFragmentProvider: PackageFragmentProvider
|
||||
get() = packageFragmentProviderForWholeModuleWithDependencies
|
||||
|
||||
private val friendModules = LinkedHashSet<ModuleDescriptor>()
|
||||
|
||||
@@ -121,4 +121,4 @@ public class LazyModuleDependencies(
|
||||
|
||||
override val descriptors: List<ModuleDescriptorImpl>
|
||||
get() = dependencies()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,28 +17,38 @@
|
||||
package org.jetbrains.kotlin.descriptors.impl
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl
|
||||
import org.jetbrains.kotlin.utils.*
|
||||
|
||||
import java.util.ArrayList
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import org.jetbrains.kotlin.utils.sure
|
||||
import java.util.ArrayList
|
||||
|
||||
public class SubpackagesScope(private val containingDeclaration: PackageViewDescriptor) : JetScopeImpl() {
|
||||
public class SubpackagesScope(private val moduleDescriptor: ModuleDescriptor, private val fqName: FqName) : JetScopeImpl() {
|
||||
override fun getContainingDeclaration(): DeclarationDescriptor {
|
||||
return containingDeclaration
|
||||
return moduleDescriptor.getPackage(fqName)
|
||||
}
|
||||
|
||||
override fun getPackage(name: Name): PackageViewDescriptor? {
|
||||
return if (name.isSpecial()) null else containingDeclaration.getModule().getPackage(containingDeclaration.getFqName().child(name))
|
||||
if (name.isSpecial()) {
|
||||
return null
|
||||
}
|
||||
val packageViewDescriptor = moduleDescriptor.getPackage(fqName.child(name))
|
||||
if (packageViewDescriptor.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
return packageViewDescriptor
|
||||
}
|
||||
|
||||
override fun getDescriptors(kindFilter: DescriptorKindFilter,
|
||||
nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
|
||||
if (!kindFilter.acceptsKinds(DescriptorKindFilter.PACKAGES_MASK)) return listOf()
|
||||
|
||||
val subFqNames = containingDeclaration.getModule().getSubPackagesOf(containingDeclaration.getFqName(), nameFilter)
|
||||
val subFqNames = moduleDescriptor.getSubPackagesOf(fqName, nameFilter)
|
||||
val result = ArrayList<DeclarationDescriptor>(subFqNames.size())
|
||||
for (subFqName in subFqNames) {
|
||||
val shortName = subFqName.shortName()
|
||||
@@ -53,7 +63,7 @@ public class SubpackagesScope(private val containingDeclaration: PackageViewDesc
|
||||
p.println(javaClass.getSimpleName(), " {")
|
||||
p.pushIndent()
|
||||
|
||||
p.println("thisDescriptor = ", containingDeclaration)
|
||||
p.println("thisDescriptor = ", getContainingDeclaration())
|
||||
|
||||
p.popIndent()
|
||||
p.println("}")
|
||||
|
||||
@@ -44,8 +44,7 @@ public val DeclarationDescriptor.module: ModuleDescriptor
|
||||
|
||||
public fun ModuleDescriptor.resolveTopLevelClass(topLevelClassFqName: FqName): ClassDescriptor? {
|
||||
assert(!topLevelClassFqName.isRoot())
|
||||
return getPackage(topLevelClassFqName.parent())?.memberScope
|
||||
?.getClassifier(topLevelClassFqName.shortName()) as? ClassDescriptor
|
||||
return getPackage(topLevelClassFqName.parent()).memberScope.getClassifier(topLevelClassFqName.shortName()) as? ClassDescriptor
|
||||
}
|
||||
|
||||
public val ClassDescriptor.classId: ClassId
|
||||
|
||||
@@ -26,7 +26,7 @@ class KPackageImpl(override val jClass: Class<*>) : KCallableContainerImpl(), KP
|
||||
val moduleData = jClass.getOrCreateModule()
|
||||
val fqName = jClass.classId.getPackageFqName()
|
||||
|
||||
moduleData.module.getPackage(fqName) ?: throw KotlinReflectionInternalError("Package not resolved: $fqName")
|
||||
moduleData.module.getPackage(fqName)
|
||||
}
|
||||
|
||||
override val scope: JetScope get() = descriptor.memberScope
|
||||
|
||||
Reference in New Issue
Block a user