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.name.Name
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.scopes.BaseImportingScope
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.resolve.scopes.utils.collectSyntheticExtensionProperties
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.*
@@ -49,7 +47,7 @@ interface SyntheticJavaPropertyDescriptor : PropertyDescriptor {
val setMethod: FunctionDescriptor?
companion object {
fun findByGetterOrSetter(getterOrSetter: FunctionDescriptor, resolutionScope: LexicalScope): SyntheticJavaPropertyDescriptor? {
fun findByGetterOrSetter(getterOrSetter: FunctionDescriptor, resolutionScope: HierarchicalScope): SyntheticJavaPropertyDescriptor? {
val name = getterOrSetter.getName()
if (name.isSpecial()) return null
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.receivers.ReceiverValue
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.types.FunctionPlaceholders
import org.jetbrains.kotlin.types.KotlinType
@@ -101,7 +101,7 @@ public fun resolvePossiblyAmbiguousCallableReference(
fun resolveInScope(traceTitle: String, staticScope: KtScope): OverloadResolutionResults<CallableDescriptor> {
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)
resolutionMode.acceptResolution(results, temporaryTraceAndCache)
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.util.FakeCallableDescriptorForObject
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.LexicalScope
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> {
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 } +
getConstructors(it.getContributedClassifier(name, location))
}
@@ -139,7 +139,7 @@ private object FunctionCollector : CallableDescriptorCollector<FunctionDescripto
}
private fun getConstructors(
scope: LexicalScope,
scope: HierarchicalScope,
name: Name,
location: LookupLocation,
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)
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.utils.getImplicitReceiversHierarchy
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.storage.StorageManager
import org.jetbrains.kotlin.types.ErrorUtils
@@ -69,7 +70,7 @@ public class TaskPrioritizer(
if (explicitReceiver is QualifierReceiver) {
val qualifierReceiver: QualifierReceiver = explicitReceiver
val receiverScope = qualifierReceiver.getNestedClassesAndPackageMembersScope().memberScopeAsImportingScope()
val receiverScope = qualifierReceiver.getNestedClassesAndPackageMembersScope().memberScopeAsLexicalScope()
doComputeTasks(NO_RECEIVER, taskPrioritizerContext.replaceScope(receiverScope))
computeTasksForClassObjectReceiver(qualifierReceiver, taskPrioritizerContext)
}
@@ -260,7 +261,7 @@ public class TaskPrioritizer(
val dynamicScope = dynamicCallableDescriptors.createDynamicDescriptorScope(c.context.call, c.scope.ownerDescriptor)
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)
@@ -281,7 +282,7 @@ public class TaskPrioritizer(
) {
c.result.addCandidates {
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)
}
}
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.resolve.lazy
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.impl.SubpackagesScope
import org.jetbrains.kotlin.name.FqName
@@ -84,10 +83,10 @@ public open class FileScopeProviderImpl(
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)")
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)")
for (additionalScope in additionalScopes.flatMap { it.scopes }) {
@@ -95,25 +94,22 @@ public open class FileScopeProviderImpl(
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)")
scope = LazyImportScope(scope, packageFragment, allUnderImportResolver, LazyImportScope.FilteringKind.VISIBLE_CLASSES,
scope = LazyImportScope(scope, allUnderImportResolver, LazyImportScope.FilteringKind.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")
scope = SubpackagesScope(moduleDescriptor, FqName.ROOT).memberScopeAsImportingScope(scope)
scope = NoSubpackagesInPackageScope(packageView).memberScopeAsImportingScope(scope) //TODO: problems with visibility too
scope = LazyImportScope(scope, packageFragment, aliasImportResolver, LazyImportScope.FilteringKind.ALL, "Alias imports in $debugName")
val lexicalScope = object : BaseLexicalScope(scope) {
override val ownerDescriptor: DeclarationDescriptor
get() = packageFragment
scope = LazyImportScope(scope, aliasImportResolver, LazyImportScope.FilteringKind.ALL, "Alias imports in $debugName")
val lexicalScope = object : BaseLexicalScope(scope, packageFragment) {
override fun printStructure(p: Printer) {
p.println("File top-level scope (empty)")
}
@@ -164,7 +164,6 @@ class LazyImportResolver(
class LazyImportScope(
override val parent: ImportingScope?,
override val ownerDescriptor: DeclarationDescriptor,
private val importResolver: LazyImportResolver,
private val filteringKind: LazyImportScope.FilteringKind,
private val debugName: String
@@ -258,8 +257,6 @@ class LazyImportScope(
p.println(javaClass.getSimpleName(), ": ", debugName, " {")
p.pushIndent()
p.println("ownerDescriptor = ", ownerDescriptor)
p.popIndent()
p.println("}")
}
@@ -94,7 +94,7 @@ class LexicalWritableScope(
}
private inner class Snapshot(val descriptorLimit: Int) : LexicalScope {
override val parent: LexicalScope?
override val parent: LexicalScope
get() = this@LexicalWritableScope.parent
override val ownerDescriptor: DeclarationDescriptor
get() = this@LexicalWritableScope.ownerDescriptor
@@ -24,13 +24,8 @@ import org.jetbrains.kotlin.utils.Printer
// see utils/ScopeUtils.kt
interface LexicalScope {
val parent: LexicalScope?
val ownerDescriptor: DeclarationDescriptor
val isOwnerDescriptorAccessibleByLabel: Boolean
val implicitReceiver: ReceiverParameterDescriptor?
interface HierarchicalScope {
val parent: HierarchicalScope?
/**
* 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)
}
interface LexicalScope: HierarchicalScope {
override val parent: HierarchicalScope
val ownerDescriptor: DeclarationDescriptor
val isOwnerDescriptorAccessibleByLabel: Boolean
val implicitReceiver: ReceiverParameterDescriptor?
}
// TODO: common base interface instead direct inheritance
interface ImportingScope : LexicalScope {
interface ImportingScope : HierarchicalScope {
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
fun getContributedPackage(name: Name): PackageViewDescriptor?
@@ -77,13 +75,7 @@ interface ImportingScope : LexicalScope {
}
}
abstract class BaseLexicalScope(override val parent: LexicalScope?) : LexicalScope {
override val isOwnerDescriptorAccessibleByLabel: Boolean
get() = false
override val implicitReceiver: ReceiverParameterDescriptor?
get() = null
abstract class BaseHierarchicalScope(override val parent: HierarchicalScope?) : HierarchicalScope {
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> = emptyList()
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()
}
abstract class BaseImportingScope(parent: ImportingScope?) : BaseLexicalScope(parent), ImportingScope {
override val parent: ImportingScope?
get() = super.parent as ImportingScope?
abstract class BaseLexicalScope(
parent: HierarchicalScope,
override val ownerDescriptor: DeclarationDescriptor
): BaseHierarchicalScope(parent), LexicalScope {
override val parent: HierarchicalScope
get() = super.parent!!
override val ownerDescriptor: DeclarationDescriptor
get() = throw UnsupportedOperationException()
override final val isOwnerDescriptorAccessibleByLabel: Boolean
override val isOwnerDescriptorAccessibleByLabel: Boolean
get() = false
override final val implicitReceiver: ReceiverParameterDescriptor?
override val implicitReceiver: ReceiverParameterDescriptor?
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
@@ -26,21 +26,21 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.util.collectionUtils.concat
import org.jetbrains.kotlin.utils.Printer
public val LexicalScope.parentsWithSelf: Sequence<LexicalScope>
public val HierarchicalScope.parentsWithSelf: Sequence<HierarchicalScope>
get() = sequence(this) { it.parent }
public val LexicalScope.parents: Sequence<LexicalScope>
public val HierarchicalScope.parents: Sequence<HierarchicalScope>
get() = parentsWithSelf.drop(1)
/**
* 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 {
it.implicitReceiver
(it as? LexicalScope)?.implicitReceiver
}
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)
}
else {
@@ -49,7 +49,7 @@ public fun LexicalScope.getDeclarationsByLabel(labelName: Name): Collection<Decl
}
// Result is guaranteed to be filtered by kind and name.
public fun LexicalScope.collectDescriptorsFiltered(
public fun HierarchicalScope.collectDescriptorsFiltered(
kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL,
nameFilter: (Name) -> Boolean = { true }
): 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) }
public fun LexicalScope.findPackage(name: Name): PackageViewDescriptor?
public fun HierarchicalScope.findPackage(name: Name): PackageViewDescriptor?
= 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) }
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) }
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 {
it.getContributedVariables(name, location).firstOrNull(predicate)?.let { return it }
}
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 {
it.getContributedFunctions(name, location).firstOrNull(predicate)?.let { return it }
}
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) }
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) }
public fun LexicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>)
public fun HierarchicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>)
= collectAllFromImportingScopes { it.getContributedSyntheticExtensionProperties(receiverTypes) }
public fun LexicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>)
public fun HierarchicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>)
= collectAllFromImportingScopes { it.getContributedSyntheticExtensionFunctions(receiverTypes) }
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
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 {
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)
= memberScope.getDescriptors(kindFilter, nameFilter)
override val ownerDescriptor: DeclarationDescriptor
get() = memberScope.getContainingDeclaration()
override fun getContributedClassifier(name: Name, location: LookupLocation) = memberScope.getClassifier(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
while (true) {
process(currentScope)
@@ -167,8 +167,8 @@ inline fun LexicalScope.processForMeAndParent(process: (LexicalScope) -> Unit) {
}
}
private inline fun <T: Any> LexicalScope.collectFromMeAndParent(
collect: (LexicalScope) -> T?
private inline fun <T: Any> HierarchicalScope.collectFromMeAndParent(
collect: (HierarchicalScope) -> T?
): List<T> {
var result: MutableList<T>? = null
processForMeAndParent {
@@ -183,39 +183,34 @@ private inline fun <T: Any> LexicalScope.collectFromMeAndParent(
return result ?: emptyList()
}
inline fun <T: Any> LexicalScope.collectAllFromMeAndParent(
collect: (LexicalScope) -> Collection<T>
inline fun <T: Any> HierarchicalScope.collectAllFromMeAndParent(
collect: (HierarchicalScope) -> Collection<T>
): Collection<T> {
var result: Collection<T>? = null
processForMeAndParent { result = result.concat(collect(it)) }
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 } }
return null
}
inline fun <T: Any> LexicalScope.collectAllFromImportingScopes(
inline fun <T: Any> HierarchicalScope.collectAllFromImportingScopes(
collect: (ImportingScope) -> Collection<T>
): Collection<T> {
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 }
}
fun LexicalScope.addImportingScopes(importScopes: List<ImportingScope>): LexicalScope {
if (this is ImportingScope) {
return chainImportingScopes(importScopes, this)!!
}
else {
val lastNonImporting = parentsWithSelf.last { it !is ImportingScope }
val firstImporting = lastNonImporting.parent as ImportingScope?
val newFirstImporting = chainImportingScopes(importScopes, firstImporting)
return LexicalScopeWrapper(this, newFirstImporting)
}
val lastLexicalScope = parentsWithSelf.last { it is LexicalScope }
val firstImporting = lastLexicalScope.parent as ImportingScope
val newFirstImporting = chainImportingScopes(importScopes, firstImporting)
return LexicalScopeWrapper(this, newFirstImporting!!)
}
fun LexicalScope.addImportingScope(importScope: ImportingScope): LexicalScope
@@ -229,22 +224,19 @@ fun ImportingScope.withParent(newParent: ImportingScope?): ImportingScope {
}
fun LexicalScope.replaceImportingScopes(importingScopeChain: ImportingScope?): LexicalScope {
return if (this is ImportingScope)
importingScopeChain ?: ImportingScope.Empty
else
LexicalScopeWrapper(this, importingScopeChain)
return LexicalScopeWrapper(this, importingScopeChain ?: ImportingScope.Empty)
}
private class LexicalScopeWrapper(val delegate: LexicalScope, val newImportingScopeChain: ImportingScope?): LexicalScope by delegate {
override val parent: LexicalScope? by lazy(LazyThreadSafetyMode.NONE) {
private class LexicalScopeWrapper(val delegate: LexicalScope, val newImportingScopeChain: ImportingScope): LexicalScope by delegate {
override val parent: HierarchicalScope by lazy(LazyThreadSafetyMode.NONE) {
assert(delegate !is ImportingScope)
val parent = delegate.parent
if (parent == null || parent is ImportingScope) {
newImportingScopeChain
if (parent is LexicalScope) {
LexicalScopeWrapper(parent, newImportingScopeChain)
}
else {
LexicalScopeWrapper(parent, newImportingScopeChain)
newImportingScopeChain
}
}
}
@@ -256,3 +248,10 @@ fun chainImportingScopes(scopes: List<ImportingScope>, tail: ImportingScope? = n
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.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(),
"Scope with implicit this for class: " + classDescriptor);
@@ -92,7 +92,7 @@ public class DefaultModalityModifiersTest extends KotlinLiteFixture {
assert aClass instanceof KtClass;
AnalysisResult bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(file);
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,
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override
@@ -25,8 +25,7 @@ import org.jetbrains.kotlin.resolve.FunctionDescriptorResolver;
import org.jetbrains.kotlin.resolve.OverloadUtil;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform;
import org.jetbrains.kotlin.resolve.scopes.ImportingScope;
import org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt;
import org.jetbrains.kotlin.resolve.scopes.LexicalScope;
import org.jetbrains.kotlin.test.ConfigurationKind;
import org.jetbrains.kotlin.test.KotlinLiteFixture;
import org.jetbrains.kotlin.test.KotlinTestUtils;
@@ -171,7 +170,7 @@ public class KotlinOverloadTest extends KotlinLiteFixture {
private FunctionDescriptor makeFunction(String 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);
}
}
@@ -201,7 +201,7 @@ public class TypeUnifierTest extends KotlinLiteFixture {
}
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,
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.resolve.scopes.BaseLexicalScope
import org.jetbrains.kotlin.resolve.scopes.KtScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
@@ -25,11 +24,9 @@ import org.jetbrains.kotlin.utils.Printer
public fun KtScope.asLexicalScope(): LexicalScope {
val importingScope = memberScopeAsImportingScope()
return object : BaseLexicalScope(importingScope) {
override val ownerDescriptor: DeclarationDescriptor
get() = importingScope.ownerDescriptor
return object : BaseLexicalScope(importingScope, getContainingDeclaration()) {
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.calls.smartcasts.SmartCastManager
import org.jetbrains.kotlin.resolve.isAnnotatedAsHidden
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindExclude
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.*
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.resolve.scopes.utils.collectDescriptorsFiltered
import org.jetbrains.kotlin.resolve.scopes.utils.collectSyntheticExtensionFunctions
@@ -299,7 +296,7 @@ class ReferenceVariantsHelper(
}
private fun MutableSet<DeclarationDescriptor>.addNonExtensionCallablesAndConstructors(
scope: LexicalScope,
scope: HierarchicalScope,
kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> 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.FunctionDescriptorUtil
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.memberScopeAsImportingScope
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 {
return when (descriptor) {
is PackageFragmentDescriptor ->
getPackageInnerScope(descriptor).memberScopeAsImportingScope()
getPackageInnerScope(descriptor).memberScopeAsImportingScope().asLexicalScope(descriptor)
is PackageViewDescriptor ->
descriptor.memberScope.memberScopeAsImportingScope()
descriptor.memberScope.memberScopeAsImportingScope().asLexicalScope(descriptor)
is ClassDescriptor ->
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.TypeResolver
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.types.IndexedParametersSubstitution
import org.jetbrains.kotlin.types.KotlinType
@@ -76,7 +77,7 @@ public class HeuristicSignatures(
private fun typeFromText(text: String, typeParameters: Collection<TypeParameterDescriptor>): KotlinType {
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") {
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.ImportPath
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 java.util.*
@@ -118,7 +118,7 @@ public class KotlinImportOptimizer() : ImportOptimizer {
private fun isAccessibleAsMember(target: DeclarationDescriptor, place: KtElement, bindingContext: BindingContext): Boolean {
if (target.containingDeclaration !is ClassDescriptor) return false
fun isInScope(scope: LexicalScope): Boolean {
fun isInScope(scope: HierarchicalScope): Boolean {
return when (target) {
is FunctionDescriptor ->
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.DescriptorUtils
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.memberScopeAsImportingScope
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsLexicalScope
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeProjectionImpl
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).
*/
class TypeCandidate(val theType: KotlinType, scope: LexicalScope? = null) {
class TypeCandidate(val theType: KotlinType, scope: HierarchicalScope? = null) {
public val typeParameters: Array<TypeParameterDescriptor>
var renderedType: String? = null
private set
@@ -162,7 +164,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
private fun computeTypeCandidates(
typeInfo: TypeInfo,
substitutions: List<JetTypeSubstitution>,
scope: LexicalScope): List<TypeCandidate> {
scope: HierarchicalScope): List<TypeCandidate> {
if (!typeInfo.substitutionsAllowed) return computeTypeCandidates(typeInfo)
return typeCandidates.getOrPut(typeInfo) {
val types = typeInfo.getPossibleTypes(this).asReversed()
@@ -337,7 +339,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
mandatoryTypeParametersAsCandidates.forEach { it.render(typeParameterNameMap, fakeFunction) }
}
private fun getDeclarationScope(): LexicalScope {
private fun getDeclarationScope(): HierarchicalScope {
if (config.isExtension || receiverClassDescriptor == null) {
return currentFileModule.getPackage(config.currentFile.packageFqName).memberScope.memberScopeAsImportingScope()
}
@@ -352,7 +354,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
.map { TypeProjectionImpl(it.getDefaultType()) }
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()}") {
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(
MutablePackageFragmentDescriptor(currentFileModule, FqName("fake")),
Annotations.EMPTY,
@@ -602,7 +604,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
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>()
mandatoryTypeParametersAsCandidates.asSequence()
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
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.utils.findClassifier
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.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 ->
val classifier = scope.findClassifier(typeParameter.name, NoLookupLocation.FROM_IDE)
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.scopes.*
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.memberScopeAsImportingScope
import org.jetbrains.kotlin.storage.LockBasedStorageManager
@@ -245,7 +246,7 @@ object ReplaceWithAnnotationAnalyzer {
}
is PackageViewDescriptor ->
chainImportingScopes(listOf(descriptor.memberScope.memberScopeAsImportingScope()) + additionalScopes)
chainImportingScopes(listOf(descriptor.memberScope.memberScopeAsImportingScope()) + additionalScopes).asLexicalScope(ownerDescriptor)
is ClassDescriptor -> {
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.OverrideResolver
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope
import java.util.*
@@ -194,7 +195,7 @@ fun DeclarationDescriptor.getContainingScope(): LexicalScope? {
val containingDescriptor = getContainingDeclaration() ?: return null
return when (containingDescriptor) {
is ClassDescriptorWithResolutionScopes -> containingDescriptor.getScopeForInitializerResolution()
is PackageFragmentDescriptor -> containingDescriptor.getMemberScope().memberScopeAsImportingScope()
is PackageFragmentDescriptor -> containingDescriptor.getMemberScope().memberScopeAsImportingScope().asLexicalScope(this)
else -> null
}
}