[FE] Support context receivers on classes

This commit is contained in:
Anastasiya Shadrina
2020-08-20 14:11:50 +07:00
committed by TeamCityServer
parent c34fe8d547
commit f4ddf66ac4
19 changed files with 232 additions and 5 deletions
@@ -77,6 +77,10 @@ public interface ClassDescriptor extends ClassifierDescriptorWithTypeParameters,
@NotNull
ReceiverParameterDescriptor getThisAsReceiverParameter();
@NotNull
@ReadOnly
List<ReceiverParameterDescriptor> getContextReceivers();
@Nullable
ClassConstructorDescriptor getUnsubstitutedPrimaryConstructor();
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.storage.StorageManager;
import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner;
import java.util.Collections;
import java.util.List;
public abstract class AbstractClassDescriptor extends ModuleAwareClassDescriptor {
@@ -114,6 +115,12 @@ public abstract class AbstractClassDescriptor extends ModuleAwareClassDescriptor
return thisAsReceiverParameter.invoke();
}
@NotNull
@Override
public List<ReceiverParameterDescriptor> getContextReceivers() {
return Collections.emptyList();
}
@NotNull
@Override
public MemberScope getMemberScope(@NotNull List<? extends TypeProjection> typeArguments, @NotNull KotlinTypeRefiner kotlinTypeRefiner) {
@@ -70,7 +70,7 @@ public class ClassConstructorDescriptorImpl extends FunctionDescriptorImpl imple
@NotNull List<TypeParameterDescriptor> typeParameterDescriptors
) {
super.initialize(
null, calculateDispatchReceiverParameter(), Collections.<ReceiverParameterDescriptor>emptyList(),
null, calculateDispatchReceiverParameter(), calculateAdditionalReceiverParameters(),
typeParameterDescriptors,
unsubstitutedValueParameters, null,
Modality.FINAL, visibility);
@@ -97,6 +97,15 @@ public class ClassConstructorDescriptorImpl extends FunctionDescriptorImpl imple
return null;
}
@NotNull
List<ReceiverParameterDescriptor> calculateAdditionalReceiverParameters() {
ClassDescriptor classDescriptor = getContainingDeclaration();
if (!classDescriptor.getContextReceivers().isEmpty()) {
return classDescriptor.getContextReceivers();
}
return Collections.emptyList();
}
@NotNull
@Override
public ClassDescriptor getContainingDeclaration() {
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class LazySubstitutingClassDescriptor extends ModuleAwareClassDescriptor {
@@ -156,6 +157,12 @@ public class LazySubstitutingClassDescriptor extends ModuleAwareClassDescriptor
throw new UnsupportedOperationException(); // TODO
}
@NotNull
@Override
public List<ReceiverParameterDescriptor> getContextReceivers() {
return Collections.emptyList();
}
@NotNull
@Override
public Collection<ClassConstructorDescriptor> getConstructors() {
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.scopes.receivers
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.types.KotlinType
class ExtensionClassReceiver(
val classDescriptor: ClassDescriptor,
receiverType: KotlinType,
original: ReceiverValue?
): AbstractReceiverValue(receiverType, original), ImplicitReceiver {
override val declarationDescriptor: DeclarationDescriptor
get() = classDescriptor
override fun replaceType(newType: KotlinType): ReceiverValue = ExtensionClassReceiver(classDescriptor, newType, original)
override fun toString(): String = "$type: Ext { $classDescriptor }"
}