Created common supertype for LexicalScope & ImportingScope -- HierarchicalScope

This commit is contained in:
Stanislav Erokhin
2015-11-02 14:57:52 +03:00
parent 4c36e20242
commit 21fdd2ffc3
22 changed files with 119 additions and 131 deletions
@@ -29,9 +29,7 @@ import org.jetbrains.kotlin.incremental.record
import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.scopes.BaseImportingScope import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.collectSyntheticExtensionProperties import org.jetbrains.kotlin.resolve.scopes.utils.collectSyntheticExtensionProperties
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
@@ -49,7 +47,7 @@ interface SyntheticJavaPropertyDescriptor : PropertyDescriptor {
val setMethod: FunctionDescriptor? val setMethod: FunctionDescriptor?
companion object { companion object {
fun findByGetterOrSetter(getterOrSetter: FunctionDescriptor, resolutionScope: LexicalScope): SyntheticJavaPropertyDescriptor? { fun findByGetterOrSetter(getterOrSetter: FunctionDescriptor, resolutionScope: HierarchicalScope): SyntheticJavaPropertyDescriptor? {
val name = getterOrSetter.getName() val name = getterOrSetter.getName()
if (name.isSpecial()) return null if (name.isSpecial()) return null
val identifier = name.getIdentifier() val identifier = name.getIdentifier()
@@ -42,7 +42,7 @@ import org.jetbrains.kotlin.resolve.scopes.JetScopeUtils
import org.jetbrains.kotlin.resolve.scopes.KtScope import org.jetbrains.kotlin.resolve.scopes.KtScope
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsLexicalScope
import org.jetbrains.kotlin.resolve.source.toSourceElement import org.jetbrains.kotlin.resolve.source.toSourceElement
import org.jetbrains.kotlin.types.FunctionPlaceholders import org.jetbrains.kotlin.types.FunctionPlaceholders
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
@@ -101,7 +101,7 @@ public fun resolvePossiblyAmbiguousCallableReference(
fun resolveInScope(traceTitle: String, staticScope: KtScope): OverloadResolutionResults<CallableDescriptor> { fun resolveInScope(traceTitle: String, staticScope: KtScope): OverloadResolutionResults<CallableDescriptor> {
val temporaryTraceAndCache = TemporaryTraceAndCache.create(context, traceTitle, reference) val temporaryTraceAndCache = TemporaryTraceAndCache.create(context, traceTitle, reference)
val newContext = context.replaceTraceAndCache(temporaryTraceAndCache).replaceScope(staticScope.memberScopeAsImportingScope()) val newContext = context.replaceTraceAndCache(temporaryTraceAndCache).replaceScope(staticScope.memberScopeAsLexicalScope())
val results = resolvePossiblyAmbiguousCallableReference(reference, ReceiverValue.NO_RECEIVER, newContext, resolutionMode, callResolver) val results = resolvePossiblyAmbiguousCallableReference(reference, ReceiverValue.NO_RECEIVER, newContext, resolutionMode, callResolver)
resolutionMode.acceptResolution(results, temporaryTraceAndCache) resolutionMode.acceptResolution(results, temporaryTraceAndCache)
return results return results
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.resolve.LibrarySourceHacks
import org.jetbrains.kotlin.resolve.calls.tasks.createSynthesizedInvokes import org.jetbrains.kotlin.resolve.calls.tasks.createSynthesizedInvokes
import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject
import org.jetbrains.kotlin.resolve.descriptorUtil.hasClassObjectType import org.jetbrains.kotlin.resolve.descriptorUtil.hasClassObjectType
import org.jetbrains.kotlin.resolve.scopes.ImportingScope import org.jetbrains.kotlin.resolve.scopes.HierarchicalScope
import org.jetbrains.kotlin.resolve.scopes.LexicalChainedScope import org.jetbrains.kotlin.resolve.scopes.LexicalChainedScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.* import org.jetbrains.kotlin.resolve.scopes.utils.*
@@ -89,7 +89,7 @@ private object FunctionCollector : CallableDescriptorCollector<FunctionDescripto
override fun getLocalNonExtensionsByName(lexicalScope: LexicalScope, name: Name, location: LookupLocation): Collection<FunctionDescriptor> { override fun getLocalNonExtensionsByName(lexicalScope: LexicalScope, name: Name, location: LookupLocation): Collection<FunctionDescriptor> {
return lexicalScope.collectAllFromMeAndParent { return lexicalScope.collectAllFromMeAndParent {
if (it !is ImportingScope && it.ownerDescriptor is FunctionDescriptor) { if (it is LexicalScope && it.ownerDescriptor is FunctionDescriptor) {
it.getContributedFunctions(name, location).filter { it.extensionReceiverParameter == null } + it.getContributedFunctions(name, location).filter { it.extensionReceiverParameter == null } +
getConstructors(it.getContributedClassifier(name, location)) getConstructors(it.getContributedClassifier(name, location))
} }
@@ -139,7 +139,7 @@ private object FunctionCollector : CallableDescriptorCollector<FunctionDescripto
} }
private fun getConstructors( private fun getConstructors(
scope: LexicalScope, scope: HierarchicalScope,
name: Name, name: Name,
location: LookupLocation, location: LookupLocation,
filterClassPredicate: (ClassDescriptor) -> Boolean = { true } filterClassPredicate: (ClassDescriptor) -> Boolean = { true }
@@ -179,7 +179,7 @@ private object VariableCollector : CallableDescriptorCollector<VariableDescripto
} }
} }
private fun getFakeDescriptorForObject(scope: LexicalScope, name: Name, location: LookupLocation): VariableDescriptor? { private fun getFakeDescriptorForObject(scope: HierarchicalScope, name: Name, location: LookupLocation): VariableDescriptor? {
val classifier = scope.findClassifier(name, location) val classifier = scope.findClassifier(name, location)
if (classifier !is ClassDescriptor || !classifier.hasClassObjectType) return null if (classifier !is ClassDescriptor || !classifier.hasClassObjectType) return null
@@ -42,6 +42,7 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue.NO_RECEIVER import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue.NO_RECEIVER
import org.jetbrains.kotlin.resolve.scopes.utils.getImplicitReceiversHierarchy import org.jetbrains.kotlin.resolve.scopes.utils.getImplicitReceiversHierarchy
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsLexicalScope
import org.jetbrains.kotlin.resolve.validation.InfixValidator import org.jetbrains.kotlin.resolve.validation.InfixValidator
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.ErrorUtils import org.jetbrains.kotlin.types.ErrorUtils
@@ -69,7 +70,7 @@ public class TaskPrioritizer(
if (explicitReceiver is QualifierReceiver) { if (explicitReceiver is QualifierReceiver) {
val qualifierReceiver: QualifierReceiver = explicitReceiver val qualifierReceiver: QualifierReceiver = explicitReceiver
val receiverScope = qualifierReceiver.getNestedClassesAndPackageMembersScope().memberScopeAsImportingScope() val receiverScope = qualifierReceiver.getNestedClassesAndPackageMembersScope().memberScopeAsLexicalScope()
doComputeTasks(NO_RECEIVER, taskPrioritizerContext.replaceScope(receiverScope)) doComputeTasks(NO_RECEIVER, taskPrioritizerContext.replaceScope(receiverScope))
computeTasksForClassObjectReceiver(qualifierReceiver, taskPrioritizerContext) computeTasksForClassObjectReceiver(qualifierReceiver, taskPrioritizerContext)
} }
@@ -260,7 +261,7 @@ public class TaskPrioritizer(
val dynamicScope = dynamicCallableDescriptors.createDynamicDescriptorScope(c.context.call, c.scope.ownerDescriptor) val dynamicScope = dynamicCallableDescriptors.createDynamicDescriptorScope(c.context.call, c.scope.ownerDescriptor)
val dynamicDescriptors = c.callableDescriptorCollectors.flatMap { val dynamicDescriptors = c.callableDescriptorCollectors.flatMap {
it.getNonExtensionsByName(dynamicScope.memberScopeAsImportingScope(), c.name, createLookupLocation(c)) it.getNonExtensionsByName(dynamicScope.memberScopeAsLexicalScope(), c.name, createLookupLocation(c))
} }
convertWithReceivers(dynamicDescriptors, explicitReceiver.value, NO_RECEIVER, createKind(DISPATCH_RECEIVER, isExplicit), c.context.call) convertWithReceivers(dynamicDescriptors, explicitReceiver.value, NO_RECEIVER, createKind(DISPATCH_RECEIVER, isExplicit), c.context.call)
@@ -281,7 +282,7 @@ public class TaskPrioritizer(
) { ) {
c.result.addCandidates { c.result.addCandidates {
val memberExtensions = val memberExtensions =
callableDescriptorCollector.getExtensionsByName(dispatchReceiver.type.memberScope.memberScopeAsImportingScope(), c.name, receiverParameter.types, createLookupLocation(c)) callableDescriptorCollector.getExtensionsByName(dispatchReceiver.type.memberScope.memberScopeAsLexicalScope(), c.name, receiverParameter.types, createLookupLocation(c))
convertWithReceivers(memberExtensions, dispatchReceiver, receiverParameter.value, receiverKind, c.context.call) convertWithReceivers(memberExtensions, dispatchReceiver, receiverParameter.value, receiverKind, c.context.call)
} }
} }
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.resolve.lazy package org.jetbrains.kotlin.resolve.lazy
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.impl.SubpackagesScope import org.jetbrains.kotlin.descriptors.impl.SubpackagesScope
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
@@ -84,10 +83,10 @@ public open class FileScopeProviderImpl(
var scope: ImportingScope var scope: ImportingScope
scope = LazyImportScope(null, packageFragment, defaultAllUnderImportResolver, LazyImportScope.FilteringKind.INVISIBLE_CLASSES, scope = LazyImportScope(null, defaultAllUnderImportResolver, LazyImportScope.FilteringKind.INVISIBLE_CLASSES,
"Default all under imports in $debugName (invisible classes only)") "Default all under imports in $debugName (invisible classes only)")
scope = LazyImportScope(scope, packageFragment, allUnderImportResolver, LazyImportScope.FilteringKind.INVISIBLE_CLASSES, scope = LazyImportScope(scope, allUnderImportResolver, LazyImportScope.FilteringKind.INVISIBLE_CLASSES,
"All under imports in $debugName (invisible classes only)") "All under imports in $debugName (invisible classes only)")
for (additionalScope in additionalScopes.flatMap { it.scopes }) { for (additionalScope in additionalScopes.flatMap { it.scopes }) {
@@ -95,25 +94,22 @@ public open class FileScopeProviderImpl(
scope = additionalScope.withParent(scope) scope = additionalScope.withParent(scope)
} }
scope = LazyImportScope(scope, packageFragment, defaultAllUnderImportResolver, LazyImportScope.FilteringKind.VISIBLE_CLASSES, scope = LazyImportScope(scope, defaultAllUnderImportResolver, LazyImportScope.FilteringKind.VISIBLE_CLASSES,
"Default all under imports in $debugName (visible classes)") "Default all under imports in $debugName (visible classes)")
scope = LazyImportScope(scope, packageFragment, allUnderImportResolver, LazyImportScope.FilteringKind.VISIBLE_CLASSES, scope = LazyImportScope(scope, allUnderImportResolver, LazyImportScope.FilteringKind.VISIBLE_CLASSES,
"All under imports in $debugName (visible classes)") "All under imports in $debugName (visible classes)")
scope = LazyImportScope(scope, packageFragment, defaultAliasImportResolver, LazyImportScope.FilteringKind.ALL, scope = LazyImportScope(scope, defaultAliasImportResolver, LazyImportScope.FilteringKind.ALL,
"Default alias imports in $debugName") "Default alias imports in $debugName")
scope = SubpackagesScope(moduleDescriptor, FqName.ROOT).memberScopeAsImportingScope(scope) scope = SubpackagesScope(moduleDescriptor, FqName.ROOT).memberScopeAsImportingScope(scope)
scope = NoSubpackagesInPackageScope(packageView).memberScopeAsImportingScope(scope) //TODO: problems with visibility too scope = NoSubpackagesInPackageScope(packageView).memberScopeAsImportingScope(scope) //TODO: problems with visibility too
scope = LazyImportScope(scope, packageFragment, aliasImportResolver, LazyImportScope.FilteringKind.ALL, "Alias imports in $debugName") scope = LazyImportScope(scope, aliasImportResolver, LazyImportScope.FilteringKind.ALL, "Alias imports in $debugName")
val lexicalScope = object : BaseLexicalScope(scope) {
override val ownerDescriptor: DeclarationDescriptor
get() = packageFragment
val lexicalScope = object : BaseLexicalScope(scope, packageFragment) {
override fun printStructure(p: Printer) { override fun printStructure(p: Printer) {
p.println("File top-level scope (empty)") p.println("File top-level scope (empty)")
} }
@@ -164,7 +164,6 @@ class LazyImportResolver(
class LazyImportScope( class LazyImportScope(
override val parent: ImportingScope?, override val parent: ImportingScope?,
override val ownerDescriptor: DeclarationDescriptor,
private val importResolver: LazyImportResolver, private val importResolver: LazyImportResolver,
private val filteringKind: LazyImportScope.FilteringKind, private val filteringKind: LazyImportScope.FilteringKind,
private val debugName: String private val debugName: String
@@ -258,8 +257,6 @@ class LazyImportScope(
p.println(javaClass.getSimpleName(), ": ", debugName, " {") p.println(javaClass.getSimpleName(), ": ", debugName, " {")
p.pushIndent() p.pushIndent()
p.println("ownerDescriptor = ", ownerDescriptor)
p.popIndent() p.popIndent()
p.println("}") p.println("}")
} }
@@ -94,7 +94,7 @@ class LexicalWritableScope(
} }
private inner class Snapshot(val descriptorLimit: Int) : LexicalScope { private inner class Snapshot(val descriptorLimit: Int) : LexicalScope {
override val parent: LexicalScope? override val parent: LexicalScope
get() = this@LexicalWritableScope.parent get() = this@LexicalWritableScope.parent
override val ownerDescriptor: DeclarationDescriptor override val ownerDescriptor: DeclarationDescriptor
get() = this@LexicalWritableScope.ownerDescriptor get() = this@LexicalWritableScope.ownerDescriptor
@@ -24,13 +24,8 @@ import org.jetbrains.kotlin.utils.Printer
// see utils/ScopeUtils.kt // see utils/ScopeUtils.kt
interface LexicalScope { interface HierarchicalScope {
val parent: LexicalScope? val parent: HierarchicalScope?
val ownerDescriptor: DeclarationDescriptor
val isOwnerDescriptorAccessibleByLabel: Boolean
val implicitReceiver: ReceiverParameterDescriptor?
/** /**
* All visible descriptors from current scope possibly filtered by the given name and kind filters * All visible descriptors from current scope possibly filtered by the given name and kind filters
@@ -50,16 +45,19 @@ interface LexicalScope {
fun printStructure(p: Printer) fun printStructure(p: Printer)
} }
interface LexicalScope: HierarchicalScope {
override val parent: HierarchicalScope
val ownerDescriptor: DeclarationDescriptor
val isOwnerDescriptorAccessibleByLabel: Boolean
val implicitReceiver: ReceiverParameterDescriptor?
}
// TODO: common base interface instead direct inheritance // TODO: common base interface instead direct inheritance
interface ImportingScope : LexicalScope { interface ImportingScope : HierarchicalScope {
override val parent: ImportingScope? override val parent: ImportingScope?
override val isOwnerDescriptorAccessibleByLabel: Boolean
get() = false
override val implicitReceiver: ReceiverParameterDescriptor?
get() = null
// methods getDeclaredSmth for this scope will be delegated to importScope // methods getDeclaredSmth for this scope will be delegated to importScope
fun getContributedPackage(name: Name): PackageViewDescriptor? fun getContributedPackage(name: Name): PackageViewDescriptor?
@@ -77,13 +75,7 @@ interface ImportingScope : LexicalScope {
} }
} }
abstract class BaseLexicalScope(override val parent: LexicalScope?) : LexicalScope { abstract class BaseHierarchicalScope(override val parent: HierarchicalScope?) : HierarchicalScope {
override val isOwnerDescriptorAccessibleByLabel: Boolean
get() = false
override val implicitReceiver: ReceiverParameterDescriptor?
get() = null
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> = emptyList() override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> = emptyList()
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = null override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = null
@@ -93,18 +85,23 @@ abstract class BaseLexicalScope(override val parent: LexicalScope?) : LexicalSco
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor> = emptyList() override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor> = emptyList()
} }
abstract class BaseImportingScope(parent: ImportingScope?) : BaseLexicalScope(parent), ImportingScope { abstract class BaseLexicalScope(
override val parent: ImportingScope? parent: HierarchicalScope,
get() = super.parent as ImportingScope? override val ownerDescriptor: DeclarationDescriptor
): BaseHierarchicalScope(parent), LexicalScope {
override val parent: HierarchicalScope
get() = super.parent!!
override val ownerDescriptor: DeclarationDescriptor override val isOwnerDescriptorAccessibleByLabel: Boolean
get() = throw UnsupportedOperationException()
override final val isOwnerDescriptorAccessibleByLabel: Boolean
get() = false get() = false
override final val implicitReceiver: ReceiverParameterDescriptor? override val implicitReceiver: ReceiverParameterDescriptor?
get() = null get() = null
}
abstract class BaseImportingScope(parent: ImportingScope?) : BaseHierarchicalScope(parent), ImportingScope {
override val parent: ImportingScope?
get() = super.parent as ImportingScope?
override fun getContributedPackage(name: Name): PackageViewDescriptor? = null override fun getContributedPackage(name: Name): PackageViewDescriptor? = null
@@ -26,21 +26,21 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.util.collectionUtils.concat import org.jetbrains.kotlin.util.collectionUtils.concat
import org.jetbrains.kotlin.utils.Printer import org.jetbrains.kotlin.utils.Printer
public val LexicalScope.parentsWithSelf: Sequence<LexicalScope> public val HierarchicalScope.parentsWithSelf: Sequence<HierarchicalScope>
get() = sequence(this) { it.parent } get() = sequence(this) { it.parent }
public val LexicalScope.parents: Sequence<LexicalScope> public val HierarchicalScope.parents: Sequence<HierarchicalScope>
get() = parentsWithSelf.drop(1) get() = parentsWithSelf.drop(1)
/** /**
* Adds receivers to the list in order of locality, so that the closest (the most local) receiver goes first * Adds receivers to the list in order of locality, so that the closest (the most local) receiver goes first
*/ */
public fun LexicalScope.getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> = collectFromMeAndParent { public fun LexicalScope.getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> = collectFromMeAndParent {
it.implicitReceiver (it as? LexicalScope)?.implicitReceiver
} }
public fun LexicalScope.getDeclarationsByLabel(labelName: Name): Collection<DeclarationDescriptor> = collectAllFromMeAndParent { public fun LexicalScope.getDeclarationsByLabel(labelName: Name): Collection<DeclarationDescriptor> = collectAllFromMeAndParent {
if (it.isOwnerDescriptorAccessibleByLabel && it.ownerDescriptor.name == labelName) { if (it is LexicalScope && it.isOwnerDescriptorAccessibleByLabel && it.ownerDescriptor.name == labelName) {
listOf(it.ownerDescriptor) listOf(it.ownerDescriptor)
} }
else { else {
@@ -49,7 +49,7 @@ public fun LexicalScope.getDeclarationsByLabel(labelName: Name): Collection<Decl
} }
// Result is guaranteed to be filtered by kind and name. // Result is guaranteed to be filtered by kind and name.
public fun LexicalScope.collectDescriptorsFiltered( public fun HierarchicalScope.collectDescriptorsFiltered(
kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL, kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL,
nameFilter: (Name) -> Boolean = { true } nameFilter: (Name) -> Boolean = { true }
): Collection<DeclarationDescriptor> { ): Collection<DeclarationDescriptor> {
@@ -72,42 +72,42 @@ public fun LexicalScope.findLocalVariable(name: Name): VariableDescriptor? {
} }
} }
public fun LexicalScope.findClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? public fun HierarchicalScope.findClassifier(name: Name, location: LookupLocation): ClassifierDescriptor?
= findFirstFromMeAndParent { it.getContributedClassifier(name, location) } = findFirstFromMeAndParent { it.getContributedClassifier(name, location) }
public fun LexicalScope.findPackage(name: Name): PackageViewDescriptor? public fun HierarchicalScope.findPackage(name: Name): PackageViewDescriptor?
= findFirstFromImportingScopes { it.getContributedPackage(name) } = findFirstFromImportingScopes { it.getContributedPackage(name) }
public fun LexicalScope.collectVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor> public fun HierarchicalScope.collectVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor>
= collectAllFromMeAndParent { it.getContributedVariables(name, location) } = collectAllFromMeAndParent { it.getContributedVariables(name, location) }
public fun LexicalScope.collectFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor> public fun HierarchicalScope.collectFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor>
= collectAllFromMeAndParent { it.getContributedFunctions(name, location) } = collectAllFromMeAndParent { it.getContributedFunctions(name, location) }
public fun LexicalScope.findVariable(name: Name, location: LookupLocation, predicate: (VariableDescriptor) -> Boolean = { true }): VariableDescriptor? { public fun HierarchicalScope.findVariable(name: Name, location: LookupLocation, predicate: (VariableDescriptor) -> Boolean = { true }): VariableDescriptor? {
processForMeAndParent { processForMeAndParent {
it.getContributedVariables(name, location).firstOrNull(predicate)?.let { return it } it.getContributedVariables(name, location).firstOrNull(predicate)?.let { return it }
} }
return null return null
} }
public fun LexicalScope.findFunction(name: Name, location: LookupLocation, predicate: (FunctionDescriptor) -> Boolean = { true }): FunctionDescriptor? { public fun HierarchicalScope.findFunction(name: Name, location: LookupLocation, predicate: (FunctionDescriptor) -> Boolean = { true }): FunctionDescriptor? {
processForMeAndParent { processForMeAndParent {
it.getContributedFunctions(name, location).firstOrNull(predicate)?.let { return it } it.getContributedFunctions(name, location).firstOrNull(predicate)?.let { return it }
} }
return null return null
} }
public fun LexicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation) public fun HierarchicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation)
= collectAllFromImportingScopes { it.getContributedSyntheticExtensionProperties(receiverTypes, name, location) } = collectAllFromImportingScopes { it.getContributedSyntheticExtensionProperties(receiverTypes, name, location) }
public fun LexicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation) public fun HierarchicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation)
= collectAllFromImportingScopes { it.getContributedSyntheticExtensionFunctions(receiverTypes, name, location) } = collectAllFromImportingScopes { it.getContributedSyntheticExtensionFunctions(receiverTypes, name, location) }
public fun LexicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>) public fun HierarchicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>)
= collectAllFromImportingScopes { it.getContributedSyntheticExtensionProperties(receiverTypes) } = collectAllFromImportingScopes { it.getContributedSyntheticExtensionProperties(receiverTypes) }
public fun LexicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>) public fun HierarchicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>)
= collectAllFromImportingScopes { it.getContributedSyntheticExtensionFunctions(receiverTypes) } = collectAllFromImportingScopes { it.getContributedSyntheticExtensionFunctions(receiverTypes) }
public fun LexicalScope.takeSnapshot(): LexicalScope = if (this is LexicalWritableScope) takeSnapshot() else this public fun LexicalScope.takeSnapshot(): LexicalScope = if (this is LexicalWritableScope) takeSnapshot() else this
@@ -115,6 +115,9 @@ public fun LexicalScope.takeSnapshot(): LexicalScope = if (this is LexicalWritab
@JvmOverloads @JvmOverloads
public fun KtScope.memberScopeAsImportingScope(parentScope: ImportingScope? = null): ImportingScope = MemberScopeToImportingScopeAdapter(parentScope, this) public fun KtScope.memberScopeAsImportingScope(parentScope: ImportingScope? = null): ImportingScope = MemberScopeToImportingScopeAdapter(parentScope, this)
@Deprecated("Temporary method for scope migration")
public fun KtScope.memberScopeAsLexicalScope(): LexicalScope = memberScopeAsImportingScope().asLexicalScope(getContainingDeclaration())
private class MemberScopeToImportingScopeAdapter(override val parent: ImportingScope?, val memberScope: KtScope) : ImportingScope { private class MemberScopeToImportingScopeAdapter(override val parent: ImportingScope?, val memberScope: KtScope) : ImportingScope {
override fun getContributedPackage(name: Name): PackageViewDescriptor? = memberScope.getPackage(name) override fun getContributedPackage(name: Name): PackageViewDescriptor? = memberScope.getPackage(name)
@@ -133,9 +136,6 @@ private class MemberScopeToImportingScopeAdapter(override val parent: ImportingS
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= memberScope.getDescriptors(kindFilter, nameFilter) = memberScope.getDescriptors(kindFilter, nameFilter)
override val ownerDescriptor: DeclarationDescriptor
get() = memberScope.getContainingDeclaration()
override fun getContributedClassifier(name: Name, location: LookupLocation) = memberScope.getClassifier(name, location) override fun getContributedClassifier(name: Name, location: LookupLocation) = memberScope.getClassifier(name, location)
override fun getContributedVariables(name: Name, location: LookupLocation) = memberScope.getProperties(name, location) override fun getContributedVariables(name: Name, location: LookupLocation) = memberScope.getProperties(name, location)
@@ -159,7 +159,7 @@ private class MemberScopeToImportingScopeAdapter(override val parent: ImportingS
} }
} }
inline fun LexicalScope.processForMeAndParent(process: (LexicalScope) -> Unit) { inline fun HierarchicalScope.processForMeAndParent(process: (HierarchicalScope) -> Unit) {
var currentScope = this var currentScope = this
while (true) { while (true) {
process(currentScope) process(currentScope)
@@ -167,8 +167,8 @@ inline fun LexicalScope.processForMeAndParent(process: (LexicalScope) -> Unit) {
} }
} }
private inline fun <T: Any> LexicalScope.collectFromMeAndParent( private inline fun <T: Any> HierarchicalScope.collectFromMeAndParent(
collect: (LexicalScope) -> T? collect: (HierarchicalScope) -> T?
): List<T> { ): List<T> {
var result: MutableList<T>? = null var result: MutableList<T>? = null
processForMeAndParent { processForMeAndParent {
@@ -183,39 +183,34 @@ private inline fun <T: Any> LexicalScope.collectFromMeAndParent(
return result ?: emptyList() return result ?: emptyList()
} }
inline fun <T: Any> LexicalScope.collectAllFromMeAndParent( inline fun <T: Any> HierarchicalScope.collectAllFromMeAndParent(
collect: (LexicalScope) -> Collection<T> collect: (HierarchicalScope) -> Collection<T>
): Collection<T> { ): Collection<T> {
var result: Collection<T>? = null var result: Collection<T>? = null
processForMeAndParent { result = result.concat(collect(it)) } processForMeAndParent { result = result.concat(collect(it)) }
return result ?: emptySet() return result ?: emptySet()
} }
inline fun <T: Any> LexicalScope.findFirstFromMeAndParent(fetch: (LexicalScope) -> T?): T? { inline fun <T: Any> HierarchicalScope.findFirstFromMeAndParent(fetch: (HierarchicalScope) -> T?): T? {
processForMeAndParent { fetch(it)?.let { return it } } processForMeAndParent { fetch(it)?.let { return it } }
return null return null
} }
inline fun <T: Any> LexicalScope.collectAllFromImportingScopes( inline fun <T: Any> HierarchicalScope.collectAllFromImportingScopes(
collect: (ImportingScope) -> Collection<T> collect: (ImportingScope) -> Collection<T>
): Collection<T> { ): Collection<T> {
return collectAllFromMeAndParent { if (it is ImportingScope) collect(it) else emptyList() } return collectAllFromMeAndParent { if (it is ImportingScope) collect(it) else emptyList() }
} }
inline fun <T: Any> LexicalScope.findFirstFromImportingScopes(fetch: (ImportingScope) -> T?): T? { inline fun <T: Any> HierarchicalScope.findFirstFromImportingScopes(fetch: (ImportingScope) -> T?): T? {
return findFirstFromMeAndParent { if (it is ImportingScope) fetch(it) else null } return findFirstFromMeAndParent { if (it is ImportingScope) fetch(it) else null }
} }
fun LexicalScope.addImportingScopes(importScopes: List<ImportingScope>): LexicalScope { fun LexicalScope.addImportingScopes(importScopes: List<ImportingScope>): LexicalScope {
if (this is ImportingScope) { val lastLexicalScope = parentsWithSelf.last { it is LexicalScope }
return chainImportingScopes(importScopes, this)!! val firstImporting = lastLexicalScope.parent as ImportingScope
} val newFirstImporting = chainImportingScopes(importScopes, firstImporting)
else { return LexicalScopeWrapper(this, newFirstImporting!!)
val lastNonImporting = parentsWithSelf.last { it !is ImportingScope }
val firstImporting = lastNonImporting.parent as ImportingScope?
val newFirstImporting = chainImportingScopes(importScopes, firstImporting)
return LexicalScopeWrapper(this, newFirstImporting)
}
} }
fun LexicalScope.addImportingScope(importScope: ImportingScope): LexicalScope fun LexicalScope.addImportingScope(importScope: ImportingScope): LexicalScope
@@ -229,22 +224,19 @@ fun ImportingScope.withParent(newParent: ImportingScope?): ImportingScope {
} }
fun LexicalScope.replaceImportingScopes(importingScopeChain: ImportingScope?): LexicalScope { fun LexicalScope.replaceImportingScopes(importingScopeChain: ImportingScope?): LexicalScope {
return if (this is ImportingScope) return LexicalScopeWrapper(this, importingScopeChain ?: ImportingScope.Empty)
importingScopeChain ?: ImportingScope.Empty
else
LexicalScopeWrapper(this, importingScopeChain)
} }
private class LexicalScopeWrapper(val delegate: LexicalScope, val newImportingScopeChain: ImportingScope?): LexicalScope by delegate { private class LexicalScopeWrapper(val delegate: LexicalScope, val newImportingScopeChain: ImportingScope): LexicalScope by delegate {
override val parent: LexicalScope? by lazy(LazyThreadSafetyMode.NONE) { override val parent: HierarchicalScope by lazy(LazyThreadSafetyMode.NONE) {
assert(delegate !is ImportingScope) assert(delegate !is ImportingScope)
val parent = delegate.parent val parent = delegate.parent
if (parent == null || parent is ImportingScope) { if (parent is LexicalScope) {
newImportingScopeChain LexicalScopeWrapper(parent, newImportingScopeChain)
} }
else { else {
LexicalScopeWrapper(parent, newImportingScopeChain) newImportingScopeChain
} }
} }
} }
@@ -256,3 +248,10 @@ fun chainImportingScopes(scopes: List<ImportingScope>, tail: ImportingScope? = n
scope.withParent(current) scope.withParent(current)
} }
} }
fun ImportingScope?.asLexicalScope(ownerDescriptor: DeclarationDescriptor): LexicalScope
= object : BaseLexicalScope(this ?: ImportingScope.Empty, ownerDescriptor) {
override fun printStructure(p: Printer) {
p.println("Empty lexical scope with owner = $ownerDescriptor and parent = ${this@asLexicalScope}.")
}
}
@@ -138,7 +138,7 @@ public class ExpectedResolveDataUtil {
emptyModule.setDependencies(emptyModule); emptyModule.setDependencies(emptyModule);
emptyModule.initialize(PackageFragmentProvider.Empty.INSTANCE$); emptyModule.initialize(PackageFragmentProvider.Empty.INSTANCE$);
LexicalScopeImpl lexicalScope = new LexicalScopeImpl(ImportingScope.Empty.INSTANCE, classDescriptor, false, LexicalScopeImpl lexicalScope = new LexicalScopeImpl(ScopeUtilsKt.asLexicalScope(ImportingScope.Empty.INSTANCE, emptyModule), classDescriptor, false,
classDescriptor.getThisAsReceiverParameter(), classDescriptor.getThisAsReceiverParameter(),
"Scope with implicit this for class: " + classDescriptor); "Scope with implicit this for class: " + classDescriptor);
@@ -92,7 +92,7 @@ public class DefaultModalityModifiersTest extends KotlinLiteFixture {
assert aClass instanceof KtClass; assert aClass instanceof KtClass;
AnalysisResult bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(file); AnalysisResult bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(file);
final DeclarationDescriptor classDescriptor = bindingContext.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, aClass); final DeclarationDescriptor classDescriptor = bindingContext.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, aClass);
return new LexicalScopeImpl(ScopeUtilsKt.memberScopeAsImportingScope(libraryScope), root, false, null, return new LexicalScopeImpl(TypeTestUtilsKt.asLexicalScope(libraryScope), root, false, null,
"JetDefaultModalityModifiersTest", RedeclarationHandler.DO_NOTHING, "JetDefaultModalityModifiersTest", RedeclarationHandler.DO_NOTHING,
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() { new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override @Override
@@ -25,8 +25,7 @@ import org.jetbrains.kotlin.resolve.FunctionDescriptorResolver;
import org.jetbrains.kotlin.resolve.OverloadUtil; import org.jetbrains.kotlin.resolve.OverloadUtil;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform; import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform;
import org.jetbrains.kotlin.resolve.scopes.ImportingScope; import org.jetbrains.kotlin.resolve.scopes.LexicalScope;
import org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt;
import org.jetbrains.kotlin.test.ConfigurationKind; import org.jetbrains.kotlin.test.ConfigurationKind;
import org.jetbrains.kotlin.test.KotlinLiteFixture; import org.jetbrains.kotlin.test.KotlinLiteFixture;
import org.jetbrains.kotlin.test.KotlinTestUtils; import org.jetbrains.kotlin.test.KotlinTestUtils;
@@ -171,7 +170,7 @@ public class KotlinOverloadTest extends KotlinLiteFixture {
private FunctionDescriptor makeFunction(String funDecl) { private FunctionDescriptor makeFunction(String funDecl) {
KtNamedFunction function = KtPsiFactoryKt.KtPsiFactory(getProject()).createFunction(funDecl); KtNamedFunction function = KtPsiFactoryKt.KtPsiFactory(getProject()).createFunction(funDecl);
ImportingScope scope = ScopeUtilsKt.memberScopeAsImportingScope(JvmPlatform.INSTANCE$.getBuiltIns().getBuiltInsPackageScope()); LexicalScope scope = TypeTestUtilsKt.asLexicalScope(JvmPlatform.INSTANCE$.getBuiltIns().getBuiltInsPackageScope());
return functionDescriptorResolver.resolveFunctionDescriptor(root, scope, function, KotlinTestUtils.DUMMY_TRACE, DataFlowInfo.EMPTY); return functionDescriptorResolver.resolveFunctionDescriptor(root, scope, function, KotlinTestUtils.DUMMY_TRACE, DataFlowInfo.EMPTY);
} }
} }
@@ -201,7 +201,7 @@ public class TypeUnifierTest extends KotlinLiteFixture {
} }
private TypeProjection makeTypeProjection(KtScope scope, String typeStr) { private TypeProjection makeTypeProjection(KtScope scope, String typeStr) {
LexicalScope withX = new LexicalScopeImpl(ScopeUtilsKt.memberScopeAsImportingScope(scope), scope.getContainingDeclaration(), LexicalScope withX = new LexicalScopeImpl(TypeTestUtilsKt.asLexicalScope(scope), scope.getContainingDeclaration(),
false, null, "With X", RedeclarationHandler.DO_NOTHING, false, null, "With X", RedeclarationHandler.DO_NOTHING,
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() { new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override @Override
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.types package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.resolve.scopes.BaseLexicalScope import org.jetbrains.kotlin.resolve.scopes.BaseLexicalScope
import org.jetbrains.kotlin.resolve.scopes.KtScope import org.jetbrains.kotlin.resolve.scopes.KtScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope
@@ -25,11 +24,9 @@ import org.jetbrains.kotlin.utils.Printer
public fun KtScope.asLexicalScope(): LexicalScope { public fun KtScope.asLexicalScope(): LexicalScope {
val importingScope = memberScopeAsImportingScope() val importingScope = memberScopeAsImportingScope()
return object : BaseLexicalScope(importingScope) { return object : BaseLexicalScope(importingScope, getContainingDeclaration()) {
override val ownerDescriptor: DeclarationDescriptor
get() = importingScope.ownerDescriptor
override fun printStructure(p: Printer) { override fun printStructure(p: Printer) {
p.println("Util scope for tests")
} }
} }
} }
@@ -30,10 +30,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.getDataFlowInfo import org.jetbrains.kotlin.resolve.bindingContextUtil.getDataFlowInfo
import org.jetbrains.kotlin.resolve.calls.smartcasts.SmartCastManager import org.jetbrains.kotlin.resolve.calls.smartcasts.SmartCastManager
import org.jetbrains.kotlin.resolve.isAnnotatedAsHidden import org.jetbrains.kotlin.resolve.isAnnotatedAsHidden
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindExclude import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.getDescriptorsFiltered
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.resolve.scopes.utils.collectDescriptorsFiltered import org.jetbrains.kotlin.resolve.scopes.utils.collectDescriptorsFiltered
import org.jetbrains.kotlin.resolve.scopes.utils.collectSyntheticExtensionFunctions import org.jetbrains.kotlin.resolve.scopes.utils.collectSyntheticExtensionFunctions
@@ -299,7 +296,7 @@ class ReferenceVariantsHelper(
} }
private fun MutableSet<DeclarationDescriptor>.addNonExtensionCallablesAndConstructors( private fun MutableSet<DeclarationDescriptor>.addNonExtensionCallablesAndConstructors(
scope: LexicalScope, scope: HierarchicalScope,
kindFilter: DescriptorKindFilter, kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean, nameFilter: (Name) -> Boolean,
constructorFilter: (ClassDescriptor) -> Boolean constructorFilter: (ClassDescriptor) -> Boolean
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.FunctionDescriptorUtil import org.jetbrains.kotlin.resolve.FunctionDescriptorUtil
import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.collectDescriptorsFiltered import org.jetbrains.kotlin.resolve.scopes.utils.collectDescriptorsFiltered
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope
import org.jetbrains.kotlin.resolve.source.PsiSourceElement import org.jetbrains.kotlin.resolve.source.PsiSourceElement
@@ -148,10 +149,10 @@ private fun getClassInnerScope(outerScope: LexicalScope, descriptor: ClassDescri
public fun getResolutionScope(resolutionFacade: ResolutionFacade, descriptor: DeclarationDescriptor): LexicalScope { public fun getResolutionScope(resolutionFacade: ResolutionFacade, descriptor: DeclarationDescriptor): LexicalScope {
return when (descriptor) { return when (descriptor) {
is PackageFragmentDescriptor -> is PackageFragmentDescriptor ->
getPackageInnerScope(descriptor).memberScopeAsImportingScope() getPackageInnerScope(descriptor).memberScopeAsImportingScope().asLexicalScope(descriptor)
is PackageViewDescriptor -> is PackageViewDescriptor ->
descriptor.memberScope.memberScopeAsImportingScope() descriptor.memberScope.memberScopeAsImportingScope().asLexicalScope(descriptor)
is ClassDescriptor -> is ClassDescriptor ->
getClassInnerScope(getOuterScope(descriptor, resolutionFacade), descriptor) getClassInnerScope(getOuterScope(descriptor, resolutionFacade), descriptor)
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.resolve.BindingTraceContext
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.TypeResolver import org.jetbrains.kotlin.resolve.TypeResolver
import org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl import org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl
import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope
import org.jetbrains.kotlin.types.IndexedParametersSubstitution import org.jetbrains.kotlin.types.IndexedParametersSubstitution
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
@@ -76,7 +77,7 @@ public class HeuristicSignatures(
private fun typeFromText(text: String, typeParameters: Collection<TypeParameterDescriptor>): KotlinType { private fun typeFromText(text: String, typeParameters: Collection<TypeParameterDescriptor>): KotlinType {
val typeRef = KtPsiFactory(project).createType(text) val typeRef = KtPsiFactory(project).createType(text)
val rootPackagesScope = SubpackagesScope(moduleDescriptor, FqName.ROOT).memberScopeAsImportingScope() val rootPackagesScope = SubpackagesScope(moduleDescriptor, FqName.ROOT).memberScopeAsImportingScope().asLexicalScope(moduleDescriptor)
val scope = LexicalScopeImpl(rootPackagesScope, moduleDescriptor, false, null, "Root packages + type parameters") { val scope = LexicalScopeImpl(rootPackagesScope, moduleDescriptor, false, null, "Root packages + type parameters") {
typeParameters.forEach { addClassifierDescriptor(it) } typeParameters.forEach { addClassifierDescriptor(it) }
} }
@@ -39,7 +39,7 @@ import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.ImportPath import org.jetbrains.kotlin.resolve.ImportPath
import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor
import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.HierarchicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.* import org.jetbrains.kotlin.resolve.scopes.utils.*
import java.util.* import java.util.*
@@ -118,7 +118,7 @@ public class KotlinImportOptimizer() : ImportOptimizer {
private fun isAccessibleAsMember(target: DeclarationDescriptor, place: KtElement, bindingContext: BindingContext): Boolean { private fun isAccessibleAsMember(target: DeclarationDescriptor, place: KtElement, bindingContext: BindingContext): Boolean {
if (target.containingDeclaration !is ClassDescriptor) return false if (target.containingDeclaration !is ClassDescriptor) return false
fun isInScope(scope: LexicalScope): Boolean { fun isInScope(scope: HierarchicalScope): Boolean {
return when (target) { return when (target) {
is FunctionDescriptor -> is FunctionDescriptor ->
scope.findFunction(target.name, NoLookupLocation.FROM_IDE) { it == target } != null scope.findFunction(target.name, NoLookupLocation.FROM_IDE) { it == target } != null
@@ -66,8 +66,10 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsLexicalScope
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeProjectionImpl import org.jetbrains.kotlin.types.TypeProjectionImpl
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
@@ -85,7 +87,7 @@ private val ATTRIBUTE_FUNCTION_NAME = "FUNCTION_NAME"
/** /**
* Represents a single choice for a type (e.g. parameter type or return type). * Represents a single choice for a type (e.g. parameter type or return type).
*/ */
class TypeCandidate(val theType: KotlinType, scope: LexicalScope? = null) { class TypeCandidate(val theType: KotlinType, scope: HierarchicalScope? = null) {
public val typeParameters: Array<TypeParameterDescriptor> public val typeParameters: Array<TypeParameterDescriptor>
var renderedType: String? = null var renderedType: String? = null
private set private set
@@ -162,7 +164,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
private fun computeTypeCandidates( private fun computeTypeCandidates(
typeInfo: TypeInfo, typeInfo: TypeInfo,
substitutions: List<JetTypeSubstitution>, substitutions: List<JetTypeSubstitution>,
scope: LexicalScope): List<TypeCandidate> { scope: HierarchicalScope): List<TypeCandidate> {
if (!typeInfo.substitutionsAllowed) return computeTypeCandidates(typeInfo) if (!typeInfo.substitutionsAllowed) return computeTypeCandidates(typeInfo)
return typeCandidates.getOrPut(typeInfo) { return typeCandidates.getOrPut(typeInfo) {
val types = typeInfo.getPossibleTypes(this).asReversed() val types = typeInfo.getPossibleTypes(this).asReversed()
@@ -337,7 +339,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
mandatoryTypeParametersAsCandidates.forEach { it.render(typeParameterNameMap, fakeFunction) } mandatoryTypeParametersAsCandidates.forEach { it.render(typeParameterNameMap, fakeFunction) }
} }
private fun getDeclarationScope(): LexicalScope { private fun getDeclarationScope(): HierarchicalScope {
if (config.isExtension || receiverClassDescriptor == null) { if (config.isExtension || receiverClassDescriptor == null) {
return currentFileModule.getPackage(config.currentFile.packageFqName).memberScope.memberScopeAsImportingScope() return currentFileModule.getPackage(config.currentFile.packageFqName).memberScope.memberScopeAsImportingScope()
} }
@@ -352,7 +354,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
.map { TypeProjectionImpl(it.getDefaultType()) } .map { TypeProjectionImpl(it.getDefaultType()) }
val memberScope = receiverClassDescriptor.getMemberScope(projections) val memberScope = receiverClassDescriptor.getMemberScope(projections)
return LexicalScopeImpl(memberScope.memberScopeAsImportingScope(), receiverClassDescriptor, false, null, return LexicalScopeImpl(memberScope.memberScopeAsLexicalScope(), receiverClassDescriptor, false, null,
"Scope with type parameters for ${receiverClassDescriptor.getName()}") { "Scope with type parameters for ${receiverClassDescriptor.getName()}") {
receiverClassDescriptor.typeConstructor.parameters.forEach { addClassifierDescriptor(it) } receiverClassDescriptor.typeConstructor.parameters.forEach { addClassifierDescriptor(it) }
} }
@@ -380,7 +382,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
} }
} }
private fun createFakeFunctionDescriptor(scope: LexicalScope, typeParameterCount: Int): FunctionDescriptor { private fun createFakeFunctionDescriptor(scope: HierarchicalScope, typeParameterCount: Int): FunctionDescriptor {
val fakeFunction = SimpleFunctionDescriptorImpl.create( val fakeFunction = SimpleFunctionDescriptorImpl.create(
MutablePackageFragmentDescriptor(currentFileModule, FqName("fake")), MutablePackageFragmentDescriptor(currentFileModule, FqName("fake")),
Annotations.EMPTY, Annotations.EMPTY,
@@ -602,7 +604,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
else classBody.addAfter(declaration, classBody.lBrace!!) as KtNamedDeclaration else classBody.addAfter(declaration, classBody.lBrace!!) as KtNamedDeclaration
} }
private fun getTypeParameterRenames(scope: LexicalScope): Map<TypeParameterDescriptor, String> { private fun getTypeParameterRenames(scope: HierarchicalScope): Map<TypeParameterDescriptor, String> {
val allTypeParametersNotInScope = LinkedHashSet<TypeParameterDescriptor>() val allTypeParametersNotInScope = LinkedHashSet<TypeParameterDescriptor>()
mandatoryTypeParametersAsCandidates.asSequence() mandatoryTypeParametersAsCandidates.asSequence()
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass
import org.jetbrains.kotlin.resolve.scopes.HierarchicalScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
@@ -80,7 +81,7 @@ private fun KotlinType.render(typeParameterNameMap: Map<TypeParameterDescriptor,
internal fun KotlinType.renderShort(typeParameterNameMap: Map<TypeParameterDescriptor, String>) = render(typeParameterNameMap, false) internal fun KotlinType.renderShort(typeParameterNameMap: Map<TypeParameterDescriptor, String>) = render(typeParameterNameMap, false)
internal fun KotlinType.renderLong(typeParameterNameMap: Map<TypeParameterDescriptor, String>) = render(typeParameterNameMap, true) internal fun KotlinType.renderLong(typeParameterNameMap: Map<TypeParameterDescriptor, String>) = render(typeParameterNameMap, true)
internal fun getTypeParameterNamesNotInScope(typeParameters: Collection<TypeParameterDescriptor>, scope: LexicalScope): List<TypeParameterDescriptor> { internal fun getTypeParameterNamesNotInScope(typeParameters: Collection<TypeParameterDescriptor>, scope: HierarchicalScope): List<TypeParameterDescriptor> {
return typeParameters.filter { typeParameter -> return typeParameters.filter { typeParameter ->
val classifier = scope.findClassifier(typeParameter.name, NoLookupLocation.FROM_IDE) val classifier = scope.findClassifier(typeParameter.name, NoLookupLocation.FROM_IDE)
classifier == null || classifier != typeParameter classifier == null || classifier != typeParameter
@@ -45,6 +45,7 @@ import org.jetbrains.kotlin.resolve.lazy.FileScopeProvider
import org.jetbrains.kotlin.resolve.lazy.descriptors.ClassResolutionScopesSupport import org.jetbrains.kotlin.resolve.lazy.descriptors.ClassResolutionScopesSupport
import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.resolve.scopes.receivers.ThisReceiver import org.jetbrains.kotlin.resolve.scopes.receivers.ThisReceiver
import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.chainImportingScopes import org.jetbrains.kotlin.resolve.scopes.utils.chainImportingScopes
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope
import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.LockBasedStorageManager
@@ -245,7 +246,7 @@ object ReplaceWithAnnotationAnalyzer {
} }
is PackageViewDescriptor -> is PackageViewDescriptor ->
chainImportingScopes(listOf(descriptor.memberScope.memberScopeAsImportingScope()) + additionalScopes) chainImportingScopes(listOf(descriptor.memberScope.memberScopeAsImportingScope()) + additionalScopes).asLexicalScope(ownerDescriptor)
is ClassDescriptor -> { is ClassDescriptor -> {
val outerScope = getResolutionScope(descriptor.containingDeclaration, ownerDescriptor, additionalScopes) ?: return null val outerScope = getResolutionScope(descriptor.containingDeclaration, ownerDescriptor, additionalScopes) ?: return null
@@ -43,6 +43,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.OverrideResolver import org.jetbrains.kotlin.resolve.OverrideResolver
import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope
import java.util.* import java.util.*
@@ -194,7 +195,7 @@ fun DeclarationDescriptor.getContainingScope(): LexicalScope? {
val containingDescriptor = getContainingDeclaration() ?: return null val containingDescriptor = getContainingDeclaration() ?: return null
return when (containingDescriptor) { return when (containingDescriptor) {
is ClassDescriptorWithResolutionScopes -> containingDescriptor.getScopeForInitializerResolution() is ClassDescriptorWithResolutionScopes -> containingDescriptor.getScopeForInitializerResolution()
is PackageFragmentDescriptor -> containingDescriptor.getMemberScope().memberScopeAsImportingScope() is PackageFragmentDescriptor -> containingDescriptor.getMemberScope().memberScopeAsImportingScope().asLexicalScope(this)
else -> null else -> null
} }
} }