Improve code after conversion, fix some warnings

This commit is contained in:
Alexander Udalov
2014-11-21 04:56:38 +03:00
parent d3635de205
commit a4d4d15ca9
4 changed files with 30 additions and 50 deletions
@@ -27,7 +27,6 @@ import org.jetbrains.jet.lang.resolve.DescriptorFactory
import org.jetbrains.jet.descriptors.serialization.ProtoBuf.Callable import org.jetbrains.jet.descriptors.serialization.ProtoBuf.Callable
import org.jetbrains.jet.descriptors.serialization.ProtoBuf.Callable.CallableKind.* import org.jetbrains.jet.descriptors.serialization.ProtoBuf.Callable.CallableKind.*
import org.jetbrains.jet.descriptors.serialization.ProtoBuf.TypeParameter import org.jetbrains.jet.descriptors.serialization.ProtoBuf.TypeParameter
import java.util.ArrayList
public class MemberDeserializer(private val context: DeserializationContext) { public class MemberDeserializer(private val context: DeserializationContext) {
private val components: DeserializationComponents get() = context.components private val components: DeserializationComponents get() = context.components
@@ -62,7 +61,7 @@ public class MemberDeserializer(private val context: DeserializationContext) {
local.typeDeserializer.type(proto.getReturnType()), local.typeDeserializer.type(proto.getReturnType()),
local.typeDeserializer.getOwnTypeParameters(), local.typeDeserializer.getOwnTypeParameters(),
getDispatchReceiverParameter(), getDispatchReceiverParameter(),
local.typeDeserializer.typeOrNull(if (proto.hasReceiverType()) proto.getReceiverType() else null) if (proto.hasReceiverType()) local.typeDeserializer.type(proto.getReceiverType()) else null
) )
val getter = if (Flags.HAS_GETTER.get(flags)) { val getter = if (Flags.HAS_GETTER.get(flags)) {
@@ -104,7 +103,6 @@ public class MemberDeserializer(private val context: DeserializationContext) {
) )
val setterLocal = local.childContext(setter, listOf()) val setterLocal = local.childContext(setter, listOf())
val valueParameters = setterLocal.memberDeserializer.valueParameters(proto, AnnotatedCallableKind.PROPERTY_SETTER) val valueParameters = setterLocal.memberDeserializer.valueParameters(proto, AnnotatedCallableKind.PROPERTY_SETTER)
assert(valueParameters.size() == 1) { "Property setter should have a single value parameter: $setter" }
setter.initialize(valueParameters.single()) setter.initialize(valueParameters.single())
setter setter
} }
@@ -119,12 +117,8 @@ public class MemberDeserializer(private val context: DeserializationContext) {
if (Flags.HAS_CONSTANT.get(flags)) { if (Flags.HAS_CONSTANT.get(flags)) {
property.setCompileTimeInitializer( property.setCompileTimeInitializer(
components.storageManager.createNullableLazyValue { components.storageManager.createNullableLazyValue {
val containingDeclaration = val container = context.containingDeclaration.asClassOrPackage()
context.containingDeclaration as? ClassOrPackageFragmentDescriptor components.constantLoader.loadPropertyConstant(container, proto, context.nameResolver, AnnotatedCallableKind.PROPERTY)
?: error("Only members in classes or package fragments should be serialized: ${context.containingDeclaration}")
components.constantLoader.loadPropertyConstant(
containingDeclaration, proto, context.nameResolver, AnnotatedCallableKind.PROPERTY
)
} }
) )
} }
@@ -139,7 +133,7 @@ public class MemberDeserializer(private val context: DeserializationContext) {
val function = DeserializedSimpleFunctionDescriptor.create(context.containingDeclaration, proto, context.nameResolver, annotations) val function = DeserializedSimpleFunctionDescriptor.create(context.containingDeclaration, proto, context.nameResolver, annotations)
val local = context.childContext(function, proto.getTypeParameterList()) val local = context.childContext(function, proto.getTypeParameterList())
function.initialize( function.initialize(
local.typeDeserializer.typeOrNull(if (proto.hasReceiverType()) proto.getReceiverType() else null), if (proto.hasReceiverType()) local.typeDeserializer.type(proto.getReceiverType()) else null,
getDispatchReceiverParameter(), getDispatchReceiverParameter(),
local.typeDeserializer.getOwnTypeParameters(), local.typeDeserializer.getOwnTypeParameters(),
local.memberDeserializer.valueParameters(proto, AnnotatedCallableKind.FUNCTION), local.memberDeserializer.valueParameters(proto, AnnotatedCallableKind.FUNCTION),
@@ -171,48 +165,38 @@ public class MemberDeserializer(private val context: DeserializationContext) {
} }
private fun getAnnotations(proto: Callable, flags: Int, kind: AnnotatedCallableKind): Annotations { private fun getAnnotations(proto: Callable, flags: Int, kind: AnnotatedCallableKind): Annotations {
val containingDeclaration =
context.containingDeclaration as? ClassOrPackageFragmentDescriptor
?: error("Only members in classes or package fragments should be serialized: ${context.containingDeclaration}")
return if (Flags.HAS_ANNOTATIONS.get(flags)) return if (Flags.HAS_ANNOTATIONS.get(flags))
components.annotationLoader.loadCallableAnnotations(containingDeclaration, proto, context.nameResolver, kind) components.annotationLoader.loadCallableAnnotations(
context.containingDeclaration.asClassOrPackage(), proto, context.nameResolver, kind
)
else else
Annotations.EMPTY Annotations.EMPTY
} }
public fun typeParameters(protos: List<TypeParameter>): List<DeserializedTypeParameterDescriptor> { public fun typeParameters(protos: List<TypeParameter>): List<DeserializedTypeParameterDescriptor> {
val result = ArrayList<DeserializedTypeParameterDescriptor>(protos.size()) return protos.withIndices().map { val (i, proto) = it
for (i in protos.indices) { DeserializedTypeParameterDescriptor(
val proto = protos.get(i)
result.add(DeserializedTypeParameterDescriptor(
components.storageManager, context.typeDeserializer, proto, context.containingDeclaration, components.storageManager, context.typeDeserializer, proto, context.containingDeclaration,
context.nameResolver.getName(proto.getName()), context.nameResolver.getName(proto.getName()),
variance(proto.getVariance()), proto.getReified(), i variance(proto.getVariance()), proto.getReified(), i
)) )
} }
return result
} }
private fun valueParameters(callable: Callable, kind: AnnotatedCallableKind): List<ValueParameterDescriptor> { private fun valueParameters(callable: Callable, kind: AnnotatedCallableKind): List<ValueParameterDescriptor> {
val classOrPackage = val containerOfCallable = context.containingDeclaration.getContainingDeclaration().asClassOrPackage()
context.containingDeclaration.getContainingDeclaration() as? ClassOrPackageFragmentDescriptor
?: error("Only members in classes or package fragments should be serialized: ${context.containingDeclaration}")
val protos = callable.getValueParameterList() return callable.getValueParameterList().withIndices().map { val (i, proto) = it
val result = ArrayList<ValueParameterDescriptor>(protos.size()) ValueParameterDescriptorImpl(
for (i in protos.indices) {
val proto = protos.get(i)
result.add(ValueParameterDescriptorImpl(
context.containingDeclaration, null, i, context.containingDeclaration, null, i,
getParameterAnnotations(classOrPackage, callable, kind, proto), getParameterAnnotations(containerOfCallable, callable, kind, proto),
context.nameResolver.getName(proto.getName()), context.nameResolver.getName(proto.getName()),
context.typeDeserializer.type(proto.getType()), context.typeDeserializer.type(proto.getType()),
Flags.DECLARES_DEFAULT_VALUE.get(proto.getFlags()), Flags.DECLARES_DEFAULT_VALUE.get(proto.getFlags()),
context.typeDeserializer.typeOrNull(if (proto.hasVarargElementType()) proto.getVarargElementType() else null), if (proto.hasVarargElementType()) context.typeDeserializer.type(proto.getVarargElementType()) else null,
SourceElement.NO_SOURCE SourceElement.NO_SOURCE
)) )
} }
return result
} }
private fun getParameterAnnotations( private fun getParameterAnnotations(
@@ -226,4 +210,8 @@ public class MemberDeserializer(private val context: DeserializationContext) {
else else
Annotations.EMPTY Annotations.EMPTY
} }
private fun DeclarationDescriptor.asClassOrPackage(): ClassOrPackageFragmentDescriptor =
this as? ClassOrPackageFragmentDescriptor
?: error("Only members in classes or package fragments should be serialized: $this")
} }
@@ -76,14 +76,6 @@ public class TypeDeserializer {
return UtilsPackage.toReadOnlyList(typeParameterDescriptors.values()); return UtilsPackage.toReadOnlyList(typeParameterDescriptors.values());
} }
@Nullable
public JetType typeOrNull(@Nullable ProtoBuf.Type proto) {
if (proto == null) {
return null;
}
return type(proto);
}
@NotNull @NotNull
public JetType type(@NotNull ProtoBuf.Type proto) { public JetType type(@NotNull ProtoBuf.Type proto) {
if (proto.hasFlexibleTypeCapabilitiesId()) { if (proto.hasFlexibleTypeCapabilitiesId()) {
@@ -169,7 +161,12 @@ public class TypeDeserializer {
TypeParameterDescriptor typeParameterDescriptor = (TypeParameterDescriptor) descriptor; TypeParameterDescriptor typeParameterDescriptor = (TypeParameterDescriptor) descriptor;
return typeParameterDescriptor.getDefaultType().getMemberScope(); return typeParameterDescriptor.getDefaultType().getMemberScope();
} }
return ((ClassDescriptor) descriptor).getMemberScope(typeArguments); else if (descriptor instanceof ClassDescriptor) {
return ((ClassDescriptor) descriptor).getMemberScope(typeArguments);
}
else {
throw new IllegalStateException("Unsupported classifier: " + descriptor);
}
} }
@Override @Override
@@ -17,10 +17,9 @@
package org.jetbrains.jet.descriptors.serialization.context package org.jetbrains.jet.descriptors.serialization.context
import org.jetbrains.jet.storage.StorageManager import org.jetbrains.jet.storage.StorageManager
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationLoader
import org.jetbrains.jet.lang.descriptors.* import org.jetbrains.jet.lang.descriptors.*
import org.jetbrains.jet.descriptors.serialization.* import org.jetbrains.jet.descriptors.serialization.*
import org.jetbrains.jet.descriptors.serialization.ProtoBuf.TypeParameter import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationLoader
import org.jetbrains.jet.descriptors.serialization.descriptors.ConstantLoader import org.jetbrains.jet.descriptors.serialization.descriptors.ConstantLoader
import org.jetbrains.jet.lang.resolve.name.ClassId import org.jetbrains.jet.lang.resolve.name.ClassId
@@ -53,14 +52,10 @@ public class DeserializationContext(
val memberDeserializer: MemberDeserializer = MemberDeserializer(this) val memberDeserializer: MemberDeserializer = MemberDeserializer(this)
// Not using default arguments here because Java code calls this function
fun childContext(descriptor: DeclarationDescriptor, typeParameterProtos: List<TypeParameter>): DeserializationContext =
childContext(descriptor, typeParameterProtos, this.nameResolver)
fun childContext( fun childContext(
descriptor: DeclarationDescriptor, descriptor: DeclarationDescriptor,
typeParameterProtos: List<TypeParameter>, typeParameterProtos: List<ProtoBuf.TypeParameter>,
nameResolver: NameResolver nameResolver: NameResolver = this.nameResolver
): DeserializationContext { ): DeserializationContext {
val child = DeserializationContext(components, nameResolver, descriptor, parentTypeDeserializer = this.typeDeserializer) val child = DeserializationContext(components, nameResolver, descriptor, parentTypeDeserializer = this.typeDeserializer)
@@ -133,7 +133,7 @@ public class DeserializedClassDescriptor(
private fun computeSuperTypes(): Collection<JetType> { private fun computeSuperTypes(): Collection<JetType> {
val supertypes = ArrayList<JetType>(classProto.getSupertypeCount()) val supertypes = ArrayList<JetType>(classProto.getSupertypeCount())
for (supertype in classProto.getSupertypeList()) { for (supertype in classProto.getSupertypeList()) {
supertypes.add(context.typeDeserializer.`type`(supertype)) supertypes.add(context.typeDeserializer.type(supertype))
} }
return supertypes return supertypes
} }