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