325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.codemodel.internal;
325N/A
325N/A
325N/A/**
325N/A * JClass for generating expressions containing operators
325N/A */
325N/A
325N/Aabstract public class JOp {
325N/A
325N/A private JOp() {
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Determine whether the top level of an expression involves an
325N/A * operator.
325N/A */
325N/A static boolean hasTopOp(JExpression e) {
325N/A return (e instanceof UnaryOp) || (e instanceof BinaryOp);
325N/A }
325N/A
325N/A /* -- Unary operators -- */
325N/A
325N/A static private class UnaryOp extends JExpressionImpl {
325N/A
325N/A protected String op;
325N/A protected JExpression e;
325N/A protected boolean opFirst = true;
325N/A
325N/A UnaryOp(String op, JExpression e) {
325N/A this.op = op;
325N/A this.e = e;
325N/A }
325N/A
325N/A UnaryOp(JExpression e, String op) {
325N/A this.op = op;
325N/A this.e = e;
325N/A opFirst = false;
325N/A }
325N/A
325N/A public void generate(JFormatter f) {
325N/A if (opFirst)
325N/A f.p('(').p(op).g(e).p(')');
325N/A else
325N/A f.p('(').g(e).p(op).p(')');
325N/A }
325N/A
325N/A }
325N/A
325N/A public static JExpression minus(JExpression e) {
325N/A return new UnaryOp("-", e);
325N/A }
325N/A
325N/A /**
325N/A * Logical not <tt>'!x'</tt>.
325N/A */
325N/A public static JExpression not(JExpression e) {
325N/A if (e == JExpr.TRUE) return JExpr.FALSE;
325N/A if (e == JExpr.FALSE) return JExpr.TRUE;
325N/A return new UnaryOp("!", e);
325N/A }
325N/A
325N/A public static JExpression complement(JExpression e) {
325N/A return new UnaryOp("~", e);
325N/A }
325N/A
325N/A static private class TightUnaryOp extends UnaryOp {
325N/A
325N/A TightUnaryOp(JExpression e, String op) {
325N/A super(e, op);
325N/A }
325N/A
325N/A public void generate(JFormatter f) {
325N/A if (opFirst)
325N/A f.p(op).g(e);
325N/A else
325N/A f.g(e).p(op);
325N/A }
325N/A
325N/A }
325N/A
325N/A public static JExpression incr(JExpression e) {
325N/A return new TightUnaryOp(e, "++");
325N/A }
325N/A
325N/A public static JExpression decr(JExpression e) {
325N/A return new TightUnaryOp(e, "--");
325N/A }
325N/A
325N/A
325N/A /* -- Binary operators -- */
325N/A
325N/A static private class BinaryOp extends JExpressionImpl {
325N/A
325N/A String op;
325N/A JExpression left;
325N/A JGenerable right;
325N/A
325N/A BinaryOp(String op, JExpression left, JGenerable right) {
325N/A this.left = left;
325N/A this.op = op;
325N/A this.right = right;
325N/A }
325N/A
325N/A public void generate(JFormatter f) {
325N/A f.p('(').g(left).p(op).g(right).p(')');
325N/A }
325N/A
325N/A }
325N/A
325N/A public static JExpression plus(JExpression left, JExpression right) {
325N/A return new BinaryOp("+", left, right);
325N/A }
325N/A
325N/A public static JExpression minus(JExpression left, JExpression right) {
325N/A return new BinaryOp("-", left, right);
325N/A }
325N/A
325N/A public static JExpression mul(JExpression left, JExpression right) {
325N/A return new BinaryOp("*", left, right);
325N/A }
325N/A
325N/A public static JExpression div(JExpression left, JExpression right) {
325N/A return new BinaryOp("/", left, right);
325N/A }
325N/A
325N/A public static JExpression mod(JExpression left, JExpression right) {
325N/A return new BinaryOp("%", left, right);
325N/A }
325N/A
325N/A public static JExpression shl(JExpression left, JExpression right) {
325N/A return new BinaryOp("<<", left, right);
325N/A }
325N/A
325N/A public static JExpression shr(JExpression left, JExpression right) {
325N/A return new BinaryOp(">>", left, right);
325N/A }
325N/A
325N/A public static JExpression shrz(JExpression left, JExpression right) {
325N/A return new BinaryOp(">>>", left, right);
325N/A }
325N/A
325N/A public static JExpression band(JExpression left, JExpression right) {
325N/A return new BinaryOp("&", left, right);
325N/A }
325N/A
325N/A public static JExpression bor(JExpression left, JExpression right) {
325N/A return new BinaryOp("|", left, right);
325N/A }
325N/A
325N/A public static JExpression cand(JExpression left, JExpression right) {
325N/A if (left == JExpr.TRUE) return right;
325N/A if (right == JExpr.TRUE) return left;
325N/A if (left == JExpr.FALSE) return left; // JExpr.FALSE
325N/A if (right == JExpr.FALSE) return right; // JExpr.FALSE
325N/A return new BinaryOp("&&", left, right);
325N/A }
325N/A
325N/A public static JExpression cor(JExpression left, JExpression right) {
325N/A if (left == JExpr.TRUE) return left; // JExpr.TRUE
325N/A if (right == JExpr.TRUE) return right; // JExpr.FALSE
325N/A if (left == JExpr.FALSE) return right;
325N/A if (right == JExpr.FALSE) return left;
325N/A return new BinaryOp("||", left, right);
325N/A }
325N/A
325N/A public static JExpression xor(JExpression left, JExpression right) {
325N/A return new BinaryOp("^", left, right);
325N/A }
325N/A
325N/A public static JExpression lt(JExpression left, JExpression right) {
325N/A return new BinaryOp("<", left, right);
325N/A }
325N/A
325N/A public static JExpression lte(JExpression left, JExpression right) {
325N/A return new BinaryOp("<=", left, right);
325N/A }
325N/A
325N/A public static JExpression gt(JExpression left, JExpression right) {
325N/A return new BinaryOp(">", left, right);
325N/A }
325N/A
325N/A public static JExpression gte(JExpression left, JExpression right) {
325N/A return new BinaryOp(">=", left, right);
325N/A }
325N/A
325N/A public static JExpression eq(JExpression left, JExpression right) {
325N/A return new BinaryOp("==", left, right);
325N/A }
325N/A
325N/A public static JExpression ne(JExpression left, JExpression right) {
325N/A return new BinaryOp("!=", left, right);
325N/A }
325N/A
325N/A public static JExpression _instanceof(JExpression left, JType right) {
325N/A return new BinaryOp("instanceof", left, right);
325N/A }
325N/A
325N/A /* -- Ternary operators -- */
325N/A
325N/A static private class TernaryOp extends JExpressionImpl {
325N/A
325N/A String op1;
325N/A String op2;
325N/A JExpression e1;
325N/A JExpression e2;
325N/A JExpression e3;
325N/A
325N/A TernaryOp(String op1, String op2,
325N/A JExpression e1, JExpression e2, JExpression e3) {
325N/A this.e1 = e1;
325N/A this.op1 = op1;
325N/A this.e2 = e2;
325N/A this.op2 = op2;
325N/A this.e3 = e3;
325N/A }
325N/A
325N/A public void generate(JFormatter f) {
325N/A f.p('(').g(e1).p(op1).g(e2).p(op2).g(e3).p(')');
325N/A }
325N/A
325N/A }
325N/A
325N/A public static JExpression cond(JExpression cond,
325N/A JExpression ifTrue, JExpression ifFalse) {
325N/A return new TernaryOp("?", ":", cond, ifTrue, ifFalse);
325N/A }
325N/A
325N/A}