KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > internal > SET > SETNode


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jerome Miecznikowski
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.SET;
21 import soot.*;
22
23 import java.io.*;
24 import java.util.*;
25 import soot.util.*;
26 import soot.dava.*;
27 import soot.dava.internal.asg.*;
28 import soot.dava.internal.AST.*;
29 import soot.dava.internal.javaRep.*;
30 import soot.dava.toolkits.base.finders.*;
31
32 public abstract class SETNode
33 {
34     private IterableSet body;
35     private SETNodeLabel label;
36
37     protected SETNode parent;
38     protected AugmentedStmt entryStmt;
39     protected IterableSet predecessors, successors;
40     protected LinkedList subBodies;
41     protected Map body2childChain;
42
43     public abstract IterableSet get_NaturalExits();
44     public abstract ASTNode emit_AST();
45     public abstract AugmentedStmt get_EntryStmt();
46     protected abstract boolean resolve( SETNode parent);
47
48
49     public SETNode( IterableSet body)
50     {
51     this.body = body;
52
53     parent = null;
54     label = new SETNodeLabel();
55     subBodies = new LinkedList();
56     body2childChain = new HashMap();
57     predecessors = new IterableSet();
58     successors = new IterableSet();
59     }
60
61     public void add_SubBody( IterableSet body)
62     {
63     subBodies.add( body);
64     body2childChain.put( body, new IterableSet());
65     }
66
67     public Map get_Body2ChildChain()
68     {
69     return body2childChain;
70     }
71
72     public List get_SubBodies()
73     {
74     return subBodies;
75     }
76
77     public IterableSet get_Body()
78     {
79     return body;
80     }
81
82     public SETNodeLabel get_Label()
83     {
84     return label;
85     }
86
87     public SETNode get_Parent()
88     {
89     return parent;
90     }
91
92     public boolean contains( Object JavaDoc o)
93     {
94     return body.contains( o);
95     }
96
97
98     public IterableSet get_Successors()
99     {
100     return successors;
101     }
102
103     public IterableSet get_Predecessors()
104     {
105     return predecessors;
106     }
107
108     public boolean add_Child( SETNode child, IterableSet children)
109     {
110     if ((this == child) || (children.contains( child)))
111         return false;
112     
113     children.add( child);
114     child.parent = this;
115     return true;
116     }
117
118     public boolean remove_Child( SETNode child, IterableSet children)
119     {
120     if ((this == child) || (children.contains( child) == false))
121         return false;
122
123     children.remove( child);
124     child.parent = null;
125     return true;
126     }
127
128     public boolean insert_ChildBefore( SETNode child, SETNode point, IterableSet children )
129     {
130     if ((this == child) || (this == point) || (children.contains( point) == false))
131         return false;
132
133     children.insertBefore( child, point);
134     child.parent = this;
135     return true;
136     }
137
138     public List emit_ASTBody( IterableSet children)
139     {
140     LinkedList l = new LinkedList();
141
142     Iterator cit = children.iterator();
143     while (cit.hasNext()) {
144         ASTNode astNode = ((SETNode) cit.next()).emit_AST();
145         
146         if (astNode != null)
147         l.addLast( astNode);
148     }
149     
150     return l;
151     }
152     
153
154     /*
155      * Basic inter-SETNode utilities.
156      */

157
158     public IterableSet get_IntersectionWith( SETNode other)
159     {
160     return body.intersection( other.get_Body());
161     }
162
163     public boolean has_IntersectionWith( SETNode other)
164     {
165     Iterator bit = other.get_Body().iterator();
166     while (bit.hasNext())
167         if (body.contains( bit.next()))
168         return true;
169
170     return false;
171     }
172
173     public boolean is_SupersetOf( SETNode other)
174     {
175     return body.isSupersetOf( other.get_Body());
176     }
177
178     public boolean is_StrictSupersetOf( SETNode other)
179     {
180     return body.isStrictSubsetOf( other.get_Body());
181     }
182     
183     
184
185     /*
186      * Tree traversing utilities.
187      */

188
189     public void find_SmallestSETNode( AugmentedStmt as)
190     {
191     Iterator sbit = subBodies.iterator();
192     while (sbit.hasNext()) {
193         Iterator it = ((IterableSet) body2childChain.get( sbit.next())).iterator();
194         while (it.hasNext()) {
195         SETNode child = (SETNode) it.next();
196         
197         if (child.contains( as)) {
198             child.find_SmallestSETNode( as);
199             return;
200         }
201         }
202     }
203         
204     as.myNode = this;
205     }
206
207     public void find_LabeledBlocks( LabeledBlockFinder lbf)
208     {
209     Iterator sbit = subBodies.iterator();
210     while (sbit.hasNext()) {
211         Iterator cit = ((IterableSet) body2childChain.get( sbit.next())).iterator();
212         while (cit.hasNext())
213         ((SETNode) cit.next()).find_LabeledBlocks( lbf);
214     }
215     
216     lbf.perform_ChildOrder( this);
217     lbf.find_LabeledBlocks( this);
218     }
219
220     public void find_StatementSequences( SequenceFinder sf, DavaBody davaBody)
221     {
222     Iterator sbit = subBodies.iterator();
223     while (sbit.hasNext()) {
224
225         IterableSet body = (IterableSet) sbit.next();
226         IterableSet children = (IterableSet) body2childChain.get( body);
227         HashSet childUnion = new HashSet();
228
229         Iterator cit = children.iterator();
230         while (cit.hasNext()) {
231         SETNode child = (SETNode) cit.next();
232
233         child.find_StatementSequences( sf, davaBody);
234         childUnion.addAll( child.get_Body());
235         }
236         
237         sf.find_StatementSequences( this, body, childUnion, davaBody);
238     }
239     }
240
241     public void find_AbruptEdges( AbruptEdgeFinder aef)
242     {
243     Iterator sbit = subBodies.iterator();
244     while (sbit.hasNext()) {
245         IterableSet body = (IterableSet) sbit.next();
246         IterableSet children = (IterableSet) body2childChain.get( body);
247         
248         Iterator cit = children.iterator();
249         while (cit.hasNext())
250         ((SETNode) cit.next()).find_AbruptEdges( aef);
251
252         aef.find_Continues( this, body, children);
253     }
254
255     sbit = subBodies.iterator();
256     while (sbit.hasNext()) {
257         IterableSet children = (IterableSet) body2childChain.get( sbit.next());
258         
259         Iterator cit = children.iterator();
260         if (cit.hasNext()) {
261
262         SETNode
263             cur = (SETNode) cit.next(),
264             prev = null;
265
266
267         while (cit.hasNext()) {
268             prev = cur;
269             cur = (SETNode) cit.next();
270
271             aef.find_Breaks( prev, cur);
272         }
273         }
274     }
275     }
276     
277     protected void remove_AugmentedStmt( AugmentedStmt as)
278     {
279     body.remove( as);
280
281     Iterator it = subBodies.iterator();
282     while (it.hasNext()) {
283         IterableSet subBody = (IterableSet) it.next();
284
285         if (subBody.contains( as)) {
286         subBody.remove( as);
287         return;
288         }
289     }
290     }
291
292
293
294     public boolean nest( SETNode other)
295     {
296     if (other.resolve( this) == false)
297         return false;
298
299     IterableSet otherBody = other.get_Body();
300     
301     Iterator sbit = subBodies.iterator();
302     while (sbit.hasNext()) {
303         IterableSet subBody = (IterableSet) sbit.next();
304         
305         if (subBody.intersects( otherBody)) {
306         IterableSet childChain = (IterableSet) body2childChain.get( subBody);
307         
308         Iterator ccit = childChain.snapshotIterator();
309         while (ccit.hasNext()) {
310             SETNode curChild = (SETNode) ccit.next();
311             
312             IterableSet childBody = curChild.get_Body();
313             
314             if (childBody.intersects( otherBody)) {
315             
316             if (childBody.isSupersetOf( otherBody))
317                 return curChild.nest( other);
318             
319             
320             else {
321                 remove_Child( curChild, childChain);
322                 
323                 Iterator osbit = other.subBodies.iterator();
324                 while (osbit.hasNext()) {
325                 IterableSet otherSubBody = (IterableSet) osbit.next();
326                 
327                 if (otherSubBody.isSupersetOf( childBody)) {
328                     other.add_Child( curChild, (IterableSet) other.get_Body2ChildChain().get( otherSubBody));
329                     break;
330                 }
331                 }
332             }
333             }
334         }
335         
336         add_Child( other, childChain);
337         }
338     }
339
340     return true;
341     }
342
343
344
345     /*
346      * Debugging stuff.
347      */

348     
349     public void dump()
350     {
351     dump( G.v().out);
352     }
353
354     public void dump( PrintStream out)
355     {
356     dump( out, "");
357     }
358
359     private void dump( PrintStream out, String JavaDoc indentation)
360     {
361     String JavaDoc
362         TOP = ".---",
363         TAB = "| " ,
364         MID = "+---",
365         BOT = "`---";
366
367     out.println( indentation);
368         out.println( indentation + TOP);
369     out.println( indentation + TAB + getClass());
370     out.println( indentation + TAB);
371     Iterator it = body.iterator();
372     while (it.hasNext())
373         out.println( indentation + TAB + ((AugmentedStmt) it.next()).toString());
374
375     Iterator sbit = subBodies.iterator();
376     while (sbit.hasNext()) {
377         IterableSet subBody = (IterableSet) sbit.next();
378
379         out.println( indentation + MID);
380         Iterator bit = subBody.iterator();
381         while (bit.hasNext())
382         out.println( indentation + TAB + ((AugmentedStmt) bit.next()).toString());
383
384         out.println( indentation + TAB);
385
386         Iterator cit = ((IterableSet) body2childChain.get( subBody)).iterator();
387         while (cit.hasNext())
388         ((SETNode) cit.next()).dump( out, TAB + indentation);
389     }
390     out.println( indentation + BOT);
391     }
392
393     public void verify()
394     {
395     Iterator sbit = subBodies.iterator();
396     while (sbit.hasNext()) {
397         IterableSet body = (IterableSet) sbit.next();
398         
399         Iterator bit = body.iterator();
400         while (bit.hasNext())
401         if ((bit.next() instanceof AugmentedStmt) == false)
402             G.v().out.println( "Error in body: " + getClass());
403
404         Iterator cit = ((IterableSet) body2childChain.get( body)).iterator();
405         while (cit.hasNext())
406         ((SETNode) cit.next()).verify();
407     }
408     }
409 }
410
Popular Tags