IR
[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 Set getRequiredDescriptors() {
22         Set v = left.getRequiredDescriptors();
23         v.addAll(right.getRequiredDescriptors());
24         v.add(relation);
25         return v;
26     }
27
28     public void generate(CodeWriter writer, VarDescriptor dest) {
29         VarDescriptor ld = VarDescriptor.makeNew();
30         left.generate(writer, ld);
31
32         VarDescriptor rd = VarDescriptor.makeNew();
33         right.generate(writer, rd);
34
35         writer.outputline("int " + dest.getSafeSymbol() + " = " + 
36                           relation.getSafeSymbol() + "_hash->get(" + ld.getSafeSymbol() + 
37                           ") == " + rd.getSafeSymbol() + ";");
38     }
39
40     public void prettyPrint(PrettyPrinter pp) {
41         pp.output("<");
42         left.prettyPrint(pp);
43         pp.output(", ");
44         right.prettyPrint(pp);
45         pp.output("> in? " + relation.getSafeSymbol());
46     }
47
48     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
49         TypeDescriptor ld = left.typecheck(sa);
50         TypeDescriptor rd = right.typecheck(sa);
51         
52         if (ld == null || rd == null) {
53             return null;
54         }
55
56         boolean ok = true;
57
58         if (ld != relation.getDomain().getType()) {
59             sa.getErrorReporter().report(null, "Type of left element '" + ld.getSymbol() + "' must match domain type '" + relation.getDomain().getType().getSymbol() + "'");
60             ok = false;
61         }
62
63         if (rd != relation.getRange().getType()) {
64             sa.getErrorReporter().report(null, "Type of right element '" + rd.getSymbol() + "' must match range type '" + relation.getRange().getType().getSymbol() + "'");
65             ok = false;
66         }
67
68         if (!ok) {
69             return null;
70         }
71
72         this.td = ReservedTypeDescriptor.INT;
73         return this.td;
74     }
75
76
77 }