KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > internal > AbstractCastExpr


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrick Lam
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.internal;
28
29 import soot.tagkit.*;
30 import soot.*;
31 import soot.jimple.*;
32 import soot.baf.*;
33 import soot.jimple.*;
34 import soot.util.*;
35 import java.util.*;
36 import soot.grimp.PrecedenceTest;
37
38 abstract public class AbstractCastExpr implements CastExpr, ConvertToBaf
39 {
40     ValueBox opBox;
41     Type type;
42
43     AbstractCastExpr(Value op, Type type)
44     {
45         this(Jimple.v().newImmediateBox(op), type);
46     }
47
48     public abstract Object JavaDoc clone();
49
50     protected AbstractCastExpr(ValueBox opBox, Type type)
51     {
52         this.opBox = opBox; this.type = type;
53     }
54
55     public boolean equivTo(Object JavaDoc o)
56     {
57         if (o instanceof AbstractCastExpr)
58         {
59             AbstractCastExpr ace = (AbstractCastExpr)o;
60             return opBox.getValue().equivTo(ace.opBox.getValue()) &&
61                 type.equals(ace.type);
62         }
63         return false;
64     }
65
66     /** Returns a hash code for this object, consistent with structural equality. */
67     public int equivHashCode()
68     {
69         return opBox.getValue().equivHashCode() * 101 + type.hashCode() + 17;
70     }
71
72     public String JavaDoc toString()
73     {
74         return "(" + type.toString() + ") " + opBox.getValue().toString();
75     }
76     
77     public void toString(UnitPrinter up) {
78         up.literal("(");
79         up.type(type);
80         up.literal(") ");
81         if( PrecedenceTest.needsBrackets( opBox, this ) ) up.literal("(");
82         opBox.toString(up);
83         if( PrecedenceTest.needsBrackets( opBox, this ) ) up.literal(")");
84     }
85
86     public Value getOp()
87     {
88         return opBox.getValue();
89     }
90
91     public void setOp(Value op)
92     {
93         opBox.setValue(op);
94     }
95
96     public ValueBox getOpBox()
97     {
98         return opBox;
99     }
100
101     public List getUseBoxes()
102     {
103         List list = new ArrayList();
104
105         list.addAll(opBox.getValue().getUseBoxes());
106         list.add(opBox);
107
108         return list;
109     }
110
111     public Type getCastType()
112     {
113         return type;
114     }
115
116     public void setCastType(Type castType)
117     {
118         this.type = castType;
119     }
120
121     public Type getType()
122     {
123         return type;
124     }
125
126     public void apply(Switch sw)
127     {
128         ((ExprSwitch) sw).caseCastExpr(this);
129     }
130
131     public void convertToBaf(JimpleToBafContext context, List out)
132     {
133         final Type toType = getCastType();
134         final Type fromType = getOp().getType();
135
136         ((ConvertToBaf)getOp()).convertToBaf(context, out);
137
138     Unit u;
139         if (toType instanceof ArrayType || toType instanceof RefType)
140             u = Baf.v().newInstanceCastInst(toType);
141         else
142             u = Baf.v().newPrimitiveCastInst(fromType, toType);
143
144     out.add(u);
145
146     Unit currentUnit = context.getCurrentUnit();
147
148     Iterator it = currentUnit.getTags().iterator();
149     while(it.hasNext()) {
150         u.addTag((Tag) it.next());
151     }
152     }
153 }
154
Popular Tags