heh
[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 Set getRequiredDescriptors() {
22         // because we don't actually use rd for any generation, we return the empty set
23         return new HashSet();
24     }
25
26     public TypeDescriptor getType() {
27         throw new IRException("unsupported");
28     }
29
30     public void generate(CodeWriter cr, VarDescriptor dest) {
31
32         // basically a sizeoffunction can have two values ... zero or one... so what we need to do
33         // is expand the guard of the rule and if its true then its 1 otherwise 0
34
35         String destname = dest.getSafeSymbol();
36         cr.outputline("int " + destname + ";");
37
38         // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we 
39         // set the destination in the guard ... otherwise maybe!
40         
41         VarDescriptor domain = vd;
42
43         cr.pushSymbolTable(rule.getSymbolTable());
44         cr.startblock(); {
45
46             // ok... symbol table is set up... lets bind that initial vardescriptor of the quantifier
47             SetQuantifier sq = ((SetQuantifier) rule.quantifiers().next());
48             VarDescriptor rulebinding = sq.getVar();
49             String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol();
50             
51             // this is to be safe about name overlap because int t = t; sets t to 0!
52             cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";");
53             cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";");
54             
55             /* pretty print! */
56             cr.outputline("// about to inbed relational function");
57             cr.output("// ");
58             rule.getGuardExpr().prettyPrint(cr);
59             cr.outputline("");
60             
61             /* now we have to generate the guard test */
62             VarDescriptor guardval = VarDescriptor.makeNew();
63             rule.getGuardExpr().generate(cr, guardval);
64             
65             cr.outputline("if (" + guardval.getSafeSymbol() + ")");
66             cr.startblock(); {
67                 
68                 cr.outputline(destname + " = 1;");
69                 
70             } cr.endblock();
71             cr.outputline("else");
72             cr.startblock(); {
73
74                 cr.outputline(destname + " = 0;");
75
76             } cr.endblock();
77
78         } cr.endblock();
79
80
81
82     }
83
84     public void prettyPrint(PrettyPrinter pp) {
85         pp.output("sizeoffunction(");
86         pp.output(vd.toString() + "." + rd.toString());
87         pp.output(")");
88     }
89
90     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
91         throw new IRException();
92     }
93         
94 }