1) Added useDescriptor method to Expr's.
[repair.git] / Repair / RepairCompiler / MCC / IR / ElementOfExpr.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class ElementOfExpr extends Expr {
6
7     Expr element;
8     SetDescriptor set;
9
10     public Set freeVars() {
11         return element.freeVars();
12     }
13
14     public ElementOfExpr(Expr element, SetDescriptor set) {
15         if (element == null || set == null) {
16             throw new NullPointerException();
17         }
18         this.element = element;
19         this.set = set;
20     }
21     public boolean usesDescriptor(Descriptor d) {
22         if (d==set)
23             return true;
24         return element.usesDescriptor(d);
25     }
26     public Set useDescriptor(Descriptor d) {
27         HashSet newset=new HashSet();
28         if (d==set)
29             newset.add(this);
30         newset.addAll(element.useDescriptor(d));
31         return newset;
32     }
33
34     public String name() {
35         return element.name()+" in "+set.toString();
36     }
37
38     public boolean equals(Map remap, Expr e) {
39         if (e==null||!(e instanceof ElementOfExpr))
40             return false;
41         ElementOfExpr eoe=(ElementOfExpr)e;
42         if (eoe.set!=set)
43             return false;
44         if (!element.equals(remap,eoe.element))
45             return false;
46         return true;
47     }
48
49     public Set getRequiredDescriptors() {
50         Set v = element.getRequiredDescriptors();
51         v.add(set);
52         return v;
53     }
54
55     public void generate(CodeWriter writer, VarDescriptor dest) {
56         VarDescriptor ed = VarDescriptor.makeNew("element");
57         element.generate(writer, ed);
58         writer.outputline("int " + dest.getSafeSymbol() + " = " + 
59                           set.getSafeSymbol() + "_hash->contains(" + ed.getSafeSymbol() + ");");
60     }
61     
62     public void prettyPrint(PrettyPrinter pp) {
63         element.prettyPrint(pp);
64         pp.output(" in? " + set.getSafeSymbol());
65     }
66
67     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
68         TypeDescriptor td = element.typecheck(sa);
69         
70         if (td == null) {
71             return null;
72         }
73
74         TypeDescriptor settype = set.getType();
75
76         if (!td.equals(settype)) {
77             sa.getErrorReporter().report(null, "Type mismatch: attempting to test for types '" + td.getSymbol() + "' in set of type '" + settype.getSymbol() + "'");
78             return null;
79         }
80         
81         this.td = ReservedTypeDescriptor.INT;
82         return this.td;
83     }
84
85 }
86