001: /* CompareUnaryOperator Copyright (C) 1998-2002 Jochen Hoenicke.
002: *
003: * This program is free software; you can redistribute it and/or modify
004: * it under the terms of the GNU Lesser General Public License as published by
005: * the Free Software Foundation; either version 2, or (at your option)
006: * any later version.
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program; see the file COPYING.LESSER. If not, write to
015: * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
016: *
017: * $Id: CompareUnaryOperator.java,v 2.16.2.1 2002/05/28 17:34:06 hoenicke Exp $
018: */
019:
020: package jode.expr;
021:
022: import jode.type.Type;
023: import jode.decompiler.TabbedPrintWriter;
024:
025: public class CompareUnaryOperator extends Operator {
026: boolean objectType;
027: Type compareType;
028:
029: public CompareUnaryOperator(Type type, int op) {
030: super (Type.tBoolean, op);
031: compareType = type;
032: objectType = (type.isOfType(Type.tUObject));
033: initOperands(1);
034: }
035:
036: public int getPriority() {
037: switch (getOperatorIndex()) {
038: case 26:
039: case 27:
040: return 500;
041: case 28:
042: case 29:
043: case 30:
044: case 31:
045: return 550;
046: }
047: throw new RuntimeException("Illegal operator");
048: }
049:
050: public Type getCompareType() {
051: return compareType;
052: }
053:
054: public void updateSubTypes() {
055: subExpressions[0].setType(Type.tSubType(compareType));
056: }
057:
058: public void updateType() {
059: }
060:
061: public Expression simplify() {
062: if (subExpressions[0] instanceof CompareToIntOperator) {
063:
064: CompareToIntOperator cmpOp = (CompareToIntOperator) subExpressions[0];
065:
066: boolean negated = false;
067: int opIndex = getOperatorIndex();
068: if (cmpOp.allowsNaN && getOperatorIndex() > NOTEQUALS_OP) {
069: if (cmpOp.greaterOnNaN == (opIndex == GREATEREQ_OP || opIndex == GREATER_OP)) {
070: negated = true;
071: opIndex ^= 1;
072: }
073: }
074: Expression newOp = new CompareBinaryOperator(
075: cmpOp.compareType, opIndex, cmpOp.allowsNaN)
076: .addOperand(cmpOp.subExpressions[1]).addOperand(
077: cmpOp.subExpressions[0]);
078:
079: if (negated)
080: return newOp.negate().simplify();
081: return newOp.simplify();
082: }
083: if (subExpressions[0].getType().isOfType(Type.tBoolean)) {
084: /* xx == false */
085: if (getOperatorIndex() == EQUALS_OP)
086: return subExpressions[0].negate().simplify();
087: /* xx != false */
088: if (getOperatorIndex() == NOTEQUALS_OP)
089: return subExpressions[0].simplify();
090: }
091: return super .simplify();
092: }
093:
094: public Expression negate() {
095: if ((getType() != Type.tFloat && getType() != Type.tDouble)
096: || getOperatorIndex() <= NOTEQUALS_OP) {
097: setOperatorIndex(getOperatorIndex() ^ 1);
098: return this ;
099: }
100: return super .negate();
101: }
102:
103: public boolean opEquals(Operator o) {
104: return (o instanceof CompareUnaryOperator)
105: && o.getOperatorIndex() == getOperatorIndex();
106: }
107:
108: public void dumpExpression(TabbedPrintWriter writer)
109: throws java.io.IOException {
110: subExpressions[0].dumpExpression(writer, getPriority() + 1);
111: writer.breakOp();
112: writer.print(getOperatorString());
113: writer.print(objectType ? "null" : "0");
114: }
115: }
|