ClassDescriptor is now a super-interface for ClassConstructorDescriptor and TypeAliasConstructorDescriptor.

This commit is contained in:
Dmitry Petrov
2016-09-15 17:47:43 +03:00
committed by Dmitry Petrov
parent 7d214c6e58
commit 796d11c860
71 changed files with 307 additions and 243 deletions
@@ -93,7 +93,7 @@ class FunctionClassDescriptor(
override fun getUnsubstitutedMemberScope() = memberScope
override fun getCompanionObjectDescriptor() = null
override fun getConstructors() = emptyList<ConstructorDescriptor>()
override fun getConstructors() = emptyList<ClassConstructorDescriptor>()
override fun getKind() = ClassKind.INTERFACE
override fun getModality() = Modality.ABSTRACT
override fun getUnsubstitutedPrimaryConstructor() = null
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2016 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.descriptors
import org.jetbrains.kotlin.types.TypeSubstitutor
interface ClassConstructorDescriptor : ConstructorDescriptor {
override fun getContainingDeclaration(): ClassDescriptor
override fun getOriginal(): ClassConstructorDescriptor
override fun substitute(substitutor: TypeSubstitutor): ClassConstructorDescriptor
override fun copy(
newOwner: DeclarationDescriptor,
modality: Modality,
visibility: Visibility,
kind: CallableMemberDescriptor.Kind,
copyOverrides: Boolean
): ClassConstructorDescriptor
}
@@ -46,7 +46,7 @@ public interface ClassDescriptor extends ClassifierDescriptorWithTypeParameters,
@NotNull
@ReadOnly
Collection<ConstructorDescriptor> getConstructors();
Collection<ClassConstructorDescriptor> getConstructors();
@Override
@NotNull
@@ -88,7 +88,7 @@ public interface ClassDescriptor extends ClassifierDescriptorWithTypeParameters,
ReceiverParameterDescriptor getThisAsReceiverParameter();
@Nullable
ConstructorDescriptor getUnsubstitutedPrimaryConstructor();
ClassConstructorDescriptor getUnsubstitutedPrimaryConstructor();
/**
* It may differ from 'typeConstructor.parameters' in current class is inner, 'typeConstructor.parameters' contains
@@ -21,7 +21,7 @@ import org.jetbrains.annotations.ReadOnly;
import java.util.List;
public interface ClassifierDescriptorWithTypeParameters extends ClassifierDescriptor {
public interface ClassifierDescriptorWithTypeParameters extends ClassifierDescriptor, DeclarationDescriptorWithVisibility {
/**
* @return <code>true</code> if this class contains a reference to its outer class (as opposed to static nested class)
*/
@@ -34,7 +34,10 @@ public interface ConstructorDescriptor extends FunctionDescriptor {
@NotNull
@Override
ClassDescriptor getContainingDeclaration();
ClassifierDescriptorWithTypeParameters getContainingDeclaration();
@NotNull
ClassDescriptor getConstructedClass();
@NotNull
@Override
@@ -28,7 +28,7 @@ public interface ScriptDescriptor extends ClassDescriptor {
@NotNull
@Override
ConstructorDescriptor getUnsubstitutedPrimaryConstructor();
ClassConstructorDescriptor getUnsubstitutedPrimaryConstructor();
List<Pair<Name, KotlinType>> getScriptParametersToPassToSuperclass();
}
@@ -52,7 +52,7 @@ public class Visibilities {
}
if (what instanceof ConstructorDescriptor) {
ClassDescriptor classDescriptor = ((ConstructorDescriptor) what).getContainingDeclaration();
ClassifierDescriptorWithTypeParameters classDescriptor = ((ConstructorDescriptor) what).getContainingDeclaration();
if (DescriptorUtils.isSealedClass(classDescriptor)
&& DescriptorUtils.isTopLevelDeclaration(classDescriptor)
&& from instanceof ConstructorDescriptor
@@ -27,13 +27,13 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements ConstructorDescriptor {
public class ClassConstructorDescriptorImpl extends FunctionDescriptorImpl implements ClassConstructorDescriptor {
protected final boolean isPrimary;
private static final Name NAME = Name.special("<init>");
protected ConstructorDescriptorImpl(
protected ClassConstructorDescriptorImpl(
@NotNull ClassDescriptor containingDeclaration,
@Nullable ConstructorDescriptor original,
@NotNull Annotations annotations,
@@ -46,16 +46,16 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
}
@NotNull
public static ConstructorDescriptorImpl create(
public static ClassConstructorDescriptorImpl create(
@NotNull ClassDescriptor containingDeclaration,
@NotNull Annotations annotations,
boolean isPrimary,
@NotNull SourceElement source
) {
return new ConstructorDescriptorImpl(containingDeclaration, null, annotations, isPrimary, Kind.DECLARATION, source);
return new ClassConstructorDescriptorImpl(containingDeclaration, null, annotations, isPrimary, Kind.DECLARATION, source);
}
public ConstructorDescriptorImpl initialize(
public ClassConstructorDescriptorImpl initialize(
@NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
@NotNull Visibility visibility,
@NotNull List<TypeParameterDescriptor> typeParameterDescriptors
@@ -68,7 +68,7 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
return this;
}
public ConstructorDescriptorImpl initialize(
public ClassConstructorDescriptorImpl initialize(
@NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
@NotNull Visibility visibility
) {
@@ -96,14 +96,20 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
@NotNull
@Override
public ConstructorDescriptor getOriginal() {
return (ConstructorDescriptor) super.getOriginal();
public ClassDescriptor getConstructedClass() {
return getContainingDeclaration();
}
@NotNull
@Override
public ConstructorDescriptor substitute(@NotNull TypeSubstitutor originalSubstitutor) {
return (ConstructorDescriptor) super.substitute(originalSubstitutor);
public ClassConstructorDescriptor getOriginal() {
return (ClassConstructorDescriptor) super.getOriginal();
}
@NotNull
@Override
public ClassConstructorDescriptor substitute(@NotNull TypeSubstitutor originalSubstitutor) {
return (ClassConstructorDescriptor) super.substitute(originalSubstitutor);
}
@Override
@@ -129,7 +135,7 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
@NotNull
@Override
protected ConstructorDescriptorImpl createSubstitutedCopy(
protected ClassConstructorDescriptorImpl createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind,
@@ -144,7 +150,7 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
"kind: " + kind);
}
assert newName == null : "Attempt to rename constructor: " + this;
return new ConstructorDescriptorImpl(
return new ClassConstructorDescriptorImpl(
(ClassDescriptor) newOwner,
this,
annotations,
@@ -156,13 +162,13 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
@NotNull
@Override
public ConstructorDescriptor copy(
public ClassConstructorDescriptor copy(
DeclarationDescriptor newOwner,
Modality modality,
Visibility visibility,
Kind kind,
boolean copyOverrides
) {
return (ConstructorDescriptor) super.copy(newOwner, modality, visibility, kind, copyOverrides);
return (ClassConstructorDescriptor) super.copy(newOwner, modality, visibility, kind, copyOverrides);
}
}
@@ -38,8 +38,8 @@ public class ClassDescriptorImpl extends ClassDescriptorBase {
private final TypeConstructor typeConstructor;
private MemberScope unsubstitutedMemberScope;
private Set<ConstructorDescriptor> constructors;
private ConstructorDescriptor primaryConstructor;
private Set<ClassConstructorDescriptor> constructors;
private ClassConstructorDescriptor primaryConstructor;
public ClassDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
@@ -60,8 +60,8 @@ public class ClassDescriptorImpl extends ClassDescriptorBase {
public final void initialize(
@NotNull MemberScope unsubstitutedMemberScope,
@NotNull Set<ConstructorDescriptor> constructors,
@Nullable ConstructorDescriptor primaryConstructor
@NotNull Set<ClassConstructorDescriptor> constructors,
@Nullable ClassConstructorDescriptor primaryConstructor
) {
this.unsubstitutedMemberScope = unsubstitutedMemberScope;
this.constructors = constructors;
@@ -82,7 +82,7 @@ public class ClassDescriptorImpl extends ClassDescriptorBase {
@NotNull
@Override
public Collection<ConstructorDescriptor> getConstructors() {
public Collection<ClassConstructorDescriptor> getConstructors() {
return constructors;
}
@@ -116,7 +116,7 @@ public class ClassDescriptorImpl extends ClassDescriptorBase {
}
@Override
public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
public ClassConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
return primaryConstructor;
}
@@ -43,7 +43,7 @@ import java.util.*;
public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
private final TypeConstructor typeConstructor;
private final ConstructorDescriptor primaryConstructor;
private final ClassConstructorDescriptor primaryConstructor;
private final MemberScope scope;
private final NotNullLazyValue<Set<Name>> enumMemberNames;
private final Annotations annotations;
@@ -86,7 +86,7 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
this.scope = new EnumEntryScope(storageManager);
this.enumMemberNames = enumMemberNames;
ConstructorDescriptorImpl primaryConstructor = DescriptorFactory.createPrimaryConstructorForObject(this, source);
ClassConstructorDescriptorImpl primaryConstructor = DescriptorFactory.createPrimaryConstructorForObject(this, source);
primaryConstructor.setReturnType(getDefaultType());
this.primaryConstructor = primaryConstructor;
}
@@ -105,7 +105,7 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
@NotNull
@Override
public Collection<ConstructorDescriptor> getConstructors() {
public Collection<ClassConstructorDescriptor> getConstructors() {
return Collections.singleton(primaryConstructor);
}
@@ -156,7 +156,7 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
@Nullable
@Override
public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
public ClassConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
return primaryConstructor;
}
@@ -143,11 +143,11 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
@NotNull
@Override
public Collection<ConstructorDescriptor> getConstructors() {
Collection<ConstructorDescriptor> originalConstructors = original.getConstructors();
Collection<ConstructorDescriptor> result = new ArrayList<ConstructorDescriptor>(originalConstructors.size());
for (ConstructorDescriptor constructor : originalConstructors) {
ConstructorDescriptor copy =
public Collection<ClassConstructorDescriptor> getConstructors() {
Collection<ClassConstructorDescriptor> originalConstructors = original.getConstructors();
Collection<ClassConstructorDescriptor> result = new ArrayList<ClassConstructorDescriptor>(originalConstructors.size());
for (ClassConstructorDescriptor constructor : originalConstructors) {
ClassConstructorDescriptor copy =
constructor.copy(this, constructor.getModality(), constructor.getVisibility(), constructor.getKind(), false);
result.add(copy.substitute(getSubstitutor()));
}
@@ -241,7 +241,7 @@ public class LazySubstitutingClassDescriptor implements ClassDescriptor {
@Nullable
@Override
public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
public ClassConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
return original.getUnsubstitutedPrimaryConstructor();
}
@@ -24,8 +24,8 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.Variance
interface TypeAliasConstructorDescriptor : FunctionDescriptor {
val underlyingConstructorDescriptor: ConstructorDescriptor
interface TypeAliasConstructorDescriptor : ConstructorDescriptor {
val underlyingConstructorDescriptor: ClassConstructorDescriptor
override fun getContainingDeclaration(): TypeAliasDescriptor
@@ -46,7 +46,7 @@ interface TypeAliasConstructorDescriptor : FunctionDescriptor {
class TypeAliasConstructorDescriptorImpl private constructor(
val typeAliasDescriptor: TypeAliasDescriptor,
override val underlyingConstructorDescriptor: ConstructorDescriptor,
override val underlyingConstructorDescriptor: ClassConstructorDescriptor,
original: TypeAliasConstructorDescriptor?,
annotations: Annotations,
kind: Kind,
@@ -54,9 +54,15 @@ class TypeAliasConstructorDescriptorImpl private constructor(
) : TypeAliasConstructorDescriptor,
FunctionDescriptorImpl(typeAliasDescriptor, original, annotations, Name.special("<init>"), kind, source)
{
override fun isPrimary(): Boolean =
underlyingConstructorDescriptor.isPrimary
override fun getContainingDeclaration(): TypeAliasDescriptor =
typeAliasDescriptor
override fun getConstructedClass(): ClassDescriptor =
underlyingConstructorDescriptor.constructedClass
override fun getReturnType(): KotlinType =
super.getReturnType()!!
@@ -103,7 +109,7 @@ class TypeAliasConstructorDescriptorImpl private constructor(
companion object {
fun create(
typeAliasDescriptor: TypeAliasDescriptor,
constructor: ConstructorDescriptor,
constructor: ClassConstructorDescriptor,
substitutor: TypeSubstitutor
): TypeAliasConstructorDescriptor? {
val typeAliasConstructor =
@@ -117,9 +123,10 @@ class TypeAliasConstructorDescriptorImpl private constructor(
val returnType = substitutor.substitute(constructor.returnType, Variance.INVARIANT)
?: return null
val containingDeclaration = constructor.containingDeclaration as ClassDescriptor
val dispatchReceiverParameter =
if (constructor.containingDeclaration.isInner)
constructor.containingDeclaration.thisAsReceiverParameter
if (containingDeclaration.isInner)
containingDeclaration.thisAsReceiverParameter
else
null
@@ -32,8 +32,8 @@ import static org.jetbrains.kotlin.resolve.DescriptorUtils.getDefaultConstructor
import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns;
public class DescriptorFactory {
private static class DefaultConstructorDescriptor extends ConstructorDescriptorImpl {
public DefaultConstructorDescriptor(@NotNull ClassDescriptor containingClass, @NotNull SourceElement source) {
private static class DefaultClassConstructorDescriptor extends ClassConstructorDescriptorImpl {
public DefaultClassConstructorDescriptor(@NotNull ClassDescriptor containingClass, @NotNull SourceElement source) {
super(containingClass, null, Annotations.Companion.getEMPTY(), true, Kind.DECLARATION, source);
initialize(Collections.<ValueParameterDescriptor>emptyList(),
getDefaultConstructorVisibility(containingClass));
@@ -116,11 +116,11 @@ public class DescriptorFactory {
}
@NotNull
public static ConstructorDescriptorImpl createPrimaryConstructorForObject(
public static ClassConstructorDescriptorImpl createPrimaryConstructorForObject(
@NotNull ClassDescriptor containingClass,
@NotNull SourceElement source
) {
return new DefaultConstructorDescriptor(containingClass, source);
return new DefaultClassConstructorDescriptor(containingClass, source);
}
@NotNull
@@ -41,7 +41,7 @@ fun ClassDescriptor.getClassObjectReferenceTarget(): ClassDescriptor = companion
fun DeclarationDescriptor.getImportableDescriptor(): DeclarationDescriptor {
return when {
this is TypeAliasConstructorDescriptor -> typeAliasDescriptor
this is TypeAliasConstructorDescriptor -> containingDeclaration
this is ConstructorDescriptor -> containingDeclaration
this is PropertyAccessorDescriptor -> correspondingProperty
else -> this
@@ -137,7 +137,7 @@ fun ClassDescriptor.getSuperInterfaces(): List<ClassDescriptor> =
else null
}
val ClassDescriptor.secondaryConstructors: List<ConstructorDescriptor>
val ClassDescriptor.secondaryConstructors: List<ClassConstructorDescriptor>
get() = constructors.filterNot { it.isPrimary }
val DeclarationDescriptor.builtIns: KotlinBuiltIns
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.ClassConstructorDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.kotlin.incremental.components.LookupLocation;
@@ -304,7 +304,8 @@ public class ErrorUtils {
super(getErrorModule(), Name.special(name == null ? "<ERROR CLASS>" : "<ERROR CLASS: " + name + ">"),
Modality.OPEN, ClassKind.CLASS, Collections.<KotlinType>emptyList(), SourceElement.NO_SOURCE);
ConstructorDescriptorImpl errorConstructor = ConstructorDescriptorImpl.create(this, Annotations.Companion.getEMPTY(), true, SourceElement.NO_SOURCE);
ClassConstructorDescriptorImpl
errorConstructor = ClassConstructorDescriptorImpl.create(this, Annotations.Companion.getEMPTY(), true, SourceElement.NO_SOURCE);
errorConstructor.initialize(Collections.<ValueParameterDescriptor>emptyList(),
Visibilities.INTERNAL);
MemberScope memberScope = createErrorScope(getName().asString());
@@ -315,7 +316,7 @@ public class ErrorUtils {
)
);
initialize(memberScope, Collections.<ConstructorDescriptor>singleton(errorConstructor), errorConstructor);
initialize(memberScope, Collections.<ClassConstructorDescriptor>singleton(errorConstructor), errorConstructor);
}
@NotNull