[KLIB tool] Refactoring. Fix visibility, add comments, drop useless code
What's done: - Reduce the visibility of all entities inside the module to `internal` or `private`. Only leave `fun main()` as `public` because it's the single legal entry point. - Add comments to few classes related to "contents" command that they should be removed together with the command after 2.0. - Drop useless `DeclarationHeaderRenderer` interface. - Rename: KlibToolLinker -> KlibToolIrLinker ^KT-62340
This commit is contained in:
committed by
Space Team
parent
fd4f6c90c7
commit
d4585ff3ce
-7
@@ -1,7 +0,0 @@
|
|||||||
package org.jetbrains.kotlin.cli.klib
|
|
||||||
|
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
|
||||||
|
|
||||||
interface DeclarationHeaderRenderer {
|
|
||||||
fun render(descriptor: DeclarationDescriptor): String
|
|
||||||
}
|
|
||||||
+9
-9
@@ -7,9 +7,9 @@ import org.jetbrains.kotlin.descriptors.DescriptorVisibilities.INTERNAL
|
|||||||
import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies
|
import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies
|
||||||
import org.jetbrains.kotlin.utils.Printer
|
import org.jetbrains.kotlin.utils.Printer
|
||||||
|
|
||||||
|
// TODO: This class is used in dumping metadata by descriptors, "contents" command. Drop it after 2.0. KT-65380
|
||||||
internal class DeclarationPrinter(
|
internal class DeclarationPrinter(
|
||||||
out: Appendable,
|
out: Appendable,
|
||||||
private val headerRenderer: DeclarationHeaderRenderer,
|
|
||||||
private val signatureRenderer: KlibSignatureRenderer
|
private val signatureRenderer: KlibSignatureRenderer
|
||||||
) {
|
) {
|
||||||
private val printer = Printer(out, 1, " ")
|
private val printer = Printer(out, 1, " ")
|
||||||
@@ -53,14 +53,14 @@ internal class DeclarationPrinter(
|
|||||||
.filter { it.shouldBePrinted }
|
.filter { it.shouldBePrinted }
|
||||||
.sortedBy { it.name }
|
.sortedBy { it.name }
|
||||||
if (children.isNotEmpty()) {
|
if (children.isNotEmpty()) {
|
||||||
printer.printWithBody(header = headerRenderer.render(descriptor)) {
|
printer.printWithBody(header = DefaultDeclarationHeaderRenderer.render(descriptor)) {
|
||||||
children.forEach { it.accept(this, data) }
|
children.forEach { it.accept(this, data) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitClassDescriptor(descriptor: ClassDescriptor, data: Unit) {
|
override fun visitClassDescriptor(descriptor: ClassDescriptor, data: Unit) {
|
||||||
val header = headerRenderer.render(descriptor)
|
val header = DefaultDeclarationHeaderRenderer.render(descriptor)
|
||||||
val signature = signatureRenderer.render(descriptor)
|
val signature = signatureRenderer.render(descriptor)
|
||||||
|
|
||||||
val children = descriptor.unsubstitutedMemberScope.getContributedDescriptors().filter { it.shouldBePrinted }
|
val children = descriptor.unsubstitutedMemberScope.getContributedDescriptors().filter { it.shouldBePrinted }
|
||||||
@@ -76,19 +76,19 @@ internal class DeclarationPrinter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: Unit) {
|
override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: Unit) {
|
||||||
printer.printPlain(header = headerRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
|
printer.printPlain(header = DefaultDeclarationHeaderRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, data: Unit) {
|
override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, data: Unit) {
|
||||||
printer.printPlain(header = headerRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
|
printer.printPlain(header = DefaultDeclarationHeaderRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
|
||||||
descriptor.getter?.takeUnless { canSkipAccessor(it, descriptor) }?.let { getter ->
|
descriptor.getter?.takeUnless { canSkipAccessor(it, descriptor) }?.let { getter ->
|
||||||
printer.pushIndent()
|
printer.pushIndent()
|
||||||
printer.printPlain(header = headerRenderer.render(getter), signature = signatureRenderer.render(getter))
|
printer.printPlain(header = DefaultDeclarationHeaderRenderer.render(getter), signature = signatureRenderer.render(getter))
|
||||||
printer.popIndent()
|
printer.popIndent()
|
||||||
}
|
}
|
||||||
descriptor.setter?.takeUnless { canSkipAccessor(it, descriptor) }?.let { setter ->
|
descriptor.setter?.takeUnless { canSkipAccessor(it, descriptor) }?.let { setter ->
|
||||||
printer.pushIndent()
|
printer.pushIndent()
|
||||||
printer.printPlain(header = headerRenderer.render(setter), signature = signatureRenderer.render(setter))
|
printer.printPlain(header = DefaultDeclarationHeaderRenderer.render(setter), signature = signatureRenderer.render(setter))
|
||||||
printer.popIndent()
|
printer.popIndent()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -98,11 +98,11 @@ internal class DeclarationPrinter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor, data: Unit) {
|
override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor, data: Unit) {
|
||||||
printer.printPlain(header = headerRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
|
printer.printPlain(header = DefaultDeclarationHeaderRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitTypeAliasDescriptor(descriptor: TypeAliasDescriptor, data: Unit) {
|
override fun visitTypeAliasDescriptor(descriptor: TypeAliasDescriptor, data: Unit) {
|
||||||
printer.printPlain(header = headerRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
|
printer.printPlain(header = DefaultDeclarationHeaderRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -7,8 +7,9 @@ import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
|||||||
import org.jetbrains.kotlin.renderer.DescriptorRendererModifier
|
import org.jetbrains.kotlin.renderer.DescriptorRendererModifier
|
||||||
import org.jetbrains.kotlin.renderer.OverrideRenderingPolicy
|
import org.jetbrains.kotlin.renderer.OverrideRenderingPolicy
|
||||||
|
|
||||||
object DefaultDeclarationHeaderRenderer : DeclarationHeaderRenderer {
|
// TODO: This class is used in dumping metadata by descriptors, "contents" command. Drop it after 2.0. KT-65380
|
||||||
override fun render(descriptor: DeclarationDescriptor): String = when (descriptor) {
|
internal object DefaultDeclarationHeaderRenderer {
|
||||||
|
fun render(descriptor: DeclarationDescriptor): String = when (descriptor) {
|
||||||
is PackageFragmentDescriptor -> render(descriptor)
|
is PackageFragmentDescriptor -> render(descriptor)
|
||||||
is ClassifierDescriptorWithTypeParameters -> render(descriptor)
|
is ClassifierDescriptorWithTypeParameters -> render(descriptor)
|
||||||
is PropertyAccessorDescriptor -> render(descriptor)
|
is PropertyAccessorDescriptor -> render(descriptor)
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import org.jetbrains.kotlin.backend.konan.serialization.KonanManglerIr
|
|||||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||||
import org.jetbrains.kotlin.ir.IrBuiltIns
|
import org.jetbrains.kotlin.ir.IrBuiltIns
|
||||||
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
|
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
|
||||||
import org.jetbrains.kotlin.ir.builders.TranslationPluginContext
|
|
||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
|
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
|
||||||
import org.jetbrains.kotlin.ir.types.IrTypeSystemContextImpl
|
import org.jetbrains.kotlin.ir.types.IrTypeSystemContextImpl
|
||||||
import org.jetbrains.kotlin.ir.util.DumpIrTreeOptions
|
import org.jetbrains.kotlin.ir.util.DumpIrTreeOptions
|
||||||
@@ -40,7 +39,7 @@ import java.io.File
|
|||||||
import kotlin.system.exitProcess
|
import kotlin.system.exitProcess
|
||||||
import org.jetbrains.kotlin.konan.file.File as KFile
|
import org.jetbrains.kotlin.konan.file.File as KFile
|
||||||
|
|
||||||
fun printUsage() {
|
private fun printUsage() {
|
||||||
println(
|
println(
|
||||||
"""
|
"""
|
||||||
Usage: klib <command> <library> [<option>]
|
Usage: klib <command> <library> [<option>]
|
||||||
@@ -98,7 +97,7 @@ private fun parseOptions(args: Array<String>): Map<String, List<String>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private class Command(args: Array<String>) {
|
internal class Command(args: Array<String>) {
|
||||||
init {
|
init {
|
||||||
if (args.size < 2) {
|
if (args.size < 2) {
|
||||||
printUsage()
|
printUsage()
|
||||||
@@ -132,20 +131,7 @@ private fun Command.parseSignatureVersion(): KotlinIrSignatureVersion? {
|
|||||||
return signatureVersion
|
return signatureVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun logWarning(text: String) {
|
internal object KlibToolLogger : Logger, IrMessageLogger {
|
||||||
println("warning: $text")
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun logError(text: String, withStacktrace: Boolean = false): Nothing {
|
|
||||||
if (withStacktrace)
|
|
||||||
error("error: $text")
|
|
||||||
else {
|
|
||||||
System.err.println("error: $text")
|
|
||||||
exitProcess(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
object KlibToolLogger : Logger, IrMessageLogger {
|
|
||||||
override fun log(message: String) = println(message)
|
override fun log(message: String) = println(message)
|
||||||
override fun warning(message: String) = logWarning(message)
|
override fun warning(message: String) = logWarning(message)
|
||||||
override fun error(message: String) = logWarning(message)
|
override fun error(message: String) = logWarning(message)
|
||||||
@@ -162,7 +148,20 @@ object KlibToolLogger : Logger, IrMessageLogger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val defaultRepository = KFile(DependencyDirectories.localKonanDir.resolve("klib").absolutePath)
|
internal fun logWarning(text: String) {
|
||||||
|
println("warning: $text")
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun logError(text: String, withStacktrace: Boolean = false): Nothing {
|
||||||
|
if (withStacktrace)
|
||||||
|
error("error: $text")
|
||||||
|
else {
|
||||||
|
System.err.println("error: $text")
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val defaultRepository = KFile(DependencyDirectories.localKonanDir.resolve("klib").absolutePath)
|
||||||
|
|
||||||
private class KlibRepoDeprecationWarning {
|
private class KlibRepoDeprecationWarning {
|
||||||
private var alreadyLogged = false
|
private var alreadyLogged = false
|
||||||
@@ -175,11 +174,11 @@ private class KlibRepoDeprecationWarning {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Library(val libraryNameOrPath: String, val requestedRepository: String?) {
|
internal class Library(val libraryNameOrPath: String, requestedRepository: String?) {
|
||||||
|
|
||||||
private val klibRepoDeprecationWarning = KlibRepoDeprecationWarning()
|
private val klibRepoDeprecationWarning = KlibRepoDeprecationWarning()
|
||||||
|
|
||||||
val repository = requestedRepository?.let {
|
private val repository = requestedRepository?.let {
|
||||||
klibRepoDeprecationWarning.logOnceIfNecessary() // Due to use of "-repository" option.
|
klibRepoDeprecationWarning.logOnceIfNecessary() // Due to use of "-repository" option.
|
||||||
KFile(it)
|
KFile(it)
|
||||||
} ?: defaultRepository
|
} ?: defaultRepository
|
||||||
@@ -242,9 +241,11 @@ class Library(val libraryNameOrPath: String, val requestedRepository: String?) {
|
|||||||
library?.libraryFile?.deleteRecursively()
|
library?.libraryFile?.deleteRecursively()
|
||||||
}
|
}
|
||||||
|
|
||||||
class KlibToolLinker(
|
class KlibToolIrLinker(
|
||||||
module: ModuleDescriptor, irBuiltIns: IrBuiltIns, symbolTable: SymbolTable
|
module: ModuleDescriptor,
|
||||||
) : KotlinIrLinker(module, KlibToolLogger, irBuiltIns, symbolTable, emptyList()) {
|
irBuiltIns: IrBuiltIns,
|
||||||
|
symbolTable: SymbolTable
|
||||||
|
) : KotlinIrLinker(module, KlibToolLogger, irBuiltIns, symbolTable, exportedDependencies = emptyList()) {
|
||||||
override val fakeOverrideBuilder = IrLinkerFakeOverrideProvider(
|
override val fakeOverrideBuilder = IrLinkerFakeOverrideProvider(
|
||||||
linker = this,
|
linker = this,
|
||||||
symbolTable = symbolTable,
|
symbolTable = symbolTable,
|
||||||
@@ -254,25 +255,23 @@ class Library(val libraryNameOrPath: String, val requestedRepository: String?) {
|
|||||||
partialLinkageSupport = PartialLinkageSupportForLinker.DISABLED,
|
partialLinkageSupport = PartialLinkageSupportForLinker.DISABLED,
|
||||||
)
|
)
|
||||||
|
|
||||||
override val returnUnboundSymbolsIfSignatureNotFound: Boolean
|
override val returnUnboundSymbolsIfSignatureNotFound get() = true
|
||||||
get() = true
|
|
||||||
|
|
||||||
override val translationPluginContext: TranslationPluginContext
|
override val translationPluginContext get() = TODO("Not needed for ir dumping")
|
||||||
get() = TODO("Not needed for ir dumping")
|
|
||||||
|
|
||||||
override fun createModuleDeserializer(moduleDescriptor: ModuleDescriptor, klib: KotlinLibrary?, strategyResolver: (String) -> DeserializationStrategy): IrModuleDeserializer {
|
override fun createModuleDeserializer(
|
||||||
return KlibToolModuleDeserializer(
|
moduleDescriptor: ModuleDescriptor,
|
||||||
module = moduleDescriptor,
|
klib: KotlinLibrary?,
|
||||||
klib = klib ?: error("Expecting kotlin library for $moduleDescriptor"),
|
strategyResolver: (String) -> DeserializationStrategy
|
||||||
strategyResolver = strategyResolver
|
): IrModuleDeserializer = KlibToolModuleDeserializer(
|
||||||
)
|
module = moduleDescriptor,
|
||||||
}
|
klib = klib ?: error("Expecting kotlin library for $moduleDescriptor"),
|
||||||
|
strategyResolver = strategyResolver
|
||||||
|
)
|
||||||
|
|
||||||
override fun isBuiltInModule(moduleDescriptor: ModuleDescriptor): Boolean {
|
override fun isBuiltInModule(moduleDescriptor: ModuleDescriptor) = false
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
inner class KlibToolModuleDeserializer(
|
private inner class KlibToolModuleDeserializer(
|
||||||
module: ModuleDescriptor,
|
module: ModuleDescriptor,
|
||||||
klib: KotlinLibrary,
|
klib: KotlinLibrary,
|
||||||
strategyResolver: (String) -> DeserializationStrategy
|
strategyResolver: (String) -> DeserializationStrategy
|
||||||
@@ -302,7 +301,7 @@ class Library(val libraryNameOrPath: String, val requestedRepository: String?) {
|
|||||||
val typeTranslator = TypeTranslatorImpl(symbolTable, ModuleDescriptorLoader.languageVersionSettings, module)
|
val typeTranslator = TypeTranslatorImpl(symbolTable, ModuleDescriptorLoader.languageVersionSettings, module)
|
||||||
val irBuiltIns = IrBuiltInsOverDescriptors(module.builtIns, typeTranslator, symbolTable)
|
val irBuiltIns = IrBuiltInsOverDescriptors(module.builtIns, typeTranslator, symbolTable)
|
||||||
|
|
||||||
val linker = KlibToolLinker(module, irBuiltIns, symbolTable)
|
val linker = KlibToolIrLinker(module, irBuiltIns, symbolTable)
|
||||||
module.allDependencyModules.forEach {
|
module.allDependencyModules.forEach {
|
||||||
linker.deserializeOnlyHeaderModule(it, it.kotlinLibrary)
|
linker.deserializeOnlyHeaderModule(it, it.kotlinLibrary)
|
||||||
linker.resolveModuleDeserializer(it, null).init()
|
linker.resolveModuleDeserializer(it, null).init()
|
||||||
@@ -363,6 +362,7 @@ class Library(val libraryNameOrPath: String, val requestedRepository: String?) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: This command is deprecated. Drop it after 2.0. KT-65380
|
||||||
fun contents(output: Appendable, printSignatures: Boolean, signatureVersion: KotlinIrSignatureVersion?) {
|
fun contents(output: Appendable, printSignatures: Boolean, signatureVersion: KotlinIrSignatureVersion?) {
|
||||||
logWarning("\"contents\" has been renamed to \"dump-metadata\". Please, use new command name.")
|
logWarning("\"contents\" has been renamed to \"dump-metadata\". Please, use new command name.")
|
||||||
val module = ModuleDescriptorLoader.load(libraryInRepoOrCurrentDir(repository, libraryNameOrPath))
|
val module = ModuleDescriptorLoader.load(libraryInRepoOrCurrentDir(repository, libraryNameOrPath))
|
||||||
@@ -370,7 +370,7 @@ class Library(val libraryNameOrPath: String, val requestedRepository: String?) {
|
|||||||
DefaultKlibSignatureRenderer(signatureVersion, "// Signature: ")
|
DefaultKlibSignatureRenderer(signatureVersion, "// Signature: ")
|
||||||
else
|
else
|
||||||
KlibSignatureRenderer.NO_SIGNATURE
|
KlibSignatureRenderer.NO_SIGNATURE
|
||||||
val printer = DeclarationPrinter(output, DefaultDeclarationHeaderRenderer, signatureRenderer)
|
val printer = DeclarationPrinter(output, signatureRenderer)
|
||||||
|
|
||||||
printer.print(module)
|
printer.print(module)
|
||||||
}
|
}
|
||||||
@@ -419,12 +419,12 @@ class Library(val libraryNameOrPath: String, val requestedRepository: String?) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun libraryInRepo(repository: KFile, name: String) =
|
private fun libraryInRepo(repository: KFile, name: String) =
|
||||||
resolverByName(listOf(repository.absolutePath), skipCurrentDir = true, logger = KlibToolLogger).resolve(name)
|
resolverByName(listOf(repository.absolutePath), skipCurrentDir = true, logger = KlibToolLogger).resolve(name)
|
||||||
|
|
||||||
fun libraryInCurrentDir(name: String) = resolverByName(emptyList(), logger = KlibToolLogger).resolve(name)
|
private fun libraryInCurrentDir(name: String) = resolverByName(emptyList(), logger = KlibToolLogger).resolve(name)
|
||||||
|
|
||||||
fun libraryInRepoOrCurrentDir(repository: KFile, name: String) =
|
private fun libraryInRepoOrCurrentDir(repository: KFile, name: String) =
|
||||||
resolverByName(listOf(repository.absolutePath), logger = KlibToolLogger).resolve(name)
|
resolverByName(listOf(repository.absolutePath), logger = KlibToolLogger).resolve(name)
|
||||||
|
|
||||||
private enum class KnownOption(val option: String) {
|
private enum class KnownOption(val option: String) {
|
||||||
|
|||||||
Reference in New Issue
Block a user