JVM_IR: only serialize inline functions themselves with -Xserialize-ir=inline
This commit is contained in:
committed by
TeamCityServer
parent
4caa71538d
commit
a9ce25cf33
+8
-20
@@ -9,8 +9,7 @@ import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclaration
|
|||||||
import org.jetbrains.kotlin.backend.common.ir.createParameterDeclarations
|
import org.jetbrains.kotlin.backend.common.ir.createParameterDeclarations
|
||||||
import org.jetbrains.kotlin.backend.common.serialization.signature.PublicIdSignatureComputer
|
import org.jetbrains.kotlin.backend.common.serialization.signature.PublicIdSignatureComputer
|
||||||
import org.jetbrains.kotlin.backend.jvm.lower.SingletonObjectJvmStaticTransformer
|
import org.jetbrains.kotlin.backend.jvm.lower.SingletonObjectJvmStaticTransformer
|
||||||
import org.jetbrains.kotlin.backend.jvm.serialization.deserializeClassFromByteArray
|
import org.jetbrains.kotlin.backend.jvm.serialization.deserializeFromByteArray
|
||||||
import org.jetbrains.kotlin.backend.jvm.serialization.deserializeIrFileFromByteArray
|
|
||||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||||
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
||||||
import org.jetbrains.kotlin.config.JvmSerializeIrMode
|
import org.jetbrains.kotlin.config.JvmSerializeIrMode
|
||||||
@@ -25,7 +24,6 @@ import org.jetbrains.kotlin.ir.builders.declarations.buildClass
|
|||||||
import org.jetbrains.kotlin.ir.declarations.*
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
|
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
|
||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
|
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
|
||||||
import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyClass
|
|
||||||
import org.jetbrains.kotlin.ir.IrBuiltIns
|
import org.jetbrains.kotlin.ir.IrBuiltIns
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrDelegatingConstructorCall
|
import org.jetbrains.kotlin.ir.expressions.IrDelegatingConstructorCall
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
|
||||||
@@ -119,28 +117,18 @@ open class JvmGeneratorExtensionsImpl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deserializeLazyClass(
|
override fun deserializeClass(
|
||||||
irClass: IrLazyClass,
|
|
||||||
stubGenerator: DeclarationStubGenerator,
|
|
||||||
parent: IrDeclarationParent,
|
|
||||||
allowErrorNodes: Boolean
|
|
||||||
): Boolean {
|
|
||||||
val serializedIr = (irClass.source as? KotlinJvmBinarySourceElement)?.binaryClass?.classHeader?.serializedIr ?: return false
|
|
||||||
deserializeClassFromByteArray(
|
|
||||||
serializedIr, stubGenerator, irClass, JvmIrTypeSystemContext(stubGenerator.irBuiltIns), allowErrorNodes
|
|
||||||
)
|
|
||||||
irClass.transform(SingletonObjectJvmStaticTransformer(stubGenerator.irBuiltIns, cachedFields), null)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun deserializeFacadeClass(
|
|
||||||
irClass: IrClass,
|
irClass: IrClass,
|
||||||
stubGenerator: DeclarationStubGenerator,
|
stubGenerator: DeclarationStubGenerator,
|
||||||
parent: IrDeclarationParent,
|
parent: IrDeclarationParent,
|
||||||
allowErrorNodes: Boolean
|
allowErrorNodes: Boolean
|
||||||
): Boolean {
|
): Boolean {
|
||||||
val serializedIr = (irClass.source as? JvmPackagePartSource)?.knownJvmBinaryClass?.classHeader?.serializedIr ?: return false
|
val serializedIr = when (val source = irClass.source) {
|
||||||
deserializeIrFileFromByteArray(
|
is KotlinJvmBinarySourceElement -> source.binaryClass.classHeader.serializedIr
|
||||||
|
is JvmPackagePartSource -> source.knownJvmBinaryClass?.classHeader?.serializedIr
|
||||||
|
else -> null
|
||||||
|
} ?: return false
|
||||||
|
deserializeFromByteArray(
|
||||||
serializedIr, stubGenerator, irClass, JvmIrTypeSystemContext(stubGenerator.irBuiltIns), allowErrorNodes
|
serializedIr, stubGenerator, irClass, JvmIrTypeSystemContext(stubGenerator.irBuiltIns), allowErrorNodes
|
||||||
)
|
)
|
||||||
irClass.transform(SingletonObjectJvmStaticTransformer(stubGenerator.irBuiltIns, cachedFields), null)
|
irClass.transform(SingletonObjectJvmStaticTransformer(stubGenerator.irBuiltIns, cachedFields), null)
|
||||||
|
|||||||
+4
-4
@@ -19,13 +19,13 @@ class JvmIrSerializerImpl(private val configuration: CompilerConfiguration) : Jv
|
|||||||
|
|
||||||
private val declarationTable = DeclarationTable(JvmGlobalDeclarationTable())
|
private val declarationTable = DeclarationTable(JvmGlobalDeclarationTable())
|
||||||
|
|
||||||
override fun serializeIrFile(irFile: IrFile): ByteArray {
|
override fun serializeIrFile(irFile: IrFile): ByteArray? {
|
||||||
return makeSerializerSession().serializeJvmIrFile(irFile).toByteArray()
|
return makeSerializerSession().serializeJvmIrFile(irFile)?.toByteArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun serializeTopLevelIrClass(irClass: IrClass): ByteArray {
|
override fun serializeTopLevelIrClass(irClass: IrClass): ByteArray? {
|
||||||
assert(irClass.parent is IrFile)
|
assert(irClass.parent is IrFile)
|
||||||
return makeSerializerSession().serializeTopLevelClass(irClass).toByteArray()
|
return makeSerializerSession().serializeTopLevelClass(irClass)?.toByteArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun makeSerializerSession() =
|
private fun makeSerializerSession() =
|
||||||
|
|||||||
@@ -28,8 +28,7 @@ class JvmFileFacadeClass(
|
|||||||
private var irLoaded: Boolean? = null
|
private var irLoaded: Boolean? = null
|
||||||
|
|
||||||
override fun loadIr(): Boolean {
|
override fun loadIr(): Boolean {
|
||||||
irLoaded?.let { return it }
|
return irLoaded ?:
|
||||||
irLoaded = stubGenerator.extensions.deserializeFacadeClass(this, stubGenerator, parent, allowErrorNodes = false)
|
stubGenerator.extensions.deserializeClass(this, stubGenerator, parent, allowErrorNodes = false).also { irLoaded = it }
|
||||||
return irLoaded!!
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,6 @@ import org.jetbrains.kotlin.ir.declarations.IrClass
|
|||||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||||
|
|
||||||
interface JvmIrSerializer {
|
interface JvmIrSerializer {
|
||||||
fun serializeTopLevelIrClass(irClass: IrClass): ByteArray
|
fun serializeTopLevelIrClass(irClass: IrClass): ByteArray?
|
||||||
fun serializeIrFile(irFile: IrFile): ByteArray
|
fun serializeIrFile(irFile: IrFile): ByteArray?
|
||||||
}
|
}
|
||||||
@@ -119,9 +119,7 @@ class IrLazyClass(
|
|||||||
|
|
||||||
override fun loadIr(): Boolean {
|
override fun loadIr(): Boolean {
|
||||||
assert(parent is IrPackageFragment)
|
assert(parent is IrPackageFragment)
|
||||||
irLoaded?.let { return it }
|
return irLoaded ?:
|
||||||
return stubGenerator.extensions.deserializeLazyClass(
|
stubGenerator.extensions.deserializeClass(this, stubGenerator, parent, allowErrorNodes = false).also { irLoaded = it }
|
||||||
this, stubGenerator, parent, allowErrorNodes = false
|
|
||||||
).also { irLoaded = it }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-2
@@ -39,13 +39,15 @@ interface IrLazyDeclarationBase : IrDeclaration {
|
|||||||
descriptor.annotations.mapNotNull(typeTranslator.constantValueGenerator::generateAnnotationConstructorCall).toMutableList()
|
descriptor.annotations.mapNotNull(typeTranslator.constantValueGenerator::generateAnnotationConstructorCall).toMutableList()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createLazyParent(): ReadWriteProperty<Any?, IrDeclarationParent> = lazyVar(stubGenerator.lock) {
|
fun createLazyParent(): ReadWriteProperty<Any?, IrDeclarationParent> = lazyVar(stubGenerator.lock, ::lazyParent)
|
||||||
|
|
||||||
|
fun lazyParent(): IrDeclarationParent {
|
||||||
val currentDescriptor = descriptor
|
val currentDescriptor = descriptor
|
||||||
|
|
||||||
val containingDeclaration =
|
val containingDeclaration =
|
||||||
((currentDescriptor as? PropertyAccessorDescriptor)?.correspondingProperty ?: currentDescriptor).containingDeclaration
|
((currentDescriptor as? PropertyAccessorDescriptor)?.correspondingProperty ?: currentDescriptor).containingDeclaration
|
||||||
|
|
||||||
when (containingDeclaration) {
|
return when (containingDeclaration) {
|
||||||
is PackageFragmentDescriptor -> run {
|
is PackageFragmentDescriptor -> run {
|
||||||
val parent = this.takeUnless { it is IrClass }?.let {
|
val parent = this.takeUnless { it is IrClass }?.let {
|
||||||
stubGenerator.generateOrGetFacadeClass(descriptor)
|
stubGenerator.generateOrGetFacadeClass(descriptor)
|
||||||
|
|||||||
@@ -39,14 +39,7 @@ open class StubGeneratorExtensions {
|
|||||||
|
|
||||||
open fun isStaticFunction(descriptor: FunctionDescriptor): Boolean = false
|
open fun isStaticFunction(descriptor: FunctionDescriptor): Boolean = false
|
||||||
|
|
||||||
open fun deserializeLazyClass(
|
open fun deserializeClass(
|
||||||
irClass: IrLazyClass,
|
|
||||||
stubGenerator: DeclarationStubGenerator,
|
|
||||||
parent: IrDeclarationParent,
|
|
||||||
allowErrorNodes: Boolean,
|
|
||||||
): Boolean = false
|
|
||||||
|
|
||||||
open fun deserializeFacadeClass(
|
|
||||||
irClass: IrClass,
|
irClass: IrClass,
|
||||||
stubGenerator: DeclarationStubGenerator,
|
stubGenerator: DeclarationStubGenerator,
|
||||||
parent: IrDeclarationParent,
|
parent: IrDeclarationParent,
|
||||||
|
|||||||
+1
-1
@@ -1428,7 +1428,7 @@ open class IrFileSerializer(
|
|||||||
open fun keepOrderOfProperties(property: IrProperty): Boolean = !property.isConst
|
open fun keepOrderOfProperties(property: IrProperty): Boolean = !property.isConst
|
||||||
open fun backendSpecificSerializeAllMembers(irClass: IrClass) = false
|
open fun backendSpecificSerializeAllMembers(irClass: IrClass) = false
|
||||||
|
|
||||||
fun memberNeedsSerialization(member: IrDeclaration): Boolean {
|
open fun memberNeedsSerialization(member: IrDeclaration): Boolean {
|
||||||
val parent = member.parent
|
val parent = member.parent
|
||||||
require(parent is IrClass)
|
require(parent is IrClass)
|
||||||
if (backendSpecificSerializeAllMembers(parent)) return true
|
if (backendSpecificSerializeAllMembers(parent)) return true
|
||||||
|
|||||||
@@ -14,23 +14,11 @@ message XStatementOrExpression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
message AuxTables {
|
message ClassOrFile {
|
||||||
// TODO: optimize the representation.
|
|
||||||
repeated common.serialization.proto.IrType type = 1;
|
|
||||||
repeated common.serialization.proto.IdSignature signature = 2;
|
|
||||||
repeated string string = 3;
|
|
||||||
repeated XStatementOrExpression body = 4;
|
|
||||||
repeated string debug_info = 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
message JvmIrFile {
|
|
||||||
repeated common.serialization.proto.IrDeclaration declaration = 1;
|
repeated common.serialization.proto.IrDeclaration declaration = 1;
|
||||||
repeated common.serialization.proto.IrConstructorCall annotation = 2;
|
repeated common.serialization.proto.IrType type = 2;
|
||||||
repeated int32 facade_fq_name = 3;
|
repeated common.serialization.proto.IdSignature signature = 3;
|
||||||
required AuxTables aux_tables = 4;
|
repeated string string = 4;
|
||||||
}
|
repeated XStatementOrExpression body = 5;
|
||||||
|
repeated string debug_info = 6;
|
||||||
message JvmIrClass {
|
|
||||||
required common.serialization.proto.IrClass ir_class = 1;
|
|
||||||
required AuxTables aux_tables = 2;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+71
-24
@@ -11,17 +11,18 @@ import org.jetbrains.kotlin.backend.common.serialization.IrFileSerializer
|
|||||||
import org.jetbrains.kotlin.backend.jvm.serialization.proto.JvmIr
|
import org.jetbrains.kotlin.backend.jvm.serialization.proto.JvmIr
|
||||||
import org.jetbrains.kotlin.config.JvmSerializeIrMode
|
import org.jetbrains.kotlin.config.JvmSerializeIrMode
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
import org.jetbrains.kotlin.ir.IrElement
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||||
import org.jetbrains.kotlin.ir.util.IrMessageLogger
|
import org.jetbrains.kotlin.ir.util.IrMessageLogger
|
||||||
import org.jetbrains.kotlin.protobuf.ByteString
|
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
|
||||||
|
import org.jetbrains.kotlin.synthetic.isVisibleOutside
|
||||||
|
|
||||||
class JvmIrSerializerSession(
|
class JvmIrSerializerSession(
|
||||||
messageLogger: IrMessageLogger,
|
messageLogger: IrMessageLogger,
|
||||||
private val declarationTable: DeclarationTable,
|
private val declarationTable: DeclarationTable,
|
||||||
expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>,
|
expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>,
|
||||||
mode: JvmSerializeIrMode,
|
private val mode: JvmSerializeIrMode,
|
||||||
skipExpects: Boolean = false,
|
skipExpects: Boolean = false,
|
||||||
) : IrFileSerializer(
|
) : IrFileSerializer(
|
||||||
messageLogger, declarationTable, expectDescriptorToSymbol, CompatibilityMode.CURRENT,
|
messageLogger, declarationTable, expectDescriptorToSymbol, CompatibilityMode.CURRENT,
|
||||||
@@ -34,42 +35,88 @@ class JvmIrSerializerSession(
|
|||||||
|
|
||||||
// Usage protocol: construct an instance, call only one of `serializeIrFile()` and `serializeTopLevelClass()` only once.
|
// Usage protocol: construct an instance, call only one of `serializeIrFile()` and `serializeTopLevelClass()` only once.
|
||||||
|
|
||||||
fun serializeJvmIrFile(irFile: IrFile): JvmIr.JvmIrFile {
|
fun serializeJvmIrFile(irFile: IrFile): JvmIr.ClassOrFile? {
|
||||||
val proto = JvmIr.JvmIrFile.newBuilder()
|
var anySaved = false
|
||||||
|
val proto = JvmIr.ClassOrFile.newBuilder()
|
||||||
|
|
||||||
declarationTable.inFile(irFile) {
|
declarationTable.inFile(irFile) {
|
||||||
irFile.declarations.filter { it !is IrClass }.forEach { declaration ->
|
irFile.declarations.filter { it !is IrClass }.forEach { topDeclaration ->
|
||||||
|
forEveryDeclarationToSerialize(topDeclaration, mode) { declaration ->
|
||||||
|
proto.addDeclaration(serializeDeclaration(declaration))
|
||||||
|
anySaved = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!anySaved) return null
|
||||||
|
|
||||||
|
serializeAuxTables(proto)
|
||||||
|
|
||||||
|
return proto.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun serializeTopLevelClass(irClass: IrClass): JvmIr.ClassOrFile? {
|
||||||
|
val proto = JvmIr.ClassOrFile.newBuilder()
|
||||||
|
declarationTable.inFile(irClass.parent as IrFile) {
|
||||||
|
forEveryDeclarationToSerialize(irClass, mode) { declaration ->
|
||||||
proto.addDeclaration(serializeDeclaration(declaration))
|
proto.addDeclaration(serializeDeclaration(declaration))
|
||||||
}
|
}
|
||||||
proto.addAllAnnotation(serializeAnnotations(irFile.annotations))
|
|
||||||
}
|
}
|
||||||
|
serializeAuxTables(proto)
|
||||||
proto.auxTables = serializeAuxTables()
|
|
||||||
|
|
||||||
return proto.build()
|
return proto.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun serializeTopLevelClass(irClass: IrClass): JvmIr.JvmIrClass {
|
private fun serializeAuxTables(proto: JvmIr.ClassOrFile.Builder) {
|
||||||
val proto = JvmIr.JvmIrClass.newBuilder()
|
|
||||||
declarationTable.inFile(irClass.parent as IrFile) {
|
|
||||||
proto.irClass = serializeIrClass(irClass)
|
|
||||||
}
|
|
||||||
proto.auxTables = serializeAuxTables()
|
|
||||||
return proto.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun serializeAuxTables(): JvmIr.AuxTables {
|
|
||||||
val proto = JvmIr.AuxTables.newBuilder()
|
|
||||||
protoTypeArray.forEach(proto::addType)
|
protoTypeArray.forEach(proto::addType)
|
||||||
protoIdSignatureArray.forEach(proto::addSignature)
|
protoIdSignatureArray.forEach(proto::addSignature)
|
||||||
protoStringArray.forEach(proto::addString)
|
protoStringArray.forEach(proto::addString)
|
||||||
protoBodyArray.forEach { proto.addBody(it.toProto()) }
|
protoBodyArray.forEach { proto.addBody(it.toProto()) }
|
||||||
protoDebugInfoArray.forEach(proto::addDebugInfo)
|
protoDebugInfoArray.forEach(proto::addDebugInfo)
|
||||||
return proto.build()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun XStatementOrExpression.toProto(): JvmIr.XStatementOrExpression = when (this) {
|
fun XStatementOrExpression.toProto(): JvmIr.XStatementOrExpression = when (this) {
|
||||||
is XStatementOrExpression.XStatement -> JvmIr.XStatementOrExpression.newBuilder().setStatement(toProtoStatement()).build()
|
is XStatementOrExpression.XStatement -> JvmIr.XStatementOrExpression.newBuilder().setStatement(toProtoStatement()).build()
|
||||||
is XStatementOrExpression.XExpression -> JvmIr.XStatementOrExpression.newBuilder().setExpression(toProtoExpression()).build()
|
is XStatementOrExpression.XExpression -> JvmIr.XStatementOrExpression.newBuilder().setExpression(toProtoExpression()).build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun forEveryDeclarationToSerialize(topDeclaration: IrDeclaration, mode: JvmSerializeIrMode, action: (IrDeclaration) -> Unit) {
|
||||||
|
when (mode) {
|
||||||
|
JvmSerializeIrMode.NONE -> error("should not even be called with serialization mode NONE")
|
||||||
|
JvmSerializeIrMode.ALL -> action(topDeclaration)
|
||||||
|
JvmSerializeIrMode.INLINE ->
|
||||||
|
topDeclaration.accept(ForVisibleInlineFunctionsVisitor, action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private object ForVisibleInlineFunctionsVisitor : IrElementVisitor<Unit, (IrDeclaration) -> Unit> {
|
||||||
|
override fun visitElement(element: IrElement, data: (IrDeclaration) -> Unit) {
|
||||||
|
error("Visitor only for nonlocal declarations")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun visitDeclaration(declaration: IrDeclarationBase, data: (IrDeclaration) -> Unit) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun visitClass(declaration: IrClass, data: (IrDeclaration) -> Unit) {
|
||||||
|
if (!declaration.visibility.isVisibleOutside()) return
|
||||||
|
for (child in declaration.declarations) {
|
||||||
|
child.accept(this, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun visitProperty(declaration: IrProperty, data: (IrDeclaration) -> Unit) {
|
||||||
|
if (!declaration.visibility.isVisibleOutside()) return
|
||||||
|
declaration.getter?.accept(this, data)
|
||||||
|
declaration.setter?.accept(this, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun visitSimpleFunction(declaration: IrSimpleFunction, data: (IrDeclaration) -> Unit) {
|
||||||
|
val action = data
|
||||||
|
if (declaration.visibility.isVisibleOutside() &&
|
||||||
|
declaration.isInline &&
|
||||||
|
!declaration.isFakeOverride
|
||||||
|
) {
|
||||||
|
action(declaration)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+24
-72
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmIrMangler
|
|||||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
|
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyDeclarationBase
|
||||||
import org.jetbrains.kotlin.ir.declarations.lazy.LazyIrFactory
|
import org.jetbrains.kotlin.ir.declarations.lazy.LazyIrFactory
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
|
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
|
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
|
||||||
@@ -35,22 +36,22 @@ import org.jetbrains.kotlin.backend.common.serialization.proto.IrExpression as P
|
|||||||
import org.jetbrains.kotlin.backend.common.serialization.proto.IrStatement as ProtoStatement
|
import org.jetbrains.kotlin.backend.common.serialization.proto.IrStatement as ProtoStatement
|
||||||
import org.jetbrains.kotlin.backend.common.serialization.proto.IrType as ProtoType
|
import org.jetbrains.kotlin.backend.common.serialization.proto.IrType as ProtoType
|
||||||
|
|
||||||
fun deserializeClassFromByteArray(
|
fun deserializeFromByteArray(
|
||||||
byteArray: ByteArray,
|
byteArray: ByteArray,
|
||||||
stubGenerator: DeclarationStubGenerator,
|
stubGenerator: DeclarationStubGenerator,
|
||||||
irClass: IrClass,
|
toplevelParent: IrClass,
|
||||||
typeSystemContext: IrTypeSystemContext,
|
typeSystemContext: IrTypeSystemContext,
|
||||||
allowErrorNodes: Boolean,
|
allowErrorNodes: Boolean,
|
||||||
) {
|
) {
|
||||||
val irBuiltIns = stubGenerator.irBuiltIns
|
val irBuiltIns = stubGenerator.irBuiltIns
|
||||||
val symbolTable = stubGenerator.symbolTable
|
val symbolTable = stubGenerator.symbolTable
|
||||||
val irProto = JvmIr.JvmIrClass.parseFrom(byteArray.codedInputStream)
|
val irProto = JvmIr.ClassOrFile.parseFrom(byteArray.codedInputStream)
|
||||||
val irLibraryFile = IrLibraryFileFromAnnotation(
|
val irLibraryFile = IrLibraryFileFromAnnotation(
|
||||||
irProto.auxTables.typeList,
|
irProto.typeList,
|
||||||
irProto.auxTables.signatureList,
|
irProto.signatureList,
|
||||||
irProto.auxTables.stringList,
|
irProto.stringList,
|
||||||
irProto.auxTables.bodyList,
|
irProto.bodyList,
|
||||||
irProto.auxTables.debugInfoList
|
irProto.debugInfoList
|
||||||
)
|
)
|
||||||
val descriptorFinder =
|
val descriptorFinder =
|
||||||
DescriptorByIdSignatureFinder(
|
DescriptorByIdSignatureFinder(
|
||||||
@@ -60,65 +61,7 @@ fun deserializeClassFromByteArray(
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Only needed for local signature computation.
|
// Only needed for local signature computation.
|
||||||
val dummyIrFile = IrFileImpl(NaiveSourceBasedFileEntryImpl("<unknown>"), IrFileSymbolImpl(), irClass.packageFqName!!)
|
val dummyIrFile = IrFileImpl(NaiveSourceBasedFileEntryImpl("<unknown>"), IrFileSymbolImpl(), toplevelParent.packageFqName!!)
|
||||||
|
|
||||||
val symbolDeserializer = IrSymbolDeserializer(
|
|
||||||
symbolTable,
|
|
||||||
irLibraryFile,
|
|
||||||
fileSymbol = dummyIrFile.symbol,
|
|
||||||
/* TODO */ actuals = emptyList(),
|
|
||||||
enqueueLocalTopLevelDeclaration = {}, // just link to it in symbolTable
|
|
||||||
handleExpectActualMapping = { _, _ -> TODO() },
|
|
||||||
deserializePublicSymbol = { idSignature, symbolKind ->
|
|
||||||
referencePublicSymbol(symbolTable, descriptorFinder, idSignature, symbolKind)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
val lazyIrFactory = LazyIrFactory(irBuiltIns.irFactory)
|
|
||||||
|
|
||||||
val deserializer = IrDeclarationDeserializer(
|
|
||||||
irBuiltIns, symbolTable, lazyIrFactory, irLibraryFile, irClass.parent,
|
|
||||||
allowErrorNodes = allowErrorNodes,
|
|
||||||
deserializeInlineFunctions = true,
|
|
||||||
deserializeBodies = true,
|
|
||||||
symbolDeserializer,
|
|
||||||
DefaultFakeOverrideClassFilter,
|
|
||||||
makeSimpleFakeOverrideBuilder(symbolTable, typeSystemContext, symbolDeserializer),
|
|
||||||
compatibilityMode = CompatibilityMode.CURRENT,
|
|
||||||
)
|
|
||||||
|
|
||||||
deserializer.deserializeIrClass(irProto.irClass)
|
|
||||||
|
|
||||||
ExternalDependenciesGenerator(stubGenerator.symbolTable, listOf(stubGenerator)).generateUnboundSymbolsAsDependencies()
|
|
||||||
buildFakeOverridesForLocalClasses(stubGenerator.symbolTable, typeSystemContext, symbolDeserializer, irClass)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun deserializeIrFileFromByteArray(
|
|
||||||
byteArray: ByteArray,
|
|
||||||
stubGenerator: DeclarationStubGenerator,
|
|
||||||
facadeClass: IrClass,
|
|
||||||
typeSystemContext: IrTypeSystemContext,
|
|
||||||
allowErrorNodes: Boolean,
|
|
||||||
) {
|
|
||||||
val irBuiltIns = stubGenerator.irBuiltIns
|
|
||||||
val symbolTable = stubGenerator.symbolTable
|
|
||||||
val irProto = JvmIr.JvmIrFile.parseFrom(byteArray.codedInputStream)
|
|
||||||
val irLibraryFile = IrLibraryFileFromAnnotation(
|
|
||||||
irProto.auxTables.typeList,
|
|
||||||
irProto.auxTables.signatureList,
|
|
||||||
irProto.auxTables.stringList,
|
|
||||||
irProto.auxTables.bodyList,
|
|
||||||
irProto.auxTables.debugInfoList
|
|
||||||
)
|
|
||||||
val descriptorFinder =
|
|
||||||
DescriptorByIdSignatureFinder(
|
|
||||||
stubGenerator.moduleDescriptor,
|
|
||||||
JvmDescriptorMangler(null),
|
|
||||||
DescriptorByIdSignatureFinder.LookupMode.MODULE_WITH_DEPENDENCIES
|
|
||||||
)
|
|
||||||
|
|
||||||
// Only needed for local signature computation.
|
|
||||||
val dummyIrFile = IrFileImpl(NaiveSourceBasedFileEntryImpl("<unknown>"), IrFileSymbolImpl(), facadeClass.packageFqName!!)
|
|
||||||
|
|
||||||
val symbolDeserializer = IrSymbolDeserializer(
|
val symbolDeserializer = IrSymbolDeserializer(
|
||||||
symbolTable,
|
symbolTable,
|
||||||
@@ -136,8 +79,10 @@ fun deserializeIrFileFromByteArray(
|
|||||||
|
|
||||||
val fakeOverrideBuilder = makeSimpleFakeOverrideBuilder(symbolTable, typeSystemContext, symbolDeserializer)
|
val fakeOverrideBuilder = makeSimpleFakeOverrideBuilder(symbolTable, typeSystemContext, symbolDeserializer)
|
||||||
|
|
||||||
|
// We have to supply topLevelParent here, but this results in wrong values for parent fields in deeply embedded declarations.
|
||||||
|
// Patching will be needed.
|
||||||
val deserializer = IrDeclarationDeserializer(
|
val deserializer = IrDeclarationDeserializer(
|
||||||
irBuiltIns, symbolTable, lazyIrFactory, irLibraryFile, facadeClass,
|
irBuiltIns, symbolTable, lazyIrFactory, irLibraryFile, toplevelParent,
|
||||||
allowErrorNodes = allowErrorNodes,
|
allowErrorNodes = allowErrorNodes,
|
||||||
deserializeInlineFunctions = true,
|
deserializeInlineFunctions = true,
|
||||||
deserializeBodies = true,
|
deserializeBodies = true,
|
||||||
@@ -147,11 +92,17 @@ fun deserializeIrFileFromByteArray(
|
|||||||
compatibilityMode = CompatibilityMode.CURRENT,
|
compatibilityMode = CompatibilityMode.CURRENT,
|
||||||
)
|
)
|
||||||
for (declarationProto in irProto.declarationList) {
|
for (declarationProto in irProto.declarationList) {
|
||||||
deserializer.deserializeDeclaration(declarationProto)
|
val declaration = deserializer.deserializeDeclaration(declarationProto)
|
||||||
|
// Either declaration is lazy, supplied by LazyIrFactory,
|
||||||
|
// or, if it is newly created, there must have been no references to it from the current module.
|
||||||
|
// These newly created declarations will never be used, so it does not matter if we patch their parent or not.
|
||||||
|
if (declaration is IrLazyDeclarationBase) {
|
||||||
|
declaration.parent = declaration.lazyParent()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ExternalDependenciesGenerator(stubGenerator.symbolTable, listOf(stubGenerator)).generateUnboundSymbolsAsDependencies()
|
ExternalDependenciesGenerator(stubGenerator.symbolTable, listOf(stubGenerator)).generateUnboundSymbolsAsDependencies()
|
||||||
buildFakeOverridesForLocalClasses(stubGenerator.symbolTable, typeSystemContext, symbolDeserializer, facadeClass)
|
buildFakeOverridesForLocalClasses(stubGenerator.symbolTable, typeSystemContext, symbolDeserializer, toplevelParent)
|
||||||
}
|
}
|
||||||
|
|
||||||
private class IrLibraryFileFromAnnotation(
|
private class IrLibraryFileFromAnnotation(
|
||||||
@@ -245,7 +196,8 @@ private fun buildFakeOverridesForLocalClasses(
|
|||||||
toplevel: IrClass
|
toplevel: IrClass
|
||||||
) {
|
) {
|
||||||
val builder = makeSimpleFakeOverrideBuilder(symbolTable, typeSystemContext, symbolDeserializer)
|
val builder = makeSimpleFakeOverrideBuilder(symbolTable, typeSystemContext, symbolDeserializer)
|
||||||
toplevel.acceptChildrenVoid(object : IrElementVisitorVoid {
|
toplevel.acceptChildrenVoid(
|
||||||
|
object : IrElementVisitorVoid {
|
||||||
override fun visitElement(element: IrElement) {
|
override fun visitElement(element: IrElement) {
|
||||||
element.acceptChildrenVoid(this)
|
element.acceptChildrenVoid(this)
|
||||||
}
|
}
|
||||||
@@ -268,4 +220,4 @@ class PrePopulatedDeclarationTable(
|
|||||||
symbol2Sig[declaration.symbol]?.let { return it }
|
symbol2Sig[declaration.symbol]?.let { return it }
|
||||||
return super.tryComputeBackendSpecificSignature(declaration)
|
return super.tryComputeBackendSpecificSignature(declaration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+826
-2221
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user