[FIR] Deserialize Klib KlibMetadataProtoBuf. classFile, functionFile & propertyFile

klibs provide information about the container source file
in the KlibMetadataProtoBuf extensions for functions,
properties and classes.

This information is deserialized and attached to the
`klibSourceFile` extension (stored in FirDeclarationDataRegistry)

^KT-66271 Fixed
This commit is contained in:
Sebastian Sellmair
2024-03-05 10:58:24 +01:00
committed by Space Team
parent 85a1d67d19
commit 6d8a4a28af
4 changed files with 92 additions and 12 deletions
@@ -12,12 +12,14 @@ import org.jetbrains.kotlin.fir.caches.createCache
import org.jetbrains.kotlin.fir.caches.firCachesFactory
import org.jetbrains.kotlin.fir.caches.getValue
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
import org.jetbrains.kotlin.fir.declarations.FirFunction
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
import org.jetbrains.kotlin.fir.isNewPlaceForBodyGeneration
import org.jetbrains.kotlin.fir.resolve.providers.FirCachedSymbolNamesProvider
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolNamesProvider
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolProvider
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolProviderInternals
import org.jetbrains.kotlin.fir.resolve.providers.FirCachedSymbolNamesProvider
import org.jetbrains.kotlin.fir.scopes.FirKotlinScopeProvider
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.metadata.ProtoBuf
@@ -32,7 +34,17 @@ import java.nio.file.Path
class PackagePartsCacheData(
val proto: ProtoBuf.Package,
val context: FirDeserializationContext,
val extra: Extra? = null,
) {
/**
* Marker interface for 'extra' data that can be attached to a given [PackagePartsCacheData].
* Example: This can be used by a [FirSymbolProvider] to attach data (like the library that this package part came from) to
* this particular package
*
* @see PackagePartsCacheData.extra
*/
interface Extra
val topLevelFunctionNameIndex by lazy {
proto.functionList.withIndex()
.groupBy({ context.nameResolver.getName(it.value.name) }) { (index) -> index }
@@ -244,10 +256,13 @@ abstract class AbstractFirDeserializedSymbolProvider(
return getPackageParts(callableId.packageName).flatMap { part ->
val functionIds = part.topLevelFunctionNameIndex[callableId.callableName] ?: return@flatMap emptyList()
functionIds.map {
part.context.memberDeserializer.loadFunction(
val proto = part.proto.getFunction(it)
val fir = part.context.memberDeserializer.loadFunction(
part.proto.getFunction(it),
deserializationOrigin = defaultDeserializationOrigin
).symbol
)
loadFunctionExtensions(part, proto, fir)
fir.symbol
}
}
}
@@ -256,11 +271,24 @@ abstract class AbstractFirDeserializedSymbolProvider(
return getPackageParts(callableId.packageName).flatMap { part ->
val propertyIds = part.topLevelPropertyNameIndex[callableId.callableName] ?: return@flatMap emptyList()
propertyIds.map {
part.context.memberDeserializer.loadProperty(part.proto.getProperty(it)).symbol
val proto = part.proto.getProperty(it)
val fir = part.context.memberDeserializer.loadProperty(proto)
loadPropertyExtensions(part, proto, fir)
fir.symbol
}
}
}
open fun loadFunctionExtensions(
packagePart: PackagePartsCacheData, proto: ProtoBuf.Function, fir: FirFunction,
) {
}
open fun loadPropertyExtensions(
packagePart: PackagePartsCacheData, proto: ProtoBuf.Property, fir: FirProperty,
) {
}
private fun getPackageParts(packageFqName: FqName): Collection<PackagePartsCacheData> =
packagePartsCache.getValue(packageFqName)