KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
30 import soot.jimple.*;
31 import soot.util.*;
32 import java.util.*;
33 import soot.grimp.PrecedenceTest;
34
35 public abstract class AbstractBinopExpr implements Expr
36 {
37     protected ValueBox op1Box;
38     protected ValueBox op2Box;
39
40     public Value getOp1()
41     {
42         return op1Box.getValue();
43     }
44
45     public Value getOp2()
46     {
47         return op2Box.getValue();
48     }
49
50     public ValueBox getOp1Box()
51     {
52         return op1Box;
53     }
54
55     public ValueBox getOp2Box()
56     {
57         return op2Box;
58     }
59
60     public void setOp1(Value op1)
61     {
62         op1Box.setValue(op1);
63     }
64
65     public void setOp2(Value op2)
66     {
67         op2Box.setValue(op2);
68     }
69
70     public List getUseBoxes()
71     {
72         List list = new ArrayList();
73
74         list.addAll(op1Box.getValue().getUseBoxes());
75         list.add(op1Box);
76         list.addAll(op2Box.getValue().getUseBoxes());
77         list.add(op2Box);
78
79         return list;
80     }
81
82     public boolean equivTo(Object JavaDoc o)
83     {
84         if (o instanceof AbstractBinopExpr)
85         {
86             AbstractBinopExpr abe = (AbstractBinopExpr)o;
87             return op1Box.getValue().equivTo(abe.op1Box.getValue()) &&
88                 op2Box.getValue().equivTo(abe.op2Box.getValue()) &&
89                 getSymbol().equals(abe.getSymbol());
90         }
91         return false;
92     }
93
94     /** Returns a hash code for this object, consistent with structural equality. */
95     public int equivHashCode()
96     {
97         return op1Box.getValue().equivHashCode() * 101 + op2Box.getValue().equivHashCode() + 17
98             ^ getSymbol().hashCode();
99     }
100
101     /** Returns the unique symbol for an operator. */
102     abstract protected String JavaDoc getSymbol();
103     abstract public Object JavaDoc clone();
104
105     public String JavaDoc toString()
106     {
107         Value op1 = op1Box.getValue(), op2 = op2Box.getValue();
108         String JavaDoc leftOp = op1.toString(), rightOp = op2.toString();
109
110         return leftOp + getSymbol() + rightOp;
111     }
112
113     public void toString( UnitPrinter up ) {
114         Value val1 = op1Box.getValue();
115         Value val2 = op2Box.getValue();
116
117         if( PrecedenceTest.needsBrackets( op1Box, this ) ) up.literal("(");
118         op1Box.toString(up);
119         if( PrecedenceTest.needsBrackets( op1Box, this ) ) up.literal(")");
120
121         up.literal(getSymbol());
122
123         if( PrecedenceTest.needsBracketsRight( op2Box, this ) ) up.literal("(");
124         op2Box.toString(up);
125         if( PrecedenceTest.needsBracketsRight( op2Box, this ) ) up.literal(")");
126     }
127 }
128
Popular Tags