KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > javaToJimple > jj > ast > JjBinary_c


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Jennifer Lhotak
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 package soot.javaToJimple.jj.ast;
21
22 import polyglot.ast.*;
23 import polyglot.types.*;
24 import polyglot.visit.*;
25 import polyglot.ext.jl.ast.*;
26 import polyglot.util.*;
27
28 public class JjBinary_c extends Binary_c {
29
30     public JjBinary_c(Position pos, Expr left, Binary.Operator op, Expr right){
31         super(pos, left, op, right);
32     }
33
34     public Type childExpectedType(Expr child, AscriptionVisitor av){
35         Expr other;
36
37         //System.out.println("child: "+child+" op: "+op);
38
if (child == left) {
39             other = right;
40         }
41         else if (child == right) {
42             other = left;
43         }
44         else {
45             return child.type();
46         }
47
48         TypeSystem ts = av.typeSystem();
49
50     if (op == EQ || op == NE) {
51             // Coercion to compatible types.
52
if (other.type().isReference() || other.type().isNull()) {
53                 return ts.Object();
54             }
55
56             if (other.type().isBoolean()) {
57                 return ts.Boolean();
58             }
59
60             if (other.type().isNumeric()) {
61                 if (other.type().isDouble() || child.type().isDouble()) {
62                     return ts.Double();
63                 }
64                 else if (other.type().isFloat() || child.type().isFloat()) {
65                     return ts.Float();
66                 }
67                 else if (other.type().isLong() || child.type().isLong()) {
68                     return ts.Long();
69                 }
70                 else {
71                     return ts.Int();
72                 }
73             }
74         }
75
76         if (op == ADD && ts.equals(type, ts.String())) {
77             // Implicit coercion to String.
78
return ts.String();
79         }
80
81         if (op == GT || op == LT || op == GE || op == LE) {
82             if (other.type().isBoolean()) {
83                 return ts.Boolean();
84             }
85             if (other.type().isNumeric()) {
86                 if (other.type().isDouble() || child.type().isDouble()) {
87                     return ts.Double();
88                 }
89                 else if (other.type().isFloat() || child.type().isFloat()) {
90                     return ts.Float();
91                 }
92                 else if (other.type().isLong() || child.type().isLong()) {
93                     return ts.Long();
94                 }
95                 else {
96                     return ts.Int();
97                 }
98             }
99         }
100
101         if (op == COND_OR || op == COND_AND) {
102             return ts.Boolean();
103         }
104
105         if (op == BIT_AND || op == BIT_OR || op == BIT_XOR) {
106             if (other.type().isBoolean()) {
107                 return ts.Boolean();
108             }
109             if (other.type().isNumeric()) {
110                 if (other.type().isLong() || child.type().isLong()) {
111                     return ts.Long();
112                 }
113                 else {
114                     return ts.Int();
115                 }
116             }
117         }
118
119         if (op == ADD || op == SUB || op == MUL || op == DIV || op == MOD) {
120             //System.out.println("other: "+other+" type: "+other.type());
121
//System.out.println("child: "+child+" child: "+child.type());
122

123             if (other.type().isNumeric()) {
124                 if (other.type().isDouble() || child.type().isDouble()) {
125                     return ts.Double();
126                 }
127                 else if (other.type().isFloat() || child.type().isFloat()) {
128                     return ts.Float();
129                 }
130                 else if (other.type().isLong() || child.type().isLong()) {
131                     return ts.Long();
132                 }
133                 else {
134                     return ts.Int();
135                 }
136             }
137         }
138         
139         if (op == SHL || op == SHR || op == USHR) {
140             if (child == right || !child.type().isLong()) {
141                 return ts.Int();
142             } else {
143                 return child.type();
144             }
145         }
146
147         return child.type();
148
149
150     }
151
152     /*public Node foldConstants(ConstantFolder cf) {
153         if (left instanceof Binary || left instanceof Field){
154             left = left.del.foldConstants(cf);
155         }
156         if (right instanceof Binary || right instanceof Field){
157             right = right.del.foldConstants(cf);
158         }
159         if (left instanceof StringLit && right instanceof StringLit) {
160             String l = ((StringLit) left).value();
161             String r = ((StringLit) right).value();
162             if (op == ADD) return nf.StringLit(position(), l + r);
163         }
164         else if (left instanceof
165     }*/

166 }
167
168
Popular Tags