001: /* ConstOperator Copyright (C) 1998-2002 Jochen Hoenicke.
002: *
003: * This program is free software; you can redistribute it and/or modify
004: * it under the terms of the GNU Lesser General Public License as published by
005: * the Free Software Foundation; either version 2, or (at your option)
006: * any later version.
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program; see the file COPYING.LESSER. If not, write to
015: * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
016: *
017: * $Id: ConstOperator.java,v 4.21.2.2 2002/05/28 17:34:06 hoenicke Exp $
018: */
019:
020: package jode.expr;
021:
022: import jode.type.Type;
023: import jode.type.IntegerType;
024: import jode.decompiler.TabbedPrintWriter;
025:
026: public class ConstOperator extends NoArgOperator {
027: Object value;
028: boolean isInitializer = false;
029:
030: private static final Type tBoolConstInt = new IntegerType(
031: IntegerType.IT_I | IntegerType.IT_C | IntegerType.IT_Z
032: | IntegerType.IT_S | IntegerType.IT_B);
033:
034: public ConstOperator(Object constant) {
035: super (Type.tUnknown);
036: if (constant instanceof Boolean) {
037: updateParentType(Type.tBoolean);
038: constant = new Integer(
039: ((Boolean) constant).booleanValue() ? 1 : 0);
040: } else if (constant instanceof Integer) {
041: int intVal = ((Integer) constant).intValue();
042: updateParentType((intVal == 0 || intVal == 1) ? tBoolConstInt
043: : (intVal < Short.MIN_VALUE || intVal > Character.MAX_VALUE) ? Type.tInt
044: : new IntegerType(
045: (intVal < Byte.MIN_VALUE) ? IntegerType.IT_S
046: | IntegerType.IT_I
047: : (intVal < 0) ? IntegerType.IT_S
048: | IntegerType.IT_B
049: | IntegerType.IT_I
050: : (intVal <= Byte.MAX_VALUE) ? (IntegerType.IT_S
051: | IntegerType.IT_B
052: | IntegerType.IT_C | IntegerType.IT_I)
053: : (intVal <= Short.MAX_VALUE) ? IntegerType.IT_S
054: | IntegerType.IT_C
055: | IntegerType.IT_I
056: : IntegerType.IT_C
057: | IntegerType.IT_I));
058: } else if (constant instanceof Long)
059: updateParentType(Type.tLong);
060: else if (constant instanceof Float)
061: updateParentType(Type.tFloat);
062: else if (constant instanceof Double)
063: updateParentType(Type.tDouble);
064: else if (constant instanceof String)
065: updateParentType(Type.tString);
066: else if (constant == null)
067: updateParentType(Type.tUObject);
068: else
069: throw new IllegalArgumentException(
070: "Illegal constant type: " + constant.getClass());
071: value = constant;
072: }
073:
074: public Object getValue() {
075: return value;
076: }
077:
078: /**
079: * Return true, if this value is a one of the given type.
080: * This is used for ++ and -- instructions.
081: * @param type the type for which this must be a one. This may
082: * be different from the type this value actually is.
083: */
084: public boolean isOne(Type type) {
085: if (type instanceof IntegerType) {
086: return (value instanceof Integer && ((Integer) value)
087: .intValue() == 1);
088: } else if (type == Type.tLong) {
089: return (value instanceof Long && ((Long) value).longValue() == 1L);
090: } else if (type == Type.tFloat) {
091: return (value instanceof Float && ((Float) value)
092: .floatValue() == 1.0f);
093: } else if (type == Type.tDouble) {
094: return (value instanceof Double && ((Double) value)
095: .doubleValue() == 1.0);
096: }
097: return false;
098: }
099:
100: public int getPriority() {
101: return 1000;
102: }
103:
104: public boolean opEquals(Operator o) {
105: if (o instanceof ConstOperator) {
106: Object otherValue = ((ConstOperator) o).value;
107: return value == null ? otherValue == null : value
108: .equals(otherValue);
109: }
110: return false;
111: }
112:
113: public void makeInitializer(Type type) {
114: isInitializer = true;
115: }
116:
117: private static String quoted(String str) {
118: StringBuffer result = new StringBuffer("\"");
119: for (int i = 0; i < str.length(); i++) {
120: char c;
121: switch (c = str.charAt(i)) {
122: case '\0':
123: result.append("\\0");
124: break;
125: case '\t':
126: result.append("\\t");
127: break;
128: case '\n':
129: result.append("\\n");
130: break;
131: case '\r':
132: result.append("\\r");
133: break;
134: case '\\':
135: result.append("\\\\");
136: break;
137: case '\"':
138: result.append("\\\"");
139: break;
140: default:
141: if (c < 32) {
142: String oct = Integer.toOctalString(c);
143: result.append(
144: "\\000".substring(0, 4 - oct.length()))
145: .append(oct);
146: } else if (c >= 32 && c < 127)
147: result.append(str.charAt(i));
148: else {
149: String hex = Integer.toHexString(c);
150: result.append(
151: "\\u0000".substring(0, 6 - hex.length()))
152: .append(hex);
153: }
154: }
155: }
156: return result.append("\"").toString();
157: }
158:
159: public String toString() {
160: String strVal = String.valueOf(value);
161: if (type.isOfType(Type.tBoolean)) {
162: int intVal = ((Integer) value).intValue();
163: if (intVal == 0)
164: return "false";
165: else if (intVal == 1)
166: return "true";
167: else
168: throw new jode.AssertError(
169: "boolean is neither false nor true");
170: }
171: if (type.getHint().equals(Type.tChar)) {
172: char c = (char) ((Integer) value).intValue();
173: switch (c) {
174: case '\0':
175: return "\'\\0\'";
176: case '\t':
177: return "\'\\t\'";
178: case '\n':
179: return "\'\\n\'";
180: case '\r':
181: return "\'\\r\'";
182: case '\\':
183: return "\'\\\\\'";
184: case '\"':
185: return "\'\\\"\'";
186: case '\'':
187: return "\'\\\'\'";
188: }
189: if (c < 32) {
190: String oct = Integer.toOctalString(c);
191: return "\'\\000".substring(0, 5 - oct.length()) + oct
192: + "\'";
193: }
194: if (c >= 32 && c < 127)
195: return "\'" + c + "\'";
196: else {
197: String hex = Integer.toHexString(c);
198: return "\'\\u0000".substring(0, 7 - hex.length()) + hex
199: + "\'";
200: }
201: } else if (type.equals(Type.tString)) {
202: return quoted(strVal);
203: } else if (parent != null) {
204: int opindex = parent.getOperatorIndex();
205: if (opindex >= OPASSIGN_OP + ADD_OP
206: && opindex < OPASSIGN_OP + ASSIGN_OP)
207: opindex -= OPASSIGN_OP;
208:
209: if (opindex >= AND_OP && opindex < AND_OP + 3) {
210: /* For bit wise and/or/xor change representation.
211: */
212: if (type.isOfType(Type.tUInt)) {
213: int i = ((Integer) value).intValue();
214: if (i < -1)
215: strVal = "~0x" + Integer.toHexString(-i - 1);
216: else
217: strVal = "0x" + Integer.toHexString(i);
218: } else if (type.equals(Type.tLong)) {
219: long l = ((Long) value).longValue();
220: if (l < -1)
221: strVal = "~0x" + Long.toHexString(-l - 1);
222: else
223: strVal = "0x" + Long.toHexString(l);
224: }
225: }
226: }
227: if (type.isOfType(Type.tLong))
228: return strVal + "L";
229: if (type.isOfType(Type.tFloat)) {
230: if (strVal.equals("NaN"))
231: return "Float.NaN";
232: if (strVal.equals("-Infinity"))
233: return "Float.NEGATIVE_INFINITY";
234: if (strVal.equals("Infinity"))
235: return "Float.POSITIVE_INFINITY";
236: return strVal + "F";
237: }
238: if (type.isOfType(Type.tDouble)) {
239: if (strVal.equals("NaN"))
240: return "Double.NaN";
241: if (strVal.equals("-Infinity"))
242: return "Double.NEGATIVE_INFINITY";
243: if (strVal.equals("Infinity"))
244: return "Double.POSITIVE_INFINITY";
245: return strVal;
246: }
247: if (!type.isOfType(Type.tInt)
248: && (type.getHint().equals(Type.tByte) || type.getHint()
249: .equals(Type.tShort))
250: && !isInitializer
251: && !(parent instanceof StoreInstruction
252: && parent.getOperatorIndex() != ASSIGN_OP && parent.subExpressions[1] == this )) {
253: /* One of the strange things in java. All constants
254: * are int and must be explicitly casted to byte,...,short.
255: * But in assignments and initializers this cast is unnecessary.
256: * See JLS section 5.2
257: */
258: return "(" + type.getHint() + ") " + strVal;
259: }
260:
261: return strVal;
262: }
263:
264: public void dumpExpression(TabbedPrintWriter writer)
265: throws java.io.IOException {
266: writer.print(toString());
267: }
268: }
|