IR
[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 ElementOfExpr(Expr element, SetDescriptor set) {
11         if (element == null || set == null) {
12             throw new NullPointerException();
13         }
14
15         this.element = element;
16         this.set = set;
17     }
18
19     public Set getRequiredDescriptors() {
20         Set v = element.getRequiredDescriptors();
21         v.add(set);
22         return v;
23     }
24
25     public void generate(CodeWriter writer, VarDescriptor dest) {
26         VarDescriptor ed = VarDescriptor.makeNew("element");
27         element.generate(writer, ed);
28         writer.outputline("int " + dest.getSafeSymbol() + " = " + 
29                           set.getSafeSymbol() + "_hash->contains(" + ed.getSafeSymbol() + ");");
30     }
31     
32     public void prettyPrint(PrettyPrinter pp) {
33         element.prettyPrint(pp);
34         pp.output(" in? " + set.getSafeSymbol());
35     }
36
37     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
38         TypeDescriptor td = element.typecheck(sa);
39         
40         if (td == null) {
41             return null;
42         }
43
44         TypeDescriptor settype = set.getType();
45
46         if (!td.equals(settype)) {
47             sa.getErrorReporter().report(null, "Type mismatch: attempting to test for types '" + td.getSymbol() + "' in set of type '" + settype.getSymbol() + "'");
48             return null;
49         }
50         
51         this.td = ReservedTypeDescriptor.INT;
52         return this.td;
53     }
54
55 }
56