Skip to content

Commit

Permalink
CLI: refactor the code around prepareJvmSessions & FrontendContext
Browse files Browse the repository at this point in the history
  • Loading branch information
mglukhikh authored and qodana-bot committed Sep 12, 2024
1 parent aeb58a8 commit 7e43572
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand All @@ -43,19 +49,70 @@ 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<KtFile>,
rootModuleNameAsString: String,
friendPaths: List<String>,
librariesScope: AbstractProjectFileSearchScope,
isScript: (KtFile) -> Boolean,
createProviderAndScopeForIncrementalCompilation: (List<KtFile>) -> IncrementalCompilationContext?,
): List<SessionWithSources<KtFile>> {
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:
* - disabled
* - legacy (one platform and one common module)
* - HMPP (multiple number of modules)
*/
fun <F> prepareJvmSessions(
internal fun <F> FrontendContext.prepareJvmSessions(
files: List<F>,
rootModuleNameAsString: String,
friendPaths: List<String>,
librariesScope: AbstractProjectFileSearchScope,
isCommonSource: (F) -> Boolean,
isScript: (F) -> Boolean,
fileBelongsToModule: (F, String) -> Boolean,
createProviderAndScopeForIncrementalCompilation: (List<F>) -> IncrementalCompilationContext?,
): List<SessionWithSources<F>> {
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<SessionWithSources<KtFile>> {
return MinimizedFrontendContext(environment, MessageCollector.NONE, emptyList(), configuration).prepareJvmSessions(
files = emptyList(),
moduleName,
environment.getSearchScopeForProjectLibraries(),
libraryList,
isCommonSource = { false },
isScript = { false },
fileBelongsToModule = { _, _ -> false },
createProviderAndScopeForIncrementalCompilation = { null }
)
}

internal fun <F> FrontendContext.prepareJvmSessions(
files: List<F>,
rootModuleName: Name,
extensionRegistrars: List<FirExtensionRegistrar>,
librariesScope: AbstractProjectFileSearchScope,
libraryList: DependencyListForCliModule,
isCommonSource: (F) -> Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@
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
import org.jetbrains.kotlin.cli.jvm.compiler.pipeline.compileSourceFilesToAnalyzedFirViaPsi
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 {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ open class VfsBasedProjectEnvironment(
} ?: GlobalSearchScope.EMPTY_SCOPE
)

fun getSearchScopeByPsiFiles(files: Iterable<PsiFile>, allowOutOfProjectRoots: Boolean= false): AbstractProjectFileSearchScope =
fun getSearchScopeByPsiFiles(files: Iterable<PsiFile>): AbstractProjectFileSearchScope =
PsiBasedProjectFileSearchScope(
files.map { it.virtualFile }.let {
if (allowOutOfProjectRoots) GlobalSearchScope.filesWithLibrariesScope(project, it)
else GlobalSearchScope.filesWithoutLibrariesScope(project, it)
GlobalSearchScope.filesWithoutLibrariesScope(project, it)
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -197,11 +196,12 @@ fun ModuleBuilder.configureFromArgs(args: K2JVMCompilerArguments) {

fun createContextForIncrementalCompilation(
projectEnvironment: AbstractProjectEnvironment,
incrementalComponents: IncrementalCompilationComponents?,
moduleConfiguration: CompilerConfiguration,
targetIds: List<TargetId>?,
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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<KtFile>,
projectEnvironment: VfsBasedProjectEnvironment,
messageCollector: MessageCollector,
renderDiagnosticName: Boolean,
configuration: CompilerConfiguration,
targetIds: List<TargetId>?,
incrementalComponents: IncrementalCompilationComponents?,
extensionRegistrars: List<FirExtensionRegistrar>
) : 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<TargetId>?,
incrementalComponents: IncrementalCompilationComponents?,
extensionRegistrars: List<FirExtensionRegistrar>
) : 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<TargetId>?,
override val incrementalComponents: IncrementalCompilationComponents?,
override val extensionRegistrars: List<FirExtensionRegistrar>
) : FrontendContext

class FrontendContextForMultiChunkMode private constructor(
override val projectEnvironment: VfsBasedProjectEnvironment,
override val messageCollector: MessageCollector,
override val incrementalComponents: IncrementalCompilationComponents?,
override val extensionRegistrars: List<FirExtensionRegistrar>,
override val configuration: CompilerConfiguration,
override val targetIds: List<TargetId>?
) : 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> = 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<FirExtensionRegistrar>,
override val configuration: CompilerConfiguration,
override val targetIds: List<TargetId>?,
) : 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<FirExtensionRegistrar>
val configuration: CompilerConfiguration
val targetIds: List<TargetId>?
}
Loading

0 comments on commit 7e43572

Please sign in to comment.