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.op;
020
021import lucee.runtime.exp.TemplateException;
022import lucee.transformer.bytecode.BytecodeContext;
023import lucee.transformer.bytecode.BytecodeException;
024import lucee.transformer.bytecode.Literal;
025import lucee.transformer.bytecode.Position;
026import lucee.transformer.bytecode.cast.CastBoolean;
027import lucee.transformer.bytecode.expression.ExprBoolean;
028import lucee.transformer.bytecode.expression.Expression;
029import lucee.transformer.bytecode.expression.ExpressionBase;
030import lucee.transformer.bytecode.literal.LitBoolean;
031import lucee.transformer.bytecode.util.Methods;
032import lucee.transformer.bytecode.util.Types;
033
034import org.objectweb.asm.Label;
035import org.objectweb.asm.Opcodes;
036import org.objectweb.asm.Type;
037import org.objectweb.asm.commons.GeneratorAdapter;
038
039public final class OpNegate extends ExpressionBase implements ExprBoolean {
040
041        private ExprBoolean expr;
042
043        private OpNegate(Expression expr, Position start, Position end)  {
044        super(start,end);
045        this.expr=CastBoolean.toExprBoolean(expr);
046    }
047    
048    /**
049     * Create a String expression from a Expression
050     * @param left 
051     * @param right 
052     * 
053     * @return String expression
054     * @throws TemplateException 
055     */
056    public static ExprBoolean toExprBoolean(Expression expr, Position start, Position end) {
057        if(expr instanceof Literal) {
058                Boolean b=((Literal) expr).getBoolean(null);
059                if(b!=null) {
060                        return new LitBoolean(!b.booleanValue(),start,end);
061                }
062        }
063        return new OpNegate(expr,start,end);
064    }
065        
066        
067        /**
068         *
069         * @see lucee.transformer.bytecode.expression.ExpressionBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
070         */
071        public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {
072                GeneratorAdapter adapter = bc.getAdapter();
073        if(mode==MODE_REF) {
074            _writeOut(bc,MODE_VALUE);
075            adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_BOOLEAN_FROM_BOOLEAN);
076            return Types.BOOLEAN;
077        }
078        
079        
080        Label l1 = new Label();
081        Label l2 = new Label();
082        
083        expr.writeOut(bc, MODE_VALUE);
084        adapter.ifZCmp(Opcodes.IFEQ,l1);
085        
086        adapter.visitInsn(Opcodes.ICONST_0);
087        adapter.visitJumpInsn(Opcodes.GOTO, l2);
088        adapter.visitLabel(l1);
089        adapter.visitInsn(Opcodes.ICONST_1);
090        adapter.visitLabel(l2);
091
092        return Types.BOOLEAN_VALUE;
093
094        }
095
096        /*public int getType() {
097                return Types._BOOLEAN;
098        }*/
099
100}