Fixed lot of random bugs. Added code generate strings for expr's.
[repair.git] / Repair / RepairCompiler / MCC / IR / TupleOfExpr.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class TupleOfExpr extends Expr {
6
7     Expr left = null;
8     Expr right = null;
9     RelationDescriptor relation = null;
10
11     public TupleOfExpr(Expr left, Expr right, RelationDescriptor relation) {
12         if ((left == null) || (right == null) || (relation == null)) {
13             throw new NullPointerException();
14         }
15         
16         this.left = left;
17         this.right = right;
18         this.relation = relation;
19     }
20
21     public String name() {
22         return "<"+left.name()+","+right.name()+"> in "+relation.toString();
23     }
24
25     public boolean usesDescriptor(Descriptor d) {
26         if (d==relation)
27             return true;
28         else
29             return left.usesDescriptor(d)||right.usesDescriptor(d);
30     }
31
32     public boolean equals(Map remap, Expr e) {
33         if (e==null||!(e instanceof TupleOfExpr))
34             return false;
35         TupleOfExpr toe=(TupleOfExpr)e;
36         if (!left.equals(remap,toe.left))
37             return false;
38         if (!right.equals(remap,toe.right))
39             return false;
40         if (relation!=toe.relation)
41             return false;
42         return true;
43     }
44
45     public Set getRequiredDescriptors() {
46         Set v = left.getRequiredDescriptors();
47         v.addAll(right.getRequiredDescriptors());
48         v.add(relation);
49         return v;
50     }
51
52     public void generate(CodeWriter writer, VarDescriptor dest) {
53         VarDescriptor ld = VarDescriptor.makeNew();
54         left.generate(writer, ld);
55
56         VarDescriptor rd = VarDescriptor.makeNew();
57         right.generate(writer, rd);
58
59         writer.outputline("int " + dest.getSafeSymbol() + " = " + 
60                           relation.getSafeSymbol() + "_hash->contains(" + 
61                           ld.getSafeSymbol() + ", " +
62                           rd.getSafeSymbol() + ");");
63     }
64
65     public void prettyPrint(PrettyPrinter pp) {
66         pp.output("<");
67         left.prettyPrint(pp);
68         pp.output(", ");
69         right.prettyPrint(pp);
70         pp.output("> in? " + relation.getSafeSymbol());
71     }
72
73     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
74         TypeDescriptor ld = left.typecheck(sa);
75         TypeDescriptor rd = right.typecheck(sa);
76         
77         if (ld == null || rd == null) {
78             return null;
79         }
80
81         boolean ok = true;
82
83         if (ld != relation.getDomain().getType()) {
84             sa.getErrorReporter().report(null, "Type of left element '" + ld.getSymbol() + "' must match domain type '" + relation.getDomain().getType().getSymbol() + "'");
85             ok = false;
86         }
87
88         if (rd != relation.getRange().getType()) {
89             sa.getErrorReporter().report(null, "Type of right element '" + rd.getSymbol() + "' must match range type '" + relation.getRange().getType().getSymbol() + "'");
90             ok = false;
91         }
92
93         if (!ok) {
94             return null;
95         }
96
97         this.td = ReservedTypeDescriptor.INT;
98         return this.td;
99     }
100
101
102 }