1 24 25 package org.aspectj.compiler.crosscuts.ast; 26 27 import org.aspectj.compiler.base.ast.*; 28 import org.aspectj.compiler.crosscuts.joinpoints.*; 29 30 import org.aspectj.compiler.base.JavaCompiler; 31 32 import java.util.*; 33 34 38 public class FormalsPattern extends ASTObject { 39 private boolean isEllipses(GenTypeName name) { 40 return name instanceof EllipsesFakeTypeName; 41 } 42 43 44 public JpPlan finishPlan(JpPlan plan, ASTObject nodes, JoinPoint point, 45 int namesIndex, int formalsIndex) { 46 if (namesIndex >= names.size()) { 47 if (formalsIndex >= nodes.getChildCount()) return plan; 48 else return JpPlan.NO_PLAN; 49 } 50 51 GenTypeName name = names.get(namesIndex); 52 if (isEllipses(name)) { 53 List plans = new LinkedList(); 56 for (; formalsIndex <= nodes.getChildCount(); formalsIndex++) { 57 JpPlan tmp = finishPlan(plan, nodes, point, namesIndex+1, formalsIndex); 58 if (tmp.isPossible()) { plans.add(tmp); } 60 } 61 JpPlan ret = JpPlan.NO_PLAN; 62 for (Iterator i = plans.iterator(); i.hasNext(); ) { 63 JpPlan p = (JpPlan)i.next(); 64 ret = ret.or(p); 65 } 66 return ret; 67 } else { 68 if (formalsIndex >= nodes.getChildCount()) return JpPlan.NO_PLAN; 69 JpPlan paramPlan = name.makePlan(point, (Expr)nodes.getChildAt(formalsIndex)); 70 if (paramPlan.isPossible()) { 71 JpPlan nextPlan = finishPlan(plan, nodes, point, namesIndex+1, formalsIndex+1); 72 return nextPlan.and(paramPlan); 73 } else { 75 return JpPlan.NO_PLAN; 76 } 77 } 78 79 80 } 108 109 110 public JpPlan finishPlan(JpPlan plan, Exprs exprs, JoinPoint point) { 111 return finishPlan(plan, exprs, point, 0, 0); 112 } 113 114 public JpPlan makePlan(Exprs exprs, JoinPoint jp) { 115 return finishPlan(JpPlan.ANY_PLAN, exprs, jp); 116 } 117 118 119 public boolean matches(Formals formals) { 120 return matches(formals, 0, 0); 121 } 122 123 public Type extractType(ASTObject node) { 124 if (node instanceof FormalDec) { 125 return ((FormalDec)node).getType(); 126 } else if (node instanceof Expr) { 127 return ((Expr)node).getType(); 128 } else { 129 return null; 130 } 131 } 132 133 public boolean matches(ASTObject nodes, int namesIndex, int formalsIndex) { 134 if (namesIndex >= names.size()) { 135 if (formalsIndex >= nodes.getChildCount()) return true; 136 else return false; 137 } 138 139 GenTypeName name = names.get(namesIndex); 140 if (isEllipses(name)) { 141 for (; formalsIndex <= nodes.getChildCount(); formalsIndex++) { 142 if (matches(nodes, namesIndex+1, formalsIndex)) return true; 143 } 144 return false; 145 } else { 146 if (formalsIndex >= nodes.getChildCount()) return false; 147 if (name.matches(extractType(nodes.getChildAt(formalsIndex)))) { 148 return matches(nodes, namesIndex+1, formalsIndex+1); 149 } else { 150 return false; 151 } 152 } 153 } 154 155 156 public String toShortString() { 157 StringBuffer buf = new StringBuffer (); 158 buf.append("("); 159 final int N = names.size(); 160 for(int i=0; i<=N; i++) { 161 if (i < N) { 162 buf.append(names.get(i).toShortString()); 163 } 164 if (i < (N-1)) { 165 buf.append(", "); 166 } 167 } 168 buf.append(")"); 169 return buf.toString(); 170 } 171 172 protected GenTypeNames names; 174 public GenTypeNames getNames() { return names; } 175 public void setNames(GenTypeNames _names) { 176 if (_names != null) _names.setParent(this); 177 names = _names; 178 } 179 180 public FormalsPattern(SourceLocation location, GenTypeNames _names) { 181 super(location); 182 setNames(_names); 183 } 184 protected FormalsPattern(SourceLocation source) { 185 super(source); 186 } 187 188 public ASTObject copyWalk(CopyWalker walker) { 189 FormalsPattern ret = new FormalsPattern(getSourceLocation()); 190 ret.preCopy(walker, this); 191 if (names != null) ret.setNames( (GenTypeNames)walker.process(names) ); 192 return ret; 193 } 194 195 public ASTObject getChildAt(int childIndex) { 196 switch(childIndex) { 197 case 0: return names; 198 default: return super.getChildAt(childIndex); 199 } 200 } 201 public String getChildNameAt(int childIndex) { 202 switch(childIndex) { 203 case 0: return "names"; 204 default: return super.getChildNameAt(childIndex); 205 } 206 } 207 public void setChildAt(int childIndex, ASTObject child) { 208 switch(childIndex) { 209 case 0: setNames((GenTypeNames)child); return; 210 default: super.setChildAt(childIndex, child); return; 211 } 212 } 213 public int getChildCount() { 214 return 1; 215 } 216 217 public String getDefaultDisplayName() { 218 return "FormalsPattern()"; 219 } 220 221 } 223 | Popular Tags |