From 7d8c86caf11b2274644fb8e5b51bcca22069ec79 Mon Sep 17 00:00:00 2001 From: Roman Artemev Date: Fri, 20 Aug 2021 16:05:25 +0300 Subject: [PATCH] [KLIB] Add API to access file's part of data as ByteArray It requires for computing fingerprint of IrFile in IC infra --- .../IncrementalCompilationSupport.kt | 10 ++++ .../jetbrains/kotlin/library/KotlinLibrary.kt | 6 +++ .../kotlin/library/impl/IrFileReaders.kt | 16 +++++-- .../kotlin/library/impl/KotlinLibraryImpl.kt | 48 ++++++++++++++++++- 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IncrementalCompilationSupport.kt b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IncrementalCompilationSupport.kt index 96362b505a0..d3a7c7af75f 100644 --- a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IncrementalCompilationSupport.kt +++ b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IncrementalCompilationSupport.kt @@ -84,6 +84,16 @@ class ICKotlinLibrary(private val icData: List) : IrLibrary { override fun file(index: Int): ByteArray = icData[index].fileData override fun fileCount(): Int = icData.size + + override fun types(fileIndex: Int): ByteArray = icData[fileIndex].types + + override fun signatures(fileIndex: Int): ByteArray = icData[fileIndex].signatures + + override fun strings(fileIndex: Int): ByteArray = icData[fileIndex].strings + + override fun declarations(fileIndex: Int): ByteArray = icData[fileIndex].declarations + + override fun bodies(fileIndex: Int): ByteArray = icData[fileIndex].bodies } class CurrentModuleWithICDeserializer( diff --git a/compiler/util-klib/src/org/jetbrains/kotlin/library/KotlinLibrary.kt b/compiler/util-klib/src/org/jetbrains/kotlin/library/KotlinLibrary.kt index 5955867db9c..130eaed0b67 100644 --- a/compiler/util-klib/src/org/jetbrains/kotlin/library/KotlinLibrary.kt +++ b/compiler/util-klib/src/org/jetbrains/kotlin/library/KotlinLibrary.kt @@ -67,6 +67,12 @@ interface IrLibrary { fun debugInfo(index: Int, fileIndex: Int): ByteArray? fun file(index: Int): ByteArray fun fileCount(): Int + + fun types(fileIndex: Int): ByteArray + fun signatures(fileIndex: Int): ByteArray + fun strings(fileIndex: Int): ByteArray + fun declarations(fileIndex: Int): ByteArray + fun bodies(fileIndex: Int): ByteArray } val BaseKotlinLibrary.uniqueName: String diff --git a/compiler/util-klib/src/org/jetbrains/kotlin/library/impl/IrFileReaders.kt b/compiler/util-klib/src/org/jetbrains/kotlin/library/impl/IrFileReaders.kt index 50b71be669c..f22ef9c503a 100644 --- a/compiler/util-klib/src/org/jetbrains/kotlin/library/impl/IrFileReaders.kt +++ b/compiler/util-klib/src/org/jetbrains/kotlin/library/impl/IrFileReaders.kt @@ -99,12 +99,12 @@ abstract class IrMultiArrayReader(private val buffer: ReadBuffer) { fun tableItemBytes(row: Int, column: Int): ByteArray { val rowOffset = indexToOffset[row] - val collumnOffsets = indexIndexToOffset.getOrPut(row) { + val columnOffsets = indexIndexToOffset.getOrPut(row) { readOffsets(rowOffset) } - val dataOffset = collumnOffsets[column] - val dataSize = collumnOffsets[column + 1] - dataOffset + val dataOffset = columnOffsets[column] + val dataSize = columnOffsets[column + 1] - dataOffset val result = ByteArray(dataSize) buffer.position = rowOffset + dataOffset @@ -155,6 +155,16 @@ abstract class IrMultiTableReader(private val buffer: ReadBuffer, private val return result } + fun tableItemBytes(idx: Int): ByteArray { + val rowOffset = indexToOffset[idx] + val nextOffset = indexToOffset[idx + 1] + val size = nextOffset - rowOffset + val result = ByteArray(size) + buffer.position = rowOffset + buffer.get(result, 0, size) + return result + } + fun tableItemBytes(row: Int, id: K): ByteArray { val rowOffset = indexToOffset[row] diff --git a/compiler/util-klib/src/org/jetbrains/kotlin/library/impl/KotlinLibraryImpl.kt b/compiler/util-klib/src/org/jetbrains/kotlin/library/impl/KotlinLibraryImpl.kt index 68d768d4e54..845c191827a 100644 --- a/compiler/util-klib/src/org/jetbrains/kotlin/library/impl/KotlinLibraryImpl.kt +++ b/compiler/util-klib/src/org/jetbrains/kotlin/library/impl/KotlinLibraryImpl.kt @@ -156,6 +156,26 @@ class IrMonoliticLibraryImpl(_access: IrLibraryAccess) : it.irFiles }) } + + override fun types(fileIndex: Int): ByteArray { + return types.tableItemBytes(fileIndex) + } + + override fun signatures(fileIndex: Int): ByteArray { + return signatures.tableItemBytes(fileIndex) + } + + override fun strings(fileIndex: Int): ByteArray { + return strings.tableItemBytes(fileIndex) + } + + override fun declarations(fileIndex: Int): ByteArray { + return combinedDeclarations.tableItemBytes(fileIndex) + } + + override fun bodies(fileIndex: Int): ByteArray { + return bodies.tableItemBytes(fileIndex) + } } class IrPerFileLibraryImpl(_access: IrLibraryAccess) : IrLibraryImpl(_access) { @@ -188,13 +208,17 @@ class IrPerFileLibraryImpl(_access: IrLibraryAccess) : Ir return dataReader.tableItemBytes(index) } - override fun signature(index: Int, fileIndex: Int): ByteArray { - val dataReader = fileToTypeMap.getOrPut(fileIndex) { + private fun signatureDataReader(fileIndex: Int): IrArrayFileReader { + return fileToTypeMap.getOrPut(fileIndex) { val fileDirectory = directories[fileIndex] IrArrayFileReader(access.realFiles { it.irSignatures(fileDirectory) }) } + } + + override fun signature(index: Int, fileIndex: Int): ByteArray { + val dataReader = signatureDataReader(fileIndex) return dataReader.tableItemBytes(index) } @@ -246,6 +270,26 @@ class IrPerFileLibraryImpl(_access: IrLibraryAccess) : Ir override fun fileCount(): Int { return directories.size } + + override fun types(fileIndex: Int): ByteArray { + TODO("Not yet implemented") + } + + override fun signatures(fileIndex: Int): ByteArray { + TODO("Not yet implemented") + } + + override fun strings(fileIndex: Int): ByteArray { + TODO("Not yet implemented") + } + + override fun declarations(fileIndex: Int): ByteArray { + TODO("Not yet implemented") + } + + override fun bodies(fileIndex: Int): ByteArray { + TODO("Not yet implemented") + } } open class KotlinLibraryImpl(