001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.transformer.bytecode.statement;
020
021import java.util.Stack;
022
023import lucee.transformer.bytecode.BytecodeContext;
024import lucee.transformer.bytecode.BytecodeException;
025import lucee.transformer.bytecode.Position;
026import lucee.transformer.bytecode.Statement;
027import lucee.transformer.bytecode.expression.Expression;
028import lucee.transformer.bytecode.util.ASMConstants;
029import lucee.transformer.bytecode.util.Types;
030import lucee.transformer.bytecode.visitor.OnFinally;
031
032import org.objectweb.asm.Opcodes;
033import org.objectweb.asm.commons.GeneratorAdapter;
034
035/**
036 * Return Statement
037 */
038public final class Return extends StatementBaseNoFinal {
039
040        Expression expr;
041
042        /**
043         * Constructor of the class
044         * @param line
045         */
046        public Return(Position start,Position end) {
047                super(start,end);
048                setHasFlowController(true);
049                //expr=LitString.toExprString("", line);
050        }
051        
052        /**
053         * Constructor of the class
054         * @param expr
055         * @param line
056         */
057        public Return(Expression expr, Position start,Position end) {
058                super(start,end);
059                this.expr=expr;
060                setHasFlowController(true);
061                //if(expr==null)expr=LitString.toExprString("", line);
062        }
063
064        public void _writeOut(BytecodeContext bc) throws BytecodeException {
065                GeneratorAdapter adapter = bc.getAdapter();
066                
067                
068                if(expr==null)ASMConstants.NULL(adapter);
069                else expr.writeOut(bc, Expression.MODE_REF);
070                
071                // call finallies
072                Stack<OnFinally> finallies = bc.getOnFinallyStack();
073                int len=finallies.size();
074                OnFinally onFinally;
075                if(len>0) {
076                        int rtn = adapter.newLocal(Types.OBJECT);
077                        adapter.storeLocal(rtn, Types.OBJECT);
078                        for(int i=len-1;i>=0;i--) {
079                                onFinally=finallies.get(i);
080                                if(!bc.insideFinally(onFinally)) 
081                                        onFinally.writeOut(bc);
082                        }
083                        adapter.loadLocal(rtn, Types.OBJECT);
084                }
085                
086                
087                
088                if(bc.getMethod().getReturnType().equals(Types.VOID)) {
089                        adapter.pop();
090                        adapter.visitInsn(Opcodes.RETURN);
091                }
092                else adapter.visitInsn(Opcodes.ARETURN);
093        }
094        
095
096        /**
097         *
098         * @see lucee.transformer.bytecode.statement.StatementBase#setParent(lucee.transformer.bytecode.Statement)
099         */
100        public void setParent(Statement parent) {
101                super.setParent(parent);
102                parent.setHasFlowController(true);
103        }
104}