View Javadoc

1   /*
2       Furia-chan: An Open Source software license violation detector.    
3       Copyright (C) 2008 Kyushu Institute of Technology
4   
5     	This program is free software: you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation, either version 3 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18  
19  /** 
20  	*  FPhiExpr 
21  	*  
22    *  @author      Arnoldo Jose Muller Molina    
23    */
24  
25  package org.kit.furia.fragment.soot.representation.internal;
26  
27  import java.util.ArrayList;
28  import java.util.HashSet;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Set;
32  import org.apache.log4j.Logger;
33  import org.kit.furia.fragment.soot.representation.*;
34  import soot.Type;
35  import soot.Unit;
36  import soot.UnitPrinter;
37  import soot.Value;
38  import soot.ValueBox;
39  import soot.shimple.PhiExpr;
40  import soot.shimple.Shimple;
41  import soot.toolkits.graph.Block;
42  import soot.toolkits.scalar.ValueUnitPair;
43  import soot.util.Switch;
44  /**
45   * @author Arnoldo Jose Muller Molina
46   * <write something useful here>
47   */
48  public class FPhiExpr implements  PhiExpr, Qable {
49  	
50    /**
51  	 * 
52  	 */
53  	private static final long serialVersionUID = 872958855836670868L;
54  
55    private static final Logger logger = Logger.getLogger(FPhiExpr.class);
56  	
57    /**
58     * A list of conditions related to each of the variables...
59     * 
60     */
61    private List args = new ArrayList();
62    protected Type type = null;
63      
64      
65    public void add(ValueBox vb){
66      args.add(vb);  	
67    }
68      
69    public FPhiExpr(List args, List preds) {
70      this(args);
71    }
72      
73    public FPhiExpr(){
74      	
75    }
76      
77      
78    public FPhiExpr(List args) {
79      Iterator it = args.iterator();
80      type = ((Value)args.get(0)).getType(); // NOPMD by amuller on 11/16/06 4:19 PM
81      while(it.hasNext()){
82        ValueBox vb = new FExprBox((Value)it.next());        	
83        this.args.add(vb);
84      }
85    }
86          
87    public List getArgs(){
88      assert args != null;
89      return args;
90    }
91      
92          
93    public void toString(UnitPrinter up)
94    {
95      up.literal(this.toString());
96    }
97    boolean usingToString =  false;
98    public String toString()
99    {
100     StringBuffer expr = new StringBuffer("");
101     if(!usingToString){
102       usingToString = true;
103       expr.append(Shimple.PHI + "(");
104       Iterator argPairsIt = getArgs().iterator();
105       int i = 0;
106       while(argPairsIt.hasNext()){
107 	ValueBox vb = (ValueBox)argPairsIt.next();
108 	Value arg = vb.getValue();
109 	expr.append(arg.toString());
110 	if(argPairsIt.hasNext())
111 	  expr.append(", ");
112             
113 	i++;
114       }
115 
116       expr.append(")");
117       usingToString = false;
118     }
119     return expr.toString();
120   }
121     
122     
123   public String toQ() throws Exception
124   {
125     StringBuffer expr = new StringBuffer("");
126     if(!usingToString){
127       usingToString = true;
128       expr.append(FuriaConstructDefinitions.FURIA_fphi + "(");
129       Iterator argPairsIt = getArgs().iterator();
130       int i = 0;
131       while(argPairsIt.hasNext()){
132 	ValueBox vb = (ValueBox)argPairsIt.next();
133 	Value arg = vb.getValue();
134 	expr.append(Frimp.toQ(arg));            
135 	if(argPairsIt.hasNext())
136 	  expr.append(",");
137             
138 	i++;
139       }
140 
141       expr.append(")");
142       usingToString = false;
143     }
144     return expr.toString();
145   }
146     
147   public List getUseBoxes()
148   {
149     Set set = new HashSet();    	                
150     Iterator argPairsIt = getArgs().iterator();
151 	       
152     while(argPairsIt.hasNext()){
153       ValueBox vb = (ValueBox) argPairsIt.next();
154       set.addAll(vb.getValue().getUseBoxes());
155       set.add(vb);
156     }
157 	        
158 	      
159     return new ArrayList(set);
160   }
161     
162   public Object clone(){ // NOPMD by amuller on 11/16/06 4:21 PM
163     	
164     List newValues = new ArrayList();
165     Iterator itArgs = getArgs().iterator();
166     while(itArgs.hasNext()){
167       ValueBox vb = (ValueBox)itArgs.next();
168       Value v = vb.getValue();
169     		
170     		
171       newValues.add(Frimp.cloneIfNecessary(v));
172     }
173     
174     FPhiExpr res = new FPhiExpr(newValues);
175     return res;
176   }
177   public int getArgCount(){
178     return args.size();
179   }
180   
181   public boolean equivTo(Object o)
182   {
183     if(o instanceof FPhiExpr){
184       FPhiExpr pe = (FPhiExpr) o;
185 
186       if(getArgCount() != pe.getArgCount())
187 	return false;
188 
189       for(int i = 0; i < getArgCount(); i++){
190 	if(!getArgValueBox(i).getValue().equivTo(pe.getArgValueBox(i).getValue()))
191 	  return false;
192       }
193 
194       return true;
195     }
196 
197     return false;
198   }
199 
200 
201    public ValueBox getArgValueBox(int i){
202     	return (ValueBox) this.args.get(i);
203     }    
204 
205   	public boolean addArg(Value arg, Block pred) {
206 		this.args.add(new FExprBox(arg));
207 		return true;
208 	}
209  
210   public int equivHashCode()
211   {
212     int hashcode = 1;
213         
214     for(int i = 0; i < getArgCount(); i++){
215       hashcode = hashcode * 17 + getArgValueBox(i).getValue().equivHashCode();
216     }
217 
218     return hashcode;
219   }
220     
221   public void apply(Switch sw)
222   {   // FIXME fix this! (+ . +)
223       // maybe it is fine to have this thing commented out.
224     //((ShimpleExprSwitch) sw).casePhiExpr(this);
225   }
226     
227   public Type getType()
228   {
229     return type;
230   }
231     
232   
233   public int getBlockId() {
234     return 0;
235   }
236 
237   public void setBlockId(int blockId) {
238 		
239   }
240 
241   public boolean addArg(soot.Value v ,soot.Unit u){
242     add(new FExprBox(v));
243     return true;
244   }
245 
246 public ValueUnitPair getArgBox(Block arg0) {
247 	return null;
248 }
249 
250 public ValueUnitPair getArgBox(int arg0) {
251 	return null;
252 }
253 
254 public ValueUnitPair getArgBox(Unit arg0) {
255 	return null;
256 }
257 
258 public int getArgIndex(Block arg0) {
259 	return 0;
260 }
261 
262 public int getArgIndex(Unit arg0) {
263 	return 0;
264 }
265 
266 public Unit getPred(int arg0) {
267 	return null;
268 }
269 
270 public List getPreds() {
271 	return null;
272 }
273 
274 public Value getValue(Block arg0) {
275 	return null;
276 }
277 
278 public Value getValue(int arg0) {
279 	return null;
280 }
281 
282 public Value getValue(Unit arg0) {
283 	return null;
284 }
285 
286 public List getValues() {
287 	return null;
288 }
289 
290 public boolean removeArg(Block arg0) {
291 	return false;
292 }
293 
294 public boolean removeArg(int arg0) {
295 	return false;
296 }
297 
298 public boolean removeArg(Unit arg0) {
299 	return false;
300 }
301 
302 public boolean removeArg(ValueUnitPair arg0) {
303 	return false;
304 }
305 
306 public boolean setArg(int arg0, Value arg1, Block arg2) {
307 	return false;
308 }
309 
310 public boolean setArg(int arg0, Value arg1, Unit arg2) {
311 	return false;
312 }
313 
314 public boolean setPred(int arg0, Block arg1) {
315 	return false;
316 }
317 
318 public boolean setPred(int arg0, Unit arg1) {
319 	return false;
320 }
321 
322 public boolean setValue(Block arg0, Value arg1) {
323 	return false;
324 }
325 
326 public boolean setValue(int arg0, Value arg1) {
327 	return false;
328 }
329 
330 public boolean setValue(Unit arg0, Value arg1) {
331 	return false;
332 }
333 
334 public void clearUnitBoxes() {
335 	
336 }
337 
338 public List getUnitBoxes() {
339 	return null;
340 }
341 
342   
343     
344 }