Rework class objects

Class objects have name "Default" by default
Do not produce synthetic class objects
Class objects have new semantics:
    class "A" has class object "B" if literal "A" can be used as a value of type "B"
Class objects act like ordinary nested objects
    i.e. are accessible from class scope via getClassifier()
Jvm backend: class object fields and class have the name of class object ("Default")
	as opposed to special "OBJECT$" and "object"
Serialization: only the name of class object is needed to serialize data
This commit is contained in:
Pavel V. Talanov
2015-01-14 15:34:33 +03:00
parent 4b6112d380
commit 0343fd8fc7
39 changed files with 254 additions and 1728 deletions
@@ -32,16 +32,16 @@ public class FieldInfo {
throw new UnsupportedOperationException("Can't create singleton field for class: " + classDescriptor);
}
ClassDescriptor ownerDescriptor = kind == ClassKind.OBJECT
? classDescriptor
: DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class);
assert ownerDescriptor != null : "Owner not found for class: " + classDescriptor;
Type ownerType = typeMapper.mapType(ownerDescriptor);
String fieldName = kind == ClassKind.ENUM_ENTRY
? classDescriptor.getName().asString()
: classDescriptor.getKind() == ClassKind.CLASS_OBJECT ? JvmAbi.CLASS_OBJECT_FIELD : JvmAbi.INSTANCE_FIELD;
return new FieldInfo(ownerType, typeMapper.mapType(classDescriptor), fieldName, true);
if (kind == ClassKind.OBJECT) {
Type type = typeMapper.mapType(classDescriptor);
return new FieldInfo(type, type, JvmAbi.INSTANCE_FIELD, true);
}
else {
ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class);
assert ownerDescriptor != null : "Owner not found for class: " + classDescriptor;
Type ownerType = typeMapper.mapType(ownerDescriptor);
return new FieldInfo(ownerType, typeMapper.mapType(classDescriptor), classDescriptor.getName().asString(), true);
}
}
@NotNull
@@ -958,7 +958,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
private void generateFieldForSingleton() {
if (isEnumEntry(descriptor)) return;
if (isEnumEntry(descriptor) || isClassObject(descriptor)) return;
ClassDescriptor classObjectDescriptor = descriptor.getClassObjectDescriptor();
ClassDescriptor fieldTypeDescriptor;
@@ -59,7 +59,6 @@ import static org.jetbrains.kotlin.codegen.AsmUtil.isPrimitive;
import static org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.SYNTHESIZED;
import static org.jetbrains.kotlin.descriptors.SourceElement.NO_SOURCE;
import static org.jetbrains.kotlin.resolve.BindingContext.VARIABLE;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isClassObject;
import static org.jetbrains.kotlin.resolve.jvm.AsmTypes.*;
import static org.jetbrains.kotlin.resolve.jvm.diagnostics.DiagnosticsPackage.OtherOrigin;
import static org.jetbrains.kotlin.resolve.jvm.diagnostics.DiagnosticsPackage.TraitImpl;
@@ -242,9 +241,7 @@ public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclaratio
String outerClassInternalName =
containing instanceof ClassDescriptor ? typeMapper.mapClass((ClassDescriptor) containing).getInternalName() : null;
String innerName = isClassObject(innerClass)
? JvmAbi.CLASS_OBJECT_CLASS_NAME
: innerClass.getName().isSpecial() ? null : innerClass.getName().asString();
String innerName = innerClass.getName().isSpecial() ? null : innerClass.getName().asString();
String innerClassInternalName = typeMapper.mapClass(innerClass).getInternalName();
v.visitInnerClass(innerClassInternalName, outerClassInternalName, innerName, calculateInnerClassAccessFlags(innerClass));
@@ -213,7 +213,8 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
assert classDescriptor != null : String.format("No class found in binding context for: \n---\n%s\n---\n",
JetPsiUtil.getElementTextWithContext(classObject));
String name = peekFromStack(nameStack) + JvmAbi.CLASS_OBJECT_SUFFIX;
//TODO_R: remove visitClassObject
String name = getName(classDescriptor);
recordClosure(classDescriptor, name);
classStack.push(classDescriptor);
@@ -80,7 +80,8 @@ public final class PsiCodegenPredictor {
if (declaration instanceof JetClassObject) {
// Get parent and assign Class object prefix
return parentInternalName + JvmAbi.CLASS_OBJECT_SUFFIX;
//TODO_R: getName() nullable
return parentInternalName + "$" + ((JetClassObject) declaration).getObjectDeclaration().getName();
}
if (!PsiTreeUtil.instanceOf(declaration, JetClass.class, JetObjectDeclaration.class, JetNamedFunction.class, JetProperty.class) ||
@@ -339,14 +339,7 @@ public class JetTypeMapper {
assert container instanceof ClassDescriptor : "Unexpected container: " + container + " for " + klass;
String containerInternalName = computeAsmTypeImpl((ClassDescriptor) container);
switch (klass.getKind()) {
case ENUM_ENTRY:
return containerInternalName;
case CLASS_OBJECT:
return containerInternalName + JvmAbi.CLASS_OBJECT_SUFFIX;
default:
return containerInternalName + "$" + name;
}
return klass.getKind() == ClassKind.ENUM_ENTRY ? containerInternalName : containerInternalName + "$" + name;
}
@NotNull
@@ -201,11 +201,6 @@ public class BuiltInsSerializer(private val dependOnOldBuiltIns: Boolean) {
writeClass(classDescriptor, classProto)
serializeClasses(classDescriptor.getUnsubstitutedInnerClassesScope().getDescriptors(), serializer, writeClass)
val classObjectDescriptor = classDescriptor.getClassObjectDescriptor()
if (classObjectDescriptor != null) {
serializeClass(classObjectDescriptor, serializer, writeClass)
}
}
private fun serializeClasses(
@@ -120,25 +120,6 @@ public class CliLightClassGenerationSupport extends LightClassGenerationSupport
@NotNull
@Override
public Collection<JetClassOrObject> findClassOrObjectDeclarations(@NotNull FqName fqName, @NotNull final GlobalSearchScope searchScope) {
if (JvmAbi.isClassObjectFqName(fqName)) {
Collection<JetClassOrObject> parentClasses = findClassOrObjectDeclarations(fqName.parent(), searchScope);
return ContainerUtil.mapNotNull(
parentClasses,
new Function<JetClassOrObject, JetClassOrObject>() {
@Override
public JetClassOrObject fun(JetClassOrObject classOrObject) {
if (classOrObject instanceof JetClass) {
JetClassObject classObject = ((JetClass) classOrObject).getClassObject();
if (classObject != null) {
return classObject.getObjectDeclaration();
}
}
return null;
}
}
);
}
Collection<ClassDescriptor> classDescriptors = ResolveSessionUtils.getClassDescriptorsByFqName(getModule(), fqName);
return ContainerUtil.mapNotNull(classDescriptors, new Function<ClassDescriptor, JetClassOrObject>() {
@@ -51,6 +51,9 @@ public final class JetNamedDeclarationUtil {
@Nullable
public static FqName getParentFqName(@NotNull JetNamedDeclaration namedDeclaration) {
PsiElement parent = namedDeclaration.getParent();
if (parent instanceof JetClassObject) {
parent = parent.getParent();
}
if (parent instanceof JetClassBody) {
// One nesting to JetClassBody doesn't affect to qualified name
parent = parent.getParent();
@@ -69,15 +72,7 @@ public final class JetNamedDeclarationUtil {
}
}
else if (parent instanceof JetObjectDeclaration) {
if (parent.getParent() instanceof JetClassObject) {
JetClassOrObject classOrObject = PsiTreeUtil.getParentOfType(parent, JetClassOrObject.class);
if (classOrObject != null) {
return getFQName(classOrObject);
}
}
else {
return getFQName((JetNamedDeclaration) parent);
}
return getFQName((JetNamedDeclaration) parent);
}
return null;
}
@@ -27,6 +27,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.JetNodeTypes;
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.name.SpecialNames;
import org.jetbrains.kotlin.psi.stubs.KotlinObjectStub;
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
@@ -50,6 +51,10 @@ public class JetObjectDeclaration extends JetNamedDeclarationStub<KotlinObjectSt
}
JetObjectDeclarationName nameAsDeclaration = getNameAsDeclaration();
if (nameAsDeclaration == null && isClassObject()) {
//NOTE: a hack in PSI that simplifies writing frontend code
return SpecialNames.DEFAULT_NAME_FOR_DEFAULT_OBJECT.toString();
}
return nameAsDeclaration == null ? null : nameAsDeclaration.getName();
}
@@ -101,7 +101,7 @@ public class DeclarationResolver {
Collection<DeclarationDescriptor> allDescriptors = classDescriptor.getScopeForMemberLookup().getOwnDeclaredDescriptors();
ClassDescriptorWithResolutionScopes classObj = classDescriptor.getClassObjectDescriptor();
if (classObj != null) {
if (classObj != null && DescriptorUtils.isClassObject(classObj)) {
Collection<DeclarationDescriptor> classObjDescriptors = classObj.getScopeForMemberLookup().getOwnDeclaredDescriptors();
if (!classObjDescriptors.isEmpty()) {
allDescriptors = Lists.newArrayList(allDescriptors);
@@ -37,8 +37,9 @@ public class Importer {
else if (descriptor is ClassDescriptor && descriptor.getKind() != ClassKind.OBJECT) {
allUnderImportScopes.add(descriptor.getStaticScope())
allUnderImportScopes.add(descriptor.getUnsubstitutedInnerClassesScope())
val classObjectDescriptor = descriptor.getClassObjectDescriptor()
if (classObjectDescriptor != null) {
if (classObjectDescriptor != null && DescriptorUtils.isClassObject(classObjectDescriptor)) {
allUnderImportScopes.add(classObjectDescriptor.getUnsubstitutedInnerClassesScope())
}
}
@@ -277,7 +277,7 @@ public class QualifiedExpressionResolver {
results.add(lookupSimpleNameReference(selector, descriptor.getStaticScope(), lookupMode, true));
ClassDescriptor classObject = descriptor.getClassObjectDescriptor();
if (classObject != null) {
if (classObject != null && !descriptor.getKind().isSingleton()) {
addResultsForClass(results, selector, lookupMode, classObject);
}
}
@@ -20,6 +20,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.name.FqName;
@@ -33,6 +34,11 @@ public abstract class JetClassOrObjectInfo<E extends JetClassOrObject> implement
this.element = element;
}
@Nullable
public Name getName() {
return element.getNameAsName();
}
@Override
public JetClassOrObject getCorrespondingClassOrObject() {
return element;
@@ -1,116 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.resolve.lazy.data;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.ClassKind;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyClassDescriptor;
import java.util.Collections;
import java.util.List;
public class SyntheticClassObjectInfo implements JetClassLikeInfo {
private final JetClassLikeInfo classInfo;
private final LazyClassDescriptor classDescriptor;
public SyntheticClassObjectInfo(@NotNull JetClassLikeInfo classInfo, @NotNull LazyClassDescriptor classDescriptor) {
this.classInfo = classInfo;
this.classDescriptor = classDescriptor;
}
@NotNull
public LazyClassDescriptor getClassDescriptor() {
return classDescriptor;
}
@NotNull
@Override
public FqName getContainingPackageFqName() {
return classInfo.getContainingPackageFqName();
}
@Nullable
@Override
public JetModifierList getModifierList() {
return null;
}
@Nullable
@Override
public JetClassObject getClassObject() {
return null;
}
@NotNull
@Override
public List<JetClassObject> getClassObjects() {
return Collections.emptyList();
}
@NotNull
@Override
public PsiElement getScopeAnchor() {
return classInfo.getScopeAnchor();
}
@Nullable
@Override
public JetClassOrObject getCorrespondingClassOrObject() {
return null;
}
@Nullable
@Override
public JetTypeParameterList getTypeParameterList() {
return null;
}
@NotNull
@Override
public List<? extends JetParameter> getPrimaryConstructorParameters() {
return Collections.emptyList();
}
@NotNull
@Override
public ClassKind getClassKind() {
return ClassKind.CLASS_OBJECT;
}
@NotNull
@Override
public List<JetAnnotationEntry> getDanglingAnnotations() {
return Collections.emptyList();
}
@NotNull
@Override
public List<JetDeclaration> getDeclarations() {
// There can be no declarations in a synthetic class object, all its members are fake overrides
return Collections.emptyList();
}
@Override
public String toString() {
return "class object of " + classInfo;
}
}
@@ -29,9 +29,12 @@ public class PsiBasedClassMemberDeclarationProvider(
override fun doCreateIndex(index: AbstractPsiBasedDeclarationProvider.Index) {
for (declaration in classInfo.getDeclarations()) {
if (declaration !is JetClassObject) { // Do nothing for class object because it will be taken directly from the classInfo
if (declaration !is JetClassObject) {
index.putToIndex(declaration)
}
else {
index.putToIndex(declaration.getObjectDeclaration())
}
}
for (parameter in classInfo.getPrimaryConstructorParameters()) {
@@ -29,7 +29,6 @@ import org.jetbrains.annotations.Mutable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.kotlin.resolve.lazy.LazyClassContext;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
@@ -38,10 +37,11 @@ import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.*;
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil;
import org.jetbrains.kotlin.resolve.lazy.LazyClassContext;
import org.jetbrains.kotlin.resolve.lazy.LazyEntity;
import org.jetbrains.kotlin.resolve.lazy.data.JetClassInfoUtil;
import org.jetbrains.kotlin.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.kotlin.resolve.lazy.data.SyntheticClassObjectInfo;
import org.jetbrains.kotlin.resolve.lazy.data.JetClassOrObjectInfo;
import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProvider;
import org.jetbrains.kotlin.resolve.scopes.*;
import org.jetbrains.kotlin.storage.MemoizedFunctionToNotNull;
@@ -55,10 +55,7 @@ import org.jetbrains.kotlin.types.TypeUtils;
import java.util.*;
import static org.jetbrains.kotlin.diagnostics.Errors.CLASS_OBJECT_NOT_ALLOWED;
import static org.jetbrains.kotlin.diagnostics.Errors.CYCLIC_INHERITANCE_HIERARCHY;
import static org.jetbrains.kotlin.diagnostics.Errors.TYPE_PARAMETERS_IN_ENUM;
import static org.jetbrains.kotlin.name.SpecialNames.getClassObjectName;
import static org.jetbrains.kotlin.diagnostics.Errors.*;
import static org.jetbrains.kotlin.resolve.BindingContext.TYPE;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isSyntheticClassObject;
import static org.jetbrains.kotlin.resolve.ModifiersChecker.*;
@@ -272,6 +269,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
thisScope.setImplicitReceiver(this.getThisAsReceiverParameter());
thisScope.changeLockLevel(WritableScope.LockLevel.READING);
//TODO:
ClassDescriptor classObject = getClassObjectDescriptor();
JetScope classObjectAdapterScope = (classObject != null) ? new ClassObjectMixinScope(classObject) : JetScope.Empty.INSTANCE$;
@@ -383,8 +381,24 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
@Nullable
private LazyClassDescriptor computeClassObjectDescriptor(@Nullable JetClassObject classObject) {
JetClassLikeInfo classObjectInfo = getClassObjectInfo(classObject);
if (classObjectInfo != null) {
return new LazyClassDescriptor(c, this, getClassObjectName(getName()), classObjectInfo);
if (classObjectInfo instanceof JetClassOrObjectInfo) {
Name name = ((JetClassOrObjectInfo) classObjectInfo).getName();
assert name != null;
ClassifierDescriptor classObjectDescriptor = getScopeForMemberLookup().getClassifier(name);
if (classObjectDescriptor instanceof LazyClassDescriptor) {
return (LazyClassDescriptor) classObjectDescriptor;
}
else {
return null;
}
}
if (getKind() == ClassKind.CLASS_OBJECT || getKind() == ClassKind.OBJECT) {
return this;
}
if (getKind() == ClassKind.ENUM_ENTRY) {
DeclarationDescriptor containingDeclaration = getContainingDeclaration();
assert containingDeclaration instanceof ClassDescriptor && ((ClassDescriptor) containingDeclaration).getKind() == ClassKind.ENUM_CLASS;
return (LazyClassDescriptor) containingDeclaration;
}
return null;
}
@@ -398,9 +412,6 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
return JetClassInfoUtil.createClassLikeInfo(classObject.getObjectDeclaration());
}
else if (getKind() == ClassKind.OBJECT || getKind() == ClassKind.ENUM_ENTRY) {
return new SyntheticClassObjectInfo(originalClassInfo, this);
}
return null;
}
@@ -462,6 +473,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
private void doForceResolveAllContents() {
resolveMemberHeaders();
//TODO:
ClassDescriptor classObjectDescriptor = getClassObjectDescriptor();
if (classObjectDescriptor != null) {
ForceResolveUtil.forceResolveAllContents(classObjectDescriptor);
@@ -534,15 +546,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
return new Supertypes(Collections.<JetType>emptyList());
}
JetClassLikeInfo info = declarationProvider.getOwnerInfo();
if (info instanceof SyntheticClassObjectInfo) {
LazyClassDescriptor descriptor = ((SyntheticClassObjectInfo) info).getClassDescriptor();
if (descriptor.getKind().isSingleton()) {
return new Supertypes(Collections.singleton(descriptor.getDefaultType()));
}
}
JetClassOrObject classOrObject = info.getCorrespondingClassOrObject();
JetClassOrObject classOrObject = declarationProvider.getOwnerInfo().getCorrespondingClassOrObject();
if (classOrObject == null) {
return new Supertypes(Collections.singleton(c.getModuleDescriptor().getBuiltIns().getAnyType()));
}
@@ -82,7 +82,7 @@ class QualifierReceiver (
scopes.add(classifier.getStaticScope())
val classObjectDescriptor = classifier.getClassObjectDescriptor()
if (classObjectDescriptor != null) {
if (classObjectDescriptor != null && DescriptorUtils.isClassObject(classObjectDescriptor)) {
// non-static members are resolved through class object receiver
scopes.add(DescriptorUtils.getStaticNestedClassesScope(classObjectDescriptor))
}
File diff suppressed because it is too large Load Diff
@@ -20,7 +20,6 @@ import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
@@ -116,7 +115,7 @@ public class RecursiveDescriptorComparator {
if (descriptor instanceof ClassDescriptor) {
ClassDescriptor klass = (ClassDescriptor) descriptor;
appendSubDescriptors(descriptor, module,
klass.getDefaultType().getMemberScope(), getConstructorsAndClassObject(klass), printer);
klass.getDefaultType().getMemberScope(), klass.getConstructors(), printer);
JetScope staticScope = klass.getStaticScope();
if (!staticScope.getAllDescriptors().isEmpty()) {
printer.println();
@@ -164,14 +163,6 @@ public class RecursiveDescriptorComparator {
}
}
@NotNull
private static List<DeclarationDescriptor> getConstructorsAndClassObject(@NotNull ClassDescriptor klass) {
List<DeclarationDescriptor> constructorsAndClassObject = Lists.newArrayList();
constructorsAndClassObject.addAll(klass.getConstructors());
ContainerUtil.addIfNotNull(constructorsAndClassObject, klass.getClassObjectDescriptor());
return constructorsAndClassObject;
}
private boolean shouldSkip(@NotNull DeclarationDescriptor subDescriptor) {
boolean isFunctionFromAny = subDescriptor.getContainingDeclaration() instanceof ClassDescriptor
&& subDescriptor instanceof FunctionDescriptor
@@ -183,7 +174,7 @@ public class RecursiveDescriptorComparator {
@NotNull DeclarationDescriptor descriptor,
@NotNull ModuleDescriptor module,
@NotNull JetScope memberScope,
@NotNull Collection<DeclarationDescriptor> extraSubDescriptors,
@NotNull Collection<? extends DeclarationDescriptor> extraSubDescriptors,
@NotNull Printer printer
) {
if (!module.equals(DescriptorUtils.getContainingModule(descriptor))) {
@@ -105,7 +105,6 @@ public class RecursiveDescriptorProcessor {
&& visitChildren(descriptor.getThisAsReceiverParameter(), data)
&& visitChildren(descriptor.getConstructors(), data)
&& visitChildren(descriptor.getTypeConstructor().getParameters(), data)
&& visitChildren(descriptor.getClassObjectDescriptor(), data)
&& visitChildren(descriptor.getDefaultType().getMemberScope().getAllDescriptors(), data);
}