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 import org.aspectj.compiler.base.cst.*; 31 32 import java.util.*; 33 34 import org.aspectj.compiler.base.bcg.CodeBuilder; 35 import org.aspectj.compiler.base.bcg.Label; 36 37 41 42 public class Decs extends Stmt { 44 45 public List getList() { 46 return Arrays.asList(children); 47 } 48 49 public ListIterator iterator() { 50 return new ListIterator() { 51 private int i = -1; 52 private boolean isRemovable = false; 53 54 public int previousIndex() { 55 return i == -1 ? size - 1 : i - 1; 56 } 57 public boolean hasPrevious() { 58 int i = previousIndex(); 59 return i >= 0 && i < size; 60 } 61 public Object previous() { 62 if (! hasPrevious()) { 63 throw new NoSuchElementException(); 64 } 65 isRemovable = true; 66 return children[--i]; 67 } 68 69 public int nextIndex() { 70 return i == -1 ? 0 : i + 1; 71 } 72 public boolean hasNext() { 73 int i = nextIndex(); 74 return i >= 0 && i < size; 75 } 76 public Object next() { 77 if (! hasNext()) { 78 throw new NoSuchElementException(); 79 } 80 isRemovable = true; 81 return children[++i]; 82 } 83 84 public void remove() { 85 if (! isRemovable) { 86 throw new IllegalStateException (); 87 } 88 isRemovable = false; 89 Decs.this.remove(i--); 90 } 91 public void set(Object o) { 92 if (! isRemovable) { 93 throw new IllegalStateException (); 94 } 95 Decs.this.set(i, (Dec)o); 96 } 97 public void add(Object o) { 98 if (i == -1) i = 0; 99 isRemovable = false; 100 Decs.this.add(i++, (Dec)o); 101 } 102 }; 103 } 104 105 public void append(Decs decs) { 107 for(int i=0; i<decs.size(); i++) { 108 append(decs.get(i)); 109 } 110 } 111 112 public void append(Dec dec) { 113 add(dec); 114 } 115 116 public void prepend(Dec dec) { 117 add(0, dec); 118 } 119 120 public void unparse(CodeWriter writer) { 121 final int N = size; 122 for (int i = 0; i < N; i++) { 123 if (i > 0) writer.newLine(); 124 writer.write(children[i]); 125 } 126 } 127 128 public void remove(Dec dec) { 129 int i = indexOf(dec); 130 if (i != -1) remove(i); 131 } 132 133 public void cleanup() { 134 for(int i=0; i < size; i++) { 135 children[i].cleanup(); 136 } 137 super.cleanup(); 138 } 139 140 protected void cgStmt(CodeBuilder cb) { 144 for (int i = 0, len = size; i < len; i++) { 145 children[i].cgTop(cb); 146 } 147 } 148 149 protected int size; 151 public Dec[] children; 152 153 public Decs(SourceLocation location, Dec[] _children) { 154 super(location); 155 for(int i=0; i<_children.length; i++) { 156 if (_children[i] != null) _children[i].setParent(this); 157 } 158 children = _children; 159 size = _children.length; 160 } 161 162 public Decs(SourceLocation location) { 163 this(location, new Dec[] {}); 164 } 165 166 public Decs(SourceLocation location, Dec child1) { 167 this(location, new Dec[] {child1}); 168 } 169 170 public Decs(SourceLocation location, Dec child1, Dec child2) { 171 this(location, new Dec[] {child1, child2}); 172 } 173 174 public Decs(SourceLocation location, Dec child1, Dec child2, Dec child3) { 175 this(location, new Dec[] {child1, child2, child3}); 176 } 177 178 public ASTObject copyWalk(CopyWalker walker) { 179 final int N = size; 180 Dec[] copiedChildren = new Dec[N]; 181 int newIndex = 0; 182 for(int oldIndex=0; oldIndex<N; oldIndex++) { 183 Dec newChild = (Dec)walker.process(children[oldIndex]); 184 if (newChild != null) copiedChildren[newIndex++] = newChild; 185 } 186 Decs ret = new Decs(getSourceLocation(),copiedChildren); 187 ret.size = newIndex; 188 ret.setSource(this); 189 return ret; 190 } 191 192 public ASTObject getChildAt(int childIndex) { return get(childIndex); } 193 public void setChildAt(int childIndex, ASTObject child) { set(childIndex, (Dec)child); } 194 public String getChildNameAt(int childIndex) { return "dec"+childIndex; } 195 196 public int getChildCount() { return size; } 197 public int size() { return size; } 198 199 public Dec get(int index) { 200 if (index >= size) throw new ArrayIndexOutOfBoundsException (); 201 return children[index]; 202 } 203 204 public void set(int index, Dec child) { 205 if (index >= size) throw new ArrayIndexOutOfBoundsException (); 206 children[index] = child; 207 child.setParent(this); 208 } 209 210 public void resize(int newSize) { 211 if (newSize > children.length) { 212 Dec[] newChildren = new Dec[children.length*2 + 1]; 213 System.arraycopy(children, 0, newChildren, 0, children.length); 214 children = newChildren; 215 } 216 size = newSize; 217 } 218 219 public void addAll(Decs collection) { 220 addAll(size, collection); 221 } 222 223 public void addAll(int index, Decs collection) { 224 for(int i=0; i<collection.size(); i++) { 225 add(index+i, collection.get(i)); 226 } 227 } 228 229 public void add(Dec child) { 230 add(size, child); 231 } 232 233 public void add(int index, Dec child) { 234 if (child == null) return; 235 236 if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException (); 237 238 resize(size+1); 239 240 for(int moveIndex = size-1; moveIndex > index; moveIndex--) { 241 children[moveIndex] = children[moveIndex-1]; 242 } 243 244 children[index] = child; 245 child.setParent(this); 246 } 247 248 public void remove(int index) { 249 if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException (); 250 251 size -= 1; 252 253 for(int moveIndex = index; moveIndex < size; moveIndex++) { 254 children[moveIndex] = children[moveIndex+1]; 255 } 256 } 257 258 public String getDefaultDisplayName() { 259 return "Decs()"; 260 } 261 262 } 264 | Popular Tags |