Added support to printout data structure update nodes (bindings/updates)
[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 Set freeVars() {
12         Set lset=left.freeVars();
13         Set rset=right.freeVars();
14         if (lset==null)
15             return rset;
16         if (rset!=null)
17             lset.addAll(rset);
18         return lset;
19     }
20
21     public TupleOfExpr(Expr left, Expr right, RelationDescriptor relation) {
22         if ((left == null) || (right == null) || (relation == null)) {
23             throw new NullPointerException();
24         }
25         
26         this.left = left;
27         this.right = right;
28         this.relation = relation;
29     }
30
31     public String name() {
32         return "<"+left.name()+","+right.name()+"> in "+relation.toString();
33     }
34
35     public boolean usesDescriptor(Descriptor d) {
36         if (d==relation)
37             return true;
38         else
39             return left.usesDescriptor(d)||right.usesDescriptor(d);
40     }
41
42     public boolean equals(Map remap, Expr e) {
43         if (e==null||!(e instanceof TupleOfExpr))
44             return false;
45         TupleOfExpr toe=(TupleOfExpr)e;
46         if (!left.equals(remap,toe.left))
47             return false;
48         if (!right.equals(remap,toe.right))
49             return false;
50         if (relation!=toe.relation)
51             return false;
52         return true;
53     }
54
55     public Set getRequiredDescriptors() {
56         Set v = left.getRequiredDescriptors();
57         v.addAll(right.getRequiredDescriptors());
58         v.add(relation);
59         return v;
60     }
61
62     public void generate(CodeWriter writer, VarDescriptor dest) {
63         VarDescriptor ld = VarDescriptor.makeNew();
64         left.generate(writer, ld);
65
66         VarDescriptor rd = VarDescriptor.makeNew();
67         right.generate(writer, rd);
68
69         writer.outputline("int " + dest.getSafeSymbol() + " = " + 
70                           relation.getSafeSymbol() + "_hash->contains(" + 
71                           ld.getSafeSymbol() + ", " +
72                           rd.getSafeSymbol() + ");");
73     }
74
75     public void prettyPrint(PrettyPrinter pp) {
76         pp.output("<");
77         left.prettyPrint(pp);
78         pp.output(", ");
79         right.prettyPrint(pp);
80         pp.output("> in? " + relation.getSafeSymbol());
81     }
82
83     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
84         TypeDescriptor ld = left.typecheck(sa);
85         TypeDescriptor rd = right.typecheck(sa);
86         
87         if (ld == null || rd == null) {
88             return null;
89         }
90
91         boolean ok = true;
92
93         if (ld != relation.getDomain().getType()) {
94             sa.getErrorReporter().report(null, "Type of left element '" + ld.getSymbol() + "' must match domain type '" + relation.getDomain().getType().getSymbol() + "'");
95             ok = false;
96         }
97
98         if (rd != relation.getRange().getType()) {
99             sa.getErrorReporter().report(null, "Type of right element '" + rd.getSymbol() + "' must match range type '" + relation.getRange().getType().getSymbol() + "'");
100             ok = false;
101         }
102
103         if (!ok) {
104             return null;
105         }
106
107         this.td = ReservedTypeDescriptor.INT;
108         return this.td;
109     }
110
111
112 }