1 package polyglot.visit; 2 3 import polyglot.ast.*; 4 import polyglot.types.*; 5 6 import java.util.*; 7 8 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 newID() { 34 return "flat$$$" + count++; 35 } 36 37 protected Node noFlatten = null; 38 39 43 public NodeVisitor enter(Node n) { 44 if (n instanceof Block) { 45 stack.addFirst(new LinkedList()); 46 } 47 48 if (n instanceof Eval) { 49 Eval s = (Eval) n; 52 noFlatten = s.expr(); 53 } 54 55 return this; 56 } 57 58 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 88 String 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 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 |