Updates
[repair.git] / Repair / RepairCompiler / MCC / IR / SizeofPredicate.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class SizeofPredicate extends Predicate {
6    
7     SetExpr setexpr;
8     Opcode opcode;
9     IntegerLiteralExpr cardinality;
10
11     public SizeofPredicate(SetExpr setexpr, Opcode opcode, IntegerLiteralExpr cardinality) {
12         if (setexpr == null || opcode == null || cardinality == null) {
13             throw new IllegalArgumentException();
14         } else if (opcode != Opcode.EQ &&
15                    opcode != Opcode.GE &&
16                    opcode != Opcode.LE) {
17             throw new IllegalArgumentException("invalid operator type");
18         }
19
20         this.setexpr = setexpr;
21         this.opcode = opcode;
22         this.cardinality = cardinality;
23     }
24
25     public int[] getRepairs(boolean negated) {
26         if (setexpr instanceof ImageSetExpr) {
27             if (opcode==Opcode.EQ)
28                 return new int[] {AbstractRepair.ADDTORELATION,
29                                       AbstractRepair.REMOVEFROMRELATION};
30             if (((opcode==Opcode.GE)&&!negated)||
31                 ((opcode==Opcode.LE)&&negated))
32                 return new int[]{AbstractRepair.ADDTORELATION};
33             else
34                 return new int[]{AbstractRepair.REMOVEFROMRELATION};
35         } else {
36             if (opcode==Opcode.EQ)
37                 return new int[] {AbstractRepair.ADDTOSET,
38                                       AbstractRepair.REMOVEFROMSET};
39
40             if (((opcode==Opcode.GE)&&!negated)||
41                 ((opcode==Opcode.LE)&&negated))
42                 return new int[] {AbstractRepair.ADDTOSET};
43             else 
44                 return new int[] {AbstractRepair.REMOVEFROMSET};
45         }
46     }
47
48     public Set getRequiredDescriptors() {
49         assert setexpr != null;
50         Set v = setexpr.getRequiredDescriptors();
51         // v.add(cardinality.getRequiredDescriptors()); // will be null
52         return v;
53     }
54      
55     public void generate(CodeWriter writer, VarDescriptor dest) {
56
57         // #TBD#: generate the set which should generate a name (variable) which is the pointer 
58         // to a hash table iterator that we can dereference get something blah blah blah
59
60         VarDescriptor size = VarDescriptor.makeNew("size");
61         setexpr.generate_size(writer, size);
62         
63         writer.outputline("int " + dest.getSafeSymbol() + " = " + size.getSafeSymbol() + opcode.toString() + cardinality.getValue() + ";");                       
64     }
65        
66 }
67     
68
69
70
71
72