Move some serialization helpers to metadata/metadata.jvm

This commit is contained in:
Alexander Udalov
2018-03-09 17:34:02 +01:00
parent 779290be0c
commit 0a78fe8ae3
12 changed files with 44 additions and 78 deletions
@@ -1,37 +1,15 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-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/LICENSE.txt file.
*/
package org.jetbrains.kotlin.serialization
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.metadata.serialization.StringTable
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.ErrorUtils
import java.io.OutputStream
interface StringTable {
fun getStringIndex(string: String): Int
/**
* @param className the fully qualified name of some class in the format: `org/foo/bar/Test.Inner`
*/
fun getQualifiedClassNameIndex(className: String, isLocal: Boolean): Int
fun serializeTo(output: OutputStream)
}
interface DescriptorAwareStringTable : StringTable {
fun getFqNameIndex(descriptor: ClassifierDescriptorWithTypeParameters): Int {
@@ -13,6 +13,10 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotated
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.Flags
import org.jetbrains.kotlin.metadata.serialization.Interner
import org.jetbrains.kotlin.metadata.serialization.MutableTypeTable
import org.jetbrains.kotlin.metadata.serialization.MutableVersionRequirementTable
import org.jetbrains.kotlin.metadata.serialization.StringTable
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.protobuf.MessageLite
@@ -30,7 +34,6 @@ import org.jetbrains.kotlin.serialization.deserialization.ProtoEnumFlags
import org.jetbrains.kotlin.serialization.deserialization.descriptors.VersionRequirement
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.contains
import org.jetbrains.kotlin.utils.Interner
import java.io.ByteArrayOutputStream
import java.util.*
@@ -1,73 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("FINITE_BOUNDS_VIOLATION_IN_JAVA")
package org.jetbrains.kotlin.serialization
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.protobuf.GeneratedMessageLite
import org.jetbrains.kotlin.utils.Interner
import java.util.*
private class TableElementWrapper<Element : GeneratedMessageLite.Builder<*, Element>>(val builder: Element) {
// If you'll try to optimize it using structured equals/hashCode, pay attention to extensions present in proto messages
private val bytes: ByteArray = builder.build().toByteArray()
private val hashCode: Int = Arrays.hashCode(bytes)
override fun hashCode() = hashCode
override fun equals(other: Any?) = other is TableElementWrapper<*> && Arrays.equals(bytes, other.bytes)
}
abstract class MutableTable<Element, Table, TableBuilder>
where Element : GeneratedMessageLite.Builder<*, Element>,
Table : GeneratedMessageLite,
TableBuilder : GeneratedMessageLite.Builder<Table, TableBuilder> {
private val interner = Interner<TableElementWrapper<Element>>()
protected abstract fun createTableBuilder(): TableBuilder
protected abstract fun addElement(builder: TableBuilder, element: Element)
operator fun get(type: Element): Int =
interner.intern(TableElementWrapper(type))
@Suppress("UNCHECKED_CAST")
fun serialize(): Table? =
if (interner.isEmpty) null
else createTableBuilder().apply {
for (obj in interner.allInternedObjects) {
addElement(this, obj.builder)
}
}.build() as Table
}
class MutableTypeTable : MutableTable<ProtoBuf.Type.Builder, ProtoBuf.TypeTable, ProtoBuf.TypeTable.Builder>() {
override fun createTableBuilder(): ProtoBuf.TypeTable.Builder = ProtoBuf.TypeTable.newBuilder()
override fun addElement(builder: ProtoBuf.TypeTable.Builder, element: ProtoBuf.Type.Builder) {
builder.addType(element)
}
}
class MutableVersionRequirementTable : MutableTable<ProtoBuf.VersionRequirement.Builder, ProtoBuf.VersionRequirementTable, ProtoBuf.VersionRequirementTable.Builder>() {
override fun createTableBuilder(): ProtoBuf.VersionRequirementTable.Builder = ProtoBuf.VersionRequirementTable.newBuilder()
override fun addElement(builder: ProtoBuf.VersionRequirementTable.Builder, element: ProtoBuf.VersionRequirement.Builder) {
builder.addRequirement(element)
}
}
@@ -18,9 +18,9 @@ package org.jetbrains.kotlin.serialization
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.ProtoBuf.QualifiedNameTable.QualifiedName
import org.jetbrains.kotlin.metadata.serialization.Interner
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.utils.Interner
import java.io.OutputStream
open class StringTableImpl : DescriptorAwareStringTable {