KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4
5 import polyglot.types.*;
6 import polyglot.util.*;
7 import polyglot.visit.*;
8 import java.util.*;
9
10 /**
11  * A <code>Formal</code> represents a formal parameter for a procedure
12  * or catch block. It consists of a type and a variable identifier.
13  */

14 public class Formal_c extends Term_c implements Formal
15 {
16     LocalInstance li;
17     Flags flags;
18     TypeNode type;
19     String JavaDoc name;
20     boolean reachable;
21
22     public Formal_c(Position pos, Flags flags, TypeNode type,
23                     String JavaDoc name)
24     {
25     super(pos);
26         this.flags = flags;
27         this.type = type;
28         this.name = name;
29     }
30
31     /** Get the type of the formal. */
32     public Type declType() {
33         return type.type();
34     }
35
36     /** Get the flags of the formal. */
37     public Flags flags() {
38     return flags;
39     }
40
41     /** Set the flags of the formal. */
42     public Formal flags(Flags flags) {
43     Formal_c n = (Formal_c) copy();
44     n.flags = flags;
45     return n;
46     }
47
48     /** Get the type node of the formal. */
49     public TypeNode type() {
50     return type;
51     }
52
53     /** Set the type node of the formal. */
54     public Formal type(TypeNode type) {
55     Formal_c n = (Formal_c) copy();
56     n.type = type;
57     return n;
58     }
59
60     /** Get the name of the formal. */
61     public String JavaDoc name() {
62     return name;
63     }
64
65     /** Set the name of the formal. */
66     public Formal name(String JavaDoc name) {
67     Formal_c n = (Formal_c) copy();
68     n.name = name;
69     return n;
70     }
71
72     /** Get the local instance of the formal. */
73     public LocalInstance localInstance() {
74         return li;
75     }
76
77     /** Set the local instance of the formal. */
78     public Formal localInstance(LocalInstance li) {
79         Formal_c n = (Formal_c) copy();
80     n.li = li;
81     return n;
82     }
83
84     /** Reconstruct the formal. */
85     protected Formal_c reconstruct(TypeNode type) {
86     if (this.type != type) {
87         Formal_c n = (Formal_c) copy();
88         n.type = type;
89         return n;
90     }
91
92     return this;
93     }
94
95     /** Visit the children of the formal. */
96     public Node visitChildren(NodeVisitor v) {
97     TypeNode type = (TypeNode) visitChild(this.type, v);
98     return reconstruct(type);
99     }
100
101     public void addDecls(Context c) {
102         c.addVariable(li);
103     }
104
105     /** Write the formal to an output file. */
106     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
107         w.write(flags.translate());
108         print(type, w, tr);
109         w.write(" ");
110         w.write(name);
111     }
112
113     /** Build type objects for the formal. */
114     public Node buildTypes(TypeBuilder tb) throws SemanticException {
115         Formal_c n = (Formal_c) super.buildTypes(tb);
116
117         TypeSystem ts = tb.typeSystem();
118
119         LocalInstance li = ts.localInstance(position(), Flags.NONE,
120                                             ts.unknownType(position()), name());
121
122         return n.localInstance(li);
123     }
124
125     public Node disambiguate(AmbiguityRemover ar) throws SemanticException {
126         if (declType().isCanonical() && ! li.type().isCanonical()) {
127             TypeSystem ts = ar.typeSystem();
128             LocalInstance li = ts.localInstance(position(), flags(),
129                                                 declType(), name());
130             return localInstance(li);
131         }
132
133         return this;
134     }
135
136     /** Type check the formal. */
137     public Node typeCheck(TypeChecker tc) throws SemanticException {
138     TypeSystem ts = tc.typeSystem();
139
140     try {
141         ts.checkLocalFlags(flags());
142     }
143     catch (SemanticException e) {
144         throw new SemanticException(e.getMessage(), position());
145     }
146
147     return this;
148     }
149
150     public Term entry() {
151         return this;
152     }
153
154     public List acceptCFG(CFGBuilder v, List succs) {
155         return succs;
156     }
157
158     public void dump(CodeWriter w) {
159     super.dump(w);
160
161     if (li != null) {
162         w.allowBreak(4, " ");
163         w.begin(0);
164         w.write("(instance " + li + ")");
165         w.end();
166     }
167
168     w.allowBreak(4, " ");
169     w.begin(0);
170     w.write("(name " + name + ")");
171     w.end();
172     }
173
174     public String JavaDoc toString() {
175         return flags.translate() + type + " " + name;
176     }
177 }
178
Popular Tags