KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.jl.ast;
2
3 import java.util.List JavaDoc;
4
5 import polyglot.ast.*;
6 import polyglot.types.*;
7 import polyglot.util.CodeWriter;
8 import polyglot.util.Position;
9 import polyglot.visit.*;
10
11 /**
12  * A <code>Special</code> is an immutable representation of a
13  * reference to <code>this</code> or <code>super</code in Java. This
14  * reference can be optionally qualified with a type such as
15  * <code>Foo.this</code>.
16  */

17 public class Special_c extends Expr_c implements Special
18 {
19     protected Special.Kind kind;
20     protected TypeNode qualifier;
21
22     public Special_c(Position pos, Special.Kind kind, TypeNode qualifier) {
23     super(pos);
24     this.kind = kind;
25     this.qualifier = qualifier;
26     }
27
28     /** Get the precedence of the expression. */
29     public Precedence precedence() {
30     return Precedence.LITERAL;
31     }
32
33     /** Get the kind of the special expression, either this or super. */
34     public Special.Kind kind() {
35     return this.kind;
36     }
37
38     /** Set the kind of the special expression, either this or super. */
39     public Special kind(Special.Kind kind) {
40     Special_c n = (Special_c) copy();
41     n.kind = kind;
42     return n;
43     }
44
45     /** Get the qualifier of the special expression. */
46     public TypeNode qualifier() {
47     return this.qualifier;
48     }
49
50     /** Set the qualifier of the special expression. */
51     public Special qualifier(TypeNode qualifier) {
52     Special_c n = (Special_c) copy();
53     n.qualifier = qualifier;
54     return n;
55     }
56
57     /** Reconstruct the expression. */
58     protected Special_c reconstruct(TypeNode qualifier) {
59     if (qualifier != this.qualifier) {
60         Special_c n = (Special_c) copy();
61         n.qualifier = qualifier;
62         return n;
63     }
64
65     return this;
66     }
67
68     /** Visit the children of the expression. */
69     public Node visitChildren(NodeVisitor v) {
70     TypeNode qualifier = (TypeNode) visitChild(this.qualifier, v);
71     return reconstruct(qualifier);
72     }
73
74     /** Type check the expression. */
75     public Node typeCheck(TypeChecker tc) throws SemanticException {
76         TypeSystem ts = tc.typeSystem();
77         Context c = tc.context();
78
79         ClassType t;
80
81         if (qualifier == null) {
82             // an unqualified "this" or "super"
83
t = c.currentClass();
84         }
85         else {
86         if (! qualifier.type().isClass()) {
87         throw new SemanticException("Qualified " + kind +
88             " expression must be of a class type",
89             qualifier.position());
90         }
91
92             t = qualifier.type().toClass();
93
94             if (!c.currentClass().hasEnclosingInstance(t)) {
95                 throw new SemanticException("The nested class \"" +
96                             c.currentClass() + "\" does not have " +
97                             "an enclosing instance of type \"" +
98                             t + "\".", qualifier.position());
99             }
100     }
101
102         if (c.inStaticContext() && ts.equals(t, c.currentClass())) {
103             // trying to access "this" or "super" from a static context.
104
throw new SemanticException("Cannot access a non-static " +
105                 "field or method, or refer to \"this\" or \"super\" " +
106                 "from a static context.", this.position());
107         }
108
109     if (kind == THIS) {
110         return type(t);
111     }
112     else if (kind == SUPER) {
113         return type(t.superType());
114     }
115         return this;
116     }
117
118     /**
119      * Return the first (sub)term performed when evaluating this
120      * term.
121      */

122     public Term entry() {
123         return this;
124     }
125
126     /**
127      * Visit this term in evaluation order.
128      */

129     public List JavaDoc acceptCFG(CFGBuilder v, List JavaDoc succs) {
130         return succs;
131     }
132
133     public String JavaDoc toString() {
134     return (qualifier != null ? qualifier + "." : "") + kind;
135     }
136
137     /** Write the expression to an output file. */
138     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
139     if (qualifier != null) {
140         print(qualifier, w, tr);
141         w.write(".");
142     }
143
144     w.write(kind.toString());
145     }
146
147     public void dump(CodeWriter w) {
148       super.dump(w);
149
150       if (kind != null) {
151         w.allowBreak(4, " ");
152         w.begin(0);
153         w.write("(kind " + kind + ")");
154         w.end();
155       }
156     }
157 }
158
Popular Tags