KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.visit.*;
6 import polyglot.util.*;
7 import java.util.*;
8
9 /**
10  * A <code>Case</code> is a representation of a Java <code>case</code>
11  * statement. It can only be contained in a <code>Switch</code>.
12  */

13 public class Case_c extends Stmt_c implements Case
14 {
15     protected Expr expr;
16     protected long value;
17
18     public Case_c(Position pos, Expr expr) {
19     super(pos);
20     this.expr = expr;
21     }
22
23     /** Returns true iff this is the default case. */
24     public boolean isDefault() {
25     return this.expr == null;
26     }
27
28     /**
29      * Get the case label. This must should a constant expression.
30      * The case label is null for the <code>default</code> case.
31      */

32     public Expr expr() {
33     return this.expr;
34     }
35
36     /** Set the case label. This must should a constant expression, or null. */
37     public Case expr(Expr expr) {
38     Case_c n = (Case_c) copy();
39     n.expr = expr;
40     return n;
41     }
42
43     /**
44      * Returns the value of the case label. This value is only valid
45      * after type-checking.
46      */

47     public long value() {
48     return this.value;
49     }
50
51     /** Set the value of the case label. */
52     public Case value(long value) {
53     Case_c n = (Case_c) copy();
54     n.value = value;
55     return n;
56     }
57
58     /** Reconstruct the statement. */
59     protected Case_c reconstruct(Expr expr) {
60     if (expr != this.expr) {
61         Case_c n = (Case_c) copy();
62         n.expr = expr;
63         return n;
64     }
65
66     return this;
67     }
68
69     /** Visit the children of the statement. */
70     public Node visitChildren(NodeVisitor v) {
71         Expr expr = (Expr) visitChild(this.expr, v);
72         return reconstruct(expr);
73     }
74
75     /** Type check the statement. */
76     public Node typeCheck(TypeChecker tc) throws SemanticException {
77         if (expr == null) {
78         return this;
79     }
80
81     TypeSystem ts = tc.typeSystem();
82
83     if (! ts.isImplicitCastValid(expr.type(), ts.Int())) {
84         throw new SemanticException(
85         "Case label must be an byte, char, short, or int.",
86         position());
87     }
88
89         Object JavaDoc o = null;
90
91     if (expr instanceof Field) {
92         FieldInstance fi = ((Field) expr).fieldInstance();
93
94         if (fi == null) {
95             throw new InternalCompilerError(
96             "Undefined FieldInstance after type-checking.");
97         }
98
99         if (! fi.isConstant()) {
100             throw new SemanticException("Case label must be an integral constant.",
101                         position());
102         }
103
104             o = fi.constantValue();
105         }
106     else if (expr instanceof Local) {
107         LocalInstance li = ((Local) expr).localInstance();
108
109         if (li == null) {
110             throw new InternalCompilerError(
111             "Undefined LocalInstance after type-checking.");
112         }
113
114         if (! li.isConstant()) {
115                 /* FIXME: isConstant() is incorrect
116             throw new SemanticException("Case label must be an integral constant.",
117                         position());
118                 */

119                 return this;
120         }
121
122             o = li.constantValue();
123     }
124     else {
125             o = expr.constantValue();
126         }
127
128         if (o instanceof Number JavaDoc && ! (o instanceof Long JavaDoc) &&
129             ! (o instanceof Float JavaDoc) && ! (o instanceof Double JavaDoc)) {
130
131             return value(((Number JavaDoc) o).longValue());
132         }
133         else if (o instanceof Character JavaDoc) {
134             return value(((Character JavaDoc) o).charValue());
135         }
136
137         throw new SemanticException("Case label must be an integral constant.",
138                                     position());
139     }
140
141     public Type childExpectedType(Expr child, AscriptionVisitor av) {
142         TypeSystem ts = av.typeSystem();
143
144         if (child == expr) {
145             return ts.Int();
146         }
147
148         return child.type();
149     }
150
151     public String JavaDoc toString() {
152         if (expr == null) {
153         return "default:";
154     }
155     else {
156         return "case " + expr + ":";
157     }
158     }
159
160     /** Write the statement to an output file. */
161     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
162         if (expr == null) {
163         w.write("default:");
164     }
165     else {
166         w.write("case ");
167         print(expr, w, tr);
168         w.write(":");
169     }
170     }
171
172     public Term entry() {
173         if (expr != null) return expr;
174         return this;
175     }
176
177     public List acceptCFG(CFGBuilder v, List succs) {
178         if (expr != null) {
179             v.visitCFG(expr, this);
180         }
181
182         return succs;
183     }
184 }
185
Popular Tags