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.transformer.bytecode.BytecodeContext;
022import lucee.transformer.bytecode.BytecodeException;
023import lucee.transformer.bytecode.Literal;
024import lucee.transformer.bytecode.expression.ExprBoolean;
025import lucee.transformer.bytecode.expression.ExprDouble;
026import lucee.transformer.bytecode.expression.ExprString;
027import lucee.transformer.bytecode.expression.Expression;
028import lucee.transformer.bytecode.expression.ExpressionBase;
029import lucee.transformer.bytecode.literal.LitString;
030import lucee.transformer.bytecode.util.Methods;
031import lucee.transformer.bytecode.util.Types;
032
033import org.objectweb.asm.Type;
034import org.objectweb.asm.commons.GeneratorAdapter;
035
036/**
037 * Cast to a String
038 */
039public final class CastString extends ExpressionBase implements ExprString,Cast {
040    
041    private Expression expr;
042
043    /**
044     * constructor of the class
045     * @param expr
046     */
047    private CastString(Expression expr) {
048        super(expr.getStart(),expr.getEnd());
049        this.expr=expr;
050    }
051    
052    /**
053     * Create a String expression from a Expression
054     * @param expr
055     * @param pos 
056     * @return String expression
057     */
058    public static ExprString toExprString(Expression expr) {
059        if(expr instanceof ExprString) return (ExprString) expr;
060        if(expr instanceof Literal) return LitString.toExprString(((Literal)expr).getString(),expr.getStart(),expr.getEnd());
061        return new CastString(expr);
062    }
063
064    /**
065     * @see lucee.transformer.bytecode.expression.Expression#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
066     */
067    public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {
068
069        GeneratorAdapter adapter = bc.getAdapter();
070        if(expr instanceof ExprBoolean) {
071            expr.writeOut(bc,MODE_VALUE);
072            adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_STRING_FROM_BOOLEAN);
073        }
074        else if(expr instanceof ExprDouble) {
075            expr.writeOut(bc,MODE_VALUE);
076            adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_STRING_FROM_DOUBLE);
077        }
078        else {
079            Type rtn = expr.writeOut(bc,MODE_REF);
080            if(rtn.equals(Types.STRING)) return Types.STRING;
081            adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_STRING);
082        }
083
084        return Types.STRING;
085    }
086
087        @Override
088        public Expression getExpr() {
089                return expr;
090        }
091
092}