Reformat module 'deserialization', fix warnings/inspections

This commit is contained in:
Alexander Udalov
2018-07-11 18:38:23 +02:00
parent ae6627fe94
commit 53a57e8e1b
22 changed files with 423 additions and 425 deletions
@@ -51,15 +51,16 @@ class AnnotationAndConstantLoaderImpl(
is ProtoBuf.Property -> proto.getExtension(protocol.propertyAnnotation) is ProtoBuf.Property -> proto.getExtension(protocol.propertyAnnotation)
else -> error("Unknown message: $proto") else -> error("Unknown message: $proto")
}.orEmpty() }.orEmpty()
return annotations.map { proto -> AnnotationWithTarget(deserializer.deserializeAnnotation(proto, container.nameResolver), null) } return annotations.map { annotationProto ->
AnnotationWithTarget(deserializer.deserializeAnnotation(annotationProto, container.nameResolver), null)
}
} }
override fun loadEnumEntryAnnotations( override fun loadEnumEntryAnnotations(container: ProtoContainer, proto: ProtoBuf.EnumEntry): List<AnnotationDescriptor> {
container: ProtoContainer,
proto: ProtoBuf.EnumEntry
): List<AnnotationDescriptor> {
val annotations = proto.getExtension(protocol.enumEntryAnnotation).orEmpty() val annotations = proto.getExtension(protocol.enumEntryAnnotation).orEmpty()
return annotations.map { proto -> deserializer.deserializeAnnotation(proto, container.nameResolver) } return annotations.map { annotationProto ->
deserializer.deserializeAnnotation(annotationProto, container.nameResolver)
}
} }
override fun loadValueParameterAnnotations( override fun loadValueParameterAnnotations(
@@ -70,7 +71,9 @@ class AnnotationAndConstantLoaderImpl(
proto: ProtoBuf.ValueParameter proto: ProtoBuf.ValueParameter
): List<AnnotationDescriptor> { ): List<AnnotationDescriptor> {
val annotations = proto.getExtension(protocol.parameterAnnotation).orEmpty() val annotations = proto.getExtension(protocol.parameterAnnotation).orEmpty()
return annotations.map { proto -> deserializer.deserializeAnnotation(proto, container.nameResolver) } return annotations.map { annotationProto ->
deserializer.deserializeAnnotation(annotationProto, container.nameResolver)
}
} }
override fun loadExtensionReceiverParameterAnnotations( override fun loadExtensionReceiverParameterAnnotations(
@@ -87,11 +90,7 @@ class AnnotationAndConstantLoaderImpl(
return proto.getExtension(protocol.typeParameterAnnotation).orEmpty().map { deserializer.deserializeAnnotation(it, nameResolver) } return proto.getExtension(protocol.typeParameterAnnotation).orEmpty().map { deserializer.deserializeAnnotation(it, nameResolver) }
} }
override fun loadPropertyConstant( override fun loadPropertyConstant(container: ProtoContainer, proto: ProtoBuf.Property, expectedType: KotlinType): ConstantValue<*>? {
container: ProtoContainer,
proto: ProtoBuf.Property,
expectedType: KotlinType
): ConstantValue<*>? {
val value = proto.getExtensionOrNull(protocol.compileTimeValue) ?: return null val value = proto.getExtensionOrNull(protocol.compileTimeValue) ?: return null
return deserializer.resolveValue(expectedType, value, container.nameResolver) return deserializer.resolveValue(expectedType, value, container.nameResolver)
} }
@@ -63,11 +63,7 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
return Pair(nameResolver.getName(proto.nameId), resolveValue(parameter.type, proto.value, nameResolver)) return Pair(nameResolver.getName(proto.nameId), resolveValue(parameter.type, proto.value, nameResolver))
} }
fun resolveValue( fun resolveValue(expectedType: KotlinType, value: Value, nameResolver: NameResolver): ConstantValue<*> {
expectedType: KotlinType,
value: Value,
nameResolver: NameResolver
): ConstantValue<*> {
val isUnsigned = Flags.IS_UNSIGNED.get(value.flags) val isUnsigned = Flags.IS_UNSIGNED.get(value.flags)
val result: ConstantValue<*> = when (value.type) { val result: ConstantValue<*> = when (value.type) {
@@ -98,10 +94,9 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
val actualArrayType = val actualArrayType =
if (arrayElements.isNotEmpty()) { if (arrayElements.isNotEmpty()) {
val actualElementType = resolveArrayElementType(arrayElements.first(), nameResolver) val actualElementType = resolveArrayElementType(arrayElements.first(), nameResolver)
builtIns.getPrimitiveArrayKotlinTypeByPrimitiveKotlinType(actualElementType) ?: builtIns.getPrimitiveArrayKotlinTypeByPrimitiveKotlinType(actualElementType)
builtIns.getArrayType(Variance.INVARIANT, actualElementType) ?: builtIns.getArrayType(Variance.INVARIANT, actualElementType)
} } else {
else {
// In the case of empty array, no element has the element type, so we fall back to the expected type, if any. // In the case of empty array, no element has the element type, so we fall back to the expected type, if any.
// This is not very accurate when annotation class has been changed without recompiling clients, // This is not very accurate when annotation class has been changed without recompiling clients,
// but should not in fact matter because the value is empty anyway // but should not in fact matter because the value is empty anyway
@@ -122,8 +117,7 @@ class AnnotationDeserializer(private val module: ModuleDescriptor, private val n
return if (result.getType(module).isSubtypeOf(expectedType)) { return if (result.getType(module).isSubtypeOf(expectedType)) {
result result
} } else {
else {
// This means that an annotation class has been changed incompatibly without recompiling clients // This means that an annotation class has been changed incompatibly without recompiling clients
ErrorValue.create("Unexpected argument value") ErrorValue.create("Unexpected argument value")
} }
@@ -51,8 +51,7 @@ class ClassDeserializer(private val components: DeserializationComponents) {
if (!outerClass.hasNestedClass(classId.shortClassName)) return null if (!outerClass.hasNestedClass(classId.shortClassName)) return null
outerClass.c outerClass.c
} } else {
else {
val fragments = components.packageFragmentProvider.getPackageFragments(classId.packageFqName) val fragments = components.packageFragmentProvider.getPackageFragments(classId.packageFqName)
val fragment = fragments.firstOrNull { it !is DeserializedPackageFragment || it.hasTopLevelClass(classId.shortClassName) } val fragment = fragments.firstOrNull { it !is DeserializedPackageFragment || it.hasTopLevelClass(classId.shortClassName) }
?: return null ?: return null
@@ -24,7 +24,7 @@ interface FlexibleTypeDeserializer {
fun create(proto: ProtoBuf.Type, flexibleId: String, lowerBound: SimpleType, upperBound: SimpleType): KotlinType fun create(proto: ProtoBuf.Type, flexibleId: String, lowerBound: SimpleType, upperBound: SimpleType): KotlinType
object ThrowException : FlexibleTypeDeserializer { object ThrowException : FlexibleTypeDeserializer {
override fun create(proto: ProtoBuf.Type, flexibleId: String, lowerBound: SimpleType, upperBound: SimpleType): KotlinType override fun create(proto: ProtoBuf.Type, flexibleId: String, lowerBound: SimpleType, upperBound: SimpleType): KotlinType =
= throw IllegalArgumentException("This method should not be used.") throw IllegalArgumentException("This method should not be used.")
} }
} }
@@ -276,7 +276,7 @@ class MemberDeserializer(private val c: DeserializationContext) {
c.containingDeclaration.asProtoContainer()?.let { c.containingDeclaration.asProtoContainer()?.let {
c.components.annotationAndConstantLoader c.components.annotationAndConstantLoader
.loadExtensionReceiverParameterAnnotations(it, proto, receiverTargetedKind) .loadExtensionReceiverParameterAnnotations(it, proto, receiverTargetedKind)
.map { AnnotationWithTarget(it, AnnotationUseSiteTarget.RECEIVER) } .map { annotation -> AnnotationWithTarget(annotation, AnnotationUseSiteTarget.RECEIVER) }
.toList() .toList()
}.orEmpty() }.orEmpty()
} }
@@ -147,7 +147,7 @@ class MetadataPackageFragment(
} }
companion object { companion object {
val DOT_METADATA_FILE_EXTENSION = ".kotlin_metadata" const val METADATA_FILE_EXTENSION = "kotlin_metadata"
val METADATA_FILE_EXTENSION = "kotlin_metadata" const val DOT_METADATA_FILE_EXTENSION = ".$METADATA_FILE_EXTENSION"
} }
} }
@@ -86,8 +86,8 @@ class TypeDeserializer(
fun ProtoBuf.Type.collectAllArguments(): List<ProtoBuf.Type.Argument> = fun ProtoBuf.Type.collectAllArguments(): List<ProtoBuf.Type.Argument> =
argumentList + outerType(c.typeTable)?.collectAllArguments().orEmpty() argumentList + outerType(c.typeTable)?.collectAllArguments().orEmpty()
val arguments = proto.collectAllArguments().mapIndexed { index, proto -> val arguments = proto.collectAllArguments().mapIndexed { index, argumentProto ->
typeArgument(constructor.parameters.getOrNull(index), proto) typeArgument(constructor.parameters.getOrNull(index), argumentProto)
}.toList() }.toList()
val simpleType = if (Flags.SUSPEND_TYPE.get(proto.flags)) { val simpleType = if (Flags.SUSPEND_TYPE.get(proto.flags)) {
@@ -171,12 +171,10 @@ class TypeDeserializer(
// kotlin.suspend is still built with LV=1.2, thus it references old Continuation // kotlin.suspend is still built with LV=1.2, thus it references old Continuation
// And otherwise, once stdlib is compiled with 1.3 one may want to stay at LV=1.2 // And otherwise, once stdlib is compiled with 1.3 one may want to stay at LV=1.2
if (c.containingDeclaration.safeAs<CallableDescriptor>()?.fqNameOrNull() == KOTLIN_SUSPEND_BUILT_IN_FUNCTION_FQ_NAME) { if (c.containingDeclaration.safeAs<CallableDescriptor>()?.fqNameOrNull() == KOTLIN_SUSPEND_BUILT_IN_FUNCTION_FQ_NAME) {
transformRuntimeFunctionTypeToSuspendFunction(functionType, false)?.let { transformRuntimeFunctionTypeToSuspendFunction(functionType, false)?.let { oldSuspend ->
if (!it.isSuspendFunctionType) { if (oldSuspend.isSuspendFunctionType) return oldSuspend
transformRuntimeFunctionTypeToSuspendFunction(functionType, true)?.let { return it }
} else { transformRuntimeFunctionTypeToSuspendFunction(functionType, true)?.let { newSuspend -> return newSuspend }
return it
}
} }
} }
@@ -186,7 +184,7 @@ class TypeDeserializer(
} }
private fun typeParameterTypeConstructor(typeParameterId: Int): TypeConstructor? = private fun typeParameterTypeConstructor(typeParameterId: Int): TypeConstructor? =
typeParameterDescriptors.get(typeParameterId)?.typeConstructor ?: parent?.typeParameterTypeConstructor(typeParameterId) typeParameterDescriptors[typeParameterId]?.typeConstructor ?: parent?.typeParameterTypeConstructor(typeParameterId)
private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? { private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? {
val id = c.nameResolver.getClassId(fqNameIndex) val id = c.nameResolver.getClassId(fqNameIndex)
@@ -62,8 +62,10 @@ class DeserializationComponents(
versionRequirementTable: VersionRequirementTable, versionRequirementTable: VersionRequirementTable,
containerSource: DeserializedContainerSource? containerSource: DeserializedContainerSource?
): DeserializationContext = ): DeserializationContext =
DeserializationContext(this, nameResolver, descriptor, typeTable, versionRequirementTable, containerSource, DeserializationContext(
parentTypeDeserializer = null, typeParameters = listOf()) this, nameResolver, descriptor, typeTable, versionRequirementTable, containerSource,
parentTypeDeserializer = null, typeParameters = listOf()
)
} }
@@ -77,10 +79,12 @@ class DeserializationContext(
parentTypeDeserializer: TypeDeserializer?, parentTypeDeserializer: TypeDeserializer?,
typeParameters: List<ProtoBuf.TypeParameter> typeParameters: List<ProtoBuf.TypeParameter>
) { ) {
val typeDeserializer = TypeDeserializer(this, parentTypeDeserializer, typeParameters, val typeDeserializer: TypeDeserializer = TypeDeserializer(
"Deserializer for ${containingDeclaration.name}") this, parentTypeDeserializer, typeParameters,
"Deserializer for ${containingDeclaration.name}"
)
val memberDeserializer = MemberDeserializer(this) val memberDeserializer: MemberDeserializer = MemberDeserializer(this)
val storageManager: StorageManager get() = components.storageManager val storageManager: StorageManager get() = components.storageManager
@@ -89,7 +93,7 @@ class DeserializationContext(
typeParameterProtos: List<ProtoBuf.TypeParameter>, typeParameterProtos: List<ProtoBuf.TypeParameter>,
nameResolver: NameResolver = this.nameResolver, nameResolver: NameResolver = this.nameResolver,
typeTable: TypeTable = this.typeTable typeTable: TypeTable = this.typeTable
) = DeserializationContext( ): DeserializationContext = DeserializationContext(
components, nameResolver, descriptor, typeTable, versionRequirementTable, this.containerSource, components, nameResolver, descriptor, typeTable, versionRequirementTable, this.containerSource,
parentTypeDeserializer = this.typeDeserializer, typeParameters = typeParameterProtos parentTypeDeserializer = this.typeDeserializer, typeParameters = typeParameterProtos
) )
@@ -68,8 +68,7 @@ class DeserializedClassDescriptor(
override val annotations = override val annotations =
if (!Flags.HAS_ANNOTATIONS.get(classProto.flags)) { if (!Flags.HAS_ANNOTATIONS.get(classProto.flags)) {
Annotations.EMPTY Annotations.EMPTY
} } else NonEmptyDeserializedAnnotations(c.storageManager) {
else NonEmptyDeserializedAnnotations(c.storageManager) {
c.components.annotationAndConstantLoader.loadClassAnnotations(thisAsProtoContainer).toList() c.components.annotationAndConstantLoader.loadClassAnnotations(thisAsProtoContainer).toList()
} }
@@ -243,9 +242,18 @@ class DeserializedClassDescriptor(
generateFakeOverrides(name, fromSupertypes, descriptors) generateFakeOverrides(name, fromSupertypes, descriptors)
} }
private fun <D : CallableMemberDescriptor> generateFakeOverrides(name: Name, fromSupertypes: Collection<D>, result: MutableCollection<D>) { private fun <D : CallableMemberDescriptor> generateFakeOverrides(
name: Name,
fromSupertypes: Collection<D>,
result: MutableCollection<D>
) {
val fromCurrent = ArrayList<CallableMemberDescriptor>(result) val fromCurrent = ArrayList<CallableMemberDescriptor>(result)
OverridingUtil.generateOverridesInFunctionGroup(name, fromSupertypes, fromCurrent, classDescriptor, object : NonReportingOverrideStrategy() { OverridingUtil.generateOverridesInFunctionGroup(
name,
fromSupertypes,
fromCurrent,
classDescriptor,
object : NonReportingOverrideStrategy() {
override fun addFakeOverride(fakeOverride: CallableMemberDescriptor) { override fun addFakeOverride(fakeOverride: CallableMemberDescriptor) {
// TODO: report "cannot infer visibility" // TODO: report "cannot infer visibility"
OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, null) OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, null)
@@ -253,7 +261,10 @@ class DeserializedClassDescriptor(
result.add(fakeOverride as D) result.add(fakeOverride as D)
} }
override fun conflict(fromSuper: CallableMemberDescriptor, fromCurrent: CallableMemberDescriptor) { override fun conflict(
fromSuper: CallableMemberDescriptor,
fromCurrent: CallableMemberDescriptor
) {
// TODO report conflicts // TODO report conflicts
} }
}) })
@@ -291,8 +302,7 @@ class DeserializedClassDescriptor(
private inner class EnumEntryClassDescriptors { private inner class EnumEntryClassDescriptors {
private val enumEntryProtos = classProto.enumEntryList.associateBy { c.nameResolver.getName(it.name) } private val enumEntryProtos = classProto.enumEntryList.associateBy { c.nameResolver.getName(it.name) }
private val enumEntryByName = c.storageManager.createMemoizedFunctionWithNullableValues<Name, ClassDescriptor> { private val enumEntryByName = c.storageManager.createMemoizedFunctionWithNullableValues<Name, ClassDescriptor> { name ->
name ->
enumEntryProtos[name]?.let { proto -> enumEntryProtos[name]?.let { proto ->
EnumEntrySyntheticClassDescriptor.create( EnumEntrySyntheticClassDescriptor.create(
@@ -72,7 +72,8 @@ class DeserializedSimpleFunctionDescriptor(
) : DeserializedCallableMemberDescriptor, ) : DeserializedCallableMemberDescriptor,
SimpleFunctionDescriptorImpl( SimpleFunctionDescriptorImpl(
containingDeclaration, original, annotations, name, kind, containingDeclaration, original, annotations, name, kind,
source ?: SourceElement.NO_SOURCE) { source ?: SourceElement.NO_SOURCE
) {
override fun createSubstitutedCopy( override fun createSubstitutedCopy(
newOwner: DeclarationDescriptor, newOwner: DeclarationDescriptor,
@@ -185,7 +186,7 @@ class DeserializedTypeAliasDescriptor(
override lateinit var underlyingType: SimpleType private set override lateinit var underlyingType: SimpleType private set
override lateinit var expandedType: SimpleType private set override lateinit var expandedType: SimpleType private set
private lateinit var typeConstructorParameters: List<TypeParameterDescriptor> private lateinit var typeConstructorParameters: List<TypeParameterDescriptor>
private lateinit var defaultTypeImpl: SimpleType private set private lateinit var defaultTypeImpl: SimpleType
fun initialize( fun initialize(
declaredTypeParameters: List<TypeParameterDescriptor>, declaredTypeParameters: List<TypeParameterDescriptor>,
@@ -203,31 +204,22 @@ class DeserializedTypeAliasDescriptor(
override val classDescriptor: ClassDescriptor? override val classDescriptor: ClassDescriptor?
get() = if (expandedType.isError) null else expandedType.constructor.declarationDescriptor as? ClassDescriptor get() = if (expandedType.isError) null else expandedType.constructor.declarationDescriptor as? ClassDescriptor
override fun getDefaultType(): SimpleType = override fun getDefaultType(): SimpleType = defaultTypeImpl
defaultTypeImpl
override fun substitute(substitutor: TypeSubstitutor): TypeAliasDescriptor { override fun substitute(substitutor: TypeSubstitutor): TypeAliasDescriptor {
if (substitutor.isEmpty) return this if (substitutor.isEmpty) return this
val substituted = DeserializedTypeAliasDescriptor( val substituted = DeserializedTypeAliasDescriptor(
storageManager, storageManager, containingDeclaration, annotations, name, visibility,
containingDeclaration, proto, nameResolver, typeTable, versionRequirementTable, containerSource
annotations,
name,
visibility,
proto,
nameResolver,
typeTable,
versionRequirementTable,
containerSource
) )
substituted.initialize(declaredTypeParameters, substituted.initialize(
declaredTypeParameters,
substitutor.safeSubstitute(underlyingType, Variance.INVARIANT).asSimpleType(), substitutor.safeSubstitute(underlyingType, Variance.INVARIANT).asSimpleType(),
substitutor.safeSubstitute(expandedType, Variance.INVARIANT).asSimpleType()) substitutor.safeSubstitute(expandedType, Variance.INVARIANT).asSimpleType()
)
return substituted return substituted
} }
override fun getTypeConstructorTypeParameters(): List<TypeParameterDescriptor> = override fun getTypeConstructorTypeParameters(): List<TypeParameterDescriptor> = typeConstructorParameters
typeConstructorParameters
} }
@@ -39,14 +39,16 @@ open class DeserializedPackageMemberScope(
components: DeserializationComponents, components: DeserializationComponents,
classNames: () -> Collection<Name> classNames: () -> Collection<Name>
) : DeserializedMemberScope( ) : DeserializedMemberScope(
components.createContext(packageDescriptor, nameResolver, TypeTable(proto.typeTable), components.createContext(
VersionRequirementTable.create(proto.versionRequirementTable), containerSource), packageDescriptor, nameResolver, TypeTable(proto.typeTable),
VersionRequirementTable.create(proto.versionRequirementTable), containerSource
),
proto.functionList, proto.propertyList, proto.typeAliasList, classNames proto.functionList, proto.propertyList, proto.typeAliasList, classNames
) { ) {
private val packageFqName = packageDescriptor.fqName private val packageFqName = packageDescriptor.fqName
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) =
= computeDescriptors(kindFilter, nameFilter, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS) + computeDescriptors(kindFilter, nameFilter, NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS) +
c.components.fictitiousClassDescriptorFactories.flatMap { it.getAllContributedClassesIfPossible(packageFqName) } c.components.fictitiousClassDescriptorFactories.flatMap { it.getAllContributedClassesIfPossible(packageFqName) }
override fun hasClass(name: Name) = override fun hasClass(name: Name) =
@@ -54,7 +56,6 @@ open class DeserializedPackageMemberScope(
override fun createClassId(name: Name) = ClassId(packageFqName, name) override fun createClassId(name: Name) = ClassId(packageFqName, name)
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? { override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? {
recordLookup(name, location) recordLookup(name, location)
return super.getContributedClassifier(name, location) return super.getContributedClassifier(name, location)
@@ -51,5 +51,6 @@ class DeserializedTypeParameterDescriptor(
} }
override fun reportSupertypeLoopError(type: KotlinType) = throw IllegalStateException( override fun reportSupertypeLoopError(type: KotlinType) = throw IllegalStateException(
"There should be no cycles for deserialized type parameters, but found for: $this") "There should be no cycles for deserialized type parameters, but found for: $this"
)
} }