[JS IR] DTS generation refactoring

Keep a DTS fragment in JsIrProgramFragment
 for each file and build the module DTS from them.
This commit is contained in:
Alexander Korepanov
2022-12-01 22:12:09 +01:00
committed by Space Team
parent 03a43e0dc0
commit 0a35d84193
10 changed files with 110 additions and 95 deletions
@@ -11,6 +11,8 @@ import org.jetbrains.kotlin.backend.common.serialization.linkerissues.checkNoUnb
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.backend.js.codegen.JsGenerationGranularity
import org.jetbrains.kotlin.ir.backend.js.export.TypeScriptFragment
import org.jetbrains.kotlin.ir.backend.js.export.toTypeScript
import org.jetbrains.kotlin.ir.backend.js.lower.collectNativeImplementations
import org.jetbrains.kotlin.ir.backend.js.lower.generateJsTests
import org.jetbrains.kotlin.ir.backend.js.lower.moveBodilessDeclarationsToSeparatePlace
@@ -24,19 +26,30 @@ import org.jetbrains.kotlin.js.backend.ast.JsProgram
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.config.RuntimeDiagnostic
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.serialization.js.ModuleKind
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import java.io.File
class CompilerResult(
val outputs: Map<TranslationMode, CompilationOutputs>,
val tsDefinitions: String? = null
)
class CompilationOutputs(
val jsCode: String,
val tsDefinitions: TypeScriptFragment? = null,
val jsProgram: JsProgram? = null,
val sourceMap: String? = null,
val dependencies: Iterable<Pair<String, CompilationOutputs>> = emptyList()
)
) {
fun addDependencies(depends: Iterable<Pair<String, CompilationOutputs>>): CompilationOutputs {
return CompilationOutputs(jsCode, tsDefinitions, jsProgram, sourceMap, depends)
}
fun getFullTsDefinition(name: String, moduleKind: ModuleKind): String {
val allTsDefinitions = dependencies.mapNotNull { it.second.tsDefinitions } + listOfNotNull(tsDefinitions)
return allTsDefinitions.toTypeScript(name, moduleKind)
}
}
class LoweredIr(
val context: JsIrBackendContext,
@@ -25,12 +25,19 @@ private const val declareExorted = "export $declare"
private const val NonExistent = "__NonExistent"
private const val syntheticObjectNameSeparator = '$'
fun ExportedModule.toTypeScript(): String {
return ExportModelToTsDeclarations().generateTypeScript(name, this)
@JvmInline
value class TypeScriptFragment(val raw: String)
fun List<ExportedDeclaration>.toTypeScriptFragment(moduleKind: ModuleKind): TypeScriptFragment {
return ExportModelToTsDeclarations().generateTypeScriptFragment(moduleKind, this)
}
fun List<ExportedDeclaration>.toTypeScript(moduleKind: ModuleKind): String {
return ExportModelToTsDeclarations().generateTypeScript(moduleKind, this)
fun List<TypeScriptFragment>.joinTypeScriptFragments(): TypeScriptFragment {
return TypeScriptFragment(joinToString("\n") { it.raw })
}
fun List<TypeScriptFragment>.toTypeScript(name: String, moduleKind: ModuleKind): String {
return ExportModelToTsDeclarations().generateTypeScript(name, moduleKind, this)
}
// TODO: Support module kinds other than plain
@@ -40,24 +47,24 @@ class ExportModelToTsDeclarations {
private val ModuleKind.indent: String
get() = if (this == ModuleKind.PLAIN) " " else ""
fun generateTypeScript(name: String, module: ExportedModule): String {
fun generateTypeScript(name: String, moduleKind: ModuleKind, declarations: List<TypeScriptFragment>): String {
val types = """
type $Nullable<T> = T | null | undefined
""".trimIndent().prependIndent(module.moduleKind.indent) + "\n"
""".trimIndent().prependIndent(moduleKind.indent) + "\n"
val declarationsDts = types + module.declarations.toTypeScript(module.moduleKind)
val declarationsDts = types + declarations.joinTypeScriptFragments().raw
val namespaceName = sanitizeName(name, withHash = false)
return when (module.moduleKind) {
return when (moduleKind) {
ModuleKind.PLAIN -> "declare namespace $namespaceName {\n$declarationsDts\n}\n"
ModuleKind.AMD, ModuleKind.COMMON_JS, ModuleKind.ES -> declarationsDts
ModuleKind.UMD -> "$declarationsDts\nexport as namespace $namespaceName;"
}
}
fun generateTypeScript(moduleKind: ModuleKind, declarations: List<ExportedDeclaration>): String {
return declarations.toTypeScript(moduleKind)
fun generateTypeScriptFragment(moduleKind: ModuleKind, declarations: List<ExportedDeclaration>): TypeScriptFragment {
return TypeScriptFragment(declarations.toTypeScript(moduleKind))
}
private fun List<ExportedDeclaration>.toTypeScript(moduleKind: ModuleKind): String {
@@ -100,7 +100,7 @@ class JsExecutableProducer(
it.jsIrHeader.externalModuleName to it.compileModule(it.jsIrHeader.externalModuleName, false)
}
stopwatch.stop()
val compilationOut = CompilationOutputs(mainModule.jsCode, mainModule.jsProgram, mainModule.sourceMap, dependencies)
val compilationOut = mainModule.addDependencies(dependencies)
return BuildResult(compilationOut, rebuildModules)
}
}
@@ -74,7 +74,7 @@ class JsMultiModuleCache(private val moduleArtifacts: List<ModuleArtifact>) {
fun fetchCompiledJsCode(artifact: ModuleArtifact) = artifact.artifactsDir?.let { cacheDir ->
val jsCode = File(cacheDir, CACHED_MODULE_JS).ifExists { readText() }
val sourceMap = File(cacheDir, CACHED_MODULE_JS_MAP).ifExists { readText() }
jsCode?.let { CompilationOutputs(it, null, sourceMap) }
jsCode?.let { CompilationOutputs(it, null, null, sourceMap) }
}
fun commitCompiledJsCode(artifact: ModuleArtifact, compilationOutputs: CompilationOutputs) = artifact.artifactsDir?.let { cacheDir ->
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.js.sourceMap.SourceMapBuilderConsumer
import org.jetbrains.kotlin.js.util.TextOutputImpl
import org.jetbrains.kotlin.serialization.js.ModuleKind
import org.jetbrains.kotlin.utils.addToStdlib.runIf
import java.io.ByteArrayOutputStream
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import java.io.File
import java.util.*
@@ -107,19 +107,15 @@ class IrModuleToJsTransformer(
private val isEsModules = moduleKind == ModuleKind.ES
private val sourceMapInfo = SourceMapsInfo.from(backendContext.configuration)
private class IrAndExportedDeclarations(val fragment: IrModuleFragment, val files: List<Pair<IrFile, List<ExportedDeclaration>>>)
private class IrFileExports(val file: IrFile, val exports: List<ExportedDeclaration>, val tsDeclarations: TypeScriptFragment?)
private fun List<IrAndExportedDeclarations>.flatExportedDeclarations(): List<ExportedDeclaration> {
return this.flatMap { data -> data.files.flatMap { it.second } }
}
private class IrAndExportedDeclarations(val fragment: IrModuleFragment, val files: List<IrFileExports>)
private fun associateIrAndExport(modules: Iterable<IrModuleFragment>): List<IrAndExportedDeclarations> {
val exportModelGenerator = ExportModelGenerator(backendContext, generateNamespacesForPackages = !isEsModules)
return modules.map { module ->
val files = module.files.map { file ->
file to exportModelGenerator.generateExportWithExternals(file)
}
val files = exportModelGenerator.generateExportWithExternals(module.files)
IrAndExportedDeclarations(module, files)
}
}
@@ -144,9 +140,6 @@ class IrModuleToJsTransformer(
fun generateModule(modules: Iterable<IrModuleFragment>, modes: Set<TranslationMode>, relativeRequirePath: Boolean): CompilerResult {
val exportData = associateIrAndExport(modules)
val dts = runIf(shouldGenerateTypeScriptDefinitions) {
ExportedModule(mainModuleName, moduleKind, exportData.flatExportedDeclarations()).toTypeScript()
}
doStaticMembersLowering(modules)
val result = EnumMap<TranslationMode, CompilationOutputs>(TranslationMode::class.java)
@@ -163,39 +156,41 @@ class IrModuleToJsTransformer(
result[it] = makeJsCodeGeneratorFromIr(exportData, it).generateJsCode(relativeRequirePath, true)
}
return CompilerResult(result, dts)
return CompilerResult(result)
}
fun makeJsCodeGeneratorAndDts(modules: Iterable<IrModuleFragment>, mode: TranslationMode): Pair<JsCodeGenerator, String?> {
fun makeJsCodeGenerator(modules: Iterable<IrModuleFragment>, mode: TranslationMode): JsCodeGenerator {
val exportData = associateIrAndExport(modules)
val dts = runIf(shouldGenerateTypeScriptDefinitions) {
ExportedModule(mainModuleName, moduleKind, exportData.flatExportedDeclarations()).toTypeScript()
}
doStaticMembersLowering(modules)
if (mode.dce) {
eliminateDeadDeclarations(modules, backendContext, removeUnusedAssociatedObjects)
}
return makeJsCodeGeneratorFromIr(exportData, mode) to dts
return makeJsCodeGeneratorFromIr(exportData, mode)
}
fun makeIrFragmentsGenerators(files: Collection<IrFile>, allModules: Collection<IrModuleFragment>): List<() -> JsIrProgramFragment> {
val exportModelGenerator = ExportModelGenerator(backendContext, generateNamespacesForPackages = !isEsModules)
val exportData = files.map { it to exportModelGenerator.generateExportWithExternals(it) }
val exportData = exportModelGenerator.generateExportWithExternals(files)
doStaticMembersLowering(allModules)
return exportData.map { (file, exports) ->
{ generateProgramFragment(file, exports, minimizedMemberNames = false) }
return exportData.map {
{ generateProgramFragment(it, minimizedMemberNames = false) }
}
}
private fun ExportModelGenerator.generateExportWithExternals(irFile: IrFile): List<ExportedDeclaration> {
val exports = generateExport(irFile)
val additionalExports = backendContext.externalPackageFragment[irFile.symbol]?.let { generateExport(it) } ?: emptyList()
return additionalExports + exports
private fun ExportModelGenerator.generateExportWithExternals(irFiles: Collection<IrFile>): List<IrFileExports> {
return irFiles.map { irFile ->
val exports = generateExport(irFile)
val additionalExports = backendContext.externalPackageFragment[irFile.symbol]?.let { generateExport(it) } ?: emptyList()
val allExports = additionalExports + exports
val tsDeclarations = runIf(shouldGenerateTypeScriptDefinitions) {
allExports.ifNotEmpty { toTypeScriptFragment(moduleKind) }
}
IrFileExports(irFile, allExports, tsDeclarations)
}
}
private fun IrModuleFragment.externalModuleName(): String {
@@ -213,8 +208,8 @@ class IrModuleToJsTransformer(
JsIrModule(
data.fragment.safeName,
data.fragment.externalModuleName(),
data.files.map { (file, exports) ->
generateProgramFragment(file, exports, mode.minimizedMemberNames)
data.files.map {
generateProgramFragment(it, mode.minimizedMemberNames)
}
)
}
@@ -226,11 +221,7 @@ class IrModuleToJsTransformer(
private val generateFilePaths = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_COMMENTS_WITH_FILE_PATH)
private val pathPrefixMap = backendContext.configuration.getMap(JSConfigurationKeys.FILE_PATHS_PREFIX_MAP)
private fun generateProgramFragment(
file: IrFile,
exports: List<ExportedDeclaration>,
minimizedMemberNames: Boolean
): JsIrProgramFragment {
private fun generateProgramFragment(fileExports: IrFileExports, minimizedMemberNames: Boolean): JsIrProgramFragment {
val nameGenerator = JsNameLinkingNamer(backendContext, minimizedMemberNames)
val globalNameScope = NameTable<IrDeclaration>()
@@ -241,9 +232,9 @@ class IrModuleToJsTransformer(
globalNameScope = globalNameScope
)
val result = JsIrProgramFragment(file.fqName.asString()).apply {
val result = JsIrProgramFragment(fileExports.file.fqName.asString()).apply {
if (shouldGeneratePolyfills) {
polyfills.statements += backendContext.polyfills.getAllPolyfillsFor(file)
polyfills.statements += backendContext.polyfills.getAllPolyfillsFor(fileExports.file)
}
}
@@ -251,20 +242,17 @@ class IrModuleToJsTransformer(
val globalNames = NameTable<String>(globalNameScope)
val exportStatements =
ExportModelToJsStatements(staticContext, { globalNames.declareFreshName(it, it) }).generateModuleExport(
ExportedModule(mainModuleName, moduleKind, exports),
ExportedModule(mainModuleName, moduleKind, fileExports.exports),
internalModuleName,
isEsModules
)
result.exports.statements += exportStatements
if (shouldGenerateTypeScriptDefinitions && exports.isNotEmpty()) {
result.dts = exports.toTypeScript(moduleKind)
}
result.dts = fileExports.tsDeclarations
val statements = result.declarations.statements
val fileStatements = file.accept(IrFileToJsTransformer(useBareParameterNames = true), staticContext).statements
val fileStatements = fileExports.file.accept(IrFileToJsTransformer(useBareParameterNames = true), staticContext).statements
if (fileStatements.isNotEmpty()) {
var startComment = ""
@@ -273,7 +261,7 @@ class IrModuleToJsTransformer(
}
if (generateRegionComments || generateFilePaths) {
val originalPath = file.path
val originalPath = fileExports.file.path
val path = pathPrefixMap.entries
.find { (k, _) -> originalPath.startsWith(k) }
?.let { (k, v) -> v + originalPath.substring(k.length) }
@@ -303,7 +291,7 @@ class IrModuleToJsTransformer(
result.initializers.statements += staticContext.initializerBlock.statements
if (mainArguments != null) {
JsMainFunctionDetector(backendContext).getMainFunctionOrNull(file)?.let {
JsMainFunctionDetector(backendContext).getMainFunctionOrNull(fileExports.file)?.let {
val jsName = staticContext.getNameForStaticFunction(it)
val generateArgv = it.valueParameters.firstOrNull()?.isStringArrayParameter() ?: false
val generateContinuation = it.isLoweredSuspendFunction(backendContext)
@@ -311,14 +299,14 @@ class IrModuleToJsTransformer(
}
}
backendContext.testFunsPerFile[file]?.let {
backendContext.testFunsPerFile[fileExports.file]?.let {
result.testFunInvocation = JsInvocation(staticContext.getNameForStaticFunction(it).makeRef()).makeStmt()
result.suiteFn = staticContext.getNameForStaticFunction(backendContext.suiteFun!!.owner)
}
result.importedModules += nameGenerator.importedModules
val definitionSet = file.declarations.toSet()
val definitionSet = fileExports.file.declarations.toSet()
fun computeTag(declaration: IrDeclaration): String? {
val tag = (backendContext.irFactory as IdSignatureRetriever).declarationSignature(declaration)?.toString()
@@ -341,7 +329,7 @@ class IrModuleToJsTransformer(
result.imports[tag] = importExpression
}
file.declarations.forEach {
fileExports.file.declarations.forEach {
computeTag(it)?.let { tag ->
result.definitions += tag
}
@@ -422,7 +410,7 @@ private fun generateWrappedModuleBody(
}
}
return CompilationOutputs(mainModule.jsCode, mainModule.jsProgram, mainModule.sourceMap, dependencies)
return mainModule.addDependencies(dependencies)
} else {
return generateSingleWrappedModuleBody(
mainModuleName,
@@ -484,6 +472,7 @@ fun generateSingleWrappedModuleBody(
return CompilationOutputs(
jsCode.toString(),
fragments.mapNotNull { it.dts }.ifNotEmpty { joinTypeScriptFragments() },
program.takeIf { outJsProgram },
sourceMapBuilder?.build()
)
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.ir.backend.js.transformers.irToJs
import org.jetbrains.kotlin.ir.backend.js.export.TypeScriptFragment
import org.jetbrains.kotlin.ir.backend.js.utils.toJsIdentifier
import org.jetbrains.kotlin.js.backend.ast.*
import java.io.File
@@ -16,7 +17,7 @@ class JsIrProgramFragment(val packageFqn: String) {
val exports = JsCompositeBlock()
val importedModules = mutableListOf<JsImportedModule>()
val imports = mutableMapOf<String, JsExpression>()
var dts: String? = null
var dts: TypeScriptFragment? = null
val classes = mutableMapOf<JsName, JsIrIcClassModel>()
val initializers = JsCompositeBlock()
var mainFunction: JsStatement? = null
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.ir.backend.js.utils.serialization
import org.jetbrains.kotlin.ir.backend.js.export.TypeScriptFragment
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.JsIrIcClassModel
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.JsIrProgramFragment
import org.jetbrains.kotlin.ir.backend.js.utils.emptyScope
@@ -81,7 +82,9 @@ class JsIrAstDeserializer : JsAstDeserializerBase() {
fragment.suiteFn = deserializeName(proto.suiteFunction)
}
fragment.dts = proto.dts
if (proto.hasDts()) {
fragment.dts = TypeScriptFragment(proto.dts)
}
fragment.definitions += proto.definitionsList.map { deserializeString(it) }
return fragment
@@ -81,7 +81,7 @@ class JsIrAstSerializer: JsAstSerializerBase() {
}
fragment.dts?.let {
fragmentBuilder.dts = it
fragmentBuilder.dts = it.raw
}
fragment.suiteFn?.let {