f80c63116045c619663688ad1dcf103f1a94e319
[repair.git] / Repair / RepairCompiler / MCC / IR / SetInclusion.java
1 package MCC.IR;
2 import MCC.Compiler;
3 import java.util.*;
4
5 public class SetInclusion extends Inclusion {
6     
7     Expr elementexpr;
8     SetDescriptor set;
9
10     public String generatedresult = null;
11     public String generatedaddeditem = null;
12
13     static boolean worklist = false;
14     public boolean dostore = true;
15
16     public String toString() {
17         String str="";
18         str+=elementexpr.name()+" in "+set;
19         return str;
20     }
21
22     public SetInclusion(Expr elementexpr, SetDescriptor set) {
23         this.elementexpr = elementexpr;
24         this.set = set;
25     }
26
27     public boolean usesDescriptor(Descriptor d) {
28         if (d==set)
29             return true;
30         else
31             return elementexpr.usesDescriptor(d);
32     }
33
34     public Expr getExpr() {
35         return elementexpr;
36     }
37
38     public SetDescriptor getSet() {
39         return set;
40     }
41
42     public Set getTargetDescriptors() {
43         HashSet v = new HashSet();
44         v.add(set);
45         return v;
46     }
47
48     public Set getRequiredDescriptors() {
49         return elementexpr.getRequiredDescriptors();
50     }
51
52     public void generate(CodeWriter writer) {
53         VarDescriptor vd = VarDescriptor.makeNew("element");
54         elementexpr.generate(writer, vd);
55
56         // allows access to the value of this set addition later
57         generatedresult = vd.getSafeSymbol();
58
59         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
60         generatedaddeditem = addeditem; // allows access to the result of the set addition later.
61
62         // we set equal to one so that if dostore == false the guard in teh 
63         // metainclusion generation for the subrules and sub quantifiers will go on        
64         writer.outputline("int " + addeditem + " = 1;");
65
66         if (dostore) {
67             writer.outputline(addeditem + " = " + set.getSafeSymbol() + "_hash->add((int)" + vd.getSafeSymbol() 
68                               +  ", (int)" + vd.getSafeSymbol() + ");");
69             
70             if (SetInclusion.worklist) {
71                 writer.outputline("if (" + addeditem + ")");
72                 writer.startblock(); {                
73                     WorkList.generate_dispatch(writer, set, vd.getSafeSymbol());
74                 }
75                 writer.endblock();
76             }
77             if (Compiler.REPAIR) {
78                 writer.outputline("if (" + addeditem + ")");
79                 writer.startblock(); {                
80                     Repair.generate_dispatch(writer, set, vd.getSafeSymbol());
81                 }
82                 writer.endblock();
83             }
84             
85         }
86         
87     }
88
89     public boolean typecheck(SemanticAnalyzer sa) {
90         TypeDescriptor td = elementexpr.typecheck(sa);
91         
92         if (td == null) {
93             return false;
94         }
95
96         TypeDescriptor settype = set.getType();
97
98         if (!td.equals(settype)) {
99             sa.getErrorReporter().report(null, "Type mismatch: attempting to test for types '" + td.getSymbol() + "' in set of type '" + settype.getSymbol() + "'");
100             return false;
101         }
102         
103         return true;
104     }
105
106 }