KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > ast > IfPcd


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

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 /**
37   * @grammar if(test)
38   * @child Expr test
39   */

40 public class IfPcd extends Pcd {
41     public String JavaDoc toShortString() {
42         return "if(" + test.unparse() + ")";
43     }
44     
45     // ------------------------------
46
// INTRO from InnerInfoPass
47

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     //!!! the parents of a formal get changed when a corresponding method is
77
//!!! made for advice. this works around that other bad design.
78
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         //!!! better error messages for after returning/throwing
90
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         //fixTestExpr();
109
//XXX make sure that thisJoinPoint, ... are 'in scope'
110
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                 //??? optimize this better
127
return FuzzyBoolean.MAYBE;
128             }
129             public JpPlan makePlan(JoinPoint jp) {
130                 //System.out.println("test: " + test.unparse());
131
Expr planTest = makeTestExpr(jp, planData);
132                 
133                 if (containsInners) {
134                     showError("can't use anonymous types in if expressions (compiler limitation)");
135                 }
136                 //System.out.println("ptest: " + planTest.unparse());
137
if (planTest == null) return JpPlan.NO_PLAN;
138                 
139                 JpPlan plan = new JpPlan(jp);
140                 plan.ifTest = planTest;
141                 //remapExprs(plan);
142
return plan;
143             }
144         };
145     }
146     
147     /*
148     void fixTestExpr() {
149         final VarDec aspectInstanceDec =
150             IfPcd.this.getAST().makeVarDec(getLexicalType(),
151                                         "aspect$instance", null);
152         System.out.println(test.unparse());
153         test = (Expr)new Walker(getCompiler()) {
154             public ASTObject process(ASTObject node) {
155                 node = super.process(node);
156                 if (node instanceof ThisExpr) {
157                     return IfPcd.this.getAST().makeVar(aspectInstanceDec);
158                 } else if (node instanceof VarExpr) {
159                     return fixVarExpr( (VarExpr)node );
160                 }
161                 return node;
162             }
163         }.process(test);
164         System.out.println(">>> " + test.unparse());
165     }
166     
167     Expr fixVarExpr(VarExpr var) {
168         if (var.getId().equals("thisJoinPoint")) {
169             return new JpExpr(var.getSourceLocation(), getTypeManager().getJoinPointType()) {
170                 public Expr resolve(JoinPoint jp) {
171                     return jp.makeDynamicJoinPointVarExpr();
172                 }
173             };
174         } else if (var.getId().equals("thisJoinPointStaticPart")) {
175             return new JpExpr(var.getSourceLocation(), getTypeManager().getJoinPointStaticPartType()) {
176                 public Expr resolve(JoinPoint jp) {
177                     return jp.makeStaticJoinPointVarExpr();
178                 }
179             };
180         } else if (var.getId().equals("thisEnclosingJoinPointStaticPart")) {
181             return new JpExpr(var.getSourceLocation(), getTypeManager().getJoinPointStaticPartType()) {
182                 public Expr resolve(JoinPoint jp) {
183                     return jp.makeStaticEnclosingJoinPointVarExpr();
184                 }
185             };
186         } else {
187             return var;
188         }
189     }
190     */

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         //XXX should simplify test and check to see if it is always false at this
213
//XXX particular join point and return null if so
214
//return (Expr)test.copy();
215
}
216     
217     //BEGIN: Generated from @child and @property
218
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 JavaDoc 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 JavaDoc getDefaultDisplayName() {
263         return "IfPcd()";
264     }
265     
266     //END: Generated from @child and @property
267
}
268
269
Popular Tags