From 2ca89e49830c2308ddaafd3503afc8fa85ad4548 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Fri, 28 Oct 2022 10:33:35 +0200 Subject: [PATCH] [Analysis API FE1.0] fix constructors in scopes --- .../components/KtFe10ScopeProvider.kt | 23 +++++++++------ .../descriptors/scopes/KtFe10PackageScope.kt | 4 +-- .../scopes/KtFe10ScopeResolution.kt | 28 +++++++++++-------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10ScopeProvider.kt b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10ScopeProvider.kt index 84fc3b5fe61..7d8383d0a4a 100644 --- a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10ScopeProvider.kt +++ b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10ScopeProvider.kt @@ -27,7 +27,6 @@ import org.jetbrains.kotlin.analysis.api.descriptors.types.base.KtFe10Type import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtCompositeScope import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtEmptyScope import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken -import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion import org.jetbrains.kotlin.analysis.api.scopes.KtScope import org.jetbrains.kotlin.analysis.api.scopes.KtTypeScope import org.jetbrains.kotlin.analysis.api.symbols.KtFileSymbol @@ -63,21 +62,21 @@ internal class KtFe10ScopeProvider( val descriptor = getDescriptor(classSymbol) ?: return getEmptyScope() - return KtFe10ScopeMember(descriptor.unsubstitutedMemberScope, analysisContext) + return KtFe10ScopeMember(descriptor.unsubstitutedMemberScope, descriptor.constructors, analysisContext) } override fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope { val descriptor = getDescriptor(classSymbol) ?: return getEmptyScope() - return KtFe10ScopeMember(DeclaredMemberScope(descriptor), analysisContext) + return KtFe10ScopeMember(DeclaredMemberScope(descriptor), descriptor.constructors, analysisContext) } override fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtScope { val descriptor = getDescriptor(classSymbol) ?: return getEmptyScope() - return KtFe10ScopeMember(DeclaredMemberScope(descriptor, forDelegatedMembersOnly = true), analysisContext) + return KtFe10ScopeMember(DeclaredMemberScope(descriptor, forDelegatedMembersOnly = true), emptyList(), analysisContext) } private class DeclaredMemberScope( @@ -90,13 +89,13 @@ internal class KtFe10ScopeProvider( override fun getContributedVariables(name: Name, location: LookupLocation): Collection { return allMemberScope.getContributedVariables(name, location).filter { - it.containingDeclaration == owner && it.isDelegatedIfRequired() + it.isDeclaredInOwner() && it.isDelegatedIfRequired() }.mapToDelegatedIfRequired() } override fun getContributedFunctions(name: Name, location: LookupLocation): Collection { return allMemberScope.getContributedFunctions(name, location).filter { - it.containingDeclaration == owner && it.isDelegatedIfRequired() + it.isDeclaredInOwner() && it.isDelegatedIfRequired() }.mapToDelegatedIfRequired() } @@ -125,7 +124,7 @@ internal class KtFe10ScopeProvider( override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? { if (forDelegatedMembersOnly) return null - return allMemberScope.getContributedClassifier(name, location)?.takeIf { it.containingDeclaration == owner } + return allMemberScope.getContributedClassifier(name, location)?.takeIf { it.isDeclaredInOwner() } } override fun getContributedDescriptors( @@ -133,7 +132,7 @@ internal class KtFe10ScopeProvider( nameFilter: (Name) -> Boolean ): Collection { return allMemberScope.getContributedDescriptors(kindFilter, nameFilter).filter { - it.containingDeclaration == owner && it.isDelegatedIfRequired() + it.isDeclaredInOwner() && it.isDelegatedIfRequired() }.mapToDelegatedIfRequired() } @@ -153,11 +152,17 @@ internal class KtFe10ScopeProvider( } } + + private fun DeclarationDescriptor.isDeclaredInOwner() = when (this) { + is CallableDescriptor -> dispatchReceiverParameter?.containingDeclaration == owner + else -> containingDeclaration == owner + } } + override fun getStaticMemberScope(symbol: KtSymbolWithMembers): KtScope { val descriptor = getDescriptor(symbol) ?: return getEmptyScope() - return KtFe10ScopeMember(descriptor.staticScope, analysisContext) + return KtFe10ScopeMember(descriptor.staticScope, emptyList(), analysisContext) } override fun getEmptyScope(): KtScope { diff --git a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/scopes/KtFe10PackageScope.kt b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/scopes/KtFe10PackageScope.kt index 49c9aad326f..2a0423c6325 100644 --- a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/scopes/KtFe10PackageScope.kt +++ b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/scopes/KtFe10PackageScope.kt @@ -7,17 +7,17 @@ package org.jetbrains.kotlin.analysis.api.descriptors.scopes import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext import org.jetbrains.kotlin.analysis.api.descriptors.symbols.KtFe10PackageSymbol +import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter import org.jetbrains.kotlin.analysis.api.symbols.KtConstructorSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol -import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion import org.jetbrains.kotlin.resolve.scopes.MemberScope internal class KtFe10PackageScope( scope: MemberScope, private val owner: KtPackageSymbol, analysisContext: Fe10AnalysisContext -) : KtFe10ScopeMember(scope, analysisContext) { +) : KtFe10ScopeMember(scope, constructors = emptyList(), analysisContext) { override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence = withValidityAssertion { val packageFragmentProvider = analysisContext.resolveSession.packageFragmentProvider return packageFragmentProvider.getSubPackagesOf(owner.fqName, nameFilter) diff --git a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/scopes/KtFe10ScopeResolution.kt b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/scopes/KtFe10ScopeResolution.kt index c1c2cf742da..b92596c5d2c 100644 --- a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/scopes/KtFe10ScopeResolution.kt +++ b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/scopes/KtFe10ScopeResolution.kt @@ -5,18 +5,18 @@ package org.jetbrains.kotlin.analysis.api.descriptors.scopes -import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeOwner import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtConstructorSymbol import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtSymbol +import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeOwner +import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken +import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion import org.jetbrains.kotlin.analysis.api.scopes.KtScope import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtClassifierSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtConstructorSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol -import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken -import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter @@ -44,14 +44,6 @@ internal abstract class KtFe10ScopeResolution : KtScope, KtLifetimeOwner { .mapNotNull { it.toKtSymbol(analysisContext) as? KtClassifierSymbol } } - override fun getConstructors(): Sequence = withValidityAssertion { - return scope - .getContributedDescriptors(kindFilter = DescriptorKindFilter.FUNCTIONS) - .asSequence() - .filterIsInstance() - .map { it.toKtConstructorSymbol(analysisContext) } - } - override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence = withValidityAssertion { emptySequence() } @@ -71,10 +63,20 @@ internal class KtFe10ScopeLexical( override fun getPossibleClassifierNames(): Set = withValidityAssertion { return emptySet() } + + override fun getConstructors(): Sequence = withValidityAssertion { + return scope + .getContributedDescriptors(kindFilter = DescriptorKindFilter.FUNCTIONS) + .asSequence() + .filterIsInstance() + .map { it.toKtConstructorSymbol(analysisContext) } + } + } internal open class KtFe10ScopeMember( override val scope: MemberScope, + private val constructors: Collection, override val analysisContext: Fe10AnalysisContext ) : KtFe10ScopeResolution() { override fun getPossibleCallableNames(): Set = withValidityAssertion { @@ -84,4 +86,8 @@ internal open class KtFe10ScopeMember( override fun getPossibleClassifierNames(): Set = withValidityAssertion { return scope.getClassifierNames() ?: emptySet() } + + override fun getConstructors(): Sequence = sequence { + constructors.forEach { yield(it.toKtConstructorSymbol(analysisContext)) } + } } \ No newline at end of file