Commonized FileRegistry with SourceFileMap for klib metadata

This commit is contained in:
Alexander Gorshenev
2019-10-05 01:23:02 +03:00
committed by alexander-gorshenev
parent b7a0546634
commit e5dbec4523
11 changed files with 336 additions and 1054 deletions
@@ -33,6 +33,7 @@ message Header {
repeated org.jetbrains.kotlin.metadata.Annotation annotation = 6;
repeated string package_fragment_name = 7;
repeated string empty_package = 8;
repeated File file = 9;
// TODO: Unused.
// TODO: We need to have backend specific extension for versionings.
@@ -42,14 +43,7 @@ message Header {
}
message File {
// If absent, id is the index of the file in the Files.file list
optional int32 id = 1;
required string name = 2;
repeated org.jetbrains.kotlin.metadata.Annotation annotation = 3;
}
message Files {
repeated File file = 1;
required string name = 1;
}
message DescriptorUniqId {
@@ -92,6 +86,10 @@ extend org.jetbrains.kotlin.metadata.EnumEntry {
optional DescriptorUniqId enum_entry_uniq_id = 172;
}
extend org.jetbrains.kotlin.metadata.ValueParameter {
repeated org.jetbrains.kotlin.metadata.Annotation parameter_annotation = 170;
}
extend org.jetbrains.kotlin.metadata.Type {
repeated org.jetbrains.kotlin.metadata.Annotation type_annotation = 170;
}
@@ -102,7 +100,7 @@ extend org.jetbrains.kotlin.metadata.TypeParameter {
}
extend org.jetbrains.kotlin.metadata.PackageFragment {
optional Files package_fragment_files = 170;
repeated int32 package_fragment_files = 170;
optional bool is_empty = 172;
optional string fq_name = 173;
// id in StringTable
@@ -1,26 +1,13 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.common.serialization.metadata
package org.jetbrains.kotlin.library.metadata
import org.jetbrains.kotlin.descriptors.SourceFile
import org.jetbrains.kotlin.library.KotlinLibrary
private class DeserializedSourceFile(
val name_: String, val index: Int, val library: KotlinLibrary) : SourceFile {
override fun getName(): String? = name_
override fun equals(other: Any?): Boolean {
return other is DeserializedSourceFile && library == other.library && index == other.index
}
override fun hashCode(): Int {
return library.hashCode() xor index
}
}
class SourceFileMap {
class KlibMetadataFileRegistry {
private val sourceToIndex = mutableMapOf<SourceFile, Int>()
private val indexToSource = mutableMapOf<Int, SourceFile>()
@@ -37,7 +24,7 @@ class SourceFileMap {
}
fun sourceFile(index: Int): SourceFile =
indexToSource[index] ?: throw Error("Unknown file for $index")
indexToSource[index] ?: throw Error("Unknown file for $index")
fun filesAndClear() =
sourceToIndex.keys.sortedBy {
@@ -51,4 +38,18 @@ class SourceFileMap {
sourceToIndex.clear()
indexToSource.clear()
}
}
class DeserializedSourceFile(
val name_: String, val index: Int, val library: KotlinLibrary
) : SourceFile {
override fun getName(): String? = name_
override fun equals(other: Any?): Boolean {
return other is DeserializedSourceFile && library == other.library && index == other.index
}
override fun hashCode(): Int {
return library.hashCode() xor index
}
}
@@ -19,6 +19,16 @@ import org.jetbrains.kotlin.serialization.deserialization.getClassId
import org.jetbrains.kotlin.serialization.deserialization.getName
import org.jetbrains.kotlin.storage.StorageManager
private val KotlinLibrary.fileSources: KlibMetadataFileRegistry get() {
val result = KlibMetadataFileRegistry()
val proto = parseModuleHeader(moduleHeaderData)
proto.fileList.forEachIndexed { index, it ->
result.provide(it.name, index, this)
}
return result
}
class KlibMetadataPackageFragment(
fqName: FqName,
private val library: KotlinLibrary,
@@ -28,6 +38,10 @@ class KlibMetadataPackageFragment(
private val partName: String
) : DeserializedPackageFragment(fqName, storageManager, module) {
val fileRegistry: KlibMetadataFileRegistry by lazy {
library.fileSources
}
lateinit var components: DeserializationComponents
override fun initialize(components: DeserializationComponents) {