diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/FirSessionConstructionUtils.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/common/FirSessionConstructionUtils.kt index fb356944b0e8e..97e9b06fb41a9 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/FirSessionConstructionUtils.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/FirSessionConstructionUtils.kt @@ -6,6 +6,11 @@ package org.jetbrains.kotlin.cli.common import org.jetbrains.kotlin.KtSourceFile +import org.jetbrains.kotlin.cli.common.messages.MessageCollector +import org.jetbrains.kotlin.cli.jvm.compiler.VfsBasedProjectEnvironment +import org.jetbrains.kotlin.cli.jvm.compiler.createLibraryListForJvm +import org.jetbrains.kotlin.cli.jvm.compiler.pipeline.FrontendContext +import org.jetbrains.kotlin.cli.jvm.compiler.pipeline.MinimizedFrontendContext import org.jetbrains.kotlin.config.* import org.jetbrains.kotlin.fir.* import org.jetbrains.kotlin.fir.analysis.checkers.CliOnlyLanguageVersionSettingsCheckers @@ -33,6 +38,7 @@ import org.jetbrains.kotlin.resolve.multiplatform.hmppModuleName import org.jetbrains.kotlin.resolve.multiplatform.isCommonSource import org.jetbrains.kotlin.wasm.config.WasmConfigurationKeys import org.jetbrains.kotlin.wasm.config.wasmTarget +import kotlin.collections.orEmpty val isCommonSourceForPsi: (KtFile) -> Boolean = { it.isCommonSource == true } val fileBelongsToModuleForPsi: (KtFile, String) -> Boolean = { file, moduleName -> file.hmppModuleName == moduleName } @@ -43,6 +49,24 @@ val GroupedKtSources.isCommonSourceForLt: (KtSourceFile) -> Boolean val GroupedKtSources.fileBelongsToModuleForLt: (KtSourceFile, String) -> Boolean get() = { file, moduleName -> sourcesByModuleName[moduleName].orEmpty().contains(file) } +fun prepareJvmSessionsForScripting( + projectEnvironment: AbstractProjectEnvironment, + configuration: CompilerConfiguration, + files: List, + rootModuleNameAsString: String, + friendPaths: List, + librariesScope: AbstractProjectFileSearchScope, + isScript: (KtFile) -> Boolean, + createProviderAndScopeForIncrementalCompilation: (List) -> IncrementalCompilationContext?, +): List> { + val extensionRegistrars = (projectEnvironment as? VfsBasedProjectEnvironment) + ?.let { FirExtensionRegistrar.getInstances(it.project) }.orEmpty() + return MinimizedFrontendContext(projectEnvironment, MessageCollector.NONE, extensionRegistrars, configuration).prepareJvmSessions( + files, rootModuleNameAsString, friendPaths, librariesScope, isCommonSourceForPsi, isScript, + fileBelongsToModuleForPsi, createProviderAndScopeForIncrementalCompilation + ) +} + /** * Creates library session and sources session for JVM platform * Number of created session depends on mode of MPP: @@ -50,12 +74,45 @@ val GroupedKtSources.fileBelongsToModuleForLt: (KtSourceFile, String) -> Boolean * - legacy (one platform and one common module) * - HMPP (multiple number of modules) */ -fun prepareJvmSessions( +internal fun FrontendContext.prepareJvmSessions( files: List, + rootModuleNameAsString: String, + friendPaths: List, + librariesScope: AbstractProjectFileSearchScope, + isCommonSource: (F) -> Boolean, + isScript: (F) -> Boolean, + fileBelongsToModule: (F, String) -> Boolean, + createProviderAndScopeForIncrementalCompilation: (List) -> IncrementalCompilationContext?, +): List> { + val libraryList = createLibraryListForJvm(rootModuleNameAsString, configuration, friendPaths) + val rootModuleName = Name.special("<$rootModuleNameAsString>") + return prepareJvmSessions( + files, rootModuleName, librariesScope, libraryList, + isCommonSource, isScript, fileBelongsToModule, createProviderAndScopeForIncrementalCompilation + ) +} + +fun prepareJvmSessionsWithoutFiles( configuration: CompilerConfiguration, - projectEnvironment: AbstractProjectEnvironment, + environment: VfsBasedProjectEnvironment, + moduleName: Name, + libraryList: DependencyListForCliModule +): List> { + return MinimizedFrontendContext(environment, MessageCollector.NONE, emptyList(), configuration).prepareJvmSessions( + files = emptyList(), + moduleName, + environment.getSearchScopeForProjectLibraries(), + libraryList, + isCommonSource = { false }, + isScript = { false }, + fileBelongsToModule = { _, _ -> false }, + createProviderAndScopeForIncrementalCompilation = { null } + ) +} + +internal fun FrontendContext.prepareJvmSessions( + files: List, rootModuleName: Name, - extensionRegistrars: List, librariesScope: AbstractProjectFileSearchScope, libraryList: DependencyListForCliModule, isCommonSource: (F) -> Boolean, diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/FirKotlinToJvmBytecodeCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/FirKotlinToJvmBytecodeCompiler.kt index e62cae9aa891e..1ed542e227baf 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/FirKotlinToJvmBytecodeCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/FirKotlinToJvmBytecodeCompiler.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.cli.jvm.compiler import org.jetbrains.kotlin.cli.common.CLICompiler -import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.cli.jvm.compiler.pipeline.FrontendContextForSingleModulePsi @@ -16,13 +15,10 @@ import org.jetbrains.kotlin.cli.jvm.compiler.pipeline.compileSourceFilesToAnalyz import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar import org.jetbrains.kotlin.config.CompilerConfiguration -import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.diagnostics.DiagnosticReporterFactory import org.jetbrains.kotlin.diagnostics.impl.PendingDiagnosticsCollectorWithSuppress -import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar import org.jetbrains.kotlin.fir.pipeline.FirResult import org.jetbrains.kotlin.modules.Module -import org.jetbrains.kotlin.modules.TargetId import org.jetbrains.kotlin.psi.KtFile object FirKotlinToJvmBytecodeCompiler { @@ -64,11 +60,7 @@ object FirKotlinToJvmBytecodeCompiler { sources, environment, messageCollector, - configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME), configuration, - configuration.get(JVMConfigurationKeys.MODULES)?.map(::TargetId), - configuration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS), - extensionRegistrars = FirExtensionRegistrar.getInstances(environment.project), ) val diagnosticsReporter = createPendingReporter(messageCollector) return context.compileSourceFilesToAnalyzedFirViaPsi( diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/VfsBasedProjectEnvironment.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/VfsBasedProjectEnvironment.kt index 651dcb240b05f..c52c9b2880483 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/VfsBasedProjectEnvironment.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/VfsBasedProjectEnvironment.kt @@ -121,11 +121,10 @@ open class VfsBasedProjectEnvironment( } ?: GlobalSearchScope.EMPTY_SCOPE ) - fun getSearchScopeByPsiFiles(files: Iterable, allowOutOfProjectRoots: Boolean= false): AbstractProjectFileSearchScope = + fun getSearchScopeByPsiFiles(files: Iterable): AbstractProjectFileSearchScope = PsiBasedProjectFileSearchScope( files.map { it.virtualFile }.let { - if (allowOutOfProjectRoots) GlobalSearchScope.filesWithLibrariesScope(project, it) - else GlobalSearchScope.filesWithoutLibrariesScope(project, it) + GlobalSearchScope.filesWithoutLibrariesScope(project, it) } ) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/cliCompilerUtils.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/cliCompilerUtils.kt index 78479109e5876..ab24cbc18f33f 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/cliCompilerUtils.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/cliCompilerUtils.kt @@ -33,7 +33,6 @@ import org.jetbrains.kotlin.fir.session.environment.AbstractProjectEnvironment import org.jetbrains.kotlin.fir.session.environment.AbstractProjectFileSearchScope import org.jetbrains.kotlin.javac.JavacWrapper import org.jetbrains.kotlin.load.kotlin.incremental.IncrementalPackagePartProvider -import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents import org.jetbrains.kotlin.modules.JavaRootPath import org.jetbrains.kotlin.modules.Module import org.jetbrains.kotlin.modules.TargetId @@ -197,11 +196,12 @@ fun ModuleBuilder.configureFromArgs(args: K2JVMCompilerArguments) { fun createContextForIncrementalCompilation( projectEnvironment: AbstractProjectEnvironment, - incrementalComponents: IncrementalCompilationComponents?, moduleConfiguration: CompilerConfiguration, - targetIds: List?, sourceScope: AbstractProjectFileSearchScope, ): IncrementalCompilationContext? { + val incrementalComponents = moduleConfiguration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS) + val targetIds = moduleConfiguration.get(JVMConfigurationKeys.MODULES)?.map(::TargetId) + if (targetIds == null || incrementalComponents == null) return null val directoryWithIncrementalPartsFromPreviousCompilation = moduleConfiguration[JVMConfigurationKeys.OUTPUT_DIRECTORY] diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/FrontendContext.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/FrontendContext.kt index ed90432ae072d..3f17c049ab512 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/FrontendContext.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/FrontendContext.kt @@ -6,125 +6,66 @@ package org.jetbrains.kotlin.cli.jvm.compiler.pipeline import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.GroupedKtSources import org.jetbrains.kotlin.cli.common.messages.MessageCollector -import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.cli.jvm.compiler.VfsBasedProjectEnvironment -import org.jetbrains.kotlin.cli.jvm.compiler.messageCollector import org.jetbrains.kotlin.config.CompilerConfiguration -import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar -import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents +import org.jetbrains.kotlin.fir.session.environment.AbstractProjectEnvironment import org.jetbrains.kotlin.modules.Module -import org.jetbrains.kotlin.modules.TargetId import org.jetbrains.kotlin.psi.KtFile -import kotlin.collections.map internal class FrontendContextForSingleModulePsi( module: Module, val allSources: List, projectEnvironment: VfsBasedProjectEnvironment, messageCollector: MessageCollector, - renderDiagnosticName: Boolean, - configuration: CompilerConfiguration, - targetIds: List?, - incrementalComponents: IncrementalCompilationComponents?, - extensionRegistrars: List -) : FrontendContextForSingleModule( - module, projectEnvironment, messageCollector, renderDiagnosticName, - configuration, targetIds, incrementalComponents, - extensionRegistrars -) + configuration: CompilerConfiguration +) : FrontendContextForSingleModule(module, projectEnvironment, messageCollector, configuration) internal class FrontendContextForSingleModuleLightTree( module: Module, val groupedSources: GroupedKtSources, projectEnvironment: VfsBasedProjectEnvironment, messageCollector: MessageCollector, - renderDiagnosticName: Boolean, - configuration: CompilerConfiguration, - targetIds: List?, - incrementalComponents: IncrementalCompilationComponents?, - extensionRegistrars: List -) : FrontendContextForSingleModule( - module, projectEnvironment, messageCollector, renderDiagnosticName, - configuration, targetIds, incrementalComponents, - extensionRegistrars -) + configuration: CompilerConfiguration +) : FrontendContextForSingleModule(module, projectEnvironment, messageCollector, configuration) -internal abstract class FrontendContextForSingleModule( +internal sealed class FrontendContextForSingleModule( val module: Module, override val projectEnvironment: VfsBasedProjectEnvironment, override val messageCollector: MessageCollector, - val renderDiagnosticName: Boolean, override val configuration: CompilerConfiguration, - override val targetIds: List?, - override val incrementalComponents: IncrementalCompilationComponents?, - override val extensionRegistrars: List -) : FrontendContext - -class FrontendContextForMultiChunkMode private constructor( - override val projectEnvironment: VfsBasedProjectEnvironment, - override val messageCollector: MessageCollector, - override val incrementalComponents: IncrementalCompilationComponents?, - override val extensionRegistrars: List, - override val configuration: CompilerConfiguration, - override val targetIds: List? ) : FrontendContext { - constructor( - projectEnvironment: VfsBasedProjectEnvironment, - environment: KotlinCoreEnvironment, - compilerConfiguration: CompilerConfiguration, - project: Project? - ) : this( - projectEnvironment, - environment.messageCollector, - compilerConfiguration, - project - ) + val renderDiagnosticName: Boolean + get() = configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME) - constructor( - projectEnvironment: VfsBasedProjectEnvironment, - messageCollector: MessageCollector, - compilerConfiguration: CompilerConfiguration, - project: Project?, - ) : this( - projectEnvironment, - messageCollector, - incrementalComponents = compilerConfiguration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS), - extensionRegistrars = project?.let { FirExtensionRegistrar.getInstances(it) } ?: emptyList(), - configuration = compilerConfiguration, - targetIds = compilerConfiguration.get(JVMConfigurationKeys.MODULES)?.map(::TargetId) - ) + override val extensionRegistrars: List = FirExtensionRegistrar.getInstances(projectEnvironment.project) } -class FrontendContextForIncrementalCompilation( - override val projectEnvironment: VfsBasedProjectEnvironment, +internal fun createFrontendContextForMultiChunkMode( + projectEnvironment: VfsBasedProjectEnvironment, + messageCollector: MessageCollector, + compilerConfiguration: CompilerConfiguration, + project: Project?, +): FrontendContext = MinimizedFrontendContext( + projectEnvironment, + messageCollector, + extensionRegistrars = project?.let { FirExtensionRegistrar.getInstances(it) } ?: emptyList(), + configuration = compilerConfiguration +) + +internal class MinimizedFrontendContext( + override val projectEnvironment: AbstractProjectEnvironment, override val messageCollector: MessageCollector, - override val incrementalComponents: IncrementalCompilationComponents?, override val extensionRegistrars: List, - override val configuration: CompilerConfiguration, - override val targetIds: List?, -) : FrontendContext { - constructor( - projectEnvironment: VfsBasedProjectEnvironment, - messageCollector: MessageCollector, - compilerConfiguration: CompilerConfiguration, - ) : this( - projectEnvironment, - messageCollector, - incrementalComponents = compilerConfiguration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS), - extensionRegistrars = projectEnvironment.project.let { FirExtensionRegistrar.getInstances(it) }, - configuration = compilerConfiguration, - targetIds = compilerConfiguration.get(JVMConfigurationKeys.MODULES)?.map(::TargetId) - ) -} + override val configuration: CompilerConfiguration +) : FrontendContext -interface FrontendContext { - val projectEnvironment: VfsBasedProjectEnvironment +internal interface FrontendContext { + val projectEnvironment: AbstractProjectEnvironment val messageCollector: MessageCollector - val incrementalComponents: IncrementalCompilationComponents? val extensionRegistrars: List val configuration: CompilerConfiguration - val targetIds: List? } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/jvmCompilerPipelineLightTree.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/jvmCompilerPipelineLightTree.kt index b052895f1037e..14c5e0dbc93e5 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/jvmCompilerPipelineLightTree.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/jvmCompilerPipelineLightTree.kt @@ -28,7 +28,6 @@ import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.runBack import org.jetbrains.kotlin.cli.jvm.compiler.NoScopeRecordCliBindingTrace import org.jetbrains.kotlin.cli.jvm.compiler.VfsBasedProjectEnvironment import org.jetbrains.kotlin.cli.jvm.compiler.applyModuleProperties -import org.jetbrains.kotlin.cli.jvm.compiler.createLibraryListForJvm import org.jetbrains.kotlin.cli.jvm.compiler.findMainClass import org.jetbrains.kotlin.cli.jvm.compiler.writeOutputsIfNeeded import org.jetbrains.kotlin.codegen.state.GenerationState @@ -48,10 +47,8 @@ import org.jetbrains.kotlin.fir.pipeline.runPlatformCheckers import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolProvider import org.jetbrains.kotlin.fir.session.environment.AbstractProjectFileSearchScope import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager -import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents import org.jetbrains.kotlin.modules.Module import org.jetbrains.kotlin.modules.TargetId -import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus import org.jetbrains.kotlin.utils.addToStdlib.runIf import java.io.File @@ -126,7 +123,7 @@ private fun compileMultiModuleChunkUsingFrontendIrAndLightTree( } val diagnosticsReporter = DiagnosticReporterFactory.createPendingReporter(messageCollector) - val frontendContext = FrontendContextForMultiChunkMode( + val frontendContext = createFrontendContextForMultiChunkMode( projectEnvironment, messageCollector, compilerConfiguration, project ) @@ -140,16 +137,15 @@ private fun compileMultiModuleChunkUsingFrontendIrAndLightTree( projectEnvironment, null, )?.also { librariesScope -= it } - val libraryList = createLibraryListForJvm( - targetDescription, configuration, chunk.fold(emptyList()) { paths, m -> paths + m.getFriendPaths() } - ) val allSources = mutableListOf().apply { addAll(groupedSources.commonSources) addAll(groupedSources.platformSources) } val sessionsWithSources = prepareJvmSessions( - allSources, configuration, this.projectEnvironment, Name.special("<$targetDescription>"), - extensionRegistrars, librariesScope, libraryList, + allSources, + targetDescription, + friendPaths = chunk.fold(emptyList()) { paths, m -> paths + m.getFriendPaths() }, + librariesScope, isCommonSource = groupedSources.isCommonSourceForLt, isScript = { false }, fileBelongsToModule = groupedSources.fileBelongsToModuleForLt, @@ -224,17 +220,12 @@ private fun compileSingleModuleUsingFrontendIrAndLightTree( ): Boolean { val moduleConfiguration = compilerConfiguration.applyModuleProperties(module, buildFile) - val renderDiagnosticNames = moduleConfiguration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME) val context = FrontendContextForSingleModuleLightTree( module, groupedSources, projectEnvironment, messageCollector, - renderDiagnosticNames, - moduleConfiguration, - targetIds = compilerConfiguration.get>(JVMConfigurationKeys.MODULES)?.map(::TargetId), - incrementalComponents = compilerConfiguration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS), - extensionRegistrars = FirExtensionRegistrar.getInstances(project) + moduleConfiguration ) val (firResult, generationState) = context.compileModule() ?: return false @@ -281,7 +272,33 @@ private fun FrontendContext.compileModuleToAnalyzedFirViaLightTree( input, diagnosticsReporter, emptyList(), null, friendPaths ) -fun FrontendContext.compileModuleToAnalyzedFirViaLightTreeIncrementally( +@RequiresOptIn(message = "In compiler:cli, please use FrontendContext extensions instead") +annotation class IncrementalCompilationApi + +@IncrementalCompilationApi +fun compileModuleToAnalyzedFirViaLightTreeIncrementally( + projectEnvironment: VfsBasedProjectEnvironment, + messageCollector: MessageCollector, + compilerConfiguration: CompilerConfiguration, + input: ModuleCompilerInput, + diagnosticsReporter: BaseDiagnosticsCollector, + incrementalExcludesScope: AbstractProjectFileSearchScope?, +): FirResult { + return MinimizedFrontendContext( + projectEnvironment, + messageCollector, + FirExtensionRegistrar.getInstances(projectEnvironment.project), + compilerConfiguration + ).compileModuleToAnalyzedFirViaLightTreeIncrementally( + input, + diagnosticsReporter, + previousStepsSymbolProviders = emptyList(), + incrementalExcludesScope, + friendPaths = emptyList() + ) +} + +internal fun FrontendContext.compileModuleToAnalyzedFirViaLightTreeIncrementally( input: ModuleCompilerInput, diagnosticsReporter: BaseDiagnosticsCollector, previousStepsSymbolProviders: List, @@ -292,7 +309,6 @@ fun FrontendContext.compileModuleToAnalyzedFirViaLightTreeIncrementally( performanceManager?.notifyAnalysisStarted() var librariesScope = projectEnvironment.getSearchScopeForProjectLibraries() - val rootModuleName = input.targetId.name val incrementalCompilationScope = createIncrementalCompilationScope( configuration, @@ -304,10 +320,11 @@ fun FrontendContext.compileModuleToAnalyzedFirViaLightTreeIncrementally( addAll(input.groupedSources.commonSources) addAll(input.groupedSources.platformSources) } - val libraryList = createLibraryListForJvm(rootModuleName, configuration, friendPaths) val sessionsWithSources = prepareJvmSessions( - allSources, configuration, projectEnvironment, Name.special("<$rootModuleName>"), - extensionRegistrars, librariesScope, libraryList, + allSources, + rootModuleNameAsString = input.targetId.name, + friendPaths, + librariesScope, isCommonSource = input.groupedSources.isCommonSourceForLt, isScript = { false }, fileBelongsToModule = input.groupedSources.fileBelongsToModuleForLt, diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/jvmCompilerPipelinePsi.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/jvmCompilerPipelinePsi.kt index 54b358aacd0b5..171a867741365 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/jvmCompilerPipelinePsi.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/pipeline/jvmCompilerPipelinePsi.kt @@ -23,7 +23,6 @@ import org.jetbrains.kotlin.cli.jvm.compiler.NoScopeRecordCliBindingTrace import org.jetbrains.kotlin.cli.jvm.compiler.VfsBasedProjectEnvironment import org.jetbrains.kotlin.cli.jvm.compiler.applyModuleProperties import org.jetbrains.kotlin.cli.jvm.compiler.createContextForIncrementalCompilation -import org.jetbrains.kotlin.cli.jvm.compiler.createLibraryListForJvm import org.jetbrains.kotlin.cli.jvm.compiler.findMainClass import org.jetbrains.kotlin.cli.jvm.compiler.writeOutputsIfNeeded import org.jetbrains.kotlin.codegen.state.GenerationState @@ -37,15 +36,11 @@ import org.jetbrains.kotlin.fir.backend.jvm.FirJvmBackendExtension import org.jetbrains.kotlin.fir.backend.jvm.JvmFir2IrExtensions import org.jetbrains.kotlin.fir.backend.utils.extractFirDeclarations import org.jetbrains.kotlin.fir.extensions.FirAnalysisHandlerExtension -import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar import org.jetbrains.kotlin.fir.pipeline.FirResult import org.jetbrains.kotlin.fir.pipeline.buildResolveAndCheckFirFromKtFiles import org.jetbrains.kotlin.fir.pipeline.runPlatformCheckers -import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents import org.jetbrains.kotlin.modules.Module -import org.jetbrains.kotlin.modules.TargetId import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.multiplatform.hmppModuleName @@ -73,11 +68,7 @@ fun compileSingleModuleUsingFrontendIrAndPsi( allSources, projectEnvironment, messageCollector, - moduleConfiguration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME), - moduleConfiguration, - targetIds = compilerConfiguration.get>(JVMConfigurationKeys.MODULES)?.map(::TargetId), - incrementalComponents = compilerConfiguration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS), - extensionRegistrars = FirExtensionRegistrar.getInstances(project) + moduleConfiguration ) val (firResult, generationState) = context.compileModule() ?: return false @@ -104,8 +95,8 @@ internal fun runFrontendAndGenerateIrForMultiModuleChunkUsingFrontendIRAndPsi( val project = projectEnvironment.project val messageCollector = environment.configuration.getNotNull(CommonConfigurationKeys.MESSAGE_COLLECTOR_KEY) val diagnosticsReporter = DiagnosticReporterFactory.createPendingReporter(messageCollector) - val frontendContext = FrontendContextForMultiChunkMode( - projectEnvironment, environment, compilerConfiguration, project + val frontendContext = createFrontendContextForMultiChunkMode( + projectEnvironment, messageCollector, compilerConfiguration, project ) with(frontendContext) { @@ -157,7 +148,7 @@ private fun FrontendContextForSingleModulePsi.compileModule(): Pair, diagnosticsReporter: BaseDiagnosticsCollector, rootModuleName: String, @@ -173,25 +164,21 @@ fun FrontendContext.compileSourceFilesToAnalyzedFirViaPsi( val scriptsInCommonSourcesErrors = reportCommonScriptsError(ktFiles) - val sourceScope = projectEnvironment.getSearchScopeByPsiFiles(ktFiles) + projectEnvironment.getSearchScopeForProjectJavaSources() + val sourceScope = (projectEnvironment as VfsBasedProjectEnvironment).getSearchScopeByPsiFiles(ktFiles) + + projectEnvironment.getSearchScopeForProjectJavaSources() var librariesScope = projectEnvironment.getSearchScopeForProjectLibraries() - val providerAndScopeForIncrementalCompilation = createContextForIncrementalCompilation( - projectEnvironment, - incrementalComponents, - configuration, - targetIds, - sourceScope - ) + val providerAndScopeForIncrementalCompilation = createContextForIncrementalCompilation(projectEnvironment, configuration, sourceScope) providerAndScopeForIncrementalCompilation?.precompiledBinariesFileScope?.let { librariesScope -= it } - val libraryList = createLibraryListForJvm(rootModuleName, configuration, friendPaths) val sessionsWithSources = prepareJvmSessions( - ktFiles, configuration, projectEnvironment, Name.special("<$rootModuleName>"), - extensionRegistrars, librariesScope, libraryList, + ktFiles, + rootModuleName, + friendPaths, + librariesScope, isCommonSource = { it.isCommonSource == true }, isScript = { it.isScript() }, fileBelongsToModule = { file, moduleName -> file.hmppModuleName == moduleName }, diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/metadata/AbstractFirMetadataSerializer.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/metadata/AbstractFirMetadataSerializer.kt index f8d261c96cd98..15a7086a5fa72 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/metadata/AbstractFirMetadataSerializer.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/metadata/AbstractFirMetadataSerializer.kt @@ -27,7 +27,6 @@ import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots import org.jetbrains.kotlin.cli.jvm.config.jvmModularRoots import org.jetbrains.kotlin.config.CommonConfigurationKeys import org.jetbrains.kotlin.config.CompilerConfiguration -import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.diagnostics.DiagnosticReporterFactory import org.jetbrains.kotlin.fir.BinaryModuleData import org.jetbrains.kotlin.fir.DependencyListForCliModule @@ -39,7 +38,6 @@ import org.jetbrains.kotlin.fir.pipeline.resolveAndCheckFir import org.jetbrains.kotlin.fir.pipeline.runPlatformCheckers import org.jetbrains.kotlin.library.metadata.resolver.impl.KotlinResolvedLibraryImpl import org.jetbrains.kotlin.library.resolveSingleFileKlib -import org.jetbrains.kotlin.modules.TargetId import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.platform.CommonPlatforms import java.io.File @@ -129,9 +127,7 @@ internal abstract class AbstractFirMetadataSerializer( projectEnvironment.getSearchScopeByPsiFiles(psiFiles) + projectEnvironment.getSearchScopeForProjectJavaSources() val providerAndScopeForIncrementalCompilation = org.jetbrains.kotlin.cli.jvm.compiler.createContextForIncrementalCompilation( projectEnvironment, - configuration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS), configuration, - configuration.get(JVMConfigurationKeys.MODULES)?.map(::TargetId), sourceScope ) providerAndScopeForIncrementalCompilation?.precompiledBinariesFileScope?.let { diff --git a/compiler/fir/analysis-tests/test/org/jetbrains/kotlin/test/LoadedMetadataDumpHandler.kt b/compiler/fir/analysis-tests/test/org/jetbrains/kotlin/test/LoadedMetadataDumpHandler.kt index ad7a56ec82239..3d406c23e318d 100644 --- a/compiler/fir/analysis-tests/test/org/jetbrains/kotlin/test/LoadedMetadataDumpHandler.kt +++ b/compiler/fir/analysis-tests/test/org/jetbrains/kotlin/test/LoadedMetadataDumpHandler.kt @@ -11,7 +11,7 @@ import org.jetbrains.kotlin.backend.common.CommonKLibResolver import org.jetbrains.kotlin.cli.common.SessionWithSources import org.jetbrains.kotlin.cli.common.messages.getLogger import org.jetbrains.kotlin.cli.common.prepareJsSessions -import org.jetbrains.kotlin.cli.common.prepareJvmSessions +import org.jetbrains.kotlin.cli.common.prepareJvmSessionsWithoutFiles import org.jetbrains.kotlin.cli.jvm.compiler.VfsBasedProjectEnvironment import org.jetbrains.kotlin.config.ApiVersion import org.jetbrains.kotlin.config.CompilerConfiguration @@ -75,17 +75,7 @@ class JvmLoadedMetadataDumpHandler(testServices: TestServices) : AbstractLoadedM moduleName: Name, libraryList: DependencyListForCliModule, ): List> { - return prepareJvmSessions( - files = emptyList(), - configuration, environment, moduleName, - extensionRegistrars = emptyList(), - environment.getSearchScopeForProjectLibraries(), - libraryList, - isCommonSource = { false }, - isScript = { false }, - fileBelongsToModule = { _, _ -> false }, - createProviderAndScopeForIncrementalCompilation = { null } - ) + return prepareJvmSessionsWithoutFiles(configuration, environment, moduleName, libraryList) } } diff --git a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/environment/AbstractProjectEnvironment.kt b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/environment/AbstractProjectEnvironment.kt index 517e3817041f6..33515d41df882 100644 --- a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/environment/AbstractProjectEnvironment.kt +++ b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/environment/AbstractProjectEnvironment.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.fir.session.environment +import com.intellij.psi.PsiFile import org.jetbrains.kotlin.KtSourceFile import org.jetbrains.kotlin.fir.FirModuleData import org.jetbrains.kotlin.fir.FirSession diff --git a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalFirJvmCompilerRunner.kt b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalFirJvmCompilerRunner.kt index 9fadcc5ab9d0e..6fd3b6810c36c 100644 --- a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalFirJvmCompilerRunner.kt +++ b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalFirJvmCompilerRunner.kt @@ -200,19 +200,14 @@ open class IncrementalFirJvmCompilerRunner( sourcesByModuleName = dirtySourcesByModuleName ) - val context = FrontendContextForIncrementalCompilation( - projectEnvironment, - messageCollector, - configuration, - ) - val analysisResults = - context.compileModuleToAnalyzedFirViaLightTreeIncrementally( + @OptIn(IncrementalCompilationApi::class) compileModuleToAnalyzedFirViaLightTreeIncrementally( + projectEnvironment, + messageCollector, + configuration, ModuleCompilerInput(targetId, groupedSources, configuration), diagnosticsReporter, - previousStepsSymbolProviders = emptyList(), incrementalExcludesScope, - friendPaths = emptyList(), ) // TODO: consider what to do if many compilations find a main class diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJvmCompilerImpls.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJvmCompilerImpls.kt index 0f4d341f399a5..944b75dad1f0f 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJvmCompilerImpls.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJvmCompilerImpls.kt @@ -24,11 +24,9 @@ import org.jetbrains.kotlin.config.* import org.jetbrains.kotlin.diagnostics.DiagnosticReporterFactory import org.jetbrains.kotlin.fir.analysis.checkers.MppCheckerKind import org.jetbrains.kotlin.fir.declarations.FirFile -import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar import org.jetbrains.kotlin.fir.pipeline.* import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil import org.jetbrains.kotlin.modules.TargetId -import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.jvm.extensions.PackageFragmentProviderExtension import org.jetbrains.kotlin.scripting.compiler.plugin.ScriptCompilerProxy @@ -338,20 +336,15 @@ private fun doCompileWithK2( projectEnvironment, incrementalExcludesScope = null )?.also { librariesScope -= it } - val extensionRegistrars = (projectEnvironment as? VfsBasedProjectEnvironment) - ?.let { FirExtensionRegistrar.getInstances(it.project) } - .orEmpty() - val rootModuleName = targetId.name - val libraryList = createLibraryListForJvm( - rootModuleName, + val session = prepareJvmSessionsForScripting( + projectEnvironment, configuration, - friendPaths = emptyList() - ) - val session = prepareJvmSessions( - sourceFiles, configuration, projectEnvironment, Name.special("<$rootModuleName>"), extensionRegistrars, - librariesScope, libraryList, isCommonSourceForPsi, { false }, - fileBelongsToModuleForPsi, + sourceFiles, + rootModuleNameAsString = targetId.name, + friendPaths = emptyList(), + librariesScope, + isScript = { false }, createProviderAndScopeForIncrementalCompilation = { files -> createContextForIncrementalCompilation( configuration,