1 package polyglot.ext.jl.ast; 2 3 import polyglot.ast.*; 4 import polyglot.types.*; 5 import polyglot.util.*; 6 import polyglot.visit.*; 7 import java.util.*; 8 9 13 public abstract class AbstractBlock_c extends Stmt_c implements Block 14 { 15 protected List statements; 16 17 public AbstractBlock_c(Position pos, List statements) { 18 super(pos); 19 this.statements = TypedList.copyAndCheck(statements, Stmt.class, true); 20 } 21 22 23 public List statements() { 24 return this.statements; 25 } 26 27 28 public Block statements(List statements) { 29 AbstractBlock_c n = (AbstractBlock_c) copy(); 30 n.statements = TypedList.copyAndCheck(statements, Stmt.class, true); 31 return n; 32 } 33 34 35 public Block append(Stmt stmt) { 36 List l = new ArrayList(statements.size()+1); 37 l.addAll(statements); 38 l.add(stmt); 39 return statements(l); 40 } 41 42 43 public Block prepend(Stmt stmt) { 44 List l = new ArrayList(statements.size()+1); 45 l.add(stmt); 46 l.addAll(statements); 47 return statements(l); 48 } 49 50 51 protected AbstractBlock_c reconstruct(List statements) { 52 if (! CollectionUtil.equals(statements, this.statements)) { 53 AbstractBlock_c n = (AbstractBlock_c) copy(); 54 n.statements = TypedList.copyAndCheck(statements, Stmt.class, true); 55 return n; 56 } 57 58 return this; 59 } 60 61 62 public Node visitChildren(NodeVisitor v) { 63 List statements = visitList(this.statements, v); 64 return reconstruct(statements); 65 } 66 67 public Context enterScope(Context c) { 68 return c.pushBlock(); 69 } 70 71 72 public void prettyPrint(CodeWriter w, PrettyPrinter tr) { 73 w.begin(0); 74 75 for (Iterator i = statements.iterator(); i.hasNext(); ) { 76 Stmt n = (Stmt) i.next(); 77 printBlock(n, w, tr); 78 79 if (i.hasNext()) { 80 w.newline(0); 81 } 82 } 83 84 w.end(); 85 } 86 87 public Term entry() { 88 return listEntry(statements, this); 89 } 90 91 public List acceptCFG(CFGBuilder v, List succs) { 92 v.visitCFGList(statements, this); 93 return succs; 94 } 95 96 public String toString() { 97 StringBuffer sb = new StringBuffer (); 98 sb.append("{"); 99 100 int count = 0; 101 102 for (Iterator i = statements.iterator(); i.hasNext(); ) { 103 if (count++ > 2) { 104 sb.append(" ..."); 105 break; 106 } 107 108 Stmt n = (Stmt) i.next(); 109 sb.append(" "); 110 sb.append(n.toString()); 111 } 112 113 sb.append(" }"); 114 return sb.toString(); 115 } 116 } 117 | Popular Tags |