1 24 25 package org.aspectj.compiler.base.ast; 26 27 import org.aspectj.compiler.base.*; 28 import org.aspectj.compiler.base.JavaCompiler; 29 import org.aspectj.compiler.base.CodeWriter; 30 31 import org.aspectj.compiler.base.bcg.CodeBuilder; 32 import org.aspectj.compiler.base.bcg.Label; 33 34 39 40 public class WhileStmt extends TestStmt { 41 42 public boolean isBreakable() { return true; } 43 public boolean isContinuable() { return true; } 44 45 public void unparse(CodeWriter writer) { 46 writer.writeKeyword("while"); 47 writer.optionalSpace(); 48 writer.parenExpr(test); 49 writer.write(body); 50 } 51 52 public void checkSpec() { 53 body.requireStmt(); 54 super.checkSpec(); 55 } 56 57 60 public void walkFlow(FlowCheckerPass w) { 61 FlowCheckerPass.Set enteringPossiblyAssigned = w.popPossiblyAssigned(); 63 w.processBoolean(getTest()); 64 FlowCheckerPass.Vars p = w.getVars(); 65 FlowCheckerPass.Vars tv = p.getTrue(); 66 FlowCheckerPass.Vars fv = p.getFalse(); 67 68 w.setLive(! getTest().isConstantFalse()); 69 w.setVars(tv); 70 w.enterContext(this); 71 w.process(getBody()); 72 w.leaveContext(); 73 74 FlowCheckerPass.Vars v = w.getVars(); 75 FlowCheckerPass.Vars cv = w.getContinueVars(this); 76 FlowCheckerPass.Vars bv = w.getBreakVars(this); 77 78 FlowCheckerPass.Set loopPossiblyAssigned = w.popPossiblyAssigned(); 79 80 w.checkLoopingFinals(this, loopPossiblyAssigned.inter(enteringPossiblyAssigned), v.join(cv)); 81 w.mergePossiblyAssigned(enteringPossiblyAssigned); 82 w.mergePossiblyAssigned(loopPossiblyAssigned); 83 84 w.setLive(! getTest().isConstantTrue() || w.isBroken(this)); 85 w.setVars(fv.join(bv)); 86 } 87 88 91 public void walkCleanup(ByteCodeCleanupPass w) { 92 if (getTest().isConstantFalse()) return; 93 w.enterContext(this); 94 w.process(getBody()); 95 w.leaveContext(); 96 w.setLive(! getTest().isConstantTrue() || w.isBroken(this)); 97 } 98 99 public ASTObject postCleanup(ByteCodeCleanupPass w) { 100 if (getTest().isConstantFalse()) { 101 return getAST().makeEmptyStmt().setSource(this); 102 } else { 103 return this; 104 } 105 } 106 107 110 protected void cgStmt(CodeBuilder cb) { 111 Label startLab = cb.genLabel(); 112 Label testLab = cb.genLabel(); 113 Label endLab = cb.genLabel(); 114 cb.emitJump(testLab); 115 cb.emitLabel(startLab); 116 cb.enterNonWindingContext(this, endLab, testLab); 117 body.cgTop(cb); 118 cb.leaveContext(); 119 cb.emitLabel(testLab); 120 test.cgTest(cb, startLab, endLab); 121 cb.emitLabel(endLab); 122 } 123 124 protected Expr test; 126 public Expr getTest() { return test; } 127 public void setTest(Expr _test) { 128 if (_test != null) _test.setParent(this); 129 test = _test; 130 } 131 132 protected Stmt body; 133 public Stmt getBody() { return body; } 134 public void setBody(Stmt _body) { 135 if (_body != null) _body.setParent(this); 136 body = _body; 137 } 138 139 public WhileStmt(SourceLocation location, Expr _test, Stmt _body) { 140 super(location); 141 setTest(_test); 142 setBody(_body); 143 } 144 protected WhileStmt(SourceLocation source) { 145 super(source); 146 } 147 148 public ASTObject copyWalk(CopyWalker walker) { 149 WhileStmt ret = new WhileStmt(getSourceLocation()); 150 ret.preCopy(walker, this); 151 if (test != null) ret.setTest( (Expr)walker.process(test) ); 152 if (body != null) ret.setBody( (Stmt)walker.process(body) ); 153 return ret; 154 } 155 156 public ASTObject getChildAt(int childIndex) { 157 switch(childIndex) { 158 case 0: return test; 159 case 1: return body; 160 default: return super.getChildAt(childIndex); 161 } 162 } 163 public String getChildNameAt(int childIndex) { 164 switch(childIndex) { 165 case 0: return "test"; 166 case 1: return "body"; 167 default: return super.getChildNameAt(childIndex); 168 } 169 } 170 public void setChildAt(int childIndex, ASTObject child) { 171 switch(childIndex) { 172 case 0: setTest((Expr)child); return; 173 case 1: setBody((Stmt)child); return; 174 default: super.setChildAt(childIndex, child); return; 175 } 176 } 177 public int getChildCount() { 178 return 2; 179 } 180 181 public String getDefaultDisplayName() { 182 return "WhileStmt()"; 183 } 184 185 } 187 | Popular Tags |