From 48c8678281015af2538d5ab0876fb1af82289c2f Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Wed, 11 May 2016 14:49:18 +0300 Subject: [PATCH] Extracted logic of obtaining delegated local variable metadata --- .../kotlin/codegen/ExpressionCodegen.java | 18 ++++++++---------- .../kotlin/codegen/JvmCodegenUtil.java | 3 ++- .../kotlin/codegen/binding/CodegenBinding.java | 10 ++++++++++ .../kotlin/codegen/context/LocalLookup.java | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index a8db83ebc6f..2d1318429aa 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -1664,8 +1664,7 @@ public class ExpressionCodegen extends KtVisitor impleme int index = myFrameMap.enter(variableDescriptor, type); if (isDelegatedLocalVariable(variableDescriptor)) { - VariableDescriptor metadataVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_PROPERTY_METADATA, variableDescriptor); - myFrameMap.enter(metadataVariableDescriptor, AsmTypes.K_PROPERTY0_TYPE); + myFrameMap.enter(getDelegatedLocalVariableMetadata(variableDescriptor, bindingContext), AsmTypes.K_PROPERTY0_TYPE); } if (isSharedVarType(type)) { @@ -1715,8 +1714,7 @@ public class ExpressionCodegen extends KtVisitor impleme @Override public Void fun(StackValue answer) { if (isDelegatedLocalVariable(variableDescriptor)) { - VariableDescriptor metadataVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_PROPERTY_METADATA, variableDescriptor); - myFrameMap.leave(metadataVariableDescriptor); + myFrameMap.leave(getDelegatedLocalVariableMetadata(variableDescriptor, bindingContext)); } int index = myFrameMap.leave(variableDescriptor); @@ -3439,10 +3437,11 @@ public class ExpressionCodegen extends KtVisitor impleme return StackValue.none(); } - @Nullable + @NotNull private StackValue getVariableMetadataValue(VariableDescriptor variableDescriptor) { - VariableDescriptor metadataVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_PROPERTY_METADATA, variableDescriptor); - return metadataVariableDescriptor != null ? findLocalOrCapturedValue(metadataVariableDescriptor) : null; + StackValue value = findLocalOrCapturedValue(getDelegatedLocalVariableMetadata(variableDescriptor, bindingContext)); + assert value != null : "Can't find stack value for local delegated variable metadata: " + variableDescriptor; + return value; } @NotNull @@ -3451,7 +3450,6 @@ public class ExpressionCodegen extends KtVisitor impleme VariableDescriptorWithAccessors variableDescriptor = (VariableDescriptorWithAccessors) descriptor; StackValue metadataValue = getVariableMetadataValue(variableDescriptor); - assert metadataValue != null : variableDescriptor; return JvmCodegenUtil.delegatedVariableValue(varValue, metadataValue, variableDescriptor, bindingContext, typeMapper); } @@ -3484,8 +3482,8 @@ public class ExpressionCodegen extends KtVisitor impleme storeTo.storeSelector(initializer.type, v); - StackValue metadataValue = getVariableMetadataValue(variableDescriptor); - if (metadataValue != null) { + if (isDelegatedLocalVariable(variableDescriptor)) { + StackValue metadataValue = getVariableMetadataValue(variableDescriptor); initializePropertyMetadata((KtProperty) variableDeclaration, variableDescriptor, metadataValue); invokePropertyDelegatedOnLocalVar(variableDescriptor, storeTo, metadataValue); } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java index 031056797bc..9b885fe8460 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java @@ -48,10 +48,11 @@ import org.jetbrains.kotlin.types.KotlinType; import java.io.File; import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.LOCAL_VARIABLE_DELEGATE; +import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.LOCAL_VARIABLE_PROPERTY_METADATA; import static org.jetbrains.kotlin.descriptors.Modality.ABSTRACT; import static org.jetbrains.kotlin.descriptors.Modality.FINAL; -import static org.jetbrains.kotlin.resolve.jvm.annotations.AnnotationUtilKt.hasJvmFieldAnnotation; import static org.jetbrains.kotlin.resolve.BindingContext.DELEGATED_PROPERTY_CALL; +import static org.jetbrains.kotlin.resolve.jvm.annotations.AnnotationUtilKt.hasJvmFieldAnnotation; public class JvmCodegenUtil { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenBinding.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenBinding.java index 612f4997f30..32dd336f4f0 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenBinding.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenBinding.java @@ -250,4 +250,14 @@ public class CodegenBinding { return allInnerClasses; } + + @NotNull + public static VariableDescriptor getDelegatedLocalVariableMetadata( + @NotNull VariableDescriptor variableDescriptor, + @NotNull BindingContext bindingContext + ) { + VariableDescriptor metadataVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_PROPERTY_METADATA, variableDescriptor); + assert metadataVariableDescriptor != null : "Metadata for local delegated property should be not null: " + variableDescriptor; + return metadataVariableDescriptor; + } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/context/LocalLookup.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/context/LocalLookup.java index 5637d41b308..43121dde452 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/context/LocalLookup.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/context/LocalLookup.java @@ -58,7 +58,6 @@ public interface LocalLookup { boolean delegatedVar = false; VariableDescriptor delegateVariableDescriptor = state.getBindingContext().get(LOCAL_VARIABLE_DELEGATE, vd); - VariableDescriptor metadataVariableDescriptor = state.getBindingContext().get(LOCAL_VARIABLE_PROPERTY_METADATA, vd); if (delegateVariableDescriptor != null) { vd = delegateVariableDescriptor; delegatedVar = true; @@ -81,6 +80,7 @@ public interface LocalLookup { else { innerValue = StackValue.field(type, classType, fieldName, false, thiz, vd); if (delegatedVar) { + VariableDescriptor metadataVariableDescriptor = getDelegatedLocalVariableMetadata(originalVariableDescriptor, state.getBindingContext()); StackValue metadataValue = innerValue(metadataVariableDescriptor, localLookup, state, closure, classType); assert metadataValue != null : originalVariableDescriptor;