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.cast;
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.expression.ExprBoolean;
026import lucee.transformer.bytecode.expression.ExprDouble;
027import lucee.transformer.bytecode.expression.ExprString;
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.Type;
035import org.objectweb.asm.commons.GeneratorAdapter;
036import org.objectweb.asm.commons.Method;
037
038/**
039 * Cast to a Boolean
040 */
041public final class CastBoolean extends ExpressionBase implements ExprBoolean,Cast {
042    
043    /**
044         * @see java.lang.Object#toString()
045         */
046        public String toString() {
047                return "(boolean)"+expr;
048        }
049
050        private Expression expr;
051
052    /**
053     * constructor of the class
054     * @param expr
055     */
056    private CastBoolean(Expression expr) {
057        super(expr.getStart(),expr.getEnd());
058        this.expr=expr;
059    }
060    
061    /**
062     * Create a String expression from a Expression
063     * @param expr
064     * @return String expression
065     * @throws TemplateException 
066     */
067    public static ExprBoolean toExprBoolean(Expression expr)  {
068        if(expr instanceof ExprBoolean) return (ExprBoolean) expr;
069        if(expr instanceof Literal) {
070            Boolean bool = ((Literal)expr).getBoolean(null);
071            if(bool!=null) return new LitBoolean(bool.booleanValue(),expr.getStart(),expr.getEnd());
072            // TODO throw new TemplateException("can't cast value to a boolean value");
073        }
074        return new CastBoolean(expr);
075    }
076
077    /**
078     * @see lucee.transformer.bytecode.expression.Expression#writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
079     */
080    public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {
081        GeneratorAdapter adapter = bc.getAdapter();
082        if(expr instanceof ExprDouble) {
083            expr.writeOut(bc,MODE_VALUE);
084            if(mode==MODE_VALUE)adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_BOOLEAN_VALUE_FROM_DOUBLE);
085            else adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_BOOLEAN_FROM_DOUBLE);
086        }
087        else if(expr instanceof ExprString) {
088            expr.writeOut(bc,MODE_REF);
089            if(mode==MODE_VALUE)adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_BOOLEAN_VALUE_FROM_STRING);
090            else adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_BOOLEAN_FROM_STRING);
091        }
092        else {
093                Type rtn = expr.writeOut(bc,mode);
094                
095                if(mode==MODE_VALUE) {
096                        if(!Types.isPrimitiveType(rtn)) {
097                                adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_BOOLEAN_VALUE);
098                        }
099                        else if(Types.BOOLEAN_VALUE.equals(rtn))        {}
100                        else if(Types.DOUBLE_VALUE.equals(rtn)) {
101                                adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_BOOLEAN_VALUE_FROM_DOUBLE);
102                        }
103                        else {
104                                adapter.invokeStatic(Types.CASTER,new Method("toRef",Types.toRefType(rtn),new Type[]{rtn}));
105                                adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_BOOLEAN_VALUE);
106                        }
107                        //return Types.BOOLEAN_VALUE;
108                }
109                else {
110                        if(Types.BOOLEAN.equals(rtn))   {}
111                        else adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_BOOLEAN);
112                }
113        }
114
115        if(mode==MODE_VALUE)return Types.BOOLEAN_VALUE;
116        return Types.BOOLEAN;
117    }
118
119        @Override
120        public Expression getExpr() {
121                return expr;
122        }
123}