Added:
[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 String name() {
27         return element.name()+" in "+set.toString();
28     }
29
30     public boolean equals(Map remap, Expr e) {
31         if (e==null||!(e instanceof ElementOfExpr))
32             return false;
33         ElementOfExpr eoe=(ElementOfExpr)e;
34         if (eoe.set!=set)
35             return false;
36         if (!element.equals(remap,eoe.element))
37             return false;
38         return true;
39     }
40
41     public Set getRequiredDescriptors() {
42         Set v = element.getRequiredDescriptors();
43         v.add(set);
44         return v;
45     }
46
47     public void generate(CodeWriter writer, VarDescriptor dest) {
48         VarDescriptor ed = VarDescriptor.makeNew("element");
49         element.generate(writer, ed);
50         writer.outputline("int " + dest.getSafeSymbol() + " = " + 
51                           set.getSafeSymbol() + "_hash->contains(" + ed.getSafeSymbol() + ");");
52     }
53     
54     public void prettyPrint(PrettyPrinter pp) {
55         element.prettyPrint(pp);
56         pp.output(" in? " + set.getSafeSymbol());
57     }
58
59     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
60         TypeDescriptor td = element.typecheck(sa);
61         
62         if (td == null) {
63             return null;
64         }
65
66         TypeDescriptor settype = set.getType();
67
68         if (!td.equals(settype)) {
69             sa.getErrorReporter().report(null, "Type mismatch: attempting to test for types '" + td.getSymbol() + "' in set of type '" + settype.getSymbol() + "'");
70             return null;
71         }
72         
73         this.td = ReservedTypeDescriptor.INT;
74         return this.td;
75     }
76
77 }
78