KickJava   Java API By Example, From Geeks To Geeks.

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


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 if (test) {then} else {else}
36   * @child Expr test
37   * @child Stmt then
38   * @child Stmt else
39   */

40
41 public class IfStmt extends TestStmt {
42
43     public void unparse(CodeWriter writer) {
44         writer.writeKeyword("if");
45         writer.optionalSpace();
46         writer.parenExpr(test);
47         writer.optionalSpace();
48         writer.write(then);
49         if (! (_else instanceof EmptyStmt && _else.isSynthetic())) {
50             writer.writeKeyword("else");
51             writer.optionalSpace();
52             writer.write(_else);
53         }
54     }
55     
56     public void checkSpec() {
57         then.requireStmt();
58         if (_else != null) _else.requireStmt();
59         super.checkSpec();
60     }
61
62     // ------------------------------
63
// INTRO from FlowCheckerPass
64

65     public void walkFlow(FlowCheckerPass w) {
66         // w.setArgs(w.getArgs());
67
w.processBoolean(getTest());
68         FlowCheckerPass.Vars p0 = w.getVars();
69
70         w.setVars(p0.getTrue());
71         w.process(getThen());
72         FlowCheckerPass.Vars v0 = w.getVars();
73         boolean trueLive = w.isLive();
74
75         w.setLive(true);
76         w.setVars(p0.getFalse());
77         w.process(getElse());
78         FlowCheckerPass.Vars v1 = w.getVars();
79         boolean falseLive = w.isLive();
80
81         w.setLive(falseLive || trueLive);
82         w.setVars(v0.join(v1));
83     }
84
85     // ------------------------------
86
// INTRO from ByteCodeCleanupPass
87

88     public void walkCleanup(ByteCodeCleanupPass w) {
89         if (getTest().isConstantTrue()) {
90             w.process(getThen());
91         } else if (getTest().isConstantFalse()) {
92             w.process(getElse());
93         } else {
94             w.setLive(true);
95             setThen((Stmt) w.process(getThen()));
96             boolean thenLive = w.isLive();
97             w.setLive(true);
98             setElse((Stmt) w.process(getElse()));
99             w.setLive(thenLive || w.isLive());
100         }
101     }
102
103     public ASTObject postCleanup(ByteCodeCleanupPass w) {
104         if (getTest().isConstantTrue()) {
105             return getThen();
106         } else if (getTest().isConstantFalse()) {
107             return getElse();
108         } else if (getThen() instanceof EmptyStmt
109                    && getElse() instanceof EmptyStmt) {
110             return getAST().makeStmt(getTest()).setSource(this);
111         } else {
112             return this;
113         }
114     }
115
116     // ------------------------------
117
// bcg
118

119     protected void cgStmt(CodeBuilder cb) {
120         Label trueLab = cb.genLabel();
121         Label falseLab = cb.genLabel();
122         Label endLab = cb.genLabel();
123         getTest().cgTest(cb, trueLab, falseLab);
124         cb.emitLabel(trueLab);
125         getThen().cgTop(cb);
126         if (getThen().completesNormally()) {
127             cb.emitJump(endLab);
128         }
129         cb.emitLabel(falseLab);
130         getElse().cgTop(cb);
131         cb.emitLabel(endLab);
132     }
133
134     //BEGIN: Generated from @child and @property
135
protected Expr test;
136     public Expr getTest() { return test; }
137     public void setTest(Expr _test) {
138         if (_test != null) _test.setParent(this);
139         test = _test;
140     }
141
142     protected Stmt then;
143     public Stmt getThen() { return then; }
144     public void setThen(Stmt _then) {
145         if (_then != null) _then.setParent(this);
146         then = _then;
147     }
148
149     protected Stmt _else;
150     public Stmt getElse() { return _else; }
151     public void setElse(Stmt __else) {
152         if (__else != null) __else.setParent(this);
153         _else = __else;
154     }
155
156     public IfStmt(SourceLocation location, Expr _test, Stmt _then, Stmt __else) {
157         super(location);
158         setTest(_test);
159         setThen(_then);
160         setElse(__else);
161     }
162     protected IfStmt(SourceLocation source) {
163         super(source);
164     }
165
166     public ASTObject copyWalk(CopyWalker walker) {
167         IfStmt ret = new IfStmt(getSourceLocation());
168         ret.preCopy(walker, this);
169         if (test != null) ret.setTest( (Expr)walker.process(test) );
170         if (then != null) ret.setThen( (Stmt)walker.process(then) );
171         if (_else != null) ret.setElse( (Stmt)walker.process(_else) );
172         return ret;
173     }
174
175     public ASTObject getChildAt(int childIndex) {
176         switch(childIndex) {
177         case 0: return test;
178         case 1: return then;
179         case 2: return _else;
180         default: return super.getChildAt(childIndex);
181         }
182     }
183      public String JavaDoc getChildNameAt(int childIndex) {
184         switch(childIndex) {
185         case 0: return "test";
186         case 1: return "then";
187         case 2: return "else";
188         default: return super.getChildNameAt(childIndex);
189         }
190     }
191      public void setChildAt(int childIndex, ASTObject child) {
192         switch(childIndex) {
193         case 0: setTest((Expr)child); return;
194         case 1: setThen((Stmt)child); return;
195         case 2: setElse((Stmt)child); return;
196         default: super.setChildAt(childIndex, child); return;
197         }
198     }
199      public int getChildCount() {
200         return 3;
201     }
202
203     public String JavaDoc getDefaultDisplayName() {
204         return "IfStmt()";
205     }
206
207     //END: Generated from @child and @property
208
}
209
210
211
Popular Tags