1 19 20 package jode.flow; 21 import jode.decompiler.LocalInfo; 22 import jode.decompiler.TabbedPrintWriter; 23 import jode.expr.Expression; 24 import jode.type.Type; 25 import jode.util.SimpleSet; 26 27 import java.util.Set ; 28 29 33 public class IfThenElseBlock extends StructuredBlock { 34 35 38 Expression cond; 39 42 VariableStack condStack; 43 44 45 48 StructuredBlock thenBlock; 49 50 53 StructuredBlock elseBlock; 54 55 59 public IfThenElseBlock(Expression cond) { 60 this.cond = cond; 61 } 62 63 67 public void setThenBlock(StructuredBlock thenBlock) { 68 this.thenBlock = thenBlock; 69 thenBlock.outer = this; 70 thenBlock.setFlowBlock(flowBlock); 71 } 72 73 77 public void setElseBlock(StructuredBlock elseBlock) { 78 this.elseBlock = elseBlock; 79 elseBlock.outer = this; 80 elseBlock.setFlowBlock(flowBlock); 81 } 82 83 85 86 92 public boolean replaceSubBlock(StructuredBlock oldBlock, 93 StructuredBlock newBlock) { 94 if (thenBlock == oldBlock) 95 thenBlock = newBlock; 96 else if (elseBlock == oldBlock) 97 elseBlock = newBlock; 98 else 99 return false; 100 return true; 101 } 102 103 109 public VariableStack mapStackToLocal(VariableStack stack) { 110 VariableStack newStack; 111 int params = cond.getFreeOperandCount(); 112 if (params > 0) { 113 condStack = stack.peek(params); 114 newStack = stack.pop(params); 115 } else 116 newStack = stack; 117 118 VariableStack after 119 = VariableStack.merge(thenBlock.mapStackToLocal(newStack), 120 elseBlock == null ? newStack 121 : elseBlock.mapStackToLocal(newStack)); 122 if (jump != null) { 123 jump.stackMap = after; 124 return null; 125 } 126 return after; 127 } 128 129 public void removePush() { 130 if (condStack != null) 131 cond = condStack.mergeIntoExpression(cond); 132 thenBlock.removePush(); 133 if (elseBlock != null) 134 elseBlock.removePush(); 135 } 136 137 public Set getDeclarables() { 138 Set used = new SimpleSet(); 139 cond.fillDeclarables(used); 140 return used; 141 } 142 143 153 public void makeDeclaration(Set done) { 154 cond.makeDeclaration(done); 155 super.makeDeclaration(done); 156 } 157 158 163 public void dumpInstruction(TabbedPrintWriter writer) 164 throws java.io.IOException 165 { 166 boolean needBrace = thenBlock.needsBraces(); 167 writer.print("if ("); 168 cond.dumpExpression(writer.EXPL_PAREN, writer); 169 writer.print(")"); 170 if (needBrace) 171 writer.openBrace(); 172 else 173 writer.println(); 174 writer.tab(); 175 thenBlock.dumpSource(writer); 176 writer.untab(); 177 if (elseBlock != null) { 178 if (needBrace) 179 writer.closeBraceContinue(); 180 181 if (elseBlock instanceof IfThenElseBlock 182 && (elseBlock.declare == null 183 || elseBlock.declare.isEmpty())) { 184 needBrace = false; 185 writer.print("else "); 186 elseBlock.dumpSource(writer); 187 } else { 188 needBrace = elseBlock.needsBraces(); 189 writer.print("else"); 190 if (needBrace) 191 writer.openBrace(); 192 else 193 writer.println(); 194 writer.tab(); 195 elseBlock.dumpSource(writer); 196 writer.untab(); 197 } 198 } 199 if (needBrace) 200 writer.closeBrace(); 201 } 202 203 206 public StructuredBlock[] getSubBlocks() { 207 return (elseBlock == null) 208 ? new StructuredBlock[] { thenBlock } 209 : new StructuredBlock[] { thenBlock, elseBlock }; 210 } 211 212 217 public boolean jumpMayBeChanged() { 218 return (thenBlock.jump != null || thenBlock.jumpMayBeChanged()) 219 && elseBlock != null 220 && (elseBlock.jump != null || elseBlock.jumpMayBeChanged()); 221 } 222 223 public void simplify() { 224 cond = cond.simplify(); 225 super.simplify(); 226 } 227 228 public boolean doTransformations() { 229 StructuredBlock last = flowBlock.lastModified; 230 return CreateCheckNull.transformJikes(this, last) 231 || CreateClassField.transform(this,last); 232 } 233 } 234 | Popular Tags |