Support new convention for local variables name mangling: '$receiver' -> '$this$<label>' (KT-26913)

This commit is contained in:
Yan Zhulanow
2018-12-06 15:38:56 +09:00
parent b5f9149702
commit ea462091c4
23 changed files with 100 additions and 61 deletions
@@ -103,6 +103,8 @@ public class AsmUtil {
public static final String LABELED_THIS_FIELD = THIS + "_";
public static final String LABELED_THIS_PARAMETER = "$" + THIS + "$";
public static final String CAPTURED_THIS_FIELD = "this$0";
public static final String RECEIVER_PARAMETER_NAME = "$receiver";
@@ -141,13 +143,35 @@ public class AsmUtil {
}
@NotNull
public static String getLabeledThisNameForReceiver(
public static String getNameForCapturedReceiverField(
@NotNull CallableDescriptor descriptor,
@NotNull BindingContext bindingContext,
@NotNull LanguageVersionSettings languageVersionSettings
) {
return getLabeledThisNameForReceiver(
descriptor, bindingContext, languageVersionSettings, LABELED_THIS_FIELD, CAPTURED_RECEIVER_FIELD);
}
@NotNull
public static String getNameForReceiverParameter(
@NotNull CallableDescriptor descriptor,
@NotNull BindingContext bindingContext,
@NotNull LanguageVersionSettings languageVersionSettings
) {
return getLabeledThisNameForReceiver(
descriptor, bindingContext, languageVersionSettings, LABELED_THIS_PARAMETER, RECEIVER_PARAMETER_NAME);
}
@NotNull
private static String getLabeledThisNameForReceiver(
@NotNull CallableDescriptor descriptor,
@NotNull BindingContext bindingContext,
@NotNull LanguageVersionSettings languageVersionSettings,
@NotNull String prefix,
@NotNull String defaultName
) {
if (!languageVersionSettings.supportsFeature(LanguageFeature.NewCapturedReceiverFieldNamingConvention)) {
return CAPTURED_RECEIVER_FIELD;
return defaultName;
}
Name callableName = null;
@@ -155,7 +179,7 @@ public class AsmUtil {
if (descriptor instanceof FunctionDescriptor) {
String labelName = bindingContext.get(CodegenBinding.CALL_LABEL_FOR_LAMBDA_ARGUMENT, (FunctionDescriptor) descriptor);
if (labelName != null) {
return getLabeledThisName(labelName, CAPTURED_RECEIVER_FIELD);
return getLabeledThisName(labelName, prefix, defaultName);
}
if (descriptor instanceof VariableAccessorDescriptor) {
@@ -169,23 +193,23 @@ public class AsmUtil {
}
if (callableName.isSpecial()) {
return CAPTURED_RECEIVER_FIELD;
return defaultName;
}
return getLabeledThisName(callableName.asString(), CAPTURED_RECEIVER_FIELD);
return getLabeledThisName(callableName.asString(), prefix, defaultName);
}
@NotNull
private static String getLabeledThisName(@NotNull String callableName, @NotNull String defaultName) {
public static String getLabeledThisName(@NotNull String callableName, @NotNull String prefix, @NotNull String defaultName) {
if (!Name.isValidIdentifier(callableName)) {
return defaultName;
}
if (!DalvikIdentifierUtils.isValidDalvikIdentifier(callableName)) {
return LABELED_THIS_FIELD + mangleLabel(callableName);
return prefix + mangleLabel(callableName);
}
return LABELED_THIS_FIELD + callableName;
return prefix + callableName;
}
private static String mangleLabel(String label) {
@@ -925,7 +949,8 @@ public class AsmUtil {
if (descriptor.isOperator()) {
ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
if (receiverParameter != null) {
genParamAssertion(v, state.getTypeMapper(), frameMap, receiverParameter, CAPTURED_RECEIVER_FIELD, descriptor);
String name = getNameForReceiverParameter(descriptor, state.getBindingContext(), state.getLanguageVersionSettings());
genParamAssertion(v, state.getTypeMapper(), frameMap, receiverParameter, name, descriptor);
}
}
return;
@@ -933,7 +958,8 @@ public class AsmUtil {
ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
if (receiverParameter != null) {
genParamAssertion(v, state.getTypeMapper(), frameMap, receiverParameter, CAPTURED_RECEIVER_FIELD, descriptor);
String name = getNameForReceiverParameter(descriptor, state.getBindingContext(), state.getLanguageVersionSettings());
genParamAssertion(v, state.getTypeMapper(), frameMap, receiverParameter, name, descriptor);
}
for (ValueParameterDescriptor parameter : descriptor.getValueParameters()) {
@@ -160,7 +160,7 @@ class DefaultParameterValueSubstitutor(val state: GenerationState) {
if (!state.classBuilderMode.generateBodies) {
FunctionCodegen.generateLocalVariablesForParameters(
mv, signature, null, Label(), Label(), remainingParameters, isStatic, typeMapper
mv, signature, functionDescriptor, null, Label(), Label(), remainingParameters, isStatic, typeMapper
)
mv.visitEnd()
return
@@ -245,7 +245,7 @@ class DefaultParameterValueSubstitutor(val state: GenerationState) {
mv.visitLabel(methodEnd)
FunctionCodegen.generateLocalVariablesForParameters(
mv, signature, null, methodBegin, methodEnd, remainingParameters, isStatic, typeMapper
mv, signature, functionDescriptor, null, methodBegin, methodEnd, remainingParameters, isStatic, typeMapper
)
FunctionCodegen.endVisit(mv, null, methodElement)
@@ -784,7 +784,7 @@ public class FunctionCodegen {
}
generateLocalVariablesForParameters(mv,
jvmMethodSignature,
jvmMethodSignature, functionDescriptor,
thisType, methodBegin, methodEnd, functionDescriptor.getValueParameters(),
destructuredParametersForSuspendLambda,
AsmUtil.isStaticMethod(ownerKind, functionDescriptor), typeMapper, shiftForDestructuringVariables
@@ -794,6 +794,7 @@ public class FunctionCodegen {
public static void generateLocalVariablesForParameters(
@NotNull MethodVisitor mv,
@NotNull JvmMethodSignature jvmMethodSignature,
@NotNull FunctionDescriptor functionDescriptor,
@Nullable Type thisType,
@NotNull Label methodBegin,
@NotNull Label methodEnd,
@@ -802,13 +803,15 @@ public class FunctionCodegen {
KotlinTypeMapper typeMapper
) {
generateLocalVariablesForParameters(
mv, jvmMethodSignature, thisType, methodBegin, methodEnd, valueParameters, Collections.emptyList(), isStatic, typeMapper,
mv, jvmMethodSignature, functionDescriptor,
thisType, methodBegin, methodEnd, valueParameters, Collections.emptyList(), isStatic, typeMapper,
0);
}
private static void generateLocalVariablesForParameters(
@NotNull MethodVisitor mv,
@NotNull JvmMethodSignature jvmMethodSignature,
@NotNull FunctionDescriptor functionDescriptor,
@Nullable Type thisType,
@NotNull Label methodBegin,
@NotNull Label methodEnd,
@@ -847,6 +850,10 @@ public class FunctionCodegen {
? computeParameterName(i, parameter)
: nameForDestructuredParameter;
}
else if (kind == JvmMethodParameterKind.RECEIVER) {
parameterName = AsmUtil.getNameForReceiverParameter(
functionDescriptor, typeMapper.getBindingContext(), typeMapper.getLanguageVersionSettings());
}
else {
String lowercaseKind = kind.name().toLowerCase();
parameterName = needIndexForVar(kind)
@@ -111,7 +111,7 @@ public final class MutableClosure implements CalculatedClosure {
return AsmUtil.CAPTURED_RECEIVER_FIELD;
}
String labeledThis = AsmUtil.getLabeledThisNameForReceiver(
String labeledThis = AsmUtil.getNameForCapturedReceiverField(
enclosingFunWithReceiverDescriptor, bindingContext, languageVersionSettings);
return AsmUtil.getCapturedFieldName(labeledThis);
@@ -46,7 +46,9 @@ internal fun generateParameterNames(
isEnumName = !isEnumName
if (!isEnumName) "\$enum\$name" else "\$enum\$ordinal"
}
JvmMethodParameterKind.RECEIVER -> AsmUtil.RECEIVER_PARAMETER_NAME
JvmMethodParameterKind.RECEIVER -> {
AsmUtil.getNameForReceiverParameter(functionDescriptor, state.bindingContext, state.languageVersionSettings)
}
JvmMethodParameterKind.OUTER -> AsmUtil.CAPTURED_THIS_FIELD
JvmMethodParameterKind.VALUE -> iterator.next().name.asString()
@@ -217,6 +217,10 @@ public class KotlinTypeMapper {
return jvmTarget;
}
public LanguageVersionSettings getLanguageVersionSettings() {
return languageVersionSettings;
}
@NotNull
public Type mapOwner(@NotNull DeclarationDescriptor descriptor) {
return mapOwner(descriptor, true);
@@ -5,21 +5,21 @@ public final class DeepInlineKt : java/lang/Object {
1 $i$f$block: I
}
public final static void foo(java.lang.String $receiver, int $i$a$-block-DeepInlineKt$foo$1$1$1) {
public final static void foo(java.lang.String $this$block, int $i$a$-block-DeepInlineKt$foo$1$1$1) {
Local variables:
14 $receiver: J
14 $this$block: J
16 $i$a$-block-DeepInlineKt$foo$1$1$1: I
13 $i$f$block: I
12 z: Z
9 $receiver: J
9 $this$block: J
11 $i$a$-block-DeepInlineKt$foo$1$1: I
8 $i$f$block: I
7 y: Z
4 $receiver: J
4 $this$block: J
6 $i$a$-block-DeepInlineKt$foo$1: I
3 $i$f$block: I
2 x: Z
0 $receiver: Ljava/lang/String;
0 $this$foo: Ljava/lang/String;
1 count: I
}
}
@@ -5,21 +5,21 @@ public final class DeepInlineWithLabelsKt : java/lang/Object {
1 $i$f$block: I
}
public final static void foo(java.lang.String $receiver, int $i$a$-block-DeepInlineWithLabelsKt$foo$1$1$1) {
public final static void foo(java.lang.String $this$b3, int $i$a$-block-DeepInlineWithLabelsKt$foo$1$1$1) {
Local variables:
14 $receiver: J
14 $this$b3: J
16 $i$a$-block-DeepInlineWithLabelsKt$foo$1$1$1: I
13 $i$f$block: I
12 z: Z
9 $receiver: J
9 $this$b2: J
11 $i$a$-block-DeepInlineWithLabelsKt$foo$1$1: I
8 $i$f$block: I
7 y: Z
4 $receiver: J
4 $this$b1: J
6 $i$a$-block-DeepInlineWithLabelsKt$foo$1: I
3 $i$f$block: I
2 x: Z
0 $receiver: Ljava/lang/String;
0 $this$foo: Ljava/lang/String;
1 count: I
}
}
@@ -9,10 +9,10 @@ final class DeepNoinlineWithLabels_afterKt$foo$1$1$1 : kotlin/jvm/internal/Lambd
public java.lang.Object invoke(java.lang.Object p0)
public final void invoke(long $receiver) {
public final void invoke(long $this$b3) {
Local variables:
0 this: LDeepNoinlineWithLabels_afterKt$foo$1$1$1;
1 $receiver: J
1 $this$b3: J
}
}
@@ -31,7 +31,7 @@ final class DeepNoinlineWithLabels_afterKt$foo$1$1 : kotlin/jvm/internal/Lambda,
Local variables:
3 z: Z
0 this: LDeepNoinlineWithLabels_afterKt$foo$1$1;
1 $receiver: J
1 $this$b2: J
}
}
@@ -50,7 +50,7 @@ final class DeepNoinlineWithLabels_afterKt$foo$1 : kotlin/jvm/internal/Lambda, k
Local variables:
3 y: Z
0 this: LDeepNoinlineWithLabels_afterKt$foo$1;
1 $receiver: J
1 $this$b1: J
}
}
@@ -60,10 +60,10 @@ public final class DeepNoinlineWithLabels_afterKt : java/lang/Object {
0 block: Lkotlin/jvm/functions/Function1;
}
public final static void foo(java.lang.String x, int $receiver) {
public final static void foo(java.lang.String x, int $this$foo) {
Local variables:
2 x: Z
0 $receiver: Ljava/lang/String;
0 $this$foo: Ljava/lang/String;
1 count: I
}
}
@@ -7,10 +7,10 @@ final class DeepNoinline_afterKt$foo$1$1$1 : kotlin/jvm/internal/Lambda, kotlin/
public java.lang.Object invoke(java.lang.Object p0)
public final void invoke(long $receiver) {
public final void invoke(long $this$block) {
Local variables:
0 this: LDeepNoinline_afterKt$foo$1$1$1;
1 $receiver: J
1 $this$block: J
}
}
@@ -27,7 +27,7 @@ final class DeepNoinline_afterKt$foo$1$1 : kotlin/jvm/internal/Lambda, kotlin/jv
Local variables:
3 z: Z
0 this: LDeepNoinline_afterKt$foo$1$1;
1 $receiver: J
1 $this$block: J
}
}
@@ -46,7 +46,7 @@ final class DeepNoinline_afterKt$foo$1 : kotlin/jvm/internal/Lambda, kotlin/jvm/
Local variables:
3 y: Z
0 this: LDeepNoinline_afterKt$foo$1;
1 $receiver: J
1 $this$block: J
}
}
@@ -56,10 +56,10 @@ public final class DeepNoinline_afterKt : java/lang/Object {
0 block: Lkotlin/jvm/functions/Function1;
}
public final static void foo(java.lang.String x, int $receiver) {
public final static void foo(java.lang.String x, int $this$foo) {
Local variables:
2 x: Z
0 $receiver: Ljava/lang/String;
0 $this$foo: Ljava/lang/String;
1 count: I
}
}
@@ -5,13 +5,13 @@ public final class InlineReceiversKt : java/lang/Object {
1 $i$f$block: I
}
public final static void foo(java.lang.String $receiver, int $i$a$-block-InlineReceiversKt$foo$1) {
public final static void foo(java.lang.String $this$block, int $i$a$-block-InlineReceiversKt$foo$1) {
Local variables:
4 $receiver: J
4 $this$block: J
6 $i$a$-block-InlineReceiversKt$foo$1: I
3 $i$f$block: I
2 x: Z
0 $receiver: Ljava/lang/String;
0 $this$foo: Ljava/lang/String;
1 count: I
}
}
@@ -9,10 +9,10 @@ final class NonInlineReceivers_afterKt$foo$1 : kotlin/jvm/internal/Lambda, kotli
public java.lang.Object invoke(java.lang.Object p0)
public final void invoke(long $receiver) {
public final void invoke(long $this$block) {
Local variables:
0 this: LNonInlineReceivers_afterKt$foo$1;
1 $receiver: J
1 $this$block: J
}
}
@@ -22,10 +22,10 @@ public final class NonInlineReceivers_afterKt : java/lang/Object {
0 block: Lkotlin/jvm/functions/Function1;
}
public final static void foo(java.lang.String x, int $receiver) {
public final static void foo(java.lang.String x, int $this$foo) {
Local variables:
2 x: Z
0 $receiver: Ljava/lang/String;
0 $this$foo: Ljava/lang/String;
1 count: I
}
}
@@ -1,3 +1,3 @@
public final class SimpleKt : java/lang/Object {
public final static void foo(java.lang.String $receiver, int a)
public final static void foo(java.lang.String $this$foo, int a)
}