Extracted logic of obtaining delegated local variable metadata

This commit is contained in:
Mikhael Bogdanov
2016-05-11 14:49:18 +03:00
parent c06b51c1d1
commit 48c8678281
4 changed files with 21 additions and 12 deletions
@@ -1664,8 +1664,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
int index = myFrameMap.enter(variableDescriptor, type); int index = myFrameMap.enter(variableDescriptor, type);
if (isDelegatedLocalVariable(variableDescriptor)) { if (isDelegatedLocalVariable(variableDescriptor)) {
VariableDescriptor metadataVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_PROPERTY_METADATA, variableDescriptor); myFrameMap.enter(getDelegatedLocalVariableMetadata(variableDescriptor, bindingContext), AsmTypes.K_PROPERTY0_TYPE);
myFrameMap.enter(metadataVariableDescriptor, AsmTypes.K_PROPERTY0_TYPE);
} }
if (isSharedVarType(type)) { if (isSharedVarType(type)) {
@@ -1715,8 +1714,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
@Override @Override
public Void fun(StackValue answer) { public Void fun(StackValue answer) {
if (isDelegatedLocalVariable(variableDescriptor)) { if (isDelegatedLocalVariable(variableDescriptor)) {
VariableDescriptor metadataVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_PROPERTY_METADATA, variableDescriptor); myFrameMap.leave(getDelegatedLocalVariableMetadata(variableDescriptor, bindingContext));
myFrameMap.leave(metadataVariableDescriptor);
} }
int index = myFrameMap.leave(variableDescriptor); int index = myFrameMap.leave(variableDescriptor);
@@ -3439,10 +3437,11 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
return StackValue.none(); return StackValue.none();
} }
@Nullable @NotNull
private StackValue getVariableMetadataValue(VariableDescriptor variableDescriptor) { private StackValue getVariableMetadataValue(VariableDescriptor variableDescriptor) {
VariableDescriptor metadataVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_PROPERTY_METADATA, variableDescriptor); StackValue value = findLocalOrCapturedValue(getDelegatedLocalVariableMetadata(variableDescriptor, bindingContext));
return metadataVariableDescriptor != null ? findLocalOrCapturedValue(metadataVariableDescriptor) : null; assert value != null : "Can't find stack value for local delegated variable metadata: " + variableDescriptor;
return value;
} }
@NotNull @NotNull
@@ -3451,7 +3450,6 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
VariableDescriptorWithAccessors variableDescriptor = (VariableDescriptorWithAccessors) descriptor; VariableDescriptorWithAccessors variableDescriptor = (VariableDescriptorWithAccessors) descriptor;
StackValue metadataValue = getVariableMetadataValue(variableDescriptor); StackValue metadataValue = getVariableMetadataValue(variableDescriptor);
assert metadataValue != null : variableDescriptor;
return JvmCodegenUtil.delegatedVariableValue(varValue, metadataValue, variableDescriptor, bindingContext, typeMapper); return JvmCodegenUtil.delegatedVariableValue(varValue, metadataValue, variableDescriptor, bindingContext, typeMapper);
} }
@@ -3484,8 +3482,8 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
storeTo.storeSelector(initializer.type, v); storeTo.storeSelector(initializer.type, v);
StackValue metadataValue = getVariableMetadataValue(variableDescriptor); if (isDelegatedLocalVariable(variableDescriptor)) {
if (metadataValue != null) { StackValue metadataValue = getVariableMetadataValue(variableDescriptor);
initializePropertyMetadata((KtProperty) variableDeclaration, variableDescriptor, metadataValue); initializePropertyMetadata((KtProperty) variableDeclaration, variableDescriptor, metadataValue);
invokePropertyDelegatedOnLocalVar(variableDescriptor, storeTo, metadataValue); invokePropertyDelegatedOnLocalVar(variableDescriptor, storeTo, metadataValue);
} }
@@ -48,10 +48,11 @@ import org.jetbrains.kotlin.types.KotlinType;
import java.io.File; 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_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.ABSTRACT;
import static org.jetbrains.kotlin.descriptors.Modality.FINAL; 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.BindingContext.DELEGATED_PROPERTY_CALL;
import static org.jetbrains.kotlin.resolve.jvm.annotations.AnnotationUtilKt.hasJvmFieldAnnotation;
public class JvmCodegenUtil { public class JvmCodegenUtil {
@@ -250,4 +250,14 @@ public class CodegenBinding {
return allInnerClasses; 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;
}
} }
@@ -58,7 +58,6 @@ public interface LocalLookup {
boolean delegatedVar = false; boolean delegatedVar = false;
VariableDescriptor delegateVariableDescriptor = state.getBindingContext().get(LOCAL_VARIABLE_DELEGATE, vd); VariableDescriptor delegateVariableDescriptor = state.getBindingContext().get(LOCAL_VARIABLE_DELEGATE, vd);
VariableDescriptor metadataVariableDescriptor = state.getBindingContext().get(LOCAL_VARIABLE_PROPERTY_METADATA, vd);
if (delegateVariableDescriptor != null) { if (delegateVariableDescriptor != null) {
vd = delegateVariableDescriptor; vd = delegateVariableDescriptor;
delegatedVar = true; delegatedVar = true;
@@ -81,6 +80,7 @@ public interface LocalLookup {
else { else {
innerValue = StackValue.field(type, classType, fieldName, false, thiz, vd); innerValue = StackValue.field(type, classType, fieldName, false, thiz, vd);
if (delegatedVar) { if (delegatedVar) {
VariableDescriptor metadataVariableDescriptor = getDelegatedLocalVariableMetadata(originalVariableDescriptor, state.getBindingContext());
StackValue metadataValue = innerValue(metadataVariableDescriptor, localLookup, state, closure, classType); StackValue metadataValue = innerValue(metadataVariableDescriptor, localLookup, state, closure, classType);
assert metadataValue != null : originalVariableDescriptor; assert metadataValue != null : originalVariableDescriptor;