001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist.compiler.ast;
017:
018: import javassist.compiler.CompileError;
019: import javassist.compiler.TokenId;
020:
021: /**
022: * Integer constant.
023: */
024: public class IntConst extends ASTree {
025: protected long value;
026: protected int type;
027:
028: public IntConst(long v, int tokenId) {
029: value = v;
030: type = tokenId;
031: }
032:
033: public long get() {
034: return value;
035: }
036:
037: public void set(long v) {
038: value = v;
039: }
040:
041: /* Returns IntConstant, CharConstant, or LongConstant.
042: */
043: public int getType() {
044: return type;
045: }
046:
047: public String toString() {
048: return Long.toString(value);
049: }
050:
051: public void accept(Visitor v) throws CompileError {
052: v.atIntConst(this );
053: }
054:
055: public ASTree compute(int op, ASTree right) {
056: if (right instanceof IntConst)
057: return compute0(op, (IntConst) right);
058: else if (right instanceof DoubleConst)
059: return compute0(op, (DoubleConst) right);
060: else
061: return null;
062: }
063:
064: private IntConst compute0(int op, IntConst right) {
065: int type1 = this .type;
066: int type2 = right.type;
067: int newType;
068: if (type1 == TokenId.LongConstant
069: || type2 == TokenId.LongConstant)
070: newType = TokenId.LongConstant;
071: else if (type1 == TokenId.CharConstant
072: && type2 == TokenId.CharConstant)
073: newType = TokenId.CharConstant;
074: else
075: newType = TokenId.IntConstant;
076:
077: long value1 = this .value;
078: long value2 = right.value;
079: long newValue;
080: switch (op) {
081: case '+':
082: newValue = value1 + value2;
083: break;
084: case '-':
085: newValue = value1 - value2;
086: break;
087: case '*':
088: newValue = value1 * value2;
089: break;
090: case '/':
091: newValue = value1 / value2;
092: break;
093: case '%':
094: newValue = value1 % value2;
095: break;
096: case '|':
097: newValue = value1 | value2;
098: break;
099: case '^':
100: newValue = value1 ^ value2;
101: break;
102: case '&':
103: newValue = value1 & value2;
104: break;
105: case TokenId.LSHIFT:
106: newValue = value << (int) value2;
107: newType = type1;
108: break;
109: case TokenId.RSHIFT:
110: newValue = value >> (int) value2;
111: newType = type1;
112: break;
113: case TokenId.ARSHIFT:
114: newValue = value >>> (int) value2;
115: newType = type1;
116: break;
117: default:
118: return null;
119: }
120:
121: return new IntConst(newValue, newType);
122: }
123:
124: private DoubleConst compute0(int op, DoubleConst right) {
125: double value1 = (double) this .value;
126: double value2 = right.value;
127: double newValue;
128: switch (op) {
129: case '+':
130: newValue = value1 + value2;
131: break;
132: case '-':
133: newValue = value1 - value2;
134: break;
135: case '*':
136: newValue = value1 * value2;
137: break;
138: case '/':
139: newValue = value1 / value2;
140: break;
141: case '%':
142: newValue = value1 % value2;
143: break;
144: default:
145: return null;
146: }
147:
148: return new DoubleConst(newValue, right.type);
149: }
150: }
|