Load definitions of symbols from .kotlin_metadata files
Extract AbstractDeserializedPackageFragmentProvider out of JvmBuiltInsPackageFragmentProvider and implement it a little bit differently in MetadataPackageFragmentProvider. The main difference is in how the package fragment scope is constructed: for built-ins, it's just a single scope that loads everything from one protobuf message. For metadata, package scope can consist of many files, some of which store information about classes and others are similar to package parts on JVM, so a ChainedMemberScope instance is created. Introduce a bunch of interfaces/methods to deliver the needed behavior to the 'deserialization' module which is not JVM-specific and does not depend on the compiler code: MetadataFinderFactory, PackagePartProvider#findMetadataPackageParts, KotlinMetadataFinder#findMetadata. Note that these declarations are currently only implemented in the compiler; no metadata package parts/fragments will be found in IDE or reflection
This commit is contained in:
+45
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.serialization.deserialization
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.utils.singletonOrEmptyList
|
||||
|
||||
abstract class AbstractDeserializedPackageFragmentProvider(
|
||||
protected val storageManager: StorageManager,
|
||||
protected val finder: KotlinMetadataFinder,
|
||||
protected val moduleDescriptor: ModuleDescriptor
|
||||
) : PackageFragmentProvider {
|
||||
protected lateinit var components: DeserializationComponents
|
||||
|
||||
private val fragments = storageManager.createMemoizedFunctionWithNullableValues<FqName, PackageFragmentDescriptor> { fqName ->
|
||||
findPackage(fqName)?.apply {
|
||||
components = this@AbstractDeserializedPackageFragmentProvider.components
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract fun findPackage(fqName: FqName): DeserializedPackageFragment?
|
||||
|
||||
override fun getPackageFragments(fqName: FqName): List<PackageFragmentDescriptor> = fragments(fqName).singletonOrEmptyList()
|
||||
|
||||
override fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection<FqName> = emptySet()
|
||||
}
|
||||
+8
-9
@@ -20,9 +20,9 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.PackageFragmentDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedMemberScope
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.storage.getValue
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class DeserializedPackageFragment(
|
||||
@@ -34,17 +34,16 @@ abstract class DeserializedPackageFragment(
|
||||
@set:Inject
|
||||
lateinit var components: DeserializationComponents
|
||||
|
||||
private val deserializedMemberScope by storageManager.createLazyValue {
|
||||
computeMemberScope()
|
||||
}
|
||||
private val memberScope = storageManager.createLazyValue { computeMemberScope() }
|
||||
|
||||
abstract val classDataFinder: ClassDataFinder
|
||||
|
||||
protected abstract fun computeMemberScope(): DeserializedPackageMemberScope
|
||||
protected abstract fun computeMemberScope(): MemberScope
|
||||
|
||||
override fun getMemberScope() = deserializedMemberScope
|
||||
override fun getMemberScope() = memberScope()
|
||||
|
||||
internal fun hasTopLevelClass(name: Name): Boolean {
|
||||
return name in getMemberScope().classNames
|
||||
open fun hasTopLevelClass(name: Name): Boolean {
|
||||
val scope = getMemberScope()
|
||||
return scope is DeserializedMemberScope && name in scope.classNames
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -16,10 +16,17 @@
|
||||
|
||||
package org.jetbrains.kotlin.serialization.deserialization
|
||||
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import java.io.InputStream
|
||||
|
||||
interface KotlinMetadataFinder {
|
||||
/**
|
||||
* @return an [InputStream] which should be used to load the .kotlin_metadata file for class with the given [classId].
|
||||
* [classId] identifies either a real top level class, or a package part (e.g. it can be "foo/bar/_1Kt")
|
||||
*/
|
||||
fun findMetadata(classId: ClassId): InputStream?
|
||||
|
||||
/**
|
||||
* @return an [InputStream] which should be used to load the .kotlin_builtins file for package with the given [packageFqName].
|
||||
*/
|
||||
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.serialization.deserialization
|
||||
|
||||
import org.jetbrains.kotlin.builtins.BuiltInSerializerProtocol
|
||||
import org.jetbrains.kotlin.builtins.BuiltInsBinaryVersion
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackagePartProvider
|
||||
import org.jetbrains.kotlin.descriptors.SourceElement
|
||||
import org.jetbrains.kotlin.incremental.components.LookupTracker
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.scopes.ChainedMemberScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.serialization.ClassData
|
||||
import org.jetbrains.kotlin.serialization.ClassDataWithSource
|
||||
import org.jetbrains.kotlin.serialization.ProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.builtins.BuiltInsProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import java.io.InputStream
|
||||
|
||||
class MetadataPackageFragmentProvider(
|
||||
storageManager: StorageManager,
|
||||
finder: KotlinMetadataFinder,
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
notFoundClasses: NotFoundClasses,
|
||||
private val packagePartProvider: PackagePartProvider
|
||||
) : AbstractDeserializedPackageFragmentProvider(storageManager, finder, moduleDescriptor) {
|
||||
init {
|
||||
components = DeserializationComponents(
|
||||
storageManager,
|
||||
moduleDescriptor,
|
||||
DeserializationConfiguration.Default, // TODO
|
||||
DeserializedClassDataFinder(this),
|
||||
AnnotationAndConstantLoaderImpl(moduleDescriptor, notFoundClasses, BuiltInSerializerProtocol),
|
||||
this,
|
||||
LocalClassifierTypeSettings.Default,
|
||||
ErrorReporter.DO_NOTHING,
|
||||
LookupTracker.Companion.DO_NOTHING,
|
||||
FlexibleTypeDeserializer.ThrowException,
|
||||
emptyList(),
|
||||
notFoundClasses, AdditionalClassPartsProvider.None, PlatformDependentDeclarationFilter.All
|
||||
)
|
||||
}
|
||||
|
||||
override fun findPackage(fqName: FqName): DeserializedPackageFragment? =
|
||||
MetadataPackageFragment(fqName, storageManager, moduleDescriptor, packagePartProvider, finder)
|
||||
}
|
||||
|
||||
class MetadataPackageFragment(
|
||||
fqName: FqName,
|
||||
storageManager: StorageManager,
|
||||
module: ModuleDescriptor,
|
||||
private val packagePartProvider: PackagePartProvider,
|
||||
private val finder: KotlinMetadataFinder
|
||||
) : DeserializedPackageFragment(fqName, storageManager, module) {
|
||||
override val classDataFinder = ClassDataFinder { classId ->
|
||||
val topLevelClassId = generateSequence(classId) { classId -> if (classId.isNestedClass) classId.outerClassId else null }.last()
|
||||
val stream = finder.findMetadata(topLevelClassId) ?: return@ClassDataFinder null
|
||||
val (message, nameResolver) = readProto(stream)
|
||||
message.class_List.firstOrNull { classProto ->
|
||||
nameResolver.getClassId(classProto.fqName) == classId
|
||||
}?.let { classProto ->
|
||||
ClassDataWithSource(ClassData(nameResolver, classProto), SourceElement.NO_SOURCE)
|
||||
}
|
||||
}
|
||||
|
||||
override fun computeMemberScope(): MemberScope {
|
||||
// For each .kotlin_metadata file which represents a package part, add a separate deserialized scope
|
||||
// with top level callables and type aliases (but no classes) only from that part
|
||||
val packageParts = packagePartProvider.findMetadataPackageParts(fqName.asString())
|
||||
val scopes = arrayListOf<DeserializedPackageMemberScope>()
|
||||
for (partName in packageParts) {
|
||||
val stream = finder.findMetadata(ClassId(fqName, Name.identifier(partName))) ?: continue
|
||||
val (proto, nameResolver) = readProto(stream)
|
||||
|
||||
scopes.add(DeserializedPackageMemberScope(
|
||||
this, proto.`package`, nameResolver, containerSource = null, components = components, classNames = { emptyList() }
|
||||
))
|
||||
}
|
||||
|
||||
// Also add the deserialized scope that can load all classes from this package
|
||||
scopes.add(object : DeserializedPackageMemberScope(
|
||||
this, ProtoBuf.Package.getDefaultInstance(),
|
||||
NameResolverImpl(ProtoBuf.StringTable.getDefaultInstance(), ProtoBuf.QualifiedNameTable.getDefaultInstance()),
|
||||
containerSource = null, components = components, classNames = { emptyList() }
|
||||
) {
|
||||
override fun hasClass(name: Name): Boolean = hasTopLevelClass(name)
|
||||
})
|
||||
|
||||
return ChainedMemberScope.create("Metadata scope", scopes)
|
||||
}
|
||||
|
||||
override fun hasTopLevelClass(name: Name): Boolean {
|
||||
// TODO: check if the corresponding file exists
|
||||
return true
|
||||
}
|
||||
|
||||
private fun readProto(stream: InputStream): Pair<BuiltInsProtoBuf.BuiltIns, NameResolverImpl> {
|
||||
val version = BuiltInsBinaryVersion.readFrom(stream)
|
||||
|
||||
if (!version.isCompatible()) {
|
||||
// TODO: report a proper diagnostic
|
||||
throw UnsupportedOperationException(
|
||||
"Kotlin metadata definition format version is not supported: " +
|
||||
"expected ${BuiltInsBinaryVersion.INSTANCE}, actual $version. " +
|
||||
"Please update Kotlin"
|
||||
)
|
||||
}
|
||||
|
||||
val message = BuiltInsProtoBuf.BuiltIns.parseFrom(stream, BuiltInSerializerProtocol.extensionRegistry)
|
||||
val nameResolver = NameResolverImpl(message.strings, message.qualifiedNames)
|
||||
return Pair(message, nameResolver)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val METADATA_FILE_EXTENSION = ".kotlin_metadata"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user