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.transformer.bytecode.BytecodeContext;
022import lucee.transformer.bytecode.BytecodeException;
023import lucee.transformer.bytecode.Literal;
024import lucee.transformer.bytecode.cast.CastString;
025import lucee.transformer.bytecode.expression.ExprString;
026import lucee.transformer.bytecode.expression.Expression;
027import lucee.transformer.bytecode.expression.ExpressionBase;
028import lucee.transformer.bytecode.literal.LitString;
029import lucee.transformer.bytecode.util.Types;
030
031import org.objectweb.asm.Type;
032import org.objectweb.asm.commons.Method;
033
034public final class OpString extends ExpressionBase implements ExprString {
035    
036    private ExprString right;
037    private ExprString left;
038
039    // String concat (String)
040    private final static Method METHOD_CONCAT = new Method("concat",
041                        Types.STRING,
042                        new Type[]{Types.STRING});
043    
044    private OpString(Expression left, Expression right) {
045        super(left.getStart(),right.getEnd());
046        this.left=CastString.toExprString(left);
047        this.right=CastString.toExprString(right);   
048    }
049    
050    /**
051     * Create a String expression from a Expression
052     * @param left 
053     * @param right 
054     * 
055     * @return String expression
056     */
057    public static ExprString toExprString(Expression left, Expression right) {
058        return toExprString(left, right, true);
059    }
060    
061    public static ExprString toExprString(Expression left, Expression right, boolean concatStatic) {
062        if(concatStatic && left instanceof Literal && right instanceof Literal) {
063            String l = ((Literal)left).getString();
064                String r = ((Literal)right).getString();
065                if((l.length()+r.length())<=LitString.MAX_SIZE)return LitString.toExprString(l.concat(r),left.getStart(),right.getEnd());
066        }
067        return new OpString(left,right);
068    }
069    
070    
071    /**
072     *
073     * @see lucee.transformer.bytecode.expression.ExpressionBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
074     */
075    public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {
076        left.writeOut(bc,MODE_REF);
077        right.writeOut(bc,MODE_REF);
078        bc.getAdapter().invokeVirtual(Types.STRING,METHOD_CONCAT);
079        return Types.STRING;
080    }
081
082    /* *
083     * @see lucee.transformer.bytecode.expression.Expression#getType()
084     * /
085    public int getType() {
086        return Types._STRING;
087    }*/
088
089}