View Javadoc
1   /*
2    * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package sun.tools.tree;
27  
28  import sun.tools.java.*;
29  import java.io.PrintStream;
30  import java.io.ByteArrayOutputStream;
31  
32  /**
33   * WARNING: The contents of this source file are not part of any
34   * supported API.  Code that depends on them does so at its own risk:
35   * they are subject to change or removal without notice.
36   */
37  public
38  class Node implements Constants, Cloneable {
39      int op;
40      long where;
41  
42      /**
43       * Constructor
44       */
45      Node(int op, long where) {
46          this.op = op;
47          this.where = where;
48      }
49  
50      /**
51       * Get the operator
52       */
53      public int getOp() {
54          return op;
55      }
56  
57      /**
58       * Get where
59       */
60      public long getWhere() {
61          return where;
62      }
63  
64      /**
65       * Implicit conversions
66       */
67      public Expression convert(Environment env, Context ctx, Type t, Expression e) {
68          if (e.type.isType(TC_ERROR) || t.isType(TC_ERROR)) {
69              // An error was already reported
70              return e;
71          }
72  
73          if (e.type.equals(t)) {
74              // The types are already the same
75              return e;
76          }
77  
78          try {
79              if (e.fitsType(env, ctx, t)) {
80                  return new ConvertExpression(where, t, e);
81              }
82  
83              if (env.explicitCast(e.type, t)) {
84                  env.error(where, "explicit.cast.needed", opNames[op], e.type, t);
85                  return new ConvertExpression(where, t, e);
86              }
87          } catch (ClassNotFound ee) {
88              env.error(where, "class.not.found", ee.name, opNames[op]);
89          }
90  
91          // The cast is not allowed
92          env.error(where, "incompatible.type", opNames[op], e.type, t);
93          return new ConvertExpression(where, Type.tError, e);
94      }
95  
96      /**
97       * Print
98       */
99      public void print(PrintStream out) {
100         throw new CompilerError("print");
101     }
102 
103     /**
104      * Clone this object.
105      */
106     public Object clone() {
107         try {
108             return super.clone();
109         } catch (CloneNotSupportedException e) {
110             // this shouldn't happen, since we are Cloneable
111             throw (InternalError) new InternalError().initCause(e);
112         }
113     }
114 
115     /*
116      * Useful for simple debugging
117      */
118     public String toString() {
119         ByteArrayOutputStream bos = new ByteArrayOutputStream();
120         print(new PrintStream(bos));
121         return bos.toString();
122     }
123 
124 }