001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: package com.sun.codemodel;
022:
023: /**
024: * JClass for generating expressions containing operators
025: */
026:
027: abstract public class JOp {
028:
029: private JOp() {
030: }
031:
032: /**
033: * Determine whether the top level of an expression involves an
034: * operator.
035: */
036: static boolean hasTopOp(JExpression e) {
037: return (e instanceof UnaryOp) || (e instanceof BinaryOp);
038: }
039:
040: /* -- Unary operators -- */
041:
042: static private class UnaryOp extends JExpressionImpl {
043:
044: protected String op;
045: protected JExpression e;
046: protected boolean opFirst = true;
047:
048: UnaryOp(String op, JExpression e) {
049: this .op = op;
050: this .e = e;
051: }
052:
053: UnaryOp(JExpression e, String op) {
054: this .op = op;
055: this .e = e;
056: opFirst = false;
057: }
058:
059: public void generate(JFormatter f) {
060: if (opFirst)
061: f.p('(').p(op).g(e).p(')');
062: else
063: f.p('(').g(e).p(op).p(')');
064: }
065:
066: }
067:
068: public static JExpression minus(JExpression e) {
069: return new UnaryOp("-", e);
070: }
071:
072: /**
073: * Logical not <tt>'!x'</tt>.
074: */
075: public static JExpression not(JExpression e) {
076: if (e == JExpr.TRUE)
077: return JExpr.FALSE;
078: if (e == JExpr.FALSE)
079: return JExpr.TRUE;
080: return new UnaryOp("!", e);
081: }
082:
083: public static JExpression complement(JExpression e) {
084: return new UnaryOp("~", e);
085: }
086:
087: static private class TightUnaryOp extends UnaryOp {
088:
089: TightUnaryOp(JExpression e, String op) {
090: super (e, op);
091: }
092:
093: public void generate(JFormatter f) {
094: if (opFirst)
095: f.p(op).g(e);
096: else
097: f.g(e).p(op);
098: }
099:
100: }
101:
102: public static JExpression incr(JExpression e) {
103: return new TightUnaryOp(e, "++");
104: }
105:
106: public static JExpression decr(JExpression e) {
107: return new TightUnaryOp(e, "--");
108: }
109:
110: /* -- Binary operators -- */
111:
112: static private class BinaryOp extends JExpressionImpl {
113:
114: String op;
115: JExpression left;
116: JGenerable right;
117:
118: BinaryOp(String op, JExpression left, JGenerable right) {
119: this .left = left;
120: this .op = op;
121: this .right = right;
122: }
123:
124: public void generate(JFormatter f) {
125: f.p('(').g(left).p(op).g(right).p(')');
126: }
127:
128: }
129:
130: public static JExpression plus(JExpression left, JExpression right) {
131: return new BinaryOp("+", left, right);
132: }
133:
134: public static JExpression minus(JExpression left, JExpression right) {
135: return new BinaryOp("-", left, right);
136: }
137:
138: public static JExpression mul(JExpression left, JExpression right) {
139: return new BinaryOp("*", left, right);
140: }
141:
142: public static JExpression div(JExpression left, JExpression right) {
143: return new BinaryOp("/", left, right);
144: }
145:
146: public static JExpression mod(JExpression left, JExpression right) {
147: return new BinaryOp("%", left, right);
148: }
149:
150: public static JExpression shl(JExpression left, JExpression right) {
151: return new BinaryOp("<<", left, right);
152: }
153:
154: public static JExpression shr(JExpression left, JExpression right) {
155: return new BinaryOp(">>", left, right);
156: }
157:
158: public static JExpression shrz(JExpression left, JExpression right) {
159: return new BinaryOp(">>>", left, right);
160: }
161:
162: public static JExpression band(JExpression left, JExpression right) {
163: return new BinaryOp("&", left, right);
164: }
165:
166: public static JExpression bor(JExpression left, JExpression right) {
167: return new BinaryOp("|", left, right);
168: }
169:
170: public static JExpression cand(JExpression left, JExpression right) {
171: if (left == JExpr.TRUE)
172: return right;
173: if (right == JExpr.TRUE)
174: return left;
175: if (left == JExpr.FALSE)
176: return left; // JExpr.FALSE
177: if (right == JExpr.FALSE)
178: return right; // JExpr.FALSE
179: return new BinaryOp("&&", left, right);
180: }
181:
182: public static JExpression cor(JExpression left, JExpression right) {
183: if (left == JExpr.TRUE)
184: return left; // JExpr.TRUE
185: if (right == JExpr.TRUE)
186: return right; // JExpr.FALSE
187: if (left == JExpr.FALSE)
188: return right;
189: if (right == JExpr.FALSE)
190: return left;
191: return new BinaryOp("||", left, right);
192: }
193:
194: public static JExpression xor(JExpression left, JExpression right) {
195: return new BinaryOp("^", left, right);
196: }
197:
198: public static JExpression lt(JExpression left, JExpression right) {
199: return new BinaryOp("<", left, right);
200: }
201:
202: public static JExpression lte(JExpression left, JExpression right) {
203: return new BinaryOp("<=", left, right);
204: }
205:
206: public static JExpression gt(JExpression left, JExpression right) {
207: return new BinaryOp(">", left, right);
208: }
209:
210: public static JExpression gte(JExpression left, JExpression right) {
211: return new BinaryOp(">=", left, right);
212: }
213:
214: public static JExpression eq(JExpression left, JExpression right) {
215: return new BinaryOp("==", left, right);
216: }
217:
218: public static JExpression ne(JExpression left, JExpression right) {
219: return new BinaryOp("!=", left, right);
220: }
221:
222: public static JExpression _instanceof (JExpression left, JType right) {
223: return new BinaryOp("instanceof", left, right);
224: }
225:
226: /* -- Ternary operators -- */
227:
228: static private class TernaryOp extends JExpressionImpl {
229:
230: String op1;
231: String op2;
232: JExpression e1;
233: JExpression e2;
234: JExpression e3;
235:
236: TernaryOp(String op1, String op2, JExpression e1,
237: JExpression e2, JExpression e3) {
238: this .e1 = e1;
239: this .op1 = op1;
240: this .e2 = e2;
241: this .op2 = op2;
242: this .e3 = e3;
243: }
244:
245: public void generate(JFormatter f) {
246: f.p('(').g(e1).p(op1).g(e2).p(op2).g(e3).p(')');
247: }
248:
249: }
250:
251: public static JExpression cond(JExpression cond,
252: JExpression ifTrue, JExpression ifFalse) {
253: return new TernaryOp("?", ":", cond, ifTrue, ifFalse);
254: }
255:
256: }
|