Changes after code review

This commit is contained in:
Alexander Gorshenev
2019-03-14 20:17:23 +03:00
committed by alexander-gorshenev
parent f8be378a65
commit ee7660065b
18 changed files with 268 additions and 792 deletions
+1 -1
View File
@@ -732,4 +732,4 @@ allprojects {
repositories.redirect() repositories.redirect()
} }
} }
} }
+9 -14
View File
@@ -74,11 +74,6 @@ message IrDeclarationOrigin {
} }
} }
message Name {
required String name = 1;
required bool is_special = 2;
}
/* ------ Top Level---------------------------------------------- */ /* ------ Top Level---------------------------------------------- */
message IrDeclarationContainer { message IrDeclarationContainer {
@@ -99,7 +94,7 @@ message IrFile {
} }
message IrModule { message IrModule {
required Name name = 1; required String name = 1;
repeated IrFile file = 2; repeated IrFile file = 2;
required IrSymbolTable symbol_table = 3; required IrSymbolTable symbol_table = 3;
required IrTypeTable type_table = 4; required IrTypeTable type_table = 4;
@@ -526,7 +521,7 @@ message IrFunction {
} }
message IrFunctionBase { message IrFunctionBase {
required Name name = 1; required String name = 1;
required Visibility visibility = 2; required Visibility visibility = 2;
required bool is_inline = 3; required bool is_inline = 3;
required bool is_external = 4; required bool is_external = 4;
@@ -548,7 +543,7 @@ message IrConstructor {
message IrField { message IrField {
required IrSymbol symbol = 1; required IrSymbol symbol = 1;
optional IrExpression initializer = 2; optional IrExpression initializer = 2;
required Name name = 3; required String name = 3;
required Visibility visibility = 4; required Visibility visibility = 4;
required bool is_final = 5; required bool is_final = 5;
required bool is_external = 6; required bool is_external = 6;
@@ -558,7 +553,7 @@ message IrField {
message IrProperty { message IrProperty {
optional DescriptorReference descriptor_reference = 1; // IrProperty doesn't have a symbol at all. Preserve this rudiment for now. optional DescriptorReference descriptor_reference = 1; // IrProperty doesn't have a symbol at all. Preserve this rudiment for now.
required Name name = 2; required String name = 2;
required Visibility visibility = 3; required Visibility visibility = 3;
required ModalityKind modality = 4; required ModalityKind modality = 4;
required bool is_var = 5; required bool is_var = 5;
@@ -572,7 +567,7 @@ message IrProperty {
} }
message IrVariable { message IrVariable {
required Name name = 1; required String name = 1;
required IrSymbol symbol = 2; required IrSymbol symbol = 2;
required IrTypeIndex type = 3; required IrTypeIndex type = 3;
required bool is_var = 4; required bool is_var = 4;
@@ -599,7 +594,7 @@ enum ModalityKind { // It is ModalityKind to not clash with Modality in descript
message IrValueParameter { message IrValueParameter {
required IrSymbol symbol = 1; required IrSymbol symbol = 1;
required Name name = 2; required String name = 2;
required int32 index = 3; required int32 index = 3;
required IrTypeIndex type = 4; required IrTypeIndex type = 4;
optional IrTypeIndex vararg_element_type = 5; optional IrTypeIndex vararg_element_type = 5;
@@ -610,7 +605,7 @@ message IrValueParameter {
message IrTypeParameter { message IrTypeParameter {
required IrSymbol symbol = 1; required IrSymbol symbol = 1;
required Name name = 2; required String name = 2;
required int32 index = 3; required int32 index = 3;
required IrTypeVariance variance = 4; required IrTypeVariance variance = 4;
repeated IrTypeIndex super_type = 5; repeated IrTypeIndex super_type = 5;
@@ -623,7 +618,7 @@ message IrTypeParameterContainer {
message IrClass { message IrClass {
required IrSymbol symbol = 1; required IrSymbol symbol = 1;
required Name name = 2; required String name = 2;
required ClassKind kind = 3; required ClassKind kind = 3;
required Visibility visibility = 4; required Visibility visibility = 4;
required ModalityKind modality = 5; required ModalityKind modality = 5;
@@ -643,7 +638,7 @@ message IrEnumEntry {
required IrSymbol symbol = 1; required IrSymbol symbol = 1;
optional IrExpression initializer = 2; optional IrExpression initializer = 2;
optional IrDeclaration corresponding_class = 3; optional IrDeclaration corresponding_class = 3;
required Name name = 4; required String name = 4;
} }
message IrAnonymousInit { message IrAnonymousInit {
@@ -64,9 +64,7 @@ private const val INDEX_HEADER_SIZE = 4 // sizeof(Int).
class CombinedIrFileWriter(val declarationCount: Int) { class CombinedIrFileWriter(val declarationCount: Int) {
private var currentDeclaration = 0 private var currentDeclaration = 0
private var currentPosition = 0 private var currentPosition = 0
private val file = Files.createTempFile("ir", "").toFile().also { private val file = Files.createTempFile("ir", "").toFile()
it.deleteOnExit()
}
private val randomAccessFile = RandomAccessFile(file.path, "rw") private val randomAccessFile = RandomAccessFile(file.path, "rw")
init { init {
@@ -178,7 +178,7 @@ class NaiveSourceBasedFileEntryImpl(override val name: String, val lineStartOffs
override fun getColumnNumber(offset: Int): Int { override fun getColumnNumber(offset: Int): Int {
assert(offset != UNDEFINED_OFFSET) assert(offset != UNDEFINED_OFFSET)
if (offset == SYNTHETIC_OFFSET) return 0 if (offset == SYNTHETIC_OFFSET) return 0
var lineNumber = getLineNumber(offset) val lineNumber = getLineNumber(offset)
return offset - lineStartOffsets[lineNumber] return offset - lineStartOffsets[lineNumber]
} }
@@ -22,20 +22,17 @@ class DescriptorTable {
abstract class DeclarationTable(val builtIns: IrBuiltIns, val descriptorTable: DescriptorTable, mangler: KotlinMangler): KotlinMangler by mangler { abstract class DeclarationTable(val builtIns: IrBuiltIns, val descriptorTable: DescriptorTable, mangler: KotlinMangler): KotlinMangler by mangler {
private val table = mutableMapOf<IrDeclaration, UniqId>() private val table = mutableMapOf<IrDeclaration, UniqId>()
val debugIndex = mutableMapOf<UniqId, String>()
val descriptors = descriptorTable val descriptors = descriptorTable
abstract protected var currentIndex: Long protected abstract var currentIndex: Long
protected var initialized: Boolean = false
open fun loadKnownBuiltins() { open fun loadKnownBuiltins(): Long {
builtIns.knownBuiltins.forEach { builtIns.knownBuiltins.forEach {
table.put(it, UniqId(currentIndex++, false)) table.put(it, UniqId(currentIndex++, false))
} }
initialized = true return currentIndex
} }
fun uniqIdByDeclaration(value: IrDeclaration) = table.getOrPut(value) { fun uniqIdByDeclaration(value: IrDeclaration) = table.getOrPut(value) {
if (!initialized) error("DeclarationTable has not been initialized")
computeUniqIdByDeclaration(value) computeUniqIdByDeclaration(value)
} }
@@ -28,7 +28,7 @@ abstract class DescriptorReferenceDeserializer(
protected fun getContributedDescriptors(packageFqNameString: String, name: String): Collection<DeclarationDescriptor> { protected fun getContributedDescriptors(packageFqNameString: String, name: String): Collection<DeclarationDescriptor> {
val packageFqName = packageFqNameString.let { val packageFqName = packageFqNameString.let {
if (it == "<root>") FqName.ROOT else FqName(it) if (it == "<root>") FqName.ROOT else FqName(it)
}// TODO: whould we store an empty string in the protobuf? }// TODO: would we store an empty string in the protobuf?
val contributedName = if (name.startsWith("<get-") || name.startsWith("<set-")) { val contributedName = if (name.startsWith("<get-") || name.startsWith("<set-")) {
name.substring(5, name.length - 1) // FIXME: rework serialization format. name.substring(5, name.length - 1) // FIXME: rework serialization format.
@@ -31,9 +31,6 @@ import org.jetbrains.kotlin.backend.common.serialization.KotlinIr.IrDeclarator.D
import org.jetbrains.kotlin.backend.common.serialization.KotlinIr.IrType.KindCase.* import org.jetbrains.kotlin.backend.common.serialization.KotlinIr.IrType.KindCase.*
import org.jetbrains.kotlin.backend.common.serialization.KotlinIr.IrVarargElement.VarargElementCase import org.jetbrains.kotlin.backend.common.serialization.KotlinIr.IrVarargElement.VarargElementCase
import org.jetbrains.kotlin.backend.common.serialization.KotlinIr.IrTypeArgument.KindCase.* import org.jetbrains.kotlin.backend.common.serialization.KotlinIr.IrTypeArgument.KindCase.*
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
@@ -55,9 +52,9 @@ abstract class IrModuleDeserializer(
abstract fun deserializeString(proto: KotlinIr.String): String abstract fun deserializeString(proto: KotlinIr.String): String
abstract fun deserializeLoopHeader(loopIndex: Int, loopBuilder: () -> IrLoopBase): IrLoopBase abstract fun deserializeLoopHeader(loopIndex: Int, loopBuilder: () -> IrLoopBase): IrLoopBase
private fun deserializeName(proto: KotlinIr.Name): Name { private fun deserializeName(proto: KotlinIr.String): Name {
val name = deserializeString(proto.name) val name = deserializeString(proto)
return if (proto.isSpecial) Name.special(name) else Name.identifier(name) return Name.guessByFirstCharacter(name)
} }
private fun deserializeTypeArguments(proto: KotlinIr.TypeArguments): List<IrType> { private fun deserializeTypeArguments(proto: KotlinIr.TypeArguments): List<IrType> {
@@ -88,12 +88,7 @@ open class IrModuleSerializer(
return proto.build() return proto.build()
} }
fun serializeName(name: Name): KotlinIr.Name { fun serializeName(name: Name): KotlinIr.String = serializeString(name.toString())
val proto = KotlinIr.Name.newBuilder()
.setName(serializeString(name.toString()))
.setIsSpecial(name.isSpecial)
return proto.build()
}
/* ------- IrSymbols -------------------------------------------------------- */ /* ------- IrSymbols -------------------------------------------------------- */
@@ -27,11 +27,11 @@ import org.jetbrains.kotlin.serialization.deserialization.descriptors.Deserializ
import java.io.File import java.io.File
abstract class KotlinIrLinker( abstract class KotlinIrLinker(
currentModule: ModuleDescriptor,
logger: LoggingContext, logger: LoggingContext,
builtIns: IrBuiltIns, builtIns: IrBuiltIns,
symbolTable: SymbolTable, symbolTable: SymbolTable,
private val forwardModuleDescriptor: ModuleDescriptor?) private val forwardModuleDescriptor: ModuleDescriptor?,
val firstKnownBuiltinsIndex: Long)
: IrModuleDeserializer(logger, builtIns, symbolTable), : IrModuleDeserializer(logger, builtIns, symbolTable),
DescriptorUniqIdAware { DescriptorUniqIdAware {
@@ -50,9 +50,18 @@ abstract class KotlinIrLinker(
abstract protected val descriptorReferenceDeserializer: DescriptorReferenceDeserializer abstract protected val descriptorReferenceDeserializer: DescriptorReferenceDeserializer
private val descriptorToDirectoryMap = mutableMapOf<ModuleDescriptor, File>() protected val indexAfterKnownBuiltins = loadKnownBuiltinSymbols()
private fun irDirectory(m: ModuleDescriptor): File = descriptorToDirectoryMap[m]!! fun loadKnownBuiltinSymbols(): Long {
var currentIndex = firstKnownBuiltinsIndex
builtIns.knownBuiltins.forEach {
require(it is IrFunction)
deserializedSymbols.put(UniqIdKey(null, UniqId(currentIndex, isLocal = false)), it.symbol)
assert(symbolTable.referenceSimpleFunction(it.descriptor) == it.symbol)
currentIndex++
}
return currentIndex
}
private fun referenceDeserializedSymbol(proto: KotlinIr.IrSymbolData, descriptor: DeclarationDescriptor?): IrSymbol = when (proto.kind) { private fun referenceDeserializedSymbol(proto: KotlinIr.IrSymbolData, descriptor: DeclarationDescriptor?): IrSymbol = when (proto.kind) {
KotlinIr.IrSymbolKind.ANONYMOUS_INIT_SYMBOL -> KotlinIr.IrSymbolKind.ANONYMOUS_INIT_SYMBOL ->
@@ -9,7 +9,7 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.ir.backend.common.serialization.* import org.jetbrains.kotlin.backend.common.serialization.*
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
@@ -21,7 +21,7 @@ internal val DeclarationDescriptor.isExpectMember: Boolean
internal val DeclarationDescriptor.isSerializableExpectClass: Boolean internal val DeclarationDescriptor.isSerializableExpectClass: Boolean
get() = this is ClassDescriptor && ExpectedActualDeclarationChecker.shouldGenerateExpectClass(this) get() = this is ClassDescriptor && ExpectedActualDeclarationChecker.shouldGenerateExpectClass(this)
tailrec internal fun DeclarationDescriptor.findPackage(): PackageFragmentDescriptor { internal tailrec fun DeclarationDescriptor.findPackage(): PackageFragmentDescriptor {
return if (this is PackageFragmentDescriptor) this return if (this is PackageFragmentDescriptor) this
else this.containingDeclaration!!.findPackage() else this.containingDeclaration!!.findPackage()
} }
@@ -1,10 +0,0 @@
/*
* Copyright 2010-2019 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.ir.backend.common.serialization
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
@@ -43,7 +43,7 @@ import org.jetbrains.kotlin.resolve.CompilerDeserializationConfiguration
import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.utils.DFS import org.jetbrains.kotlin.utils.DFS
import java.io.File import java.io.File
import java.nio.file.Files import java.nio.file.Files.move
import java.nio.file.Paths import java.nio.file.Paths
import java.nio.file.StandardCopyOption import java.nio.file.StandardCopyOption
@@ -328,7 +328,7 @@ fun serializeModuleIntoKlib(
dependencies: List<CompiledModule>, dependencies: List<CompiledModule>,
moduleFragment: IrModuleFragment moduleFragment: IrModuleFragment
) { ) {
val declarationTable = JsDeclarationTable(moduleFragment.irBuiltins, DescriptorTable()).apply { loadKnownBuiltins() } val declarationTable = JsDeclarationTable(moduleFragment.irBuiltins, DescriptorTable())
val serializedIr = JsIrModuleSerializer(logggg, declarationTable).serializedIrModule(moduleFragment) val serializedIr = JsIrModuleSerializer(logggg, declarationTable).serializedIrModule(moduleFragment)
val serializer = JsKlibMetadataSerializationUtil val serializer = JsKlibMetadataSerializationUtil
@@ -355,7 +355,7 @@ fun serializeModuleIntoKlib(
val irDeclarationDir = File(klibDir, declarationsDirName).also { it.mkdir() } val irDeclarationDir = File(klibDir, declarationsDirName).also { it.mkdir() }
val irCombinedFile = File(irDeclarationDir, "irCombined.knd") val irCombinedFile = File(irDeclarationDir, "irCombined.knd")
Files.copy(Paths.get(serializedIr.combinedDeclarationFilePath), Paths.get(irCombinedFile.path), StandardCopyOption.REPLACE_EXISTING) move(Paths.get(serializedIr.combinedDeclarationFilePath), Paths.get(irCombinedFile.path), StandardCopyOption.REPLACE_EXISTING)
File(klibDir, "${moduleDescription.name}.${JsKlibMetadataSerializationUtil.CLASS_METADATA_FILE_EXTENSION}").also { File(klibDir, "${moduleDescription.name}.${JsKlibMetadataSerializationUtil.CLASS_METADATA_FILE_EXTENSION}").also {
it.writeBytes(serializedData.asByteArray()) it.writeBytes(serializedData.asByteArray())
@@ -11,11 +11,9 @@ class JsDeclarationTable(builtIns: IrBuiltIns, descriptorTable: DescriptorTable)
override var currentIndex = 0x1_0000_0000L override var currentIndex = 0x1_0000_0000L
private /* lateinit */ var FUNCTION_INDEX_START: Long = 0xdeadbeefL private val FUNCTION_INDEX_START: Long = loadKnownBuiltins()
override fun loadKnownBuiltins() { init {
super.loadKnownBuiltins()
FUNCTION_INDEX_START = currentIndex
currentIndex += BUILT_IN_UNIQ_ID_GAP currentIndex += BUILT_IN_UNIQ_ID_GAP
} }
@@ -18,25 +18,13 @@ class JsIrLinker(
logger: LoggingContext, logger: LoggingContext,
builtIns: IrBuiltIns, builtIns: IrBuiltIns,
symbolTable: SymbolTable) : symbolTable: SymbolTable) :
KotlinIrLinker(currentModule, logger, builtIns, symbolTable, null), KotlinIrLinker(logger, builtIns, symbolTable, null, 0x1_0000_0000L),
DescriptorUniqIdAware by JsDescriptorUniqIdAware { DescriptorUniqIdAware by JsDescriptorUniqIdAware {
private val FUNCTION_INDEX_START: Long private val FUNCTION_INDEX_START: Long = indexAfterKnownBuiltins
val moduleToReaderMap = mutableMapOf<ModuleDescriptor, CombinedIrFileReader>() val moduleToReaderMap = mutableMapOf<ModuleDescriptor, CombinedIrFileReader>()
init {
// TODO: think about order
var currentIndex = 0x1_0000_0000L
builtIns.knownBuiltins.forEach {
require(it is IrFunction)
deserializedSymbols.put(UniqIdKey(null, UniqId(currentIndex, isLocal = false)), it.symbol)
assert(symbolTable.referenceSimpleFunction(it.descriptor) == it.symbol)
currentIndex++
}
FUNCTION_INDEX_START = currentIndex
}
override fun getPrimitiveTypeOrNull(symbol: IrClassifierSymbol, hasQuestionMark: Boolean) = override fun getPrimitiveTypeOrNull(symbol: IrClassifierSymbol, hasQuestionMark: Boolean) =
builtIns.getPrimitiveTypeOrNullByDescriptor(symbol.descriptor, hasQuestionMark) builtIns.getPrimitiveTypeOrNullByDescriptor(symbol.descriptor, hasQuestionMark)
@@ -58,7 +58,6 @@ val PROTO_PATHS: List<ProtoPath> = listOf(
ProtoPath("core/metadata.jvm/src/jvm_metadata.proto"), ProtoPath("core/metadata.jvm/src/jvm_metadata.proto"),
ProtoPath("core/metadata.jvm/src/jvm_module.proto"), ProtoPath("core/metadata.jvm/src/jvm_module.proto"),
ProtoPath("build-common/src/java_descriptors.proto"), ProtoPath("build-common/src/java_descriptors.proto"),
//ProtoPath("compiler/ir/backend.js/src/ir.proto", false),
ProtoPath("compiler/ir/backend.js/src/js.proto", false), ProtoPath("compiler/ir/backend.js/src/js.proto", false),
ProtoPath("compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/serialization/KotlinIr.proto", false) ProtoPath("compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/serialization/KotlinIr.proto", false)
) )