KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > internal > AST > ASTForLoopNode


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Nomair A. Naeem
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.dava.internal.AST;
21
22 import java.util.*;
23 import soot.*;
24 import soot.jimple.*;
25 import soot.dava.internal.SET.*;
26 import soot.dava.internal.asg.*;
27 import soot.dava.toolkits.base.AST.*;
28 import soot.dava.toolkits.base.AST.analysis.*;
29
30 /*
31   Will contain the For loop Construct
32          ________ _______ _______
33   for ( |___A____| ;|___B___| ;|___C___| )
34
35   * A has to be the following (look at the java grammar specs)
36       --> local variable declarations (int a=0,b=3,c=10)
37                 OR
38       --> assignment expressions
39       --> increment/decrement expressions both pre and post e.g. ++bla,--bla,bla++,bla--
40       --> all sorts of method invocations
41       --> new class instance declaration (dont know exactly what these include)
42       
43   * B can be any ASTCondition
44
45   * C can be
46       --> assignment expressions
47       --> increment/decrement expressions both pre and post e.g. ++bla,--bla,bla++,bla--
48       --> all sorts of method invocations
49       --> new class instance declaration (dont know exactly what these include)
50            
51
52   Extend the ASTControlFlowNode since there is a B (ASTCondition involved)
53   and also since that extends ASTlabeledNoce and a for loop can have an associated label
54
55 */

56 public class ASTForLoopNode extends ASTControlFlowNode
57 {
58     private List init; //list of values
59
//notice B is an ASTCondition and is stored in the parent
60
private List update; //list of values
61

62     private List body;
63
64     public ASTForLoopNode( SETNodeLabel label, List init, ASTCondition condition, List update, List body)
65     {
66     super( label, condition);
67     this.body = body;
68     this.init=init;
69     this.update=update;
70
71     subBodies.add( body);
72     }
73
74     public List getInit(){
75     return init;
76     }
77
78     public List getUpdate(){
79     return update;
80     }
81
82     public void replaceBody(List body){
83     this.body=body;
84     subBodies=new ArrayList();
85     subBodies.add(body);
86     }
87
88     public Object JavaDoc clone()
89     {
90     return new ASTForLoopNode( get_Label(), init,get_Condition(),update, body);
91     }
92     
93     public void toString( UnitPrinter up ){
94     label_toString( up );
95           
96     up.literal( "for" );
97     up.literal( " " );
98     up.literal( "(" );
99     
100     Iterator it = init.iterator();
101     while(it.hasNext()){
102         AugmentedStmt as = (AugmentedStmt)it.next();
103             Unit u = as.get_Stmt();
104             u.toString( up );
105         if(it.hasNext()){
106         up.literal(" , ");
107         }
108     }
109
110     up.literal("; ");
111
112
113     condition.toString( up );
114     up.literal("; ");
115
116     it = update.iterator();
117     while(it.hasNext()){
118         AugmentedStmt as = (AugmentedStmt)it.next();
119             Unit u = as.get_Stmt();
120             u.toString( up );
121         if(it.hasNext()){
122         up.literal(" , ");
123         }
124     }
125
126     up.literal( ")" );
127     up.newline();
128           
129     up.literal( "{" );
130     up.newline();
131           
132     up.incIndent();
133     body_toString( up, body );
134     up.decIndent();
135           
136     up.literal( "}" );
137     up.newline();
138     
139     }
140     
141     public String JavaDoc toString( )
142     {
143     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
144     
145     b.append( label_toString( ));
146     b.append( "for (");
147
148     Iterator it = init.iterator();
149     while(it.hasNext()){
150         b.append( ((Unit) ((AugmentedStmt) it.next()).get_Stmt()).toString());
151         if(it.hasNext()){
152         b.append(" , ");
153         }
154     }
155     b.append("; ");
156
157
158     b.append( get_Condition().toString());
159     b.append( "; ");
160
161
162     it = update.iterator();
163     while(it.hasNext()){
164         b.append( ((Unit) ((AugmentedStmt) it.next()).get_Stmt()).toString());
165         if(it.hasNext()){
166         b.append(" , ");
167         }
168     }
169
170     b.append(")");
171     b.append( NEWLINE);
172       
173     b.append( "{");
174     b.append( NEWLINE);
175       
176     b.append( body_toString( body));
177       
178     b.append( "}");
179     b.append( NEWLINE);
180       
181     return b.toString();
182     }
183
184     
185     public void apply(Analysis a){
186     a.caseASTForLoopNode(this);
187     }
188     
189 }
190
Popular Tags