Cleanup ClassDescriptorImpl & ErrorClassDescriptor

ClassDescriptorImpl now extends ClassDescriptorBase and reuses most of its
functionality. ErrorClassDescriptor now is initialized in its own constructor.
Inline trivial arguments everywhere
This commit is contained in:
Alexander Udalov
2013-11-20 20:14:00 +04:00
parent 62068bf741
commit a77d6a006d
8 changed files with 73 additions and 193 deletions
@@ -25,7 +25,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.Type;
import org.jetbrains.jet.codegen.SamCodegenUtil;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.ClassDescriptorImpl;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
@@ -93,21 +92,10 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
@NotNull
private ClassDescriptor recordClassForFunction(@NotNull FunctionDescriptor funDescriptor, @NotNull JetType superType) {
ClassDescriptor classDescriptor;
classDescriptor = new ClassDescriptorImpl(
funDescriptor.getContainingDeclaration(),
Collections.<AnnotationDescriptor>emptyList(),
Modality.FINAL,
Name.special("<closure>"));
((ClassDescriptorImpl)classDescriptor).initialize(
false,
Collections.<TypeParameterDescriptor>emptyList(),
Collections.singleton(superType),
JetScope.EMPTY,
Collections.<ConstructorDescriptor>emptySet(),
null,
false);
ClassDescriptorImpl classDescriptor =
new ClassDescriptorImpl(funDescriptor.getContainingDeclaration(), Name.special("<closure>"), Modality.FINAL,
Collections.singleton(superType));
classDescriptor.initialize(JetScope.EMPTY, Collections.<ConstructorDescriptor>emptySet(), null);
bindingTrace.record(CLASS_FOR_FUNCTION, funDescriptor, classDescriptor);
return classDescriptor;
@@ -22,7 +22,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.Type;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.ClassDescriptorImpl;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
@@ -141,19 +140,10 @@ public class CodegenBinding {
@NotNull ScriptDescriptor scriptDescriptor,
@NotNull Type asmType
) {
ClassDescriptorImpl classDescriptor = new ClassDescriptorImpl(
scriptDescriptor,
Collections.<AnnotationDescriptor>emptyList(),
Modality.FINAL,
Name.special("<script-" + asmType.getInternalName() + ">"));
classDescriptor.initialize(
false,
Collections.<TypeParameterDescriptor>emptyList(),
Collections.singletonList(KotlinBuiltIns.getInstance().getAnyType()),
JetScope.EMPTY,
Collections.<ConstructorDescriptor>emptySet(),
null,
false);
ClassDescriptorImpl classDescriptor =
new ClassDescriptorImpl(scriptDescriptor, Name.special("<script-" + asmType.getInternalName() + ">"), Modality.FINAL,
Collections.singleton(KotlinBuiltIns.getInstance().getAnyType()));
classDescriptor.initialize(JetScope.EMPTY, Collections.<ConstructorDescriptor>emptySet(), null);
recordClosure(bindingTrace, null, classDescriptor, null, asmType);
@@ -27,8 +27,8 @@ import org.jetbrains.jet.lang.resolve.DescriptorFactory;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.Variance;
import org.jetbrains.jet.lang.types.error.ErrorClassDescriptor;
import org.jetbrains.jet.storage.StorageManager;
import java.util.ArrayList;
@@ -207,7 +207,7 @@ public class DescriptorDeserializer {
if (objectClass == null) {
// if we are not able to find the class for object property
// then something bad has happened since they should be in the same jar
objectClass = ErrorUtils.createErrorClass(objectId.asSingleFqName().asString());
objectClass = new ErrorClassDescriptor(objectId.asSingleFqName().asString());
}
return new PropertyDescriptorForObjectImpl(containingDeclaration, annotations, visibility, name, objectClass);
}
@@ -145,14 +145,10 @@ public final class JavaPropertyResolver {
//TODO: this is a hack to indicate that this enum entry is an object
// class descriptor for enum entries is not used by backends so for now this should be safe to use
ClassDescriptorImpl dummyClassDescriptorForEnumEntryObject =
new ClassDescriptorImpl(owner, Collections.<AnnotationDescriptor>emptyList(), Modality.FINAL, propertyName);
dummyClassDescriptorForEnumEntryObject.initialize(
true,
Collections.<TypeParameterDescriptor>emptyList(),
Collections.<JetType>emptyList(), JetScope.EMPTY,
Collections.<ConstructorDescriptor>emptySet(), null,
false);
return new JavaPropertyDescriptorForObject(owner, annotations, visibility, propertyName, dummyClassDescriptorForEnumEntryObject);
new ClassDescriptorImpl(owner, propertyName, Modality.FINAL, Collections.<JetType>emptyList());
dummyClassDescriptorForEnumEntryObject.initialize(JetScope.EMPTY, Collections.<ConstructorDescriptor>emptySet(), null);
return new JavaPropertyDescriptorForObject(owner, annotations, visibility, propertyName,
dummyClassDescriptorForEnumEntryObject);
}
return new JavaPropertyDescriptor(owner, annotations, visibility, isVar, propertyName);
@@ -64,21 +64,11 @@ public class ScriptDescriptor extends DeclarationDescriptorNonRootImpl {
super(containingDeclaration, Collections.<AnnotationDescriptor>emptyList(), NAME);
this.priority = priority;
classDescriptor = new ClassDescriptorImpl(
containingDeclaration,
Collections.<AnnotationDescriptor>emptyList(),
Modality.FINAL,
className);
classDescriptor = new ClassDescriptorImpl(containingDeclaration, className, Modality.FINAL,
Collections.singleton(KotlinBuiltIns.getInstance().getAnyType()));
classScope = new WritableScopeImpl(scriptScope, containingDeclaration, RedeclarationHandler.DO_NOTHING, "script members");
classScope.changeLockLevel(WritableScope.LockLevel.BOTH);
classDescriptor.initialize(
false,
Collections.<TypeParameterDescriptor>emptyList(),
Collections.singletonList(KotlinBuiltIns.getInstance().getAnyType()),
classScope,
new HashSet<ConstructorDescriptor>(),
null,
false);
classDescriptor.initialize(classScope, new HashSet<ConstructorDescriptor>(), null);
}
public void initialize(
@@ -16,6 +16,7 @@
package org.jetbrains.jet.lang.descriptors.impl;
import jet.Function0;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
@@ -23,70 +24,64 @@ import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorFactory;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.SubstitutingScope;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeConstructor;
import org.jetbrains.jet.lang.types.TypeConstructorImpl;
import org.jetbrains.jet.storage.LockBasedStorageManager;
import org.jetbrains.jet.storage.NotNullLazyValue;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ClassDescriptorImpl extends DeclarationDescriptorNonRootImpl implements ClassDescriptor {
private TypeConstructor typeConstructor;
public class ClassDescriptorImpl extends ClassDescriptorBase {
private final Modality modality;
private final TypeConstructor typeConstructor;
private final NotNullLazyValue<ReceiverParameterDescriptor> thisAsReceiverParameter;
private JetScope memberDeclarations;
private JetScope scopeForMemberLookup;
private Set<ConstructorDescriptor> constructors;
private ConstructorDescriptor primaryConstructor;
private ReceiverParameterDescriptor thisAsReceiverParameter;
private final Modality modality;
private ClassDescriptor classObjectDescriptor;
private final ClassKind kind;
private boolean isInner;
public ClassDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull List<AnnotationDescriptor> annotations,
@NotNull Modality modality,
@NotNull Name name
) {
this(containingDeclaration, ClassKind.CLASS, annotations, modality, name);
}
public ClassDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull ClassKind kind,
@NotNull List<AnnotationDescriptor> annotations,
@NotNull Name name,
@NotNull Modality modality,
@NotNull Name name) {
super(containingDeclaration, annotations, name);
this.kind = kind;
@NotNull Collection<JetType> supertypes
) {
super(LockBasedStorageManager.NO_LOCKS, containingDeclaration, name);
this.modality = modality;
this.typeConstructor = new TypeConstructorImpl(this, Collections.<AnnotationDescriptor>emptyList(), false, getName().asString(),
Collections.<TypeParameterDescriptor>emptyList(), supertypes);
this.thisAsReceiverParameter = LockBasedStorageManager.NO_LOCKS.createLazyValue(new Function0<ReceiverParameterDescriptor>() {
@Override
public ReceiverParameterDescriptor invoke() {
return DescriptorFactory.createLazyReceiverParameterDescriptor(ClassDescriptorImpl.this);
}
});
}
public final ClassDescriptorImpl initialize(
boolean isFinal,
@NotNull List<? extends TypeParameterDescriptor> typeParameters,
@NotNull Collection<JetType> supertypes,
@NotNull JetScope memberDeclarations,
public final void initialize(
@NotNull JetScope scopeForMemberLookup,
@NotNull Set<ConstructorDescriptor> constructors,
@Nullable ConstructorDescriptor primaryConstructor,
boolean isInner
@Nullable ConstructorDescriptor primaryConstructor
) {
this.typeConstructor = new TypeConstructorImpl(this, getAnnotations(), isFinal, getName().asString(), typeParameters, supertypes);
this.memberDeclarations = memberDeclarations;
this.scopeForMemberLookup = scopeForMemberLookup;
this.constructors = constructors;
this.primaryConstructor = primaryConstructor;
this.isInner = isInner;
return this;
}
public void setPrimaryConstructor(@NotNull ConstructorDescriptor primaryConstructor) {
this.primaryConstructor = primaryConstructor;
}
public void setClassObjectDescriptor(@NotNull ClassDescriptor classObjectDescriptor) {
this.classObjectDescriptor = classObjectDescriptor;
@NotNull
@Override
public List<AnnotationDescriptor> getAnnotations() {
return Collections.emptyList();
}
@Override
@@ -95,24 +90,6 @@ public class ClassDescriptorImpl extends DeclarationDescriptorNonRootImpl implem
return typeConstructor;
}
@Override
@NotNull
public JetScope getMemberScope(List<? extends TypeProjection> typeArguments) {
assert typeArguments.size() == typeConstructor.getParameters().size() : "Wrong number or arguments: " + typeArguments + " for " + this;
if (typeConstructor.getParameters().isEmpty()) {
return memberDeclarations;
}
Map<TypeConstructor, TypeProjection> substitutionContext = SubstitutionUtils
.buildSubstitutionContext(typeConstructor.getParameters(), typeArguments);
return new SubstitutingScope(memberDeclarations, TypeSubstitutor.create(substitutionContext));
}
@NotNull
@Override
public JetType getDefaultType() {
return TypeUtils.makeUnsubstitutedType(this, memberDeclarations);
}
@NotNull
@Override
public Collection<ConstructorDescriptor> getConstructors() {
@@ -121,29 +98,20 @@ public class ClassDescriptorImpl extends DeclarationDescriptorNonRootImpl implem
@NotNull
@Override
public ClassDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public JetType getClassObjectType() {
return getClassObjectDescriptor().getDefaultType();
protected JetScope getScopeForMemberLookup() {
return scopeForMemberLookup;
}
@Nullable
@Override
public ClassDescriptor getClassObjectDescriptor() {
return classObjectDescriptor;
return null;
}
@NotNull
@Override
public ClassKind getKind() {
return kind;
}
@Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitClassDescriptor(this, data);
return ClassKind.CLASS;
}
@Override
@@ -165,21 +133,17 @@ public class ClassDescriptorImpl extends DeclarationDescriptorNonRootImpl implem
@Override
public boolean isInner() {
return isInner;
return false;
}
@NotNull
@Override
public ReceiverParameterDescriptor getThisAsReceiverParameter() {
if (thisAsReceiverParameter == null) {
thisAsReceiverParameter = DescriptorFactory.createLazyReceiverParameterDescriptor(this);
}
return thisAsReceiverParameter;
return thisAsReceiverParameter.invoke();
}
@NotNull
@Override
public JetScope getUnsubstitutedInnerClassesScope() {
return JetScope.EMPTY;
public String toString() {
return "class " + getName();
}
}
@@ -22,7 +22,6 @@ import org.jetbrains.jet.lang.ModuleConfiguration;
import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.ConstructorDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.PropertyDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.jet.lang.resolve.ImportPath;
@@ -255,24 +254,6 @@ public class ErrorUtils {
private static final ErrorClassDescriptor ERROR_CLASS = new ErrorClassDescriptor("");
private static final Set<ConstructorDescriptor> ERROR_CONSTRUCTOR_GROUP = Collections.singleton(createErrorConstructor());
private static final ConstructorDescriptor ERROR_CONSTRUCTOR = new ConstructorDescriptorImpl(ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), true);
static {
ERROR_CLASS.initializeErrorClass();
}
@NotNull
public static Set<ConstructorDescriptor> getErrorConstructorGroup() {
return ERROR_CONSTRUCTOR_GROUP;
}
@NotNull
public static ConstructorDescriptor getErrorConstructor() {
return ERROR_CONSTRUCTOR;
}
@NotNull
public static JetScope createErrorScope(@NotNull String debugMessage) {
return createErrorScope(debugMessage, false);
@@ -316,18 +297,6 @@ public class ErrorUtils {
return function;
}
@NotNull
private static ConstructorDescriptor createErrorConstructor() {
ConstructorDescriptorImpl r = new ConstructorDescriptorImpl(ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), false);
r.initialize(
Collections.<TypeParameterDescriptor>emptyList(), // TODO
Collections.<ValueParameterDescriptor>emptyList(), // TODO
Visibilities.INTERNAL
);
r.setReturnType(createErrorType("<ERROR RETURN TYPE>"));
return r;
}
@NotNull
public static JetType createErrorType(@NotNull String debugMessage) {
return new ErrorTypeImpl(createErrorTypeConstructor(debugMessage), createErrorScope(debugMessage));
@@ -373,13 +342,6 @@ public class ErrorUtils {
return candidate instanceof ErrorClassDescriptor;
}
@NotNull
public static ErrorClassDescriptor createErrorClass(@NotNull String debugMessage) {
ErrorClassDescriptor result = new ErrorClassDescriptor(debugMessage);
result.initializeErrorClass();
return result;
}
private static class ErrorTypeImpl implements JetType {
private final TypeConstructor constructor;
private final JetScope memberScope;
@@ -17,38 +17,33 @@
package org.jetbrains.jet.lang.types.error;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
import org.jetbrains.jet.lang.descriptors.Modality;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.ClassDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.ConstructorDescriptorImpl;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import java.util.Collection;
import java.util.Collections;
import static org.jetbrains.jet.lang.types.ErrorUtils.*;
public final class ErrorClassDescriptor extends ClassDescriptorImpl {
public ErrorClassDescriptor(@NotNull String debugMessage) {
super(ErrorUtils.getErrorModule(), Collections.<AnnotationDescriptor>emptyList(), Modality.OPEN,
Name.special("<ERROR CLASS: " + debugMessage + ">"));
}
super(getErrorModule(), Name.special("<ERROR CLASS: " + debugMessage + ">"), Modality.OPEN, Collections.<JetType>emptyList());
@NotNull
@Override
public Collection<ConstructorDescriptor> getConstructors() {
return getErrorConstructorGroup();
}
ConstructorDescriptorImpl errorConstructor =
new ConstructorDescriptorImpl(this, Collections.<AnnotationDescriptor>emptyList(), true);
@NotNull
@Override
public Modality getModality() {
return Modality.OPEN;
errorConstructor.initialize(
Collections.<TypeParameterDescriptor>emptyList(), // TODO
Collections.<ValueParameterDescriptor>emptyList(), // TODO
Visibilities.INTERNAL
);
errorConstructor.setReturnType(createErrorType("<ERROR RETURN TYPE>"));
initialize(createErrorScope("ERROR_CLASS"), Collections.<ConstructorDescriptor>singleton(errorConstructor), errorConstructor);
}
@NotNull
@@ -61,9 +56,4 @@ public final class ErrorClassDescriptor extends ClassDescriptorImpl {
public String toString() {
return getName().asString();
}
public void initializeErrorClass() {
initialize(true, Collections.<TypeParameterDescriptor>emptyList(), Collections.<JetType>emptyList(),
createErrorScope("ERROR_CLASS"), getErrorConstructorGroup(), getErrorConstructor(), false);
}
}