KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > AST > structuredAnalysis > MustMayInitialize


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2006 Nomair A. Naeem (nomair.naeem@mail.mcgill.ca)
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 /*
21  * Maintained by Nomair A. Naeem
22  */

23
24 /*
25  * CHANGE LOG: * 30th Jan 2006 Class Created (used atleast by FinalFieldDefinition)
26  *
27  *
28  */

29
30 package soot.dava.toolkits.base.AST.structuredAnalysis;
31
32 import soot.*;
33 import soot.dava.*;
34 import java.util.*;
35 import soot.jimple.*;
36 //import soot.dava.internal.javaRep.*;
37
import soot.dava.internal.AST.*;
38
39 /*
40  * The analysis stores all defs of Locals/SootField. The user can then ask whether a local or SootField
41  isMustInitialized or isMayInitialized
42  *
43  MustInitialize/MayInitialize
44   Step 1:
45           Set of initialized locals/SootField
46   Step 2:
47           A local or SootField is MUST initialized at a program point p if on all paths from the start to this
48       point the local or SootField is assigned a value.
49
50       Similarly a local or SootField is MAY initialized at a program point p if there is a path from the start
51       to this point on wich the local or SootField is assigned
52   Step 3:
53           Forward Analysis
54   Step 4:
55          Intersection/Union
56   Step 5:
57          x = expr
58      kill = {}
59      
60      if x is a local or SootField, gen(x) = {x}
61   Step 6:
62          out(start) = {}
63          newInitialFlow: No copies are available. an empty flow set
64      remember new InitialFlow is ONLY used for input to catchBodies
65  *
66  *
67  */

