KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > WhileStmt


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.base.ast;
26
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.base.JavaCompiler;
29 import org.aspectj.compiler.base.CodeWriter;
30
31 import org.aspectj.compiler.base.bcg.CodeBuilder;
32 import org.aspectj.compiler.base.bcg.Label;
33
34 /**
35  * @grammar while (test) {body}
36  * @child Expr test
37  * @child Stmt body
38  */

39
40 public class WhileStmt extends TestStmt {
41
42     public boolean isBreakable() { return true; }
43     public boolean isContinuable() { return true; }
44
45     public void unparse(CodeWriter writer) {
46         writer.writeKeyword("while");
47         writer.optionalSpace();
48         writer.parenExpr(test);
49         writer.write(body);
50     }
51     
52     public void checkSpec() {
53         body.requireStmt();
54         super.checkSpec();
55     }
56
57     // ------------------------------
58
// INTRO from FlowCheckerPass
59

60     public void walkFlow(FlowCheckerPass w) {
61         // w.setArgs(w.getArgs());
62
FlowCheckerPass.Set enteringPossiblyAssigned = w.popPossiblyAssigned();
63         w.processBoolean(getTest());
64         FlowCheckerPass.Vars p = w.getVars();
65         FlowCheckerPass.Vars tv = p.getTrue();
66         FlowCheckerPass.Vars fv = p.getFalse();
67
68         w.setLive(! getTest().isConstantFalse());
69         w.setVars(tv);
70         w.enterContext(this);
71         w.process(getBody());
72         w.leaveContext();
73
74         FlowCheckerPass.Vars v = w.getVars();
75         FlowCheckerPass.Vars cv = w.getContinueVars(this);
76         FlowCheckerPass.Vars bv = w.getBreakVars(this);
77
78         FlowCheckerPass.Set loopPossiblyAssigned = w.popPossiblyAssigned();
79
80         w.checkLoopingFinals(this, loopPossiblyAssigned.inter(enteringPossiblyAssigned), v.join(cv));
81         w.mergePossiblyAssigned(enteringPossiblyAssigned);
82         w.mergePossiblyAssigned(loopPossiblyAssigned);
83
84         w.setLive(! getTest().isConstantTrue() || w.isBroken(this));
85         w.setVars(fv.join(bv));
86     }
87
88     // ------------------------------
89
// INTRO from ByteCodeCleanupPass
90

91     public void walkCleanup(ByteCodeCleanupPass w) {
92         if (getTest().isConstantFalse()) return;
93         w.enterContext(this);
94         w.process(getBody());
95         w.leaveContext();
96         w.setLive(! getTest().isConstantTrue() || w.isBroken(this));
97     }
98
99     public ASTObject postCleanup(ByteCodeCleanupPass w) {
100         if (getTest().isConstantFalse()) {
101             return getAST().makeEmptyStmt().setSource(this);
102         } else {
103             return this;
104         }
105     }
106
107     // ------------------------------
108
// bcg
109

110     protected void cgStmt(CodeBuilder cb) {
111         Label startLab = cb.genLabel();
112         Label testLab = cb.genLabel();
113         Label endLab = cb.genLabel();
114         cb.emitJump(testLab);
115         cb.emitLabel(startLab);
116         cb.enterNonWindingContext(this, endLab, testLab);
117         body.cgTop(cb);
118         cb.leaveContext();
119         cb.emitLabel(testLab);
120         test.cgTest(cb, startLab, endLab);
121         cb.emitLabel(endLab);
122     }
123
124     //BEGIN: Generated from @child and @property
125
protected Expr test;
126     public Expr getTest() { return test; }
127     public void setTest(Expr _test) {
128         if (_test != null) _test.setParent(this);
129         test = _test;
130     }
131
132     protected Stmt body;
133     public Stmt getBody() { return body; }
134     public void setBody(Stmt _body) {
135         if (_body != null) _body.setParent(this);
136         body = _body;
137     }
138
139     public WhileStmt(SourceLocation location, Expr _test, Stmt _body) {
140         super(location);
141         setTest(_test);
142         setBody(_body);
143     }
144     protected WhileStmt(SourceLocation source) {
145         super(source);
146     }
147
148     public ASTObject copyWalk(CopyWalker walker) {
149         WhileStmt ret = new WhileStmt(getSourceLocation());
150         ret.preCopy(walker, this);
151         if (test != null) ret.setTest( (Expr)walker.process(test) );
152         if (body != null) ret.setBody( (Stmt)walker.process(body) );
153         return ret;
154     }
155
156     public ASTObject getChildAt(int childIndex) {
157         switch(childIndex) {
158         case 0: return test;
159         case 1: return body;
160         default: return super.getChildAt(childIndex);
161         }
162     }
163      public String JavaDoc getChildNameAt(int childIndex) {
164         switch(childIndex) {
165         case 0: return "test";
166         case 1: return "body";
167         default: return super.getChildNameAt(childIndex);
168         }
169     }
170      public void setChildAt(int childIndex, ASTObject child) {
171         switch(childIndex) {
172         case 0: setTest((Expr)child); return;
173         case 1: setBody((Stmt)child); return;
174         default: super.setChildAt(childIndex, child); return;
175         }
176     }
177      public int getChildCount() {
178         return 2;
179     }
180
181     public String JavaDoc getDefaultDisplayName() {
182         return "WhileStmt()";
183     }
184
185     //END: Generated from @child and @property
186
}
187
Popular Tags