KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > visit > FlattenVisitor


1 package polyglot.visit;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5
6 import java.util.*;
7
8 /**
9  * The FlattenVisitor flattens the AST,
10  */

11 public class FlattenVisitor extends NodeVisitor
12 {
13     protected TypeSystem ts;
14     protected NodeFactory nf;
15     protected LinkedList stack;
16
17     public FlattenVisitor(TypeSystem ts, NodeFactory nf) {
18     this.ts = ts;
19     this.nf = nf;
20     stack = new LinkedList();
21     }
22
23     public Node override(Node n) {
24     if (n instanceof FieldDecl || n instanceof ConstructorCall) {
25         return n;
26     }
27
28     return null;
29     }
30
31     static int count = 0;
32
33     protected static String JavaDoc newID() {
34     return "flat$$$" + count++;
35     }
36
37     protected Node noFlatten = null;
38
39     /**
40      * When entering a BlockStatement, place a new StatementList
41      * onto the stack
42      */

43     public NodeVisitor enter(Node n) {
44     if (n instanceof Block) {
45         stack.addFirst(new LinkedList());
46     }
47
48     if (n instanceof Eval) {
49         // Don't flatten the expression contained in the statement, but
50
// flatten its subexpressions.
51
Eval s = (Eval) n;
52         noFlatten = s.expr();
53     }
54
55     return this;
56     }
57
58     /**
59      * Flatten complex expressions within the AST
60      */

61     public Node leave(Node old, Node n, NodeVisitor v) {
62     if (n == noFlatten) {
63         noFlatten = null;
64         return n;
65     }
66
67     if (n instanceof Block) {
68         List l = (List) stack.removeFirst();
69         return ((Block) n).statements(l);
70     }
71     else if (n instanceof Stmt && ! (n instanceof LocalDecl)) {
72         List l = (List) stack.getFirst();
73         l.add(n);
74         return n;
75     }
76     else if (n instanceof Expr && ! (n instanceof Lit) &&
77           ! (n instanceof Special) && ! (n instanceof Local)) {
78
79         Expr e = (Expr) n;
80
81         if (e instanceof Assign) {
82             return n;
83         }
84
85         // create a local temp, initialized to the value of the complex
86
// expression
87

88         String JavaDoc name = newID();
89         LocalDecl def = nf.LocalDecl(e.position(), Flags.FINAL,
90                      nf.CanonicalTypeNode(e.position(),
91                                           e.type()),
92                      name, e);
93         def = def.localInstance(ts.localInstance(e.position(), Flags.FINAL,
94                              e.type(), name));
95
96         List l = (List) stack.getFirst();
97         l.add(def);
98
99         // return the local temp instead of the complex expression
100
Local use = nf.Local(e.position(), name);
101         use = (Local) use.type(e.type());
102         use = use.localInstance(ts.localInstance(e.position(), Flags.FINAL,
103                              e.type(), name));
104         return use;
105     }
106
107     return n;
108     }
109 }
110
Popular Tags