From 34009a2134780e20406f58f9fd2482fa417b816d Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 10 Apr 2013 15:18:04 +0400 Subject: [PATCH] Refactor copy() function generation for data classes Use FunctionCodegen and the newly created FunctionGenerationStrategy to generate the needed instructions. Make some methods in FunctionCodegen private since they're not used anymore outside of the class --- .../jet/codegen/FunctionCodegen.java | 6 +- .../codegen/ImplementationBodyCodegen.java | 86 ++++++++----------- 2 files changed, 38 insertions(+), 54 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java index 619c450dc9b..2e44915976c 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java @@ -209,7 +209,7 @@ public class FunctionCodegen extends GenerationStateAware { } @NotNull - public static List getParameterNamesAsStrings(@NotNull FunctionDescriptor functionDescriptor) { + private static List getParameterNamesAsStrings(@NotNull FunctionDescriptor functionDescriptor) { List parameters = functionDescriptor.getValueParameters(); List result = new ArrayList(parameters.size()); for (ValueParameterDescriptor parameter : parameters) { @@ -218,7 +218,7 @@ public class FunctionCodegen extends GenerationStateAware { return result; } - public static void generateLocalVariableTable( + private static void generateLocalVariableTable( @NotNull JetTypeMapper typeMapper, @NotNull MethodVisitor mv, @NotNull FunctionDescriptor functionDescriptor, @@ -301,7 +301,7 @@ public class FunctionCodegen extends GenerationStateAware { return labelsForSharedVars; } - public static void generateStaticDelegateMethodBody( + private static void generateStaticDelegateMethodBody( @NotNull MethodVisitor mv, @NotNull Method asmMethod, @NotNull OwnerKind.StaticDelegateKind dk diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java index e1512545272..28f38f1ce10 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java @@ -697,67 +697,51 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { } private void generateCopyFunction(@NotNull final FunctionDescriptor function) { - JetType returnType = function.getReturnType(); - assert returnType != null : "Return type of copy function should not be null: " + function; - JvmMethodSignature methodSignature = typeMapper.mapSignature(function.getName(), function); - String methodDesc = methodSignature.getAsmMethod().getDescriptor(); - - MethodVisitor mv = v.newMethod(myClass, AsmUtil.getMethodAsmFlags(function, OwnerKind.IMPLEMENTATION), - function.getName().getName(), methodDesc, - null, null); - - FunctionCodegen.genJetAnnotations(state, function, null, null, mv); - - mv.visitCode(); - InstructionAdapter iv = new InstructionAdapter(mv); - - ConstructorDescriptor constructor = DescriptorUtils.getConstructorOfDataClass(descriptor); - - - Label methodBegin = new Label(); - mv.visitLabel(methodBegin); final Type thisDescriptorType = typeMapper.mapType(descriptor.getDefaultType()); - iv.anew(thisDescriptorType); - iv.dup(); - String thisInternalName = thisDescriptorType.getInternalName(); + FunctionCodegen fc = new FunctionCodegen(context, v, state); + fc.generateMethod(myClass, methodSignature, true, null, function, new FunctionGenerationStrategy() { + @Override + public void generateBody( + @NotNull MethodVisitor mv, + @NotNull JvmMethodSignature signature, + @NotNull MethodContext context, + @NotNull Collection localVariableNames, + @NotNull FrameMap frameMap + ) { + InstructionAdapter iv = new InstructionAdapter(mv); - assert function.getValueParameters().size() == constructor.getValueParameters().size() : - "Number of parameters of copy function and constructor are different. Copy: " + function.getValueParameters().size() + ", constructor: " + constructor.getValueParameters().size(); + iv.anew(thisDescriptorType); + iv.dup(); - MutableClosure closure = context.closure; - if (closure != null && closure.getCaptureThis() != null) { - Type type = typeMapper.mapType(enclosingClassDescriptor(bindingContext, descriptor)); - iv.load(0, classAsmType); - iv.getfield(JvmClassName.byType(classAsmType).getInternalName(), CAPTURED_THIS_FIELD, type.getDescriptor()); - } + ConstructorDescriptor constructor = DescriptorUtils.getConstructorOfDataClass(descriptor); + assert function.getValueParameters().size() == constructor.getValueParameters().size() : + "Number of parameters of copy function and constructor are different. " + + "Copy: " + function.getValueParameters().size() + ", " + + "constructor: " + constructor.getValueParameters().size(); - int parameterIndex = 1; // localVariable 0 = this - for (ValueParameterDescriptor parameterDescriptor : function.getValueParameters()) { - Type type = typeMapper.mapType(parameterDescriptor.getType()); - iv.load(parameterIndex, type); - parameterIndex += type.getSize(); - } + MutableClosure closure = ImplementationBodyCodegen.this.context.closure; + if (closure != null && closure.getCaptureThis() != null) { + Type type = typeMapper.mapType(enclosingClassDescriptor(bindingContext, descriptor)); + iv.load(0, classAsmType); + iv.getfield(JvmClassName.byType(classAsmType).getInternalName(), CAPTURED_THIS_FIELD, type.getDescriptor()); + } - String constructorJvmDescriptor = typeMapper.mapToCallableMethod(constructor).getSignature().getAsmMethod().getDescriptor(); - iv.invokespecial(thisInternalName, "", constructorJvmDescriptor); + int parameterIndex = 1; // localVariable 0 = this + for (ValueParameterDescriptor parameterDescriptor : function.getValueParameters()) { + Type type = typeMapper.mapType(parameterDescriptor.getType()); + iv.load(parameterIndex, type); + parameterIndex += type.getSize(); + } - iv.areturn(thisDescriptorType); + String constructorJvmDescriptor = typeMapper.mapToCallableMethod(constructor).getSignature().getAsmMethod().getDescriptor(); + iv.invokespecial(thisDescriptorType.getInternalName(), "", constructorJvmDescriptor); - Label methodEnd = new Label(); - mv.visitLabel(methodEnd); - - FunctionCodegen.MethodInfo methodInfo = new FunctionCodegen.MethodInfo( - methodBegin, - methodEnd, - FunctionCodegen.getParameterNamesAsStrings(function), - Collections.emptyMap() - ); - FunctionCodegen.generateLocalVariableTable(typeMapper, mv, function, thisDescriptorType, methodInfo); - - FunctionCodegen.endVisit(mv, function.getName().getName(), myClass); + iv.areturn(thisDescriptorType); + } + }); MethodContext functionContext = context.intoFunction(function); FunctionCodegen.generateDefaultIfNeeded(functionContext, state, v, methodSignature.getAsmMethod(), function, OwnerKind.IMPLEMENTATION,