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 java.io.PrintStream;
030: import java.io.ByteArrayOutputStream;
031:
032: /**
033: * WARNING: The contents of this source file are not part of any
034: * supported API. Code that depends on them does so at its own risk:
035: * they are subject to change or removal without notice.
036: */
037: public class Node implements Constants, Cloneable {
038: int op;
039: long where;
040:
041: /**
042: * Constructor
043: */
044: Node(int op, long where) {
045: this .op = op;
046: this .where = where;
047: }
048:
049: /**
050: * Get the operator
051: */
052: public int getOp() {
053: return op;
054: }
055:
056: /**
057: * Get where
058: */
059: public long getWhere() {
060: return where;
061: }
062:
063: /**
064: * Implicit conversions
065: */
066: public Expression convert(Environment env, Context ctx, Type t,
067: Expression e) {
068: if (e.type.isType(TC_ERROR) || t.isType(TC_ERROR)) {
069: // An error was already reported
070: return e;
071: }
072:
073: if (e.type.equals(t)) {
074: // The types are already the same
075: return e;
076: }
077:
078: try {
079: if (e.fitsType(env, ctx, t)) {
080: return new ConvertExpression(where, t, e);
081: }
082:
083: if (env.explicitCast(e.type, t)) {
084: env.error(where, "explicit.cast.needed", opNames[op],
085: e.type, t);
086: return new ConvertExpression(where, t, e);
087: }
088: } catch (ClassNotFound ee) {
089: env.error(where, "class.not.found", ee.name, opNames[op]);
090: }
091:
092: // The cast is not allowed
093: env.error(where, "incompatible.type", opNames[op], e.type, t);
094: return new ConvertExpression(where, Type.tError, e);
095: }
096:
097: /**
098: * Print
099: */
100: public void print(PrintStream out) {
101: throw new CompilerError("print");
102: }
103:
104: /**
105: * Clone this object.
106: */
107: public Object clone() {
108: try {
109: return super .clone();
110: } catch (CloneNotSupportedException e) {
111: // this shouldn't happen, since we are Cloneable
112: throw new InternalError();
113: }
114: }
115:
116: /*
117: * Useful for simple debugging
118: */
119: public String toString() {
120: ByteArrayOutputStream bos = new ByteArrayOutputStream();
121: print(new PrintStream(bos));
122: return bos.toString();
123: }
124:
125: }
|