Expect/actual support in klibs under -Xklib-mpp
This commit is contained in:
committed by
alexander-gorshenev
parent
218d7c31ed
commit
dc8240c24e
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsMangler
|
||||
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.metadata.KlibMetadataIncrementalSerializer
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
import org.jetbrains.kotlin.ir.util.ExpectDeclarationRemover
|
||||
import org.jetbrains.kotlin.ir.util.IrDeserializer
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
@@ -80,6 +81,9 @@ val emptyLoggingContext = object : LoggingContext {
|
||||
private val CompilerConfiguration.metadataVersion
|
||||
get() = get(CommonConfigurationKeys.METADATA_VERSION) as? KlibMetadataVersion ?: KlibMetadataVersion.INSTANCE
|
||||
|
||||
private val CompilerConfiguration.klibMpp: Boolean
|
||||
get() = get(CommonConfigurationKeys.KLIB_MPP) ?: false
|
||||
|
||||
class KotlinFileSerializedData(val metadata: ByteArray, val irData: SerializedIrFile)
|
||||
|
||||
fun generateKLib(
|
||||
@@ -124,11 +128,16 @@ fun generateKLib(
|
||||
|
||||
val psi2IrContext = runAnalysisAndPreparePsi2Ir(depsDescriptors)
|
||||
|
||||
val moduleFragment = psi2IrContext.generateModuleFragmentWithPlugins(project, files)
|
||||
val expectDescriptorToSymbol = mutableMapOf<DeclarationDescriptor, IrSymbol>()
|
||||
|
||||
val moduleFragment = psi2IrContext.generateModuleFragmentWithPlugins(project, files,
|
||||
deserializer = null, expectDescriptorToSymbol = expectDescriptorToSymbol)
|
||||
|
||||
val moduleName = configuration[CommonConfigurationKeys.MODULE_NAME]!!
|
||||
|
||||
moduleFragment.acceptVoid(ExpectDeclarationRemover(psi2IrContext.symbolTable, false))
|
||||
if (!configuration.klibMpp) {
|
||||
moduleFragment.acceptVoid(ExpectDeclarationRemover(psi2IrContext.symbolTable, false))
|
||||
}
|
||||
|
||||
serializeModuleIntoKlib(
|
||||
moduleName,
|
||||
@@ -138,6 +147,7 @@ fun generateKLib(
|
||||
outputKlibPath,
|
||||
allDependencies.getFullList(),
|
||||
moduleFragment,
|
||||
expectDescriptorToSymbol,
|
||||
icData,
|
||||
nopack
|
||||
)
|
||||
@@ -181,6 +191,8 @@ fun loadIr(
|
||||
deserializer.deserializeIrModuleHeader(depsDescriptors.getModuleDescriptor(it))!!
|
||||
}
|
||||
|
||||
deserializer.initializeExpectActualLinker()
|
||||
|
||||
val moduleFragment = psi2IrContext.generateModuleFragmentWithPlugins(project, files, deserializer)
|
||||
|
||||
return IrModuleInfo(moduleFragment, deserializedModuleFragments, irBuiltIns, symbolTable, deserializer)
|
||||
@@ -202,7 +214,8 @@ private fun runAnalysisAndPreparePsi2Ir(depsDescriptors: ModulesStructure): Gene
|
||||
fun GeneratorContext.generateModuleFragmentWithPlugins(
|
||||
project: Project,
|
||||
files: List<KtFile>,
|
||||
deserializer: IrDeserializer? = null
|
||||
deserializer: IrDeserializer? = null,
|
||||
expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>? = null
|
||||
): IrModuleFragment {
|
||||
val irProviders = generateTypicalIrProviderList(moduleDescriptor, irBuiltIns, symbolTable, deserializer)
|
||||
val psi2Ir = Psi2IrTranslator(languageVersionSettings, configuration, mangler = JsMangler)
|
||||
@@ -227,16 +240,17 @@ fun GeneratorContext.generateModuleFragmentWithPlugins(
|
||||
psi2Ir.generateModuleFragment(
|
||||
this,
|
||||
files,
|
||||
irProviders
|
||||
irProviders,
|
||||
expectDescriptorToSymbol
|
||||
)
|
||||
return moduleFragment
|
||||
}
|
||||
|
||||
fun GeneratorContext.generateModuleFragment(files: List<KtFile>, deserializer: IrDeserializer? = null): IrModuleFragment {
|
||||
fun GeneratorContext.generateModuleFragment(files: List<KtFile>, deserializer: IrDeserializer? = null, expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>? = null): IrModuleFragment {
|
||||
val irProviders = generateTypicalIrProviderList(moduleDescriptor, irBuiltIns, symbolTable, deserializer)
|
||||
return Psi2IrTranslator(
|
||||
languageVersionSettings, configuration, mangler = JsMangler
|
||||
).generateModuleFragment(this, files, irProviders)
|
||||
).generateModuleFragment(this, files, irProviders, expectDescriptorToSymbol)
|
||||
}
|
||||
|
||||
|
||||
@@ -358,6 +372,7 @@ fun serializeModuleIntoKlib(
|
||||
klibPath: String,
|
||||
dependencies: List<KotlinLibrary>,
|
||||
moduleFragment: IrModuleFragment,
|
||||
expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>,
|
||||
cleanFiles: List<KotlinFileSerializedData>,
|
||||
nopack: Boolean
|
||||
) {
|
||||
@@ -365,7 +380,7 @@ fun serializeModuleIntoKlib(
|
||||
|
||||
val descriptorTable = DescriptorTable.createDefault()
|
||||
val serializedIr =
|
||||
JsIrModuleSerializer(emptyLoggingContext, moduleFragment.irBuiltins, descriptorTable).serializedIrModule(moduleFragment)
|
||||
JsIrModuleSerializer(emptyLoggingContext, moduleFragment.irBuiltins, descriptorTable, skipExpects = !configuration.klibMpp, expectDescriptorToSymbol = expectDescriptorToSymbol).serializedIrModule(moduleFragment)
|
||||
|
||||
val moduleDescriptor = moduleFragment.descriptor
|
||||
|
||||
@@ -375,7 +390,10 @@ fun serializeModuleIntoKlib(
|
||||
val metadataSerializer = KlibMetadataIncrementalSerializer(
|
||||
languageVersionSettings,
|
||||
metadataVersion,
|
||||
descriptorTable)
|
||||
moduleDescriptor,
|
||||
descriptorTable,
|
||||
skipExpects = !configuration.klibMpp
|
||||
)
|
||||
|
||||
fun serializeScope(fqName: FqName, memberScope: Collection<DeclarationDescriptor>): ByteArray {
|
||||
return metadataSerializer.serializePackageFragment(
|
||||
|
||||
+5
-1
@@ -9,12 +9,16 @@ import org.jetbrains.kotlin.backend.common.LoggingContext
|
||||
import org.jetbrains.kotlin.backend.common.serialization.IrFileSerializer
|
||||
import org.jetbrains.kotlin.backend.common.serialization.DeclarationTable
|
||||
import org.jetbrains.kotlin.ir.declarations.IrProperty
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
|
||||
class JsIrFileSerializer(
|
||||
logger: LoggingContext,
|
||||
declarationTable: DeclarationTable,
|
||||
private val expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>,
|
||||
skipExpects: Boolean,
|
||||
bodiesOnlyForInlines: Boolean = false
|
||||
) : IrFileSerializer(logger, declarationTable, bodiesOnlyForInlines) {
|
||||
) : IrFileSerializer(logger, declarationTable, expectDescriptorToSymbol, bodiesOnlyForInlines = bodiesOnlyForInlines, skipExpects = skipExpects) {
|
||||
|
||||
// Temporary keep order of any property, even of constants
|
||||
override fun keepOrderOfProperties(property: IrProperty): Boolean = true
|
||||
|
||||
+10
-3
@@ -9,14 +9,21 @@ import org.jetbrains.kotlin.backend.common.LoggingContext
|
||||
import org.jetbrains.kotlin.backend.common.serialization.DeclarationTable
|
||||
import org.jetbrains.kotlin.backend.common.serialization.DescriptorTable
|
||||
import org.jetbrains.kotlin.backend.common.serialization.IrModuleSerializer
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
|
||||
class JsIrModuleSerializer(logger: LoggingContext, irBuiltIns: IrBuiltIns, private val descriptorTable: DescriptorTable) :
|
||||
IrModuleSerializer<JsIrFileSerializer>(logger) {
|
||||
class JsIrModuleSerializer(
|
||||
logger: LoggingContext,
|
||||
irBuiltIns: IrBuiltIns,
|
||||
private val descriptorTable: DescriptorTable,
|
||||
private val expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>,
|
||||
val skipExpects: Boolean
|
||||
) : IrModuleSerializer<JsIrFileSerializer>(logger) {
|
||||
|
||||
private val globalDeclarationTable = JsGlobalDeclarationTable(irBuiltIns)
|
||||
|
||||
override fun createSerializerForFile(file: IrFile): JsIrFileSerializer =
|
||||
JsIrFileSerializer(logger, DeclarationTable(descriptorTable, globalDeclarationTable, 0))
|
||||
JsIrFileSerializer(logger, DeclarationTable(descriptorTable, globalDeclarationTable, 0), expectDescriptorToSymbol, skipExpects = skipExpects)
|
||||
}
|
||||
Reference in New Issue
Block a user