From 6f761d4e7da15bcfb2f4871eb26ce77f4ad4569a Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Fri, 3 Jun 2016 16:57:55 +0300 Subject: [PATCH] Construct delegates in expression codegen, not in LocalLookup --- .../kotlin/codegen/ExpressionCodegen.java | 45 ++++++++++++++++++- .../codegen/ImplementationBodyCodegen.java | 2 +- .../kotlin/codegen/JvmCodegenUtil.java | 21 --------- .../kotlin/codegen/context/LocalLookup.java | 22 +-------- 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 5613afa1a76..f2fac4bcc05 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -2330,7 +2330,28 @@ public class ExpressionCodegen extends KtVisitor impleme return lookupCapturedValueInConstructorParameters(descriptor); } - return context.lookupInContext(descriptor, StackValue.LOCAL_0, state, false); + return lookupInContext(descriptor, StackValue.LOCAL_0, state, false, context); + } + + @Nullable + StackValue lookupInContext( + @NotNull DeclarationDescriptor descriptor, + @NotNull StackValue prefix, + @NotNull GenerationState state, + boolean ignoreNoOuter, + @NotNull CodegenContext context + ) { + StackValue value = context.lookupInContext(descriptor, prefix, state, ignoreNoOuter); + if(!isDelegatedLocalVariable(descriptor) || value == null) { + return value; + } + + + VariableDescriptor metadata = getDelegatedLocalVariableMetadata((VariableDescriptor) descriptor, state.getBindingContext()); + StackValue metadataValue = context.lookupInContext(metadata, prefix, state, ignoreNoOuter); + assert metadataValue != null : "Metadata stack value should be non-null for local delegated property"; + return delegatedVariableValue(value, metadataValue, (VariableDescriptorWithAccessors) descriptor, state.getBindingContext(), + state.getTypeMapper()); } @Nullable @@ -3752,7 +3773,7 @@ public class ExpressionCodegen extends KtVisitor impleme VariableDescriptorWithAccessors variableDescriptor = (VariableDescriptorWithAccessors) descriptor; StackValue metadataValue = getVariableMetadataValue(variableDescriptor); - return JvmCodegenUtil.delegatedVariableValue(varValue, metadataValue, variableDescriptor, bindingContext, typeMapper); + return delegatedVariableValue(varValue, metadataValue, variableDescriptor, bindingContext, typeMapper); } private void initializeLocalVariable( @@ -4454,4 +4475,24 @@ The "returned" value of try expression with no finally is either the last expres labelName = name; } } + + @NotNull + private static StackValue.Delegate delegatedVariableValue( + @NotNull StackValue delegateValue, + @NotNull StackValue metadataValue, + @NotNull VariableDescriptorWithAccessors variableDescriptor, + @NotNull BindingContext bindingContext, + @NotNull KotlinTypeMapper typeMapper + ) { + VariableDescriptor delegateVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_DELEGATE, variableDescriptor); + assert delegateVariableDescriptor != null : variableDescriptor; + + VariableAccessorDescriptor getterDescriptor = variableDescriptor.getGetter(); + VariableAccessorDescriptor setterDescriptor = variableDescriptor.getSetter(); + + //noinspection ConstantConditions + CallableMethod getterMethod = typeMapper.mapToCallableMethod(getterDescriptor, false); + CallableMethod setterMethod = setterDescriptor != null ? typeMapper.mapToCallableMethod(setterDescriptor, false) : null; + return StackValue.delegate(typeMapper.mapType(variableDescriptor.getType()), delegateValue, metadataValue, getterMethod, setterMethod); + } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java index 1c09d6ad7f4..4d28b1dafae 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java @@ -1256,7 +1256,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { private void lookupInContext(@NotNull DeclarationDescriptor toLookup) { - context.lookupInContext(toLookup, StackValue.LOCAL_0, state, true); + ExpressionCodegen.lookupInContext(toLookup, StackValue.LOCAL_0, state, true, context); } @Override diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java index 12c64717eae..cebc835a0ca 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java @@ -49,7 +49,6 @@ import org.jetbrains.org.objectweb.asm.Opcodes; import java.io.File; -import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.LOCAL_VARIABLE_DELEGATE; import static org.jetbrains.kotlin.descriptors.ClassKind.ANNOTATION_CLASS; import static org.jetbrains.kotlin.descriptors.ClassKind.INTERFACE; import static org.jetbrains.kotlin.descriptors.Modality.ABSTRACT; @@ -293,24 +292,4 @@ public class JvmCodegenUtil { } return null; } - - @NotNull - public static StackValue.Delegate delegatedVariableValue( - @NotNull StackValue delegateValue, - @NotNull StackValue metadataValue, - VariableDescriptorWithAccessors variableDescriptor, - @NotNull BindingContext bindingContext, - @NotNull KotlinTypeMapper typeMapper - ) { - VariableDescriptor delegateVariableDescriptor = bindingContext.get(LOCAL_VARIABLE_DELEGATE, variableDescriptor); - assert delegateVariableDescriptor != null : variableDescriptor; - - VariableAccessorDescriptor getterDescriptor = variableDescriptor.getGetter(); - VariableAccessorDescriptor setterDescriptor = variableDescriptor.getSetter(); - - //noinspection ConstantConditions - CallableMethod getterMethod = typeMapper.mapToCallableMethod(getterDescriptor, false); - CallableMethod setterMethod = setterDescriptor != null ? typeMapper.mapToCallableMethod(setterDescriptor, false) : null; - return StackValue.delegate(typeMapper.mapType(variableDescriptor.getType()), delegateValue, metadataValue, getterMethod, setterMethod); - } } 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 43121dde452..2a404b23e93 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/context/LocalLookup.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/context/LocalLookup.java @@ -51,20 +51,13 @@ public interface LocalLookup { Type classType ) { VariableDescriptor vd = (VariableDescriptor) d; - VariableDescriptor originalVariableDescriptor = vd; boolean idx = localLookup != null && localLookup.lookupLocal(vd); if (!idx) return null; - boolean delegatedVar = false; VariableDescriptor delegateVariableDescriptor = state.getBindingContext().get(LOCAL_VARIABLE_DELEGATE, vd); - if (delegateVariableDescriptor != null) { - vd = delegateVariableDescriptor; - delegatedVar = true; - } - Type sharedVarType = state.getTypeMapper().getSharedVarType(vd); - Type localType = state.getTypeMapper().mapType(vd); + Type localType = state.getTypeMapper().mapType(delegateVariableDescriptor != null ? delegateVariableDescriptor : vd); Type type = sharedVarType != null ? sharedVarType : localType; String fieldName = "$" + vd.getName(); @@ -79,19 +72,6 @@ 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; - - innerValue = JvmCodegenUtil.delegatedVariableValue( - innerValue, - metadataValue, - (VariableDescriptorWithAccessors) originalVariableDescriptor, - state.getBindingContext(), - state.getTypeMapper() - ); - } enclosedValueDescriptor = new EnclosedValueDescriptor(fieldName, d, innerValue, type); }