KickJava   Java API By Example, From Geeks To Geeks.

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


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 (stmt)*
36  * @children Stmt stmt
37  */

38
39 public class Stmts extends ASTObject {
40     public Stmt getSingleStmt() {
41         if (size() == 0) return new EmptyStmt(getSourceLocation());
42         else if (size() == 1) return get(0);
43         else return new BlockStmt(getSourceLocation(),this);
44     }
45
46     public BlockStmt makeBlockStmt() {
47         if (size() == 1 && get(0) instanceof BlockStmt) {
48             return ((BlockStmt)get(0));
49         } else {
50             return getAST().makeBlock(this);
51         }
52     }
53
54
55     static class RemoveReturnWalker extends Walker {
56         public boolean hasReturn = false;
57         public String JavaDoc label;
58
59         public RemoveReturnWalker(JavaCompiler compiler, String JavaDoc label) {
60             super(compiler);
61             this.label = label;
62         }
63
64         public Stmts removeReturns(Stmts stmts) {
65             Stmts ret = (Stmts)process(stmts);
66             if (hasReturn) {
67                 final AST ast = getAST();
68                 return ast.makeStmts(
69                   new LabeledStmt(ast.getSourceLocation(), label, ast.makeBlock(stmts)));
70             } else {
71                 return ret;
72             }
73         }
74
75         public ASTObject process(ASTObject node) {
76             if (node instanceof TypeDec) return node;
77             if (node instanceof CodeDec) return node;
78
79             if (node instanceof ReturnStmt) {
80                 hasReturn = true;
81                 return new BreakStmt(getAST().getSourceLocation(), label);
82             }
83             return super.process(node);
84         }
85     }
86
87
88     public Stmts removeReturns() {
89         RemoveReturnWalker retWalker =
90                 new RemoveReturnWalker(getCompiler(), "AJC$BLOCK"+size());
91         return retWalker.removeReturns(this);
92     }
93
94
95     public void unparse(CodeWriter writer) {
96         final int N = size;
97         int lastBeginLine = -1;
98         for (int i = 0; i < N; i++) {
99             Stmt stmt = children[i];
100
101             // see if this is separated from the statement above by any extra lines
102
// if so add an extra line to the output
103
// int thisBeginLine = stmt.getBeginLine();
104
// if (lastBeginLine != -1 && thisBeginLine != -1) {
105
// if ((thisBeginLine - lastBeginLine) > 1) {
106
// writer.newLine();
107
// }
108
// }
109
// lastBeginLine = thisBeginLine;
110

111             // now actually write the child
112
writer.write(children[i]);
113             writer.newLine();
114         }
115     }
116
117     // ------------------------------
118
// INTRO: ByteCodeCleanupPass
119

120     public void walkCleanup(ByteCodeCleanupPass walker) {
121         final int N = size;
122         int j = 0;
123         for(int i = 0; i < N; i++) {
124             if (! walker.isLive()) break;
125             ASTObject s = walker.process(get(i));
126             if (s != null && ! (s instanceof EmptyStmt)) {
127                 set(j++, (Stmt) s);
128             }
129         }
130         for (int i = j; i < N; i++) {
131             children[i] = null;
132         }
133         size = j;
134     }
135
136     // ------------------------------
137
// bcg
138

139     final void cgStmts(CodeBuilder cb) {
140         for (int i = 0, len = size; i < len; i++) {
141             children[i].cgTop(cb);
142         }
143     }
144
145     //BEGIN: Generated from @child and @property
146
protected int size;
147     public Stmt[] children;
148
149     public Stmts(SourceLocation location, Stmt[] _children) {
150         super(location);
151         for(int i=0; i<_children.length; i++) {
152             if (_children[i] != null) _children[i].setParent(this);
153         }
154         children = _children;
155         size = _children.length;
156     }
157
158     public Stmts(SourceLocation location) {
159         this(location, new Stmt[] {});
160     }
161
162     public Stmts(SourceLocation location, Stmt child1) {
163         this(location, new Stmt[] {child1});
164     }
165
166     public Stmts(SourceLocation location, Stmt child1, Stmt child2) {
167         this(location, new Stmt[] {child1, child2});
168     }
169
170     public Stmts(SourceLocation location, Stmt child1, Stmt child2, Stmt child3) {
171         this(location, new Stmt[] {child1, child2, child3});
172     }
173
174     public ASTObject copyWalk(CopyWalker walker) {
175         final int N = size;
176         Stmt[] copiedChildren = new Stmt[N];
177         int newIndex = 0;
178         for(int oldIndex=0; oldIndex<N; oldIndex++) {
179             Stmt newChild = (Stmt)walker.process(children[oldIndex]);
180             if (newChild != null) copiedChildren[newIndex++] = newChild;
181         }
182         Stmts ret = new Stmts(getSourceLocation(),copiedChildren);
183         ret.size = newIndex;
184         ret.setSource(this);
185         return ret;
186     }
187
188     public ASTObject getChildAt(int childIndex) { return get(childIndex); }
189     public void setChildAt(int childIndex, ASTObject child) { set(childIndex, (Stmt)child); }
190     public String JavaDoc getChildNameAt(int childIndex) { return "stmt"+childIndex; }
191
192     public int getChildCount() { return size; }
193     public int size() { return size; }
194
195     public Stmt get(int index) {
196         if (index >= size) throw new ArrayIndexOutOfBoundsException JavaDoc();
197         return children[index];
198     }
199
200     public void set(int index, Stmt child) {
201         if (index >= size) throw new ArrayIndexOutOfBoundsException JavaDoc();
202         children[index] = child;
203         child.setParent(this);
204     }
205
206     public void resize(int newSize) {
207         if (newSize > children.length) {
208             Stmt[] newChildren = new Stmt[children.length*2 + 1];
209             System.arraycopy(children, 0, newChildren, 0, children.length);
210             children = newChildren;
211         }
212         size = newSize;
213     }
214
215     public void addAll(Stmts collection) {
216         addAll(size, collection);
217     }
218
219     public void addAll(int index, Stmts collection) {
220         for(int i=0; i<collection.size(); i++) {
221             add(index+i, collection.get(i));
222         }
223     }
224
225     public void add(Stmt child) {
226         add(size, child);
227     }
228
229     public void add(int index, Stmt child) {
230         if (child == null) return;
231
232         if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException JavaDoc();
233
234         resize(size+1);
235
236         for(int moveIndex = size-1; moveIndex > index; moveIndex--) {
237             children[moveIndex] = children[moveIndex-1];
238         }
239
240         children[index] = child;
241         child.setParent(this);
242     }
243
244     public void remove(int index) {
245         if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException JavaDoc();
246
247         size -= 1;
248
249         for(int moveIndex = index; moveIndex < size; moveIndex++) {
250             children[moveIndex] = children[moveIndex+1];
251         }
252     }
253
254     public String JavaDoc getDefaultDisplayName() {
255         return "Stmts()";
256     }
257
258     //END: Generated from @child and @property
259
}
260
Popular Tags