kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-reflect Show documentation
Show all versions of kotlin-reflect Show documentation
Kotlin Full Reflection Library
/*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors
import kotlin.reflect.jvm.internal.impl.descriptors.*
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.Annotations
import kotlin.reflect.jvm.internal.impl.descriptors.impl.AbstractClassDescriptor
import kotlin.reflect.jvm.internal.impl.descriptors.impl.EnumEntrySyntheticClassDescriptor
import kotlin.reflect.jvm.internal.impl.incremental.components.LookupLocation
import kotlin.reflect.jvm.internal.impl.incremental.components.NoLookupLocation
import kotlin.reflect.jvm.internal.impl.incremental.record
import kotlin.reflect.jvm.internal.impl.metadata.ProtoBuf
import kotlin.reflect.jvm.internal.impl.metadata.deserialization.*
import kotlin.reflect.jvm.internal.impl.name.Name
import kotlin.reflect.jvm.internal.impl.resolve.DescriptorFactory
import kotlin.reflect.jvm.internal.impl.resolve.NonReportingOverrideStrategy
import kotlin.reflect.jvm.internal.impl.resolve.OverridingUtil
import kotlin.reflect.jvm.internal.impl.resolve.descriptorUtil.classId
import kotlin.reflect.jvm.internal.impl.resolve.descriptorUtil.computeSealedSubclasses
import kotlin.reflect.jvm.internal.impl.resolve.scopes.DescriptorKindFilter
import kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope
import kotlin.reflect.jvm.internal.impl.resolve.scopes.StaticScopeForKotlinEnum
import kotlin.reflect.jvm.internal.impl.serialization.deserialization.*
import kotlin.reflect.jvm.internal.impl.types.AbstractClassTypeConstructor
import kotlin.reflect.jvm.internal.impl.types.KotlinType
import kotlin.reflect.jvm.internal.impl.types.SimpleType
import kotlin.reflect.jvm.internal.impl.types.TypeConstructor
import kotlin.reflect.jvm.internal.impl.types.checker.KotlinTypeRefiner
import kotlin.reflect.jvm.internal.impl.types.refinement.TypeRefinement
import kotlin.reflect.jvm.internal.impl.utils.addToStdlib.flatMapToNullable
import java.util.*
class DeserializedClassDescriptor(
outerContext: DeserializationContext,
val classProto: ProtoBuf.Class,
nameResolver: NameResolver,
val metadataVersion: BinaryVersion,
private val sourceElement: SourceElement
) : AbstractClassDescriptor(
outerContext.storageManager,
nameResolver.getClassId(classProto.fqName).shortClassName
), DeserializedDescriptor {
private val classId = nameResolver.getClassId(classProto.fqName)
private val modality = ProtoEnumFlags.modality(Flags.MODALITY.get(classProto.flags))
private val visibility = ProtoEnumFlags.visibility(Flags.VISIBILITY.get(classProto.flags))
private val kind = ProtoEnumFlags.classKind(Flags.CLASS_KIND.get(classProto.flags))
val c = outerContext.childContext(
this, classProto.typeParameterList, nameResolver, TypeTable(classProto.typeTable),
VersionRequirementTable.create(classProto.versionRequirementTable), metadataVersion
)
private val staticScope = if (kind == ClassKind.ENUM_CLASS) StaticScopeForKotlinEnum(c.storageManager, this) else MemberScope.Empty
private val typeConstructor = DeserializedClassTypeConstructor()
private val memberScopeHolder =
ScopesHolderForClass.create(this, c.storageManager, c.components.kotlinTypeChecker.kotlinTypeRefiner, this::DeserializedClassMemberScope)
private val memberScope get() = memberScopeHolder.getScope(c.components.kotlinTypeChecker.kotlinTypeRefiner)
private val enumEntries = if (kind == ClassKind.ENUM_CLASS) EnumEntryClassDescriptors() else null
private val containingDeclaration = outerContext.containingDeclaration
private val primaryConstructor = c.storageManager.createNullableLazyValue { computePrimaryConstructor() }
private val constructors = c.storageManager.createLazyValue { computeConstructors() }
private val companionObjectDescriptor = c.storageManager.createNullableLazyValue { computeCompanionObjectDescriptor() }
private val sealedSubclasses = c.storageManager.createLazyValue { computeSubclassesForSealedClass() }
internal val thisAsProtoContainer: ProtoContainer.Class = ProtoContainer.Class(
classProto, c.nameResolver, c.typeTable, sourceElement,
(containingDeclaration as? DeserializedClassDescriptor)?.thisAsProtoContainer
)
val versionRequirements: List
get() = VersionRequirement.create(classProto, c.nameResolver, c.versionRequirementTable)
override val annotations =
if (!Flags.HAS_ANNOTATIONS.get(classProto.flags)) {
Annotations.EMPTY
} else NonEmptyDeserializedAnnotations(c.storageManager) {
c.components.annotationAndConstantLoader.loadClassAnnotations(thisAsProtoContainer).toList()
}
override fun getContainingDeclaration(): DeclarationDescriptor = containingDeclaration
override fun getTypeConstructor(): TypeConstructor = typeConstructor
override fun getKind() = kind
override fun getModality() = modality
override fun getVisibility() = visibility
override fun isInner() = Flags.IS_INNER.get(classProto.flags)
override fun isData() = Flags.IS_DATA.get(classProto.flags)
override fun isInline() = Flags.IS_INLINE_CLASS.get(classProto.flags)
override fun isExpect() = Flags.IS_EXPECT_CLASS.get(classProto.flags)
override fun isActual() = false
override fun isExternal() = Flags.IS_EXTERNAL_CLASS.get(classProto.flags)
override fun isFun() = Flags.IS_FUN_INTERFACE.get(classProto.flags)
override fun getUnsubstitutedMemberScope(kotlinTypeRefiner: KotlinTypeRefiner): MemberScope =
memberScopeHolder.getScope(kotlinTypeRefiner)
override fun getStaticScope() = staticScope
override fun isCompanionObject(): Boolean = Flags.CLASS_KIND.get(classProto.flags) == ProtoBuf.Class.Kind.COMPANION_OBJECT
private fun computePrimaryConstructor(): ClassConstructorDescriptor? {
if (kind.isSingleton) {
return DescriptorFactory.createPrimaryConstructorForObject(this, SourceElement.NO_SOURCE).apply {
returnType = getDefaultType()
}
}
return classProto.constructorList.firstOrNull { !Flags.IS_SECONDARY.get(it.flags) }?.let { constructorProto ->
c.memberDeserializer.loadConstructor(constructorProto, true)
}
}
override fun getUnsubstitutedPrimaryConstructor(): ClassConstructorDescriptor? = primaryConstructor()
private fun computeConstructors(): Collection =
computeSecondaryConstructors() + listOfNotNull(unsubstitutedPrimaryConstructor) +
c.components.additionalClassPartsProvider.getConstructors(this)
private fun computeSecondaryConstructors(): List =
classProto.constructorList.filter { Flags.IS_SECONDARY.get(it.flags) }.map {
c.memberDeserializer.loadConstructor(it, false)
}
override fun getConstructors() = constructors()
private fun computeCompanionObjectDescriptor(): ClassDescriptor? {
if (!classProto.hasCompanionObjectName()) return null
val companionObjectName = c.nameResolver.getName(classProto.companionObjectName)
return memberScope.getContributedClassifier(companionObjectName, NoLookupLocation.FROM_DESERIALIZATION) as? ClassDescriptor
}
override fun getCompanionObjectDescriptor(): ClassDescriptor? = companionObjectDescriptor()
internal fun hasNestedClass(name: Name): Boolean =
name in memberScope.classNames
private fun computeSubclassesForSealedClass(): Collection {
if (modality != Modality.SEALED) return emptyList()
val fqNames = classProto.sealedSubclassFqNameList
if (fqNames.isNotEmpty()) {
return fqNames.mapNotNull { index ->
c.components.deserializeClass(c.nameResolver.getClassId(index))
}
}
// This is needed because classes compiled with Kotlin 1.0 did not contain the sealed_subclass_fq_name field
return computeSealedSubclasses(this)
}
override fun getSealedSubclasses() = sealedSubclasses()
override fun toString() = "deserialized ${if (isExpect()) "expect" else ""} class $name" // not using descriptor render to preserve laziness
override fun getSource() = sourceElement
override fun getDeclaredTypeParameters() = c.typeDeserializer.ownTypeParameters
override fun getDefaultFunctionTypeForSamInterface(): SimpleType? {
return c.components.samConversionResolver.resolveFunctionTypeIfSamInterface(this)
}
override fun isDefinitelyNotSamInterface() = !isFun
private inner class DeserializedClassTypeConstructor : AbstractClassTypeConstructor(c.storageManager) {
private val parameters = c.storageManager.createLazyValue {
this@DeserializedClassDescriptor.computeConstructorTypeParameters()
}
override fun computeSupertypes(): Collection {
val result = classProto.supertypes(c.typeTable).map { supertypeProto ->
c.typeDeserializer.type(supertypeProto)
} + c.components.additionalClassPartsProvider.getSupertypes(this@DeserializedClassDescriptor)
val unresolved = result.mapNotNull { supertype ->
supertype.constructor.declarationDescriptor as? NotFoundClasses.MockClassDescriptor
}
if (unresolved.isNotEmpty()) {
c.components.errorReporter.reportIncompleteHierarchy(
this@DeserializedClassDescriptor,
unresolved.map { it.classId?.asSingleFqName()?.asString() ?: it.name.asString() }
)
}
return result.toList()
}
override fun getParameters() = parameters()
override fun isDenotable() = true
override fun getDeclarationDescriptor() = this@DeserializedClassDescriptor
override fun toString() = name.toString()
override val supertypeLoopChecker: SupertypeLoopChecker
// TODO: inject implementation
get() = SupertypeLoopChecker.EMPTY
}
private inner class DeserializedClassMemberScope(private val kotlinTypeRefiner: KotlinTypeRefiner) : DeserializedMemberScope(
c, classProto.functionList, classProto.propertyList, classProto.typeAliasList,
classProto.nestedClassNameList.map(c.nameResolver::getName).let { { it } } // workaround KT-13454
) {
private val classDescriptor: DeserializedClassDescriptor get() = this@DeserializedClassDescriptor
private val allDescriptors = c.storageManager.createLazyValue {
computeDescriptors(DescriptorKindFilter.ALL, MemberScope.ALL_NAME_FILTER, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS)
}
private val refinedSupertypes = c.storageManager.createLazyValue {
@OptIn(TypeRefinement::class)
kotlinTypeRefiner.refineSupertypes(classDescriptor)
}
override fun getContributedDescriptors(
kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean
): Collection = allDescriptors()
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection {
recordLookup(name, location)
return super.getContributedFunctions(name, location)
}
override fun getContributedVariables(name: Name, location: LookupLocation): Collection {
recordLookup(name, location)
return super.getContributedVariables(name, location)
}
override fun isDeclaredFunctionAvailable(function: SimpleFunctionDescriptor): Boolean {
return c.components.platformDependentDeclarationFilter.isFunctionAvailable(this@DeserializedClassDescriptor, function)
}
override fun computeNonDeclaredFunctions(name: Name, functions: MutableList) {
val fromSupertypes = ArrayList()
for (supertype in refinedSupertypes()) {
fromSupertypes.addAll(supertype.memberScope.getContributedFunctions(name, NoLookupLocation.FOR_ALREADY_TRACKED))
}
functions.addAll(c.components.additionalClassPartsProvider.getFunctions(name, this@DeserializedClassDescriptor))
generateFakeOverrides(name, fromSupertypes, functions)
}
override fun computeNonDeclaredProperties(name: Name, descriptors: MutableList) {
val fromSupertypes = ArrayList()
for (supertype in refinedSupertypes()) {
fromSupertypes.addAll(supertype.memberScope.getContributedVariables(name, NoLookupLocation.FOR_ALREADY_TRACKED))
}
generateFakeOverrides(name, fromSupertypes, descriptors)
}
private fun generateFakeOverrides(
name: Name,
fromSupertypes: Collection,
result: MutableList
) {
val fromCurrent = ArrayList(result)
c.components.kotlinTypeChecker.overridingUtil.generateOverridesInFunctionGroup(
name,
fromSupertypes,
fromCurrent,
classDescriptor,
object : NonReportingOverrideStrategy() {
override fun addFakeOverride(fakeOverride: CallableMemberDescriptor) {
// TODO: report "cannot infer visibility"
OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, null)
@Suppress("UNCHECKED_CAST")
result.add(fakeOverride as D)
}
override fun conflict(
fromSuper: CallableMemberDescriptor,
fromCurrent: CallableMemberDescriptor
) {
// TODO report conflicts
}
})
}
override fun getNonDeclaredFunctionNames(): Set {
return classDescriptor.typeConstructor.supertypes.flatMapTo(LinkedHashSet()) {
it.memberScope.getFunctionNames()
}.apply { addAll(c.components.additionalClassPartsProvider.getFunctionsNames(this@DeserializedClassDescriptor)) }
}
override fun getNonDeclaredVariableNames(): Set {
return classDescriptor.typeConstructor.supertypes.flatMapTo(LinkedHashSet()) {
it.memberScope.getVariableNames()
}
}
override fun getNonDeclaredClassifierNames(): Set? {
return classDescriptor.typeConstructor.supertypes.flatMapToNullable(LinkedHashSet()) {
it.memberScope.getClassifierNames()
}
}
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? {
recordLookup(name, location)
classDescriptor.enumEntries?.findEnumEntry(name)?.let { return it }
return super.getContributedClassifier(name, location)
}
override fun createClassId(name: Name) = classId.createNestedClassId(name)
override fun addEnumEntryDescriptors(result: MutableCollection, nameFilter: (Name) -> Boolean) {
result.addAll(classDescriptor.enumEntries?.all().orEmpty())
}
override fun recordLookup(name: Name, location: LookupLocation) {
c.components.lookupTracker.record(location, classDescriptor, name)
}
}
private inner class EnumEntryClassDescriptors {
private val enumEntryProtos = classProto.enumEntryList.associateBy { c.nameResolver.getName(it.name) }
private val enumEntryByName = c.storageManager.createMemoizedFunctionWithNullableValues { name ->
enumEntryProtos[name]?.let { proto ->
EnumEntrySyntheticClassDescriptor.create(
c.storageManager, this@DeserializedClassDescriptor, name, enumMemberNames,
DeserializedAnnotations(c.storageManager) {
c.components.annotationAndConstantLoader.loadEnumEntryAnnotations(thisAsProtoContainer, proto).toList()
},
SourceElement.NO_SOURCE
)
}
}
private val enumMemberNames = c.storageManager.createLazyValue { computeEnumMemberNames() }
fun findEnumEntry(name: Name): ClassDescriptor? = enumEntryByName(name)
private fun computeEnumMemberNames(): Set {
// NOTE: order of enum entry members should be irrelevant
// because enum entries are effectively invisible to user (as classes)
val result = HashSet()
for (supertype in getTypeConstructor().supertypes) {
for (descriptor in supertype.memberScope.getContributedDescriptors()) {
if (descriptor is SimpleFunctionDescriptor || descriptor is PropertyDescriptor) {
result.add(descriptor.name)
}
}
}
return classProto.functionList.mapTo(result) { c.nameResolver.getName(it.name) } +
classProto.propertyList.mapTo(result) { c.nameResolver.getName(it.name) }
}
fun all(): Collection =
enumEntryProtos.keys.mapNotNull { name -> findEnumEntry(name) }
}
}