001: /*
002: * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.tree;
027:
028: import sun.tools.java.*;
029: import sun.tools.asm.Assembler;
030: import java.io.PrintStream;
031: import java.util.Hashtable;
032:
033: /**
034: * WARNING: The contents of this source file are not part of any
035: * supported API. Code that depends on them does so at its own risk:
036: * they are subject to change or removal without notice.
037: */
038: public class ConvertExpression extends UnaryExpression {
039: /**
040: * Constructor
041: */
042: public ConvertExpression(long where, Type type, Expression right) {
043: super (CONVERT, where, type, right);
044: }
045:
046: /**
047: * Check the value
048: */
049: public Vset checkValue(Environment env, Context ctx, Vset vset,
050: Hashtable exp) {
051: return right.checkValue(env, ctx, vset, exp);
052: }
053:
054: /**
055: * Simplify
056: */
057: Expression simplify() {
058: switch (right.op) {
059: case BYTEVAL:
060: case CHARVAL:
061: case SHORTVAL:
062: case INTVAL: {
063: int value = ((IntegerExpression) right).value;
064: switch (type.getTypeCode()) {
065: case TC_BYTE:
066: return new ByteExpression(right.where, (byte) value);
067: case TC_CHAR:
068: return new CharExpression(right.where, (char) value);
069: case TC_SHORT:
070: return new ShortExpression(right.where, (short) value);
071: case TC_INT:
072: return new IntExpression(right.where, (int) value);
073: case TC_LONG:
074: return new LongExpression(right.where, (long) value);
075: case TC_FLOAT:
076: return new FloatExpression(right.where, (float) value);
077: case TC_DOUBLE:
078: return new DoubleExpression(right.where, (double) value);
079: }
080: break;
081: }
082: case LONGVAL: {
083: long value = ((LongExpression) right).value;
084: switch (type.getTypeCode()) {
085: case TC_BYTE:
086: return new ByteExpression(right.where, (byte) value);
087: case TC_CHAR:
088: return new CharExpression(right.where, (char) value);
089: case TC_SHORT:
090: return new ShortExpression(right.where, (short) value);
091: case TC_INT:
092: return new IntExpression(right.where, (int) value);
093: case TC_FLOAT:
094: return new FloatExpression(right.where, (float) value);
095: case TC_DOUBLE:
096: return new DoubleExpression(right.where, (double) value);
097: }
098: break;
099: }
100: case FLOATVAL: {
101: float value = ((FloatExpression) right).value;
102: switch (type.getTypeCode()) {
103: case TC_BYTE:
104: return new ByteExpression(right.where, (byte) value);
105: case TC_CHAR:
106: return new CharExpression(right.where, (char) value);
107: case TC_SHORT:
108: return new ShortExpression(right.where, (short) value);
109: case TC_INT:
110: return new IntExpression(right.where, (int) value);
111: case TC_LONG:
112: return new LongExpression(right.where, (long) value);
113: case TC_DOUBLE:
114: return new DoubleExpression(right.where, (double) value);
115: }
116: break;
117: }
118: case DOUBLEVAL: {
119: double value = ((DoubleExpression) right).value;
120: switch (type.getTypeCode()) {
121: case TC_BYTE:
122: return new ByteExpression(right.where, (byte) value);
123: case TC_CHAR:
124: return new CharExpression(right.where, (char) value);
125: case TC_SHORT:
126: return new ShortExpression(right.where, (short) value);
127: case TC_INT:
128: return new IntExpression(right.where, (int) value);
129: case TC_LONG:
130: return new LongExpression(right.where, (long) value);
131: case TC_FLOAT:
132: return new FloatExpression(right.where, (float) value);
133: }
134: break;
135: }
136: }
137: return this ;
138: }
139:
140: /**
141: * Check if the expression is equal to a value
142: */
143: public boolean equals(int i) {
144: return right.equals(i);
145: }
146:
147: public boolean equals(boolean b) {
148: return right.equals(b);
149: }
150:
151: /**
152: * Inline
153: */
154: public Expression inline(Environment env, Context ctx) {
155: // super.inline throws away the op.
156: // This is sometimes incorrect, since casts can have side effects.
157: if (right.type.inMask(TM_REFERENCE)
158: && type.inMask(TM_REFERENCE)) {
159: try {
160: if (!env.implicitCast(right.type, type))
161: return inlineValue(env, ctx);
162: } catch (ClassNotFound e) {
163: throw new CompilerError(e);
164: }
165: }
166: return super .inline(env, ctx);
167: }
168:
169: /**
170: * Code
171: */
172: public void codeValue(Environment env, Context ctx, Assembler asm) {
173: right.codeValue(env, ctx, asm);
174: codeConversion(env, ctx, asm, right.type, type);
175: }
176:
177: /**
178: * Print
179: */
180: public void print(PrintStream out) {
181: out.print("(" + opNames[op] + " " + type.toString() + " ");
182: right.print(out);
183: out.print(")");
184: }
185: }
|