aecc4e995b0022f5819dc46b77656db0ff58f36f
[repair.git] / Repair / RepairCompiler / MCC / IR / AbstractRepair.java
1 package MCC.IR;
2
3 class AbstractRepair {
4     public final static int ADDTOSET=1;
5     public final static int REMOVEFROMSET=2;
6     public final static int ADDTORELATION=3;
7     public final static int REMOVEFROMRELATION=4;
8     public final static int MODIFYRELATION=5;
9
10     DNFPredicate torepair;
11     int type;
12     Descriptor descriptor;
13     Sources sources;
14
15     public String type() {
16         switch(type) {
17         case ADDTOSET:
18             return "AddToSet";
19         case REMOVEFROMSET:
20             return "RemoveToSet";
21         case ADDTORELATION:
22             return "AddToRelation";
23         case REMOVEFROMRELATION:
24             return "RemoveFromRelation";
25         case MODIFYRELATION:
26             return "ModifyRelation";
27         default:
28             return "Unknown";
29         }
30     }
31
32     public boolean isNewObject(boolean isdomain) {
33         if (getType()==ADDTOSET) {
34             return sources.allocSource((SetDescriptor)descriptor);
35         } else if (getType()==ADDTORELATION) {
36             RelationDescriptor rd=(RelationDescriptor)descriptor;
37             return sources.relallocSource(rd,isdomain);
38         } else throw new Error(type());
39     }
40
41     public SetDescriptor getDomainSet() {
42         if (torepair==null)
43             return null;
44         Predicate predicate=torepair.getPredicate();
45         if (!(predicate.getDescriptor() instanceof RelationDescriptor))
46             return null;
47
48         /* Have relation descriptor now */
49         if (predicate instanceof InclusionPredicate) {
50             InclusionPredicate ip=(InclusionPredicate)predicate;
51             if (ip.inverted())
52                 return ip.expr.getSet();
53             else if (ip.setexpr instanceof ImageSetExpr) {
54                 ImageSetExpr ise=(ImageSetExpr)ip.setexpr;
55                 if (ise.isimageset)
56                     return ise.getImageSetExpr().getSet();
57                 else
58                     return ise.getVar().getSet();
59             }
60         } else if (predicate instanceof ExprPredicate) {
61             ExprPredicate ep=(ExprPredicate)predicate;
62
63             if (ep.inverted()&&ep.getType()==ExprPredicate.SIZE)
64                 return sources.relgetSourceSet((RelationDescriptor)predicate.getDescriptor(),true);
65             else if (ep.inverted()&&ep.getType()==ExprPredicate.COMPARISON)
66                 return ((OpExpr)ep.expr).right.getSet();
67             else if (!ep.inverted()) {
68                 switch(ep.getType()) {
69                 case ExprPredicate.SIZE: 
70                     {
71                         SizeofExpr soe=((SizeofExpr)((OpExpr)ep.expr).left);
72                         ImageSetExpr ise=(ImageSetExpr)soe.setexpr;
73                         if (ise.isimageset)
74                             return ise.getImageSetExpr().getSet();
75                         else
76                             return ise.getVar().getSet();
77                     }
78                 case ExprPredicate.COMPARISON:
79                     {
80                         RelationExpr re=((RelationExpr)((OpExpr)ep.expr).left);
81                         return re.expr.getSet();
82                     }
83                 default:
84                     throw new Error("");
85                 }
86             }
87         } else throw new Error("Unrecognized predicate");
88         return null;
89     }
90
91     public SetDescriptor getRangeSet() {
92         if (torepair==null)
93             return null;
94         Predicate predicate=torepair.getPredicate();
95         if (!(predicate.getDescriptor() instanceof RelationDescriptor))
96             return null;
97
98         /* Have relation descriptor now */
99         if (predicate instanceof InclusionPredicate) {
100             InclusionPredicate ip=(InclusionPredicate)predicate;
101             if (!ip.inverted())
102                 return ip.expr.getSet();
103             else if (ip.setexpr instanceof ImageSetExpr) {
104                 ImageSetExpr ise=(ImageSetExpr)ip.setexpr;
105                 if (ise.isimageset)
106                     return ise.getImageSetExpr().getSet();
107                 else
108                     return ise.getVar().getSet();
109             }
110         } else if (predicate instanceof ExprPredicate) {
111             ExprPredicate ep=(ExprPredicate)predicate;
112
113             if (!ep.inverted()&&ep.getType()==ExprPredicate.SIZE)
114                 return sources.relgetSourceSet((RelationDescriptor)predicate.getDescriptor(),false);
115             else if (!ep.inverted()&&ep.getType()==ExprPredicate.COMPARISON)
116                 return ((OpExpr)ep.expr).right.getSet();
117             else if (ep.inverted()) {
118                 switch(ep.getType()) {
119                 case ExprPredicate.SIZE: 
120                     {
121                         SizeofExpr soe=((SizeofExpr)((OpExpr)ep.expr).left);
122                         ImageSetExpr ise=(ImageSetExpr)soe.setexpr;
123                         if (ise.isimageset)
124                             return ise.getImageSetExpr().getSet();
125                         else
126                             return ise.getVar().getSet();
127                     }
128                 case ExprPredicate.COMPARISON:
129                     {
130                         RelationExpr re=((RelationExpr)((OpExpr)ep.expr).left);
131                         return re.expr.getSet();
132                     }
133                 default:
134                     throw new Error("");
135                 }
136             }
137         } else throw new Error("Unrecognized predicate");
138         return null;
139     }
140
141     public int getType() {
142         return type;
143     }
144
145     public DNFPredicate getPredicate() {
146         return torepair;
147     }
148
149     public Descriptor getDescriptor() {
150         return descriptor;
151     }
152
153     public AbstractRepair(DNFPredicate dp,int typ, Descriptor d, Sources s) {
154         torepair=dp;
155         type=typ;
156         descriptor=d;
157         sources=s;
158     }
159 }