Standardize context receiver parameter names

Previously, FIR used `_context_receiver_n` while FE10 used `<this>` for
all context receiver parameters. This commit changes the code in FE10
to follow the convention from FIR.
This commit is contained in:
Steven Schäfer
2022-10-04 15:19:30 +02:00
committed by Alexander Udalov
parent 5cf1a88c42
commit 21fef70367
80 changed files with 544 additions and 1728 deletions
@@ -61,4 +61,8 @@ object NameUtils {
fun propertyDelegateName(propertyName: Name): Name {
return Name.identifier("${propertyName.asString()}\$delegate")
}
@JvmStatic
fun contextReceiverName(index: Int): Name =
Name.identifier("_context_receiver_$index")
}
@@ -20,6 +20,7 @@ import org.jetbrains.annotations.NotNull;
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.scopes.receivers.TransientReceiver;
import org.jetbrains.kotlin.types.KotlinType;
@@ -36,6 +37,10 @@ public abstract class AbstractReceiverParameterDescriptor extends DeclarationDes
super(annotations, SpecialNames.THIS);
}
public AbstractReceiverParameterDescriptor(@NotNull Annotations annotations, @NotNull Name name) {
super(annotations, name);
}
@Nullable
@Override
public ReceiverParameterDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
@@ -628,6 +628,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
List<ReceiverParameterDescriptor> substitutedContextReceiverParameters = new ArrayList<ReceiverParameterDescriptor>();
if (!configuration.newContextReceiverParameters.isEmpty()) {
int index = 0;
for (ReceiverParameterDescriptor newContextReceiverParameter : configuration.newContextReceiverParameters) {
KotlinType substitutedContextReceiverType =
substitutor.substitute(newContextReceiverParameter.getType(), Variance.IN_VARIANCE);
@@ -637,7 +638,8 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
ReceiverParameterDescriptor substitutedContextReceiverParameter =
DescriptorFactory.createContextReceiverParameterForCallable(substitutedDescriptor, substitutedContextReceiverType,
((ImplicitContextReceiver)newContextReceiverParameter.getValue()).getCustomLabelName(),
newContextReceiverParameter.getAnnotations());
newContextReceiverParameter.getAnnotations(),
index++);
substitutedContextReceiverParameters.add(substitutedContextReceiverParameter);
wereChanges[0] |= substitutedContextReceiverType != newContextReceiverParameter.getType();
@@ -20,6 +20,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.name.SpecialNames;
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
@@ -33,7 +35,16 @@ public class ReceiverParameterDescriptorImpl extends AbstractReceiverParameterDe
@NotNull ReceiverValue value,
@NotNull Annotations annotations
) {
super(annotations);
this(containingDeclaration, value, annotations, SpecialNames.THIS);
}
public ReceiverParameterDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull ReceiverValue value,
@NotNull Annotations annotations,
@NotNull Name name
) {
super(annotations, name);
this.containingDeclaration = containingDeclaration;
this.value = value;
}
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.*;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.name.NameUtils;
import org.jetbrains.kotlin.name.StandardClassIds;
import org.jetbrains.kotlin.resolve.scopes.receivers.ContextClassReceiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ContextReceiver;
@@ -231,11 +232,13 @@ public class DescriptorFactory {
@NotNull CallableDescriptor owner,
@Nullable KotlinType receiverParameterType,
@Nullable Name customLabelName,
@NotNull Annotations annotations
@NotNull Annotations annotations,
int index
) {
return receiverParameterType == null
? null
: new ReceiverParameterDescriptorImpl(owner, new ContextReceiver(owner, receiverParameterType, customLabelName, null), annotations);
: new ReceiverParameterDescriptorImpl(owner, new ContextReceiver(owner, receiverParameterType, customLabelName, null), annotations,
NameUtils.contextReceiverName(index));
}
@Nullable
@@ -60,7 +60,7 @@ class MemberDeserializer(private val c: DeserializationContext) {
proto.receiverType(c.typeTable)?.let(local.typeDeserializer::type)?.let { receiverType ->
DescriptorFactory.createExtensionReceiverParameterForCallable(property, receiverType, receiverAnnotations)
},
proto.contextReceiverTypes(c.typeTable).map { it.toContextReceiver(local, property) }
proto.contextReceiverTypes(c.typeTable).mapIndexed { index, type -> type.toContextReceiver(local, property, index) }
)
// Per documentation on Property.getter_flags in metadata.proto, if an accessor flags field is absent, its value should be computed
@@ -212,7 +212,7 @@ class MemberDeserializer(private val c: DeserializationContext) {
DescriptorFactory.createExtensionReceiverParameterForCallable(function, receiverType, receiverAnnotations)
},
getDispatchReceiverParameter(),
proto.contextReceiverTypes(c.typeTable).mapNotNull { it.toContextReceiver(local, function) },
proto.contextReceiverTypes(c.typeTable).mapIndexedNotNull { index, type -> type.toContextReceiver(local, function, index) },
local.typeDeserializer.ownTypeParameters,
local.memberDeserializer.valueParameters(proto.valueParameterList, proto, AnnotatedCallableKind.FUNCTION),
local.typeDeserializer.type(proto.returnType(c.typeTable)),
@@ -355,14 +355,16 @@ class MemberDeserializer(private val c: DeserializationContext) {
private fun ProtoBuf.Type.toContextReceiver(
deserializationContext: DeserializationContext,
callableDescriptor: CallableDescriptor
callableDescriptor: CallableDescriptor,
index: Int
): ReceiverParameterDescriptor? {
val contextReceiverType = deserializationContext.typeDeserializer.type(this)
return DescriptorFactory.createContextReceiverParameterForCallable(
callableDescriptor,
contextReceiverType,
/* customLabelName = */ null/*todo store custom label name in metadata?*/,
Annotations.EMPTY
Annotations.EMPTY,
index
)
}
}