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
@@ -23,28 +23,34 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSubstitutor
class AccessorForConstructorDescriptor(
private val calleeDescriptor: ConstructorDescriptor,
private val calleeDescriptor: ClassConstructorDescriptor,
containingDeclaration: DeclarationDescriptor,
private val superCallTarget: ClassDescriptor?
) : AbstractAccessorForFunctionDescriptor(containingDeclaration, Name.special("<init>")),
ConstructorDescriptor,
ClassConstructorDescriptor,
AccessorForCallableDescriptor<ConstructorDescriptor> {
override fun getCalleeDescriptor(): ConstructorDescriptor = calleeDescriptor
override fun getContainingDeclaration(): ClassDescriptor = calleeDescriptor.containingDeclaration
override fun getConstructedClass(): ClassDescriptor = calleeDescriptor.constructedClass
override fun isPrimary(): Boolean = false
override fun getReturnType(): KotlinType = super.getReturnType()!!
override fun getSuperCallTarget(): ClassDescriptor? = superCallTarget
override fun substitute(substitutor: TypeSubstitutor) = super.substitute(substitutor) as ConstructorDescriptor
override fun substitute(substitutor: TypeSubstitutor) = super.substitute(substitutor) as ClassConstructorDescriptor
override fun copy(
newOwner: DeclarationDescriptor?, modality: Modality?, visibility: Visibility?, kind: CallableMemberDescriptor.Kind?, copyOverrides: Boolean
newOwner: DeclarationDescriptor,
modality: Modality,
visibility: Visibility,
kind: CallableMemberDescriptor.Kind,
copyOverrides: Boolean
): AccessorForConstructorDescriptor {
throw UnsupportedOperationException("Trying to copy synthetic accessor $this")
throw UnsupportedOperationException()
}
override fun getOriginal(): AccessorForConstructorDescriptor = this
@@ -227,7 +227,7 @@ class DefaultParameterValueSubstitutor(val state: GenerationState) {
}
private fun isEmptyConstructorNeeded(constructorDescriptor: ConstructorDescriptor, classOrObject: KtClassOrObject): Boolean {
val classDescriptor = constructorDescriptor.containingDeclaration
val classDescriptor = constructorDescriptor.constructedClass
if (classDescriptor.kind != ClassKind.CLASS) return false
if (classOrObject.isLocal()) return false
@@ -1643,7 +1643,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
assert primaryConstructor != null : "There should be primary constructor for object literal";
ResolvedCall<ConstructorDescriptor> superCall = getDelegationConstructorCall(bindingContext, primaryConstructor);
ResolvedCall<ClassConstructorDescriptor> superCall = getDelegationConstructorCall(bindingContext, primaryConstructor);
if (superCall != null) {
// For an anonymous object, we should also generate all non-default arguments that it captures for its super call
ConstructorDescriptor superConstructor = superCall.getResultingDescriptor();
@@ -1672,7 +1672,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
argumentGenerator.generate(valueArguments, valueArguments);
}
Collection<ConstructorDescriptor> constructors = classDescriptor.getConstructors();
Collection<ClassConstructorDescriptor> constructors = classDescriptor.getConstructors();
assert constructors.size() == 1 : "Unexpected number of constructors for class: " + classDescriptor + " " + constructors;
ConstructorDescriptor constructorDescriptor = CollectionsKt.single(constructors);
@@ -3984,11 +3984,11 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
@NotNull
public ConstructorDescriptor getConstructorDescriptor(@NotNull ResolvedCall<?> resolvedCall) {
public ClassConstructorDescriptor getConstructorDescriptor(@NotNull ResolvedCall<?> resolvedCall) {
FunctionDescriptor accessibleDescriptor = accessibleFunctionDescriptor(resolvedCall);
assert accessibleDescriptor instanceof ConstructorDescriptor :
"getConstructorDescriptor must be called only for constructors: " + accessibleDescriptor;
return (ConstructorDescriptor) accessibleDescriptor;
return (ClassConstructorDescriptor) accessibleDescriptor;
}
@NotNull
@@ -3999,7 +3999,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
v.anew(objectType);
v.dup();
ConstructorDescriptor constructor = getConstructorDescriptor(resolvedCall);
ClassConstructorDescriptor constructor = getConstructorDescriptor(resolvedCall);
ReceiverParameterDescriptor dispatchReceiver = constructor.getDispatchReceiverParameter();
ClassDescriptor containingDeclaration = constructor.getContainingDeclaration();
@@ -383,7 +383,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
try {
lookupConstructorExpressionsInClosureIfPresent();
generatePrimaryConstructor(delegationFieldsInfo);
for (ConstructorDescriptor secondaryConstructor : DescriptorUtilsKt.getSecondaryConstructors(descriptor)) {
for (ClassConstructorDescriptor secondaryConstructor : DescriptorUtilsKt.getSecondaryConstructors(descriptor)) {
generateSecondaryConstructor(secondaryConstructor);
}
}
@@ -919,7 +919,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
private void generatePrimaryConstructor(final DelegationFieldsInfo delegationFieldsInfo) {
if (isInterface(descriptor) || isAnnotationClass(descriptor)) return;
final ConstructorDescriptor constructorDescriptor = descriptor.getUnsubstitutedPrimaryConstructor();
final ClassConstructorDescriptor constructorDescriptor = descriptor.getUnsubstitutedPrimaryConstructor();
if (constructorDescriptor == null) return;
ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor);
@@ -942,7 +942,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
new DefaultParameterValueSubstitutor(state).generatePrimaryConstructorOverloadsIfNeeded(constructorDescriptor, v, this, kind, myClass);
}
private void generateSecondaryConstructor(@NotNull final ConstructorDescriptor constructorDescriptor) {
private void generateSecondaryConstructor(@NotNull final ClassConstructorDescriptor constructorDescriptor) {
if (!canHaveDeclaredConstructors(descriptor)) return;
ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor);
@@ -969,7 +969,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
private void generatePrimaryConstructorImpl(
@NotNull ConstructorDescriptor constructorDescriptor,
@NotNull ClassConstructorDescriptor constructorDescriptor,
@NotNull ExpressionCodegen codegen,
@NotNull DelegationFieldsInfo fieldsInfo,
@Nullable KtPrimaryConstructor primaryConstructor
@@ -1030,7 +1030,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
private void generateSecondaryConstructorImpl(
@NotNull ConstructorDescriptor constructorDescriptor,
@NotNull ClassConstructorDescriptor constructorDescriptor,
@NotNull ExpressionCodegen codegen
) {
InstructionAdapter iv = codegen.v;
@@ -1040,7 +1040,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
markLineNumberForConstructor(constructorDescriptor, constructor, codegen);
ResolvedCall<ConstructorDescriptor> constructorDelegationCall =
ResolvedCall<ClassConstructorDescriptor> constructorDelegationCall =
getDelegationConstructorCall(bindingContext, constructorDescriptor);
ConstructorDescriptor delegateConstructor = constructorDelegationCall == null ? null :
constructorDelegationCall.getResultingDescriptor();
@@ -1061,7 +1061,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
private static void markLineNumberForConstructor(
@NotNull ConstructorDescriptor descriptor,
@NotNull ClassConstructorDescriptor descriptor,
@Nullable KtConstructor constructor,
@NotNull ExpressionCodegen codegen
) {
@@ -1365,8 +1365,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
private void generateDelegatorToConstructorCall(
@NotNull InstructionAdapter iv,
@NotNull ExpressionCodegen codegen,
@NotNull ConstructorDescriptor constructorDescriptor,
@Nullable ResolvedCall<ConstructorDescriptor> delegationConstructorCall
@NotNull ClassConstructorDescriptor constructorDescriptor,
@Nullable ResolvedCall<ClassConstructorDescriptor> delegationConstructorCall
) {
if (delegationConstructorCall == null) {
genSimpleSuperCall(iv);
@@ -21,7 +21,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorBase;
import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.ClassConstructorDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorImpl;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.resolve.scopes.MemberScope;
@@ -126,13 +126,13 @@ public class MutableClassDescriptor extends ClassDescriptorBase implements Class
@NotNull
@Override
public Set<ConstructorDescriptor> getConstructors() {
public Set<ClassConstructorDescriptor> getConstructors() {
return Collections.emptySet();
}
@Override
@Nullable
public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
public ClassConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
return null;
}
@@ -155,7 +155,7 @@ public class MutableClassDescriptor extends ClassDescriptorBase implements Class
this, Annotations.Companion.getEMPTY(), ModalityKt.isFinalClass(this), typeParameters, supertypes
);
for (FunctionDescriptor functionDescriptor : getConstructors()) {
((ConstructorDescriptorImpl) functionDescriptor).setReturnType(getDefaultType());
((ClassConstructorDescriptorImpl) functionDescriptor).setReturnType(getDefaultType());
}
}
@@ -449,8 +449,8 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
(FunctionDescriptor) descriptor, contextDescriptor, superCallTarget, nameSuffix
);
}
else if (descriptor instanceof ConstructorDescriptor) {
accessor = new AccessorForConstructorDescriptor((ConstructorDescriptor) descriptor, contextDescriptor, superCallTarget);
else if (descriptor instanceof ClassConstructorDescriptor) {
accessor = new AccessorForConstructorDescriptor((ClassConstructorDescriptor) descriptor, contextDescriptor, superCallTarget);
}
else if (descriptor instanceof PropertyDescriptor) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
@@ -584,8 +584,8 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
descriptorContext = ExpressionCodegen.getParentContextSubclassOf((ClassDescriptor) enclosed, this);
}
if (descriptorContext == null && descriptor instanceof ConstructorDescriptor) {
ClassDescriptor classDescriptor = ((ConstructorDescriptor) descriptor).getContainingDeclaration();
if (descriptorContext == null && descriptor instanceof ClassConstructorDescriptor) {
ClassDescriptor classDescriptor = ((ClassConstructorDescriptor) descriptor).getContainingDeclaration();
if (DescriptorUtils.isSealedClass(classDescriptor)) {
CodegenContext parentContextForClass = findParentContextWithDescriptor(classDescriptor.getContainingDeclaration());
if (parentContextForClass != null) {
@@ -27,7 +27,7 @@ internal class FictitiousArrayConstructor(arrayClass: ClassDescriptor) : SimpleF
companion object Factory {
@JvmStatic
fun create(arrayConstructor: ConstructorDescriptor): FictitiousArrayConstructor {
val arrayClass = arrayConstructor.containingDeclaration
val arrayClass = arrayConstructor.constructedClass
return FictitiousArrayConstructor(arrayClass).apply {
this.initialize(null, null, arrayConstructor.typeParameters, arrayConstructor.valueParameters, arrayClass.defaultType,
Modality.FINAL, Visibilities.PUBLIC)
@@ -929,9 +929,9 @@ public class KotlinTypeMapper {
? new JvmSignatureWriter()
: new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
if (f instanceof ConstructorDescriptor) {
if (f instanceof ClassConstructorDescriptor) {
sw.writeParametersStart();
writeAdditionalConstructorParameters((ConstructorDescriptor) f, sw);
writeAdditionalConstructorParameters((ClassConstructorDescriptor) f, sw);
for (ValueParameterDescriptor parameter : valueParameters) {
writeParameter(sw, parameter.getType(), f);
@@ -1211,7 +1211,7 @@ public class KotlinTypeMapper {
sw.writeParameterTypeEnd();
}
private void writeAdditionalConstructorParameters(@NotNull ConstructorDescriptor descriptor, @NotNull JvmSignatureWriter sw) {
private void writeAdditionalConstructorParameters(@NotNull ClassConstructorDescriptor descriptor, @NotNull JvmSignatureWriter sw) {
MutableClosure closure = bindingContext.get(CodegenBinding.CLOSURE, descriptor.getContainingDeclaration());
ClassDescriptor captureThis = getDispatchReceiverParameterForConstructorCall(descriptor, closure);
@@ -1260,7 +1260,7 @@ public class KotlinTypeMapper {
// We may generate a slightly wrong signature for a local class / anonymous object in light classes mode but we don't care,
// because such classes are not accessible from the outside world
if (classBuilderMode.generateBodies) {
ResolvedCall<ConstructorDescriptor> superCall = findFirstDelegatingSuperCall(descriptor);
ResolvedCall<ClassConstructorDescriptor> superCall = findFirstDelegatingSuperCall(descriptor);
if (superCall == null) return;
writeSuperConstructorCallParameters(sw, descriptor, superCall, captureThis != null);
}
@@ -1268,8 +1268,8 @@ public class KotlinTypeMapper {
private void writeSuperConstructorCallParameters(
@NotNull JvmSignatureWriter sw,
@NotNull ConstructorDescriptor descriptor,
@NotNull ResolvedCall<ConstructorDescriptor> superCall,
@NotNull ClassConstructorDescriptor descriptor,
@NotNull ResolvedCall<ClassConstructorDescriptor> superCall,
boolean hasOuter
) {
ConstructorDescriptor superDescriptor = SamCodegenUtil.resolveSamAdapter(superCall.getResultingDescriptor());
@@ -1308,10 +1308,10 @@ public class KotlinTypeMapper {
}
@Nullable
private ResolvedCall<ConstructorDescriptor> findFirstDelegatingSuperCall(@NotNull ConstructorDescriptor descriptor) {
private ResolvedCall<ClassConstructorDescriptor> findFirstDelegatingSuperCall(@NotNull ClassConstructorDescriptor descriptor) {
ClassDescriptor classDescriptor = descriptor.getContainingDeclaration();
while (true) {
ResolvedCall<ConstructorDescriptor> next = getDelegationConstructorCall(bindingContext, descriptor);
ResolvedCall<ClassConstructorDescriptor> next = getDelegationConstructorCall(bindingContext, descriptor);
if (next == null) return null;
descriptor = next.getResultingDescriptor();
if (descriptor.getContainingDeclaration() != classDescriptor) return next;