*** empty log message ***
[repair.git] / Repair / RepairCompiler / MCC / IR / RelationFunctionExpr.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class RelationFunctionExpr extends Expr {
6
7     // #WHAT I WAS DOING: about to define relationfunctionexpr thich should take a expr, relation and rule and generated
8     // the functional value or "maybe" if not there!
9
10     Expr expr;
11     RelationDescriptor relation;
12     Rule rule;
13
14     public RelationFunctionExpr(Expr expr, RelationDescriptor relation, Rule rule) {
15         this.expr = expr;
16         this.relation = relation;
17         this.rule = rule;
18     }
19
20     public boolean equals(Map remap, Expr e) {
21         throw new Error("UNIMPLEMENTED!!!");
22     }
23
24     public Descriptor getDescriptor() {
25         return relation;
26     }
27
28     public RelationDescriptor getRelation() {
29         return relation;
30     }
31
32     public Set getInversedRelations() {
33         return expr.getInversedRelations();
34     }
35
36     public Set getRequiredDescriptors() {
37         Set v = expr.getRequiredDescriptors();        
38         v.add(relation);
39         return v;
40     }
41
42     public boolean usesDescriptor(Descriptor rd) {
43         if (rd==relation)
44             return true;
45         else
46             return expr.usesDescriptor(rd);
47     }
48
49     public void generate(CodeWriter cr, VarDescriptor dest) {
50         
51         String destname = dest.getSafeSymbol();
52         cr.outputline("int " + destname + ";");
53
54         // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we 
55         // set the destination in the guard ... otherwise maybe!
56         
57         VarDescriptor domain = VarDescriptor.makeNew("domain");
58         expr.generate(cr, domain);
59
60         cr.pushSymbolTable(rule.getSymbolTable());
61         cr.startblock(); {
62
63             // ok... symbol table is set up... lets bind that initial vardescriptor of the quantifier
64             SetQuantifier sq = ((SetQuantifier) rule.quantifiers().next());
65             VarDescriptor rulebinding = sq.getVar();
66             String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol();
67             
68             // this is to be safe about name overlap because int t = t; sets t to 0!
69             cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";");
70             cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";");
71             
72             /* pretty print! */
73             cr.outputline("// about to inbed relational function");
74             cr.output("// ");
75             rule.getGuardExpr().prettyPrint(cr);
76             cr.outputline("");
77             
78             /* now we have to generate the guard test */
79             VarDescriptor guardval = VarDescriptor.makeNew();
80             rule.getGuardExpr().generate(cr, guardval);
81             
82             cr.outputline("if (" + guardval.getSafeSymbol() + ")");
83             cr.startblock(); {
84                 
85                 /* now we have to generate the inclusion code */
86                 RelationInclusion ri = (RelationInclusion) rule.getInclusion();
87                 
88                 // basically, destname = righthandside<r, r.field>            
89                 VarDescriptor tempdest = VarDescriptor.makeNew("tempdest");
90                 Expr rhs = ri.getRightExpr();
91                 rhs.generate(cr, tempdest);
92                 
93                 cr.outputline(destname + " = " + tempdest.getSafeSymbol() + ";");
94                 
95             } cr.endblock();
96             cr.outputline("else");
97             cr.startblock(); {
98
99                 // three valued logic. if the relation (which is a partial function)
100                 // fails its guard, then we have a "maybe" condition, which must
101                 // propagate
102
103                 cr.outputline("maybe = 1;");
104
105             } cr.endblock();
106
107         } cr.endblock();
108
109     }
110
111     public void prettyPrint(PrettyPrinter pp) {
112         expr.prettyPrint(pp);
113         pp.output(".");
114         pp.output(relation.getSafeSymbol());
115     }
116
117     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
118         throw new IRException();
119     }
120
121 }