[k1] add information about context receiver labels to ContextClassReceiver/ContextReceiver

This commit is contained in:
Ilya Kirillov
2022-06-30 17:50:30 +02:00
parent c1ec1d9d4c
commit 2f822ab4d9
12 changed files with 70 additions and 22 deletions
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationsKt;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.resolve.DescriptorFactory;
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitContextReceiver;
import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.utils.SmartList;
@@ -635,6 +636,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
}
ReceiverParameterDescriptor substitutedContextReceiverParameter =
DescriptorFactory.createContextReceiverParameterForCallable(substitutedDescriptor, substitutedContextReceiverType,
((ImplicitContextReceiver)newContextReceiverParameter.getValue()).getCustomLabelName(),
newContextReceiverParameter.getAnnotations());
substitutedContextReceiverParameters.add(substitutedContextReceiverParameter);
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.resolve.scopes.receivers.ContextReceiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitContextReceiver;
import org.jetbrains.kotlin.types.*;
import org.jetbrains.kotlin.utils.SmartSet;
@@ -561,7 +562,10 @@ public class PropertyDescriptorImpl extends VariableDescriptorWithInitializerImp
if (substitutedType == null) return null;
return new ReceiverParameterDescriptorImpl(
substitutedPropertyDescriptor,
new ContextReceiver(substitutedPropertyDescriptor, substitutedType, receiverParameterDescriptor.getValue()),
new ContextReceiver(substitutedPropertyDescriptor,
substitutedType,
((ImplicitContextReceiver) receiverParameterDescriptor.getValue()).getCustomLabelName(),
receiverParameterDescriptor.getValue()),
receiverParameterDescriptor.getAnnotations()
);
}
@@ -22,6 +22,7 @@ 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.scopes.receivers.ImplicitContextReceiver
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.types.*
@@ -209,6 +210,7 @@ class TypeAliasConstructorDescriptorImpl private constructor(
DescriptorFactory.createContextReceiverParameterForClass(
classDescriptor,
substitutorForUnderlyingClass.safeSubstitute(it.type, Variance.INVARIANT),
(it.value as ImplicitContextReceiver).customLabelName,
Annotations.EMPTY
)
}
@@ -200,21 +200,23 @@ public class DescriptorFactory {
public static ReceiverParameterDescriptor createContextReceiverParameterForCallable(
@NotNull CallableDescriptor owner,
@Nullable KotlinType receiverParameterType,
@Nullable Name customLabelName,
@NotNull Annotations annotations
) {
return receiverParameterType == null
? null
: new ReceiverParameterDescriptorImpl(owner, new ContextReceiver(owner, receiverParameterType, null), annotations);
: new ReceiverParameterDescriptorImpl(owner, new ContextReceiver(owner, receiverParameterType, customLabelName, null), annotations);
}
@Nullable
public static ReceiverParameterDescriptor createContextReceiverParameterForClass(
@NotNull ClassDescriptor owner,
@Nullable KotlinType receiverParameterType,
@Nullable Name customLabelName,
@NotNull Annotations annotations
) {
return receiverParameterType == null
? null
: new ReceiverParameterDescriptorImpl(owner, new ContextClassReceiver(owner, receiverParameterType, null), annotations);
: new ReceiverParameterDescriptorImpl(owner, new ContextClassReceiver(owner, receiverParameterType, customLabelName, null), annotations);
}
}
@@ -7,17 +7,19 @@ package org.jetbrains.kotlin.resolve.scopes.receivers
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.KotlinType
class ContextClassReceiver(
val classDescriptor: ClassDescriptor,
receiverType: KotlinType,
override val customLabelName: Name?,
original: ReceiverValue?
): AbstractReceiverValue(receiverType, original), ImplicitReceiver {
): AbstractReceiverValue(receiverType, original), ImplicitContextReceiver {
override val declarationDescriptor: DeclarationDescriptor
get() = classDescriptor
override fun replaceType(newType: KotlinType): ReceiverValue = ContextClassReceiver(classDescriptor, newType, original)
override fun replaceType(newType: KotlinType): ReceiverValue = ContextClassReceiver(classDescriptor, newType, customLabelName, original)
override fun toString(): String = "$type: Ctx { $classDescriptor }"
}
@@ -6,14 +6,16 @@
package org.jetbrains.kotlin.resolve.scopes.receivers
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.KotlinType
class ContextReceiver(
override val declarationDescriptor: CallableDescriptor,
receiverType: KotlinType,
override val customLabelName: Name?,
original: ReceiverValue?
) : AbstractReceiverValue(receiverType, original), ImplicitReceiver {
override fun replaceType(newType: KotlinType): ReceiverValue = ContextReceiver(declarationDescriptor, newType, original)
) : AbstractReceiverValue(receiverType, original), ImplicitContextReceiver {
override fun replaceType(newType: KotlinType): ReceiverValue = ContextReceiver(declarationDescriptor, newType, customLabelName, original)
override fun toString(): String = "Cxt { $declarationDescriptor }"
}
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.resolve.scopes.receivers
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.name.Name
/**
* Describes an implicit "this" receiver
@@ -24,3 +25,8 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
interface ImplicitReceiver : ReceiverValue {
val declarationDescriptor: DeclarationDescriptor
}
interface ImplicitContextReceiver : ImplicitReceiver {
val customLabelName: Name?
}
@@ -358,6 +358,11 @@ class MemberDeserializer(private val c: DeserializationContext) {
callableDescriptor: CallableDescriptor
): ReceiverParameterDescriptor? {
val contextReceiverType = deserializationContext.typeDeserializer.type(this)
return DescriptorFactory.createContextReceiverParameterForCallable(callableDescriptor, contextReceiverType, Annotations.EMPTY)
return DescriptorFactory.createContextReceiverParameterForCallable(
callableDescriptor,
contextReceiverType,
/* customLabelName = */ null/*todo store custom label name in metadata?*/,
Annotations.EMPTY
)
}
}
@@ -150,7 +150,12 @@ class DeserializedClassDescriptor(
val contextReceiverType = c.typeDeserializer.type(it)
ReceiverParameterDescriptorImpl(
thisAsReceiverParameter,
ContextClassReceiver(this, contextReceiverType, null),
ContextClassReceiver(
this,
contextReceiverType,
/* customLabelName = */ null/*todo store custom label name in metadata?*/,
null
),
Annotations.EMPTY
);
}