Somewhat simplified descriptor management for IR serialization.
The public descriptors don't need to keep their containing declaration index. KonanIr.KotlinDescriptor already has this index, so use it instead. That allows to remove some more clutter from KonanLinkData.
This commit is contained in:
committed by
alexander-gorshenev
parent
503399ade3
commit
0859abbb9d
+10
-10
@@ -79,25 +79,26 @@ internal class IrDescriptorDeserializer(val context: Context,
|
||||
context.log{"### deserialized Kotlin Type index=$index, text=$text:\t$realType"}
|
||||
return realType
|
||||
}
|
||||
fun deserializeLocalDeclaration(irProto: KonanIr.KotlinDescriptor, proto: KonanIr.DeclarationDescriptor): DeclarationDescriptor {
|
||||
fun deserializeLocalDeclaration(irProto: KonanIr.KotlinDescriptor): DeclarationDescriptor {
|
||||
val localDeclarationProto = irProto.irLocalDeclaration.descriptor
|
||||
when {
|
||||
proto.hasFunction() -> {
|
||||
val functionProto = proto.function
|
||||
localDeclarationProto.hasFunction() -> {
|
||||
val functionProto = localDeclarationProto.function
|
||||
val index = irProto.index
|
||||
val descriptor = localDeserializer.deserializeFunction(functionProto)
|
||||
val descriptor = localDeserializer.deserializeFunction(irProto)
|
||||
descriptorIndex.put(index, descriptor)
|
||||
return descriptor
|
||||
}
|
||||
|
||||
proto.hasProperty() -> {
|
||||
val propertyProto = proto.property
|
||||
localDeclarationProto.hasProperty() -> {
|
||||
val propertyProto = localDeclarationProto.property
|
||||
val index = irProto.index
|
||||
val descriptor = localDeserializer.deserializeProperty(propertyProto)
|
||||
val descriptor = localDeserializer.deserializeProperty(irProto)
|
||||
descriptorIndex.put(index, descriptor)
|
||||
return descriptor
|
||||
}
|
||||
// TODO
|
||||
// proto.hasClazz() ->
|
||||
// localDclarationProto.hasClazz() ->
|
||||
else -> TODO("Unexpected descriptor kind")
|
||||
}
|
||||
}
|
||||
@@ -135,8 +136,7 @@ internal class IrDescriptorDeserializer(val context: Context,
|
||||
context.log{"### deserializeDescriptor ${proto.kind} ${proto.index}"}
|
||||
|
||||
val descriptor = if (proto.hasIrLocalDeclaration()) {
|
||||
val realDescriptor = proto.irLocalDeclaration.descriptor
|
||||
deserializeLocalDeclaration(proto, realDescriptor)
|
||||
deserializeLocalDeclaration(proto)
|
||||
} else
|
||||
deserializeKnownDescriptor(proto)
|
||||
|
||||
|
||||
+3
-1
@@ -3,7 +3,6 @@ syntax = "proto2";
|
||||
package org.jetbrains.kotlin.serialization;
|
||||
|
||||
import "org/jetbrains/kotlin/backend/konan/serialization/descriptors.proto1";
|
||||
import "org/jetbrains/kotlin/backend/konan/serialization/KonanLinkData.proto";
|
||||
option java_outer_classname = "KonanIr";
|
||||
option optimize_for = LITE_RUNTIME;
|
||||
|
||||
@@ -28,7 +27,10 @@ message KotlinDescriptor {
|
||||
required UniqId original_uniq_id = 4;
|
||||
// index in fq name table
|
||||
optional int32 class_or_package = 5;
|
||||
|
||||
// Descriptor kind specific fields.
|
||||
// TODO: consider introducing specific messages
|
||||
// for functions, variables etc.
|
||||
optional KotlinType type = 6;
|
||||
repeated KotlinDescriptor value_parameter = 7;
|
||||
repeated KotlinDescriptor type_parameter = 8;
|
||||
|
||||
-3
@@ -25,18 +25,15 @@ extend Class {
|
||||
|
||||
extend Constructor {
|
||||
repeated Annotation constructor_annotation = 170;
|
||||
optional int32 constructor_parent = 172;
|
||||
}
|
||||
|
||||
extend Function {
|
||||
repeated Annotation function_annotation = 170;
|
||||
optional int32 function_parent = 172;
|
||||
optional InlineIrBody inline_ir_body = 174;
|
||||
}
|
||||
|
||||
extend Property {
|
||||
repeated Annotation property_annotation = 170;
|
||||
optional int32 property_parent = 172;
|
||||
optional bool used_as_variable = 173;
|
||||
optional Annotation.Argument.Value compile_time_value = 174;
|
||||
optional InlineIrBody inline_getter_ir_body = 175;
|
||||
|
||||
-18
@@ -53,22 +53,8 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer
|
||||
super.serializeEnumEntry(descriptor, proto)
|
||||
}
|
||||
|
||||
fun DeclarationDescriptor.parentFqNameIndex(): Int? {
|
||||
|
||||
if (this.containingDeclaration is ClassOrPackageFragmentDescriptor) {
|
||||
val parentIndex = stringTable.getClassOrPackageFqNameIndex(
|
||||
this.containingDeclaration as ClassOrPackageFragmentDescriptor)
|
||||
return parentIndex
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
override fun serializeConstructor(descriptor: ConstructorDescriptor, proto: ProtoBuf.Constructor.Builder) {
|
||||
|
||||
val parentIndex = descriptor.parentFqNameIndex()
|
||||
if (parentIndex != null) proto.setExtension(KonanLinkData.constructorParent, parentIndex)
|
||||
|
||||
super.serializeConstructor(descriptor, proto)
|
||||
}
|
||||
|
||||
@@ -79,8 +65,6 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer
|
||||
|
||||
override fun serializeFunction(descriptor: FunctionDescriptor, proto: ProtoBuf.Function.Builder) {
|
||||
|
||||
val parentIndex = descriptor.parentFqNameIndex()
|
||||
if (parentIndex != null) proto.setExtension(KonanLinkData.functionParent, parentIndex)
|
||||
super.serializeFunction(descriptor, proto)
|
||||
}
|
||||
|
||||
@@ -91,8 +75,6 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer
|
||||
backingFieldClass, emptyMap(), SourceElement.NO_SOURCE)
|
||||
|
||||
override fun serializeProperty(descriptor: PropertyDescriptor, proto: ProtoBuf.Property.Builder) {
|
||||
val parentIndex = descriptor.parentFqNameIndex()
|
||||
if (parentIndex != null) proto.setExtension(KonanLinkData.propertyParent, parentIndex)
|
||||
val variable = originalVariables[descriptor]
|
||||
if (variable != null) {
|
||||
proto.setExtension(KonanLinkData.usedAsVariable, true)
|
||||
|
||||
+12
-10
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.backend.konan.serialization
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor
|
||||
import org.jetbrains.kotlin.serialization.Flags
|
||||
import org.jetbrains.kotlin.serialization.KonanIr
|
||||
import org.jetbrains.kotlin.serialization.KonanLinkData
|
||||
import org.jetbrains.kotlin.serialization.ProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.ProtoBuf.QualifiedNameTable.QualifiedName
|
||||
@@ -29,7 +30,6 @@ import org.jetbrains.kotlin.serialization.deserialization.descriptors.*
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.SinceKotlinInfoTable
|
||||
import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.protobuf.GeneratedMessageLite
|
||||
|
||||
// This class knows how to construct contexts for
|
||||
// MemberDeserializer to deserialize descriptors declared in IR.
|
||||
@@ -97,29 +97,31 @@ class LocalDeclarationDeserializer(val rootDescriptor: DeserializedCallableMembe
|
||||
|
||||
}
|
||||
|
||||
private fun memberDeserializer(proto: GeneratedMessageLite): MemberDeserializer {
|
||||
val containingFqName = proto.parent
|
||||
private fun memberDeserializer(irProto: KonanIr.KotlinDescriptor): MemberDeserializer {
|
||||
val containingFqName = irProto.classOrPackage
|
||||
return if (containingFqName != null) {
|
||||
memberDeserializerByParentFqNameIndex(containingFqName)
|
||||
// TODO: learn to take the containing IR declaration
|
||||
} else this.memberDeserializer
|
||||
}
|
||||
|
||||
fun deserializeFunction(proto: ProtoBuf.Function): FunctionDescriptor =
|
||||
memberDeserializer(proto).loadFunction(proto)
|
||||
fun deserializeFunction(irProto: KonanIr.KotlinDescriptor): FunctionDescriptor =
|
||||
memberDeserializer(irProto).loadFunction(irProto.irLocalDeclaration.descriptor.function)
|
||||
|
||||
fun deserializeConstructor(proto: ProtoBuf.Constructor): ConstructorDescriptor {
|
||||
fun deserializeConstructor(irProto: KonanIr.KotlinDescriptor): ConstructorDescriptor {
|
||||
|
||||
val containingFqName = proto.getExtension(KonanLinkData.constructorParent)
|
||||
val memberDeserializer = memberDeserializerByParentFqNameIndex(containingFqName)
|
||||
val proto = irProto.irLocalDeclaration.descriptor.constructor
|
||||
val memberDeserializer = memberDeserializerByParentFqNameIndex(irProto.classOrPackage)
|
||||
val isPrimary = !Flags.IS_SECONDARY.get(proto.flags)
|
||||
val constructor = memberDeserializer.loadConstructor(proto, isPrimary)
|
||||
|
||||
return constructor
|
||||
}
|
||||
|
||||
fun deserializeProperty(proto: ProtoBuf.Property): VariableDescriptor {
|
||||
val property = memberDeserializer(proto).loadProperty(proto)
|
||||
fun deserializeProperty(irProto: KonanIr.KotlinDescriptor): VariableDescriptor {
|
||||
|
||||
val proto = irProto.irLocalDeclaration.descriptor.property
|
||||
val property = memberDeserializer(irProto).loadProperty(proto)
|
||||
|
||||
return if (proto.getExtension(KonanLinkData.usedAsVariable)) {
|
||||
propertyToVariable(property)
|
||||
|
||||
-32
@@ -4,7 +4,6 @@ import org.jetbrains.kotlin.serialization.KonanIr
|
||||
import org.jetbrains.kotlin.serialization.KonanLinkData
|
||||
import org.jetbrains.kotlin.serialization.KonanLinkData.*
|
||||
import org.jetbrains.kotlin.serialization.ProtoBuf
|
||||
import org.jetbrains.kotlin.protobuf.GeneratedMessageLite
|
||||
|
||||
fun newUniqId(index: Long): KonanIr.UniqId =
|
||||
KonanIr.UniqId.newBuilder().setIndex(index).build()
|
||||
@@ -63,37 +62,6 @@ fun inlineBody(encodedIR: String)
|
||||
.setEncodedIr(encodedIR)
|
||||
.build()
|
||||
|
||||
// -----------------------------------------------------------
|
||||
|
||||
val ProtoBuf.Function.parent: Int?
|
||||
get() = if (this.hasExtension(KonanLinkData.functionParent))
|
||||
this.getExtension(KonanLinkData.functionParent)
|
||||
else null
|
||||
|
||||
val ProtoBuf.Property.parent: Int?
|
||||
get() = if (this.hasExtension(KonanLinkData.propertyParent))
|
||||
this.getExtension(KonanLinkData.propertyParent)
|
||||
else null
|
||||
|
||||
|
||||
val ProtoBuf.Constructor.parent: Int?
|
||||
get() = if (this.hasExtension(KonanLinkData.constructorParent))
|
||||
this.getExtension(KonanLinkData.constructorParent)
|
||||
else null
|
||||
|
||||
val GeneratedMessageLite.parent: Int?
|
||||
get() = when (this) {
|
||||
is ProtoBuf.Function
|
||||
-> this.parent
|
||||
is ProtoBuf.Property
|
||||
-> this.parent
|
||||
is ProtoBuf.Constructor
|
||||
-> this.parent
|
||||
else
|
||||
-> error("Unexpected protobuf message")
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------
|
||||
|
||||
internal fun printType(proto: ProtoBuf.Type) {
|
||||
|
||||
Reference in New Issue
Block a user