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 40 41 public class IfStmt extends TestStmt { 42 43 public void unparse(CodeWriter writer) { 44 writer.writeKeyword("if"); 45 writer.optionalSpace(); 46 writer.parenExpr(test); 47 writer.optionalSpace(); 48 writer.write(then); 49 if (! (_else instanceof EmptyStmt && _else.isSynthetic())) { 50 writer.writeKeyword("else"); 51 writer.optionalSpace(); 52 writer.write(_else); 53 } 54 } 55 56 public void checkSpec() { 57 then.requireStmt(); 58 if (_else != null) _else.requireStmt(); 59 super.checkSpec(); 60 } 61 62 65 public void walkFlow(FlowCheckerPass w) { 66 w.processBoolean(getTest()); 68 FlowCheckerPass.Vars p0 = w.getVars(); 69 70 w.setVars(p0.getTrue()); 71 w.process(getThen()); 72 FlowCheckerPass.Vars v0 = w.getVars(); 73 boolean trueLive = w.isLive(); 74 75 w.setLive(true); 76 w.setVars(p0.getFalse()); 77 w.process(getElse()); 78 FlowCheckerPass.Vars v1 = w.getVars(); 79 boolean falseLive = w.isLive(); 80 81 w.setLive(falseLive || trueLive); 82 w.setVars(v0.join(v1)); 83 } 84 85 88 public void walkCleanup(ByteCodeCleanupPass w) { 89 if (getTest().isConstantTrue()) { 90 w.process(getThen()); 91 } else if (getTest().isConstantFalse()) { 92 w.process(getElse()); 93 } else { 94 w.setLive(true); 95 setThen((Stmt) w.process(getThen())); 96 boolean thenLive = w.isLive(); 97 w.setLive(true); 98 setElse((Stmt) w.process(getElse())); 99 w.setLive(thenLive || w.isLive()); 100 } 101 } 102 103 public ASTObject postCleanup(ByteCodeCleanupPass w) { 104 if (getTest().isConstantTrue()) { 105 return getThen(); 106 } else if (getTest().isConstantFalse()) { 107 return getElse(); 108 } else if (getThen() instanceof EmptyStmt 109 && getElse() instanceof EmptyStmt) { 110 return getAST().makeStmt(getTest()).setSource(this); 111 } else { 112 return this; 113 } 114 } 115 116 119 protected void cgStmt(CodeBuilder cb) { 120 Label trueLab = cb.genLabel(); 121 Label falseLab = cb.genLabel(); 122 Label endLab = cb.genLabel(); 123 getTest().cgTest(cb, trueLab, falseLab); 124 cb.emitLabel(trueLab); 125 getThen().cgTop(cb); 126 if (getThen().completesNormally()) { 127 cb.emitJump(endLab); 128 } 129 cb.emitLabel(falseLab); 130 getElse().cgTop(cb); 131 cb.emitLabel(endLab); 132 } 133 134 protected Expr test; 136 public Expr getTest() { return test; } 137 public void setTest(Expr _test) { 138 if (_test != null) _test.setParent(this); 139 test = _test; 140 } 141 142 protected Stmt then; 143 public Stmt getThen() { return then; } 144 public void setThen(Stmt _then) { 145 if (_then != null) _then.setParent(this); 146 then = _then; 147 } 148 149 protected Stmt _else; 150 public Stmt getElse() { return _else; } 151 public void setElse(Stmt __else) { 152 if (__else != null) __else.setParent(this); 153 _else = __else; 154 } 155 156 public IfStmt(SourceLocation location, Expr _test, Stmt _then, Stmt __else) { 157 super(location); 158 setTest(_test); 159 setThen(_then); 160 setElse(__else); 161 } 162 protected IfStmt(SourceLocation source) { 163 super(source); 164 } 165 166 public ASTObject copyWalk(CopyWalker walker) { 167 IfStmt ret = new IfStmt(getSourceLocation()); 168 ret.preCopy(walker, this); 169 if (test != null) ret.setTest( (Expr)walker.process(test) ); 170 if (then != null) ret.setThen( (Stmt)walker.process(then) ); 171 if (_else != null) ret.setElse( (Stmt)walker.process(_else) ); 172 return ret; 173 } 174 175 public ASTObject getChildAt(int childIndex) { 176 switch(childIndex) { 177 case 0: return test; 178 case 1: return then; 179 case 2: return _else; 180 default: return super.getChildAt(childIndex); 181 } 182 } 183 public String getChildNameAt(int childIndex) { 184 switch(childIndex) { 185 case 0: return "test"; 186 case 1: return "then"; 187 case 2: return "else"; 188 default: return super.getChildNameAt(childIndex); 189 } 190 } 191 public void setChildAt(int childIndex, ASTObject child) { 192 switch(childIndex) { 193 case 0: setTest((Expr)child); return; 194 case 1: setThen((Stmt)child); return; 195 case 2: setElse((Stmt)child); return; 196 default: super.setChildAt(childIndex, child); return; 197 } 198 } 199 public int getChildCount() { 200 return 3; 201 } 202 203 public String getDefaultDisplayName() { 204 return "IfStmt()"; 205 } 206 207 } 209 210 211 | Popular Tags |