Refactoring of Importer - PlatformToKotlinClassMap is the same for all imports

This commit is contained in:
Valentin Kipyatkov
2015-01-14 18:02:37 +03:00
parent 58fb303f8c
commit 8fc0063410
5 changed files with 33 additions and 29 deletions
@@ -25,14 +25,16 @@ import org.jetbrains.kotlin.resolve.scopes.WritableScope
import java.util.ArrayList
public trait Importer {
public fun addAllUnderImport(descriptor: DeclarationDescriptor, platformToKotlinClassMap: PlatformToKotlinClassMap)
public fun addAllUnderImport(descriptor: DeclarationDescriptor)
public fun addAliasImport(descriptor: DeclarationDescriptor, aliasName: Name)
public open class StandardImporter(private val fileScope: WritableScope) : Importer {
public open class StandardImporter(
private val fileScope: WritableScope,
private val platformToKotlinClassMap: PlatformToKotlinClassMap
) : Importer {
override fun addAllUnderImport(descriptor: DeclarationDescriptor, platformToKotlinClassMap: PlatformToKotlinClassMap) {
importAllUnderDeclaration(descriptor, platformToKotlinClassMap)
override fun addAllUnderImport(descriptor: DeclarationDescriptor) {
importAllUnderDeclaration(descriptor)
}
override fun addAliasImport(descriptor: DeclarationDescriptor, aliasName: Name) {
@@ -49,7 +51,7 @@ public trait Importer {
}
}
protected fun importAllUnderDeclaration(descriptor: DeclarationDescriptor, platformToKotlinClassMap: PlatformToKotlinClassMap) {
protected fun importAllUnderDeclaration(descriptor: DeclarationDescriptor) {
if (descriptor is PackageViewDescriptor) {
val scope = NoSubpackagesInPackageScope(descriptor)
fileScope.importScope(createFilteringScope(scope, descriptor, platformToKotlinClassMap))
@@ -71,17 +73,21 @@ public trait Importer {
}
}
public class DelayedImporter(fileScope: WritableScope) : StandardImporter(fileScope) {
public class DelayedImporter(
fileScope: WritableScope,
platformToKotlinClassMap: PlatformToKotlinClassMap
) : StandardImporter(fileScope, platformToKotlinClassMap) {
private trait DelayedImportEntry
private class AllUnderImportEntry(val first: DeclarationDescriptor, val second: PlatformToKotlinClassMap) : DelayedImportEntry
private class AllUnderImportEntry(val descriptor: DeclarationDescriptor) : DelayedImportEntry
private class AliasImportEntry(val first: DeclarationDescriptor, val second: Name) : DelayedImportEntry
private class AliasImportEntry(val descriptor: DeclarationDescriptor, val name: Name) : DelayedImportEntry
private val imports = ArrayList<DelayedImportEntry>()
override fun addAllUnderImport(descriptor: DeclarationDescriptor, platformToKotlinClassMap: PlatformToKotlinClassMap) {
imports.add(AllUnderImportEntry(descriptor, platformToKotlinClassMap))
override fun addAllUnderImport(descriptor: DeclarationDescriptor) {
imports.add(AllUnderImportEntry(descriptor))
}
override fun addAliasImport(descriptor: DeclarationDescriptor, aliasName: Name) {
@@ -91,18 +97,18 @@ public trait Importer {
public fun processImports() {
for (anImport in imports) {
if (anImport is AllUnderImportEntry) {
importAllUnderDeclaration(anImport.first, anImport.second)
importAllUnderDeclaration(anImport.descriptor)
}
else {
anImport as AliasImportEntry
importDeclarationAlias(anImport.first, anImport.second)
importDeclarationAlias(anImport.descriptor, anImport.name)
}
}
}
}
object DoNothingImporter : Importer {
override fun addAllUnderImport(descriptor: DeclarationDescriptor, platformToKotlinClassMap: PlatformToKotlinClassMap) {
override fun addAllUnderImport(descriptor: DeclarationDescriptor) {
}
override fun addAliasImport(descriptor: DeclarationDescriptor, aliasName: Name) {
@@ -99,7 +99,7 @@ public class ImportsResolver {
) {
@NotNull JetScope rootScope = JetModuleUtil.getSubpackagesOfRootScope(module);
Importer.DelayedImporter delayedImporter = new Importer.DelayedImporter(fileScope);
Importer.DelayedImporter delayedImporter = new Importer.DelayedImporter(fileScope, module.getPlatformToKotlinClassMap());
if (lookupMode == LookupMode.EVERYTHING) {
fileScope.clearImports();
}
@@ -110,7 +110,7 @@ public class ImportsResolver {
JetImportDirective defaultImportDirective = importsFactory.createImportDirective(defaultImportPath);
qualifiedExpressionResolver.processImportReference(defaultImportDirective, rootScope, fileScope, delayedImporter,
temporaryTrace, module, lookupMode);
temporaryTrace, lookupMode);
}
Map<JetImportDirective, Collection<? extends DeclarationDescriptor>> resolvedDirectives = Maps.newHashMap();
@@ -120,7 +120,7 @@ public class ImportsResolver {
for (JetImportDirective importDirective : importDirectives) {
Collection<? extends DeclarationDescriptor> descriptors =
qualifiedExpressionResolver.processImportReference(importDirective, rootScopeForFile, fileScope, delayedImporter,
trace, module, lookupMode);
trace, lookupMode);
if (!descriptors.isEmpty()) {
resolvedDirectives.put(importDirective, descriptors);
}
@@ -60,7 +60,6 @@ public class QualifiedExpressionResolver {
@NotNull JetScope scopeToCheckVisibility,
@NotNull Importer importer,
@NotNull BindingTrace trace,
@NotNull ModuleDescriptor module,
@NotNull LookupMode lookupMode
) {
if (importDirective.isAbsoluteInRootPackage()) {
@@ -93,7 +92,7 @@ public class QualifiedExpressionResolver {
}
for (DeclarationDescriptor descriptor : descriptors) {
importer.addAllUnderImport(descriptor, module.getPlatformToKotlinClassMap());
importer.addAllUnderImport(descriptor);
}
return Collections.emptyList();
}
@@ -69,14 +69,14 @@ public class LazyImportScope(private val resolveSession: ResolveSession,
val directiveImportScope = WritableScopeImpl(JetScope.Empty, containingDeclaration, RedeclarationHandler.DO_NOTHING, "Scope for import '" + directive.getDebugText() + "' resolve in " + toString())
directiveImportScope.changeLockLevel(WritableScope.LockLevel.BOTH)
val importer = Importer.StandardImporter(directiveImportScope)
val importer = Importer.StandardImporter(directiveImportScope, resolveSession.getModuleDescriptor().platformToKotlinClassMap)
directiveUnderResolve = directive
val descriptors: Collection<DeclarationDescriptor>
try {
val resolver = resolveSession.getQualifiedExpressionResolver()
descriptors = resolver.processImportReference(directive, rootScope, containingDeclaration.getMemberScope(),
importer, traceForImportResolve, resolveSession.getModuleDescriptor(), mode)
importer, traceForImportResolve, mode)
if (mode == LookupMode.EVERYTHING) {
ImportsResolver.checkPlatformTypesMappedToKotlin(containingDeclaration.getModule(), traceForImportResolve, directive, descriptors)
}
@@ -56,7 +56,7 @@ public class KotlinIndicesHelper(
return declarations.flatMap {
if (it.getContainingJetFile().isCompiled()) {
val importDirective = JetPsiFactory(project).createImportDirective(it.getFqName().asString())
analyzeImportReference(importDirective, resolutionScope, BindingTraceContext(), moduleDescriptor).filterIsInstance<CallableDescriptor>()
analyzeImportReference(importDirective, resolutionScope, BindingTraceContext()).filterIsInstance<CallableDescriptor>()
}
else {
(resolutionFacade.resolveToDescriptor(it) as? CallableDescriptor).singletonOrEmptyList()
@@ -118,7 +118,7 @@ public class KotlinIndicesHelper(
val receiverValue = ExpressionReceiver(receiverExpression, expressionType)
matchingNames.flatMapTo(this) {
findSuitableExtensions(it, index, receiverValue, dataFlowInfo, callType, resolutionScope, moduleDescriptor, bindingContext)
findSuitableExtensions(it, index, receiverValue, dataFlowInfo, callType, resolutionScope, bindingContext)
}
}
else {
@@ -126,7 +126,7 @@ public class KotlinIndicesHelper(
for (receiver in resolutionScope.getImplicitReceiversWithInstance()) {
matchingNames.flatMapTo(this) {
findSuitableExtensions(it, index, receiver.getValue(), dataFlowInfo, CallType.NORMAL, resolutionScope, moduleDescriptor, bindingContext)
findSuitableExtensions(it, index, receiver.getValue(), dataFlowInfo, CallType.NORMAL, resolutionScope, bindingContext)
}
}
}
@@ -141,13 +141,12 @@ public class KotlinIndicesHelper(
dataFlowInfo: DataFlowInfo,
callType: CallType,
resolutionScope: JetScope,
module: ModuleDescriptor,
bindingContext: BindingContext): Stream<CallableDescriptor> {
val fqnString = callableFQN.asString()
val extensions = index.get(fqnString, project, scope).filter { it.getReceiverTypeReference() != null }
val descriptors = if (extensions.any { it.getContainingJetFile().isCompiled() } ) {
val importDirective = JetPsiFactory(project).createImportDirective(fqnString)
analyzeImportReference(importDirective, resolutionScope, BindingTraceContext(), module)
analyzeImportReference(importDirective, resolutionScope, BindingTraceContext())
.filterIsInstance<CallableDescriptor>()
.filter { it.getExtensionReceiverParameter() != null }
}
@@ -180,14 +179,14 @@ public class KotlinIndicesHelper(
private fun findTopLevelCallables(fqName: FqName, context: JetExpression, jetScope: JetScope): Collection<CallableDescriptor> {
val importDirective = JetPsiFactory(context.getProject()).createImportDirective(ImportPath(fqName, false))
val allDescriptors = analyzeImportReference(importDirective, jetScope, BindingTraceContext(), moduleDescriptor)
val allDescriptors = analyzeImportReference(importDirective, jetScope, BindingTraceContext())
return allDescriptors.filterIsInstance<CallableDescriptor>().filter { it.getExtensionReceiverParameter() == null }
}
private fun analyzeImportReference(
importDirective: JetImportDirective, scope: JetScope, trace: BindingTrace, module: ModuleDescriptor
importDirective: JetImportDirective, scope: JetScope, trace: BindingTrace
): Collection<DeclarationDescriptor> {
return QualifiedExpressionResolver().processImportReference(importDirective, scope, scope, Importer.DoNothingImporter, trace,
module, LookupMode.EVERYTHING)
LookupMode.EVERYTHING)
}
}