5 public class OpExpr extends Expr {
11 public OpExpr(Opcode opcode, Expr left, Expr right) {
16 assert (right == null && opcode == Opcode.NOT) || (right != null);
19 public String name() {
20 if (opcode==Opcode.NOT)
21 return "!("+left.name()+")";
22 String name=left.name()+opcode.toString();
28 public Opcode getOpcode() {
35 public boolean equals(Map remap, Expr e) {
36 if (e==null||!(e instanceof OpExpr))
39 if (opcode!=oe.opcode)
41 if (!left.equals(remap,oe.left))
43 if (opcode!=Opcode.NOT)
44 if (!right.equals(remap,oe.right))
49 public DNFRule constructDNF() {
50 if (opcode==Opcode.AND) {
51 DNFRule leftd=left.constructDNF();
52 DNFRule rightd=right.constructDNF();
53 return leftd.and(rightd);
54 } else if (opcode==Opcode.OR) {
55 DNFRule leftd=left.constructDNF();
56 DNFRule rightd=right.constructDNF();
57 return leftd.or(rightd);
58 } else if (opcode==Opcode.NOT) {
59 DNFRule leftd=left.constructDNF();
61 } else return new DNFRule(this);
64 public boolean usesDescriptor(Descriptor d) {
65 if (opcode==Opcode.GT||opcode==Opcode.GE||opcode==Opcode.LT||
66 opcode==Opcode.LE||opcode==Opcode.EQ||opcode==Opcode.NE)
67 return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
68 // return right.usesDescriptor(d);
70 return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
74 public int[] getRepairs(boolean negated) {
75 if (left instanceof RelationExpr)
76 return new int[] {AbstractRepair.MODIFYRELATION};
77 if (left instanceof SizeofExpr) {
78 boolean isRelation=((SizeofExpr)left).setexpr instanceof ImageSetExpr;
80 if (opcode==Opcode.EQ)
81 if (((IntegerLiteralExpr)right).getValue()==0)
82 return new int[] {AbstractRepair.REMOVEFROMRELATION};
84 return new int[] {AbstractRepair.ADDTORELATION,
85 AbstractRepair.REMOVEFROMRELATION};
86 if (((opcode==Opcode.GE)&&!negated)||
87 ((opcode==Opcode.LE)&&negated)) {
88 if (((IntegerLiteralExpr)right).getValue()==0)
91 return new int[]{AbstractRepair.ADDTORELATION}; }
93 return new int[]{AbstractRepair.REMOVEFROMRELATION};
95 if (opcode==Opcode.EQ)
96 if (((IntegerLiteralExpr)right).getValue()==0)
97 return new int[] {AbstractRepair.REMOVEFROMSET};
99 return new int[] {AbstractRepair.ADDTOSET,
100 AbstractRepair.REMOVEFROMSET};
102 if (((opcode==Opcode.GE)&&!negated)||
103 ((opcode==Opcode.LE)&&negated)) {
104 if (((IntegerLiteralExpr)right).getValue()==0)
107 return new int[] {AbstractRepair.ADDTOSET}; }
109 return new int[] {AbstractRepair.REMOVEFROMSET};
112 throw new Error("BAD");
115 public Descriptor getDescriptor() {
116 return left.getDescriptor();
119 public boolean inverted() {
120 return left.inverted();
123 public Set getInversedRelations() {
124 Set set = left.getInversedRelations();
126 set.addAll(right.getInversedRelations());
131 public Set getRequiredDescriptors() {
132 Set v = left.getRequiredDescriptors();
135 v.addAll(right.getRequiredDescriptors());
141 public void generate(CodeWriter writer, VarDescriptor dest) {
142 VarDescriptor ld = VarDescriptor.makeNew("leftop");
143 left.generate(writer, ld);
144 VarDescriptor rd = null;
147 rd = VarDescriptor.makeNew("rightop");
148 right.generate(writer, rd);
152 if (opcode != Opcode.NOT) { /* two operands */
154 writer.outputline("int " + dest.getSafeSymbol() + " = " +
155 ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
157 writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
161 public void prettyPrint(PrettyPrinter pp) {
163 if (opcode == Opcode.NOT) {
164 left.prettyPrint(pp);
166 left.prettyPrint(pp);
167 pp.output(" " + opcode.toString() + " ");
168 assert right != null;
169 right.prettyPrint(pp);
174 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
175 TypeDescriptor lt = left.typecheck(sa);
176 TypeDescriptor rt = right == null ? null : right.typecheck(sa);
180 } else if (right != null && rt == null) {
186 // #ATTN#: if we want node.next != literal(0) to represent a null check than we need to allow ptr arithmetic
187 // either that or we use a isvalid clause to check for null
190 if (lt != ReservedTypeDescriptor.INT) {
191 sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
196 if (rt != ReservedTypeDescriptor.INT) {
197 sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
207 this.td = ReservedTypeDescriptor.INT;