1 24 25 package org.aspectj.compiler.base.ast; 26 27 import org.aspectj.compiler.base.*; 28 import org.aspectj.compiler.base.cst.*; 29 30 import java.util.*; 31 32 36 37 public class Formals extends ASTObject { 38 39 public CodeDec getEnclosingCodeDec() { 41 if (getParent() instanceof CodeDec) return (CodeDec)getParent(); 42 return super.getEnclosingCodeDec(); 43 } 44 45 public Set makeSet() { 46 Set ret = new HashSet(); 47 for (int i = 0, len = size(); i < len; i++) { 48 ret.add(get(i)); 49 } 50 return ret; 51 } 52 53 public Iterator iterator() { 54 return new Iterator() { 55 private int i = 0; 56 public boolean hasNext() { return i < size; } 57 public Object next() { return children[i++]; } 58 public void remove() { 59 throw new UnsupportedOperationException (); 60 } 61 }; 62 } 63 64 public boolean canBeCalledWith(Exprs parameters) { 65 if (this.size() != parameters.size()) return false; 66 for(int i=0; i<parameters.size(); i++) { 67 if (!parameters.get(i).getType().isMethodConvertableTo(this.get(i).getType())) { 68 return false; 69 } 70 } 71 return true; 72 } 73 74 public boolean matches(Formals otherFormals) { 75 78 if (size() != otherFormals.size()) return false; 79 for(int i=0; i<size(); i++) { 80 if (!get(i).getType().isEquivalent(otherFormals.get(i).getType())) return false; 82 } 83 return true; 84 } 85 86 public FormalDec findName(String name) { 87 final int N = size(); 88 for(int i=0; i<N; i++) { 89 if (get(i).getId().equals(name)) { 90 return get(i); 91 } 92 } 93 return null; 94 } 95 96 101 public Map findTransform(Formals otherFormals) { 102 FormalDec[] formals1 = children; 103 FormalDec[] formals2 = otherFormals.children; 104 Map ft = new HashMap(); 105 106 if (formals1.length != formals2.length) return null; 107 for(int i=0; i<formals1.length; i++) { 108 if (!formals1[i].getType().isEquivalent(formals2[i].getType())) return null; 109 ft.put(formals1[i].getName(), formals2[i]); 110 } 111 return ft; 112 } 113 114 public String toShortString() { 115 StringBuffer buf = new StringBuffer (); 116 buf.append("("); 117 final int N = size(); 118 for(int i=0; i<N; i++) { 119 if (i > 0) buf.append(", "); 120 FormalDec child = get(i); 121 if (child == null) { 122 this.showWarning("null child: "+i+" : "+children.length); 123 continue; 125 } 126 TypeD typeD = child.getTypeD(); 127 if (typeD == null) child.showError("null typeD on :"+child.getId()); 128 buf.append(typeD.toShortString()); 129 } 130 buf.append(")"); 131 return buf.toString(); 132 } 133 134 135 public void unparse(CodeWriter writer) { 136 writer.openParen('('); 137 writer.writeChildrenWithCommas(this); 138 writer.closeParen(')'); 139 } 140 141 public Exprs makeExprs() { 142 final AST ast = getAST(); 143 144 final int N = size(); 145 Exprs args = ast.makeExprs(); 146 for(int i=0; i<N; i++) { 147 args.add(ast.makeVar(get(i))); 148 } 149 return args; 150 } 151 152 protected int size; 154 public FormalDec[] children; 155 156 public Formals(SourceLocation location, FormalDec[] _children) { 157 super(location); 158 for(int i=0; i<_children.length; i++) { 159 if (_children[i] != null) _children[i].setParent(this); 160 } 161 children = _children; 162 size = _children.length; 163 } 164 165 public Formals(SourceLocation location) { 166 this(location, new FormalDec[] {}); 167 } 168 169 public Formals(SourceLocation location, FormalDec child1) { 170 this(location, new FormalDec[] {child1}); 171 } 172 173 public Formals(SourceLocation location, FormalDec child1, FormalDec child2) { 174 this(location, new FormalDec[] {child1, child2}); 175 } 176 177 public Formals(SourceLocation location, FormalDec child1, FormalDec child2, FormalDec child3) { 178 this(location, new FormalDec[] {child1, child2, child3}); 179 } 180 181 public ASTObject copyWalk(CopyWalker walker) { 182 final int N = size; 183 FormalDec[] copiedChildren = new FormalDec[N]; 184 int newIndex = 0; 185 for(int oldIndex=0; oldIndex<N; oldIndex++) { 186 FormalDec newChild = (FormalDec)walker.process(children[oldIndex]); 187 if (newChild != null) copiedChildren[newIndex++] = newChild; 188 } 189 Formals ret = new Formals(getSourceLocation(),copiedChildren); 190 ret.size = newIndex; 191 ret.setSource(this); 192 return ret; 193 } 194 195 public ASTObject getChildAt(int childIndex) { return get(childIndex); } 196 public void setChildAt(int childIndex, ASTObject child) { set(childIndex, (FormalDec)child); } 197 public String getChildNameAt(int childIndex) { return "formal"+childIndex; } 198 199 public int getChildCount() { return size; } 200 public int size() { return size; } 201 202 public FormalDec get(int index) { 203 if (index >= size) throw new ArrayIndexOutOfBoundsException (); 204 return children[index]; 205 } 206 207 public void set(int index, FormalDec child) { 208 if (index >= size) throw new ArrayIndexOutOfBoundsException (); 209 children[index] = child; 210 child.setParent(this); 211 } 212 213 public void resize(int newSize) { 214 if (newSize > children.length) { 215 FormalDec[] newChildren = new FormalDec[children.length*2 + 1]; 216 System.arraycopy(children, 0, newChildren, 0, children.length); 217 children = newChildren; 218 } 219 size = newSize; 220 } 221 222 public void addAll(Formals collection) { 223 addAll(size, collection); 224 } 225 226 public void addAll(int index, Formals collection) { 227 for(int i=0; i<collection.size(); i++) { 228 add(index+i, collection.get(i)); 229 } 230 } 231 232 public void add(FormalDec child) { 233 add(size, child); 234 } 235 236 public void add(int index, FormalDec child) { 237 if (child == null) return; 238 239 if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException (); 240 241 resize(size+1); 242 243 for(int moveIndex = size-1; moveIndex > index; moveIndex--) { 244 children[moveIndex] = children[moveIndex-1]; 245 } 246 247 children[index] = child; 248 child.setParent(this); 249 } 250 251 public void remove(int index) { 252 if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException (); 253 254 size -= 1; 255 256 for(int moveIndex = index; moveIndex < size; moveIndex++) { 257 children[moveIndex] = children[moveIndex+1]; 258 } 259 } 260 261 public String getDefaultDisplayName() { 262 return "Formals()"; 263 } 264 265 } 267 | Popular Tags |