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);
}
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.load.java;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.name.SpecialNames;
public final class JvmAbi {
/**
@@ -35,7 +36,7 @@ public final class JvmAbi {
public static final String GETTER_PREFIX = "get";
public static final String SETTER_PREFIX = "set";
public static final String CLASS_OBJECT_CLASS_NAME = "object";
public static final String CLASS_OBJECT_CLASS_NAME = SpecialNames.DEFAULT_NAME_FOR_DEFAULT_OBJECT.asString();
public static final String CLASS_OBJECT_SUFFIX = "$" + CLASS_OBJECT_CLASS_NAME;
public static final String DELEGATED_PROPERTY_NAME_SUFFIX = "$delegate";
@@ -43,7 +44,7 @@ public final class JvmAbi {
public static final String ANNOTATED_PROPERTY_METHOD_NAME_SUFFIX = "$annotations";
public static final String INSTANCE_FIELD = "INSTANCE$";
public static final String CLASS_OBJECT_FIELD = "OBJECT$";
public static final String CLASS_OBJECT_FIELD = CLASS_OBJECT_CLASS_NAME;
public static final FqName K_OBJECT = new FqName("kotlin.jvm.internal.KObject");
public static final String KOTLIN_CLASS_FIELD_NAME = "$kotlinClass";
@@ -20,24 +20,25 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor;
import org.jetbrains.kotlin.load.java.JvmAbi;
import org.jetbrains.kotlin.name.*;
import org.jetbrains.kotlin.name.ClassId;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.FqNameUnsafe;
import org.jetbrains.kotlin.name.Name;
import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.kotlin.name.SpecialNames.isClassObjectName;
public class DeserializedResolverUtils {
private DeserializedResolverUtils() {
}
//TODO_R: remove usages
@NotNull
public static FqName kotlinFqNameToJavaFqName(@NotNull FqNameUnsafe kotlinFqName) {
List<Name> segments = kotlinFqName.pathSegments();
List<String> correctedSegments = new ArrayList<String>(segments.size());
for (Name segment : segments) {
correctedSegments.add(isClassObjectName(segment) ? JvmAbi.CLASS_OBJECT_CLASS_NAME : segment.getIdentifier());
correctedSegments.add(segment.getIdentifier());
}
return FqName.fromSegments(correctedSegments);
}
@@ -47,6 +48,7 @@ public class DeserializedResolverUtils {
return new ClassId(kotlinClassId.getPackageFqName(), kotlinFqNameToJavaFqName(kotlinClassId.getRelativeClassName()).toUnsafe());
}
//TODO_R: remove usages
@NotNull
public static FqNameUnsafe javaFqNameToKotlinFqName(@NotNull FqName javaFqName) {
if (javaFqName.isRoot()) {
@@ -56,14 +58,12 @@ public class DeserializedResolverUtils {
List<Name> correctedSegments = new ArrayList<Name>(segments.size());
correctedSegments.add(segments.get(0));
for (int i = 1; i < segments.size(); i++) {
Name segment = segments.get(i);
boolean isClassObjectName = segment.asString().equals(JvmAbi.CLASS_OBJECT_CLASS_NAME);
Name correctedSegment = isClassObjectName ? SpecialNames.getClassObjectName(segments.get(i - 1)) : segment;
correctedSegments.add(correctedSegment);
correctedSegments.add(segments.get(i));
}
return FqNameUnsafe.fromSegments(correctedSegments);
}
//TODO_R: remove usages
@NotNull
public static ClassId javaClassIdToKotlinClassId(@NotNull ClassId javaClassId) {
return new ClassId(javaClassId.getPackageFqName(), javaFqNameToKotlinFqName(javaClassId.getRelativeClassName().toSafe()));
@@ -51,7 +51,6 @@ public class JvmClassName {
return byFqNameWithoutInnerClasses(new FqName(fqName));
}
private final static String CLASS_OBJECT_REPLACE_GUARD = "<class_object>";
private final static String TRAIT_IMPL_REPLACE_GUARD = "<trait_impl>";
// Internal name: kotlin/Map$Entry
@@ -71,12 +70,10 @@ public class JvmClassName {
public FqName getFqNameForClassNameWithoutDollars() {
if (fqName == null) {
String fqName = internalName
.replace(JvmAbi.CLASS_OBJECT_CLASS_NAME, CLASS_OBJECT_REPLACE_GUARD)
.replace(JvmAbi.TRAIT_IMPL_CLASS_NAME, TRAIT_IMPL_REPLACE_GUARD)
.replace('$', '.')
.replace('/', '.')
.replace(TRAIT_IMPL_REPLACE_GUARD, JvmAbi.TRAIT_IMPL_CLASS_NAME)
.replace(CLASS_OBJECT_REPLACE_GUARD, JvmAbi.CLASS_OBJECT_CLASS_NAME);
.replace(TRAIT_IMPL_REPLACE_GUARD, JvmAbi.TRAIT_IMPL_CLASS_NAME);
this.fqName = new FqName(fqName);
}
return fqName;
@@ -24,22 +24,10 @@ public object BuiltInsSerializationUtil {
private val PACKAGE_FILE_NAME = ".kotlin_package"
private val STRING_TABLE_FILE_NAME = ".kotlin_string_table"
private val CLASS_NAMES_FILE_NAME = ".kotlin_class_names"
private val CLASS_OBJECT_NAME = "object"
private fun relativeClassNameToFilePath(className: FqNameUnsafe): String? {
return FqName.fromSegments(className.pathSegments().map { segment ->
when {
SpecialNames.isClassObjectName(segment) -> CLASS_OBJECT_NAME
!segment.isSpecial() -> segment.getIdentifier()
else -> return null
}
}).asString()
}
platformStatic public fun getClassMetadataPath(classId: ClassId): String? {
val filePath = relativeClassNameToFilePath(classId.getRelativeClassName())
if (filePath == null) return null
return packageFqNameToPath(classId.getPackageFqName()) + "/" + filePath + "." + CLASS_METADATA_FILE_EXTENSION
return packageFqNameToPath(classId.getPackageFqName()) + "/" + classId.getRelativeClassName().asString() +
"." + CLASS_METADATA_FILE_EXTENSION
}
platformStatic public fun getPackageFilePath(fqName: FqName): String =
@@ -23,7 +23,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.name.SpecialNames;
import org.jetbrains.kotlin.resolve.DescriptorFactory;
import org.jetbrains.kotlin.resolve.OverridingUtil;
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter;
@@ -44,12 +43,11 @@ import java.util.HashSet;
import java.util.Set;
public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
private final ClassKind kind;
private final TypeConstructor typeConstructor;
private final ConstructorDescriptor primaryConstructor;
private final JetScope scope;
private final JetScope staticScope = new StaticScopeForKotlinClass(this);
private final EnumEntrySyntheticClassDescriptor classObjectDescriptor;
private final ClassDescriptor classObjectDescriptor;
private final NotNullLazyValue<Collection<Name>> enumMemberNames;
/**
@@ -66,7 +64,7 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
) {
JetType enumType = enumClass.getDefaultType();
return new EnumEntrySyntheticClassDescriptor(storageManager, enumClass, enumType, name, ClassKind.ENUM_ENTRY, enumMemberNames, source);
return new EnumEntrySyntheticClassDescriptor(storageManager, enumClass, enumType, name, enumMemberNames, source);
}
private EnumEntrySyntheticClassDescriptor(
@@ -74,12 +72,11 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
@NotNull ClassDescriptor containingClass,
@NotNull JetType supertype,
@NotNull Name name,
@NotNull ClassKind kind,
@NotNull NotNullLazyValue<Collection<Name>> enumMemberNames,
@NotNull SourceElement source
) {
super(storageManager, containingClass, name, source);
this.kind = kind;
assert containingClass.getKind() == ClassKind.ENUM_CLASS;
this.typeConstructor =
TypeConstructorImpl.createForClass(this, getAnnotations(), true, "enum entry", Collections.<TypeParameterDescriptor>emptyList(),
@@ -92,11 +89,7 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
primaryConstructor.setReturnType(getDefaultType());
this.primaryConstructor = primaryConstructor;
this.classObjectDescriptor =
kind == ClassKind.CLASS_OBJECT
? null
: new EnumEntrySyntheticClassDescriptor(storageManager, this, getDefaultType(), SpecialNames.getClassObjectName(name),
ClassKind.CLASS_OBJECT, enumMemberNames, SourceElement.NO_SOURCE);
this.classObjectDescriptor = containingClass;
}
@NotNull
@@ -132,7 +125,7 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
@NotNull
@Override
public ClassKind getKind() {
return kind;
return ClassKind.ENUM_ENTRY;
}
@NotNull
@@ -23,7 +23,7 @@ public class SpecialNames {
public static final Name NO_NAME_PROVIDED = Name.special("<no name provided>");
public static final Name ROOT_PACKAGE = Name.special("<root package>");
private static final String CLASS_OBJECT_FOR = "<class-object-for-";
public static final Name DEFAULT_NAME_FOR_DEFAULT_OBJECT = Name.identifier("Default");
// This name is used as a key for the case when something has no name _due to a syntactic error_
// Example: fun (x: Int) = 5
@@ -36,15 +36,5 @@ public class SpecialNames {
return name != null && !name.isSpecial() ? name : SAFE_IDENTIFIER_FOR_NO_NAME;
}
@NotNull
public static Name getClassObjectName(@NotNull Name className) {
return Name.special(CLASS_OBJECT_FOR + className.asString() + ">");
}
public static boolean isClassObjectName(@NotNull Name name) {
return name.isSpecial() && name.asString().startsWith(CLASS_OBJECT_FOR);
}
private SpecialNames() {}
}
+2 -8
View File
@@ -179,14 +179,8 @@ message Class {
required int32 fq_name = 3;
message ClassObject {
// If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
// Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
optional Class data = 1;
}
// This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
optional ClassObject class_object = 4;
// If this field is present, it contains the name of default object.
optional int32 class_object_name = 4;
repeated TypeParameter type_parameter = 5;
repeated Type supertype = 6;
@@ -131,8 +131,8 @@ public class DescriptorSerializer {
}
ClassDescriptor classObject = classDescriptor.getClassObjectDescriptor();
if (classObject != null) {
builder.setClassObject(classObjectProto(classObject));
if (classObject != null && isClassObject(classObject)) {
builder.setClassObjectName(stringTable.getSimpleNameIndex(classObject.getName()));
}
extension.serializeClass(classDescriptor, builder, stringTable);
@@ -140,15 +140,6 @@ public class DescriptorSerializer {
return builder;
}
@NotNull
private ProtoBuf.Class.ClassObject classObjectProto(@NotNull ClassDescriptor classObject) {
if (isObject(classObject.getContainingDeclaration())) {
return ProtoBuf.Class.ClassObject.newBuilder().setData(classProto(classObject)).build();
}
return ProtoBuf.Class.ClassObject.getDefaultInstance();
}
@NotNull
public ProtoBuf.Callable.Builder callableProto(@NotNull CallableMemberDescriptor descriptor) {
ProtoBuf.Callable.Builder builder = ProtoBuf.Callable.newBuilder();
@@ -7336,23 +7336,23 @@ public final class ProtoBuf {
*/
int getFqName();
// optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;
// optional int32 class_object_name = 4;
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
* <code>optional int32 class_object_name = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* If this field is present, it contains the name of default object.
* </pre>
*/
boolean hasClassObject();
boolean hasClassObjectName();
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
* <code>optional int32 class_object_name = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* If this field is present, it contains the name of default object.
* </pre>
*/
org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject getClassObject();
int getClassObjectName();
// repeated .org.jetbrains.kotlin.serialization.TypeParameter type_parameter = 5;
/**
@@ -7518,17 +7518,9 @@ public final class ProtoBuf {
fqName_ = input.readInt32();
break;
}
case 34: {
org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.Builder subBuilder = null;
if (((bitField0_ & 0x00000008) == 0x00000008)) {
subBuilder = classObject_.toBuilder();
}
classObject_ = input.readMessage(org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.PARSER, extensionRegistry);
if (subBuilder != null) {
subBuilder.mergeFrom(classObject_);
classObject_ = subBuilder.buildPartial();
}
case 32: {
bitField0_ |= 0x00000008;
classObjectName_ = input.readInt32();
break;
}
case 42: {
@@ -7760,437 +7752,6 @@ public final class ProtoBuf {
// @@protoc_insertion_point(enum_scope:org.jetbrains.kotlin.serialization.Class.Kind)
}
public interface ClassObjectOrBuilder
extends com.google.protobuf.MessageLiteOrBuilder {
// optional .org.jetbrains.kotlin.serialization.Class data = 1;
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
boolean hasData();
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
org.jetbrains.kotlin.serialization.ProtoBuf.Class getData();
}
/**
* Protobuf type {@code org.jetbrains.kotlin.serialization.Class.ClassObject}
*/
public static final class ClassObject extends
com.google.protobuf.GeneratedMessageLite
implements ClassObjectOrBuilder {
// Use ClassObject.newBuilder() to construct.
private ClassObject(com.google.protobuf.GeneratedMessageLite.Builder builder) {
super(builder);
}
private ClassObject(boolean noInit) {}
private static final ClassObject defaultInstance;
public static ClassObject getDefaultInstance() {
return defaultInstance;
}
public ClassObject getDefaultInstanceForType() {
return defaultInstance;
}
private ClassObject(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
initFields();
int mutable_bitField0_ = 0;
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!parseUnknownField(input,
extensionRegistry, tag)) {
done = true;
}
break;
}
case 10: {
org.jetbrains.kotlin.serialization.ProtoBuf.Class.Builder subBuilder = null;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
subBuilder = data_.toBuilder();
}
data_ = input.readMessage(org.jetbrains.kotlin.serialization.ProtoBuf.Class.PARSER, extensionRegistry);
if (subBuilder != null) {
subBuilder.mergeFrom(data_);
data_ = subBuilder.buildPartial();
}
bitField0_ |= 0x00000001;
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
makeExtensionsImmutable();
}
}
public static com.google.protobuf.Parser<ClassObject> PARSER =
new com.google.protobuf.AbstractParser<ClassObject>() {
public ClassObject parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new ClassObject(input, extensionRegistry);
}
};
@java.lang.Override
public com.google.protobuf.Parser<ClassObject> getParserForType() {
return PARSER;
}
private int bitField0_;
// optional .org.jetbrains.kotlin.serialization.Class data = 1;
public static final int DATA_FIELD_NUMBER = 1;
private org.jetbrains.kotlin.serialization.ProtoBuf.Class data_;
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
public boolean hasData() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
public org.jetbrains.kotlin.serialization.ProtoBuf.Class getData() {
return data_;
}
private void initFields() {
data_ = org.jetbrains.kotlin.serialization.ProtoBuf.Class.getDefaultInstance();
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized != -1) return isInitialized == 1;
if (hasData()) {
if (!getData().isInitialized()) {
memoizedIsInitialized = 0;
return false;
}
}
memoizedIsInitialized = 1;
return true;
}
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
getSerializedSize();
if (((bitField0_ & 0x00000001) == 0x00000001)) {
output.writeMessage(1, data_);
}
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(1, data_);
}
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
@java.lang.Override
protected java.lang.Object writeReplace()
throws java.io.ObjectStreamException {
return super.writeReplace();
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); }
/**
* Protobuf type {@code org.jetbrains.kotlin.serialization.Class.ClassObject}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageLite.Builder<
org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject, Builder>
implements org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObjectOrBuilder {
// Construct using org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
}
private static Builder create() {
return new Builder();
}
public Builder clear() {
super.clear();
data_ = org.jetbrains.kotlin.serialization.ProtoBuf.Class.getDefaultInstance();
bitField0_ = (bitField0_ & ~0x00000001);
return this;
}
public Builder clone() {
return create().mergeFrom(buildPartial());
}
public org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject getDefaultInstanceForType() {
return org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.getDefaultInstance();
}
public org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject build() {
org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject buildPartial() {
org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject result = new org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
result.data_ = data_;
result.bitField0_ = to_bitField0_;
return result;
}
public Builder mergeFrom(org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject other) {
if (other == org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.getDefaultInstance()) return this;
if (other.hasData()) {
mergeData(other.getData());
}
return this;
}
public final boolean isInitialized() {
if (hasData()) {
if (!getData().isInitialized()) {
return false;
}
}
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
// optional .org.jetbrains.kotlin.serialization.Class data = 1;
private org.jetbrains.kotlin.serialization.ProtoBuf.Class data_ = org.jetbrains.kotlin.serialization.ProtoBuf.Class.getDefaultInstance();
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
public boolean hasData() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
public org.jetbrains.kotlin.serialization.ProtoBuf.Class getData() {
return data_;
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
public Builder setData(org.jetbrains.kotlin.serialization.ProtoBuf.Class value) {
if (value == null) {
throw new NullPointerException();
}
data_ = value;
bitField0_ |= 0x00000001;
return this;
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
public Builder setData(
org.jetbrains.kotlin.serialization.ProtoBuf.Class.Builder builderForValue) {
data_ = builderForValue.build();
bitField0_ |= 0x00000001;
return this;
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
public Builder mergeData(org.jetbrains.kotlin.serialization.ProtoBuf.Class value) {
if (((bitField0_ & 0x00000001) == 0x00000001) &&
data_ != org.jetbrains.kotlin.serialization.ProtoBuf.Class.getDefaultInstance()) {
data_ =
org.jetbrains.kotlin.serialization.ProtoBuf.Class.newBuilder(data_).mergeFrom(value).buildPartial();
} else {
data_ = value;
}
bitField0_ |= 0x00000001;
return this;
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class data = 1;</code>
*
* <pre>
* If this field is present, it contains serialized data for a synthetic class object, for which there's no class file.
* Otherwise class object was compiled to a separate class file and serialized data can be found in the annotation on that class
* </pre>
*/
public Builder clearData() {
data_ = org.jetbrains.kotlin.serialization.ProtoBuf.Class.getDefaultInstance();
bitField0_ = (bitField0_ & ~0x00000001);
return this;
}
// @@protoc_insertion_point(builder_scope:org.jetbrains.kotlin.serialization.Class.ClassObject)
}
static {
defaultInstance = new ClassObject(true);
defaultInstance.initFields();
}
// @@protoc_insertion_point(class_scope:org.jetbrains.kotlin.serialization.Class.ClassObject)
}
public interface PrimaryConstructorOrBuilder
extends com.google.protobuf.MessageLiteOrBuilder {
@@ -8728,28 +8289,28 @@ public final class ProtoBuf {
return fqName_;
}
// optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;
public static final int CLASS_OBJECT_FIELD_NUMBER = 4;
private org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject classObject_;
// optional int32 class_object_name = 4;
public static final int CLASS_OBJECT_NAME_FIELD_NUMBER = 4;
private int classObjectName_;
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
* <code>optional int32 class_object_name = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* If this field is present, it contains the name of default object.
* </pre>
*/
public boolean hasClassObject() {
public boolean hasClassObjectName() {
return ((bitField0_ & 0x00000008) == 0x00000008);
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
* <code>optional int32 class_object_name = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* If this field is present, it contains the name of default object.
* </pre>
*/
public org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject getClassObject() {
return classObject_;
public int getClassObjectName() {
return classObjectName_;
}
// repeated .org.jetbrains.kotlin.serialization.TypeParameter type_parameter = 5;
@@ -8949,7 +8510,7 @@ public final class ProtoBuf {
flags_ = 0;
extraVisibility_ = "";
fqName_ = 0;
classObject_ = org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.getDefaultInstance();
classObjectName_ = 0;
typeParameter_ = java.util.Collections.emptyList();
supertype_ = java.util.Collections.emptyList();
nestedClassName_ = java.util.Collections.emptyList();
@@ -8966,12 +8527,6 @@ public final class ProtoBuf {
memoizedIsInitialized = 0;
return false;
}
if (hasClassObject()) {
if (!getClassObject().isInitialized()) {
memoizedIsInitialized = 0;
return false;
}
}
for (int i = 0; i < getTypeParameterCount(); i++) {
if (!getTypeParameter(i).isInitialized()) {
memoizedIsInitialized = 0;
@@ -9020,7 +8575,7 @@ public final class ProtoBuf {
output.writeInt32(3, fqName_);
}
if (((bitField0_ & 0x00000008) == 0x00000008)) {
output.writeMessage(4, classObject_);
output.writeInt32(4, classObjectName_);
}
for (int i = 0; i < typeParameter_.size(); i++) {
output.writeMessage(5, typeParameter_.get(i));
@@ -9063,7 +8618,7 @@ public final class ProtoBuf {
}
if (((bitField0_ & 0x00000008) == 0x00000008)) {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(4, classObject_);
.computeInt32Size(4, classObjectName_);
}
for (int i = 0; i < typeParameter_.size(); i++) {
size += com.google.protobuf.CodedOutputStream
@@ -9196,7 +8751,7 @@ public final class ProtoBuf {
bitField0_ = (bitField0_ & ~0x00000002);
fqName_ = 0;
bitField0_ = (bitField0_ & ~0x00000004);
classObject_ = org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.getDefaultInstance();
classObjectName_ = 0;
bitField0_ = (bitField0_ & ~0x00000008);
typeParameter_ = java.util.Collections.emptyList();
bitField0_ = (bitField0_ & ~0x00000010);
@@ -9248,7 +8803,7 @@ public final class ProtoBuf {
if (((from_bitField0_ & 0x00000008) == 0x00000008)) {
to_bitField0_ |= 0x00000008;
}
result.classObject_ = classObject_;
result.classObjectName_ = classObjectName_;
if (((bitField0_ & 0x00000010) == 0x00000010)) {
typeParameter_ = java.util.Collections.unmodifiableList(typeParameter_);
bitField0_ = (bitField0_ & ~0x00000010);
@@ -9295,8 +8850,8 @@ public final class ProtoBuf {
if (other.hasFqName()) {
setFqName(other.getFqName());
}
if (other.hasClassObject()) {
mergeClassObject(other.getClassObject());
if (other.hasClassObjectName()) {
setClassObjectName(other.getClassObjectName());
}
if (!other.typeParameter_.isEmpty()) {
if (typeParameter_.isEmpty()) {
@@ -9360,12 +8915,6 @@ public final class ProtoBuf {
return false;
}
if (hasClassObject()) {
if (!getClassObject().isInitialized()) {
return false;
}
}
for (int i = 0; i < getTypeParameterCount(); i++) {
if (!getTypeParameter(i).isInitialized()) {
@@ -9616,88 +9165,52 @@ public final class ProtoBuf {
return this;
}
// optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;
private org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject classObject_ = org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.getDefaultInstance();
// optional int32 class_object_name = 4;
private int classObjectName_ ;
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
* <code>optional int32 class_object_name = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* If this field is present, it contains the name of default object.
* </pre>
*/
public boolean hasClassObject() {
public boolean hasClassObjectName() {
return ((bitField0_ & 0x00000008) == 0x00000008);
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
* <code>optional int32 class_object_name = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* If this field is present, it contains the name of default object.
* </pre>
*/
public org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject getClassObject() {
return classObject_;
public int getClassObjectName() {
return classObjectName_;
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
* <code>optional int32 class_object_name = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* If this field is present, it contains the name of default object.
* </pre>
*/
public Builder setClassObject(org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject value) {
if (value == null) {
throw new NullPointerException();
}
classObject_ = value;
public Builder setClassObjectName(int value) {
bitField0_ |= 0x00000008;
classObjectName_ = value;
return this;
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
* <code>optional int32 class_object_name = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* If this field is present, it contains the name of default object.
* </pre>
*/
public Builder setClassObject(
org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.Builder builderForValue) {
classObject_ = builderForValue.build();
bitField0_ |= 0x00000008;
return this;
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* </pre>
*/
public Builder mergeClassObject(org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject value) {
if (((bitField0_ & 0x00000008) == 0x00000008) &&
classObject_ != org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.getDefaultInstance()) {
classObject_ =
org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.newBuilder(classObject_).mergeFrom(value).buildPartial();
} else {
classObject_ = value;
}
bitField0_ |= 0x00000008;
return this;
}
/**
* <code>optional .org.jetbrains.kotlin.serialization.Class.ClassObject class_object = 4;</code>
*
* <pre>
* This field is present if and only if the class has a class object. Its proto should be found either here or in the separate file
* </pre>
*/
public Builder clearClassObject() {
classObject_ = org.jetbrains.kotlin.serialization.ProtoBuf.Class.ClassObject.getDefaultInstance();
public Builder clearClassObjectName() {
bitField0_ = (bitField0_ & ~0x00000008);
classObjectName_ = 0;
return this;
}
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.resolve.scopes.StaticScopeForKotlinClass
import org.jetbrains.kotlin.types.AbstractClassTypeConstructor
import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.serialization.deserialization
import org.jetbrains.kotlin.name.SpecialNames.getClassObjectName
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
@@ -111,18 +110,13 @@ public class DeserializedClassDescriptor(
}
private fun computeClassObjectDescriptor(): ClassDescriptor? {
if (!classProto.hasClassObject()) return null
if (getKind() == ClassKind.OBJECT) {
val classObjectProto = classProto.getClassObject()
if (!classObjectProto.hasData()) {
throw IllegalStateException("Object should have a serialized class object: $classId")
}
return DeserializedClassDescriptor(c, classObjectProto.getData(), c.nameResolver)
if (getKind() == ClassKind.OBJECT || getKind() == ClassKind.CLASS_OBJECT) {
return this
}
if (!classProto.hasClassObjectName()) return null
return c.components.deserializeClass(classId.createNestedClassId(getClassObjectName(getName())))
val classObjectName = c.nameResolver.getName(classProto.getClassObjectName())
return memberScope.getClassifier(classObjectName) as? ClassDescriptor
}
override fun getClassObjectDescriptor(): ClassDescriptor? = classObjectDescriptor()
@@ -22,22 +22,13 @@ import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.name.ClassId
private fun findInnerClass(classDescriptor: ClassDescriptor, name: Name): ClassDescriptor? {
return if (SpecialNames.isClassObjectName(name)) {
classDescriptor.getClassObjectDescriptor()
}
else {
classDescriptor.getUnsubstitutedInnerClassesScope().getClassifier(name) as? ClassDescriptor
}
}
public fun ModuleDescriptor.findClassAcrossModuleDependencies(classId: ClassId): ClassDescriptor? {
val packageViewDescriptor = getPackage(classId.getPackageFqName()) ?: return null
val segments = classId.getRelativeClassName().pathSegments()
val topLevelClass = packageViewDescriptor.getMemberScope().getClassifier(segments.first()) as? ClassDescriptor ?: return null
var result = topLevelClass
for (name in segments.subList(1, segments.size())) {
result = findInnerClass(result, name) ?: return null
result = result.getUnsubstitutedInnerClassesScope().getClassifier(name) as? ClassDescriptor ?: return null
}
return result
}
@@ -256,11 +256,7 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport
@NotNull
private static FqName getClassRelativeName(@NotNull JetClassOrObject decompiledClassOrObject) {
Name name = decompiledClassOrObject.getNameAsName();
if (name == null) {
assert decompiledClassOrObject instanceof JetObjectDeclaration &&
((JetObjectDeclaration) decompiledClassOrObject).isClassObject();
name = Name.identifier(JvmAbi.CLASS_OBJECT_CLASS_NAME);
}
assert name != null;
JetClassOrObject parent = PsiTreeUtil.getParentOfType(decompiledClassOrObject, JetClassOrObject.class, true);
if (parent == null) {
assert decompiledClassOrObject.isTopLevel();
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.psi.JetDelegationSpecifierList
import org.jetbrains.kotlin.psi.JetDelegatorToSuperClass
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.name.SpecialNames.getClassObjectName
import org.jetbrains.kotlin.psi.JetClassObject
import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer
import org.jetbrains.kotlin.psi.stubs.impl.KotlinModifierListStubImpl
@@ -68,6 +67,7 @@ private class ClassClsStubBuilder(
supertypeIds
}
}
private val classObjectName = if (classProto.hasClassObjectName()) c.nameResolver.getName(classProto.getClassObjectName()) else null
private val classOrObjectStub = createClassOrObjectStubAndModifierListStub()
@@ -112,8 +112,8 @@ private class ClassClsStubBuilder(
private fun doCreateClassOrObjectStub(parent: StubElement<out PsiElement>): StubElement<out PsiElement> {
val isClassObject = classKind == ProtoBuf.Class.Kind.CLASS_OBJECT
val fqName = if (!isClassObject) outerContext.memberFqNameProvider.getMemberFqName(classId.getRelativeClassName().shortName()) else null
val shortName = fqName?.shortName()?.ref()
val fqName = outerContext.memberFqNameProvider.getMemberFqName(classId.getRelativeClassName().shortName())
val shortName = fqName.shortName()?.ref()
val superTypeRefs = supertypeIds.filter {
//TODO: filtering function types should go away
!KotlinBuiltIns.isExactFunctionType(it.asSingleFqName()) && !KotlinBuiltIns.isExactExtensionFunctionType(it.asSingleFqName())
@@ -132,7 +132,7 @@ private class ClassClsStubBuilder(
KotlinClassStubImpl(
JetClassElementType.getStubType(classKind == ProtoBuf.Class.Kind.ENUM_ENTRY),
parent,
fqName?.ref(),
fqName.ref(),
shortName,
superTypeRefs,
isTrait = classKind == ProtoBuf.Class.Kind.TRAIT,
@@ -181,11 +181,11 @@ private class ClassClsStubBuilder(
}
private fun createClassObjectStub(classBody: KotlinPlaceHolderStubImpl<JetClassBody>) {
if (!classProto.hasClassObject() || classKind == ProtoBuf.Class.Kind.OBJECT) {
if (classObjectName == null) {
return
}
val classObjectId = classId.createNestedClassId(getClassObjectName(classId.getRelativeClassName().shortName()))
val classObjectId = classId.createNestedClassId(classObjectName)
createNestedClassStub(classBody, classObjectId)
}
@@ -221,8 +221,11 @@ private class ClassClsStubBuilder(
private fun createInnerAndNestedClasses(classBody: KotlinPlaceHolderStubImpl<JetClassBody>) {
classProto.getNestedClassNameList().forEach { id ->
val nestedClassId = classId.createNestedClassId(c.nameResolver.getName(id))
createNestedClassStub(classBody, nestedClassId)
val nestedClassName = c.nameResolver.getName(id)
if (nestedClassName != classObjectName) {
val nestedClassId = classId.createNestedClassId(nestedClassName)
createNestedClassStub(classBody, nestedClassId)
}
}
}
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.load.kotlin.AbstractBinaryClassAnnotationAndConstant
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinaryClass
import org.jetbrains.kotlin.name.SpecialNames
class ClsStubBuilderComponents(
@@ -42,11 +41,11 @@ class ClsStubBuilderComponents(
}
}
//TODO_R: remove?
class MemberFqNameProvider(val fqName: FqName) {
fun getMemberFqName(name: Name): FqName = fqName.child(name)
fun child(name: Name?): MemberFqNameProvider =
if (name == null || SpecialNames.isClassObjectName(name)) this else MemberFqNameProvider(fqName.child(name))
fun child(name: Name?): MemberFqNameProvider = if (name == null) this else MemberFqNameProvider(fqName.child(name))
}
trait TypeParameters {
@@ -97,8 +97,7 @@ fun createStubForPackageName(packageDirectiveStub: KotlinPlaceHolderStubImpl<Jet
}
fun createStubForTypeName(typeClassId: ClassId, parent: StubElement<out PsiElement>): KotlinUserTypeStub {
//TODO: should go away with default objects
val segments = typeClassId.asSingleFqName().pathSegments().filter { !SpecialNames.isClassObjectName(it) }.toArrayList()
val segments = typeClassId.asSingleFqName().pathSegments().toArrayList()
assert(segments.isNotEmpty())
val iterator = segments.listIterator(segments.size())
@@ -148,7 +148,7 @@ private fun buildDecompiledText(packageFqName: FqName, descriptors: List<Declara
var firstPassed = false
val subindent = indent + " "
val classObject = descriptor.getClassObjectDescriptor()
if (classObject != null && !isSyntheticClassObject(classObject)) {
if (classObject != null && !descriptor.getKind().isSingleton() && !isSyntheticClassObject(classObject)) {
firstPassed = true
builder.append(subindent)
appendDescriptor(classObject, subindent)
@@ -157,6 +157,9 @@ private fun buildDecompiledText(packageFqName: FqName, descriptors: List<Declara
if (member.getContainingDeclaration() != descriptor) {
continue
}
if (member == classObject) {
continue
}
if (member is CallableMemberDescriptor
&& member.getKind() != CallableMemberDescriptor.Kind.DECLARATION
//TODO: not synthesized and component like
@@ -114,16 +114,7 @@ public class DeprecatedAnnotationVisitor extends AfterAnalysisHighlightingVisito
}
private void checkClassDescriptor(@NotNull JetExpression expression, @NotNull ClassDescriptor target) {
// Deprecated for Class, for ClassObject (if reference isn't in UserType or in ModifierList (trait))
if (!reportAnnotationIfNeeded(expression, target)) {
if (PsiTreeUtil.getParentOfType(expression, JetUserType.class) == null &&
PsiTreeUtil.getParentOfType(expression, JetModifierList.class) == null) {
ClassDescriptor classObjectDescriptor = target.getClassObjectDescriptor();
if (classObjectDescriptor != null) {
reportAnnotationIfNeeded(expression, classObjectDescriptor);
}
}
}
reportAnnotationIfNeeded(expression, target);
}
private void checkPropertyDescriptor(
@@ -56,25 +56,11 @@ public class StubIndexServiceImpl implements StubIndexService {
@Override
public void indexObject(KotlinObjectStub stub, IndexSink sink) {
String name = stub.getName();
FqName fqName = stub.getFqName();
if (stub.isClassObject()) {
StubElement parentClassStub = stub.getParentStub().getParentStub().getParentStub();
assert parentClassStub instanceof KotlinStubWithFqName<?>
: "Something but a class/object is a parent to class object stub: " + parentClassStub;
name = JvmAbi.CLASS_OBJECT_CLASS_NAME;
FqName parentFqName = ((KotlinStubWithFqName<?>) parentClassStub).getFqName();
if (parentFqName != null) {
fqName = parentFqName.child(Name.identifier(name));
}
}
if (name != null) {
sink.occurrence(JetClassShortNameIndex.getInstance().getKey(), name);
}
FqName fqName = stub.getFqName();
if (fqName != null) {
sink.occurrence(JetFullClassNameIndex.getInstance().getKey(), fqName.asString());