Move some serialization helpers to metadata/metadata.jvm
This commit is contained in:
@@ -18,7 +18,6 @@ import org.jetbrains.kotlin.codegen.binding.CalculatedClosure;
|
||||
import org.jetbrains.kotlin.codegen.context.CodegenContext;
|
||||
import org.jetbrains.kotlin.codegen.intrinsics.HashCode;
|
||||
import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicMethods;
|
||||
import org.jetbrains.kotlin.codegen.serialization.JvmStringTable;
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState;
|
||||
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
|
||||
import org.jetbrains.kotlin.config.JvmTarget;
|
||||
@@ -27,6 +26,7 @@ import org.jetbrains.kotlin.lexer.KtTokens;
|
||||
import org.jetbrains.kotlin.load.java.JavaVisibilities;
|
||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames;
|
||||
import org.jetbrains.kotlin.metadata.jvm.deserialization.BitEncoding;
|
||||
import org.jetbrains.kotlin.metadata.jvm.serialization.JvmStringTable;
|
||||
import org.jetbrains.kotlin.name.ClassId;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.protobuf.MessageLite;
|
||||
|
||||
+1
-1
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.codegen.ClassBuilder
|
||||
import org.jetbrains.kotlin.codegen.StackValue
|
||||
import org.jetbrains.kotlin.codegen.coroutines.COROUTINE_IMPL_ASM_TYPE
|
||||
import org.jetbrains.kotlin.codegen.serialization.JvmCodegenStringTable
|
||||
import org.jetbrains.kotlin.codegen.serialization.JvmStringTable
|
||||
import org.jetbrains.kotlin.codegen.writeKotlinMetadata
|
||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
|
||||
import org.jetbrains.kotlin.load.kotlin.FileBasedKotlinClass
|
||||
@@ -30,6 +29,7 @@ import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||
import org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor
|
||||
import org.jetbrains.kotlin.metadata.jvm.JvmProtoBuf
|
||||
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
|
||||
import org.jetbrains.kotlin.metadata.jvm.serialization.JvmStringTable
|
||||
import org.jetbrains.kotlin.protobuf.MessageLite
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin.Companion.NO_ORIGIN
|
||||
|
||||
+1
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper
|
||||
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmNameResolver
|
||||
import org.jetbrains.kotlin.metadata.jvm.serialization.JvmStringTable
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.serialization.DescriptorAwareStringTable
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codegen.serialization
|
||||
|
||||
import org.jetbrains.kotlin.metadata.jvm.JvmProtoBuf
|
||||
import org.jetbrains.kotlin.metadata.jvm.JvmProtoBuf.StringTableTypes.Record
|
||||
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmNameResolver
|
||||
import org.jetbrains.kotlin.serialization.StringTable
|
||||
import java.io.OutputStream
|
||||
|
||||
// TODO: optimize by reordering records to minimize storage of 'range' fields
|
||||
open class JvmStringTable(nameResolver: JvmNameResolver? = null) : StringTable {
|
||||
val strings = ArrayList<String>()
|
||||
private val records = ArrayList<Record.Builder>()
|
||||
private val map = HashMap<String, Int>()
|
||||
private val localNames = LinkedHashSet<Int>()
|
||||
|
||||
init {
|
||||
if (nameResolver != null) {
|
||||
strings.addAll(nameResolver.strings)
|
||||
nameResolver.records.mapTo(records, JvmProtoBuf.StringTableTypes.Record::toBuilder)
|
||||
for (index in strings.indices) {
|
||||
map[nameResolver.getString(index)] = index
|
||||
}
|
||||
localNames.addAll(nameResolver.types.localNameList)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getStringIndex(string: String): Int =
|
||||
map.getOrPut(string) {
|
||||
strings.size.apply {
|
||||
strings.add(string)
|
||||
|
||||
val lastRecord = records.lastOrNull()
|
||||
if (lastRecord != null && lastRecord.isTrivial()) {
|
||||
lastRecord.range = lastRecord.range + 1
|
||||
}
|
||||
else records.add(Record.newBuilder())
|
||||
}
|
||||
}
|
||||
|
||||
private fun Record.Builder.isTrivial(): Boolean {
|
||||
return !hasPredefinedIndex() && !hasOperation() && substringIndexCount == 0 && replaceCharCount == 0
|
||||
}
|
||||
|
||||
// We use the following format to encode ClassId: "pkg/Outer.Inner".
|
||||
// It represents a unique name, but such names don't usually appear in the constant pool, so we're writing "Lpkg/Outer$Inner;"
|
||||
// instead and an instruction to drop the first and the last character in this string and replace all '$' with '.'.
|
||||
// This works most of the time, except in two rare cases:
|
||||
// - the name of the class or any of its outer classes contains dollars. In this case we're just storing the described
|
||||
// string literally: "pkg/Outer.Inner$with$dollars"
|
||||
// - the class is local or nested in local. In this case we're also storing the literal string, and also storing the fact that
|
||||
// this name represents a local class in a separate list
|
||||
override fun getQualifiedClassNameIndex(className: String, isLocal: Boolean): Int {
|
||||
map[className]?.let { recordedIndex ->
|
||||
// If we already recorded such string, we only return its index if it's local and our name is local
|
||||
// OR it's not local and our name is not local as well
|
||||
if (isLocal == (recordedIndex in localNames)) {
|
||||
return recordedIndex
|
||||
}
|
||||
}
|
||||
|
||||
val index = strings.size
|
||||
if (isLocal) {
|
||||
localNames.add(index)
|
||||
}
|
||||
|
||||
val record = Record.newBuilder()
|
||||
|
||||
// If the class is local or any of its outer class names contains '$', store a literal string
|
||||
if (isLocal || '$' in className) {
|
||||
strings.add(className)
|
||||
}
|
||||
else {
|
||||
val predefinedIndex = JvmNameResolver.getPredefinedStringIndex(className)
|
||||
if (predefinedIndex != null) {
|
||||
record.predefinedIndex = predefinedIndex
|
||||
// TODO: move all records with predefined names to the end and do not write associated strings for them (since they are ignored)
|
||||
strings.add("")
|
||||
}
|
||||
else {
|
||||
record.operation = Record.Operation.DESC_TO_CLASS_ID
|
||||
strings.add("L${className.replace('.', '$')};")
|
||||
}
|
||||
}
|
||||
|
||||
records.add(record)
|
||||
|
||||
map[className] = index
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
override fun serializeTo(output: OutputStream) {
|
||||
with(JvmProtoBuf.StringTableTypes.newBuilder()) {
|
||||
addAllRecord(records.map { it.build() })
|
||||
addAllLocalName(localNames)
|
||||
build().writeDelimitedTo(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-25
@@ -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 {
|
||||
+4
-1
@@ -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 {
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.utils
|
||||
|
||||
class Interner<T>(private val parent: Interner<T>? = null) {
|
||||
private val firstIndex: Int = parent?.run { interned.size + firstIndex } ?: 0
|
||||
private val interned = hashMapOf<T, Int>()
|
||||
|
||||
val allInternedObjects: List<T>
|
||||
get() = interned.keys.sortedBy(interned::get)
|
||||
|
||||
val isEmpty: Boolean
|
||||
get() = interned.isEmpty() && parent?.isEmpty != false
|
||||
|
||||
private fun find(obj: T): Int? {
|
||||
assert(parent == null || parent.interned.size + parent.firstIndex == firstIndex) {
|
||||
"Parent changed in parallel with child: indexes will be wrong"
|
||||
}
|
||||
return parent?.find(obj) ?: interned[obj]
|
||||
}
|
||||
|
||||
fun intern(obj: T): Int =
|
||||
find(obj) ?: (firstIndex + interned.size).also {
|
||||
interned[obj] = it
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user