From 3da6ff7ec3b407cf052d0c0e27b1b2fa735689a8 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Tue, 2 Jun 2020 23:12:20 +0200 Subject: [PATCH] Optimize scopes handling inside LexicalChainedScope --- .../ClassResolutionScopesSupport.kt | 6 +++-- .../resolve/scopes/LexicalChainedScope.kt | 24 +++++++++++++++++-- .../kotlin/types/TypeSubstitutorTest.java | 9 +++---- .../kotlin/idea/kdoc/resolveKDocLink.kt | 4 ++-- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/ClassResolutionScopesSupport.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/ClassResolutionScopesSupport.kt index 9cf63411e38..086cd59bd95 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/ClassResolutionScopesSupport.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/ClassResolutionScopesSupport.kt @@ -99,12 +99,14 @@ class ClassResolutionScopesSupport( val parentForNewScope = companionObjectDescriptor?.packScopesOfCompanionSupertypes(parent, ownerDescriptor) ?: parent - val lexicalChainedScope = LexicalChainedScope( + val lexicalChainedScope = LexicalChainedScope.create( parentForNewScope, ownerDescriptor, isOwnerDescriptorAccessibleByLabel = false, implicitReceiver = companionObjectDescriptor?.thisAsReceiverParameter, kind = LexicalScopeKind.CLASS_INHERITANCE, - memberScopes = staticScopes, + classDescriptor.staticScope, + classDescriptor.unsubstitutedInnerClassesScope, + companionObjectDescriptor?.getStaticScopeOfCompanionObject(classDescriptor), isStaticScope = true ) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/scopes/LexicalChainedScope.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/scopes/LexicalChainedScope.kt index f85a76afd6f..983f0d01f9b 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/scopes/LexicalChainedScope.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/scopes/LexicalChainedScope.kt @@ -25,10 +25,11 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.scopes.utils.takeSnapshot import org.jetbrains.kotlin.util.collectionUtils.getFirstClassifierDiscriminateHeaders import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes +import org.jetbrains.kotlin.util.collectionUtils.listOfNonEmptyScopes import org.jetbrains.kotlin.utils.Printer import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult -class LexicalChainedScope @JvmOverloads constructor( +class LexicalChainedScope private constructor( parent: LexicalScope, override val ownerDescriptor: DeclarationDescriptor, override val isOwnerDescriptorAccessibleByLabel: Boolean, @@ -36,7 +37,7 @@ class LexicalChainedScope @JvmOverloads constructor( override val kind: LexicalScopeKind, // NB. Here can be very special subtypes of MemberScope (e.g., DeprecatedMemberScope). // Please, do not leak them outside of LexicalChainedScope, because other parts of compiler are not ready to work with them - private val memberScopes: List, + private val memberScopes: Array, @Deprecated("This value is temporary hack for resolve -- don't use it!") val isStaticScope: Boolean = false ) : LexicalScope { @@ -93,4 +94,23 @@ class LexicalChainedScope @JvmOverloads constructor( override fun definitelyDoesNotContainName(name: Name): Boolean { return memberScopes.all { it.definitelyDoesNotContainName(name) } } + + companion object { + + @JvmOverloads + fun create( + parent: LexicalScope, + ownerDescriptor: DeclarationDescriptor, + isOwnerDescriptorAccessibleByLabel: Boolean, + implicitReceiver: ReceiverParameterDescriptor?, + kind: LexicalScopeKind, + vararg memberScopes: MemberScope?, + isStaticScope: Boolean = false + ): LexicalScope = + LexicalChainedScope( + parent, ownerDescriptor, isOwnerDescriptorAccessibleByLabel, implicitReceiver, kind, + listOfNonEmptyScopes(*memberScopes).toTypedArray(), + isStaticScope + ) + } } diff --git a/compiler/tests/org/jetbrains/kotlin/types/TypeSubstitutorTest.java b/compiler/tests/org/jetbrains/kotlin/types/TypeSubstitutorTest.java index ee0799d628a..6ed9a1b88b5 100644 --- a/compiler/tests/org/jetbrains/kotlin/types/TypeSubstitutorTest.java +++ b/compiler/tests/org/jetbrains/kotlin/types/TypeSubstitutorTest.java @@ -48,7 +48,6 @@ import org.jetbrains.kotlin.tests.di.InjectionKt; import java.io.File; import java.io.IOException; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -100,12 +99,10 @@ public class TypeSubstitutorTest extends KotlinTestWithEnvironment { return Unit.INSTANCE; } ); - return new LexicalChainedScope( + return LexicalChainedScope.Companion.create( typeParameters, module, false, null, LexicalScopeKind.SYNTHETIC, - Arrays.asList( - contextClass.getDefaultType().getMemberScope(), - module.getBuiltIns().getBuiltInsPackageScope() - ) + contextClass.getDefaultType().getMemberScope(), + module.getBuiltIns().getBuiltInsPackageScope() ); } diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/kdoc/resolveKDocLink.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/kdoc/resolveKDocLink.kt index 20ef2a09f1c..d7b34ef244c 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/kdoc/resolveKDocLink.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/kdoc/resolveKDocLink.kt @@ -156,12 +156,12 @@ private fun getClassInnerScope(outerScope: LexicalScope, descriptor: ClassDescri descriptor.constructors.forEach { addFunctionDescriptor(it) } } - val scopeChain = listOfNotNull( + return LexicalChainedScope.create( + headerScope, descriptor, false, null, LexicalScopeKind.SYNTHETIC, descriptor.defaultType.memberScope, descriptor.staticScope, descriptor.companionObjectDescriptor?.defaultType?.memberScope ) - return LexicalChainedScope(headerScope, descriptor, false, null, LexicalScopeKind.SYNTHETIC, scopeChain) } fun getKDocLinkResolutionScope(resolutionFacade: ResolutionFacade, contextDescriptor: DeclarationDescriptor): LexicalScope {