Minor: use List instead of Array as container in chained scopes

This commit is contained in:
Zalim Bashorov
2015-12-15 18:41:04 +03:00
parent 5008a66a5b
commit 976fbf32ba
12 changed files with 36 additions and 35 deletions
@@ -130,7 +130,7 @@ public class IncrementalPackageFragmentProvider(
MemberScope.Empty MemberScope.Empty
} }
else { else {
ChainedMemberScope("Member scope for incremental compilation: union of package parts data", *scopes.toTypedArray()) ChainedMemberScope("Member scope for incremental compilation: union of package parts data", scopes)
} }
} }
} }
@@ -155,7 +155,7 @@ public class IncrementalPackageFragmentProvider(
val scopes = partsData.map { IncrementalPackageScope(JvmProtoBufUtil.readPackageDataFrom(it.data, it.strings)) } val scopes = partsData.map { IncrementalPackageScope(JvmProtoBufUtil.readPackageDataFrom(it.data, it.strings)) }
ChainedMemberScope( ChainedMemberScope(
"Member scope for incremental compilation: union of multifile class parts data for $multifileClassFqName", "Member scope for incremental compilation: union of multifile class parts data for $multifileClassFqName",
*scopes.toTypedArray<MemberScope>()) scopes)
} }
} }
@@ -141,7 +141,7 @@ class ClassResolutionScopesSupport(
return LexicalChainedScope(parentForNewScope, ownerDescriptor, false, return LexicalChainedScope(parentForNewScope, ownerDescriptor, false,
implicitReceiver, implicitReceiver,
LexicalScopeKind.CLASS_INHERITANCE, LexicalScopeKind.CLASS_INHERITANCE,
memberScopes = *staticScopes.toTypedArray(), isStaticScope = true) memberScopes = staticScopes, isStaticScope = true)
} }
private fun <T : Any> StorageManager.createLazyValue(onRecursion: ((Boolean) -> T), compute: () -> T) = private fun <T : Any> StorageManager.createLazyValue(onRecursion: ((Boolean) -> T), compute: () -> T) =
@@ -31,21 +31,20 @@ public class LexicalChainedScope @JvmOverloads constructor(
override val isOwnerDescriptorAccessibleByLabel: Boolean, override val isOwnerDescriptorAccessibleByLabel: Boolean,
override val implicitReceiver: ReceiverParameterDescriptor?, override val implicitReceiver: ReceiverParameterDescriptor?,
override val kind: LexicalScopeKind, override val kind: LexicalScopeKind,
vararg memberScopes: MemberScope, private val memberScopes: List<MemberScope>,
@Deprecated("This value is temporary hack for resolve -- don't use it!") @Deprecated("This value is temporary hack for resolve -- don't use it!")
val isStaticScope: Boolean = false val isStaticScope: Boolean = false
): LexicalScope { ): LexicalScope {
override val parent = parent.takeSnapshot() override val parent = parent.takeSnapshot()
private val scopeChain = memberScopes.clone()
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= getFromAllScopes(scopeChain) { it.getContributedDescriptors() } = getFromAllScopes(memberScopes) { it.getContributedDescriptors() }
override fun getContributedClassifier(name: Name, location: LookupLocation) = getFirstMatch(scopeChain) { it.getContributedClassifier(name, location) } override fun getContributedClassifier(name: Name, location: LookupLocation) = getFirstMatch(memberScopes) { it.getContributedClassifier(name, location) }
override fun getContributedVariables(name: Name, location: LookupLocation) = getFromAllScopes(scopeChain) { it.getContributedVariables(name, location) } override fun getContributedVariables(name: Name, location: LookupLocation) = getFromAllScopes(memberScopes) { it.getContributedVariables(name, location) }
override fun getContributedFunctions(name: Name, location: LookupLocation) = getFromAllScopes(scopeChain) { it.getContributedFunctions(name, location) } override fun getContributedFunctions(name: Name, location: LookupLocation) = getFromAllScopes(memberScopes) { it.getContributedFunctions(name, location) }
override fun toString(): String = kind.toString() override fun toString(): String = kind.toString()
@@ -54,7 +53,7 @@ public class LexicalChainedScope @JvmOverloads constructor(
" with implicitReceiver: ", implicitReceiver?.value ?: "NONE", " {") " with implicitReceiver: ", implicitReceiver?.value ?: "NONE", " {")
p.pushIndent() p.pushIndent()
for (scope in scopeChain) { for (scope in memberScopes) {
scope.printScopeStructure(p) scope.printScopeStructure(p)
} }
@@ -65,4 +64,4 @@ public class LexicalChainedScope @JvmOverloads constructor(
p.println("}") p.println("}")
} }
} }
@@ -25,8 +25,8 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.classValueType import org.jetbrains.kotlin.resolve.descriptorUtil.classValueType
import org.jetbrains.kotlin.resolve.scopes.ChainedMemberScope import org.jetbrains.kotlin.resolve.scopes.ChainedMemberScope
import org.jetbrains.kotlin.resolve.scopes.FilteringScope import org.jetbrains.kotlin.resolve.scopes.FilteringScope
import org.jetbrains.kotlin.resolve.scopes.ScopeUtils
import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.ScopeUtils
import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.addIfNotNull
import java.util.* import java.util.*
@@ -112,7 +112,7 @@ class ClassQualifier(
scopes.add(classifier.unsubstitutedInnerClassesScope) scopes.add(classifier.unsubstitutedInnerClassesScope)
} }
return ChainedMemberScope("Member scope for $name as class or object", *scopes.toTypedArray()) return ChainedMemberScope("Member scope for $name as class or object", scopes)
} }
override fun getNestedClassesAndPackageMembersScope(): MemberScope { override fun getNestedClassesAndPackageMembersScope(): MemberScope {
@@ -128,7 +128,7 @@ class ClassQualifier(
scopes.add(ScopeUtils.getStaticNestedClassesScope(classifier)) scopes.add(ScopeUtils.getStaticNestedClassesScope(classifier))
} }
return ChainedMemberScope("Static scope for $name as class or object", *scopes.toTypedArray()) return ChainedMemberScope("Static scope for $name as class or object", scopes)
} }
override fun toString() = "Class{$classifier}" override fun toString() = "Class{$classifier}"
@@ -194,7 +194,10 @@ public abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdi
private val scope: MemberScope private val scope: MemberScope
init { init {
scope = ChainedMemberScope("synthetic package view for test", ScopeWithClassifiers(classes), *packageScopes.toTypedArray()) val list = ArrayList<MemberScope>(packageScopes.size + 1)
list.add(ScopeWithClassifiers(classes))
list.addAll(packageScopes)
scope = ChainedMemberScope("synthetic package view for test", list)
} }
override val fqName: FqName override val fqName: FqName
@@ -54,6 +54,7 @@ import org.jetbrains.kotlin.tests.di.InjectionKt;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
@@ -104,10 +105,10 @@ public class TypeSubstitutorTest extends KotlinTestWithEnvironment {
} }
}); });
return new LexicalChainedScope(typeParameters, module, false, null, LexicalScopeKind.SYNTHETIC, return new LexicalChainedScope(typeParameters, module, false, null, LexicalScopeKind.SYNTHETIC,
new MemberScope[] { Arrays.asList(
contextClass.getDefaultType().getMemberScope(), contextClass.getDefaultType().getMemberScope(),
module.getBuiltIns().getBuiltInsPackageScope() module.getBuiltIns().getBuiltInsPackageScope()
}); ));
} }
private void doTest(@Nullable String expectedTypeStr, String initialTypeStr, Pair<String, String>... substitutionStrs) { private void doTest(@Nullable String expectedTypeStr, String initialTypeStr, Pair<String, String>... substitutionStrs) {
@@ -108,7 +108,7 @@ public final class DeserializedDescriptorResolver {
if (list.isEmpty()) { if (list.isEmpty()) {
return MemberScope.Empty.INSTANCE; return MemberScope.Empty.INSTANCE;
} }
return new ChainedMemberScope("Member scope for union of package parts data", list.toArray(new MemberScope[list.size()])); return new ChainedMemberScope("Member scope for union of package parts data", list);
} }
@Nullable @Nullable
@@ -23,8 +23,8 @@ import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.scopes.ChainedMemberScope import org.jetbrains.kotlin.resolve.scopes.ChainedMemberScope
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.LazyScopeAdapter import org.jetbrains.kotlin.resolve.scopes.LazyScopeAdapter
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.types.TypeSubstitutor import org.jetbrains.kotlin.types.TypeSubstitutor
@@ -46,7 +46,7 @@ public class LazyPackageViewDescriptorImpl(
else { else {
// Packages from SubpackagesScope are got via getContributedDescriptors(DescriptorKindFilter.PACKAGES, MemberScope.ALL_NAME_FILTER) // Packages from SubpackagesScope are got via getContributedDescriptors(DescriptorKindFilter.PACKAGES, MemberScope.ALL_NAME_FILTER)
val scopes = fragments.map { it.getMemberScope() } + SubpackagesScope(module, fqName) val scopes = fragments.map { it.getMemberScope() } + SubpackagesScope(module, fqName)
ChainedMemberScope("package view scope for $fqName in ${module.getName()}", *scopes.toTypedArray()) ChainedMemberScope("package view scope for $fqName in ${module.name}", scopes)
} }
}) })
@@ -27,21 +27,19 @@ import org.jetbrains.kotlin.utils.Printer
public class ChainedMemberScope( public class ChainedMemberScope(
internal val debugName: String, internal val debugName: String,
vararg scopes: MemberScope private val scopes: List<MemberScope>
) : MemberScope { ) : MemberScope {
private val scopeChain = scopes.clone()
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor?
= getFirstMatch(scopeChain) { it.getContributedClassifier(name, location) } = getFirstMatch(scopes) { it.getContributedClassifier(name, location) }
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor>
= getFromAllScopes(scopeChain) { it.getContributedVariables(name, location) } = getFromAllScopes(scopes) { it.getContributedVariables(name, location) }
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor> override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor>
= getFromAllScopes(scopeChain) { it.getContributedFunctions(name, location) } = getFromAllScopes(scopes) { it.getContributedFunctions(name, location) }
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= getFromAllScopes(scopeChain) { it.getContributedDescriptors(kindFilter, nameFilter) } = getFromAllScopes(scopes) { it.getContributedDescriptors(kindFilter, nameFilter) }
override fun toString() = debugName override fun toString() = debugName
@@ -49,7 +47,7 @@ public class ChainedMemberScope(
p.println(javaClass.getSimpleName(), ": ", debugName, " {") p.println(javaClass.getSimpleName(), ": ", debugName, " {")
p.pushIndent() p.pushIndent()
for (scope in scopeChain) { for (scope in scopes) {
scope.printScopeStructure(p) scope.printScopeStructure(p)
} }
@@ -46,7 +46,7 @@ class TypeIntersectionScope private constructor(override val workerScope: Chaine
companion object { companion object {
@JvmStatic @JvmStatic
fun create(message: String, types: List<KotlinType>): MemberScope { fun create(message: String, types: List<KotlinType>): MemberScope {
val chainedScope = ChainedMemberScope(message, *types.map { it.memberScope }.toTypedArray()) val chainedScope = ChainedMemberScope(message, types.map { it.memberScope })
if (types.size <= 1) return chainedScope if (types.size <= 1) return chainedScope
return TypeIntersectionScope(chainedScope) return TypeIntersectionScope(chainedScope)
@@ -53,7 +53,7 @@ public fun <T> concatInOrder(c1: Collection<T>?, c2: Collection<T>?): Collection
return result ?: emptySet() return result ?: emptySet()
} }
public inline fun <Scope, T> getFromAllScopes(scopes: Array<out Scope>, callback: (Scope) -> Collection<T>): Collection<T> { public inline fun <Scope, T> getFromAllScopes(scopes: List<Scope>, callback: (Scope) -> Collection<T>): Collection<T> {
if (scopes.isEmpty()) return emptySet() if (scopes.isEmpty()) return emptySet()
var result: Collection<T>? = null var result: Collection<T>? = null
for (scope in scopes) { for (scope in scopes) {
@@ -62,11 +62,11 @@ public inline fun <Scope, T> getFromAllScopes(scopes: Array<out Scope>, callback
return result ?: emptySet() return result ?: emptySet()
} }
public inline fun <Scope, T : Any> getFirstMatch(scopes: Array<out Scope>, callback: (Scope) -> T?): T? { public inline fun <Scope, T : Any> getFirstMatch(scopes: List<Scope>, callback: (Scope) -> T?): T? {
// NOTE: This is performance-sensitive; please don't replace with map().firstOrNull() // NOTE: This is performance-sensitive; please don't replace with map().firstOrNull()
for (scope in scopes) { for (scope in scopes) {
val result = callback(scope) val result = callback(scope)
if (result != null) return result if (result != null) return result
} }
return null return null
} }
@@ -148,7 +148,7 @@ private fun getClassInnerScope(outerScope: LexicalScope, descriptor: ClassDescri
return LexicalChainedScope(headerScope, descriptor, false, null, return LexicalChainedScope(headerScope, descriptor, false, null,
LexicalScopeKind.SYNTHETIC, LexicalScopeKind.SYNTHETIC,
*scopeChain.toTypedArray()) scopeChain)
} }
public fun getResolutionScope(resolutionFacade: ResolutionFacade, descriptor: DeclarationDescriptor): LexicalScope { public fun getResolutionScope(resolutionFacade: ResolutionFacade, descriptor: DeclarationDescriptor): LexicalScope {