From 8c99183970bae07bce70e9823576a7c702c35508 Mon Sep 17 00:00:00 2001 From: Natalia Ukhorskaya Date: Tue, 5 May 2015 16:31:43 +0300 Subject: [PATCH] Write local functions to localVariables table --- .../kotlin/codegen/ExpressionCodegen.java | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index e5507e4c2d1..e39774f6ac1 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -1621,7 +1621,9 @@ public class ExpressionCodegen extends JetVisitor implem if (statement instanceof JetNamedFunction) { DeclarationDescriptor descriptor = bindingContext.get(DECLARATION_TO_DESCRIPTOR, statement); - myFrameMap.enter(descriptor, OBJECT_TYPE); + assert descriptor instanceof FunctionDescriptor : "Couldn't find function declaration in binding context " + statement.getText(); + Type type = asmTypeForAnonymousClass(bindingContext, (FunctionDescriptor) descriptor); + myFrameMap.enter(descriptor, type); } } @@ -1657,14 +1659,7 @@ public class ExpressionCodegen extends JetVisitor implem } if (statement instanceof JetNamedFunction) { - final DeclarationDescriptor descriptor = bindingContext.get(DECLARATION_TO_DESCRIPTOR, statement); - leaveTasks.add(new Function() { - @Override - public Void fun(StackValue value) { - myFrameMap.leave(descriptor); - return null; - } - }); + removeNamedFunctionFromFrameMap((JetNamedFunction) statement, blockEnd, leaveTasks); } } @@ -1696,6 +1691,31 @@ public class ExpressionCodegen extends JetVisitor implem }); } + private void removeNamedFunctionFromFrameMap( + final @NotNull JetNamedFunction statement, + final Label blockEnd, + @NotNull List> leaveTasks + ) { + final FunctionDescriptor functionDescriptor = (FunctionDescriptor) bindingContext.get(DECLARATION_TO_DESCRIPTOR, statement); + assert functionDescriptor != null; + + final Type type = asmTypeForAnonymousClass(bindingContext, functionDescriptor); + + final Label scopeStart = new Label(); + v.mark(scopeStart); + + leaveTasks.add(new Function() { + @Override + public Void fun(StackValue answer) { + int index = myFrameMap.leave(functionDescriptor); + + assert !functionDescriptor.getName().isSpecial() : "Local variable should be generated only for function with name: " + statement.getText(); + v.visitLocalVariable(functionDescriptor.getName().asString() + "$", type.getDescriptor(), null, scopeStart, blockEnd, index); + return null; + } + }); + } + public boolean isShouldMarkLineNumbers() { return shouldMarkLineNumbers; }