Simplify type deserializer construction even more

This commit is contained in:
Alexander Udalov
2014-11-22 20:19:40 +03:00
parent 2b189c5193
commit ab6fec1540
4 changed files with 38 additions and 55 deletions
@@ -26,7 +26,6 @@ import org.jetbrains.jet.lang.descriptors.impl.*
import org.jetbrains.jet.lang.resolve.DescriptorFactory
import org.jetbrains.jet.descriptors.serialization.ProtoBuf.Callable
import org.jetbrains.jet.descriptors.serialization.ProtoBuf.Callable.CallableKind.*
import org.jetbrains.jet.descriptors.serialization.ProtoBuf.TypeParameter
public class MemberDeserializer(private val context: DeserializationContext) {
private val components: DeserializationComponents get() = context.components
@@ -173,16 +172,6 @@ public class MemberDeserializer(private val context: DeserializationContext) {
Annotations.EMPTY
}
public fun typeParameters(protos: List<TypeParameter>): List<DeserializedTypeParameterDescriptor> {
return protos.withIndices().map { val (i, proto) = it
DeserializedTypeParameterDescriptor(
components.storageManager, context.typeDeserializer, proto, context.containingDeclaration,
context.nameResolver.getName(proto.getName()),
variance(proto.getVariance()), proto.getReified(), i
)
}
}
private fun valueParameters(callable: Callable, kind: AnnotatedCallableKind): List<ValueParameterDescriptor> {
val containerOfCallable = context.containingDeclaration.getContainingDeclaration().asClassOrPackage()
@@ -28,19 +28,27 @@ import java.util.LinkedHashMap
public class TypeDeserializer(
private val context: DeserializationContext,
private val parent: TypeDeserializer?,
private val typeParameterProtos: List<ProtoBuf.TypeParameter>,
private val debugName: String
) {
private val typeParameterDescriptors = LinkedHashMap<Int, TypeParameterDescriptor>()
private val classDescriptors: (Int) -> ClassDescriptor? = context.components.storageManager.createMemoizedFunctionWithNullableValues {
fqNameIndex -> computeClassDescriptor(fqNameIndex)
}
fun addTypeParameter(typeParameterDescriptor: DeserializedTypeParameterDescriptor) {
typeParameterDescriptors.put(typeParameterDescriptor.getProtoId(), typeParameterDescriptor)
private val typeParameterDescriptors = context.components.storageManager.createLazyValue {
if (typeParameterProtos.isEmpty()) {
mapOf<Int, TypeParameterDescriptor>()
}
else {
val result = LinkedHashMap<Int, TypeParameterDescriptor>()
for ((index, proto) in typeParameterProtos.withIndices()) {
result[proto.getId()] = DeserializedTypeParameterDescriptor(context, proto, index)
}
result
}
}
fun getOwnTypeParameters(): List<TypeParameterDescriptor> = typeParameterDescriptors.values().toReadOnlyList()
fun getOwnTypeParameters(): List<TypeParameterDescriptor> = typeParameterDescriptors().values().toReadOnlyList()
fun type(proto: ProtoBuf.Type): JetType {
if (proto.hasFlexibleTypeCapabilitiesId()) {
@@ -79,7 +87,7 @@ public class TypeDeserializer(
}
private fun typeParameterTypeConstructor(proto: ProtoBuf.Type.Constructor): TypeConstructor? =
typeParameterDescriptors.get(proto.getId())?.getTypeConstructor() ?:
typeParameterDescriptors().get(proto.getId())?.getTypeConstructor() ?:
parent?.typeParameterTypeConstructor(proto)
private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? =
@@ -37,7 +37,7 @@ public class DeserializationComponents(
public fun deserializeClass(classId: ClassId): ClassDescriptor? = classDeserializer.deserializeClass(classId)
public fun createContext(descriptor: PackageFragmentDescriptor, nameResolver: NameResolver): DeserializationContext =
DeserializationContext(this, nameResolver, descriptor, parentTypeDeserializer = null)
DeserializationContext(this, nameResolver, descriptor, parentTypeDeserializer = null, typeParameters = listOf())
}
@@ -45,24 +45,19 @@ public class DeserializationContext(
public val components: DeserializationComponents,
public val nameResolver: NameResolver,
public val containingDeclaration: DeclarationDescriptor,
parentTypeDeserializer: TypeDeserializer?
parentTypeDeserializer: TypeDeserializer?,
typeParameters: List<ProtoBuf.TypeParameter>
) {
val typeDeserializer: TypeDeserializer =
TypeDeserializer(this, parentTypeDeserializer, "Deserializer for ${containingDeclaration.getName()}")
val typeDeserializer = TypeDeserializer(this, parentTypeDeserializer, typeParameters,
"Deserializer for ${containingDeclaration.getName()}")
val memberDeserializer: MemberDeserializer = MemberDeserializer(this)
val memberDeserializer = MemberDeserializer(this)
fun childContext(
descriptor: DeclarationDescriptor,
typeParameterProtos: List<ProtoBuf.TypeParameter>,
nameResolver: NameResolver = this.nameResolver
): DeserializationContext {
val child = DeserializationContext(components, nameResolver, descriptor, parentTypeDeserializer = this.typeDeserializer)
for (typeParameter in child.memberDeserializer.typeParameters(typeParameterProtos)) {
child.typeDeserializer.addTypeParameter(typeParameter)
}
return child
}
) = DeserializationContext(
components, nameResolver, descriptor, parentTypeDeserializer = this.typeDeserializer, typeParameters = typeParameterProtos
)
}
@@ -18,53 +18,44 @@ package org.jetbrains.jet.descriptors.serialization.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.descriptors.serialization.SerializationPackage;
import org.jetbrains.jet.descriptors.serialization.TypeDeserializer;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.descriptors.serialization.context.DeserializationContext;
import org.jetbrains.jet.lang.descriptors.SourceElement;
import org.jetbrains.jet.lang.descriptors.impl.AbstractLazyTypeParameterDescriptor;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.Variance;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.storage.StorageManager;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
public class DeserializedTypeParameterDescriptor extends AbstractLazyTypeParameterDescriptor {
private final ProtoBuf.TypeParameter proto;
private final TypeDeserializer typeDeserializer;
public DeserializedTypeParameterDescriptor(
@NotNull StorageManager storageManager,
@NotNull TypeDeserializer typeDeserializer,
@NotNull ProtoBuf.TypeParameter proto,
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Name name,
@NotNull Variance variance,
boolean isReified,
int index
) {
super(storageManager, containingDeclaration, name, variance, isReified, index, SourceElement.NO_SOURCE);
public DeserializedTypeParameterDescriptor(@NotNull DeserializationContext context, @NotNull ProtoBuf.TypeParameter proto, int index) {
super(context.getComponents().getStorageManager(),
context.getContainingDeclaration(),
context.getNameResolver().getName(proto.getName()),
SerializationPackage.variance(proto.getVariance()),
proto.getReified(),
index,
SourceElement.NO_SOURCE);
this.proto = proto;
this.typeDeserializer = typeDeserializer;
this.typeDeserializer = context.getTypeDeserializer();
}
@NotNull
@Override
protected Set<JetType> resolveUpperBounds() {
if (proto.getUpperBoundCount() == 0) {
return Collections.singleton(KotlinBuiltIns.getInstance().getDefaultBound());
}
Set<JetType> result = new LinkedHashSet<JetType>(proto.getUpperBoundCount());
for (ProtoBuf.Type upperBound : proto.getUpperBoundList()) {
result.add(typeDeserializer.type(upperBound));
}
if (result.isEmpty()) {
result.add(KotlinBuiltIns.getInstance().getDefaultBound());
}
return result;
}
public int getProtoId() {
return proto.getId();
}
}