KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.Local;
4 import polyglot.ast.Node;
5 import polyglot.ast.Term;
6 import polyglot.ast.Precedence;
7 import polyglot.types.Context;
8 import polyglot.types.Flags;
9 import polyglot.types.LocalInstance;
10 import polyglot.types.SemanticException;
11 import polyglot.types.TypeSystem;
12 import polyglot.util.CodeWriter;
13 import polyglot.util.Position;
14 import polyglot.visit.PrettyPrinter;
15 import polyglot.visit.TypeBuilder;
16 import polyglot.visit.TypeChecker;
17 import polyglot.visit.CFGBuilder;
18 import java.util.List JavaDoc;
19
20 /**
21  * A local variable expression.
22  */

23 public class Local_c extends Expr_c implements Local
24 {
25   protected String JavaDoc name;
26   protected LocalInstance li;
27
28   public Local_c(Position pos, String JavaDoc name) {
29     super(pos);
30     this.name = name;
31   }
32
33   /** Get the precedence of the local. */
34   public Precedence precedence() {
35     return Precedence.LITERAL;
36   }
37
38   /** Get the name of the local. */
39   public String JavaDoc name() {
40     return this.name;
41   }
42
43   /** Set the name of the local. */
44   public Local name(String JavaDoc name) {
45     Local_c n = (Local_c) copy();
46     n.name = name;
47     return n;
48   }
49
50   /** Return the access flags of the variable. */
51   public Flags flags() {
52     return li.flags();
53   }
54
55   /** Get the local instance of the local. */
56   public LocalInstance localInstance() {
57     return li;
58   }
59
60   /** Set the local instance of the local. */
61   public Local localInstance(LocalInstance li) {
62     Local_c n = (Local_c) copy();
63     n.li = li;
64     return n;
65   }
66
67   public Node buildTypes(TypeBuilder tb) throws SemanticException {
68       Local_c n = (Local_c) super.buildTypes(tb);
69
70       TypeSystem ts = tb.typeSystem();
71
72       LocalInstance li = ts.localInstance(position(), Flags.NONE,
73                                           ts.unknownType(position()), name);
74       return n.localInstance(li);
75   }
76
77   /** Type check the local. */
78   public Node typeCheck(TypeChecker tc) throws SemanticException {
79     Context c = tc.context();
80     LocalInstance li = c.findLocal(name);
81     
82     // if the local is defined in an outer class, then it must be final
83
if (!c.isLocal(li.name())) {
84         // this local is defined in an outer class
85
if (!li.flags().isFinal()) {
86             throw new SemanticException("Local variable \"" + li.name() +
87                     "\" is accessed from an inner class, and must be declared " +
88                     "final.",
89                     this.position());
90         }
91     }
92     
93     return localInstance(li).type(li.type());
94   }
95
96   /**
97    * Return the first (sub)term performed when evaluating this
98    * term.
99    */

100   public Term entry() {
101       return this;
102   }
103
104   /**
105    * Visit this term in evaluation order.
106    */

107   public List JavaDoc acceptCFG(CFGBuilder v, List JavaDoc succs) {
108       return succs;
109   }
110
111   public String JavaDoc toString() {
112     return name;
113   }
114
115   /** Write the local to an output file. */
116   public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
117     w.write(name);
118   }
119
120   /** Dumps the AST. */
121   public void dump(CodeWriter w) {
122     super.dump(w);
123
124     if (li != null) {
125     w.allowBreak(4, " ");
126     w.begin(0);
127     w.write("(instance " + li + ")");
128     w.end();
129     }
130
131     w.allowBreak(4, " ");
132     w.begin(0);
133     w.write("(name " + name + ")");
134     w.end();
135   }
136
137   public boolean isConstant() {
138     return li.isConstant();
139   }
140
141   public Object JavaDoc constantValue() {
142     return li.constantValue();
143   }
144
145 }
146
Popular Tags