JVM IR: introduce CodegenFactory.convertToIr

The steps of psi2ir and JVM backend need to be separated in the API
because in case of cyclic module dependencies (which are allowed in JPS)
psi2ir should be run first on all sources, and then JVM backend on each
module separately. `CodegenFactory.convertToIr` does nothing in the old
backend.

Also, move the ignoreErrors to GenerationState for simplicity.
This commit is contained in:
Alexander Udalov
2021-09-13 23:42:32 +02:00
parent bfeb9c219d
commit 5d6ca27c93
9 changed files with 79 additions and 38 deletions
@@ -16,15 +16,43 @@
package org.jetbrains.kotlin.codegen
import com.intellij.openapi.project.Project
import com.intellij.util.containers.MultiMap
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingContext
interface CodegenFactory {
fun generateModule(state: GenerationState, files: Collection<KtFile>)
fun convertToIr(input: IrConversionInput): BackendInput
fun generateModule(state: GenerationState, input: BackendInput)
class IrConversionInput(
val project: Project,
val files: Collection<KtFile>,
val configuration: CompilerConfiguration,
val module: ModuleDescriptor,
val bindingContext: BindingContext,
val languageVersionSettings: LanguageVersionSettings,
val ignoreErrors: Boolean,
) {
companion object {
fun fromGenerationState(state: GenerationState): IrConversionInput =
with(state) {
IrConversionInput(
project, files, configuration, module, originalFrontendBindingContext, languageVersionSettings, ignoreErrors
)
}
}
}
interface BackendInput
companion object {
fun doCheckCancelled(state: GenerationState) {
@@ -36,11 +64,15 @@ interface CodegenFactory {
}
object DefaultCodegenFactory : CodegenFactory {
override fun generateModule(state: GenerationState, files: Collection<KtFile>) {
object DummyOldBackendInput : CodegenFactory.BackendInput
override fun convertToIr(input: CodegenFactory.IrConversionInput): CodegenFactory.BackendInput = DummyOldBackendInput
override fun generateModule(state: GenerationState, input: CodegenFactory.BackendInput) {
val filesInPackages = MultiMap<FqName, KtFile>()
val filesInMultifileClasses = MultiMap<FqName, KtFile>()
for (file in files) {
for (file in state.files) {
val fileClassInfo = JvmFileClassUtil.getFileClassInfoNoResolve(file)
if (fileClassInfo.withJvmMultifileClass) {
@@ -32,7 +32,12 @@ public class KotlinCodegenFacade {
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
state.getCodegenFactory().generateModule(state, state.getFiles());
CodegenFactory.IrConversionInput psi2irInput = CodegenFactory.IrConversionInput.Companion.fromGenerationState(state);
CodegenFactory.BackendInput backendInput = state.getCodegenFactory().convertToIr(psi2irInput);
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
state.getCodegenFactory().generateModule(state, backendInput);
CodegenFactory.Companion.doCheckCancelled(state);
state.getFactory().done();
@@ -67,9 +67,9 @@ class GenerationState private constructor(
private val onIndependentPartCompilationEnd: GenerationStateEventCallback,
wantsDiagnostics: Boolean,
val jvmBackendClassResolver: JvmBackendClassResolver,
val isIrBackend: Boolean
val isIrBackend: Boolean,
val ignoreErrors: Boolean,
) {
class Builder(
private val project: Project,
private val builderFactory: ClassBuilderFactory,
@@ -118,12 +118,16 @@ class GenerationState private constructor(
fun isIrBackend(v: Boolean) =
apply { isIrBackend = v }
var ignoreErrors: Boolean = false
fun ignoreErrors(v: Boolean): Builder =
apply { ignoreErrors = v }
fun build() =
GenerationState(
project, builderFactory, module, bindingContext, files, configuration,
generateDeclaredClassFilter, codegenFactory, targetId,
moduleName, outDirectory, onIndependentPartCompilationEnd, wantsDiagnostics,
jvmBackendClassResolver, isIrBackend
jvmBackendClassResolver, isIrBackend, ignoreErrors
)
}