1 24 25 package org.aspectj.compiler.crosscuts.ast; 26 27 import org.aspectj.compiler.base.ast.*; 28 import org.aspectj.compiler.base.cst.*; 29 import org.aspectj.compiler.crosscuts.joinpoints.*; 30 31 import org.aspectj.compiler.base.*; 32 import org.aspectj.util.FuzzyBoolean; 33 34 import java.util.*; 35 36 40 public class IfPcd extends Pcd { 41 public String toShortString() { 42 return "if(" + test.unparse() + ")"; 43 } 44 45 48 public void walkInnerInfo(InnerInfoPass w) { 49 int context = w.inMember(true); 50 super.walkInnerInfo(w); 51 w.restoreContext(context); 52 } 53 54 public void checkStatic() { showNonStaticError(); } 55 56 public void checkSpec() { 57 getTest().assertType(getTypeManager().booleanType); 58 new Walker(getCompiler()) { 59 public void postProcess(ASTObject object) { 60 if (object instanceof VarExpr) { 61 checkVarExpr((VarExpr)object); 62 } 63 } 64 }.process(getTest()); 65 } 66 67 private AdviceDec getEnclosingAdviceDec(Pcd pcd) { 68 ASTObject parent = pcd.getParent(); 69 if (parent == null) return null; 70 if (parent instanceof AdviceDec) return (AdviceDec)parent; 71 if (parent instanceof Pcd) return getEnclosingAdviceDec((Pcd)parent); 72 return null; 73 } 74 75 76 private void checkVarExpr(VarExpr var) { 79 if (var.isLhs()) { 80 var.showError("can not assign to join point bound variables"); 81 } 82 83 84 AdviceDec adviceDec = getEnclosingAdviceDec(this); 85 if (adviceDec == null) return; 86 87 VarDec dec = var.getVarDec(); 88 89 if (adviceDec instanceof AfterReturningAdviceDec) { 91 if (((AfterReturningAdviceDec)adviceDec).getExtraFormal() == dec) { 92 var.showError("can not use after returning formal in 'if' pcd"); 93 } 94 return; 95 } 96 if (adviceDec instanceof AfterThrowingAdviceDec) { 97 if (((AfterThrowingAdviceDec)adviceDec).getExtraFormal() == dec) { 98 var.showError("can not use after throwing formal in 'if' pcd"); 99 } 100 return; 101 } 102 } 103 104 public void walkScope(ScopeWalker walker) { 105 if (!walker.walkBodies()) return; 106 107 walker.pushBlockScope(); 108 final AST ast = getAST(); 111 walker.addVarDec( 112 ast.makeFinalFormal(getTypeManager().getJoinPointType(), "thisJoinPoint")); 113 walker.addVarDec( 114 ast.makeFinalFormal(getTypeManager().getJoinPointStaticPartType(), "thisJoinPointStaticPart")); 115 116 super.walkScope(walker); 117 118 walker.popScope(); 119 } 120 121 122 private boolean containsInners = false; 123 public JpPlanner makePlanner(final PlanData planData) { 124 return new JpPlanner() { 125 protected FuzzyBoolean fastMatch(JoinPoint jp) { 126 return FuzzyBoolean.MAYBE; 128 } 129 public JpPlan makePlan(JoinPoint jp) { 130 Expr planTest = makeTestExpr(jp, planData); 132 133 if (containsInners) { 134 showError("can't use anonymous types in if expressions (compiler limitation)"); 135 } 136 if (planTest == null) return JpPlan.NO_PLAN; 138 139 JpPlan plan = new JpPlan(jp); 140 plan.ifTest = planTest; 141 return plan; 143 } 144 }; 145 } 146 147 191 192 193 Expr makeTestExpr(JoinPoint jp, PlanData planData) { 194 final VarDec aspectInstanceDec = 195 IfPcd.this.getAST().makeVarDec(planData.getAspectDec().getType(), 196 "aspect$instance", null); 197 198 CopyWalker copyWalker = new CopyWalker(getCompiler()) { 199 public ASTObject process(ASTObject node) { 200 node = super.process(node); 201 if (node instanceof ThisExpr) { 202 return IfPcd.this.getAST().makeVar(aspectInstanceDec); 203 } else if (node instanceof TypeDec) { 204 containsInners = true; 205 } 206 return node; 207 } 208 }; 209 210 return (Expr)copyWalker.process(test); 211 212 } 216 217 protected Expr test; 219 public Expr getTest() { return test; } 220 public void setTest(Expr _test) { 221 if (_test != null) _test.setParent(this); 222 test = _test; 223 } 224 225 public IfPcd(SourceLocation location, Expr _test) { 226 super(location); 227 setTest(_test); 228 } 229 protected IfPcd(SourceLocation source) { 230 super(source); 231 } 232 233 public ASTObject copyWalk(CopyWalker walker) { 234 IfPcd ret = new IfPcd(getSourceLocation()); 235 ret.preCopy(walker, this); 236 if (test != null) ret.setTest( (Expr)walker.process(test) ); 237 return ret; 238 } 239 240 public ASTObject getChildAt(int childIndex) { 241 switch(childIndex) { 242 case 0: return test; 243 default: return super.getChildAt(childIndex); 244 } 245 } 246 public String getChildNameAt(int childIndex) { 247 switch(childIndex) { 248 case 0: return "test"; 249 default: return super.getChildNameAt(childIndex); 250 } 251 } 252 public void setChildAt(int childIndex, ASTObject child) { 253 switch(childIndex) { 254 case 0: setTest((Expr)child); return; 255 default: super.setChildAt(childIndex, child); return; 256 } 257 } 258 public int getChildCount() { 259 return 1; 260 } 261 262 public String getDefaultDisplayName() { 263 return "IfPcd()"; 264 } 265 266 } 268 269 | Popular Tags |