From 108d6b63ddad1e0553d05eee1f4fce70f886576a Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Mon, 25 Oct 2021 13:51:11 +0300 Subject: [PATCH] [TESTS] Add abstraction at IrBackendInput to work with several backends --- .../backend/handlers/IrInlineBodiesHandler.kt | 4 +-- .../handlers/IrInterpreterBackendHandler.kt | 2 +- .../handlers/IrPrettyKotlinDumpHandler.kt | 2 +- .../backend/handlers/IrTextDumpHandler.kt | 4 +-- .../backend/handlers/IrTreeVerifierHandler.kt | 2 +- .../kotlin/test/backend/ir/IrBackendInput.kt | 29 ++++++++++++++++--- .../test/backend/ir/JvmIrBackendFacade.kt | 3 ++ .../classic/ClassicFrontend2IrConverter.kt | 5 +++- .../frontend/fir/Fir2IrResultsConverter.kt | 2 +- 9 files changed, 40 insertions(+), 13 deletions(-) diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrInlineBodiesHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrInlineBodiesHandler.kt index d8ad471108f..fdccd135afc 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrInlineBodiesHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrInlineBodiesHandler.kt @@ -21,10 +21,10 @@ class IrInlineBodiesHandler(testServices: TestServices) : AbstractIrHandler(test val declaredInlineFunctionSignatures = mutableSetOf() override fun processModule(module: TestModule, info: IrBackendInput) { - val irModule = info.backendInput.irModuleFragment + val irModule = info.irModuleFragment irModule.acceptChildrenVoid(InlineFunctionsCollector()) irModule.acceptChildrenVoid(InlineCallBodiesCheck()) - assertions.assertTrue(info.backendInput.symbolTable.allUnbound.isEmpty()) + assertions.assertTrue((info as IrBackendInput.JvmIrBackendInput).backendInput.symbolTable.allUnbound.isEmpty()) } override fun processAfterAllModules(someAssertionWasFailed: Boolean) { diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrInterpreterBackendHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrInterpreterBackendHandler.kt index 86e09641b11..94e9ab8bf67 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrInterpreterBackendHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrInterpreterBackendHandler.kt @@ -27,7 +27,7 @@ open class IrInterpreterBackendHandler(testServices: TestServices) : AbstractIrH override fun processAfterAllModules(someAssertionWasFailed: Boolean) {} override fun processModule(module: TestModule, info: IrBackendInput) { - val moduleFragment = info.backendInput.irModuleFragment + val moduleFragment = info.irModuleFragment val evaluator = Evaluator(IrInterpreter(moduleFragment.irBuiltins), globalMetadataInfoHandler) for ((irFile, testFile) in moduleFragment.files.zip(module.files)) { if (testFile.isAdditional) continue diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrPrettyKotlinDumpHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrPrettyKotlinDumpHandler.kt index 408238f12a0..8a324bf27d7 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrPrettyKotlinDumpHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrPrettyKotlinDumpHandler.kt @@ -36,7 +36,7 @@ class IrPrettyKotlinDumpHandler(testServices: TestServices) : AbstractIrHandler( override fun processModule(module: TestModule, info: IrBackendInput) { if (DUMP_KT_IR !in module.directives || SKIP_KT_DUMP in module.directives) return - val irFiles = info.backendInput.irModuleFragment.files + val irFiles = info.irModuleFragment.files val builder = dumper.builderForModule(module) val filteredIrFiles = irFiles.groupWithTestFiles(module).filterNot { it.first?.directives?.contains(EXTERNAL_FILE) == true diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrTextDumpHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrTextDumpHandler.kt index 6ceb4527542..c7e6cf91725 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrTextDumpHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrTextDumpHandler.kt @@ -63,7 +63,7 @@ class IrTextDumpHandler(testServices: TestServices) : AbstractIrHandler(testServ @OptIn(ExperimentalStdlibApi::class) override fun processModule(module: TestModule, info: IrBackendInput) { if (DUMP_IR !in module.directives) return - val irFiles = info.backendInput.irModuleFragment.files + val irFiles = info.irModuleFragment.files val testFileToIrFile = irFiles.groupWithTestFiles(module) val builder = baseDumper.builderForModule(module) for ((testFile, irFile) in testFileToIrFile) { @@ -87,7 +87,7 @@ class IrTextDumpHandler(testServices: TestServices) : AbstractIrHandler(testServ // TODO: why JS one is used here in original AbstractIrTextTestCase? val mangler = JsManglerDesc val signaturer = IdSignatureDescriptor(mangler) - val irModule = info.backendInput.irModuleFragment + val irModule = info.irModuleFragment val stubGenerator = DeclarationStubGeneratorImpl( irModule.descriptor, SymbolTable(signaturer, IrFactoryImpl), // TODO diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrTreeVerifierHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrTreeVerifierHandler.kt index acff06595ce..847efd67203 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrTreeVerifierHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrTreeVerifierHandler.kt @@ -22,7 +22,7 @@ class IrTreeVerifierHandler(testServices: TestServices) : AbstractIrHandler(test override fun processModule(module: TestModule, info: IrBackendInput) { if (CodegenTestDirectives.DUMP_IR !in module.directives) return - val irFiles = info.backendInput.irModuleFragment.files + val irFiles = info.irModuleFragment.files val testFileToIrFile = irFiles.groupWithTestFiles(module) for ((testFile, irFile) in testFileToIrFile) { if (testFile?.directives?.contains(EXTERNAL_FILE) == true) continue diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/ir/IrBackendInput.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/ir/IrBackendInput.kt index ca92efdbb4b..91aa4fce85d 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/ir/IrBackendInput.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/ir/IrBackendInput.kt @@ -7,14 +7,35 @@ package org.jetbrains.kotlin.test.backend.ir import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory import org.jetbrains.kotlin.codegen.state.GenerationState +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsIrLinker +import org.jetbrains.kotlin.ir.declarations.IrModuleFragment +import org.jetbrains.kotlin.ir.symbols.IrSymbol +import org.jetbrains.kotlin.ir.util.SymbolTable +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.test.model.BackendKinds import org.jetbrains.kotlin.test.model.ResultingArtifact // IR backend (JVM, JS, Native) -data class IrBackendInput( - val state: GenerationState, - val backendInput: JvmIrCodegenFactory.JvmIrBackendInput -) : ResultingArtifact.BackendInput() { +sealed class IrBackendInput : ResultingArtifact.BackendInput() { override val kind: BackendKinds.IrBackend get() = BackendKinds.IrBackend + + abstract val irModuleFragment: IrModuleFragment + + data class JsIrBackendInput( + override val irModuleFragment: IrModuleFragment, + val sourceFiles: List, + val bindingContext: BindingContext, + val expectDescriptorToSymbol: MutableMap, + ) : IrBackendInput() + + data class JvmIrBackendInput( + val state: GenerationState, + val backendInput: JvmIrCodegenFactory.JvmIrBackendInput + ) : IrBackendInput() { + override val irModuleFragment: IrModuleFragment + get() = backendInput.irModuleFragment + } } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/ir/JvmIrBackendFacade.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/ir/JvmIrBackendFacade.kt index 4f01fa3a4ba..f2f15db55f0 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/ir/JvmIrBackendFacade.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/ir/JvmIrBackendFacade.kt @@ -24,6 +24,9 @@ class JvmIrBackendFacade( module: TestModule, inputArtifact: IrBackendInput ): BinaryArtifacts.Jvm? { + if (inputArtifact !is IrBackendInput.JvmIrBackendInput) { + error("JvmIrBackendFacade expects IrBackendInput.JvmIrBackendInput as input") + } val state = inputArtifact.state val codegenFactory = state.codegenFactory as JvmIrCodegenFactory try { diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt index 6d3aa56fe01..1bb33bf33bc 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt @@ -47,6 +47,9 @@ class ClassicFrontend2IrConverter( .diagnosticReporter(DiagnosticReporterFactory.createReporter()) .build() - return IrBackendInput(state, codegenFactory.convertToIr(CodegenFactory.IrConversionInput.fromGenerationState(state))) + return IrBackendInput.JvmIrBackendInput( + state, + codegenFactory.convertToIr(CodegenFactory.IrConversionInput.fromGenerationState(state)) + ) } } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/Fir2IrResultsConverter.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/Fir2IrResultsConverter.kt index 4a5c50dc91d..54b61300910 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/Fir2IrResultsConverter.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/Fir2IrResultsConverter.kt @@ -74,7 +74,7 @@ class Fir2IrResultsConverter( val irProviders = codegenFactory.configureBuiltInsAndGenerateIrProvidersInFrontendIRMode(irModuleFragment, symbolTable, extensions) - return IrBackendInput( + return IrBackendInput.JvmIrBackendInput( generationState, JvmIrCodegenFactory.JvmIrBackendInput( irModuleFragment,