From e75ca75e3ed13926a1d3059a2c4ef675f184c5f9 Mon Sep 17 00:00:00 2001 From: Ting-Yuan Huang Date: Tue, 23 Mar 2021 22:15:40 -0700 Subject: [PATCH] K2JsIrCompiler: hoist common front-end preparation logic Instead of creating ModuleStructure and run analysis in each backend, the common preparation logic is moved into K2JsIrCompiler.doExecute(). --- .../jetbrains/kotlin/cli/js/K2JsIrCompiler.kt | 56 ++++++----- .../kotlin/ir/backend/js/compiler.kt | 22 +---- .../kotlin/ir/backend/js/ic/fileUtil.kt | 3 +- .../jetbrains/kotlin/ir/backend/js/ic/ic.kt | 60 +++--------- .../jetbrains/kotlin/backend/wasm/compiler.kt | 16 +-- .../jetbrains/kotlin/ir/backend/js/klib.kt | 98 ++++++++++++------- .../org/jetbrains/kotlin/js/test/ApiTest.kt | 11 ++- .../kotlin/js/test/BasicIrBoxTest.kt | 80 +++++++++------ .../kotlin/js/test/BasicWasmBoxTest.kt | 20 ++-- 9 files changed, 192 insertions(+), 174 deletions(-) diff --git a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt index 9e9d1df97aa..a04d69b8b60 100644 --- a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt +++ b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt @@ -205,7 +205,6 @@ class K2JsIrCompiler : CLICompiler() { cachePath = outputFilePath, project = projectJs, mainModule = mainModule, - analyzer = AnalyzerWithCompilerReport(config.configuration), configuration = config.configuration, dependencies = libraries, friendDependencies = friendLibraries, @@ -219,18 +218,29 @@ class K2JsIrCompiler : CLICompiler() { return OK } + // Run analysis if main module is sources + lateinit var sourceModule: ModulesStructure + if (arguments.includes == null) { + sourceModule = prepareAnalyzedSourceModule( + projectJs, + environmentForJS.getSourceFiles(), + configurationJs, + libraries, + friendLibraries, + AnalyzerWithCompilerReport(config.configuration), + icUseGlobalSignatures = icCaches.isNotEmpty(), + icUseStdlibCache = icCaches.isNotEmpty(), + icCache = if (icCaches.isNotEmpty()) checkCaches(libraries, icCaches, skipLib = arguments.includes).data else emptyMap() + ) + } + if (arguments.irProduceKlibDir || arguments.irProduceKlibFile) { if (arguments.irProduceKlibFile) { require(outputFile.extension == KLIB_FILE_EXTENSION) { "Please set up .klib file as output" } } generateKLib( - project = config.project, - files = sourcesFiles, - analyzer = AnalyzerWithCompilerReport(config.configuration), - configuration = config.configuration, - dependencies = libraries, - friendDependencies = friendLibraries, + sourceModule, irFactory = PersistentIrFactory(), // TODO IrFactoryImpl? outputKlibPath = outputFile.path, nopack = arguments.irProduceKlibDir, @@ -246,28 +256,33 @@ class K2JsIrCompiler : CLICompiler() { val includes = arguments.includes - val mainModule = if (includes != null) { + val module = if (includes != null) { if (sourcesFiles.isNotEmpty()) { messageCollector.report(ERROR, "Source files are not supported when -Xinclude is present") } val includesPath = File(includes).canonicalPath val mainLibPath = libraries.find { File(it).canonicalPath == includesPath } ?: error("No library with name $includes ($includesPath) found") - MainModule.Klib(mainLibPath) + val kLib = MainModule.Klib(mainLibPath) + ModulesStructure( + projectJs, + kLib, + configuration, + libraries, + friendLibraries, + icUseGlobalSignatures = icCaches.isNotEmpty(), + icUseStdlibCache = icCaches.isNotEmpty(), + icCache = if (icCaches.isNotEmpty()) checkCaches(libraries, icCaches, skipLib = includes).data else emptyMap() + ) } else { - MainModule.SourceFiles(sourcesFiles) + sourceModule } if (arguments.wasm) { val res = compileWasm( - projectJs, - mainModule, - AnalyzerWithCompilerReport(config.configuration), - config.configuration, + module, PhaseConfig(wasmPhases), IrFactoryImpl, - dependencies = libraries, - friendDependencies = friendLibraries, exportedDeclarations = setOf(FqName("main")) ) val outputWasmFile = outputFile.withReplacedExtensionOrNull(outputFile.extension, "wasm")!! @@ -289,14 +304,9 @@ class K2JsIrCompiler : CLICompiler() { val start = System.currentTimeMillis() val compiledModule = compile( - projectJs, - mainModule, - AnalyzerWithCompilerReport(config.configuration), - config.configuration, + module, phaseConfig, if (arguments.irDceDriven) PersistentIrFactory() else IrFactoryImpl, - dependencies = libraries, - friendDependencies = friendLibraries, mainArguments = mainCallArguments, generateFullJs = !arguments.irDce, generateDceJs = arguments.irDce, @@ -316,8 +326,6 @@ class K2JsIrCompiler : CLICompiler() { messageCollector ), lowerPerModule = icCaches.isNotEmpty(), - useStdlibCache = icCaches.isNotEmpty(), - icCache = if (icCaches.isNotEmpty()) checkCaches(libraries, icCaches, skipLib = includes).data else emptyMap(), ) messageCollector.report(INFO, "Executable production duration: ${System.currentTimeMillis() - start}ms") diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt index c935bffaaa7..0c87ac35b23 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt @@ -39,14 +39,9 @@ class CompilationOutputs( ) fun compile( - project: Project, - mainModule: MainModule, - analyzer: AbstractAnalyzerWithCompilerReport, - configuration: CompilerConfiguration, + depsDescriptors: ModulesStructure, phaseConfig: PhaseConfig, irFactory: IrFactory, - dependencies: Collection, - friendDependencies: Collection, mainArguments: List?, exportedDeclarations: Set = emptySet(), generateFullJs: Boolean = true, @@ -63,18 +58,11 @@ fun compile( lowerPerModule: Boolean = false, safeExternalBoolean: Boolean = false, safeExternalBooleanDiagnostic: RuntimeDiagnostic? = null, - useStdlibCache: Boolean = false, - icCache: Map = emptyMap(), ): CompilerResult { if (lowerPerModule) { return icCompile( - project, - mainModule, - analyzer, - configuration, - dependencies, - friendDependencies, + depsDescriptors, mainArguments, exportedDeclarations, generateFullJs, @@ -88,13 +76,13 @@ fun compile( legacyPropertyAccess, safeExternalBoolean, safeExternalBooleanDiagnostic, - useStdlibCache, - icCache, ) } val (moduleFragment: IrModuleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer, moduleToName) = - loadIr(project, mainModule, analyzer, configuration, dependencies, friendDependencies, irFactory, verifySignatures) + loadIr(depsDescriptors, irFactory, verifySignatures) + val mainModule = depsDescriptors.mainModule + val configuration = depsDescriptors.compilerConfiguration val moduleDescriptor = moduleFragment.descriptor diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/fileUtil.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/fileUtil.kt index 7f7edaed81a..ec0220d6b97 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/fileUtil.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/fileUtil.kt @@ -26,7 +26,6 @@ fun buildCache( cachePath: String, project: Project, mainModule: MainModule.Klib, - analyzer: AbstractAnalyzerWithCompilerReport, configuration: CompilerConfiguration, dependencies: Collection, friendDependencies: Collection, @@ -50,7 +49,7 @@ fun buildCache( icDir.deleteRecursively() icDir.mkdirs() - val icData = prepareSingleLibraryIcCache(project, analyzer, configuration, mainModule.libPath, dependencies, friendDependencies, exportedDeclarations, icCache.data) + val icData = prepareSingleLibraryIcCache(project, configuration, mainModule.libPath, dependencies, friendDependencies, exportedDeclarations, icCache.data) icData.writeTo(File(cachePath)) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/ic.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/ic.kt index ae05bf3ac1d..bbe7701b798 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/ic.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/ic.kt @@ -29,7 +29,6 @@ import java.io.PrintWriter fun prepareSingleLibraryIcCache( project: Project, - analyzer: AbstractAnalyzerWithCompilerReport, configuration: CompilerConfiguration, libPath: String, dependencies: Collection, @@ -41,21 +40,23 @@ fun prepareSingleLibraryIcCache( val controller = WholeWorldStageController() irFactory.stageController = controller - val (context, deserializer, allModules) = prepareIr( + val depsDescriptor = ModulesStructure( project, MainModule.Klib(libPath), - analyzer, configuration, dependencies, friendDependencies, + true, + true, + icCache + ) + val (context, deserializer, allModules) = prepareIr( + depsDescriptor, exportedDeclarations, null, false, false, irFactory, - useGlobalSignatures = true, - useStdlibCache = true, - icCache = icCache ) val moduleFragment = allModules.last() @@ -118,12 +119,7 @@ private fun dumpIr(module: IrModuleFragment, fileName: String) { } fun icCompile( - project: Project, - mainModule: MainModule, - analyzer: AbstractAnalyzerWithCompilerReport, - configuration: CompilerConfiguration, - dependencies: Collection, - friendDependencies: Collection, + depsDescriptor: ModulesStructure, mainArguments: List?, exportedDeclarations: Set = emptySet(), generateFullJs: Boolean = true, @@ -137,8 +133,6 @@ fun icCompile( baseClassIntoMetadata: Boolean = false, safeExternalBoolean: Boolean = false, safeExternalBooleanDiagnostic: RuntimeDiagnostic? = null, - useStdlibCache: Boolean, - icCache: Map = emptyMap() ): CompilerResult { val irFactory = PersistentIrFactory() @@ -146,12 +140,7 @@ fun icCompile( irFactory.stageController = controller val (context, _, allModules, moduleToName, loweredIrLoaded) = prepareIr( - project, - mainModule, - analyzer, - configuration, - dependencies, - friendDependencies, + depsDescriptor, exportedDeclarations, dceRuntimeDiagnostic, es6mode, @@ -160,10 +149,7 @@ fun icCompile( baseClassIntoMetadata, legacyPropertyAccess, safeExternalBoolean, - safeExternalBooleanDiagnostic, - useStdlibCache, - useStdlibCache, - icCache, + safeExternalBooleanDiagnostic ) val modulesToLower = allModules.filter { it !in loweredIrLoaded } @@ -220,12 +206,7 @@ fun lowerPreservingIcData(module: IrModuleFragment, context: JsIrBackendContext, } private fun prepareIr( - project: Project, - mainModule: MainModule, - analyzer: AbstractAnalyzerWithCompilerReport, - configuration: CompilerConfiguration, - dependencies: Collection, - friendDependencies: Collection, + depsDescriptor: ModulesStructure, exportedDeclarations: Set = emptySet(), dceRuntimeDiagnostic: RuntimeDiagnostic? = null, es6mode: Boolean = false, @@ -235,26 +216,13 @@ private fun prepareIr( baseClassIntoMetadata: Boolean = false, safeExternalBoolean: Boolean = false, safeExternalBooleanDiagnostic: RuntimeDiagnostic? = null, - useGlobalSignatures: Boolean, - useStdlibCache: Boolean, - icCache: Map, ): PreparedIr { - val cacheProvider: LoweringsCacheProvider? = when { - useStdlibCache -> object : LoweringsCacheProvider { - override fun cacheByPath(path: String): SerializedIcData? { - return icCache[path] - } - } - useGlobalSignatures -> EmptyLoweringsCacheProvider - else -> null - } - val (moduleFragment: IrModuleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer, moduleToName, loweredIrLoaded) = - loadIr(project, mainModule, analyzer, configuration, dependencies, friendDependencies, irFactory, false, cacheProvider) + loadIr(depsDescriptor, irFactory, false) val moduleDescriptor = moduleFragment.descriptor - val allModules = when (mainModule) { + val allModules = when (depsDescriptor.mainModule) { is MainModule.SourceFiles -> dependencyModules + listOf(moduleFragment) is MainModule.Klib -> dependencyModules } @@ -265,7 +233,7 @@ private fun prepareIr( symbolTable, allModules.first(), exportedDeclarations, - configuration, + depsDescriptor.compilerConfiguration, es6mode = es6mode, dceRuntimeDiagnostic = dceRuntimeDiagnostic, propertyLazyInitialization = propertyLazyInitialization, diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/compiler.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/compiler.kt index fc287c4e907..262a040d716 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/compiler.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/compiler.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.backend.wasm.ir2wasm.WasmModuleFragmentGenerator import org.jetbrains.kotlin.backend.wasm.ir2wasm.generateStringLiteralsSupport import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.ir.backend.js.MainModule +import org.jetbrains.kotlin.ir.backend.js.ModulesStructure import org.jetbrains.kotlin.ir.backend.js.loadIr import org.jetbrains.kotlin.ir.declarations.IrFactory import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator @@ -28,21 +29,14 @@ import java.io.ByteArrayOutputStream class WasmCompilerResult(val wat: String, val js: String, val wasm: ByteArray) fun compileWasm( - project: Project, - mainModule: MainModule, - analyzer: AbstractAnalyzerWithCompilerReport, - configuration: CompilerConfiguration, + depsDescriptors: ModulesStructure, phaseConfig: PhaseConfig, irFactory: IrFactory, - dependencies: Collection, - friendDependencies: Collection, exportedDeclarations: Set = emptySet() ): WasmCompilerResult { - val (moduleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer) = - loadIr( - project, mainModule, analyzer, configuration, dependencies, friendDependencies, - irFactory, verifySignatures = false - ) + val mainModule = depsDescriptors.mainModule + val configuration = depsDescriptors.compilerConfiguration + val (moduleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer) = loadIr(depsDescriptors, irFactory, verifySignatures = false) val allModules = when (mainModule) { is MainModule.SourceFiles -> dependencyModules + listOf(moduleFragment) diff --git a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt index 8a54fa578c7..e9efb59235a 100644 --- a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt +++ b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt @@ -120,12 +120,7 @@ private fun IrMessageLogger?.toResolverLogger(): Logger { } fun generateKLib( - project: Project, - files: List, - analyzer: AbstractAnalyzerWithCompilerReport, - configuration: CompilerConfiguration, - dependencies: Collection, - friendDependencies: Collection, + depsDescriptors: ModulesStructure, irFactory: IrFactory, outputKlibPath: String, nopack: Boolean, @@ -133,6 +128,10 @@ fun generateKLib( abiVersion: KotlinAbiVersion = KotlinAbiVersion.CURRENT, jsOutputName: String? ) { + val project = depsDescriptors.project + val files = (depsDescriptors.mainModule as MainModule.SourceFiles).files + val configuration = depsDescriptors.compilerConfiguration + val allDependencies = depsDescriptors.allDependencies val incrementalDataProvider = configuration.get(JSConfigurationKeys.INCREMENTAL_DATA_PROVIDER) val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT val messageLogger = configuration.get(IrMessageLogger.IR_MESSAGE_LOGGER) ?: IrMessageLogger.None @@ -167,10 +166,7 @@ fun generateKLib( serializedIrFiles = null } - val depsDescriptors = - ModulesStructure(project, MainModule.SourceFiles(files), analyzer, configuration, dependencies, friendDependencies, EmptyLoweringsCacheProvider) - val allDependencies = depsDescriptors.allDependencies - val (psi2IrContext, hasErrors) = runAnalysisAndPreparePsi2Ir(depsDescriptors, errorPolicy, SymbolTable(IdSignatureDescriptor(JsManglerDesc), irFactory)) + val (psi2IrContext, hasErrors) = preparePsi2Ir(depsDescriptors, errorPolicy, SymbolTable(IdSignatureDescriptor(JsManglerDesc), irFactory)) val irBuiltIns = psi2IrContext.irBuiltIns val expectDescriptorToSymbol = mutableMapOf() @@ -256,27 +252,23 @@ object EmptyLoweringsCacheProvider : LoweringsCacheProvider { @OptIn(ObsoleteDescriptorBasedAPI::class) fun loadIr( - project: Project, - mainModule: MainModule, - analyzer: AbstractAnalyzerWithCompilerReport, - configuration: CompilerConfiguration, - dependencies: Collection, - friendDependencies: Collection, + depsDescriptors: ModulesStructure, irFactory: IrFactory, - verifySignatures: Boolean, - loweringsCacheProvider: LoweringsCacheProvider? = null + verifySignatures: Boolean ): IrModuleInfo { - val depsDescriptors = ModulesStructure(project, mainModule, analyzer, configuration, dependencies, friendDependencies, loweringsCacheProvider ?: EmptyLoweringsCacheProvider) + val project = depsDescriptors.project + val mainModule = depsDescriptors.mainModule + val configuration = depsDescriptors.compilerConfiguration + val allDependencies = depsDescriptors.allDependencies val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT val messageLogger = configuration.get(IrMessageLogger.IR_MESSAGE_LOGGER) ?: IrMessageLogger.None - val allDependencies = depsDescriptors.allDependencies val signaturer = IdSignatureDescriptor(JsManglerDesc) val symbolTable = SymbolTable(signaturer, irFactory) when (mainModule) { is MainModule.SourceFiles -> { - val (psi2IrContext, _) = runAnalysisAndPreparePsi2Ir(depsDescriptors, errorPolicy, symbolTable) + val (psi2IrContext, _) = preparePsi2Ir(depsDescriptors, errorPolicy, symbolTable) val irBuiltIns = psi2IrContext.irBuiltIns val feContext = psi2IrContext.run { JsIrLinker.JsFePluginContext(moduleDescriptor, symbolTable, typeTranslator, irBuiltIns) @@ -335,12 +327,12 @@ fun loadIr( TypeTranslatorImpl(symbolTable, depsDescriptors.compilerConfiguration.languageVersionSettings, moduleDescriptor) val irBuiltIns = IrBuiltInsOverDescriptors(moduleDescriptor.builtIns, typeTranslator, symbolTable) - val loweredIcData = if (loweringsCacheProvider == null) emptyMap() else { + val loweredIcData = if (!depsDescriptors.icUseStdlibCache && !depsDescriptors.icUseStdlibCache) emptyMap() else { val result = mutableMapOf() for (lib in depsDescriptors.moduleDependencies.keys) { val path = lib.libraryFile.absolutePath - val icData = loweringsCacheProvider.cacheByPath(path) + val icData = depsDescriptors.loweringsCacheProvider.cacheByPath(path) if (icData != null) { val desc = depsDescriptors.getModuleDescriptor(lib) result[desc] = icData @@ -393,12 +385,31 @@ fun loadIr( } } -private fun runAnalysisAndPreparePsi2Ir( +fun prepareAnalyzedSourceModule( + project: Project, + files: List, + configuration: CompilerConfiguration, + dependencies: List, + friendDependencies: List, + analyzer: AbstractAnalyzerWithCompilerReport, + icUseGlobalSignatures: Boolean = false, + icUseStdlibCache: Boolean = false, + icCache: Map = emptyMap(), + errorPolicy: ErrorTolerancePolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT, +): ModulesStructure { + val mainModule = MainModule.SourceFiles(files) + val sourceModule = ModulesStructure(project, mainModule, configuration, dependencies, friendDependencies, icUseGlobalSignatures, icUseStdlibCache, icCache) + return sourceModule.apply { + runAnalysis(errorPolicy, analyzer) + } +} + +private fun preparePsi2Ir( depsDescriptors: ModulesStructure, errorIgnorancePolicy: ErrorTolerancePolicy, symbolTable: SymbolTable, ): Pair { - val analysisResult = depsDescriptors.runAnalysis(errorIgnorancePolicy) + val analysisResult = depsDescriptors.jsFrontEndResult val psi2Ir = Psi2IrTranslator( depsDescriptors.compilerConfiguration.languageVersionSettings, Psi2IrConfiguration(errorIgnorancePolicy.allowErrors) @@ -469,15 +480,26 @@ sealed class MainModule { class Klib(val libPath: String) : MainModule() } -private class ModulesStructure( - private val project: Project, - private val mainModule: MainModule, - private val analyzer: AbstractAnalyzerWithCompilerReport, +class ModulesStructure( + val project: Project, + val mainModule: MainModule, val compilerConfiguration: CompilerConfiguration, - dependencies: Collection, + val dependencies: Collection, friendDependenciesPaths: Collection, - private val loweringsCacheProvider: LoweringsCacheProvider + val icUseGlobalSignatures: Boolean, + val icUseStdlibCache: Boolean, + val icCache: Map, ) { + val loweringsCacheProvider: LoweringsCacheProvider = when { + icUseStdlibCache -> object : LoweringsCacheProvider { + override fun cacheByPath(path: String): SerializedIcData? { + return icCache[path] + } + } + icUseGlobalSignatures -> EmptyLoweringsCacheProvider + else -> EmptyLoweringsCacheProvider + } + val allResolvedDependencies = jsResolveLibraries( dependencies, compilerConfiguration[JSConfigurationKeys.REPOSITORIES] ?: emptyList(), @@ -502,9 +524,17 @@ private class ModulesStructure( val builtInsDep = allDependencies.find { it.library.isBuiltIns } - class JsFrontEndResult(val moduleDescriptor: ModuleDescriptor, val bindingContext: BindingContext, val hasErrors: Boolean) + class JsFrontEndResult(val jsAnalysisResult: AnalysisResult, val hasErrors: Boolean) { + val moduleDescriptor: ModuleDescriptor + get() = jsAnalysisResult.moduleDescriptor - fun runAnalysis(errorPolicy: ErrorTolerancePolicy): JsFrontEndResult { + val bindingContext: BindingContext + get() = jsAnalysisResult.bindingContext + } + + lateinit var jsFrontEndResult: JsFrontEndResult + + fun runAnalysis(errorPolicy: ErrorTolerancePolicy, analyzer: AbstractAnalyzerWithCompilerReport) { require(mainModule is MainModule.SourceFiles) val files = mainModule.files @@ -538,7 +568,7 @@ private class ModulesStructure( hasErrors = TopDownAnalyzerFacadeForJSIR.checkForErrors(files, analysisResult.bindingContext, errorPolicy) || hasErrors - return JsFrontEndResult(analysisResult.moduleDescriptor, analysisResult.bindingContext, hasErrors) + jsFrontEndResult = JsFrontEndResult(analysisResult, hasErrors) } private val languageVersionSettings: LanguageVersionSettings = compilerConfiguration.languageVersionSettings diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ApiTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/ApiTest.kt index 2f2e58658d4..d03c74cf736 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ApiTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ApiTest.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.impl.EnumEntrySyntheticClassDescriptor import org.jetbrains.kotlin.ir.backend.js.MainModule +import org.jetbrains.kotlin.ir.backend.js.ModulesStructure import org.jetbrains.kotlin.ir.backend.js.loadIr import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl import org.jetbrains.kotlin.js.config.JSConfigurationKeys @@ -72,13 +73,19 @@ class ApiTest : KotlinTestWithEnvironment() { val project = environment.project val configuration = environment.configuration - return loadIr( + val klibModule = ModulesStructure( project, MainModule.Klib(File(fullRuntimeKlib).canonicalPath), - AnalyzerWithCompilerReport(configuration), configuration, listOf(fullRuntimeKlib), emptyList(), + false, + false, + emptyMap() + ) + + return loadIr( + klibModule, IrFactoryImpl, verifySignatures = true ).module.descriptor.packagesSerialized() diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt index 15fa6c1d62a..3ea44734eaa 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt @@ -128,13 +128,16 @@ abstract class BasicIrBoxTest( prepareRuntimePirCaches(config, icCache) if (isMainModule && klibMainModule) { + val module = prepareAnalyzedSourceModule( + config.project, + filesToCompile, + config.configuration, + allKlibPaths, + emptyList(), + AnalyzerWithCompilerReport(config.configuration), + ) generateKLib( - project = config.project, - files = filesToCompile, - analyzer = AnalyzerWithCompilerReport(config.configuration), - configuration = config.configuration, - dependencies = allKlibPaths, - friendDependencies = emptyList(), + module, irFactory = IrFactoryImpl, outputKlibPath = klibPath, nopack = true, @@ -168,23 +171,42 @@ abstract class BasicIrBoxTest( PhaseConfig(jsPhases) } - val mainModule = if (!klibMainModule) { - MainModule.SourceFiles(filesToCompile) - } else { - MainModule.Klib(klibPath) + fun prepareModule(allowIc: Boolean): ModulesStructure { + val useIc = runIcMode && allowIc + val icCache = if (useIc) icCache else emptyMap() + return if (!klibMainModule) { + prepareAnalyzedSourceModule( + config.project, + filesToCompile, + config.configuration, + allKlibPaths, + emptyList(), + AnalyzerWithCompilerReport(config.configuration), + icUseGlobalSignatures = useIc, + icUseStdlibCache = useIc, + icCache = icCache + ) + } else { + ModulesStructure( + config.project, + MainModule.Klib(klibPath), + config.configuration, + allKlibPaths, + emptyList(), + icUseGlobalSignatures = useIc, + icUseStdlibCache = useIc, + icCache = icCache + ) + } } if (!skipRegularMode) { + val module = prepareModule(true) val irFactory = if (lowerPerModule) PersistentIrFactory() else IrFactoryImpl val compiledModule = compile( - project = config.project, - mainModule = mainModule, - analyzer = AnalyzerWithCompilerReport(config.configuration), - configuration = config.configuration, + module, phaseConfig = phaseConfig, irFactory = irFactory, - dependencies = allKlibPaths, - friendDependencies = emptyList(), mainArguments = mainCallParameters.run { if (shouldBeGenerated()) arguments() else null }, exportedDeclarations = setOf(FqName.fromSegments(listOfNotNull(testPackage, testFunction))), generateFullJs = true, @@ -196,8 +218,6 @@ abstract class BasicIrBoxTest( safeExternalBoolean = safeExternalBoolean, safeExternalBooleanDiagnostic = safeExternalBooleanDiagnostic, verifySignatures = !skipMangleVerification, - useStdlibCache = runIcMode, - icCache = icCache ) compiledModule.outputs!!.writeTo(outputFile, config) @@ -212,15 +232,11 @@ abstract class BasicIrBoxTest( } if (runIrPir && !skipDceDriven) { + val module = prepareModule(false) compile( - project = config.project, - mainModule = mainModule, - analyzer = AnalyzerWithCompilerReport(config.configuration), - configuration = config.configuration, + module, phaseConfig = phaseConfig, irFactory = PersistentIrFactory(), - dependencies = allKlibPaths, - friendDependencies = emptyList(), mainArguments = mainCallParameters.run { if (shouldBeGenerated()) arguments() else null }, exportedDeclarations = setOf(FqName.fromSegments(listOfNotNull(testPackage, testFunction))), dceDriven = true, @@ -233,13 +249,16 @@ abstract class BasicIrBoxTest( ).outputs!!.writeTo(pirOutputFile, config) } } else { + val module = prepareAnalyzedSourceModule( + config.project, + filesToCompile, + config.configuration, + allKlibPaths, + emptyList(), + AnalyzerWithCompilerReport(config.configuration) + ) generateKLib( - project = config.project, - files = filesToCompile, - analyzer = AnalyzerWithCompilerReport(config.configuration), - configuration = config.configuration, - dependencies = allKlibPaths, - friendDependencies = emptyList(), + module, irFactory = IrFactoryImpl, outputKlibPath = actualOutputFile, nopack = true, @@ -262,7 +281,6 @@ abstract class BasicIrBoxTest( private fun createPirCache(path: String, allKlibPaths: Collection, config: JsConfig, icCache: Map): SerializedIcData { val icData = predefinedKlibHasIcCache[path] ?: prepareSingleLibraryIcCache( project = project, - analyzer = AnalyzerWithCompilerReport(config.configuration), configuration = config.configuration, libPath = path, dependencies = allKlibPaths, diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicWasmBoxTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicWasmBoxTest.kt index cbe0e8eaebe..e28ef6ff214 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicWasmBoxTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicWasmBoxTest.kt @@ -16,7 +16,9 @@ import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.config.* import org.jetbrains.kotlin.ir.backend.js.MainModule +import org.jetbrains.kotlin.ir.backend.js.ModulesStructure import org.jetbrains.kotlin.ir.backend.js.loadKlib +import org.jetbrains.kotlin.ir.backend.js.prepareAnalyzedSourceModule import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl import org.jetbrains.kotlin.js.config.JsConfig import org.jetbrains.kotlin.js.facade.TranslationUnit @@ -135,16 +137,20 @@ abstract class BasicWasmBoxTest( PhaseConfig(wasmPhases) } + val sourceModule = prepareAnalyzedSourceModule( + config.project, + filesToCompile, + config.configuration, + // TODO: Bypass the resolver fow wasm. + listOf(System.getProperty("kotlin.wasm.stdlib.path")!!), + emptyList(), + AnalyzerWithCompilerReport(config.configuration) + ) + val compilerResult = compileWasm( - project = config.project, - mainModule = MainModule.SourceFiles(filesToCompile), - analyzer = AnalyzerWithCompilerReport(config.configuration), - configuration = config.configuration, + sourceModule, phaseConfig = phaseConfig, irFactory = IrFactoryImpl, - // TODO: Bypass the resolver fow wasm. - dependencies = listOf(System.getProperty("kotlin.wasm.stdlib.path")!!), - friendDependencies = emptyList(), exportedDeclarations = setOf(FqName.fromSegments(listOfNotNull(testPackage, testFunction))) )