Using lazy type parameter descriptors

This commit is contained in:
Andrey Breslav
2013-06-18 17:24:34 +04:00
committed by Alexander Udalov
parent 37707e51a2
commit fd3f2d93be
2 changed files with 84 additions and 28 deletions
@@ -19,17 +19,18 @@ package org.jetbrains.jet.descriptors.serialization;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.descriptors.serialization.descriptors.AnnotationDeserializer;
import org.jetbrains.jet.descriptors.serialization.descriptors.DeserializedTypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.Modality;
import org.jetbrains.jet.lang.descriptors.Visibility;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.*;
import org.jetbrains.jet.lang.resolve.DescriptorResolver;
import org.jetbrains.jet.lang.resolve.lazy.storage.LockBasedStorageManager;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
import org.jetbrains.jet.lang.types.Variance;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
import static org.jetbrains.jet.descriptors.serialization.ProtoBuf.*;
@@ -61,6 +62,8 @@ public class DescriptorDeserializer {
private final TypeDeserializer typeDeserializer;
private final AnnotationDeserializer annotationDeserializer;
private final StorageManager storageManager = new LockBasedStorageManager();
private DescriptorDeserializer(
@NotNull TypeDeserializer typeDeserializer,
@NotNull DeclarationDescriptor containingDeclaration,
@@ -288,44 +291,32 @@ public class DescriptorDeserializer {
@NotNull
public List<TypeParameterDescriptor> typeParameters(@NotNull List<TypeParameter> protos) {
List<TypeParameterDescriptorImpl> result = new ArrayList<TypeParameterDescriptorImpl>(protos.size());
List<TypeParameterDescriptor> result = new ArrayList<TypeParameterDescriptor>(protos.size());
for (int i = 0; i < protos.size(); i++) {
TypeParameter proto = protos.get(i);
TypeParameterDescriptorImpl descriptor = typeParameter(proto, i);
DeserializedTypeParameterDescriptor descriptor = typeParameter(proto, i);
result.add(descriptor);
}
// Account for circular bounds:
for (int i = 0; i < protos.size(); i++) {
TypeParameter proto = protos.get(i);
TypeParameterDescriptorImpl descriptor = result.get(i);
addTypeParameterBounds(proto, descriptor);
}
//noinspection unchecked
return (List) result;
return result;
}
private TypeParameterDescriptorImpl typeParameter(TypeParameter proto, int index) {
@NotNull
private DeserializedTypeParameterDescriptor typeParameter(TypeParameter proto, int index) {
int id = proto.getId();
TypeParameterDescriptorImpl descriptor = TypeParameterDescriptorImpl.createForFurtherModification(
DeserializedTypeParameterDescriptor descriptor = new DeserializedTypeParameterDescriptor(
storageManager,
typeDeserializer,
proto,
containingDeclaration,
// TODO (type parameter annotations are not supported in Java 7)
Collections.<AnnotationDescriptor>emptyList(),
proto.getReified(),
variance(proto.getVariance()),
nameResolver.getName(proto.getName()),
index);
variance(proto.getVariance()),
proto.getReified(),
index
);
typeDeserializer.registerTypeParameter(id, descriptor);
return descriptor;
}
private void addTypeParameterBounds(TypeParameter proto, TypeParameterDescriptorImpl descriptor) {
for (Type upperBound : proto.getUpperBoundsList()) {
descriptor.addUpperBound(typeDeserializer.type(upperBound));
}
descriptor.addDefaultUpperBound();
descriptor.setInitialized();
}
private static Variance variance(TypeParameter.Variance proto) {
switch (proto) {
case IN:
@@ -0,0 +1,65 @@
/*
* Copyright 2010-2013 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.jet.descriptors.serialization.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.descriptors.serialization.ProtoBuf;
import org.jetbrains.jet.descriptors.serialization.TypeDeserializer;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.resolve.lazy.descriptors.AbstractLazyTypeParameterDescriptor;
import org.jetbrains.jet.lang.resolve.lazy.storage.StorageManager;
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 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);
this.proto = proto;
this.typeDeserializer = typeDeserializer;
}
@NotNull
@Override
protected Set<JetType> resolveUpperBounds() {
Set<JetType> result = new LinkedHashSet<JetType>(proto.getUpperBoundsCount());
for (ProtoBuf.Type upperBound : proto.getUpperBoundsList()) {
result.add(typeDeserializer.type(upperBound));
}
if (result.isEmpty()) {
result.add(KotlinBuiltIns.getInstance().getDefaultBound());
}
return result;
}
}