8
|
1 |
/*
|
|
2 |
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.codemodel.internal;
|
|
27 |
|
|
28 |
|
|
29 |
/**
|
|
30 |
* JClass for generating expressions containing operators
|
|
31 |
*/
|
|
32 |
|
|
33 |
abstract public class JOp {
|
|
34 |
|
|
35 |
private JOp() {
|
|
36 |
}
|
|
37 |
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Determine whether the top level of an expression involves an
|
|
41 |
* operator.
|
|
42 |
*/
|
|
43 |
static boolean hasTopOp(JExpression e) {
|
|
44 |
return (e instanceof UnaryOp) || (e instanceof BinaryOp);
|
|
45 |
}
|
|
46 |
|
|
47 |
/* -- Unary operators -- */
|
|
48 |
|
|
49 |
static private class UnaryOp extends JExpressionImpl {
|
|
50 |
|
|
51 |
protected String op;
|
|
52 |
protected JExpression e;
|
|
53 |
protected boolean opFirst = true;
|
|
54 |
|
|
55 |
UnaryOp(String op, JExpression e) {
|
|
56 |
this.op = op;
|
|
57 |
this.e = e;
|
|
58 |
}
|
|
59 |
|
|
60 |
UnaryOp(JExpression e, String op) {
|
|
61 |
this.op = op;
|
|
62 |
this.e = e;
|
|
63 |
opFirst = false;
|
|
64 |
}
|
|
65 |
|
|
66 |
public void generate(JFormatter f) {
|
|
67 |
if (opFirst)
|
|
68 |
f.p('(').p(op).g(e).p(')');
|
|
69 |
else
|
|
70 |
f.p('(').g(e).p(op).p(')');
|
|
71 |
}
|
|
72 |
|
|
73 |
}
|
|
74 |
|
|
75 |
public static JExpression minus(JExpression e) {
|
|
76 |
return new UnaryOp("-", e);
|
|
77 |
}
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Logical not <tt>'!x'</tt>.
|
|
81 |
*/
|
|
82 |
public static JExpression not(JExpression e) {
|
|
83 |
if (e == JExpr.TRUE) return JExpr.FALSE;
|
|
84 |
if (e == JExpr.FALSE) return JExpr.TRUE;
|
|
85 |
return new UnaryOp("!", e);
|
|
86 |
}
|
|
87 |
|
|
88 |
public static JExpression complement(JExpression e) {
|
|
89 |
return new UnaryOp("~", e);
|
|
90 |
}
|
|
91 |
|
|
92 |
static private class TightUnaryOp extends UnaryOp {
|
|
93 |
|
|
94 |
TightUnaryOp(JExpression e, String op) {
|
|
95 |
super(e, op);
|
|
96 |
}
|
|
97 |
|
|
98 |
public void generate(JFormatter f) {
|
|
99 |
if (opFirst)
|
|
100 |
f.p(op).g(e);
|
|
101 |
else
|
|
102 |
f.g(e).p(op);
|
|
103 |
}
|
|
104 |
|
|
105 |
}
|
|
106 |
|
|
107 |
public static JExpression incr(JExpression e) {
|
|
108 |
return new TightUnaryOp(e, "++");
|
|
109 |
}
|
|
110 |
|
|
111 |
public static JExpression decr(JExpression e) {
|
|
112 |
return new TightUnaryOp(e, "--");
|
|
113 |
}
|
|
114 |
|
|
115 |
|
|
116 |
/* -- Binary operators -- */
|
|
117 |
|
|
118 |
static private class BinaryOp extends JExpressionImpl {
|
|
119 |
|
|
120 |
String op;
|
|
121 |
JExpression left;
|
|
122 |
JGenerable right;
|
|
123 |
|
|
124 |
BinaryOp(String op, JExpression left, JGenerable right) {
|
|
125 |
this.left = left;
|
|
126 |
this.op = op;
|
|
127 |
this.right = right;
|
|
128 |
}
|
|
129 |
|
|
130 |
public void generate(JFormatter f) {
|
|
131 |
f.p('(').g(left).p(op).g(right).p(')');
|
|
132 |
}
|
|
133 |
|
|
134 |
}
|
|
135 |
|
|
136 |
public static JExpression plus(JExpression left, JExpression right) {
|
|
137 |
return new BinaryOp("+", left, right);
|
|
138 |
}
|
|
139 |
|
|
140 |
public static JExpression minus(JExpression left, JExpression right) {
|
|
141 |
return new BinaryOp("-", left, right);
|
|
142 |
}
|
|
143 |
|
|
144 |
public static JExpression mul(JExpression left, JExpression right) {
|
|
145 |
return new BinaryOp("*", left, right);
|
|
146 |
}
|
|
147 |
|
|
148 |
public static JExpression div(JExpression left, JExpression right) {
|
|
149 |
return new BinaryOp("/", left, right);
|
|
150 |
}
|
|
151 |
|
|
152 |
public static JExpression mod(JExpression left, JExpression right) {
|
|
153 |
return new BinaryOp("%", left, right);
|
|
154 |
}
|
|
155 |
|
|
156 |
public static JExpression shl(JExpression left, JExpression right) {
|
|
157 |
return new BinaryOp("<<", left, right);
|
|
158 |
}
|
|
159 |
|
|
160 |
public static JExpression shr(JExpression left, JExpression right) {
|
|
161 |
return new BinaryOp(">>", left, right);
|
|
162 |
}
|
|
163 |
|
|
164 |
public static JExpression shrz(JExpression left, JExpression right) {
|
|
165 |
return new BinaryOp(">>>", left, right);
|
|
166 |
}
|
|
167 |
|
|
168 |
public static JExpression band(JExpression left, JExpression right) {
|
|
169 |
return new BinaryOp("&", left, right);
|
|
170 |
}
|
|
171 |
|
|
172 |
public static JExpression bor(JExpression left, JExpression right) {
|
|
173 |
return new BinaryOp("|", left, right);
|
|
174 |
}
|
|
175 |
|
|
176 |
public static JExpression cand(JExpression left, JExpression right) {
|
|
177 |
if (left == JExpr.TRUE) return right;
|
|
178 |
if (right == JExpr.TRUE) return left;
|
|
179 |
if (left == JExpr.FALSE) return left; // JExpr.FALSE
|
|
180 |
if (right == JExpr.FALSE) return right; // JExpr.FALSE
|
|
181 |
return new BinaryOp("&&", left, right);
|
|
182 |
}
|
|
183 |
|
|
184 |
public static JExpression cor(JExpression left, JExpression right) {
|
|
185 |
if (left == JExpr.TRUE) return left; // JExpr.TRUE
|
|
186 |
if (right == JExpr.TRUE) return right; // JExpr.FALSE
|
|
187 |
if (left == JExpr.FALSE) return right;
|
|
188 |
if (right == JExpr.FALSE) return left;
|
|
189 |
return new BinaryOp("||", left, right);
|
|
190 |
}
|
|
191 |
|
|
192 |
public static JExpression xor(JExpression left, JExpression right) {
|
|
193 |
return new BinaryOp("^", left, right);
|
|
194 |
}
|
|
195 |
|
|
196 |
public static JExpression lt(JExpression left, JExpression right) {
|
|
197 |
return new BinaryOp("<", left, right);
|
|
198 |
}
|
|
199 |
|
|
200 |
public static JExpression lte(JExpression left, JExpression right) {
|
|
201 |
return new BinaryOp("<=", left, right);
|
|
202 |
}
|
|
203 |
|
|
204 |
public static JExpression gt(JExpression left, JExpression right) {
|
|
205 |
return new BinaryOp(">", left, right);
|
|
206 |
}
|
|
207 |
|
|
208 |
public static JExpression gte(JExpression left, JExpression right) {
|
|
209 |
return new BinaryOp(">=", left, right);
|
|
210 |
}
|
|
211 |
|
|
212 |
public static JExpression eq(JExpression left, JExpression right) {
|
|
213 |
return new BinaryOp("==", left, right);
|
|
214 |
}
|
|
215 |
|
|
216 |
public static JExpression ne(JExpression left, JExpression right) {
|
|
217 |
return new BinaryOp("!=", left, right);
|
|
218 |
}
|
|
219 |
|
|
220 |
public static JExpression _instanceof(JExpression left, JType right) {
|
|
221 |
return new BinaryOp("instanceof", left, right);
|
|
222 |
}
|
|
223 |
|
|
224 |
/* -- Ternary operators -- */
|
|
225 |
|
|
226 |
static private class TernaryOp extends JExpressionImpl {
|
|
227 |
|
|
228 |
String op1;
|
|
229 |
String op2;
|
|
230 |
JExpression e1;
|
|
231 |
JExpression e2;
|
|
232 |
JExpression e3;
|
|
233 |
|
|
234 |
TernaryOp(String op1, String op2,
|
|
235 |
JExpression e1, JExpression e2, JExpression e3) {
|
|
236 |
this.e1 = e1;
|
|
237 |
this.op1 = op1;
|
|
238 |
this.e2 = e2;
|
|
239 |
this.op2 = op2;
|
|
240 |
this.e3 = e3;
|
|
241 |
}
|
|
242 |
|
|
243 |
public void generate(JFormatter f) {
|
|
244 |
f.p('(').g(e1).p(op1).g(e2).p(op2).g(e3).p(')');
|
|
245 |
}
|
|
246 |
|
|
247 |
}
|
|
248 |
|
|
249 |
public static JExpression cond(JExpression cond,
|
|
250 |
JExpression ifTrue, JExpression ifFalse) {
|
|
251 |
return new TernaryOp("?", ":", cond, ifTrue, ifFalse);
|
|
252 |
}
|
|
253 |
|
|
254 |
}
|