68
69
70 public class MustMayInitialize extends StructuredAnalysis{
71     HashMap mapping;
72     DavaFlowSet finalResult;
73     
74     public static final int MUST=0;
75     public static final int MAY=1;
76
77     int MUSTMAY;
78
79     public MustMayInitialize(Object JavaDoc analyze,int MUSTorMAY){
80     super();
81     mapping = new HashMap();
82     MUSTMAY=MUSTorMAY;
83     
84     //System.out.println("MustOrMay value is"+MUSTorMAY);
85
setMergeType();
86     //the input to the process method is an empty DavaFlow Set meaning out(start) ={} (no var initialized)
87
finalResult = (DavaFlowSet)process(analyze,new DavaFlowSet());
88
89     //finalResult contains the flowSet of having processed the whole of the method
90
}
91
92
93     public void setMergeType(){
94     //System.out.println("here"+MUSTMAY);
95
if(MUSTMAY == MUST){
96        MERGETYPE=INTERSECTION;
97        //System.out.println("MERGETYPE set to intersection");
98
}
99     else if(MUSTMAY == MAY){
100         MERGETYPE=UNION;
101         //System.out.println("MERGETYPE set to union");
102
}
103     else
104         throw new DavaFlowAnalysisException("Only allowed 0 or 1 for MUST or MAY values");
105     }
106
107     /*
108      * newInitialFlow set is used only for start of catch bodies and here we assume that no var
109      * is ever being initialized
110      */

111     public Object JavaDoc newInitialFlow(){
112     return new DavaFlowSet();
113     }
114     
115     public Object JavaDoc cloneFlowSet(Object JavaDoc flowSet){
116     if(flowSet instanceof DavaFlowSet){
117         return ((DavaFlowSet)flowSet).clone();
118     }
119     else
120         throw new RuntimeException JavaDoc("cloneFlowSet not implemented for other flowSet types");
121     }
122
123
124
125
126     /*
127      * By construction conditions never have assignment statements.
128      * Hence processing a condition has no effect on this analysis
129      */

130     public Object JavaDoc processUnaryBinaryCondition(ASTUnaryBinaryCondition cond, Object JavaDoc input){
131     if(!(input instanceof DavaFlowSet)){
132         throw new DavaFlowAnalysisException("processCondition is not implemented for other flowSet types");
133     }
134     return input;
135     }
136
137
138     /*
139      * By construction the synchronized Local is a Value and can definetly not have an assignment stmt
140      * Processing a synch local has no effect on this analysis
141      */

142     public Object JavaDoc processSynchronizedLocal(Local local,Object JavaDoc input){
143     if(!(input instanceof DavaFlowSet)){
144         throw new RuntimeException JavaDoc("processCondition is not implemented for other flowSet types");
145     }
146     return input;
147     }
148
149
150
151     /*
152      * The switch key is stored as a value and hence can never have an assignment stmt
153      * Processing the switch key has no effect on the analysis
154      */

155     public Object JavaDoc processSwitchKey(Value key,Object JavaDoc input){
156     if(!(input instanceof DavaFlowSet)){
157         throw new RuntimeException JavaDoc("processCondition is not implemented for other flowSet types");
158     }
159     return input;
160     }
161
162
163
164
165
166
167
168
169
170
171     /*
172      * This method internally invoked by the process method decides which Statement
173      * specialized method to call
174      */

175     public Object JavaDoc processStatement(Stmt s, Object JavaDoc input){
176     if(!(input instanceof DavaFlowSet)){
177         throw new RuntimeException JavaDoc("processStatement is not implemented for other flowSet types");
178     }
179     DavaFlowSet inSet = (DavaFlowSet)input;
180
181     /*
182       If this path will not be taken return no path straightaway
183     */

184     if(inSet == NOPATH){
185         return inSet;
186     }
187
188     if(s instanceof DefinitionStmt){
189         DavaFlowSet toReturn = (DavaFlowSet)cloneFlowSet(inSet);
190         // x = expr;
191

192         Value leftOp = ((DefinitionStmt)s).getLeftOp();
193
194         SootField field = null;;
195         if(leftOp instanceof Local){
196         toReturn.add(leftOp);
197
198
199         /*
200          * Gather more information just in case someone might need the def points
201          */

202         Object JavaDoc temp = mapping.get(leftOp);
203         List defs;
204
205         if(temp == null){
206             //first definition
207
defs = new ArrayList();
208         }
209         else{
210             defs = (ArrayList)temp;
211         }
212         defs.add(s);
213         mapping.put(leftOp,defs);
214
215         }
216         else if(leftOp instanceof FieldRef){
217         field = ((FieldRef)leftOp).getField();
218         toReturn.add(field);
219         
220         /*
221          * Gather more information just in case someone might need the def points
222          */

223         Object JavaDoc temp = mapping.get(field);
224         List defs;
225         
226         if(temp == null){
227             //first definition
228
defs = new ArrayList();
229         }
230         else{
231             defs = (ArrayList)temp;
232         }
233         defs.add(s);
234
235         mapping.put(field,defs);
236         }
237         return toReturn;
238     }
239     return input;
240     }
241
242
243
244     public boolean isMayInitialized(SootField field){
245     if(MUSTMAY == MAY){
246         Object JavaDoc temp = mapping.get(field);
247         if(temp == null)
248         return false;
249         else{
250         List list = (List)temp;
251         if(list.size()==0)
252             return false;
253         else
254             return true;
255         }
256     }
257     else
258         throw new RuntimeException JavaDoc("Cannot invoke isMayInitialized for a MUST analysis");
259     }
260
261
262
263     public boolean isMayInitialized(Value local){
264     if(MUSTMAY == MAY){
265         Object JavaDoc temp = mapping.get(local);
266         if(temp == null)
267         return false;
268         else{
269         List list = (List)temp;
270         if(list.size()==0)
271             return false;
272         else
273             return true;
274         }
275     }
276     else
277         throw new RuntimeException JavaDoc("Cannot invoke isMayInitialized for a MUST analysis");
278     }
279
280
281
282
283     public boolean isMustInitialized(SootField field){
284     if(MUSTMAY == MUST){
285         if(finalResult.contains(field))
286         return true;
287         return false;
288     }
289     else
290         throw new RuntimeException JavaDoc("Cannot invoke isMustinitialized for a MAY analysis");
291     }
292
293     public boolean isMustInitialized(Value local){
294     if(MUSTMAY == MUST){
295         if(finalResult.contains(local))
296         return true;
297         return false;
298     }
299     else
300         throw new RuntimeException JavaDoc("Cannot invoke isMustinitialized for a MAY analysis");
301     }
302
303     /*
304      * Given a local ask for all def positions
305      * Notice this could be null in the case there was no definition
306      */

307     public List getDefs(Value local){
308     Object JavaDoc temp = mapping.get(local);
309     if(temp == null)
310         return null;
311     else
312         return (List)temp;
313     }
314
315
316     /*
317      * Given a field ask for all def positions
318      * Notice this could be null in the case there was no definition
319      */

320     public List getDefs(SootField field){
321     Object JavaDoc temp = mapping.get(field);
322     if(temp == null)
323         return null;
324     else
325         return (List)temp;
326     }
327
328
329 }
Popular Tags