From 0d84565570e5117751b10c9b7ed789a63685ce9d Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Tue, 24 Sep 2013 15:02:17 -0700 Subject: [PATCH] name and containingDeclaration pulled up --- .../DeserializedClassDescriptor.java | 20 +---- .../lazy/descriptors/LazyClassDescriptor.java | 26 ++---- .../ClassDescriptorFromJvmBytecode.java | 4 +- .../java/resolver/JavaClassResolver.java | 6 +- .../impl/AbstractClassDescriptor.java | 83 +++++++++++++++++++ .../descriptors/impl/ClassDescriptorBase.java | 67 ++------------- .../impl/MutableClassDescriptor.java | 10 +-- .../impl/MutableClassDescriptorLite.java | 23 +---- 8 files changed, 109 insertions(+), 130 deletions(-) create mode 100644 core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/AbstractClassDescriptor.java diff --git a/compiler/frontend/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.java b/compiler/frontend/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.java index 4e211c8d368..ad648d7896d 100644 --- a/compiler/frontend/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.java +++ b/compiler/frontend/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedClassDescriptor.java @@ -64,8 +64,6 @@ public class DeserializedClassDescriptor extends ClassDescriptorBase implements private final NestedClassDescriptors nestedClasses; private final NestedClassDescriptors nestedObjects; - private final Name name; - private final DeclarationDescriptor containingDeclaration; private final DeserializedClassTypeConstructor typeConstructor; private final Modality modality; private final Visibility visibility; @@ -84,20 +82,20 @@ public class DeserializedClassDescriptor extends ClassDescriptorBase implements @NotNull ProtoBuf.Class classProto, @Nullable TypeDeserializer outerTypeDeserializer ) { + super(containingDeclaration, nameResolver.getName(classProto.getName())); + this.classId = classId; this.classProto = classProto; this.descriptorFinder = descriptorFinder; - this.name = nameResolver.getName(classProto.getName()); TypeDeserializer notNullTypeDeserializer = new TypeDeserializer(storageManager, outerTypeDeserializer, nameResolver, - descriptorFinder, "Deserializer for class " + name, NONE); + descriptorFinder, "Deserializer for class " + getName(), NONE); DescriptorDeserializer outerDeserializer = DescriptorDeserializer.create(storageManager, notNullTypeDeserializer, this, nameResolver, annotationResolver); List typeParameters = new ArrayList(classProto.getTypeParameterCount()); this.deserializer = outerDeserializer.createChildDeserializer(this, classProto.getTypeParameterList(), typeParameters); this.typeDeserializer = deserializer.getTypeDeserializer(); - this.containingDeclaration = containingDeclaration; this.typeConstructor = new DeserializedClassTypeConstructor(typeParameters); this.memberScope = new DeserializedClassMemberScope(storageManager, this); this.innerClassesScope = new InnerClassesScopeWrapper(memberScope); @@ -153,12 +151,6 @@ public class DeserializedClassDescriptor extends ClassDescriptorBase implements return result; } - @NotNull - @Override - public DeclarationDescriptor getContainingDeclaration() { - return containingDeclaration; - } - @NotNull @Override public TypeConstructor getTypeConstructor() { @@ -189,12 +181,6 @@ public class DeserializedClassDescriptor extends ClassDescriptorBase implements return isInner; } - @NotNull - @Override - public Name getName() { - return name; - } - private List computeAnnotations() { if (!Flags.HAS_ANNOTATIONS.get(classProto.getFlags())) { return Collections.emptyList(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/descriptors/LazyClassDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/descriptors/LazyClassDescriptor.java index e7f116a34d8..5d960d496ad 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/descriptors/LazyClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/descriptors/LazyClassDescriptor.java @@ -69,8 +69,6 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc private final JetClassLikeInfo originalClassInfo; private final ClassMemberDeclarationProvider declarationProvider; - private final Name name; - private final DeclarationDescriptor containingDeclaration; private final LazyClassTypeConstructor typeConstructor; private final Modality modality; private final Visibility visibility; @@ -94,8 +92,8 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc @NotNull Name name, @NotNull JetClassLikeInfo classLikeInfo ) { + super(containingDeclaration, name); this.resolveSession = resolveSession; - this.name = name; if (classLikeInfo.getCorrespondingClassOrObject() != null) { this.resolveSession.getTrace().record(BindingContext.CLASS, classLikeInfo.getCorrespondingClassOrObject(), this); @@ -104,8 +102,8 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc this.originalClassInfo = classLikeInfo; JetClassLikeInfo classLikeInfoForMembers = classLikeInfo.getClassKind() != ClassKind.ENUM_CLASS ? classLikeInfo : noEnumEntries(classLikeInfo); - this.declarationProvider = resolveSession.getDeclarationProviderFactory().getClassMemberDeclarationProvider(classLikeInfoForMembers); - this.containingDeclaration = containingDeclaration; + this.declarationProvider = + resolveSession.getDeclarationProviderFactory().getClassMemberDeclarationProvider(classLikeInfoForMembers); this.unsubstitutedMemberScope = new LazyClassMemberScope(resolveSession, declarationProvider, this); this.unsubstitutedInnerClassesScope = new InnerClassesScopeWrapper(unsubstitutedMemberScope); @@ -181,7 +179,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc @NotNull private JetScope computeScopeForClassHeaderResolution() { - WritableScopeImpl scope = new WritableScopeImpl(JetScope.EMPTY, this, RedeclarationHandler.DO_NOTHING, "Scope with type parameters for " + name); + WritableScopeImpl scope = new WritableScopeImpl(JetScope.EMPTY, this, RedeclarationHandler.DO_NOTHING, "Scope with type parameters for " + getName()); for (TypeParameterDescriptor typeParameterDescriptor : getTypeConstructor().getParameters()) { scope.addClassifierDescriptor(typeParameterDescriptor); } @@ -201,7 +199,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc @NotNull private JetScope computeScopeForMemberDeclarationResolution() { - WritableScopeImpl thisScope = new WritableScopeImpl(JetScope.EMPTY, this, RedeclarationHandler.DO_NOTHING, "Scope with 'this' for " + name); + WritableScopeImpl thisScope = new WritableScopeImpl(JetScope.EMPTY, this, RedeclarationHandler.DO_NOTHING, "Scope with 'this' for " + getName()); thisScope.addLabeledDeclaration(this); thisScope.changeLockLevel(WritableScope.LockLevel.READING); @@ -227,7 +225,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc ConstructorDescriptor primaryConstructor = getUnsubstitutedPrimaryConstructor(); if (primaryConstructor == null) return getScopeForMemberDeclarationResolution(); - WritableScopeImpl scope = new WritableScopeImpl(JetScope.EMPTY, this, RedeclarationHandler.DO_NOTHING, "Scope with constructor parameters in " + name); + WritableScopeImpl scope = new WritableScopeImpl(JetScope.EMPTY, this, RedeclarationHandler.DO_NOTHING, "Scope with constructor parameters in " + getName()); for (ValueParameterDescriptor valueParameterDescriptor : primaryConstructor.getValueParameters()) { scope.addVariableDescriptor(valueParameterDescriptor); } @@ -250,12 +248,6 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc return unsubstitutedMemberScope.getPrimaryConstructor(); } - @NotNull - @Override - public DeclarationDescriptor getContainingDeclaration() { - return containingDeclaration; - } - @NotNull @Override public TypeConstructor getTypeConstructor() { @@ -352,12 +344,6 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc } } - @NotNull - @Override - public Name getName() { - return name; - } - @Override public String toString() { return "lazy class " + getName().toString(); diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/descriptor/ClassDescriptorFromJvmBytecode.java b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/descriptor/ClassDescriptorFromJvmBytecode.java index 25e3dcfc48e..60e2ecb4102 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/descriptor/ClassDescriptorFromJvmBytecode.java +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/descriptor/ClassDescriptorFromJvmBytecode.java @@ -23,6 +23,7 @@ import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.impl.MutableClassDescriptorLite; import org.jetbrains.jet.lang.resolve.java.scope.JavaClassNonStaticMembersScope; +import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.types.JetType; import java.util.Collection; @@ -37,10 +38,11 @@ public class ClassDescriptorFromJvmBytecode extends MutableClassDescriptorLite { public ClassDescriptorFromJvmBytecode( @NotNull DeclarationDescriptor containingDeclaration, + @NotNull Name name, @NotNull ClassKind kind, boolean isInner ) { - super(containingDeclaration, kind, isInner); + super(containingDeclaration, name, kind, isInner); } diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/resolver/JavaClassResolver.java b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/resolver/JavaClassResolver.java index 1ebd3a1cd86..72756bba65e 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/resolver/JavaClassResolver.java +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/resolver/JavaClassResolver.java @@ -271,11 +271,10 @@ public final class JavaClassResolver { @NotNull ClassOrNamespaceDescriptor containingDeclaration ) { ClassDescriptorFromJvmBytecode classDescriptor = - new ClassDescriptorFromJvmBytecode(containingDeclaration, determineClassKind(javaClass), isInnerClass(javaClass)); + new ClassDescriptorFromJvmBytecode(containingDeclaration, javaClass.getName(), determineClassKind(javaClass), isInnerClass(javaClass)); cache(javaClassToKotlinFqName(fqName), classDescriptor); - classDescriptor.setName(javaClass.getName()); JavaTypeParameterResolver.Initializer typeParameterInitializer = typeParameterResolver.resolveTypeParameters(classDescriptor, javaClass); classDescriptor.setTypeParameterDescriptors(typeParameterInitializer.getDescriptors()); @@ -462,9 +461,8 @@ public final class JavaClassResolver { @NotNull private ClassDescriptorFromJvmBytecode createSyntheticClassObject(@NotNull ClassDescriptor containing, @NotNull JavaClass javaClass) { ClassDescriptorFromJvmBytecode classObjectDescriptor = - new ClassDescriptorFromJvmBytecode(containing, ClassKind.CLASS_OBJECT, false); + new ClassDescriptorFromJvmBytecode(containing, getClassObjectName(containing.getName()), ClassKind.CLASS_OBJECT, false); - classObjectDescriptor.setName(getClassObjectName(containing.getName())); classObjectDescriptor.setModality(Modality.FINAL); classObjectDescriptor.setVisibility(containing.getVisibility()); classObjectDescriptor.setTypeParameterDescriptors(Collections.emptyList()); diff --git a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/AbstractClassDescriptor.java b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/AbstractClassDescriptor.java new file mode 100644 index 00000000000..f108ca7f9a1 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/AbstractClassDescriptor.java @@ -0,0 +1,83 @@ +package org.jetbrains.jet.lang.descriptors.impl; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorVisitor; +import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; +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 java.util.List; +import java.util.Map; + +public abstract class AbstractClassDescriptor implements ClassDescriptor { + protected final Name name; + protected volatile JetType defaultType; + + public AbstractClassDescriptor(@NotNull Name name) { + this.name = name; + } + + @NotNull + @Override + public Name getName() { + return name; + } + + @NotNull + @Override + public DeclarationDescriptor getOriginal() { + return this; + } + + protected abstract JetScope getScopeForMemberLookup(); + + @NotNull + @Override + public JetScope getMemberScope(List typeArguments) { + assert typeArguments.size() == getTypeConstructor().getParameters().size() : "Illegal number of type arguments: expected " + + getTypeConstructor().getParameters().size() + " but was " + typeArguments.size() + + " for " + getTypeConstructor() + " " + getTypeConstructor().getParameters(); + if (typeArguments.isEmpty()) return getScopeForMemberLookup(); + + List typeParameters = getTypeConstructor().getParameters(); + Map substitutionContext = SubstitutionUtils.buildSubstitutionContext(typeParameters, typeArguments); + + // Unsafe substitutor is OK, because no recursion can hurt us upon a trivial substitution: + // all the types are written explicitly in the code already, they can not get infinite. + // One exception is *-projections, but they need to be handled separately anyways. + TypeSubstitutor substitutor = TypeSubstitutor.createUnsafe(substitutionContext); + return new SubstitutingScope(getScopeForMemberLookup(), substitutor); + } + + @NotNull + @Override + public ClassDescriptor substitute(@NotNull TypeSubstitutor substitutor) { + if (substitutor.isEmpty()) { + return this; + } + return new LazySubstitutingClassDescriptor(this, substitutor); + } + + @NotNull + @Override + public JetType getDefaultType() { + if (defaultType == null) { + defaultType = TypeUtils.makeUnsubstitutedType(this, getScopeForMemberLookup()); + } + return defaultType; + } + + @Override + public void acceptVoid(DeclarationDescriptorVisitor visitor) { + visitor.visitClassDescriptor(this, null); + } + + @Override + public R accept(DeclarationDescriptorVisitor visitor, D data) { + return visitor.visitClassDescriptor(this, data); + } +} diff --git a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/ClassDescriptorBase.java b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/ClassDescriptorBase.java index e9b34877e4c..8b7764001ef 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/ClassDescriptorBase.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/ClassDescriptorBase.java @@ -17,72 +17,21 @@ package org.jetbrains.jet.lang.descriptors.impl; import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorVisitor; -import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; -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.resolve.name.Name; -import java.util.List; -import java.util.Map; +public abstract class ClassDescriptorBase extends AbstractClassDescriptor { -public abstract class ClassDescriptorBase implements ClassDescriptor { + private final DeclarationDescriptor containingDeclaration; - protected volatile JetType defaultType; - - @NotNull - @Override - public DeclarationDescriptor getOriginal() { - return this; - } - - protected abstract JetScope getScopeForMemberLookup(); - - @NotNull - @Override - public JetScope getMemberScope(List typeArguments) { - assert typeArguments.size() == getTypeConstructor().getParameters().size() : "Illegal number of type arguments: expected " - + getTypeConstructor().getParameters().size() + " but was " + typeArguments.size() - + " for " + getTypeConstructor() + " " + getTypeConstructor().getParameters(); - if (typeArguments.isEmpty()) return getScopeForMemberLookup(); - - List typeParameters = getTypeConstructor().getParameters(); - Map substitutionContext = SubstitutionUtils.buildSubstitutionContext(typeParameters, typeArguments); - - // Unsafe substitutor is OK, because no recursion can hurt us upon a trivial substitution: - // all the types are written explicitly in the code already, they can not get infinite. - // One exception is *-projections, but they need to be handled separately anyways. - TypeSubstitutor substitutor = TypeSubstitutor.createUnsafe(substitutionContext); - return new SubstitutingScope(getScopeForMemberLookup(), substitutor); + protected ClassDescriptorBase(@NotNull DeclarationDescriptor containingDeclaration, @NotNull Name name) { + super(name); + this.containingDeclaration = containingDeclaration; } @NotNull @Override - public ClassDescriptor substitute(@NotNull TypeSubstitutor substitutor) { - if (substitutor.isEmpty()) { - return this; - } - return new LazySubstitutingClassDescriptor(this, substitutor); - } - - @NotNull - @Override - public JetType getDefaultType() { - if (defaultType == null) { - defaultType = TypeUtils.makeUnsubstitutedType(this, getScopeForMemberLookup()); - } - return defaultType; - } - - @Override - public void acceptVoid(DeclarationDescriptorVisitor visitor) { - visitor.visitClassDescriptor(this, null); - } - - @Override - public R accept(DeclarationDescriptorVisitor visitor, D data) { - return visitor.visitClassDescriptor(this, data); + public DeclarationDescriptor getContainingDeclaration() { + return containingDeclaration; } } diff --git a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/MutableClassDescriptor.java b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/MutableClassDescriptor.java index 510bfd47750..1c6f7cf3dde 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/MutableClassDescriptor.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/MutableClassDescriptor.java @@ -43,7 +43,7 @@ public class MutableClassDescriptor extends MutableClassDescriptorLite { public MutableClassDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope outerScope, ClassKind kind, boolean isInner, Name name) { - super(containingDeclaration, kind, isInner); + super(containingDeclaration, name, kind, isInner); RedeclarationHandler redeclarationHandler = RedeclarationHandler.DO_NOTHING; @@ -57,7 +57,7 @@ public class MutableClassDescriptor extends MutableClassDescriptorLite { setUpScopeForInitializers(this); } - setName(name); + scopeForMemberResolution.addLabeledDeclaration(this); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -128,12 +128,6 @@ public class MutableClassDescriptor extends MutableClassDescriptorLite { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @Override - public void setName(@NotNull Name name) { - super.setName(name); - scopeForMemberResolution.addLabeledDeclaration(this); - } - @Override public void createTypeConstructor() { super.createTypeConstructor(); diff --git a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/MutableClassDescriptorLite.java b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/MutableClassDescriptorLite.java index 18f64705628..e1fdf3d49a4 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/MutableClassDescriptorLite.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/descriptors/impl/MutableClassDescriptorLite.java @@ -60,35 +60,16 @@ public abstract class MutableClassDescriptorLite extends ClassDescriptorBase { private ReceiverParameterDescriptor implicitReceiver; - private Name name; - private final DeclarationDescriptor containingDeclaration; - public MutableClassDescriptorLite(@NotNull DeclarationDescriptor containingDeclaration, + @NotNull Name name, @NotNull ClassKind kind, boolean isInner ) { - this.containingDeclaration = containingDeclaration; + super(containingDeclaration, name); this.kind = kind; this.isInner = isInner; } - @NotNull - @Override - public DeclarationDescriptor getContainingDeclaration() { - return containingDeclaration; - } - - @NotNull - @Override - public Name getName() { - return name; - } - - public void setName(@NotNull Name name) { - assert this.name == null : this.name; - this.name = name; - } - @NotNull @Override public TypeConstructor getTypeConstructor() {