Minor refactoring in imports resolve

This commit is contained in:
Valentin Kipyatkov
2015-04-01 15:14:17 +03:00
parent 12bf8ca94f
commit 21083b48ce
5 changed files with 106 additions and 115 deletions
@@ -0,0 +1,74 @@
/*
* 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
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.PlatformToKotlinClassMap
import org.jetbrains.kotlin.resolve.scopes.FilteringScope
import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.resolve.scopes.WritableScope
import java.util.ArrayList
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.utils.Printer
class AllUnderImportsScope : JetScope {
private val scopes = ArrayList<JetScope>()
public fun addAllUnderImport(descriptor: DeclarationDescriptor) {
if (descriptor is PackageViewDescriptor) {
scopes.add(NoSubpackagesInPackageScope(descriptor))
}
else if (descriptor is ClassDescriptor && QualifiedExpressionResolver.canAllUnderImportFromClass(descriptor)) {
scopes.add(descriptor.getStaticScope())
scopes.add(descriptor.getUnsubstitutedInnerClassesScope())
}
}
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
return scopes.flatMap { it.getDescriptors(kindFilter, nameFilter) }
}
override fun getClassifier(name: Name): ClassifierDescriptor? {
return scopes.stream().map { it.getClassifier(name) }.filterNotNull().singleOrNull()
}
override fun getProperties(name: Name): Collection<VariableDescriptor> {
return scopes.flatMap { it.getProperties(name) }
}
override fun getFunctions(name: Name): Collection<FunctionDescriptor> {
return scopes.flatMap { it.getFunctions(name) }
}
override fun getPackage(name: Name): PackageViewDescriptor? = null // packages are not imported by all under imports
override fun getLocalVariable(name: Name): VariableDescriptor? = null
override fun getContainingDeclaration(): DeclarationDescriptor = throw UnsupportedOperationException()
override fun getDeclarationsByLabel(labelName: Name): Collection<DeclarationDescriptor> = listOf()
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> = listOf()
override fun getOwnDeclaredDescriptors(): Collection<DeclarationDescriptor> = listOf()
override fun printScopeStructure(p: Printer) {
p.println(javaClass.getSimpleName())
}
}
@@ -1,95 +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
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.PlatformToKotlinClassMap
import org.jetbrains.kotlin.resolve.scopes.FilteringScope
import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.resolve.scopes.WritableScope
import java.util.ArrayList
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.utils.Printer
public class Importer {
private val allUnderImportScopes = ArrayList<JetScope>()
private val explicitImports = ArrayList<Pair<DeclarationDescriptor, Name>>()
public fun addAllUnderImport(descriptor: DeclarationDescriptor) {
if (descriptor is PackageViewDescriptor) {
allUnderImportScopes.add(NoSubpackagesInPackageScope(descriptor))
}
else if (descriptor is ClassDescriptor && QualifiedExpressionResolver.canAllUnderImportFromClass(descriptor)) {
allUnderImportScopes.add(descriptor.getStaticScope())
allUnderImportScopes.add(descriptor.getUnsubstitutedInnerClassesScope())
}
}
public fun addAliasImport(descriptor: DeclarationDescriptor, aliasName: Name) {
explicitImports.add(descriptor to aliasName)
}
public fun doImport(targetScope: WritableScope) {
// import all imports with '*' first because they have lower priority
targetScope.importScope(AllUnderImportsScope(allUnderImportScopes))
for ((descriptor, name) in explicitImports) {
when (descriptor) {
is ClassifierDescriptor -> targetScope.importClassifierAlias(name, descriptor)
is PackageViewDescriptor -> targetScope.importPackageAlias(name, descriptor)
is FunctionDescriptor -> targetScope.importFunctionAlias(name, descriptor)
is VariableDescriptor -> targetScope.importVariableAlias(name, descriptor)
else -> error("Unknown descriptor")
}
}
}
private class AllUnderImportsScope(private val scopes: Collection<JetScope>) : JetScope {
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> {
return scopes.flatMap { it.getDescriptors(kindFilter, nameFilter) }
}
override fun getClassifier(name: Name): ClassifierDescriptor? {
return scopes.stream().map { it.getClassifier(name) }.filterNotNull().singleOrNull()
}
override fun getProperties(name: Name): Collection<VariableDescriptor> {
return scopes.flatMap { it.getProperties(name) }
}
override fun getFunctions(name: Name): Collection<FunctionDescriptor> {
return scopes.flatMap { it.getFunctions(name) }
}
override fun getPackage(name: Name): PackageViewDescriptor? = null // packages are not imported by all under imports
override fun getLocalVariable(name: Name): VariableDescriptor? = null
override fun getContainingDeclaration(): DeclarationDescriptor = throw UnsupportedOperationException()
override fun getDeclarationsByLabel(labelName: Name): Collection<DeclarationDescriptor> = listOf()
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> = listOf()
override fun getOwnDeclaredDescriptors(): Collection<DeclarationDescriptor> = listOf()
override fun printScopeStructure(p: Printer) {
p.println(javaClass.getSimpleName())
}
}
}
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.scopes.AbstractScopeAdapter;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.resolve.scopes.WritableScope;
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
import java.util.Collection;
@@ -79,7 +80,7 @@ public class QualifiedExpressionResolver {
@NotNull JetImportDirective importDirective,
@NotNull JetScope scope,
@NotNull JetScope scopeToCheckVisibility,
@Nullable Importer importer,
@Nullable WritableScope importIntoScope,
@NotNull BindingTrace trace,
@NotNull LookupMode lookupMode
) {
@@ -117,26 +118,29 @@ public class QualifiedExpressionResolver {
return Collections.emptyList();
}
if (importer != null) {
if (importIntoScope != null) {
AllUnderImportsScope importsScope = new AllUnderImportsScope();
for (DeclarationDescriptor descriptor : descriptors) {
importer.addAllUnderImport(descriptor);
importsScope.addAllUnderImport(descriptor);
}
importIntoScope.importScope(importsScope);
}
return Collections.emptyList();
}
else {
Name aliasName = JetPsiUtil.getAliasName(importDirective);
if (aliasName == null) {
return Collections.emptyList();
}
if (importIntoScope != null) {
for (DeclarationDescriptor descriptor : descriptors) {
importIntoScope.importAlias(aliasName, descriptor);
}
}
return Collections.emptyList();
}
Name aliasName = JetPsiUtil.getAliasName(importDirective);
if (aliasName == null) {
return Collections.emptyList();
return descriptors;
}
if (importer != null) {
for (DeclarationDescriptor descriptor : descriptors) {
importer.addAliasImport(descriptor, aliasName);
}
}
return descriptors;
}
private static boolean canImportMembersFrom(
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.psi.JetImportDirective
import org.jetbrains.kotlin.psi.debugText.*
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.Importer
import org.jetbrains.kotlin.resolve.JetModuleUtil
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.*
@@ -100,15 +99,13 @@ class LazyImportResolver(
val directiveImportScope = WritableScopeImpl(JetScope.Empty, packageView, RedeclarationHandler.DO_NOTHING, "Scope for import '" + directive.getDebugText() + "' resolve in " + toString())
directiveImportScope.changeLockLevel(WritableScope.LockLevel.BOTH)
val importer = Importer()
directiveUnderResolve = directive
val descriptors: Collection<DeclarationDescriptor>
try {
val resolver = resolveSession.getQualifiedExpressionResolver()
descriptors = resolver.processImportReference(directive, rootScope, packageView.getMemberScope(),
importer, traceForImportResolve, mode)
importer.doImport(directiveImportScope)
directiveImportScope, traceForImportResolve, mode)
if (mode == LookupMode.EVERYTHING) {
org.jetbrains.kotlin.resolve.PlatformTypesMappedToKotlinChecker.checkPlatformTypesMappedToKotlin(packageView.getModule(), traceForImportResolve, directive, descriptors)
}
@@ -64,4 +64,15 @@ public trait WritableScope : JetScope {
public fun importVariableAlias(aliasName: Name, variableDescriptor: VariableDescriptor)
public fun clearImports()
public fun importAlias(aliasName: Name, descriptor: DeclarationDescriptor) {
when (descriptor) {
is ClassifierDescriptor -> importClassifierAlias(aliasName, descriptor)
is PackageViewDescriptor -> importPackageAlias(aliasName, descriptor)
is FunctionDescriptor -> importFunctionAlias(aliasName, descriptor)
is VariableDescriptor -> importVariableAlias(aliasName, descriptor)
else -> error("Unknown descriptor")
}
}
}