KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > scalar > LocalCreation


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Florian Loitsch
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  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.jimple.toolkits.scalar;
28
29 import soot.*;
30 import soot.jimple.*;
31 import soot.util.*;
32 import java.util.*;
33
34 /**
35  * provides an easy interface to handle new var-names. New names are automaticly
36  * added to the chain, and the provided locals are guaranteed to have a unique name.
37  */

38 public class LocalCreation {
39   /** if no prefix is given, this one's used */
40   public static final String JavaDoc DEFAULT_PREFIX = "soot";
41   private String JavaDoc prefix;
42   private int counter;
43   private Set locals;
44   private Chain localChain;
45
46   /**
47    * all actions are done on the given locals-chain. as prefix the
48    * <code>DEFAULT-PREFIX</code> will be used.
49    *
50    * @param chain the locals-chain of a Jimple-body
51    */

52   public LocalCreation(Chain locals) {
53     this(locals, DEFAULT_PREFIX);
54   }
55
56   /**
57    * whenever <code>newLocal(type)</code> will be called, the given prefix is
58    * used.
59    *
60    * @param Chain the locals-chain of a Jimple-body
61    * @param String prefix overrides the DEFAULT-PREFIX
62    */

63   public LocalCreation(Chain locals, String JavaDoc prefix) {
64     this.locals = new HashSet(locals.size());
65     localChain = locals;
66     Iterator it = locals.iterator();
67     while (it.hasNext()) {
68       Local l = (Local)it.next();
69       this.locals.add(l.getName());
70     }
71     this.prefix = prefix;
72     counter = 0; //try the first one with suffix 0.
73
}
74
75   /**
76    * returns a new local with the prefix given to the constructor (or the
77    * default-prefix if none has been given) and the given type.<br>
78    * The returned local will automaticly added to the locals-chain.<br>
79    * The local will be of the form: <tt>prefix</tt><i>X</i> (where the last
80    * <i>X</i> is a number, so the localname is unique).
81    *
82    * @param type the Type of the new local.
83    * @return a new local with a unique name and the given type.
84    */

85   public Local newLocal(Type type) {
86     return newLocal(prefix, type);
87   }
88
89   /**
90    * returns a new local with the given prefix and the given type.<br>
91    * the returned local will automaticly added to the locals-chain.
92    * The local will be of the form: <tt>prefix</tt><i>X</i> (where the last
93    * <i>X</i> is a number, so the localname is unique).
94    *
95    * @param prefix the prefix for the now local.
96    * @param type the Type of the now local.
97    * @return a local with the given prefix and the given type.
98    */

99   public Local newLocal(String JavaDoc prefix, Type type) {
100     int suffix = 0;
101     if (prefix == this.prefix ||
102         prefix.equals(this.prefix)) {
103       suffix = counter;
104     }
105
106     while (locals.contains(prefix + suffix)) suffix++;
107
108     if (prefix == this.prefix ||
109         prefix.equals(this.prefix)) {
110       counter = suffix + 1;
111     }
112     String JavaDoc newName = prefix + suffix;
113     Local newLocal = Jimple.v().newLocal(newName, type);
114     localChain.addLast(newLocal);
115     locals.add(newName);
116     return newLocal;
117   }
118 }
119
Popular Tags