From 31c490e14e979657fa0e70a824431de23a1aa89e Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Wed, 26 Apr 2017 01:12:50 +0300 Subject: [PATCH] Reflect the absence of parent fqname index in the absence of protobuf extension. --- .../serialization/IrDescriptorSerializer.kt | 6 ++-- .../backend/konan/serialization/KonanIr.proto | 2 +- .../serialization/KonanSerializerExtension.kt | 10 +++--- .../LocalDeclarationDeserializer.kt | 33 +++++++------------ .../konan/serialization/ProtobufUtil.kt | 32 ++++++++++++++++++ 5 files changed, 54 insertions(+), 29 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorSerializer.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorSerializer.kt index c311d65de44..a420bf813f0 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorSerializer.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorSerializer.kt @@ -134,7 +134,7 @@ internal class IrDescriptorSerializer( val classOrPackage = descriptor.classOrPackage val parentFqNameIndex = if (classOrPackage is ClassOrPackageFragmentDescriptor) { stringTable.getClassOrPackageFqNameIndex(classOrPackage) - } else { -1 } + } else null val index = descriptorTable.indexByValue(descriptor) // For getters and setters we use @@ -150,7 +150,9 @@ internal class IrDescriptorSerializer( .setKind(kotlinDescriptorKind(descriptor)) .setIndex(index) .setOriginalIndex(originalIndex) - .setClassOrPackage(parentFqNameIndex) + + if (parentFqNameIndex != null) + proto.setClassOrPackage(parentFqNameIndex) when (descriptor) { is FunctionDescriptor -> diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIr.proto b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIr.proto index 2f940a19bb2..0eb16a132c3 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIr.proto +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIr.proto @@ -26,7 +26,7 @@ message KotlinDescriptor { required UniqId uniq_id = 3; required UniqId original_uniq_id = 4; // index in fq name table - required int32 class_or_package = 5; + optional int32 class_or_package = 5; // Descriptor kind specific fields. optional KotlinType type = 6; repeated KotlinDescriptor value_parameter = 7; diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanSerializerExtension.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanSerializerExtension.kt index 626488435ff..639035e785d 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanSerializerExtension.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanSerializerExtension.kt @@ -53,14 +53,14 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer super.serializeEnumEntry(descriptor, proto) } - fun DeclarationDescriptor.parentFqNameIndex(): Int { + fun DeclarationDescriptor.parentFqNameIndex(): Int? { if (this.containingDeclaration is ClassOrPackageFragmentDescriptor) { val parentIndex = stringTable.getClassOrPackageFqNameIndex( this.containingDeclaration as ClassOrPackageFragmentDescriptor) return parentIndex } else { - return -1 + return null } } @@ -69,7 +69,7 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer proto.setConstructorIndex( inlineDescriptorTable.indexByValue(descriptor)) val parentIndex = descriptor.parentFqNameIndex() - proto.setExtension(KonanLinkData.constructorParent, parentIndex) + if (parentIndex != null) proto.setExtension(KonanLinkData.constructorParent, parentIndex) super.serializeConstructor(descriptor, proto) } @@ -86,7 +86,7 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer proto.setFunctionIndex( inlineDescriptorTable.indexByValue(descriptor)) val parentIndex = descriptor.parentFqNameIndex() - proto.setExtension(KonanLinkData.functionParent, parentIndex) + if (parentIndex != null) proto.setExtension(KonanLinkData.functionParent, parentIndex) 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) { val parentIndex = descriptor.parentFqNameIndex() - proto.setExtension(KonanLinkData.propertyParent, parentIndex) + if (parentIndex != null) proto.setExtension(KonanLinkData.propertyParent, parentIndex) val variable = originalVariables[descriptor] if (variable != null) { proto.setExtension(KonanLinkData.usedAsVariable, true) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/LocalDeclarationDeserializer.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/LocalDeclarationDeserializer.kt index 864b747855c..299f1b73766 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/LocalDeclarationDeserializer.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/LocalDeclarationDeserializer.kt @@ -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.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. @@ -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) { parent.classProto.typeParameterList } else listOf() @@ -96,18 +97,17 @@ class LocalDeclarationDeserializer(val rootDescriptor: DeserializedCallableMembe } - fun deserializeFunction(proto: ProtoBuf.Function): FunctionDescriptor { - val containingFqName = proto.getExtension(KonanLinkData.functionParent) - // TODO: learn to take the containing IR declaration - val memberDeserializer = if (containingFqName == -1) - this.memberDeserializer - else + private fun memberDeserializer(proto: GeneratedMessageLite): MemberDeserializer { + val containingFqName = proto.parent + return if (containingFqName != null) { memberDeserializerByParentFqNameIndex(containingFqName) - - val function = memberDeserializer.loadFunction(proto) - return function + // TODO: learn to take the containing IR declaration + } else this.memberDeserializer } + fun deserializeFunction(proto: ProtoBuf.Function): FunctionDescriptor = + memberDeserializer(proto).loadFunction(proto) + fun deserializeConstructor(proto: ProtoBuf.Constructor): ConstructorDescriptor { val containingFqName = proto.getExtension(KonanLinkData.constructorParent) @@ -118,17 +118,8 @@ class LocalDeclarationDeserializer(val rootDescriptor: DeserializedCallableMembe return constructor } - fun deserializeProperty(proto: ProtoBuf.Property): VariableDescriptor { - val containingFqName = proto.getExtension(KonanLinkData.propertyParent) - - // TODO: learn to take the containing IR declaration - val memberDeserializer = if (containingFqName == -1) - this.memberDeserializer - else - memberDeserializerByParentFqNameIndex(containingFqName) - - val property = memberDeserializer.loadProperty(proto) + val property = memberDeserializer(proto).loadProperty(proto) return if (proto.getExtension(KonanLinkData.usedAsVariable)) { propertyToVariable(property) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/ProtobufUtil.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/ProtobufUtil.kt index d769c99f7d4..8963efd7943 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/ProtobufUtil.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/ProtobufUtil.kt @@ -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.ProtoBuf +import org.jetbrains.kotlin.protobuf.GeneratedMessageLite fun newUniqId(index: Long): KonanLinkData.UniqId = KonanLinkData.UniqId.newBuilder().setIndex(index).build() @@ -84,6 +85,37 @@ 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) {