169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/* 269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit. 369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved. 469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version 669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with 769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License. Alternatively, the contents of this file may be used under 869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later. 969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis, 1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the 1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License. 1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.compiler.ast; 1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.CompileError; 1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.TokenId; 2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/** 2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Integer constant. 2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class IntConst extends ASTree { 2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal protected long value; 2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal protected int type; 2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public IntConst(long v, int tokenId) { value = v; type = tokenId; } 2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public long get() { return value; } 3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public void set(long v) { value = v; } 3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /* Returns IntConstant, CharConstant, or LongConstant. 3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public int getType() { return type; } 3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public String toString() { return Long.toString(value); } 3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public void accept(Visitor v) throws CompileError { 4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal v.atIntConst(this); 4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public ASTree compute(int op, ASTree right) { 4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (right instanceof IntConst) 4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return compute0(op, (IntConst)right); 4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal else if (right instanceof DoubleConst) 4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return compute0(op, (DoubleConst)right); 4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal else 5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return null; 5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal private IntConst compute0(int op, IntConst right) { 5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal int type1 = this.type; 5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal int type2 = right.type; 5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal int newType; 5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (type1 == TokenId.LongConstant || type2 == TokenId.LongConstant) 5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newType = TokenId.LongConstant; 5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal else if (type1 == TokenId.CharConstant 6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal && type2 == TokenId.CharConstant) 6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newType = TokenId.CharConstant; 6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal else 6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newType = TokenId.IntConstant; 6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal long value1 = this.value; 6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal long value2 = right.value; 6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal long newValue; 6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal switch (op) { 6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '+' : 7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 + value2; 7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '-' : 7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 - value2; 7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '*' : 7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 * value2; 7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '/' : 7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 / value2; 8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '%' : 8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 % value2; 8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '|' : 8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 | value2; 8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '^' : 8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 ^ value2; 8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '&' : 9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 & value2; 9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case TokenId.LSHIFT : 9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value << (int)value2; 9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newType = type1; 9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case TokenId.RSHIFT : 9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value >> (int)value2; 9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newType = type1; 10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case TokenId.ARSHIFT : 10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value >>> (int)value2; 10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newType = type1; 10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal default : 10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return null; 10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return new IntConst(newValue, newType); 11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal private DoubleConst compute0(int op, DoubleConst right) { 11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal double value1 = (double)this.value; 11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal double value2 = right.value; 11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal double newValue; 11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal switch (op) { 11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '+' : 11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 + value2; 11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '-' : 12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 - value2; 12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '*' : 12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 * value2; 12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 12669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '/' : 12769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 / value2; 12869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 12969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal case '%' : 13069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal newValue = value1 % value2; 13169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal break; 13269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal default : 13369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return null; 13469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 13569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 13669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return new DoubleConst(newValue, right.type); 13769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 13869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal} 139