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 sun.tools.asm.Label;
031: import java.io.PrintStream;
032: import java.util.Hashtable;
033:
034: /**
035: * WARNING: The contents of this source file are not part of any
036: * supported API. Code that depends on them does so at its own risk:
037: * they are subject to change or removal without notice.
038: */
039: public class CastExpression extends BinaryExpression {
040: /**
041: * constructor
042: */
043: public CastExpression(long where, Expression left, Expression right) {
044: super (CAST, where, left.type, left, right);
045: }
046:
047: /**
048: * Check the expression
049: */
050: public Vset checkValue(Environment env, Context ctx, Vset vset,
051: Hashtable exp) {
052: type = left.toType(env, ctx);
053: vset = right.checkValue(env, ctx, vset, exp);
054:
055: if (type.isType(TC_ERROR) || right.type.isType(TC_ERROR)) {
056: // An error was already reported
057: return vset;
058: }
059:
060: if (type.equals(right.type)) {
061: // The types are already the same
062: return vset;
063: }
064:
065: try {
066: if (env.explicitCast(right.type, type)) {
067: right = new ConvertExpression(where, type, right);
068: return vset;
069: }
070: } catch (ClassNotFound e) {
071: env.error(where, "class.not.found", e.name, opNames[op]);
072: }
073:
074: // The cast is not allowed
075: env.error(where, "invalid.cast", right.type, type);
076: return vset;
077: }
078:
079: /**
080: * Check if constant
081: */
082: public boolean isConstant() {
083: if (type.inMask(TM_REFERENCE) && !type.equals(Type.tString)) {
084: // must be a primitive type, or String
085: return false;
086: }
087: return right.isConstant();
088: }
089:
090: /**
091: * Inline
092: */
093: public Expression inline(Environment env, Context ctx) {
094: return right.inline(env, ctx);
095: }
096:
097: public Expression inlineValue(Environment env, Context ctx) {
098: return right.inlineValue(env, ctx);
099: }
100:
101: public int costInline(int thresh, Environment env, Context ctx) {
102: if (ctx == null) {
103: return 1 + right.costInline(thresh, env, ctx);
104: }
105: // sourceClass is the current class trying to inline this method
106: ClassDefinition sourceClass = ctx.field.getClassDefinition();
107: try {
108: // We only allow the inlining if the current class can access
109: // the casting class
110: if (left.type.isType(TC_ARRAY)
111: || sourceClass.permitInlinedAccess(env, env
112: .getClassDeclaration(left.type)))
113: return 1 + right.costInline(thresh, env, ctx);
114: } catch (ClassNotFound e) {
115: }
116: return thresh;
117: }
118:
119: /**
120: * Print
121: */
122: public void print(PrintStream out) {
123: out.print("(" + opNames[op] + " ");
124: if (type.isType(TC_ERROR)) {
125: left.print(out);
126: } else {
127: out.print(type);
128: }
129: out.print(" ");
130: right.print(out);
131: out.print(")");
132: }
133: }
|