Reflect the absence of parent fqname index in the absence of protobuf extension.

This commit is contained in:
Alexander Gorshenev
2017-04-26 01:12:50 +03:00
committed by alexander-gorshenev
parent 0cc3fa56ab
commit 31c490e14e
5 changed files with 54 additions and 29 deletions
@@ -134,7 +134,7 @@ internal class IrDescriptorSerializer(
val classOrPackage = descriptor.classOrPackage val classOrPackage = descriptor.classOrPackage
val parentFqNameIndex = if (classOrPackage is ClassOrPackageFragmentDescriptor) { val parentFqNameIndex = if (classOrPackage is ClassOrPackageFragmentDescriptor) {
stringTable.getClassOrPackageFqNameIndex(classOrPackage) stringTable.getClassOrPackageFqNameIndex(classOrPackage)
} else { -1 } } else null
val index = descriptorTable.indexByValue(descriptor) val index = descriptorTable.indexByValue(descriptor)
// For getters and setters we use // For getters and setters we use
@@ -150,7 +150,9 @@ internal class IrDescriptorSerializer(
.setKind(kotlinDescriptorKind(descriptor)) .setKind(kotlinDescriptorKind(descriptor))
.setIndex(index) .setIndex(index)
.setOriginalIndex(originalIndex) .setOriginalIndex(originalIndex)
.setClassOrPackage(parentFqNameIndex)
if (parentFqNameIndex != null)
proto.setClassOrPackage(parentFqNameIndex)
when (descriptor) { when (descriptor) {
is FunctionDescriptor -> is FunctionDescriptor ->
@@ -26,7 +26,7 @@ message KotlinDescriptor {
required UniqId uniq_id = 3; required UniqId uniq_id = 3;
required UniqId original_uniq_id = 4; required UniqId original_uniq_id = 4;
// index in fq name table // index in fq name table
required int32 class_or_package = 5; optional int32 class_or_package = 5;
// Descriptor kind specific fields. // Descriptor kind specific fields.
optional KotlinType type = 6; optional KotlinType type = 6;
repeated KotlinDescriptor value_parameter = 7; repeated KotlinDescriptor value_parameter = 7;
@@ -53,14 +53,14 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer
super.serializeEnumEntry(descriptor, proto) super.serializeEnumEntry(descriptor, proto)
} }
fun DeclarationDescriptor.parentFqNameIndex(): Int { fun DeclarationDescriptor.parentFqNameIndex(): Int? {
if (this.containingDeclaration is ClassOrPackageFragmentDescriptor) { if (this.containingDeclaration is ClassOrPackageFragmentDescriptor) {
val parentIndex = stringTable.getClassOrPackageFqNameIndex( val parentIndex = stringTable.getClassOrPackageFqNameIndex(
this.containingDeclaration as ClassOrPackageFragmentDescriptor) this.containingDeclaration as ClassOrPackageFragmentDescriptor)
return parentIndex return parentIndex
} else { } else {
return -1 return null
} }
} }
@@ -69,7 +69,7 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer
proto.setConstructorIndex( proto.setConstructorIndex(
inlineDescriptorTable.indexByValue(descriptor)) inlineDescriptorTable.indexByValue(descriptor))
val parentIndex = descriptor.parentFqNameIndex() val parentIndex = descriptor.parentFqNameIndex()
proto.setExtension(KonanLinkData.constructorParent, parentIndex) if (parentIndex != null) proto.setExtension(KonanLinkData.constructorParent, parentIndex)
super.serializeConstructor(descriptor, proto) super.serializeConstructor(descriptor, proto)
} }
@@ -86,7 +86,7 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer
proto.setFunctionIndex( proto.setFunctionIndex(
inlineDescriptorTable.indexByValue(descriptor)) inlineDescriptorTable.indexByValue(descriptor))
val parentIndex = descriptor.parentFqNameIndex() val parentIndex = descriptor.parentFqNameIndex()
proto.setExtension(KonanLinkData.functionParent, parentIndex) if (parentIndex != null) proto.setExtension(KonanLinkData.functionParent, parentIndex)
super.serializeFunction(descriptor, proto) super.serializeFunction(descriptor, proto)
} }
@@ -98,7 +98,7 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer
override fun serializeProperty(descriptor: PropertyDescriptor, proto: ProtoBuf.Property.Builder) { override fun serializeProperty(descriptor: PropertyDescriptor, proto: ProtoBuf.Property.Builder) {
val parentIndex = descriptor.parentFqNameIndex() val parentIndex = descriptor.parentFqNameIndex()
proto.setExtension(KonanLinkData.propertyParent, parentIndex) if (parentIndex != null) proto.setExtension(KonanLinkData.propertyParent, parentIndex)
val variable = originalVariables[descriptor] val variable = originalVariables[descriptor]
if (variable != null) { if (variable != null) {
proto.setExtension(KonanLinkData.usedAsVariable, true) proto.setExtension(KonanLinkData.usedAsVariable, true)
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.serialization.deserialization.descriptors.*
import org.jetbrains.kotlin.serialization.deserialization.descriptors.SinceKotlinInfoTable import org.jetbrains.kotlin.serialization.deserialization.descriptors.SinceKotlinInfoTable
import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.protobuf.GeneratedMessageLite
// This class knows how to construct contexts for // This class knows how to construct contexts for
// MemberDeserializer to deserialize descriptors declared in IR. // MemberDeserializer to deserialize descriptors declared in IR.
@@ -84,9 +85,9 @@ class LocalDeclarationDeserializer(val rootDescriptor: DeserializedCallableMembe
} }
} }
fun memberDeserializerByParentFqNameIndex(fqName: Int): MemberDeserializer { fun memberDeserializerByParentFqNameIndex(fqNameIndex: Int): MemberDeserializer {
val parent = getDescriptorByFqNameIndex(module, fqName) val parent = getDescriptorByFqNameIndex(module, fqNameIndex)
val typeParameters = if (parent is DeserializedClassDescriptor) { val typeParameters = if (parent is DeserializedClassDescriptor) {
parent.classProto.typeParameterList parent.classProto.typeParameterList
} else listOf<ProtoBuf.TypeParameter>() } else listOf<ProtoBuf.TypeParameter>()
@@ -96,18 +97,17 @@ class LocalDeclarationDeserializer(val rootDescriptor: DeserializedCallableMembe
} }
fun deserializeFunction(proto: ProtoBuf.Function): FunctionDescriptor { private fun memberDeserializer(proto: GeneratedMessageLite): MemberDeserializer {
val containingFqName = proto.getExtension(KonanLinkData.functionParent) val containingFqName = proto.parent
// TODO: learn to take the containing IR declaration return if (containingFqName != null) {
val memberDeserializer = if (containingFqName == -1)
this.memberDeserializer
else
memberDeserializerByParentFqNameIndex(containingFqName) memberDeserializerByParentFqNameIndex(containingFqName)
// TODO: learn to take the containing IR declaration
val function = memberDeserializer.loadFunction(proto) } else this.memberDeserializer
return function
} }
fun deserializeFunction(proto: ProtoBuf.Function): FunctionDescriptor =
memberDeserializer(proto).loadFunction(proto)
fun deserializeConstructor(proto: ProtoBuf.Constructor): ConstructorDescriptor { fun deserializeConstructor(proto: ProtoBuf.Constructor): ConstructorDescriptor {
val containingFqName = proto.getExtension(KonanLinkData.constructorParent) val containingFqName = proto.getExtension(KonanLinkData.constructorParent)
@@ -118,17 +118,8 @@ class LocalDeclarationDeserializer(val rootDescriptor: DeserializedCallableMembe
return constructor return constructor
} }
fun deserializeProperty(proto: ProtoBuf.Property): VariableDescriptor { fun deserializeProperty(proto: ProtoBuf.Property): VariableDescriptor {
val containingFqName = proto.getExtension(KonanLinkData.propertyParent) val property = memberDeserializer(proto).loadProperty(proto)
// TODO: learn to take the containing IR declaration
val memberDeserializer = if (containingFqName == -1)
this.memberDeserializer
else
memberDeserializerByParentFqNameIndex(containingFqName)
val property = memberDeserializer.loadProperty(proto)
return if (proto.getExtension(KonanLinkData.usedAsVariable)) { return if (proto.getExtension(KonanLinkData.usedAsVariable)) {
propertyToVariable(property) propertyToVariable(property)
@@ -4,6 +4,7 @@ import org.jetbrains.kotlin.serialization.KonanIr
import org.jetbrains.kotlin.serialization.KonanLinkData import org.jetbrains.kotlin.serialization.KonanLinkData
import org.jetbrains.kotlin.serialization.KonanLinkData.* import org.jetbrains.kotlin.serialization.KonanLinkData.*
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.protobuf.GeneratedMessageLite
fun newUniqId(index: Long): KonanLinkData.UniqId = fun newUniqId(index: Long): KonanLinkData.UniqId =
KonanLinkData.UniqId.newBuilder().setIndex(index).build() KonanLinkData.UniqId.newBuilder().setIndex(index).build()
@@ -84,6 +85,37 @@ fun inlineBody(encodedIR: String)
.setEncodedIr(encodedIR) .setEncodedIr(encodedIR)
.build() .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) { internal fun printType(proto: ProtoBuf.Type) {