KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > spark > sets > DoublePointsToSet


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Ondrej Lhotak
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.jimple.spark.sets;
21 import soot.jimple.spark.*;
22 import soot.jimple.spark.pag.Node;
23 import soot.jimple.spark.pag.PAG;
24 import java.util.*;
25 import soot.*;
26
27 /** Implementation of points-to set that holds two sets: one for new
28  * elements that have not yet been propagated, and the other for elements
29  * that have already been propagated.
30  * @author Ondrej Lhotak
31  */

32 public class DoublePointsToSet extends PointsToSetInternal {
33     public DoublePointsToSet( Type type, PAG pag ) {
34         super( type );
35         newSet = G.v().newSetFactory.newSet( type, pag );
36         oldSet = G.v().oldSetFactory.newSet( type, pag );
37         this.pag = pag;
38     }
39     /** Returns true if this set contains no run-time objects. */
40     public boolean isEmpty() {
41         return oldSet.isEmpty() && newSet.isEmpty();
42     }
43     /** Returns true if this set shares some objects with other. */
44     public boolean hasNonEmptyIntersection( PointsToSet other ) {
45         return oldSet.hasNonEmptyIntersection(other)
46             || newSet.hasNonEmptyIntersection(other);
47     }
48     /** Set of all possible run-time types of objects in the set. */
49     public Set possibleTypes() {
50         Set ret = new HashSet();
51         ret.addAll(oldSet.possibleTypes());
52         ret.addAll(newSet.possibleTypes());
53         return ret;
54     }
55     /** Adds contents of other into this set, returns true if this set
56      * changed. */

57     public boolean addAll( PointsToSetInternal other,
58             PointsToSetInternal exclude ) {
59         if( exclude != null ) {
60             throw new RuntimeException JavaDoc( "NYI" );
61         }
62         return newSet.addAll( other, oldSet );
63     }
64     /** Calls v's visit method on all nodes in this set. */
65     public boolean forall( P2SetVisitor v ) {
66         oldSet.forall( v );
67         newSet.forall( v );
68         return v.getReturnValue();
69     }
70     /** Adds n to this set, returns true if n was not already in this set. */
71     public boolean add( Node n ) {
72         if( oldSet.contains( n ) ) return false;
73         return newSet.add( n );
74     }
75     /** Returns set of nodes already present before last call to flushNew. */
76     public PointsToSetInternal getOldSet() { return oldSet; }
77     /** Returns set of newly-added nodes since last call to flushNew. */
78     public PointsToSetInternal getNewSet() { return newSet; }
79     /** Sets all newly-added nodes to old nodes. */
80     public void flushNew() {
81         oldSet.addAll( newSet, null );
82         newSet = G.v().newSetFactory.newSet( type, pag );
83     }
84     /** Sets all nodes to newly-added nodes. */
85     public void unFlushNew() {
86         newSet.addAll( oldSet, null );
87         oldSet = G.v().oldSetFactory.newSet( type, pag );
88     }
89     /** Merges other into this set. */
90     public void mergeWith( PointsToSetInternal other ) {
91         if( !( other instanceof DoublePointsToSet ) ) {
92             throw new RuntimeException JavaDoc( "NYI" );
93         }
94         final DoublePointsToSet o = (DoublePointsToSet) other;
95         if( other.type != null && !( other.type.equals( type ) ) ) {
96             throw new RuntimeException JavaDoc( "different types "+type+" and "+other.type );
97         }
98         if( other.type == null && type != null ) {
99             throw new RuntimeException JavaDoc( "different types "+type+" and "+other.type );
100         }
101         final PointsToSetInternal newNewSet = G.v().newSetFactory.newSet( type, pag );
102         final PointsToSetInternal newOldSet = G.v().oldSetFactory.newSet( type, pag );
103         oldSet.forall( new P2SetVisitor() {
104         public final void visit( Node n ) {
105             if( o.oldSet.contains( n ) ) newOldSet.add( n );
106         }} );
107         newNewSet.addAll( this, newOldSet );
108         newNewSet.addAll( o, newOldSet );
109         newSet = newNewSet;
110         oldSet = newOldSet;
111     }
112     /** Returns true iff the set contains n. */
113     public boolean contains( Node n ) {
114         return oldSet.contains( n ) || newSet.contains( n );
115     }
116
117     public static P2SetFactory getFactory( P2SetFactory newFactory,
118             P2SetFactory oldFactory ) {
119         G.v().newSetFactory = newFactory;
120         G.v().oldSetFactory = oldFactory;
121         return new P2SetFactory() {
122             public PointsToSetInternal newSet( Type type, PAG pag ) {
123                 return new DoublePointsToSet( type, pag );
124             }
125         };
126     }
127
128     /* End of public methods. */
129     /* End of package methods. */
130
131     private PAG pag;
132     protected PointsToSetInternal newSet;
133     protected PointsToSetInternal oldSet;
134 }
135
136
Popular Tags