KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > expr > CompareUnaryOperator


1 /* CompareUnaryOperator Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: CompareUnaryOperator.java,v 2.16.2.1 2002/05/28 17:34:06 hoenicke Exp $
18  */

19
20 package jode.expr;
21 import jode.type.Type;
22 import jode.decompiler.TabbedPrintWriter;
23
24 public class CompareUnaryOperator extends Operator {
25     boolean objectType;
26     Type compareType;
27
28     public CompareUnaryOperator(Type type, int op) {
29         super(Type.tBoolean, op);
30         compareType = type;
31         objectType = (type.isOfType(Type.tUObject));
32     initOperands(1);
33     }
34
35     public int getPriority() {
36         switch (getOperatorIndex()) {
37         case 26:
38         case 27:
39             return 500;
40         case 28:
41         case 29:
42         case 30:
43         case 31:
44             return 550;
45         }
46         throw new RuntimeException JavaDoc("Illegal operator");
47     }
48
49     public Type getCompareType() {
50     return compareType;
51     }
52
53     public void updateSubTypes() {
54     subExpressions[0].setType(Type.tSubType(compareType));
55     }
56
57     public void updateType() {
58     }
59
60     public Expression simplify() {
61         if (subExpressions[0] instanceof CompareToIntOperator) {
62
63         CompareToIntOperator cmpOp
64         = (CompareToIntOperator) subExpressions[0];
65
66         boolean negated = false;
67         int opIndex = getOperatorIndex();
68         if (cmpOp.allowsNaN && getOperatorIndex() > NOTEQUALS_OP) {
69         if (cmpOp.greaterOnNaN ==
70             (opIndex == GREATEREQ_OP || opIndex == GREATER_OP)) {
71             negated = true;
72             opIndex ^= 1;
73         }
74         }
75             Expression newOp = new CompareBinaryOperator
76                 (cmpOp.compareType, opIndex, cmpOp.allowsNaN)
77         .addOperand(cmpOp.subExpressions[1])
78         .addOperand(cmpOp.subExpressions[0]);
79
80         if (negated)
81         return newOp.negate().simplify();
82         return newOp.simplify();
83         }
84         if (subExpressions[0].getType().isOfType(Type.tBoolean)) {
85             /* xx == false */
86             if (getOperatorIndex() == EQUALS_OP)
87                 return subExpressions[0].negate().simplify();
88             /* xx != false */
89             if (getOperatorIndex() == NOTEQUALS_OP)
90                 return subExpressions[0].simplify();
91     }
92     return super.simplify();
93     }
94
95     public Expression negate() {
96     if ((getType() != Type.tFloat && getType() != Type.tDouble)
97         || getOperatorIndex() <= NOTEQUALS_OP) {
98             setOperatorIndex(getOperatorIndex() ^ 1);
99             return this;
100         }
101         return super.negate();
102     }
103
104     public boolean opEquals(Operator o) {
105     return (o instanceof CompareUnaryOperator)
106         && o.getOperatorIndex() == getOperatorIndex();
107     }
108
109     public void dumpExpression(TabbedPrintWriter writer)
110     throws java.io.IOException JavaDoc {
111     subExpressions[0].dumpExpression(writer, getPriority()+1);
112     writer.breakOp();
113     writer.print(getOperatorString());
114     writer.print(objectType?"null":"0");
115     }
116 }
117
Popular Tags