KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > Cast_c


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4
5 import polyglot.util.*;
6 import polyglot.visit.*;
7 import polyglot.types.*;
8 import java.util.*;
9
10 /**
11  * A <code>Cast</code> is an immutable representation of a casting
12  * operation. It consists of an <code>Expr</code> being cast and a
13  * <code>TypeNode</code> being cast to.
14  */

15 public class Cast_c extends Expr_c implements Cast
16 {
17     protected TypeNode castType;
18     protected Expr expr;
19
20     public Cast_c(Position pos, TypeNode castType, Expr expr) {
21     super(pos);
22     this.castType = castType;
23     this.expr = expr;
24     }
25
26     /** Get the precedence of the expression. */
27     public Precedence precedence() {
28     return Precedence.CAST;
29     }
30
31     /** Get the cast type of the expression. */
32     public TypeNode castType() {
33     return this.castType;
34     }
35
36     /** Set the cast type of the expression. */
37     public Cast castType(TypeNode castType) {
38     Cast_c n = (Cast_c) copy();
39     n.castType = castType;
40     return n;
41     }
42
43     /** Get the expression being cast. */
44     public Expr expr() {
45     return this.expr;
46     }
47
48     /** Set the expression being cast. */
49     public Cast expr(Expr expr) {
50     Cast_c n = (Cast_c) copy();
51     n.expr = expr;
52     return n;
53     }
54
55     /** Reconstruct the expression. */
56     protected Cast_c reconstruct(TypeNode castType, Expr expr) {
57     if (castType != this.castType || expr != this.expr) {
58         Cast_c n = (Cast_c) copy();
59         n.castType = castType;
60         n.expr = expr;
61         return n;
62     }
63
64     return this;
65     }
66
67     /** Visit the children of the expression. */
68     public Node visitChildren(NodeVisitor v) {
69     TypeNode castType = (TypeNode) visitChild(this.castType, v);
70     Expr expr = (Expr) visitChild(this.expr, v);
71     return reconstruct(castType, expr);
72     }
73
74     /** Type check the expression. */
75     public Node typeCheck(TypeChecker tc) throws SemanticException
76     {
77         TypeSystem ts = tc.typeSystem();
78
79         if (! ts.isCastValid(expr.type(), castType.type())) {
80         throw new SemanticException("Cannot cast the expression of type \""
81                     + expr.type() + "\" to type \""
82                     + castType.type() + "\".",
83                         position());
84     }
85
86     return type(castType.type());
87     }
88
89     public Type childExpectedType(Expr child, AscriptionVisitor av) {
90         TypeSystem ts = av.typeSystem();
91
92         if (child == expr) {
93             if (castType.type().isReference()) {
94                 return ts.Object();
95             }
96             else if (castType.type().isNumeric()) {
97                 return ts.Double();
98             }
99             else if (castType.type().isBoolean()) {
100                 return ts.Boolean();
101             }
102         }
103
104         return child.type();
105     }
106   
107     public String JavaDoc toString() {
108     return "(" + castType + ") " + expr;
109     }
110
111     /** Write the expression to an output file. */
112     public void prettyPrint(CodeWriter w, PrettyPrinter tr)
113     {
114     w.begin(0);
115     w.write("(");
116     print(castType, w, tr);
117     w.write(")");
118     w.allowBreak(2, " ");
119     printSubExpr(expr, w, tr);
120     w.end();
121     }
122
123     public Term entry() {
124         return expr.entry();
125     }
126
127     public List acceptCFG(CFGBuilder v, List succs) {
128         v.visitCFG(expr, this);
129         return succs;
130     }
131
132     public List throwTypes(TypeSystem ts) {
133         if (expr.type().isReference()) {
134             return Collections.singletonList(ts.ClassCastException());
135         }
136
137         return Collections.EMPTY_LIST;
138     }
139     
140     public boolean isConstant() {
141     return expr.isConstant() && castType.type().isPrimitive();
142     }
143     
144     public Object JavaDoc constantValue() {
145         Object JavaDoc v = expr.constantValue();
146
147     if (v == null) {
148         return null;
149     }
150     
151         if (v instanceof Boolean JavaDoc) {
152             if (castType.type().isBoolean()) return v;
153         }
154
155         if (v instanceof String JavaDoc) {
156             TypeSystem ts = castType.type().typeSystem();
157             if (castType.type().equals(ts.String())) return v;
158         }
159
160         if (v instanceof Double JavaDoc) {
161             double vv = ((Double JavaDoc) v).doubleValue();
162
163             if (castType.type().isDouble()) return new Double JavaDoc((double) vv);
164             if (castType.type().isFloat()) return new Float JavaDoc((float) vv);
165             if (castType.type().isLong()) return new Long JavaDoc((long) vv);
166             if (castType.type().isInt()) return new Integer JavaDoc((int) vv);
167             if (castType.type().isChar()) return new Character JavaDoc((char) vv);
168             if (castType.type().isShort()) return new Short JavaDoc((short) vv);
169             if (castType.type().isByte()) return new Byte JavaDoc((byte) vv);
170         }
171
172         if (v instanceof Float JavaDoc) {
173             float vv = ((Float JavaDoc) v).floatValue();
174
175             if (castType.type().isDouble()) return new Double JavaDoc((double) vv);
176             if (castType.type().isFloat()) return new Float JavaDoc((float) vv);
177             if (castType.type().isLong()) return new Long JavaDoc((long) vv);
178             if (castType.type().isInt()) return new Integer JavaDoc((int) vv);
179             if (castType.type().isChar()) return new Character JavaDoc((char) vv);
180             if (castType.type().isShort()) return new Short JavaDoc((short) vv);
181             if (castType.type().isByte()) return new Byte JavaDoc((byte) vv);
182         }
183
184         if (v instanceof Number JavaDoc) {
185             long vv = ((Number JavaDoc) v).longValue();
186
187             if (castType.type().isDouble()) return new Double JavaDoc((double) vv);
188             if (castType.type().isFloat()) return new Float JavaDoc((float) vv);
189             if (castType.type().isLong()) return new Long JavaDoc((long) vv);
190             if (castType.type().isInt()) return new Integer JavaDoc((int) vv);
191             if (castType.type().isChar()) return new Character JavaDoc((char) vv);
192             if (castType.type().isShort()) return new Short JavaDoc((short) vv);
193             if (castType.type().isByte()) return new Byte JavaDoc((byte) vv);
194         }
195
196         if (v instanceof Character JavaDoc) {
197             char vv = ((Character JavaDoc) v).charValue();
198
199             if (castType.type().isDouble()) return new Double JavaDoc((double) vv);
200             if (castType.type().isFloat()) return new Float JavaDoc((float) vv);
201             if (castType.type().isLong()) return new Long JavaDoc((long) vv);
202             if (castType.type().isInt()) return new Integer JavaDoc((int) vv);
203             if (castType.type().isChar()) return new Character JavaDoc((char) vv);
204             if (castType.type().isShort()) return new Short JavaDoc((short) vv);
205             if (castType.type().isByte()) return new Byte JavaDoc((byte) vv);
206         }
207
208         // not a constant
209
return null;
210     }
211 }
212
Popular Tags