KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > IntLit_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
8 /**
9  * An <code>IntLit</code> represents a literal in Java of an integer
10  * type.
11  */

12 public class IntLit_c extends NumLit_c implements IntLit
13 {
14     /** The kind of literal: INT or LONG. */
15     protected Kind kind;
16
17     public IntLit_c(Position pos, Kind kind, long value) {
18     super(pos, value);
19         this.kind = kind;
20     }
21
22     /**
23      * @return True if this is a boundary case: the literal can only appear
24      * as the operand of a unary minus.
25      */

26     public boolean boundary() {
27         return (kind == INT && (int) value == Integer.MIN_VALUE)
28             || (kind == LONG && value == Long.MIN_VALUE);
29     }
30
31     /** Get the value of the expression. */
32     public long value() {
33         return longValue();
34     }
35
36     /** Set the value of the expression. */
37     public IntLit value(long value) {
38         IntLit_c n = (IntLit_c) copy();
39     n.value = value;
40     return n;
41     }
42
43     /** Get the kind of the expression. */
44     public IntLit.Kind kind() {
45         return kind;
46     }
47
48     /** Set the kind of the expression. */
49     public IntLit kind(IntLit.Kind kind) {
50     IntLit_c n = (IntLit_c) copy();
51     n.kind = kind;
52     return n;
53     }
54
55     /** Type check the expression. */
56     public Node typeCheck(TypeChecker tc) throws SemanticException {
57         TypeSystem ts = tc.typeSystem();
58
59     Kind kind = kind();
60
61         if (kind == INT) {
62         return type(ts.Int());
63     }
64     else if (kind == LONG) {
65         return type(ts.Long());
66     }
67     else {
68         throw new InternalCompilerError("Unrecognized IntLit kind " + kind);
69     }
70     }
71
72     public String JavaDoc positiveToString() {
73     if (kind() == LONG) {
74             if (boundary()) {
75                 // the literal is negative, but print it as positive.
76
return "9223372036854775808L";
77             }
78             else {
79                 return Long.toString(value) + "L";
80             }
81     }
82     else {
83             if (boundary()) {
84                 // the literal is negative, but print it as positive.
85
return "2147483648";
86             }
87             else {
88                 return Long.toString(value);
89             }
90     }
91     }
92
93     public String JavaDoc toString() {
94     if (kind() == LONG) {
95             return Long.toString(value) + "L";
96     }
97     else {
98             return Long.toString(value);
99     }
100     }
101
102     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
103         w.write(toString());
104     }
105
106     public Object JavaDoc constantValue() {
107     if (kind() == LONG) {
108             return new Long JavaDoc(value);
109     }
110     else {
111             return new Integer JavaDoc((int) value);
112     }
113     }
114
115     public Precedence precedence() {
116         if (value < 0L && ! boundary()) {
117             return Precedence.UNARY;
118         }
119         else {
120             return Precedence.LITERAL;
121         }
122     }
123 }
124
Popular Tags