Primary constructors supported

This commit is contained in:
Andrey Breslav
2013-05-16 20:21:05 +04:00
committed by Alexander Udalov
parent 99d1a9b7d7
commit 2e978950ab
3 changed files with 54 additions and 8 deletions
@@ -22,10 +22,7 @@ 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.PropertyDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.SimpleFunctionDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.ValueParameterDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.*;
import org.jetbrains.jet.lang.types.Variance;
import java.util.ArrayList;
@@ -91,7 +88,7 @@ public class DescriptorDeserializer {
case VAR:
return loadProperty(proto);
case CONSTRUCTOR:
break;
return loadConstructor(proto);
}
throw new IllegalArgumentException("Unsupported callable kind: " + callableKind);
}
@@ -148,6 +145,25 @@ public class DescriptorDeserializer {
return function;
}
@NotNull
private CallableMemberDescriptor loadConstructor(@NotNull Callable proto) {
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
ConstructorDescriptorImpl descriptor = new ConstructorDescriptorImpl(
classDescriptor,
// TODO
Collections.<AnnotationDescriptor>emptyList(),
// TODO: primary
true);
DescriptorDeserializer local = new DescriptorDeserializer(this, descriptor, nameResolver);
descriptor.initialize(
classDescriptor.getTypeConstructor().getParameters(),
local.valueParameters(proto.getValueParametersList()),
visibility(Flags.getVisibility(proto.getFlags())),
!classDescriptor.isInner()
);
return descriptor;
}
private static CallableMemberDescriptor.Kind memberKind(Callable.MemberKind memberKind) {
switch (memberKind) {
case DECLARATION:
@@ -73,7 +73,13 @@ public class DescriptorSerializer {
}
// TODO: nested classes
// TODO: constructor
ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
if (primaryConstructor != null) {
builder.setPrimaryConstructor(local.callableProto(primaryConstructor));
}
// TODO: other constructors
// TODO: class object
for (DeclarationDescriptor descriptor : classDescriptor.getDefaultType().getMemberScope().getAllDescriptors()) {
@@ -25,6 +25,8 @@ import org.jetbrains.jet.lang.descriptors.impl.ClassDescriptorBase;
import org.jetbrains.jet.lang.descriptors.impl.ReceiverParameterDescriptorImpl;
import org.jetbrains.jet.lang.resolve.OverrideResolver;
import org.jetbrains.jet.lang.resolve.TraceUtil;
import org.jetbrains.jet.lang.resolve.lazy.storage.NullableLazyValue;
import org.jetbrains.jet.lang.resolve.lazy.storage.NullableLazyValueImpl;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ClassReceiver;
@@ -41,6 +43,8 @@ public class DeserializedClassDescriptor extends ClassDescriptorBase implements
private final DeserializedMemberScope memberScope;
private final ReceiverParameterDescriptor thisAsReceiverParameter;
private final NullableLazyValue<ConstructorDescriptor> primaryConstructor;
private final Name name;
private final DeclarationDescriptor containingDeclaration;
private final DeserializedClassTypeConstructor typeConstructor;
@@ -71,6 +75,13 @@ public class DeserializedClassDescriptor extends ClassDescriptorBase implements
this.visibility = DescriptorDeserializer.visibility(Flags.getVisibility(flags));
this.kind = DescriptorDeserializer.classKind(Flags.getClassKind(flags));
this.isInner = Flags.isInner(flags);
this.primaryConstructor = new NullableLazyValueImpl<ConstructorDescriptor>() {
@Override
protected ConstructorDescriptor doCompute() {
return computePrimaryConstructor();
}
};
}
@NotNull
@@ -137,16 +148,29 @@ public class DeserializedClassDescriptor extends ClassDescriptorBase implements
return JetScope.EMPTY; // TODO inner classes
}
@Nullable
private ConstructorDescriptor computePrimaryConstructor() {
if (!classProto.hasPrimaryConstructor()) return null;
ProtoBuf.Callable constructorProto = classProto.getPrimaryConstructor();
return (ConstructorDescriptor) deserializer.loadCallable(constructorProto);
}
@Nullable
@Override
public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
return null; // TODO
return primaryConstructor.compute();
}
@NotNull
@Override
public Collection<ConstructorDescriptor> getConstructors() {
return Collections.emptyList(); // TODO
ConstructorDescriptor constructor = getUnsubstitutedPrimaryConstructor();
if (constructor == null) {
return Collections.emptyList();
}
// TODO: other constructors
return Collections.singletonList(constructor);
}
@Nullable