diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyTopDownAnalyzerForTopLevel.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyTopDownAnalyzerForTopLevel.java index 011c8ac8b60..bb3d107d13f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyTopDownAnalyzerForTopLevel.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyTopDownAnalyzerForTopLevel.java @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.psi.KtFile; import org.jetbrains.kotlin.psi.KtScript; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.lazy.KotlinCodeAnalyzer; -import org.jetbrains.kotlin.resolve.lazy.LazyFileScope; +import org.jetbrains.kotlin.resolve.lazy.LazyImportingScope; import java.util.Arrays; import java.util.Collection; @@ -89,7 +89,7 @@ public class LazyTopDownAnalyzerForTopLevel { } private static void resolveAndCheckImports(@NotNull KtFile file, @NotNull KotlinCodeAnalyzer resolveSession) { - LazyFileScope fileScope = resolveSession.getFileScopeProvider().getFileScope(file); + LazyImportingScope fileScope = resolveSession.getFileScopeProvider().getFileScope(file); fileScope.forceResolveAllImports(); } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeProvider.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeProvider.kt index 3cc70c0cd96..832af41989e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeProvider.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeProvider.kt @@ -20,10 +20,10 @@ import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.scopes.KtScope public interface FileScopeProvider { - fun getFileScope(file: KtFile): LazyFileScope + fun getFileScope(file: KtFile): LazyImportingScope public object ThrowException : FileScopeProvider { - override fun getFileScope(file: KtFile): LazyFileScope { + override fun getFileScope(file: KtFile): LazyImportingScope { throw UnsupportedOperationException("Should not be called") } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeProviderImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeProviderImpl.kt index bebaf569741..ff84c84edf0 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeProviderImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/FileScopeProviderImpl.kt @@ -51,7 +51,7 @@ public class FileScopeProviderImpl( override fun getFileScope(file: KtFile) = fileScopes(file) - private fun createFileScope(file: KtFile): LazyFileScope { + private fun createFileScope(file: KtFile): LazyImportingScope { val debugName = "LazyFileScope for file " + file.getName() val tempTrace = TemporaryBindingTrace.create(bindingTrace, "Transient trace for default imports lazy resolve") @@ -89,7 +89,7 @@ public class FileScopeProviderImpl( scopeChain.add(LazyImportScope(packageFragment, allUnderImportResolver, LazyImportScope.FilteringKind.INVISIBLE_CLASSES, "All under imports in $debugName (invisible classes only)")) scopeChain.add(LazyImportScope(packageFragment, defaultAllUnderImportResolver, LazyImportScope.FilteringKind.INVISIBLE_CLASSES, "Default all under imports in $debugName (invisible classes only)")) - val lazyFileScope = LazyFileScope(scopeChain, aliasImportResolver, allUnderImportResolver, packageFragment, debugName) + val lazyFileScope = LazyImportingScope(scopeChain, aliasImportResolver, allUnderImportResolver, packageFragment, debugName) bindingTrace.recordScope(lazyFileScope, file) return lazyFileScope } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/LazyFileScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/LazyImportingScope.kt similarity index 93% rename from compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/LazyFileScope.kt rename to compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/LazyImportingScope.kt index 86b5bd51c4b..b68ee3bcc1c 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/LazyFileScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/LazyImportingScope.kt @@ -22,17 +22,20 @@ import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.KtImportDirective import org.jetbrains.kotlin.resolve.scopes.ChainedScope -import org.jetbrains.kotlin.resolve.scopes.FileScope +import org.jetbrains.kotlin.resolve.scopes.ImportingScope import org.jetbrains.kotlin.resolve.scopes.KtScope import org.jetbrains.kotlin.utils.Printer -class LazyFileScope( +class LazyImportingScope( scopeChain: List, private val aliasImportResolver: LazyImportResolver, private val allUnderImportResolver: LazyImportResolver, containingDeclaration: PackageFragmentDescriptor, debugName: String -) : ChainedScope(containingDeclaration, debugName, *scopeChain.toTypedArray()), FileScope { +) : ChainedScope(containingDeclaration, debugName, *scopeChain.toTypedArray()), ImportingScope { + override val parent: ImportingScope? + get() = null + override fun getDeclaredDescriptors() = emptyList() override fun printStructure(p: Printer) = printScopeStructure(p) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/ResolveSession.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/ResolveSession.java index e75359fd72a..dab9cc39e7e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/ResolveSession.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/ResolveSession.java @@ -207,7 +207,7 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext { } private LazyAnnotations createAnnotations(KtFile file, List annotationEntries) { - LazyFileScope scope = fileScopeProvider.getFileScope(file); + LazyImportingScope scope = fileScopeProvider.getFileScope(file); LazyAnnotationsContextImpl lazyAnnotationContext = new LazyAnnotationsContextImpl(annotationResolve, storageManager, trace, scope); return new LazyAnnotations(lazyAnnotationContext, annotationEntries); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt index b17976b2061..ea2281d74ef 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt @@ -21,21 +21,21 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.lazy.LazyFileScope +import org.jetbrains.kotlin.resolve.lazy.LazyImportingScope import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.util.collectionUtils.concat import org.jetbrains.kotlin.utils.Printer -public fun LexicalScope.getFileScope(): FileScope { +public fun LexicalScope.getFileScope(): ImportingScope { var currentScope = this while(currentScope.parent != null) { currentScope = currentScope.parent!! } - assert(currentScope is FileScope) { + assert(currentScope is ImportingScope) { "Not FileScope without parent: $currentScope" // todo improve debug message } - return currentScope as FileScope + return currentScope as ImportingScope } /** @@ -45,7 +45,7 @@ public fun LexicalScope.getImplicitReceiversHierarchy(): List = collectAllFromMeAndParent { - if(it is MemberScopeToFileScopeAdapter) { // todo remove this hack + if(it is MemberScopeToImportingScopeAdapter) { // todo remove this hack it.memberScope.getDeclarationsByLabel(labelName) } else if (it.isOwnerDescriptorAccessibleByLabel && it.ownerDescriptor.name == labelName) { @@ -78,7 +78,7 @@ public fun LexicalScope.getDescriptorsFiltered( ): Collection { if (kindFilter.kindMask == 0) return listOf() return collectAllFromMeAndParent { - if (it is FileScope) { + if (it is ImportingScope) { it.getDescriptors(kindFilter, nameFilter) } else { it.getDeclaredDescriptors() @@ -93,13 +93,13 @@ public fun LexicalScope.getLocalVariable(name: Name): VariableDescriptor? { if (it is LexicalScopeWrapper) { return it.delegate.getLocalVariable(name) } - else if (it is LazyFileScope) { + else if (it is LazyImportingScope) { return it.getLocalVariable(name) // todo: remove hack for repl interpreter } - else if (it is MemberScopeToFileScopeAdapter) { // todo remove hack + else if (it is MemberScopeToImportingScopeAdapter) { // todo remove hack return it.memberScope.getLocalVariable(name) } - else if (it !is FileScope && it !is LexicalChainedScope) { // todo check this + else if (it !is ImportingScope && it !is LexicalChainedScope) { // todo check this it.getDeclaredVariables(name, NoLookupLocation.UNSORTED).singleOrNull()?.let { return it } } } @@ -117,11 +117,11 @@ public fun LexicalScope.takeSnapshot(): LexicalScope = if (this is LexicalWritab public fun LexicalScope.asJetScope(): KtScope { if (this is KtScope) return this - if (this is MemberScopeToFileScopeAdapter) return this.memberScope + if (this is MemberScopeToImportingScopeAdapter) return this.memberScope return LexicalToJetScopeAdapter(this) } -public fun KtScope.memberScopeAsFileScope(): FileScope = MemberScopeToFileScopeAdapter(this) +public fun KtScope.memberScopeAsFileScope(): ImportingScope = MemberScopeToImportingScopeAdapter(this) @Deprecated("Remove this method after scope refactoring") public fun KtScope.asLexicalScope(): LexicalScope @@ -141,7 +141,7 @@ private class LexicalToJetScopeAdapter(lexicalScope: LexicalScope): KtScope { override fun getProperties(name: Name, location: LookupLocation): Collection { val fileScope = lexicalScope.getFileScope() - if (fileScope is MemberScopeToFileScopeAdapter) { + if (fileScope is MemberScopeToImportingScopeAdapter) { return fileScope.memberScope.getProperties(name, location) } else { @@ -172,7 +172,7 @@ private class LexicalToJetScopeAdapter(lexicalScope: LexicalScope): KtScope { override fun getDeclarationsByLabel(labelName: Name) = lexicalScope.getDeclarationsByLabel(labelName) override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) = lexicalScope.collectAllFromMeAndParent { - if (it is FileScope) { + if (it is ImportingScope) { it.getDescriptors(kindFilter, nameFilter) } else it.getDeclaredDescriptors() } @@ -197,7 +197,10 @@ private class LexicalToJetScopeAdapter(lexicalScope: LexicalScope): KtScope { } } -private class MemberScopeToFileScopeAdapter(val memberScope: KtScope) : FileScope { +private class MemberScopeToImportingScopeAdapter(val memberScope: KtScope) : ImportingScope { + override val parent: ImportingScope? + get() = null + override fun getPackage(name: Name): PackageViewDescriptor? = memberScope.getPackage(name) override fun getSyntheticExtensionProperties(receiverTypes: Collection, name: Name, location: LookupLocation) @@ -226,7 +229,7 @@ private class MemberScopeToFileScopeAdapter(val memberScope: KtScope) : FileScop override fun getDeclaredFunctions(name: Name, location: LookupLocation) = memberScope.getFunctions(name, location) - override fun equals(other: Any?) = other is MemberScopeToFileScopeAdapter && other.memberScope == memberScope + override fun equals(other: Any?) = other is MemberScopeToImportingScopeAdapter && other.memberScope == memberScope override fun hashCode() = memberScope.hashCode() @@ -285,19 +288,19 @@ public fun LexicalScope.addImportScope(importScope: KtScope): LexicalScope { } public fun LexicalScope.replaceFileScope(fileScopeReplace: LexicalScope): LexicalScope { - if (this is FileScope) return fileScopeReplace + if (this is ImportingScope) return fileScopeReplace return LexicalScopeWrapper(this, fileScopeReplace) } -public fun LexicalScope.withNoFileScope(): LexicalScope = replaceFileScope(MemberScopeToFileScopeAdapter(KtScope.Empty)) +public fun LexicalScope.withNoFileScope(): LexicalScope = replaceFileScope(MemberScopeToImportingScopeAdapter(KtScope.Empty)) private class LexicalScopeWrapper(val delegate: LexicalScope, val fileScopeReplace: LexicalScope): LexicalScope by delegate { override val parent: LexicalScope? by lazy(LazyThreadSafetyMode.NONE) { - assert(delegate !is FileScope) { "We should replace FileScope($delegate) to $fileScopeReplace" } + assert(delegate !is ImportingScope) { "We should replace FileScope($delegate) to $fileScopeReplace" } val parent = delegate.parent!! - if (parent is FileScope) { + if (parent is ImportingScope) { fileScopeReplace } else { diff --git a/compiler/tests/org/jetbrains/kotlin/types/JetOverloadTest.java b/compiler/tests/org/jetbrains/kotlin/types/JetOverloadTest.java index 3878eb619b5..70cd76ad707 100644 --- a/compiler/tests/org/jetbrains/kotlin/types/JetOverloadTest.java +++ b/compiler/tests/org/jetbrains/kotlin/types/JetOverloadTest.java @@ -25,7 +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.FileScope; +import org.jetbrains.kotlin.resolve.scopes.ImportingScope; import org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt; import org.jetbrains.kotlin.test.ConfigurationKind; import org.jetbrains.kotlin.test.JetLiteFixture; @@ -171,7 +171,7 @@ public class JetOverloadTest extends JetLiteFixture { private FunctionDescriptor makeFunction(String funDecl) { KtNamedFunction function = KtPsiFactoryKt.KtPsiFactory(getProject()).createFunction(funDecl); - FileScope scope = ScopeUtilsKt.memberScopeAsFileScope(JvmPlatform.INSTANCE$.getBuiltIns().getBuiltInsPackageScope()); + ImportingScope scope = ScopeUtilsKt.memberScopeAsFileScope(JvmPlatform.INSTANCE$.getBuiltIns().getBuiltInsPackageScope()); return functionDescriptorResolver.resolveFunctionDescriptor(root, scope, function, JetTestUtils.DUMMY_TRACE, DataFlowInfo.EMPTY); } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/Scopes.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/Scopes.kt index 44cf060ea07..51a4f3f82df 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/Scopes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/Scopes.kt @@ -18,35 +18,47 @@ package org.jetbrains.kotlin.resolve.scopes import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.incremental.components.LookupLocation -import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.utils.Printer // see ScopeUtils.kt in the frontend module -public interface LexicalScope { - public val parent: LexicalScope? +interface LexicalScope { + val parent: LexicalScope? - public val ownerDescriptor: DeclarationDescriptor - public val isOwnerDescriptorAccessibleByLabel: Boolean + val ownerDescriptor: DeclarationDescriptor + val isOwnerDescriptorAccessibleByLabel: Boolean - public val implicitReceiver: ReceiverParameterDescriptor? + val implicitReceiver: ReceiverParameterDescriptor? - public fun getDeclaredDescriptors(): Collection + /** + * All visible descriptors from current scope possibly filtered by the given name and kind filters + * (that means that the implementation is not obliged to use the filters but may do so when it gives any performance advantage). + */ + open fun getDescriptors( + kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL, + nameFilter: (Name) -> Boolean = KtScope.ALL_NAME_FILTER + ): Collection = getDeclaredDescriptors() - public fun getDeclaredClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? + //TODO: rename to getDescriptors or getAllDescriptors + fun getDeclaredDescriptors(): Collection - // need collection here because there may be extension property foo and usual property foo - public fun getDeclaredVariables(name: Name, location: LookupLocation): Collection - public fun getDeclaredFunctions(name: Name, location: LookupLocation): Collection + //TODO: rename to getClassifier + fun getDeclaredClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? - public fun printStructure(p: Printer) + //TODO: rename to getVariables + fun getDeclaredVariables(name: Name, location: LookupLocation): Collection + + //TODO: rename to getFunctions + fun getDeclaredFunctions(name: Name, location: LookupLocation): Collection + + fun printStructure(p: Printer) } -public interface FileScope: LexicalScope { - override val parent: LexicalScope? - get() = null +// TODO: common base interface instead direct inheritance +interface ImportingScope : LexicalScope { + override val parent: ImportingScope? override val isOwnerDescriptorAccessibleByLabel: Boolean get() = false @@ -58,14 +70,16 @@ public interface FileScope: LexicalScope { fun getPackage(name: Name): PackageViewDescriptor? - public fun getSyntheticExtensionProperties(receiverTypes: Collection, name: Name, location: LookupLocation): Collection - public fun getSyntheticExtensionFunctions(receiverTypes: Collection, name: Name, location: LookupLocation): Collection + fun getSyntheticExtensionProperties(receiverTypes: Collection, name: Name, location: LookupLocation): Collection + fun getSyntheticExtensionFunctions(receiverTypes: Collection, name: Name, location: LookupLocation): Collection - public fun getSyntheticExtensionProperties(receiverTypes: Collection): Collection - public fun getSyntheticExtensionFunctions(receiverTypes: Collection): Collection + fun getSyntheticExtensionProperties(receiverTypes: Collection): Collection + fun getSyntheticExtensionFunctions(receiverTypes: Collection): Collection - public fun getDescriptors( - kindFilter: DescriptorKindFilter = DescriptorKindFilter.ALL, - nameFilter: (Name) -> Boolean = KtScope.ALL_NAME_FILTER - ): Collection + // please, do not override this method + override fun getDeclaredDescriptors(): Collection { + return getDescriptors() + } + + override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt index c0a5e180f63..b9a9aead01d 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/resolutionApi.kt @@ -35,7 +35,7 @@ import org.jetbrains.kotlin.resolve.ImportPath import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.lazy.FileScopeProvider -import org.jetbrains.kotlin.resolve.lazy.LazyFileScope +import org.jetbrains.kotlin.resolve.lazy.LazyImportingScope public fun KtElement.getResolutionFacade(): ResolutionFacade { return KotlinCacheService.getInstance(getProject()).getResolutionFacade(listOf(this)) @@ -102,6 +102,6 @@ public fun getResolveScope(file: KtFile): GlobalSearchScope { } } -public fun ResolutionFacade.getFileTopLevelScope(file: KtFile): LazyFileScope { +public fun ResolutionFacade.getFileTopLevelScope(file: KtFile): LazyImportingScope { return frontendService().getFileScope(file) } diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ConflictingExtensionPropertyInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ConflictingExtensionPropertyInspection.kt index 43a72c5f611..d751bca3ca3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ConflictingExtensionPropertyInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ConflictingExtensionPropertyInspection.kt @@ -49,7 +49,7 @@ import org.jetbrains.kotlin.renderer.render import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess import org.jetbrains.kotlin.resolve.isAnnotatedAsHidden -import org.jetbrains.kotlin.resolve.scopes.FileScope +import org.jetbrains.kotlin.resolve.scopes.ImportingScope import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull @@ -86,9 +86,9 @@ public class ConflictingExtensionPropertyInspection : AbstractKotlinInspection() } } - private fun conflictingSyntheticExtension(descriptor: PropertyDescriptor, fileScope: FileScope): SyntheticJavaPropertyDescriptor? { + private fun conflictingSyntheticExtension(descriptor: PropertyDescriptor, importingScope: ImportingScope): SyntheticJavaPropertyDescriptor? { val extensionReceiverType = descriptor.extensionReceiverParameter?.type ?: return null - return fileScope + return importingScope .getSyntheticExtensionProperties(listOf(extensionReceiverType), descriptor.name, NoLookupLocation.FROM_IDE) .firstIsInstanceOrNull() }