diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Environment.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Environment.kt index 9bec763161..973c761994 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Environment.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Environment.kt @@ -18,35 +18,38 @@ package com.google.j2cl.transpiler.backend.kotlin import com.google.j2cl.transpiler.ast.HasName import com.google.j2cl.transpiler.backend.kotlin.ast.Import -/** Code generation environment. */ +/** + * Code generation environment. + * + * @property nameToIdentifierMap a map from named node to rendered identifier string + * @property identifierSet a set of used identifier strings, which potentially shadow imports + * @property importedSimpleNameToQualifiedNameMap a mutable map from simple name string to qualified + * name string of types to be imported, filled-in during code generation + * @property importedOptInQualifiedNames a mutable set with imported qualified names for [@OptIn] + * annotation, filled-in during code generation + */ internal data class Environment( - /** Name to identifier mapping. */ private val nameToIdentifierMap: Map = emptyMap(), - - /** A set of used identifiers, which potentially shadow imports. */ private val identifierSet: Set = emptySet(), - - /** Mutable map from simple name to qualified name of types to be imported. */ val importedSimpleNameToQualifiedNameMap: MutableMap = mutableMapOf(), - - /** Mutable map with imported classes for OptIn annotation. */ val importedOptInQualifiedNames: MutableSet = mutableSetOf() ) { - /** Returns identifier for the given name */ + /** Returns identifier for the given named node. */ fun identifier(hasName: HasName): String = nameToIdentifierMap[hasName] ?: error("No such identifier: $hasName") /** Returns whether the given identifier is used. */ fun containsIdentifier(identifier: String): Boolean = identifierSet.contains(identifier) -} -/** Returns a list of collected imports. */ -internal fun Environment.imports(): List = - importedSimpleNameToQualifiedNameMap.entries.map { (simpleName, qualifiedName) -> - Import( - qualifiedName.qualifiedNameComponents(), - simpleName - .takeUnless { it == qualifiedName.qualifiedNameToSimpleName() } - ?.let { Import.Suffix.WithAlias(it) } - ) - } + /** A list of collected imports. */ + internal val imports: List + get() = + importedSimpleNameToQualifiedNameMap.entries.map { (simpleName, qualifiedName) -> + Import( + qualifiedName.qualifiedNameComponents(), + simpleName + .takeUnless { it == qualifiedName.qualifiedNameToSimpleName() } + ?.let { Import.Suffix.WithAlias(it) } + ) + } +} diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/ImportRenderer.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/ImportRenderer.kt index 1a07b61ba2..a8f578a8fc 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/ImportRenderer.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/ImportRenderer.kt @@ -33,7 +33,7 @@ internal fun Renderer.importsSource(): Source = newLineSeparated(imports().map { private fun defaultImports() = setOf(starImport("javaemul", "lang")) private fun Renderer.imports(): List = - defaultImports().plus(environment.imports()).sortedWith(lexicographicalOrder()) + defaultImports().plus(environment.imports).sortedWith(lexicographicalOrder()) private fun Import.source(): Source = spaceSeparated(