From 00ee5e3d160860468d43b9725aeada1edde2ff23 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Fri, 2 Mar 2018 15:21:26 +0100 Subject: [PATCH] Refactor: extract mapping package to files to a separate component Make it extensible --- .../kotlin/resolve/DescriptorResolver.java | 17 ++------- .../kotlin/resolve/FilePreprocessor.kt | 36 +++++++++++++++++++ .../kotlin/resolve/LazyTopDownAnalyzer.kt | 5 +-- 3 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/kotlin/resolve/FilePreprocessor.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java index 76643b5984f..7a4a39a9656 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java @@ -18,7 +18,6 @@ package org.jetbrains.kotlin.resolve; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.common.collect.Sets; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import kotlin.Pair; @@ -40,7 +39,6 @@ import org.jetbrains.kotlin.descriptors.impl.*; import org.jetbrains.kotlin.diagnostics.Errors; import org.jetbrains.kotlin.incremental.components.NoLookupLocation; import org.jetbrains.kotlin.lexer.KtTokens; -import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt; @@ -67,7 +65,8 @@ import java.util.*; import static org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.*; import static org.jetbrains.kotlin.diagnostics.Errors.*; import static org.jetbrains.kotlin.lexer.KtTokens.*; -import static org.jetbrains.kotlin.resolve.BindingContext.*; +import static org.jetbrains.kotlin.resolve.BindingContext.CONSTRUCTOR; +import static org.jetbrains.kotlin.resolve.BindingContext.TYPE_ALIAS; import static org.jetbrains.kotlin.resolve.DescriptorUtils.*; import static org.jetbrains.kotlin.resolve.ModifiersChecker.resolveMemberModalityFromModifiers; import static org.jetbrains.kotlin.resolve.ModifiersChecker.resolveVisibilityFromModifiers; @@ -1343,16 +1342,4 @@ public class DescriptorResolver { public static ClassDescriptor getContainingClass(@NotNull LexicalScope scope) { return getParentOfType(scope.getOwnerDescriptor(), ClassDescriptor.class, false); } - - public static void registerFileInPackage(@NotNull BindingTrace trace, @NotNull KtFile file) { - // Register files corresponding to this package - // The trace currently does not support bi-di multimaps that would handle this task nicer - FqName fqName = file.getPackageFqName(); - Collection files = trace.get(PACKAGE_TO_FILES, fqName); - if (files == null) { - files = Sets.newIdentityHashSet(); - } - files.add(file); - trace.record(BindingContext.PACKAGE_TO_FILES, fqName, files); - } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/FilePreprocessor.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/FilePreprocessor.kt new file mode 100644 index 00000000000..8b281077f64 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/FilePreprocessor.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.resolve + +import com.google.common.collect.Sets +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.resolve.BindingContext.PACKAGE_TO_FILES + +interface FilePreprocessorExtension { + fun preprocessFile(file: KtFile) +} + +class FilePreprocessor( + private val trace: BindingTrace, + private val extensions: Iterable +) { + fun preprocessFile(file: KtFile) { + registerFileByPackage(file) + + for (extension in extensions) { + extension.preprocessFile(file) + } + } + + private fun registerFileByPackage(file: KtFile) { + // Register files corresponding to this package + // The trace currently does not support bi-di multimaps that would handle this task nicer + val fqName = file.packageFqName + val files = trace.get(PACKAGE_TO_FILES, fqName) ?: Sets.newIdentityHashSet() + files.add(file) + trace.record(PACKAGE_TO_FILES, fqName, files) + } +} \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyTopDownAnalyzer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyTopDownAnalyzer.kt index c87277e514b..ff518b28d0e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyTopDownAnalyzer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyTopDownAnalyzer.kt @@ -51,7 +51,8 @@ class LazyTopDownAnalyzer( private val identifierChecker: IdentifierChecker, private val languageVersionSettings: LanguageVersionSettings, private val deprecationResolver: DeprecationResolver, - private val classifierUsageCheckers: Iterable + private val classifierUsageCheckers: Iterable, + private val filePreprocessor: FilePreprocessor ) { fun analyzeDeclarations( topDownAnalysisMode: TopDownAnalysisMode, @@ -91,7 +92,7 @@ class LazyTopDownAnalyzer( } override fun visitKtFile(file: KtFile) { - DescriptorResolver.registerFileInPackage(trace, file) + filePreprocessor.preprocessFile(file) registerDeclarations(file.declarations) val packageDirective = file.packageDirective assert(file.isScript() || packageDirective != null) { "No package in a non-script file: " + file }