correct
[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     public 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
65
66         if (dostore) {
67             /*      if (!Compiler.REPAIR) {
68                 writer.outputline("int " + addeditem + " = 1;");
69                 writer.outputline(addeditem + " = SimpleHashadd("+set.getSafeSymbol()+"_hash, (int)" + vd.getSafeSymbol()
70                                   +  ", (int)" + vd.getSafeSymbol() + ");");
71                                   } else {*/
72                 Repair.generate_dispatch(writer, set, vd.getSafeSymbol());
73                 //          }
74
75             if (SetInclusion.worklist) {
76                 writer.outputline("if (" + addeditem + ")");
77                 writer.startblock(); {
78                     WorkList.generate_dispatch(writer, set, vd.getSafeSymbol());
79                 }
80                 writer.endblock();
81             }
82         }
83     }
84
85     public boolean typecheck(SemanticAnalyzer sa) {
86         TypeDescriptor td = elementexpr.typecheck(sa);
87
88         if (td == null) {
89             sa.getErrorReporter().report(null, "No type found for:"+elementexpr);
90             return false;
91         }
92
93         TypeDescriptor settype = set.getType();
94
95         if (!td.equals(settype)) {
96             sa.getErrorReporter().report(null, "Type mismatch: attempting to test for types '" + td.getSymbol() + "' in set of type '" + settype.getSymbol() + "'");
97             return false;
98         }
99
100         return true;
101     }
102
103 }