1 package polyglot.ext.jl.ast; 2 3 import polyglot.ast.*; 4 import polyglot.types.*; 5 import polyglot.visit.*; 6 import polyglot.util.*; 7 import java.util.*; 8 9 14 public class While_c extends Loop_c implements While 15 { 16 protected Expr cond; 17 protected Stmt body; 18 19 public While_c(Position pos, Expr cond, Stmt body) { 20 super(pos); 21 this.cond = cond; 22 this.body = body; 23 } 24 25 26 public Expr cond() { 27 return this.cond; 28 } 29 30 31 public While cond(Expr cond) { 32 While_c n = (While_c) copy(); 33 n.cond = cond; 34 return n; 35 } 36 37 38 public Stmt body() { 39 return this.body; 40 } 41 42 43 public While body(Stmt body) { 44 While_c n = (While_c) copy(); 45 n.body = body; 46 return n; 47 } 48 49 50 protected While_c reconstruct(Expr cond, Stmt body) { 51 if (cond != this.cond || body != this.body) { 52 While_c n = (While_c) copy(); 53 n.cond = cond; 54 n.body = body; 55 return n; 56 } 57 58 return this; 59 } 60 61 62 public Node visitChildren(NodeVisitor v) { 63 Expr cond = (Expr) visitChild(this.cond, v); 64 Stmt body = (Stmt) visitChild(this.body, v); 65 return reconstruct(cond, body); 66 } 67 68 69 public Node typeCheck(TypeChecker tc) throws SemanticException { 70 TypeSystem ts = tc.typeSystem(); 71 72 if (! ts.equals(cond.type(), ts.Boolean())) { 73 throw new SemanticException( 74 "Condition of while statement must have boolean type.", 75 cond.position()); 76 } 77 78 return this; 79 } 80 81 public Type childExpectedType(Expr child, AscriptionVisitor av) { 82 TypeSystem ts = av.typeSystem(); 83 84 if (child == cond) { 85 return ts.Boolean(); 86 } 87 88 return child.type(); 89 } 90 91 public String toString() { 92 return "while (" + cond + ") ..."; 93 } 94 95 96 public void prettyPrint(CodeWriter w, PrettyPrinter tr) { 97 w.write("while ("); 98 printBlock(cond, w, tr); 99 w.write(")"); 100 printSubStmt(body, w, tr); 101 } 102 103 public Term entry() { 104 return cond.entry(); 105 } 106 107 public List acceptCFG(CFGBuilder v, List succs) { 108 if (condIsConstantTrue()) { 109 v.visitCFG(cond, body.entry()); 110 } 111 else { 112 v.visitCFG(cond, FlowGraph.EDGE_KEY_TRUE, body.entry(), 113 FlowGraph.EDGE_KEY_FALSE, this); 114 } 115 116 v.push(this).visitCFG(body, cond.entry()); 117 118 return succs; 119 } 120 121 public Term continueTarget() { 122 return cond.entry(); 123 } 124 } 125 | Popular Tags |