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

View File

@@ -732,4 +732,4 @@ allprojects {
repositories.redirect()
}
}
}
}

View File

@@ -74,11 +74,6 @@ message IrDeclarationOrigin {
}
}
message Name {
required String name = 1;
required bool is_special = 2;
}
/* ------ Top Level---------------------------------------------- */
message IrDeclarationContainer {
@@ -99,7 +94,7 @@ message IrFile {
}
message IrModule {
required Name name = 1;
required String name = 1;
repeated IrFile file = 2;
required IrSymbolTable symbol_table = 3;
required IrTypeTable type_table = 4;
@@ -526,7 +521,7 @@ message IrFunction {
}
message IrFunctionBase {
required Name name = 1;
required String name = 1;
required Visibility visibility = 2;
required bool is_inline = 3;
required bool is_external = 4;
@@ -548,7 +543,7 @@ message IrConstructor {
message IrField {
required IrSymbol symbol = 1;
optional IrExpression initializer = 2;
required Name name = 3;
required String name = 3;
required Visibility visibility = 4;
required bool is_final = 5;
required bool is_external = 6;
@@ -558,7 +553,7 @@ message IrField {
message IrProperty {
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 ModalityKind modality = 4;
required bool is_var = 5;
@@ -572,7 +567,7 @@ message IrProperty {
}
message IrVariable {
required Name name = 1;
required String name = 1;
required IrSymbol symbol = 2;
required IrTypeIndex type = 3;
required bool is_var = 4;
@@ -599,7 +594,7 @@ enum ModalityKind { // It is ModalityKind to not clash with Modality in descript
message IrValueParameter {
required IrSymbol symbol = 1;
required Name name = 2;
required String name = 2;
required int32 index = 3;
required IrTypeIndex type = 4;
optional IrTypeIndex vararg_element_type = 5;
@@ -610,7 +605,7 @@ message IrValueParameter {
message IrTypeParameter {
required IrSymbol symbol = 1;
required Name name = 2;
required String name = 2;
required int32 index = 3;
required IrTypeVariance variance = 4;
repeated IrTypeIndex super_type = 5;
@@ -623,7 +618,7 @@ message IrTypeParameterContainer {
message IrClass {
required IrSymbol symbol = 1;
required Name name = 2;
required String name = 2;
required ClassKind kind = 3;
required Visibility visibility = 4;
required ModalityKind modality = 5;
@@ -643,7 +638,7 @@ message IrEnumEntry {
required IrSymbol symbol = 1;
optional IrExpression initializer = 2;
optional IrDeclaration corresponding_class = 3;
required Name name = 4;
required String name = 4;
}
message IrAnonymousInit {

View File

@@ -64,9 +64,7 @@ private const val INDEX_HEADER_SIZE = 4 // sizeof(Int).
class CombinedIrFileWriter(val declarationCount: Int) {
private var currentDeclaration = 0
private var currentPosition = 0
private val file = Files.createTempFile("ir", "").toFile().also {
it.deleteOnExit()
}
private val file = Files.createTempFile("ir", "").toFile()
private val randomAccessFile = RandomAccessFile(file.path, "rw")
init {

View File

@@ -178,7 +178,7 @@ class NaiveSourceBasedFileEntryImpl(override val name: String, val lineStartOffs
override fun getColumnNumber(offset: Int): Int {
assert(offset != UNDEFINED_OFFSET)
if (offset == SYNTHETIC_OFFSET) return 0
var lineNumber = getLineNumber(offset)
val lineNumber = getLineNumber(offset)
return offset - lineStartOffsets[lineNumber]
}

View File

@@ -22,20 +22,17 @@ class DescriptorTable {
abstract class DeclarationTable(val builtIns: IrBuiltIns, val descriptorTable: DescriptorTable, mangler: KotlinMangler): KotlinMangler by mangler {
private val table = mutableMapOf<IrDeclaration, UniqId>()
val debugIndex = mutableMapOf<UniqId, String>()
val descriptors = descriptorTable
abstract protected var currentIndex: Long
protected var initialized: Boolean = false
protected abstract var currentIndex: Long
open fun loadKnownBuiltins() {
open fun loadKnownBuiltins(): Long {
builtIns.knownBuiltins.forEach {
table.put(it, UniqId(currentIndex++, false))
}
initialized = true
return currentIndex
}
fun uniqIdByDeclaration(value: IrDeclaration) = table.getOrPut(value) {
if (!initialized) error("DeclarationTable has not been initialized")
computeUniqIdByDeclaration(value)
}

View File

@@ -28,7 +28,7 @@ abstract class DescriptorReferenceDeserializer(
protected fun getContributedDescriptors(packageFqNameString: String, name: String): Collection<DeclarationDescriptor> {
val packageFqName = packageFqNameString.let {
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-")) {
name.substring(5, name.length - 1) // FIXME: rework serialization format.

View File

@@ -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.IrVarargElement.VarargElementCase
import org.jetbrains.kotlin.backend.common.serialization.KotlinIr.IrTypeArgument.KindCase.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
@@ -55,9 +52,9 @@ abstract class IrModuleDeserializer(
abstract fun deserializeString(proto: KotlinIr.String): String
abstract fun deserializeLoopHeader(loopIndex: Int, loopBuilder: () -> IrLoopBase): IrLoopBase
private fun deserializeName(proto: KotlinIr.Name): Name {
val name = deserializeString(proto.name)
return if (proto.isSpecial) Name.special(name) else Name.identifier(name)
private fun deserializeName(proto: KotlinIr.String): Name {
val name = deserializeString(proto)
return Name.guessByFirstCharacter(name)
}
private fun deserializeTypeArguments(proto: KotlinIr.TypeArguments): List<IrType> {

View File

@@ -88,12 +88,7 @@ open class IrModuleSerializer(
return proto.build()
}
fun serializeName(name: Name): KotlinIr.Name {
val proto = KotlinIr.Name.newBuilder()
.setName(serializeString(name.toString()))
.setIsSpecial(name.isSpecial)
return proto.build()
}
fun serializeName(name: Name): KotlinIr.String = serializeString(name.toString())
/* ------- IrSymbols -------------------------------------------------------- */

View File

@@ -27,11 +27,11 @@ import org.jetbrains.kotlin.serialization.deserialization.descriptors.Deserializ
import java.io.File
abstract class KotlinIrLinker(
currentModule: ModuleDescriptor,
logger: LoggingContext,
builtIns: IrBuiltIns,
symbolTable: SymbolTable,
private val forwardModuleDescriptor: ModuleDescriptor?)
private val forwardModuleDescriptor: ModuleDescriptor?,
val firstKnownBuiltinsIndex: Long)
: IrModuleDeserializer(logger, builtIns, symbolTable),
DescriptorUniqIdAware {
@@ -50,9 +50,18 @@ abstract class KotlinIrLinker(
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) {
KotlinIr.IrSymbolKind.ANONYMOUS_INIT_SYMBOL ->

View File

@@ -9,7 +9,7 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor
import org.jetbrains.kotlin.descriptors.Visibilities
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.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.*

View File

@@ -21,7 +21,7 @@ internal val DeclarationDescriptor.isExpectMember: Boolean
internal val DeclarationDescriptor.isSerializableExpectClass: Boolean
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
else this.containingDeclaration!!.findPackage()
}

View File

@@ -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

View File

@@ -43,7 +43,7 @@ import org.jetbrains.kotlin.resolve.CompilerDeserializationConfiguration
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.utils.DFS
import java.io.File
import java.nio.file.Files
import java.nio.file.Files.move
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
@@ -328,7 +328,7 @@ fun serializeModuleIntoKlib(
dependencies: List<CompiledModule>,
moduleFragment: IrModuleFragment
) {
val declarationTable = JsDeclarationTable(moduleFragment.irBuiltins, DescriptorTable()).apply { loadKnownBuiltins() }
val declarationTable = JsDeclarationTable(moduleFragment.irBuiltins, DescriptorTable())
val serializedIr = JsIrModuleSerializer(logggg, declarationTable).serializedIrModule(moduleFragment)
val serializer = JsKlibMetadataSerializationUtil
@@ -355,7 +355,7 @@ fun serializeModuleIntoKlib(
val irDeclarationDir = File(klibDir, declarationsDirName).also { it.mkdir() }
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 {
it.writeBytes(serializedData.asByteArray())

View File

@@ -11,11 +11,9 @@ class JsDeclarationTable(builtIns: IrBuiltIns, descriptorTable: DescriptorTable)
override var currentIndex = 0x1_0000_0000L
private /* lateinit */ var FUNCTION_INDEX_START: Long = 0xdeadbeefL
private val FUNCTION_INDEX_START: Long = loadKnownBuiltins()
override fun loadKnownBuiltins() {
super.loadKnownBuiltins()
FUNCTION_INDEX_START = currentIndex
init {
currentIndex += BUILT_IN_UNIQ_ID_GAP
}

View File

@@ -18,25 +18,13 @@ class JsIrLinker(
logger: LoggingContext,
builtIns: IrBuiltIns,
symbolTable: SymbolTable) :
KotlinIrLinker(currentModule, logger, builtIns, symbolTable, null),
KotlinIrLinker(logger, builtIns, symbolTable, null, 0x1_0000_0000L),
DescriptorUniqIdAware by JsDescriptorUniqIdAware {
private val FUNCTION_INDEX_START: Long
private val FUNCTION_INDEX_START: Long = indexAfterKnownBuiltins
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) =
builtIns.getPrimitiveTypeOrNullByDescriptor(symbol.descriptor, hasQuestionMark)

View File

@@ -58,7 +58,6 @@ val PROTO_PATHS: List<ProtoPath> = listOf(
ProtoPath("core/metadata.jvm/src/jvm_metadata.proto"),
ProtoPath("core/metadata.jvm/src/jvm_module.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.common/src/org/jetbrains/kotlin/backend/common/serialization/KotlinIr.proto", false)
)