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 38 39 public class Stmts extends ASTObject { 40 public Stmt getSingleStmt() { 41 if (size() == 0) return new EmptyStmt(getSourceLocation()); 42 else if (size() == 1) return get(0); 43 else return new BlockStmt(getSourceLocation(),this); 44 } 45 46 public BlockStmt makeBlockStmt() { 47 if (size() == 1 && get(0) instanceof BlockStmt) { 48 return ((BlockStmt)get(0)); 49 } else { 50 return getAST().makeBlock(this); 51 } 52 } 53 54 55 static class RemoveReturnWalker extends Walker { 56 public boolean hasReturn = false; 57 public String label; 58 59 public RemoveReturnWalker(JavaCompiler compiler, String label) { 60 super(compiler); 61 this.label = label; 62 } 63 64 public Stmts removeReturns(Stmts stmts) { 65 Stmts ret = (Stmts)process(stmts); 66 if (hasReturn) { 67 final AST ast = getAST(); 68 return ast.makeStmts( 69 new LabeledStmt(ast.getSourceLocation(), label, ast.makeBlock(stmts))); 70 } else { 71 return ret; 72 } 73 } 74 75 public ASTObject process(ASTObject node) { 76 if (node instanceof TypeDec) return node; 77 if (node instanceof CodeDec) return node; 78 79 if (node instanceof ReturnStmt) { 80 hasReturn = true; 81 return new BreakStmt(getAST().getSourceLocation(), label); 82 } 83 return super.process(node); 84 } 85 } 86 87 88 public Stmts removeReturns() { 89 RemoveReturnWalker retWalker = 90 new RemoveReturnWalker(getCompiler(), "AJC$BLOCK"+size()); 91 return retWalker.removeReturns(this); 92 } 93 94 95 public void unparse(CodeWriter writer) { 96 final int N = size; 97 int lastBeginLine = -1; 98 for (int i = 0; i < N; i++) { 99 Stmt stmt = children[i]; 100 101 111 writer.write(children[i]); 113 writer.newLine(); 114 } 115 } 116 117 120 public void walkCleanup(ByteCodeCleanupPass walker) { 121 final int N = size; 122 int j = 0; 123 for(int i = 0; i < N; i++) { 124 if (! walker.isLive()) break; 125 ASTObject s = walker.process(get(i)); 126 if (s != null && ! (s instanceof EmptyStmt)) { 127 set(j++, (Stmt) s); 128 } 129 } 130 for (int i = j; i < N; i++) { 131 children[i] = null; 132 } 133 size = j; 134 } 135 136 139 final void cgStmts(CodeBuilder cb) { 140 for (int i = 0, len = size; i < len; i++) { 141 children[i].cgTop(cb); 142 } 143 } 144 145 protected int size; 147 public Stmt[] children; 148 149 public Stmts(SourceLocation location, Stmt[] _children) { 150 super(location); 151 for(int i=0; i<_children.length; i++) { 152 if (_children[i] != null) _children[i].setParent(this); 153 } 154 children = _children; 155 size = _children.length; 156 } 157 158 public Stmts(SourceLocation location) { 159 this(location, new Stmt[] {}); 160 } 161 162 public Stmts(SourceLocation location, Stmt child1) { 163 this(location, new Stmt[] {child1}); 164 } 165 166 public Stmts(SourceLocation location, Stmt child1, Stmt child2) { 167 this(location, new Stmt[] {child1, child2}); 168 } 169 170 public Stmts(SourceLocation location, Stmt child1, Stmt child2, Stmt child3) { 171 this(location, new Stmt[] {child1, child2, child3}); 172 } 173 174 public ASTObject copyWalk(CopyWalker walker) { 175 final int N = size; 176 Stmt[] copiedChildren = new Stmt[N]; 177 int newIndex = 0; 178 for(int oldIndex=0; oldIndex<N; oldIndex++) { 179 Stmt newChild = (Stmt)walker.process(children[oldIndex]); 180 if (newChild != null) copiedChildren[newIndex++] = newChild; 181 } 182 Stmts ret = new Stmts(getSourceLocation(),copiedChildren); 183 ret.size = newIndex; 184 ret.setSource(this); 185 return ret; 186 } 187 188 public ASTObject getChildAt(int childIndex) { return get(childIndex); } 189 public void setChildAt(int childIndex, ASTObject child) { set(childIndex, (Stmt)child); } 190 public String getChildNameAt(int childIndex) { return "stmt"+childIndex; } 191 192 public int getChildCount() { return size; } 193 public int size() { return size; } 194 195 public Stmt get(int index) { 196 if (index >= size) throw new ArrayIndexOutOfBoundsException (); 197 return children[index]; 198 } 199 200 public void set(int index, Stmt child) { 201 if (index >= size) throw new ArrayIndexOutOfBoundsException (); 202 children[index] = child; 203 child.setParent(this); 204 } 205 206 public void resize(int newSize) { 207 if (newSize > children.length) { 208 Stmt[] newChildren = new Stmt[children.length*2 + 1]; 209 System.arraycopy(children, 0, newChildren, 0, children.length); 210 children = newChildren; 211 } 212 size = newSize; 213 } 214 215 public void addAll(Stmts collection) { 216 addAll(size, collection); 217 } 218 219 public void addAll(int index, Stmts collection) { 220 for(int i=0; i<collection.size(); i++) { 221 add(index+i, collection.get(i)); 222 } 223 } 224 225 public void add(Stmt child) { 226 add(size, child); 227 } 228 229 public void add(int index, Stmt child) { 230 if (child == null) return; 231 232 if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException (); 233 234 resize(size+1); 235 236 for(int moveIndex = size-1; moveIndex > index; moveIndex--) { 237 children[moveIndex] = children[moveIndex-1]; 238 } 239 240 children[index] = child; 241 child.setParent(this); 242 } 243 244 public void remove(int index) { 245 if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException (); 246 247 size -= 1; 248 249 for(int moveIndex = index; moveIndex < size; moveIndex++) { 250 children[moveIndex] = children[moveIndex+1]; 251 } 252 } 253 254 public String getDefaultDisplayName() { 255 return "Stmts()"; 256 } 257 258 } 260 | Popular Tags |