get rid of JetScopeSelectorUtil, inline its contents into Kotlin code
This commit is contained in:
@@ -29,9 +29,6 @@ import org.jetbrains.kotlin.resolve.PlatformTypesMappedToKotlinChecker
|
|||||||
import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver.LookupMode
|
import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver.LookupMode
|
||||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||||
import org.jetbrains.kotlin.resolve.scopes.JetScope
|
import org.jetbrains.kotlin.resolve.scopes.JetScope
|
||||||
import org.jetbrains.kotlin.resolve.scopes.JetScopeSelectorUtil
|
|
||||||
import org.jetbrains.kotlin.resolve.scopes.JetScopeSelectorUtil.ScopeByNameMultiSelector
|
|
||||||
import org.jetbrains.kotlin.resolve.scopes.JetScopeSelectorUtil.ScopeByNameSelector
|
|
||||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
|
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
|
||||||
import org.jetbrains.kotlin.utils.Printer
|
import org.jetbrains.kotlin.utils.Printer
|
||||||
import java.util.HashSet
|
import java.util.HashSet
|
||||||
@@ -146,8 +143,7 @@ class LazyImportResolver(
|
|||||||
val importedReference = importDirective.getImportedReference()
|
val importedReference = importDirective.getImportedReference()
|
||||||
if (importedReference == null || resolvedTo == null) return
|
if (importedReference == null || resolvedTo == null) return
|
||||||
|
|
||||||
val aliasName = JetPsiUtil.getAliasName(importDirective)
|
val aliasName = JetPsiUtil.getAliasName(importDirective) ?: return
|
||||||
if (aliasName == null) return
|
|
||||||
|
|
||||||
if (resolvedTo.size() != 1) return
|
if (resolvedTo.size() != 1) return
|
||||||
|
|
||||||
@@ -169,7 +165,7 @@ class LazyImportResolver(
|
|||||||
public fun <D : DeclarationDescriptor> selectSingleFromImports(
|
public fun <D : DeclarationDescriptor> selectSingleFromImports(
|
||||||
name: Name,
|
name: Name,
|
||||||
lookupMode: LookupMode,
|
lookupMode: LookupMode,
|
||||||
descriptorSelector: ScopeByNameSelector<D>
|
descriptorSelector: (JetScope, Name) -> D?
|
||||||
): D? {
|
): D? {
|
||||||
fun compute(): D? {
|
fun compute(): D? {
|
||||||
val imports = indexedImports.importsForName(name)
|
val imports = indexedImports.importsForName(name)
|
||||||
@@ -180,7 +176,7 @@ class LazyImportResolver(
|
|||||||
|
|
||||||
var target: D? = null
|
var target: D? = null
|
||||||
for (directive in imports) {
|
for (directive in imports) {
|
||||||
val resolved = descriptorSelector.get(getImportScope(directive, lookupMode), name) ?: continue
|
val resolved = descriptorSelector(getImportScope(directive, lookupMode), name) ?: continue
|
||||||
if (target != null && target != resolved) return null // ambiguity
|
if (target != null && target != resolved) return null // ambiguity
|
||||||
target = resolved
|
target = resolved
|
||||||
}
|
}
|
||||||
@@ -192,7 +188,7 @@ class LazyImportResolver(
|
|||||||
public fun <D : DeclarationDescriptor> collectFromImports(
|
public fun <D : DeclarationDescriptor> collectFromImports(
|
||||||
name: Name,
|
name: Name,
|
||||||
lookupMode: LookupMode,
|
lookupMode: LookupMode,
|
||||||
descriptorsSelector: ScopeByNameMultiSelector<D>
|
descriptorsSelector: (JetScope, Name) -> Collection<D>
|
||||||
): Collection<D> {
|
): Collection<D> {
|
||||||
return resolveSession.getStorageManager().compute {
|
return resolveSession.getStorageManager().compute {
|
||||||
val descriptors = HashSet<D>()
|
val descriptors = HashSet<D>()
|
||||||
@@ -202,7 +198,7 @@ class LazyImportResolver(
|
|||||||
throw IllegalStateException("Recursion while resolving many imports: " + directive.getText())
|
throw IllegalStateException("Recursion while resolving many imports: " + directive.getText())
|
||||||
}
|
}
|
||||||
|
|
||||||
descriptors.addAll(descriptorsSelector.get(getImportScope(directive, lookupMode), name))
|
descriptors.addAll(descriptorsSelector(getImportScope(directive, lookupMode), name))
|
||||||
}
|
}
|
||||||
|
|
||||||
descriptors
|
descriptors
|
||||||
@@ -231,40 +227,36 @@ class LazyImportScope(
|
|||||||
INVISIBLE_CLASSES
|
INVISIBLE_CLASSES
|
||||||
}
|
}
|
||||||
|
|
||||||
private val classifierDescriptorSelector = object : ScopeByNameSelector<ClassifierDescriptor> {
|
fun isClassVisible(descriptor: ClassDescriptor): Boolean {
|
||||||
override fun get(scope: JetScope, name: Name): ClassifierDescriptor? {
|
if (filteringKind == FilteringKind.ALL) return true
|
||||||
val descriptor = JetScopeSelectorUtil.CLASSIFIER_DESCRIPTOR_SCOPE_SELECTOR.get(scope, name)
|
val visibility = descriptor.getVisibility()
|
||||||
return if (descriptor != null && filter(descriptor as ClassDescriptor/*no type parameter can be imported*/)) descriptor else null
|
val includeVisible = filteringKind == FilteringKind.VISIBLE_CLASSES
|
||||||
}
|
if (!visibility.mustCheckInImports()) return includeVisible
|
||||||
|
return Visibilities.isVisible(ReceiverValue.IRRELEVANT_RECEIVER, descriptor, importResolver.packageView) == includeVisible
|
||||||
private fun filter(descriptor: ClassDescriptor): Boolean {
|
|
||||||
if (filteringKind == FilteringKind.ALL) return true
|
|
||||||
val visibility = descriptor.getVisibility()
|
|
||||||
val includeVisible = filteringKind == FilteringKind.VISIBLE_CLASSES
|
|
||||||
if (!visibility.mustCheckInImports()) return includeVisible
|
|
||||||
return Visibilities.isVisible(ReceiverValue.IRRELEVANT_RECEIVER, descriptor, importResolver.packageView) == includeVisible
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getClassifier(name: Name): ClassifierDescriptor? {
|
override fun getClassifier(name: Name): ClassifierDescriptor? {
|
||||||
return importResolver.selectSingleFromImports(name, LookupMode.ONLY_CLASSES_AND_PACKAGES, classifierDescriptorSelector)
|
return importResolver.selectSingleFromImports(name, LookupMode.ONLY_CLASSES_AND_PACKAGES) { scope, name ->
|
||||||
|
val descriptor = scope.getClassifier(name)
|
||||||
|
if (descriptor != null && isClassVisible(descriptor as ClassDescriptor/*no type parameter can be imported*/)) descriptor else null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getPackage(name: Name): PackageViewDescriptor? {
|
override fun getPackage(name: Name): PackageViewDescriptor? {
|
||||||
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return null
|
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return null
|
||||||
return importResolver.selectSingleFromImports(name, LookupMode.ONLY_CLASSES_AND_PACKAGES, JetScopeSelectorUtil.PACKAGE_SCOPE_SELECTOR)
|
return importResolver.selectSingleFromImports(name, LookupMode.ONLY_CLASSES_AND_PACKAGES) { scope, name -> scope.getPackage(name) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getProperties(name: Name): Collection<VariableDescriptor> {
|
override fun getProperties(name: Name): Collection<VariableDescriptor> {
|
||||||
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
|
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
|
||||||
return importResolver.collectFromImports(name, LookupMode.EVERYTHING, JetScopeSelectorUtil.NAMED_PROPERTIES_SCOPE_SELECTOR)
|
return importResolver.collectFromImports(name, LookupMode.EVERYTHING) { scope, name -> scope.getProperties(name) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getLocalVariable(name: Name) = null
|
override fun getLocalVariable(name: Name) = null
|
||||||
|
|
||||||
override fun getFunctions(name: Name): Collection<FunctionDescriptor> {
|
override fun getFunctions(name: Name): Collection<FunctionDescriptor> {
|
||||||
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
|
if (filteringKind == FilteringKind.INVISIBLE_CLASSES) return listOf()
|
||||||
return importResolver.collectFromImports(name, LookupMode.EVERYTHING, JetScopeSelectorUtil.NAMED_FUNCTION_SCOPE_SELECTOR)
|
return importResolver.collectFromImports(name, LookupMode.EVERYTHING) { scope, name -> scope.getFunctions(name) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getDeclarationsByLabel(labelName: Name): Collection<DeclarationDescriptor> = listOf()
|
override fun getDeclarationsByLabel(labelName: Name): Collection<DeclarationDescriptor> = listOf()
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.descriptors.*
|
|||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.utils.Printer
|
import org.jetbrains.kotlin.utils.Printer
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import org.jetbrains.kotlin.resolve.scopes.JetScopeSelectorUtil.*
|
|
||||||
|
|
||||||
public open class ChainedScope(
|
public open class ChainedScope(
|
||||||
private val containingDeclaration: DeclarationDescriptor?/* it's nullable as a hack for TypeUtils.intersect() */,
|
private val containingDeclaration: DeclarationDescriptor?/* it's nullable as a hack for TypeUtils.intersect() */,
|
||||||
@@ -30,20 +29,42 @@ public open class ChainedScope(
|
|||||||
private val scopeChain = scopes.clone()
|
private val scopeChain = scopes.clone()
|
||||||
private var implicitReceiverHierarchy: List<ReceiverParameterDescriptor>? = null
|
private var implicitReceiverHierarchy: List<ReceiverParameterDescriptor>? = null
|
||||||
|
|
||||||
|
private inline fun getFirstMatch<T>(callback: (JetScope) -> T): T {
|
||||||
|
// NOTE: This is performance-sensitive; please don't replace with map().firstOrNull()
|
||||||
|
for (scope in scopeChain) {
|
||||||
|
val result = callback(scope)
|
||||||
|
if (result != null) return result
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private inline fun getFromAllScopes<T>(callback: (JetScope) -> Collection<T>): Set<T> {
|
||||||
|
if (scopeChain.isEmpty()) return emptySet()
|
||||||
|
var result: MutableSet<T>? = null
|
||||||
|
for (scope in scopeChain) {
|
||||||
|
val fromScope = callback(scope)
|
||||||
|
if (result == null) {
|
||||||
|
result = LinkedHashSet<T>()
|
||||||
|
}
|
||||||
|
result.addAll(fromScope)
|
||||||
|
}
|
||||||
|
return result ?: emptySet()
|
||||||
|
}
|
||||||
|
|
||||||
override fun getClassifier(name: Name): ClassifierDescriptor?
|
override fun getClassifier(name: Name): ClassifierDescriptor?
|
||||||
= getFirstMatch(scopeChain, name, CLASSIFIER_DESCRIPTOR_SCOPE_SELECTOR)
|
= getFirstMatch { it.getClassifier(name) }
|
||||||
|
|
||||||
override fun getPackage(name: Name): PackageViewDescriptor?
|
override fun getPackage(name: Name): PackageViewDescriptor?
|
||||||
= getFirstMatch(scopeChain, name, PACKAGE_SCOPE_SELECTOR)
|
= getFirstMatch { it.getPackage(name) }
|
||||||
|
|
||||||
override fun getProperties(name: Name): Set<VariableDescriptor>
|
override fun getProperties(name: Name): Set<VariableDescriptor>
|
||||||
= getFromAllScopes(scopeChain, name, NAMED_PROPERTIES_SCOPE_SELECTOR)
|
= getFromAllScopes { it.getProperties(name) }
|
||||||
|
|
||||||
override fun getLocalVariable(name: Name): VariableDescriptor?
|
override fun getLocalVariable(name: Name): VariableDescriptor?
|
||||||
= getFirstMatch(scopeChain, name, VARIABLE_DESCRIPTOR_SCOPE_SELECTOR)
|
= getFirstMatch { it.getLocalVariable(name) }
|
||||||
|
|
||||||
override fun getFunctions(name: Name): Set<FunctionDescriptor>
|
override fun getFunctions(name: Name): Set<FunctionDescriptor>
|
||||||
= getFromAllScopes(scopeChain, name, NAMED_FUNCTION_SCOPE_SELECTOR)
|
= getFromAllScopes { it.getFunctions(name) }
|
||||||
|
|
||||||
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> {
|
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> {
|
||||||
if (implicitReceiverHierarchy == null) {
|
if (implicitReceiverHierarchy == null) {
|
||||||
|
|||||||
@@ -1,128 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.kotlin.resolve.scopes;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.kotlin.descriptors.*;
|
|
||||||
import org.jetbrains.kotlin.name.Name;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class JetScopeSelectorUtil {
|
|
||||||
private JetScopeSelectorUtil() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface ScopeByNameSelector<D extends DeclarationDescriptor> {
|
|
||||||
@Nullable
|
|
||||||
D get(@NotNull JetScope scope, @NotNull Name name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface ScopeByNameMultiSelector<D extends DeclarationDescriptor> {
|
|
||||||
@NotNull
|
|
||||||
Collection<D> get(JetScope scope, Name name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static <D extends DeclarationDescriptor> Collection<D> collect(Collection<JetScope> scopes, ScopeByNameMultiSelector<D> selector, Name name) {
|
|
||||||
Set<D> descriptors = new HashSet<D>();
|
|
||||||
|
|
||||||
for (JetScope scope : scopes) {
|
|
||||||
descriptors.addAll(selector.get(scope, name));
|
|
||||||
}
|
|
||||||
|
|
||||||
return descriptors;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final ScopeByNameSelector<ClassifierDescriptor> CLASSIFIER_DESCRIPTOR_SCOPE_SELECTOR =
|
|
||||||
new ScopeByNameSelector<ClassifierDescriptor>() {
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public ClassifierDescriptor get(@NotNull JetScope scope, @NotNull Name name) {
|
|
||||||
return scope.getClassifier(name);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static final ScopeByNameSelector<PackageViewDescriptor> PACKAGE_SCOPE_SELECTOR =
|
|
||||||
new ScopeByNameSelector<PackageViewDescriptor>() {
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public PackageViewDescriptor get(@NotNull JetScope scope, @NotNull Name name) {
|
|
||||||
return scope.getPackage(name);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static final ScopeByNameSelector<VariableDescriptor> VARIABLE_DESCRIPTOR_SCOPE_SELECTOR =
|
|
||||||
new ScopeByNameSelector<VariableDescriptor>() {
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public VariableDescriptor get(@NotNull JetScope scope, @NotNull Name name) {
|
|
||||||
return scope.getLocalVariable(name);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static final ScopeByNameMultiSelector<FunctionDescriptor> NAMED_FUNCTION_SCOPE_SELECTOR =
|
|
||||||
new ScopeByNameMultiSelector<FunctionDescriptor>() {
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Collection<FunctionDescriptor> get(@NotNull JetScope scope, @NotNull Name name) {
|
|
||||||
return scope.getFunctions(name);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static final ScopeByNameMultiSelector<VariableDescriptor> NAMED_PROPERTIES_SCOPE_SELECTOR =
|
|
||||||
new ScopeByNameMultiSelector<VariableDescriptor>() {
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Collection<VariableDescriptor> get(@NotNull JetScope scope, @NotNull Name name) {
|
|
||||||
return scope.getProperties(name);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static <D extends DeclarationDescriptor> D getFirstMatch(
|
|
||||||
@NotNull JetScope[] scopes,
|
|
||||||
@NotNull Name name,
|
|
||||||
@NotNull ScopeByNameSelector<D> descriptorSelector
|
|
||||||
) {
|
|
||||||
for (JetScope scope : scopes) {
|
|
||||||
D descriptor = descriptorSelector.get(scope, name);
|
|
||||||
|
|
||||||
if (descriptor != null) {
|
|
||||||
return descriptor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static <D extends DeclarationDescriptor> Set<D> getFromAllScopes(
|
|
||||||
@NotNull JetScope[] scopes,
|
|
||||||
@NotNull Name name,
|
|
||||||
@NotNull ScopeByNameMultiSelector<D> descriptorsSelector
|
|
||||||
) {
|
|
||||||
if (scopes.length == 0) return Collections.emptySet();
|
|
||||||
|
|
||||||
Set<D> descriptors = new LinkedHashSet<D>();
|
|
||||||
for (JetScope jetScope : scopes) {
|
|
||||||
descriptors.addAll(descriptorsSelector.get(jetScope, name));
|
|
||||||
}
|
|
||||||
|
|
||||||
return descriptors;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user