Checking in code to perform safety checks on repair dependency graph.
[repair.git] / Repair / RepairCompiler / MCC / IR / SizeofFunction.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class SizeofFunction extends Expr {
6
7     public VarDescriptor vd;
8     public RelationDescriptor rd;
9     public Rule rule;
10
11     public SizeofFunction(VarDescriptor vd, RelationDescriptor rd, Rule rule) {
12         this.vd = vd;
13         this.rd = rd;
14         this.rule = rule;
15     }
16
17     public Set getInversedRelations() {
18         return new HashSet();
19     }
20
21     public boolean equals(Map remap,Expr e) {
22         throw new Error("UNIMPLEMENTED");
23     }
24
25     public Set getRequiredDescriptors() {
26         // because we don't actually use rd for any generation, we return the empty set
27         return new HashSet();
28     }
29
30     public TypeDescriptor getType() {
31         throw new IRException("unsupported");
32     }
33
34     public void generate(CodeWriter cr, VarDescriptor dest) {
35
36         // basically a sizeoffunction can have two values ... zero or one... so what we need to do
37         // is expand the guard of the rule and if its true then its 1 otherwise 0
38
39         String destname = dest.getSafeSymbol();
40         cr.outputline("int " + destname + ";");
41
42         // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we 
43         // set the destination in the guard ... otherwise maybe!
44         
45         VarDescriptor domain = vd;
46
47         cr.pushSymbolTable(rule.getSymbolTable());
48         cr.startblock(); {
49
50             // ok... symbol table is set up... lets bind that initial vardescriptor of the quantifier
51             SetQuantifier sq = ((SetQuantifier) rule.quantifiers().next());
52             VarDescriptor rulebinding = sq.getVar();
53             String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol();
54             
55             // this is to be safe about name overlap because int t = t; sets t to 0!
56             cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";");
57             cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";");
58             
59             /* pretty print! */
60             cr.outputline("// about to inbed relational function");
61             cr.output("// ");
62             rule.getGuardExpr().prettyPrint(cr);
63             cr.outputline("");
64             
65             /* now we have to generate the guard test */
66             VarDescriptor guardval = VarDescriptor.makeNew();
67             rule.getGuardExpr().generate(cr, guardval);
68             
69             cr.outputline("if (" + guardval.getSafeSymbol() + ")");
70             cr.startblock(); {
71                 
72                 cr.outputline(destname + " = 1;");
73                 
74             } cr.endblock();
75             cr.outputline("else");
76             cr.startblock(); {
77
78                 cr.outputline(destname + " = 0;");
79
80             } cr.endblock();
81
82         } cr.endblock();
83
84
85
86     }
87
88     public void prettyPrint(PrettyPrinter pp) {
89         pp.output("sizeoffunction(");
90         pp.output(vd.toString() + "." + rd.toString());
91         pp.output(")");
92     }
93
94     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
95         throw new IRException();
96     }
97         
98 }