KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > coffi > TypeStack


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997 Clark Verbrugge
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
28
29
30
31
32 package soot.coffi;
33
34 import java.io.*;
35
36 import soot.*;
37
38 /*
39  * A less resource hungry implementation of the TypeStack would just have pointers to
40  * 'sub-stacks' instead of copying the entire array around.
41  */

42
43 class TypeStack
44 {
45     private Type[] types;
46
47     private TypeStack()
48     {
49         // no constructor
50
}
51
52     public Object JavaDoc clone()
53     {
54         TypeStack newTypeStack = new TypeStack();
55
56         newTypeStack.types = (Type[]) types.clone();
57
58         return newTypeStack;
59     }
60
61     /**
62      * Returns an empty stack.
63      */

64
65     public static TypeStack v()
66     {
67         TypeStack typeStack = new TypeStack();
68
69         typeStack.types = new Type[0];
70
71         return typeStack;
72     }
73
74     public TypeStack pop()
75     {
76         TypeStack newStack = new TypeStack();
77
78         newStack.types = new Type[types.length - 1];
79         System.arraycopy(types, 0, newStack.types, 0, types.length - 1);
80
81         return newStack;
82     }
83
84     public TypeStack push(Type type)
85     {
86         TypeStack newStack = new TypeStack();
87
88         newStack.types = new Type[types.length + 1];
89         System.arraycopy(types, 0, newStack.types, 0, types.length);
90
91         newStack.types[types.length] = type;
92
93         return newStack;
94     }
95
96     public Type get(int index)
97     {
98         return types[index];
99     }
100
101     public int topIndex()
102     {
103         return types.length - 1;
104     }
105
106     public Type top()
107     {
108         if(types.length == 0)
109             throw new RuntimeException JavaDoc("TypeStack is empty");
110         else
111             return types[types.length - 1];
112     }
113
114     public boolean equals(Object JavaDoc object)
115     {
116         if(object instanceof TypeStack)
117         {
118             TypeStack otherStack = (TypeStack) object;
119
120             if(otherStack.types.length != types.length)
121                 return false;
122
123             for(int i = 0; i < types.length; i++)
124                 if(!types[i].equals(otherStack.types[i]))
125                     return false;
126
127             return true;
128         }
129         else
130             return false;
131     }
132
133     public TypeStack merge(TypeStack other)
134     {
135
136         if(types.length != other.types.length)
137             throw new RuntimeException JavaDoc("TypeStack merging failed; unequal " +
138             "stack lengths: " + types.length + " and " + other.types.length);
139
140         TypeStack newStack = new TypeStack();
141
142         newStack.types = new Type[other.types.length];
143
144         for(int i = 0; i < types.length; i++)
145             if(types[i].equals(other.types[i]))
146                 newStack.types[i] = types[i];
147             else {
148                 if((!(types[i] instanceof ArrayType) && !(types[i] instanceof RefType)) ||
149                     (!(other.types[i] instanceof RefType) && !(other.types[i] instanceof ArrayType)))
150                 {
151                     throw new RuntimeException JavaDoc("TypeStack merging failed; incompatible types " + types[i] + " and " + other.types[i]);
152                 }
153
154                 // G.v().out.println("Merging: " + types[i] + " with " + other.types[i]);
155

156                 newStack.types[i] = RefType.v("java.lang.Object");
157             }
158
159         return newStack;
160     }
161
162     public void print(PrintStream out)
163     {
164         for(int i = types.length - 1; i >= 0; i--)
165             out.println(i + ": " + types[i].toString());
166
167         if(types.length == 0)
168             out.println("<empty>");
169     }
170 }
171
Popular Tags