Fixed lot of random bugs. Added code generate strings for expr's.
[repair.git] / Repair / RepairCompiler / MCC / IR / SetInclusion.java
1 package MCC.IR;
2
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 SetInclusion(Expr elementexpr, SetDescriptor set) {
17         this.elementexpr = elementexpr;
18         this.set = set;
19     }
20
21     public boolean usesDescriptor(Descriptor d) {
22         if (d==set)
23             return true;
24         else
25             return elementexpr.usesDescriptor(d);
26     }
27
28     public SetDescriptor getSet() {
29         return set;
30     }
31
32     public Set getTargetDescriptors() {
33         HashSet v = new HashSet();
34         v.add(set);
35         return v;
36     }
37
38     public Set getRequiredDescriptors() {
39         return elementexpr.getRequiredDescriptors();
40     }
41
42     public void generate(CodeWriter writer) {
43         VarDescriptor vd = VarDescriptor.makeNew("element");
44         elementexpr.generate(writer, vd);
45
46         // allows access to the value of this set addition later
47         generatedresult = vd.getSafeSymbol();
48
49         String addeditem = (VarDescriptor.makeNew("addeditem")).getSafeSymbol();
50         generatedaddeditem = addeditem; // allows access to the result of the set addition later.
51
52         // we set equal to one so that if dostore == false the guard in teh 
53         // metainclusion generation for the subrules and sub quantifiers will go on        
54         writer.outputline("int " + addeditem + " = 1;");
55
56         if (dostore) {
57         
58             writer.outputline(addeditem + " = " + set.getSafeSymbol() + "_hash->add((int)" + vd.getSafeSymbol() 
59                               +  ", (int)" + vd.getSafeSymbol() + ");");
60
61             if (SetInclusion.worklist) {
62                 writer.outputline("if (" + addeditem + ")");
63                 writer.startblock(); {                
64                     WorkList.generate_dispatch(writer, set, vd.getSafeSymbol());
65                 }
66                 writer.endblock();
67             }
68
69         }
70         
71     }
72
73     public boolean typecheck(SemanticAnalyzer sa) {
74         TypeDescriptor td = elementexpr.typecheck(sa);
75         
76         if (td == null) {
77             return false;
78         }
79
80         TypeDescriptor settype = set.getType();
81
82         if (!td.equals(settype)) {
83             sa.getErrorReporter().report(null, "Type mismatch: attempting to test for types '" + td.getSymbol() + "' in set of type '" + settype.getSymbol() + "'");
84             return false;
85         }
86         
87         return true;
88     }
89
90 }