KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > expr > CompareBinaryOperator


1 /* CompareBinaryOperator 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: CompareBinaryOperator.java,v 4.11.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 CompareBinaryOperator extends Operator {
25     boolean allowsNaN = false;
26     Type compareType;
27
28     public CompareBinaryOperator(Type type, int op) {
29         super(Type.tBoolean, op);
30     compareType = type;
31     initOperands(2);
32     }
33
34     public CompareBinaryOperator(Type type, int op, boolean allowsNaN) {
35         super(Type.tBoolean, op);
36     compareType = type;
37     this.allowsNaN = allowsNaN;
38     initOperands(2);
39     }
40
41     public int getPriority() {
42         switch (getOperatorIndex()) {
43         case 26:
44         case 27:
45             return 500;
46         case 28:
47         case 29:
48         case 30:
49         case 31:
50             return 550;
51         }
52         throw new RuntimeException JavaDoc("Illegal operator");
53     }
54
55     public Type getCompareType() {
56     return compareType;
57     }
58
59     public void updateSubTypes() {
60     subExpressions[0].setType(Type.tSubType(compareType));
61     subExpressions[1].setType(Type.tSubType(compareType));
62     }
63
64     public void updateType() {
65     Type leftType = Type.tSuperType(subExpressions[0].getType());
66     Type rightType = Type.tSuperType(subExpressions[1].getType());
67     compareType = compareType
68         .intersection(leftType).intersection(rightType);
69     subExpressions[0].setType(Type.tSubType(rightType));
70     subExpressions[1].setType(Type.tSubType(leftType));
71     /* propagate hints? XXX */
72     }
73
74     public Expression negate() {
75     if (!allowsNaN || getOperatorIndex() <= NOTEQUALS_OP) {
76             setOperatorIndex(getOperatorIndex() ^ 1);
77             return this;
78         }
79         return super.negate();
80     }
81
82     public boolean opEquals(Operator o) {
83     return (o instanceof CompareBinaryOperator)
84         && o.operatorIndex == operatorIndex;
85     }
86
87     public void dumpExpression(TabbedPrintWriter writer)
88     throws java.io.IOException JavaDoc {
89     subExpressions[0].dumpExpression(writer, getPriority()+1);
90     writer.breakOp();
91     writer.print(getOperatorString());
92     subExpressions[1].dumpExpression(writer, getPriority()+1);
93     }
94 }
95
Popular Tags